[DemoCode下载] M0系列的UID应用方法

[复制链接]
410|5
 楼主| wanduzi 发表于 2020-3-29 12:15 | 显示全部楼层 |阅读模式
ui, AC
EC_M051_IO_Toggle_With_UID_Protect_V1.00.zip (4.05 MB, 下载次数: 4)
 楼主| wanduzi 发表于 2020-3-29 12:15 | 显示全部楼层
  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. }
heisexingqisi 发表于 2020-3-29 13:18 | 显示全部楼层
读取出来判断?
heisexingqisi 发表于 2020-3-29 13:18 | 显示全部楼层
这种处理方式如何实现批量
磨砂 发表于 2020-4-6 15:47 | 显示全部楼层
非常感谢楼主分享
晓伍 发表于 2020-4-6 15:47 | 显示全部楼层
非常感谢楼主分享
您需要登录后才可以回帖 登录 | 注册

本版积分规则

156

主题

1882

帖子

3

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