<?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 write and read Multiple bytes.</title><link>https://devzone.nordicsemi.com/f/nordic-q-a/99486/spi-write-and-read-multiple-bytes</link><description>Hi All, 
 I am using nrf52840 in my project and working on sdk 17.0.2. I have an SPI based IC MAX3421E connected to my device. I am able to read and write single bytes through SPI code. But I am having issues with reading and writing multiple bytes. I</description><dc:language>en-US</dc:language><generator>Telligent Community 13</generator><lastBuildDate>Fri, 14 Jul 2023 11:32:48 GMT</lastBuildDate><atom:link rel="self" type="application/rss+xml" href="https://devzone.nordicsemi.com/f/nordic-q-a/99486/spi-write-and-read-multiple-bytes" /><item><title>RE: SPI write and read Multiple bytes.</title><link>https://devzone.nordicsemi.com/thread/436461?ContentTypeID=1</link><pubDate>Fri, 14 Jul 2023 11:32:48 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:52423b20-3d85-4de9-9389-5a17158b14bf</guid><dc:creator>sne_333</dc:creator><description>&lt;p&gt;&lt;a href="https://devzone.nordicsemi.com/members/joh2"&gt;Jørgen Holmefjord&lt;/a&gt;&amp;nbsp;Thank you so much. This solved my issue.&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: SPI write and read Multiple bytes.</title><link>https://devzone.nordicsemi.com/thread/436426?ContentTypeID=1</link><pubDate>Fri, 14 Jul 2023 09:25:04 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:52e7a93f-d248-4f88-a685-eedb799d43e0</guid><dc:creator>J&amp;#248;rgen Holmefjord</dc:creator><description>&lt;p&gt;Something like this would be equivalent for Nordic TWI driver API:&lt;/p&gt;
