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

Bma2x2 drivers and TWI

HI, Im developing a custom board (NRF58232) with a bmx055 connected by TWI.

I have used the twi_scanner and twi_sensor and all OK, It was working ! 

But now I want to use the Bosch drivers API to a better implementation.

I can see that, the only thing that I must to do its implement the read and write functions but I dont know how !! 

And, Must I initialize the TWI like in the twi_sensor like this ? 


void twi_init(void) {
    ret_code_t
    err_code;
    nrf_gpio_cfg_output(9);

    const nrf_drv_twi_config_t bmx_config = {
            .scl                = 10,
            .sda                = 8,
            .frequency          = NRF_DRV_TWI_FREQ_400K,
            .interrupt_priority = APP_IRQ_PRIORITY_HIGH,
            .clear_bus_init     = false
    };

    err_code = nrf_drv_twi_init(&m_twi, &bmx_config, NULL, NULL);
    APP_ERROR_CHECK(err_code);
    NRF_LOG_INFO("TWI_ENABLED");
    nrf_drv_twi_enable(&m_twi);

}

The drivers can be found here 

And the implementation about write/read are however i know it`s not like that : 

s8 BMA2x2_I2C_bus_read(u8 dev_addr, u8 reg_addr, u8 *reg_data, u8 cnt) {

    uint8_t
    data_11 = {reg_addr};
    nrf_drv_twi_tx(&m_twi, dev_addr, &data_11, sizeof(data_11), true);
    nrf_delay_ms(10);
    nrf_drv_twi_rx(&m_twi, dev_addr, &reg_data, sizeof(reg_data));

    /* Please take the below function as your reference
     * for read the data using I2C communication
     * add your I2C rad function here.
     * "IERROR = I2C_WRITE_READ_STRING(DEV_ADDR, ARRAY, ARRAY, 1, CNT)"
     * iError is an return value of SPI write function
     * Please select your valid return value
     * In the driver SUCCESS defined as 0
     * and FAILURE defined as -1
     */
    return (s8) reg_data;

}

s8 BMA2x2_I2C_bus_write(u8 dev_addr, u8 reg_addr, u8 *reg_data, u8 cnt) {

    uint8_t
    data_11 = {reg_addr};
    nrf_drv_twi_tx(&m_twi, dev_addr, &data_11, sizeof(data_11), true);
    nrf_delay_ms(10);
    nrf_drv_twi_tx(&m_twi, dev_addr, &reg_data, sizeof(reg_data),true);  
    /* Please take the below function as your reference
     * for read the data using I2C communication
     * add your I2C rad function here.
     * "IERROR = I2C_WRITE_READ_STRING(DEV_ADDR, ARRAY, ARRAY, 1, CNT)"
     * iError is an return value of SPI write function
     * Please select your valid return value
     * In the driver SUCCESS defined as 0
     * and FAILURE defined as -1
     */
    return (s8) reg_data;

}

Can you help me please? A lot of thanks !

Parents
  • Hi,

    The way I understand the BMA2x2 driver you just have to implement the BMA2x2_I2C_bus_read() and BMA2x2_I2C_bus_write() functions. Please refer to the TWI documentation and examples in the SDK to see how it is used. It seems like the TWI sensor library (<SDK>\components\libraries\twi_sensor\) should match well, so I suggest you consider using that.

  • Yes I Understand the driver in the same way  !!  And yes, I think too that the twi_sensor its perfect because have all the necesary librarys for this work

    Buts the bosch implementation file example not clear at all 

    The steps to work bosch drivers with nordic_sdk will be:

    1- Init twi like normally  I would in nordic sdk

    2- implement write and read functions but Im stucked on this step, for example: 

    Bosch says : 

    s8 BMA2x2_I2C_bus_read(u8 dev_addr, u8 reg_addr, u8 *reg_data, u8 cnt) {
        s32 iError = BMA2x2_INIT_VALUE;
        u8 array[I2C_BUFFER_LEN] = {BMA2x2_INIT_VALUE};
        u8 stringpos = BMA2x2_INIT_VALUE;
    
        array[BMA2x2_INIT_VALUE] = reg_addr;
        /* Please take the below function as your reference
         * for read the data using I2C communication
         * add your I2C rad function here.
         * "IERROR = I2C_WRITE_READ_STRING(DEV_ADDR, ARRAY, ARRAY, 1, CNT)"
         * iError is an return value of SPI write function
         * Please select your valid return value
         * In the driver SUCCESS defined as 0
         * and FAILURE defined as -1
         */
    
        for (stringpos = BMA2x2_INIT_VALUE; stringpos < cnt; stringpos++)
            *(reg_data + stringpos) = array[stringpos];
        return (s8) iError;
    }

    but, Normally, in the nordic SDK i will read like this :

        ret_code_t
        err_code = nrf_drv_twi_tx(&m_twi, BMX055_ACC_ADDRESS, &data_11, sizeof(data_11), true);
        nrf_delay_ms(10);
        if (err_code == NRF_SUCCESS) {
            nrf_drv_twi_rx(&m_twi, BMX055_ACC_ADDRESS, &rawData, sizeof(rawData));
    }

    Im on the same situation that yesterday, I dont see clear how can 'merge' drivers into the SDK, and I dont found any relevant information.

    In any case, thanks for your response !!!

  • I do not see any trace of you actually trying to use the twi_sensor library. The code snippets you have pasted use the twi_driver (nrf_drv_twi_* functions) directly instead of the twi_sensor library. Using the twi_sensor library will save you a lot of time.

    Unfortunately, I am not able to test on my side, but look at how the twi_sensor library is used in other drivers in the SDK. You can find several examples under <SDK>\components\drivers_ext\. I am happy to help if you face any difficulties and have specific questions regarding the implementation.

  • Hi, the first, thanks for your reply ! 

    Yes, mi code is based in nrf_drv_twi_* functions because Its based in the <SDK>\examples\peripheral\twi_sensor 

    But, why these example its based in the nrf_drv_twi instead of twi_sensor directly?? 

    I saw the drivers that you said me, and for now, I dont know how can use with my drivers for now , but I will do it, dont close the issue yet ! 

    The nrf_drv_twi functions will dissapear in new sdk versions ? I have readed something like that in this forum. 

Reply
  • Hi, the first, thanks for your reply ! 

    Yes, mi code is based in nrf_drv_twi_* functions because Its based in the <SDK>\examples\peripheral\twi_sensor 

    But, why these example its based in the nrf_drv_twi instead of twi_sensor directly?? 

    I saw the drivers that you said me, and for now, I dont know how can use with my drivers for now , but I will do it, dont close the issue yet ! 

    The nrf_drv_twi functions will dissapear in new sdk versions ? I have readed something like that in this forum. 

Children
  • Hi,

    Yes, the naming of the examples and libraries is unfortunate. The twi_sensor example does not use the twi_sensor library, and that is not obvious at all before you look at the code. In your case you need the functionality from the twi_sensor library, so it is much better to use that than to just use the driver than having to implement everything yourself.

    The TWI sensor example is based on the nrf_drv_twi because the sensor is so simple that it does not need the TWI sensor library functions (which essentially is to combine read and write operations so that you can write the register address to the sensor first, telling it what you want to read). The TWI sensor library use the TWI driver internally.

    The nrf_drv_twi functions will not disappear in the near future. You might be thinking of the nrfx driver layer, which is used by nrf_drv_twi and the other "legacy" drivers. The reason for making nrfx is to make a driver package that is SDK independent. For instance it is used together with the Zephyr RTOS in addition to the nRF5 SDK. The legacy drivers will not disappear from the nRF5 SDK. 

Related