<?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>[SPIM+USBD] Data loss between SPIM and USBD</title><link>https://devzone.nordicsemi.com/f/nordic-q-a/114813/spim-usbd-data-loss-between-spim-and-usbd</link><description>I am programming with the nRF52840DKs. [Toolchain Manager: v1.3.0, IDE: Visual Studio Code (VSCode), SDK: ncs v2.6.0, window11 pro] 
 I am currently working on integrating &amp;#39;USBD&amp;#39; and &amp;#39;SPIM&amp;#39;. 
 My goal: communicate with the ADC via SPIM and transmit the</description><dc:language>en-US</dc:language><generator>Telligent Community 13</generator><lastBuildDate>Fri, 27 Sep 2024 11:14:27 GMT</lastBuildDate><atom:link rel="self" type="application/rss+xml" href="https://devzone.nordicsemi.com/f/nordic-q-a/114813/spim-usbd-data-loss-between-spim-and-usbd" /><item><title>RE: [SPIM+USBD] Data loss between SPIM and USBD</title><link>https://devzone.nordicsemi.com/thread/504155?ContentTypeID=1</link><pubDate>Fri, 27 Sep 2024 11:14:27 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:468ebc9b-415e-441e-bfc8-85a55c4f61ee</guid><dc:creator>Hung Bui</dc:creator><description>&lt;p&gt;Hi Seongmin,&amp;nbsp;&lt;br /&gt;I&amp;#39;m sorry for the late response and that I haven&amp;#39;t got time to dig deeper in your code.&amp;nbsp;&lt;/p&gt;
