<?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>nrf52832 optimization Level 3 result ble cannot re-advertise</title><link>https://devzone.nordicsemi.com/f/nordic-q-a/36192/nrf52832-optimization-level-3-result-ble-cannot-re-advertise</link><description>I am using BLE for NUS template example to create my app , and when I Compile my code with Keil 5.23.0.0 with optimization Level 0, It behaves good with the following operations: 
 1. Download app to chip, connect with APP on mobilephone 
 2. disconnet</description><dc:language>en-US</dc:language><generator>Telligent Community 13</generator><lastBuildDate>Wed, 11 Jul 2018 12:49:37 GMT</lastBuildDate><atom:link rel="self" type="application/rss+xml" href="https://devzone.nordicsemi.com/f/nordic-q-a/36192/nrf52832-optimization-level-3-result-ble-cannot-re-advertise" /><item><title>RE: nrf52832 optimization Level 3 result ble cannot re-advertise</title><link>https://devzone.nordicsemi.com/thread/139725?ContentTypeID=1</link><pubDate>Wed, 11 Jul 2018 12:49:37 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:975f5b74-2271-4a2c-84ae-a8a8972bfd69</guid><dc:creator>MartinBL</dc:creator><description>&lt;p&gt;It seems like the application&amp;nbsp;is testing the battery at startup and if the voltage is less than a certain level you put the application in system off mode. If I comment out&amp;nbsp;enterSystemOff() inside&amp;nbsp;battery_applications() in ADC.c my kit continues to advertise and I can connect.&amp;nbsp;&lt;/p&gt;
&lt;p&gt;I now see similar behavior as you. I can connect once, but after disconnecting the device doesn&amp;#39;t start advertising again. Here is why:&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;When you disconnect, a BLE_GAP_EVT_DISCONNECTED event is sent from the Softdevice to your application.&amp;nbsp;&lt;/li&gt;
&lt;li&gt;The advertising library picks up this event in&amp;nbsp;ble_advertising_on_ble_evt() in ble_advertisign.c&lt;/li&gt;
&lt;li&gt;The advertising library calls&amp;nbsp;on_disconnected().&lt;/li&gt;
&lt;li&gt;The library checks if it is ok to start advertising again: &lt;br /&gt;&lt;pre class="ui-code" data-mode="c_cpp"&gt;static void on_disconnected(ble_advertising_t * const p_advertising, ble_evt_t const * p_ble_evt)
{
    uint32_t ret;

    p_advertising-&amp;gt;whitelist_temporarily_disabled = false;

    if (p_ble_evt-&amp;gt;evt.gap_evt.conn_handle == p_advertising-&amp;gt;current_slave_link_conn_handle &amp;amp;&amp;amp;
        p_advertising-&amp;gt;adv_modes_config.ble_adv_on_disconnect_disabled == false)
    {
       ret = ble_advertising_start(p_advertising, BLE_ADV_MODE_DIRECTED);
       if ((ret != NRF_SUCCESS) &amp;amp;&amp;amp; (p_advertising-&amp;gt;error_handler != NULL))
       {
           p_advertising-&amp;gt;error_handler(ret);
       }
    }
}&lt;/pre&gt;&lt;/li&gt;
&lt;li&gt;The problem is that you change the advertising mode configuration incorrectly in&amp;nbsp;check_adv_mode_on_connect() which is called on every&amp;nbsp;BLE_GAP_EVT_CONNECTED event.&amp;nbsp;&lt;/li&gt;
&lt;li&gt;You need zero-initialize the&amp;nbsp;p_adv_modes_config structure to make sure it is not filled with garbage data and random variables. Typically we do it like this in the SDK:&lt;br /&gt;&lt;pre class="ui-code" data-mode="c_cpp"&gt;memset(&amp;amp;p_adv_modes_config, 0, sizeof(p_adv_modes_config));&lt;/pre&gt;&lt;/li&gt;
&lt;li&gt;If you don&amp;#39;t do this, then&amp;nbsp;&lt;code&gt;p_advertising-&amp;gt;adv_modes_config.ble_adv_on_disconnect_disabled&lt;/code&gt; is going to get a&amp;nbsp;random value. This value might be read as &lt;code&gt;true&lt;/code&gt; and the advertising library will not start advertising again after you have disconnected.&amp;nbsp;&lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;After adding the memset() function I was able to reconnect.&amp;nbsp;&lt;/p&gt;
&lt;p&gt;One strange thing though, is that for me it didn&amp;#39;t work with neither optimization level 0 nor level 3. I&amp;#39;m guessing it is a compiler thing. In your case, optimization level 0 caused the compiler to set &lt;code&gt;p_advertising-&amp;gt;adv_modes_config.ble_adv_on_disconnect_disabled&lt;/code&gt;&amp;nbsp;to 0 at instantiation, but on level 3 it got some other value. In my case, it got the wrong value in both cases.&amp;nbsp;&lt;/p&gt;
&lt;p&gt;So the bottom line is: make sure to zero-initialize all the structs that you use to&amp;nbsp;configure the Softdevice and SDK in general.&amp;nbsp;&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: nrf52832 optimization Level 3 result ble cannot re-advertise</title><link>https://devzone.nordicsemi.com/thread/139499?ContentTypeID=1</link><pubDate>Tue, 10 Jul 2018 05:49:08 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:0bdeb52c-cf1e-436f-b868-15013f8f3482</guid><dc:creator>rc</dc:creator><description>&lt;p&gt;1. &amp;nbsp;&lt;span&gt;LED 1 and 3 turn on is beacuse I use the IO for GPIOTE input&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&lt;span&gt;2. &amp;nbsp;External device is MPU9250/6 channel GPIOTE / 6 button / 74HC595 etc . &amp;nbsp;But I think it will not stop ble advertising, I decrease the project init operation, please help me re-check it!&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&lt;span&gt;3.&amp;nbsp;UART /&amp;nbsp;RTT / LOG module is disabled in my project&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&lt;span&gt;4. the suggest code &amp;quot;init.error_handler = advertising_error_handler;&amp;quot; I have added in main.c&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&lt;span&gt;5. S132 version is SDK14.1.0 &amp;nbsp;&amp;nbsp;s132_nrf52_5.0.0_softdevice.hex &amp;nbsp;pca10040&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&lt;span&gt;6. I am sure that the code is usable to advertise for a long time, And perform well in LEVEL 0, and this code is written for a product.&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&lt;span&gt;&lt;a href="https://devzone.nordicsemi.com/cfs-file/__key/communityserver-discussions-components-files/4/3007.BLE_5F00_CUBIC_2D00_testLevel3.7z"&gt;devzone.nordicsemi.com/.../3007.BLE_5F00_CUBIC_2D00_testLevel3.7z&lt;/a&gt;&amp;nbsp;&lt;a href="https://devzone.nordicsemi.com/cfs-file/__key/communityserver-discussions-components-files/4/_3575EF8DFE56_.zip"&gt;devzone.nordicsemi.com/.../_3575EF8DFE56_.zip&lt;/a&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&lt;span&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&lt;span&gt;&lt;/span&gt;&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: nrf52832 optimization Level 3 result ble cannot re-advertise</title><link>https://devzone.nordicsemi.com/thread/139454?ContentTypeID=1</link><pubDate>Mon, 09 Jul 2018 16:07:19 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:ed4ce638-7ab9-4994-b7f3-948fd0864382</guid><dc:creator>MartinBL</dc:creator><description>&lt;p&gt;When I run your project on my nRF52 DK development kit I can only see the device advertising for about a second, but then it stops and LED 1 and 3 turn on. I&amp;#39;m not able to connect and I get all kinds of weird behaviour when debugging. Is it supposed to be possible to run this project on a simple nRF52 DK without any external circuitry etc.?&lt;/p&gt;
&lt;p&gt;What S132 version are you using?&amp;nbsp;&lt;/p&gt;
&lt;p&gt;Have you tried debugging yourself? Are you able to determine what the application is doing when it is not advertising?&lt;/p&gt;
&lt;p&gt;Are you able to use UART or RTT to print some logging information?&lt;/p&gt;
&lt;p&gt;Can you try to implement an error handler for the Advertising Library? You can do it as simple as this:&lt;/p&gt;
&lt;p&gt;&lt;pre class="ui-code" data-mode="c_cpp"&gt;void advertising_error_handler(uint32_t nrf_error)
{
    // Use a debugger and/or LEDs etc. to check for errors. 
}

