<?xml version="1.0" encoding="UTF-8" ?>
<?xml-stylesheet type="text/xsl" href="https://devzone.nordicsemi.com/cfs-file/__key/system/syndication/rss.xsl" media="screen"?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/" xmlns:wfw="http://wellformedweb.org/CommentAPI/" xmlns:atom="http://www.w3.org/2005/Atom"><channel><title>Stop button 1 entering DFU and button 4 disconnecting from BLE in custom buttonless DFU template</title><link>https://devzone.nordicsemi.com/f/nordic-q-a/83361/stop-button-1-entering-dfu-and-button-4-disconnecting-from-ble-in-custom-buttonless-dfu-template</link><description>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</description><dc:language>en-US</dc:language><generator>Telligent Community 13</generator><lastBuildDate>Fri, 21 Jan 2022 09:23:26 GMT</lastBuildDate><atom:link rel="self" type="application/rss+xml" href="https://devzone.nordicsemi.com/f/nordic-q-a/83361/stop-button-1-entering-dfu-and-button-4-disconnecting-from-ble-in-custom-buttonless-dfu-template" /><item><title>RE: Stop button 1 entering DFU and button 4 disconnecting from BLE in custom buttonless DFU template</title><link>https://devzone.nordicsemi.com/thread/348866?ContentTypeID=1</link><pubDate>Fri, 21 Jan 2022 09:23:26 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:b27e01e1-d670-413c-95bf-051c81e2cc1e</guid><dc:creator>Vidar Berg</dc:creator><description>&lt;p&gt;Thanks for sharing the project. The problem was that you were declaring the &amp;#39;m_cus&amp;#39; variable in a header file causing m_cus to become redeclared in every source you inserted the header in.&lt;/p&gt;
&lt;p&gt;The pointer you passed to ble_lbs_on_button_change() was pointing to the zero initialized &amp;#39;m_cus&amp;#39; varible declared in voltage.c, and not m_cus in main.c that was initilized by ble_cus_init().&lt;/p&gt;
&lt;p&gt;A simple fix is to declare it in main.c then remove the &amp;quot;ble_cus_t * p_lbs&amp;quot; paramater from your ble_lbs_on_button_change() function so you don&amp;#39;t have to share the variable across source files:&lt;/p&gt;
&lt;p&gt;&lt;pre class="ui-code" data-mode="diff"&gt;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);                                                           /**&amp;lt; GATT module instance. */
 NRF_BLE_QWR_DEF(m_qwr);                                                             /**&amp;lt; Context for the Queued Write module.*/
 BLE_ADVERTISING_DEF(m_advertising);                                            /**&amp;lt; Advertising module instance. */
+BLE_CUS_DEF(m_cus);    
+
 
 static void advertising_start(bool erase_bonds);                                    /**&amp;lt; 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 &amp;quot;nrf_log.h&amp;quot;
 
 
+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-&amp;gt;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(&amp;amp;params, 0, sizeof(params));
     params.type   = BLE_GATT_HVX_NOTIFICATION;
-    params.handle = p_lbs-&amp;gt;custom_value_handles.value_handle;
+    params.handle = mp_cus-&amp;gt;custom_value_handles.value_handle;
     params.offset = 0;    
     params.p_data = arr;
     params.p_len  = &amp;amp;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 &amp;gt; 0){
             //send stack
             printf(&amp;quot;__________BLE transmit attempt___________\n&amp;quot;);
-            err_code = ble_lbs_on_button_change(m_conn_handle, &amp;amp;m_cus, arr);
+            err_code = ble_lbs_on_button_change(m_conn_handle, arr);
             if(err_code == NRF_SUCCESS){
               printf(&amp;quot;BLE transmit complete\n&amp;quot;);
               transmit_success = true;
@@ -282,7 +282,7 @@ void transmit_packet(){
         }
         //send current draw
         printf(&amp;quot;__________BLE transmit attempt___________\n&amp;quot;);
-        err_code = ble_lbs_on_button_change(m_conn_handle, &amp;amp;m_cus, arr);
+        err_code = ble_lbs_on_button_change(m_conn_handle, arr);
         if(err_code == NRF_SUCCESS){
           printf(&amp;quot;BLE transmit complete\n&amp;quot;);
           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                         /**&amp;lt; Is on when device has connected. */
 #define GREEN_LED                       BSP_BOARD_LED_2   
 
-BLE_CUS_DEF(m_cus);                          /**&amp;lt; Handle of the current connection. */
+                      /**&amp;lt; Handle of the current connection. */
 
 extern uint16_t Draw_length;
 extern uint16_t Draw_length_average;
