Skip to content

Commit 37c94a0

Browse files
committed
STM I2C: manage Is Device Ready case
Some device drivers use a data lenght of 0 to check if device is ready. STM32 HAL provides a dedicated service for that, so let's use it.
1 parent c0ca0a7 commit 37c94a0

File tree

1 file changed

+16
-2
lines changed

1 file changed

+16
-2
lines changed

targets/TARGET_STM/i2c_api.c

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -634,9 +634,16 @@ void i2c_reset(i2c_t *obj) {
634634
int i2c_read(i2c_t *obj, int address, char *data, int length, int stop) {
635635
struct i2c_s *obj_s = I2C_S(obj);
636636
I2C_HandleTypeDef *handle = &(obj_s->handle);
637-
int count = 0, ret = 0;
637+
int count = I2C_ERROR_BUS_BUSY, ret = 0;
638638
uint32_t timeout = 0;
639639

640+
if((length == 0) || (data == 0)) {
641+
if(HAL_I2C_IsDeviceReady(handle, address, 1, 10) == HAL_OK)
642+
return 0;
643+
else
644+
return I2C_ERROR_BUS_BUSY;
645+
}
646+
640647
if ((obj_s->XferOperation == I2C_FIRST_AND_LAST_FRAME) ||
641648
(obj_s->XferOperation == I2C_LAST_FRAME)) {
642649
if (stop)
@@ -686,9 +693,16 @@ int i2c_read(i2c_t *obj, int address, char *data, int length, int stop) {
686693
int i2c_write(i2c_t *obj, int address, const char *data, int length, int stop) {
687694
struct i2c_s *obj_s = I2C_S(obj);
688695
I2C_HandleTypeDef *handle = &(obj_s->handle);
689-
int count = 0, ret = 0;
696+
int count = I2C_ERROR_BUS_BUSY, ret = 0;
690697
uint32_t timeout = 0;
691698

699+
if((length == 0) || (data == 0)) {
700+
if(HAL_I2C_IsDeviceReady(handle, address, 1, 10) == HAL_OK)
701+
return 0;
702+
else
703+
return I2C_ERROR_BUS_BUSY;
704+
}
705+
692706
if ((obj_s->XferOperation == I2C_FIRST_AND_LAST_FRAME) ||
693707
(obj_s->XferOperation == I2C_LAST_FRAME)) {
694708
if (stop)

0 commit comments

Comments
 (0)