Skip to content

MIMXRT1050: Fix I2C Byte transfer functions #7843

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Aug 23, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
69 changes: 48 additions & 21 deletions targets/TARGET_NXP/TARGET_MCUXpresso_MCUS/TARGET_IMX/i2c_api.c
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,6 @@
#include "fsl_lpi2c.h"
#include "PeripheralPins.h"

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

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

int i2c_start(i2c_t *obj)
{
if (LPI2C_MasterStart(i2c_addrs[obj->instance], 0, kLPI2C_Write) != kStatus_Success) {
return 1;
}
LPI2C_Type *base = i2c_addrs[obj->instance];
int status = 0;

return 0;
obj->address_set = 0;

/* Clear all flags. */
LPI2C_MasterClearStatusFlags(base, kLPI2C_MasterEndOfPacketFlag |
kLPI2C_MasterStopDetectFlag |
kLPI2C_MasterNackDetectFlag |
kLPI2C_MasterArbitrationLostFlag |
kLPI2C_MasterFifoErrFlag |
kLPI2C_MasterPinLowTimeoutFlag |
kLPI2C_MasterDataMatchFlag);

/* Turn off auto-stop option. */
base->MCFGR1 &= ~LPI2C_MCFGR1_AUTOSTOP_MASK;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this ever turned on again, or is it simply not needed anymore?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It is not needed.


return status;
}

int i2c_stop(i2c_t *obj)
Expand All @@ -72,6 +83,8 @@ int i2c_stop(i2c_t *obj)
return 1;
}

obj->address_set = 0;

return 0;
}

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

i2c_address = address >> 1;
memset(&master_xfer, 0, sizeof(master_xfer));
master_xfer.slaveAddress = address >> 1;
master_xfer.direction = kLPI2C_Read;
Expand Down Expand Up @@ -164,11 +176,10 @@ int i2c_byte_read(i2c_t *obj, int last)
lpi2c_master_transfer_t master_xfer;

memset(&master_xfer, 0, sizeof(master_xfer));
master_xfer.slaveAddress = i2c_address;
master_xfer.direction = kLPI2C_Read;
master_xfer.data = &data;
master_xfer.dataSize = 1;
master_xfer.flags = kLPI2C_TransferNoStopFlag;
master_xfer.flags = kLPI2C_TransferNoStopFlag | kLPI2C_TransferNoStartFlag;

if (LPI2C_MasterTransferBlocking(base, &master_xfer) != kStatus_Success) {
return I2C_ERROR_NO_SLAVE;
Expand All @@ -179,21 +190,37 @@ int i2c_byte_read(i2c_t *obj, int last)
int i2c_byte_write(i2c_t *obj, int data)
{
LPI2C_Type *base = i2c_addrs[obj->instance];
lpi2c_master_transfer_t master_xfer;
status_t ret_value;

memset(&master_xfer, 0, sizeof(master_xfer));
master_xfer.slaveAddress = i2c_address;
master_xfer.direction = kLPI2C_Write;
master_xfer.data = &data;
master_xfer.dataSize = 1;
master_xfer.flags = kLPI2C_TransferNoStopFlag;
uint32_t status;
size_t txCount;
size_t txFifoSize = FSL_FEATURE_LPI2C_FIFO_SIZEn(base);

/* Clear error flags. */
LPI2C_MasterClearStatusFlags(base, LPI2C_MasterGetStatusFlags(base));

/* Wait till there is room in the TX FIFO */
do {
/* Get the number of words in the tx fifo and compute empty slots. */
LPI2C_MasterGetFifoCounts(base, NULL, &txCount);
txCount = txFifoSize - txCount;
} while (!txCount);

if (!obj->address_set) {
obj->address_set = 1;
/* Issue start command. */
base->MTDR = LPI2C_MTDR_CMD(0x4U) | LPI2C_MTDR_DATA(data);
} else {
/* Write byte into LPI2C master data register. */
base->MTDR = data;
}

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

if (ret_value == kStatus_Success) {
status = LPI2C_MasterCheckAndClearError(base, LPI2C_MasterGetStatusFlags(base));
if (status == kStatus_Success) {
return 1;
} else if (ret_value == kStatus_LPI2C_Nak) {
} else if (status == kStatus_LPI2C_Nak) {
return 0;
} else {
return 2;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ struct analogin_s {

struct i2c_s {
uint32_t instance;
uint8_t address_set;
};

struct spi_s {
Expand Down