打印
[STM32]

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

[复制链接]
693|12
手机看帖
扫描二维码
随时随地手机跟帖
跳转到指定楼层
楼主
比神乐|  楼主 | 2024-9-2 11:46 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式

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


代码如下

#include "main.h"

/** @addtogroup STM32L4xx_HAL_Examples
  * @{
  */

/** @addtogroup CRC_Bytes_Stream_7bit_CRC
  * @{
  */

/* Private typedef -----------------------------------------------------------*/
/* Private define ------------------------------------------------------------*/
#define BUFFER_SIZE_5  5  /* CRC7_DATA8_TEST5[] is 5-byte long   */
#define BUFFER_SIZE_17 17 /* CRC7_DATA8_TEST17[] is 17-byte long */
#define BUFFER_SIZE_1  1  /* CRC7_DATA8_TEST1[] is 1-byte long   */
#define BUFFER_SIZE_2  2  /* CRC7_DATA8_TEST2[] is 2-byte long   */

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

/* Private macro -------------------------------------------------------------*/
/* Private variables ---------------------------------------------------------*/
/* CRC handler declaration */
CRC_HandleTypeDef   CrcHandle;

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

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

      

/* Expected CRC Values */
/* The 7 LSB bits are the 7-bit long 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 */

/* Private function prototypes -----------------------------------------------*/
void SystemClock_Config(void);
static void Error_Handler(void);

/* Private functions ---------------------------------------------------------*/

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

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

  /* Configure LED1 and LED3 */
  BSP_LED_Init(LED1);
  BSP_LED_Init(LED3);


  /****************************************************************************/
  /*                                                                          */
  /*                     CRC peripheral initialization                        */
  /*                                                                          */   
  /****************************************************************************/
   
  CrcHandle.Instance = CRC;

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

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

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

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

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

  /* De-initialize the CRC peripheral */
  if (HAL_CRC_DeInit(&CrcHandle) != HAL_OK)
  {
    /* Initialization Error */
    Error_Handler();
  }  

  /* Then, initialize the CRC handle */
  if (HAL_CRC_Init(&CrcHandle) != HAL_OK)
  {
    /* Initialization Error */
    Error_Handler();
  }


  /****************************************************************************/
  /*                                                                          */
  /*         CRC computation of a first bytes stream                          */
  /*                                                                          */   
  /****************************************************************************/

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

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

  /* Compare the CRC value to the expected one */
  if (uwCRCValue != uwExpectedCRCValue_1)
  {
    /* Wrong CRC value: Turn LED3 on */
    Error_Handler();
  }

  
  /****************************************************************************/
  /*                                                                          */
  /*         CRC computation of a second bytes stream                         */
  /*                                                                          */   
  /****************************************************************************/

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

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

  /* Compare the CRC value to the expected one */
  if (uwCRCValue != uwExpectedCRCValue_2)
  {
    /* Wrong CRC value: Turn LED3 on */
    Error_Handler();
  }


  /****************************************************************************/
  /*                                                                          */
  /*         CRC computation of a single byte                                 */
  /*                                                                          */   
  /****************************************************************************/

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

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

  /* Compare the CRC value to the expected one */
  if (uwCRCValue != uwExpectedCRCValue_3)
  {
    /* Wrong CRC value: Turn LED3 on */
    Error_Handler();
  }


  /****************************************************************************/
  /*                                                                          */
  /*         CRC computation of the last bytes stream                         */
  /*                                                                          */   
  /****************************************************************************/

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

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

  /* Compare the CRC value to the expected one */
  if (uwCRCValue != uwExpectedCRCValue_4)
  {
    /* Wrong CRC value: Turn LED3 on */
    Error_Handler();
  }
  else
  {
    /* Right CRC value: Turn LED1 on */
    BSP_LED_On(LED1);
  }  


  /* Infinite loop */
  while (1)
  {
  }
}

多项式是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 | 只看该作者
uint8_t checkCRC7(uint8_t *data, uint32_t len){
    uint8_t crc = 0xfe;
    uint8_t crcP = 0x65;
    uint8_t i, j;
   
    for ( i = 0; i < len; i++){
        crc ^= data[i];
        for (j = 0; j < 8; j++){
            if ( crc &0x80){
                crc ^= 0x65;
            }
            crc = crc <<1;
        }
    }       
   
        crc = crc >> 1;
    return crc;   
}
这段代码计算第一组和第四组是对的
第二组和第三组就不对了

使用特权

评论回复
地板
比神乐|  楼主 | 2024-9-2 13:22 | 只看该作者
uint32_t checkCRC71(uint8_t *data, uint32_t len){
    uint32_t crc = 0x000000fe;
    uint8_t crcP = 0x65;
    uint8_t i, j;
   
    for ( i = 0; i < len; i++){
        crc ^= (uint32_t)data[i];
        for (j = 0; j < 32; j++){
            if ( crc &0x80000000){
                crc ^= 0x65;
            }
            crc = crc <<1;
        }
    }       
   
        //crc = crc >> 1;
    return crc;   
}

使用特权

评论回复
评论
比神乐 2024-9-2 13:22 回复TA
改成这样,只有第四组是对的 
5
Chad1989| | 2024-9-3 09:28 | 只看该作者
直接调用HAL库,管他怎么生成的

使用特权

评论回复
6
WoodData| | 2024-9-7 09:03 | 只看该作者
试下多项式0x165计算

使用特权

评论回复
7
比神乐|  楼主 | 2024-9-8 18:53 | 只看该作者
WoodData 发表于 2024-9-7 09:03
试下多项式0x165计算

恐怕不行吧,顶多0xe5
因为x7+x6+x5+x2+1

使用特权

评论回复
8
WoodData| | 2024-9-9 09:24 | 只看该作者
比神乐 发表于 2024-9-8 18:53
恐怕不行吧,顶多0xe5
因为x7+x6+x5+x2+1

那就试试,好像多项式最高位必须为1的

使用特权

评论回复
9
比神乐|  楼主 | 2024-9-9 10:33 | 只看该作者
还是不行

使用特权

评论回复
10
飞思啦| | 2024-9-9 12:01 | 只看该作者
刚刚用AI问了一下,居然也是无法计算

使用特权

评论回复
11
momososo| | 2024-9-10 09:55 | 只看该作者
HAL_CRC_Accumulate
HAL_CRC_Calculate
初始值不同

使用特权

评论回复
12
Dick Hou| | 2024-9-11 09:39 | 只看该作者
楼上说对了。
HAL_CRC_Accumulate 是连续计算,没有重新初始化,上次计算的结果会累积到本次。
HAL_CRC_Calculate 会重新初始化初值,和之前计算没有关系。

使用特权

评论回复
发新帖 我要提问
您需要登录后才可以回帖 登录 | 注册

本版积分规则

380

主题

3212

帖子

7

粉丝