<?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>How to read register from ADXL362 via SPI</title><link>https://devzone.nordicsemi.com/f/nordic-q-a/19194/how-to-read-register-from-adxl362-via-spi</link><description>Hi, 
 I have written a short driver for the ADXL362 accelerometer, which I&amp;#39;m attaching here, and I&amp;#39;ve connected breakout board of the accelerometer from Sparkfun to the nRF51 DK as specified in the picture from the datasheet. I also connected the accelerometer</description><dc:language>en-US</dc:language><generator>Telligent Community 13</generator><lastBuildDate>Fri, 25 Oct 2019 18:12:48 GMT</lastBuildDate><atom:link rel="self" type="application/rss+xml" href="https://devzone.nordicsemi.com/f/nordic-q-a/19194/how-to-read-register-from-adxl362-via-spi" /><item><title>RE: How to read register from ADXL362 via SPI</title><link>https://devzone.nordicsemi.com/thread/216918?ContentTypeID=1</link><pubDate>Fri, 25 Oct 2019 18:12:48 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:f5a3a72d-71ff-42e2-9cb4-2a2d460a778e</guid><dc:creator>Francisco Martinez</dc:creator><description>&lt;p&gt;Padmashree,&lt;/p&gt;
&lt;p&gt;Please see the code on my reply to Bjorn-spockeli.&lt;/p&gt;
&lt;p&gt;Best regards,&lt;/p&gt;
&lt;p&gt;-Francisco&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: How to read register from ADXL362 via SPI</title><link>https://devzone.nordicsemi.com/thread/216917?ContentTypeID=1</link><pubDate>Fri, 25 Oct 2019 17:34:32 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:0b920387-0190-48e2-97af-923b0796d8e7</guid><dc:creator>Francisco Martinez</dc:creator><description>&lt;p&gt;Bjorn,&lt;/p&gt;
&lt;p&gt;I modified the code quite a lot to have the main application configure the driver, and just have the driver run according to that settings. However, using Roger&amp;#39;s suggestion is quite easy. To read a register from the accelerometer, you will need a transmit buffer 3 bytes long. One to store the read command (defined ADXL362_READ_REG_CMD), which is 0x0B, the second byte needs to contain the register&amp;#39;s address, which in my case is 0x00, and the last byte can be whatever you want. You will need this last byte so while you are transmitting this last one, you will be reading the content of the register you requested. As pointed out by Roger, while you transmit the first two bytes of the tx buffer, you will also be reading bytes that you can ignore and the content of the register read will be on rx_buffer[2] or the third byte of the rx buffer.&lt;/p&gt;
&lt;p&gt;This is the code:&lt;/p&gt;
&lt;p&gt;&lt;/p&gt;
&lt;p&gt;&lt;pre class="ui-code" data-mode="text"&gt;/**@brief Function for reading ADXL362 registers over SPI.
 *
 * @param[in] address 		ADXL362 Register address
 * @param[in] p_rx_buffer 	Pointer to RX buffer
 * @param[in] bytes 		Number of bytes to be read
 *
 */
