This post is older than 2 years and might not be relevant anymore
More Info: Consider searching for newer posts

nRF52840 Receiving bad Device ID from ADXL362

I have an ADXL362 eval board connected to a nRF52840-DK. When I try to read the device ID from the ADXL362 instead of returning 0xAD it most often returns 0x21 and at other times the ADXL362 returns other hex values that are also not correct.

I have attached the ADXL362 eval board to a Particle Photon board and when I run the same series of commands I do receive the correct 0xAD command I expect for the device id.

I have attached my code that I run on the nRF52840-DK and the Logic analyzer results from both the successful run on the Photon and the 0x21 response on the nRF52840-DK.

What settings do I need to change to get the 0xAD response on the nRF52840-DK?

Main.c

#include "nrf_drv_spi.h"
#include "app_util_platform.h"
#include "nrf_gpio.h"
#include "nrf_delay.h"
#include "boards.h"
#include "app_error.h"
#include <string.h>
#include "nrf_log.h"
#include "nrf_log_ctrl.h"
#include "nrf_log_default_backends.h"

#define ADXL362_CS_PIN   7    //P0.07
#define ADXL362_MOSI_PIN 12   //P0.12
#define ADXL362_MISO_PIN 8    //P0.08
#define ADXL362_SCK_PIN  11   //P0.11

#define READ 0x0B
#define WRITE 0x0A
#define ID  0x00

#define SPI_INSTANCE  0 /**< SPI instance index. */
static const nrf_drv_spi_t spi = NRF_DRV_SPI_INSTANCE(SPI_INSTANCE);  /**< SPI instance. */
static volatile bool spi_xfer_done;  /**< Flag used to indicate that SPI instance completed the transfer. */

#define TEST_STRING "Nordic"
uint8_t       m_tx_buf[3];           /**< TX buffer. */
uint8_t       m_rx_buf[3];    /**< RX buffer. */
static const uint8_t m_length = sizeof(m_tx_buf);        /**< Transfer length. */

/**
 * @brief SPI user event handler.
 * @param event
 */
void spi_event_handler(nrf_drv_spi_evt_t const * p_event, void * p_context)
{
    spi_xfer_done = true;
    printf("Transfer completed.\n");
    uint8_t out0 = m_rx_buf[0];
    uint8_t out1 = m_rx_buf[1];
    uint8_t out2 = m_rx_buf[2];
    printf("Out=0x%x, 0x%x, 0x%x \n", out0, out1, out2);
    
    if (m_rx_buf[2] != 0)
    {
        printf("Received:");
        printf(m_rx_buf);
        NRF_LOG_HEXDUMP_INFO(m_rx_buf, strlen((const char *)m_rx_buf));
    }
}

void device_id(nrf_drv_spi_config_t *spi_config){
        printf("Device_id");
        memset(m_rx_buf, 0, m_length);
        spi_xfer_done = false;

        m_tx_buf[0] = 0x0B;
        m_tx_buf[1] = 0x00;
        m_tx_buf[2] = 0x00;

        nrf_gpio_pin_clear(spi_config->ss_pin);
        APP_ERROR_CHECK(nrf_drv_spi_transfer(&spi, m_tx_buf, m_length, m_rx_buf, m_length));

        while (!spi_xfer_done)
        {
            __WFE();
        }
        nrf_gpio_pin_set(spi_config->ss_pin);
        
        NRF_LOG_FLUSH();

        bsp_board_led_invert(BSP_BOARD_LED_0);
        nrf_delay_ms(1000);
}

void soft_reset(nrf_drv_spi_config_t *spi_config){
     printf("soft_reset\n");
     memset(m_rx_buf, 0, m_length);
     spi_xfer_done = false;

     m_tx_buf[0] = 0x0A;
     m_tx_buf[1] = 0x1F;
     m_tx_buf[2] = 0x52;

     nrf_gpio_pin_clear(spi_config->ss_pin);
     APP_ERROR_CHECK(nrf_drv_spi_transfer(&spi, m_tx_buf, m_length, m_rx_buf, m_length));

     while (!spi_xfer_done)
     {
        __WFE();
     }
     nrf_gpio_pin_set(spi_config->ss_pin);
        
     NRF_LOG_FLUSH();

     bsp_board_led_invert(BSP_BOARD_LED_0);
     nrf_delay_ms(10000);
}

void start_measurement(nrf_drv_spi_config_t *spi_config){
     printf("start_measurement\n");
     memset(m_rx_buf, 0, m_length);
     spi_xfer_done = false;

     m_tx_buf[0] = 0x0A;
     m_tx_buf[1] = 0x2D; //Power Control
     m_tx_buf[2] = 0x02;

     nrf_gpio_pin_clear(spi_config->ss_pin);
     APP_ERROR_CHECK(nrf_drv_spi_transfer(&spi, m_tx_buf, m_length, m_rx_buf, m_length));

     while (!spi_xfer_done)
     {
        __WFE();
     }
     nrf_gpio_pin_set(spi_config->ss_pin);
        
     NRF_LOG_FLUSH();

     bsp_board_led_invert(BSP_BOARD_LED_0);
}

