Skip to content

Commit d06efae

Browse files
committed
Merge pull request #1102 from masaohamanaka/master
RZ_A1H - Fix bugs of I2C and Update a header file of Video driver.
2 parents 31db62f + 494b8e4 commit d06efae

File tree

2 files changed

+87
-67
lines changed
  • libraries/mbed/targets

2 files changed

+87
-67
lines changed

libraries/mbed/targets/cmsis/TARGET_RENESAS/TARGET_RZ_A1H/inc/iodefines/lvds_iodefine.h

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@
2424
* File Name : lvds_iodefine.h
2525
* $Rev: $
2626
* $Date:: $
27-
* Description : Definition of I/O Register (V1.00a)
27+
* Description : Definition of I/O Register (V1.01a)
2828
******************************************************************************/
2929
#ifndef LVDS_IODEFINE_H
3030
#define LVDS_IODEFINE_H
@@ -37,7 +37,8 @@ struct st_lvds
3737
volatile uint8_t dummy608[24]; /* */
3838
volatile uint32_t LCLKSELR; /* LCLKSELR */
3939
volatile uint32_t LPLLSETR; /* LPLLSETR */
40-
volatile uint32_t LPLLMONR; /* LPLLMONR */
40+
volatile uint8_t dummy609[4]; /* */
41+
volatile uint32_t LPHYACC; /* LPHYACC */
4142
};
4243

4344

@@ -48,6 +49,6 @@ struct st_lvds
4849
#define LVDSLVDSFCL LVDS.LVDSFCL
4950
#define LVDSLCLKSELR LVDS.LCLKSELR
5051
#define LVDSLPLLSETR LVDS.LPLLSETR
51-
#define LVDSLPLLMONR LVDS.LPLLMONR
52+
#define LVDSLPHYACC LVDS.LPHYACC
5253
/* <-SEC M1.10.1 */
5354
#endif

libraries/mbed/targets/hal/TARGET_RENESAS/TARGET_RZ_A1H/i2c_api.c

Lines changed: 83 additions & 64 deletions
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ volatile struct st_riic *RIIC[] = RIIC_ADDRESS_LIST;
5858
#define SR2_TEND (1 << 6)
5959
#define SR2_TDRE (1 << 7)
6060

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. */
6262

