问答

汇集网友智慧,解决技术难题

21ic问答首页 -

CRC se ck TE UART checksum

2025-08-23
  1. /**************************************************************************//**
  2. * [url=home.php?mod=space&uid=288409]@file[/url]     main.c
  3. * [url=home.php?mod=space&uid=895143]@version[/url]  V3.00
  4. * $Revision: 2 $
  5. * $Date: 16/10/25 4:28p $
  6. * [url=home.php?mod=space&uid=247401]@brief[/url]    Implement CRC in CRC-8 mode and get the CRC checksum result.
  7. * @note
  8. * Copyright (C) 2016 Nuvoton Technology Corp. All rights reserved.
  9. ******************************************************************************/
  10. #include <stdio.h>
  11. #include "NUC029xGE.h"


  12. #define PLL_CLOCK       72000000


  13. void SYS_Init(void)
  14. {
  15.     /*---------------------------------------------------------------------------------------------------------*/
  16.     /* Init System Clock                                                                                       */
  17.     /*---------------------------------------------------------------------------------------------------------*/
  18.     /* Enable HIRC clock */
  19.     CLK_EnableXtalRC(CLK_PWRCTL_HIRCEN_Msk);

  20.     /* Waiting for HIRC clock ready */
  21.     CLK_WaitClockReady(CLK_STATUS_HIRCSTB_Msk);

  22.     /* Switch HCLK clock source to HIRC */
  23.     CLK_SetHCLK(CLK_CLKSEL0_HCLKSEL_HIRC, CLK_CLKDIV0_HCLK(1));

  24.     /* Enable HXT */
  25.     CLK_EnableXtalRC(CLK_PWRCTL_HXTEN_Msk);

  26.     /* Waiting for clock ready */
  27.     CLK_WaitClockReady(CLK_STATUS_HXTSTB_Msk);

  28.     /* Set core clock as PLL_CLOCK from PLL and SysTick source to HCLK/2*/
  29.     CLK_SetCoreClock(PLL_CLOCK);
  30.     CLK_SetSysTickClockSrc(CLK_CLKSEL0_STCLKSEL_HCLK_DIV2);

  31.     /* Enable peripheral clock */
  32.     CLK_EnableModuleClock(UART0_MODULE);
  33.     CLK_EnableModuleClock(CRC_MODULE);

  34.     /* Peripheral clock source */
  35.     CLK_SetModuleClock(UART0_MODULE, CLK_CLKSEL1_UARTSEL_PLL, CLK_CLKDIV0_UART(1));

  36.     /*---------------------------------------------------------------------------------------------------------*/
  37.     /* Init I/O Multi-function                                                                                 */
  38.     /*---------------------------------------------------------------------------------------------------------*/
  39.     /* Set multi-function pins for UART0 RXD and TXD */
  40.     SYS->GPA_MFPL &= ~(SYS_GPA_MFPL_PA2MFP_Msk | SYS_GPA_MFPL_PA3MFP_Msk);
  41.     SYS->GPA_MFPL |= (SYS_GPA_MFPL_PA3MFP_UART0_RXD | SYS_GPA_MFPL_PA2MFP_UART0_TXD);
  42. }

  43. void UART0_Init(void)
  44. {
  45.     /*---------------------------------------------------------------------------------------------------------*/
  46.     /* Init UART                                                                                               */
  47.     /*---------------------------------------------------------------------------------------------------------*/
  48.     /* Reset UART module */
  49.     SYS_ResetModule(UART0_RST);

  50.     /* Configure UART0 and set UART0 Baudrate */
  51.     UART_Open(UART0, 115200);
  52. }

  53. /*---------------------------------------------------------------------------------------------------------*/
  54. /*  MAIN function                                                                                          */
  55. /*---------------------------------------------------------------------------------------------------------*/
  56. int main(void)
  57. {
  58.     const uint8_t acCRCSrcPattern[] = {0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37, 0x38, 0x39};
  59.     uint32_t i, u32TargetChecksum = 0x58, u32CalChecksum = 0;

  60.     /* Unlock protected registers */
  61.     SYS_UnlockReg();

  62.     /* Init System, peripheral clock and multi-function I/O */
  63.     SYS_Init();

  64.     /* Lock protected registers */
  65.     SYS_LockReg();

  66.     /* Init UART0 for printf */
  67.     UART0_Init();

  68.     printf("\n\nCPU [url=home.php?mod=space&uid=72445]@[/url] %d Hz\n", SystemCoreClock);
  69.     printf("+-----------------------------------------+\n");
  70.     printf("|    CRC-8 Polynomial Mode Sample Code    |\n");
  71.     printf("+-----------------------------------------+\n\n");

  72.     printf("# Calculate [0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37, 0x38, 0x39] CRC-8 checksum value.\n");
  73.     printf("    - Seed value is 0x5A             \n");
  74.     printf("    - CPU write data length is 8-bit \n");
  75.     printf("    - Checksum complement disable    \n");
  76.     printf("    - Checksum reverse disable       \n");
  77.     printf("    - Write data complement disable  \n");
  78.     printf("    - Write data reverse disable     \n");
  79.     printf("    - Checksum should be 0x%X        \n\n", u32TargetChecksum);

  80.     /* Configure CRC controller for CRC-8 CPU mode */
  81.     CRC_Open(CRC_8, 0, 0x5A, CRC_CPU_WDATA_8);

  82.     /* Start to execute CRC-8 CPU operation */
  83.     for(i = 0; i < sizeof(acCRCSrcPattern); i++)
  84.     {
  85.         CRC_WRITE_DATA((acCRCSrcPattern[i] & 0xFF));
  86.     }

  87.     /* Get CRC-8 checksum value */
  88.     u32CalChecksum = CRC_GetChecksum();
  89.     printf("CRC checksum is 0x%X ... %s.\n", u32CalChecksum, (u32CalChecksum == u32TargetChecksum) ? "PASS" : "FAIL");

  90.     /* Disable CRC function */
  91.     CLK_DisableModuleClock(CRC_MODULE);

  92.     while(1);
  93. }

  94. /*** (C) COPYRIGHT 2016 Nuvoton Technology Corp. ***/


回答 +关注
人浏览 人回答问题 分享 举报
个回答

您需要登录后才可以回复 登录 | 注册