SPIM — Serial peripheral interface master with EasyDMA

The SPI master can communicate with multiple slaves using individual chip select signals for each of the slave devices attached to a bus.

Listed here are the main features for the SPIM

  • Three SPIM instances
  • SPI mode 0-3
  • EasyDMA direct transfer to/from RAM for both SPI Slave and SPI Master
  • Individual selection of IO pin for each SPI signal
Figure 1. SPIM — SPI master with EasyDMA

The SPIM does not implement support for chip select directly. Therefore, the CPU must use available GPIOs to select the correct slave and control this independently of the SPI master. The SPIM supports SPI modes 0 through 3. The CONFIG register allows setting CPOL and CPHA appropriately.

Table 1. SPI modes
ModeClock polarityClock phase
 CPOLCPHA
SPI_MODE00 (Active High)0 (Leading)
SPI_MODE10 (Active High)1 (Trailing)
SPI_MODE21 (Active Low)0 (Leading)
SPI_MODE31 (Active Low)1 (Trailing)

Shared resources

The SPI shares registers and other resources with other peripherals that have the same ID as the SPI. Therefore, the user must disable all peripherals that have the same ID as the SPI before the SPI can be configured and used.

Disabling a peripheral that has the same ID as the SPI will not reset any of the registers that are shared with the SPI. It is therefore important to configure all relevant SPI registers explicitly to secure that it operates correctly.

See the Instantiation table in Instantiation for details on peripherals and their IDs.

EasyDMA

The SPI master implements EasyDMA for reading and writing of data packets from and to the DATA RAM without CPU involvement.

The RXD.PTR and TXD.PTR point to the RXD buffer (receive buffer) and TXD buffer (transmit buffer) respectively, see Figure 1. RXD.MAXCNT and TXD.MAXCNT specify the maximum number of bytes allocated to the buffers.

The SPI master will automatically stop transmitting after TXD.MAXCNT bytes have been transmitted and RXD.MAXCNT bytes have been received. If TXD.MAXCNT is larger than RXD.MAXCNT, the superfluous received bytes will be ignored. If RXD.MAXCNT is larger than TXD.MAXCNT, the remaining transmitted bytes will contain the value defined in the ORC register.

If the RXD.PTR and the TXD.PTR are not pointing to the Data RAM region, an EasyDMA transfer may result in a HardFault or RAM corruption. See Memory for more information about the different memory regions.

The .PTR and .MAXCNT registers are double-buffered. They can be updated and prepared for the next transmission immediately after having received the STARTED event.

The ENDRX/ENDTX event indicate that EasyDMA has finished accessing respectively the RX/TX buffer in RAM. The END event gets generated when both RX and TX are finished accessing the buffers in RAM.

EasyDMA list

EasyDMA supports one list type.

The supported list type is:

  • Array list

EasyDMA array list

The EasyDMA array list can be represented by the data structure ArrayList_type.

For illustration, see the code example below. This data structure includes only a buffer with size equal to Channel.MAXCNT. EasyDMA will use the Channel.MAXCNT register to determine when the buffer is full. Replace 'Channel' by the specific data channel you want to use, for instance 'NRF_SPIM->RXD', 'NRF_SPIM->TXD', 'NRF_TWIM->RXD', etc.

The Channel.MAXCNT register cannot be specified larger than the actual size of the buffer. If Channel.MAXCNT is specified larger than the size of the buffer, the EasyDMA channel may overflow the buffer.

This array list does not provide a mechanism to explicitly specify where the next item in the list is located. Instead, it assumes that the list is organized as a linear array where items are located one after the other in RAM.

      
  #define BUFFER_SIZE  4
  
  typedef struct ArrayList
  {
    uint8_t buffer[BUFFER_SIZE];
  } ArrayList_type;

  ArrayList_type MyArrayList[3];

  //replace 'Channel' below by the specific data channel you want to use,
  //         for instance 'NRF_SPIM->RXD', 'NRF_TWIM->RXD', etc.
  Channel.MAXCNT = BUFFER_SIZE;
  Channel.PTR = &MyArrayList;
      

Figure 2. EasyDMA array list

SPI master transaction sequence

An SPI master transaction consists of a sequence started by the START task followed by a number of events, and finally the STOP task.

An SPI master transaction is started by triggering the START task. The ENDTX event will be generated when the transmitter has transmitted all bytes in the TXD buffer as specified in the TXD.MAXCNT register. The ENDRX event will be generated when the receiver has filled the RXD buffer, i.e. received the last possible byte as specified in the RXD.MAXCNT register.

Following a START task, the SPI master will generate an END event when both ENDRX and ENDTX have been generated.

The SPI master is stopped by triggering the STOP task. A STOPPED event is generated when the SPI master has stopped.

