mirror of https://github.com/ARMmbed/mbed-os.git
Merge pull request #383 from dbestm/master
[NUCLEOs] enhance I2C API to make it work with EEPROMpull/361/merge
commit
4f86d39719
|
@ -178,8 +178,6 @@ int i2c_read(i2c_t *obj, int address, char *data, int length, int stop) {
|
|||
int timeout;
|
||||
int value;
|
||||
|
||||
if (length == 0) return 0;
|
||||
|
||||
// Configure slave address, nbytes, reload, end mode and start or stop generation
|
||||
I2C_TransferHandling(i2c, address, length, I2C_SoftEnd_Mode, I2C_Generate_Start_Read);
|
||||
|
||||
|
@ -192,7 +190,7 @@ int i2c_read(i2c_t *obj, int address, char *data, int length, int stop) {
|
|||
timeout = FLAG_TIMEOUT;
|
||||
while (!I2C_GetFlagStatus(i2c, I2C_FLAG_TC)) {
|
||||
timeout--;
|
||||
if (timeout == 0) return 0;
|
||||
if (timeout == 0) return -1;
|
||||
}
|
||||
|
||||
if (stop) i2c_stop(obj);
|
||||
|
@ -205,8 +203,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;
|
||||
|
||||
// Configure slave address, nbytes, reload, end mode and start generation
|
||||
I2C_TransferHandling(i2c, address, length, I2C_SoftEnd_Mode, I2C_Generate_Start_Write);
|
||||
|
||||
|
@ -218,7 +214,7 @@ int i2c_write(i2c_t *obj, int address, const char *data, int length, int stop) {
|
|||
timeout = FLAG_TIMEOUT;
|
||||
while (!I2C_GetFlagStatus(i2c, I2C_FLAG_TC)) {
|
||||
timeout--;
|
||||
if (timeout == 0) return 0;
|
||||
if (timeout == 0) return -1;
|
||||
}
|
||||
|
||||
if (stop) i2c_stop(obj);
|
||||
|
@ -236,7 +232,7 @@ int i2c_byte_read(i2c_t *obj, int last) {
|
|||
while (I2C_GetFlagStatus(i2c, I2C_ISR_RXNE) == RESET) {
|
||||
timeout--;
|
||||
if (timeout == 0) {
|
||||
return 0;
|
||||
return -1;
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -159,8 +159,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);
|
||||
|
@ -176,7 +174,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;
|
||||
}
|
||||
}
|
||||
__HAL_I2C_CLEAR_FLAG(&I2cHandle, I2C_FLAG_TC);
|
||||
|
@ -189,7 +187,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 */
|
||||
|
@ -205,8 +203,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);
|
||||
|
@ -222,7 +218,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;
|
||||
}
|
||||
}
|
||||
__HAL_I2C_CLEAR_FLAG(&I2cHandle, I2C_FLAG_TC);
|
||||
|
@ -235,7 +231,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 */
|
||||
|
@ -253,7 +249,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;
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -355,8 +351,6 @@ int i2c_slave_receive(i2c_t *obj) {
|
|||
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;
|
||||
|
@ -366,8 +360,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++;
|
||||
|
|
|
@ -139,8 +139,6 @@ int i2c_read(i2c_t *obj, int address, char *data, int length, int stop) {
|
|||
int count;
|
||||
int value;
|
||||
|
||||
if (length == 0) return 0;
|
||||
|
||||
i2c_start(obj);
|
||||
|
||||
// Send slave address for read
|
||||
|
@ -151,7 +149,7 @@ int i2c_read(i2c_t *obj, int address, char *data, int length, int stop) {
|
|||
while (I2C_CheckEvent(i2c, I2C_EVENT_MASTER_RECEIVER_MODE_SELECTED) == ERROR) {
|
||||
timeout--;
|
||||
if (timeout == 0) {
|
||||
return 0;
|
||||
return -1;
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -189,7 +187,7 @@ int i2c_write(i2c_t *obj, int address, const char *data, int length, int stop) {
|
|||
while (I2C_CheckEvent(i2c, I2C_EVENT_MASTER_TRANSMITTER_MODE_SELECTED) == ERROR) {
|
||||
timeout--;
|
||||
if (timeout == 0) {
|
||||
return 0;
|
||||
return -1;
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -226,7 +224,7 @@ int i2c_byte_read(i2c_t *obj, int last) {
|
|||
while (I2C_GetFlagStatus(i2c, I2C_FLAG_RXNE) == RESET) {
|
||||
timeout--;
|
||||
if (timeout == 0) {
|
||||
return 0;
|
||||
return -1;
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -219,8 +219,6 @@ int i2c_read(i2c_t *obj, int address, char *data, int length, int stop) {
|
|||
int timeout;
|
||||
int value;
|
||||
|
||||
if (length == 0) return 0;
|
||||
|
||||
// Configure slave address, nbytes, reload, end mode and start or stop generation
|
||||
I2C_TransferHandling(i2c, address, length, I2C_SoftEnd_Mode, I2C_Generate_Start_Read);
|
||||
|
||||
|
@ -233,7 +231,7 @@ int i2c_read(i2c_t *obj, int address, char *data, int length, int stop) {
|
|||
timeout = FLAG_TIMEOUT;
|
||||
while (!I2C_GetFlagStatus(i2c, I2C_FLAG_TC)) {
|
||||
timeout--;
|
||||
if (timeout == 0) return 0;
|
||||
if (timeout == 0) return -1;
|
||||
}
|
||||
|
||||
if (stop) i2c_stop(obj);
|
||||
|
@ -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;
|
||||
|
||||
// Configure slave address, nbytes, reload, end mode and start generation
|
||||
I2C_TransferHandling(i2c, address, length, I2C_SoftEnd_Mode, I2C_Generate_Start_Write);
|
||||
|
||||
|
@ -260,7 +256,7 @@ int i2c_write(i2c_t *obj, int address, const char *data, int length, int stop) {
|
|||
timeout = FLAG_TIMEOUT;
|
||||
while (!I2C_GetFlagStatus(i2c, I2C_FLAG_TC)) {
|
||||
timeout--;
|
||||
if (timeout == 0) return 0;
|
||||
if (timeout == 0) return -1;
|
||||
}
|
||||
|
||||
if (stop) i2c_stop(obj);
|
||||
|
@ -278,7 +274,7 @@ int i2c_byte_read(i2c_t *obj, int last) {
|
|||
while (I2C_GetFlagStatus(i2c, I2C_ISR_RXNE) == RESET) {
|
||||
timeout--;
|
||||
if (timeout == 0) {
|
||||
return 0;
|
||||
return -1;
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -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++;
|
||||
|
|
|
@ -155,8 +155,6 @@ int i2c_read(i2c_t *obj, int address, char *data, int length, int stop) {
|
|||
int count;
|
||||
int value;
|
||||
|
||||
if (length == 0) return 0;
|
||||
|
||||
i2c_start(obj);
|
||||
|
||||
// Wait until SB flag is set
|
||||
|
@ -164,7 +162,7 @@ int i2c_read(i2c_t *obj, int address, char *data, int length, int stop) {
|
|||
while (__HAL_I2C_GET_FLAG(&I2cHandle, I2C_FLAG_SB) == RESET) {
|
||||
timeout--;
|
||||
if (timeout == 0) {
|
||||
return 0;
|
||||
return -1;
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -176,7 +174,7 @@ int i2c_read(i2c_t *obj, int address, char *data, int length, int stop) {
|
|||
while (__HAL_I2C_GET_FLAG(&I2cHandle, I2C_FLAG_ADDR) == RESET) {
|
||||
timeout--;
|
||||
if (timeout == 0) {
|
||||
return 0;
|
||||
return -1;
|
||||
}
|
||||
}
|
||||
__HAL_I2C_CLEAR_ADDRFLAG(&I2cHandle);
|
||||
|
@ -206,7 +204,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;
|
||||
i2c_start(obj);
|
||||
|
||||
// Wait until SB flag is set
|
||||
|
@ -214,7 +211,7 @@ int i2c_write(i2c_t *obj, int address, const char *data, int length, int stop) {
|
|||
while (__HAL_I2C_GET_FLAG(&I2cHandle, I2C_FLAG_SB) == RESET) {
|
||||
timeout--;
|
||||
if (timeout == 0) {
|
||||
return 0;
|
||||
return -1;
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -226,7 +223,7 @@ int i2c_write(i2c_t *obj, int address, const char *data, int length, int stop) {
|
|||
while (__HAL_I2C_GET_FLAG(&I2cHandle, I2C_FLAG_ADDR) == RESET) {
|
||||
timeout--;
|
||||
if (timeout == 0) {
|
||||
return 0;
|
||||
return -1;
|
||||
}
|
||||
}
|
||||
__HAL_I2C_CLEAR_ADDRFLAG(&I2cHandle);
|
||||
|
@ -234,7 +231,7 @@ int i2c_write(i2c_t *obj, int address, const char *data, int length, int stop) {
|
|||
for (count = 0; count < length; count++) {
|
||||
if (i2c_byte_write(obj, data[count]) != 1) {
|
||||
i2c_stop(obj);
|
||||
return 0;
|
||||
return -1;
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -262,7 +259,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;
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -353,7 +350,6 @@ int i2c_slave_receive(i2c_t *obj) {
|
|||
int i2c_slave_read(i2c_t *obj, char *data, int length) {
|
||||
uint32_t Timeout;
|
||||
int size = 0;
|
||||
if (length == 0) return 0;
|
||||
|
||||
I2cHandle.Instance = (I2C_TypeDef *)(obj->i2c);
|
||||
|
||||
|
@ -364,7 +360,7 @@ int i2c_slave_read(i2c_t *obj, char *data, int length) {
|
|||
while (__HAL_I2C_GET_FLAG(&I2cHandle, I2C_FLAG_RXNE) == RESET) {
|
||||
Timeout--;
|
||||
if (Timeout == 0) {
|
||||
return 0;
|
||||
return -1;
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -386,7 +382,7 @@ int i2c_slave_read(i2c_t *obj, char *data, int length) {
|
|||
while (__HAL_I2C_GET_FLAG(&I2cHandle, I2C_FLAG_STOPF) == RESET) {
|
||||
Timeout--;
|
||||
if (Timeout == 0) {
|
||||
return 0;
|
||||
return -1;
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -398,7 +394,7 @@ int i2c_slave_read(i2c_t *obj, char *data, int length) {
|
|||
while (__HAL_I2C_GET_FLAG(&I2cHandle, I2C_FLAG_BUSY) == SET) {
|
||||
Timeout--;
|
||||
if (Timeout == 0) {
|
||||
return 0;
|
||||
return -1;
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -408,7 +404,6 @@ int i2c_slave_read(i2c_t *obj, char *data, int length) {
|
|||
int i2c_slave_write(i2c_t *obj, const char *data, int length) {
|
||||
uint32_t Timeout;
|
||||
int size = 0;
|
||||
if (length == 0) return 0;
|
||||
|
||||
I2cHandle.Instance = (I2C_TypeDef *)(obj->i2c);
|
||||
|
||||
|
@ -418,7 +413,7 @@ int i2c_slave_write(i2c_t *obj, const char *data, int length) {
|
|||
while (__HAL_I2C_GET_FLAG(&I2cHandle, I2C_FLAG_TXE) == RESET) {
|
||||
Timeout--;
|
||||
if (Timeout == 0) {
|
||||
return 0;
|
||||
return -1;
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -441,7 +436,7 @@ int i2c_slave_write(i2c_t *obj, const char *data, int length) {
|
|||
while (__HAL_I2C_GET_FLAG(&I2cHandle, I2C_FLAG_AF) == RESET) {
|
||||
Timeout--;
|
||||
if (Timeout == 0) {
|
||||
return 0;
|
||||
return -1;
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -455,7 +450,7 @@ int i2c_slave_write(i2c_t *obj, const char *data, int length) {
|
|||
while (__HAL_I2C_GET_FLAG(&I2cHandle, I2C_FLAG_BUSY) == SET) {
|
||||
Timeout--;
|
||||
if (Timeout == 0) {
|
||||
return 0;
|
||||
return -1;
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -162,8 +162,6 @@ int i2c_read(i2c_t *obj, int address, char *data, int length, int stop)
|
|||
int count;
|
||||
int value;
|
||||
|
||||
if (length == 0) return 0;
|
||||
|
||||
i2c_start(obj);
|
||||
|
||||
// Wait until SB flag is set
|
||||
|
@ -171,7 +169,7 @@ int i2c_read(i2c_t *obj, int address, char *data, int length, int stop)
|
|||
while (__HAL_I2C_GET_FLAG(&I2cHandle, I2C_FLAG_SB) == RESET) {
|
||||
timeout--;
|
||||
if (timeout == 0) {
|
||||
return 0;
|
||||
return -1;
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -183,7 +181,7 @@ int i2c_read(i2c_t *obj, int address, char *data, int length, int stop)
|
|||
while (__HAL_I2C_GET_FLAG(&I2cHandle, I2C_FLAG_ADDR) == RESET) {
|
||||
timeout--;
|
||||
if (timeout == 0) {
|
||||
return 0;
|
||||
return -1;
|
||||
}
|
||||
}
|
||||
__HAL_I2C_CLEAR_ADDRFLAG(&I2cHandle);
|
||||
|
@ -214,7 +212,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;
|
||||
i2c_start(obj);
|
||||
|
||||
// Wait until SB flag is set
|
||||
|
@ -222,7 +219,7 @@ int i2c_write(i2c_t *obj, int address, const char *data, int length, int stop)
|
|||
while (__HAL_I2C_GET_FLAG(&I2cHandle, I2C_FLAG_SB) == RESET) {
|
||||
timeout--;
|
||||
if (timeout == 0) {
|
||||
return 0;
|
||||
return -1;
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -234,7 +231,7 @@ int i2c_write(i2c_t *obj, int address, const char *data, int length, int stop)
|
|||
while (__HAL_I2C_GET_FLAG(&I2cHandle, I2C_FLAG_ADDR) == RESET) {
|
||||
timeout--;
|
||||
if (timeout == 0) {
|
||||
return 0;
|
||||
return -1;
|
||||
}
|
||||
}
|
||||
__HAL_I2C_CLEAR_ADDRFLAG(&I2cHandle);
|
||||
|
@ -242,7 +239,7 @@ int i2c_write(i2c_t *obj, int address, const char *data, int length, int stop)
|
|||
for (count = 0; count < length; count++) {
|
||||
if (i2c_byte_write(obj, data[count]) != 1) {
|
||||
i2c_stop(obj);
|
||||
return 0;
|
||||
return -1;
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -271,7 +268,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;
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -368,7 +365,6 @@ int i2c_slave_read(i2c_t *obj, char *data, int length)
|
|||
{
|
||||
uint32_t Timeout;
|
||||
int size = 0;
|
||||
if (length == 0) return 0;
|
||||
|
||||
I2cHandle.Instance = (I2C_TypeDef *)(obj->i2c);
|
||||
|
||||
|
@ -379,7 +375,7 @@ int i2c_slave_read(i2c_t *obj, char *data, int length)
|
|||
while (__HAL_I2C_GET_FLAG(&I2cHandle, I2C_FLAG_RXNE) == RESET) {
|
||||
Timeout--;
|
||||
if (Timeout == 0) {
|
||||
return 0;
|
||||
return -1;
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -401,7 +397,7 @@ int i2c_slave_read(i2c_t *obj, char *data, int length)
|
|||
while (__HAL_I2C_GET_FLAG(&I2cHandle, I2C_FLAG_STOPF) == RESET) {
|
||||
Timeout--;
|
||||
if (Timeout == 0) {
|
||||
return 0;
|
||||
return -1;
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -413,7 +409,7 @@ int i2c_slave_read(i2c_t *obj, char *data, int length)
|
|||
while (__HAL_I2C_GET_FLAG(&I2cHandle, I2C_FLAG_BUSY) == SET) {
|
||||
Timeout--;
|
||||
if (Timeout == 0) {
|
||||
return 0;
|
||||
return -1;
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -424,7 +420,6 @@ int i2c_slave_write(i2c_t *obj, const char *data, int length)
|
|||
{
|
||||
uint32_t Timeout;
|
||||
int size = 0;
|
||||
if (length == 0) return 0;
|
||||
|
||||
I2cHandle.Instance = (I2C_TypeDef *)(obj->i2c);
|
||||
|
||||
|
@ -434,7 +429,7 @@ int i2c_slave_write(i2c_t *obj, const char *data, int length)
|
|||
while (__HAL_I2C_GET_FLAG(&I2cHandle, I2C_FLAG_TXE) == RESET) {
|
||||
Timeout--;
|
||||
if (Timeout == 0) {
|
||||
return 0;
|
||||
return -1;
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -457,7 +452,7 @@ int i2c_slave_write(i2c_t *obj, const char *data, int length)
|
|||
while (__HAL_I2C_GET_FLAG(&I2cHandle, I2C_FLAG_AF) == RESET) {
|
||||
Timeout--;
|
||||
if (Timeout == 0) {
|
||||
return 0;
|
||||
return -1;
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -471,7 +466,7 @@ int i2c_slave_write(i2c_t *obj, const char *data, int length)
|
|||
while (__HAL_I2C_GET_FLAG(&I2cHandle, I2C_FLAG_BUSY) == SET) {
|
||||
Timeout--;
|
||||
if (Timeout == 0) {
|
||||
return 0;
|
||||
return -1;
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -159,8 +159,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);
|
||||
|
@ -176,7 +174,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;
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -190,7 +188,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 */
|
||||
|
@ -206,8 +204,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);
|
||||
|
@ -221,7 +217,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;
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -235,7 +231,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 */
|
||||
|
@ -253,7 +249,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;
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -354,8 +350,6 @@ int i2c_slave_receive(i2c_t *obj) {
|
|||
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;
|
||||
|
@ -365,8 +359,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++;
|
||||
|
|
|
@ -158,8 +158,6 @@ int i2c_read(i2c_t *obj, int address, char *data, int length, int stop) {
|
|||
int count;
|
||||
int value;
|
||||
|
||||
if (length == 0) return 0;
|
||||
|
||||
i2c_start(obj);
|
||||
|
||||
// Send slave address for read
|
||||
|
@ -170,7 +168,7 @@ int i2c_read(i2c_t *obj, int address, char *data, int length, int stop) {
|
|||
while (I2C_CheckEvent(i2c, I2C_EVENT_MASTER_RECEIVER_MODE_SELECTED) == ERROR) {
|
||||
timeout--;
|
||||
if (timeout == 0) {
|
||||
return 0;
|
||||
return -1;
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -208,14 +206,14 @@ int i2c_write(i2c_t *obj, int address, const char *data, int length, int stop) {
|
|||
while (I2C_CheckEvent(i2c, I2C_EVENT_MASTER_TRANSMITTER_MODE_SELECTED) == ERROR) {
|
||||
timeout--;
|
||||
if (timeout == 0) {
|
||||
return 0;
|
||||
return -1;
|
||||
}
|
||||
}
|
||||
|
||||
for (count = 0; count < length; count++) {
|
||||
if (i2c_byte_write(obj, data[count]) != 1) {
|
||||
i2c_stop(obj);
|
||||
return 0;
|
||||
return -1;
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -245,7 +243,7 @@ int i2c_byte_read(i2c_t *obj, int last) {
|
|||
while (I2C_GetFlagStatus(i2c, I2C_FLAG_RXNE) == RESET) {
|
||||
timeout--;
|
||||
if (timeout == 0) {
|
||||
return 0;
|
||||
return -1;
|
||||
}
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in New Issue