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:
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.
Thanks for reading.
I didn't have to change the macro and I just use it like this in a .cpp file
// Register a handler for BLE events.
NRF_SDH_BLE_OBSERVER(g_BleObserver, BLEAPP_OBSERVER_PRIO, ble_evt_dispatch, NULL);
To use SDK with C++, check out this library github.com/.../IOsonata
Hello,Thanks for your answer. But it replies not really to my question.I have already a complete C++ wrapper around the Nordic SDK, which works perfectly with Keil. My problem occurs when I use GCC.
I use GCC. That is why I replied to you. That code compile well with GCC. Set GCC to C++11
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