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);
Parents
  • 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?

  • Martin, may I ask why is this not changed in the SDK itself (using SDK v14.0.0)? Considering the fact that the union reg in nrf_drv_uart_t struct typedef (file nrf_drv_twi.h) is always defined with at least two members (p_regs and p_twim or p_regs and p_twi) and p_regs pointer is always present, why don't you just do:

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

    if you are already casting NRF_DRV_TWI_PERIPHERAL(id) macro to void *? Otherwise, there shall be:

    #if defined(TWIM_IN_USE)
        .reg = { .p_twim = (NRF_TWIM_Type *) NRF_DRV_TWI_PERIPHERAL(id) }, \
    #elif defined(TWI_IN_USE)
        .reg = { .p_twi = (NRF_TWI_Type *) NRF_DRV_TWI_PERIPHERAL(id) }, \
    #endif
    

    The same goes for NRF_DRV_UART_INSTANCE(id) which also throws -fpermissive error using C++. Is solution with .p_regs not used because of missing initializer warning?

Reply
  • Martin, may I ask why is this not changed in the SDK itself (using SDK v14.0.0)? Considering the fact that the union reg in nrf_drv_uart_t struct typedef (file nrf_drv_twi.h) is always defined with at least two members (p_regs and p_twim or p_regs and p_twi) and p_regs pointer is always present, why don't you just do:

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

    if you are already casting NRF_DRV_TWI_PERIPHERAL(id) macro to void *? Otherwise, there shall be:

    #if defined(TWIM_IN_USE)
        .reg = { .p_twim = (NRF_TWIM_Type *) NRF_DRV_TWI_PERIPHERAL(id) }, \
    #elif defined(TWI_IN_USE)
        .reg = { .p_twi = (NRF_TWI_Type *) NRF_DRV_TWI_PERIPHERAL(id) }, \
    #endif
    

    The same goes for NRF_DRV_UART_INSTANCE(id) which also throws -fpermissive error using C++. Is solution with .p_regs not used because of missing initializer warning?

Children
No Data
Related