Beware that this post is related to an SDK in maintenance mode
More Info: Consider nRF Connect SDK for new designs
This discussion has been locked.
You can no longer post new replies to this discussion. If you have a question you can start a new discussion

Stop button 1 entering DFU and button 4 disconnecting from BLE in custom buttonless DFU template

I am using the buttonless DFU template to run my own program which uses some of the buttons on the dev kit.  For some reason, my button configuration is being overlapped somewhere, making button 1 disconnect the dev kit from my mobile device after a long hold, and button 4 enter DFU mode alongside the normal functionality I want.  I never configured this myself and am wondering where else the button is being configured from.  Is there somewhere I should be looking within my project which will point me in the right direction?

#define DRAW_BUTTON                BSP_BUTTON_0    
#define STAT_BUTTON                BSP_BUTTON_1
#define PRESS_BUTTON               BSP_BUTTON_2  
#define ACCLRM_BUTTON              BSP_BUTTON_3  

static void button_event_handler(uint8_t pin_no, uint8_t button_action)
{
    ret_code_t err_code;

    switch (pin_no)
    {
        case DRAW_BUTTON:
            record_draw();                  
            break;
        case STAT_BUTTON:
            check_status();
            break;
        case ACCLRM_BUTTON:
            read_acclrm();
            break;
        case PRESS_BUTTON:
            read_pressure();
            break;
        default:
            APP_ERROR_HANDLER(pin_no);
            break;
    }
}

void buttons_init(void)
{
    ret_code_t err_code;

    //The array must be static because a pointer to it will be saved in the button handler module.
    static app_button_cfg_t buttons[] =
    {
        {BSP_BUTTON_0, false, BUTTON_PULL, button_event_handler},
        {BSP_BUTTON_1, false, BUTTON_PULL, button_event_handler},
        {BSP_BUTTON_2, false, BUTTON_PULL, button_event_handler},
        {BSP_BUTTON_3, false, BUTTON_PULL, button_event_handler},
    };

    err_code = app_button_init(buttons, ARRAY_SIZE(buttons),
                               BUTTON_DETECTION_DELAY);
    APP_ERROR_CHECK(err_code);

    err_code = app_button_enable();

    APP_ERROR_CHECK(err_code);
}
int main(void)
{
    bool       erase_bonds;
    ret_code_t err_code;

    log_init();

    // Initialize the async SVCI interface to bootloader before any interrupts are enabled.
    err_code = ble_dfu_buttonless_async_svci_init();
    APP_ERROR_CHECK(err_code);

    timers_init();
    power_management_init();
    //buttons_leds_init(&erase_bonds);
    buttons_init();
    ble_stack_init();
    peer_manager_init();
    gap_params_init();
    gatt_init();
    services_init();
    advertising_init();
    conn_params_init();

    NRF_LOG_INFO("Buttonless DFU Application started.");

    // Start execution.
    application_timers_start();
    rtc_config();
    twi_init();
    nrf_drv_rtc_disable(&rtc);
    nrf_drv_rtc_tick_disable(&rtc);
    advertising_start(erase_bonds);
    
    // Enter main loop.
    for (;;)
    {
        idle_state_handle();
    }
}