void adxl362_spi_drv_readRegister(uint8_t address, uint8_t * p_rx_buffer, uint8_t bytes)
{
	uint32_t err_code;

	//Prepare tx_buffer
	uint8_t tx_buffer[3];
	tx_buffer[0] = ADXL362_READ_REG_CMD;
	tx_buffer[1] = address;
	tx_buffer[2] = 0x00;

	// Pull chip select line low
	nrf_gpio_pin_clear(m_spi_config-&amp;gt;ss_pin);

	err_code = nrf_drv_spi_transfer(m_spi_master, tx_buffer, sizeof(tx_buffer), p_rx_buffer, sizeof(p_rx_buffer));

	// Set chip select line high
	nrf_gpio_pin_set(m_spi_config-&amp;gt;ss_pin);

	APP_ERROR_CHECK(err_code);

}&lt;/pre&gt;&lt;/p&gt;
&lt;p&gt;As you can see, the trick is to add an additional byte to the transmit buffer. I you want to receive more than one byte back, you would need to add those additional bytes so as you are sending them, you receive the same number of bytes.&lt;/p&gt;
&lt;p&gt;The new function is pretty much the same, except for a few pointers I used to store SPI configuration and to raise or lower the chip select line required by the SPI protocol.&lt;/p&gt;
&lt;p&gt;Hope this helps.&lt;/p&gt;
&lt;p&gt;-Francisco&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: How to read register from ADXL362 via SPI</title><link>https://devzone.nordicsemi.com/thread/216596?ContentTypeID=1</link><pubDate>Thu, 24 Oct 2019 11:40:09 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:acfab8fa-8615-4dfd-af0f-35e64a50b2b5</guid><dc:creator>bjorn-spockeli</dc:creator><description>&lt;p&gt;&lt;a href="https://devzone.nordicsemi.com/members/pemex44"&gt;Francisco Martinez&lt;/a&gt;: Would it be possible for you share the code with the fix?&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: How to read register from ADXL362 via SPI</title><link>https://devzone.nordicsemi.com/thread/215822?ContentTypeID=1</link><pubDate>Mon, 21 Oct 2019 07:09:06 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:61632b7f-25a3-4422-ae07-188a6cc2378c</guid><dc:creator>Padmashree N S</dc:creator><description>&lt;p&gt;Hi,&lt;/p&gt;
&lt;p&gt;I had the same problem with my code. I did all the changes suggested. I made the TX and RX buffer size same, but I am still not getting the required result. Are there any other changes to be done? I have the same code as attached to this. Please help.&lt;/p&gt;
&lt;p&gt;Thank you&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: How to read register from ADXL362 via SPI</title><link>https://devzone.nordicsemi.com/thread/74343?ContentTypeID=1</link><pubDate>Fri, 10 Feb 2017 20:21:46 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:8d62230c-224e-4985-93d6-5dbd9b6c20b0</guid><dc:creator>Roger Clark</dc:creator><description>&lt;p&gt;No worries ;-)&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: How to read register from ADXL362 via SPI</title><link>https://devzone.nordicsemi.com/thread/74342?ContentTypeID=1</link><pubDate>Fri, 10 Feb 2017 18:22:37 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:a6e2a81a-b3dc-4962-a21d-e79a64f9f891</guid><dc:creator>Francisco Martinez</dc:creator><description>&lt;p&gt;Roger,&lt;/p&gt;
&lt;p&gt;I modified my code according to your suggestion and it worked like a charm! Thanks a lot for your help.&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: How to read register from ADXL362 via SPI</title><link>https://devzone.nordicsemi.com/thread/74341?ContentTypeID=1</link><pubDate>Fri, 27 Jan 2017 06:17:30 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:c5c20bdb-dde5-48b2-b584-f6b723d5a78d</guid><dc:creator>Roger Clark</dc:creator><description>&lt;p&gt;No worries ;-)&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: How to read register from ADXL362 via SPI</title><link>https://devzone.nordicsemi.com/thread/74340?ContentTypeID=1</link><pubDate>Thu, 26 Jan 2017 21:35:54 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:ba751693-f1f4-4412-a94a-1c5bca709540</guid><dc:creator>Francisco Martinez</dc:creator><description>&lt;p&gt;Thanks a lot for the info Roger. I&amp;#39;ll modify my code an try that. If you don&amp;#39;t mind it, I&amp;#39;ll get back to you to let you know how it went&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: How to read register from ADXL362 via SPI</title><link>https://devzone.nordicsemi.com/thread/74339?ContentTypeID=1</link><pubDate>Thu, 26 Jan 2017 04:28:15 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:15c281d6-7e68-4c96-a487-a47294e1f1b7</guid><dc:creator>Roger Clark</dc:creator><description>&lt;p&gt;I can&amp;#39;t see anything in the API call spec that says it does what you think it does&lt;/p&gt;
&lt;p&gt;In my experience (albeit I may be using this the wrong way)&lt;/p&gt;
&lt;p&gt;The RX buffer gets filled for every TX byte that is sent.&lt;/p&gt;
&lt;p&gt;I think the reason why RX buffer size can be set (or even set to null) is that if you do not expect a response to the write , you do not need to have an RX buf at all.&lt;/p&gt;
&lt;p&gt;Anyway, my code works for me.&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: How to read register from ADXL362 via SPI</title><link>https://devzone.nordicsemi.com/thread/74338?ContentTypeID=1</link><pubDate>Thu, 26 Jan 2017 03:24:25 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:e381ff46-0f9e-40a4-869e-27baa60a1f3f</guid><dc:creator>Francisco Martinez</dc:creator><description>&lt;p&gt;Roger,&lt;/p&gt;
&lt;p&gt;Thank you for your answer. However, I was under the impression that calling the &amp;#39;nrf_drv_spi_transfer&amp;#39; function with the &amp;#39;tx_buffer_length&amp;#39; parameter made the driver rise the clock enough times to output the whole tx_buffer and then rise the clock enough times to let all the bytes specified in the &amp;#39;bytes&amp;#39; parameter to come in while outputing 0xCC as specified in the .orc field in the nrf_drv_spi_config_t structure.&lt;/p&gt;
&lt;p&gt;Now I get from your comment that this is not so. Is that correct?&lt;/p&gt;
&lt;p&gt;Thanks in advance for all your help.&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: How to read register from ADXL362 via SPI</title><link>https://devzone.nordicsemi.com/thread/74337?ContentTypeID=1</link><pubDate>Tue, 24 Jan 2017 03:09:11 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:4a79ae27-f275-485c-9d96-82fef6d7f895</guid><dc:creator>Roger Clark</dc:creator><description>&lt;p&gt;Your response buffer needs to be at least as large as your transmit buffer&lt;/p&gt;
&lt;p&gt;I don&amp;#39;t think you understand how SPI works.&lt;/p&gt;
&lt;p&gt;For every clock cycle, generated by the nRF51, one &amp;quot;bit&amp;quot; of data gets clocked out of the nRF51 into the peripheral, and one bit gets clocked back into the nRF51 from the peripheral&lt;/p&gt;
&lt;p&gt;So in your case, if the protocol has 2 bytes of command data (1 byte to indicate a command plus one byte for the actual command)&lt;/p&gt;
&lt;p&gt;You will need to sent those 2 bytes, plus an additional byte containing zero (or anything really), as the extra byte is where the response from the peripheral is clocked back into the nRF51&lt;/p&gt;
&lt;p&gt;So both your TX and RX buffers need to be 3 bytes, assuming the response is just one byte.&lt;/p&gt;
&lt;p&gt;And you will need to look in rxbuf[2] for the response.&lt;/p&gt;
&lt;p&gt;Note. This is not specific to the nRF51, most MCU&amp;#39;s operate the same way&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item></channel></rss>