6363
static const PinMap PinMap_I2C_SDA[] = {
6464
{P1_1 , I2C_0, 1},
@@ -106,7 +106,7 @@ static inline int i2c_wait_RDRF(i2c_t *obj) {
106106
int timeout = 0;
107107

108108
/* 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) {
110110
timeout ++;
111111
if (timeout >= WAIT_TIMEOUT) {
112112
return -1;
@@ -120,7 +120,7 @@ static int i2c_wait_TDRE(i2c_t *obj) {
120120
int timeout = 0;
121121

122122
/* 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) {
124124
timeout ++;
125125
if (timeout >= WAIT_TIMEOUT) {
126126
return -1;
@@ -134,7 +134,7 @@ static int i2c_wait_TEND(i2c_t *obj) {
134134
int timeout = 0;
135135

136136
/* 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) {
138138
timeout ++;
139139
if (timeout >= WAIT_TIMEOUT) {
140140
return -1;
@@ -149,7 +149,7 @@ static int i2c_wait_START(i2c_t *obj) {
149149
int timeout = 0;
150150

151151
/* 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) {
153153
timeout ++;
154154
if (timeout >= WAIT_TIMEOUT) {
155155
return -1;
@@ -163,7 +163,7 @@ static int i2c_wait_STOP(i2c_t *obj) {
163163
int timeout = 0;
164164

165165
/* 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) {
167167
timeout ++;
168168
if (timeout >= WAIT_TIMEOUT) {
169169
return -1;
@@ -173,6 +173,15 @@ static int i2c_wait_STOP(i2c_t *obj) {
173173
return 0;
174174
}
175175

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+
176185
static void i2c_set_SR2_NACKF_STOP(i2c_t *obj) {
177186
/* SR2.NACKF = 0 */
178187
REG(SR2.UINT32) &= ~SR2_NACKF;
@@ -235,7 +244,7 @@ void i2c_init(i2c_t *obj, PinName sda, PinName scl) {
235244
inline int i2c_start(i2c_t *obj) {
236245
int timeout = 0;
237246

238-
while (REG(CR2.UINT32) & CR2_BBSY) {
247+
while ((REG(CR2.UINT32) & CR2_BBSY) != 0) {
239248
timeout ++;
240249
if (timeout >= obj->bbsy_wait_cnt) {
241250
break;
@@ -257,16 +266,15 @@ static inline int i2c_restart(i2c_t *obj) {
257266
}
258267

259268
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+
265273
return 0;
266274
}
267275

268276
static void i2c_set_err_noslave(i2c_t *obj) {
269-
(void)i2c_stop(obj);
277+
(void)i2c_set_STOP(obj);
270278
(void)i2c_wait_STOP(obj);
271279
i2c_set_SR2_NACKF_STOP(obj);
272280
obj->last_stop_flag = 1;
@@ -275,25 +283,15 @@ static void i2c_set_err_noslave(i2c_t *obj) {
275283
static inline int i2c_do_write(i2c_t *obj, int value) {
276284
int timeout = 0;
277285

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;
291291
}
292-
/* write the data */
293-
REG(DRT.UINT32) = value;
294-
} else {
295-
return -1;
296292
}
293+
/* write the data */
294+
REG(DRT.UINT32) = value;
297295

298296
return 0;
299297
}
@@ -440,9 +438,9 @@ int i2c_read(i2c_t *obj, int address, char *data, int length, int stop) {
440438
/* wait RDRF */
441439
status = i2c_wait_RDRF(obj);
442440
/* check ACK/NACK */
443-
if ((status != 0) || (REG(SR2.UINT32) & SR2_NACKF == 1)) {
441+
if ((status != 0) || ((REG(SR2.UINT32) & SR2_NACKF) != 0)) {
444442
/* Slave sends NACK */
445-
i2c_stop(obj);
443+
(void)i2c_set_STOP(obj);
446444
/* dummy read */
447445
value = REG(DRR.UINT32);
448446
(void)i2c_wait_STOP(obj);
@@ -502,9 +500,9 @@ int i2c_read(i2c_t *obj, int address, char *data, int length, int stop) {
502500

503501
/* If not repeated start, send stop. */
504502
if (stop) {
505-
(void)i2c_stop(obj);
503+
(void)i2c_set_STOP(obj);
506504
/* RIICnDRR read */
507-
value = REG(DRR.UINT32) & 0xFF;
505+
value = (REG(DRR.UINT32) & 0xFF);
508506
data[count] = (char)value;
509507
/* RIICnMR3.WAIT = 0 */
510508
REG(MR3.UINT32) &= ~MR3_WAIT;
@@ -513,7 +511,7 @@ int i2c_read(i2c_t *obj, int address, char *data, int length, int stop) {
513511
} else {
514512
(void)i2c_restart(obj);
515513
/* RIICnDRR read */
516-
value = REG(DRR.UINT32) & 0xFF;
514+
value = (REG(DRR.UINT32) & 0xFF);
517515
data[count] = (char)value;
518516
/* RIICnMR3.WAIT = 0 */
519517
REG(MR3.UINT32) &= ~MR3_WAIT;
@@ -548,23 +546,32 @@ int i2c_write(i2c_t *obj, int address, const char *data, int length, int stop) {
548546
i2c_set_err_noslave(obj);
549547
return I2C_ERROR_NO_SLAVE;
550548
}
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+
}
551556
/* Send Write data */
552557
for (cnt=0; cnt<length; cnt++) {
553558
status = i2c_do_write(obj, data[cnt]);
554559
if(status != 0) {
555560
i2c_set_err_noslave(obj);
556561
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+
}
557570
}
558571
}
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-
}
565572
/* If not repeated start, send stop. */
566573
if (stop) {
567-
(void)i2c_stop(obj);
574+
(void)i2c_set_STOP(obj);
568575
(void)i2c_wait_STOP(obj);
569576
i2c_set_SR2_NACKF_STOP(obj);
570577
} else {
@@ -579,34 +586,48 @@ int i2c_write(i2c_t *obj, int address, const char *data, int length, int stop) {
579586
}
580587

581588
void i2c_reset(i2c_t *obj) {
582-
i2c_stop(obj);
589+
(void)i2c_set_STOP(obj);
583590
(void)i2c_wait_STOP(obj);
584591
i2c_set_SR2_NACKF_STOP(obj);
585592
}
586593

587594
int i2c_byte_read(i2c_t *obj, int last) {
588595
int status;
596+
int data;
589597

598+
data = i2c_do_read(obj, last);
590599
/* wait for it to arrive */
591600
status = i2c_wait_RDRF(obj);
592601
if (status != 0) {
593-
i2c_set_err_noslave(obj);
602+
i2c_set_SR2_NACKF_STOP(obj);
594603
return I2C_ERROR_NO_SLAVE;
595604
}
596605

597-
return (i2c_do_read(obj, last));
606+
return data;
598607
}
599608

600609
int i2c_byte_write(i2c_t *obj, int data) {
601-
int ack;
610+
int ack = 0;
602611
int status;
612+
int timeout = 0;
603613

604614
status = i2c_do_write(obj, (data & 0xFF));
605615
if (status != 0) {
606-
i2c_set_err_noslave(obj);
607-
ack = 0;
616+
i2c_set_SR2_NACKF_STOP(obj);
608617
} 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+
}
610631
}
611632

612633
return ack;
@@ -624,7 +645,7 @@ int i2c_slave_receive(i2c_t *obj) {
624645
int status;
625646
int retval;
626647

627-
status = REG(SR1.UINT8[0]) & SR1_AAS0;
648+
status = (REG(SR1.UINT8[0]) & SR1_AAS0);
628649
status |= (REG(CR2.UINT8[0]) & CR2_TRS) >> 4;
629650

630651
switch(status) {
@@ -659,10 +680,8 @@ int i2c_slave_read(i2c_t *obj, char *data, int length) {
659680
}
660681
for (count = 0; ((count < (length + 1)) && (break_flg == 0)); count++) {
661682
/* 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) {
666685
break_flg = 1;
667686
break;
668687
}
@@ -683,7 +702,7 @@ int i2c_slave_read(i2c_t *obj, char *data, int length) {
683702
if (break_flg == 0) {
684703
(void)i2c_wait_STOP(obj);
685704
} else {
686-
if (i2c_status(obj) & SR2_RDRF) {
705+
if ((i2c_status(obj) & SR2_RDRF) != 0) {
687706
if (count <= 1) {
688707
/* fail safe */
689708
/* dummy read */
@@ -709,15 +728,15 @@ int i2c_slave_write(i2c_t *obj, const char *data, int length) {
709728

710729
while ((count < length) && (status == 0)) {
711730
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+
}
720738
}
739+
count++;
721740
}
722741
/* dummy read */
723742
(void)REG(DRR.UINT32);
@@ -728,5 +747,5 @@ int i2c_slave_write(i2c_t *obj, const char *data, int length) {
728747
}
729748

730749
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);
732751
}

0 commit comments

Comments
 (0)