/**@brief Function for initializing the Advertising functionality.
 */
static void advertising_init(void)
{
    uint32_t               err_code;
    ble_advertising_init_t init;
    
    // Use an Advertising Library error handler
    init.error_handler = advertising_error_handler;

    memset(&amp;amp;init, 0, sizeof(init));

    init.advdata.name_type          = BLE_ADVDATA_FULL_NAME;
    init.advdata.include_appearance = false;
    init.advdata.flags              = BLE_GAP_ADV_FLAGS_LE_ONLY_GENERAL_DISC_MODE;
    
    etc...
    ....&lt;/pre&gt;&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: nrf52832 optimization Level 3 result ble cannot re-advertise</title><link>https://devzone.nordicsemi.com/thread/139439?ContentTypeID=1</link><pubDate>Mon, 09 Jul 2018 14:26:19 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:07e8df5a-7feb-4f12-8299-a28f633eb406</guid><dc:creator>rc</dc:creator><description>&lt;p&gt;&lt;a href="https://devzone.nordicsemi.com/cfs-file/__key/communityserver-discussions-components-files/4/BLE_5F00_CUBIC_2D00_testLevel3.7z"&gt;devzone.nordicsemi.com/.../BLE_5F00_CUBIC_2D00_testLevel3.7z&lt;/a&gt; Here is my project, it&amp;#39;s Level 0&amp;nbsp;Now, You can search a ECube_XXX ble devive from your phone app, you can reconnect to the device; But if you change build setting to Level 3, you cannot reconnect&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: nrf52832 optimization Level 3 result ble cannot re-advertise</title><link>https://devzone.nordicsemi.com/thread/139429?ContentTypeID=1</link><pubDate>Mon, 09 Jul 2018 13:25:30 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:d8a91633-4546-4862-808d-cc19f7c6504a</guid><dc:creator>MartinBL</dc:creator><description>&lt;p&gt;Hi,&lt;/p&gt;
&lt;p&gt;The first thing that comes to mind is&amp;nbsp;&lt;a href="https://barrgroup.com/Embedded-Systems/How-To/C-Volatile-Keyword"&gt;volatile variables&lt;/a&gt;. Could that be something? If not, can you upload your code so I can have a look at it?&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: nrf52832 optimization Level 3 result ble cannot re-advertise</title><link>https://devzone.nordicsemi.com/thread/139353?ContentTypeID=1</link><pubDate>Mon, 09 Jul 2018 03:48:14 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:8ffc87b4-75d4-401d-a807-6a8b4e2f0b2c</guid><dc:creator>rc</dc:creator><description>&lt;p&gt;I am using SDK14.1.0 &amp;nbsp;S132&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item></channel></rss>