<?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 connection to ADXL345 with Nrf52840</title><link>https://devzone.nordicsemi.com/f/nordic-q-a/92339/spi-connection-to-adxl345-with-nrf52840</link><description>Hi, 
 I&amp;#39;m trying to test a simple SPI communication between nrf52840 dev board and adxl345. I&amp;#39;m following this guy&amp;#39;s youtube tutorial on segger embedded studio. Here&amp;#39;s the code to read the device ID and also the hardware setup. I&amp;#39;m getting 242 instead</description><dc:language>en-US</dc:language><generator>Telligent Community 13</generator><lastBuildDate>Thu, 29 Sep 2022 11:25:43 GMT</lastBuildDate><atom:link rel="self" type="application/rss+xml" href="https://devzone.nordicsemi.com/f/nordic-q-a/92339/spi-connection-to-adxl345-with-nrf52840" /><item><title>RE: SPI connection to ADXL345 with Nrf52840</title><link>https://devzone.nordicsemi.com/thread/388550?ContentTypeID=1</link><pubDate>Thu, 29 Sep 2022 11:25:43 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:4b06cd66-2836-45a3-a6e4-c14ad32e8a94</guid><dc:creator>J&amp;#248;rgen Holmefjord</dc:creator><description>&lt;p&gt;According to the &lt;a href="https://www.analog.com/media/en/technical-documentation/data-sheets/adxl345.pdf#page=14"&gt;datasheet of ADXL345&lt;/a&gt;:&lt;/p&gt;
&lt;p&gt;&amp;quot;&amp;nbsp;The maximum SPI clock speed is 5 MHz with 100 pF maximum loading, and &lt;span style="background-color:rgba(255, 255, 0, 1);"&gt;the timing scheme follows clock polarity (CPOL) = 1 and clock phase (CPHA) = 1&lt;/span&gt;.&amp;quot;&lt;/p&gt;
&lt;p&gt;See &lt;a href="https://infocenter.nordicsemi.com/topic/ps_nrf52840/spim.html#concept_v4x_5w2_wr"&gt;table 2 in nRF52840 SPIM peripheral documentation&lt;/a&gt;.&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: SPI connection to ADXL345 with Nrf52840</title><link>https://devzone.nordicsemi.com/thread/388507?ContentTypeID=1</link><pubDate>Thu, 29 Sep 2022 09:31:42 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:b909fe28-8cb6-4717-ab9b-1eab6501f158</guid><dc:creator>Naing</dc:creator><description>&lt;p&gt;I just tried out with SPI_MODE_3 and it seems be working. I don&amp;#39;t know why it wouldn&amp;#39;t work with SPI_MODE_0. Any thoughts?&lt;/p&gt;
&lt;p&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/pastedimage1664443860339v1.png" alt=" " /&gt;&amp;nbsp;&lt;pre class="ui-code" data-mode="text"&gt;#include &amp;quot;nrf_drv_spi.h&amp;quot;
#include &amp;quot;app_util_platform.h&amp;quot;
#include &amp;quot;nrf_gpio.h&amp;quot;
#include &amp;quot;nrf_delay.h&amp;quot;
#include &amp;quot;boards.h&amp;quot;
#include &amp;quot;app_error.h&amp;quot;
#include &amp;lt;string.h&amp;gt;
#include &amp;quot;nrf_log.h&amp;quot;
#include &amp;quot;nrf_log_ctrl.h&amp;quot;
#include &amp;quot;nrf_log_default_backends.h&amp;quot;

#define ADD_REG_WHO_AM_I 0x00
#define UC_WHO_AM_I_DEFAULT_VALUE 0xE5


#define SPI_BUFSIZE 8     // SPI communication buffer size
#define SPI_INSTANCE 0    // SPI instance to be used
#define SET_READ_SINGLE_CMD(x) (x | 0x80)   // local macro - set read single command (DEPENDS ON SENSOR)

uint8_t spi_tx_buf[SPI_BUFSIZE];    // spi tx buffer
uint8_t spi_rx_buf[SPI_BUFSIZE];    // spi rx buffer

static volatile bool spi_xfer_done; // flag to signal that the SPI instance has completed the transfer

static const nrf_drv_spi_t m_spi = NRF_DRV_SPI_INSTANCE(SPI_INSTANCE);      // create SPI instance

