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.
Previous article:Main features of STM8 I2C interface
Next article:STM8 SPI status flags
- Popular Resources
- Popular amplifiers
- Learn ARM development(16)
- Learn ARM development(17)
- Learn ARM development(18)
- Embedded system debugging simulation tool
- A small question that has been bothering me recently has finally been solved~~
- Learn ARM development (1)
- Learn ARM development (2)
- Learn ARM development (4)
- Learn ARM development (6)
Professor at Beihang University, dedicated to promoting microcontrollers and embedded systems for over 20 years.
- LED chemical incompatibility test to see which chemicals LEDs can be used with
- Application of ARM9 hardware coprocessor on WinCE embedded motherboard
- What are the key points for selecting rotor flowmeter?
- LM317 high power charger circuit
- A brief analysis of Embest's application and development of embedded medical devices
- Single-phase RC protection circuit
- stm32 PVD programmable voltage monitor
- Introduction and measurement of edge trigger and level trigger of 51 single chip microcomputer
- Improved design of Linux system software shell protection technology
- What to do if the ABB robot protection device stops
- CGD and Qorvo to jointly revolutionize motor control solutions
- CGD and Qorvo to jointly revolutionize motor control solutions
- Keysight Technologies FieldFox handheld analyzer with VDI spread spectrum module to achieve millimeter wave analysis function
- Infineon's PASCO2V15 XENSIV PAS CO2 5V Sensor Now Available at Mouser for Accurate CO2 Level Measurement
- Advanced gameplay, Harting takes your PCB board connection to a new level!
- Advanced gameplay, Harting takes your PCB board connection to a new level!
- A new chapter in Great Wall Motors R&D: solid-state battery technology leads the future
- Naxin Micro provides full-scenario GaN driver IC solutions
- Interpreting Huawei’s new solid-state battery patent, will it challenge CATL in 2030?
- Are pure electric/plug-in hybrid vehicles going crazy? A Chinese company has launched the world's first -40℃ dischargeable hybrid battery that is not afraid of cold
- Career Options
- ST BlueNRG-LP Evaluation Board DesignSpark Unboxing
- TI Engineer's Annual Review: Processors (Hot Products: MSP430, C2000, C6000 Multicore, etc.) Selected Q&A
- How to choose an analog-to-digital converter?
- [Project source code] FPGA-based asynchronous FIFO show-ahead mode
- [Construction Monitoring and Security System] XIV. Realization of the core functions of the project
- Is the Linux interrupt handling process global or local interrupts?
- RSL10-002GEVB power management node design
- How to measure current ranging from 1uA to 1.5A
- Analysis and application of four common constant current source circuits