NRF52832 learning notes (37) -- use of SPI (slave) interface

1, SPI introduction

SPI (Serial Peripheral Interface) protocol is a communication protocol proposed by Motorola, that is, Serial Peripheral Interface. It is a high-speed full duplex communication bus. It is widely used between ADC, LCD and MCU, which requires high communication rate.

Only four wires are used on the pins of the chip.
MISO: master device data output and slave device data input.
MOSI: master device data input and slave device data output.
SCK: clock signal, which is controlled and sent by the main equipment.
NSS (CS): select the signal from the slave device, which is controlled by the master device. When NSS is low, select the slave device.

2, API description

The following SPI header files must be included_ drv_ spis. h.

#include "nrf_drv_spis.h"

2.1 NRF_DRV_SPIS_INSTANCE

functionInstantiate an SPI slave
Function definitionNRFX_SPIS_INSTANCE(id)
parameterid: SPI slave instance number
returnSPI slave instance with EasyDMA

2.2 nrf_drv_spis_init

functionSPI slave driver initialization
Function definitionret_code_t nrf_drv_spis_init(nrf_drv_spis_t const * const p_instance,
nrf_drv_spis_config_t const * p_config,
nrf_drv_spis_event_handler_t event_handler)
parameterp_instance: SPI slave instance
p_config: configuration parameters
event_handler: callback handler
return0 - success, or other error codes

2.3 nrf_drv_spis_buffers_set

functionConfigure SPI slave send and receive buffer
Function definitionnrfx_err_t nrfx_spis_buffers_set(nrfx_spis_t const * const p_instance,
uint8_t const * p_tx_buffer,
size_t tx_buffer_length,
uint8_t * p_rx_buffer,
size_t rx_buffer_length)
parameterp_instance: SPI slave instance
p_tx_buffer: send data buffer
tx_buffer_length: sending data length
p_rx_buffer: receive data buffer
rx_buffer_length: length of received data
return0 - success, or other error codes

3, SDK configuration

Click sdk_config.h file

Select Configuration Wizard

nRF_ Check SPIS related options in drivers


At NRF_ Add files to drivers

nrfx_spis.cNew version of SPIS compatibility Librarymodules\nrfx\drivers\src
nrf_drv_spis.cOld version SPIS basic libraryintegration\nrfx\legacy

4, Hardware connection

Function portPin
MISO30
MOSI29
CLK26
CSN31

5, Use examples

Open the SDK sample project examples\peripheral\spis

5.1 include header file

#include "nrf_drv_spis.h"

5.2 instantiate SPI slave

#define SPIS_INSTANCE 1 /**< SPIS instance index. */
static const nrf_drv_spis_t spis = NRF_DRV_SPIS_INSTANCE(SPIS_INSTANCE);/**< SPIS instance. */

5.3 define SPI slave event handling function

spis_event_handler receives the event handling function and enters this function when SPI receives data from the machine.
spis_ xfer_ The done receive data flag is set to 1 when the SPI slave receives data, and clear 0 after processing the received data.

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;
        SEGGER_RTT_printf(0," Transfer completed. Received: %s\n",(uint32_t)m_rx_buf);
    }
}

5.4 initialize SPI slave

#define APP_SPIS_CS_PIN 31
#define APP_SPIS_MISO_PIN 30
#define APP_SPIS_MOSI_PIN 29
#define APP_SPIS_SCK_PIN 26

nrf_drv_spis_config_t spis_config = NRF_DRV_SPIS_DEFAULT_CONFIG;
spis_config.csn_pin               = APP_SPIS_CS_PIN;
spis_config.miso_pin              = APP_SPIS_MISO_PIN;
spis_config.mosi_pin              = APP_SPIS_MOSI_PIN;
spis_config.sck_pin               = APP_SPIS_SCK_PIN;

APP_ERROR_CHECK(nrf_drv_spis_init(&spis, &spis_config, spis_event_handler));

5.5 configuring send and receive buffers

#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. */

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

5.6 complete code

Due to the original project NRF_LOG can't print out. Here I use segger instead_ RTT printing. Header file segger must be included_ RTT. h

#include "sdk_config.h"
#include "nrf_drv_spis.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"
#include "SEGGER_RTT.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;
        SEGGER_RTT_printf(0," Transfer completed. Received: %s\n",(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_init(BSP_INIT_LEDS);  // Onboard LED initialization

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

    SEGGER_RTT_printf(0,"SPIS example\n");

    nrf_drv_spis_config_t spis_config = NRF_DRV_SPIS_DEFAULT_CONFIG;
    spis_config.csn_pin               = APP_SPIS_CS_PIN;
    spis_config.miso_pin              = APP_SPIS_MISO_PIN;
    spis_config.mosi_pin              = APP_SPIS_MOSI_PIN;
    spis_config.sck_pin               = APP_SPIS_SCK_PIN;

    APP_ERROR_CHECK(nrf_drv_spis_init(&spis, &spis_config, spis_event_handler));

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

        APP_ERROR_CHECK(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);  // LED light is reversed
    }
}

View print:
Use the SPI host project in SDK to receive.

• by Leung Written on May 18, 2021

• reference: nRF52832 register operation SPI Slave

Keywords: SPI BLE NRF52832

Added by colby.anderson on Wed, 09 Feb 2022 15:15:57 +0200