Skip to content

Commit 5c45fa7

Browse files
author
Arto Kinnunen
authored
Merge pull request #11003 from NXPmicro/Fix_LPC_I2C
LPC MCUXpresso: Remove extra I2C transaction on byte write
2 parents 1a9020a + aef60d7 commit 5c45fa7

File tree

2 files changed

+19
-19
lines changed

2 files changed

+19
-19
lines changed

targets/TARGET_NXP/TARGET_MCUXpresso_MCUS/TARGET_LPC/i2c_api.c

Lines changed: 18 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ void i2c_init(i2c_t *obj, PinName sda, PinName scl)
3232
uint32_t i2c_scl = pinmap_peripheral(scl, PinMap_I2C_SCL);
3333
obj->instance = pinmap_merge(i2c_sda, i2c_scl);
3434
obj->next_repeated_start = 0;
35+
obj->issue_start = 0;
3536
MBED_ASSERT((int)obj->instance != NC);
3637

3738
i2c_master_config_t master_config;
@@ -92,23 +93,7 @@ void i2c_init(i2c_t *obj, PinName sda, PinName scl)
9293

9394
int i2c_start(i2c_t *obj)
9495
{
95-
I2C_Type *base = i2c_addrs[obj->instance];
96-
uint32_t status;
97-
98-
do {
99-
status = I2C_GetStatusFlags(base);
100-
} while ((status & I2C_STAT_MSTPENDING_MASK) == 0);
101-
102-
/* Clear controller state. */
103-
I2C_MasterClearStatusFlags(base, I2C_STAT_MSTARBLOSS_MASK | I2C_STAT_MSTSTSTPERR_MASK);
104-
105-
/* Start the transfer */
106-
base->MSTDAT = 0;
107-
base->MSTCTL = I2C_MSTCTL_MSTSTART_MASK;
108-
109-
do {
110-
status = I2C_GetStatusFlags(base);
111-
} while ((status & I2C_STAT_MSTPENDING_MASK) == 0);
96+
obj->issue_start = 1;
11297

11398
return 0;
11499
}
@@ -131,6 +116,8 @@ int i2c_stop(i2c_t *obj)
131116
status = I2C_GetStatusFlags(base);
132117
} while ((status & I2C_STAT_MSTPENDING_MASK) == 0);
133118

119+
obj->issue_start = 0;
120+
134121
return 0;
135122
}
136123

@@ -236,12 +223,24 @@ int i2c_byte_write(i2c_t *obj, int data)
236223
// write the data
237224
base->MSTDAT = data;
238225

239-
base->MSTCTL = I2C_MSTCTL_MSTCONTINUE_MASK;
240-
241226
do {
242227
status = I2C_GetStatusFlags(base);
243228
} while ((status & I2C_STAT_MSTPENDING_MASK) == 0);
244229

230+
/* Clear controller state. */
231+
I2C_MasterClearStatusFlags(base, I2C_STAT_MSTARBLOSS_MASK | I2C_STAT_MSTSTSTPERR_MASK);
232+
233+
if (obj->issue_start) {
234+
base->MSTCTL = I2C_MSTCTL_MSTSTART_MASK;
235+
/* Clear the flag */
236+
obj->issue_start = 0;
237+
} else {
238+
base->MSTCTL = I2C_MSTCTL_MSTCONTINUE_MASK;
239+
}
240+
241+
do {
242+
status = I2C_GetStatusFlags(base);
243+
} while ((status & I2C_STAT_MSTPENDING_MASK) == 0);
245244

246245
/* Check if arbitration lost */
247246
if (status & I2C_STAT_MSTARBLOSS_MASK) {

targets/TARGET_NXP/TARGET_MCUXpresso_MCUS/TARGET_LPC/objects.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@ struct analogin_s {
5252
struct i2c_s {
5353
uint32_t instance;
5454
uint8_t next_repeated_start;
55+
uint8_t issue_start;
5556
};
5657

5758
struct spi_s {

0 commit comments

Comments
 (0)