Skip to content

Commit 64e5eb0

Browse files
committed
MIMXRT1050_EVK: Update the I2C driver
1. Remove the repeated_start flag and code as this is not needed for the LPI2C module 2. Enable the SION bit on the I2C pins 3. Enable 22K Pullup option of the I2C pins 4. Update the 0 byte write implementation to ensure the START command gets flushed out of the FIFO Signed-off-by: Mahesh Mahadevan <[email protected]>
1 parent 871ee09 commit 64e5eb0

File tree

5 files changed

+21
-17
lines changed

5 files changed

+21
-17
lines changed

targets/TARGET_NXP/TARGET_MCUXpresso_MCUS/TARGET_IMX/i2c_api.c

Lines changed: 16 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ void i2c_init(i2c_t *obj, PinName sda, PinName scl)
3737
uint32_t i2c_sda = pinmap_peripheral(sda, PinMap_I2C_SDA);
3838
uint32_t i2c_scl = pinmap_peripheral(scl, PinMap_I2C_SCL);
3939
obj->instance = pinmap_merge(i2c_sda, i2c_scl);
40-
obj->next_repeated_start = 0;
40+
4141
MBED_ASSERT((int)obj->instance != NC);
4242

4343
lpi2c_master_config_t master_config;
@@ -50,6 +50,9 @@ void i2c_init(i2c_t *obj, PinName sda, PinName scl)
5050
pinmap_pinout(sda, PinMap_I2C_SDA);
5151
pinmap_pinout(scl, PinMap_I2C_SCL);
5252

53+
pin_mode(sda, PullUp_22K);
54+
pin_mode(scl, PullUp_22K);
55+
5356
pin_mode_opendrain(sda, true);
5457
pin_mode_opendrain(scl, true);
5558
}
@@ -65,7 +68,6 @@ int i2c_start(i2c_t *obj)
6568

6669
int i2c_stop(i2c_t *obj)
6770
{
68-
obj->next_repeated_start = 0;
6971
if (LPI2C_MasterStop(i2c_addrs[obj->instance]) != kStatus_Success) {
7072
return 1;
7173
}
@@ -78,7 +80,7 @@ void i2c_frequency(i2c_t *obj, int hz)
7880
uint32_t busClock;
7981

8082
busClock = i2c_get_clock();
81-
LPI2C_MasterSetBaudRate(i2c_addrs[obj->instance], hz, busClock);
83+
LPI2C_MasterSetBaudRate(i2c_addrs[obj->instance], busClock, hz);
8284
}
8385

8486
int i2c_read(i2c_t *obj, int address, char *data, int length, int stop)
@@ -92,13 +94,9 @@ int i2c_read(i2c_t *obj, int address, char *data, int length, int stop)
9294
master_xfer.direction = kLPI2C_Read;
9395
master_xfer.data = (uint8_t *)data;
9496
master_xfer.dataSize = length;
95-
if (obj->next_repeated_start) {
96-
master_xfer.flags |= kLPI2C_TransferRepeatedStartFlag;
97-
}
9897
if (!stop) {
9998
master_xfer.flags |= kLPI2C_TransferNoStopFlag;
10099
}
101-
obj->next_repeated_start = master_xfer.flags & kLPI2C_TransferNoStopFlag ? 1 : 0;
102100

