Files
GD32_BOOT/Robot_V2/sys/initial.cpp
T

248 lines
6.9 KiB
C++
Raw Normal View History

2025-12-25 12:07:06 +08:00
#include "initial.h"
#include "gd32c10x.h"
#include "gd32c10x_gpio.h"
#include "gd32c10x_rcu.h"
#include <cstdio>
#include <stdint.h>
#include <stdio.h>
2026-01-05 00:52:20 +08:00
#include "config.h"
2025-12-25 12:07:06 +08:00
/* retarget the C library printf function to the USART */
u8 USART0_init() {
gd_eval_com_init(EVAL_COM0);
return 1;
}
int fputc(int ch, FILE *f) throw() {
usart_data_transmit(EVAL_COM0, (uint8_t)ch);
while (RESET == usart_flag_get(EVAL_COM0, USART_FLAG_TBE))
;
return ch;
}
void unused_pin_init() {
rcu_periph_clock_enable(RCU_GPIOA); // 使能GPIOA时钟
rcu_periph_clock_enable(RCU_GPIOB); // 使能GPIOB时钟
rcu_periph_clock_enable(RCU_GPIOC); // 使能GPIOC时钟
gpio_init(GPIOA, GPIO_MODE_IPU, GPIO_OSPEED_2MHZ,
GPIO_PIN_8 | GPIO_PIN_11 | GPIO_PIN_12);
gpio_init(GPIOB, GPIO_MODE_IPU, GPIO_OSPEED_2MHZ,
GPIO_PIN_6 | GPIO_PIN_7 | GPIO_PIN_15);
gpio_init(GPIOC, GPIO_MODE_IPU, GPIO_OSPEED_2MHZ,
GPIO_PIN_13 | GPIO_PIN_14 | GPIO_PIN_15);
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);
}
2026-01-05 00:52:20 +08:00
/**
* @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 = APPLATEST_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 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);
// }