[STM32] 这些CRC7校验码怎么算出来的

[复制链接]
 楼主| 比神乐 发表于 2024-9-2 11:46 | 显示全部楼层 |阅读模式

我找到一个STM32L4R5ZIT6的关于CRC的例程,


代码如下

  1. #include "main.h"

  2. /** @addtogroup STM32L4xx_HAL_Examples
  3.   * @{
  4.   */

  5. /** @addtogroup CRC_Bytes_Stream_7bit_CRC
  6.   * @{
  7.   */

  8. /* Private typedef -----------------------------------------------------------*/
  9. /* Private define ------------------------------------------------------------*/
  10. #define BUFFER_SIZE_5  5  /* CRC7_DATA8_TEST5[] is 5-byte long   */
  11. #define BUFFER_SIZE_17 17 /* CRC7_DATA8_TEST17[] is 17-byte long */
  12. #define BUFFER_SIZE_1  1  /* CRC7_DATA8_TEST1[] is 1-byte long   */
  13. #define BUFFER_SIZE_2  2  /* CRC7_DATA8_TEST2[] is 2-byte long   */

  14. /* User-defined polynomial */
  15. #define CRC_POLYNOMIAL_7B  0x65  /* X^7 + X^6 + X^5 + X^2 + 1,
  16.                                    used in Train Communication Network, IEC 60870-5[17] */

  17. /* Private macro -------------------------------------------------------------*/
  18. /* Private variables ---------------------------------------------------------*/
  19. /* CRC handler declaration */
  20. CRC_HandleTypeDef   CrcHandle;

  21. /* Used for storing CRC Value */
  22. __IO uint32_t uwCRCValue = 0;

  23. /* Bytes buffers that will consecutively yield CRCs */
  24. static const uint8_t CRC7_DATA8_TEST5[5]   = {0x12,0x34,0xBA,0x71,0xAD};
  25. static const uint8_t CRC7_DATA8_TEST17[17] = {0x12,0x34,0xBA,0x71,0xAD,
  26.                                               0x11,0x56,0xDC,0x88,0x1B,
  27.                                               0xEE,0x4D,0x82, 0x93,0xA6,
  28.                                               0x7F,0xC3};
  29. static const uint8_t CRC7_DATA8_TEST1[1]   = {0x19};                                                
  30. static const uint8_t CRC7_DATA8_TEST2[2]   = {0xAB,0xCD};

  31.       

  32. /* Expected CRC Values */
  33. /* The 7 LSB bits are the 7-bit long CRC */
  34. uint32_t uwExpectedCRCValue_1 = 0x00000057;    /* First byte stream CRC  */
  35. uint32_t uwExpectedCRCValue_2 = 0x0000006E;    /* Second byte stream CRC */
  36. uint32_t uwExpectedCRCValue_3 = 0x0000004B;    /* Third byte stream CRC  */
  37. uint32_t uwExpectedCRCValue_4 = 0x00000026;    /* Fourth byte stream CRC */

  38. /* Private function prototypes -----------------------------------------------*/
  39. void SystemClock_Config(void);
  40. static void Error_Handler(void);

  41. /* Private functions ---------------------------------------------------------*/

  42. /**
  43.   * [url=home.php?mod=space&uid=159083]@brief[/url] Main program
  44.   * @param  None
  45.   * @retval None
  46.   */
  47. int main(void)
  48. {

  49.   /* STM32L4xx HAL library initialization:
  50.        - Configure the Flash prefetch
  51.        - Systick timer is configured by default as source of time base, but user
  52.          can eventually implement his proper time base source (a general purpose
  53.          timer for example or other time source), keeping in mind that Time base
  54.          duration should be kept 1ms since PPP_TIMEOUT_VALUEs are defined and
  55.          handled in milliseconds basis.
  56.        - Set NVIC Group Priority to 4
  57.        - Low Level Initialization
  58.      */
  59.   HAL_Init();
  60.   
  61.   /* Configure the system clock to 120 MHz */
  62.   SystemClock_Config();

  63.   /* Configure LED1 and LED3 */
  64.   BSP_LED_Init(LED1);
  65.   BSP_LED_Init(LED3);


  66.   /****************************************************************************/
  67.   /*                                                                          */
  68.   /*                     CRC peripheral initialization                        */
  69.   /*                                                                          */   
  70.   /****************************************************************************/
  71.    
  72.   CrcHandle.Instance = CRC;

  73.   /* The default polynomial is not used. The one to be used must be defined
  74.      in CrcHandle.Init.GeneratingPolynomial */  
  75.   CrcHandle.Init.DefaultPolynomialUse    = DEFAULT_POLYNOMIAL_DISABLE;
  76.   
  77.   /* Set the value of the generating polynomial.
  78.     The one used in that example is the 7-bit long CRC generating
  79.     polynomial X^7 + X^6 + X^5 + X^2 + 1 */
  80.   CrcHandle.Init.GeneratingPolynomial    = CRC_POLYNOMIAL_7B;
  81.   
  82.   /* The user-defined generating polynomial yields a 7-bit long CRC */
  83.   CrcHandle.Init.CRCLength               = CRC_POLYLENGTH_7B;

  84.   /* The default init value is used */
  85.   CrcHandle.Init.DefaultInitValueUse     = DEFAULT_INIT_VALUE_ENABLE;

  86.   /* The input data are not inverted */
  87.   CrcHandle.Init.InputDataInversionMode  = CRC_INPUTDATA_INVERSION_NONE;

  88.   /* The output data are not inverted */
  89.   CrcHandle.Init.OutputDataInversionMode = CRC_OUTPUTDATA_INVERSION_DISABLE;

  90.   /* The input data are bytes (8-bit long data) */
  91.   CrcHandle.InputDataFormat              = CRC_INPUTDATA_FORMAT_BYTES;

  92.   /* De-initialize the CRC peripheral */
  93.   if (HAL_CRC_DeInit(&CrcHandle) != HAL_OK)
  94.   {
  95.     /* Initialization Error */
  96.     Error_Handler();
  97.   }  

  98.   /* Then, initialize the CRC handle */
  99.   if (HAL_CRC_Init(&CrcHandle) != HAL_OK)
  100.   {
  101.     /* Initialization Error */
  102.     Error_Handler();
  103.   }


  104.   /****************************************************************************/
  105.   /*                                                                          */
  106.   /*         CRC computation of a first bytes stream                          */
  107.   /*                                                                          */   
  108.   /****************************************************************************/

  109.   /* The 7-bit long CRC of a 5-byte buffer is computed. After peripheral initialization,
  110.      the CRC calculator is initialized with the default value that is 0x7F for
  111.      a 7-bit CRC.
  112.    
  113.     The computed CRC is stored in uint32_t uwCRCValue. The 7-bit long CRC is made of
  114.     uwCRCValue 7 LSB bits. */

  115.   uwCRCValue = HAL_CRC_Accumulate(&CrcHandle, (uint32_t *)&CRC7_DATA8_TEST5, BUFFER_SIZE_5);

  116.   /* Compare the CRC value to the expected one */
  117.   if (uwCRCValue != uwExpectedCRCValue_1)
  118.   {
  119.     /* Wrong CRC value: Turn LED3 on */
  120.     Error_Handler();
  121.   }

  122.   
  123.   /****************************************************************************/
  124.   /*                                                                          */
  125.   /*         CRC computation of a second bytes stream                         */
  126.   /*                                                                          */   
  127.   /****************************************************************************/

  128.   /* The 7-bit long CRC of a 17-byte buffer is computed. The CRC calculator
  129.     is not re-initialized, instead the previously computed CRC is used
  130.     as initial value. */

  131.   uwCRCValue = HAL_CRC_Accumulate(&CrcHandle, (uint32_t *)&CRC7_DATA8_TEST17, BUFFER_SIZE_17);

  132.   /* Compare the CRC value to the expected one */
  133.   if (uwCRCValue != uwExpectedCRCValue_2)
  134.   {
  135.     /* Wrong CRC value: Turn LED3 on */
  136.     Error_Handler();
  137.   }


  138.   /****************************************************************************/
  139.   /*                                                                          */
  140.   /*         CRC computation of a single byte                                 */
  141.   /*                                                                          */   
  142.   /****************************************************************************/

  143.   /* The 7-bit long CRC of a 1-byte buffer is computed. The CRC calculator
  144.     is not re-initialized, instead the previously computed CRC is used
  145.     as initial value. */

  146.   uwCRCValue = HAL_CRC_Accumulate(&CrcHandle, (uint32_t *)&CRC7_DATA8_TEST1, BUFFER_SIZE_1);

  147.   /* Compare the CRC value to the expected one */
  148.   if (uwCRCValue != uwExpectedCRCValue_3)
  149.   {
  150.     /* Wrong CRC value: Turn LED3 on */
  151.     Error_Handler();
  152.   }


  153.   /****************************************************************************/
  154.   /*                                                                          */
  155.   /*         CRC computation of the last bytes stream                         */
  156.   /*                                                                          */   
  157.   /****************************************************************************/

  158.   /* The 7-bit long CRC of a 2-byte buffer is computed. The CRC calculator
  159.     is re-initialized with the default value that is 0x7F for a 7-bit CRC.
  160.     This is done with a call to HAL_CRC_Calculate() instead of
  161.     HAL_CRC_Accumulate(). */

  162.   uwCRCValue = HAL_CRC_Calculate(&CrcHandle, (uint32_t *)&CRC7_DATA8_TEST2, BUFFER_SIZE_2);

  163.   /* Compare the CRC value to the expected one */
  164.   if (uwCRCValue != uwExpectedCRCValue_4)
  165.   {
  166.     /* Wrong CRC value: Turn LED3 on */
  167.     Error_Handler();
  168.   }
  169.   else
  170.   {
  171.     /* Right CRC value: Turn LED1 on */
  172.     BSP_LED_On(LED1);
  173.   }  


  174.   /* Infinite loop */
  175.   while (1)
  176.   {
  177.   }
  178. }

