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

C++ and GCC error while registering SoftDevice observers

Hi everyone,

I have a project written in C++ for nRF52 series using the SDK 15.3.

If I use Keil and uVIsion, the project compiles and works great. With the same source code, if I use VSCode and GCC, the project compiles but crashes at run time. I identify the problem but I do not find the way to solve it.

Nordic SDK uses MACRO to register SoftDevice observers. This macro is called: 'NRF_SDH_BLE_OBSERVER'. The first problem is that this MACRO can be used with C++98 (for Keil). So I wrote directly in C++ the corresponding code in my project. The code is the following:

#if defined(__CC_ARM)
    static nrf_sdh_ble_evt_observer_t _scannerBleObs __attribute__ ((section(STRINGIFY(CONCAT_2(sdh_ble_observers,
                     NRF_BLE_SCAN_OBSERVER_PRIO))))) __attribute__((used)) = {
        nrf_ble_scan_on_ble_evt,
        static_cast<void*>(&_scanInstance)
    };
#elif defined(__GNUC__)
    static nrf_sdh_ble_evt_observer_t _scannerBleObs __attribute__ ((section("." STRINGIFY(CONCAT_2(sdh_ble_observers,
                     NRF_BLE_SCAN_OBSERVER_PRIO))))) __attribute__((used)) = {
        nrf_ble_scan_on_ble_evt,
        static_cast<void*>(&_scanInstance)
    };
#endif

This code is used to register in 'sdh_ble_observersX' section of the flash, my observer which is needed to get events back from SoftDevice to my BLE scanner. With that code, both Keil compiler and GCC are happy and both compile my project without error or warning.

Now at runtime, with Keil, the software works great, the observer is correctly registered and my BLE scanner is notified  without fail by the SoftDevice when events are dedicated to it. The next pictures is a screenshot of Keil running in debug mode my software with a breakpoint just after the code above. You can see in that picture that the handler and the p_context of  _scannerBleObs are well defined, with correct pointer address.

Using GCC, the trick is very different ! In the next picture, which is a screenshot of vscode running my software in debug mode, you can see that the handler of my observer is set correctly, but the p_context is null !!!

As this context's pointer is NULL, my scanner is never notified by the SoftDevice.

  • What are the CXXFLAGS and LDFLAGS that Keil uses to make it work?
  • Has anyone ever encountered this problem before?
  • Does anyone have a solution for me?

nRF52840
S140 v6.1.1
SDK15.3

Thanks for reading.

BR

Parents Reply
  • Hi,

    The thing is that I need a context... 

    I found the problem in my code. The p_context MUST be known at compilation time to be set in the flash section correctly. My '&_scanInstance' is not known at compilation time when registering the observer and the p_context is NULL. But for some reason, the Keil compiler do the object initialisation in an other order and when the macro is called to register the observer my '&_scanInstance' has been already initialised.

    This was no easy to discover this and this is not easy to modify the code around this...

    BR

Children
No Data
Related