@@ -416,9 +416,6 @@ int i2c_read(i2c_t *obj, int address, char *data, int length, int stop) {
416
416
int value ;
417
417
volatile uint32_t work_reg = 0 ;
418
418
419
- if (length <= 0 ) {
420
- return 0 ;
421
- }
422
419
i2c_set_MR3_ACK (obj );
423
420
/* There is a STOP condition for last processing */
424
421
if (obj -> i2c .last_stop_flag != 0 ) {
@@ -448,76 +445,90 @@ int i2c_read(i2c_t *obj, int address, char *data, int length, int stop) {
448
445
obj -> i2c .last_stop_flag = 1 ;
449
446
return I2C_ERROR_NO_SLAVE ;
450
447
}
451
- /* Read in all except last byte */
452
- if (length > 2 ) {
453
- /* dummy read */
454
- value = REG (DRR .UINT32 );
455
- for (count = 0 ; count < (length - 1 ); count ++ ) {
448
+ if (length != 0 ) {
449
+ /* Read in all except last byte */
450
+ if (length > 2 ) {
451
+ /* dummy read */
452
+ value = REG (DRR .UINT32 );
453
+ for (count = 0 ; count < (length - 1 ); count ++ ) {
454
+ /* wait for it to arrive */
455
+ status = i2c_wait_RDRF (obj );
456
+ if (status != 0 ) {
457
+ i2c_set_err_noslave (obj );
458
+ return I2C_ERROR_NO_SLAVE ;
459
+ }
460
+ /* Recieve the data */
461
+ if (count == (length - 2 )) {
462
+ value = i2c_do_read (obj , 1 );
463
+ } else if ((length >= 3 ) && (count == (length - 3 ))) {
464
+ value = i2c_do_read (obj , 2 );
465
+ } else {
466
+ value = i2c_do_read (obj , 0 );
467
+ }
468
+ data [count ] = (char )value ;
469
+ }
470
+ } else if (length == 2 ) {
471
+ /* Set MR3 WAIT bit is 1 */
472
+ REG (MR3 .UINT32 ) |= MR3_WAIT ;
473
+ /* dummy read */
474
+ value = REG (DRR .UINT32 );
456
475
/* wait for it to arrive */
457
476
status = i2c_wait_RDRF (obj );
458
477
if (status != 0 ) {
459
478
i2c_set_err_noslave (obj );
460
479
return I2C_ERROR_NO_SLAVE ;
461
480
}
462
- /* Recieve the data */
463
- if (count == (length - 2 )) {
464
- value = i2c_do_read (obj , 1 );
465
- } else if ((length >= 3 ) && (count == (length - 3 ))) {
466
- value = i2c_do_read (obj , 2 );
467
- } else {
468
- value = i2c_do_read (obj , 0 );
469
- }
470
- data [count ] = (char )value ;
481
+ i2c_set_MR3_NACK (obj );
482
+ data [count ] = (char )REG (DRR .UINT32 );
483
+ count ++ ;
484
+ } else {
485
+ /* length == 1 */
486
+ /* Set MR3 WAIT bit is 1 */ ;
487
+ REG (MR3 .UINT32 ) |= MR3_WAIT ;
488
+ i2c_set_MR3_NACK (obj );
489
+ /* dummy read */
490
+ value = REG (DRR .UINT32 );
471
491
}
472
- } else if (length == 2 ) {
473
- /* Set MR3 WAIT bit is 1 */
474
- REG (MR3 .UINT32 ) |= MR3_WAIT ;
475
- /* dummy read */
476
- value = REG (DRR .UINT32 );
477
492
/* wait for it to arrive */
478
493
status = i2c_wait_RDRF (obj );
479
494
if (status != 0 ) {
480
495
i2c_set_err_noslave (obj );
481
496
return I2C_ERROR_NO_SLAVE ;
482
497
}
483
- i2c_set_MR3_NACK (obj );
484
- data [count ] = (char )REG (DRR .UINT32 );
485
- count ++ ;
486
- } else {
487
- /* length == 1 */
488
- /* Set MR3 WAIT bit is 1 */ ;
489
- REG (MR3 .UINT32 ) |= MR3_WAIT ;
490
- i2c_set_MR3_NACK (obj );
491
- /* dummy read */
492
- value = REG (DRR .UINT32 );
493
- }
494
- /* wait for it to arrive */
495
- status = i2c_wait_RDRF (obj );
496
- if (status != 0 ) {
497
- i2c_set_err_noslave (obj );
498
- return I2C_ERROR_NO_SLAVE ;
499
- }
500
498
501
- /* If not repeated start, send stop. */
502
- if (stop ) {
503
- (void )i2c_set_STOP (obj );
504
- /* RIICnDRR read */
505
- value = (REG (DRR .UINT32 ) & 0xFF );
506
- data [count ] = (char )value ;
507
- /* RIICnMR3.WAIT = 0 */
508
- REG (MR3 .UINT32 ) &= ~MR3_WAIT ;
509
- (void )i2c_wait_STOP (obj );
510
- i2c_set_SR2_NACKF_STOP (obj );
499
+ /* If not repeated start, send stop. */
500
+ if (stop ) {
501
+ (void )i2c_set_STOP (obj );
502
+ /* RIICnDRR read */
503
+ value = (REG (DRR .UINT32 ) & 0xFF );
504
+ data [count ] = (char )value ;
505
+ /* RIICnMR3.WAIT = 0 */
506
+ REG (MR3 .UINT32 ) &= ~MR3_WAIT ;
507
+ (void )i2c_wait_STOP (obj );
508
+ i2c_set_SR2_NACKF_STOP (obj );
509
+ } else {
510
+ (void )i2c_restart (obj );
511
+ /* RIICnDRR read */
512
+ value = (REG (DRR .UINT32 ) & 0xFF );
513
+ data [count ] = (char )value ;
514
+ /* RIICnMR3.WAIT = 0 */
515
+ REG (MR3 .UINT32 ) &= ~MR3_WAIT ;
516
+ (void )i2c_wait_START (obj );
517
+ /* SR2.START = 0 */
518
+ REG (SR2 .UINT32 ) &= ~SR2_START ;
519
+ }
511
520
} else {
512
- (void )i2c_restart (obj );
513
- /* RIICnDRR read */
514
- value = (REG (DRR .UINT32 ) & 0xFF );
515
- data [count ] = (char )value ;
516
- /* RIICnMR3.WAIT = 0 */
517
- REG (MR3 .UINT32 ) &= ~MR3_WAIT ;
518
- (void )i2c_wait_START (obj );
519
- /* SR2.START = 0 */
520
- REG (SR2 .UINT32 ) &= ~SR2_START ;
521
+ /* If not repeated start, send stop. */
522
+ if (stop ) {
523
+ (void )i2c_set_STOP (obj );
524
+ (void )i2c_wait_STOP (obj );
525
+ i2c_set_SR2_NACKF_STOP (obj );
526
+ } else {
527
+ (void )i2c_restart (obj );
528
+ (void )i2c_wait_START (obj );
529
+ /* SR2.START = 0 */
530
+ REG (SR2 .UINT32 ) &= ~SR2_START ;
531
+ }
521
532
}
522
533
523
534
return length ;
@@ -527,10 +538,6 @@ int i2c_write(i2c_t *obj, int address, const char *data, int length, int stop) {
527
538
int cnt ;
528
539
int status ;
529
540
530
- if (length <= 0 ) {
531
- return 0 ;
532
- }
533
-
534
541
/* There is a STOP condition for last processing */
535
542
if (obj -> i2c .last_stop_flag != 0 ) {
536
543
status = i2c_start (obj );
0 commit comments