[NUCLEO_F334R8] enhance I2C for EEPROM

pull/383/head
dbestm 2014-07-01 14:36:32 +02:00
parent 7b4bcd6eaf
commit 7b6a59a068
1 changed files with 5 additions and 13 deletions

View File

@ -199,8 +199,6 @@ int i2c_read(i2c_t *obj, int address, char *data, int length, int stop)
int count;
int value;
if (length == 0) return 0;
/* update CR2 register */
i2c->CR2 = (i2c->CR2 & (uint32_t)~((uint32_t)(I2C_CR2_SADD | I2C_CR2_NBYTES | I2C_CR2_RELOAD | I2C_CR2_AUTOEND | I2C_CR2_RD_WRN | I2C_CR2_START | I2C_CR2_STOP)))
| (uint32_t)(((uint32_t)address & I2C_CR2_SADD) | (((uint32_t)length << 16) & I2C_CR2_NBYTES) | (uint32_t)I2C_SOFTEND_MODE | (uint32_t)I2C_GENERATE_START_READ);
@ -216,7 +214,7 @@ int i2c_read(i2c_t *obj, int address, char *data, int length, int stop)
while (__HAL_I2C_GET_FLAG(&I2cHandle, I2C_FLAG_TC) == RESET) {
timeout--;
if (timeout == 0) {
return 0;
return -1;
}
}
@ -230,7 +228,7 @@ int i2c_read(i2c_t *obj, int address, char *data, int length, int stop)
while (__HAL_I2C_GET_FLAG(&I2cHandle, I2C_FLAG_STOPF) == RESET) {
timeout--;
if (timeout == 0) {
return 0;
return -1;
}
}
/* Clear STOP Flag */
@ -247,8 +245,6 @@ int i2c_write(i2c_t *obj, int address, const char *data, int length, int stop)
int timeout;
int count;
if (length == 0) return 0;
/* update CR2 register */
i2c->CR2 = (i2c->CR2 & (uint32_t)~((uint32_t)(I2C_CR2_SADD | I2C_CR2_NBYTES | I2C_CR2_RELOAD | I2C_CR2_AUTOEND | I2C_CR2_RD_WRN | I2C_CR2_START | I2C_CR2_STOP)))
| (uint32_t)(((uint32_t)address & I2C_CR2_SADD) | (((uint32_t)length << 16) & I2C_CR2_NBYTES) | (uint32_t)I2C_SOFTEND_MODE | (uint32_t)I2C_GENERATE_START_WRITE);
@ -262,7 +258,7 @@ int i2c_write(i2c_t *obj, int address, const char *data, int length, int stop)
while (__HAL_I2C_GET_FLAG(&I2cHandle, I2C_FLAG_TC) == RESET) {
timeout--;
if (timeout == 0) {
return 0;
return -1;
}
}
@ -276,7 +272,7 @@ int i2c_write(i2c_t *obj, int address, const char *data, int length, int stop)
while (__HAL_I2C_GET_FLAG(&I2cHandle, I2C_FLAG_STOPF) == RESET) {
timeout--;
if (timeout == 0) {
return 0;
return -1;
}
}
/* Clear STOP Flag */
@ -295,7 +291,7 @@ int i2c_byte_read(i2c_t *obj, int last)
timeout = FLAG_TIMEOUT;
while (__HAL_I2C_GET_FLAG(&I2cHandle, I2C_FLAG_RXNE) == RESET) {
if ((timeout--) == 0) {
return 0;
return -1;
}
}
@ -396,8 +392,6 @@ int i2c_slave_read(i2c_t *obj, char *data, int length)
{
char size = 0;
if (length == 0) return 0;
while (size < length) data[size++] = (char)i2c_byte_read(obj, 0);
return size;
@ -408,8 +402,6 @@ int i2c_slave_write(i2c_t *obj, const char *data, int length)
char size = 0;
I2cHandle.Instance = (I2C_TypeDef *)(obj->i2c);
if (length == 0) return 0;
do {
i2c_byte_write(obj, data[size]);
size++;