@@ -58,7 +58,7 @@ volatile struct st_riic *RIIC[] = RIIC_ADDRESS_LIST;
58
58
#define SR2_TEND (1 << 6)
59
59
#define SR2_TDRE (1 << 7)
60
60
61
- #define WAIT_TIMEOUT (4200 ) /* Loop counter : Time-out is about 1ms . By 4200 loops, measured value is 1009ms . */
61
+ #define WAIT_TIMEOUT (3600000 ) /* Loop counter : Time-out is about 1s . By 3600000 loops, measured value is 969ms . */
62
62
63
63
static const PinMap PinMap_I2C_SDA [] = {
64
64
{P1_1 , I2C_0 , 1 },
@@ -106,7 +106,7 @@ static inline int i2c_wait_RDRF(i2c_t *obj) {
106
106
int timeout = 0 ;
107
107
108
108
/* There is no timeout, but the upper limit value is set to avoid an infinite loop. */
109
- while (! (i2c_status (obj ) & SR2_RDRF )) {
109
+ while ((i2c_status (obj ) & SR2_RDRF ) == 0 ) {
110
110
timeout ++ ;
111
111
if (timeout >= WAIT_TIMEOUT ) {
112
112
return -1 ;
@@ -120,7 +120,7 @@ static int i2c_wait_TDRE(i2c_t *obj) {
120
120
int timeout = 0 ;
121
121
122
122
/* There is no timeout, but the upper limit value is set to avoid an infinite loop. */
123
- while (! (i2c_status (obj ) & SR2_TDRE )) {
123
+ while ((i2c_status (obj ) & SR2_TDRE ) == 0 ) {
124
124
timeout ++ ;
125
125
if (timeout >= WAIT_TIMEOUT ) {
126
126
return -1 ;
@@ -134,7 +134,7 @@ static int i2c_wait_TEND(i2c_t *obj) {
134
134
int timeout = 0 ;
135
135
136
136
/* There is no timeout, but the upper limit value is set to avoid an infinite loop. */
137
- while (! (i2c_status (obj ) & SR2_TEND )) {
137
+ while ((i2c_status (obj ) & SR2_TEND ) == 0 ) {
138
138
timeout ++ ;
139
139
if (timeout >= WAIT_TIMEOUT ) {
140
140
return -1 ;
@@ -149,7 +149,7 @@ static int i2c_wait_START(i2c_t *obj) {
149
149
int timeout = 0 ;
150
150
151
151
/* There is no timeout, but the upper limit value is set to avoid an infinite loop. */
152
- while (! (i2c_status (obj ) & SR2_START )) {
152
+ while ((i2c_status (obj ) & SR2_START ) == 0 ) {
153
153
timeout ++ ;
154
154
if (timeout >= WAIT_TIMEOUT ) {
155
155
return -1 ;
@@ -163,7 +163,7 @@ static int i2c_wait_STOP(i2c_t *obj) {
163
163
int timeout = 0 ;
164
164
165
165
/* There is no timeout, but the upper limit value is set to avoid an infinite loop. */
166
- while (! (i2c_status (obj ) & SR2_STOP )) {
166
+ while ((i2c_status (obj ) & SR2_STOP ) == 0 ) {
167
167
timeout ++ ;
168
168
if (timeout >= WAIT_TIMEOUT ) {
169
169
return -1 ;
@@ -173,6 +173,15 @@ static int i2c_wait_STOP(i2c_t *obj) {
173
173
return 0 ;
174
174
}
175
175
176
+ static int i2c_set_STOP (i2c_t * obj ) {
177
+ /* SR2.STOP = 0 */
178
+ REG (SR2 .UINT32 ) &= ~SR2_STOP ;
179
+ /* Stop condition */
180
+ REG (CR2 .UINT32 ) |= CR2_SP ;
181
+
182
+ return 0 ;
183
+ }
184
+
176
185
static void i2c_set_SR2_NACKF_STOP (i2c_t * obj ) {
177
186
/* SR2.NACKF = 0 */
178
187
REG (SR2 .UINT32 ) &= ~SR2_NACKF ;
@@ -235,7 +244,7 @@ void i2c_init(i2c_t *obj, PinName sda, PinName scl) {
235
244
inline int i2c_start (i2c_t * obj ) {
236
245
int timeout = 0 ;
237
246
238
- while (REG (CR2 .UINT32 ) & CR2_BBSY ) {
247
+ while (( REG (CR2 .UINT32 ) & CR2_BBSY ) != 0 ) {
239
248
timeout ++ ;
240
249
if (timeout >= obj -> bbsy_wait_cnt ) {
241
250
break ;
@@ -257,16 +266,15 @@ static inline int i2c_restart(i2c_t *obj) {
257
266
}
258
267
259
268
inline int i2c_stop (i2c_t * obj ) {
260
- /* SR2.STOP = 0 */
261
- REG (SR2 .UINT32 ) &= ~SR2_STOP ;
262
- /* Stop condition */
263
- REG (CR2 .UINT32 ) |= CR2_SP ;
264
-
269
+ (void )i2c_set_STOP (obj );
270
+ (void )i2c_wait_STOP (obj );
271
+ i2c_set_SR2_NACKF_STOP (obj );
272
+
265
273
return 0 ;
266
274
}
267
275
268
276
static void i2c_set_err_noslave (i2c_t * obj ) {
269
- (void )i2c_stop (obj );
277
+ (void )i2c_set_STOP (obj );
270
278
(void )i2c_wait_STOP (obj );
271
279
i2c_set_SR2_NACKF_STOP (obj );
272
280
obj -> last_stop_flag = 1 ;
@@ -275,25 +283,15 @@ static void i2c_set_err_noslave(i2c_t *obj) {
275
283
static inline int i2c_do_write (i2c_t * obj , int value ) {
276
284
int timeout = 0 ;
277
285
278
- if (!(i2c_status (obj ) & SR2_NACKF )) {
279
- /* RIICnSR2.NACKF=0 */
280
- /* There is no timeout, but the upper limit value is set to avoid an infinite loop. */
281
- while (!(i2c_status (obj ) & SR2_TDRE )) {
282
- /* RIICnSR2.TDRE=0 */
283
- timeout ++ ;
284
- if (timeout >= WAIT_TIMEOUT ) {
285
- return -1 ;
286
- }
287
- if (i2c_status (obj ) & SR2_NACKF ) {
288
- /* RIICnSR2.NACKF=1 */
289
- return -1 ;
290
- }
286
+ /* There is no timeout, but the upper limit value is set to avoid an infinite loop. */
287
+ while ((i2c_status (obj ) & SR2_TDRE ) == 0 ) {
288
+ timeout ++ ;
289
+ if (timeout >= WAIT_TIMEOUT ) {
290
+ return -1 ;
291
291
}
292
- /* write the data */
293
- REG (DRT .UINT32 ) = value ;
294
- } else {
295
- return -1 ;
296
292
}
293
+ /* write the data */
294
+ REG (DRT .UINT32 ) = value ;
297
295
298
296
return 0 ;
299
297
}
@@ -440,9 +438,9 @@ int i2c_read(i2c_t *obj, int address, char *data, int length, int stop) {
440
438
/* wait RDRF */
441
439
status = i2c_wait_RDRF (obj );
442
440
/* check ACK/NACK */
443
- if ((status != 0 ) || (REG (SR2 .UINT32 ) & SR2_NACKF == 1 )) {
441
+ if ((status != 0 ) || (( REG (SR2 .UINT32 ) & SR2_NACKF ) != 0 )) {
444
442
/* Slave sends NACK */
445
- i2c_stop (obj );
443
+ ( void ) i2c_set_STOP (obj );
446
444
/* dummy read */
447
445
value = REG (DRR .UINT32 );
448
446
(void )i2c_wait_STOP (obj );
@@ -502,9 +500,9 @@ int i2c_read(i2c_t *obj, int address, char *data, int length, int stop) {
502
500
503
501
/* If not repeated start, send stop. */
504
502
if (stop ) {
505
- (void )i2c_stop (obj );
503
+ (void )i2c_set_STOP (obj );
506
504
/* RIICnDRR read */
507
- value = REG (DRR .UINT32 ) & 0xFF ;
505
+ value = ( REG (DRR .UINT32 ) & 0xFF ) ;
508
506
data [count ] = (char )value ;
509
507
/* RIICnMR3.WAIT = 0 */
510
508
REG (MR3 .UINT32 ) &= ~MR3_WAIT ;
@@ -513,7 +511,7 @@ int i2c_read(i2c_t *obj, int address, char *data, int length, int stop) {
513
511
} else {
514
512
(void )i2c_restart (obj );
515
513
/* RIICnDRR read */
516
- value = REG (DRR .UINT32 ) & 0xFF ;
514
+ value = ( REG (DRR .UINT32 ) & 0xFF ) ;
517
515
data [count ] = (char )value ;
518
516
/* RIICnMR3.WAIT = 0 */
519
517
REG (MR3 .UINT32 ) &= ~MR3_WAIT ;
@@ -548,23 +546,32 @@ int i2c_write(i2c_t *obj, int address, const char *data, int length, int stop) {
548
546
i2c_set_err_noslave (obj );
549
547
return I2C_ERROR_NO_SLAVE ;
550
548
}
549
+ /* Wait send end */
550
+ status = i2c_wait_TEND (obj );
551
+ if ((status != 0 ) || ((REG (SR2 .UINT32 ) & SR2_NACKF ) != 0 )) {
552
+ /* Slave sends NACK */
553
+ i2c_set_err_noslave (obj );
554
+ return I2C_ERROR_NO_SLAVE ;
555
+ }
551
556
/* Send Write data */
552
557
for (cnt = 0 ; cnt < length ; cnt ++ ) {
553
558
status = i2c_do_write (obj , data [cnt ]);
554
559
if (status != 0 ) {
555
560
i2c_set_err_noslave (obj );
556
561
return cnt ;
562
+ } else {
563
+ /* Wait send end */
564
+ status = i2c_wait_TEND (obj );
565
+ if ((status != 0 ) || ((REG (SR2 .UINT32 ) & SR2_NACKF ) != 0 )) {
566
+ /* Slave sends NACK */
567
+ i2c_set_err_noslave (obj );
568
+ return I2C_ERROR_NO_SLAVE ;
569
+ }
557
570
}
558
571
}
559
- /* Wait send end */
560
- status = i2c_wait_TEND (obj );
561
- if (status != 0 ) {
562
- i2c_set_err_noslave (obj );
563
- return I2C_ERROR_NO_SLAVE ;
564
- }
565
572
/* If not repeated start, send stop. */
566
573
if (stop ) {
567
- (void )i2c_stop (obj );
574
+ (void )i2c_set_STOP (obj );
568
575
(void )i2c_wait_STOP (obj );
569
576
i2c_set_SR2_NACKF_STOP (obj );
570
577
} else {
@@ -579,34 +586,48 @@ int i2c_write(i2c_t *obj, int address, const char *data, int length, int stop) {
579
586
}
580
587
581
588
void i2c_reset (i2c_t * obj ) {
582
- i2c_stop (obj );
589
+ ( void ) i2c_set_STOP (obj );
583
590
(void )i2c_wait_STOP (obj );
584
591
i2c_set_SR2_NACKF_STOP (obj );
585
592
}
586
593
587
594
int i2c_byte_read (i2c_t * obj , int last ) {
588
595
int status ;
596
+ int data ;
589
597
598
+ data = i2c_do_read (obj , last );
590
599
/* wait for it to arrive */
591
600
status = i2c_wait_RDRF (obj );
592
601
if (status != 0 ) {
593
- i2c_set_err_noslave (obj );
602
+ i2c_set_SR2_NACKF_STOP (obj );
594
603
return I2C_ERROR_NO_SLAVE ;
595
604
}
596
605
597
- return ( i2c_do_read ( obj , last )) ;
606
+ return data ;
598
607
}
599
608
600
609
int i2c_byte_write (i2c_t * obj , int data ) {
601
- int ack ;
610
+ int ack = 0 ;
602
611
int status ;
612
+ int timeout = 0 ;
603
613
604
614
status = i2c_do_write (obj , (data & 0xFF ));
605
615
if (status != 0 ) {
606
- i2c_set_err_noslave (obj );
607
- ack = 0 ;
616
+ i2c_set_SR2_NACKF_STOP (obj );
608
617
} else {
609
- ack = 1 ;
618
+ while (((i2c_status (obj ) & SR2_RDRF ) == 0 ) && ((i2c_status (obj ) & SR2_TEND ) == 0 )) {
619
+ timeout ++ ;
620
+ if (timeout >= WAIT_TIMEOUT ) {
621
+ return ack ;
622
+ }
623
+ }
624
+ /* check ACK/NACK */
625
+ if ((REG (SR2 .UINT32 ) & SR2_NACKF ) != 0 ) {
626
+ /* NACK */
627
+ i2c_set_SR2_NACKF_STOP (obj );
628
+ } else {
629
+ ack = 1 ;
630
+ }
610
631
}
611
632
612
633
return ack ;
@@ -624,7 +645,7 @@ int i2c_slave_receive(i2c_t *obj) {
624
645
int status ;
625
646
int retval ;
626
647
627
- status = REG (SR1 .UINT8 [0 ]) & SR1_AAS0 ;
648
+ status = ( REG (SR1 .UINT8 [0 ]) & SR1_AAS0 ) ;
628
649
status |= (REG (CR2 .UINT8 [0 ]) & CR2_TRS ) >> 4 ;
629
650
630
651
switch (status ) {
@@ -659,10 +680,8 @@ int i2c_slave_read(i2c_t *obj, char *data, int length) {
659
680
}
660
681
for (count = 0 ; ((count < (length + 1 )) && (break_flg == 0 )); count ++ ) {
661
682
/* There is no timeout, but the upper limit value is set to avoid an infinite loop. */
662
- while ((i2c_status (obj ) & SR2_STOP ) || (!(i2c_status (obj ) & SR2_RDRF ))) {
663
- /* RIICnSR2.STOP = 1 or RIICnSR2.RDRF = 0 */
664
- if (i2c_status (obj ) & SR2_STOP ) {
665
- /* RIICnSR2.STOP = 1 */
683
+ while (((i2c_status (obj ) & SR2_STOP ) != 0 ) || ((i2c_status (obj ) & SR2_RDRF ) == 0 )) {
684
+ if ((i2c_status (obj ) & SR2_STOP ) != 0 ) {
666
685
break_flg = 1 ;
667
686
break ;
668
687
}
@@ -683,7 +702,7 @@ int i2c_slave_read(i2c_t *obj, char *data, int length) {
683
702
if (break_flg == 0 ) {
684
703
(void )i2c_wait_STOP (obj );
685
704
} else {
686
- if (i2c_status (obj ) & SR2_RDRF ) {
705
+ if (( i2c_status (obj ) & SR2_RDRF ) != 0 ) {
687
706
if (count <= 1 ) {
688
707
/* fail safe */
689
708
/* dummy read */
@@ -709,15 +728,15 @@ int i2c_slave_write(i2c_t *obj, const char *data, int length) {
709
728
710
729
while ((count < length ) && (status == 0 )) {
711
730
status = i2c_do_write (obj , data [count ]);
712
- count ++ ;
713
- }
714
- if (status == 0 ) {
715
- /* Wait send end */
716
- status = i2c_wait_TEND (obj );
717
- if (status != 0 ) {
718
- i2c_set_err_noslave (obj );
719
- return 0 ;
731
+ if (status == 0 ) {
732
+ /* Wait send end */
733
+ status = i2c_wait_TEND (obj );
734
+ if ((status != 0 ) || ((count < (length - 1 )) && ((REG (SR2 .UINT32 ) & SR2_NACKF ) != 0 ))) {
735
+ /* NACK */
736
+ break ;
737
+ }
720
738
}
739
+ count ++ ;
721
740
}
722
741
/* dummy read */
723
742
(void )REG (DRR .UINT32 );
@@ -728,5 +747,5 @@ int i2c_slave_write(i2c_t *obj, const char *data, int length) {
728
747
}
729
748
730
749
void i2c_slave_address (i2c_t * obj , int idx , uint32_t address , uint32_t mask ) {
731
- REG (SAR0 .UINT32 ) = address & 0xfffffffe ;
750
+ REG (SAR0 .UINT32 ) = ( address & 0xfffffffe ) ;
732
751
}
0 commit comments