103101
/* The below function will issue a STOP signal at the end of the transfer.
104102
* This is required by the hardware in order to receive the last byte
@@ -120,11 +118,20 @@ int i2c_write(i2c_t *obj, int address, const char *data, int length, int stop)
120118
return I2C_ERROR_NO_SLAVE;
121119
}
122120

121+
/* Wait till START has been flushed out of the FIFO */
122+
while (!(base->MSR & kLPI2C_MasterBusBusyFlag)) {
123+
}
124+
125+
/* Send the STOP signal */
126+
base->MTDR = LPI2C_MTDR_CMD(0x2U);
127+
128+
/* Wait till STOP has been sent successfully */
129+
while (!(base->MSR & kLPI2C_MasterStopDetectFlag)) {
130+
}
131+
123132
if (base->MSR & kLPI2C_MasterNackDetectFlag) {
124-
i2c_stop(obj);
125133
return I2C_ERROR_NO_SLAVE;
126134
} else {
127-
i2c_stop(obj);
128135
return length;
129136
}
130137
}
@@ -134,13 +141,9 @@ int i2c_write(i2c_t *obj, int address, const char *data, int length, int stop)
134141
master_xfer.direction = kLPI2C_Write;
135142
master_xfer.data = (uint8_t *)data;
136143
master_xfer.dataSize = length;
137-
if (obj->next_repeated_start) {
138-
master_xfer.flags |= kLPI2C_TransferRepeatedStartFlag;
139-
}
140144
if (!stop) {
141145
master_xfer.flags |= kLPI2C_TransferNoStopFlag;
142146
}
143-
obj->next_repeated_start = master_xfer.flags & kLPI2C_TransferNoStopFlag ? 1 : 0;
144147

145148
if (LPI2C_MasterTransferBlocking(base, &master_xfer) != kStatus_Success) {
146149
return I2C_ERROR_NO_SLAVE;

targets/TARGET_NXP/TARGET_MCUXpresso_MCUS/TARGET_IMX/objects.h

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

5151
struct i2c_s {
5252
uint32_t instance;
53-
uint8_t next_repeated_start;
5453
};
5554

5655
struct spi_s {

targets/TARGET_NXP/TARGET_MCUXpresso_MCUS/TARGET_IMX/pinmap.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,8 @@ void pin_function(PinName pin, int function)
5454
}
5555

5656
/* Write to the mux register */
57-
*((volatile uint32_t *)muxregister) = IOMUXC_SW_MUX_CTL_PAD_MUX_MODE(function);
57+
*((volatile uint32_t *)muxregister) = IOMUXC_SW_MUX_CTL_PAD_MUX_MODE(function) |
58+
IOMUXC_SW_MUX_CTL_PAD_SION((function >> SION_BIT_SHIFT) & 0x1);
5859

5960
/* If required write to the input daisy register */
6061
daisyregister = (function >> DAISY_REG_SHIFT) & 0xFFF;

targets/TARGET_NXP/TARGET_MCUXpresso_MCUS/TARGET_MIMXRT1050/TARGET_EVK/PeripheralNames.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ typedef enum {
3737
#define STDIO_UART_RX USBRX
3838
#define STDIO_UART UART_1
3939

40+
#define SION_BIT_SHIFT (3)
4041
#define DAISY_REG_SHIFT (4)
4142
#define DAISY_REG_VALUE_SHIFT (16)
4243

targets/TARGET_NXP/TARGET_MCUXpresso_MCUS/TARGET_MIMXRT1050/TARGET_EVK/PeripheralPins.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,12 +39,12 @@ const PinMap PinMap_DAC[] = {
3939

4040
/************I2C***************/
4141
const PinMap PinMap_I2C_SDA[] = {
42-
{GPIO_AD_B1_01, I2C_1, ((1U << DAISY_REG_VALUE_SHIFT) | (0x4D0 << DAISY_REG_SHIFT) | 3)},
42+
{GPIO_AD_B1_01, I2C_1, ((1U << DAISY_REG_VALUE_SHIFT) | (0x4D0 << DAISY_REG_SHIFT) | (1U << SION_BIT_SHIFT) | 3)},
4343
{NC , NC , 0}
4444
};
4545

4646
const PinMap PinMap_I2C_SCL[] = {
47-
{GPIO_AD_B1_00, I2C_1, ((1U << DAISY_REG_VALUE_SHIFT) | (0x4CC << DAISY_REG_SHIFT) | 3)},
47+
{GPIO_AD_B1_00, I2C_1, ((1U << DAISY_REG_VALUE_SHIFT) | (0x4CC << DAISY_REG_SHIFT) | (1U << SION_BIT_SHIFT) | 3)},
4848
{NC , NC , 0}
4949
};
5050

0 commit comments

Comments
 (0)