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

Error when using c++ with TWI

I am trying to use gcc to compile a c++ code for nRF52. I am using the TWI library and am getting the error below. Can anyone help me rewriting this macro so that it compiles normally ?

Compiling file: main.cpp
In file included from main.cpp:6:0:
../nRF5_SDK_11.0.0_89a8197/components/drivers_nrf/twi_master/nrf_drv_twi.h:91:1: error: invalid conversion from 'void*' to 'NRF_TWIM_Type*' [-fpermissive]
 }
 ^
main.cpp:28:40: note: in expansion of macro 'NRF_DRV_TWI_INSTANCE'
 static const nrf_drv_twi_t m_twi_GPS = NRF_DRV_TWI_INSTANCE(0);
  • Hi,

    In this line in the header file:

    .reg          = {NRF_DRV_TWI_PERIPHERAL(id)},       \
    

    we assign things to a union, but without specifying which particular element in the union. Some compilers might not like that. Can you please try to change

    #define NRF_DRV_TWI_INSTANCE(id)                        \
    {                                                       \
        .reg          = {NRF_DRV_TWI_PERIPHERAL(id)},       \
        .drv_inst_idx = CONCAT_3(TWI, id, _INSTANCE_INDEX), \
        .use_easy_dma = CONCAT_3(TWI, id, _USE_EASY_DMA)    \
    }
    

    to

    #define NRF_DRV_TWI_INSTANCE(id)                        \
    {                                                       \
        .reg          = {.p_twi = NRF_DRV_TWI_PERIPHERAL(id)},         \
        .drv_inst_idx = CONCAT_3(TWI, id, _INSTANCE_INDEX), \
        .use_easy_dma = CONCAT_3(TWI, id, _USE_EASY_DMA)    \
    }
    

    in nrf_drw_twi.h?

  • Thanks Martin for your reply. I tried that and still am getting same error. Any other workarounds ? Thanks much !

  • I just realized that in your error it says NRF_TWIM_Type so I guess you are using EasyDMA. Hence the code above should probably say

    .reg          = {.p_twim = NRF_DRV_TWI_PERIPHERAL(id)},         \
    

    Although if you still get the same error then obviously it wasn't the problem.

    I googled a bit and the error seems related to converting pointers and types back and forth, so maybe it is worth a shot to explicitly cast NRF_DRV_TWI_PERIPHERAL(id) to a NRF_TWIM_Type like this:

    .reg          = {.p_twim = (NRF_TWIM_Type*)NRF_DRV_TWI_PERIPHERAL(id)},         \
    

    I'm unable to test it here right now. If that doesn't work then I'm out of ideas I'm afraid. I talked to the developer of the driver and he didn't have any further suggestions either.

  • i found solution too.

    just change with this

    .reg = { .p_twi = (NRF_TWI_Type*)NRF_DRV_TWI_PERIPHERAL(id) },         \
    
  • In sdk14 i get

    In file included from ../../../main.cpp:56:0:
    ../../../../../../components/drivers_nrf/twi_master/nrf_drv_twi.h:173:1: error: 'nrf_drv_twi_t::<unnamed union>' has no non-static data member named 'p_twi'
     }
     ^
    ../../../main.cpp:71:36: note: in expansion of macro 'NRF_DRV_TWI_INSTANCE'
     static const nrf_drv_twi_t m_twi = NRF_DRV_TWI_INSTANCE(TWI_INSTANCE_ID);
    
Related