@@ -442,60 +442,6 @@ i2c_t *get_i2c_obj(I2C_HandleTypeDef *hi2c){
442
442
return (obj );
443
443
}
444
444
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
-
499
445
/*
500
446
* UNITARY APIS.
501
447
* For very basic operations, direct registers access is needed
@@ -537,10 +483,18 @@ int i2c_start(i2c_t *obj) {
537
483
int i2c_stop (i2c_t * obj ) {
538
484
struct i2c_s * obj_s = I2C_S (obj );
539
485
I2C_TypeDef * i2c = (I2C_TypeDef * )obj_s -> i2c ;
486
+ I2C_HandleTypeDef * handle = & (obj_s -> handle );
487
+ int timeout ;
540
488
541
489
// Generate the STOP condition
542
490
i2c -> CR1 |= I2C_CR1_STOP ;
543
491
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
+
544
498
return 0 ;
545
499
}
546
500
@@ -685,12 +639,78 @@ void i2c_reset(i2c_t *obj) {
685
639
/*
686
640
* SYNC APIS
687
641
*/
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
+
688
701
int i2c_write (i2c_t * obj , int address , const char * data , int length , int stop ) {
689
702
struct i2c_s * obj_s = I2C_S (obj );
690
703
I2C_HandleTypeDef * handle = & (obj_s -> handle );
691
- int count = 0 , ret = 0 ;
704
+ int count = I2C_ERROR_BUS_BUSY , ret = 0 ;
692
705
uint32_t timeout = 0 ;
693
706
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
+
694
714
if ((obj_s -> XferOperation == I2C_FIRST_AND_LAST_FRAME ) ||
695
715
(obj_s -> XferOperation == I2C_LAST_FRAME )) {
696
716
if (stop )
@@ -712,7 +732,7 @@ int i2c_write(i2c_t *obj, int address, const char *data, int length, int stop) {
712
732
ret = HAL_I2C_Master_Sequential_Transmit_IT (handle , address , (uint8_t * ) data , length , obj_s -> XferOperation );
713
733
714
734
if (ret == HAL_OK ) {
715
- timeout = BYTE_TIMEOUT_US * length ;
735
+ timeout = BYTE_TIMEOUT_US * ( length + 1 ) ;
716
736
/* transfer started : wait completion or timeout */
717
737
while (!(obj_s -> event & I2C_EVENT_ALL ) && (-- timeout != 0 )) {
718
738
wait_us (1 );
@@ -891,7 +911,7 @@ int i2c_slave_read(i2c_t *obj, char *data, int length) {
891
911
ret = HAL_I2C_Slave_Sequential_Receive_IT (handle , (uint8_t * ) data , length , I2C_NEXT_FRAME );
892
912
893
913
if (ret == HAL_OK ) {
894
- timeout = BYTE_TIMEOUT_US * length ;
914
+ timeout = BYTE_TIMEOUT_US * ( length + 1 ) ;
895
915
while (obj_s -> pending_slave_rx_maxter_tx && (-- timeout != 0 )) {
896
916
wait_us (1 );
897
917
}
@@ -916,7 +936,7 @@ int i2c_slave_write(i2c_t *obj, const char *data, int length) {
916
936
ret = HAL_I2C_Slave_Sequential_Transmit_IT (handle , (uint8_t * ) data , length , I2C_NEXT_FRAME );
917
937
918
938
if (ret == HAL_OK ) {
919
- timeout = BYTE_TIMEOUT_US * length ;
939
+ timeout = BYTE_TIMEOUT_US * ( length + 1 ) ;
920
940
while (obj_s -> pending_slave_tx_master_rx && (-- timeout != 0 )) {
921
941
wait_us (1 );
922
942
}
0 commit comments