@@ -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 TIMEOUT_1S (3600000 ) /* Loop counter : Time-out is about 1s . By 3600000 loops, measured value is 969ms . */
61
+ #define WAIT_TIMEOUT (4200 ) /* Loop counter : Time-out is about 1ms . By 4200 loops, measured value is 1009ms . */
62
62
63
63
static const PinMap PinMap_I2C_SDA [] = {
64
64
{P1_1 , I2C_0 , 1 },
@@ -108,7 +108,7 @@ static inline int i2c_wait_RDRF(i2c_t *obj) {
108
108
/* There is no timeout, but the upper limit value is set to avoid an infinite loop. */
109
109
while (!(i2c_status (obj ) & SR2_RDRF )) {
110
110
timeout ++ ;
111
- if (timeout >= TIMEOUT_1S ) {
111
+ if (timeout >= WAIT_TIMEOUT ) {
112
112
return -1 ;
113
113
}
114
114
}
@@ -122,7 +122,7 @@ static int i2c_wait_TDRE(i2c_t *obj) {
122
122
/* There is no timeout, but the upper limit value is set to avoid an infinite loop. */
123
123
while (!(i2c_status (obj ) & SR2_TDRE )) {
124
124
timeout ++ ;
125
- if (timeout >= TIMEOUT_1S ) {
125
+ if (timeout >= WAIT_TIMEOUT ) {
126
126
return -1 ;
127
127
}
128
128
}
@@ -136,7 +136,7 @@ static int i2c_wait_TEND(i2c_t *obj) {
136
136
/* There is no timeout, but the upper limit value is set to avoid an infinite loop. */
137
137
while (!(i2c_status (obj ) & SR2_TEND )) {
138
138
timeout ++ ;
139
- if (timeout >= TIMEOUT_1S ) {
139
+ if (timeout >= WAIT_TIMEOUT ) {
140
140
return -1 ;
141
141
}
142
142
}
@@ -151,7 +151,7 @@ static int i2c_wait_START(i2c_t *obj) {
151
151
/* There is no timeout, but the upper limit value is set to avoid an infinite loop. */
152
152
while (!(i2c_status (obj ) & SR2_START )) {
153
153
timeout ++ ;
154
- if (timeout >= TIMEOUT_1S ) {
154
+ if (timeout >= WAIT_TIMEOUT ) {
155
155
return -1 ;
156
156
}
157
157
}
@@ -165,7 +165,7 @@ static int i2c_wait_STOP(i2c_t *obj) {
165
165
/* There is no timeout, but the upper limit value is set to avoid an infinite loop. */
166
166
while (!(i2c_status (obj ) & SR2_STOP )) {
167
167
timeout ++ ;
168
- if (timeout >= TIMEOUT_1S ) {
168
+ if (timeout >= WAIT_TIMEOUT ) {
169
169
return -1 ;
170
170
}
171
171
}
@@ -265,17 +265,11 @@ inline int i2c_stop(i2c_t *obj) {
265
265
return 0 ;
266
266
}
267
267
268
- static void i2c_set_err_noslave (i2c_t * obj , int stop ) {
269
- if (stop ) {
270
- (void )i2c_stop (obj );
271
- (void )i2c_wait_STOP (obj );
272
- i2c_set_SR2_NACKF_STOP (obj );
273
- } else {
274
- (void )i2c_restart (obj );
275
- (void )i2c_wait_START (obj );
276
- /* SR2.START = 0 */
277
- REG (SR2 .UINT32 ) &= ~SR2_START ;
278
- }
268
+ static void i2c_set_err_noslave (i2c_t * obj ) {
269
+ (void )i2c_stop (obj );
270
+ (void )i2c_wait_STOP (obj );
271
+ i2c_set_SR2_NACKF_STOP (obj );
272
+ obj -> last_stop_flag = 1 ;
279
273
}
280
274
281
275
static inline int i2c_do_write (i2c_t * obj , int value ) {
@@ -287,7 +281,7 @@ static inline int i2c_do_write(i2c_t *obj, int value) {
287
281
while (!(i2c_status (obj ) & SR2_TDRE )) {
288
282
/* RIICnSR2.TDRE=0 */
289
283
timeout ++ ;
290
- if (timeout >= TIMEOUT_1S ) {
284
+ if (timeout >= WAIT_TIMEOUT ) {
291
285
return -1 ;
292
286
}
293
287
if (i2c_status (obj ) & SR2_NACKF ) {
@@ -432,37 +426,28 @@ int i2c_read(i2c_t *obj, int address, char *data, int length, int stop) {
432
426
if (obj -> last_stop_flag != 0 ) {
433
427
status = i2c_start (obj );
434
428
if (status != 0 ) {
435
- i2c_set_err_noslave (obj , stop );
429
+ i2c_set_err_noslave (obj );
436
430
return I2C_ERROR_BUS_BUSY ;
437
431
}
438
432
}
439
433
obj -> last_stop_flag = stop ;
440
434
/* Send Slave address */
441
435
status = i2c_read_address_write (obj , (address | 0x01 ));
442
436
if (status != 0 ) {
443
- i2c_set_err_noslave (obj , stop );
437
+ i2c_set_err_noslave (obj );
444
438
return I2C_ERROR_NO_SLAVE ;
445
439
}
446
440
/* wait RDRF */
447
441
status = i2c_wait_RDRF (obj );
448
442
/* check ACK/NACK */
449
443
if ((status != 0 ) || (REG (SR2 .UINT32 ) & SR2_NACKF == 1 )) {
450
444
/* Slave sends NACK */
451
- /* If not repeated start, send stop. */
452
- if (stop ) {
453
- i2c_stop (obj );
454
- /* dummy read */
455
- value = REG (DRR .UINT32 );
456
- (void )i2c_wait_STOP (obj );
457
- i2c_set_SR2_NACKF_STOP (obj );
458
- } else {
459
- (void )i2c_restart (obj );
460
- /* dummy read */
461
- value = REG (DRR .UINT32 );
462
- (void )i2c_wait_START (obj );
463
- /* SR2.START = 0 */
464
- REG (SR2 .UINT32 ) &= ~SR2_START ;
465
- }
445
+ i2c_stop (obj );
446
+ /* dummy read */
447
+ value = REG (DRR .UINT32 );
448
+ (void )i2c_wait_STOP (obj );
449
+ i2c_set_SR2_NACKF_STOP (obj );
450
+ obj -> last_stop_flag = 1 ;
466
451
return I2C_ERROR_NO_SLAVE ;
467
452
}
468
453
/* Read in all except last byte */
@@ -473,7 +458,7 @@ int i2c_read(i2c_t *obj, int address, char *data, int length, int stop) {
473
458
/* wait for it to arrive */
474
459
status = i2c_wait_RDRF (obj );
475
460
if (status != 0 ) {
476
- i2c_set_err_noslave (obj , stop );
461
+ i2c_set_err_noslave (obj );
477
462
return I2C_ERROR_NO_SLAVE ;
478
463
}
479
464
/* Recieve the data */
@@ -494,7 +479,7 @@ int i2c_read(i2c_t *obj, int address, char *data, int length, int stop) {
494
479
/* wait for it to arrive */
495
480
status = i2c_wait_RDRF (obj );
496
481
if (status != 0 ) {
497
- i2c_set_err_noslave (obj , stop );
482
+ i2c_set_err_noslave (obj );
498
483
return I2C_ERROR_NO_SLAVE ;
499
484
}
500
485
i2c_set_MR3_NACK (obj );
@@ -511,7 +496,7 @@ int i2c_read(i2c_t *obj, int address, char *data, int length, int stop) {
511
496
/* wait for it to arrive */
512
497
status = i2c_wait_RDRF (obj );
513
498
if (status != 0 ) {
514
- i2c_set_err_noslave (obj , stop );
499
+ i2c_set_err_noslave (obj );
515
500
return I2C_ERROR_NO_SLAVE ;
516
501
}
517
502
@@ -552,29 +537,29 @@ int i2c_write(i2c_t *obj, int address, const char *data, int length, int stop) {
552
537
if (obj -> last_stop_flag != 0 ) {
553
538
status = i2c_start (obj );
554
539
if (status != 0 ) {
555
- i2c_set_err_noslave (obj , stop );
540
+ i2c_set_err_noslave (obj );
556
541
return I2C_ERROR_BUS_BUSY ;
557
542
}
558
543
}
559
544
obj -> last_stop_flag = stop ;
560
545
/* Send Slave address */
561
546
status = i2c_do_write (obj , address );
562
547
if (status != 0 ) {
563
- i2c_set_err_noslave (obj , stop );
548
+ i2c_set_err_noslave (obj );
564
549
return I2C_ERROR_NO_SLAVE ;
565
550
}
566
551
/* Send Write data */
567
552
for (cnt = 0 ; cnt < length ; cnt ++ ) {
568
553
status = i2c_do_write (obj , data [cnt ]);
569
554
if (status != 0 ) {
570
- i2c_set_err_noslave (obj , stop );
555
+ i2c_set_err_noslave (obj );
571
556
return cnt ;
572
557
}
573
558
}
574
559
/* Wait send end */
575
560
status = i2c_wait_TEND (obj );
576
561
if (status != 0 ) {
577
- i2c_set_err_noslave (obj , stop );
562
+ i2c_set_err_noslave (obj );
578
563
return I2C_ERROR_NO_SLAVE ;
579
564
}
580
565
/* If not repeated start, send stop. */
@@ -605,7 +590,7 @@ int i2c_byte_read(i2c_t *obj, int last) {
605
590
/* wait for it to arrive */
606
591
status = i2c_wait_RDRF (obj );
607
592
if (status != 0 ) {
608
- i2c_set_err_noslave (obj , 1 );
593
+ i2c_set_err_noslave (obj );
609
594
return I2C_ERROR_NO_SLAVE ;
610
595
}
611
596
@@ -618,7 +603,7 @@ int i2c_byte_write(i2c_t *obj, int data) {
618
603
619
604
status = i2c_do_write (obj , (data & 0xFF ));
620
605
if (status != 0 ) {
621
- i2c_set_err_noslave (obj , 1 );
606
+ i2c_set_err_noslave (obj );
622
607
ack = 0 ;
623
608
} else {
624
609
ack = 1 ;
@@ -682,7 +667,7 @@ int i2c_slave_read(i2c_t *obj, char *data, int length) {
682
667
break ;
683
668
}
684
669
timeout ++ ;
685
- if (timeout >= TIMEOUT_1S ) {
670
+ if (timeout >= WAIT_TIMEOUT ) {
686
671
return -1 ;
687
672
}
688
673
}
@@ -730,7 +715,7 @@ int i2c_slave_write(i2c_t *obj, const char *data, int length) {
730
715
/* Wait send end */
731
716
status = i2c_wait_TEND (obj );
732
717
if (status != 0 ) {
733
- i2c_set_err_noslave (obj , 1 );
718
+ i2c_set_err_noslave (obj );
734
719
return 0 ;
735
720
}
736
721
}
0 commit comments