<?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>nRF51 DK Event/Function on connect/disconnect</title><link>https://devzone.nordicsemi.com/f/nordic-q-a/41082/nrf51-dk-event-function-on-connect-disconnect</link><description>Hello community, 
 I&amp;#39;m facing a problem with nRF51 DK PCA10028 and a function call on connected or disconnected state including NRF_LOG commands. Using SDK 12.3.0 
 On connected state I want to wakeup a device on SPI and start communication. On disconnect</description><dc:language>en-US</dc:language><generator>Telligent Community 13</generator><lastBuildDate>Wed, 05 Dec 2018 17:32:48 GMT</lastBuildDate><atom:link rel="self" type="application/rss+xml" href="https://devzone.nordicsemi.com/f/nordic-q-a/41082/nrf51-dk-event-function-on-connect-disconnect" /><item><title>RE: nRF51 DK Event/Function on connect/disconnect</title><link>https://devzone.nordicsemi.com/thread/160463?ContentTypeID=1</link><pubDate>Wed, 05 Dec 2018 17:32:48 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:90d72205-75bc-4a60-ae2f-aba9914c9d92</guid><dc:creator>Lampi87</dc:creator><description>&lt;p&gt;I made some changes and now it seems to work. Please correct me if it&amp;#39;s the wrong way!&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;SPI0_USE_EASY_DMA set to 1&lt;/li&gt;
&lt;li&gt;
&lt;p class="p1"&gt;&lt;span class="s1"&gt;SPI_DEFAULT_CONFIG_IRQ_PRIORITY increased to 1 (initial 3)&lt;/span&gt;&lt;/p&gt;
&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;&lt;span class="s1"&gt;Now I can call device_start() and device_stop() on on_ble_evt function without any changes on spi communication&lt;/span&gt;&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: nRF51 DK Event/Function on connect/disconnect</title><link>https://devzone.nordicsemi.com/thread/160178?ContentTypeID=1</link><pubDate>Tue, 04 Dec 2018 11:38:31 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:820390c7-f6f3-4ea5-bdc2-06391e224cd3</guid><dc:creator>Lampi87</dc:creator><description>&lt;p&gt;Thank you for clarification, I will give it a try.&lt;/p&gt;
&lt;p&gt;I use the spi_send_recv function inside different functions. And at some places I need to wait for the response. If I remove the while function, rxBuf will stay empty when spi_send_recv is exited. Am I right?&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: nRF51 DK Event/Function on connect/disconnect</title><link>https://devzone.nordicsemi.com/thread/160134?ContentTypeID=1</link><pubDate>Tue, 04 Dec 2018 08:39:38 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:8feefef9-d7ab-4fe3-b2aa-80f9cfcdc4c9</guid><dc:creator>Sigurd</dc:creator><description>[quote user="Lampi87"]So you mean it would be better to have a timer to check if a device is connected or not an execute device start inside timer? And in &amp;quot;on_ble_evt&amp;quot; I only set a boolean flag &amp;quot;connected&amp;quot; to true or false?[/quote]
&lt;p&gt;The idea behind the timer in this case would just be to delay initializing the SPI device, to some time after you get the&amp;nbsp;&lt;span&gt;BLE_GAP_EVT_CONNECTED event.&amp;nbsp;&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&lt;span&gt;But I now see that you&amp;nbsp;are waiting for&amp;nbsp;&lt;em&gt;spi_xfer_done&lt;/em&gt; to be set true in&amp;nbsp;&lt;em&gt;spi_send_recv()&lt;/em&gt;,&amp;nbsp;but the&amp;nbsp;spi_event_handler() IRQ priority is most likely the same as the&amp;nbsp;ble_evt_dispatch()&amp;nbsp;priority, so you will never exit the &lt;em&gt;spi_send_recv()&lt;/em&gt; function. Try to remove the &amp;quot;while (!spi_xfer_done)&amp;nbsp; {&amp;nbsp;__WFE();} &amp;quot; code.&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: nRF51 DK Event/Function on connect/disconnect</title><link>https://devzone.nordicsemi.com/thread/159987?ContentTypeID=1</link><pubDate>Mon, 03 Dec 2018 14:09:41 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:24ee3c65-e0ea-4bcb-ae08-6b6f97b15e8c</guid><dc:creator>Lampi87</dc:creator><description>&lt;p&gt;Hi,&lt;/p&gt;
&lt;p&gt;the device_start() function:&lt;/p&gt;
&lt;p&gt;&lt;pre class="ui-code" data-mode="c_cpp"&gt;device_start() {
    NRF_LOG_INFO(&amp;quot;WAKEUP device\r\n&amp;quot;);
    send_command(COM_WAKEUP);
    nrf_delay_us(3); // Need to wait &amp;gt; 4 tclk cycles (&amp;gt; 4 * 514ns = &amp;gt; 2,1us) before continue
    NRF_LOG_INFO(&amp;quot;START conversion and RDATAC mode\r\n&amp;quot;);
    send_command(COM_START);
    send_command(COM_RDATAC);
    // Enable DRDY interrupt
    nrf_drv_gpiote_in_event_enable(PIN_DRDY, true);
    NRF_LOG_INFO(&amp;quot;Gpio nDRDY interrupt started\r\n&amp;quot;);
}&lt;/pre&gt;&lt;/p&gt;
&lt;p&gt;I made a few tests and it seems, that I get the same error, if I call device_start() after start_application_timers().&lt;br /&gt;There is one timer for battery service in parallel.&lt;/p&gt;
&lt;p&gt;So you mean it would be better to have a timer to check if a device is connected or not an execute device start inside timer? And in &amp;quot;on_ble_evt&amp;quot; I only set a boolean flag &amp;quot;connected&amp;quot; to true or false?&lt;/p&gt;
&lt;p&gt;UPDATE:&lt;br /&gt;The function send_command(...) is sending one byte via SPI. The last output on LOG Terminal is &amp;quot;SPI send data&amp;quot;.&lt;/p&gt;
&lt;p class="p1"&gt;&lt;span class="s1"&gt;&lt;b&gt;&lt;/b&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p class="p1"&gt;&lt;pre class="ui-code" data-mode="c_cpp"&gt;void send_command(uint8_t command)
{
    NRF_LOG_INFO(&amp;quot;Send command 0x%02X\r\n&amp;quot;, command);
    uint8_t rxBuf[1]
    spi_send_recv(&amp;amp;command, rxBuf, 1);
}