/* SPI Event handler function, on every tranfer this callback function is triggered */
void spi_event_handler(nrf_drv_spi_evt_t const *p_event, void *p_context){
  spi_xfer_done = true;
  NRF_LOG_INFO(&amp;quot;Transfer completed.&amp;quot;);
  if (spi_rx_buf[0] != 0)
  {
      NRF_LOG_INFO(&amp;quot; Received:&amp;quot;);
      NRF_LOG_HEXDUMP_INFO(spi_rx_buf, strlen((const char *)spi_rx_buf));
  }
}

/* Initialize the spi instance */
void spi_init(void){
  // Create a sturct to hold the configurations and set these values to default */
  nrf_drv_spi_config_t spi_config = NRF_DRV_SPI_DEFAULT_CONFIG;
  
  // Assign pins to SPI instance 
  spi_config.ss_pin   = SPI_SS_PIN;
  spi_config.miso_pin = SPI_MISO_PIN;
  spi_config.mosi_pin = SPI_MOSI_PIN;
  spi_config.sck_pin  = SPI_SCK_PIN;
  spi_config.mode     = NRF_DRV_SPI_MODE_3;
  // Configure the transfer speed by setting the clock for data transmission
  spi_config.frequency =  NRF_DRV_SPI_FREQ_4M;

  /* Call the SPI initialization function within APP_ERROR_CHECK function so that if any error
   occurs during initialization then we can get the response on debug window */
  APP_ERROR_CHECK(nrf_drv_spi_init(&amp;amp;m_spi, &amp;amp;spi_config, spi_event_handler, NULL));

}

int read_reg(int reg){
  spi_tx_buf[0] = SET_READ_SINGLE_CMD(reg); // set the read command for reading a single byte 
  
  spi_xfer_done = false;                    // reset the flag

  APP_ERROR_CHECK(nrf_drv_spi_transfer(&amp;amp;m_spi, spi_tx_buf, 2, spi_rx_buf, 2));    // call the spi transfer function

  while(spi_xfer_done == false){};  // wait till the transfer is done

  return spi_rx_buf[1];                // return the data
  
}