&lt;/pre&gt;&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: Stop button 1 entering DFU and button 4 disconnecting from BLE in custom buttonless DFU template</title><link>https://devzone.nordicsemi.com/thread/348811?ContentTypeID=1</link><pubDate>Thu, 20 Jan 2022 23:29:03 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:b582381b-d8e8-44bf-afb1-fbb7a50a4123</guid><dc:creator>joeyp2k</dc:creator><description>&lt;p&gt;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.&amp;nbsp; 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&amp;#39;t get any futher.&amp;nbsp; My project can be found here&amp;nbsp;&lt;a href="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"&gt;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&lt;/a&gt;&amp;nbsp; I am using the s132 and button 1 works the main function I am trying to run.&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: Stop button 1 entering DFU and button 4 disconnecting from BLE in custom buttonless DFU template</title><link>https://devzone.nordicsemi.com/thread/348655?ContentTypeID=1</link><pubDate>Thu, 20 Jan 2022 10:02:31 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:3322404d-5b4a-4da2-b27e-c6697c90dadf</guid><dc:creator>Vidar Berg</dc:creator><description>&lt;p&gt;Are you able to debug the code and check if the&amp;nbsp; value handle assigned by&amp;nbsp; &lt;span&gt;&lt;a title="sd_ble_gatts_characteristic_add" href="https://infocenter.nordicsemi.com/topic/com.nordic.infocenter.s140.api.v6.0.0/group___b_l_e___g_a_t_t_s___f_u_n_c_t_i_o_n_s.html?cp=4_7_4_6_2_4_2_1#ga9ee07ea4b96dcca1537b01ff9a7692ba"&gt;sd_ble_gatts_characteristic_add&lt;/a&gt;&lt;/span&gt;() has the same value when it&amp;#39;s used in ble_lbs_on_button_change()?&lt;/p&gt;
&lt;p&gt;I can debug it here as well if you are able to share your project.&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: Stop button 1 entering DFU and button 4 disconnecting from BLE in custom buttonless DFU template</title><link>https://devzone.nordicsemi.com/thread/348598?ContentTypeID=1</link><pubDate>Wed, 19 Jan 2022 22:42:27 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:9ade805e-986f-4bfc-974b-697d2e82984e</guid><dc:creator>joeyp2k</dc:creator><description>&lt;p&gt;I did not notice the&amp;nbsp;&lt;span&gt;button_char_handles in place of the custom_value_handles, but unfortunately, after making the modification the problem still persists&lt;/span&gt;&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: Stop button 1 entering DFU and button 4 disconnecting from BLE in custom buttonless DFU template</title><link>https://devzone.nordicsemi.com/thread/346366?ContentTypeID=1</link><pubDate>Thu, 06 Jan 2022 08:25:42 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:5328a859-4117-4d45-b7c0-0a577468ab7f</guid><dc:creator>Vidar Berg</dc:creator><description>&lt;p&gt;&lt;span&gt;&lt;a title="sd_ble_gatts_hvx" href="https://infocenter.nordicsemi.com/topic/com.nordic.infocenter.s140.api.v6.0.0/group___b_l_e___g_a_t_t_s___f_u_n_c_t_i_o_n_s.html?cp=4_7_4_6_2_4_2_4#ga313fe43c2e93267da668572e885945db"&gt;sd_ble_gatts_hvx&lt;/a&gt;&lt;/span&gt;() in&amp;nbsp;ble_lbs_on_button_change() will return with BLE_ERROR_INVALID_ATTR_HANDLE if you pass an invalid attribute handle to it.&lt;/p&gt;
&lt;p&gt;Does it work if you change this line in ble_lbs_on_button_change():&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; params.handle = p_lbs-&amp;gt;button_char_handles.value_handle;&lt;/p&gt;
&lt;p&gt;with this:&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; params.handle = p_lbs-&amp;gt;&lt;strong&gt;custom_value_handles&lt;/strong&gt;.value_handle;&lt;/p&gt;
&lt;p&gt;?&lt;/p&gt;
&lt;p&gt;Your &lt;span&gt;&lt;a title="sd_ble_gatts_characteristic_add" href="https://infocenter.nordicsemi.com/topic/com.nordic.infocenter.s140.api.v6.0.0/group___b_l_e___g_a_t_t_s___f_u_n_c_t_i_o_n_s.html?cp=4_7_4_6_2_4_2_1#ga9ee07ea4b96dcca1537b01ff9a7692ba"&gt;sd_ble_gatts_characteristic_add&lt;/a&gt;&lt;/span&gt;() call in custom_value_char_add()&amp;nbsp; populates the att handles in &amp;#39;custom_value_handles&amp;#39;, and&amp;nbsp; not in &amp;#39;button_char_handles&amp;#39;.&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: Stop button 1 entering DFU and button 4 disconnecting from BLE in custom buttonless DFU template</title><link>https://devzone.nordicsemi.com/thread/346329?ContentTypeID=1</link><pubDate>Thu, 06 Jan 2022 03:05:24 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:e7d9b1ce-59d5-4392-a536-f3948d7cd261</guid><dc:creator>joeyp2k</dc:creator><description>&lt;p&gt;Turns out the DFU mode was being entered because I was flashing the application incorrectly.&amp;nbsp; Merging the bootloader settings with my application before flashing with the softdevice and bootloader solved the problem.&amp;nbsp; However, I&amp;nbsp;found that the cause of my issue with button 1 is an unknown error 12291 from my ble_lbs_on_button_change.&amp;nbsp; After some searching, I found that this could be BLE_ERROR_INVALID_ATTR_HANDLE, but I can&amp;#39;t figure out where I went wrong defining my custom service.&amp;nbsp; The characteristic should be readable and writable by a connected device.&lt;/p&gt;
&lt;p&gt;&lt;pre class="ui-code" data-mode="text"&gt;/**@brief   Macro for defining a ble_cus instance.
 *
 * @param   _name   Name of the instance.
 * @hideinitializer
 */
