Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Fork of mbed-src by
Diff: targets/hal/TARGET_NXP/TARGET_LPC15XX/i2c_api.c
- Revision:
- 117:e0a7df0a9a56
- Parent:
- 103:9b881da47c92
- Child:
- 182:242346c42295
--- a/targets/hal/TARGET_NXP/TARGET_LPC15XX/i2c_api.c Tue Mar 11 17:15:06 2014 +0000 +++ b/targets/hal/TARGET_NXP/TARGET_LPC15XX/i2c_api.c Wed Mar 12 10:30:07 2014 +0000 @@ -29,7 +29,7 @@ // Wait until the Serial Interrupt (SI) is set static int i2c_wait_SI(i2c_t *obj) { - int timeout = 0; + volatile int timeout = 0; while (!(LPC_I2C0->STAT & (1 << 0))) { timeout++; if (timeout > 100000) return -1; @@ -41,25 +41,21 @@ LPC_I2C0->CFG |= (1 << 0); } -static inline void i2c_power_enable(i2c_t *obj) { +void i2c_init(i2c_t *obj, PinName sda, PinName scl) { + if ((sda != P0_23) | (scl != P0_22)) { + error("I2C pin mapping failed"); + } + // Enables clock for I2C0 - LPC_SYSCON->SYSAHBCLKCTRL1 |= (1<<13); -// LPC_SYSCON->PRESETCTRL1 &= ~(0x1<<13); - LPC_SYSCON->PRESETCTRL1 |= (0x1<<13); - LPC_SYSCON->PRESETCTRL1 &= ~(0x1 << 13); + LPC_SYSCON->SYSAHBCLKCTRL1 |= (1 << 13); -} + LPC_SYSCON->PRESETCTRL1 |= (1 << 13); + LPC_SYSCON->PRESETCTRL1 &= ~(1 << 13); -void i2c_init(i2c_t *obj, PinName sda, PinName scl) { - - // sè`ÌmFǤµæ¤c - - - // enable power - i2c_power_enable(obj); // pin enable LPC_SWM->PINENABLE1 &= ~(0x3 << 3); - // set default frequency at 100k + + // set default frequency at 100kHz i2c_frequency(obj, 100000); i2c_interface_enable(obj); } @@ -76,7 +72,7 @@ } inline int i2c_stop(i2c_t *obj) { - int timeout = 0; + volatile int timeout = 0; LPC_I2C0->MSTCTL = (1 << 2) | (1 << 0); while ((LPC_I2C0->STAT & ((1 << 0) | (7 << 1))) != ((1 << 0) | (0 << 1))) { @@ -107,14 +103,12 @@ LPC_I2C0->MSTCTL = (1 << 0); // return the data - //return (I2C_DAT(obj) & 0xFF); return (LPC_I2C0->MSTDAT & 0xFF); } void i2c_frequency(i2c_t *obj, int hz) { // No peripheral clock divider on the M0 uint32_t PCLK = SystemCoreClock; - uint32_t clkdiv = PCLK / (hz * 4) - 1; LPC_I2C0->DIV = clkdiv; @@ -123,19 +117,15 @@ int i2c_read(i2c_t *obj, int address, char *data, int length, int stop) { int count, status; - int timeout = 0; i2c_start(obj); - //status = i2c_do_write(obj, (address | 0x01), 1); LPC_I2C0->MSTDAT = (address | 0x01); LPC_I2C0->MSTCTL |= 0x20; - while (!(LPC_I2C0->STAT & (1 << 0))) { - timeout++; - if (timeout > 100000) return -1; - } + if (i2c_wait_SI(obj) == -1) + return -1; + status = ((LPC_I2C0->STAT >> 1) & (0x07)); - if (status != 0x01) { i2c_stop(obj); return I2C_ERROR_NO_SLAVE; @@ -143,39 +133,27 @@ // Read in all except last byte for (count = 0; count < (length - 1); count++) { - //int value = i2c_do_read(obj, 0); - while (!(LPC_I2C0->STAT & (1 << 0))) { - timeout++; - if (timeout > 100000) return -1; - } - if (!0) - LPC_I2C0->MSTCTL = (1 << 0); - data[count] = (LPC_I2C0->MSTDAT & 0xFF); - // - status = ((LPC_I2C0->STAT >> 1) & (0x07)); + if (i2c_wait_SI(obj) == -1) + return -1; + LPC_I2C0->MSTCTL = (1 << 0); + data[count] = (LPC_I2C0->MSTDAT & 0xFF); + status = ((LPC_I2C0->STAT >> 1) & (0x07)); if (status != 0x00) { i2c_stop(obj); return count; } - //data[count] = (char) value; } // read in last byte - //int value = i2c_do_read(obj, 1); - while (!(LPC_I2C0->STAT & (1 << 0))) { - timeout++; - if (timeout > 100000) return -1; - } + if (i2c_wait_SI(obj) == -1) + return -1; + data[count] = (LPC_I2C0->MSTDAT & 0xFF); - // status = i2c_status(obj); if (status != 0x01) { i2c_stop(obj); return length - 1; } - - //data[count] = (char) value; - // If not repeated start, send stop. if (stop) { i2c_stop(obj); @@ -188,35 +166,27 @@ int i2c_write(i2c_t *obj, int address, const char *data, int length, int stop) { int i, status; - int timeout = 0; i2c_start(obj); - //status = i2c_do_write(obj, (address & 0xFE), 1); LPC_I2C0->MSTDAT = (address & 0xFE); LPC_I2C0->MSTCTL |= 0x20; - // wait and return status - while (!(LPC_I2C0->STAT & (1 << 0))) { - timeout++; - if (timeout > 100000) return -1; - } + if (i2c_wait_SI(obj) == -1) + return -1; + status = ((LPC_I2C0->STAT >> 1) & (0x07)); - if (status != 0x02) { i2c_stop(obj); return I2C_ERROR_NO_SLAVE; } for (i=0; i<length; i++) { - //status = i2c_do_write(obj, data[i], 0); LPC_I2C0->MSTDAT = data[i]; LPC_I2C0->MSTCTL = (1 << 0); - // wait and return status - while (!(LPC_I2C0->STAT & (1 << 0))) { - timeout++; - if (timeout > 100000) return -1; - } - status = ((LPC_I2C0->STAT >> 1) & (0x07)); + if (i2c_wait_SI(obj) == -1) + return -1; + + status = ((LPC_I2C0->STAT >> 1) & (0x07)); if (status != 0x02) { i2c_stop(obj); return i; @@ -242,17 +212,9 @@ } int i2c_byte_write(i2c_t *obj, int data) { - int ack; - int status = i2c_do_write(obj, (data & 0xFF), 0); - - switch(status) { - case 2: - ack = 1; - break; - default: - ack = 0; - break; + if (i2c_do_write(obj, (data & 0xFF), 0) == 2) { + return 1; + } else { + return 0; } - - return ack; }