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

Confusing how to integrate SPI master and slave drivers/HAL in my C code

Ok so I am trying to integrate SPI master and slave initialization code. I am very confused about legacy and non-legacy drivers and HAL. I am getting strange compilation and linking errors. Please help me out on this.

I am compiling following SPI related driver files in my IAR project:

  • modules\nrfx\drivers\src\nrfx_spi.c
  • modules\nrfx\drivers\src\nrfx_spis.c
  • modules\nrfx\drivers\src\nrfx_spim.c
  • integration\nrfx\legacy\nrf_drv_spi.c
  • integration\nrfx\legacy\nrf_drv_spis.c

sdk_config.h settings for SPI:

Fullscreen
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
// <e> NRFX_SPIM_ENABLED - nrfx_spim - SPIM peripheral driver
//==========================================================
#ifndef NRFX_SPIM_ENABLED
#define NRFX_SPIM_ENABLED 0
#endif
// <q> NRFX_SPIM0_ENABLED - Enable SPIM0 instance
#ifndef NRFX_SPIM0_ENABLED
#define NRFX_SPIM0_ENABLED 0
#endif
// <q> NRFX_SPIM1_ENABLED - Enable SPIM1 instance
#ifndef NRFX_SPIM1_ENABLED
#define NRFX_SPIM1_ENABLED 0
#endif
// <q> NRFX_SPIM2_ENABLED - Enable SPIM2 instance
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX

init.c

Fullscreen
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#include "common.h"
#include "nrf_drv_spi.h"
#include "nrf_drv_spis.h"
volatile bool bSpiMasterTxRxDone = false;
volatile bool bSpiSlaveTxRxDone = false;
nrf_drv_spi_t const hSpiMasterDev0 = NRF_DRV_SPI_INSTANCE(SPI_DEV_ID_0);
nrf_drv_spis_t const hSpiSlaveDev0 = NRF_DRV_SPIS_INSTANCE(SPI_DEV_ID_0);
/**
* @brief SPI master event handler.
* @param event
*/
static void spi_master_event_handler(nrf_drv_spi_evt_t const * p_event,
void * p_context)
{
bSpiMasterTxRxDone = true;
NRF_LOG_INFO("Transfer completed.");
}
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX

common.h

Fullscreen
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#ifndef COMMON_H
#define COMMON_H
/* SPI Modes */
typedef enum
{
SPI_MODE_MASTER,
SPI_MODE_SLAVE
}SPI_MODE_TYPE;
/* GPIO pin for SPI MOSI(12/P0.13) */
#define SPI_MOSI_PIN ((nrfx_gpiote_pin_t)13)
/* GPIO pin for SPI MISO(14/P0.14) */
#define SPI_MISO_PIN ((nrfx_gpiote_pin_t)14)
/* GPIO pin for SPI SCK(13/P0.12)*/
#define SPI_SCK_PIN ((nrfx_gpiote_pin_t)12)
/* GPIO pin for SPI CS(11/P0.15)*/
#define SPI_CS_PIN ((nrfx_gpiote_pin_t)15)
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX

main.c

Fullscreen
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#include "common.h"
#include "nrf_drv_spi.h"
#include "nrf_drv_spis.h"
extern nrf_drv_spi_t const hSpiMasterDev0;
extern nrf_drv_spis_t const hSpiSlaveDev0;
int main()
{
// various init codes
// ...
//..
// spi init
spi_init(&hSpiSlaveDev0, BMNL_SPI_MODE_SLAVE );
}
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX

What I need:

  • One SPI controller on nrf52832 that I want o use in either master or mode (as I have just four hardwired GPIO pins connected to my another device on my custom board. ).  I will switch the mode of the same SPI controller device from slave to master and vice versa as needed(depends on which state my nrf52 is in).
  • Specify frequency(mbps,hz) for both master/slave configuration. I did find this setting for master, but for some reason I cannot locate this in initialization C structures for slave configuration.
  • Blocking/non-blocking modes in DMA/non-DMA configuration
  • I prefer not to use any legacy SPI driver/hal unless I will compromise on above requirements.

So here are my questions:

  1. Which SPI driver/HAL header (.h) files should I use in my C code and which C driver/HAL files should I compile & link with my project ? 
  2. What  is the different between SPI, SPIM and SPIS ? Which one should I use for master and which one for slave ? 
  3. Which of the following flags should I enable to use 0th SPI controller instance for master/slave ?
  4. How do I specify frequency configuration for SPI slave configuration ?