Skip to content

Commit cb8d09a

Browse files
author
Cruz Monrreal
authored
Merge pull request #7843 from codeauroraforum/MXRT_Fix_I2C_Byte_Transfer
MIMXRT1050: Fix I2C Byte transfer functions
2 parents 603c4f9 + f71004c commit cb8d09a

File tree

2 files changed

+49
-21
lines changed

2 files changed

+49
-21
lines changed

targets/TARGET_NXP/TARGET_MCUXpresso_MCUS/TARGET_IMX/i2c_api.c

Lines changed: 48 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,6 @@
2323
#include "fsl_lpi2c.h"
2424
#include "PeripheralPins.h"
2525

26-
/* 7 bit IIC addr - R/W flag not included */
27-
static int i2c_address = 0;
2826
/* Array of I2C peripheral base address. */
2927
static LPI2C_Type *const i2c_addrs[] = LPI2C_BASE_PTRS;
3028

@@ -59,11 +57,24 @@ void i2c_init(i2c_t *obj, PinName sda, PinName scl)
5957

6058
int i2c_start(i2c_t *obj)
6159
{
62-
if (LPI2C_MasterStart(i2c_addrs[obj->instance], 0, kLPI2C_Write) != kStatus_Success) {
63-
return 1;
64-
}
60+
LPI2C_Type *base = i2c_addrs[obj->instance];
61+
int status = 0;
6562

66-
return 0;
63+
obj->address_set = 0;
64+
65+
/* Clear all flags. */
66+
LPI2C_MasterClearStatusFlags(base, kLPI2C_MasterEndOfPacketFlag |
67+
kLPI2C_MasterStopDetectFlag |
68+
kLPI2C_MasterNackDetectFlag |
69+
kLPI2C_MasterArbitrationLostFlag |
70+
kLPI2C_MasterFifoErrFlag |
71+
kLPI2C_MasterPinLowTimeoutFlag |
72+
kLPI2C_MasterDataMatchFlag);
73+
74+
/* Turn off auto-stop option. */
75+
base->MCFGR1 &= ~LPI2C_MCFGR1_AUTOSTOP_MASK;
76+
77+
return status;
6778
}
6879

6980
int i2c_stop(i2c_t *obj)
@@ -72,6 +83,8 @@ int i2c_stop(i2c_t *obj)
7283
return 1;
7384
}
7485

86+
obj->address_set = 0;
87+
7588
return 0;
7689
}
7790

@@ -88,7 +101,6 @@ int i2c_read(i2c_t *obj, int address, char *data, int length, int stop)
88101
LPI2C_Type *base = i2c_addrs[obj->instance];
89102
lpi2c_master_transfer_t master_xfer;
90103

91-
i2c_address = address >> 1;
92104
memset(&master_xfer, 0, sizeof(master_xfer));
93105
master_xfer.slaveAddress = address >> 1;
94106
master_xfer.direction = kLPI2C_Read;
@@ -164,11 +176,10 @@ int i2c_byte_read(i2c_t *obj, int last)
164176
lpi2c_master_transfer_t master_xfer;
165177

166178
memset(&master_xfer, 0, sizeof(master_xfer));
167-
master_xfer.slaveAddress = i2c_address;
168179
master_xfer.direction = kLPI2C_Read;
169180
master_xfer.data = &data;
170181
master_xfer.dataSize = 1;
171-
master_xfer.flags = kLPI2C_TransferNoStopFlag;
182+
master_xfer.flags = kLPI2C_TransferNoStopFlag | kLPI2C_TransferNoStartFlag;
172183

173184
if (LPI2C_MasterTransferBlocking(base, &master_xfer) != kStatus_Success) {
174185
return I2C_ERROR_NO_SLAVE;
@@ -179,21 +190,37 @@ int i2c_byte_read(i2c_t *obj, int last)
179190
int i2c_byte_write(i2c_t *obj, int data)
180191
{
181192
LPI2C_Type *base = i2c_addrs[obj->instance];
182-
lpi2c_master_transfer_t master_xfer;
183-
status_t ret_value;
184-
185-
memset(&master_xfer, 0, sizeof(master_xfer));
186-
master_xfer.slaveAddress = i2c_address;
187-
master_xfer.direction = kLPI2C_Write;
188-
master_xfer.data = &data;
189-
master_xfer.dataSize = 1;
190-
master_xfer.flags = kLPI2C_TransferNoStopFlag;
193+
uint32_t status;
194+
size_t txCount;
195+
size_t txFifoSize = FSL_FEATURE_LPI2C_FIFO_SIZEn(base);
196+
197+
/* Clear error flags. */
198+
LPI2C_MasterClearStatusFlags(base, LPI2C_MasterGetStatusFlags(base));
199+
200+
/* Wait till there is room in the TX FIFO */
201+
do {
202+
/* Get the number of words in the tx fifo and compute empty slots. */
203+
LPI2C_MasterGetFifoCounts(base, NULL, &txCount);
204+
txCount = txFifoSize - txCount;
205+
} while (!txCount);
206+
207+
if (!obj->address_set) {
208+
obj->address_set = 1;
209+
/* Issue start command. */
210+
base->MTDR = LPI2C_MTDR_CMD(0x4U) | LPI2C_MTDR_DATA(data);
211+
} else {
212+
/* Write byte into LPI2C master data register. */
213+
base->MTDR = data;
214+
}
191215

192-
ret_value = LPI2C_MasterTransferBlocking(base, &master_xfer);
216+
/* Wait till data is pushed out of the FIFO */
217+
while (!(base->MSR & kLPI2C_MasterTxReadyFlag)) {
218+
}
193219

194-
if (ret_value == kStatus_Success) {
220+
status = LPI2C_MasterCheckAndClearError(base, LPI2C_MasterGetStatusFlags(base));
221+
if (status == kStatus_Success) {
195222
return 1;
196-
} else if (ret_value == kStatus_LPI2C_Nak) {
223+
} else if (status == kStatus_LPI2C_Nak) {
197224
return 0;
198225
} else {
199226
return 2;

targets/TARGET_NXP/TARGET_MCUXpresso_MCUS/TARGET_IMX/objects.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ struct analogin_s {
5050

5151
struct i2c_s {
5252
uint32_t instance;
53+
uint8_t address_set;
5354
};
5455

5556
struct spi_s {

0 commit comments

Comments
 (0)