4 Commits

15 changed files with 146 additions and 275 deletions

1
.gitignore vendored
View File

@@ -1,5 +1,6 @@
# 忽略构建输出目录
Build/
.vscode/
# 忽略 Toolchain 目录下所有内容,但保留目录本身
Toolchain/*

View File

@@ -1,26 +0,0 @@
{
"recommendations": [
"ms-vscode.cmake-tools",
"ms-vscode.cpptools",
"ms-vscode.cpptools-extension-pack",
"ms-vscode.cpptools-themes",
"ms-vscode.vscode-embedded-tools",
"ms-vscode.hexeditor",
"ms-vscode.notepadplusplus-keybindings",
"twxs.cmake",
"xaver.clang-format",
"marus25.cortex-debug",
"cheshirekow.cmake-format",
"mcu-debug.debug-tracker-vscode",
"mcu-debug.memory-view",
"mcu-debug.peripheral-viewer",
"mcu-debug.rtos-views",
"trond-snekvik.gnu-mapfiles",
"zixuanwang.linkerscript",
"gurumukhi.selected-lines-count",
"gruntfuggly.todo-tree",
"vscode-icons-team.vscode-icons",
"jeff-hykin.better-cpp-syntax",
"dan-c-underwood.arm"
]
}

36
.vscode/launch.json vendored
View File

@@ -1,36 +0,0 @@
{
"version": "0.2.0",
"configurations": [
{
"cwd": "${workspaceFolder}",
"executable": "${workspaceFolder}/Build/Debug/Application/Application.elf",
"name": "Debug with OpenOCD",
"request": "launch",
"type": "cortex-debug",
"runToEntryPoint": "main",
"showDevDebugOutput": "none",
"gdbPath": "${workspaceFolder}/Toolchain/xpack-arm-none-eabi-gcc-11.3.1-1.1/bin/arm-none-eabi-gdb.exe",
"servertype": "openocd",
"serverpath": "${workspaceFolder}/Toolchain/xpack-openocd-0.11.0-3/bin/openocd.exe",
"svdFile": "${workspaceFolder}/GD32E230.svd",
"liveWatch": {
"enabled": true,
"samplesPerSecond": 1
},
"configFiles": [
"${workspaceFolder}/Toolchain/xpack-openocd-0.11.0-3/scripts/target/openocd_gdlink_gd32e23x.cfg"
],
"serverArgs": [
"-s", "${workspaceFolder}/Toolchain/xpack-openocd-0.11.0-3/scripts"
],
"searchDir": [
"${workspaceFolder}"
],
"preLaunchTask": "Build",
"preRestartCommands": [
"load",
"continue"
],
},
]
}

30
.vscode/settings.json vendored
View File

@@ -1,30 +0,0 @@
{
"terminal.integrated.tabs.enabled": true,
"terminal.integrated.profiles.windows": {
"Git Bash": {
"path": "C:\\Program Files\\Git\\bin\\bash.exe",
"icon": "terminal-bash"
},
"Git-Bash": {
"path": "D:\\Git\\bin\\bash.exe",
"icon": "terminal-bash"
}
},
"terminal.integrated.defaultProfile.windows": "Git-Bash",
"clang-format.assumeFilename": ".clang-format",
"clang-format.executable": "clang-format",
"C_Cpp.default.configurationProvider": "ms-vscode.cmake-tools",
"cmake.configureOnOpen": true,
"cmake.buildDirectory": "${workspaceFolder}/Build",
"vcpkg.storageLocation": "C:\\Dev\\Tools\\vcpkg",
"files.associations": {
"*.h": "c",
"*.c": "c",
"array": "c",
"string": "c",
"string_view": "c",
"ranges": "c",
"span": "c"
},
"cortex-debug.variableUseNaturalFormat": true,
}

145
.vscode/tasks.json vendored
View File

@@ -1,145 +0,0 @@
{
"version": "2.0.0",
"tasks": [
{
"label": "Build and Flash",
"group": {
"kind": "build",
"isDefault": true
},
"dependsOn": [
"Build",
"Flash MCU"
],
"dependsOrder": "sequence",
"icon": {
"id": "insert",
"tooltip": "Build and Flash"
}
},
{
"label": "Flash MCU",
"type": "shell",
"command": "'${workspaceFolder}/Toolchain/xpack-openocd-0.11.0-3/bin/openocd.exe' -s '${workspaceFolder}' -f '${workspaceFolder}/Toolchain/xpack-openocd-0.11.0-3/scripts/target/openocd_gdlink_gd32e23x.cfg' -c 'init; reset halt; flash write_image erase ${command:cmake.launchTargetFilename}; reset; exit'",
"group": {
"kind": "build",
"isDefault": true
},
"problemMatcher": [],
"options": {
"cwd": "${command:cmake.buildDirectory}/Application",
"environment": {
"CLICOLOR_FORCE": "1",
"OPENOCD_SCRIPTS": ""
}
},
"presentation": {
"clear": true
},
"icon": {
"id": "gather",
"tooltip": "Flash MCU"
}
},
{
"label": "Reset MCU",
"type": "shell",
"command": "'${workspaceFolder}/Toolchain/xpack-openocd-0.11.0-3/bin/openocd.exe' -s '${workspaceFolder}' -f '${workspaceFolder}/Toolchain/xpack-openocd-0.11.0-3/scripts/target/openocd_gdlink_gd32e23x.cfg' -c 'init; reset; exit'",
"group": {
"kind": "build",
"isDefault": true
},
"problemMatcher": [],
"options": {
"cwd": "${command:cmake.buildDirectory}/Application",
"environment": {
"CLICOLOR_FORCE": "1",
"OPENOCD_SCRIPTS": ""
}
},
"presentation": {
"clear": true
},
"icon": {
"id": "discard",
"tooltip": "Reset MCU"
}
},
{
"label": "Mass Erase MCU",
"type": "shell",
"command": "'${workspaceFolder}/Toolchain/xpack-openocd-0.11.0-3/bin/openocd.exe' -s '${workspaceFolder}' -f '${workspaceFolder}/Toolchain/xpack-openocd-0.11.0-3/scripts/target/openocd_gdlink_gd32e23x.cfg' -c 'init; reset halt; gd32e23x mass_erase 0; exit'",
"group": {
"kind": "build",
"isDefault": true
},
"problemMatcher": [],
"options": {
"cwd": "${command:cmake.buildDirectory}/Application",
"environment": {
"CLICOLOR_FORCE": "1",
"OPENOCD_SCRIPTS": ""
}
},
"presentation": {
"clear": true
},
"icon": {
"id": "clear-all",
"tooltip": "Erase MCU"
}
},
{
"label": "OpenOCD Server",
"type": "shell",
"command": [
"'${workspaceFolder}/Toolchain/xpack-openocd-0.11.0-3/bin/openocd.exe' -s '${workspaceFolder}' -f '${workspaceFolder}/Toolchain/xpack-openocd-0.11.0-3/scripts/target/openocd_gdlink_gd32e23x.cfg'"
],
"group": {
"kind": "build",
"isDefault": true
},
"problemMatcher": [],
"options": {
"cwd": "${command:cmake.buildDirectory}/Application",
"environment": {
"CLICOLOR_FORCE": "1",
"OPENOCD_SCRIPTS": ""
}
},
"presentation": {
"clear": true
}
},
{
"label": "Build",
"type": "cmake",
"command": "build",
"group": {
"kind": "build",
"isDefault": true
},
"problemMatcher": [
{
"base": "$gcc",
"fileLocation": [
"relative",
"${command:cmake.buildDirectory}"
]
}
],
"options": {
"environment": {
"CLICOLOR_FORCE": "1"
}
},
"presentation": {
"clear": true
},
"icon": {
"id": "code",
"tooltip": "Build"
}
}
]
}

View File

@@ -30,6 +30,7 @@ set(TARGET_SRC
Src/uart_ring_buffer.c
Src/command.c
Src/i2c.c
Src/board_config.c
)
# 设置输出目录

View File

@@ -1,6 +1,10 @@
#ifndef BOARD_CONFIG_H
#define BOARD_CONFIG_H
#define GD32E23XF4 0x10
#define GD32E23XF6 0x20
#define GD32E23XF8 0x40
/* >>>>>>>>>>>>>>>>>>>>[RS485 PHY DEFINE]<<<<<<<<<<<<<<<<<<<< */
// #define RS485_MAX13487 // RS485 PHY : MAX13487 (AutoDir)
@@ -23,6 +27,23 @@
/******************************************************************************/
/* Dynamic USART Configuration Structure */
typedef struct {
uint32_t rcu_usart;
uint32_t usart_periph;
IRQn_Type irq_type;
void (*irq_handler)(void); // 函数指针:指向中断处理函数
} usart_config_t;
extern usart_config_t g_usart_config;
extern uint8_t g_mcu_flash_size;
/* USART中断处理函数声明 */
void usart0_irq_handler(void);
void usart1_irq_handler(void);
/******************************************************************************/
#define RCU_GPIO_I2C RCU_GPIOF
#define RCU_I2C RCU_I2C0
#define I2C_SCL_PORT GPIOF
@@ -35,22 +56,25 @@
/******************************************************************************/
#define LED_RCU RCU_GPIOA
#define LED_PORT GPIOA
#define LED_PIN GPIO_PIN_7
#define LED_RCU RCU_GPIOA
/******************************************************************************/
#define RS485_RCU RCU_USART0
#define RS485_RCU (g_usart_config.rcu_usart)
#define RS485_PHY (g_usart_config.usart_periph)
#define RS485_IRQ (g_usart_config.irq_type)
#define RS485_GPIO_RCU RCU_GPIOA
#define RS485_GPIO_PORT GPIOA
#define RS485_EN_PIN GPIO_PIN_1
#define RS485_TX_PIN GPIO_PIN_2
#define RS485_RX_PIN GPIO_PIN_3
#define RS485_PHY USART0
#define RS485_BAUDRATE 115200U
#define RS485_EN_PIN GPIO_PIN_1
#define RS485_IRQ USART0_IRQn
/******************************************************************************/
void mcu_detect_and_config(void);
uint8_t get_flash_size(void);
#endif //BOARD_CONFIG_H

