Talk about the cyclic redundancy check technology commonly used in embedded systems!

In embedded product applications, it is often necessary to deal with the integrity of system data during storage or transmission.

The so-called integrity refers to the accuracy and consistency of data in its life cycle. These data may be stored in EEPROM/FLASH, or transmitted based on communication protocols, they may be destroyed due to external interference, program errors, or even system intrusion. If these data are not verified before use, the product function may be invalid. In some specific areas, it may endanger the user's property and even life safety in severe cases.

This article will talk about the more widely used cyclic redundancy check technology, as well as some specific experience in STM32.

The so-called cyclic redundancy check (CRC: Cyclic Redundancy Check) is an error detection algorithm, which is usually used in communication protocols or storage devices to detect unexpected changes in original data. It can be simply understood as calculating the useful data according to a certain algorithm, extracting a feature value, and attaching it to the useful data. In the application, the useful data is extracted according to a specific algorithm to compare the feature value with the pre-stored feature value. If it is equal, the verification passes, otherwise the verification fails, so as to identify whether the data is abnormal.

Why verify data integrity (Data Integrity)?

Data may change during storage and transmission. Taking data communication application scenarios as an example, there are roughly two failure modes for common errors:

  • Single Bit Error : Only one data bit has an error, as shown in the figure:

  • Burst Error: Two or more data bits have errors in the code stream, as shown in the figure:

Why might these bit errors occur? For electronic system communication, it involves the physical layer, link layer, communication medium, etc. The physical layer mainly modulates the original binary data using a certain codec principle, and then transmits the modulated signal to the transmission medium through the sending circuit. The receiving end uses the receiving circuit to receive and demodulate, and restore the information into a binary code stream. In this process, the medium may be interfered, and the receiving circuit, the sending circuit, the modulation circuit, and the demodulation circuit may all work out of work due to some interference reasons and cause bit errors. At this time, if there is no good mechanism to ensure the correctness of the data, such as certain control commands in a flight control system, CAN message data in the vehicle system, the system directly uses these error data to control the controlled object (such as the motor , Engines, etc.), in serious cases, it will cause immeasurable life and property disasters.

The same goes for data in the storage system. Generally speaking, when the system is powered on, the system parameters, such as some calibration data, will be loaded from the physical storage medium. If some bits of the media are damaged or software bugs cause the data to be mishandled without data integrity detection, such data is directly applied to system control, which will also cause security risks.

Therefore, the importance of data integrity testing is self-evident. There are many common data integrity algorithms, such as simple XOR check, CRC cyclic redundancy check, FEC forward error correction algorithm and so on. The cyclic redundancy check is widely used in embedded systems, and it is widely used in communication protocol formulation, data storage, and compression and decompression algorithms.

Cyclic Redundancy Check uses binary division as the algorithm principle and has a powerful error detection mechanism. It can be realized by using a small amount of hardware logic circuit for binary division. As for the software code implementation, there are two ideas and strategies: look-up table method and shift calculation. The look-up method uses space for time, and the shift calculation method uses time for space.

What is cyclic redundancy check?

The core mathematical algorithm principle of the cyclic redundancy check is based on the cyclic code, which expands the information on the basis of not increasing the information of the original data, and stores its redundant characteristics at a very small storage cost. The algorithm was invented by W. Wesley Peterson in 1961.

  • The n-bit binary data here is the effective information load. (May be useful information transmitted or stored)

  • The m-bit redundancy code is calculated according to the CRC algorithm, that is, the characteristic redundancy code is extracted from the previous valid data according to the CRC check polynomial and the CRC algorithm. This is the true meaning of redundancy.

  • What is actually transmitted or stored is n+m-bit binary data.

Here is a concept: polynomial. In the CRC check algorithm, the polynomial can be understood and expressed as follows:

Its essence is the mathematical representation of multi-base, here is binary, so X is 2.

The basic algorithm processing process is as follows:

Assuming that the valid data to be sent is a binary polynomial M(x), and the check polynomial P(x) is agreed upon by the sender and receiver, and both parties know it, here are the meanings of several polynomials and related processing procedures:

The receiver performs a CRC check after receiving the data. The remainder is 0, and the check is passed.

In fact, the essence of CRC is the calculation process of binary polynomial division to obtain redundant codes. The essence is the same regardless of the software look-up method, shift calculation method, or pure hardware logic circuit implementation. It is more advantageous to use shift calculation for digital logic circuits, because it takes almost no CPU time.

Common CRC check polynomial

What are the common CRC check polynomial operators?

In addition to the complexity of different check polynomials, what are the differences from an application point of view? From an application perspective, it is mainly reflected in the error diagnosis rate. Take a look at the error detection effect of CRC-16 and CRC-CCITT:

  • Can fully detect single bit and double bit errors

  • Odd bits error

  • Can detect 16-bit length and burst errors less than 16

  • Can detect errors with a length of 17 bits or more with a probability of 99.997%

Choosing different check polynomial operators has different bit error diagnosis success rates, and of course its calculation overhead is also different. Let's check the authoritative IEC standard. The figure below is cut from "IEC61508-7".

It can be seen from the above that CRC-8 can diagnose a 99.6% bit error probability, while CRC-16 can increase to a 99.998% bit error probability.

Note: IEC61508 is the International Electrotechnical Commission's functional safety standard (Functional safety of electrical/electronic/programmable electronicsafety-related systems).

Since the development of technology, a large number of different check polynomial generators have been used in various industries. Below is a screenshot from wikipedia for your reference:

STM32 CRC hardware peripherals

As shown in the figure below, STM32 has a built-in CRC-32 hardware calculation unit, which realizes a fixed polynomial 0x4C11DB7 (hexadecimal representation), which can be applied to the calculation of the Ethernet packet check code.

All STM32 products have CRC peripherals, which provide hardware support for CRC calculations, saving application code storage space. The CRC check value can be used to verify the correctness of the data in transmission and also to check the integrity of the data during storage. In IEC60335, it is also accepted to check the integrity of FLASH through CRC verification. In the application of FLASH integrity check, it is necessary to calculate the CRC check value of the entire FLASH in advance (excluding the byte that saved the CRC value at the end) and place it at the end of the FLASH. In the process of program startup or running, recalculate the CRC check value of the entire FLASH using the same method, and then compare it with the CRC value stored in the address space at the end of the FLASH.

EWARM has supported CRC calculation of STM32 chips since v5.5. The process of calculating the CRC check value of the entire FLASH and storing it at the end of the FLASH can be completed in IAR. By configuring the CRC calculation parameters of EWARM, CRC calculation is automatically performed on the entire FLASH space, and the calculation result is placed at the end of the internal FLASH space.

You may ask, what is the application value of this? Take the MCU program-based upgrade as an example. During the code upgrade process, if the binary program file passed in by the bootloader upgrade interface is not verified, the code errors that occurred during the upgrade process cannot be found in time. On the contrary, if the original code has a check code added, the upgrade program will perform a check calculation after receiving the upgrade file and compare it with the check code at the end of the file to be upgraded. If it does not match, the upgrade will be abandoned, so that it will not be changed. Invalid and even potentially safe code is written into the chip.

  • Modify the Link file, specify the storage location of checksum in FLASH, and add the following statement in the Link file.

place at end of ROM_region { ro p .checksum };    

This statement specifies to place the CRC value at the end of the FLASH space. It is the end of the entire FLASH space, not the end of the application code. In this way, the position of the CRC value is fixed and will not change with the code size. 

  • Configure the parameters of the Checksum page

IAR Checksum page description (v6.4 and above)

IAR's checksum page is divided into two parts:

  • The part encircled in red : defines the range of CRC that needs to be calculated in FLASH and the filling value of free bytes.

  • The setting part of checksum calculation parameters: Checksum size: select the size of checksum (number of bytes)
    Alignment: specify the alignment of checksum. If it is left blank, the default is 2 byte alignment.

    Algorithm: Select the algorithm of checksum

    Complement: Whether to perform complement calculation. Choosing "As is" means no complement calculation.

    Bit order: the order of bit output. MSB first, the high bit of each byte comes first. LSB first, the low bit of each byte is first.

    Reverse byte order within word: For input data, reverse the order of each byte within a word.

    Initial value: Initial value calculated by checksum 

    Checksum unit size: Select the unit size for iteration, whether it is 8-bit, 16-bit or 32-bit for iteration.

  • IAR configuration when STM32 CRC peripherals use the default configuration