多项式是0x65,没说初始值,有四组数据,

得到的CRC校验值分别为

uint32_t uwExpectedCRCValue_1 = 0x00000057;    /* First byte stream CRC  */

uint32_t uwExpectedCRCValue_2 = 0x0000006E;    /* Second byte stream CRC */

uint32_t uwExpectedCRCValue_3 = 0x0000004B;    /* Third byte stream CRC  */

uint32_t uwExpectedCRCValue_4 = 0x00000026;    /* Fourth byte stream CRC */

我不知道这个校验码是怎么得来的?网上下了好多CRC计算器,算的值都不对。

请问高手,谁知道是怎么计算的?谢谢!



 楼主| 比神乐 发表于 2024-9-2 11:48 | 显示全部楼层
uint8_t crc7_mmc(uint8_t *data, uint16_t length)
{
    uint8_t i;
    uint8_t crc = 0x7f;        // Initial value
    while(length--)
    {
        crc ^= *data++;        // crc ^= *data; data++;
        for ( i = 0; i < 8; i++ )
        {
            if ( crc & 0x80 )
                crc = (crc << 1) ^ 0x65;        // 0x12 = 0x09<<(8-7)
            else
                crc <<= 1;
        }
    }
    return crc >> 1;
}
这是我的程序,可惜算出来不对。不知道哪里出错了

 楼主| 比神乐 发表于 2024-9-2 12:51 | 显示全部楼层
  1. uint8_t checkCRC7(uint8_t *data, uint32_t len){
  2.     uint8_t crc = 0xfe;
  3.     uint8_t crcP = 0x65;
  4.     uint8_t i, j;
  5.    
  6.     for ( i = 0; i < len; i++){
  7.         crc ^= data[i];
  8.         for (j = 0; j < 8; j++){
  9.             if ( crc &0x80){
  10.                 crc ^= 0x65;
  11.             }
  12.             crc = crc <<1;
  13.         }
  14.     }       
  15.    
  16.         crc = crc >> 1;
  17.     return crc;   
  18. }
