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>
|
|
|
|
|
|
|
|
|
|
/* 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-03 18:00:28 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// // 发送单个字节到串口 (用于调试)
|
|
|
|
|
// 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);
|
|
|
|
|
// }
|