[STM32L4] CRC校验的问题

[复制链接]
1201|0
 楼主| 比神乐 发表于 2023-5-11 12:43 | 显示全部楼层 |阅读模式
我找到一个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=247401]@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计算器,算的值都不对。
请问高手,谁知道是怎么计算的?谢谢!
您需要登录后才可以回帖 登录 | 注册

本版积分规则

470

主题

3537

帖子

7

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