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

Multiple BLE_CUS_DEF() calls error

Hi I am trying to add multiple services to the custom_ble_service tutorial example right now. When I try to create a second ble_cus_t instance 'mcus2' by calling BLE_CUS_DEF(m_cus2) the code compiles, but after I flash the code and connect to DK board with nrf_connect app there is disconnection and I cannot reconnect. I check and there is an error that causes the NRF in the DK to stop working. 

So I inspect the code and I think that I cannot make multiple calls to BLE_CUS_DEF() with different parameters. Is there a way to set and use multiple NRF_SDH_BLE_OBSERVER() if my goal is to have multiple custom services? 

#define BLE_CUS_DEF(_name)                                                                          \
static ble_cus_t _name;                                                                             \
NRF_SDH_BLE_OBSERVER(_name ## _obs,                                                                 \
                     BLE_HRS_BLE_OBSERVER_PRIO,                                                     \
                     ble_cus_on_ble_evt, &_name)

NRF_BLE_GATT_DEF(m_gatt);
NRF_BLE_QWR_DEF(m_qwr);                                                         /**< GATT module instance. */
BLE_CUS_DEF(m_cus); /**< Context for the Queued Write module.*/
BLE_CUS_DEF(m_cus2);
BLE_ADVERTISING_DEF(m_advertising);    

Parents
  • Hello,

    I suggest you study the SDK\examples\ble_central\ble_app_uart_c example, and look at how it uses defines:

    /** @brief Macro for defining multiple ble_nus_c instances.
     *
     * @param   _name   Name of the array of instances.
     * @param   _cnt    Number of instances to define.
     * @hideinitializer
     */
    #define BLE_NUS_C_ARRAY_DEF(_name, _cnt)                 \
    static ble_nus_c_t _name[_cnt];                          \
    NRF_SDH_BLE_OBSERVERS(_name ## _obs,                     \
                          BLE_NUS_C_BLE_OBSERVER_PRIO,       \
                          ble_nus_c_on_ble_evt, &_name, _cnt)

    in ble_nus_c.h. You can do something similar in your application. Create a macro called BLE_CUSTOM_SERVICE_ARRAY_DEF(_name, _cnt), and use it from your main.c. The only thing you need to change is the name of the macro (BLE_CUSTOM_SERVICE_ARRAY_DEF(), and make sure to keep the _cnt argument up to date from where it is being called in main.c. (you can set it equal to NRF_SDH_BLE_PERIPHERAL_LINK_COUNT or NRF_SDH_BLE_CENTRAL_LINK_COUNT, perhaps).

    You also need to replace ble_nus_c_on_ble_evt with the name of the callback function that you want to use for this observer, and to replace the BLE_NUS_C_BLE_OBSERVER_PRIO with whatever priority you want to use. Either create a new definition in sdk_config.h, or just use one of the ones that are available in the sdk_config.h file. Search for "OBSERVER_PRIO", and use one of them. As you can see, they are typically set to 2, so you can use this.

    Best regards,

    Edvin

  • Thx for your fast reply.

    I defined like below.

    static void * const obs_context[] = {NULL, NULL, NULL, NULL, NULL, NULL }

    NRF_SDH_BLE_OBSERVERS(m_ble_observers, 3, ble_evt_handler, obs_context, 6);   //This define is in ble_stack_init() function.

    But an error corrupt,  'expression must have a constant value'. I cant understand which parameter should be in NRF_SDH_BLE_OBSERVERS(). 


  • /* near top of your main.c file */
    #define BLE_CUSTOM_SERVICE_ARRAY_DEF(_name, _cnt)                 \
    static ble_nus_c_t _name[_cnt];                          \
    NRF_SDH_BLE_OBSERVERS(_name ## _obs,                     \
                          BLE_NUS_C_BLE_OBSERVER_PRIO,       \
                          ble_evt_handler, &_name, _cnt)
                          
                          
    BLE_CUSTOM_SERVICE_ARRAY_DEF(m_ble_observers, MAX_CONNECTIONS)
    
    

    Try adding this close to the top of your main.c. This will generate context pointers to each of your instances. You need to place it in main.c, since this is where your ble_evt_handler is defined. You may need to declare the ble_evt_handler before your call to BLE_CUSTOM_SERVICE_ARRAY_DEF() like this:

    static void ble_evt_handler(ble_evt_t const * p_ble_evt, void * p_context);

    Best regards,

    Edvin

Reply
  • /* near top of your main.c file */
    #define BLE_CUSTOM_SERVICE_ARRAY_DEF(_name, _cnt)                 \
    static ble_nus_c_t _name[_cnt];                          \
    NRF_SDH_BLE_OBSERVERS(_name ## _obs,                     \
                          BLE_NUS_C_BLE_OBSERVER_PRIO,       \
                          ble_evt_handler, &_name, _cnt)
                          
                          
    BLE_CUSTOM_SERVICE_ARRAY_DEF(m_ble_observers, MAX_CONNECTIONS)
    
    

    Try adding this close to the top of your main.c. This will generate context pointers to each of your instances. You need to place it in main.c, since this is where your ble_evt_handler is defined. You may need to declare the ble_evt_handler before your call to BLE_CUSTOM_SERVICE_ARRAY_DEF() like this:

    static void ble_evt_handler(ble_evt_t const * p_ble_evt, void * p_context);

    Best regards,

    Edvin

Children
No Data
Related