View File

@@ -78,5 +78,4 @@ void command_process(void);
void handle_command(const uint8_t *cmd, uint8_t len);
/** @} */ // end of Command group
#endif // COMMAND_H

52
Src/board_config.c Normal file
View File

@@ -0,0 +1,52 @@
#include "gd32e23x.h"
#include "board_config.h"
#include "systick.h"
/******************************************************************************/
#define FLASH_SIZE_ADDR (*(const uint8_t *)0x1FFFF7E0) // Flash base address
/******************************************************************************/
/* 前向声明中断处理函数 */
void usart0_irq_handler(void);
void usart1_irq_handler(void);
usart_config_t g_usart_config = {
.rcu_usart = RCU_USART1,
.usart_periph = USART1,
.irq_type = USART1_IRQn,
.irq_handler = usart1_irq_handler // 初始化函数指针
};
uint8_t g_mcu_flash_size = 0;
void mcu_detect_and_config(void) {
g_mcu_flash_size = FLASH_SIZE_ADDR;
switch (g_mcu_flash_size) {
case GD32E23XF4:
g_usart_config.rcu_usart = RCU_USART0;
g_usart_config.usart_periph = USART0;
g_usart_config.irq_type = USART0_IRQn;
g_usart_config.irq_handler = usart0_irq_handler; // 指向USART0处理函数
break;
case GD32E23XF6:
g_usart_config.rcu_usart = RCU_USART1;
g_usart_config.usart_periph = USART1;
g_usart_config.irq_type = USART1_IRQn;
g_usart_config.irq_handler = usart1_irq_handler; // 指向USART1处理函数
break;
default: // Default to GD32E23XF8
g_usart_config.rcu_usart = RCU_USART1;
g_usart_config.usart_periph = USART1;
g_usart_config.irq_type = USART1_IRQn;
g_usart_config.irq_handler = usart1_irq_handler; // 指向USART1处理函数
break;
}
}
uint8_t get_flash_size(void) {
return g_mcu_flash_size;
}

