[DemoCode下载] 透过判断自定义UID来翻转IO

[复制链接]
957|1
 楼主| 小明的同学 发表于 2024-4-21 15:27 | 显示全部楼层 |阅读模式
EC_M051_IO_Toggle_With_UID_Protect_V1.00 (3).zip (2.62 MB, 下载次数: 0)
Nuvoton M051 系列微处理器提供UID (Unique Identifier),开发者在应用上可透过识别UID的正确性,决定应用是否继续执行,确保产品不被任意复制仿冒。单纯的UID识别安全性似乎不足,因此将提供一个可自行增加识别复杂度范例,开发者可以基于微处理器的UID,再进行自定义的运算,使用运算过的UID识别有更佳的安全性。
47166624bfd7d77b0.png
  1. /**************************************************************************//**
  2. * [url=home.php?mod=space&uid=288409]@file[/url]     main.c
  3. * @brief
  4. *           Execution IO toggle example with UID protect.
  5. * @note
  6. * Copyright (C) 2019 Nuvoton Technology Corp. All rights reserved.
  7. *****************************************************************************/
  8. #include <stdio.h>
  9. #include "M051Series.h"

  10. #define PLL_CLOCK                   50000000
  11.                                     
  12. #define UID_START_ADDR              0x00002000   // UID start address of APROM
  13. #define UID_ADDR_OFFSET             0x100        // UID address offset of APROM

  14. __attribute__((at(UID_START_ADDR + UID_ADDR_OFFSET))) static const uint32_t gau32UIDAddr[4]= {2,2,2};

  15. /*---------------------------------------------------------------------------
  16.    Function:    SYS_Init
  17.    Parameters:  None.
  18.    Returns:     None.
  19.    Description: System and periphial module clock setting.
  20. ---------------------------------------------------------------------------*/
  21. void SYS_Init(void)
  22. {

  23.     /* Enable Internal RC 22.1184MHz clock */
  24.     CLK_EnableXtalRC(CLK_PWRCON_OSC22M_EN_Msk);

  25.     /* Waiting for Internal RC clock ready */
  26.     CLK_WaitClockReady(CLK_CLKSTATUS_OSC22M_STB_Msk);

  27.     /* Switch HCLK clock source to Internal RC and HCLK source divide 1 */
  28.     CLK_SetHCLK(CLK_CLKSEL0_HCLK_S_HIRC, CLK_CLKDIV_HCLK(1));

  29.     /* Enable external XTAL 12MHz clock */
  30.     CLK_EnableXtalRC(CLK_PWRCON_XTL12M_EN_Msk);

  31.     /* Waiting for external XTAL clock ready */
  32.     CLK_WaitClockReady(CLK_CLKSTATUS_XTL12M_STB_Msk);

  33.     /* Set core clock as PLL_CLOCK from PLL */
  34.     CLK_SetCoreClock(PLL_CLOCK);

  35.     /* Enable UART module clock */
  36.     CLK_EnableModuleClock(UART0_MODULE);

  37.     /* Select UART module clock source */
  38.     CLK_SetModuleClock(UART0_MODULE, CLK_CLKSEL1_UART_S_HXT, CLK_CLKDIV_UART(1));

  39.     /* Set GPB multi-function pins for UART0 RXD(P0.3) and TXD(P0.2) */
  40.     SYS->P0_MFP &= ~(SYS_MFP_P03_Msk | SYS_MFP_P02_Msk);
  41.     SYS->P0_MFP |= (SYS_MFP_P03_RXD0 | SYS_MFP_P02_TXD0);

  42. }

  43. /*---------------------------------------------------------------------------
  44.    Function:    UART0_Init
  45.    Parameters:  None.
  46.    Returns:     None.
  47.    Description: Initialize UART0 module, setting buadrate is 115200 bps
  48. ---------------------------------------------------------------------------*/
  49. void UART0_Init()
  50. {
  51.     /* Reset UART0 module */
  52.     SYS_ResetModule(UART0_RST);

  53.     /* Configure UART0 and set UART0 Baudrate */
  54.     UART_Open(UART0, 115200);
  55. }

  56. /*---------------------------------------------------------------------------
  57.    Function:    Delay_count
  58.    Parameters:  None.
  59.    Returns:     None.
  60.    Description: Delay of IO toggle.
  61. ---------------------------------------------------------------------------*/
  62. void Delay_count(int32_t u32count)
  63. {
  64.     int32_t i32i,i32j;
  65.     for(i32i=0; i32i<=3000; i32i++)
  66.         for(i32j=0; i32j<u32count; i32j++);
  67. }

  68. /*---------------------------------------------------------------------------
  69.    Function:    main
  70.    Parameters:  None.
  71.    Returns:     None.
  72.    Description: main routine of the example.
  73. ---------------------------------------------------------------------------*/
  74. int main(void)
  75. {
  76.     int32_t  i32_cnt;
  77.     uint32_t au32mcuUID[4];
  78.     uint32_t au32modfiyUID[4];

  79.     /* Unlock protected registers */
  80.     SYS_UnlockReg();

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

  83.     /* Enable FMC ISP function */
  84.     FMC_Open();

  85.     /* Lock protected registers */
  86.     SYS_LockReg();

  87.     /* Init UART0 for printf */
  88.     UART0_Init();

  89.     printf("\n\nCPU [url=home.php?mod=space&uid=72445]@[/url] %d Hz\n", SystemCoreClock);
  90.     printf("Example : IO toggle with UID protect\n");

  91.     /* Configure PB.13 as Output mode*/
  92.     GPIO_SetMode(P3, BIT5, GPIO_PMD_OUTPUT);

  93.     SYS_UnlockReg();

  94.     for(i32_cnt = 0; i32_cnt < 3; i32_cnt++)
  95.     {
  96.         /* Read UID using FMC ISP form chip */
  97.         au32mcuUID[i32_cnt] = FMC_ReadUID(i32_cnt);
  98.         printf("  Unique ID %d ........................... [0x%08x]\n", i32_cnt, au32mcuUID[i32_cnt]);
  99.     }

  100.     for(i32_cnt = 0; i32_cnt < 3; i32_cnt++)
  101.     {
  102.         /* Read chnage UID form APROM */
  103.         au32modfiyUID[i32_cnt] = FMC_Read(UID_START_ADDR+UID_ADDR_OFFSET+(i32_cnt*4));
  104.         printf("  Modify ID %d ........................... [0x%08x]\n", i32_cnt, au32modfiyUID[i32_cnt]);
  105.     }

  106.     SYS_LockReg();

  107.     /************* User change UID compare implement ***************/
  108.     /* Compare original UID and change UID */
  109.     if((au32mcuUID[0] == au32modfiyUID[0]) && (au32mcuUID[1] == au32modfiyUID[1]) && (au32mcuUID[2] == au32modfiyUID[2]))
  110.     {
  111.         printf("=> Match UID, MCU toggle PB13...\n");
  112.         while(1)
  113.         {
  114.             Delay_count(500);
  115.             P35 = 0;
  116.             Delay_count(500);
  117.             P35 = 1;
  118.         }
  119.     }
  120.     else
  121.     {
  122.         printf("=> Conflict UID, MCU enter suspend...\n");
  123.         __WFI();
  124.     }

  125.     while(1);
  126. }


21mengnan 发表于 2024-4-21 18:30 | 显示全部楼层
UID是写在了FMC上,所以需要FMC才能读取。
您需要登录后才可以回帖 登录 | 注册

本版积分规则

159

主题

1640

帖子

2

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