This post is older than 2 years and might not be relevant anymore
More Info: Consider searching for newer posts

TWI (I2C) RX buffer doesn't contain the data received

I am trying to communicate with a MLX90615 temperature sensor via I2C and I am able to get the desired response from the sensor, verified with a logic analyser.

I am now trying to get the program to simply print the values into the debug terminal but the buffers contain completely different data from that displayed on the logic analyser output.

I am using the NRFX_TWIM_XFER_TXRX descriptor to send 1 byte then receive 3 with a repeated start bit. The temperature sensor is sending the correct data back but when I read the secondary buffer in the <twim_handler> function I get the output F9 FF FF where it should be 6B 3A 1E.

I am running Segger Embedded Studio with SDK v15. It is highly possible that I'm simply reading the buffers incorrectly or doing it in the wrong place so any suggestions or assistance would be greatly appreciated!

Here is my <main> function followed by my <twim_handler> and <twim_init> functions:

int main(void)
{
    
    log_init();
    nrfx_err_t err_code;

    twim_init();
    
    uint8_t command[1] = {0x27};
    uint8_t readBuf[3] = {0, 0, 0};
    size_t writeBufSize = 1;
    size_t readBufSize = 3;
    uint8_t addr = 0x5B;


    nrfx_twim_xfer_desc_t transferType = NRFX_TWIM_XFER_DESC_TXRX(addr, command, writeBufSize, readBuf, readBufSize);

    err_code = nrfx_twim_xfer(&twimInstance, &transferType, 0);
    NRF_LOG_INFO("Xfer error code: %s \n", errorcode_printer(err_code));

    APP_ERROR_CHECK(err_code);


}

void twim_init (void)
{
    ret_code_t err_code;

    const nrfx_twim_config_t twimConfig = {
       .scl                = ARDUINO_SCL_PIN,
       .sda                = ARDUINO_SDA_PIN,
       .frequency          = NRF_TWIM_FREQ_100K,
       .interrupt_priority = APP_IRQ_PRIORITY_HIGH,
       .hold_bus_uninit     = true
    };

    err_code = nrfx_twim_init(&twimInstance, &twimConfig, twim_handler, NULL);
    APP_ERROR_CHECK(err_code);

    nrfx_twim_enable(&twimInstance);
}

void twim_handler(nrfx_twim_evt_t const * p_event, void * p_context)
{

    size_t primaryBufferSize = p_event->xfer_desc.primary_length;
    uint8_t *bytesTransferred = p_event->xfer_desc.p_primary_buf;
    size_t secondaryBufferSize = p_event->xfer_desc.secondary_length;
    uint8_t *bytesRead = p_event->xfer_desc.p_secondary_buf;

    switch (p_event->type)
    {
        case NRFX_TWIM_EVT_DONE:
    
            NRF_LOG_INFO("STATE OF BUFFERS IN EVENT HANDLER:");
            NRF_LOG_INFO("\t Size of primary buffer: %d", primaryBufferSize);
            NRF_LOG_INFO("\t Bytes in primary buffer: %X", bytesTransferred[0]);
            NRF_LOG_INFO("\t Size of secondary buffer: %d", secondaryBufferSize);
            NRF_LOG_INFO("\t Bytes in secondary buffer: %X, %X, %X", bytesRead[0], bytesRead[1], bytesRead[2]);
            break;
        case NRFX_TWIM_EVT_ADDRESS_NACK:
            NRF_LOG_INFO("Received NACK after sending address!");
            break;
        case NRFX_TWIM_EVT_DATA_NACK:
            NRF_LOG_INFO("Received NACK after sending data.");
            break;
        default:
            break;
    }
}

Related