这段代码计算第一组和第四组是对的
第二组和第三组就不对了
 楼主| 比神乐 发表于 2024-9-2 13:22 | 显示全部楼层
  1. uint32_t checkCRC71(uint8_t *data, uint32_t len){
  2.     uint32_t crc = 0x000000fe;
  3.     uint8_t crcP = 0x65;
  4.     uint8_t i, j;
  5.    
  6.     for ( i = 0; i < len; i++){
  7.         crc ^= (uint32_t)data[i];
  8.         for (j = 0; j < 32; j++){
  9.             if ( crc &0x80000000){
  10.                 crc ^= 0x65;
  11.             }
  12.             crc = crc <<1;
  13.         }
  14.     }       
  15.    
  16.         //crc = crc >> 1;
  17.     return crc;   
  18. }

评论

改成这样,只有第四组是对的  发表于 2024-9-2 13:22
Chad1989 发表于 2024-9-3 09:28 | 显示全部楼层
直接调用HAL库,管他怎么生成的
WoodData 发表于 2024-9-7 09:03 | 显示全部楼层
试下多项式0x165计算
 楼主| 比神乐 发表于 2024-9-8 18:53 | 显示全部楼层
WoodData 发表于 2024-9-7 09:03
试下多项式0x165计算

恐怕不行吧,顶多0xe5
因为x7+x6+x5+x2+1
WoodData 发表于 2024-9-9 09:24 | 显示全部楼层
比神乐 发表于 2024-9-8 18:53
恐怕不行吧,顶多0xe5
因为x7+x6+x5+x2+1

那就试试,好像多项式最高位必须为1的
 楼主| 比神乐 发表于 2024-9-9 10:33 | 显示全部楼层
还是不行
飞思啦 发表于 2024-9-9 12:01 | 显示全部楼层
刚刚用AI问了一下,居然也是无法计算
momososo 发表于 2024-9-10 09:55 | 显示全部楼层
HAL_CRC_Accumulate
HAL_CRC_Calculate
初始值不同
Dick Hou 发表于 2024-9-11 09:39 | 显示全部楼层
楼上说对了。
HAL_CRC_Accumulate 是连续计算,没有重新初始化,上次计算的结果会累积到本次。
HAL_CRC_Calculate 会重新初始化初值,和之前计算没有关系。
您需要登录后才可以回帖 登录 | 注册

本版积分规则

470

主题

3535

帖子

7

粉丝
快速回复 在线客服 返回列表 返回顶部

470

主题

3535

帖子

7

粉丝
快速回复 在线客服 返回列表 返回顶部