Beware that this post is related to an SDK in maintenance mode
More Info: Consider nRF Connect SDK for new designs
This post is older than 2 years and might not be relevant anymore
More Info: Consider searching for newer posts

Unable to interface nrf51822 with LIS3DH (accelerometer) using SPI being nrf as master

Hi, 

I tried interfacing LIS3Dh using SPI, I used the SPI example to start with, when I didn't get the desired result, I look numerous question on the forum and tried to modify the code but it does not seem to be working.

I am simply trying to read WHO_AM_I register/or any other register, I am getting FF always, 

the sensor is embedded on PCB with following configurations: 

Configurations:

LIS3DH nrf 51822 pin  sdk_config.h 
SCK 9 SPI_SCK_PIN 9
SDI 10 SPI_MOSI_PIN 10
CS 12 SPI_SS_PIN 12
SDO 7 SPI_MISO_PIN 11

Below code is trying to read who_am_i ( 0x0F) 

Fullscreen
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#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"
static uint8_t m_tx_buf[]={0x0F}; /**< TX buffer. */
static uint8_t m_rx_buf[]={0x00, 0x00,0x00, 0x00,0x00}; /**< RX buffer. */
//static const uint8_t m_length = sizeof(m_rx_buf); /**< Transfer length. */
/**
* @brief SPI user event handler.
* @param event
*/
void spi_event_handler(nrf_drv_spi_evt_t const * p_event)
{
spi_xfer_done = true;
SEGGER_RTT_printf(0,"Transfer completed.\r\n");
if (1 != 0)
{
SEGGER_RTT_printf(0," Received: \r\n");
NRF_LOG_HEXDUMP_INFO(m_rx_buf, strlen((const char *)m_rx_buf));
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX

log output ( in every case I am getting FF value in rx_buff ) 

SDK Used: 12.2

using JLink to transfer the via SWO

I have tried with different modes and frequencies also but no luck. 

Please help.

  • Hi ,

      I did what you say, but I only get 0xFF :/

      SPI 0 is enabled on config.

      I checked some repos on the internet and the example on sdk 14.2, but no progress. When I change SDA and SDO, I get no response at all or 0xFF.

      What could it be?

    Thanks :D

  • There is no SDA or SDO signal on an SPI interface; SDA or SDO are only applicable to I2C which is a very different protocol being both more complex to implement and much slower in operation. There are examples for I2C and I2C fully supported by the Nordic libraries so perhaps search for those. Commonly I2C is used where multiple devices exist on a single 2-wire serial bus.

  • Ok, MOSI and MISO, sorry for the inaccurate terminology.

    I managed to make the I2C (or TWI) work, but I need to do it with the SPI and the SDK functions are not working for me.

    I even did a bit-bang for the SPI and it worked, but I need help with the SDK SPI.

    I checked on the oscilloscope and CS in going low during communication window, MOSI is fine, CLK is fine, but get 0xFF on MISO when asking for WHO_AM_I. I initialized the SPI, tried modes 1 to 3, changed the data rate, enlarged the buffers (rx and tx with the same size), but I am still missing something :/

  • You tried modes 1,2,3 but did you try mode 0? I use mode 0 with the similar part. Maybe post your code again is case we can spot something ..

  • I just noticed something: Product Spec v3.3 SPI Max frequency is listed at 4MHz; I see you are using 8MHz

    Fullscreen
    1
    fSCK SCK frequency. 0.125 4 MHz
    XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX

1 2 3 4