int main(void)
{
    bsp_board_init(BSP_INIT_LEDS);

    APP_ERROR_CHECK(NRF_LOG_INIT(NULL));
    NRF_LOG_DEFAULT_BACKENDS_INIT();

    nrf_drv_spi_config_t spi_config = NRF_DRV_SPI_DEFAULT_CONFIG;
    spi_config.frequency = NRF_DRV_SPI_FREQ_125K;
//    spi_config.frequency = NRF_DRV_SPI_FREQ_250K;
//    spi_config.frequency = NRF_DRV_SPI_FREQ_1M;
    spi_config.mode = NRF_DRV_SPI_MODE_0;
    spi_config.bit_order = NRF_DRV_SPI_BIT_ORDER_MSB_FIRST;
//    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.ss_pin   = ADXL362_CS_PIN;
    spi_config.miso_pin = ADXL362_MISO_PIN;
    
    spi_config.mosi_pin = ADXL362_MOSI_PIN;
    spi_config.sck_pin  = ADXL362_SCK_PIN;

    APP_ERROR_CHECK(nrf_drv_spi_init(&spi, &spi_config, spi_event_handler, NULL));
//    NRF_GPIO_Type * reg = nrf_gpio_pin_port_decode(ADXL362_SCK_PIN);
//    reg->PIN_CNF[ADXL362_SCK_PIN] |= (NRF_GPIO_PIN_H0H1 << GPIO_PIN_CNF_DRIVE_Pos);
//
//    NRF_GPIO_Type * reg2 = nrf_gpio_pin_port_decode(ADXL362_MISO_PIN);
//    reg2->PIN_CNF[ADXL362_MISO_PIN] |= (NRF_GPIO_PIN_H0H1 << GPIO_PIN_CNF_DRIVE_Pos);

    NRF_LOG_INFO("SPI example started.");

//    soft_reset(&spi_config);

    while (1)
    {
      device_id(&spi_config);
    }
}

Good device ID from photon

Good Device ID with Photon

Bad Device ID from nRF52840-DK

  • Hi,

    Can you check what the config NRF_SPI_DRV_MISO_PULLUP_CFG is set to in your sdk_config.h file? If not set to this already, please try to set it to 0 (NRF_GPIO_PIN_NOPULL).

    Best regards,
    Jørgen

  • It was previously set to 1 and I have changed it to 0
    // <o> NRF_SPI_DRV_MISO_PULLUP_CFG  - MISO PIN pull-up configuration.
     
    // <0=> NRF_GPIO_PIN_NOPULL 
    // <1=> NRF_GPIO_PIN_PULLDOWN 
    // <3=> NRF_GPIO_PIN_PULLUP 

    #ifndef NRF_SPI_DRV_MISO_PULLUP_CFG
    #define NRF_SPI_DRV_MISO_PULLUP_CFG 0 // previously = 1
    #endif
    Now I get 0x90 out from the ADXL362
  • In the other case it was recommended setting the clock and miso lines to H0H1. I added these lines to the main file.

    nrf_gpio_cfg(spi_config.sck_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_config.miso_pin, NRF_GPIO_PIN_DIR_INPUT, NRF_GPIO_PIN_INPUT_CONNECT, NRF_GPIO_PIN_NOPULL, NRF_GPIO_PIN_H0H1, NRF_GPIO_PIN_NOSENSE);

    int main(void)
    {
        bsp_board_init(BSP_INIT_LEDS);
    
        APP_ERROR_CHECK(NRF_LOG_INIT(NULL));
        NRF_LOG_DEFAULT_BACKENDS_INIT();
    
        nrf_drv_spi_config_t spi_config = NRF_DRV_SPI_DEFAULT_CONFIG;
        spi_config.frequency = NRF_DRV_SPI_FREQ_125K;
    //    spi_config.frequency = NRF_DRV_SPI_FREQ_250K;
    //    spi_config.frequency = NRF_DRV_SPI_FREQ_1M;
        spi_config.mode = NRF_DRV_SPI_MODE_0;
        spi_config.bit_order = NRF_DRV_SPI_BIT_ORDER_MSB_FIRST;
    //    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.ss_pin   = ADXL362_CS_PIN;
        spi_config.orc      = 0xCC;
        spi_config.miso_pin = ADXL362_MISO_PIN;
        
        spi_config.mosi_pin = ADXL362_MOSI_PIN;
        spi_config.sck_pin  = ADXL362_SCK_PIN;
    
        nrf_gpio_cfg(spi_config.sck_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_config.miso_pin, NRF_GPIO_PIN_DIR_INPUT, NRF_GPIO_PIN_INPUT_CONNECT, NRF_GPIO_PIN_NOPULL, NRF_GPIO_PIN_H0H1, NRF_GPIO_PIN_NOSENSE);
    
        APP_ERROR_CHECK(nrf_drv_spi_init(&spi, &spi_config, spi_event_handler, NULL));
    
    //    NRF_GPIO_Type * reg2 = nrf_gpio_pin_port_decode(ADXL362_MISO_PIN);
    //    reg2->PIN_CNF[ADXL362_MISO_PIN] |= (NRF_GPIO_PIN_H0H1 << GPIO_PIN_CNF_DRIVE_Pos);
    
        NRF_LOG_INFO("SPI example started.");
    
    //    soft_reset(&spi_config);
    
        while (1)
        {
          device_id(&spi_config);
        }
    }

    Now I am getting 0xBF out and my MISO line appears to be picking up minor noise from the SCK line.

    Any other thoughts on how to get this working?

  • It ended up that I need to set the H0H1 for the MOSI and SCK pins after the nrf_spi_init() function call. I am getting 0xAD out for the device id now.

     APP_ERROR_CHECK(nrf_drv_spi_init(&spi, &spi_config, spi_event_handler, NULL));
    
    nrf_gpio_cfg(spi_config.sck_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_config.mosi_pin, NRF_GPIO_PIN_DIR_OUTPUT, NRF_GPIO_PIN_INPUT_DISCONNECT, NRF_GPIO_PIN_NOPULL, NRF_GPIO_PIN_H0H1, NRF_GPIO_PIN_NOSENSE);
    

Related