#define BLE_CUS_DEF(_name)                                                                          \
static ble_cus_t _name;  

/**@brief Custom Service init structure. This contains all options and data needed for
 *        initialization of the service.*/
typedef struct
{
    uint8_t                       initial_custom_value;           /**&amp;lt; Initial custom value */
    ble_srv_cccd_security_mode_t  custom_value_char_attr_md;     /**&amp;lt; Initial security level for Custom characteristics attribute */

} ble_cus_init_t;

/**@brief Custom Service structure. This contains various status information for the service. */
struct ble_cus_s
{
    uint16_t                      service_handle;                 /**&amp;lt; Handle of Custom Service (as provided by the BLE stack). */
    ble_gatts_char_handles_t      custom_value_handles;           /**&amp;lt; Handles related to the Custom Value characteristic. */
    ble_gatts_char_handles_t    button_char_handles;
    uint16_t                      conn_handle;                    /**&amp;lt; Handle of the current connection (as provided by the BLE stack, is BLE_CONN_HANDLE_INVALID if not in a connection). */
    uint8_t                       uuid_type; 
};

// Forward declaration of the ble_cus_t type.
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);&lt;/pre&gt;&lt;pre class="ui-code" data-mode="text"&gt;/**@brief Function for adding the Custom Value characteristic.
 *
 * @param[in]   p_cus        Custom Service structure.
 * @param[in]   p_cus_init   Information needed to initialize the service.
 *
 * @return      NRF_SUCCESS on success, otherwise an error code.
 */

static uint32_t custom_value_char_add(ble_cus_t * p_cus, const ble_cus_init_t * p_cus_init)
{
    uint32_t            err_code;
    ble_gatts_char_md_t char_md;
    ble_gatts_attr_md_t cccd_md;
    ble_gatts_attr_t    attr_char_value;
    ble_uuid_t          ble_uuid;
    ble_gatts_attr_md_t attr_md;

    memset(&amp;amp;char_md, 0, sizeof(char_md));

    char_md.char_props.read   = 1;
    char_md.char_props.write  = 1;
    char_md.char_props.notify = 1; 
    char_md.p_char_user_desc  = NULL;
    char_md.p_char_pf         = NULL;
    char_md.p_user_desc_md    = NULL;
    char_md.p_cccd_md         = NULL; 
    char_md.p_sccd_md         = NULL;
		
    memset(&amp;amp;attr_md, 0, sizeof(attr_md));

    attr_md.read_perm  = p_cus_init-&amp;gt;custom_value_char_attr_md.read_perm;
    attr_md.write_perm = p_cus_init-&amp;gt;custom_value_char_attr_md.write_perm;
    attr_md.vloc       = BLE_GATTS_VLOC_STACK;
    attr_md.rd_auth    = 0;
    attr_md.wr_auth    = 0;
    attr_md.vlen       = 0;

    ble_uuid.type = p_cus-&amp;gt;uuid_type;
    ble_uuid.uuid = CUSTOM_VALUE_CHAR_UUID;

    memset(&amp;amp;attr_char_value, 0, sizeof(attr_char_value));

    attr_char_value.p_uuid    = &amp;amp;ble_uuid;
    attr_char_value.p_attr_md = &amp;amp;attr_md;
    attr_char_value.init_len  = 14;
    attr_char_value.init_offs = 0;
    attr_char_value.max_len   = 14;


    err_code = sd_ble_gatts_characteristic_add(p_cus-&amp;gt;service_handle, &amp;amp;char_md,
                                               &amp;amp;attr_char_value,
                                               &amp;amp;p_cus-&amp;gt;custom_value_handles);
    
    if (err_code != NRF_SUCCESS)
    {
        return err_code;
    }

    return NRF_SUCCESS;
}