void spi_event_handler(nrf_drv_spi_evt_t const * p_event)
{
    spi_xfer_done = true;
}

void spi_send_recv(uint8_t* txBuf, uint8_t* rxBuf, uint16_t length)
{
    NRF_LOG_INFO(&amp;quot;SPI Send data&amp;quot;);
    memset(rxBuf, 0, length);
    spi_xfer_done = false;
    APP_ERROR_CHECK(nrf_drv_spi_transfer(&amp;amp;spi, txBuf, length, rxBuf, length));
    while (!spi_xfer_done) {
        __WFE();
    }
    NRF_LOG_INFO(&amp;quot;SPI recevied data&amp;quot;);
}&lt;/pre&gt;&lt;/p&gt;
&lt;p&gt;Best regards,&lt;br /&gt;Alexander&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: nRF51 DK Event/Function on connect/disconnect</title><link>https://devzone.nordicsemi.com/thread/159982?ContentTypeID=1</link><pubDate>Mon, 03 Dec 2018 14:00:16 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:94cbba81-cb6f-4cae-8034-d2fcafeb4f84</guid><dc:creator>Sigurd</dc:creator><description>&lt;p&gt;Hi,&lt;/p&gt;
&lt;p&gt;Could you post the code for the&amp;nbsp;&lt;span&gt;device_start() function? Are you spending alot of time in&amp;nbsp;device_start() ?&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&lt;span&gt;An alternative to moving the&amp;nbsp;device_start()&amp;nbsp; function to main(), is to start a app_timer when you get the connect event, and when the timer expires some defined numbers of seconds later, you call the&amp;nbsp;device_start() in the app_timer handler.&lt;/span&gt;&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item></channel></rss>