STM8 CRC calculation

Publisher:温暖阳光Latest update time:2020-07-02 Source: eefocusKeywords:STM8 Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere

STM8 CRC calculation

CRC check is only used to ensure the reliability of communication. Separate CRC calculators are used for data transmission and data reception. CRC is calculated by performing a programmable polynomial operation on each received bit. The CRC calculation is performed on the sampling clock edge defined by the CPHA and CPOL bits in the SPI_CR1 register.

CRC calculation is enabled by setting the CRCEN bit in the SPI_CR1 register. The CRC registers (SPI_RXCRCR and SPI_TXCRCR) are reset at the same time when the CRCEN bit is set. When the CRCNEXT bit of SPI_CR2 is set, the content of SPI_TXCRCR will be sent after the current byte is sent.

If there is already a byte in the Tx buffer, the CRC value will be sent after the byte is sent. During the process of sending the CRC value, the CRC calculator is turned off and the value of the CRC register remains unchanged.


If during the transmission of the SPI_TXCRCR value, the received shift register value does not match the SPI_RXCRCR value, the CRCERR flag in the SPI_SR register is set.


SPI communication can use the STM8's CRC by following these steps:

  • Set the values ​​of CPOL, CPHA, LSBFirst, BR, SSM, SSI, and MSTR;

  • Enter the polynomial in the SPI_CRCPR register;

  • Enable CRC calculation by setting the CRCEN bit in the SPI_CR1 register. This operation will also clear the registers SPI_RXCRCR and SPI_TXCRCR.

  • Set the SPE bit of the SPI_CR1 register to start the SPI function;

  • Start communication and maintain communication until only the last byte is left to be sent or received;

  • When the last byte is written into the transmit buffer, the CRCNext bit of SPI_CR2 is set to instruct the hardware to send the CRC after the last data segment is sent. During the CRC transmission, the CRC calculation stops;

  • When the last byte is sent, the SPI sends the CRC and the CRCNext bit is reset. Similarly, the received CRC is compared with the SPI_RXCRCR value. If the comparison does not match, the CRCERR flag on the SPI_SR is set, and an interrupt is generated when the ERRIE of the SPI_ICR register is set.

Note: When the SPI clock frequency is high, the user must be careful when using CRC check transmission. During the entire transmission period using CRC data check, the CPU time should be used as little as possible. In order to avoid errors when receiving the last data and CRC, function calls should be disabled during the data transmission process with CRC check value.

Keywords:STM8 Reference address:STM8 CRC calculation

Previous article:Main features of STM8 I2C interface
Next article:STM8 SPI status flags

Latest Microcontroller Articles
Change More Related Popular Components

EEWorld
subscription
account

EEWorld
service
account

Automotive
development
circle

About Us Customer Service Contact Information Datasheet Sitemap LatestNews


Room 1530, 15th Floor, Building B, No.18 Zhongguancun Street, Haidian District, Beijing, Postal Code: 100190 China Telephone: 008610 8235 0740

Copyright © 2005-2024 EEWORLD.com.cn, Inc. All rights reserved 京ICP证060456号 京ICP备10001474号-1 电信业务审批[2006]字第258号函 京公网安备 11010802033920号