View File

@@ -250,21 +250,18 @@ void handle_command(const uint8_t *frame, uint8_t len) {
if (cmd_index == cmd_len) {
// 仅基础命令,如 M1, M2, M3
switch (base_cmd) {
case 1u: // M1: enable sensor report
case 1u: // M1
send_response(RESP_TYPE_OK, s_report_status_ok, sizeof(s_report_status_ok));
return;
case 2u: // M2: disable sensor report
send_response(RESP_TYPE_OK, s_report_status_ok, sizeof(s_report_status_ok));
return;
// case 2u: // M2
// return;
case 3u:
send_response(RESP_TYPE_OK, s_report_status_ok, sizeof(s_report_status_ok));
return;
// case 3u:
// return;
case 4u:
send_response(RESP_TYPE_OK, s_report_status_ok, sizeof(s_report_status_ok));
return;
// case 4u:
// return;
// case 201u: // M201命令
// send_response(RESP_TYPE_OK, s_report_status_ok, sizeof(s_report_status_ok));
@@ -404,4 +401,3 @@ void command_process(void) {
}
}
}

View File

@@ -37,6 +37,7 @@ OF SUCH DAMAGE.
#include "uart.h"
#include "uart_ring_buffer.h"
#include "led.h"
#include "board_config.h"
/*!
\brief this function handles NMI exception
@@ -102,8 +103,15 @@ void SysTick_Handler(void) {
}
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); // 缓冲满时丢弃,返回值可用于统计
// 检查当前配置是否使用USART0并且函数指针不为空
if(g_usart_config.usart_periph == USART0 && g_usart_config.irq_handler != 0) {
g_usart_config.irq_handler(); // 通过函数指针调用对应的处理函数
}
}
void USART1_IRQHandler(void) {
// 检查当前配置是否使用USART1并且函数指针不为空
if(g_usart_config.usart_periph == USART1 && g_usart_config.irq_handler != 0) {
g_usart_config.irq_handler(); // 通过函数指针调用对应的处理函数
}
}

View File

@@ -49,23 +49,17 @@ OF SUCH DAMAGE.
*/
int main(void)
{
led_init();
mcu_detect_and_config();
setbuf(stdout, NULL);
systick_config();
rs485_init();
led_init();
printf("Flash size: %d Kbytes\n", get_flash_size());
#ifdef DEBUG_VERBOSE
char hello_world[] = {"Hello World!\r\n"};
for (uint8_t i = 0; i < sizeof(hello_world); i++)
{
while (usart_flag_get(RS485_PHY, USART_FLAG_TBE) == RESET) {}
usart_data_transmit(RS485_PHY, hello_world[i]);
}
while (usart_flag_get(RS485_PHY, USART_FLAG_TC) == RESET) {}
printf("Hello World!\r\n");
#endif
i2c_config();

View File

@@ -16,6 +16,7 @@
#include <unistd.h>
#include <sys/wait.h>
#include "gd32e23x_usart.h"
#include "board_config.h"
#undef errno
extern int errno;
@@ -164,7 +165,7 @@ int _execve(char *name, char **argv, char **env)
// USART0 printf重定向实现
int __io_putchar(int ch) {
// 等待发送缓冲区空
while (usart_flag_get(USART0, USART_FLAG_TBE) == RESET) {}
usart_data_transmit(USART0, (uint8_t)ch);
while (usart_flag_get(RS485_PHY, USART_FLAG_TBE) == RESET) {}
usart_data_transmit(RS485_PHY, (uint8_t)ch);
return ch;
}

View File

@@ -3,6 +3,7 @@
#include "gd32e23x_rcu.h"
#include "gd32e23x_gpio.h"
#include "board_config.h"
#include "uart_ring_buffer.h"
void rs485_init(void) {
@@ -37,7 +38,7 @@ void rs485_init(void) {
usart_enable(RS485_PHY);
nvic_irq_enable(USART0_IRQn, 0);
nvic_irq_enable(RS485_IRQ, 0);
usart_interrupt_enable(RS485_PHY, USART_INT_RBNE);
// usart_interrupt_enable(RS485_PHY, USART_INT_IDLE);
@@ -70,3 +71,37 @@ void rs485_init(void) {
#endif // RS485_MAX13487
}
/******************************************************************************/
/* 具体的中断处理函数实现 */
/******************************************************************************/
void usart0_irq_handler(void) {
// 处理USART0的接收中断
if(usart_interrupt_flag_get(USART0, USART_INT_FLAG_RBNE)) {
uint8_t data = usart_data_receive(USART0);
// 使用原有的环形缓冲区处理逻辑
(void)uart_ring_buffer_put(data); // 缓冲满时丢弃,返回值可用于统计
}
// 处理USART0的空闲中断
if(usart_interrupt_flag_get(USART0, USART_INT_FLAG_IDLE)) {
usart_interrupt_flag_clear(USART0, USART_INT_FLAG_IDLE);
// 在这里添加空闲中断处理逻辑
}
}
void usart1_irq_handler(void) {
// 处理USART1的接收中断
if(usart_interrupt_flag_get(USART1, USART_INT_FLAG_RBNE)) {
uint8_t data = usart_data_receive(USART1);
// 使用原有的环形缓冲区处理逻辑
(void)uart_ring_buffer_put(data); // 缓冲满时丢弃,返回值可用于统计
}
// 处理USART1的空闲中断
if(usart_interrupt_flag_get(USART1, USART_INT_FLAG_IDLE)) {
usart_interrupt_flag_clear(USART1, USART_INT_FLAG_IDLE);
// 在这里添加空闲中断处理逻辑
}
}

View File

@@ -1,3 +0,0 @@
@echo off
set OPENOCD_SCRIPTS=
start "" "D:\Microsoft VS Code\Code.exe"