If the ENDRX event has not already been generated when the SPI master has come to a stop, the SPI master will generate the ENDRX event explicitly even though the RX buffer is not full.

If the ENDTX event has not already been generated when the SPI master has come to a stop, the SPI master will generate the ENDTX event explicitly even though all bytes in the TXD buffer, as specified in the TXD.MAXCNT register, have not been transmitted.

The SPI master is a synchronous interface, and for every byte that is sent, a different byte will be received at the same time; this is illustrated in Figure 3.

Figure 3. SPI master transaction

Low power

When putting the system in low power and the peripheral is not needed, lowest possible power consumption is achieved by stopping, and then disabling the peripheral.

The STOP task may not be always needed (the peripheral might already be stopped), but if it is sent, software shall wait until the STOPPED event was received as a response before disabling the peripheral through the ENABLE register.

Master mode pin configuration

The SCK, MOSI, and MISO signals associated with the SPI master are mapped to physical pins according to the configuration specified in the PSEL.SCK, PSEL.MOSI, and PSEL.MISO registers respectively.

The PSEL.SCK, PSEL.MOSI, and PSEL.MISO registers and their configurations are only used as long as the SPI master is enabled, and retained only as long as the device is in ON mode. PSEL.SCK, PSEL.MOSI and PSEL.MISO must only be configured when the SPI master is disabled.

To secure correct behavior in the SPI, the pins used by the SPI must be configured in the GPIO peripheral as described in Table 2 prior to enabling the SPI. This configuration must be retained in the GPIO for the selected IOs as long as the SPI is enabled.

Only one peripheral can be assigned to drive a particular GPIO pin at a time. Failing to do so may result in unpredictable behavior.

Table 2. GPIO configuration
SPI master signalSPI master pinDirectionOutput valueComments
SCKAs specified in PSEL.SCKOutputSame as CONFIG.CPOL 
MOSIAs specified in PSEL.MOSIOutput0 
MISOAs specified in PSEL.MISOInputNot applicable 

Registers

Table 3. Instances
Base addressPeripheralInstanceDescriptionConfiguration
0x40003000SPIMSPIM0SPI master 0  
0x40004000SPIMSPIM1SPI master 1  
0x40023000SPIMSPIM2SPI master 2  
Table 4. Register Overview
RegisterOffsetDescription
TASKS_START0x010Start SPI transaction 
TASKS_STOP0x014Stop SPI transaction 
TASKS_SUSPEND0x01CSuspend SPI transaction 
TASKS_RESUME0x020Resume SPI transaction 
EVENTS_STOPPED0x104SPI transaction has stopped 
EVENTS_ENDRX0x110End of RXD buffer reached 
EVENTS_END0x118End of RXD buffer and TXD buffer reached 
EVENTS_ENDTX0x120End of TXD buffer reached 
EVENTS_STARTED0x14CTransaction started 
SHORTS0x200Shortcut register 
INTENSET0x304Enable interrupt 
INTENCLR0x308Disable interrupt 
ENABLE0x500Enable SPIM 
PSEL.SCK0x508Pin select for SCK 
PSEL.MOSI0x50CPin select for MOSI signal 
PSEL.MISO0x510Pin select for MISO signal 
FREQUENCY0x524SPI frequency. Accuracy depends on the HFCLK source selected. 
RXD.PTR0x534Data pointer 
RXD.MAXCNT0x538Maximum number of bytes in receive buffer 
RXD.AMOUNT0x53CNumber of bytes transferred in the last transaction 
RXD.LIST0x540EasyDMA list type 
TXD.PTR0x544Data pointer 
TXD.MAXCNT0x548Maximum number of bytes in transmit buffer 
TXD.AMOUNT0x54CNumber of bytes transferred in the last transaction 
TXD.LIST0x550EasyDMA list type 
CONFIG0x554Configuration register 
ORC0x5C0Over-read character. Character clocked out in case and over-read of the TXD buffer. 

SHORTS

Address offset: 0x200

Shortcut register

Bit number313029282726252423222120191817161514131211109876543210
Id              A                 
Reset 0x0000000000000000000000000000000000000000
IdRWFieldValue IdValueDescription
ARWEND_START  

Shortcut between END event and START task

See EVENTS_END and TASKS_START

   Disabled0Disable shortcut
   Enabled1Enable shortcut
 

INTENSET

Address offset: 0x304

Enable interrupt

Bit number313029282726252423222120191817161514131211109876543210
Id            E          D C B  A 
Reset 0x0000000000000000000000000000000000000000
IdRWFieldValue IdValueDescription
ARWSTOPPED  

Write '1' to Enable interrupt for STOPPED event

See EVENTS_STOPPED

   Set1Enable
   Disabled0Read: Disabled
   Enabled1Read: Enabled
BRWENDRX  