&lt;p&gt;I don&amp;#39;t see any loop or anything that can make the&amp;nbsp;spim1_handler() take long time. So I am not sure why you think that the handler&amp;#39;s task is too heavy. What you think that may take time? Have you tried to measure the time it take ?&amp;nbsp;&lt;br /&gt;&lt;br /&gt;&lt;/p&gt;
&lt;p&gt;You didn&amp;#39;t mention that you use BLE in this question, so I thought that you only use USBD to send data to PC, not BLE.&amp;nbsp; But we also need to figure out why BLE causing the problem. &lt;br /&gt;Have you tried to use a logic analyzer to check the SPI communication ?&amp;nbsp;&lt;br /&gt;&lt;br /&gt;I&amp;#39;m not sure I understand about your switching TXD/RXD.PTR logic, you may want to explain it to me.&amp;nbsp;&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: [SPIM+USBD] Data loss between SPIM and USBD</title><link>https://devzone.nordicsemi.com/thread/503479?ContentTypeID=1</link><pubDate>Tue, 24 Sep 2024 02:02:42 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:1208d61b-f069-4b57-b3c8-e2cd77ffc509</guid><dc:creator>seongmincho</dc:creator><description>&lt;p class="whitespace-pre-wrap break-words"&gt;After modifying the SPIM handler, we observed the following results:&lt;br /&gt;&lt;pre class="ui-code" data-mode="text"&gt;void spim1_handler(nrfx_spim_evt_t const * p_event, void * p_context) { 

   if (spim_buf == NULL) {
      spim_buf = k_malloc(sizeof(struct fifo_data_t));
      spim_buf-&amp;gt;len = 0;  
   }

   spim_buf-&amp;gt;data[buffer_index][0] = spim_rx_buf_A[buffer_index][0];
   spim_buf-&amp;gt;data[buffer_index][1] = spim_rx_buf_A[buffer_index][1];
   debug_ptr[buffer_index] = spim1_inst.p_reg-&amp;gt;TXD.PTR;
   buffer_index++;
   spim_buf-&amp;gt;len++;

   if (!initialization_stop) {
      initialization_counter++;
      if (initialization_counter == 30) {
         initialization_stop = true;
         spim1_inst.p_reg-&amp;gt;TXD.PTR = (uint32_t)spim_tx_buf_repeat[0];
         spim1_inst.p_reg-&amp;gt;RXD.PTR = (uint32_t)spim_rx_buf_A[0];
         k_fifo_put(&amp;amp;uart_fifo, spim_buf); 
         buffer_index = 0;
         spim_buf = NULL;
      }
      return;
   }

   if (buffer_index == 108){    
      spim1_inst.p_reg-&amp;gt;RXD.PTR = (uint32_t)spim_rx_buf_A[0];
      spim1_inst.p_reg-&amp;gt;TXD.PTR = (uint32_t)spim_tx_buf_repeat[0];
      k_fifo_put(&amp;amp;uart_fifo, spim_buf); 
      buffer_index = 0;
      spim_buf = NULL;
   }


}
&lt;/pre&gt;&lt;/p&gt;
&lt;p class="whitespace-pre-wrap break-words"&gt;1. Overall data issues have decreased.&lt;/p&gt;
&lt;p class="whitespace-pre-wrap break-words"&gt;2. Data problems still persist, showing the following characteristics:&lt;br /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;- Occurring at intervals of multiples of 108&lt;br /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;- The occurrence timing was similar to the period of the NUS-connection interval&lt;/p&gt;
&lt;p class="whitespace-pre-wrap break-words"&gt;3. Increasing the TIME_TO_WAIT_US value resolved these data issues.&lt;/p&gt;
&lt;p class="whitespace-pre-wrap break-words"&gt;4.&amp;nbsp;Initialization results were output in parts where repetition results were expected. The specific address values are as follows: &lt;br /&gt;spim_tx_buf_initial address[0]: 0x20002313 &lt;br /&gt;spim_tx_buf_initial address[29]: 0x2000234d &lt;br /&gt;spim_tx_buf_repeat address[0]: 0x2000223b &lt;br /&gt;spim_tx_buf_repeat address[107]: 0x20002311&lt;/p&gt;
&lt;p class="whitespace-pre-wrap break-words"&gt;5.&amp;nbsp;When using code such as debug_ptr[buffer_index] = spim1_inst.p_reg-&amp;gt;TXD.PTR; for debugging, spim1_inst.p_reg-&amp;gt;TXD.PTR did not function correctly.&lt;/p&gt;
&lt;p class="whitespace-pre-wrap break-words"&gt;&lt;strong&gt;Considering these observations&lt;/strong&gt;, I can draw the following inferences and conclusions: &lt;br /&gt;1) It appears that the TXD or RXD.PTR switching part within the handler is not functioning properly. &lt;br /&gt;2) These issues are likely occurring because the handler&amp;#39;s task is too heavy.&lt;/p&gt;
&lt;p class="whitespace-pre-wrap break-words"&gt;Im currently seeking solutions, and advice is needed particularly on the following two approaches: &lt;br /&gt;1) Methods to reduce the handler&amp;#39;s load &lt;br /&gt;2) Ways to optimize the TXD/RXD.PTR switching logic&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: [SPIM+USBD] Data loss between SPIM and USBD</title><link>https://devzone.nordicsemi.com/thread/503324?ContentTypeID=1</link><pubDate>Sat, 21 Sep 2024 07:49:00 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:865769bf-9a8f-4973-859b-f4d916d53a03</guid><dc:creator>seongmincho</dc:creator><description>[quote userid="2121" url="~/f/nordic-q-a/114813/spim-usbd-data-loss-between-spim-and-usbd/503303"]So you send 2 bytes - 16 bit every 45us ? Or it&amp;#39;s 240 bytes every 45us ?&amp;nbsp;[/quote]
&lt;p&gt;&lt;span&gt;&amp;#39;2 bytes - 16 bit every 45us&amp;#39; is correct !&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;+)&amp;nbsp;The &lt;strong&gt;TIMER&lt;/strong&gt; generates an event at regular intervals (TIME_TO_WAIT_US = 45), and this event triggers the start of &lt;strong&gt;SPIM&lt;/strong&gt; transmission via &lt;strong&gt;PPI&lt;/strong&gt;. This setup allows automatic, timer-based periodic SPIM transmissions. &lt;br /&gt;For more details, please refer to the main code.( &amp;#39;peripheral_setup&amp;#39; and &amp;#39;spim_handler&amp;#39; functions)&lt;/span&gt;&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: [SPIM+USBD] Data loss between SPIM and USBD</title><link>https://devzone.nordicsemi.com/thread/503303?ContentTypeID=1</link><pubDate>Fri, 20 Sep 2024 14:47:54 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:182c1572-a952-4007-b6e9-7d5213d48272</guid><dc:creator>Hung Bui</dc:creator><description>&lt;p&gt;Hi,&amp;nbsp;&lt;/p&gt;
[quote user="seongmincho"]SPIM communication occurs every 45μs, and during each SPIM communication, 16-bit data is exchanged through MISO and MOSI. After 120 SPIM communications, a total of 120 * 16 bits, or 240 bytes, of data is stored in &amp;#39;spim_buf&amp;#39;, and this data is inserted into &amp;#39;uart_fifo&amp;#39; using &amp;#39;k_fifo_put(&amp;amp;uart_fifo, spim_buf)&amp;#39;.&lt;br /&gt;[/quote]
&lt;p&gt;So you send 2 bytes - 16 bit every 45us ? Or it&amp;#39;s 240 bytes every 45us ?&amp;nbsp;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;/p&gt;
[quote user="seongmincho"]Data loss occurs 2-3 times consecutively out of 2000 results (16 * 2000 bits). As a result, it seemed difficult to store and output the data in the buffer. However, now that I think about it, there might be a way to predict when data loss is likely to happen and store the data only during those times. I&amp;#39;ll try that[/quote]
&lt;p&gt;You can think of just storing it in RAM and then put a debug point and check the RAM to see if there is any issue. This way you don&amp;#39;t have to use other way of printing the buffer out like USBD or UART.&amp;nbsp;&lt;br /&gt;&lt;br /&gt;Please do try to get a logic analyzer, it will make your life easier :)&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: [SPIM+USBD] Data loss between SPIM and USBD</title><link>https://devzone.nordicsemi.com/thread/503248?ContentTypeID=1</link><pubDate>Fri, 20 Sep 2024 11:28:04 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:12c6473a-bbcf-45da-abce-1b874a89a020</guid><dc:creator>seongmincho</dc:creator><description>[quote userid="2121" url="~/f/nordic-q-a/114813/spim-usbd-data-loss-between-spim-and-usbd/503230"]My understanding is that you want to transmit every 45uS ? How many bytes you are transmitting on each session ?&amp;nbsp;&amp;nbsp;[/quote]
&lt;p&gt;&lt;strong&gt;1) SPIM&lt;/strong&gt;&lt;br /&gt;assumption: &lt;em&gt;TIME_TO_WAIT_US&lt;/em&gt; = 45&amp;nbsp; (45us)&lt;br /&gt;SPIM communication occurs every 45&amp;mu;s, and during each SPIM communication, 16-bit data is exchanged through MISO and MOSI. After 120 SPIM communications, a total of 120 * 16 bits, or 240 bytes, of data is stored in &amp;#39;spim_buf&amp;#39;, and this data is inserted into &amp;#39;uart_fifo&amp;#39; using &amp;#39;k_fifo_put(&amp;amp;uart_fifo, spim_buf)&amp;#39;.&lt;br /&gt;&lt;br /&gt;&lt;strong&gt;2) USBD&lt;br /&gt;&lt;/strong&gt;The USBD waits until data is available in the &amp;#39;uart_fifo&amp;#39;, and once the data arrives, it is transmitted via UART. One &amp;#39;uart_fifo data block consists of 120 * 16 bits, or 240 bytes. (This is actually designed for BLE transmission, and since the MTU size for BLE is 244 bytes, the transmission unit was set to 240 bytes.)&lt;br /&gt;&lt;br /&gt;&lt;strong&gt;&lt;br /&gt;&lt;/strong&gt;&lt;/p&gt;
[quote userid="2121" url="~/f/nordic-q-a/114813/spim-usbd-data-loss-between-spim-and-usbd/503230"]Have you tried to test without USBD and check if the buffer you receive via SPI has the correct data ?&amp;nbsp;&lt;br /&gt;[/quote]
&lt;p&gt;Data loss occurs 2-3 times consecutively out of 2000 results (16 * 2000 bits). As a result, it seemed difficult to store and output the data in the buffer. However, now that I think about it, there might be a way to predict when data loss is likely to happen and store the data only during those times. I&amp;#39;ll try that&lt;/p&gt;
&lt;p&gt;&lt;/p&gt;
[quote userid="2121" url="~/f/nordic-q-a/114813/spim-usbd-data-loss-between-spim-and-usbd/503230"]You can use a l&lt;a href="https://www.google.com/search?q=logic+analyzer"&gt;ogic analyzer&amp;nbsp;&lt;/a&gt;&amp;nbsp;to verify that SPI communication is fine or not. It can visualize the communication and show you the data being transmitted on the lines. For example[/quote]
&lt;p&gt;wow it&amp;#39;s cool!!! but we don&amp;#39;t have a logic analyzer in our lab, so I&amp;#39;ll need to either ask my lab colleagues or inquire with another lab.&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: [SPIM+USBD] Data loss between SPIM and USBD</title><link>https://devzone.nordicsemi.com/thread/503230?ContentTypeID=1</link><pubDate>Fri, 20 Sep 2024 09:26:50 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:7017664b-b4cc-4385-b554-dd52026e4b1a</guid><dc:creator>Hung Bui</dc:creator><description>&lt;p&gt;Hi Seongmin,&amp;nbsp;&lt;br /&gt;&lt;br /&gt;&lt;/p&gt;
&lt;p&gt;I&amp;#39;m happy to help :)&amp;nbsp;&lt;br /&gt;You are correct about isolating. When you see something doesn&amp;#39;t work as it should, and if it&amp;#39;s possible to break it down to smaller part. Try to break it down&amp;nbsp;to smaller part and check if each part would do as it should.&amp;nbsp;&lt;br /&gt;For example in your case, you have SPI communication and USBD communication. They may affect each other. But you also need to check and verify SPI works as it should and USBD work as it should.&amp;nbsp;&lt;br /&gt;&lt;br /&gt;You can use a l&lt;a href="https://www.google.com/search?q=logic+analyzer"&gt;ogic analyzer&amp;nbsp;&lt;/a&gt;&amp;nbsp;to verify that SPI communication is fine or not. It can visualize the communication and show you the data being transmitted on the lines. For example:&amp;nbsp;&lt;br /&gt;&lt;img style="max-height:240px;max-width:320px;" src="https://devzone.nordicsemi.com/resized-image/__size/640x480/__key/communityserver-discussions-components-files/4/pastedimage1726823914644v1.png" alt=" " /&gt;&lt;br /&gt;&lt;br /&gt;&lt;/p&gt;
&lt;p&gt;&lt;/p&gt;
&lt;p&gt;My understanding is that you want to transmit every 45uS ? How many bytes you are transmitting on each session ?&amp;nbsp;&amp;nbsp;&lt;br /&gt;&lt;br /&gt;Have you tried to test without USBD and check if the buffer you receive via SPI has the correct data ?&amp;nbsp;&lt;br /&gt;&lt;br /&gt;After you enable USBD, you can also check on the SPI lines with the logic analyzer to see if there is any interruption. You are calling the UART sending from a thread, but I am not sure the UART/USB driver doesn&amp;#39;t use interrupt, I think it does.&amp;nbsp;&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: [SPIM+USBD] Data loss between SPIM and USBD</title><link>https://devzone.nordicsemi.com/thread/503126?ContentTypeID=1</link><pubDate>Thu, 19 Sep 2024 12:27:46 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:df3c2b05-5925-4ac7-8ecd-e2f5252972c4</guid><dc:creator>seongmincho</dc:creator><description>&lt;p&gt;Nice to see you again! You are my teacher!&lt;/p&gt;
&lt;p&gt;1.&amp;nbsp;Your point is valid. Increasing &amp;#39;TIME_TO_WAIT_US&amp;#39; from 45 to 100 allows it to function properly without data loss. However, I need to keep &amp;#39;TIME_TO_WAIT_US&amp;#39; at 45. Is there any other solution?&lt;/p&gt;
&lt;p&gt;2. I am not entirely sure about the specific meaning of &amp;#39;isolate&amp;#39; and &amp;#39;logic analyzer.&amp;#39; &lt;br /&gt;By &amp;#39;isolate,&amp;#39; do you mean flashing only the spim code to check its operation? Or is it about checking the operation of just spim when both spim and usbd code are flashed together? (I used an oscilloscope to check the SPI signals and do not have other measurement hardware available.&amp;nbsp;&lt;span class="emoticon" data-url="https://devzone.nordicsemi.com/cfs-file/__key/system/emoji/1f605.svg" title="Sweat smile"&gt;&amp;#x1f605;&lt;/span&gt;)&lt;/p&gt;
&lt;p&gt;3.&amp;nbsp;Isn&amp;#39;t the interrupt handler always supposed to have a higher priority than task of thread?&amp;nbsp;&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: [SPIM+USBD] Data loss between SPIM and USBD</title><link>https://devzone.nordicsemi.com/thread/503118?ContentTypeID=1</link><pubDate>Thu, 19 Sep 2024 12:13:02 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:bccccc83-75c3-496f-8243-74203170724b</guid><dc:creator>Hung Bui</dc:creator><description>&lt;p&gt;Hi Seongmin,&amp;nbsp;&lt;/p&gt;
&lt;p&gt;As mentioned in the previous ticket, please try to isolate the problem.&amp;nbsp;&lt;br /&gt;Please double check if you give the SPI enough time for communication with the&amp;nbsp;&lt;em&gt;TIME_TO_WAIT_US &lt;/em&gt;time.&amp;nbsp;&lt;/p&gt;
&lt;p&gt;As far as I can see if the SPI is handled by PPI and interrupt handler, it should have higher priority than the USB thread. You need to isolate the issue, try to use a logic analyzer to check the communication on SPI to see if it&amp;#39;s OK or not. Then check the fifo buffer to see if there is any problem.&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item></channel></rss>