Skip to content

Fix MCUXpresso LPC I2C driver #6647

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 2 commits into from
Apr 17, 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
70 changes: 43 additions & 27 deletions targets/TARGET_NXP/TARGET_MCUXpresso_MCUS/TARGET_LPC/i2c_api.c
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,6 @@
#include "fsl_i2c.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 I2C_Type *const i2c_addrs[] = I2C_BASE_PTRS;

Expand Down Expand Up @@ -98,17 +95,21 @@ int i2c_start(i2c_t *obj)
I2C_Type *base = i2c_addrs[obj->instance];
uint32_t status;

do
{
do {
status = I2C_GetStatusFlags(base);
} while ((status & I2C_STAT_MSTPENDING_MASK) == 0);

/* Clear controller state. */
I2C_MasterClearStatusFlags(base, I2C_STAT_MSTARBLOSS_MASK | I2C_STAT_MSTSTSTPERR_MASK);

/* Start the transfer */
base->MSTDAT = 0;
base->MSTCTL = I2C_MSTCTL_MSTSTART_MASK;

do {
status = I2C_GetStatusFlags(base);
} while ((status & I2C_STAT_MSTPENDING_MASK) == 0);

return 0;
}

Expand All @@ -117,8 +118,7 @@ int i2c_stop(i2c_t *obj)
I2C_Type *base = i2c_addrs[obj->instance];
uint32_t status;

do
{
do {
status = I2C_GetStatusFlags(base);
} while ((status & I2C_STAT_MSTPENDING_MASK) == 0);

Expand All @@ -127,6 +127,10 @@ int i2c_stop(i2c_t *obj)

base->MSTCTL = I2C_MSTCTL_MSTSTOP_MASK;

do {
status = I2C_GetStatusFlags(base);
} while ((status & I2C_STAT_MSTPENDING_MASK) == 0);

return 0;
}

Expand All @@ -140,7 +144,6 @@ int i2c_read(i2c_t *obj, int address, char *data, int length, int stop)
I2C_Type *base = i2c_addrs[obj->instance];
i2c_master_transfer_t master_xfer;

i2c_address = address >> 1;
memset(&master_xfer, 0, sizeof(master_xfer));
master_xfer.slaveAddress = address >> 1;
master_xfer.direction = kI2C_Read;
Expand All @@ -154,9 +157,6 @@ int i2c_read(i2c_t *obj, int address, char *data, int length, int stop)
}
obj->next_repeated_start = master_xfer.flags & kI2C_TransferNoStopFlag ? 1 : 0;

/* The below function will issue a STOP signal at the end of the transfer.
* This is required by the hardware in order to receive the last byte
*/
if (I2C_MasterTransferBlocking(base, &master_xfer) != kStatus_Success) {
return I2C_ERROR_NO_SLAVE;
}
Expand Down Expand Up @@ -212,33 +212,49 @@ int i2c_byte_read(i2c_t *obj, int last)
{
uint8_t data;
I2C_Type *base = i2c_addrs[obj->instance];
i2c_master_transfer_t master_xfer;
uint32_t status;

memset(&master_xfer, 0, sizeof(master_xfer));
master_xfer.slaveAddress = i2c_address;
master_xfer.direction = kI2C_Read;
master_xfer.data = &data;
master_xfer.dataSize = 1;
do {
status = I2C_GetStatusFlags(base);
} while ((status & I2C_STAT_MSTPENDING_MASK) == 0);

if (I2C_MasterTransferBlocking(base, &master_xfer) != kStatus_Success) {
return I2C_ERROR_NO_SLAVE;
data = base->MSTDAT;

if (!last) {
base->MSTCTL = I2C_MSTCTL_MSTCONTINUE_MASK; //ACK and Continue
}

return data;
}

int i2c_byte_write(i2c_t *obj, int data)
{
status_t ret_value;
I2C_Type *base = i2c_addrs[obj->instance];
uint32_t status;
int ret_value = 1;

ret_value = I2C_MasterWriteBlocking(i2c_addrs[obj->instance], (uint8_t *)(&data), 1, kI2C_TransferNoStopFlag);
// write the data
base->MSTDAT = data;

if (ret_value == kStatus_Success) {
return 1;
} else if (ret_value == kStatus_I2C_Nak) {
return 0;
} else {
return 2;
base->MSTCTL = I2C_MSTCTL_MSTCONTINUE_MASK;

do {
status = I2C_GetStatusFlags(base);
} while ((status & I2C_STAT_MSTPENDING_MASK) == 0);


/* Check if arbitration lost */
if (status & I2C_STAT_MSTARBLOSS_MASK) {
I2C_MasterClearStatusFlags(base, I2C_STAT_MSTARBLOSS_MASK);
ret_value = 2;
}

/* Check if no acknowledgement (NAK) */
if (((status & I2C_STAT_MSTSTATE_MASK) >> I2C_STAT_MSTSTATE_SHIFT) == I2C_STAT_MSTCODE_NACKDAT) {
ret_value = 0;
}

return ret_value;
}


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,8 @@ status_t I2C_MasterStart(I2C_Type *base, uint8_t address, i2c_direction_t direct
/* Start the transfer */
base->MSTCTL = I2C_MSTCTL_MSTSTART_MASK;

I2C_PendingStatusWait(base);

return kStatus_Success;
}

Expand All @@ -171,6 +173,9 @@ status_t I2C_MasterStop(I2C_Type *base)
I2C_PendingStatusWait(base);

base->MSTCTL = I2C_MSTCTL_MSTSTOP_MASK;

I2C_PendingStatusWait(base);

return kStatus_Success;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -201,6 +201,12 @@ status_t I2C_MasterStart(I2C_Type *base, uint8_t address, i2c_direction_t direct
/* Start the transfer */
base->MSTCTL = I2C_MSTCTL_MSTSTART_MASK;

result = I2C_PendingStatusWait(base);
if (result == kStatus_I2C_Timeout)
{
return kStatus_I2C_Timeout;
}

return kStatus_Success;
}

Expand All @@ -214,6 +220,13 @@ status_t I2C_MasterStop(I2C_Type *base)
}

base->MSTCTL = I2C_MSTCTL_MSTSTOP_MASK;

result = I2C_PendingStatusWait(base);
if (result == kStatus_I2C_Timeout)
{
return kStatus_I2C_Timeout;
}

return kStatus_Success;
}

Expand Down