Reading and writing to Multiple I2C slave using lpc54618

cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

Reading and writing to Multiple I2C slave using lpc54618

1,429 Views
subodhmankar
Contributor II

Hi,

I am using I2C lpc54618 for connecting multiple sensors on the same I2C bus with 3.3K pull-up resistor(3.3v).

I am able to read and write data using I2C (Single slave sensor not multiple)of lpc54618 on flexcomm2 but not flexcomm6.

In pinmux I have initialized port1_12 ,port1_13 and port1_14 as below to use for multiple sensors.

//I2C6 SCL

const uint32_t port1_pin12_config = (

IOCON_PIO_FUNC2 | /* Pin is configured as FC6_TXD_SCL_MISO */

IOCON_PIO_MODE_PULLUP | /* Selects pull-up function */

IOCON_PIO_INV_DI | /* Input function is not inverted */

IOCON_PIO_DIGITAL_EN | /* Enables digital function */

IOCON_PIO_INPFILT_OFF | /* Input filter disabled */

IOCON_PIO_OPENDRAIN_DI /* Open drain is disabled */

);

IOCON_PinMuxSet(IOCON, PORT1_IDX, PIN12_IDX, port1_pin12_config); /* PORT1 PIN12 (coords: N5) is configured as FC6_SCK */

//I2C6 SDA

const uint32_t port1_pin13_config = (

IOCON_PIO_FUNC2 | /* Pin is configured as FC6_TXD_SCL_MISO */

IOCON_PIO_MODE_PULLUP | /* Selects pull-up function */

IOCON_PIO_INV_DI | /* Input function is not inverted */

IOCON_PIO_DIGITAL_EN | /* Enables digital function */

IOCON_PIO_INPFILT_OFF | /* Input filter disabled */

IOCON_PIO_OPENDRAIN_DI /* Open drain is disabled */

);

IOCON_PinMuxSet(IOCON, PORT1_IDX, PIN13_IDX, port1_pin13_config); /* PORT1 PIN13 (coords: N5) is configured as FC6_RXD_SDA_MOSI_DATA */

//INTR pin

const uint32_t port1_pin14_config = (

IOCON_PIO_FUNC0 | /*INTR PIN*/

IOCON_PIO_MODE_PULLUP | /* Selects pull-up function */

IOCON_PIO_INV_DI | /* Input function is not inverted */

IOCON_PIO_DIGITAL_EN | /* Enables digital function */

IOCON_PIO_INPFILT_OFF | /* Input filter disabled */

IOCON_PIO_OPENDRAIN_DI /* Open drain is disabled */

);

IOCON_PinMuxSet(IOCON, PORT1_IDX, PIN14_IDX, port1_pin14_config); /* PORT1 PIN14 (coords: N5) is configured INTR pin */

Similar to port1_14 I have configured port1_15 and port1_16 for other sensors.

I have configured lpc54618 as master as below:-

#define EXAMPLE_I2C6_MASTER_BASE (I2C6_BASE)

#define I2C_MASTER_CLOCK_FREQUENCY (12000000)

#define EXAMPLE_I2C6_MASTER ((I2C_Type *)EXAMPLE_I2C6_MASTER_BASE)

#define I2C_MASTER_SLAVE_ADDR_7BIT SENSOR_I2C_ADDRESS

#define I2C_BAUDRATE (400000) /* 100K */

#define I2C_DATA_LENGTH (8) /* MAX is 256 */

i2c_master_handle_t g_m_handle;

volatile bool g_MasterCompletionFlag = false;

i2c_master_config_t masterConfig;

uint8_t status;

status_t result = kStatus_Success;

uint8_t txBuff[I2C_DATA_LENGTH];

i2c_master_transfer_t masterXfer;

char DataReady;

static void i2c_master_callback(I2C_Type *base, i2c_master_handle_t *handle, status_t status, void *userData)

{

/* Signal transfer success when received success status. */

if (status == kStatus_Success)

{

g_MasterCompletionFlag = true;

}

}

.

.

.

.

CLOCK_AttachClk(kFRO12M_to_FLEXCOMM6);

RESET_PeripheralReset(kFC6_RST_SHIFT_RSTn);

