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

Programming nRF52 DK in C++ in Segger Embedded Studio

Hi.

I am writing software for my custom nRF52832 device but hardware wise it is identical to the nRF52 DK. 

I'm not that good of an embedded coder (or any kind of coder for that matter) and I am trying to integrate some i2c sensors on my device. 

But all the libraries of these sensors are written in C++, and are using objects and classes. Which C doesn't support.

I have managed to convert *some* of the code (in probably a very inefficient way) to plain C, but most of the code is beyond my capabilities. 

How can I code with C++ on this thing using Segger Embedded Studio? 

Any help would be greatly appreciated. 

- Usman

Parents
  • Hi Usman

    Any .cpp file should automatically be compiled using the C++ compiler, while any .c files will be compiled by the C compiler, so it should be possible to mix and match C and C++ code. 

    There might be library files in the SDK that will not compile in C++, because they use language features only available in C, but it should be possible to get around this by using C++ for your sensor libraries only.

    Have you tried to integrate any .cpp files in your project?

    If yes, did you face any issues?

    Best regards
    Torbjørn 

  • Hi Torbjørn, thanks for pointing that out.

    When I compile, it gives this error where the object class is defined

    unknown type name 'class'

    What am I missing here?
    Regards
    Usman

  • Hi Torbjørn.

    Thanks for the insight. It makes a lot of sense, since I'm using ble_app_uart example as a base. 

    So what I should do is, make a main.cpp file, put my non BLE code into it, rename the old main.c file into something else, and call the .c file in my main.cpp file? 

    Best regards,

    Usman

  • Hi Torbjørn

    you move the BLE functionality out of the main file and into a separate C file (that you can then call from the main.cpp file)

    I did this, now my code is like this, but it shows the exact same errors. 

    If I remove #include "c_file.c" from the top, and mainx() in the main function, then it compiles. But now with them. What am I doing wrong here?

    #include <stdint.h>
    #include <stdbool.h>
    #include <string.h>
    #include "c_file.c"
    
    int main(void)
    {
        mainx();
    }

    Thanks, and regards,

    Usman

  • Hi Usman

    First off you shouldn't include a C file from another C file. Instead you need to create a header file (c_file.h essentially) that contains the function prototypes for the functions in c_file.c that you need to access, and include this header file only from main.c. 

    Also, in this file I would suggest splitting the code into a couple of independent functions for doing things such as:

    - Initializing the Bluetooth stack
    - Starting advertising
    - Sending or receiving data from a connected device
    - anything else that your application needs to do over Bluetooth

    In the end your main file could look something like this:

    static void on_ble_event(app_ble_event_t *event)
    {
        switch(event->type)
        {
            case APP_BLE_EVT_DATA_RECEIVED:
                break;
            case APP_BLE_EVT_CONNECTED:
                break;
                
            // And any other events you need to send back to the main function
        }            
    }
    
    int main(void)
    {
        app_ble_initialize(/*Arguments here*/);
        app_ble_set_callback(on_ble_event);
        
        app_ble_start_advertising();
        
        // Main loop
        while(1)
        {
            // If you have something to do repeatedly in the main loop in c_file.c then you can call it from here:
            app_ble_execute();
        }
    }

    Hopefully this makes sense?
    Please note that my pseudo code suggestion above is not the only way to do it, but I think it's a good way to move the details of configuring and managing the Bluetooth stack into a separate file (I think it's a good idea to do this even if you don't need to compile your project for C++). 

    Best regards
    Torbjørn

  • Hi Torbjørn. 

    Thanks for the correction. I did exactly as you told, but it still gives the same old errors

    I even tried removing ALL code from the cpp file and just put a printf( ) just to see if that works, but doesn't compile either. At this point, I think its more of a configuration issue, but I could be wrong. 

    Any guidance would be highly appreciated. 

    Regards

    Usman

  • Hi Usman

    Did you move all the instance definitions at the top of main.c into the separate file? 

    They should look something like this:

    BLE_NUS_DEF(m_nus, NRF_SDH_BLE_TOTAL_LINK_COUNT);                                   /**< BLE NUS service instance. */
    NRF_BLE_GATT_DEF(m_gatt);                                                           /**< GATT module instance. */
    NRF_BLE_QWR_DEF(m_qwr);                                                             /**< Context for the Queued Write module.*/
    BLE_ADVERTISING_DEF(m_advertising);                                                 /**< Advertising module instance. */
    

    These macros will not compile in C++, and they will also have to be available to the rest of the BLE code if you move this into a separate file. 

    Best regards
    Torbjørn

Reply
  • Hi Usman

    Did you move all the instance definitions at the top of main.c into the separate file? 

    They should look something like this:

    BLE_NUS_DEF(m_nus, NRF_SDH_BLE_TOTAL_LINK_COUNT);                                   /**< BLE NUS service instance. */
    NRF_BLE_GATT_DEF(m_gatt);                                                           /**< GATT module instance. */
    NRF_BLE_QWR_DEF(m_qwr);                                                             /**< Context for the Queued Write module.*/
    BLE_ADVERTISING_DEF(m_advertising);                                                 /**< Advertising module instance. */
    

    These macros will not compile in C++, and they will also have to be available to the rest of the BLE code if you move this into a separate file. 

    Best regards
    Torbjørn

Children
Related