<?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>SPI from BLE event</title><link>https://devzone.nordicsemi.com/f/nordic-q-a/27351/spi-from-ble-event</link><description>Hi,
I am using SDK14.1 with nrf52832 and want to write to eeprom memory over SPI from BLE characteristic call. 
 My spi module works and everything was working fine until i tried to write something to eeprom over BLE. 
 This is my SPI function 
 _xfer_done</description><dc:language>en-US</dc:language><generator>Telligent Community 13</generator><lastBuildDate>Tue, 21 Nov 2017 13:47:27 GMT</lastBuildDate><atom:link rel="self" type="application/rss+xml" href="https://devzone.nordicsemi.com/f/nordic-q-a/27351/spi-from-ble-event" /><item><title>RE: SPI from BLE event</title><link>https://devzone.nordicsemi.com/thread/108021?ContentTypeID=1</link><pubDate>Tue, 21 Nov 2017 13:47:27 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:bf13d048-e7f1-4eb6-886c-f47ca184dcde</guid><dc:creator>MartinBL</dc:creator><description>&lt;p&gt;Q2. Something like this:&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;volatile bool start_spi_tx_flag = false; // Make sure to declare flag as &amp;#39;volatile&amp;#39;

// In BLE event handler
void some_ble_event_handler(...)
{
	// [....]
	start_spi_tx_flag = true;
}

// In main 
int main(void)
{
	// [....]
	
	while(1) // Main while forever loop
	{
		// [....]	
		if(start_spi_tx_flag)
		{
			// Do SPI transfer and set start_spi_tx_flag = false when you are ready
		}		
	}
}
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Q3. Please have a look at the &lt;a href="http://infocenter.nordicsemi.com/topic/com.nordic.infocenter.sdk5.v14.2.0/lib_scheduler.html?cp=4_0_0_3_34"&gt;scheduler library in the SDK&lt;/a&gt;.&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: SPI from BLE event</title><link>https://devzone.nordicsemi.com/thread/108022?ContentTypeID=1</link><pubDate>Tue, 21 Nov 2017 09:46:21 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:9d3478c2-1eac-423a-8691-e044bac097bb</guid><dc:creator>schef</dc:creator><description>&lt;p&gt;If i understand corectly solution nr. 2 would include a bool and a buffer[20]. When the write to the characteristic is finished i would check in main context if my bool is true and then write to the spi.
I don&amp;#39;t understand the implementation of the 3. solution.&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: SPI from BLE event</title><link>https://devzone.nordicsemi.com/thread/108020?ContentTypeID=1</link><pubDate>Tue, 21 Nov 2017 07:35:39 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:94b02455-ccc8-4811-a61a-9e22934bd9e3</guid><dc:creator>MartinBL</dc:creator><description>&lt;p&gt;It could it be caused by &lt;a href="https://devzone.nordicsemi.com/question/68758/twi-function-not-working-when-called-from-a-pin-interrupt-handler/"&gt;interrupt nesting&lt;/a&gt;, as @endnode suggests,
or some &lt;a href="https://devzone.nordicsemi.com/question/79226/can-ble_nus_string_send-be-called-from-a-twi-callback/"&gt;interrupt priority&lt;/a&gt; issue.&lt;/p&gt;
&lt;p&gt;You start an SPI transfer from within an interrupt context (the BLE event) and then wait for a transfer complete interrupt with &lt;code&gt;while (!_xfer_done)&lt;/code&gt;. However, what probably happens is that since the SPI interrupt has lower priority than the BLE event, the SPI interrupt will not be executed before your BLE event code is completed because it is queued behind the BLE event. Hence the flag _xfer_done is never set, and you effectively end up waiting forever in the &lt;code&gt;while (!_xfer_done)&lt;/code&gt; loop.&lt;/p&gt;
&lt;p&gt;Solutions are:&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;Raise the SPI priority to a level above the BLE event level (&lt;a href="http://infocenter.nordicsemi.com/topic/com.nordic.infocenter.s132.sds/dita/softdevices/s130/processor_avail_interrupt_latency/exception_mgmt_sd.html?cp=2_3_1_0_15_1"&gt;level 4&lt;/a&gt;), as you have already done by setting the level to 3. Then the SPI interrupt will not be queued behind the BLE event, but executed right away. Then the _xfer_done flag will be set letting your code move on from the while loop. This is relatively safe, but it is not good practice to have a while() loop inside an interrupt, so I would rather recommend one of the solutions below.&lt;/li&gt;
&lt;li&gt;Set a flag in the BLE event handler and use that flag to trigger an SPI transfer from the main context.&lt;/li&gt;
&lt;li&gt;Or use a scheduler and schedule an SPI transfer in the BLE event handler.&lt;/li&gt;
&lt;/ol&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: SPI from BLE event</title><link>https://devzone.nordicsemi.com/thread/108018?ContentTypeID=1</link><pubDate>Fri, 17 Nov 2017 13:26:06 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:166f60b4-c1e0-472a-bcdd-21b51aebcedc</guid><dc:creator>endnode</dc:creator><description>&lt;p&gt;Good question. I personally do all as scheduled actions through scheduler later outside event call-backs (because these are typically done inside some interrupt handler and time to execute APP FW code is limited).&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: SPI from BLE event</title><link>https://devzone.nordicsemi.com/thread/108019?ContentTypeID=1</link><pubDate>Fri, 17 Nov 2017 13:04:27 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:861fc7f3-0e8b-4ade-af8f-3aa5f33e89f1</guid><dc:creator>schef</dc:creator><description>&lt;p&gt;I have put the &lt;code&gt;#define SPI_DEFAULT_CONFIG_IRQ_PRIORITY 3&lt;/code&gt; and now it works. It this safe?&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item></channel></rss>