www.geehy.com Page 300
(it should be odd, and does not support even number).
Enable CRC computing by configuring CRCEN bit in SPI_CTRL1 register; at the
same time, reset the CRC register (SPI_RXCRC and SPI_TXCRC).
CRC is managed by CPU during transmission
To obtain the CRC value of transmission calculation, after the last data is written
to the transmit buffer, it is required to set CRCNXT bit of SPI_CTRL1; indicate
that the hardware sends the CRC value after the last data is transmitted, and the
CRCNXT bit will be cleared; during CRC data transmission, CRC computing will
be frozen.
The received CRC data will be stored in RXFIFO. A CRC transaction usually
needs one more data frame to communicate at the end of the data sequence.
However, when an 8-bit data frame checked by 16-bit CRC is set, two data
frames are needed to send the complete CRC. When the last CRC data is
received, the received value and the value of SPI_RXCRC register will be
compared. By checking CRCEFLG flag bit in SPI_STS register, judge whether
the data are damaged in the process of transmission. CRCEFLG bit can be
cleared by writing 0. RXBNEFLG bit can be cleared by reading SPI_DATA
register.
Sequence of clearing CRC values
(
1
)
Disable SPI (SPIEN=0)
(
2
)
Clear CRCEN bit
(
3
)
Set CRCEN bit to 1
(
4
)
Enable SPI (SPIEN=1)
Note: When SPI works in slave mode, the software must enable CRC operation when the clock is stable.
And in the data phase and CRC phase, the NSS signal needs to be pulled down and maintained.
DMA Function
For high-speed data transmission, the request/response DMA mechanism in SPI
improves the system efficiency and can transfer data to SPI transmit buffer
promptly, and the receive buffer can read the data in time to prevent overflow.
When SPI only sends data, it is only needed to enable DMA transmission
channel.
When SPI only receives data, it is only needed to enable DMA receiving channel.
DMA function of SPI mode can be enabled by configuring TXDEN and RXDEN
bits of SPI_CTRL2 register.
When transmitting: When TXBEFLG flag bit is set to 1, issue the DMA
request, DMA controller writes data to SPI_DATA, and then the
TXBEFLG flag bit will be cleared.
When receiving: When setting RXBNEFLG flag bit to 1, issue the DMA
request, DMA controller reads data from SPI_DATA register, and then
RXBNEFLG flag bit is cleared.
By monitoring BSYFLG flag bit, confirm whether SPI communication is over after
DMA has transferred all data to be transmitted in transmit mode, which can avoid
damaging the transmission of last data.