打印
[STM32L4]

再问STM32L4R5板子CRC程序的问题

[复制链接]
141|7
手机看帖
扫描二维码
随时随地手机跟帖
跳转到指定楼层
楼主
比神乐|  楼主 | 2023-6-5 16:14 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
我有一个STM32L4R5板子,有四个CRC例程,我搞好了三个。
还有一个CRC7。
代码如下:
#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] */
#define CRC_POLYNOMIAL_7B  0x65  /* X^7 + X^6 + X^5 + X^2 + 1,*/
/* 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=247401]@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)
  {
  }
}
我找到一个例子,代码如下:
# include <stdio.h>
# include <string.h>

#define uint8_t unsigned char
#define uint16_t unsigned int
#define uint32_t unsigned  long int

#define TAB_LEN 256
#define ALPHA 0x65

#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   */

const uint8_t CRC7_DATA8_TEST5[5]   = {0x12,0x34,0xBA,0x71,0xAD};
const uint8_t CRC7_DATA8_TEST17[17] = {0x12,0x34,0xBA,0x71,0xAD,
                                              0x11,0x56,0xDC,0x88,0x1B,
                                              0xEE,0x4D,0x82, 0x93,0xA6,
                                              0x7F,0xC3};
const uint8_t CRC7_DATA8_TEST1[1]   = {0x19};                                                
const uint8_t CRC7_DATA8_TEST2[2]   = {0xAB,0xCD};
const unsigned char testdat[10] = "0123456789";
      

/* 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 */


unsigned char result;

int table_gen8(unsigned char *buf){
    unsigned int alpha = ALPHA;  //x^7+x^3+1
    int i,j;
    unsigned char tmp;
    for(i=0;i<TAB_LEN;i++){
        tmp = i;
        for(j=0;j<8;j++){
            if(tmp & 0x80)         
                tmp ^= alpha;
            tmp <<= 1;
        }      
        buf[i] = tmp>>1;    /*余数为7bit,计算中用了8bit,结尾多一位0要去掉*/
    }   
    return 0;
}

uint8_t checkCRC7(uint8_t *data, uint32_t len){
    uint8_t crc = 0x7F;
    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;   
}

unsigned char get_crc7(unsigned char start, const unsigned char *buff, int len, unsigned char *table){
    unsigned char accu = start;
    //unsigned char accu = (start<<1);
    unsigned int i= 0;
    for (i=0;  i < len; i++) {
        accu = table[(accu << 1) ^ buff[i]];
        //accu = table[accu^ buff[i]];
    }
    return (accu);
}



int main(void){
    unsigned char data[TAB_LEN] = {0};
    int i,j;
    printf("CRC7 table:\n");
    table_gen8(data);
   /* for(i=0;i<TAB_LEN/8;i++){
        for(j=0;j<8;j++)
            printf("0x%02x   ",data[i*8+j]);
        printf("\n");
    }
    printf("\n");*/
    /*Test*/
   
    result = get_crc7(0x7f, CRC7_DATA8_TEST5, 5, data);
    //result = get_crc7(0x7f, CRC7_DATA8_TEST17, 17, data);
    //result = get_crc7(0x7f, CRC7_DATA8_TEST1, 1, data);
    //result = get_crc7(0x7f, CRC7_DATA8_TEST2, 2, data);
    printf("get_crc7:0x%02x\n",result);
    return 0;
}
一共四组数据,第一组和第四组值是对的。而第二组和第三组值不对。
本来应该第二组是0x5E,第三组是0x4b.实测值为0x26和0x2f。
请问高手,谁知道软件如何计算出正确的值?或者我写的这个程序哪里有问题?谢谢!

使用特权

评论回复
沙发
Stahan| | 2023-6-6 17:03 | 只看该作者
CRC是不是应该有初始值和标志啊

使用特权

评论回复
板凳
比神乐|  楼主 | 2023-6-7 09:47 | 只看该作者
我的初始值0x7f,标志是什么意思?

使用特权

评论回复
地板
比神乐|  楼主 | 2023-6-17 11:45 | 只看该作者
我自己写了一个程序,代码如下:
uint8_t crc7_mmc(uint8_t *data, uint16_t length)
{
    uint8_t i;
    uint8_t crc = 0xFE;     
    while(length--)
    {
        crc ^= *data++;        
        for ( i = 0; i < 8; i++ )
        {
            if ( crc & 0x80 )
                //crc = (crc << 1) ^ 0x65;   
                                crc = (crc << 1) ^ 0xCA;      
            else
                crc <<= 1;
        }
    }
    return crc >> 1;
}
用这个程序计算也是第一组和第四组数据对,是第二组和第三组数据不对。
我真的无语了。

使用特权

评论回复
5
LLGTR| | 2023-6-17 13:17 | 只看该作者
比神乐 发表于 2023-6-17 11:45
我自己写了一个程序,代码如下:
用这个程序计算也是第一组和第四组数据对,是第二组和第三组数据不对。
我 ...

CRC算法应该是看不到内部是什么样的吧。

使用特权

评论回复
6
比神乐|  楼主 | 2023-6-18 11:43 | 只看该作者
LLGTR 发表于 2023-6-17 13:17
CRC算法应该是看不到内部是什么样的吧。

是看不到的。如果找不到算法,那单片机的这个功能基本上就是摆设了。

使用特权

评论回复
7
LLGTR| | 2023-7-7 16:18 | 只看该作者
比神乐 发表于 2023-6-18 11:43
是看不到的。如果找不到算法,那单片机的这个功能基本上就是摆设了。

那只能两个同样的单片机才能用这个CRC了吧

使用特权

评论回复
8
比神乐|  楼主 | 2023-7-8 08:58 | 只看该作者
本帖最后由 比神乐 于 2023-7-8 09:00 编辑
LLGTR 发表于 2023-7-7 16:18
那只能两个同样的单片机才能用这个CRC了吧

我还是想找到这个算法。如果单片机和上位机通信呢?

使用特权

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

本版积分规则

337

主题

3061

帖子

7

粉丝