int main(void)
{
    int intRegValue;

    APP_ERROR_CHECK(NRF_LOG_INIT(NULL)); // initialize the logger module and check if any error occurred during initialization
    NRF_LOG_DEFAULT_BACKENDS_INIT();      // initialize the default backends for nrf logger

    spi_init();                           // call the SPI initialization function

    NRF_LOG_INFO(&amp;quot;APP STARTED&amp;quot;);    
    nrf_gpio_cfg(SPI_SS_PIN,  NRF_GPIO_PIN_DIR_OUTPUT, NRF_GPIO_PIN_INPUT_DISCONNECT, NRF_GPIO_PIN_NOPULL, NRF_GPIO_PIN_H0H1, NRF_GPIO_PIN_NOSENSE);
    nrf_gpio_cfg(SPI_MISO_PIN,  NRF_GPIO_PIN_DIR_INPUT, NRF_GPIO_PIN_INPUT_DISCONNECT, NRF_GPIO_PIN_NOPULL, NRF_GPIO_PIN_H0H1, NRF_GPIO_PIN_NOSENSE);
    nrf_gpio_cfg(SPI_MOSI_PIN, NRF_GPIO_PIN_DIR_OUTPUT, NRF_GPIO_PIN_INPUT_DISCONNECT, NRF_GPIO_PIN_NOPULL, NRF_GPIO_PIN_H0H1, NRF_GPIO_PIN_NOSENSE);
    nrf_gpio_cfg(SPI_SCK_PIN,  NRF_GPIO_PIN_DIR_OUTPUT, NRF_GPIO_PIN_INPUT_DISCONNECT, NRF_GPIO_PIN_NOPULL, NRF_GPIO_PIN_H0H1, NRF_GPIO_PIN_NOSENSE);

    intRegValue = read_reg(ADD_REG_WHO_AM_I);   // get the returned value
    NRF_LOG_INFO(&amp;quot;this came up = %d&amp;quot;, intRegValue);
    if(intRegValue == UC_WHO_AM_I_DEFAULT_VALUE){
      NRF_LOG_INFO(&amp;quot;Correct data!&amp;quot;);
    }else{
      NRF_LOG_INFO(&amp;quot;INCORRECT&amp;quot;);
    }


    while (1)
    {
        
    }
}&lt;/pre&gt;&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: SPI connection to ADXL345 with Nrf52840</title><link>https://devzone.nordicsemi.com/thread/388497?ContentTypeID=1</link><pubDate>Thu, 29 Sep 2022 08:54:28 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:9d412dd3-9227-4d85-9f72-7da26d38c154</guid><dc:creator>Naing</dc:creator><description>&lt;p&gt;Same result with MISO pin as high drive. I&amp;#39;m assuming we have to set it as INPUT? I don&amp;#39;t have a logic trace/scope with me, It&amp;#39;ll take me a while to get it sorted out. In the meantime, what&amp;nbsp;are your thoughts on the problem? &lt;pre class="ui-code" data-mode="text"&gt; nrf_gpio_cfg(SPI_MISO_PIN,  NRF_GPIO_PIN_DIR_INPUT, NRF_GPIO_PIN_INPUT_DISCONNECT, NRF_GPIO_PIN_NOPULL, NRF_GPIO_PIN_H0H1, NRF_GPIO_PIN_NOSENSE);&lt;/pre&gt;&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: SPI connection to ADXL345 with Nrf52840</title><link>https://devzone.nordicsemi.com/thread/388491?ContentTypeID=1</link><pubDate>Thu, 29 Sep 2022 08:41:51 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:cd46b28b-54d2-4daa-a64b-b80dfc500f06</guid><dc:creator>J&amp;#248;rgen Holmefjord</dc:creator><description>&lt;p&gt;Please try configuring the MISO pin as high drive as well. If that does not work, please check if lowering the SPI frequency will affect the results.&lt;/p&gt;
&lt;p&gt;Can you provide a logic trace/scope image of the communication on the SPI bus? Do the output and input look as expected?&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: SPI connection to ADXL345 with Nrf52840</title><link>https://devzone.nordicsemi.com/thread/388440?ContentTypeID=1</link><pubDate>Thu, 29 Sep 2022 00:54:12 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:3ea26f48-3107-4f55-aecf-74aafa7a5eb8</guid><dc:creator>Naing</dc:creator><description>&lt;p&gt;I added the following after spi_init() after reading some other posts in the forum but i&amp;#39;m still getting &amp;quot;242&amp;quot;. On the side, I tried to read deviceID on an arduino UNO, i got &amp;quot;229&amp;quot; correctly. So, I&amp;#39;m assuming it&amp;#39;s firmware or the board?&lt;pre class="ui-code" data-mode="text"&gt;nrf_gpio_cfg(SPI_SS,       NRF_GPIO_PIN_DIR_OUTPUT, NRF_GPIO_PIN_INPUT_DISCONNECT, NRF_GPIO_PIN_NOPULL, NRF_GPIO_PIN_H0H1, NRF_GPIO_PIN_NOSENSE);
  nrf_gpio_cfg(SPI_MOSI_PIN, NRF_GPIO_PIN_DIR_OUTPUT, NRF_GPIO_PIN_INPUT_DISCONNECT, NRF_GPIO_PIN_NOPULL, NRF_GPIO_PIN_H0H1, NRF_GPIO_PIN_NOSENSE);
  nrf_gpio_cfg(SPI_SCK_PIN,  NRF_GPIO_PIN_DIR_OUTPUT, NRF_GPIO_PIN_INPUT_DISCONNECT, NRF_GPIO_PIN_NOPULL, NRF_GPIO_PIN_H0H1, NRF_GPIO_PIN_NOSENSE);&lt;/pre&gt;&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: SPI connection to ADXL345 with Nrf52840</title><link>https://devzone.nordicsemi.com/thread/388189?ContentTypeID=1</link><pubDate>Tue, 27 Sep 2022 15:06:49 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:8e880332-641b-4ccb-a26d-a8dee287f109</guid><dc:creator>J&amp;#248;rgen Holmefjord</dc:creator><description>&lt;p&gt;Hi,&lt;/p&gt;
&lt;p&gt;Can y&lt;span&gt;ou try to increase the GPIO drive strength&lt;/span&gt;&lt;span&gt;&amp;nbsp;of the SPI pins by calling&amp;nbsp;&lt;a href="https://github.com/NordicSemiconductor/nrfx/blob/v1.9.0/hal/nrf_gpio.h#L524"&gt;nrf_gpio_cfg&lt;/a&gt;() with drive parameter set to&amp;nbsp;(&lt;a href="https://infocenter.nordicsemi.com/topic/sdk_nrf5_v17.0.2/group__nrf__gpio__hal.html#ggabb86c9557487ac1eda0cec28f258a725aae4d4424759f4afd546132ac8b2b22d1"&gt;NRF_GPIO_PIN_H0H1&lt;/a&gt;) after spi_init()? I have seen this solve similar issues in the past.&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&lt;span&gt;Best regards,&lt;br /&gt;Jørgen&lt;/span&gt;&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item></channel></rss>