NVIC_SetPriority(FLEXCOMM6_IRQn, 1);

/* Get default configuration for master. */

I2C_MasterGetDefaultConfig(&masterConfig);

masterConfig.baudRate_Bps = I2C_BAUDRATE;

/* Init I2C master. */

I2C_MasterInit(EXAMPLE_I2C6_MASTER, &masterConfig, I2C_MASTER_CLOCK_FREQUENCY);

I2C_MasterTransferCreateHandle(EXAMPLE_I2C6_MASTER, &g_m_handle, i2c_master_callback, NULL);

.

.

.

.

.

Following are the read and write functions for I2C based sensor registers

uint32_t sensor_i2c_register_write(uint8_t register_address, uint8_t value)

{

   status_t reVal = kStatus_Fail;

   txBuff[0] = register_address;

   txBuff[1] = value;

    masterXfer.slaveAddress = I2C_MASTER_SLAVE_ADDR_7BIT;

    masterXfer.direction = kI2C_Write;

    masterXfer.subaddress = 0;

    masterXfer.subaddressSize = 0;

    masterXfer.data = txBuff;

    masterXfer.dataSize = 2;

    masterXfer.flags = kI2C_TransferDefaultFlag;

    reVal = I2C_MasterTransferNonBlocking(EXAMPLE_I2C6_MASTER, &g_m_handle, &masterXfer);

    if (reVal != kStatus_Success)

    {

          return kStatus_Fail;

    }

    /* Wait for transfer completed. */

    while (!g_MasterCompletionFlag)

   {

   }

   g_MasterCompletionFlag = false;

   return kStatus_Success;

}

uint32_t sensor_i2c_register_read(uint8_t first_reg_address, uint8_t * p_destination, uint8_t num_bytes)

{

   status_t reVal = kStatus_Fail;

    txBuff[0] = first_reg_address;

    masterXfer.slaveAddress = I2C_MASTER_SLAVE_ADDR_7BIT;

    masterXfer.direction = kI2C_Write;

    masterXfer.subaddress = 0;

    masterXfer.subaddressSize = 0;

    masterXfer.data = txBuff;

    masterXfer.dataSize = 1;

    masterXfer.flags = kI2C_TransferNoStopFlag;

    reVal = I2C_MasterTransferNonBlocking(EXAMPLE_I2C6_MASTER, &g_m_handle, &masterXfer);

    if (reVal != kStatus_Success)

    {

         return kStatus_Fail;

    }

   /* Wait for transfer completed. */

    while (!g_MasterCompletionFlag)

   {

   }

   g_MasterCompletionFlag = false;

   masterXfer.slaveAddress = I2C_MASTER_SLAVE_ADDR_7BIT;

   masterXfer.direction = kI2C_Read;

   masterXfer.subaddress = 0;

   masterXfer.subaddressSize = 0;

   masterXfer.data = p_destination;

   masterXfer.dataSize = num_bytes;

   masterXfer.flags = kI2C_TransferRepeatedStartFlag;

   reVal = I2C_MasterTransferNonBlocking(EXAMPLE_I2C6_MASTER, &g_m_handle, &masterXfer);

   if (reVal != kStatus_Success)

   {

      return kStatus_Fail;

   }

   /* Wait for transfer completed. */

   while (!g_MasterCompletionFlag)

   {

   }

   g_MasterCompletionFlag = false;

   return reVal;

}

when I try to read data from sensors from flexcomm6( port1_12 and port1_13) no data is given back and MCU stops executing further code.

Could anyone please provide me some pointers for..
1] Are there any special config needed for master to connect to one slave or many slave?
2] are register read/write i2c functions correct?

1 Reply

825 Views
Carlos_Mendoza
NXP Employee
NXP Employee

Hi Subodh,

Maybe the problem is the PIO1_12 pin, it does not have the FC6 SCL function:

pastedImage_1.png

You could use the PIO1_16, PIO4_3 or PIO0_22 instead.

About your configuration, everything seems to be correct, I just have one comment about the read register function, instead of using the flag kI2C_TransferRepeatedStartFlag you could use the kI2C_TransferDefaultFlag.


Hope it helps!

Best Regards,
Carlos Mendoza
Technical Support Engineer