Please use mbed-src instead of this library.mbed-src supports GR-PEACH rev.C. mbed-srcライブラリをご利用ください。mbed-srcはGR-PEACH rev.Cに対応しています。
Fork of mbed-src_GR-PEACH_rev_c by
Revision 491:affe2fb21f3a, committed 2015-04-06
- Comitter:
- RyoheiHagimoto
- Date:
- Mon Apr 06 12:35:13 2015 +0000
- Parent:
- 490:fb22baa91223
- Commit message:
- The time-out of I2C is changed to 10ms from 1s.
Changed in this revision
targets/hal/TARGET_RENESAS/TARGET_RZ_A1H/i2c_api.c | Show annotated file Show diff for this revision Revisions of this file |
--- a/targets/hal/TARGET_RENESAS/TARGET_RZ_A1H/i2c_api.c Fri Apr 03 04:42:19 2015 +0000 +++ b/targets/hal/TARGET_RENESAS/TARGET_RZ_A1H/i2c_api.c Mon Apr 06 12:35:13 2015 +0000 @@ -58,7 +58,7 @@ #define SR2_TEND (1 << 6) #define SR2_TDRE (1 << 7) -#define TIMEOUT_1S (3600000) /* Loop counter : Time-out is about 1s. By 3600000 loops, measured value is 969ms. */ +#define WAIT_TIMEOUT (4200) /* Loop counter : Time-out is about 1ms. By 4200 loops, measured value is 1009ms. */ static const PinMap PinMap_I2C_SDA[] = { {P1_1 , I2C_0, 1}, @@ -108,7 +108,7 @@ /* There is no timeout, but the upper limit value is set to avoid an infinite loop. */ while (!(i2c_status(obj) & SR2_RDRF)) { timeout ++; - if (timeout >= TIMEOUT_1S) { + if (timeout >= WAIT_TIMEOUT) { return -1; } } @@ -122,7 +122,7 @@ /* There is no timeout, but the upper limit value is set to avoid an infinite loop. */ while (!(i2c_status(obj) & SR2_TDRE)) { timeout ++; - if (timeout >= TIMEOUT_1S) { + if (timeout >= WAIT_TIMEOUT) { return -1; } } @@ -136,7 +136,7 @@ /* There is no timeout, but the upper limit value is set to avoid an infinite loop. */ while (!(i2c_status(obj) & SR2_TEND)) { timeout ++; - if (timeout >= TIMEOUT_1S) { + if (timeout >= WAIT_TIMEOUT) { return -1; } } @@ -151,7 +151,7 @@ /* There is no timeout, but the upper limit value is set to avoid an infinite loop. */ while (!(i2c_status(obj) & SR2_START)) { timeout ++; - if (timeout >= TIMEOUT_1S) { + if (timeout >= WAIT_TIMEOUT) { return -1; } } @@ -165,7 +165,7 @@ /* There is no timeout, but the upper limit value is set to avoid an infinite loop. */ while (!(i2c_status(obj) & SR2_STOP)) { timeout ++; - if (timeout >= TIMEOUT_1S) { + if (timeout >= WAIT_TIMEOUT) { return -1; } } @@ -265,17 +265,11 @@ return 0; } -static void i2c_set_err_noslave(i2c_t *obj, int stop) { - if (stop) { - (void)i2c_stop(obj); - (void)i2c_wait_STOP(obj); - i2c_set_SR2_NACKF_STOP(obj); - } else { - (void)i2c_restart(obj); - (void)i2c_wait_START(obj); - /* SR2.START = 0 */ - REG(SR2.UINT32) &= ~SR2_START; - } +static void i2c_set_err_noslave(i2c_t *obj) { + (void)i2c_stop(obj); + (void)i2c_wait_STOP(obj); + i2c_set_SR2_NACKF_STOP(obj); + obj->last_stop_flag = 1; } static inline int i2c_do_write(i2c_t *obj, int value) { @@ -287,7 +281,7 @@ while (!(i2c_status(obj) & SR2_TDRE)) { /* RIICnSR2.TDRE=0 */ timeout ++; - if (timeout >= TIMEOUT_1S) { + if (timeout >= WAIT_TIMEOUT) { return -1; } if (i2c_status(obj) & SR2_NACKF) { @@ -432,7 +426,7 @@ if (obj->last_stop_flag != 0) { status = i2c_start(obj); if (status != 0) { - i2c_set_err_noslave(obj, stop); + i2c_set_err_noslave(obj); return I2C_ERROR_BUS_BUSY; } } @@ -440,7 +434,7 @@ /* Send Slave address */ status = i2c_read_address_write(obj, (address | 0x01)); if (status != 0) { - i2c_set_err_noslave(obj, stop); + i2c_set_err_noslave(obj); return I2C_ERROR_NO_SLAVE; } /* wait RDRF */ @@ -448,21 +442,12 @@ /* check ACK/NACK */ if ((status != 0) || (REG(SR2.UINT32) & SR2_NACKF == 1)) { /* Slave sends NACK */ - /* If not repeated start, send stop. */ - if (stop) { - i2c_stop(obj); - /* dummy read */ - value = REG(DRR.UINT32); - (void)i2c_wait_STOP(obj); - i2c_set_SR2_NACKF_STOP(obj); - } else { - (void)i2c_restart(obj); - /* dummy read */ - value = REG(DRR.UINT32); - (void)i2c_wait_START(obj); - /* SR2.START = 0 */ - REG(SR2.UINT32) &= ~SR2_START; - } + i2c_stop(obj); + /* dummy read */ + value = REG(DRR.UINT32); + (void)i2c_wait_STOP(obj); + i2c_set_SR2_NACKF_STOP(obj); + obj->last_stop_flag = 1; return I2C_ERROR_NO_SLAVE; } /* Read in all except last byte */ @@ -473,7 +458,7 @@ /* wait for it to arrive */ status = i2c_wait_RDRF(obj); if (status != 0) { - i2c_set_err_noslave(obj, stop); + i2c_set_err_noslave(obj); return I2C_ERROR_NO_SLAVE; } /* Recieve the data */ @@ -494,7 +479,7 @@ /* wait for it to arrive */ status = i2c_wait_RDRF(obj); if (status != 0) { - i2c_set_err_noslave(obj, stop); + i2c_set_err_noslave(obj); return I2C_ERROR_NO_SLAVE; } i2c_set_MR3_NACK(obj); @@ -511,7 +496,7 @@ /* wait for it to arrive */ status = i2c_wait_RDRF(obj); if (status != 0) { - i2c_set_err_noslave(obj, stop); + i2c_set_err_noslave(obj); return I2C_ERROR_NO_SLAVE; } @@ -552,7 +537,7 @@ if (obj->last_stop_flag != 0) { status = i2c_start(obj); if (status != 0) { - i2c_set_err_noslave(obj, stop); + i2c_set_err_noslave(obj); return I2C_ERROR_BUS_BUSY; } } @@ -560,21 +545,21 @@ /* Send Slave address */ status = i2c_do_write(obj, address); if (status != 0) { - i2c_set_err_noslave(obj, stop); + i2c_set_err_noslave(obj); return I2C_ERROR_NO_SLAVE; } /* Send Write data */ for (cnt=0; cnt<length; cnt++) { status = i2c_do_write(obj, data[cnt]); if(status != 0) { - i2c_set_err_noslave(obj, stop); + i2c_set_err_noslave(obj); return cnt; } } /* Wait send end */ status = i2c_wait_TEND(obj); if (status != 0) { - i2c_set_err_noslave(obj, stop); + i2c_set_err_noslave(obj); return I2C_ERROR_NO_SLAVE; } /* If not repeated start, send stop. */ @@ -605,7 +590,7 @@ /* wait for it to arrive */ status = i2c_wait_RDRF(obj); if (status != 0) { - i2c_set_err_noslave(obj, 1); + i2c_set_err_noslave(obj); return I2C_ERROR_NO_SLAVE; } @@ -618,7 +603,7 @@ status = i2c_do_write(obj, (data & 0xFF)); if (status != 0) { - i2c_set_err_noslave(obj, 1); + i2c_set_err_noslave(obj); ack = 0; } else { ack = 1; @@ -682,7 +667,7 @@ break; } timeout ++; - if (timeout >= TIMEOUT_1S) { + if (timeout >= WAIT_TIMEOUT) { return -1; } } @@ -730,7 +715,7 @@ /* Wait send end */ status = i2c_wait_TEND(obj); if (status != 0) { - i2c_set_err_noslave(obj, 1); + i2c_set_err_noslave(obj); return 0; } }