fc10ef2e10
GD32F470ZGT6 based project with full peripheral support: - SDRAM (W9825G6KH) via EXMC - LCD NT35510 (480x800) via EXMC NOR/PSRAM - Flash (W25Q64) via SPI - UART (USART0) debug console - LED indicators - CMSIS-DAP debug interface - CMake + ARM GCC toolchain build system
118 lines
3.1 KiB
C
118 lines
3.1 KiB
C
#include "gd32f4xx.h"
|
||
#include "systick.h"
|
||
#include "../../libs/thirdparty/GD32F4xx_Official/LCD_TOUCH_MCU_LiangShanPi/Hardware/usart0/usart0.h"
|
||
#include <stdio.h>
|
||
|
||
// LED引脚定义 - 梁山Pi开发板使用PD7作为LED1
|
||
#define LED_PORT GPIOD
|
||
#define LED_PIN GPIO_PIN_7
|
||
|
||
// 简单软件延时函数
|
||
static void software_delay_ms(uint32_t ms)
|
||
{
|
||
// 基于16MHz时钟的粗略延时
|
||
for(volatile uint32_t i = 0; i < ms * 8000; i++);
|
||
}
|
||
|
||
// LED闪烁函数
|
||
static void led_blink(uint8_t times, uint16_t on_ms, uint16_t off_ms)
|
||
{
|
||
for(int i = 0; i < times; i++) {
|
||
gpio_bit_set(LED_PORT, LED_PIN);
|
||
software_delay_ms(on_ms);
|
||
gpio_bit_reset(LED_PORT, LED_PIN);
|
||
software_delay_ms(off_ms);
|
||
}
|
||
}
|
||
|
||
// 直接串口发送函数(不使用printf)
|
||
static void uart_send_string(const char *str)
|
||
{
|
||
while(*str) {
|
||
usart_data_transmit(USART0, (uint8_t)(*str));
|
||
while(RESET == usart_flag_get(USART0, USART_FLAG_TBE));
|
||
str++;
|
||
}
|
||
}
|
||
|
||
int main(void)
|
||
{
|
||
// ===== 第1步:时钟初始化 =====
|
||
// 注意:hardware_init.c应该在启动时已调用SystemInit()
|
||
// 确保系统时钟为16MHz
|
||
|
||
// ===== 第2步:LED初始化 =====
|
||
// 启用GPIOD时钟
|
||
rcu_periph_clock_enable(RCU_GPIOD);
|
||
|
||
// 配置PD7为推挽输出
|
||
gpio_mode_set(LED_PORT, GPIO_MODE_OUTPUT, GPIO_PUPD_NONE, LED_PIN);
|
||
gpio_output_options_set(LED_PORT, GPIO_OTYPE_PP, GPIO_OSPEED_50MHZ, LED_PIN);
|
||
|
||
// 初始状态:LED灭
|
||
gpio_bit_reset(LED_PORT, LED_PIN);
|
||
|
||
// ===== 第3步:LED测试 =====
|
||
// 快速闪烁3次确认系统正常运行
|
||
led_blink(3, 100, 100);
|
||
software_delay_ms(500);
|
||
|
||
// ===== 第4步:串口初始化 =====
|
||
// 初始化串口调试输出(USART0, PA9/PA10, 9600 baud)
|
||
usart0_init();
|
||
|
||
// ===== 第5步:串口测试 =====
|
||
// 发送测试字符串
|
||
uart_send_string("\r\n\r\n=== 最小系统测试程序 ===\r\n");
|
||
uart_send_string("系统时钟: 16MHz IRC16M\r\n");
|
||
uart_send_string("串口波特率: 9600\r\n");
|
||
uart_send_string("LED引脚: PD7\r\n");
|
||
uart_send_string("开始循环测试...\r\n");
|
||
|
||
// ===== 第6步:SysTick初始化 =====
|
||
// 初始化SysTick定时器(1ms中断)
|
||
systick_config();
|
||
|
||
// 等待SysTick稳定
|
||
for(volatile uint32_t i = 0; i < 100000; i++);
|
||
|
||
// ===== 第7步:主循环 =====
|
||
// 循环发送状态信息并闪烁LED
|
||
uint32_t counter = 0;
|
||
while(1) {
|
||
counter++;
|
||
|
||
// 发送状态信息
|
||
char msg[64];
|
||
// 使用简单的字符拼接,避免sprintf
|
||
uart_send_string("计数: ");
|
||
|
||
// 发送十进制计数器
|
||
uint32_t temp = counter;
|
||
char buf[12];
|
||
int idx = 0;
|
||
|
||
if(temp == 0) {
|
||
uart_send_string("0");
|
||
} else {
|
||
// 反向构建数字字符串
|
||
while(temp > 0) {
|
||
buf[idx++] = '0' + (temp % 10);
|
||
temp /= 10;
|
||
}
|
||
// 反向发送
|
||
while(idx > 0) {
|
||
usart_data_transmit(USART0, (uint8_t)buf[--idx]);
|
||
while(RESET == usart_flag_get(USART0, USART_FLAG_TBE));
|
||
}
|
||
}
|
||
|
||
uart_send_string(" - LED闪烁\r\n");
|
||
|
||
// 闪烁LED
|
||
led_blink(1, 200, 200);
|
||
|
||
// 延时2秒
|
||
for(volatile uint32_t i = 0; i < 2000000; i++);
|
||
}
|
||
} |