1552 views|13 replies

3180

Posts

0

Resources
The OP
 

Problems with CRC program on STM32L4R5 board [Copy link]

 

Code:

#include "main.h"

/** @addtogroup STM32L4xx_HAL_Examples
  * @{
  */

/** @addtogroup CRC_UserDefinedPolynomial
  * @{
  */

/* Private typedef -----------------------------------------------------------*/
/* Private define ------------------------------------------------------------*/
/* aDataBuffer is 32 bit long*/
#define BUFFER_SIZE 1

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

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

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

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

/* Expected CRC Value */
uint32_t uwExpectedCRCValue = 0xEF;

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

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

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

  /* 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 32-bit long */
  CrcHandle.InputDataFormat              = CRC_INPUTDATA_FORMAT_WORDS;

  if (HAL_CRC_Init(&CrcHandle) != HAL_OK)
  {
    /* Initialization Error */
    Error_Handler();
  }

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

  /*##-3- Compare the CRC value to the Expected one ##########################*/
  if (uwCRCValue != uwExpectedCRCValue)
  {
    /* Wrong CRC value: Turn LED3 on */
    Error_Handler();
  }
  else
  {
    /* Right CRC value: Turn LED1 on */
    BSP_LED_On(LED1);
  }

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

I want to know how it is calculated. So I wrote a C program to verify

#include "stdio.h"

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

#define BUFFER_SIZE    1

uint32_t  temp;

static const uint32_t aDataBuffer = 0x1234;

/******************************************************************************
 * Name:    CRC-32/MPEG-2  x32+x26+x23+x22+x16+x12+x11+x10+x8+x7+x5+x4+x2+x+1
 * Poly:    0x4C11DB7
 * Init:    0xFFFFFFF
 * Refin:   False
 * Refout:  False
 * Xorout:  0x0000000
 *****************************************************************************/
uint32_t crc32_mpeg_2(uint32_t *data, uint16_t length)
{
    uint8_t i;
    uint32_t crc = 0xffffffff;  // Initial value
    while(length--)
    {
        crc ^= (*data++) << 0;
        for (i = 0; i < 32; ++i)
        {
            if ( crc & 0x80000000 )
                crc = (crc << 1) ^ 0x0000009b;
            else
                crc <<= 1;
        }
    }
    return crc;
}


void main(void)
{
	temp=crc32_mpeg_2(&aDataBuffer, BUFFER_SIZE);
	printf("crc32 value=:%x\r\n",temp);
} 

The result is:

The value obtained is 0xfff75e7f

Not 0xef.

Please give me some advice, thank you!

This post is from stm32/stm8

Latest reply

The results of the lowest 8 bits of the 32-bit check are the same as those of the 8-bit check. I remember that the value of the polynomial of stm32 cannot be changed, so it cannot be suitable for all algorithms, and I didn't study it later.   Details Published on 2023-6-5 09:11
Personal signature为江山踏坏了乌骓马,为社稷拉断了宝雕弓。
 

5998

Posts

6

Resources
2
 

There is something wrong with the program I wrote. Shouldn't CRC have an initial value and a flag? You can look it up online.

This post is from stm32/stm8

Comments

The initial value I wrote is 0xffffffff, but the initial value is not clearly stated in STM32. It is not easy to find it online.  Details Published on 2023-6-3 09:37
 
Personal signature

在爱好的道路上不断前进,在生活的迷雾中播撒光引

 

2865

Posts

4

Resources
3
 

Don't calculate it yourself. There are special websites that can calculate various CRCs. You need to pay attention to the initialization of CRC. You must initialize the original register. It seems that the default is 0xFFFF, or it may be 0x0. There is also the CRC check formula, and the required calculation and number of bits. As long as these three are the same, it will not be wrong. I have tested this.

This post is from stm32/stm8

Comments

I am not satisfied with your answer. For example, if I use a single-chip microcomputer to communicate with a host computer, and the single-chip microcomputer uses hardware CRC, you can't use a special CRC calculator to calculate it on the host computer. You can only find a suitable algorithm yourself. Otherwise, the hardware CRC of the single-chip microcomputer is meaningless.  Details Published on 2023-6-3 09:39
 
 

3180

Posts

0

Resources
4
 
Qintianqintian0303 posted on 2023-6-2 22:45 There is something wrong with the program I wrote. Shouldn't CRC have an initial value and a flag? You can look it up online

The initial value I wrote is 0xffffffff, but the initial value is not clearly stated in STM32. It is not easy to find it online.

This post is from stm32/stm8
 
Personal signature为江山踏坏了乌骓马,为社稷拉断了宝雕弓。
 
 

3180

Posts

0

Resources
5
 
bigbat posted on 2023-6-3 09:19 Don't calculate it yourself, there are special websites that can calculate various CRCs. You need to pay attention to the initialization of CRC. You must initialize the original register. It seems that the default...

I am not satisfied with your answer. For example, if I use a single-chip microcomputer to communicate with a host computer, and the single-chip microcomputer uses hardware CRC, you can't use a special CRC calculator to calculate it on the host computer. You can only find a suitable algorithm yourself. Otherwise, the hardware CRC of the single-chip microcomputer is meaningless.

This post is from stm32/stm8
 
Personal signature为江山踏坏了乌骓马,为社稷拉断了宝雕弓。
 
 

6062

Posts

4

Resources
6
 

There are CRC32, CRC16, CRC8

CrcHandle.Init.GeneratingPolynomial = CRC_POLYNOMIAL_8B;

/* The user-defined generating polynomial generates a 8-bit long CRC */

CrcHandle.Init.CRCLength = CRC_POLYLENGTH_8B;

