/**************************************************************************//**
* [url=home.php?mod=space&uid=288409]@file[/url] main.c
* [url=home.php?mod=space&uid=895143]@version[/url] V3.00
* $Revision: 2 $
* $Date: 20/05/28 4:40p $
* [url=home.php?mod=space&uid=247401]@brief[/url] Implement CRC in CRC-CCITT mode and get the CRC checksum result.
*
* SPDX-License-Identifier: Apache-2.0
* Copyright (C) 2020 Nuvoton Technology Corp. All rights reserved.
******************************************************************************/
#include <stdio.h>
#include "NuMicro.h"
void SYS_Init(void)
{
/* Unlock protected registers */
SYS_UnlockReg();
/* Enable HIRC clock */
CLK_EnableXtalRC(CLK_PWRCTL_HIRCEN_Msk);
/* Waiting for HIRC clock ready */
CLK_WaitClockReady(CLK_STATUS_HIRCSTB_Msk);
/* Switch HCLK clock source to HIRC and HCLK source divide 1 */
CLK_SetHCLK(CLK_CLKSEL0_HCLKSEL_HIRC, CLK_CLKDIV0_HCLK(1));
/* Enable UART0 clock */
CLK_EnableModuleClock(UART0_MODULE);
/* Switch UART0 clock source to HIRC */
CLK_SetModuleClock(UART0_MODULE, CLK_CLKSEL1_UART0SEL_HIRC, CLK_CLKDIV0_UART0(1));
/* Enable CRC clock */
CLK_EnableModuleClock(CRC_MODULE);
/* Update System Core Clock */
SystemCoreClockUpdate();
/* Set PB multi-function pins for UART0 RXD=PB.12 and TXD=PB.13 */
SYS->GPB_MFPH = (SYS->GPB_MFPH & ~(SYS_GPB_MFPH_PB12MFP_Msk | SYS_GPB_MFPH_PB13MFP_Msk))
|(SYS_GPB_MFPH_PB12MFP_UART0_RXD | SYS_GPB_MFPH_PB13MFP_UART0_TXD);
/* Lock protected registers */
SYS_LockReg();
}
void UART0_Init(void)
{
/*---------------------------------------------------------------------------------------------------------*/
/* Init UART */
/*---------------------------------------------------------------------------------------------------------*/
/* Reset UART module */
SYS_ResetModule(UART0_RST);
/* Configure UART0 and set UART0 Baudrate */
UART_Open(UART0, 115200);
}
/*---------------------------------------------------------------------------------------------------------*/
/* MAIN function */
/*---------------------------------------------------------------------------------------------------------*/
int main(void)
{
const uint16_t u16CRCSrcPattern[] = {0x3231, 0x3433, 0x3635, 0x3837};
uint32_t i, u32TargetChecksum = 0xA12B, u32CalChecksum = 0;
/* Unlock protected registers */
SYS_UnlockReg();
/* Init System, peripheral clock and multi-function I/O */
SYS_Init();
/* Lock protected registers */
SYS_LockReg();
/* Init UART0 for printf */
UART0_Init();
printf("\n\nCPU [url=home.php?mod=space&uid=72445]@[/url] %d Hz\n", SystemCoreClock);
printf("+---------------------------------------------+\n");
printf("| CRC-CCITT Polynomial Mode Sample Code |\n");
printf("+---------------------------------------------+\n\n");
printf("# Calculate [0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37, 0x38] CRC-CCITT checksum value.\n");
printf(" - Seed value is 0xFFFF \n");
printf(" - CPU write data length is 16-bit \n");
printf(" - Checksum complement disable \n");
printf(" - Checksum reverse disable \n");
printf(" - Write data complement disable \n");
printf(" - Write data reverse disable \n");
printf(" - Checksum should be 0x%X \n\n", u32TargetChecksum);
/* Configure CRC controller for CRC-CCITT CPU mode */
CRC_Open(CRC_CCITT, 0, 0xFFFF, CRC_WDATA_16);
/* Start to execute CRC-CCITT operation */
for(i = 0; i < sizeof(u16CRCSrcPattern) / sizeof(u16CRCSrcPattern[0]); i++)
{
CRC_WRITE_DATA((u16CRCSrcPattern[i]));
}
/* Get CRC-CCITT checksum value */
u32CalChecksum = CRC_GetChecksum();
printf("CRC checksum is 0x%X ... %s.\n", u32CalChecksum, (u32CalChecksum == u32TargetChecksum) ? "PASS" : "FAIL");
/* Disable CRC function */
CLK_DisableModuleClock(CRC_MODULE);
while(1);
}
/*** (C) COPYRIGHT 2020 Nuvoton Technology Corp. ***/
这段代码是一个用于在CRC-CCITT模式下计算CRC校验值的示例程序。以下是对程序的主要部分的解释:
初始化函数 SYS_Init 和 UART0_Init:
SYS_Init: 用于初始化系统、外设时钟和多功能IO。
UART0_Init: 用于初始化UART0模块,设置波特率为115200。
主函数 main:
系统初始化:
解锁保护寄存器,以允许对一些关键寄存器的写操作。
启用高频内部振荡器(HIRC)作为系统时钟源。
配置时钟源,将HCLK时钟源切换到HIRC,并设置HCLK分频系数为1。
启用UART0模块和CRC模块的时钟。
设置PB12和PB13引脚为UART0的RXD和TXD。
锁定保护寄存器,以防止对关键寄存器的未经授权的写入。
|