OpenEdv-开源电子网

 找回密码
 立即注册
正点原子全套STM32/Linux/FPGA开发资料,上千讲STM32视频教程免费下载...
查看: 1005|回复: 0

STM32L4R5板子CRC程序的问题

[复制链接]

72

主题

179

帖子

0

精华

高级会员

Rank: 4

积分
561
金钱
561
注册时间
2014-10-15
在线时间
132 小时
发表于 2023-6-2 22:27:57 | 显示全部楼层 |阅读模式
1金钱
代码:
  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.   * @brief  Main program
  31.   * [url=home.php?mod=space&uid=271674]@param[/url]  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.

请高手指教,谢谢!

正点原子逻辑分析仪DL16劲爆上市
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则



关闭

原子哥极力推荐上一条 /2 下一条

正点原子公众号

QQ|手机版|OpenEdv-开源电子网 ( 粤ICP备12000418号-1 )

GMT+8, 2024-11-24 06:29

Powered by OpenEdv-开源电子网

© 2001-2030 OpenEdv-开源电子网

快速回复 返回顶部 返回列表