目录
概述
一、使用方法
二、STM32CubeMx配置
三、Examples
四、运行结果
五、总结
本篇文章介绍如何使用STM32HAL库,来读取 UID唯一码加密示例。
硬件:STM32F103CBT6最小系统板
软件:Keil 5.29 + STM32CubeMX6.01
请参考这篇博文《STM32HAL库-读取芯片维一码(UID)》,在此不做过多讲解。
1、bsp_cpu_id.c文件
#include "bsp_cpu_id.h"
#include "stdio.h"
uint32_t CPU_ID[3];
uint8_t UID[12];
uint8_t MAC[12];
/**
* 函数功能: 获取芯片ID
* 输入参数: 无
* 返 回 值: 无
* 说 明:每个芯片都有唯一的 96_bit unique ID
*/
void Get_ChipID(void)
{
CPU_ID[0] = *(__IO uint32_t *)(0X1FFFF7F0); // 高字节
CPU_ID[1] = *(__IO uint32_t *)(0X1FFFF7EC); //
CPU_ID[2] = *(__IO uint32_t *)(0X1FFFF7E8); // 低字节
}
/**
* 函数功能: 获取芯片ID
* 输入参数: 无
* 返 回 值: 无
* 说 明:每个芯片都有唯一的 96_bit unique ID
*/
void Get_UID(void)
{
uint8_t i = 0;
//用芯片唯一ID来做模块的MAC F0系列 0x1FFFF7AC F1系列 0x1FFFF7E8
for(i=0; i<8; i++)
{
MAC[i] = *(uint8_t *)(0x1FFFF7E8 + i);
UID[i] = MAC[i];
}
UID[8] = *(uint8_t *)(0x1FFFF7E8 + 8);
UID[9] = *(uint8_t *)(0x1FFFF7E8 + 9);
UID[10] = *(uint8_t *)(0x1FFFF7E8 + 10);
UID[11] = *(uint8_t *)(0x1FFFF7E8 + 11);
MAC[4] += UID[8];
MAC[5] += UID[9];
MAC[6] += UID[10];
MAC[7] += UID[11];
/* 芯片的唯一ID */
printf("\r\n UID: %0.2X-%0.2X-%0.2X-%0.2X\r\n",UID[8],UID[9],UID[10],UID[11]);
printf("\r\n MAC: %X-%X-%X-%X\r\n",MAC[4],MAC[5],MAC[6],MAC[7]);
}
/**
* 函数功能: 获取芯片ID
* 输入参数: 无
* 返 回 值: 无
* 说 明:每个芯片都有唯一的 96_bit unique ID
*/
void Get_UID2(void)
{
uint8_t i = 0;
//用芯片唯一ID来做模块的MAC F0系列 0x1FFFF7AC F1系列 0x1FFFF7E8
for(i=0; i<12; i++)
{
MAC[i] = *(uint8_t *)(0x1FFFF7E8 + i);
printf(" %0.2X", MAC[i]);
}
//芯片的唯一ID为: 30 FF 69 06 4D 50 35 35 39 25 08 43
if(MAC[0] == 0x30 && MAC[1] == 0xFF && MAC[2] == 0x69 &&
MAC[3] == 0x06 && MAC[4] == 0x4D && MAC[5] == 0x50 &&
MAC[6] == 0x35 && MAC[7] == 0x35 && MAC[8] == 0x39 &&
MAC[9] == 0x25 && MAC[10] == 0x08 && MAC[11] == 0x43
)
{
printf("\r\n pass ok \r\n");
}
else
{
printf("\r\n error \r\n");
}
}
uint32_t Get_key_vlaue(void)
{
uint32_t key_num,key_temp;
uint32_t ID_COM_7BIT,date;
uint16_t year = 2116;
uint8_t month = 1, day = 7;
Get_ChipID();
// CPU_ID[0]=0xABCDEF01;
// CPU_ID[1]=0x12345678;
// CPU_ID[2]=0xFEDCBA98;
/* 芯片的唯一ID */
printf("\r\n芯片的唯一ID为: %08X-%08X-%08X\r\n",CPU_ID[0],CPU_ID[1],CPU_ID[2]);
ID_COM_7BIT=(0x0FF00000&CPU_ID[0]) + (0x000FF000&CPU_ID[1]) + (0x00000FFF&CPU_ID[2]);
printf("ID_temp:0x%08X (十进制)%d\n",ID_COM_7BIT,ID_COM_7BIT);
date=year*10000+month*100+day;
printf("year:%d month:%d day:%d -> date:%d\n",year,month,day,date);
if(ID_COM_7BIT>0x7FFFFFF)
{
key_num = ID_COM_7BIT-date;
}
else
{
key_num=ID_COM_7BIT+date;
}
key_temp=key_num&0x0000FFFF;
key_num=(key_temp<<16)|(key_num>>16);
printf("key_num:0x%08X (十进制)%d\n",key_num,key_num);
printf("\r\n芯片flash的容量为: %dK \r\n", *(__IO uint16_t *)(0X1FFFF7E0));
return key_num;
}
void test(void)
{
Get_ChipID();
Get_UID();
Get_UID2();
/* 芯片的容量 */
printf("\r\n芯片flash的容量为: %dK \r\n", *(__IO uint16_t *)(0X1FFFF7E0));
}
2、bsp_cpu_id.h文件
#ifndef __CHIPID_H
#define __CHIPID_H
#include "stm32f1xx_hal.h"
uint32_t Get_key_vlaue(void);
void test(void);
#endif /* __CHIPID_H */
3、main.c文件
/* USER CODE BEGIN Header */
/**
******************************************************************************
* @file : main.c
* @brief : Main program body
******************************************************************************
* @attention
*
* © Copyright (c) 2021 STMicroelectronics.
* All rights reserved.
*
* This software component is licensed by ST under BSD 3-Clause license,
* the "License"; You may not use this file except in compliance with the
* License. You may obtain a copy of the License at:
* opensource.org/licenses/BSD-3-Clause
*
******************************************************************************
*/
/* USER CODE END Header */
/* Includes ------------------------------------------------------------------*/
#include "main.h"
#include "usart.h"
#include "gpio.h"
/* Private includes ----------------------------------------------------------*/
/* USER CODE BEGIN Includes */
#include "stdio.h"
#include "bsp_cpu_id.h"
/* USER CODE END Includes */
/* Private typedef -----------------------------------------------------------*/
/* USER CODE BEGIN PTD */
#define SELECT 1
/* USER CODE END PTD */
/* Private define ------------------------------------------------------------*/
/* USER CODE BEGIN PD */
/* USER CODE END PD */
/* Private macro -------------------------------------------------------------*/
/* USER CODE BEGIN PM */
/* USER CODE END PM */
/* Private variables ---------------------------------------------------------*/
/* USER CODE BEGIN PV */
void Get_ChipIDNUM(__IO uint32_t *ID_num)
{
ID_num[0] = *(__IO uint32_t *)(0X1FFFF7F0); // 高字节
ID_num[1] = *(__IO uint32_t *)(0X1FFFF7EC); //
ID_num[2] = *(__IO uint32_t *)(0X1FFFF7E8); // 低字节
}
uint8_t KEY_to_DATE(__IO uint32_t key_num,__IO uint16_t *pyear,__IO uint8_t *pmonth,__IO uint8_t *pday)
{
__IO uint32_t ID_temp,ID_num[3];
__IO uint32_t date;
uint32_t key_temp=key_num&0x0000FFFF;
key_num=(key_temp<<16)|(key_num>>16);
printf("key_num:%08X\n",key_num);
Get_ChipIDNUM(ID_num);
// ID_num[0]=0xABCDEF01;
// ID_num[1]=0x12345678;
// ID_num[2]=0xFEDCBA98;
ID_temp=(0x0FF00000&ID_num[0]) + (0x000FF000&ID_num[1]) + (0x00000FFF&ID_num[2]);
if(ID_temp>0x7FFFFFF)
{
date = ID_temp-key_num;
}
else
{
date = key_num-ID_temp;
}
*pyear=date/10000;
*pmonth=date%10000/100;
*pday=date%100;
if((*pyear>2199)||(*pyear<2000))
return 1; // 日期获取失败
if(*pmonth>12)
return 1; // 日期获取失败
if(*pday>31)
return 1; // 日期获取失败
return 0; // 日期获取成功
}
/* USER CODE END PV */
/* Private function prototypes -----------------------------------------------*/
void SystemClock_Config(void);
/* USER CODE BEGIN PFP */
/* USER CODE END PFP */
/* Private user code ---------------------------------------------------------*/
/* USER CODE BEGIN 0 */
#ifdef __GNUC__
/* With GCC/RAISONANCE, small printf (option LD Linker->Libraries->Small printf
set to 'Yes') calls __io_putchar() */
#define PUTCHAR_PROTOTYPE int __io_putchar(int ch)
#else
#define PUTCHAR_PROTOTYPE int fputc(int ch, FILE *f)
#endif /* __GNUC__ */
/**
* @brief Retargets the C library printf function to the USART.
* @param None
* @retval None
*/
PUTCHAR_PROTOTYPE
{
/* Place your implementation of fputc here */
/* e.g. write a character to the EVAL_COM1 and Loop until the end of transmission */
HAL_UART_Transmit(&huart1, (uint8_t *)&ch, 1, 0xFFFF);
return ch;
}
int fgetc(FILE * f)
{
uint8_t ch = 0;
HAL_UART_Receive(&huart1, (uint8_t *)&ch, 1, 0xffff);
return ch;
}
/* USER CODE END 0 */
/**
* @brief The application entry point.
* @retval int
*/
int main(void)
{
/* USER CODE BEGIN 1 */
__IO uint32_t key = 0; //0x3FDB0448;
__IO uint16_t year;
__IO uint8_t month,day;
/* USER CODE END 1 */
/* MCU Configuration--------------------------------------------------------*/
/* Reset of all peripherals, Initializes the Flash interface and the Systick. */
HAL_Init();
/* USER CODE BEGIN Init */
/* USER CODE END Init */
/* Configure the system clock */
SystemClock_Config();
/* USER CODE BEGIN SysInit */
/* USER CODE END SysInit */
/* Initialize all configured peripherals */
MX_GPIO_Init();
MX_USART1_UART_Init();
/* USER CODE BEGIN 2 */
//把获取到的KEY,赋值到key解析结果是否正确
key = Get_key_vlaue();
printf("*************************\r\n");
printf("key:%08X->",key);
KEY_to_DATE(key,&year,&month,&day);
printf("year:%d month:%d day:%d\n",year,month,day);
/* USER CODE END 2 */
/* Infinite loop */
/* USER CODE BEGIN WHILE */
while (1)
{
HAL_Delay(1000);
HAL_GPIO_TogglePin(LED_GPIO_Port, LED_Pin);
/* USER CODE END WHILE */
/* USER CODE BEGIN 3 */
}
/* USER CODE END 3 */
}
/**
* @brief System Clock Configuration
* @retval None
*/
void SystemClock_Config(void)
{
RCC_OscInitTypeDef RCC_OscInitStruct = {0};
RCC_ClkInitTypeDef RCC_ClkInitStruct = {0};
/** Initializes the RCC Oscillators according to the specified parameters
* in the RCC_OscInitTypeDef structure.
*/
RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSE;
RCC_OscInitStruct.HSEState = RCC_HSE_ON;
RCC_OscInitStruct.HSEPredivValue = RCC_HSE_PREDIV_DIV1;
RCC_OscInitStruct.HSIState = RCC_HSI_ON;
RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;
RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSE;
RCC_OscInitStruct.PLL.PLLMUL = RCC_PLL_MUL9;
if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK)
{
Error_Handler();
}
/** Initializes the CPU, AHB and APB buses clocks
*/
RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK|RCC_CLOCKTYPE_SYSCLK
|RCC_CLOCKTYPE_PCLK1|RCC_CLOCKTYPE_PCLK2;
RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK;
RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;
RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV2;
RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV1;
if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_2) != HAL_OK)
{
Error_Handler();
}
}
/* USER CODE BEGIN 4 */
/* USER CODE END 4 */
/**
* @brief This function is executed in case of error occurrence.
* @retval None
*/
void Error_Handler(void)
{
/* USER CODE BEGIN Error_Handler_Debug */
/* User can add his own implementation to report the HAL error return state */
/* USER CODE END Error_Handler_Debug */
}
#ifdef USE_FULL_ASSERT
/**
* @brief Reports the name of the source file and the source line number
* where the assert_param error has occurred.
* @param file: pointer to the source file name
* @param line: assert_param error line source number
* @retval None
*/
void assert_failed(uint8_t *file, uint32_t line)
{
/* USER CODE BEGIN 6 */
/* User can add his own implementation to report the file name and line number,
tex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */
/* USER CODE END 6 */
}
#endif /* USE_FULL_ASSERT */
/************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/
传送门->代码
好了,就介绍到此。