Write '1' to Enable interrupt for ENDRX event

See EVENTS_ENDRX

   Set1Enable
   Disabled0Read: Disabled
   Enabled1Read: Enabled
CRWEND  

Write '1' to Enable interrupt for END event

See EVENTS_END

   Set1Enable
   Disabled0Read: Disabled
   Enabled1Read: Enabled
DRWENDTX  

Write '1' to Enable interrupt for ENDTX event

See EVENTS_ENDTX

   Set1Enable
   Disabled0Read: Disabled
   Enabled1Read: Enabled
ERWSTARTED  

Write '1' to Enable interrupt for STARTED event

See EVENTS_STARTED

   Set1Enable
   Disabled0Read: Disabled
   Enabled1Read: Enabled
 

INTENCLR

Address offset: 0x308

Disable interrupt

Bit number313029282726252423222120191817161514131211109876543210
Id            E          D C B  A 
Reset 0x0000000000000000000000000000000000000000
IdRWFieldValue IdValueDescription
ARWSTOPPED  

Write '1' to Disable interrupt for STOPPED event

See EVENTS_STOPPED

   Clear1Disable
   Disabled0Read: Disabled
   Enabled1Read: Enabled
BRWENDRX  

Write '1' to Disable interrupt for ENDRX event

See EVENTS_ENDRX

   Clear1Disable
   Disabled0Read: Disabled
   Enabled1Read: Enabled
CRWEND  

Write '1' to Disable interrupt for END event

See EVENTS_END

   Clear1Disable
   Disabled0Read: Disabled
   Enabled1Read: Enabled
DRWENDTX  

Write '1' to Disable interrupt for ENDTX event

See EVENTS_ENDTX

   Clear1Disable
   Disabled0Read: Disabled
   Enabled1Read: Enabled
ERWSTARTED  

Write '1' to Disable interrupt for STARTED event

See EVENTS_STARTED

   Clear1Disable
   Disabled0Read: Disabled
   Enabled1Read: Enabled
 

ENABLE

Address offset: 0x500

Enable SPIM

Bit number313029282726252423222120191817161514131211109876543210
Id                            AAAA
Reset 0x0000000000000000000000000000000000000000
IdRWFieldValue IdValueDescription
ARWENABLE  Enable or disable SPIM
   Disabled0Disable SPIM
   Enabled7Enable SPIM
 

PSEL.SCK

Address offset: 0x508

Pin select for SCK

Bit number313029282726252423222120191817161514131211109876543210
IdC                         AAAAA
Reset 0xFFFFFFFF11111111111111111111111111111111
IdRWFieldValue IdValueDescription
ARWPIN [0..31]Pin number
CRWCONNECT  Connection
   Disconnected1Disconnect
   Connected0Connect
 

PSEL.MOSI

Address offset: 0x50C

Pin select for MOSI signal

Bit number313029282726252423222120191817161514131211109876543210
IdC                         AAAAA
Reset 0xFFFFFFFF11111111111111111111111111111111
IdRWFieldValue IdValueDescription
ARWPIN [0..31]Pin number
CRWCONNECT  Connection
   Disconnected1Disconnect
   Connected0Connect
 

PSEL.MISO

Address offset: 0x510

Pin select for MISO signal

Bit number313029282726252423222120191817161514131211109876543210
IdC                         AAAAA
Reset 0xFFFFFFFF11111111111111111111111111111111
IdRWFieldValue IdValueDescription
ARWPIN [0..31]Pin number
CRWCONNECT  Connection
   Disconnected1Disconnect
   Connected0Connect
 

FREQUENCY

Address offset: 0x524

SPI frequency. Accuracy depends on the HFCLK source selected.

Bit number313029282726252423222120191817161514131211109876543210
IdAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA
Reset 0x0400000000000100000000000000000000000000
IdRWFieldValue IdValueDescription
ARWFREQUENCY  SPI master data rate
   K1250x02000000125 kbps
   K2500x04000000250 kbps
   K5000x08000000500 kbps
   M10x100000001 Mbps
   M20x200000002 Mbps
   M40x400000004 Mbps
   M80x800000008 Mbps
 

RXD.PTR

Address offset: 0x534

Data pointer

Bit number313029282726252423222120191817161514131211109876543210
IdAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA
Reset 0x0000000000000000000000000000000000000000
IdRWFieldValue IdValueDescription
ARWPTR  Data pointer
 

RXD.MAXCNT

Address offset: 0x538

Maximum number of bytes in receive buffer

Bit number313029282726252423222120191817161514131211109876543210
Id                        AAAAAAAA
Reset 0x0000000000000000000000000000000000000000
IdRWFieldValue IdValueDescription
ARWMAXCNT  Maximum number of bytes in receive buffer
 

RXD.AMOUNT

