Skip to content

MIMXRT1050_EVK: Update the I2C driver #7823

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 21, 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
29 changes: 16 additions & 13 deletions targets/TARGET_NXP/TARGET_MCUXpresso_MCUS/TARGET_IMX/i2c_api.c
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ void i2c_init(i2c_t *obj, PinName sda, PinName scl)
uint32_t i2c_sda = pinmap_peripheral(sda, PinMap_I2C_SDA);
uint32_t i2c_scl = pinmap_peripheral(scl, PinMap_I2C_SCL);
obj->instance = pinmap_merge(i2c_sda, i2c_scl);
obj->next_repeated_start = 0;

MBED_ASSERT((int)obj->instance != NC);

lpi2c_master_config_t master_config;
Expand All @@ -50,6 +50,9 @@ void i2c_init(i2c_t *obj, PinName sda, PinName scl)
pinmap_pinout(sda, PinMap_I2C_SDA);
pinmap_pinout(scl, PinMap_I2C_SCL);

pin_mode(sda, PullUp_22K);
pin_mode(scl, PullUp_22K);

pin_mode_opendrain(sda, true);
pin_mode_opendrain(scl, true);
}
Expand All @@ -65,7 +68,6 @@ int i2c_start(i2c_t *obj)

int i2c_stop(i2c_t *obj)
{
obj->next_repeated_start = 0;
if (LPI2C_MasterStop(i2c_addrs[obj->instance]) != kStatus_Success) {
return 1;
}
Expand All @@ -78,7 +80,7 @@ void i2c_frequency(i2c_t *obj, int hz)
uint32_t busClock;

busClock = i2c_get_clock();
LPI2C_MasterSetBaudRate(i2c_addrs[obj->instance], hz, busClock);
LPI2C_MasterSetBaudRate(i2c_addrs[obj->instance], busClock, hz);
}

int i2c_read(i2c_t *obj, int address, char *data, int length, int stop)
Expand All @@ -92,13 +94,9 @@ int i2c_read(i2c_t *obj, int address, char *data, int length, int stop)
master_xfer.direction = kLPI2C_Read;
master_xfer.data = (uint8_t *)data;
master_xfer.dataSize = length;
if (obj->next_repeated_start) {
master_xfer.flags |= kLPI2C_TransferRepeatedStartFlag;
}
if (!stop) {
master_xfer.flags |= kLPI2C_TransferNoStopFlag;
}
obj->next_repeated_start = master_xfer.flags & kLPI2C_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
Expand All @@ -120,11 +118,20 @@ int i2c_write(i2c_t *obj, int address, const char *data, int length, int stop)
return I2C_ERROR_NO_SLAVE;
}

/* Wait till START has been flushed out of the FIFO */
while (!(base->MSR & kLPI2C_MasterBusBusyFlag)) {
}

/* Send the STOP signal */
base->MTDR = LPI2C_MTDR_CMD(0x2U);

/* Wait till STOP has been sent successfully */
while (!(base->MSR & kLPI2C_MasterStopDetectFlag)) {
}

if (base->MSR & kLPI2C_MasterNackDetectFlag) {
i2c_stop(obj);
return I2C_ERROR_NO_SLAVE;
} else {
i2c_stop(obj);
return length;
}
}
Expand All @@ -134,13 +141,9 @@ int i2c_write(i2c_t *obj, int address, const char *data, int length, int stop)
master_xfer.direction = kLPI2C_Write;
master_xfer.data = (uint8_t *)data;
master_xfer.dataSize = length;
if (obj->next_repeated_start) {
master_xfer.flags |= kLPI2C_TransferRepeatedStartFlag;
}
if (!stop) {
master_xfer.flags |= kLPI2C_TransferNoStopFlag;
}
obj->next_repeated_start = master_xfer.flags & kLPI2C_TransferNoStopFlag ? 1 : 0;

if (LPI2C_MasterTransferBlocking(base, &master_xfer) != kStatus_Success) {
return I2C_ERROR_NO_SLAVE;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,6 @@ struct analogin_s {

struct i2c_s {
uint32_t instance;
uint8_t next_repeated_start;
};

struct spi_s {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,8 @@ void pin_function(PinName pin, int function)
}

/* Write to the mux register */
*((volatile uint32_t *)muxregister) = IOMUXC_SW_MUX_CTL_PAD_MUX_MODE(function);
*((volatile uint32_t *)muxregister) = IOMUXC_SW_MUX_CTL_PAD_MUX_MODE(function) |
IOMUXC_SW_MUX_CTL_PAD_SION((function >> SION_BIT_SHIFT) & 0x1);

/* If required write to the input daisy register */
daisyregister = (function >> DAISY_REG_SHIFT) & 0xFFF;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ typedef enum {
#define STDIO_UART_RX USBRX
#define STDIO_UART UART_1

#define SION_BIT_SHIFT (3)
#define DAISY_REG_SHIFT (4)
#define DAISY_REG_VALUE_SHIFT (16)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,12 +39,12 @@ const PinMap PinMap_DAC[] = {

/************I2C***************/
const PinMap PinMap_I2C_SDA[] = {
{GPIO_AD_B1_01, I2C_1, ((1U << DAISY_REG_VALUE_SHIFT) | (0x4D0 << DAISY_REG_SHIFT) | 3)},
{GPIO_AD_B1_01, I2C_1, ((1U << DAISY_REG_VALUE_SHIFT) | (0x4D0 << DAISY_REG_SHIFT) | (1U << SION_BIT_SHIFT) | 3)},
{NC , NC , 0}
};

const PinMap PinMap_I2C_SCL[] = {
{GPIO_AD_B1_00, I2C_1, ((1U << DAISY_REG_VALUE_SHIFT) | (0x4CC << DAISY_REG_SHIFT) | 3)},
{GPIO_AD_B1_00, I2C_1, ((1U << DAISY_REG_VALUE_SHIFT) | (0x4CC << DAISY_REG_SHIFT) | (1U << SION_BIT_SHIFT) | 3)},
{NC , NC , 0}
};

Expand Down