583
/
838
Nations Technologies Inc.
Tel
:
+86-755-86309900
:
Address: Nations Tower, #109 Baoshen Road, Hi-tech Park North.
Nanshan District, Shenzhen, 518057, P.R.China
Figure 21-12 Reception using DMA
21.3.6
CRC calculation
SPI contains two independent CRC calculators for data sending and data receiving to ensure the correctness of data
transmission. According to the sending and receiving data frame format, CRC adopts different calculation methods,
the 8-bit data frame format adopts CRC8, and the 16-bit data frame format adopts CRC16. The polynomial used in
the SPI CRC calculation is set by the SPI_CRCPOLY register, and the user enables the CRC calculation by setting
the SPI_CTRL1.CRCEN = 1.
In send mode, after the last data is written into the send buffer, set the SPI_CTRL1.CRCNEXT = 1, which indicates
that the hardware will start sending the CRC value (SPI_CRCTDAT value) after sending the data. When the CRC is
sent, the CRC calculation will stop.
In receive mode, after the penultimate data frame is received, set the SPI_CTRL1.CRCNEXT = 1. The received CRC
and SPI_CRCRDAT values are compared, if they are different, the SPI_STS.CRCERR bit is set to 1. If the
SPI_CTRL2.ERRINTEN bit is set to 1, an interrupt will be generated.
In order to keep the synchronization of the next CRC calculation result of the master-slave device, the user should
clear the CRC value of the master-slave device. Setting the SPI_CTRL1.CRCEN bit resets the SPI_CRCRDAT and
SPI_CRCTDAT registers. Take the following steps in order: SPI_CTRL1.SPIEN = 0; SPI_CTRL1.CRCEN = 0;
SPI_CTRL1.CRCEN = 1; SPI_CTRL1.SPIEN = 1.
Most importantly, when the SPI is configured in slave mode and CRC is enabled, as long as there is a clock pulse on
SCLK pin, the CRC calculation will still be performed even if the NSS pin is high. This situation is common when
the master device communicates with multiple slave devices alternately, so it is necessary to avoid CRC misoperation.
When the SPI hardware CRC check is enabled (SPI_CTRL1.CRCEN = 1) and the DMA is enabled, the hardware
automatically completes the sending and receiving of CRC bytes when the communication ends.
b0
b2
b1
b3
b4
b6
b5
b0
b7
b1
b2
b3
b5
b4
b6
b7
b1
b0
b3
b2
b4
b5
b7
b6
DATA1=0xAA
DATA2=0xBB
DATA3=0xCC
SCK
MISO/MOSI
(in)
RNE flag
Rx buffer
(
read from SPI_DAT
)
CLKPOL=1.CLKPHA=1
Set by hardware
Clear by DMA
read
Configure the DMA
Rx channel and
enable the SPI
DMA reads 0xAA from
SPI_DAT
DMA transfer is complete
0xAA
DMA request
DMA read from
SPI_DAT
DMA flag
(
DMA transfer complete
)
DMA reads 0xBB from
SPI_DAT
DMA reads 0xCC from SPI_DAT
0xBB
0xCC
Set by hardware
Clear by software