[STM32L5] STM32L4R5板子CRC程序的问题

[复制链接]
 楼主| 比神乐 发表于 2023-6-2 22:08 | 显示全部楼层 |阅读模式
代码:
  1. #include "main.h"

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

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

  8. /* Private typedef -----------------------------------------------------------*/
  9. /* Private define ------------------------------------------------------------*/
  10. /* aDataBuffer is 32 bit long*/
  11. #define BUFFER_SIZE 1

  12. /* The user defined polynomial*/
  13. #define CRC_POLYNOMIAL_8B 0x9B /* X^8 + X^7 + X^4 + X^3 + X + 1 */

  14. /* Private macro -------------------------------------------------------------*/
  15. /* Private variables ---------------------------------------------------------*/
  16. /* CRC handler declaration */
  17. CRC_HandleTypeDef   CrcHandle;

  18. /* Used for storing CRC Value */
  19. __IO uint32_t uwCRCValue = 0;

  20. /* Buffer containing the data on which the CRC will be calculated
  21.   (one-word buffer in this example) */
  22. static const uint32_t aDataBuffer = 0x1234;

  23. /* Expected CRC Value */
  24. uint32_t uwExpectedCRCValue = 0xEF;

  25. /* Private function prototypes -----------------------------------------------*/
  26. void SystemClock_Config(void);
  27. static void Error_Handler(void);

  28. /* Private functions ---------------------------------------------------------*/

  29. /**
  30.   * [url=home.php?mod=space&uid=247401]@brief[/url]  Main program
  31.   * @param  None
  32.   * @retval None
  33.   */
  34. int main(void)
  35. {
  36.   
  37.   /* STM32L4xx HAL library initialization:
  38.        - Configure the Flash prefetch
  39.        - Systick timer is configured by default as source of time base, but user
  40.          can eventually implement his proper time base source (a general purpose
  41.          timer for example or other time source), keeping in mind that Time base
  42.          duration should be kept 1ms since PPP_TIMEOUT_VALUEs are defined and
  43.          handled in milliseconds basis.
  44.        - Set NVIC Group Priority to 4
  45.        - Low Level Initialization
  46.      */
  47.   HAL_Init();
  48.   
  49.   /* Configure the system clock to 120 MHz */
  50.   SystemClock_Config();

  51.   /* Configure LED1 and LED3 */
  52.   BSP_LED_Init(LED1);
  53.   BSP_LED_Init(LED3);

  54.   /*##-1- Configure the CRC peripheral #######################################*/
  55.   CrcHandle.Instance = CRC;

  56.   /* The default polynomial is not used. It is required to defined it in CrcHandle.Init.GeneratingPolynomial*/  
  57.   CrcHandle.Init.DefaultPolynomialUse    = DEFAULT_POLYNOMIAL_DISABLE;
  58.   
  59.   /* Set the value of the polynomial */
  60.   CrcHandle.Init.GeneratingPolynomial    = CRC_POLYNOMIAL_8B;
  61.   
  62.   /* The user-defined generating polynomial generates a
  63.      8-bit long CRC */
  64.   CrcHandle.Init.CRCLength               = CRC_POLYLENGTH_8B;

  65.   /* The default init value is used */
  66.   CrcHandle.Init.DefaultInitValueUse     = DEFAULT_INIT_VALUE_ENABLE;

  67.   /* The input data are not inverted */
  68.   CrcHandle.Init.InputDataInversionMode  = CRC_INPUTDATA_INVERSION_NONE;

  69.   /* The output data are not inverted */
  70.   CrcHandle.Init.OutputDataInversionMode = CRC_OUTPUTDATA_INVERSION_DISABLE;

  71.   /* The input data are 32-bit long */
  72.   CrcHandle.InputDataFormat              = CRC_INPUTDATA_FORMAT_WORDS;

  73.   if (HAL_CRC_Init(&CrcHandle) != HAL_OK)
  74.   {
  75.     /* Initialization Error */
  76.     Error_Handler();
  77.   }

  78.   /*##-2- Compute the CRC of "aDataBuffer" ###################################*/
  79.   uwCRCValue = HAL_CRC_Calculate(&CrcHandle, (uint32_t *)&aDataBuffer, BUFFER_SIZE);

  80.   /*##-3- Compare the CRC value to the Expected one ##########################*/
  81.   if (uwCRCValue != uwExpectedCRCValue)
  82.   {
  83.     /* Wrong CRC value: Turn LED3 on */
  84.     Error_Handler();
  85.   }
  86.   else
  87.   {
  88.     /* Right CRC value: Turn LED1 on */
  89.     BSP_LED_On(LED1);
  90.   }

  91.   /* Infinite loop */
  92.   while (1)
  93.   {
  94.   }
  95. }
