SPI connection between nrf52832 (dwm1001) and bcm2835 (Raspberry 3 Pi)

Hi again, I tried to set the SPI communication where the Raspberry is the SPI Master and the dwm1001 is the SPI Slave.

This is the firmware code:

[code]#include “sdk_config.h”
#include “nrf_drv_spis.h”
#include “nrf_gpio.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 SPIS_INSTANCE 1 /< SPIS instance index. */
static const nrf_drv_spis_t spis = NRF_DRV_SPIS_INSTANCE(SPIS_INSTANCE);/
< SPIS instance. */

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

static volatile bool spis_xfer_done; /**< Flag used to indicate that SPIS instance completed the transfer. */

/**

  • @brief SPIS user event handler.
  • @param event
    */
    void spis_event_handler(nrf_drv_spis_event_t event)
    {
    if (event.evt_type == NRF_DRV_SPIS_XFER_DONE)
    {
    spis_xfer_done = true;
    NRF_LOG_INFO(" Transfer completed. Received: %s",(uint32_t)m_rx_buf);
    }
    }

int main(void)
{
// Enable the constant latency sub power mode to minimize the time it takes
// for the SPIS peripheral to become active after the CSN line is asserted
// (when the CPU is in sleep mode).
NRF_POWER->TASKS_CONSTLAT = 1;

bsp_board_leds_init();

NRF_LOG_INIT(NULL);
NRF_LOG_DEFAULT_BACKENDS_INIT();

NRF_LOG_INFO("SPIS example");

nrf_drv_spis_config_t spis_config = NRF_DRV_SPIS_DEFAULT_CONFIG;
spis_config.csn_pin               = 29;
spis_config.miso_pin              = 26;
spis_config.mosi_pin              = 27;
spis_config.sck_pin               = 28;
spis_config.mode                  = NRF_DRV_SPIS_MODE_1;

nrf_drv_spis_init(&spis, &spis_config, spis_event_handler);

while (1)
{
    memset(m_rx_buf, 0, m_length);
    spis_xfer_done = false;

    nrf_drv_spis_buffers_set(&spis, m_tx_buf, m_length, m_rx_buf, m_length);

    while (!spis_xfer_done)
    {
        __WFE();
    }

    NRF_LOG_FLUSH();

    bsp_board_led_invert(BSP_BOARD_LED_0);
}

}
[/code]

And from Raspberry’s side I have:

[code]// spin.c
//
// Example program for bcm2835 library
// Shows how to interface with SPI to transfer a number of bytes to and from an SPI device
//
// After installing bcm2835, you can build this
// with something like:
// gcc -o spin spin.c -l bcm2835
// sudo ./spin
//
// Or you can test it before installing with:
// gcc -o spin -I …/…/src …/…/src/bcm2835.c spin.c
// sudo ./spin

#include <bcm2835.h>
#include <stdio.h>

#define PIN RPI_GPIO_P1_12

int main(int argc, char **argv)
{
if (!bcm2835_init())
{
printf(“bcm2835_init failed. Are you running as root??\n”);
return 1;
}
if (!bcm2835_spi_begin())
{
printf(“bcm2835_spi_begin failed. Are you running as root??\n”);
return 1;
}
bcm2835_spi_begin();

bcm2835_gpio_fsel(PIN, BCM2835_GPIO_FSEL_OUTP);
bcm2835_gpio_write(PIN, LOW);
//bcm2835_gpio_set_pud(PIN, BCM2835_GPIO_PUD_DOWN);

bcm2835_spi_setBitOrder(BCM2835_SPI_BIT_ORDER_MSBFIRST);      // The default
bcm2835_spi_setDataMode(BCM2835_SPI_MODE1);                   // The default
bcm2835_spi_setClockDivider(BCM2835_SPI_CLOCK_DIVIDER_65536); // The default
bcm2835_spi_chipSelect(BCM2835_SPI_CS0);                      // The default
bcm2835_spi_setChipSelectPolarity(BCM2835_SPI_CS0, LOW);      // the default

// Send a byte to the slave and simultaneously read a byte back from the slave
// If you tie MISO to MOSI, you should read back what was sent
uint8_t send_data = 0x23;
uint8_t read_data = bcm2835_spi_transfer(send_data);
printf("Sent to SPI: 0x%02X. Read back from SPI: 0x%02X.\n", send_data, read_data);

bcm2835_gpio_write(PIN, HIGH);
bcm2835_spi_end();
bcm2835_close();
return 0;

}[/code]

I installed the firmware into the dwm1001 module and executed in debug mode.
Then executer the “spi” code from Raspberry.
But I cannot see any message from dwm module.

Do I made mistakes?

Hi Bettis,

As a first step, have you tried to setup a spi connection between the raspberry pi and the DWM1001 running its factory firmware ?

I’ll try to take a look at your application when I have a chance.

Thank you,
Kind regards
Yves

Yes I tried successfully several days ago.

I used the examples hosted to GitHub.
Obviously I used the factory firmware for the antennas and I ran the code from raspberry. SPI connection was fine, but not very useful for me since the API doesn’t allow to send custom commands or particular strings.

According the datasheet, dwm1001 has a SPI Slave (SPI S2), while SPI M1 directly communicates with UWB transceiver.
[attachment=130]
So, the question is: which pins I have to connect from my DWM1001 board in order to connect SPI S2 to the RPi’s one?

Hi bettis,

Please have a look at the section 3 on the document :

If you use the raspberry pi header on the dem1001-dev, the right SPI connection should be implemented.

Let me know how it goes,
Thanks
Regards
Yves

Ok Yves,
I tried again to connect RPi and dwm1001-dev using the 26 pins header.

So in my spis example, from nordik’s folder, I have:

#define SPIS_INSTANCE 2 /**< SPIS instance index. */

And…

[code]nrf_drv_spis_config_t spis_config = NRF_DRV_SPIS_DEFAULT_CONFIG;
spis_config.csn_pin = 3; // GPIO8/SPI_CE0 on RPi
spis_config.miso_pin = 7; // GPIO9/SPI_MISO on RPi
spis_config.mosi_pin = 6; // GPIO10/SPI_MOSI on RPi
spis_config.sck_pin = 4; // GPIO11/SPI_SCLK on RPi
spis_config.mode = NRF_DRV_SPIS_MODE_1;

nrf_drv_spis_init(&spis, &spis_config, spis_event_handler);

while (1)
{
    memset(m_rx_buf, 0, m_length);
    spis_xfer_done = false;

    nrf_drv_spis_buffers_set(&spis, m_tx_buf, m_length, m_rx_buf, m_length);

    while (!spis_xfer_done)
    {
        __WFE();
    }

    NRF_LOG_FLUSH();

    bsp_board_led_invert(BSP_BOARD_LED_0);
}[/code]

…because if you read DWM1001_Datasheet there is a mapping between the dwm numbering and nordik [N] numbering.
So for example, SPIS_MISO in dwm is 26 but the pin is rerouted [N] P0.07. For this reason I set:

[size=small][font=Monaco, Consolas, Courier, monospace]spis_config.miso_pin = 7;[/font][/size]

I finally managed to reach my goal :slight_smile:

Ok great to know. What did you have to change to make it work ?

Thanks
Yves

I used the pins configurations for SPIS and SPI2 as slave (configured inside the sdk_config.c file).
And obviously the header connection (26 pins).

Thanks.