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

Interfacing a Sensor Hub with TWI drivers?

Hi,

I am trying to adapt the read register function that use the TwoWire lib of Arduino in order to read a Sensor Hub Response Byte of a defined Host Command :

uint8_t readByte(uint8_t _familyByte, uint8_t _indexByte,\
                                           uint8_t _writeByte)
{

    uint8_t returnByte;
    uint8_t statusByte;

    _i2cPort->beginTransmission(_address);
    _i2cPort->write(_familyByte);
    _i2cPort->write(_indexByte);
    _i2cPort->write(_writeByte);
    _i2cPort->endTransmission();
    delay(CMD_DELAY);

    _i2cPort->requestFrom(_address, sizeof(returnByte) + sizeof(statusByte));
    statusByte = _i2cPort->read();
    if (statusByte)// SUCCESS (0x00)
        return statusByte; // Return the error, see: READ_STATUS_BYTE_VALUE 

    returnByte = _i2cPort->read();
    return returnByte; // If good then return the actual byte. 

}

to have a function that uses the nrf_dr_twi, like this: 

uint32_t nrf_drv_read_registers(uint8_t reg, uint8_t* p_data, uint32_t length)
{
    uint32_t err_code;
    uint32_t timeout = OXI_TWI_TIMEOUT;

    err_code = nrf_drv_twi_tx(&m_twi_instance, OXI_ADDRESS, &reg, 1, false);
    if (err_code != NRF_SUCCESS) return err_code;

    while ((!twi_tx_done) && --timeout) ;
    if (!timeout) return NRF_ERROR_TIMEOUT;
    twi_tx_done = false;

    err_code = nrf_drv_twi_rx(&m_twi_instance, OXI_ADDRESS, p_data, length);
    if (err_code != NRF_SUCCESS) return err_code;

    timeout = OXI_TWI_TIMEOUT;
    while ((!twi_rx_done) && --timeout) ;
    if (!timeout) return NRF_ERROR_TIMEOUT;
    twi_rx_done = false;

    return err_code;
}

Have you some ideas on how i can use the family and index byte of the Sensor Hub in order to retrieve the correct byte? 

Thank you very much in advance,

Kind regards

  • Hi my dear Karl! 

    Thanks for helping me again!

    However, I am not sure that this will behave as expected, since when you are using the TXRX transfer description there is no STOP condition generated in between the TX and RX - this is not as shown in the illustration you posted earlier.

    I used the TX and RX transfers separately in this way:

    uint8_t readByte_WB(uint8_t familyByte, uint8_t indexByte, uint8_t writeByte)
    {
    
    		nrfx_err_t err_code;
    		uint8_t StatusByte;    
            uint8_t ReturnByte;
       
            uint8_t rx_buffer[2];
    		size_t rx_lenght =2;
    
    		uint8_t tx_buffer[] = {familyByte, indexByte, writeByte};    
    		size_t tx_lenght = sizeof(familyByte) + sizeof(indexByte) + sizeof(writeByte);
    
    		nrfx_twim_xfer_desc_t tx_xfer = NRFX_TWIM_XFER_DESC_TX(address, tx_buffer, tx_lenght);
    		err_code = nrfx_twim_xfer(&twim_instance, &tx_xfer, 0);
    		APP_ERROR_CHECK(err_code);
    		
    		nrfx_twim_xfer_desc_t rx_xfer = NRFX_TWIM_XFER_DESC_RX(address, rx_buffer, rx_lenght);
    		err_code = nrfx_twim_xfer(&twim_instance, &rx_xfer, 0);
    		APP_ERROR_CHECK(err_code);
        
    		StatusByte = rx_buffer[0];
            NRF_LOG_INFO("Status Byte: %lu \n",StatusByte); //0x00 for SUCCESS
        
    		ReturnByte = rx_buffer[1];  
    		return ReturnByte;
    }

    Can be a good solution for the read function? the write one it ll be very similar following this logic.

    Unfortunately i am stack with other ( many) errors in a previous part of my code and i cannot verify the correctness of these functions, but i ll do that very soon and i ll let you know if there are some problems!

    I just want to know if the logic behind this is correct, than i ll debug this part of the code properly.

    if you have a static array length that your twim transactions always will return, then it is preferable that you create a define for this, for increased readability.

    I will do that, thanks for the advice!

    thanks, 

    polimarte

  • Hello Polimarte,

    polimarte said:
    Thanks for helping me again!

    It is no problem at all, Polimarte! I am happy to help, and glad to see that you appreciate and implement the advice and suggestions you get in the forum - it is great!

    polimarte said:
    Can be a good solution for the read function? the write one it ll be very similar following this logic.

    The logic / approach seems all right in this function, you might have to add a delay / wait in between the two xfer calls, as shown in your illustration. It will be easier to debug once you are able to compile and flash you program, and monitor how it behaves.

    polimarte said:
    Unfortunately i am stack with other ( many) errors in a previous part of my code and i cannot verify the correctness of these functions, but i ll do that very soon and i ll let you know if there are some problems!

    I am sorry to hear that you are experiencing some other issues, and I look forward to hearing if this works as intended when you have cleared out the other errors.

    Good luck with your debugging and development!

    Best regards,
    Karl

Related