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 Reply
  • 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

Children
  • 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

  • Hi Torbjørn

    Yes, I tried that as well. But no good.

    And so I tried to compile them with just printf( ) in it and here's what my files look like:

    main.cpp:

    #include "c_file.h"
    
    int main(void)
    {
        func();
    }

    c_file.h:

    #ifndef CFile_H
    #define CFILE_H
    
    #include <stdbool.h>
    #include <stdint.h>
    #include <string.h>
    
    int func();
    
    #endif 

    c_file.c:

    #ifndef CFile_C
    #define CFILE_C
    
    #include <stdbool.h>
    #include <stdint.h>
    #include <string.h>
    
    int func()
    {
        printf("this c file");
        return 0;
    }
    
    #endif 

    And it gives me this error: undefined reference to `func()'

    Am I doing something wrong here? 

    Regards

    Usman

  • Hi Usman

    Good point, I forgot to mention this. Every header file that defines functions that you need to include in a C++ file needs to use the extern "C" header, otherwise they won't be visible to the C++ linker. 

    If you open any of the standard header files in the SDK you can spot this. 

    Essentially they will have the following somewhere at the top of the header file (after any includes but before any defines or function prototypes): 

    #ifdef __cplusplus
    extern "C" {
    #endif

    And then the following at the bottom of the file (before the final #endif):

    #ifdef __cplusplus
    }
    #endif

    Best regards
    Torbjørn

Related