[DemoCode下载] UID的用法,M031 APROM程序绑定UID

[复制链接]
955|2
 楼主| 捉虫天师 发表于 2024-1-29 19:21 | 显示全部楼层 |阅读模式
EC_M031_APROM_Bind_UID_V1.00 (1).zip (1.46 MB, 下载次数: 2)
  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. * [url=home.php?mod=space&uid=247401]@brief[/url]    APROM bind UID sample code.
  5. *
  6. * SPDX-License-Identifier: Apache-2.0
  7. * [url=home.php?mod=space&uid=17282]@CopyRight[/url] (C) 2021 Nuvoton Technology Corp. All rights reserved.
  8. ******************************************************************************/
  9. #include "stdio.h"
  10. #include "NuMicro.h"
  11. #include "uid.h"


  12. /*----------------------------------------------------------------------*/
  13. /* System Init                                                          */
  14. /*----------------------------------------------------------------------*/
  15. void SYS_Init(void)
  16. {
  17.     /* Unlock protected registers */
  18.     SYS_UnlockReg();

  19.     /* Enable HIRC */
  20.     CLK_EnableXtalRC(CLK_PWRCTL_HIRCEN_Msk);

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

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

  25.     /* Set both PCLK0 and PCLK1 as HCLK/2 */
  26.     CLK->PCLKDIV = (CLK_PCLKDIV_APB0DIV_DIV2 | CLK_PCLKDIV_APB1DIV_DIV2);

  27.     /* Switch UART0 clock source to HIRC */
  28.     CLK_SetModuleClock(UART0_MODULE, CLK_CLKSEL1_UART0SEL_HIRC, CLK_CLKDIV0_UART0(1));

  29.     /* Enable UART peripheral clock */
  30.     CLK_EnableModuleClock(UART0_MODULE);

  31.     /* Update System Core Clock */
  32.     /* User can use SystemCoreClockUpdate() to calculate PllClock, SystemCoreClock and CycylesPerUs automatically. */
  33.     SystemCoreClockUpdate();

  34.     /*----------------------------------------------------------------------*/
  35.     /* Init I/O Multi-function                                              */
  36.     /*----------------------------------------------------------------------*/
  37.     /* Set GPB multi-function pins for UART0 RXD and TXD */
  38.     SYS->GPB_MFPH = (SYS->GPB_MFPH & ~(SYS_GPB_MFPH_PB12MFP_Msk | SYS_GPB_MFPH_PB13MFP_Msk)) |
  39.                     (SYS_GPB_MFPH_PB12MFP_UART0_RXD | SYS_GPB_MFPH_PB13MFP_UART0_TXD);

  40.     /* Lock protected registers */
  41.     SYS_LockReg();
  42. }


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

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


  53. int32_t main(void)
  54. {
  55.     /* Init System, IP clock and multi-function I/O. */
  56.     SYS_Init();

  57.     /* Init UART0 for printf */
  58.     UART0_Init();

  59.     printf("\n\nCPU [url=home.php?mod=space&uid=72445]@[/url] %dHz\n", SystemCoreClock);

  60.     printf("+-------------------------------------------------+\n");
  61.     printf("|    APROM Bind UID Sample Code                   |\n");
  62.     printf("+-------------------------------------------------+\n");

  63.     /* Unlock protected registers */
  64.     SYS_UnlockReg();

  65.     /* Check password according to the UID */
  66.     if(UID_PasswordCheck() == 1)
  67.     {
  68.         printf("Password Right ! \n");
  69.     }
  70.     else
  71.     {
  72.         printf("Password Error ! \n");
  73.     }

  74.     /* Lock protected registers */
  75.     SYS_LockReg();
  76.     while(1);
  77. }


 楼主| 捉虫天师 发表于 2024-1-29 19:22 | 显示全部楼层
  1. /**************************************************************************//**
  2. * @file     uid.c
  3. * @brief
  4. *           APROM bind UID source file
  5. *
  6. * @note
  7. * SPDX-License-Identifier: Apache-2.0
  8. * Copyright (C) 2021 Nuvoton Technology Corp. All rights reserved.
  9. *****************************************************************************/

  10. #include "uid.h"


  11. /*---------------------------------------------------------------------------------------------------------*/
  12. /*  Define global variables and constants                                                                  */
  13. /*---------------------------------------------------------------------------------------------------------*/
  14. const uint32_t g_au32Password[FMC_FLASH_PAGE_SIZE / 4] __attribute__((at(PASSWORD_ADDR))) = {INIT_PASSWORD_0, INIT_PASSWORD_1, INIT_PASSWORD_2};


  15. /**
  16.   * @brief Read UID.
  17.   * @param[in]  au32Uid Buffer to storage UID.
  18.   * [url=home.php?mod=space&uid=266161]@return[/url]  None.
  19.   */
  20. void UID_Read(uint32_t au32Uid[3])
  21. {
  22.     uint8_t i;

  23.     FMC_Open();

  24.     for(i = 0; i < 3; i++)
  25.     {
  26.         au32Uid[i] = FMC_ReadUID(i);
  27.     }

  28.     FMC_Close();
  29. }


  30. /**
  31.   * @brief UID Encrypt.
  32.   * @param[in]  au32Uid Buffer to storage UID.
  33.   * @return  None.
  34.   * [url=home.php?mod=space&uid=1543424]@Details[/url]  Encrypt the UID, users can change it according to their encryption algorithm.
  35.   */
  36. void UID_Encrypt(uint32_t au32Uid[3])
  37. {
  38.     uint8_t i;

  39.     /* Encrypt the UID, users can change it according to their encryption algorithm */
  40.     for(i = 0; i < 3; i++)
  41.     {
  42.         au32Uid[i] = (~au32Uid[i]) * 5 / i;
  43.     }
  44. }


  45. /**
  46.   * @brief Write password to APROM.
  47.   * @param[in]  au32Password The Buffer of the password.
  48.   * @return  None.
  49.   */
  50. void UID_PasswordWrite(uint32_t au32Password[3])
  51. {
  52.     FMC_Open();

  53.     FMC_ENABLE_AP_UPDATE();

  54.     FMC_Erase((uint32_t)g_au32Password);

  55.     FMC_Write((uint32_t)&g_au32Password[0], au32Password[0]);
  56.     FMC_Write((uint32_t)&g_au32Password[1], au32Password[1]);
  57.     FMC_Write((uint32_t)&g_au32Password[2], au32Password[2]);

  58.     FMC_DISABLE_AP_UPDATE();

  59.     FMC_Close();
  60. }


  61. /**
  62.   * @brief Check password.
  63.   * @param[in]  au32Uid Buffer to storage UID.
  64.   * @return Password right or not.
  65.   * @retval   0  Password error.
  66.   * @retval   1  Password right.
  67.   */
  68. uint8_t UID_PasswordCheck(void)
  69. {
  70.     uint8_t i;
  71.     uint32_t au32Uid[3];
  72.     uint32_t *pu32Password = (uint32_t *)g_au32Password;

  73.     if((pu32Password[0] == INIT_PASSWORD_0) && (pu32Password[1] == INIT_PASSWORD_1) && (pu32Password[2] == INIT_PASSWORD_2))
  74.     {
  75.         UID_Read(au32Uid);
  76.         UID_Encrypt(au32Uid);
  77.         UID_PasswordWrite(au32Uid);
  78.     }
  79.     else
  80.     {
  81.         UID_Read(au32Uid);
  82.         UID_Encrypt(au32Uid);

  83.         for(i = 0; i < 3; i++)
  84.         {
  85.             if(au32Uid[i] != pu32Password[i])
  86.             {
  87.                 return 0;
  88.             }
  89.         }
  90.     }

  91.     return 1;
  92. }

  93. /*** (C) COPYRIGHT 2021 Nuvoton Technology Corp. ***/





 楼主| 捉虫天师 发表于 2024-1-29 19:28 | 显示全部楼层
很多人说不知道如何用UID,这个程序讲解了如何用UID。
您需要登录后才可以回帖 登录 | 注册

本版积分规则

213

主题

3276

帖子

7

粉丝
快速回复 在线客服 返回列表 返回顶部