Problem sending I2C message

cancel
Showing results for 
Search instead for 
Did you mean: 

Problem sending I2C message

605 Views
PedroCastro
Contributor III

Hi,

I am using the FRDM-KV31F and I cannot receive an I2C message. Sending is OK, but it still has some issue I haven't found yet. Since I am using SDK v2, I have almost copied the demos so I don't know where the mistake is.

As far as I understood  the slave datasheet, I have to send a write message with no data, just the address of the register and next send a read message with the register address and a one byte buffer for the slave response.

Here follows the code:

...

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;
    }
}

...

* Get default configuration for master. */
    I2C_MasterGetDefaultConfig(&masterConfig);
    /* Init I2C master. */
    I2C_MasterInit(I2C0, &masterConfig, I2C_MASTER_CLK);

    masterXfer.slaveAddress = I2C_BMS_ADDR;
    masterXfer.direction = kI2C_Write;
    masterXfer.subaddress = 0x04;
    masterXfer.subaddressSize = 1;
    masterXfer.data = txBuff;
    masterXfer.dataSize = 1;
    masterXfer.flags = kI2C_TransferNoStopFlag;

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


    I2C_MasterTransferNonBlocking(I2C0, &g_m_handle, &masterXfer);

    /*  Wait for transfer completed. */
    while (!g_MasterCompletionFlag){}
    g_MasterCompletionFlag = false;

   masterXfer.direction = kI2C_Read;

   masterXfer.flags = kI2C_TransferRepeatedStartFlag;
  

   I2C_MasterTransferNonBlocking(I2C0, &g_m_handle, &masterXfer);

   /*  Wait for transfer completed. */
    while (!g_MasterCompletionFlag){}
    g_MasterCompletionFlag = false;

...

Thanks in advance

Labels (1)
0 Kudos
1 Reply

134 Views
jorge_a_vazquez
NXP Employee
NXP Employee

Hi Pedro Correa


You mentioned "can send commands to the slave, but cant read information because the interruption isnt started"

Does this mean that you get stuck in the first  while (!g_MasterCompletionFlag){} ?
Problem here is that this flag is set in the master callback, but the implementation to trigger this callback is to call it when the master send the stop.

In this implementation, this callback will not be triggered because you set transfer with kI2C_TransferNoStopFlag, master is still in idle state to transfer, this is why this callback is not called.

You could remove this blocking while and just check this flag after they call the read transfer with kI2C_TransferRepeatedStartFlag (this transfer will call the stop transfer), something like:

masterXfer.slaveAddress = I2C_BMS_ADDR;
masterXfer.direction = kI2C_Write;
masterXfer.subaddress = 0x04;
masterXfer.subaddressSize = 1;
masterXfer.data = txBuff;
masterXfer.dataSize = 1;
masterXfer.flags = kI2C_TransferNoStopFlag;

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

I2C_MasterTransferNonBlocking(I2C0, &g_m_handle, &masterXfer);

masterXfer.direction = kI2C_Read;

masterXfer.flags = kI2C_TransferRepeatedStartFlag;
 
I2C_MasterTransferNonBlocking(I2C0, &g_m_handle, &masterXfer);
 
/*  Wait for transfer completed. */
    while (!g_MasterCompletionFlag){}
    g_MasterCompletionFlag = false

I made some test in my side and I could successfully write and read with these flags.

Hope this information could help you.

Best Regards

Jorge Alcala

0 Kudos