<?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>Issues Writing Registers on ADXL375 with SPI</title><link>https://devzone.nordicsemi.com/f/nordic-q-a/50164/issues-writing-registers-on-adxl375-with-spi</link><description>I&amp;#39;m attempting to write a library to allow a NRF52 DK (PCA10040) to read data from a ADXL375 accelerometer through SPI. I&amp;#39;m using SDK 15.0.0. I am able to read from the registers just fine, but cannot write to them. Depending on the circumstances, it</description><dc:language>en-US</dc:language><generator>Telligent Community 13</generator><lastBuildDate>Mon, 29 Jul 2019 16:06:29 GMT</lastBuildDate><atom:link rel="self" type="application/rss+xml" href="https://devzone.nordicsemi.com/f/nordic-q-a/50164/issues-writing-registers-on-adxl375-with-spi" /><item><title>RE: Issues Writing Registers on ADXL375 with SPI</title><link>https://devzone.nordicsemi.com/thread/201049?ContentTypeID=1</link><pubDate>Mon, 29 Jul 2019 16:06:29 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:05813e67-5e4b-4c2e-b6d4-1c93046440d7</guid><dc:creator>asharkl</dc:creator><description>&lt;p&gt;You are completely right, thank you for pointing that out. In this case it did not cause any problems because it address is being OR-ed with 0x00 (ADXL375_SPI_WRITE) so functionally does not change, but for a more generalized solution the OR-ing should happen before copying into packet[].&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: Issues Writing Registers on ADXL375 with SPI</title><link>https://devzone.nordicsemi.com/thread/200621?ContentTypeID=1</link><pubDate>Thu, 25 Jul 2019 23:47:25 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:d8862aef-5afb-4da3-86bc-06416e739b5d</guid><dc:creator>awneil</dc:creator><description>&lt;p&gt;This doesn&amp;#39;t look right:&lt;/p&gt;
&lt;p&gt;&lt;pre class="ui-code" data-mode="c_cpp"&gt;    uint8_t packet[2] = {address, data};
    address = address | ADXL375_SPI_WRITE;    
    err_code = nrf_drv_spi_transfer(p_spi_master, packet, 2, NULL, 0);&lt;/pre&gt;&lt;/p&gt;
&lt;p&gt;You copy the address into packet[0], and you use the packet[] array for your transfer.&lt;/p&gt;
&lt;p&gt;Therefore OR-ing the address with&amp;nbsp;ADXL375_SPI_WRITE &lt;em&gt;after&lt;/em&gt; you&amp;#39;ve copied it into the array won&amp;#39;t have any effect on the transmission!&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: Issues Writing Registers on ADXL375 with SPI</title><link>https://devzone.nordicsemi.com/thread/200606?ContentTypeID=1</link><pubDate>Thu, 25 Jul 2019 18:26:06 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:96ec6118-6ab3-4e93-854d-190bcc5745b0</guid><dc:creator>asharkl</dc:creator><description>&lt;p&gt;Thank you all for the help. I rewrote the SPITransfer function using an array as suggested by &lt;span class="user-name"&gt;&lt;a class="internal-link view-user-profile" href="https://devzone.nordicsemi.com/members/hmolesworth"&gt;hmolesworth &lt;/a&gt;&lt;/span&gt;, and made sure that I had somewhere to store my data. The biggest difference was made by sending the write data as an array instead of 2 separate transmissions. The code that worked correctly was this, case this is of use to anyone:&lt;/p&gt;
&lt;p&gt;&lt;pre class="ui-code" data-mode="c_cpp"&gt;uint32_t oneByteWrite(uint8_t address, uint8_t data) 
{
    uint32_t err_code;
    uint8_t packet[2] = {address, data};
    address = address | ADXL375_SPI_WRITE;    
    err_code = nrf_drv_spi_transfer(p_spi_master, packet, 2, NULL, 0);
    if(err_code != NRF_SUCCESS)
    {
        return err_code;
    }
    return err_code;
}&lt;/pre&gt;&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: Issues Writing Registers on ADXL375 with SPI</title><link>https://devzone.nordicsemi.com/thread/200588?ContentTypeID=1</link><pubDate>Thu, 25 Jul 2019 15:33:34 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:522b79c4-5f81-4429-a8a6-afa9787702ea</guid><dc:creator>awneil</dc:creator><description>[quote userid="65515" url="~/f/nordic-q-a/50164/issues-writing-registers-on-adxl375-with-spi/200583"]the use of SPI in not quite as you hope[/quote]
&lt;p&gt;Indeed.&lt;/p&gt;
&lt;p&gt;&lt;pre class="ui-code" data-mode="c_cpp"&gt;    uint8_t p_tx_data;
    uint8_t p_rx_data;&lt;/pre&gt;&lt;/p&gt;