我想知道怎么算出来的。于是我写了一个C程序验证
  1. #include "stdio.h"

  2. #define uint32_t unsigned  int
  3. #define uint16_t unsigned short
  4. #define uint8_t  unsigned char

  5. #define BUFFER_SIZE    1

  6. uint32_t  temp;

  7. static const uint32_t aDataBuffer = 0x1234;

  8. /******************************************************************************
  9. * Name:    CRC-32/MPEG-2  x32+x26+x23+x22+x16+x12+x11+x10+x8+x7+x5+x4+x2+x+1
  10. * Poly:    0x4C11DB7
  11. * Init:    0xFFFFFFF
  12. * Refin:   False
  13. * Refout:  False
  14. * Xorout:  0x0000000
  15. *****************************************************************************/
  16. uint32_t crc32_mpeg_2(uint32_t *data, uint16_t length)
  17. {
  18.     uint8_t i;
  19.     uint32_t crc = 0xffffffff;  // Initial value
  20.     while(length--)
  21.     {
  22.         crc ^= (*data++) << 0;
  23.         for (i = 0; i < 32; ++i)
  24.         {
  25.             if ( crc & 0x80000000 )
  26.                 crc = (crc << 1) ^ 0x0000009b;
  27.             else
  28.                 crc <<= 1;
  29.         }
  30.     }
  31.     return crc;
  32. }


  33. void main(void)
  34. {
  35.         temp=crc32_mpeg_2(&aDataBuffer, BUFFER_SIZE);
  36.         printf("crc32 value=:%x\r\n",temp);
  37. }
结果得到结果:
9.jpg
得到的值是0xfff75e7f
不是0xef.
请高手指教,谢谢!
Stahan 发表于 2023-6-6 18:06 | 显示全部楼层
写的程序有问题吧
 楼主| 比神乐 发表于 2024-9-20 16:36 | 显示全部楼层
是程序有问题
现在搞好了
  1. #include <stdio.h>  
  2. #include <stdint.h>  
  3.   
  4. // CRC-8 计算函数,使用多项式0x9B  
  5. uint8_t crc8_0x9b(uint8_t *data, size_t len) {  
  6.     uint8_t crc = 0xFF; // 初始CRC值  
  7.     while (len--) {  
  8.         crc ^= *data++; // 将当前字节的数据与CRC寄存器进行异或  
  9.         for (int i = 0; i < 8; i++) {  
  10.             if (crc & 0x80) { // 如果最高位是1  
  11.                 crc = (crc << 1) ^ 0x9B; // 左移一位,并与多项式进行异或  
  12.             } else {  
  13.                 crc = crc << 1; // 左移一位  
  14.             }  
  15.         }  
  16.     }  
  17.     return crc;  
  18. }  
  19.   
  20. int main() {  
  21.     uint8_t data[4] = {0,0,0x12,0X34};  
  22.     //uint8_t *data_bytes = (uint8_t *)&data; // 将16位数据转换为字节数组  
  23.   
  24.     // 计算CRC,因为data是16位,但CRC是针对8位(字节)计算的,  
  25.     // 这里我们假设只计算低8位(即0x34)的CRC,因为高位(0x12)对CRC的贡献取决于具体应用场景  
  26.     // 如果你需要同时处理两个字节,你可能需要调整循环或处理逻辑  
  27.     uint8_t crc = crc8_0x9b(data, 4); // 只计算低8位(即0x34)的CRC  
  28.   
  29.     printf("CRC-8 (0x9B) for 0x%02X is 0x%02X\n", data, crc);  
  30.   
  31.     // 注意:如果你的预期结果是0xEF,并且这与上面的代码不匹配,  
  32.     // 那么可能需要调整初始值、处理顺序、多项式或考虑同时处理两个字节的数据。  
  33.     // 但根据常见的CRC-8实现,上面的代码应该为0x34计算出一个合理的CRC值。  
  34.   
  35.     return 0;  
  36. }
运行
2.png
您需要登录后才可以回帖 登录 | 注册

本版积分规则

470

主题

3535

帖子

7

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