cancel
Showing results for 
Search instead for 
Did you mean: 

STM32L073 SPI CRC calculation

waiyang93
Associate II
Posted on July 19, 2016 at 09:06

I am using SPI to communicate with an external NVM memory and hope to have some data validate at the end by using CRC. But I do not understand how does the SPI CRC calculation contribute to verify the data that I transmit and received, can anyone explain?

/*##-1- Configure the SPI peripheral #######################################*/
/* Set the SPI parameters */
SpiHandle.Instance = SPIx;
SpiHandle.Init.BaudRatePrescaler = SPI_BAUDRATEPRESCALER_256;
SpiHandle.Init.Direction = SPI_DIRECTION_2LINES;
SpiHandle.Init.CLKPhase = SPI_PHASE_1EDGE; 
SpiHandle.Init.CLKPolarity = SPI_POLARITY_LOW; 
SpiHandle.Init.DataSize = SPI_DATASIZE_8BIT;
SpiHandle.Init.FirstBit = SPI_FIRSTBIT_MSB;
SpiHandle.Init.TIMode = SPI_TIMODE_DISABLE;
SpiHandle.Init.CRCCalculation = SPI_CRCCALCULATION_ENABLE; <-----
SpiHandle.Init.CRCPolynomial = 7;
SpiHandle.Init.NSS = SPI_NSS_SOFT;

3 REPLIES 3
Walid FTITI_O
Senior II
Posted on July 19, 2016 at 17:25

Hi wy,

As mentioneed in reference manual :

''

Hardware CRC feature for reliable communication:

–CRC value can be transmitted as last byte in Tx mode

–Automatic CRC error checking for last received byte

'' 32.3.7 SPI onfiguration part describes how to configure the CRC feature.

And this part describes how to check the CRC error:

''

CRC error (CRCERR)

This flag is used to verify the validity of the value received when the CRCEN bit in the

SPIx_CR1 register is set. The CRCERR flag in the SPIx_SR register is set if the value

received in the shift register does not match the receiver SPIx_RXCRC value. The flag is

cleared by the software.

''

I recommend to have a look to the example in

http://www.st.com/content/st_com/en/products/embedded-software/mcus-embedded-software/stm32-embedded-software/stm32cube-embedded-software/stm32cubel0.html

about CRC feature at this path: STM32Cube_FW_L0_V1.7.0\Projects\STM32L073Z_EVAL\Examples\CRC\CRC_Example\Projects\STM32L073Z_EVAL\Examples\CRC\CRC_Example

-Hannibal-

Posted on July 19, 2016 at 18:43

This is for peripherals that support CRC in their SPI protocol.

The SPI could be used to perform a CRC calculation of your data as written if after initializing and sending your whole data stream you would then switch modes and have the SPI peripheral shift out the computed CRC. In the read back case you'd read all your data and the CRC, and then the CRC would be zero (ie division with no remainder).

For data integrity in your own memory structures you'd probably want to use a CRC or checksum of your own.

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..
waiyang93
Associate II
Posted on July 20, 2016 at 04:41

then probably I would need to change my code, because right now I am sending byte by byte and this caused the CRC generated every byte and the receive data CRC check back incorrect.

This is my code:

uint8_t sFLASH_SendByte(uint8_t byte)
{
uint8_t TxBuf[1]; // Buffer used for transmission
memset(TxBuf, 0, 1);
uint8_t RxBuf[1]; // Buffer used for reception
memset(RxBuf, 0, 1);
TxBuf[0] = byte;
HAL_SPI_TransmitReceive_IT(&SpiHandle, (uint8_t*)TxBuf, (uint8_t*)RxBuf, 1);
while (wTransferState == TRANSFER_WAIT)
{
}
wTransferState = TRANSFER_WAIT;
return RxBuf[0];
}
uint8_t sFLASH_ReadByte(void)
{
return (sFLASH_SendByte(DUMMY_BYTE));
}

Currently , I am think using XOR method to XOR all the data that transmitted out and saved the XOR result after the data. Is this method acceptable? Data the be saved: [byte1][byte2][byte3][XOR byte] XOR byte = byte1 ^ byte 2 ^ byte 3