From dc68b76d0f35d609aebcdeeaa28ba43832c30c87 Mon Sep 17 00:00:00 2001 From: Matthew Else Date: Thu, 1 Aug 2013 15:46:05 +0100 Subject: [PATCH 1/3] Added timeout for i2c_stop in LPC1114 target. --- .../targets/hal/TARGET_NXP/TARGET_LPC11XX/i2c_api.c | 12 ++++++++---- workspace_tools/toolchains/arm.py | 2 +- 2 files changed, 9 insertions(+), 5 deletions(-) diff --git a/libraries/mbed/targets/hal/TARGET_NXP/TARGET_LPC11XX/i2c_api.c b/libraries/mbed/targets/hal/TARGET_NXP/TARGET_LPC11XX/i2c_api.c index 5185aa4b2d..55470ebfeb 100644 --- a/libraries/mbed/targets/hal/TARGET_NXP/TARGET_LPC11XX/i2c_api.c +++ b/libraries/mbed/targets/hal/TARGET_NXP/TARGET_LPC11XX/i2c_api.c @@ -125,13 +125,17 @@ inline int i2c_start(i2c_t *obj) { inline int i2c_stop(i2c_t *obj) { // write the stop bit + int timeout = 0; i2c_conset(obj, 0, 1, 0, 0); i2c_clear_SI(obj); // wait for STO bit to reset - while(I2C_CONSET(obj) & (1 << 4)); - - return 0; + while(I2C_CONSET(obj) & (1 << 4)) { + timeout++; + if (timeout>100000) return 1; + } + + return 0; } @@ -159,7 +163,7 @@ static inline int i2c_do_read(i2c_t *obj, int last) { i2c_clear_SI(obj); // wait for it to arrive - i2c_wait_SI(obj); + //i2c_wait_SI(obj); // return the data return (I2C_DAT(obj) & 0xFF); diff --git a/workspace_tools/toolchains/arm.py b/workspace_tools/toolchains/arm.py index 55cde033a7..ae040adb78 100644 --- a/workspace_tools/toolchains/arm.py +++ b/workspace_tools/toolchains/arm.py @@ -25,7 +25,7 @@ class ARM(mbedToolchain): common = [join(ARM_BIN, "armcc"), "-c", "--cpu=%s" % cpu, "--gnu", - "-Ospace", "--split_sections", "--apcs=interwork", + "-O0", "--split_sections", "--apcs=interwork", "--brief_diagnostics", "--restrict" ] From df5c36a9ae8f78770b15f1b458299bf34e317ca5 Mon Sep 17 00:00:00 2001 From: Matthew Else Date: Thu, 1 Aug 2013 16:04:18 +0100 Subject: [PATCH 2/3] Uncommented i2c_wait_SI(obj), as it should not have been commented out. --- libraries/mbed/targets/hal/TARGET_NXP/TARGET_LPC11XX/i2c_api.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/libraries/mbed/targets/hal/TARGET_NXP/TARGET_LPC11XX/i2c_api.c b/libraries/mbed/targets/hal/TARGET_NXP/TARGET_LPC11XX/i2c_api.c index 55470ebfeb..646f2ff2bf 100644 --- a/libraries/mbed/targets/hal/TARGET_NXP/TARGET_LPC11XX/i2c_api.c +++ b/libraries/mbed/targets/hal/TARGET_NXP/TARGET_LPC11XX/i2c_api.c @@ -163,7 +163,7 @@ static inline int i2c_do_read(i2c_t *obj, int last) { i2c_clear_SI(obj); // wait for it to arrive - //i2c_wait_SI(obj); + i2c_wait_SI(obj); // return the data return (I2C_DAT(obj) & 0xFF); From 0f7ece02a8ec12f913b24366d79331d5a8d3a22b Mon Sep 17 00:00:00 2001 From: Matthew Else Date: Thu, 1 Aug 2013 16:23:13 +0100 Subject: [PATCH 3/3] Fixed I2C API --- .../hal/TARGET_NXP/TARGET_LPC11XX/i2c_api.c | 42 ++++++++++--------- 1 file changed, 23 insertions(+), 19 deletions(-) diff --git a/libraries/mbed/targets/hal/TARGET_NXP/TARGET_LPC11XX/i2c_api.c b/libraries/mbed/targets/hal/TARGET_NXP/TARGET_LPC11XX/i2c_api.c index 646f2ff2bf..6fb7b23850 100644 --- a/libraries/mbed/targets/hal/TARGET_NXP/TARGET_LPC11XX/i2c_api.c +++ b/libraries/mbed/targets/hal/TARGET_NXP/TARGET_LPC11XX/i2c_api.c @@ -35,8 +35,9 @@ static const PinMap PinMap_I2C_SCL[] = { #define I2C_SCLL(x, val) (x->i2c->SCLL = val) #define I2C_SCLH(x, val) (x->i2c->SCLH = val) -static const uint32_t I2C_addr_offset[4] = { - 0x0C, 0x20, 0x24, 0x28 +static const uint32_t I2C_addr_offset[2][4] = { + {0x0C, 0x20, 0x24, 0x28}, + {0x30, 0x34, 0x38, 0x3C} }; static inline void i2c_conclr(i2c_t *obj, int start, int stop, int interrupt, int acknowledge) { @@ -124,17 +125,18 @@ inline int i2c_start(i2c_t *obj) { } inline int i2c_stop(i2c_t *obj) { + int timeout = 0; + // write the stop bit - int timeout = 0; i2c_conset(obj, 0, 1, 0, 0); i2c_clear_SI(obj); // wait for STO bit to reset while(I2C_CONSET(obj) & (1 << 4)) { - timeout++; - if (timeout>100000) return 1; - } - + timeout ++; + if (timeout > 100000) return 1; + } + return 0; } @@ -201,13 +203,13 @@ int i2c_read(i2c_t *obj, int address, char *data, int length, int stop) { if ((status != 0x10) && (status != 0x08)) { i2c_stop(obj); - return status; + return I2C_ERROR_BUS_BUSY; } status = i2c_do_write(obj, (address | 0x01), 1); if (status != 0x40) { i2c_stop(obj); - return status; + return I2C_ERROR_NO_SLAVE; } // Read in all except last byte @@ -216,7 +218,7 @@ int i2c_read(i2c_t *obj, int address, char *data, int length, int stop) { status = i2c_status(obj); if (status != 0x50) { i2c_stop(obj); - return status; + return count; } data[count] = (char) value; } @@ -226,7 +228,7 @@ int i2c_read(i2c_t *obj, int address, char *data, int length, int stop) { status = i2c_status(obj); if (status != 0x58) { i2c_stop(obj); - return status; + return length - 1; } data[count] = (char) value; @@ -236,7 +238,7 @@ int i2c_read(i2c_t *obj, int address, char *data, int length, int stop) { i2c_stop(obj); } - return 0; + return length; } int i2c_write(i2c_t *obj, int address, const char *data, int length, int stop) { @@ -246,31 +248,33 @@ int i2c_write(i2c_t *obj, int address, const char *data, int length, int stop) { if ((status != 0x10) && (status != 0x08)) { i2c_stop(obj); - return status; + return I2C_ERROR_BUS_BUSY; } status = i2c_do_write(obj, (address & 0xFE), 1); if (status != 0x18) { i2c_stop(obj); - return status; + return I2C_ERROR_NO_SLAVE; } for (i=0; i= 0) && (idx <= 3)) { - addr = ((uint32_t)obj->i2c) + I2C_addr_offset[idx]; + addr = ((uint32_t)obj->i2c) + I2C_addr_offset[0][idx]; *((uint32_t *) addr) = address & 0xFF; } }