Address offset: 0x53C

Number of bytes transferred in the last transaction

Bit number313029282726252423222120191817161514131211109876543210
Id                        AAAAAAAA
Reset 0x0000000000000000000000000000000000000000
IdRWFieldValue IdValueDescription
ARAMOUNT  Number of bytes transferred in the last transaction
 

RXD.LIST

Address offset: 0x540

EasyDMA list type

Bit number313029282726252423222120191817161514131211109876543210
Id                             AAA
Reset 0x0000000000000000000000000000000000000000
IdRWFieldValue IdValueDescription
ARWLIST  List type
   Disabled0Disable EasyDMA list
   ArrayList1Use array list
 

TXD.PTR

Address offset: 0x544

Data pointer

Bit number313029282726252423222120191817161514131211109876543210
IdAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA
Reset 0x0000000000000000000000000000000000000000
IdRWFieldValue IdValueDescription
ARWPTR  Data pointer
 

TXD.MAXCNT

Address offset: 0x548

Maximum number of bytes in transmit buffer

Bit number313029282726252423222120191817161514131211109876543210
Id                        AAAAAAAA
Reset 0x0000000000000000000000000000000000000000
IdRWFieldValue IdValueDescription
ARWMAXCNT  Maximum number of bytes in transmit buffer
 

TXD.AMOUNT

Address offset: 0x54C

Number of bytes transferred in the last transaction

Bit number313029282726252423222120191817161514131211109876543210
Id                        AAAAAAAA
Reset 0x0000000000000000000000000000000000000000
IdRWFieldValue IdValueDescription
ARAMOUNT  Number of bytes transferred in the last transaction
 

TXD.LIST

Address offset: 0x550

EasyDMA list type

Bit number313029282726252423222120191817161514131211109876543210
Id                             AAA
Reset 0x0000000000000000000000000000000000000000
IdRWFieldValue IdValueDescription
ARWLIST  List type
   Disabled0Disable EasyDMA list
   ArrayList1Use array list
 

CONFIG

Address offset: 0x554

Configuration register

Bit number313029282726252423222120191817161514131211109876543210
Id                             CBA
Reset 0x0000000000000000000000000000000000000000
IdRWFieldValue IdValueDescription
ARWORDER  Bit order
   MsbFirst0Most significant bit shifted out first
   LsbFirst1Least significant bit shifted out first
BRWCPHA  Serial clock (SCK) phase
   Leading0Sample on leading edge of clock, shift serial data on trailing edge
   Trailing1Sample on trailing edge of clock, shift serial data on leading edge
CRWCPOL  Serial clock (SCK) polarity
   ActiveHigh0Active high
   ActiveLow1Active low
 

ORC

Address offset: 0x5C0

Over-read character. Character clocked out in case and over-read of the TXD buffer.

Bit number313029282726252423222120191817161514131211109876543210
Id                        AAAAAAAA
Reset 0x0000000000000000000000000000000000000000
IdRWFieldValue IdValueDescription
ARWORC  Over-read character. Character clocked out in case and over-read of the TXD buffer.
 

Electrical specification

SPIM master interface electrical specifications

SymbolDescriptionMin.Typ.Max.Units
fSPIMBit rates for SPIM182Mbps
ISPIM,2MbpsRun current for SPIM, 2 Mbps50µA
ISPIM,8MbpsRun current for SPIM, 8 Mbps50µA
ISPIM,IDLEIdle current for SPIM (STARTed, no CSN activity)1µA
tSPIM,STARTTime from START task to transmission started......µs

Serial Peripheral Interface Master (SPIM) timing specifications

SymbolDescriptionMin.Typ.Max.Units
tSPIM,CSCKSCK period......ns
tSPIM,RSCK,LDSCK rise time, standard driveatRF,25pF 
tSPIM,RSCK,HDSCK rise time, high driveatHRF,25pF 
tSPIM,FSCK,LDSCK fall time, standard driveatRF,25pF 
tSPIM,FSCK,HDSCK fall time, high driveatHRF,25pF 
tSPIM,WHSCKSCK high timea(0.5*tCSCK) – tRSCK 
tSPIM,WLSCKSCK low timea(0.5*tCSCK) – tFSCK 
tSPIM,SUMIMISO to CLK edge setup time19ns
tSPIM,HMICLK edge to MISO hold time18ns
tSPIM,VMOCLK edge to MOSI valid59ns
tSPIM,HMOMOSI hold time after CLK edge20ns
Figure 4. SPIM timing diagram
1 High bit rates may require GPIOs to be set as High Drive, see GPIO chapter for more details.
2 The actual maximum data rate depends on the slave's CLK to MISO and MOSI setup and hold timings.
a At 25pF load, including GPIO pin capacitance, see GPIO spec.