<?xml version="1.0" encoding="UTF-8" ?>
<?xml-stylesheet type="text/xsl" href="https://test-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 transfer in timer handler retrieves wrong values, hangs and crashes</title><link>https://test-devzone.nordicsemi.com/f/nordic-q-a/88866/spi-transfer-in-timer-handler-retrieves-wrong-values-hangs-and-crashes</link><description>I was having an issue where reading from SPI inside a timer handler always yielded 0xFF instead of the actual values. I tried to reproduce the issue with a simple test program but it shows a different behavior than my actual application - it simply hangs</description><dc:language>en-US</dc:language><generator>Telligent Community 13 Non-Production</generator><lastBuildDate>Mon, 13 Jun 2022 15:12:18 GMT</lastBuildDate><atom:link rel="self" type="application/rss+xml" href="https://test-devzone.nordicsemi.com/f/nordic-q-a/88866/spi-transfer-in-timer-handler-retrieves-wrong-values-hangs-and-crashes" /><item><title>RE: SPI transfer in timer handler retrieves wrong values, hangs and crashes</title><link>https://test-devzone.nordicsemi.com/thread/372205?ContentTypeID=1</link><pubDate>Mon, 13 Jun 2022 15:12:18 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:ee877527-12d7-414d-97c6-5959d25e2161</guid><dc:creator>user112696</dc:creator><description>&lt;p&gt;OMG, I had actually read that exact paragraph (&lt;a href="https://docs.zephyrproject.org/latest/kernel/services/timing/timers.html#using-a-timer-expiry-function"&gt;in the Zephyr docs&lt;/a&gt;) but I hadn&amp;#39;t looked properly at the code and I assumed that when I use an expiry function it will &lt;em&gt;always&lt;/em&gt; run it&amp;nbsp;in the system workqueue thread.&lt;/p&gt;
&lt;p&gt;But no,&amp;nbsp;the timer handler is part of the ISR and if I want to defer the work to a &amp;quot;userspace&amp;quot; thread I have to do that myself.&lt;/p&gt;
&lt;p&gt;I&amp;#39;m not really surprised that doing SPI I/O is not possible in an ISR, I just didn&amp;#39;t know the timer handler is executed while still handling the interrupt.&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: SPI transfer in timer handler retrieves wrong values, hangs and crashes</title><link>https://test-devzone.nordicsemi.com/thread/372176?ContentTypeID=1</link><pubDate>Mon, 13 Jun 2022 13:42:03 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:6f46e73b-0678-4933-9cb0-d7139e58775f</guid><dc:creator>user111520</dc:creator><description>&lt;p&gt;So what you&amp;#39;re doing in your code is similar to &lt;a href="http://developer.nordicsemi.com/nRF_Connect_SDK/doc/latest/zephyr/kernel/services/timing/timers.html#using-a-timer-expiry-function"&gt;this example&lt;/a&gt;?&lt;/p&gt;
&lt;p&gt;Yes, the timer expiry function is executed at interrupt level.&lt;/p&gt;
&lt;p&gt;-Einar&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: SPI transfer in timer handler retrieves wrong values, hangs and crashes</title><link>https://test-devzone.nordicsemi.com/thread/372145?ContentTypeID=1</link><pubDate>Mon, 13 Jun 2022 12:42:55 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:22d38fe9-de81-40e7-b94d-c74be280f216</guid><dc:creator>user112696</dc:creator><description>&lt;p&gt;Well, in the demo program above that is intentional, yes, to demonstrate that it works from the main function but not from the timer expiry function.&lt;/p&gt;
&lt;p&gt;For now I used a workaround similar to the semaphore,&amp;nbsp;I used a message queue&amp;nbsp;with the main loop blocking on&amp;nbsp;&lt;span style="font-family:&amp;#39;courier new&amp;#39;, courier;"&gt;k_msgq_get()&lt;/span&gt; and the timer expiry function publishing to the message queue. But since I couldn&amp;#39;t find anything about (not) doing SPI transfers in a timer handler I assumed I was doing something wrong.&lt;/p&gt;
&lt;p&gt;Does the timer expiry function actually run&amp;nbsp;as part of&amp;nbsp;an ISR? I assumed Zephyr internally handles the actual timer interrupt and then runs the expiry function on a normal thread.&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: SPI transfer in timer handler retrieves wrong values, hangs and crashes</title><link>https://test-devzone.nordicsemi.com/thread/372131?ContentTypeID=1</link><pubDate>Mon, 13 Jun 2022 12:00:43 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:4f68690e-6693-4002-9813-ffb83f95174b</guid><dc:creator>user111520</dc:creator><description>&lt;p&gt;Hello!&lt;/p&gt;
&lt;p&gt;It looks like you&amp;#39;re calling read_byte_from_spi() both in your main loop and in your timer handler, is this intentional?&lt;/p&gt;
&lt;p&gt;Also, I would advise against reading spi inside your timer interrupt, this could be why you&amp;#39;re seeing unexpected behavior. Instead I would use a&amp;nbsp;&lt;a href="https://docs.zephyrproject.org/3.0.0/reference/kernel/synchronization/semaphores.html"&gt;semaphore&lt;/a&gt;&amp;nbsp;to signal from the interrupt that the timer has expired, and do the reading in your main function or another thread.&lt;/p&gt;
&lt;p&gt;Best regards,&lt;/p&gt;
&lt;p&gt;Einar&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item></channel></rss>