/**@brief Function for initializing the Custom Service.
 *
 * @param[out]  p_cus       Custom Service structure. This structure will have to be supplied by
 *                          the application. It will be initialized by this function, and will later
 *                          be used to identify this particular service instance.
 * @param[in]   p_cus_init  Information needed to initialize the service.
 *
 * @return      NRF_SUCCESS on successful initialization of service, otherwise an error code.
 */

uint32_t ble_cus_init(ble_cus_t * p_cus, const ble_cus_init_t * p_cus_init)
{
    if (p_cus == NULL || p_cus_init == NULL)
    {
        return NRF_ERROR_NULL;
    }

    uint32_t   err_code;
    ble_uuid_t ble_uuid;

    //Initialize service structure
    p_cus-&amp;gt;conn_handle               = BLE_CONN_HANDLE_INVALID;

    // Add Custom Service UUID
    ble_uuid128_t base_uuid = {CUSTOM_SERVICE_UUID_BASE};
    err_code =  sd_ble_uuid_vs_add(&amp;amp;base_uuid, &amp;amp;p_cus-&amp;gt;uuid_type);
    VERIFY_SUCCESS(err_code);
    
    ble_uuid.type = p_cus-&amp;gt;uuid_type;
    ble_uuid.uuid = CUSTOM_SERVICE_UUID;

    // Add the Custom Service
    err_code = sd_ble_gatts_service_add(BLE_GATTS_SRVC_TYPE_PRIMARY, &amp;amp;ble_uuid, &amp;amp;p_cus-&amp;gt;service_handle);
    if (err_code != NRF_SUCCESS)
    {
        return err_code;
    }

    // Add Custom Value characteristic
    return custom_value_char_add(p_cus, 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
{
    ble_gatts_hvx_params_t params;
    uint16_t len = 14;

        uint8_t arr[10];

        for(int i = 0; i &amp;lt; 10; i++){
            printf(&amp;quot;Data Array[%d]: %d\n&amp;quot;, i, *button_state);
            arr[i] = *button_state;
            button_state++;
        }
        
    memset(&amp;amp;params, 0, sizeof(params));
    params.type   = BLE_GATT_HVX_NOTIFICATION;
    params.handle = p_lbs-&amp;gt;button_char_handles.value_handle;
    params.offset = 0;    
    params.p_data = arr;
    params.p_len  = &amp;amp;len;

    return sd_ble_gatts_hvx(conn_handle, &amp;amp;params);
}&lt;/pre&gt;&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: Stop button 1 entering DFU and button 4 disconnecting from BLE in custom buttonless DFU template</title><link>https://devzone.nordicsemi.com/thread/346202?ContentTypeID=1</link><pubDate>Wed, 05 Jan 2022 13:13:48 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:7e47cff2-a490-49ab-8084-b3cf49272f8a</guid><dc:creator>Vidar Berg</dc:creator><description>&lt;p&gt;Hello,&lt;/p&gt;
&lt;p&gt;The bootloader configures&amp;nbsp; button 4 pin as an input on startup, then it reads its input state to determine whether it should enter DFU mode or proceed to boot the main app. This mechanism can be removed by setting NRF_BL_DFU_ENTER_METHOD_BUTTON to &amp;#39;0&amp;#39; in the bootloader&amp;#39;s sdk_config.h file. It will not affect the button configuration in your app however. So it does not explain why you end up in DFU mode.&lt;/p&gt;
&lt;p&gt;If you have not already tried, please debug the application to see if it ends up in the app error handler (see &lt;span&gt;&lt;a title="Error module" href="https://infocenter.nordicsemi.com/topic/sdk_nrf5_v17.1.0/lib_error.html?cp=8_1_3_13"&gt;Error module&lt;/a&gt;&lt;/span&gt;). A runtime error could explain why the device is unexpectedly resetting into the bootloader.&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item></channel></rss>