STM32CRC peripheral configuration:

POLY= 0x4C11DB7(CRC32) 

Initial_Crc = 0Xffffffff 

Input/output data is not inverted 

Input data: 0x08000000~0x0801FFFB. (The last 4 bytes are used to put the calculated CRC value)

During the experiment, it was found that "Alignment" seemed to have no effect on the calculated CRC value. But the configuration of "Reverse byte order within word" and "Checksumunit size" has a certain relationship. If the latter chooses 32-bit, you cannot check the former; if the latter chooses 8-bit, you must check "Reverse byte order within word". You can also refer to the following figure to set:

For versions below IAR v6.4, there is no "Checksum unit size" option. The reference configuration is as follows:

How to write code?

As described above, this application can be used to verify data in Flash. The reference code is as follows:

/*-1- 配置CRC外设 */ 
  CrcHandle.Instance = CRC; 
 
  /* 默认二进制多项式使能 */ 
  CrcHandle.Init.DefaultPolynomialUse    = DEFAULT_POLYNOMIAL_ENABLE; 
 
  /* 默认初值设置 */ 
  CrcHandle.Init.DefaultInitValueUse     = DEFAULT_INIT_VALUE_ENABLE; 
 
  /* 输入数据不反转 */ 
  CrcHandle.Init.InputDataInversionMode  = CRC_INPUTDATA_INVERSION_NONE; 


 /* 输出数据不反转 */ 
  CrcHandle.Init.OutputDataInversionMode = CRC_OUTPUTDATA_INVERSION_DISABLED; 
 
  /* 输入数据基本单元长度为32bit */ 
  CrcHandle.InputDataFormat              = CRC_INPUTDATA_FORMAT_WORDS; 
 
  if (HAL_CRC_Init(&CrcHandle) != HAL_OK) 
  { 
    /* 初始化错误 */ 
    Error_Handler(); 
  } 
 
  pdata = (uint32_t*)ROM_START; 
 
  /*##-2- 调用HAL库利用硬件CRC外设对ROM区计算CRC-32校验码*/ 
  uwCRCValue = HAL_CRC_Calculate(&CrcHandle, pdata, ROM_SIZEinWORDS);


summary

For CRC applications, you can also write pure software solutions based on polynomial operators. There are many ready-made codes on the Internet. The basic idea is nothing more than the look-up table method and the shift calculation method. The difference is that one sacrifices storage space in exchange for computing efficiency, and the other sacrifices computing time to save storage space. As for how to choose, it is based on the comprehensive consideration of the designed system, generally based on the application scenario.

  • Use the CRC algorithm to calculate the redundant code of the block data. Some articles and standards call this redundant code a signature. In actual application, the check code obtained by calculating the valid data is compared with the pre-stored check code. If they are equal, the check is passed, otherwise, the check fails. Of course, the original data and the stored check code can also be passed into the check algorithm, and if the result is 0, the check passes, otherwise it fails.

  • For data communication, a valid data check code is generally added to the end of the message, and the receiver verifies the data integrity of the received message.


1. Analyze the impact of "interpretability" on artificial intelligence from an embedded perspective!

2. [MCU] Register, standard library, HAL library, LL library, so many libraries! How do you ask me to choose?

3. How many steps are there to develop embedded projects with Linux?

4. How does the program itself know its size? This is a question of whether a chicken lays an egg or an egg lays a chicken!

5. Domestic integrated development environment helps domestic RISC-V break the monopoly of foreign giants in chip technology

6. When doing embedded development, how do you realize LCD display?

Disclaimer: This article is reproduced online, and the copyright belongs to the original author. If you are involved in copyright issues, please contact us, we will confirm the copyright based on the copyright certification materials you provide and pay the author's remuneration or delete the content.

Guess you like

Origin blog.csdn.net/DP29syM41zyGndVF/article/details/109881592