<?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>Issue in SPI trensfer inside timer handler function</title><link>https://test-devzone.nordicsemi.com/f/nordic-q-a/88744/issue-in-spi-trensfer-inside-timer-handler-function</link><description>I need to read a SPI device every fixed milliseconds so I thought to create a TIMER and read SPI slave inside its handler. But there are some problems cause I get all zero values and in some cases I get the &amp;quot;SPI timeout transfer&amp;quot; error. I think that is</description><dc:language>en-US</dc:language><generator>Telligent Community 13 Non-Production</generator><lastBuildDate>Tue, 14 Jun 2022 15:23:26 GMT</lastBuildDate><atom:link rel="self" type="application/rss+xml" href="https://test-devzone.nordicsemi.com/f/nordic-q-a/88744/issue-in-spi-trensfer-inside-timer-handler-function" /><item><title>RE: Issue in SPI trensfer inside timer handler function</title><link>https://test-devzone.nordicsemi.com/thread/372431?ContentTypeID=1</link><pubDate>Tue, 14 Jun 2022 15:23:26 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:656cc2d7-0e77-4bb9-ba4d-c457fc87a44d</guid><dc:creator>user112991</dc:creator><description>[quote userid="14926" url="~/f/nordic-q-a/88744/issue-in-spi-trensfer-inside-timer-handler-function/372362#372362"]You should not use both APIs for the same peripheral/instance at the same time, to prevent compatibility issues. Apart from that, most of the Zephyr APIs should already use nrfx drivers (e.g &lt;a href="https://github.com/nrfconnect/sdk-zephyr/blob/v3.0.99-ncs1/drivers/spi/spi_nrfx_spim.c"&gt;https://github.com/nrfconnect/sdk-zephyr/blob/v3.0.99-ncs1/drivers/spi/spi_nrfx_spim.c&lt;/a&gt;), so you would get runtime errors in case of overlapping usage.[/quote]
&lt;p&gt;Ok I got it. Thanks.&lt;/p&gt;
[quote userid="14926" url="~/f/nordic-q-a/88744/issue-in-spi-trensfer-inside-timer-handler-function/372362#372362"]I&amp;#39;m not aware of any nRF Connect SDK/Zephyr examples showing this, but you can find a nrfx_spim example in &lt;a href="https://test-devzone.nordicsemi.com/f/nordic-q-a/70105/how-to-use-spi-interface-in-a-zephyr-sample-example/288587#288587"&gt;this post&lt;/a&gt;. &lt;a href="https://github.com/nrfconnect/sdk-zephyr/tree/v3.0.99-ncs1/samples/boards/nrf/nrfx"&gt;This example&lt;/a&gt; shows how to use nrfx_ppi. Usage of nrfx_timer should be very similar, set &amp;quot;CONFIG_NRFX_TIMER=y&amp;quot; and &amp;quot;&lt;a href="https://developer.nordicsemi.com/nRF_Connect_SDK/doc/2.0.0/kconfig/index.html#CONFIG_NRFX_TIMER0"&gt;CONFIG_NRFX_TIMERx&lt;/a&gt;=y&amp;quot; (where x is the TIMER instance you want to use), and include &amp;quot;nrfx_timer.h&amp;quot; in your application.[/quote]
&lt;p&gt;I tryed to set the SPI and TIMER through PPI&amp;nbsp;but I have some issue. I opened a different post &lt;a href="https://test-devzone.nordicsemi.com/f/nordic-q-a/88962/how-to-properly-configure-periodic-spi-transfers-with-ppi-and-timer"&gt;here&lt;/a&gt;.&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: Issue in SPI trensfer inside timer handler function</title><link>https://test-devzone.nordicsemi.com/thread/372362?ContentTypeID=1</link><pubDate>Tue, 14 Jun 2022 11:39:08 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:7ea28192-a3a7-4460-b459-8617aa01c61a</guid><dc:creator>user14926</dc:creator><description>[quote user="Thoraz"]1) Until now I&amp;#39;ve used the Zephyr APIs. There&amp;nbsp;is something fundamental to know about the integration between the NRFX and Zephyr&amp;#39;s API?[/quote]
&lt;p&gt;You should not use both APIs for the same peripheral/instance at the same time, to prevent compatibility issues. Apart from that, most of the Zephyr APIs should already use nrfx drivers (e.g &lt;a href="https://github.com/nrfconnect/sdk-zephyr/blob/v3.0.99-ncs1/drivers/spi/spi_nrfx_spim.c"&gt;https://github.com/nrfconnect/sdk-zephyr/blob/v3.0.99-ncs1/drivers/spi/spi_nrfx_spim.c&lt;/a&gt;), so you would get runtime errors in case of overlapping usage.&lt;/p&gt;
[quote user="Thoraz"]2) About the PPI, do you have&amp;nbsp;a good starting point example (maybe with SPI and TIMERS)?[/quote]
&lt;p&gt;I&amp;#39;m not aware of any nRF Connect SDK/Zephyr examples showing this, but you can find a nrfx_spim example in &lt;a href="https://test-devzone.nordicsemi.com/f/nordic-q-a/70105/how-to-use-spi-interface-in-a-zephyr-sample-example/288587#288587"&gt;this post&lt;/a&gt;. &lt;a href="https://github.com/nrfconnect/sdk-zephyr/tree/v3.0.99-ncs1/samples/boards/nrf/nrfx"&gt;This example&lt;/a&gt; shows how to use nrfx_ppi. Usage of nrfx_timer should be very similar, set &amp;quot;CONFIG_NRFX_TIMER=y&amp;quot; and &amp;quot;&lt;a href="https://developer.nordicsemi.com/nRF_Connect_SDK/doc/2.0.0/kconfig/index.html#CONFIG_NRFX_TIMER0"&gt;CONFIG_NRFX_TIMERx&lt;/a&gt;=y&amp;quot; (where x is the TIMER instance you want to use), and include &amp;quot;nrfx_timer.h&amp;quot; in your application.&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: Issue in SPI trensfer inside timer handler function</title><link>https://test-devzone.nordicsemi.com/thread/371732?ContentTypeID=1</link><pubDate>Thu, 09 Jun 2022 18:28:50 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:5e82642c-b054-40e1-a418-51b4397772ff</guid><dc:creator>user112991</dc:creator><description>[quote userid="14926" url="~/f/nordic-q-a/88744/issue-in-spi-trensfer-inside-timer-handler-function/371705#371705"]This depends on your accuracy requirements. If you need the SPI readings to happen at very precise timing intervals, you could get better accuracy by using NRFX drivers and connect the TIMER/RTC event directly to trigger a predefined SPI transfer over a PPI channel (see for instance the &lt;a title="Advanced usage" href="https://infocenter.nordicsemi.com/topic/sdk_nrf5_v17.1.0/hardware_driver_spi_master.html?cp=8_1_2_0_13_2#hardware_driver_spi_advanced"&gt;Advanced usage&lt;/a&gt;&amp;nbsp;in nRF5 SDK driver description documentation). This will give exact timing regardless of interrupts and BLE activity in your application. When handling the transfers in software, you may be blocked by higher priority tasks (BLE, etc), so the transfers may be a bit delayed.[/quote]
&lt;p&gt;Actually I need to read the SPI device every precise time intervals, so I think that I&amp;#39;ll follow your suggestion about the use of NRFX drivers and PPI. But since I&amp;#39;m newbie with Nordic devices I have two last questions:&lt;/p&gt;
&lt;p&gt;1) Until now I&amp;#39;ve used the Zephyr APIs. There&amp;nbsp;is something fundamental to know about the integration between the NRFX and Zephyr&amp;#39;s API?&lt;/p&gt;
&lt;p&gt;2) About the PPI, do you have&amp;nbsp;a good starting point example (maybe with SPI and TIMERS)?&lt;/p&gt;
&lt;p&gt;Thanks very much.&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: Issue in SPI trensfer inside timer handler function</title><link>https://test-devzone.nordicsemi.com/thread/371705?ContentTypeID=1</link><pubDate>Thu, 09 Jun 2022 14:20:38 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:b806fb48-b4dc-44b8-ba19-35901e130712</guid><dc:creator>user14926</dc:creator><description>&lt;p&gt;Hi,&lt;/p&gt;
[quote user=""]1) What&amp;#39;s the problem?[/quote]
&lt;p&gt;Most likely, the K_TIMER event handler runs at the same or lower priority as the SPI interrupt handler, which prevents the SPI interrupt handler from being executed as long as you stay in the timer handler. You can try to use &lt;a href="https://developer.nordicsemi.com/nRF_Connect_SDK/doc/2.0.0/zephyr/hardware/peripherals/spi.html#c.spi_transceive_async"&gt;spi_transceive_async&lt;/a&gt;() and exit the timer handler to allow the SPI interrupt to complete the transfer, see &lt;a href="https://test-devzone.nordicsemi.com/f/nordic-q-a/64810/what-does-config_spi_async-do/264740#264740"&gt;this post&lt;/a&gt; for more info. You will then be notified in the provided handler when the transfer is completed.&lt;/p&gt;
[quote user=""]2) When I create a timer with the&amp;nbsp;K_TIMER_DEFINE macro&amp;nbsp;am I going to use a&amp;nbsp;TIMERx peripheral? If so, how can I &lt;span&gt;choose&lt;/span&gt;&lt;span&gt;&amp;nbsp;one in&amp;nbsp;&lt;/span&gt;particular?[/quote]
&lt;p&gt;No, the system timer is by default using the 32.768 kHz system clock/RTC peripheral. If you need better resolution for the timers, you can use the &lt;a href="https://developer.nordicsemi.com/nRF_Connect_SDK/doc/2.0.0/nrfx/drivers/timer/driver.html"&gt;nrfx_timer driver&lt;/a&gt;, see &lt;a href="https://test-devzone.nordicsemi.com/f/nordic-q-a/60049/configure-system-clock-faster-than-32768hz-in-ncs-zephyr/244400#244400"&gt;this post&lt;/a&gt;.&lt;/p&gt;
[quote user=""]3) OUT TOPIC: is the above SPI read routine a good choice in terms of time performance? If not,&amp;nbsp;have you some suggestions?[/quote]
&lt;p&gt;This depends on your accuracy requirements. If you need the SPI readings to happen at very precise timing intervals, you could get better accuracy by using NRFX drivers and connect the TIMER/RTC event directly to trigger a predefined SPI transfer over a PPI channel (see for instance the &lt;a title="Advanced usage" href="https://infocenter.nordicsemi.com/topic/sdk_nrf5_v17.1.0/hardware_driver_spi_master.html?cp=8_1_2_0_13_2#hardware_driver_spi_advanced"&gt;Advanced usage&lt;/a&gt;&amp;nbsp;in nRF5 SDK driver description documentation). This will give exact timing regardless of interrupts and BLE activity in your application. When handling the transfers in software, you may be blocked by higher priority tasks (BLE, etc), so the transfers may be a bit delayed.&lt;/p&gt;
&lt;p&gt;Best regards,&lt;br /&gt;Jørgen&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item></channel></rss>