feat(ota): OTA功能测试通过,CANFD解包效率优化
- 添加OTA固件升级功能,包括应用程序验证和跳转机制 - 实现CANFD数据传输协议,支持加密和CRC校验 - 优化CAN通信处理,添加调试输出和错误处理 - 更新RTE组件配置,升级csolution版本到2.12.0 - 增加Flash擦除和写入的安全验证机制 - 添加应用程序区域扫描和验证功能 refactor(can): 重构CAN通信模块和OTA数据处理 - 移除未使用的常量定义和辅助函数 - 改进数据解包和加密解密流程 - 添加条件编译调试输出 - 优化Flash操作的安全性和可靠性 chore(build): 更新项目配置和忽略文件 - 添加tmp/和out/目录到.gitignore - 扩展ROM0_SIZE从0x0020000到0x08005000 - 添加bin输出格式支持
This commit is contained in:
@@ -17,7 +17,7 @@
|
||||
#define __ROM0_BASE 0x08000000
|
||||
// <o> Region size [bytes] <0x0-0xFFFFFFFF:8>
|
||||
// <i> Defines size of memory region. Default: 0x00020000
|
||||
#define __ROM0_SIZE 0x00020000
|
||||
#define __ROM0_SIZE 0x08005000
|
||||
// </h>
|
||||
|
||||
// <h> __ROM1 (unused)
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
/*
|
||||
* CSOLUTION generated file: DO NOT EDIT!
|
||||
* Generated by: csolution version 2.11.0
|
||||
* Generated by: csolution version 2.12.0+p41-gfeadfc5e
|
||||
*
|
||||
* Project: 'Robot_V2.Debug+GD32C103CB'
|
||||
* Target: 'Debug+GD32C103CB'
|
||||
@@ -15,10 +15,10 @@
|
||||
*/
|
||||
#define CMSIS_device_header "gd32c10x.h"
|
||||
|
||||
/* GigaDevice::Device:GD32C10x_StdPeripherals:ADC@1.0.0 */
|
||||
#define RTE_DEVICE_STDPERIPHERALS_ADC
|
||||
/* GigaDevice::Device:GD32C10x_StdPeripherals:CAN@1.0.0 */
|
||||
#define RTE_DEVICE_STDPERIPHERALS_CAN
|
||||
/* GigaDevice::Device:GD32C10x_StdPeripherals:CRC@1.0.0 */
|
||||
#define RTE_DEVICE_STDPERIPHERALS_CRC
|
||||
/* GigaDevice::Device:GD32C10x_StdPeripherals:DBG@1.0.0 */
|
||||
#define RTE_DEVICE_STDPERIPHERALS_DBG
|
||||
/* GigaDevice::Device:GD32C10x_StdPeripherals:DMA@1.0.0 */
|
||||
@@ -37,14 +37,8 @@
|
||||
#define RTE_DEVICE_STDPERIPHERALS_PMU
|
||||
/* GigaDevice::Device:GD32C10x_StdPeripherals:RCU@1.0.0 */
|
||||
#define RTE_DEVICE_STDPERIPHERALS_RCU
|
||||
/* GigaDevice::Device:GD32C10x_StdPeripherals:SPI_I2S@1.0.0 */
|
||||
#define RTE_DEVICE_STDPERIPHERALS_SPI
|
||||
/* GigaDevice::Device:GD32C10x_StdPeripherals:TIMER@1.0.0 */
|
||||
#define RTE_DEVICE_STDPERIPHERALS_TIMER
|
||||
/* GigaDevice::Device:GD32C10x_StdPeripherals:USART@1.0.0 */
|
||||
#define RTE_DEVICE_STDPERIPHERALS_USART
|
||||
/* GigaDevice::Device:GD32C10x_StdPeripherals:WWDGT@1.0.0 */
|
||||
#define RTE_DEVICE_STDPERIPHERALS_WWDGT
|
||||
|
||||
|
||||
#endif /* RTE_COMPONENTS_H */
|
||||
|
||||
@@ -42,6 +42,7 @@ project:
|
||||
type:
|
||||
- elf
|
||||
- hex
|
||||
- bin
|
||||
- map
|
||||
packs:
|
||||
- pack: ARM::CMSIS
|
||||
|
||||
+22
-9
@@ -14,11 +14,6 @@
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
constexpr u16 CANX = CAN0;
|
||||
constexpr u16 DEV_CAN0_ID = 0x0000;
|
||||
constexpr u16 DEV_CAN0_MASK = 0x0000;
|
||||
constexpr u16 DEV_CAN_LOOPBACK_ID = 0x0000;
|
||||
|
||||
CAN_transmiter CAN0_transmiter; // CAN初始化
|
||||
extern BootloaderSystem sys;
|
||||
extern OTAHandler ota;
|
||||
@@ -93,9 +88,6 @@ void can_dma_init(void) {
|
||||
// 使能DMA通道
|
||||
dma_channel_enable(DMA0, DMA_CH4);
|
||||
dma_channel_enable(DMA0, DMA_CH5);
|
||||
|
||||
// 使能CAN接收DMA
|
||||
// can_dma_enable(CAN0, CAN_DMA_RECEIVE);
|
||||
}
|
||||
|
||||
/*!
|
||||
@@ -221,7 +213,11 @@ void CAN_transmiter::communication_check() {
|
||||
receive_message.rx_data,
|
||||
receive_message.rx_dlen);
|
||||
} else {
|
||||
|
||||
#if CAN_DEBUG
|
||||
printf("Unknown CAN ID: %X\n", receive_message.rx_efid);
|
||||
|
||||
#endif
|
||||
}
|
||||
can0_receive_flag = RESET;
|
||||
}
|
||||
@@ -233,7 +229,7 @@ u8 CAN_transmiter::sendDATA_ACK(u32 packet_num, u8 *data) { // 握手的时候
|
||||
transmit_data[1] = 0xC0;
|
||||
// 修复原始代码中不完整的循环
|
||||
for (int i = 2; i < 6; i++) {
|
||||
transmit_data[i] = data[i-2];
|
||||
transmit_data[i] = data[i - 2];
|
||||
}
|
||||
|
||||
return CANFD_AsyncSend(CANFD_OTA_ACK_ID, 6, transmit_data);
|
||||
@@ -278,13 +274,18 @@ void CAN0_RX0_IRQHandler(void) {
|
||||
if (can_interrupt_flag_get(CAN0, CAN_INT_FLAG_RFF0) != RESET) {
|
||||
// FIFO0满中断
|
||||
can_interrupt_flag_clear(CAN0, CAN_INT_FLAG_RFF0);
|
||||
#if CAN_DEBUG
|
||||
printf("CAN FIFO0 full\n");
|
||||
#endif
|
||||
}
|
||||
|
||||
if (can_interrupt_flag_get(CAN0, CAN_INT_FLAG_RFO0) != RESET) {
|
||||
// FIFO0溢出中断
|
||||
can_interrupt_flag_clear(CAN0, CAN_INT_FLAG_RFO0);
|
||||
#if CAN_DEBUG
|
||||
printf("CAN FIFO0 overflow\n");
|
||||
|
||||
#endif
|
||||
}
|
||||
}
|
||||
|
||||
@@ -328,13 +329,18 @@ u8 CAN_transmiter::CANFD_AsyncSend(u32 id, u8 length, u8 *data) {
|
||||
u8 valid_length = length;
|
||||
// 检查CANFD模式是否启用
|
||||
if (!(CAN_CTL(CAN0) & CAN_CTL_DFZ)) {
|
||||
#if CAN_DEBUG
|
||||
printf("CANFD mode not enabled\r\n");
|
||||
#endif
|
||||
return 0;
|
||||
}
|
||||
|
||||
// 检查数据指针是否有效
|
||||
if (data == nullptr) {
|
||||
#if CAN_DEBUG
|
||||
printf("Invalid data pointer\r\n");
|
||||
|
||||
#endif
|
||||
return 0;
|
||||
}
|
||||
|
||||
@@ -355,7 +361,10 @@ u8 CAN_transmiter::CANFD_AsyncSend(u32 id, u8 length, u8 *data) {
|
||||
} else if (length > 48 and length < 65) {
|
||||
valid_length = 64;
|
||||
} else {
|
||||
#if CAN_DEBUG
|
||||
printf("CANFD data length error\r\n");
|
||||
|
||||
#endif
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
@@ -384,9 +393,13 @@ u8 CAN_transmiter::CANFD_AsyncSend(u32 id, u8 length, u8 *data) {
|
||||
|
||||
// 检查是否成功获取mailbox
|
||||
if (transmit_mailbox == CAN_NOMAILBOX) {
|
||||
|
||||
#if CAN_DEBUG
|
||||
// 没有可用的mailbox,启用发送中断以便在邮箱可用时重试
|
||||
printf("No available mailbox for CANFD transmission, enabling TX "
|
||||
"interrupt\r\n");
|
||||
|
||||
#endif
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
||||
@@ -11,36 +11,6 @@ extern "C" {
|
||||
#include <stdio.h>
|
||||
#include "sys/config.h"
|
||||
|
||||
|
||||
|
||||
// 添加辅助函数用于将u16类型数据打包到u8数组中
|
||||
static inline void pack_u16_to_bytes(u8 *buffer, u16 offset, u16 value) {
|
||||
buffer[offset] = (u8)(value & 0xFF);
|
||||
buffer[offset + 1] = (u8)((value >> 8) & 0xFF);
|
||||
}
|
||||
|
||||
// 添加辅助函数用于将float类型数据打包到u8数组中
|
||||
static inline void pack_u32_to_bytes(u8 *buffer, u16 offset,
|
||||
u32_union_t &u32_union) {
|
||||
buffer[offset] = u32_union.b[0];
|
||||
buffer[offset + 1] = u32_union.b[1];
|
||||
buffer[offset + 2] = u32_union.b[2];
|
||||
buffer[offset + 3] = u32_union.b[3];
|
||||
}
|
||||
|
||||
// 添加辅助函数用于从u8数组中解包short类型数据
|
||||
static inline short unpack_bytes_to_short(u8 *buffer, u16 offset) {
|
||||
short value = (short)(buffer[offset] | (buffer[offset + 1] << 8));
|
||||
return value;
|
||||
}
|
||||
|
||||
// 添加辅助函数用于从u8数组中解包u16类型数据
|
||||
static inline u16 unpack_bytes_to_u16(u8 *buffer, u16 offset) {
|
||||
u16 value = (u16)(buffer[offset] | (buffer[offset + 1] << 8));
|
||||
return value;
|
||||
}
|
||||
|
||||
|
||||
u8 can_gpio_init(void); // CAN引脚初始化
|
||||
void canfd_config(void); // CANFD初始化
|
||||
void can_dma_init(void); // CAN DMA初始化
|
||||
|
||||
+105
-113
@@ -2,9 +2,12 @@
|
||||
#include "Can.h"
|
||||
#include "aes128.h"
|
||||
#include "initial.h"
|
||||
#include "sys/OTA.h"
|
||||
#include "sys/config.h"
|
||||
#include "sys/systick.h"
|
||||
#include <cstdio>
|
||||
|
||||
|
||||
extern CAN_transmiter CAN0_transmiter; // CAN初始化
|
||||
|
||||
BootloaderSystem sys;
|
||||
@@ -18,133 +21,122 @@ void BootloaderSystem::initHardware() {
|
||||
crc_deinit(); // 初始化CRC模块
|
||||
CAN0_transmiter.init();
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief 扫描APP地址范围内的Flash内容
|
||||
* @param print_content 是否打印Flash内容,默认为true
|
||||
* @param page_count 扫描的页数,默认为0表示扫描整个APP区域
|
||||
* @retval 无
|
||||
*/
|
||||
void scanAppFlashContent(bool print_content = true, u32 page_count = 0) {
|
||||
u32 start_addr = APP_START_ADDR;
|
||||
u32 total_size = 0xC000;
|
||||
|
||||
// 如果指定了页数,则按页数计算大小
|
||||
if (page_count > 0) {
|
||||
total_size = page_count * FMC_PAGE_SIZE;
|
||||
}
|
||||
|
||||
u32 end_addr = start_addr + total_size;
|
||||
|
||||
#if DEBUG_OTA
|
||||
printf("Scanning Flash from 0x%08X to 0x%08X (%d bytes)\r\n", start_addr,
|
||||
end_addr, total_size);
|
||||
#endif
|
||||
|
||||
u32 page_count_scanned = 0;
|
||||
u32 empty_pages = 0;
|
||||
u32 data_pages = 0;
|
||||
|
||||
for (u32 addr = start_addr; addr < end_addr; addr += FMC_PAGE_SIZE) {
|
||||
bool page_empty = true;
|
||||
|
||||
// 检查当前页是否全为0xFF(空页)
|
||||
for (u32 offset = 0; offset < FMC_PAGE_SIZE; offset += 4) {
|
||||
u32 data = *(u32 *)(addr + offset);
|
||||
|
||||
if (data != 0xFFFFFFFF) {
|
||||
page_empty = false;
|
||||
break; // 找到非空数据,立即退出检查
|
||||
}
|
||||
}
|
||||
|
||||
if (page_empty) {
|
||||
empty_pages++;
|
||||
} else {
|
||||
data_pages++;
|
||||
|
||||
// 只有在非空页面且需要打印时才输出内容
|
||||
if (print_content) {
|
||||
printf("Page 0x%08X: \r\n", addr);
|
||||
|
||||
// 打印所有内容
|
||||
for (u32 offset = 0; offset < FMC_PAGE_SIZE;
|
||||
offset += 16) { // 每行打印16字节
|
||||
// 打印地址偏移
|
||||
printf(" +%03X: ", offset);
|
||||
|
||||
// 打印十六进制数据
|
||||
for (u32 byte_idx = 0; byte_idx < 16; byte_idx++) {
|
||||
if ((offset + byte_idx) < FMC_PAGE_SIZE) {
|
||||
u8 data = *(__IO u8 *)(addr + offset + byte_idx);
|
||||
printf("%02X ", data);
|
||||
} else {
|
||||
printf(" "); // 空白填充
|
||||
}
|
||||
|
||||
// 每8字节加个小空格,便于阅读
|
||||
if (byte_idx == 7) {
|
||||
printf(" ");
|
||||
}
|
||||
}
|
||||
|
||||
// 打印可打印字符
|
||||
printf("|");
|
||||
for (u32 byte_idx = 0; byte_idx < 16; byte_idx++) {
|
||||
if ((offset + byte_idx) < FMC_PAGE_SIZE) {
|
||||
u8 data = *(__IO u8 *)(addr + offset + byte_idx);
|
||||
if (data >= 32 && data <= 126) { // 可打印字符范围
|
||||
printf("%c", data);
|
||||
} else {
|
||||
printf(".");
|
||||
}
|
||||
} else {
|
||||
printf(" "); // 空白填充
|
||||
}
|
||||
}
|
||||
printf("|\r\n");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
page_count_scanned++;
|
||||
}
|
||||
|
||||
#if DEBUG_OTA
|
||||
printf("Scan complete: %d pages scanned, %d empty pages, %d data pages\r\n",
|
||||
page_count_scanned, empty_pages, data_pages);
|
||||
#endif
|
||||
}
|
||||
|
||||
void BootloaderSystem::run() {
|
||||
// 检查是否触发OTA模式
|
||||
if (1) {
|
||||
if (!validateApplication()) {
|
||||
printf("OTA Mode\r\n");
|
||||
// OTA模式
|
||||
while (1) {
|
||||
CAN0_transmiter.communication_check();
|
||||
ota.process();
|
||||
}
|
||||
} else {
|
||||
printf("Normal Mode\r\n");
|
||||
// 正常模式:跳转到APP
|
||||
ota.jumpToApplication();
|
||||
}
|
||||
}
|
||||
|
||||
// 发送单个字节到串口 (用于调试)
|
||||
void uart_send_byte(uint8_t data) {
|
||||
// 实际的串口发送代码
|
||||
while (RESET == usart_flag_get(USART0, USART_FLAG_TBE))
|
||||
;
|
||||
usart_data_transmit(USART0, data);
|
||||
}
|
||||
|
||||
// 以十六进制格式打印数据
|
||||
// 安全的十六进制打印函数,不使用sprintf,不使用大缓冲区
|
||||
void safe_print_hex(const char *prefix, const uint8_t *data, size_t length) {
|
||||
// 1. 先打印前缀
|
||||
const char *p = prefix;
|
||||
while (*p) {
|
||||
while (RESET == usart_flag_get(USART0, USART_FLAG_TBE))
|
||||
;
|
||||
usart_data_transmit(USART0, *p++);
|
||||
}
|
||||
|
||||
while (RESET == usart_flag_get(USART0, USART_FLAG_TBE))
|
||||
;
|
||||
usart_data_transmit(USART0, ':');
|
||||
while (RESET == usart_flag_get(USART0, USART_FLAG_TBE))
|
||||
;
|
||||
usart_data_transmit(USART0, ' ');
|
||||
|
||||
// 2. 逐字节打印数据,每16字节换行
|
||||
for (size_t i = 0; i < length; i++) {
|
||||
uint8_t byte = data[i];
|
||||
|
||||
// 打印高4位
|
||||
uint8_t high = (byte >> 4) & 0x0F;
|
||||
char high_char = (high < 10) ? ('0' + high) : ('A' + high - 10);
|
||||
while (RESET == usart_flag_get(USART0, USART_FLAG_TBE))
|
||||
;
|
||||
usart_data_transmit(USART0, high_char);
|
||||
|
||||
// 打印低4位
|
||||
uint8_t low = byte & 0x0F;
|
||||
char low_char = (low < 10) ? ('0' + low) : ('A' + low - 10);
|
||||
while (RESET == usart_flag_get(USART0, USART_FLAG_TBE))
|
||||
;
|
||||
usart_data_transmit(USART0, low_char);
|
||||
|
||||
// 打印空格
|
||||
while (RESET == usart_flag_get(USART0, USART_FLAG_TBE))
|
||||
;
|
||||
usart_data_transmit(USART0, ' ');
|
||||
|
||||
// 每16字节换行
|
||||
if ((i + 1) % 16 == 0 && i < length - 1) {
|
||||
// 换行
|
||||
while (RESET == usart_flag_get(USART0, USART_FLAG_TBE))
|
||||
;
|
||||
usart_data_transmit(USART0, '\r');
|
||||
while (RESET == usart_flag_get(USART0, USART_FLAG_TBE))
|
||||
;
|
||||
usart_data_transmit(USART0, '\n');
|
||||
|
||||
// 缩进(4个空格)
|
||||
for (int j = 0; j < 4; j++) {
|
||||
while (RESET == usart_flag_get(USART0, USART_FLAG_TBE))
|
||||
;
|
||||
usart_data_transmit(USART0, ' ');
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// 3. 打印换行
|
||||
while (RESET == usart_flag_get(USART0, USART_FLAG_TBE))
|
||||
;
|
||||
usart_data_transmit(USART0, '\r');
|
||||
while (RESET == usart_flag_get(USART0, USART_FLAG_TBE))
|
||||
;
|
||||
usart_data_transmit(USART0, '\n');
|
||||
}
|
||||
|
||||
void test_decrypt() {
|
||||
printf("AES Encryption Verification Starting...\r\n");
|
||||
|
||||
// 定义48字节测试数据
|
||||
uint8_t plaintext[48] = {
|
||||
0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0A, 0x0B,
|
||||
0x0C, 0x0D, 0x0E, 0x0F, 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17,
|
||||
0x18, 0x19, 0x1A, 0x1B, 0x1C, 0x1D, 0x1E, 0x1F, 0x20, 0x21, 0x22, 0x23,
|
||||
0x24, 0x25, 0x26, 0x27, 0x28, 0x29, 0x2A, 0x2B, 0x2C, 0x2D, 0x2E, 0x2F};
|
||||
|
||||
// 定义128位密钥 (16字节)
|
||||
uint8_t key[16] = {0x2b, 0x7e, 0x15, 0x16, 0x28, 0xae, 0xd2, 0xa6,
|
||||
0xab, 0xf7, 0x15, 0x88, 0x09, 0xcf, 0x4f, 0x3c};
|
||||
|
||||
// 创建AES上下文
|
||||
aes128_context ctx;
|
||||
|
||||
// 设置密钥
|
||||
aes128_set_key(&ctx, key);
|
||||
|
||||
// 打印原始数据
|
||||
safe_print_hex("Original Data", plaintext, 48);
|
||||
|
||||
uint8_t ciphertext[48];
|
||||
uint8_t decrypted[48];
|
||||
|
||||
// 加密
|
||||
aes128_ecb_encrypt_48bytes(&ctx, plaintext, ciphertext);
|
||||
safe_print_hex("Encrypted Data", ciphertext, 48);
|
||||
|
||||
// 解密
|
||||
aes128_ecb_decrypt_48bytes(&ctx, ciphertext, decrypted);
|
||||
safe_print_hex("Decrypted Data", decrypted, 48);
|
||||
}
|
||||
int main(void) {
|
||||
sys.initHardware();
|
||||
// scanAppFlashContent();
|
||||
// test_decrypt();
|
||||
sys.run();
|
||||
|
||||
|
||||
+161
-35
@@ -47,33 +47,63 @@ void OTAHandler::process() {
|
||||
}
|
||||
}
|
||||
|
||||
bool OTAHandler::validateApplication() {
|
||||
bool validateApplication() {
|
||||
u32 *app_vector = (u32 *)APP_START_ADDR;
|
||||
u32 stack_ptr = app_vector[0];
|
||||
u32 reset_handler = app_vector[1];
|
||||
|
||||
#if DEBUG_OTA
|
||||
printf("APP_START_ADDR: 0x%08X\r\n", APP_START_ADDR);
|
||||
printf("Stack Pointer: 0x%08X\r\n", stack_ptr);
|
||||
printf("Reset Handler: 0x%08X\r\n", reset_handler);
|
||||
printf("APP Range: 0x%08X - 0x%08X\r\n", APP_START_ADDR,
|
||||
APP_START_ADDR + APP_MAX_SIZE);
|
||||
#endif
|
||||
|
||||
// 简单验证:检查栈指针是否在SRAM范围内
|
||||
return (stack_ptr >= 0x20000000 && stack_ptr < 0x20008000) &&
|
||||
(reset_handler >= APP_START_ADDR &&
|
||||
reset_handler < (APP_START_ADDR + APP_MAX_SIZE));
|
||||
bool stack_valid = (stack_ptr >= 0x20000000 && stack_ptr < 0x20008000);
|
||||
bool reset_valid = (reset_handler >= APP_START_ADDR &&
|
||||
reset_handler < (APP_START_ADDR + APP_MAX_SIZE));
|
||||
|
||||
#if DEBUG_OTA
|
||||
if (!stack_valid) {
|
||||
printf("Stack pointer validation failed!\r\n");
|
||||
}
|
||||
if (!reset_valid) {
|
||||
printf("Reset handler validation failed!\r\n");
|
||||
}
|
||||
#endif
|
||||
|
||||
return stack_valid && reset_valid;
|
||||
}
|
||||
|
||||
void OTAHandler::jumpToApplication() {
|
||||
if (!validateApplication())
|
||||
#if DEBUG_OTA
|
||||
printf("try jump to app\r\n");
|
||||
if (!validateApplication()) {
|
||||
printf("app valid failed\r\n");
|
||||
return;
|
||||
|
||||
} else {
|
||||
printf("valid result:successful\r\n");
|
||||
u8 check_data[2] = {0x0A, 0xAC};
|
||||
CAN0_transmiter.CANFD_AsyncSend(CANFD_OTA_WAIT_ID, 2, check_data);
|
||||
}
|
||||
printf("jumping1\r\n");
|
||||
#endif
|
||||
// 1. 关闭所有中断
|
||||
__disable_irq();
|
||||
for (int i = 0; i < 8; i++) {
|
||||
NVIC->ICER[i] = 0xFFFFFFFF;
|
||||
}
|
||||
|
||||
// 2. 重置外设(特别是CAN,防止冲突)
|
||||
can_deinit(CAN0);
|
||||
|
||||
// 3. 重置系统时钟到默认状态
|
||||
rcu_system_clock_source_config(RCU_CKSYSSRC_IRC8M);
|
||||
|
||||
#if DEBUG_OTA
|
||||
printf("jumping2\r\n");
|
||||
#endif
|
||||
// 4. 关闭一些关键外设时钟
|
||||
rcu_periph_clock_disable(RCU_GPIOA);
|
||||
rcu_periph_clock_disable(RCU_GPIOB);
|
||||
@@ -83,41 +113,130 @@ void OTAHandler::jumpToApplication() {
|
||||
rcu_periph_clock_disable(RCU_TIMER0);
|
||||
rcu_periph_clock_disable(RCU_DMA0);
|
||||
|
||||
// 5. 设置MSP并跳转
|
||||
uint32_t msp_value = *(__IO uint32_t *)APP_START_ADDR;
|
||||
__set_MSP(msp_value);
|
||||
#if DEBUG_OTA
|
||||
printf("jumping3\r\n");
|
||||
#endif
|
||||
// 5. 设置中断向量表偏移到应用程序起始地址
|
||||
SCB->VTOR = APP_START_ADDR;
|
||||
|
||||
// 6. 设置MSP并跳转
|
||||
uint32_t msp_value = *(__IO uint32_t *)APP_START_ADDR;
|
||||
uint32_t reset_handler = *(__IO uint32_t *)(APP_START_ADDR + 4);
|
||||
typedef void (*AppEntryFunc)(void);
|
||||
AppEntryFunc app_entry = (AppEntryFunc)reset_handler;
|
||||
|
||||
// 6. 跳转
|
||||
#if DEBUG_OTA
|
||||
printf("jumping5, MSP:0x%08X, Reset Handler:0x%08X\r\n", msp_value,
|
||||
reset_handler);
|
||||
#endif
|
||||
__set_MSP(msp_value);
|
||||
// 7. 跳转
|
||||
app_entry();
|
||||
|
||||
// 如果程序执行到这里,说明跳转失败
|
||||
#if DEBUG_OTA
|
||||
printf("Jump to application failed!\r\n");
|
||||
#endif
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief 擦除应用程序区域
|
||||
* @param 无
|
||||
* @retval 无
|
||||
*/
|
||||
void OTAHandler::eraseApplicationArea() {
|
||||
// 擦除APP区域
|
||||
fmc_unlock();
|
||||
|
||||
// 擦除前禁用中断,防止中断处理程序执行期间发生Flash操作导致问题
|
||||
__disable_irq();
|
||||
|
||||
fmc_state_enum fmc_status;
|
||||
for (u32 addr = APP_START_ADDR; addr < (APP_START_ADDR + APP_MAX_SIZE);
|
||||
addr += FMC_PAGE_SIZE) {
|
||||
fmc_page_erase(addr);
|
||||
fmc_status = fmc_page_erase(addr);
|
||||
if (fmc_status != FMC_READY) {
|
||||
// 如果擦除失败,记录错误并退出
|
||||
#if DEBUG_OTA
|
||||
printf("Flash erase failed at address 0x%08X, status: %d\r\n", addr,
|
||||
fmc_status);
|
||||
#endif
|
||||
ota_error = OTA_ERROR_FLASH_ERROR;
|
||||
send_error_frame(OTA_ERROR_FLASH_ERROR);
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
// 重新启用中断
|
||||
__enable_irq();
|
||||
|
||||
fmc_lock();
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief 写入应用程序数据
|
||||
* @param offset: 写入偏移量
|
||||
* @param data: 数据指针
|
||||
* @param size: 数据大小
|
||||
* @retval 无
|
||||
*/
|
||||
void OTAHandler::writeApplicationData(u32 offset, u8 *data, u32 size) {
|
||||
fmc_unlock();
|
||||
u32 write_addr = APP_START_ADDR + offset;
|
||||
|
||||
// 32位对齐写入
|
||||
for (u32 i = 0; i < size; i += 4) {
|
||||
u32 i = 0;
|
||||
u8 *src = data;
|
||||
|
||||
// 32位对齐写入 - 处理完整的32位字
|
||||
for (; i <= size - 4; i += 4) {
|
||||
fmc_word_program(write_addr + i, *(u32 *)&src[i]);
|
||||
}
|
||||
|
||||
// 处理剩余字节(如果有的话)
|
||||
if (i < size) {
|
||||
u32 word = 0;
|
||||
memcpy(&word, &data[i], 4);
|
||||
u8 *word_ptr = (u8 *)&word;
|
||||
u32 remaining_bytes = size - i;
|
||||
|
||||
// 直接复制剩余字节到word变量
|
||||
for (u32 j = 0; j < remaining_bytes; j++) {
|
||||
word_ptr[j] = src[i + j];
|
||||
}
|
||||
|
||||
fmc_word_program(write_addr + i, word);
|
||||
}
|
||||
fmc_lock();
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief 验证应用程序数据
|
||||
* @param offset: 写入偏移量
|
||||
* @param data: 原始数据指针
|
||||
* @param size: 数据大小
|
||||
* @retval true if verification passes, false otherwise
|
||||
*/
|
||||
bool OTAHandler::verifyApplicationData(u32 offset, u8 *data, u32 size) {
|
||||
u32 verify_addr = APP_START_ADDR + offset;
|
||||
|
||||
for (u32 i = 0; i < size; i++) {
|
||||
if (((u8 *)verify_addr)[i] != data[i]) {
|
||||
#if DEBUG_OTA
|
||||
printf("Data verification failed at offset %d, expected 0x%02X, "
|
||||
"got 0x%02X\r\n",
|
||||
i, data[i], ((u8 *)verify_addr)[i]);
|
||||
#endif
|
||||
return false;
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief 处理OTA数据包
|
||||
* @param id: CAN帧ID
|
||||
* @param data: CAN帧数据
|
||||
* @param len: CAN帧数据长度
|
||||
* @retval 无
|
||||
*/
|
||||
void OTAHandler::handleOTAMessage(u32 id, u8 *data, u8 len) {
|
||||
if (id == CANFD_OTA_DATA_ID) {
|
||||
ota_time = 0;
|
||||
@@ -125,43 +244,50 @@ void OTAHandler::handleOTAMessage(u32 id, u8 *data, u8 len) {
|
||||
#if DEBUG_OTA
|
||||
printf("stage8:OTA_RECEIVING_FIRMWARE\r\n");
|
||||
#endif
|
||||
// 数据解包
|
||||
u32 receive_session_num;
|
||||
u32 receive_packet_num;
|
||||
u8 receive_encrypted_data[OTA_PACKET_SIZE];
|
||||
u32 receive_IFEND;
|
||||
u8 receive_dataCRC32[4];
|
||||
receive_session_num = extract_u32_from_4bytes(data);
|
||||
receive_packet_num = extract_u32_from_4bytes(data + 4);
|
||||
for (int i = 0; i < 48; i++) {
|
||||
receive_encrypted_data[i] = data[i + 8];
|
||||
}
|
||||
receive_IFEND = extract_u32_from_4bytes(data + 56);
|
||||
for (int i = 0; i < 4; i++) {
|
||||
receive_dataCRC32[i] = data[i + 60];
|
||||
}
|
||||
// 使用安全的数据解包方式
|
||||
u32 receive_session_num = extract_u32_from_4bytes(data);
|
||||
u32 receive_packet_num = extract_u32_from_4bytes(data + 4);
|
||||
u32 receive_IFEND = extract_u32_from_4bytes(data + 56);
|
||||
|
||||
// 解密
|
||||
// 直接使用数据指针,避免不必要的内存复制
|
||||
const u8 *receive_encrypted_data =
|
||||
data + 8; // 加密数据从偏移8字节开始
|
||||
const u8 *receive_dataCRC32 = data + 60; // CRC数据从偏移60字节开始
|
||||
|
||||
// 解密 - 使用栈上数组存储解密结果
|
||||
u8 decrypted_data[OTA_PACKET_SIZE];
|
||||
u8 calculated_dataCRC32[4];
|
||||
aes128_set_key(&aes_ctx, current_K_session);
|
||||
aes128_ecb_decrypt_48bytes(&aes_ctx, receive_encrypted_data,
|
||||
aes128_ecb_decrypt_48bytes(&aes_ctx,
|
||||
const_cast<u8 *>(receive_encrypted_data),
|
||||
decrypted_data);
|
||||
|
||||
// 计算解密后CRC数据
|
||||
calculate_crc32_with_key(current_K_session, decrypted_data,
|
||||
calculated_dataCRC32);
|
||||
|
||||
// CRC数据匹配
|
||||
if (memcmp(receive_dataCRC32, calculated_dataCRC32, SIZE_U32) !=
|
||||
0) {
|
||||
ota_error = OTA_ERROR_INVALID_CRC;
|
||||
send_error_frame(OTA_ERROR_INVALID_CRC);
|
||||
return;
|
||||
receive_packet_num -= 1;
|
||||
} else {
|
||||
// 写入Flash前验证数据
|
||||
u32 offset = receive_packet_num * OTA_PACKET_SIZE;
|
||||
if (offset + OTA_PACKET_SIZE <= APP_MAX_SIZE) {
|
||||
writeApplicationData(offset, decrypted_data,
|
||||
OTA_PACKET_SIZE);
|
||||
} else {
|
||||
ota_error = OTA_ERROR_INVALID_SIZE;
|
||||
send_error_frame(OTA_ERROR_INVALID_SIZE);
|
||||
}
|
||||
}
|
||||
if (receive_IFEND == 0xFFFFFFFF) { // 接收到尾帧,发送结束信号
|
||||
u8 pack_number[4] = {0xFF, 0xFF, 0xFF, 0xFF};
|
||||
CAN0_transmiter.sendDATA_ACK(CANFD_OTA_ACK_ID, pack_number);
|
||||
ota_state = OTA_STATE_FINISHED;
|
||||
jumpToApplication();
|
||||
return;
|
||||
}
|
||||
// 下一个请求
|
||||
@@ -184,9 +310,9 @@ void OTAHandler::handleOTAMessage(u32 id, u8 *data, u8 len) {
|
||||
send_error_frame(OTA_ERROR_INVALID_SIZE);
|
||||
return;
|
||||
}
|
||||
// eraseApplicationArea();
|
||||
u8 send_data[6] = {0xBA, 0xC0, 0x00,
|
||||
0x00, 0x00, 0x00}; // 确认开始传输
|
||||
eraseApplicationArea();
|
||||
// 发送接收包0请求
|
||||
CAN0_transmiter.CANFD_AsyncSend(CANFD_OTA_ACK_ID, 6, send_data);
|
||||
ota_state = OTA_STATE_RECEIVING_FIRMWARE;
|
||||
|
||||
+5
-6
@@ -22,9 +22,9 @@ class OTAHandler {
|
||||
|
||||
private:
|
||||
u16 ota_time;
|
||||
bool validateApplication();
|
||||
void eraseApplicationArea();
|
||||
void writeApplicationData(uint32_t offset, uint8_t *data, uint32_t size);
|
||||
bool verifyApplicationData(u32 offset, u8 *data, u32 size);
|
||||
typedef enum {
|
||||
OTA_STATE_IDLE = 0,
|
||||
OTA_STATE_WAITING_RANDOM, // 第二阶段等待随机数
|
||||
@@ -36,17 +36,15 @@ class OTAHandler {
|
||||
typedef enum {
|
||||
OTA_ERROR_NONE = 0,
|
||||
OTA_ERROR_TIMEOUT,
|
||||
OTA_ERROR_FLASH_ERROR,
|
||||
OTA_ERROR_INVALID_KEY,
|
||||
OTA_ERROR_INVALID_PACKET,
|
||||
OTA_ERROR_INVALID_HANDSHAKE,
|
||||
OTA_ERROR_INVALID_CRC,
|
||||
OTA_ERROR_INVALID_SIZE
|
||||
} ota_error_t;
|
||||
// 全局变量(需在安全区域存储)
|
||||
uint8_t K_master[16] = {0x01, 0x23, 0x45, 0x67, 0x89, 0xAB,
|
||||
0xCD, 0xEF, 0xFE, 0xDC, 0xBA, 0x98,
|
||||
0x76, 0x54, 0x32, 0x10}; // 预置主密钥
|
||||
uint8_t device_uid[12]; // 从GD32 UID寄存器读取
|
||||
|
||||
uint8_t device_uid[12]; // 从GD32 UID寄存器读取
|
||||
uint8_t current_K_session[16];
|
||||
uint8_t nonce_server[16];
|
||||
|
||||
@@ -58,4 +56,5 @@ void getDeviceSerialNumber(uint8_t *buffer);
|
||||
void calculate_crc32_with_key(const uint8_t *key, const uint8_t *data,
|
||||
u8 *output);
|
||||
void send_error_frame(uint8_t error);
|
||||
bool validateApplication();
|
||||
#endif
|
||||
@@ -52,7 +52,7 @@ const u8 inv_sbox[256] = {
|
||||
|
||||
// 轮常量
|
||||
static const u8 rcon[10] = {0x01, 0x02, 0x04, 0x08, 0x10,
|
||||
0x20, 0x40, 0x80, 0x1b, 0x36};
|
||||
0x20, 0x40, 0x80, 0x1b, 0x36};
|
||||
|
||||
// 伽罗瓦域乘法 (xtime) - 确保正确
|
||||
static u8 xtime(u8 x) { return (x << 1) ^ (((x >> 7) & 1) * 0x1b); }
|
||||
@@ -281,8 +281,7 @@ void aes128_set_key(aes128_context *ctx, const u8 *key) {
|
||||
}
|
||||
|
||||
// 加密单个块
|
||||
void aes128_encrypt_block(aes128_context *ctx, const u8 *input,
|
||||
u8 *output) {
|
||||
void aes128_encrypt_block(aes128_context *ctx, const u8 *input, u8 *output) {
|
||||
// 复制输入到状态
|
||||
memcpy(ctx->state, input, AES_BLOCK_SIZE);
|
||||
|
||||
@@ -316,8 +315,7 @@ void aes128_ecb_encrypt_48bytes(aes128_context *ctx, const u8 *input,
|
||||
}
|
||||
|
||||
// 解密单个块
|
||||
void aes128_decrypt_block(aes128_context *ctx, const u8 *input,
|
||||
u8 *output) {
|
||||
void aes128_decrypt_block(aes128_context *ctx, const u8 *input, u8 *output) {
|
||||
// 复制输入到状态
|
||||
memcpy(ctx->state, input, AES_BLOCK_SIZE);
|
||||
|
||||
@@ -351,8 +349,8 @@ void aes128_ecb_decrypt_48bytes(aes128_context *ctx, const u8 *input,
|
||||
}
|
||||
|
||||
// 验证函数:加密后解密,对比原始数据
|
||||
u8 aes128_verify_encryption(aes128_context *ctx,
|
||||
const u8 *original_data, size_t length) {
|
||||
u8 aes128_verify_encryption(aes128_context *ctx, const u8 *original_data,
|
||||
size_t length) {
|
||||
u8 temp_ciphertext[48];
|
||||
u8 temp_plaintext[48];
|
||||
u8 result = 1; // 1表示验证成功,0表示失败
|
||||
|
||||
@@ -11,9 +11,9 @@
|
||||
|
||||
// AES 状态结构
|
||||
typedef struct {
|
||||
u8 key[AES128_KEY_LEN]; // 128位/16字节密钥
|
||||
u8 round_keys[11][16]; // 10轮 + 初始轮密钥
|
||||
u8 state[AES_BLOCK_SIZE]; // 状态矩阵
|
||||
u8 key[AES128_KEY_LEN]; // 128位/16字节密钥
|
||||
u8 round_keys[11][16]; // 10轮 + 初始轮密钥
|
||||
u8 state[AES_BLOCK_SIZE]; // 状态矩阵
|
||||
} aes128_context;
|
||||
|
||||
// 内部函数声明(需要在头文件中声明,因为解密函数会使用)
|
||||
@@ -28,10 +28,13 @@ void inv_mix_columns(u8 *state);
|
||||
// 公共函数声明
|
||||
void aes128_set_key(aes128_context *ctx, const u8 *key);
|
||||
void aes128_encrypt_block(aes128_context *ctx, const u8 *input, u8 *output);
|
||||
void aes128_ecb_encrypt_48bytes(aes128_context *ctx, const u8 *input, u8 *output);
|
||||
void aes128_ecb_encrypt_48bytes(aes128_context *ctx, const u8 *input,
|
||||
u8 *output);
|
||||
void aes128_decrypt_block(aes128_context *ctx, const u8 *input, u8 *output);
|
||||
void aes128_ecb_decrypt_48bytes(aes128_context *ctx, const u8 *input, u8 *output);
|
||||
u8 aes128_verify_encryption(aes128_context *ctx, const u8 *original_data, size_t length);
|
||||
void aes128_ecb_decrypt_48bytes(aes128_context *ctx, const u8 *input,
|
||||
u8 *output);
|
||||
u8 aes128_verify_encryption(aes128_context *ctx, const u8 *original_data,
|
||||
size_t length);
|
||||
|
||||
// 逆S盒 (用于解密)
|
||||
extern const u8 inv_sbox[256];
|
||||
|
||||
+6
-14
@@ -4,6 +4,7 @@
|
||||
|
||||
#define EEPDEBUG 0
|
||||
#define DEBUG_OTA 0
|
||||
#define CAN_DEBUG 0
|
||||
|
||||
// Flash分区配置
|
||||
constexpr u32 BOOTLOADER_SIZE = 0x4000; // 16KB
|
||||
@@ -25,11 +26,6 @@ constexpr u32 DEVICE_SERIAL_ADDR_LSB = 0x1FFFF7F0; // 最低32位
|
||||
|
||||
// CANFD数据段最大64字节,我们用1字节序号 + 48字节数据 + 1字节CRC校验
|
||||
constexpr u32 OTA_PACKET_SIZE = 48;
|
||||
typedef struct {
|
||||
u32 packet_num; // 包序号
|
||||
u8 data[OTA_PACKET_SIZE]; // 48字节有效数据
|
||||
u32 crc32; // 简单校验
|
||||
} canfd_ota_packet_t;
|
||||
|
||||
// 定义一个联合体类型用于float和byte数组之间的转换
|
||||
typedef union {
|
||||
@@ -37,15 +33,11 @@ typedef union {
|
||||
u8 b[4];
|
||||
} u32_union_t;
|
||||
|
||||
// 添加辅助函数用于从u8数组中解包float类型数据
|
||||
static inline u32_union_t unpack_bytes_to_float(u8 *buffer, u16 offset) {
|
||||
u32_union_t u32_union;
|
||||
u32_union.b[0] = buffer[offset];
|
||||
u32_union.b[1] = buffer[offset + 1];
|
||||
u32_union.b[2] = buffer[offset + 2];
|
||||
u32_union.b[3] = buffer[offset + 3];
|
||||
return u32_union;
|
||||
}
|
||||
|
||||
// 全局变量(需在安全区域存储)
|
||||
constexpr uint8_t K_master[16] = {0x01, 0x23, 0x45, 0x67, 0x89, 0xAB,
|
||||
0xCD, 0xEF, 0xFE, 0xDC, 0xBA, 0x98,
|
||||
0x76, 0x54, 0x32, 0x10}; // 预置主密钥
|
||||
// 其他
|
||||
constexpr u32 OTA_TIMEOUT = 10000;
|
||||
constexpr u8 SIZE_U32 = sizeof(uint32_t);
|
||||
|
||||
+115
-1
@@ -33,4 +33,118 @@ void unused_pin_init() {
|
||||
gpio_bit_reset(GPIOA, GPIO_PIN_8 | GPIO_PIN_11 | GPIO_PIN_12);
|
||||
gpio_bit_reset(GPIOB, GPIO_PIN_6 | GPIO_PIN_7 | GPIO_PIN_15);
|
||||
gpio_bit_reset(GPIOC, GPIO_PIN_13 | GPIO_PIN_14 | GPIO_PIN_15);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
// // 发送单个字节到串口 (用于调试)
|
||||
// void uart_send_byte(uint8_t data) {
|
||||
// // 实际的串口发送代码
|
||||
// while (RESET == usart_flag_get(USART0, USART_FLAG_TBE))
|
||||
// ;
|
||||
// usart_data_transmit(USART0, data);
|
||||
// }
|
||||
|
||||
// // 以十六进制格式打印数据
|
||||
// // 安全的十六进制打印函数,不使用sprintf,不使用大缓冲区
|
||||
// void safe_print_hex(const char *prefix, const uint8_t *data, size_t length) {
|
||||
// // 1. 先打印前缀
|
||||
// const char *p = prefix;
|
||||
// while (*p) {
|
||||
// while (RESET == usart_flag_get(USART0, USART_FLAG_TBE))
|
||||
// ;
|
||||
// usart_data_transmit(USART0, *p++);
|
||||
// }
|
||||
|
||||
// while (RESET == usart_flag_get(USART0, USART_FLAG_TBE))
|
||||
// ;
|
||||
// usart_data_transmit(USART0, ':');
|
||||
// while (RESET == usart_flag_get(USART0, USART_FLAG_TBE))
|
||||
// ;
|
||||
// usart_data_transmit(USART0, ' ');
|
||||
|
||||
// // 2. 逐字节打印数据,每16字节换行
|
||||
// for (size_t i = 0; i < length; i++) {
|
||||
// uint8_t byte = data[i];
|
||||
|
||||
// // 打印高4位
|
||||
// uint8_t high = (byte >> 4) & 0x0F;
|
||||
// char high_char = (high < 10) ? ('0' + high) : ('A' + high - 10);
|
||||
// while (RESET == usart_flag_get(USART0, USART_FLAG_TBE))
|
||||
// ;
|
||||
// usart_data_transmit(USART0, high_char);
|
||||
|
||||
// // 打印低4位
|
||||
// uint8_t low = byte & 0x0F;
|
||||
// char low_char = (low < 10) ? ('0' + low) : ('A' + low - 10);
|
||||
// while (RESET == usart_flag_get(USART0, USART_FLAG_TBE))
|
||||
// ;
|
||||
// usart_data_transmit(USART0, low_char);
|
||||
|
||||
// // 打印空格
|
||||
// while (RESET == usart_flag_get(USART0, USART_FLAG_TBE))
|
||||
// ;
|
||||
// usart_data_transmit(USART0, ' ');
|
||||
|
||||
// // 每16字节换行
|
||||
// if ((i + 1) % 16 == 0 && i < length - 1) {
|
||||
// // 换行
|
||||
// while (RESET == usart_flag_get(USART0, USART_FLAG_TBE))
|
||||
// ;
|
||||
// usart_data_transmit(USART0, '\r');
|
||||
// while (RESET == usart_flag_get(USART0, USART_FLAG_TBE))
|
||||
// ;
|
||||
// usart_data_transmit(USART0, '\n');
|
||||
|
||||
// // 缩进(4个空格)
|
||||
// for (int j = 0; j < 4; j++) {
|
||||
// while (RESET == usart_flag_get(USART0, USART_FLAG_TBE))
|
||||
// ;
|
||||
// usart_data_transmit(USART0, ' ');
|
||||
// }
|
||||
// }
|
||||
// }
|
||||
|
||||
// // 3. 打印换行
|
||||
// while (RESET == usart_flag_get(USART0, USART_FLAG_TBE))
|
||||
// ;
|
||||
// usart_data_transmit(USART0, '\r');
|
||||
// while (RESET == usart_flag_get(USART0, USART_FLAG_TBE))
|
||||
// ;
|
||||
// usart_data_transmit(USART0, '\n');
|
||||
// }
|
||||
|
||||
// void test_decrypt() {
|
||||
// printf("AES Encryption Verification Starting...\r\n");
|
||||
|
||||
// // 定义48字节测试数据
|
||||
// uint8_t plaintext[48] = {
|
||||
// 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0A, 0x0B,
|
||||
// 0x0C, 0x0D, 0x0E, 0x0F, 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17,
|
||||
// 0x18, 0x19, 0x1A, 0x1B, 0x1C, 0x1D, 0x1E, 0x1F, 0x20, 0x21, 0x22, 0x23,
|
||||
// 0x24, 0x25, 0x26, 0x27, 0x28, 0x29, 0x2A, 0x2B, 0x2C, 0x2D, 0x2E, 0x2F};
|
||||
|
||||
// // 定义128位密钥 (16字节)
|
||||
// uint8_t key[16] = {0x2b, 0x7e, 0x15, 0x16, 0x28, 0xae, 0xd2, 0xa6,
|
||||
// 0xab, 0xf7, 0x15, 0x88, 0x09, 0xcf, 0x4f, 0x3c};
|
||||
|
||||
// // 创建AES上下文
|
||||
// aes128_context ctx;
|
||||
|
||||
// // 设置密钥
|
||||
// aes128_set_key(&ctx, key);
|
||||
|
||||
// // 打印原始数据
|
||||
// safe_print_hex("Original Data", plaintext, 48);
|
||||
|
||||
// uint8_t ciphertext[48];
|
||||
// uint8_t decrypted[48];
|
||||
|
||||
// // 加密
|
||||
// aes128_ecb_encrypt_48bytes(&ctx, plaintext, ciphertext);
|
||||
// safe_print_hex("Encrypted Data", ciphertext, 48);
|
||||
|
||||
// // 解密
|
||||
// aes128_ecb_decrypt_48bytes(&ctx, ciphertext, decrypted);
|
||||
// safe_print_hex("Decrypted Data", decrypted, 48);
|
||||
// }
|
||||
|
||||
@@ -57,18 +57,6 @@ void systick_config(void) {
|
||||
/* configure the systick handler priority */
|
||||
NVIC_SetPriority(SysTick_IRQn, 0x00U);
|
||||
}
|
||||
/*!
|
||||
\brief delay a time in seconds
|
||||
\param[in] count: count in seconds
|
||||
\param[out] none
|
||||
\retval none
|
||||
*/
|
||||
void delay_1s(uint32_t count) {
|
||||
uint32_t i;
|
||||
for (i = 0; i < count; i++) {
|
||||
delay_1ms(1000);
|
||||
}
|
||||
}
|
||||
|
||||
/*!
|
||||
\brief delay a time in milliseconds
|
||||
@@ -79,8 +67,6 @@ void delay_1s(uint32_t count) {
|
||||
void delay_1ms(uint32_t count) {
|
||||
delay = count;
|
||||
while (0U != delay) {
|
||||
fwdgt_counter_reload();
|
||||
fwdgt_counter_reload();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user