打印
[DemoCode下载]

UID的用法,M031 APROM程序绑定UID

[复制链接]
722|2
手机看帖
扫描二维码
随时随地手机跟帖
跳转到指定楼层
楼主
捉虫天师|  楼主 | 2024-1-29 19:21 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
EC_M031_APROM_Bind_UID_V1.00 (1).zip (1.46 MB)
/**************************************************************************//**
* [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);
}


使用特权

评论回复
沙发
捉虫天师|  楼主 | 2024-1-29 19:22 | 只看该作者
/**************************************************************************//**
* @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. ***/





使用特权

评论回复
板凳
捉虫天师|  楼主 | 2024-1-29 19:28 | 只看该作者
很多人说不知道如何用UID,这个程序讲解了如何用UID。

使用特权

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

本版积分规则

193

主题

3057

帖子

7

粉丝