generated from hulk/gd32f4xx_template_cmake_vscode
Initial commit
This commit is contained in:
@@ -0,0 +1,104 @@
|
||||
#ifndef BOARD_CONFIG_H
|
||||
#define BOARD_CONFIG_H
|
||||
|
||||
#ifndef CFG_DISABLE
|
||||
#define CFG_DISABLE 0
|
||||
#endif
|
||||
|
||||
#ifndef CFG_ENABLE
|
||||
#define CFG_ENABLE 1
|
||||
#endif
|
||||
|
||||
#define PROTOCOL_BOARD_TYPE 0x01 /**< 板卡类型标识 */
|
||||
|
||||
#include "version.h"
|
||||
|
||||
/* GD32_FLASH_SIZE_KB(512/1024/2048)由 CMake 按 GD32_FLASH_CAPACITY 注入,
|
||||
切换容量:cmake --preset Debug -DGD32_FLASH_CAPACITY=xG */
|
||||
|
||||
/* >>>>>>>>>>>>>>>>>>>>[IIC TYPE DEFINE]<<<<<<<<<<<<<<<<<<<< */
|
||||
|
||||
#define SOFTWARE_IIC CFG_DISABLE // CFG_DISABLE: Hardware IIC; CFG_ENABLE: Software IIC
|
||||
/* TODO: I2C 驱动尚未从 GD32E23x 工程移植,先保证工程可编译(见 AGENTS.md 进度清单) */
|
||||
|
||||
/* >>>>>>>>>>>>>>>>>>>>[DEBUG MODE]<<<<<<<<<<<<<<<<<<<< */
|
||||
|
||||
#define DEBUG_MODE CFG_ENABLE // CFG_DISABLE: Release Mode; CFG_ENABLE: Debug Mode(发布/量产前改回 CFG_DISABLE)
|
||||
|
||||
/* >>>>>>>>>>>>>>>>>>>>[COMMAND DEBUG]<<<<<<<<<<<<<<<<<<<< */
|
||||
|
||||
#define COM_DEBUG CFG_DISABLE // CFG_DISABLE: Command debug off; CFG_ENABLE: Command debug on
|
||||
/* TODO: 串口命令协议模块(command.c)尚未移植(见 AGENTS.md 进度清单) */
|
||||
|
||||
/* >>>>>>>>>>>>>>>>>>>>[DEBUG ASSERTIONS DEFINE]<<<<<<<<<<<<<<<<<<<< */
|
||||
|
||||
#define DEBUG_VERBOSE CFG_DISABLE // CFG_DISABLE: Verbose debug off; CFG_ENABLE: Verbose debug on
|
||||
|
||||
/* >>>>>>>>>>>>>>>>>>>>[SEGGER RTT DETECTION]<<<<<<<<<<<<<<<<<<<< */
|
||||
|
||||
#define SEGGER_RTT_DETECTION CFG_ENABLE // CFG_DISABLE: SEGGER RTT off; CFG_ENABLE: SEGGER RTT on
|
||||
|
||||
#if SEGGER_RTT_DETECTION == CFG_ENABLE
|
||||
#include "SEGGER_RTT.h"
|
||||
#define RTT_printf(ch, ...) SEGGER_RTT_printf(ch, __VA_ARGS__)
|
||||
#define RTT_WriteString(ch, s) SEGGER_RTT_WriteString(ch, s)
|
||||
#define RTT_PutChar(ch, c) SEGGER_RTT_PutChar(ch, c)
|
||||
#else
|
||||
#define RTT_printf(ch, ...)
|
||||
#define RTT_WriteString(ch, s)
|
||||
#define RTT_PutChar(ch, c)
|
||||
#endif
|
||||
|
||||
/* >>>>>>>>>>>>>>>>>>>>[RS485 MODE]<<<<<<<<<<<<<<<<<<<< */
|
||||
|
||||
#define RS485_MODE CFG_DISABLE // CFG_DISABLE: RS485 off; CFG_ENABLE: RS485 on
|
||||
/* GD32F4xx USART 无硬件 RS485 DE 驱动(E23x 特有),DE 引脚由 GPIO 手动控制 */
|
||||
|
||||
#if RS485_MODE == CFG_ENABLE
|
||||
#define RS485_DE_RCU RCU_GPIOA
|
||||
#define RS485_DE_PORT GPIOA
|
||||
#define RS485_DE_PIN GPIO_PIN_1 // 根据实际硬件修改 DE 引脚
|
||||
#endif
|
||||
|
||||
/******************************************************************************/
|
||||
|
||||
/* 调试串口:USART1(PA2 TX / PA3 RX,AF7),纯串口,非 RS485。
|
||||
注意 GD32F4xx 外设编号比 STM32 整体小 1:PA2/PA3 是 USART1(=STM32 的 USART2),
|
||||
PA9/PA10 才是 USART0(=STM32 的 USART1) */
|
||||
#define UART_RCU RCU_USART1
|
||||
#define UART_PHY USART1
|
||||
#define UART_IRQ USART1_IRQn
|
||||
#define UART_GPIO_RCU RCU_GPIOA
|
||||
#define UART_GPIO_PORT GPIOA
|
||||
#define UART_TX_PIN GPIO_PIN_2
|
||||
#define UART_RX_PIN GPIO_PIN_3
|
||||
#define UART_BAUDRATE 115200U
|
||||
|
||||
/* USART中断处理函数声明 */
|
||||
void usart0_irq_handler(void);
|
||||
void usart1_irq_handler(void);
|
||||
void usart2_irq_handler(void);
|
||||
|
||||
/******************************************************************************/
|
||||
|
||||
/* I2C0: PB6 SCL / PB7 SDA, AF4 */
|
||||
#define RCU_GPIO_I2C RCU_GPIOB
|
||||
#define RCU_I2C RCU_I2C0
|
||||
#define I2C_SCL_PORT GPIOB
|
||||
#define I2C_SCL_PIN GPIO_PIN_6
|
||||
#define I2C_SDA_PORT GPIOB
|
||||
#define I2C_SDA_PIN GPIO_PIN_7
|
||||
#define I2C_GPIO_AF GPIO_AF_4
|
||||
|
||||
/******************************************************************************/
|
||||
|
||||
/* LED: PA15(demo 板实测引脚,低电平点亮按原理图确认) */
|
||||
#define LED_RCU RCU_GPIOA
|
||||
#define LED_PORT GPIOA
|
||||
#define LED_PIN GPIO_PIN_15
|
||||
|
||||
/******************************************************************************/
|
||||
|
||||
#define DEBUG_UART USART1
|
||||
|
||||
#endif //BOARD_CONFIG_H
|
||||
@@ -0,0 +1,58 @@
|
||||
/*!
|
||||
\file gd32f4xx_it.h
|
||||
\brief the header file of the ISR
|
||||
|
||||
\version 2025-02-10, V2.4.0, demo for GD32F4xx
|
||||
*/
|
||||
|
||||
/*
|
||||
Copyright (c) 2025, GigaDevice Semiconductor Inc.
|
||||
|
||||
Redistribution and use in source and binary forms, with or without modification,
|
||||
are permitted provided that the following conditions are met:
|
||||
|
||||
1. Redistributions of source code must retain the above copyright notice, this
|
||||
list of conditions and the following disclaimer.
|
||||
2. Redistributions in binary form must reproduce the above copyright notice,
|
||||
this list of conditions and the following disclaimer in the documentation
|
||||
and/or other materials provided with the distribution.
|
||||
3. Neither the name of the copyright holder nor the names of its contributors
|
||||
may be used to endorse or promote products derived from this software without
|
||||
specific prior written permission.
|
||||
|
||||
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
|
||||
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
|
||||
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
|
||||
IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
|
||||
INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
|
||||
NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
|
||||
PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
|
||||
WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
|
||||
ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY
|
||||
OF SUCH DAMAGE.
|
||||
*/
|
||||
|
||||
#ifndef GD32F4XX_IT_H
|
||||
#define GD32F4XX_IT_H
|
||||
|
||||
#include "gd32f4xx.h"
|
||||
|
||||
/* function declarations */
|
||||
/* this function handles NMI exception */
|
||||
void NMI_Handler(void);
|
||||
/* this function handles HardFault exception */
|
||||
void HardFault_Handler(void);
|
||||
/* this function handles MemManage exception */
|
||||
void MemManage_Handler(void);
|
||||
/* this function handles BusFault exception */
|
||||
void BusFault_Handler(void);
|
||||
/* this function handles UsageFault exception */
|
||||
void UsageFault_Handler(void);
|
||||
/* this function handles SVC exception */
|
||||
void SVC_Handler(void);
|
||||
/* this function handles PendSV exception */
|
||||
void PendSV_Handler(void);
|
||||
/* this function handles SysTick exception */
|
||||
void SysTick_Handler(void);
|
||||
|
||||
#endif /* GD32F4XX_IT_H */
|
||||
@@ -0,0 +1,78 @@
|
||||
/*!
|
||||
\file gd32f4xx_libopt.h
|
||||
\brief library optional for gd32f4xx
|
||||
|
||||
\version 2026-02-12, V3.3.3, demo for GD32F4xx
|
||||
*/
|
||||
|
||||
/*
|
||||
Copyright (c) 2026, GigaDevice Semiconductor Inc.
|
||||
|
||||
Redistribution and use in source and binary forms, with or without modification,
|
||||
are permitted provided that the following conditions are met:
|
||||
|
||||
1. Redistributions of source code must retain the above copyright notice, this
|
||||
list of conditions and the following disclaimer.
|
||||
2. Redistributions in binary form must reproduce the above copyright notice,
|
||||
this list of conditions and the following disclaimer in the documentation
|
||||
and/or other materials provided with the distribution.
|
||||
3. Neither the name of the copyright holder nor the names of its contributors
|
||||
may be used to endorse or promote products derived from this software without
|
||||
specific prior written permission.
|
||||
|
||||
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
|
||||
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
|
||||
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
|
||||
IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
|
||||
INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
|
||||
NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
|
||||
PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
|
||||
WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
|
||||
ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY
|
||||
OF SUCH DAMAGE.
|
||||
*/
|
||||
|
||||
#ifndef GD32F4XX_LIBOPT_H
|
||||
#define GD32F4XX_LIBOPT_H
|
||||
|
||||
#if defined (GD32F450) || defined (GD32F405) || defined (GD32F407) || defined (GD32F470) || defined (GD32F425) || defined (GD32F427)
|
||||
#include "gd32f4xx_rcu.h"
|
||||
#include "gd32f4xx_adc.h"
|
||||
#include "gd32f4xx_can.h"
|
||||
#include "gd32f4xx_crc.h"
|
||||
#include "gd32f4xx_ctc.h"
|
||||
#include "gd32f4xx_dac.h"
|
||||
#include "gd32f4xx_dbg.h"
|
||||
#include "gd32f4xx_dci.h"
|
||||
#include "gd32f4xx_dma.h"
|
||||
#include "gd32f4xx_exti.h"
|
||||
#include "gd32f4xx_fmc.h"
|
||||
#include "gd32f4xx_fwdgt.h"
|
||||
#include "gd32f4xx_gpio.h"
|
||||
#include "gd32f4xx_syscfg.h"
|
||||
#include "gd32f4xx_i2c.h"
|
||||
#include "gd32f4xx_iref.h"
|
||||
#include "gd32f4xx_pmu.h"
|
||||
#include "gd32f4xx_rtc.h"
|
||||
#include "gd32f4xx_sdio.h"
|
||||
#include "gd32f4xx_spi.h"
|
||||
#include "gd32f4xx_timer.h"
|
||||
#include "gd32f4xx_trng.h"
|
||||
#include "gd32f4xx_usart.h"
|
||||
#include "gd32f4xx_wwdgt.h"
|
||||
#include "gd32f4xx_misc.h"
|
||||
#endif
|
||||
|
||||
#if defined (GD32F450) || defined (GD32F470)
|
||||
#include "gd32f4xx_enet.h"
|
||||
#include "gd32f4xx_exmc.h"
|
||||
#include "gd32f4xx_ipa.h"
|
||||
#include "gd32f4xx_tli.h"
|
||||
#endif
|
||||
|
||||
#if defined (GD32F407) || defined (GD32F427)
|
||||
#include "gd32f4xx_enet.h"
|
||||
#include "gd32f4xx_exmc.h"
|
||||
#endif
|
||||
|
||||
#endif /* GD32F4XX_LIBOPT_H */
|
||||
@@ -0,0 +1,13 @@
|
||||
#ifndef LED_H
|
||||
#define LED_H
|
||||
|
||||
#include "gd32f4xx.h"
|
||||
#include "board_config.h"
|
||||
|
||||
void led_init(void);
|
||||
void led_on(void);
|
||||
void led_off(void);
|
||||
void led_toggle(void);
|
||||
void led_heart_beat(void);
|
||||
|
||||
#endif // LED_H
|
||||
@@ -0,0 +1,30 @@
|
||||
/**
|
||||
* ************************************************************************
|
||||
*
|
||||
* @file systick.h
|
||||
* @author GD32
|
||||
* @brief
|
||||
*
|
||||
* ************************************************************************
|
||||
* @copyright Copyright (c) 2024 GD32
|
||||
* ************************************************************************
|
||||
*/
|
||||
#ifndef SYS_TICK_H
|
||||
#define SYS_TICK_H
|
||||
|
||||
#include <stdint.h>
|
||||
|
||||
/* function declarations */
|
||||
/* configure systick */
|
||||
void systick_config(void);
|
||||
|
||||
/* delay a time in 10 microseconds */
|
||||
void delay_10us(uint32_t count);
|
||||
|
||||
/* delay a time in milliseconds */
|
||||
void delay_ms(uint32_t count);
|
||||
|
||||
/* decrement delay counters */
|
||||
void delay_decrement(void);
|
||||
|
||||
#endif /* SYS_TICK_H */
|
||||
+38
@@ -0,0 +1,38 @@
|
||||
#ifndef UART_H
|
||||
#define UART_H
|
||||
|
||||
#include "gd32f4xx.h"
|
||||
#include "board_config.h"
|
||||
|
||||
/*!
|
||||
\brief initialize UART interface
|
||||
\param[in] none
|
||||
\param[out] none
|
||||
\retval none
|
||||
*/
|
||||
void uart_init(void);
|
||||
void debug_usart_init(void);
|
||||
|
||||
/*!
|
||||
\brief pause UART RX interrupt (RBNE)
|
||||
\param[in] none
|
||||
\param[out] none
|
||||
\retval none
|
||||
*/
|
||||
void uart_rx_irq_pause(void);
|
||||
|
||||
/*!
|
||||
\brief resume UART RX interrupt (RBNE)
|
||||
\param[in] none
|
||||
\param[out] none
|
||||
\retval none
|
||||
*/
|
||||
void uart_rx_irq_resume(void);
|
||||
|
||||
#if RS485_MODE == CFG_ENABLE
|
||||
/* RS485 DE 引脚手动控制(GD32F4xx USART 无硬件 DE 驱动) */
|
||||
void rs485_de_set(void);
|
||||
void rs485_de_clear(void);
|
||||
#endif
|
||||
|
||||
#endif // UART_H
|
||||
@@ -0,0 +1,125 @@
|
||||
/**
|
||||
* @file uart_ring_buffer.h
|
||||
* @brief 简单高效的环形接收缓冲区(字节队列)接口声明。
|
||||
* @details 提供字节写入/读取、可读长度查询、清空与丢弃统计等 API,
|
||||
* 适用于中断接收(写)与主循环解析(读)的典型嵌入式串口场景。
|
||||
*/
|
||||
#ifndef UART_RING_BUFFER_H
|
||||
#define UART_RING_BUFFER_H
|
||||
|
||||
#include <stdint.h>
|
||||
#include <stdbool.h>
|
||||
|
||||
/**
|
||||
* @def UART_RX_BUFFER_SIZE
|
||||
* @brief 接收环形缓冲区容量(单位:字节)。
|
||||
* @note 采用“预留一格”区分空/满策略,最大可用容量为 UART_RX_BUFFER_SIZE-1。
|
||||
*/
|
||||
#define UART_RX_BUFFER_SIZE 64
|
||||
|
||||
/**
|
||||
* @defgroup RingBuffer 环形缓冲区
|
||||
* @brief 字节环形缓冲区(接收端)
|
||||
* @{
|
||||
*/
|
||||
|
||||
/**
|
||||
* @section RingBuffer_Usage 使用说明
|
||||
* 典型用法:中断接收(写入环形缓冲)、主循环解析(读取环形缓冲)。
|
||||
*
|
||||
* 并发访问约束(SPSC,无锁单写单读):
|
||||
* - ISR 生产者:仅调用 uart_ring_buffer_put() 写入数据。
|
||||
* - 主循环消费者:仅调用 uart_ring_buffer_get()/uart_ring_buffer_available() 读取数据。
|
||||
* - uart_ring_buffer_init()/uart_ring_buffer_clear() 会同时修改读写索引,内部使用最小临界区保护。
|
||||
* - 建议在主循环调用 clear/init 前先暂停 UART RX 中断,完成后再恢复,避免与 ISR 竞争。
|
||||
*
|
||||
* 1) 初始化
|
||||
* @code{.c}
|
||||
* uart_ring_buffer_init();
|
||||
* @endcode
|
||||
*
|
||||
* 2) 使能串口接收非空中断(RBNE)并开启中断(以 USART0 为例)
|
||||
* @code{.c}
|
||||
* usart_interrupt_enable(USART0, USART_INT_RBNE);
|
||||
* nvic_irq_enable(USART0_IRQn, 2, 0); // 根据工程需要设置优先级
|
||||
* @endcode
|
||||
*
|
||||
* 3) 在中断服务函数中写入环形缓冲(参考你当前工程的写法)
|
||||
* @code{.c}
|
||||
* void USART0_IRQHandler(void) {
|
||||
* if (RESET != usart_interrupt_flag_get(USART0, USART_INT_FLAG_RBNE)) {
|
||||
* uint8_t data = usart_data_receive(USART0);
|
||||
* (void)uart_ring_buffer_put(data); // 缓冲满时丢弃并计数
|
||||
* }
|
||||
* }
|
||||
* @endcode
|
||||
*
|
||||
* 4) 在主循环中读取处理
|
||||
* @code{.c}
|
||||
* while (uart_ring_buffer_available() > 0) {
|
||||
* int b = uart_ring_buffer_get();
|
||||
* if (b >= 0) {
|
||||
* // 处理字节 b
|
||||
* }
|
||||
* }
|
||||
* @endcode
|
||||
*
|
||||
* @note 缓冲区满时新字节会被丢弃,可用 uart_ring_buffer_drop_count() 查看累计丢弃数。
|
||||
* @note 采用“预留一格”区分空/满,最大可用容量为 UART_RX_BUFFER_SIZE-1。
|
||||
*/
|
||||
|
||||
/**
|
||||
* @brief 初始化环形缓冲区。
|
||||
* @details 复位读/写索引与丢弃计数,准备接收数据。
|
||||
* @note 若在中断环境使用,初始化前建议关闭相关接收中断以避免并发竞争。
|
||||
* @ingroup RingBuffer
|
||||
*/
|
||||
void uart_ring_buffer_init(void);
|
||||
|
||||
/**
|
||||
* @brief 获取当前可读的字节数。
|
||||
* @details 返回范围为 [0, UART_RX_BUFFER_SIZE-1]。
|
||||
* @return 可读字节数(uint8_t)。
|
||||
* @note 预留一个空槽区分“空/满”,因此满时返回 UART_RX_BUFFER_SIZE-1。
|
||||
* @ingroup RingBuffer
|
||||
*/
|
||||
uint8_t uart_ring_buffer_available(void);
|
||||
|
||||
/**
|
||||
* @brief 从环形缓冲区读取一个字节。
|
||||
* @details 若缓冲区非空,返回队头字节并推进读指针;若为空,返回 -1。
|
||||
* @return 读取到的字节(0..255),或 -1 表示缓冲区为空。
|
||||
* @retval -1 缓冲区为空,无数据可读。
|
||||
* @ingroup RingBuffer
|
||||
*/
|
||||
int uart_ring_buffer_get(void);
|
||||
|
||||
/**
|
||||
* @brief 向环形缓冲区写入一个字节。
|
||||
* @param data 待写入的字节。
|
||||
* @return 是否写入成功。
|
||||
* @retval true 写入成功。
|
||||
* @retval false 写入失败(缓冲区已满,数据被丢弃并计数)。
|
||||
* @note 如需改为“覆盖写入”策略,可在满时先推进读指针再写入。
|
||||
* @ingroup RingBuffer
|
||||
*/
|
||||
bool uart_ring_buffer_put(uint8_t data);
|
||||
|
||||
/**
|
||||
* @brief 清空环形缓冲区。
|
||||
* @details 复位读/写索引与丢弃计数,相当于逻辑上丢弃所有已接收数据,不擦除数据区内容。
|
||||
* @ingroup RingBuffer
|
||||
*/
|
||||
void uart_ring_buffer_clear(void);
|
||||
|
||||
/**
|
||||
* @brief 获取因缓冲区满而被丢弃的字节累计数量。
|
||||
* @details 该计数在 init/clear 时清零。
|
||||
* @return 丢弃的累计字节数。
|
||||
* @ingroup RingBuffer
|
||||
*/
|
||||
uint32_t uart_ring_buffer_drop_count(void);
|
||||
|
||||
/** @} */
|
||||
|
||||
#endif // UART_RING_BUFFER_H
|
||||
Reference in New Issue
Block a user