/**************************************************************************//** * [url=home.php?mod=space&uid=288409]@file[/url] main.c * [url=home.php?mod=space&uid=895143]@version[/url] V3.00 * [url=home.php?mod=space&uid=247401]@brief[/url] APROM bind UID sample code. * * SPDX-License-Identifier: Apache-2.0 * [url=home.php?mod=space&uid=17282]@CopyRight[/url] (C) 2021 Nuvoton Technology Corp. All rights reserved. ******************************************************************************/ #include "stdio.h" #include "NuMicro.h" #include "uid.h" /*----------------------------------------------------------------------*/ /* System Init */ /*----------------------------------------------------------------------*/ void SYS_Init(void) { /* Unlock protected registers */ SYS_UnlockReg(); /* Enable HIRC */ CLK_EnableXtalRC(CLK_PWRCTL_HIRCEN_Msk); /* Waiting for HIRC clock ready */ CLK_WaitClockReady(CLK_STATUS_HIRCSTB_Msk); /* Switch HCLK clock source to HIRC */ CLK_SetHCLK(CLK_CLKSEL0_HCLKSEL_HIRC, CLK_CLKDIV0_HCLK(1)); /* Set both PCLK0 and PCLK1 as HCLK/2 */ CLK->PCLKDIV = (CLK_PCLKDIV_APB0DIV_DIV2 | CLK_PCLKDIV_APB1DIV_DIV2); /* Switch UART0 clock source to HIRC */ CLK_SetModuleClock(UART0_MODULE, CLK_CLKSEL1_UART0SEL_HIRC, CLK_CLKDIV0_UART0(1)); /* Enable UART peripheral clock */ CLK_EnableModuleClock(UART0_MODULE); /* Update System Core Clock */ /* User can use SystemCoreClockUpdate() to calculate PllClock, SystemCoreClock and CycylesPerUs automatically. */ SystemCoreClockUpdate(); /*----------------------------------------------------------------------*/ /* Init I/O Multi-function */ /*----------------------------------------------------------------------*/ /* Set GPB multi-function pins for UART0 RXD and TXD */ 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(); } /*----------------------------------------------------------------------*/ /* Init UART0 */ /*----------------------------------------------------------------------*/ void UART0_Init(void) { /* Reset UART0 */ SYS_ResetModule(UART0_RST); /* Configure UART0 and set UART0 baud rate */ UART_Open(UART0, 115200); } int32_t main(void) { /* Init System, IP clock and multi-function I/O. */ SYS_Init(); /* Init UART0 for printf */ UART0_Init(); printf("\n\nCPU [url=home.php?mod=space&uid=72445]@[/url] %dHz\n", SystemCoreClock); printf("+-------------------------------------------------+\n"); printf("| APROM Bind UID Sample Code |\n"); printf("+-------------------------------------------------+\n"); /* Unlock protected registers */ SYS_UnlockReg(); /* Check password according to the UID */ if(UID_PasswordCheck() == 1) { printf("Password Right ! \n"); } else { printf("Password Error ! \n"); } /* Lock protected registers */ SYS_LockReg(); while(1); }
收藏0 举报
/**************************************************************************//** * @file uid.c * @brief * APROM bind UID source file * * @note * SPDX-License-Identifier: Apache-2.0 * Copyright (C) 2021 Nuvoton Technology Corp. All rights reserved. *****************************************************************************/ #include "uid.h" /*---------------------------------------------------------------------------------------------------------*/ /* Define global variables and constants */ /*---------------------------------------------------------------------------------------------------------*/ const uint32_t g_au32Password[FMC_FLASH_PAGE_SIZE / 4] __attribute__((at(PASSWORD_ADDR))) = {INIT_PASSWORD_0, INIT_PASSWORD_1, INIT_PASSWORD_2}; /** * @brief Read UID. * @param[in] au32Uid Buffer to storage UID. * [url=home.php?mod=space&uid=266161]@return[/url] None. */ void UID_Read(uint32_t au32Uid[3]) { uint8_t i; FMC_Open(); for(i = 0; i < 3; i++) { au32Uid[i] = FMC_ReadUID(i); } FMC_Close(); } /** * @brief UID Encrypt. * @param[in] au32Uid Buffer to storage UID. * @return None. * [url=home.php?mod=space&uid=1543424]@Details[/url] Encrypt the UID, users can change it according to their encryption algorithm. */ void UID_Encrypt(uint32_t au32Uid[3]) { uint8_t i; /* Encrypt the UID, users can change it according to their encryption algorithm */ for(i = 0; i < 3; i++) { au32Uid[i] = (~au32Uid[i]) * 5 / i; } } /** * @brief Write password to APROM. * @param[in] au32Password The Buffer of the password. * @return None. */ void UID_PasswordWrite(uint32_t au32Password[3]) { FMC_Open(); FMC_ENABLE_AP_UPDATE(); FMC_Erase((uint32_t)g_au32Password); FMC_Write((uint32_t)&g_au32Password[0], au32Password[0]); FMC_Write((uint32_t)&g_au32Password[1], au32Password[1]); FMC_Write((uint32_t)&g_au32Password[2], au32Password[2]); FMC_DISABLE_AP_UPDATE(); FMC_Close(); } /** * @brief Check password. * @param[in] au32Uid Buffer to storage UID. * @return Password right or not. * @retval 0 Password error. * @retval 1 Password right. */ uint8_t UID_PasswordCheck(void) { uint8_t i; uint32_t au32Uid[3]; uint32_t *pu32Password = (uint32_t *)g_au32Password; if((pu32Password[0] == INIT_PASSWORD_0) && (pu32Password[1] == INIT_PASSWORD_1) && (pu32Password[2] == INIT_PASSWORD_2)) { UID_Read(au32Uid); UID_Encrypt(au32Uid); UID_PasswordWrite(au32Uid); } else { UID_Read(au32Uid); UID_Encrypt(au32Uid); for(i = 0; i < 3; i++) { if(au32Uid[i] != pu32Password[i]) { return 0; } } } return 1; } /*** (C) COPYRIGHT 2021 Nuvoton Technology Corp. ***/
本版积分规则 发表回复 回帖并转播 回帖后跳转到最后一页
213
3276
7
扫码关注 21ic 官方微信
扫码关注嵌入式微处理器
扫码关注电源系统设计
扫码关注21ic项目外包
扫码浏览21ic手机版
本站介绍 | 申请友情链接 | 欢迎投稿 | 隐私声明 | 广告业务 | 网站地图 | 联系我们 | 诚聘英才
京公网安备 11010802024343号