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

Operating NRF52 peripherals (TWI) in c++ mode

I'm trying to operate the  ''twi_sensor" example of SDK 15.30, using Kielu5, but the compiler is issuing errors for the syntax of the TWI libraries. From my side I only changed the main source file from c to c++.


The following image shows how I changed the source file from c to c++.

Image : changing source file extension


The following errors are the displayed errors once compiled:

compiling nrf_drv_twi.c...
compiling main.c...
..\..\..\..\..\..\integration\nrfx\legacy\nrf_drv_twi.h(588): error:  #29: expected an expression
              .type             = (nrfx_twim_xfer_type_t)p_xfer_desc->type,
..\..\..\..\..\..\integration\nrfx\legacy\nrf_drv_twi.h(589): error:  #29: expected an expression
              .address          = p_xfer_desc->address,
..\..\..\..\..\..\integration\nrfx\legacy\nrf_drv_twi.h(590): error:  #29: expected an expression
              .primary_length   = p_xfer_desc->primary_length,
..\..\..\..\..\..\integration\nrfx\legacy\nrf_drv_twi.h(591): error:  #29: expected an expression
              .secondary_length = p_xfer_desc->secondary_length,
..\..\..\..\..\..\integration\nrfx\legacy\nrf_drv_twi.h(592): error:  #29: expected an expression
              .p_primary_buf    = p_xfer_desc->p_primary_buf,
..\..\..\..\..\..\integration\nrfx\legacy\nrf_drv_twi.h(593): error:  #29: expected an expression
              .p_secondary_buf  = p_xfer_desc->p_secondary_buf,
..\..\..\..\..\..\integration\nrfx\legacy\nrf_drv_twi.h(603): error:  #29: expected an expression
              .type             = (nrfx_twi_xfer_type_t)p_xfer_desc->type,
..\..\..\..\..\..\integration\nrfx\legacy\nrf_drv_twi.h(604): error:  #29: expected an expression
              .address          = p_xfer_desc->address,
..\..\..\..\..\..\integration\nrfx\legacy\nrf_drv_twi.h(605): error:  #29: expected an expression
              .primary_length   = p_xfer_desc->primary_length,
..\..\..\..\..\..\integration\nrfx\legacy\nrf_drv_twi.h(606): error:  #29: expected an expression
              .secondary_length = p_xfer_desc->secondary_length,
..\..\..\..\..\..\integration\nrfx\legacy\nrf_drv_twi.h(607): error:  #29: expected an expression
              .p_primary_buf    = p_xfer_desc->p_primary_buf,
..\..\..\..\..\..\integration\nrfx\legacy\nrf_drv_twi.h(608): error:  #29: expected an expression
              .p_secondary_buf  = p_xfer_desc->p_secondary_buf,

  The above errors are indicating errors in the following lines of the nrf_drv_twi.h, and are pointing to the lines of code in the image below:

Image: nrf_drv_twi.h code

ret_code_t result = 0;
    if (NRF_DRV_TWI_USE_TWIM)
    {
    #ifdef TWIM_PRESENT
        nrfx_twim_xfer_desc_t const twim_xfer_desc =
        { 
            .type             = (nrfx_twim_xfer_type_t)p_xfer_desc->type,
            .address          = p_xfer_desc->address,
            .primary_length   = p_xfer_desc->primary_length,
            .secondary_length = p_xfer_desc->secondary_length,
            .p_primary_buf    = p_xfer_desc->p_primary_buf,
            .p_secondary_buf  = p_xfer_desc->p_secondary_buf,
        };
        result = nrfx_twim_xfer(&p_instance->u.twim, &twim_xfer_desc, flags);
    #endif
    }
    else if (NRF_DRV_TWI_USE_TWI)
    {
    #ifdef TWI_PRESENT
        nrfx_twi_xfer_desc_t const twi_xfer_desc =
        {
            .type             = (nrfx_twi_xfer_type_t)p_xfer_desc->type,
            .address          = p_xfer_desc->address,
            .primary_length   = p_xfer_desc->primary_length,
            .secondary_length = p_xfer_desc->secondary_length,
            .p_primary_buf    = p_xfer_desc->p_primary_buf,
            .p_secondary_buf  = p_xfer_desc->p_secondary_buf,
        };
        result = nrfx_twi_xfer(&p_instance->u.twi, &twi_xfer_desc, flags);
    #endif

Is there a step by step procedure to use the provided code from the SDK's to be used in C++ compilers ? and how can i rectify the issue I am dealing with?

Thanks

Parents
  • Maybe it was not clear in my question, just to make it clear, I want the code to work in a C++ environment instead of C

  • The problem you're likely running into is that C++ requires initializers to be in order whereas C will let you initialize members in any order. What this means for you, if you want to use C++ with the SDK, is that instead of using the helper macros to initialize a peripheral instance you need to explicitly initialize the structs in the correct order.

    For example, in the twi_sensor example, lets look at line 80. Rather than use the macro to initialize the TWI instance you would need to do something like the following:

    // Using nrf_drv
    static const nrf_drv_twi_t m_twi = { .inst_idx = 0, .twi = NRF_DRV_TWI_INSTANCE_0, .use_easy_dma = true };
    
    // If using nrfx
    static const nrfx_twim_t m_twi = { .p_twim = NRF_TWIM0, .drv_inst_idx = NRFX_TWIM0_INST_IDX };

    Looking further into the example you may also need to replace the function calls to nrf_drv_twi_tx and nrf_drv_twi_rx with the version that takes a transfer descriptor that you have created, initializing the members in order.

    I am using the nrfx drivers in my projects so hopefully I have gotten the nrf_drv initialization correct. If nothing else you should have an idea of what you need to do now to clear your compiler errors.

    *The above applies to initializing other peripherals as well. It is somewhat an inconvenience but worth the effort in my opinion.

Reply
  • The problem you're likely running into is that C++ requires initializers to be in order whereas C will let you initialize members in any order. What this means for you, if you want to use C++ with the SDK, is that instead of using the helper macros to initialize a peripheral instance you need to explicitly initialize the structs in the correct order.

    For example, in the twi_sensor example, lets look at line 80. Rather than use the macro to initialize the TWI instance you would need to do something like the following:

    // Using nrf_drv
    static const nrf_drv_twi_t m_twi = { .inst_idx = 0, .twi = NRF_DRV_TWI_INSTANCE_0, .use_easy_dma = true };
    
    // If using nrfx
    static const nrfx_twim_t m_twi = { .p_twim = NRF_TWIM0, .drv_inst_idx = NRFX_TWIM0_INST_IDX };

    Looking further into the example you may also need to replace the function calls to nrf_drv_twi_tx and nrf_drv_twi_rx with the version that takes a transfer descriptor that you have created, initializing the members in order.

    I am using the nrfx drivers in my projects so hopefully I have gotten the nrf_drv initialization correct. If nothing else you should have an idea of what you need to do now to clear your compiler errors.

    *The above applies to initializing other peripherals as well. It is somewhat an inconvenience but worth the effort in my opinion.

Children
No Data
Related