Skip to content

Commit c0ca0a7

Browse files
committed
STM I2C - move i2c_read in SYNC part
just change the place of code to have i2c_read and i2c_write together
1 parent 315d893 commit c0ca0a7

File tree

1 file changed

+52
-54
lines changed

1 file changed

+52
-54
lines changed

targets/TARGET_STM/i2c_api.c

Lines changed: 52 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -442,60 +442,6 @@ i2c_t *get_i2c_obj(I2C_HandleTypeDef *hi2c){
442442
return (obj);
443443
}
444444

445-
/* SYNCHRONOUS API FUNCTIONS */
446-
447-
int i2c_read(i2c_t *obj, int address, char *data, int length, int stop) {
448-
struct i2c_s *obj_s = I2C_S(obj);
449-
I2C_HandleTypeDef *handle = &(obj_s->handle);
450-
int count = 0, ret = 0;
451-
uint32_t timeout = 0;
452-
453-
if ((obj_s->XferOperation == I2C_FIRST_AND_LAST_FRAME) ||
454-
(obj_s->XferOperation == I2C_LAST_FRAME)) {
455-
if (stop)
456-
obj_s->XferOperation = I2C_FIRST_AND_LAST_FRAME;
457-
else
458-
obj_s->XferOperation = I2C_FIRST_FRAME;
459-
} else if ((obj_s->XferOperation == I2C_FIRST_FRAME) ||
460-
(obj_s->XferOperation == I2C_NEXT_FRAME)) {
461-
if (stop)
462-
obj_s->XferOperation = I2C_LAST_FRAME;
463-
else
464-
obj_s->XferOperation = I2C_NEXT_FRAME;
465-
}
466-
467-
obj_s->event = 0;
468-
469-
/* Activate default IRQ handlers for sync mode
470-
* which would be overwritten in async mode
471-
*/
472-
i2c_ev_err_enable(obj, i2c_get_irq_handler(obj));
473-
474-
ret = HAL_I2C_Master_Sequential_Receive_IT(handle, address, (uint8_t *) data, length, obj_s->XferOperation);
475-
476-
if(ret == HAL_OK) {
477-
timeout = BYTE_TIMEOUT_US * length;
478-
/* transfer started : wait completion or timeout */
479-
while(!(obj_s->event & I2C_EVENT_ALL) && (--timeout != 0)) {
480-
wait_us(1);
481-
}
482-
483-
i2c_ev_err_disable(obj);
484-
485-
if((timeout == 0) || (obj_s->event != I2C_EVENT_TRANSFER_COMPLETE)) {
486-
DEBUG_PRINTF(" TIMEOUT or error in i2c_read\r\n");
487-
/* re-init IP to try and get back in a working state */
488-
i2c_init(obj, obj_s->sda, obj_s->scl);
489-
} else {
490-
count = length;
491-
}
492-
} else {
493-
DEBUG_PRINTF("ERROR in i2c_read\r\n");
494-
}
495-
496-
return count;
497-
}
498-
499445
/*
500446
* UNITARY APIS.
501447
* For very basic operations, direct registers access is needed
@@ -685,6 +631,58 @@ void i2c_reset(i2c_t *obj) {
685631
/*
686632
* SYNC APIS
687633
*/
634+
int i2c_read(i2c_t *obj, int address, char *data, int length, int stop) {
635+
struct i2c_s *obj_s = I2C_S(obj);
636+
I2C_HandleTypeDef *handle = &(obj_s->handle);
637+
int count = 0, ret = 0;
638+
uint32_t timeout = 0;
639+
640+
if ((obj_s->XferOperation == I2C_FIRST_AND_LAST_FRAME) ||
641+
(obj_s->XferOperation == I2C_LAST_FRAME)) {
642+
if (stop)
643+
obj_s->XferOperation = I2C_FIRST_AND_LAST_FRAME;
644+
else
645+
obj_s->XferOperation = I2C_FIRST_FRAME;
646+
} else if ((obj_s->XferOperation == I2C_FIRST_FRAME) ||
647+
(obj_s->XferOperation == I2C_NEXT_FRAME)) {
648+
if (stop)
649+
obj_s->XferOperation = I2C_LAST_FRAME;
650+
else
651+
obj_s->XferOperation = I2C_NEXT_FRAME;
652+
}
653+
654+
obj_s->event = 0;
655+
656+
/* Activate default IRQ handlers for sync mode
657+
* which would be overwritten in async mode
658+
*/
659+
i2c_ev_err_enable(obj, i2c_get_irq_handler(obj));
660+
661+
ret = HAL_I2C_Master_Sequential_Receive_IT(handle, address, (uint8_t *) data, length, obj_s->XferOperation);
662+
663+
if(ret == HAL_OK) {
664+
timeout = BYTE_TIMEOUT_US * length;
665+
/* transfer started : wait completion or timeout */
666+
while(!(obj_s->event & I2C_EVENT_ALL) && (--timeout != 0)) {
667+
wait_us(1);
668+
}
669+
670+
i2c_ev_err_disable(obj);
671+
672+
if((timeout == 0) || (obj_s->event != I2C_EVENT_TRANSFER_COMPLETE)) {
673+
DEBUG_PRINTF(" TIMEOUT or error in i2c_read\r\n");
674+
/* re-init IP to try and get back in a working state */
675+
i2c_init(obj, obj_s->sda, obj_s->scl);
676+
} else {
677+
count = length;
678+
}
679+
} else {
680+
DEBUG_PRINTF("ERROR in i2c_read:%d\r\n", ret);
681+
}
682+
683+
return count;
684+
}
685+
688686
int i2c_write(i2c_t *obj, int address, const char *data, int length, int stop) {
689687
struct i2c_s *obj_s = I2C_S(obj);
690688
I2C_HandleTypeDef *handle = &(obj_s->handle);

0 commit comments

Comments
 (0)