<?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 fails after removing print statements</title><link>https://devzone.nordicsemi.com/f/nordic-q-a/25833/spi-fails-after-removing-print-statements</link><description>So i am utilizing SPI and i got it to work, however for some absurd reason after removing the segger_print statement below from before the while loop the spi would stop functioning. otherwise it works perfectly fine. Any idea why a print statement would</description><dc:language>en-US</dc:language><generator>Telligent Community 13</generator><lastBuildDate>Thu, 12 Oct 2017 05:58:52 GMT</lastBuildDate><atom:link rel="self" type="application/rss+xml" href="https://devzone.nordicsemi.com/f/nordic-q-a/25833/spi-fails-after-removing-print-statements" /><item><title>RE: spi fails after removing print statements</title><link>https://devzone.nordicsemi.com/thread/101762?ContentTypeID=1</link><pubDate>Thu, 12 Oct 2017 05:58:52 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:1a0ccac1-d929-4865-8bb4-c5a4e90fdeb1</guid><dc:creator>kl-cruz</dc:creator><description>&lt;p&gt;Yes, You can do something like this like You describe. It is better to do things asynchronously than synchronously. Of course if You want to save some energy.
You can also try to fill buffers in main function, start first transmission, then fill next buffers and then only update pointer in irq handler/callback to next buffers and start next transmission (in irq context). You can do two things in one time (load new data to buffers and send another data). Just doublebuffered!&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: spi fails after removing print statements</title><link>https://devzone.nordicsemi.com/thread/101761?ContentTypeID=1</link><pubDate>Thu, 12 Oct 2017 05:52:16 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:d5ff6d12-ac60-4486-b3bb-f2a9b4b053ce</guid><dc:creator>kl-cruz</dc:creator><description>&lt;p&gt;It is not needed to load it everytime from RAM memory. In the end variable will be updated, but without volatile keyword You worked on local copy.
RTT functions adding and removing - Compiler probably was lack of available registers and do writing/reading operations. That&amp;#39;s why it worked. So You are lucky one.&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: spi fails after removing print statements</title><link>https://devzone.nordicsemi.com/thread/101763?ContentTypeID=1</link><pubDate>Thu, 12 Oct 2017 05:51:57 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:ec52997f-c9b8-49ff-a297-53d8a34a9cdf</guid><dc:creator>kl-cruz</dc:creator><description>&lt;p&gt;&amp;quot; i still dont fully understand why the elimination of a comment would prevent it from working and simply adding the keyword volatile would permit it to work again. &amp;quot;
Well, compiler is an interesting thing. Trying to simplify and speed up code as much as it can. Variable without volatile keyword will be probably stored locally and in addition if You modify twice this variable then it can be optimized to one, only last statement (so it is important to use volatile in hardware and peripherall contexts).
I mean that compiler fetch this variable from RAM memory and will store it in CPU register within your function context. Variable in interrupt handler will work the same way. Compiler probably load value from RAM memory to register and change it locally within irq context. It is part of optimization. Look, in most cases You uses variable in within one context.&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: spi fails after removing print statements</title><link>https://devzone.nordicsemi.com/thread/101760?ContentTypeID=1</link><pubDate>Wed, 11 Oct 2017 18:51:45 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:dcfd2564-42aa-43db-acc2-a98d8a9470e4</guid><dc:creator>faikab</dc:creator><description>&lt;p&gt;thanks kl-cruz it actually worked. i still dont fully understand why the elimination of a comment would prevent it from working and simply adding the keyword volatile would permit it to work again.&lt;/p&gt;
&lt;p&gt;as for your other recommendation, i didnt fully understand it. I dont have a thread or OS. simply one main method.   but i guess what you are recommending is to do something similar to this right?&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;if (spi_lcd_xfer_done ==true) {
      nrf_drv_spi_transfer();
}
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;so it wont send data unless transfer is done and that way it wont wait at while loop right?&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: spi fails after removing print statements</title><link>https://devzone.nordicsemi.com/thread/101759?ContentTypeID=1</link><pubDate>Wed, 11 Oct 2017 05:46:40 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:46f10157-170b-4c88-834f-cbaa532a7d49</guid><dc:creator>kl-cruz</dc:creator><description>&lt;p&gt;Okay, but it looks like compiler placed code in other registers/memory. Try to change:&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;static bool spi_lcd_xfer_done = false;
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;to:&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;static volatile bool spi_lcd_xfer_done = false;
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;It should prevent compiler from code optimization with this variable so its address and access should be now the same in every place in code.&lt;/p&gt;
&lt;p&gt;You can also try to implement state machine to send data to the LCD to prevent creating additional variable and putting while loop. Unfortunately while loop is not energy efficient.&lt;/p&gt;
&lt;p&gt;Try to do it this way:&lt;/p&gt;
&lt;p&gt;send command (from main thread/context) and wait to irq -&amp;gt; in irq set data and start sending -&amp;gt; wait until next irq -&amp;gt; in next irq send next data
but remember that access from many context requires mutexes/critical section and other similar things.&lt;/p&gt;
&lt;p&gt;greetings!&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: spi fails after removing print statements</title><link>https://devzone.nordicsemi.com/thread/101758?ContentTypeID=1</link><pubDate>Tue, 10 Oct 2017 21:59:16 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:5559e069-2a0e-4d80-a5aa-8db385fe71d9</guid><dc:creator>faikab</dc:creator><description>&lt;p&gt;it is set in true as follows, but not volatile just to be clear everything is working in code above. however it stops working when the the SEGGER_RTT line which prints  &amp;quot;Im writing a command&amp;quot; is commented out/deleted&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;static bool spi_lcd_xfer_done = false;

/**
 * @brief SPI user event handler.
 * @param event
 */
void spi_lcd_event_handler(nrf_drv_spi_evt_t const * p_event)
{
	spi_lcd_xfer_done = true;
	switch(p_event-&amp;gt;type)
		{
		case NRF_DRV_SPI_EVENT_DONE:
		    printf(&amp;quot; Transfer completed.\r\n&amp;quot;);
			break;
		default:
			break;
		}
}
&lt;/code&gt;&lt;/pre&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: spi fails after removing print statements</title><link>https://devzone.nordicsemi.com/thread/101757?ContentTypeID=1</link><pubDate>Tue, 10 Oct 2017 21:32:10 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:0059e266-eb4f-452d-a1d4-0bd81972a5a9</guid><dc:creator>kl-cruz</dc:creator><description>&lt;p&gt;I assume that spi_lcd_xfer_done is setting to true in interrupt routine or callback. Has it volatile keyword?&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item></channel></rss>