int main(void)
{
    bool       erase_bonds;
    ret_code_t err_code;

    log_init();

    // Initialize the async SVCI interface to bootloader before any interrupts are enabled.
    err_code = ble_dfu_buttonless_async_svci_init();
    APP_ERROR_CHECK(err_code);
    
    leds_init();
    timers_init();
    power_management_init();
    //buttons_leds_init(&erase_bonds);
    buttons_init();
    ble_stack_init();
    peer_manager_init();
    gap_params_init();
    gatt_init();
    services_init();
    advertising_init();
    conn_params_init();

    NRF_LOG_INFO("Buttonless DFU Application started.");

    // Start execution.
    application_timers_start();
    rtc_config();
    twi_init();
    nrf_drv_rtc_disable(&rtc);
    nrf_drv_rtc_tick_disable(&rtc);
    advertising_start(erase_bonds);
    
    // Enter main loop.
    for (;;)
    {
        idle_state_handle();
    }
}

  • The value handles are the same between sd_ble_gatts_characteristic_add and ble_lbs_on_button_change but the handles are empty in both.  I spent some time today trying to figure out why, and where I could have deviated from the custom ble service tutorial on Github, but couldn't get any futher.  My project can be found here https://github.com/joeyp2k/Cuitt-NRF/tree/main/Desktop/Engineering/Cuitt/Development/nRF5_SDK_17.0.0_9d13099/examples/ble_peripheral/ble_app_buttonless_dfu  I am using the s132 and button 1 works the main function I am trying to run.

  • Thanks for sharing the project. The problem was that you were declaring the 'm_cus' variable in a header file causing m_cus to become redeclared in every source you inserted the header in.

    The pointer you passed to ble_lbs_on_button_change() was pointing to the zero initialized 'm_cus' varible declared in voltage.c, and not m_cus in main.c that was initilized by ble_cus_init().

    A simple fix is to declare it in main.c then remove the "ble_cus_t * p_lbs" paramater from your ble_lbs_on_button_change() function so you don't have to share the variable across source files:

    diff --git a/main.c b/main.c
    index 6dd3e65..27f6484 100644
    --- a/main.c
    +++ b/main.c
    @@ -91,6 +91,8 @@
    @@ -125,6 +127,8 @@ uint8_t sec, min, hr, day, date, month, year;
     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. */
    +BLE_CUS_DEF(m_cus);    
    +
     
     static void advertising_start(bool erase_bonds);                                    /**< Forward declaration of advertising start function */
     
    --- a/pca10040/s132/ses/ble_cus.c
    +++ b/pca10040/s132/ses/ble_cus.c
    @@ -8,6 +8,9 @@
     #include "nrf_log.h"
     
     
    +static ble_cus_t * mp_cus;
    +
    +
     /**@brief Function for adding the Custom Value characteristic.
      *
      * @param[in]   p_cus        Custom Service structure.
    @@ -124,6 +127,8 @@ uint32_t ble_cus_init(ble_cus_t * p_cus, const ble_cus_init_t * p_cus_init)
         uint32_t   err_code;
         ble_uuid_t ble_uuid;
     
    +    mp_cus = p_cus;
    +
         //Initialize service structure
         p_cus->conn_handle               = BLE_CONN_HANDLE_INVALID;
     
    @@ -148,7 +153,7 @@ uint32_t ble_cus_init(ble_cus_t * p_cus, const ble_cus_init_t * p_cus_init)
     
     
     
    -uint32_t ble_lbs_on_button_change(uint16_t conn_handle, ble_cus_t * p_lbs, uint8_t * button_state)//button_state is the value sent
    +uint32_t ble_lbs_on_button_change(uint16_t conn_handle, uint8_t * button_state)//button_state is the value sent
     {
         ble_gatts_hvx_params_t params;
         uint16_t len = 14;
    @@ -163,7 +168,7 @@ uint32_t ble_lbs_on_button_change(uint16_t conn_handle, ble_cus_t * p_lbs, uint8
             
         memset(&params, 0, sizeof(params));
         params.type   = BLE_GATT_HVX_NOTIFICATION;
    -    params.handle = p_lbs->custom_value_handles.value_handle;
    +    params.handle = mp_cus->custom_value_handles.value_handle;
         params.offset = 0;    
         params.p_data = arr;
         params.p_len  = &len;
    diff --git a/pca10040/s132/ses/ble_cus.h b/pca10040/s132/ses/ble_cus.h
    index 50d14b6..9215545 100644
    --- a/pca10040/s132/ses/ble_cus.h
    +++ b/pca10040/s132/ses/ble_cus.h
    @@ -43,4 +43,4 @@ typedef struct ble_cus_s ble_cus_t;
     
     uint32_t ble_cus_init(ble_cus_t * p_cus, const ble_cus_init_t * p_cus_init);
     
    -uint32_t ble_lbs_on_button_change(uint16_t conn_handle, ble_cus_t * p_lbs, uint8_t * button_state);
    +uint32_t ble_lbs_on_button_change(uint16_t conn_handle, uint8_t * button_state);
    diff --git a/pca10040/s132/ses/voltage.c b/pca10040/s132/ses/voltage.c
    index 01b593c..1c357dc 100644
    --- a/pca10040/s132/ses/voltage.c
    +++ b/pca10040/s132/ses/voltage.c
    @@ -262,7 +262,7 @@ void transmit_packet(){
             while(stack_size > 0){
                 //send stack
                 printf("__________BLE transmit attempt___________\n");
    -            err_code = ble_lbs_on_button_change(m_conn_handle, &m_cus, arr);
    +            err_code = ble_lbs_on_button_change(m_conn_handle, arr);
                 if(err_code == NRF_SUCCESS){
                   printf("BLE transmit complete\n");
                   transmit_success = true;
    @@ -282,7 +282,7 @@ void transmit_packet(){
             }
             //send current draw
             printf("__________BLE transmit attempt___________\n");
    -        err_code = ble_lbs_on_button_change(m_conn_handle, &m_cus, arr);
    +        err_code = ble_lbs_on_button_change(m_conn_handle, arr);
             if(err_code == NRF_SUCCESS){
               printf("BLE transmit complete\n");
               transmit_success = true;
    diff --git a/pca10040/s132/ses/voltage.h b/pca10040/s132/ses/voltage.h
    index 76426a6..6a007c0 100644
    --- a/pca10040/s132/ses/voltage.h
    +++ b/pca10040/s132/ses/voltage.h
    @@ -13,7 +13,7 @@
     #define RED_LED                         BSP_BOARD_LED_1                         /**< Is on when device has connected. */
     #define GREEN_LED                       BSP_BOARD_LED_2   
     
    -BLE_CUS_DEF(m_cus);                          /**< Handle of the current connection. */
    +                      /**< Handle of the current connection. */
     
     extern uint16_t Draw_length;
     extern uint16_t Draw_length_average;
    

Related