Skip to content

Commit 518b072

Browse files
authored
Merge pull request #3502 from NXPmicro/I2C_Zerobyte_Write
MCUXpresso I2C: Handle 0 byte write
2 parents 306cc1a + 1280dfc commit 518b072

File tree

1 file changed

+20
-1
lines changed
  • targets/TARGET_Freescale/TARGET_MCUXpresso_MCUS/api

1 file changed

+20
-1
lines changed

targets/TARGET_Freescale/TARGET_MCUXpresso_MCUS/api/i2c_api.c

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,8 +81,8 @@ int i2c_start(i2c_t *obj)
8181

8282
int i2c_stop(i2c_t *obj)
8383
{
84+
obj->next_repeated_start = 0;
8485
if (I2C_MasterStop(i2c_addrs[obj->instance]) != kStatus_Success) {
85-
obj->next_repeated_start = 0;
8686
return 1;
8787
}
8888

@@ -131,6 +131,25 @@ int i2c_write(i2c_t *obj, int address, const char *data, int length, int stop)
131131
I2C_Type *base = i2c_addrs[obj->instance];
132132
i2c_master_transfer_t master_xfer;
133133

134+
if (length == 0) {
135+
if (I2C_MasterStart(base, address >> 1, kI2C_Write) != kStatus_Success) {
136+
return I2C_ERROR_NO_SLAVE;
137+
}
138+
139+
while (!(base->S & kI2C_IntPendingFlag)) {
140+
}
141+
142+
base->S = kI2C_IntPendingFlag;
143+
144+
if (base->S & kI2C_ReceiveNakFlag) {
145+
i2c_stop(obj);
146+
return I2C_ERROR_NO_SLAVE;
147+
} else {
148+
i2c_stop(obj);
149+
return length;
150+
}
151+
}
152+
134153
memset(&master_xfer, 0, sizeof(master_xfer));
135154
master_xfer.slaveAddress = address >> 1;
136155
master_xfer.direction = kI2C_Write;

0 commit comments

Comments
 (0)