&lt;p&gt;&lt;pre class="ui-code" data-mode="text"&gt;char* MAXbytes_wr( uint8_t reg, uint8_t nbytes, char* data )
{
	static uint8_t spi_tx_buf[nbytes + 1];	// Declare buffer that can hold reg address + data
	spi_tx_buf[0] = reg;					// Set first byte of TX buffer to register address
	memcpy(&amp;amp;spi_tx_buf[1], data, nbytes);	// Copy data to TX buffer
	
	nrf_gpio_pin_clear(SPI_SS_PIN);			// assert SS

	nrf_drv_spi_transfer(&amp;amp;m_spi, spi_tx_buf, nbytes+1 , NULL, 0);  // No need for a while loop handling each byte separately. SPI driver will transfer all bytes in one go.

	while(!spi_xfer_done)					// Wait for transfer to complete
	{
		spi_xfer_done = false;
	}

	nrf_gpio_pin_set(SPI_SS_PIN);			// deassert SS
	return( data );
}&lt;/pre&gt;&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: SPI write and read Multiple bytes.</title><link>https://devzone.nordicsemi.com/thread/436395?ContentTypeID=1</link><pubDate>Fri, 14 Jul 2023 07:48:56 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:cced516a-c9cb-4c25-ac47-2d13bc5d9034</guid><dc:creator>sne_333</dc:creator><description>&lt;p&gt;Hi Jorgen, can you help me with converting this function??&lt;/p&gt;
&lt;p&gt;I did conversion like mentioned above but its not working. Please help.&lt;/p&gt;
&lt;p&gt;char* MAXbytes_wr( BYTE reg, BYTE nbytes, char* data )&lt;br /&gt;{&lt;br /&gt; Select_MAX3421E; //assert SS&lt;br /&gt; SPI_wr ( reg + 2 ); //set W/R bit and select register &lt;br /&gt; while( nbytes ) { &lt;br /&gt; SPI_wr( *data ); // send the next data byte&lt;br /&gt; data++; // advance the pointer&lt;br /&gt; nbytes--;&lt;br /&gt; }&lt;br /&gt; Deselect_MAX3421E; //deassert SS&lt;br /&gt; return( data );&lt;br /&gt;}&lt;/p&gt;
&lt;p&gt;&lt;/p&gt;
&lt;p&gt;Regards,&lt;br /&gt;Snehal&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: SPI write and read Multiple bytes.</title><link>https://devzone.nordicsemi.com/thread/424541?ContentTypeID=1</link><pubDate>Tue, 09 May 2023 12:31:12 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:587ddd8e-398a-4d3d-9c2f-bdc443926417</guid><dc:creator>J&amp;#248;rgen Holmefjord</dc:creator><description>&lt;p&gt;Hi,&lt;/p&gt;
&lt;p&gt;The warning is likely because you are passing the reference to &amp;#39;data&amp;#39; to nrf_drv_spi_transfer(), while data is already a pointer in the input to the function.&lt;/p&gt;
&lt;p&gt;I think your approach of translating each line of the original function to a similar line in nRF code is not optimal, you should rather understand how the SPI driver works and rewrite the entire function to give the same functionality.&lt;/p&gt;
&lt;p&gt;I would do something like this:&lt;/p&gt;
&lt;p&gt;&lt;pre class="ui-code" data-mode="c_cpp"&gt;char* MAXbytes_rd ( uint8_t reg, uint8_t nbytes, char* data )
{

spi_tx_buf[0] = reg;    //Only need to send the actual register address, the driver will automatically clock out OCR byte to clock in data in RX buffer

nrf_gpio_pin_clear(SPI_SS_PIN);

nrf_drv_spi_transfer(&amp;amp;m_spi, spi_tx_buf, 1, data, nbytes);  // No need for a while loop handling each byte separately. SPI driver will fill buffer with requested number of bytes (nbytes)

while(!spi_xfer_done)   // Wait for transfer to complete
spi_xfer_done = false;

nrf_gpio_pin_set(SPI_SS_PIN);


return( data );

}&lt;/pre&gt;&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: SPI write and read Multiple bytes.</title><link>https://devzone.nordicsemi.com/thread/424355?ContentTypeID=1</link><pubDate>Mon, 08 May 2023 16:21:32 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:117ee1f2-f307-450f-a06a-559f2bf87089</guid><dc:creator>sne_333</dc:creator><description>&lt;p&gt;Hi Jorgen,&lt;/p&gt;
&lt;p&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;Yes I misunderstood. you are right, the error code&amp;nbsp;was returned instead of data. I modified the code in following way but still it doesnt seem to work. Gives me a warning on&amp;nbsp;&lt;span&gt;nrf_drv_spi_transfer line.&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;char* MAXbytes_rd ( uint8_t reg, uint8_t nbytes, char* data )&lt;br /&gt;{&lt;/p&gt;
&lt;p&gt;spi_tx_buf[0] = reg;&lt;br /&gt; spi_tx_buf[1] = 0x00;&lt;/p&gt;
&lt;p&gt;nrf_gpio_pin_clear(SPI_SS_PIN);&lt;/p&gt;
&lt;p&gt;while( nbytes ) {&lt;br /&gt;&amp;nbsp; nrf_drv_spi_transfer(&amp;amp;m_spi, spi_tx_buf, 2, &amp;amp;data, nbytes);&lt;br /&gt;&amp;nbsp; data++;&lt;br /&gt;&amp;nbsp; nbytes--;&lt;br /&gt;}&lt;br /&gt; nrf_gpio_pin_set(SPI_SS_PIN);&lt;/p&gt;
&lt;p&gt;&lt;br /&gt; return( data );&lt;/p&gt;
&lt;p&gt;}&lt;/p&gt;
&lt;p&gt;Is still something wrong here?&lt;/p&gt;
&lt;p&gt;Regards,&lt;/p&gt;
&lt;p&gt;Snehal&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: SPI write and read Multiple bytes.</title><link>https://devzone.nordicsemi.com/thread/424351?ContentTypeID=1</link><pubDate>Mon, 08 May 2023 15:32:46 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:1aa87d54-2da1-4211-9ed0-89c9b7b43e1d</guid><dc:creator>J&amp;#248;rgen Holmefjord</dc:creator><description>&lt;p&gt;Hi,&lt;/p&gt;
&lt;p&gt;You should make sure to check the error codes returned from functions like&amp;nbsp;nrf_drv_spi_transfer(), in&amp;nbsp;your functions you do not do this. It also looks like you have misunderstood&amp;nbsp;the return value from this function in&amp;nbsp;MAXbytes_rd(), it only returns an error code, not the data received by the SPI. Normally, the SPI driver is non-blocking, which means that you have to wait for a transfer to complete before starting a new one, for instance by waiting for the&amp;nbsp;spi_xfer_done flag like you have in one of the functions.&amp;nbsp;&lt;/p&gt;
&lt;p&gt;It should be sufficient to call&amp;nbsp;nrf_drv_spi_transfer() once with the requested number of TX and RX bytes. Note that RX and TX buffer will be filled at the same time, meaning that if you want to receive a number of bytes&amp;nbsp;&lt;em&gt;after all data in the TX buffer have been clocked out&lt;/em&gt;, you need to set the RX length to TX length + number of expected RX bytes, the bytes clocked into the RX buffer while clocking out TX buffer can be ignored.&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>