 |
1楼
巨大八爪鱼
2026-3-8 17:16
標準庫:GD32F4xx_Firmware_Library_V3.3.3.7z - 7-Zip 壓縮文件, 解包大小為 19,224,511 字節 
|
 |
7楼
巨大八爪鱼
2026-3-8 17:18
【main.c】 #include <gd32f4xx.h> #include <stdio.h> #include "common.h"
int main(void) { int i = 0; sys_now_init(); usart_init(115200); printf("GD32F470ZI USART0\n"); printf("SystemCoreClock=%u\n", SystemCoreClock); while (1) { printf("Shujen Chang is a gay. i=%d\n", i); i++; delay(1000); } }
|
 |
8楼
巨大八爪鱼
2026-3-8 17:19
【common.c】 #include <gd32f4xx.h> #include <stdio.h> #include <stdlib.h> #include "common.h"
#ifdef __MICROLIB #warning "Please do NOT use MicroLib" #endif
#pragma import(__use_no_semihosting) // 禁用半主機模式 (不然調用printf就會進HardFault)
FILE __stdout = {1}; FILE __stderr = {2}; static uint32_t sys_ticks;
/* main函數返回時執行的函數 */ void _sys_exit(int returncode) { printf("Exited! returncode=%d\n", returncode); while (1); }
void _ttywrch(int ch) { if (ch == '\n') { while (usart_flag_get(USART0, USART_FLAG_TBE) == RESET); // 等待前一字符發送完畢 usart_data_transmit(USART0, '\r'); } while (usart_flag_get(USART0, USART_FLAG_TBE) == RESET); usart_data_transmit(USART0, ch); }
/* 等待串口發送端空閒 */ // LWIP_ASSERT會調用此函數 int fflush(FILE *stream) { switch (stream->handle) { case 1: case 2: while (usart_flag_get(USART0, USART_FLAG_TC) == RESET); return 0; default: return -1; } }
/* printf和perror重定向到串口 */ int fputc(int ch, FILE *fp) { if (fp->handle == 1 || fp->handle == 2) { _ttywrch(ch); return ch; } return EOF; }
/* 延時n毫秒 (不精確) */ // 實際延遲的時間t: nms<t<=nms+1 void delay(uint16_t nms) { uint32_t diff, start; start = sys_now(); do { diff = sys_now() - start; } while (diff <= nms); }
/* 顯示數據內容 */ void dump_data(const void *data, int len) { const uint8_t *p = data; while (len--) printf("%02X", *p++); printf("\n"); }
/* 獲取系統時間毫秒數 (lwip協議棧要求實現的函數) */ // 該函數必須保證: 除非定時器溢出, 否則後獲取的時間必須大於先獲取的時間 uint32_t sys_now(void) { return sys_ticks; }
/* 初始化系統時間毫秒計數器 */ void sys_now_init(void) { SysTick_Config(SystemCoreClock / 1000); }
/* 初始化串口 */ void usart_init(int baud_rate) { rcu_periph_clock_enable(RCU_GPIOA); rcu_periph_clock_enable(RCU_USART0); // 串口發送引腳PA9設為復用推輓輸出, 串口接收引腳PA10保持默認的浮空輸入 gpio_af_set(GPIOA, GPIO_AF_7, GPIO_PIN_9); gpio_mode_set(GPIOA, GPIO_MODE_AF, GPIO_PUPD_NONE, GPIO_PIN_9); gpio_output_options_set(GPIOA, GPIO_OTYPE_PP, GPIO_OSPEED_50MHZ, GPIO_PIN_9); gpio_af_set(GPIOA, GPIO_AF_7, GPIO_PIN_10); gpio_mode_set(GPIOA, GPIO_MODE_AF, GPIO_PUPD_PULLUP, GPIO_PIN_10); gpio_output_options_set(GPIOA, GPIO_OTYPE_PP, GPIO_OSPEED_50MHZ, GPIO_PIN_10); usart_baudrate_set(USART0, baud_rate); usart_stop_bit_set(USART0, USART_STB_1BIT); usart_word_length_set(USART0, USART_WL_8BIT); usart_parity_config(USART0, USART_PM_NONE); usart_transmit_config(USART0, USART_TRANSMIT_ENABLE); usart_receive_config(USART0, USART_RECEIVE_ENABLE); usart_enable(USART0); }
void HardFault_Handler(void) { printf("Hard Error!\n"); while (1); }
void SysTick_Handler(void) { sys_ticks++; }
【common.h】 #ifndef _COMMON_H #define _COMMON_H
struct __FILE { int handle; };
void delay(uint16_t nms); void dump_data(const void *data, int len); uint32_t sys_now(void); void sys_now_init(void); void usart_init(int baud_rate);
#endif
|
 |
9楼
巨大八爪鱼
2026-3-8 17:21
程序運行結果: 
|