打印
[DemoCode下载]

M0系列的UID应用方法

[复制链接]
255|5
手机看帖
扫描二维码
随时随地手机跟帖
跳转到指定楼层
楼主
wanduzi|  楼主 | 2020-3-29 12:15 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
ui, AC
EC_M051_IO_Toggle_With_UID_Protect_V1.00.zip (4.05 MB)

使用特权

评论回复
沙发
wanduzi|  楼主 | 2020-3-29 12:15 | 只看该作者
/**************************************************************************//**
* [url=home.php?mod=space&uid=288409]@file[/url]     main.c
* @brief
*           Execution IO toggle example with UID protect.
* @note
* Copyright (C) 2019 Nuvoton Technology Corp. All rights reserved.
*****************************************************************************/
#include <stdio.h>
#include "M051Series.h"

#define PLL_CLOCK                   50000000
                                    
#define UID_START_ADDR              0x00002000   // UID start address of APROM
#define UID_ADDR_OFFSET             0x100        // UID address offset of APROM

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

/*---------------------------------------------------------------------------
   Function:    SYS_Init
   Parameters:  None.
   Returns:     None.
   Description: System and periphial module clock setting.
---------------------------------------------------------------------------*/
void SYS_Init(void)
{

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

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

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

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

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

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

    /* Enable UART module clock */
    CLK_EnableModuleClock(UART0_MODULE);

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

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

}

/*---------------------------------------------------------------------------
   Function:    UART0_Init
   Parameters:  None.
   Returns:     None.
   Description: Initialize UART0 module, setting buadrate is 115200 bps
---------------------------------------------------------------------------*/
void UART0_Init()
{
    /* Reset UART0 module */
    SYS_ResetModule(UART0_RST);

    /* Configure UART0 and set UART0 Baudrate */
    UART_Open(UART0, 115200);
}

/*---------------------------------------------------------------------------
   Function:    Delay_count
   Parameters:  None.
   Returns:     None.
   Description: Delay of IO toggle.
---------------------------------------------------------------------------*/
void Delay_count(int32_t u32count)
{
    int32_t i32i,i32j;
    for(i32i=0; i32i<=3000; i32i++)
        for(i32j=0; i32j<u32count; i32j++);
}

/*---------------------------------------------------------------------------
   Function:    main
   Parameters:  None.
   Returns:     None.
   Description: main routine of the example.
---------------------------------------------------------------------------*/
int main(void)
{
    int32_t  i32_cnt;
    uint32_t au32mcuUID[4];
    uint32_t au32modfiyUID[4];

    /* Unlock protected registers */
    SYS_UnlockReg();

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

    /* Enable FMC ISP function */
    FMC_Open();

    /* 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("Example : IO toggle with UID protect\n");

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

    SYS_UnlockReg();

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

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

    SYS_LockReg();

    /************* User change UID compare implement ***************/
    /* Compare original UID and change UID */
    if((au32mcuUID[0] == au32modfiyUID[0]) && (au32mcuUID[1] == au32modfiyUID[1]) && (au32mcuUID[2] == au32modfiyUID[2]))
    {
        printf("=> Match UID, MCU toggle PB13...\n");
        while(1)
        {
            Delay_count(500);
            P35 = 0;
            Delay_count(500);
            P35 = 1;
        }
    }
    else
    {
        printf("=> Conflict UID, MCU enter suspend...\n");
        __WFI();
    }

    while(1);
}

使用特权

评论回复
板凳
heisexingqisi| | 2020-3-29 13:18 | 只看该作者
读取出来判断?

使用特权

评论回复
地板
heisexingqisi| | 2020-3-29 13:18 | 只看该作者
这种处理方式如何实现批量

使用特权

评论回复
5
磨砂| | 2020-4-6 15:47 | 只看该作者
非常感谢楼主分享

使用特权

评论回复
6
晓伍| | 2020-4-6 15:47 | 只看该作者
非常感谢楼主分享

使用特权

评论回复
发新帖 我要提问
您需要登录后才可以回帖 登录 | 注册

本版积分规则

143

主题

1728

帖子

3

粉丝