&lt;p&gt;&lt;a href="https://devzone.nordicsemi.com/members/asharkl"&gt;asharkl&lt;/a&gt; -&amp;nbsp;The use of &amp;quot;p_&amp;quot; as a prefix like that usually indicates that the variable is a &lt;em&gt;pointer&lt;/em&gt;.&lt;/p&gt;
&lt;p&gt;Is that what you intended?&lt;/p&gt;
&lt;p&gt;If so, you also need to define somewhere to store the actual data!&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: Issues Writing Registers on ADXL375 with SPI</title><link>https://devzone.nordicsemi.com/thread/200583?ContentTypeID=1</link><pubDate>Thu, 25 Jul 2019 15:18:18 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:ff11b37c-9e04-493b-a562-3094e146af7d</guid><dc:creator>hmolesworth</dc:creator><description>&lt;p&gt;I can&amp;#39;t spend more than a few minutes, but it&amp;#39;s clear the use of SPI in not quite as you hope. The code above shows this:&lt;/p&gt;
&lt;p&gt;&lt;pre class="ui-code" data-mode="c_cpp"&gt;uint8_t SPItransfer(uint8_t txData)
{
    uint8_t p_tx_data;
    uint8_t p_rx_data;
    p_tx_data = txData;
    uint32_t err_code = nrf_drv_spi_transfer(p_spi_master, &amp;amp;p_tx_data, 1, &amp;amp;p_rx_data, 2);
    APP_ERROR_CHECK(err_code);
    return p_rx_data;
}&lt;/pre&gt;&lt;/p&gt;
&lt;p&gt;That code will not work correctly, since the rx buffer will corrupt the stack when 2 bytes are stuffed into an area for 1 byte. This is a corrected version:&lt;/p&gt;
&lt;p&gt;&lt;pre class="ui-code" data-mode="c_cpp"&gt;uint8_t SPItransfer(uint8_t txData)
{
    uint8_t p_tx_data;
    uint8_t p_rx_data[2];  // Make this an array to receive more bytes, 2 in this case
    p_tx_data = txData;
    uint32_t err_code = nrf_drv_spi_transfer(p_spi_master, &amp;amp;p_tx_data, 1, &amp;amp;p_rx_data, 2);
    APP_ERROR_CHECK(err_code);
    return p_rx_data[1];  // Return the second byte, not the first
}&lt;/pre&gt;&lt;/p&gt;
&lt;p&gt;This corrected version only works for a single command byte expecting a single response byte which will be the second of two bytes received over SPI. To read more than a single byte after a single command byte do not use a loop, ask for all the (sequential) bytes in a single transfer.&lt;/p&gt;
&lt;p&gt;&lt;pre class="ui-code" data-mode="c_cpp"&gt;uint8_t tx_data;
uint8_t rx_data[20];  // Make this an array to receive lots ofbytes, say 20

uint32_t SPItransfer(uint8_t txData)
{
    tx_data = txData;
    uint32_t err_code = nrf_drv_spi_transfer(p_spi_master, &amp;amp;tx_data, 1, &amp;amp;rx_data, sizeof(rx_data));
    APP_ERROR_CHECK(err_code);
    return err_code;  // Return the error code, look in p_rx_data for response string
}

