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:
+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