Skip to content

Commit 1219f88

Browse files
authored
Merge pull request #3430 from LMESTM/fix_ci_shield_eeprom_test
Fix ci shield eeprom test
2 parents 315d893 + 580d964 commit 1219f88

File tree

1 file changed

+78
-58
lines changed

1 file changed

+78
-58
lines changed

targets/TARGET_STM/i2c_api.c

Lines changed: 78 additions & 58 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
@@ -537,10 +483,18 @@ int i2c_start(i2c_t *obj) {
537483
int i2c_stop(i2c_t *obj) {
538484
struct i2c_s *obj_s = I2C_S(obj);
539485
I2C_TypeDef *i2c = (I2C_TypeDef *)obj_s->i2c;
486+
I2C_HandleTypeDef *handle = &(obj_s->handle);
487+
int timeout;
540488

541489
// Generate the STOP condition
542490
i2c->CR1 |= I2C_CR1_STOP;
543491

492+
/* In case of mixed usage of the APIs (unitary + SYNC)
493+
* re-inti HAL state
494+
*/
495+
if(obj_s->XferOperation != I2C_FIRST_AND_LAST_FRAME)
496+
i2c_init(obj, obj_s->sda, obj_s->scl);
497+
544498
return 0;
545499
}
546500

@@ -685,12 +639,78 @@ void i2c_reset(i2c_t *obj) {
685639
/*
686640
* SYNC APIS
687641
*/
642+
int i2c_read(i2c_t *obj, int address, char *data, int length, int stop) {
643+
struct i2c_s *obj_s = I2C_S(obj);
644+
I2C_HandleTypeDef *handle = &(obj_s->handle);
645+
int count = I2C_ERROR_BUS_BUSY, ret = 0;
646+
uint32_t timeout = 0;
647+
648+
if((length == 0) || (data == 0)) {
649+
if(HAL_I2C_IsDeviceReady(handle, address, 1, 10) == HAL_OK)
650+
return 0;
651+
else
652+
return I2C_ERROR_BUS_BUSY;
653+
}
654+
655+
if ((obj_s->XferOperation == I2C_FIRST_AND_LAST_FRAME) ||
656+
(obj_s->XferOperation == I2C_LAST_FRAME)) {
657+
if (stop)
658+
obj_s->XferOperation = I2C_FIRST_AND_LAST_FRAME;
659+
else
660+
obj_s->XferOperation = I2C_FIRST_FRAME;
661+
} else if ((obj_s->XferOperation == I2C_FIRST_FRAME) ||
662+
(obj_s->XferOperation == I2C_NEXT_FRAME)) {
663+
if (stop)
664+
obj_s->XferOperation = I2C_LAST_FRAME;
665+
else
666+
obj_s->XferOperation = I2C_NEXT_FRAME;
667+
}
668+
669+
obj_s->event = 0;
670+
671+
/* Activate default IRQ handlers for sync mode
672+
* which would be overwritten in async mode
673+
*/
674+
i2c_ev_err_enable(obj, i2c_get_irq_handler(obj));
675+
676+
ret = HAL_I2C_Master_Sequential_Receive_IT(handle, address, (uint8_t *) data, length, obj_s->XferOperation);
677+
678+
if(ret == HAL_OK) {
679+
timeout = BYTE_TIMEOUT_US * (length + 1);
680+
/* transfer started : wait completion or timeout */
681+
while(!(obj_s->event & I2C_EVENT_ALL) && (--timeout != 0)) {
682+
wait_us(1);
683+
}
684+
685+
i2c_ev_err_disable(obj);
686+
687+
if((timeout == 0) || (obj_s->event != I2C_EVENT_TRANSFER_COMPLETE)) {
688+
DEBUG_PRINTF(" TIMEOUT or error in i2c_read\r\n");
689+
/* re-init IP to try and get back in a working state */
690+
i2c_init(obj, obj_s->sda, obj_s->scl);
691+
} else {
692+
count = length;
693+
}
694+
} else {
695+
DEBUG_PRINTF("ERROR in i2c_read:%d\r\n", ret);
696+
}
697+
698+
return count;
699+
}
700+
688701
int i2c_write(i2c_t *obj, int address, const char *data, int length, int stop) {
689702
struct i2c_s *obj_s = I2C_S(obj);
690703
I2C_HandleTypeDef *handle = &(obj_s->handle);
691-
int count = 0, ret = 0;
704+
int count = I2C_ERROR_BUS_BUSY, ret = 0;
692705
uint32_t timeout = 0;
693706

707+
if((length == 0) || (data == 0)) {
708+
if(HAL_I2C_IsDeviceReady(handle, address, 1, 10) == HAL_OK)
709+
return 0;
710+
else
711+
return I2C_ERROR_BUS_BUSY;
712+
}
713+
694714
if ((obj_s->XferOperation == I2C_FIRST_AND_LAST_FRAME) ||
695715
(obj_s->XferOperation == I2C_LAST_FRAME)) {
696716
if (stop)
@@ -712,7 +732,7 @@ int i2c_write(i2c_t *obj, int address, const char *data, int length, int stop) {
712732
ret = HAL_I2C_Master_Sequential_Transmit_IT(handle, address, (uint8_t *) data, length, obj_s->XferOperation);
713733

714734
if(ret == HAL_OK) {
715-
timeout = BYTE_TIMEOUT_US * length;
735+
timeout = BYTE_TIMEOUT_US * (length + 1);
716736
/* transfer started : wait completion or timeout */
717737
while(!(obj_s->event & I2C_EVENT_ALL) && (--timeout != 0)) {
718738
wait_us(1);
@@ -891,7 +911,7 @@ int i2c_slave_read(i2c_t *obj, char *data, int length) {
891911
ret = HAL_I2C_Slave_Sequential_Receive_IT(handle, (uint8_t *) data, length, I2C_NEXT_FRAME);
892912

893913
if(ret == HAL_OK) {
894-
timeout = BYTE_TIMEOUT_US * length;
914+
timeout = BYTE_TIMEOUT_US * (length + 1);
895915
while(obj_s->pending_slave_rx_maxter_tx && (--timeout != 0)) {
896916
wait_us(1);
897917
}
@@ -916,7 +936,7 @@ int i2c_slave_write(i2c_t *obj, const char *data, int length) {
916936
ret = HAL_I2C_Slave_Sequential_Transmit_IT(handle, (uint8_t *) data, length, I2C_NEXT_FRAME);
917937

918938
if(ret == HAL_OK) {
919-
timeout = BYTE_TIMEOUT_US * length;
939+
timeout = BYTE_TIMEOUT_US * (length + 1);
920940
while(obj_s->pending_slave_tx_master_rx && (--timeout != 0)) {
921941
wait_us(1);
922942
}

0 commit comments

Comments
 (0)