CRC_POLYNOMIAL_32B

CRC_POLYLENGTH_32B

This post is from stm32/stm8
 
 
 

4005

Posts

0

Resources
7
 

The number of bits you have is not correct. The hardware is 8 bits and the software is 32 bits. 32 bits can replace 8 bits, but each byte must be checked.

In addition to the number of bits, the shift direction, initial value and XOR value will affect the result.

All the same, that's all

This post is from stm32/stm8

Comments

Thank you! [attachimg]702896[/attachimg] MCU program, the polynomial is 8 bits, but the data input format is 32 bits, the default initial value. [attachimg]702900[/attachimg] Some of the factors you mentioned are indeed unclear, and the program does not mention them. I don't know what STM32 does.  Details Published on 2023-6-5 09:03
 
 
 

4005

Posts

0

Resources
8
 

A calculator can help you confirm the algorithm

This post is from stm32/stm8

Comments

Thank you! There is no such calculator. This polynomial is custom made.  Details Published on 2023-6-5 09:08
 
 
 

3180

Posts

0

Resources
9
 
huo_hu posted on 2023-6-5 08:48 The number of bits you have doesn't match. The hardware is 8 bits and the software is 32 bits. 32 bits can replace 8 bits, but each byte must be checked in addition to the number of bits and the shift direction...

Thanks!

In the microcontroller program, the polynomial is 8 bits, but the data input format is 32 bits, with the default initial value.

Some of the factors you mentioned are indeed unclear and are not mentioned in the program.

I don't know what the STM32 is doing. That's why I posted this thread to see if anyone knows.

This post is from stm32/stm8
 
Personal signature为江山踏坏了乌骓马,为社稷拉断了宝雕弓。
 
 

3180

Posts

0

Resources
10
 
huo_hu posted on 2023-6-5 08:54 The calculator can help you confirm the algorithm

Thank you! There is no such calculator. This polynomial is custom made.

This post is from stm32/stm8
 
Personal signature为江山踏坏了乌骓马,为社稷拉断了宝雕弓。
 
 

4005

Posts

0

Resources
11
 

The results of the lowest 8 bits of the 32-bit check are the same as those of the 8-bit check.

I remember that the value of the polynomial of stm32 cannot be changed, so it cannot be suitable for all algorithms, and I didn't study it later.

This post is from stm32/stm8

Comments

Regarding the 7-bit CRC check, I found a table lookup program on the Internet, modified it, and found that two of the four sets of data were correct, the first and fourth, 0x57, 0x26. The second and third were incorrect: the MCU program was 0x6e, 0x4b, and I calculated it to be 0x26, 0x2f. Code:  Details Published on 2023-6-5 12:44
 
 
 

3180

Posts

0

Resources
12
 
This post was last edited by chenbingjy on 2023-6-5 10:49
huo_hu posted on 2023-6-5 09:11 The results of the lowest 8 bits of the 32-bit check are the same as those of the 8-bit check. I remember that the value of the polynomial of stm32 cannot be changed, so it cannot be suitable for all...

Thank you! I found a CRC calculator on the Internet that can customize the 8-bit polynomial and can calculate the correct result.

Then, I wrote the software calculation program in C language.

But there is also a 7-bit CRC calculation program:

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

/**
  * @brief  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)
  {
  }
}

The polynomial is 7 bits long, and I don't know the initial value, nor do I know whether to calculate only 7 bits or 8 bits. If only 7 bits are calculated, should I calculate the high 7 bits or the low 7 bits?

I wrote a C program, but the calculation results are wrong

uint8_t crc7_mpeg_2(uint8_t *data, uint16_t length)
{
    uint8_t i;
    uint32_t crc = 0x7f;  // Initial value
    while(length--)
    {
        crc ^= (*data++) << 0;
        for (i = 0; i < 8; ++i)
        {
            if ( crc & 0x80 )
                crc = (crc << 1) ^ 0x65;
            else
                crc <<= 1;
        }
    }
    return (crc^0x00);
}

This post is from stm32/stm8
 
Personal signature为江山踏坏了乌骓马,为社稷拉断了宝雕弓。
 
 

3180

Posts

0

Resources
13
 
huo_hu posted on 2023-6-5 09:11 The result of the lowest 8 bits of the 32-bit checksum is the same as the 8-bit checksum. I remember that the value of the polynomial of stm32 cannot be changed, so it cannot be suitable for all...

Regarding the 7-bit CRC check, I found a table lookup program on the Internet and modified it a bit.

Four sets of data were found, two of which were correct, the first and fourth, 0x57, 0x26.

The second and third ones are wrong:

The MCU program is 0x6e, 0x4b, and I calculated it to be 0x26, 0x2f.

Code:

# 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;
}

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(0, testdat, 10, data);
    //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;
}

This post is from stm32/stm8
 
Personal signature为江山踏坏了乌骓马,为社稷拉断了宝雕弓。
 
 

3180

Posts

0

Resources
14
 

I developed a program myself, the code is as follows:

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;
}

The first and fourth sets of data are correct, but the second and third sets are not correct, which is the same as the result of the previous calculation.

This post is from stm32/stm8
 
Personal signature为江山踏坏了乌骓马,为社稷拉断了宝雕弓。
 
 

Guess Your Favourite
Just looking around
Find a datasheet?

EEWorld Datasheet Technical Support

Copyright © 2005-2024 EEWORLD.com.cn, Inc. All rights reserved 京B2-20211791 京ICP备10001474号-1 电信业务审批[2006]字第258号函 京公网安备 11010802033920号
快速回复 返回顶部 Return list