<?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 buffer lengths -- how to calculate and specify</title><link>https://devzone.nordicsemi.com/f/nordic-q-a/85380/spim-buffer-lengths----how-to-calculate-and-specify</link><description>I am trying to use SPIM2 on an NRF52840 based board. The SDK is 17.0.2. 
 For the life of me I cannot understand how to &amp;#39;consistently&amp;#39; determine the values to be specified as &amp;#39;tx_length&amp;#39; and &amp;#39;rx_length&amp;#39; to be provided to the calls to the transfer function</description><dc:language>en-US</dc:language><generator>Telligent Community 13</generator><lastBuildDate>Thu, 03 Mar 2022 15:40:03 GMT</lastBuildDate><atom:link rel="self" type="application/rss+xml" href="https://devzone.nordicsemi.com/f/nordic-q-a/85380/spim-buffer-lengths----how-to-calculate-and-specify" /><item><title>RE: SPIM buffer lengths -- how to calculate and specify</title><link>https://devzone.nordicsemi.com/thread/356105?ContentTypeID=1</link><pubDate>Thu, 03 Mar 2022 15:40:03 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:574ae611-d4cd-4072-9a4c-ac570377d4fd</guid><dc:creator>RVM</dc:creator><description>&lt;p&gt;From my understanding, SPI systems are &amp;#39;serial&amp;#39; and &amp;#39;duplex&amp;#39; so I fail to understand why the need to compare the maximum number of transmit bytes to the maximum number of receive bytes -- one should simply pick an order and stream all the bytes sequentially. In my specific case it turns out to be that the SPIM should stream OUT 1 command byte and then stream IN the next 10 receive bytes -- for a total of 11 data transmit clocks. I simply do not see why there is a need to expect any other values for the two &amp;#39;length&amp;#39; parameters (such as the 11 I had to specify to get the SPIM to talk correctly to the MCP2515)&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: SPIM buffer lengths -- how to calculate and specify</title><link>https://devzone.nordicsemi.com/thread/356104?ContentTypeID=1</link><pubDate>Thu, 03 Mar 2022 15:33:09 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:99a96cae-9e60-4dfd-b9b3-7ef0a68d92c3</guid><dc:creator>RVM</dc:creator><description>&lt;p&gt;Perhaps it will help if I paraphrase my original observations.&lt;/p&gt;
&lt;p&gt;It is not the SPIM hardware that is the issue but rather Nordic SDK&amp;#39;s firmware design of the spin lock in the SPIM driver that manages the transfer.&lt;/p&gt;
&lt;p&gt;Going back to my original example of 1 command byte and 10 receive bytes.... I get receive buffer underrun UNLESS I set both the RXLENGTH and TXLENGTH to be (10+1).&lt;/p&gt;
&lt;p&gt;I was expecting that setting TXLENGTH to 1 and RXLENGTH to 10 &amp;#39;should work&amp;#39; (as suggested by both mrono and Jorgen) but it did not -- regardless of the ORC byte.&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: SPIM buffer lengths -- how to calculate and specify</title><link>https://devzone.nordicsemi.com/thread/356052?ContentTypeID=1</link><pubDate>Thu, 03 Mar 2022 13:04:36 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:563f13e8-6f4c-45a7-8ddc-ed667761ef72</guid><dc:creator>J&amp;#248;rgen Holmefjord</dc:creator><description>&lt;p&gt;Hi,&lt;/p&gt;
&lt;p&gt;Although &lt;em&gt;your&lt;/em&gt; SPI slave works this way, does not mean &lt;em&gt;every&lt;/em&gt; SPI slave will work the same way. I&amp;#39;m quite confident that your code will work equally well if you set the tx_length to 1 and rx length to 11, as mentioned by &lt;a href="https://devzone.nordicsemi.com/members/mrono"&gt;mrono&lt;/a&gt;, the SPI peripheral will clock out the ORC byte when the tx_length have been reached. The &lt;em&gt;typical&lt;/em&gt; SPI protocol will output the RX data after receiving the command, but this is not always the case.&lt;/p&gt;
&lt;p&gt;SPI works as a shift register, bits are clocked in and out of the register on each clock cycle. The SPI protocol does not care how the data in the registers are used, but it will always clock in and out data at the same time (given that the MISO/MOSI pins are connected of course). In the&amp;nbsp;typical case described above, you need to set the rx_length to the expected read length + the length of the command, as the SPI peripheral will start filling the RX buffer when the command is clocked out. However, some SPI slave may implement a protocol where you do not need to send a command byte with each read, the slave may be configured to only clock out a preconfigured value on each read. Or the SPI slave can for instance clock out data for the previous command on MISO line while receiving a new command on the MOSI line.&lt;/p&gt;
&lt;p&gt;As you can understand, it is much more flexible to allow the user to specify both tx_length and rx_length as separate parameters, as the lengths will not always be the same.&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><item><title>RE: SPIM buffer lengths -- how to calculate and specify</title><link>https://devzone.nordicsemi.com/thread/355953?ContentTypeID=1</link><pubDate>Thu, 03 Mar 2022 08:57:19 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:fbe56764-feb2-434d-9b4a-e5c6958b6337</guid><dc:creator>mrono</dc:creator><description>&lt;p&gt;This is what the datasheet says:&lt;/p&gt;
&lt;p&gt;When the maximum number of bytes in the receive buffer is larger than the number of bytes in the transmit buffer, the contents of register ORC on page 424 will be transmitted after the last byte in the transmit buffer has been transmitted.&lt;/p&gt;
&lt;p&gt;So it is a feature. No need to specify a tx buffer larger than necessary. I&amp;#39;m not sure how the spim driver handles that. Based on your description not very well.&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item></channel></rss>