void multiByteRead(int startAddress, char* buffer, int size) {
    uint8_t tx = (ADXL375_SPI_READ | ADXL375_MULTI_BYTE | (startAddress &amp;amp; 0x3F));
    //Send address to start reading from.
    SPItransfer(tx);
    for (int i = 0; i &amp;lt; size; i++) {
        // this is just a simple example, but try not to be silly
        if (i &amp;gt;= sizeof(rx_data)) break;
        buffer[i] = rx_data[i];
    }
}
&lt;/pre&gt;&lt;/p&gt;
&lt;p&gt;There are other potential issues; you might have to increase port pin drive strength on the SCK pin; 4MHz might be too high a clock speed so maybe try something lower until it works then speed up until it doesn&amp;#39;t; the accelerometer may require a delay between the command byte and the subsequent response bytes (some do, some don&amp;#39;t).&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: Issues Writing Registers on ADXL375 with SPI</title><link>https://devzone.nordicsemi.com/thread/200452?ContentTypeID=1</link><pubDate>Thu, 25 Jul 2019 09:34:22 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:3d3c7814-c818-41bb-8439-9af77b2d9a08</guid><dc:creator>awneil</dc:creator><description>&lt;p&gt;You&amp;#39;re welcome.&lt;/p&gt;
&lt;p&gt;Yes, it is particularly badly presented!&lt;/p&gt;
&lt;p&gt;You may wish to provide feedback here:&lt;/p&gt;
&lt;p&gt;&lt;a href="https://devzone.nordicsemi.com/nordic/backstage/f/backstage-forum/41454/options-for-posting-source-code-are-confusing"&gt;https://devzone.nordicsemi.com/nordic/backstage/f/backstage-forum/41454/options-for-posting-source-code-are-confusing&lt;/a&gt;&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: Issues Writing Registers on ADXL375 with SPI</title><link>https://devzone.nordicsemi.com/thread/200447?ContentTypeID=1</link><pubDate>Thu, 25 Jul 2019 09:22:39 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:83328824-8b92-43dd-9c91-23856acbb367</guid><dc:creator>awneil</dc:creator><description>[quote userid="13562" url="~/f/nordic-q-a/50164/issues-writing-registers-on-adxl375-with-spi/200429"]the signal looks good I guess[/quote]
&lt;p&gt;It looks like the scope is in &amp;quot;digital&amp;quot; mode - so it won&amp;#39;t be showing any analogue &amp;quot;anomolies&amp;quot; ... ?&lt;/p&gt;
[quote userid="13562" url="~/f/nordic-q-a/50164/issues-writing-registers-on-adxl375-with-spi/200429"]I suggest you also get an analog scope of the signal[/quote]
&lt;p&gt;Indeed - for the above reason!&lt;/p&gt;
&lt;p&gt;&lt;a href="https://devzone.nordicsemi.com/members/asharkl"&gt;asharkl&lt;/a&gt; -&amp;nbsp;Presumably, this scope has the facility to decode serial bus comms?&lt;/p&gt;
&lt;p&gt;If you don&amp;#39;t already have it, now would be a good time to at least try the free evaluation!&lt;/p&gt;
[quote userid="13562" url="~/f/nordic-q-a/50164/issues-writing-registers-on-adxl375-with-spi/200429"] I suggest you ask AD for help as well[/quote]
&lt;p&gt;+1&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: Issues Writing Registers on ADXL375 with SPI</title><link>https://devzone.nordicsemi.com/thread/200429?ContentTypeID=1</link><pubDate>Thu, 25 Jul 2019 08:27:03 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:b2652350-e566-4fce-a868-9cbfaea5fc59</guid><dc:creator>haakonsh</dc:creator><description>&lt;p&gt;I can&amp;#39;t really read what the bits states on the data lines are, but the signal looks good I guess. &lt;br /&gt;&lt;br /&gt;I suggest you also get an analog scope of the signal.&lt;br /&gt;&lt;br /&gt;If the SPI communication checks out then I suggest you ask AD for help as well, they should be able to help you. Have you tried their driver?&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: Issues Writing Registers on ADXL375 with SPI</title><link>https://devzone.nordicsemi.com/thread/200356?ContentTypeID=1</link><pubDate>Wed, 24 Jul 2019 20:45:12 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:e66b88af-2d8a-439f-b04c-718e3d7bae2a</guid><dc:creator>asharkl</dc:creator><description>&lt;p&gt;I removed the manual control over the SS pin and my read function still works but the write function is still broken. I have verified proper procedure against the ADXL data sheet. Here is the SPI Write procedure as per the data sheet.&lt;/p&gt;
&lt;p&gt;&lt;img alt=" " height="193" src="https://devzone.nordicsemi.com/cfs-file/__key/communityserver-discussions-components-files/4/ADXL_5F00_SPI_5F00_write.bmp" width="640" /&gt;&lt;/p&gt;
&lt;p&gt;In order to set the ADXL into measurement mode, 0x08 must be sent to the Power Control Register (0x2D). As per the data sheet I OR&amp;#39;ed the SPI Write command (0x00) with the address and sent that via SPI, immediately followed by sending 0x08. Below is a scope image of just a single write.&amp;nbsp;&lt;img alt=" " src="https://devzone.nordicsemi.com/cfs-file/__key/communityserver-discussions-components-files/4/scope_5F00_6.bmp" /&gt;&lt;/p&gt;
&lt;p&gt;And below is a picture of a single write, immediately followed by attempting to read the register. Even though I sent 0x08 to the Power Control register, it reads as 0x0C regardless of what is sent. The same applies to all the register I write to, they will get set to an arbitrary value and nothing short of power cycling the board will get it to revert to the default value.&lt;/p&gt;
&lt;p&gt;&lt;img alt=" " src="https://devzone.nordicsemi.com/cfs-file/__key/communityserver-discussions-components-files/4/8877.scope_5F00_7.bmp" /&gt;&lt;/p&gt;
&lt;p&gt;Thank you for your help, and please let me know if you require closeups of any part of the logic signal.&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: Issues Writing Registers on ADXL375 with SPI</title><link>https://devzone.nordicsemi.com/thread/200354?ContentTypeID=1</link><pubDate>Wed, 24 Jul 2019 20:24:52 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:bbdbd447-36f8-4fb3-a8f0-8a4648d25fea</guid><dc:creator>asharkl</dc:creator><description>&lt;p&gt;Thank you for that! I could not for the life of me figure out how to do that before.&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: Issues Writing Registers on ADXL375 with SPI</title><link>https://devzone.nordicsemi.com/thread/200223?ContentTypeID=1</link><pubDate>Wed, 24 Jul 2019 08:35:34 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:40f40ef8-b328-4e9d-a23d-dd5641db3d3f</guid><dc:creator>awneil</dc:creator><description>&lt;p&gt;How to properly post source code:&lt;/p&gt;
&lt;p&gt;&lt;img alt=" " src="https://devzone.nordicsemi.com/resized-image/__size/320x240/__key/communityserver-discussions-components-files/4/73348._5F00_Insert-Code-_2D00_-Nordic.png" /&gt;&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: Issues Writing Registers on ADXL375 with SPI</title><link>https://devzone.nordicsemi.com/thread/200215?ContentTypeID=1</link><pubDate>Wed, 24 Jul 2019 08:19:02 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:ed4c9a91-2c25-4c04-be4b-3be4511aed22</guid><dc:creator>haakonsh</dc:creator><description>&lt;p&gt;First, you should not need to control the SS pin manually, the SPI driver will handle it.&amp;nbsp;&lt;/p&gt;
&lt;p&gt;Second, writing certain registers might require a special procedure of some kind. I suggest you triple-check with the adxl datasheet.&amp;nbsp;&lt;br /&gt;&lt;br /&gt;Do you have a scope of the SPI writes?&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item></channel></rss>