Compare commits
12 Commits
6d9328ca23
...
main
| Author | SHA1 | Date | |
|---|---|---|---|
| 3a4435e7a6 | |||
| 38db72d94f | |||
| bfa311b70f | |||
| 03810e9cad | |||
| bb97bac349 | |||
| f1edccc13d | |||
| 1e50a94427 | |||
| 7419dec1b5 | |||
| d324d5f92a | |||
| 750b5f3215 | |||
| 2df3a1d8a5 | |||
| cad99ca5f2 |
@@ -1,5 +1,6 @@
|
|||||||
# 忽略构建输出目录
|
# 忽略构建输出目录
|
||||||
Build/
|
Build/
|
||||||
|
.vscode/
|
||||||
|
|
||||||
# 忽略 Toolchain 目录下所有内容,但保留目录本身
|
# 忽略 Toolchain 目录下所有内容,但保留目录本身
|
||||||
Toolchain/*
|
Toolchain/*
|
||||||
|
|||||||
Vendored
-26
@@ -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"
|
|
||||||
]
|
|
||||||
}
|
|
||||||
Vendored
-36
@@ -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"
|
|
||||||
],
|
|
||||||
},
|
|
||||||
]
|
|
||||||
}
|
|
||||||
Vendored
-30
@@ -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,
|
|
||||||
}
|
|
||||||
Vendored
-145
@@ -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"
|
|
||||||
}
|
|
||||||
}
|
|
||||||
]
|
|
||||||
}
|
|
||||||
+7
-1
@@ -6,6 +6,10 @@ include(${CMAKE_SOURCE_DIR}/cmake/project.cmake)
|
|||||||
|
|
||||||
project(${PROJECT_NAME} LANGUAGES C CXX ASM)
|
project(${PROJECT_NAME} LANGUAGES C CXX ASM)
|
||||||
|
|
||||||
|
# Generate version header from CMake version variables
|
||||||
|
file(MAKE_DIRECTORY ${CMAKE_BINARY_DIR}/generated)
|
||||||
|
configure_file(${CMAKE_SOURCE_DIR}/cmake/version.h.in ${CMAKE_BINARY_DIR}/generated/version.h @ONLY)
|
||||||
|
|
||||||
# 添加SDK库
|
# 添加SDK库
|
||||||
add_subdirectory(SDK/CMSIS)
|
add_subdirectory(SDK/CMSIS)
|
||||||
add_subdirectory(SDK/GD32E23x_standard_peripheral)
|
add_subdirectory(SDK/GD32E23x_standard_peripheral)
|
||||||
@@ -30,6 +34,7 @@ set(TARGET_SRC
|
|||||||
Src/uart_ring_buffer.c
|
Src/uart_ring_buffer.c
|
||||||
Src/command.c
|
Src/command.c
|
||||||
Src/i2c.c
|
Src/i2c.c
|
||||||
|
Src/board_config.c
|
||||||
)
|
)
|
||||||
|
|
||||||
# 设置输出目录
|
# 设置输出目录
|
||||||
@@ -44,6 +49,7 @@ project_add_target_properties(${PROJECT_NAME})
|
|||||||
# 头文件路径
|
# 头文件路径
|
||||||
target_include_directories(${PROJECT_NAME} PRIVATE
|
target_include_directories(${PROJECT_NAME} PRIVATE
|
||||||
${CMAKE_SOURCE_DIR}/Inc
|
${CMAKE_SOURCE_DIR}/Inc
|
||||||
|
${CMAKE_BINARY_DIR}/generated
|
||||||
|
|
||||||
# Add new include directories here, e.g. ${CMAKE_SOURCE_DIR}/Application/User/Inc
|
# Add new include directories here, e.g. ${CMAKE_SOURCE_DIR}/Application/User/Inc
|
||||||
|
|
||||||
@@ -65,7 +71,7 @@ target_link_libraries(${PROJECT_NAME} PRIVATE CMSIS)
|
|||||||
target_link_libraries(${PROJECT_NAME} PRIVATE GD32E23x_standard_peripheral)
|
target_link_libraries(${PROJECT_NAME} PRIVATE GD32E23x_standard_peripheral)
|
||||||
|
|
||||||
# 生成 bin/hex/list 文件名格式:[工程名_版本号_编译条件_编译日期]
|
# 生成 bin/hex/list 文件名格式:[工程名_版本号_编译条件_编译日期]
|
||||||
set(OUTPUT_PREFIX "${PROJECT_NAME}_${VERSION}_${IIC_TYPE}_${BUILD_DATE}")
|
set(OUTPUT_PREFIX "${PROJECT_NAME}_${VERSION}_${BUILD_VARIANT}_${BUILD_DATE}")
|
||||||
|
|
||||||
add_custom_command(TARGET ${PROJECT_NAME}
|
add_custom_command(TARGET ${PROJECT_NAME}
|
||||||
POST_BUILD
|
POST_BUILD
|
||||||
|
|||||||
+73
-18
@@ -1,25 +1,50 @@
|
|||||||
#ifndef BOARD_CONFIG_H
|
#ifndef BOARD_CONFIG_H
|
||||||
#define BOARD_CONFIG_H
|
#define BOARD_CONFIG_H
|
||||||
|
|
||||||
/* >>>>>>>>>>>>>>>>>>>>[RS485 PHY DEFINE]<<<<<<<<<<<<<<<<<<<< */
|
#define GD32E23XF4 0x10
|
||||||
|
#define GD32E23XF6 0x20
|
||||||
|
#define GD32E23XF8 0x40
|
||||||
|
|
||||||
// #define RS485_MAX13487 // RS485 PHY : MAX13487 (AutoDir)
|
#define PROTOCOL_BOARD_TYPE 0x01 /**< 板卡类型标识 */
|
||||||
#undef RS485_MAX13487 // RS485 PHY : SP3487 (no AutoDir)
|
|
||||||
|
#include "version.h"
|
||||||
|
|
||||||
/* >>>>>>>>>>>>>>>>>>>>[IIC TYPE DEFINE]<<<<<<<<<<<<<<<<<<<< */
|
/* >>>>>>>>>>>>>>>>>>>>[IIC TYPE DEFINE]<<<<<<<<<<<<<<<<<<<< */
|
||||||
|
|
||||||
// #define SOFTWARE_IIC // IIC Type : Software IIC
|
// #define SOFTWARE_IIC // IIC Type : Software IIC
|
||||||
#undef SOFTWARE_IIC // IIC Type : Hardware IIC
|
#undef SOFTWARE_IIC // IIC Type : Hardware IIC
|
||||||
|
|
||||||
|
/* >>>>>>>>>>>>>>>>>>>>[DEBUG MODE]<<<<<<<<<<<<<<<<<<<< */
|
||||||
|
|
||||||
|
// #define DEBUG_MODE // Operating Mode : Debug Mode
|
||||||
|
#undef DEBUG_MODE // Operating Mode : Release Mode
|
||||||
|
|
||||||
|
/* >>>>>>>>>>>>>>>>>>>>[COMMAND DEBUG]<<<<<<<<<<<<<<<<<<<< */
|
||||||
|
|
||||||
|
// #define COM_DEBUG // Enable Command Debug Information
|
||||||
|
#undef COM_DEBUG // Disable Command Debug Information
|
||||||
|
|
||||||
/* >>>>>>>>>>>>>>>>>>>>[DEBUG ASSERTIONS DEFINE]<<<<<<<<<<<<<<<<<<<< */
|
/* >>>>>>>>>>>>>>>>>>>>[DEBUG ASSERTIONS DEFINE]<<<<<<<<<<<<<<<<<<<< */
|
||||||
|
|
||||||
// #define DEBUG_VERBOSE // Debug Assertions Status : Debug Verbose Information
|
// #define DEBUG_VERBOSE // Debug Assertions Status : Debug Verbose Information
|
||||||
#undef DEBUG_VERBOSE // Debug Assertions Status : No Debug Verbose Information
|
#undef DEBUG_VERBOSE // Debug Assertions Status : No Debug Verbose Information
|
||||||
|
|
||||||
/* >>>>>>>>>>>>>>>>>>>>[EDDY DRIVE CURRENT DETECTION]<<<<<<<<<<<<<<<<<<<< */
|
/******************************************************************************/
|
||||||
|
|
||||||
// #define EDDY_DRIVE_CURRENT_DETECTION // Eddy Drive Current Detection : Enable
|
/* Dynamic USART Configuration Structure */
|
||||||
#undef EDDY_DRIVE_CURRENT_DETECTION // Eddy Drive Current Detection : Disable
|
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);
|
||||||
|
|
||||||
/******************************************************************************/
|
/******************************************************************************/
|
||||||
|
|
||||||
@@ -31,26 +56,56 @@
|
|||||||
#define I2C_SDA_PIN GPIO_PIN_0
|
#define I2C_SDA_PIN GPIO_PIN_0
|
||||||
#define I2C_GPIO_AF GPIO_AF_1
|
#define I2C_GPIO_AF GPIO_AF_1
|
||||||
|
|
||||||
#define I2C_DEBUG_UART USART0
|
|
||||||
|
|
||||||
/******************************************************************************/
|
/******************************************************************************/
|
||||||
|
|
||||||
|
#define LED_RCU RCU_GPIOA
|
||||||
#define LED_PORT GPIOA
|
#define LED_PORT GPIOA
|
||||||
#define LED_PIN GPIO_PIN_7
|
#define LED_PIN GPIO_PIN_7
|
||||||
#define LED_RCU RCU_GPIOA
|
|
||||||
|
|
||||||
/******************************************************************************/
|
/******************************************************************************/
|
||||||
|
|
||||||
#define RS485_RCU RCU_USART0
|
#define UART_RCU (g_usart_config.rcu_usart)
|
||||||
#define RS485_GPIO_RCU RCU_GPIOA
|
#define UART_PHY (g_usart_config.usart_periph)
|
||||||
#define RS485_GPIO_PORT GPIOA
|
#define UART_IRQ (g_usart_config.irq_type)
|
||||||
#define RS485_TX_PIN GPIO_PIN_2
|
#define UART_GPIO_RCU RCU_GPIOA
|
||||||
#define RS485_RX_PIN GPIO_PIN_3
|
#define UART_GPIO_PORT GPIOA
|
||||||
#define RS485_PHY USART0
|
#define UART_TX_PIN GPIO_PIN_2
|
||||||
#define RS485_BAUDRATE 115200U
|
#define UART_RX_PIN GPIO_PIN_3
|
||||||
#define RS485_EN_PIN GPIO_PIN_1
|
#define UART_BAUDRATE 115200U
|
||||||
#define RS485_IRQ USART0_IRQn
|
|
||||||
|
|
||||||
/******************************************************************************/
|
/******************************************************************************/
|
||||||
|
|
||||||
|
#define FAN_T_GPIO_PORT_RCU RCU_GPIOA
|
||||||
|
#define FAN_T_GPIO_PORT GPIOA
|
||||||
|
#define FAN_T_PWR_EN GPIO_PIN_5
|
||||||
|
#define FAN_T_PWM GPIO_PIN_6
|
||||||
|
#define FAN_T_DETECT GPIO_PIN_4
|
||||||
|
|
||||||
|
#define FAN_A_GPIO_PORT_RCU RCU_GPIOA
|
||||||
|
#define FAN_A_PWR_PORT_RCU RCU_GPIOB
|
||||||
|
#define FAN_A_GPIO_PORT GPIOA
|
||||||
|
#define FAN_A_PWR_PORT GPIOB
|
||||||
|
#define FAN_A_PWR_EN GPIO_PIN_1
|
||||||
|
#define FAN_A_PWM GPIO_PIN_9
|
||||||
|
#define FAN_A_DETECT GPIO_PIN_10
|
||||||
|
|
||||||
|
/******************************************************************************/
|
||||||
|
|
||||||
|
// UV-LED NTC Temperature Sensor Definitions AD_CHANNEL_0
|
||||||
|
#define LAMP_TEMP_GPIO_PORT_RCU RCU_GPIOA
|
||||||
|
#define LAMP_TEMP_GPIO_PORT GPIOA
|
||||||
|
#define LAMP_TEMP_AD_PIN GPIO_PIN_0
|
||||||
|
|
||||||
|
// DMD NTC Temperature Sensor Definitions AD_CHANNEL_1
|
||||||
|
#define DMD_TEMP_GPIO_PORT_RCU RCU_GPIOA
|
||||||
|
#define DMD_TEMP_GPIO_PORT GPIOA
|
||||||
|
#define DMD_TEMP_AD_PIN GPIO_PIN_1
|
||||||
|
|
||||||
|
/******************************************************************************/
|
||||||
|
|
||||||
|
#define DEBUG_UART USART0
|
||||||
|
|
||||||
|
void mcu_detect_and_config(void);
|
||||||
|
uint8_t get_flash_size(void);
|
||||||
|
|
||||||
#endif //BOARD_CONFIG_H
|
#endif //BOARD_CONFIG_H
|
||||||
|
|||||||
+19
-1
@@ -21,7 +21,7 @@
|
|||||||
* 接收命令帧格式:
|
* 接收命令帧格式:
|
||||||
* @code
|
* @code
|
||||||
* [0] HEADER = 0xD5 // 包头标识
|
* [0] HEADER = 0xD5 // 包头标识
|
||||||
* [1] BOARD_TYPE = 0x03 // 板卡类型标识
|
* [1] BOARD_TYPE = 0x01 // 板卡类型标识
|
||||||
* [2] LEN = 数据区字节数 // 有效载荷长度
|
* [2] LEN = 数据区字节数 // 有效载荷长度
|
||||||
* [3..(3+LEN-1)] 数据 // 命令数据
|
* [3..(3+LEN-1)] 数据 // 命令数据
|
||||||
* [last] CRC // 校验码(从索引1累加到len-2的低8位)
|
* [last] CRC // 校验码(从索引1累加到len-2的低8位)
|
||||||
@@ -77,6 +77,24 @@ void command_process(void);
|
|||||||
*/
|
*/
|
||||||
void handle_command(const uint8_t *cmd, uint8_t len);
|
void handle_command(const uint8_t *cmd, uint8_t len);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @brief 执行命令(简化版)
|
||||||
|
* @details 根据命令字符串直接构造命令帧并执行,无需手动构造协议帧
|
||||||
|
* @param cmd_str 命令字符串(如"M730S0T1000"、"M731S100"等)
|
||||||
|
* @note 简化的测试函数,自动处理协议帧构造、CRC计算和命令执行
|
||||||
|
* @ingroup Command
|
||||||
|
*/
|
||||||
|
void command_execute(const char *cmd_str);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @brief M737命令投光结束回调函数
|
||||||
|
* @details 当定时器结束时调用此函数,向等待中的M737命令发送OK响应
|
||||||
|
* @param none
|
||||||
|
* @note 此函数由定时器中断调用,不应直接调用
|
||||||
|
* @ingroup Command
|
||||||
|
*/
|
||||||
|
void command_m737_timer_finished_callback(void);
|
||||||
|
|
||||||
/** @} */ // end of Command group
|
/** @} */ // end of Command group
|
||||||
|
|
||||||
#endif // COMMAND_H
|
#endif // COMMAND_H
|
||||||
|
|||||||
@@ -18,7 +18,7 @@
|
|||||||
|
|
||||||
/******************************************************************************/
|
/******************************************************************************/
|
||||||
|
|
||||||
#define I2C_SPEED 100000U /* 100kHz */
|
#define I2C_SPEED 20000U /* 20kHz */
|
||||||
#define I2C_TIME_OUT 5000U /* 5000 loops timeout */
|
#define I2C_TIME_OUT 5000U /* 5000 loops timeout */
|
||||||
#define I2C_MAX_RETRY 3U /* Maximum retry attempts */
|
#define I2C_MAX_RETRY 3U /* Maximum retry attempts */
|
||||||
#define I2C_DELAY_10US 10U /* Delay in microseconds for bus reset */
|
#define I2C_DELAY_10US 10U /* Delay in microseconds for bus reset */
|
||||||
@@ -107,15 +107,39 @@ i2c_result_t i2c_write_16bits(uint8_t slave_addr, uint8_t reg_addr, uint8_t data
|
|||||||
*/
|
*/
|
||||||
i2c_result_t i2c_read_16bits(uint8_t slave_addr, uint8_t reg_addr, uint8_t *data);
|
i2c_result_t i2c_read_16bits(uint8_t slave_addr, uint8_t reg_addr, uint8_t *data);
|
||||||
|
|
||||||
|
/* Generic read/write functions with configurable length */
|
||||||
/*!
|
/*!
|
||||||
\brief read 16-bit data from I2C device
|
\brief write data to I2C device with configurable length
|
||||||
\param[in] slave_addr: 7-bit slave address
|
\param[in] slave_addr: slave device address (7-bit)
|
||||||
\param[in] reg_addr: register address
|
\param[in] reg_addr: register address
|
||||||
\param[out] data: pointer to 2-byte data buffer
|
\param[in] data: pointer to data buffer
|
||||||
\retval i2c_result_t
|
\param[in] length: number of bytes to write (1-255)
|
||||||
|
\param[out] none
|
||||||
|
\retval i2c_result_t: operation result
|
||||||
*/
|
*/
|
||||||
i2c_result_t i2c_read_16bits(uint8_t slave_addr, uint8_t reg_addr, uint8_t *data);
|
i2c_result_t i2c_write(uint8_t slave_addr, uint8_t reg_addr, uint8_t *data, uint8_t length);
|
||||||
|
|
||||||
|
/*!
|
||||||
|
\brief read data from I2C device with configurable length
|
||||||
|
\param[in] slave_addr: slave device address (7-bit)
|
||||||
|
\param[in] reg_addr: register address
|
||||||
|
\param[out] data: pointer to data buffer
|
||||||
|
\param[in] length: number of bytes to read (1-255)
|
||||||
|
\retval i2c_result_t: operation result
|
||||||
|
*/
|
||||||
|
i2c_result_t i2c_read(uint8_t slave_addr, uint8_t reg_addr, uint8_t *data, uint8_t length);
|
||||||
|
|
||||||
|
/* Pure read response without sub-address phase (for commands that already sent read request parameters) */
|
||||||
|
/*!
|
||||||
|
\brief read data from I2C device without register/sub-address phase
|
||||||
|
\param[in] slave_addr: slave device address (7-bit)
|
||||||
|
\param[out] data: pointer to data buffer
|
||||||
|
\param[in] length: number of bytes to read (1-255)
|
||||||
|
\retval i2c_result_t: operation result
|
||||||
|
*/
|
||||||
|
i2c_result_t i2c_read_raw(uint8_t slave_addr, uint8_t *data, uint8_t length);
|
||||||
|
|
||||||
|
#ifdef DEBUG_VERBOSE
|
||||||
/*!
|
/*!
|
||||||
\brief get status string for debugging
|
\brief get status string for debugging
|
||||||
\param[in] status: i2c_result_t value
|
\param[in] status: i2c_result_t value
|
||||||
@@ -123,5 +147,6 @@ i2c_result_t i2c_read_16bits(uint8_t slave_addr, uint8_t reg_addr, uint8_t *data
|
|||||||
\retval const char* status string
|
\retval const char* status string
|
||||||
*/
|
*/
|
||||||
const char* i2c_get_status_string(i2c_result_t status);
|
const char* i2c_get_status_string(i2c_result_t status);
|
||||||
|
#endif
|
||||||
|
|
||||||
#endif //I2C_H
|
#endif //I2C_H
|
||||||
|
|||||||
+24
-1
@@ -3,6 +3,29 @@
|
|||||||
|
|
||||||
#include "gd32e23x.h"
|
#include "gd32e23x.h"
|
||||||
|
|
||||||
void rs485_init(void);
|
/*!
|
||||||
|
\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);
|
||||||
|
|
||||||
#endif // UART_H
|
#endif // UART_H
|
||||||
|
|||||||
@@ -27,6 +27,12 @@
|
|||||||
* @section RingBuffer_Usage 使用说明
|
* @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) 初始化
|
* 1) 初始化
|
||||||
* @code{.c}
|
* @code{.c}
|
||||||
* uart_ring_buffer_init();
|
* uart_ring_buffer_init();
|
||||||
|
|||||||
@@ -8,11 +8,14 @@
|
|||||||
|
|
||||||
- [适用范围](#适用范围)
|
- [适用范围](#适用范围)
|
||||||
- [默认配置](#默认配置)
|
- [默认配置](#默认配置)
|
||||||
|
- [快速开始](#快速开始)
|
||||||
|
- [工程结构](#工程结构)
|
||||||
|
- [板级配置](#板级配置)
|
||||||
|
- [Flash 偏移配置(配合 Bootloader)](#flash-偏移配置配合-bootloader)
|
||||||
- [工具链准备](#工具链准备)
|
- [工具链准备](#工具链准备)
|
||||||
- [使用说明](#使用说明)
|
- [使用说明](#使用说明)
|
||||||
- [时钟配置说明](#时钟配置说明)
|
- [时钟配置说明](#时钟配置说明)
|
||||||
- [vcpkg 依赖管理(可选)](#vcpkg-依赖管理可选)
|
- [vcpkg 依赖管理(可选)](#vcpkg-依赖管理可选)
|
||||||
- [建议补充内容](#建议补充内容)
|
|
||||||
|
|
||||||
---
|
---
|
||||||
|
|
||||||
@@ -27,44 +30,204 @@
|
|||||||
## 默认配置
|
## 默认配置
|
||||||
|
|
||||||
- MCU 主频:内部 RC 振荡器,系统时钟配置为 72MHz
|
- MCU 主频:内部 RC 振荡器,系统时钟配置为 72MHz
|
||||||
|
- 调试串口:USART0(PA2 TX / PA3 RX),115200 波特率
|
||||||
|
- I2C:默认硬件 I2C0(PF0 SDA / PF1 SCL),可通过 `board_config.h` 切换为软件 I2C
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
## 快速开始
|
||||||
|
|
||||||
|
### 基于模板创建新项目
|
||||||
|
|
||||||
|
1. **克隆或复制本仓库**
|
||||||
|
```bash
|
||||||
|
git clone https://gitea.hulk.wang/hulk/gd32e23x_template_cmake_vscode.git my-new-project
|
||||||
|
cd my-new-project
|
||||||
|
```
|
||||||
|
|
||||||
|
2. **修改项目配置** — 编辑 `cmake/project_config.cmake`:
|
||||||
|
```cmake
|
||||||
|
set(PROJECT_NAME "MyProject") # 项目名称
|
||||||
|
set(BOARD_TYPE_CODE 20) # 板卡类型码(协议帧中的标识)
|
||||||
|
set(VERSION_MAJOR 1) # 主版本号
|
||||||
|
set(VERSION_MINOR 0) # 次版本号
|
||||||
|
set(VERSION_PATCH 0) # 修订号
|
||||||
|
set(BUILD_VARIANT "APP") # 编译变体
|
||||||
|
```
|
||||||
|
|
||||||
|
3. **添加业务源文件** — 编辑 `CMakeLists.txt`,在 `TARGET_SRC` 中添加你的 `.c` 文件。
|
||||||
|
|
||||||
|
4. **配置板级引脚** — 编辑 `Inc/board_config.h`,修改 I2C、UART、LED 等引脚定义。
|
||||||
|
|
||||||
|
5. **编译**
|
||||||
|
```bash
|
||||||
|
cmake --preset Debug
|
||||||
|
cmake --build build/Debug
|
||||||
|
```
|
||||||
|
产物在 `build/Debug/` 下,包含 `.elf`、`.hex`、`.bin`、`.map`、`.list`。
|
||||||
|
|
||||||
|
### 分支说明
|
||||||
|
|
||||||
|
| 分支 | 用途 |
|
||||||
|
|------|------|
|
||||||
|
| `template_pc` | **主模板**(推荐),用于桌面端 VSCode 开发 |
|
||||||
|
| `template_xl` | 小琅适配版 |
|
||||||
|
| `main` | 早期版本,不推荐使用 |
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
## 工程结构
|
||||||
|
|
||||||
|
```
|
||||||
|
.
|
||||||
|
├── CMakeLists.txt # 主构建文件
|
||||||
|
├── CMakePresets.json # CMake 预设(Debug/Release)
|
||||||
|
├── cmake/
|
||||||
|
│ ├── arm-none-eabi-gcc.cmake # ARM GCC 工具链配置
|
||||||
|
│ ├── project.cmake # 编译选项(-Os/-O0, -mcpu=cortex-m23)
|
||||||
|
│ ├── project_config.cmake # 项目名/版本号/编译变体
|
||||||
|
│ └── version.h.in # 自动生成固件版本头
|
||||||
|
├── Inc/ # 头文件
|
||||||
|
│ ├── board_config.h # 板级引脚定义 + 功能开关
|
||||||
|
│ ├── command.h # 串口命令协议
|
||||||
|
│ ├── i2c.h / led.h / systick.h / uart.h
|
||||||
|
│ └── uart_ring_buffer.h / gd32e23x_it.h / gd32e23x_libopt.h
|
||||||
|
├── Src/ # 源码
|
||||||
|
│ ├── main.c # 入口函数
|
||||||
|
│ ├── command.c # 命令解析处理
|
||||||
|
│ ├── board_config.c # MCU 型号自动检测
|
||||||
|
│ ├── i2c.c / led.c / systick.c / uart.c / uart_ring_buffer.c
|
||||||
|
│ └── gd32e23x_it.c / system_gd32e23x.c / syscalls.c
|
||||||
|
├── SDK/
|
||||||
|
│ ├── CMSIS/ # ARM CMSIS Core (Cortex-M23) + GD 启动文件
|
||||||
|
│ └── GD32E23x_standard_peripheral/ # GD32 标准外设库
|
||||||
|
├── LD/gd32e23x_flash.ld # 链接脚本
|
||||||
|
├── doc/ # 芯片数据手册
|
||||||
|
└── .vscode/ # VSCode 调试/烧录配置
|
||||||
|
```
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
## 板级配置
|
||||||
|
|
||||||
|
编辑 `Inc/board_config.h` 可切换以下功能:
|
||||||
|
|
||||||
|
```c
|
||||||
|
/* I2C 类型:软件 I2C 或 硬件 I2C */
|
||||||
|
// #define SOFTWARE_IIC
|
||||||
|
#undef SOFTWARE_IIC
|
||||||
|
|
||||||
|
/* 调试模式:开启 printf 输出 */
|
||||||
|
// #define DEBUG_MODE
|
||||||
|
#undef DEBUG_MODE
|
||||||
|
|
||||||
|
/* 调试详细模式:I2C 扫描等额外信息 */
|
||||||
|
// #define DEBUG_VERBOSE
|
||||||
|
#undef DEBUG_VERBOSE
|
||||||
|
```
|
||||||
|
|
||||||
|
引脚定义集中在同一文件中,按需修改:
|
||||||
|
|
||||||
|
```c
|
||||||
|
#define I2C_SCL_PORT GPIOF
|
||||||
|
#define I2C_SCL_PIN GPIO_PIN_1
|
||||||
|
#define I2C_SDA_PORT GPIOF
|
||||||
|
#define I2C_SDA_PIN GPIO_PIN_0
|
||||||
|
|
||||||
|
#define LED_PORT GPIOA
|
||||||
|
#define LED_PIN GPIO_PIN_7
|
||||||
|
|
||||||
|
#define UART_TX_PIN GPIO_PIN_2
|
||||||
|
#define UART_RX_PIN GPIO_PIN_3
|
||||||
|
```
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
## Flash 偏移配置(配合 Bootloader)
|
||||||
|
|
||||||
|
如果固件需要通过 Bootloader 启动(Bootloader 占用 Flash 前部区域),需修改两处:
|
||||||
|
|
||||||
|
**1. 链接脚本** — `LD/gd32e23x_flash.ld` 第 15 行:
|
||||||
|
```c
|
||||||
|
// 默认:从 Flash 起始运行
|
||||||
|
FLASH (rx) : ORIGIN = 0x08000000, LENGTH = 16K
|
||||||
|
|
||||||
|
// 配合 Bootloader:前 8KB 留给 Bootloader,App 从 0x08002000 开始
|
||||||
|
FLASH (rx) : ORIGIN = 0x08002000, LENGTH = 8K
|
||||||
|
```
|
||||||
|
|
||||||
|
**2. 向量表偏移** — `Src/system_gd32e23x.c` 第 44 行:
|
||||||
|
```c
|
||||||
|
// 默认
|
||||||
|
#define VECT_TAB_OFFSET (uint32_t)0x00
|
||||||
|
|
||||||
|
// 配合 Bootloader(值 = Flash 偏移量,不含 0x0800 前缀)
|
||||||
|
#define VECT_TAB_OFFSET (uint32_t)0x2000
|
||||||
|
```
|
||||||
|
|
||||||
|
> ⚠️ 两个偏移值必须对应修改:`LD` 中的 `ORIGIN` 减去 `0x08000000` 应等于 `VECT_TAB_OFFSET`。
|
||||||
|
|
||||||
---
|
---
|
||||||
|
|
||||||
## 工具链准备
|
## 工具链准备
|
||||||
|
|
||||||
### 1. xPack GNU Arm Embedded GCC Toolchain
|
### 1. ARM GCC 工具链
|
||||||
|
|
||||||
- **版本**:xpack-arm-none-eabi-gcc-11.3.1-1.1
|
- **版本**:xpack-arm-none-eabi-gcc-11.3.1-1.1
|
||||||
- **建议解压路径**:Tools/xpack-arm-none-eabi-gcc-11.3.1-1.1
|
- **建议解压路径**:工程根目录下 `Toolchain/xpack-arm-none-eabi-gcc-11.3.1-1.1`
|
||||||
- **官方下载地址**:https://github.com/xpack-dev-tools/arm-none-eabi-gcc-xpack/releases
|
- **官方下载地址**:https://github.com/xpack-dev-tools/arm-none-eabi-gcc-xpack/releases
|
||||||
- **路径自定义说明**:
|
- **路径自定义**:
|
||||||
如需自定义工具链路径,请同步修改以下文件:
|
如需自定义工具链路径,修改 `cmake/arm-none-eabi-gcc.cmake` 中的 `_TOOLCHAIN_CANDIDATES` 列表,或通过 CMake 参数传入:
|
||||||
- `Projects/<BoardName>/<ProjectName>/cmake/arm-none-eabi-gcc.cmake`(第2行)
|
```bash
|
||||||
- `Projects/<BoardName>/<ProjectName>/.vscode/launch.json`(第12行)
|
cmake --preset Debug -DTOOLCHAIN_DIRECTORY=/your/path/bin
|
||||||
|
```
|
||||||
|
|
||||||
### 2. OpenOCD
|
### 2. OpenOCD(调试/烧录)
|
||||||
|
|
||||||
- **版本**:xpack-openocd-0.11.0-3
|
- **版本**:xpack-openocd-0.11.0-3
|
||||||
- **建议解压路径**:Tools/xpack-openocd-0.11.0-3
|
- **建议解压路径**:任意位置(在 `.vscode/launch.json` 中配置路径)
|
||||||
- **获取地址**:https://github.com/burakenez/gd32-tools-xpack-openocd/tree/v0.11.0-3
|
- **获取地址**:https://github.com/burakenez/gd32-tools-xpack-openocd/tree/v0.11.0-3
|
||||||
- **说明**:
|
- **说明**:
|
||||||
- 本版本提取自 Embedded Builder V1.4.1.23782。
|
- 本版本提取自 Embedded Builder V1.4.1.23782。
|
||||||
- ⚠️ 请勿随意更换版本,因 GD32 MCU 支持有限,推荐严格使用此版本。
|
- ⚠️ 请勿随意更换版本,因 GD32 MCU 支持有限,推荐严格使用此版本。
|
||||||
- **路径自定义说明**:
|
- **路径自定义**:
|
||||||
如需自定义 OpenOCD 路径,请同步修改以下文件:
|
修改 `.vscode/launch.json` 中 `miDebuggerPath` 和 `serverpath` 指向你的 OpenOCD 路径。
|
||||||
- `Projects/<BoardName>/<ProjectName>/.vscode/launch.json`(第14、17行)
|
|
||||||
- `Projects/<BoardName>/<ProjectName>/.vscode/task.json` 中所有相关路径
|
|
||||||
|
|
||||||
---
|
---
|
||||||
|
|
||||||
## 使用说明
|
## 使用说明
|
||||||
|
|
||||||
1. **准备工具链**
|
### 编译
|
||||||
- 按上述说明下载并解压 ARM GCC 和 OpenOCD 到 Tools 目录。
|
|
||||||
- Toolchain 目录内容不会被 git 跟踪,需自行维护。
|
|
||||||
|
|
||||||
2. **烧录固件**
|
```bash
|
||||||
- 可直接使用 VSCode 任务栏的 Flash MCU 任务,或命令行运行 OpenOCD。
|
# Debug 构建(-O0, -g3)
|
||||||
|
cmake --preset Debug
|
||||||
|
cmake --build build/Debug
|
||||||
|
|
||||||
|
# Release 构建(-Os, -g0)
|
||||||
|
cmake --preset Release
|
||||||
|
cmake --build build/Release
|
||||||
|
```
|
||||||
|
|
||||||
|
### 烧录
|
||||||
|
|
||||||
|
通过 VSCode 任务栏运行 `Flash MCU` 任务,或命令行:
|
||||||
|
|
||||||
|
```bash
|
||||||
|
openocd -f interface/cmsis-dap.cfg -f target/gd32e23x.cfg -c "program build/Debug/Application.elf verify reset exit"
|
||||||
|
```
|
||||||
|
|
||||||
|
### 产物
|
||||||
|
|
||||||
|
编译输出位于 `build/<Config>/`:
|
||||||
|
|
||||||
|
| 文件 | 说明 |
|
||||||
|
|------|------|
|
||||||
|
| `Application.elf` | ELF 固件(调试用) |
|
||||||
|
| `{项目名}_{版本}_{编译条件}_{日期}.hex` | Hex 文件 |
|
||||||
|
| `{项目名}_{版本}_{编译条件}_{日期}.bin` | 二进制文件 |
|
||||||
|
| `{项目名}_{版本}_{编译条件}_{日期}.list` | 反汇编清单 |
|
||||||
|
| `{项目名}_{版本}_{编译条件}_{日期}.map` | 内存映射 |
|
||||||
|
|
||||||
---
|
---
|
||||||
|
|
||||||
@@ -84,7 +247,7 @@
|
|||||||
2. 取消你需要的时钟方案的注释,并注释掉其它方案。
|
2. 取消你需要的时钟方案的注释,并注释掉其它方案。
|
||||||
3. 保存后重新编译工程即可生效。
|
3. 保存后重新编译工程即可生效。
|
||||||
|
|
||||||
详细时钟初始化流程可参考 `system_gd32e23x.c` 文件中的 `system_clock_config` 及相关函数实现。
|
详细时钟初始化流程可参考 `Src/system_gd32e23x.c` 文件中的 `system_clock_config` 及相关函数实现。
|
||||||
|
|
||||||
---
|
---
|
||||||
|
|
||||||
@@ -122,18 +285,3 @@
|
|||||||
2. 启动 VSCode 或命令行,vcpkg 会自动检测并安装所需工具。
|
2. 启动 VSCode 或命令行,vcpkg 会自动检测并安装所需工具。
|
||||||
|
|
||||||
如不需要 vcpkg,可忽略本文件。
|
如不需要 vcpkg,可忽略本文件。
|
||||||
|
|
||||||
---
|
|
||||||
|
|
||||||
## 建议补充内容
|
|
||||||
|
|
||||||
- **快速上手示例**:如 main.c 的最小点灯/串口输出代码片段。
|
|
||||||
- **常见问题与解答**:如构建失败、烧录失败的排查建议。
|
|
||||||
- **调试说明**:如何用 VSCode 调试、断点、查看寄存器等。
|
|
||||||
- **多板卡适配说明**:如有多种硬件,如何切换 BoardName。
|
|
||||||
- **贡献指南**:如何提交 PR、代码风格约定等。
|
|
||||||
- **License 说明**:开源协议和版权声明。
|
|
||||||
|
|
||||||
---
|
|
||||||
|
|
||||||
如需进一步完善或有其他建议,欢迎随时反馈!
|
|
||||||
|
|||||||
@@ -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;
|
||||||
|
}
|
||||||
+299
-60
@@ -11,12 +11,16 @@
|
|||||||
|
|
||||||
#include "command.h"
|
#include "command.h"
|
||||||
#include "uart_ring_buffer.h"
|
#include "uart_ring_buffer.h"
|
||||||
|
#include "uart.h"
|
||||||
#include "led.h"
|
#include "led.h"
|
||||||
#include <stdint.h>
|
#include <stdint.h>
|
||||||
#include <stdbool.h>
|
#include <stdbool.h>
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
|
#include <string.h>
|
||||||
#include "board_config.h"
|
#include "board_config.h"
|
||||||
#include "gd32e23x_usart.h"
|
#include "gd32e23x_usart.h"
|
||||||
|
#include "i2c.h"
|
||||||
|
#include "core_cm23.h"
|
||||||
|
|
||||||
/* ============================================================================
|
/* ============================================================================
|
||||||
* 协议格式说明
|
* 协议格式说明
|
||||||
@@ -28,7 +32,7 @@
|
|||||||
* @details
|
* @details
|
||||||
* Host -> Device 命令帧格式:
|
* Host -> Device 命令帧格式:
|
||||||
* [0] HEADER = 0xD5 // 包头标识
|
* [0] HEADER = 0xD5 // 包头标识
|
||||||
* [1] BOARD_TYPE = 0x03 // 板卡类型标识
|
* [1] BOARD_TYPE = 0x01 // 板卡类型标识
|
||||||
* [2] LEN = 数据区字节数 // 有效载荷长度
|
* [2] LEN = 数据区字节数 // 有效载荷长度
|
||||||
* [3..(3+LEN-1)] 数据 // 命令数据,如 "M1", "M2S123"
|
* [3..(3+LEN-1)] 数据 // 命令数据,如 "M1", "M2S123"
|
||||||
* [last] CRC = 校验码 // 从索引1到(last-1)的累加和低8位
|
* [last] CRC = 校验码 // 从索引1到(last-1)的累加和低8位
|
||||||
@@ -52,24 +56,26 @@
|
|||||||
/** @name 协议帧标识符
|
/** @name 协议帧标识符
|
||||||
* @{ */
|
* @{ */
|
||||||
#define PROTOCOL_PACKAGE_HEADER 0xD5 /**< 命令帧包头标识 */
|
#define PROTOCOL_PACKAGE_HEADER 0xD5 /**< 命令帧包头标识 */
|
||||||
#define PROTOCOL_BOARD_TYPE 0x03 /**< 板卡类型标识 */
|
|
||||||
/** @} */
|
/** @} */
|
||||||
|
|
||||||
/** @name 命令长度限制
|
/** @name 命令长度限制
|
||||||
* @{ */
|
* @{ */
|
||||||
#define COMMAND_MIN_LEN 2 /**< 最小命令长度,如"M1" */
|
#define COMMAND_MIN_LEN 2 /**< 最小命令长度,如"M1" */
|
||||||
#define PROTOCOL_MIN_FRAME_LEN (3 + COMMAND_MIN_LEN + 1) /**< 最小完整帧长度:header+type+len+payload+crc = 6 */
|
#define PROTOCOL_MIN_FRAME_LEN (3 + COMMAND_MIN_LEN + 1) /**< 最小完整帧长度:header+type+len+payload+crc = 6 */
|
||||||
#define PROTOCOL_MAX_FRAME_LEN 16 /**< 最大完整帧长度 */
|
#define PROTOCOL_MAX_FRAME_LEN 32 /**< 最大完整帧长度 */
|
||||||
/** @} */
|
/** @} */
|
||||||
|
|
||||||
/** @name 响应帧标识符
|
/** @name 响应帧标识符
|
||||||
* @{ */
|
* @{ */
|
||||||
#define RESP_HEADER 0xB5 /**< 响应帧包头标识 */
|
#define RESP_HEADER 0xB5 /**< 响应帧包头标识 >**/
|
||||||
#define RESP_TYPE_OK 0xF0 /**< 成功响应类型 */
|
#define RESP_TYPE_OK 0xF0 /**< 成功响应类型 >**/
|
||||||
#define RESP_TYPE_CRC_ERR 0xF1 /**< CRC校验错误 */
|
#define RESP_TYPE_CRC_ERR 0xF1 /**< CRC校验错误 >**/
|
||||||
#define RESP_TYPE_HEADER_ERR 0xF2 /**< 包头错误 */
|
#define RESP_TYPE_HEADER_ERR 0xF2 /**< 包头错误 >**/
|
||||||
#define RESP_TYPE_TYPE_ERR 0xF3 /**< 类型错误 */
|
#define RESP_TYPE_TYPE_ERR 0xF3 /**< 板类型错误 >**/
|
||||||
#define RESP_TYPE_LEN_ERR 0xF4 /**< 长度错误 */
|
#define RESP_TYPE_LEN_ERR 0xF4 /**< 长度错误 >**/
|
||||||
|
#define RESP_TYPE_PARAM_ERR 0xFD /**< 参数错误 >**/
|
||||||
|
#define RESP_TYPE_CMD_ERR 0xFE /**< 命令错误 >**/
|
||||||
|
#define RESP_TYPE_ERR 0xFF /**< 通用错误 >**/
|
||||||
/** @} */
|
/** @} */
|
||||||
|
|
||||||
/* ============================================================================
|
/* ============================================================================
|
||||||
@@ -78,10 +84,29 @@
|
|||||||
|
|
||||||
/** @name 预设响应数据
|
/** @name 预设响应数据
|
||||||
* @{ */
|
* @{ */
|
||||||
static const uint8_t s_report_status_ok[] = { 'o', 'k' }; /**< 成功响应数据 */
|
static const uint8_t s_report_status_ok[] __attribute__((unused)) = { 'o', 'k' }; /**< 成功响应数据 */
|
||||||
static const uint8_t s_report_status_err[] = { 'e','r','r' }; /**< 错误响应数据 */
|
static const uint8_t s_report_status_err[] = { 'e','r','r' }; /**< 错误响应数据 */
|
||||||
/** @} */
|
/** @} */
|
||||||
|
|
||||||
|
void system_software_reset(void)
|
||||||
|
{
|
||||||
|
// 确保所有待处理的内存访问完成
|
||||||
|
__DSB();
|
||||||
|
|
||||||
|
// 执行系统复位
|
||||||
|
NVIC_SystemReset();
|
||||||
|
|
||||||
|
// 以下代码不会执行
|
||||||
|
while(1);
|
||||||
|
}
|
||||||
|
|
||||||
|
/* Debug output control */
|
||||||
|
#ifdef COM_DEBUG
|
||||||
|
#include <stdio.h>
|
||||||
|
#define COMMAND_DEBUG(fmt, ...) printf("[COMMAND] " fmt "\n", ##__VA_ARGS__)
|
||||||
|
#else
|
||||||
|
#define COMMAND_DEBUG(fmt, ...)
|
||||||
|
#endif
|
||||||
/* ============================================================================
|
/* ============================================================================
|
||||||
* 公共接口函数
|
* 公共接口函数
|
||||||
* ============================================================================ */
|
* ============================================================================ */
|
||||||
@@ -144,12 +169,12 @@ static void send_response(uint8_t type, const uint8_t *payload, uint8_t len)
|
|||||||
// 使用GD32E230标准库函数逐字节发送(标准库实现)
|
// 使用GD32E230标准库函数逐字节发送(标准库实现)
|
||||||
for (uint8_t i = 0; i < buf_len; i++) {
|
for (uint8_t i = 0; i < buf_len; i++) {
|
||||||
// 等待发送缓冲区空
|
// 等待发送缓冲区空
|
||||||
while (usart_flag_get(RS485_PHY, USART_FLAG_TBE) == RESET) {}
|
while (usart_flag_get(UART_PHY, USART_FLAG_TBE) == RESET) {}
|
||||||
usart_data_transmit(RS485_PHY, buf[i]);
|
usart_data_transmit(UART_PHY, buf[i]);
|
||||||
}
|
}
|
||||||
|
|
||||||
// 等待发送完成
|
// 等待发送完成
|
||||||
while (usart_flag_get(RS485_PHY, USART_FLAG_TC) == RESET) {}
|
while (usart_flag_get(UART_PHY, USART_FLAG_TC) == RESET) {}
|
||||||
|
|
||||||
// // 使用printf发送(通过重定向到串口)
|
// // 使用printf发送(通过重定向到串口)
|
||||||
// for (uint8_t i = 0; i < buf_len; i++) {
|
// for (uint8_t i = 0; i < buf_len; i++) {
|
||||||
@@ -160,6 +185,54 @@ static void send_response(uint8_t type, const uint8_t *payload, uint8_t len)
|
|||||||
// fflush(stdout);
|
// fflush(stdout);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @brief 发送协议响应帧(调试用,发送到DEBUG_UART)。
|
||||||
|
* @details 构造并发送格式为 B5 TYPE LEN [payload] CRC 的响应帧,
|
||||||
|
* 自动计算CRC校验值并通过DEBUG_UART输出。
|
||||||
|
* @param type 响应类型码(如 RESP_TYPE_OK, RESP_TYPE_CRC_ERR 等)。
|
||||||
|
* @param payload 指向响应数据的缓冲区,当len为0时可为NULL。
|
||||||
|
* @param len 响应数据长度(字节),为0时不复制payload数据。
|
||||||
|
* @note 内部使用固定大小缓冲区,超长响应将被丢弃。
|
||||||
|
* @warning 使用GD32E230标准库函数发送,确保DEBUG_UART已正确初始化。
|
||||||
|
* @ingroup Command
|
||||||
|
*/
|
||||||
|
static void send_response_debug(uint8_t type, const uint8_t *payload, uint8_t len) __attribute__((unused));
|
||||||
|
static void send_response_debug(uint8_t type, const uint8_t *payload, uint8_t len)
|
||||||
|
{
|
||||||
|
uint8_t buf_len = (uint8_t)(3 + len + 1);
|
||||||
|
uint8_t buf[16]; // 简单场景足够,必要时可增大
|
||||||
|
if (buf_len > sizeof(buf)) return; // 防御
|
||||||
|
|
||||||
|
buf[0] = RESP_HEADER;
|
||||||
|
buf[1] = type;
|
||||||
|
buf[2] = len;
|
||||||
|
|
||||||
|
// 简化逻辑:只有当len > 0且payload非空时才复制数据
|
||||||
|
if (len > 0 && payload != NULL) {
|
||||||
|
for (uint8_t i = 0; i < len; i++) {
|
||||||
|
buf[3 + i] = payload[i];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
buf[buf_len - 1] = command_sum_crc_calc(buf, buf_len);
|
||||||
|
|
||||||
|
// 使用GD32E230标准库函数逐字节发送(标准库实现)
|
||||||
|
for (uint8_t i = 0; i < buf_len; i++) {
|
||||||
|
// 等待发送缓冲区空
|
||||||
|
while (usart_flag_get(DEBUG_UART, USART_FLAG_TBE) == RESET) {}
|
||||||
|
usart_data_transmit(DEBUG_UART, buf[i]);
|
||||||
|
}
|
||||||
|
|
||||||
|
// 发送换行符 \r\n
|
||||||
|
// while (usart_flag_get(DEBUG_UART, USART_FLAG_TBE) == RESET) {}
|
||||||
|
// usart_data_transmit(DEBUG_UART, '\r');
|
||||||
|
// while (usart_flag_get(DEBUG_UART, USART_FLAG_TBE) == RESET) {}
|
||||||
|
// usart_data_transmit(DEBUG_UART, '\n');
|
||||||
|
|
||||||
|
// 等待发送完成
|
||||||
|
while (usart_flag_get(DEBUG_UART, USART_FLAG_TC) == RESET) {}
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @brief 判断字符是否为十进制数字字符。
|
* @brief 判断字符是否为十进制数字字符。
|
||||||
* @param c 待检查的字符(ASCII码值)。
|
* @param c 待检查的字符(ASCII码值)。
|
||||||
@@ -170,6 +243,57 @@ static void send_response(uint8_t type, const uint8_t *payload, uint8_t len)
|
|||||||
*/
|
*/
|
||||||
static inline bool is_dec_digit(uint8_t c) { return (c >= '0' && c <= '9'); }
|
static inline bool is_dec_digit(uint8_t c) { return (c >= '0' && c <= '9'); }
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @brief 将一个无符号整数转换为字符串并追加到缓冲区。
|
||||||
|
* @param value 要转换的数字。
|
||||||
|
* @param buffer 指向目标缓冲区的指针,转换后的字符串将写入此处。
|
||||||
|
* @return uint8_t 写入的字符数。
|
||||||
|
*/
|
||||||
|
static uint8_t uint_to_str(uint32_t value, char *buffer) {
|
||||||
|
char temp[10]; // 32位无符号整数最多10位
|
||||||
|
int i = 0;
|
||||||
|
|
||||||
|
if (value == 0) {
|
||||||
|
buffer[0] = '0';
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
// 将数字逆序转换为字符存入临时数组
|
||||||
|
while (value > 0) {
|
||||||
|
temp[i++] = (char)((value % 10) + '0');
|
||||||
|
value /= 10;
|
||||||
|
}
|
||||||
|
|
||||||
|
// 将逆序的字符串反转并存入目标缓冲区
|
||||||
|
uint8_t len = (uint8_t)i;
|
||||||
|
for (int j = 0; j < len; j++) {
|
||||||
|
buffer[j] = temp[--i];
|
||||||
|
}
|
||||||
|
|
||||||
|
return len;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @brief 将有符号整数转换为字符串
|
||||||
|
* @param value 要转换的数字
|
||||||
|
* @param buffer 目标缓冲区
|
||||||
|
* @return uint8_t 写入的字符数
|
||||||
|
*/
|
||||||
|
static uint8_t __attribute__((unused)) int_to_str(int32_t value, char *buffer) {
|
||||||
|
uint8_t len = 0;
|
||||||
|
|
||||||
|
if (value < 0) {
|
||||||
|
buffer[0] = '-';
|
||||||
|
len++;
|
||||||
|
// 处理最小负数溢出问题 (虽然int16不会溢出int32,但为了健壮性)
|
||||||
|
// 这里直接取反转为正数处理
|
||||||
|
value = -value;
|
||||||
|
}
|
||||||
|
|
||||||
|
len += uint_to_str((uint32_t)value, &buffer[len]);
|
||||||
|
return len;
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @brief 从缓冲区解析十进制无符号整数。
|
* @brief 从缓冲区解析十进制无符号整数。
|
||||||
* @details 从指定位置开始连续读取十进制数字字符,累加构成32位无符号整数。
|
* @details 从指定位置开始连续读取十进制数字字符,累加构成32位无符号整数。
|
||||||
@@ -198,6 +322,74 @@ static uint8_t parse_uint_dec(const uint8_t *s, uint8_t n, uint32_t *out)
|
|||||||
return i;
|
return i;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @brief 通用T参数解析函数
|
||||||
|
* @details 解析命令中的T参数(定时参数),格式为T<数字>
|
||||||
|
* @param cmd 指向命令缓冲区
|
||||||
|
* @param cmd_index 当前解析位置的指针(会被更新)
|
||||||
|
* @param cmd_len 命令总长度
|
||||||
|
* @param timer_value 输出参数,存储解析到的定时值
|
||||||
|
* @return bool 解析结果
|
||||||
|
* @retval true 成功解析到T参数
|
||||||
|
* @retval false 没有T参数或解析失败
|
||||||
|
* @note 如果解析成功,cmd_index会被更新到T参数后的位置
|
||||||
|
* @ingroup Command
|
||||||
|
*/
|
||||||
|
static bool __attribute__((unused)) parse_timer_parameter(const uint8_t *cmd, uint8_t *cmd_index, uint8_t cmd_len, uint32_t *timer_value)
|
||||||
|
{
|
||||||
|
if (*cmd_index >= cmd_len || cmd[*cmd_index] != 'T') {
|
||||||
|
return false; // 没有T参数
|
||||||
|
}
|
||||||
|
|
||||||
|
uint8_t temp_index = *cmd_index + 1; // T后的位置
|
||||||
|
const uint8_t used_timer_cmd = parse_uint_dec(&cmd[temp_index], (uint8_t)(cmd_len - temp_index), timer_value);
|
||||||
|
|
||||||
|
if (used_timer_cmd == 0) {
|
||||||
|
return false; // T后面没有有效数字
|
||||||
|
}
|
||||||
|
|
||||||
|
*cmd_index = (uint8_t)(temp_index + used_timer_cmd); // 更新索引
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @brief 检查命令是否完全解析完毕
|
||||||
|
* @details 验证命令中是否还有未解析的字符,用于格式验证
|
||||||
|
* @param cmd_index 当前解析位置
|
||||||
|
* @param cmd_len 命令总长度
|
||||||
|
* @return bool 检查结果
|
||||||
|
* @retval true 命令完全解析完毕
|
||||||
|
* @retval false 还有未解析的字符
|
||||||
|
* @ingroup Command
|
||||||
|
*/
|
||||||
|
static bool __attribute__((unused)) is_command_fully_parsed(uint8_t cmd_index, uint8_t cmd_len)
|
||||||
|
{
|
||||||
|
return (cmd_index == cmd_len);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @brief 在命令字符串中查找指定参数的值
|
||||||
|
* @param cmd 指向命令起始位置(Mxxx 之后)
|
||||||
|
* @param cmd_len 命令剩余长度
|
||||||
|
* @param key 要查找的参数字符(如 'P', 'S')
|
||||||
|
* @param value 输出参数,存储解析到的值
|
||||||
|
* @return bool 是否找到该参数
|
||||||
|
*/
|
||||||
|
static bool __attribute__((unused)) find_parameter_value(const uint8_t *cmd, uint8_t cmd_len, char key, uint32_t *value)
|
||||||
|
{
|
||||||
|
for (uint8_t i = 0; i < cmd_len; i++) {
|
||||||
|
if (cmd[i] == (uint8_t)key) {
|
||||||
|
uint8_t param_idx = i + 1;
|
||||||
|
if (param_idx >= cmd_len) return false;
|
||||||
|
if (parse_uint_dec(&cmd[param_idx], cmd_len - param_idx, value) > 0) {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
/* ============================================================================
|
/* ============================================================================
|
||||||
* 命令处理函数
|
* 命令处理函数
|
||||||
* ============================================================================ */
|
* ============================================================================ */
|
||||||
@@ -219,6 +411,7 @@ static uint8_t parse_uint_dec(const uint8_t *s, uint8_t n, uint32_t *out)
|
|||||||
* @warning 假设输入帧已通过基本协议校验(包头、类型、CRC等)。
|
* @warning 假设输入帧已通过基本协议校验(包头、类型、CRC等)。
|
||||||
* @ingroup Command
|
* @ingroup Command
|
||||||
*/
|
*/
|
||||||
|
|
||||||
void handle_command(const uint8_t *frame, uint8_t len) {
|
void handle_command(const uint8_t *frame, uint8_t len) {
|
||||||
// 帧格式:D5 03 LEN [cmd] CRC; cmd 支持变长,如 "M1"、"M10"、"M201"、"M123S400",有最小长度限制和命令长度校验
|
// 帧格式:D5 03 LEN [cmd] CRC; cmd 支持变长,如 "M1"、"M10"、"M201"、"M123S400",有最小长度限制和命令长度校验
|
||||||
uint8_t cmd_len = frame[2];
|
uint8_t cmd_len = frame[2];
|
||||||
@@ -246,71 +439,68 @@ void handle_command(const uint8_t *frame, uint8_t len) {
|
|||||||
|
|
||||||
cmd_index = (uint8_t)(cmd_index + used_base_cmd); // 更新索引到命令后
|
cmd_index = (uint8_t)(cmd_index + used_base_cmd); // 更新索引到命令后
|
||||||
|
|
||||||
// 情况A:无附加参数的基础命令
|
|
||||||
if (cmd_index == cmd_len) {
|
|
||||||
// 仅基础命令,如 M1, M2, M3
|
|
||||||
switch (base_cmd) {
|
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));
|
send_response(RESP_TYPE_OK, s_report_status_ok, sizeof(s_report_status_ok));
|
||||||
return;
|
return;
|
||||||
|
|
||||||
case 2u: // M2: disable sensor report
|
/* ==========================================
|
||||||
|
* M888 软件重启命令
|
||||||
|
* ========================================== */
|
||||||
|
case 888u:
|
||||||
|
// 先发送确认响应
|
||||||
send_response(RESP_TYPE_OK, s_report_status_ok, sizeof(s_report_status_ok));
|
send_response(RESP_TYPE_OK, s_report_status_ok, sizeof(s_report_status_ok));
|
||||||
|
|
||||||
|
// 短暂延时确保响应发送完成
|
||||||
|
delay_ms(100);
|
||||||
|
|
||||||
|
// 执行软件重启
|
||||||
|
system_software_reset();
|
||||||
return;
|
return;
|
||||||
|
|
||||||
case 3u:
|
/* ==========================================
|
||||||
send_response(RESP_TYPE_OK, s_report_status_ok, sizeof(s_report_status_ok));
|
* M999 输出固件版本号命令
|
||||||
return;
|
* ========================================== */
|
||||||
|
case 999u: //M999: 输出固件版本号
|
||||||
case 4u:
|
|
||||||
send_response(RESP_TYPE_OK, s_report_status_ok, sizeof(s_report_status_ok));
|
|
||||||
return;
|
|
||||||
|
|
||||||
// case 201u: // M201命令
|
|
||||||
// send_response(RESP_TYPE_OK, s_report_status_ok, sizeof(s_report_status_ok));
|
|
||||||
// return;
|
|
||||||
|
|
||||||
default:
|
|
||||||
// 其它无参数命令在此扩展(示例:M100)处理逻辑该如何待定
|
|
||||||
// send_response(RESP_TYPE_OK, s_report_status_ok, sizeof(s_report_status_ok));
|
|
||||||
// return;
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
// 未在处理列表的无参数基础命令,回复错误
|
|
||||||
send_response(RESP_TYPE_TYPE_ERR, s_report_status_err, sizeof(s_report_status_err));
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
// 情况B:有附加参数的命令
|
|
||||||
if (cmd[cmd_index] == 'S') {
|
|
||||||
cmd_index++;
|
|
||||||
uint32_t param_value = 0;
|
|
||||||
const uint8_t used_param_cmd = parse_uint_dec(&cmd[cmd_index], (uint8_t)(cmd_len - cmd_index), ¶m_value);
|
|
||||||
if (used_param_cmd == 0) {
|
|
||||||
// 'S' 后没有数字,格式错误
|
|
||||||
send_response(RESP_TYPE_LEN_ERR, s_report_status_err, sizeof(s_report_status_err));
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
switch (base_cmd)
|
|
||||||
{
|
{
|
||||||
// case 100u:
|
char version_str[16];
|
||||||
// // set_pwm(param_value);
|
char *p = version_str;
|
||||||
// printf("Set PWM to %u\n", param_value);
|
|
||||||
// return;
|
*p++ = 'v';
|
||||||
|
p += uint_to_str(BOARD_TYPE_CODE, p);
|
||||||
|
*p++ = '.';
|
||||||
|
p += uint_to_str(FW_VERSION_MAJOR, p);
|
||||||
|
*p++ = '.';
|
||||||
|
p += uint_to_str(FW_VERSION_MINOR, p);
|
||||||
|
*p++ = '.';
|
||||||
|
p += uint_to_str(FW_VERSION_PATCH, p);
|
||||||
|
*p = '\0'; // null-terminate for printf safety
|
||||||
|
|
||||||
|
uint8_t n = (uint8_t)(p - version_str);
|
||||||
|
send_response(RESP_TYPE_OK, (uint8_t *)version_str, n);
|
||||||
|
COMMAND_DEBUG("Firmware Version: %s", version_str);
|
||||||
|
}
|
||||||
|
return;
|
||||||
|
|
||||||
|
/* ==========================================
|
||||||
|
* M9999 进入OTA模式
|
||||||
|
* ========================================== */
|
||||||
|
case 9999u: //M9999: 进入OTA模式
|
||||||
|
__disable_irq(); // 关中断,防止竞态条件
|
||||||
|
NVIC_SystemReset(); // 触发系统复位,进入Bootloader
|
||||||
|
return;
|
||||||
|
|
||||||
default:
|
default:
|
||||||
|
send_response(RESP_TYPE_CMD_ERR, s_report_status_err, sizeof(s_report_status_err));
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
send_response(RESP_TYPE_TYPE_ERR, s_report_status_err, sizeof(s_report_status_err));
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @brief 处理串口环形缓冲区中的命令数据,解析完整的协议帧。
|
* @brief 处理串口环形缓冲区中的命令数据,解析完整的协议帧。
|
||||||
* @details 本函数实现一个基于状态机的协议解析器,用于处理格式为 D5 03 LEN [cmd] CRC 的命令帧:
|
* @details 本函数实现一个基于状态机的协议解析器,用于处理格式为 D5 03 LEN [cmd] CRC 的命令帧:
|
||||||
* - 状态1:等待包头字节 PROTOCOL_PACKAGE_HEADER (0xD5)
|
* - 状态1:等待包头字节 PROTOCOL_PACKAGE_HEADER (0xD5)
|
||||||
* - 状态2:接收板卡类型字节 PROTOCOL_BOARD_TYPE (0x03)
|
* - 状态2:接收板卡类型字节 PROTOCOL_BOARD_TYPE
|
||||||
* - 状态3:接收长度字段并计算期望的完整帧长度
|
* - 状态3:接收长度字段并计算期望的完整帧长度
|
||||||
* - 状态4:继续接收剩余数据直到完整帧
|
* - 状态4:继续接收剩余数据直到完整帧
|
||||||
* - 状态5:对完整帧进行校验(包头、板卡类型、CRC)并处理
|
* - 状态5:对完整帧进行校验(包头、板卡类型、CRC)并处理
|
||||||
@@ -353,6 +543,7 @@ void command_process(void) {
|
|||||||
// 防御:缓冲溢出,复位状态机
|
// 防御:缓冲溢出,复位状态机
|
||||||
cmd_len = 0;
|
cmd_len = 0;
|
||||||
expected_cmd_len = 0;
|
expected_cmd_len = 0;
|
||||||
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
// 缓存后续字节
|
// 缓存后续字节
|
||||||
@@ -396,6 +587,11 @@ void command_process(void) {
|
|||||||
|
|
||||||
if (verification_status) {
|
if (verification_status) {
|
||||||
handle_command(cmd_buf, expected_cmd_len);
|
handle_command(cmd_buf, expected_cmd_len);
|
||||||
|
} else {
|
||||||
|
// 验证失败时清空缓冲区,避免后续帧受影响
|
||||||
|
uart_rx_irq_pause();
|
||||||
|
uart_ring_buffer_clear();
|
||||||
|
uart_rx_irq_resume();
|
||||||
}
|
}
|
||||||
|
|
||||||
// 复位,等待下一帧
|
// 复位,等待下一帧
|
||||||
@@ -405,3 +601,46 @@ void command_process(void) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @brief 执行命令(简化版)
|
||||||
|
* @details 根据命令字符串直接构造命令帧并执行,无需手动构造协议帧
|
||||||
|
* @param cmd_str 命令字符串(如"M730S0T1000"、"M731S100"等)
|
||||||
|
* @note 简化的测试函数,自动处理协议帧构造、CRC计算和命令执行
|
||||||
|
* @ingroup Command
|
||||||
|
*/
|
||||||
|
void command_execute(const char *cmd_str)
|
||||||
|
{
|
||||||
|
if (cmd_str == NULL) return;
|
||||||
|
|
||||||
|
uint8_t cmd_len = (uint8_t)strlen(cmd_str);
|
||||||
|
uint8_t frame_len = 3 + cmd_len + 1; // header + type + len + cmd + crc
|
||||||
|
uint8_t frame_buf[32]; // 简单固定缓冲区
|
||||||
|
|
||||||
|
if (frame_len > sizeof(frame_buf)) return;
|
||||||
|
|
||||||
|
// 构造命令帧
|
||||||
|
frame_buf[0] = PROTOCOL_PACKAGE_HEADER; // 0xD5
|
||||||
|
frame_buf[1] = PROTOCOL_BOARD_TYPE; // Board Type
|
||||||
|
frame_buf[2] = cmd_len; // 命令长度
|
||||||
|
|
||||||
|
// 复制命令数据
|
||||||
|
for (uint8_t i = 0; i < cmd_len; i++) {
|
||||||
|
frame_buf[3 + i] = (uint8_t)cmd_str[i];
|
||||||
|
}
|
||||||
|
|
||||||
|
// 计算CRC
|
||||||
|
uint16_t crc = 0;
|
||||||
|
for (uint8_t i = 1; i < (frame_len - 1); i++) {
|
||||||
|
crc += frame_buf[i];
|
||||||
|
}
|
||||||
|
frame_buf[frame_len - 1] = (uint8_t)(crc & 0xFF);
|
||||||
|
|
||||||
|
// 清空缓冲区并执行命令
|
||||||
|
uart_rx_irq_pause();
|
||||||
|
uart_ring_buffer_clear();
|
||||||
|
uart_rx_irq_resume();
|
||||||
|
for (uint8_t i = 0; i < frame_len; i++) {
|
||||||
|
uart_ring_buffer_put(frame_buf[i]);
|
||||||
|
}
|
||||||
|
command_process();
|
||||||
|
}
|
||||||
|
|||||||
+17
-3
@@ -37,6 +37,7 @@ OF SUCH DAMAGE.
|
|||||||
#include "uart.h"
|
#include "uart.h"
|
||||||
#include "uart_ring_buffer.h"
|
#include "uart_ring_buffer.h"
|
||||||
#include "led.h"
|
#include "led.h"
|
||||||
|
#include "board_config.h"
|
||||||
|
|
||||||
/*!
|
/*!
|
||||||
\brief this function handles NMI exception
|
\brief this function handles NMI exception
|
||||||
@@ -102,8 +103,21 @@ void SysTick_Handler(void) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
void USART0_IRQHandler(void) {
|
void USART0_IRQHandler(void) {
|
||||||
if (RESET != usart_interrupt_flag_get(USART0, USART_INT_FLAG_RBNE)) {
|
// 主配置口使用 USART0 时
|
||||||
uint8_t data = usart_data_receive(USART0);
|
if(g_usart_config.usart_periph == USART0 && g_usart_config.irq_handler != 0) {
|
||||||
(void)uart_ring_buffer_put(data); // 缓冲满时丢弃,返回值可用于统计
|
g_usart_config.irq_handler(); // 通过函数指针调用对应的处理函数
|
||||||
|
}
|
||||||
|
// 作为调试口(第二串口)时也接收数据,便于模拟上位机命令
|
||||||
|
#ifdef DEBUG_MODE
|
||||||
|
else {
|
||||||
|
usart0_irq_handler();
|
||||||
|
}
|
||||||
|
#endif
|
||||||
|
}
|
||||||
|
|
||||||
|
void USART1_IRQHandler(void) {
|
||||||
|
// 检查当前配置是否使用USART1,并且函数指针不为空
|
||||||
|
if(g_usart_config.usart_periph == USART1 && g_usart_config.irq_handler != 0) {
|
||||||
|
g_usart_config.irq_handler(); // 通过函数指针调用对应的处理函数
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
+15
-15
@@ -35,6 +35,7 @@ OF SUCH DAMAGE.
|
|||||||
#include "gd32e23x.h"
|
#include "gd32e23x.h"
|
||||||
#include "systick.h"
|
#include "systick.h"
|
||||||
#include "uart.h"
|
#include "uart.h"
|
||||||
|
#include "uart_ring_buffer.h"
|
||||||
#include "led.h"
|
#include "led.h"
|
||||||
#include "command.h"
|
#include "command.h"
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
@@ -49,35 +50,34 @@ OF SUCH DAMAGE.
|
|||||||
*/
|
*/
|
||||||
int main(void)
|
int main(void)
|
||||||
{
|
{
|
||||||
|
|
||||||
setbuf(stdout, NULL);
|
|
||||||
systick_config();
|
|
||||||
rs485_init();
|
|
||||||
|
|
||||||
led_init();
|
led_init();
|
||||||
|
mcu_detect_and_config();
|
||||||
|
|
||||||
#ifdef DEBUG_VERBOSE
|
// delay_ms(1000);
|
||||||
char hello_world[] = {"Hello World!\r\n"};
|
|
||||||
|
|
||||||
for (uint8_t i = 0; i < sizeof(hello_world); i++)
|
systick_config();
|
||||||
{
|
|
||||||
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) {}
|
uart_ring_buffer_init();
|
||||||
#endif
|
uart_init();
|
||||||
|
|
||||||
i2c_config();
|
i2c_config();
|
||||||
|
|
||||||
|
#ifdef DEBUG_MODE
|
||||||
|
printf("Hello World!\r\n");
|
||||||
|
#endif
|
||||||
|
|
||||||
#ifdef DEBUG_VERBOSE
|
#ifdef DEBUG_VERBOSE
|
||||||
i2c_scan();
|
i2c_scan();
|
||||||
|
|
||||||
i2c_bus_reset();
|
i2c_bus_reset();
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
/* ========== Command Testing ========== */
|
||||||
|
|
||||||
|
/* ========== */
|
||||||
|
|
||||||
while(1){
|
while(1){
|
||||||
command_process();
|
command_process(); /* Process UART commands */
|
||||||
delay_ms(10);
|
delay_ms(10);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
+4
-3
@@ -16,6 +16,7 @@
|
|||||||
#include <unistd.h>
|
#include <unistd.h>
|
||||||
#include <sys/wait.h>
|
#include <sys/wait.h>
|
||||||
#include "gd32e23x_usart.h"
|
#include "gd32e23x_usart.h"
|
||||||
|
#include "board_config.h"
|
||||||
|
|
||||||
#undef errno
|
#undef errno
|
||||||
extern int errno;
|
extern int errno;
|
||||||
@@ -161,10 +162,10 @@ int _execve(char *name, char **argv, char **env)
|
|||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
// USART0 printf重定向实现
|
// UART printf重定向实现
|
||||||
int __io_putchar(int ch) {
|
int __io_putchar(int ch) {
|
||||||
// 等待发送缓冲区空
|
// 等待发送缓冲区空
|
||||||
while (usart_flag_get(USART0, USART_FLAG_TBE) == RESET) {}
|
while (usart_flag_get(DEBUG_UART, USART_FLAG_TBE) == RESET) {}
|
||||||
usart_data_transmit(USART0, (uint8_t)ch);
|
usart_data_transmit(DEBUG_UART, (uint8_t)ch);
|
||||||
return ch;
|
return ch;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -53,7 +53,7 @@
|
|||||||
It is strongly recommended to include it to avoid issues caused by self-removal.
|
It is strongly recommended to include it to avoid issues caused by self-removal.
|
||||||
*/
|
*/
|
||||||
#define RCU_MODIFY(__delay) do{ \
|
#define RCU_MODIFY(__delay) do{ \
|
||||||
volatile uint32_t i,reg; \
|
__IO uint32_t i,reg; \
|
||||||
if(0 != __delay){ \
|
if(0 != __delay){ \
|
||||||
reg = RCU_CFG0; \
|
reg = RCU_CFG0; \
|
||||||
reg &= ~(RCU_CFG0_AHBPSC); \
|
reg &= ~(RCU_CFG0_AHBPSC); \
|
||||||
|
|||||||
+4
-4
@@ -12,7 +12,7 @@
|
|||||||
#include "gd32e23x.h"
|
#include "gd32e23x.h"
|
||||||
#include "systick.h"
|
#include "systick.h"
|
||||||
|
|
||||||
volatile static uint32_t delay_count = 0;
|
static __IO uint32_t delay_count = 0;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* ************************************************************************
|
* ************************************************************************
|
||||||
@@ -47,7 +47,7 @@ void delay_10us(uint32_t count)
|
|||||||
uint32_t loops_per_10us = SystemCoreClock / 1700000; // 粗略估计,每10微秒的循环次数
|
uint32_t loops_per_10us = SystemCoreClock / 1700000; // 粗略估计,每10微秒的循环次数
|
||||||
|
|
||||||
for(uint32_t i = 0; i < count; i++) {
|
for(uint32_t i = 0; i < count; i++) {
|
||||||
for(volatile uint32_t j = 0; j < loops_per_10us; j++);
|
for(__IO uint32_t j = 0; j < loops_per_10us; j++);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -95,7 +95,7 @@ void delay_decrement(void)
|
|||||||
// uint32_t loops_per_ms = SystemCoreClock / 14000; // 粗略估计
|
// uint32_t loops_per_ms = SystemCoreClock / 14000; // 粗略估计
|
||||||
|
|
||||||
// for(uint32_t i = 0; i < count; i++) {
|
// for(uint32_t i = 0; i < count; i++) {
|
||||||
// for(volatile uint32_t j = 0; j < loops_per_ms; j++);
|
// for(__IO uint32_t j = 0; j < loops_per_ms; j++);
|
||||||
// }
|
// }
|
||||||
// }
|
// }
|
||||||
|
|
||||||
@@ -113,6 +113,6 @@ void delay_decrement(void)
|
|||||||
// uint32_t loops_per_us = SystemCoreClock / 22000000; // 粗略估计,每微秒的循环次数
|
// uint32_t loops_per_us = SystemCoreClock / 22000000; // 粗略估计,每微秒的循环次数
|
||||||
|
|
||||||
// for(uint32_t i = 0; i < count; i++) {
|
// for(uint32_t i = 0; i < count; i++) {
|
||||||
// for(volatile uint32_t j = 0; j < loops_per_us; j++);
|
// for(__IO uint32_t j = 0; j < loops_per_us; j++);
|
||||||
// }
|
// }
|
||||||
// }
|
// }
|
||||||
+91
-54
@@ -3,70 +3,107 @@
|
|||||||
#include "gd32e23x_rcu.h"
|
#include "gd32e23x_rcu.h"
|
||||||
#include "gd32e23x_gpio.h"
|
#include "gd32e23x_gpio.h"
|
||||||
#include "board_config.h"
|
#include "board_config.h"
|
||||||
|
#include "uart_ring_buffer.h"
|
||||||
|
|
||||||
|
|
||||||
void rs485_init(void) {
|
void uart_init(void) {
|
||||||
|
/* 使能 GPIOA 和 USART 时钟 */
|
||||||
|
rcu_periph_clock_enable(UART_GPIO_RCU);
|
||||||
|
rcu_periph_clock_enable(UART_RCU);
|
||||||
|
|
||||||
#ifndef RS485_MAX13487
|
/* 配置 PA2 为 USART_TX,PA3 为 USART_RX */
|
||||||
/* 使能 GPIOA 和 USART0 时钟 */
|
gpio_af_set(UART_GPIO_PORT, GPIO_AF_1, UART_TX_PIN | UART_RX_PIN);
|
||||||
rcu_periph_clock_enable(RS485_GPIO_RCU);
|
|
||||||
rcu_periph_clock_enable(RS485_RCU);
|
|
||||||
|
|
||||||
/* 配置 PA2 为 USART0_TX,PA3 为 USART0_RX */
|
gpio_mode_set(UART_GPIO_PORT, GPIO_MODE_AF, GPIO_PUPD_PULLUP, UART_TX_PIN | UART_RX_PIN);
|
||||||
gpio_af_set(RS485_GPIO_PORT, GPIO_AF_1, RS485_TX_PIN | RS485_RX_PIN | RS485_EN_PIN);
|
gpio_output_options_set(UART_GPIO_PORT, GPIO_OTYPE_PP, GPIO_OSPEED_50MHZ, UART_TX_PIN | UART_RX_PIN);
|
||||||
|
|
||||||
gpio_mode_set(RS485_GPIO_PORT, GPIO_MODE_AF, GPIO_PUPD_PULLUP, RS485_TX_PIN | RS485_RX_PIN);
|
|
||||||
gpio_output_options_set(RS485_GPIO_PORT, GPIO_OTYPE_PP, GPIO_OSPEED_50MHZ, RS485_TX_PIN | RS485_RX_PIN);
|
|
||||||
|
|
||||||
gpio_mode_set(RS485_GPIO_PORT, GPIO_MODE_AF, GPIO_PUPD_NONE, RS485_EN_PIN);
|
|
||||||
gpio_output_options_set(RS485_GPIO_PORT, GPIO_OTYPE_PP, GPIO_OSPEED_50MHZ, RS485_EN_PIN);
|
|
||||||
|
|
||||||
/* 配置波特率、数据位、停止位等 */
|
/* 配置波特率、数据位、停止位等 */
|
||||||
usart_deinit(RS485_PHY);
|
usart_deinit(UART_PHY);
|
||||||
usart_word_length_set(RS485_PHY, USART_WL_8BIT);
|
usart_word_length_set(UART_PHY, USART_WL_8BIT);
|
||||||
usart_stop_bit_set(RS485_PHY, USART_STB_1BIT);
|
usart_stop_bit_set(UART_PHY, USART_STB_1BIT);
|
||||||
usart_parity_config(RS485_PHY, USART_PM_NONE);
|
usart_parity_config(UART_PHY, USART_PM_NONE);
|
||||||
usart_baudrate_set(RS485_PHY, RS485_BAUDRATE);
|
usart_baudrate_set(UART_PHY, UART_BAUDRATE);
|
||||||
usart_receive_config(RS485_PHY, USART_RECEIVE_ENABLE);
|
usart_receive_config(UART_PHY, USART_RECEIVE_ENABLE);
|
||||||
usart_transmit_config(RS485_PHY, USART_TRANSMIT_ENABLE);
|
usart_transmit_config(UART_PHY, USART_TRANSMIT_ENABLE);
|
||||||
|
|
||||||
usart_driver_assertime_config(RS485_PHY, 0x01);
|
usart_enable(UART_PHY);
|
||||||
usart_driver_deassertime_config(RS485_PHY, 0x10);
|
|
||||||
|
|
||||||
usart_rs485_driver_enable(RS485_PHY);
|
nvic_irq_enable(UART_IRQ, 0);
|
||||||
|
usart_interrupt_enable(UART_PHY, USART_INT_RBNE);
|
||||||
|
}
|
||||||
|
|
||||||
usart_enable(RS485_PHY);
|
void uart_rx_irq_pause(void) {
|
||||||
|
usart_interrupt_disable(UART_PHY, USART_INT_RBNE);
|
||||||
|
}
|
||||||
|
|
||||||
nvic_irq_enable(USART0_IRQn, 0);
|
void uart_rx_irq_resume(void) {
|
||||||
usart_interrupt_enable(RS485_PHY, USART_INT_RBNE);
|
usart_interrupt_enable(UART_PHY, USART_INT_RBNE);
|
||||||
// usart_interrupt_enable(RS485_PHY, USART_INT_IDLE);
|
}
|
||||||
|
|
||||||
#else
|
/******************************************************************************/
|
||||||
rcu_periph_clock_enable(RS485_GPIO_RCU);
|
/* 具体的中断处理函数实现 */
|
||||||
rcu_periph_clock_enable(RS485_RCU);
|
/******************************************************************************/
|
||||||
|
|
||||||
gpio_af_set(RS485_GPIO_PORT, GPIO_AF_1, GPIO_PIN_2 | GPIO_PIN_3);
|
void usart0_irq_handler(void) {
|
||||||
|
// 处理USART0的接收中断
|
||||||
/* configure USART Tx&Rx as alternate function push-pull */
|
if(usart_interrupt_flag_get(USART0, USART_INT_FLAG_RBNE)) {
|
||||||
gpio_mode_set(RS485_GPIO_PORT, GPIO_MODE_AF, GPIO_PUPD_PULLUP, RS485_TX_PIN | RS485_RX_PIN);
|
uint8_t data = usart_data_receive(USART0);
|
||||||
gpio_output_options_set(RS485_GPIO_PORT, GPIO_OTYPE_PP, GPIO_OSPEED_10MHZ, RS485_TX_PIN | RS485_RX_PIN);
|
// 使用原有的环形缓冲区处理逻辑
|
||||||
|
(void)uart_ring_buffer_put(data); // 缓冲满时丢弃,返回值可用于统计
|
||||||
/* configure RS485 EN Pin */
|
|
||||||
gpio_mode_set(RS485_GPIO_PORT, GPIO_MODE_OUTPUT, GPIO_PUPD_NONE, RS485_EN_PIN);
|
|
||||||
gpio_output_options_set(RS485_GPIO_PORT, GPIO_OTYPE_PP, GPIO_OSPEED_50MHZ, RS485_EN_PIN);
|
|
||||||
gpio_bit_write(RS485_GPIO_PORT, RS485_EN_PIN, SET);
|
|
||||||
|
|
||||||
/* USART configure */
|
|
||||||
usart_deinit(RS485_PHY);
|
|
||||||
usart_baudrate_set(RS485_PHY, RS485_BAUDRATE);
|
|
||||||
usart_receive_config(RS485_PHY, USART_RECEIVE_ENABLE);
|
|
||||||
usart_transmit_config(RS485_PHY, USART_TRANSMIT_ENABLE);
|
|
||||||
|
|
||||||
usart_enable(RS485_PHY);
|
|
||||||
|
|
||||||
nvic_irq_enable(USART0_IRQn, 0);
|
|
||||||
usart_interrupt_enable(RS485_PHY, USART_INT_RBNE);
|
|
||||||
usart_interrupt_enable(RS485_PHY, USART_INT_IDLE);
|
|
||||||
|
|
||||||
#endif // RS485_MAX13487
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// 处理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);
|
||||||
|
// 在这里添加空闲中断处理逻辑
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/* 临时调试串口初始化 (PA9/PA10 USART0) */
|
||||||
|
void debug_usart_init(void)
|
||||||
|
{
|
||||||
|
/* 使能 GPIOA 和 USART0 时钟 */
|
||||||
|
rcu_periph_clock_enable(RCU_GPIOA);
|
||||||
|
rcu_periph_clock_enable(RCU_USART0);
|
||||||
|
|
||||||
|
/* 配置 PA9(TX) 和 PA10(RX) 复用功能 */
|
||||||
|
gpio_af_set(GPIOA, GPIO_AF_1, GPIO_PIN_9 | GPIO_PIN_10);
|
||||||
|
|
||||||
|
/* 配置 GPIO 模式 */
|
||||||
|
gpio_mode_set(GPIOA, GPIO_MODE_AF, GPIO_PUPD_PULLUP, GPIO_PIN_9 | GPIO_PIN_10);
|
||||||
|
gpio_output_options_set(GPIOA, GPIO_OTYPE_PP, GPIO_OSPEED_50MHZ, GPIO_PIN_9 | GPIO_PIN_10);
|
||||||
|
|
||||||
|
/* USART0 配置 */
|
||||||
|
usart_deinit(USART0);
|
||||||
|
usart_baudrate_set(USART0, 115200U);
|
||||||
|
usart_word_length_set(USART0, USART_WL_8BIT);
|
||||||
|
usart_stop_bit_set(USART0, USART_STB_1BIT);
|
||||||
|
usart_parity_config(USART0, USART_PM_NONE);
|
||||||
|
usart_hardware_flow_rts_config(USART0, USART_RTS_DISABLE);
|
||||||
|
usart_hardware_flow_cts_config(USART0, USART_CTS_DISABLE);
|
||||||
|
usart_receive_config(USART0, USART_RECEIVE_ENABLE);
|
||||||
|
usart_transmit_config(USART0, USART_TRANSMIT_ENABLE);
|
||||||
|
|
||||||
|
/* 打开接收中断,作为辅助命令输入口 */
|
||||||
|
usart_interrupt_enable(USART0, USART_INT_RBNE);
|
||||||
|
|
||||||
|
usart_enable(USART0);
|
||||||
|
|
||||||
|
/* NVIC 使能串口0中断,优先级可略低于主口 */
|
||||||
|
nvic_irq_enable(USART0_IRQn, 1);
|
||||||
|
}
|
||||||
|
|||||||
+21
-4
@@ -6,11 +6,24 @@
|
|||||||
* @ingroup RingBuffer
|
* @ingroup RingBuffer
|
||||||
*/
|
*/
|
||||||
#include "uart_ring_buffer.h"
|
#include "uart_ring_buffer.h"
|
||||||
|
#include "gd32e23x.h"
|
||||||
|
|
||||||
static volatile uint8_t uart_rx_buffer[UART_RX_BUFFER_SIZE];
|
static __IO uint8_t uart_rx_buffer[UART_RX_BUFFER_SIZE];
|
||||||
static volatile uint8_t write_index = 0;
|
static __IO uint8_t write_index = 0;
|
||||||
static volatile uint8_t read_index = 0;
|
static __IO uint8_t read_index = 0;
|
||||||
static volatile uint32_t dropped_bytes = 0;
|
static __IO uint32_t dropped_bytes = 0;
|
||||||
|
|
||||||
|
static inline uint32_t irq_save(void) {
|
||||||
|
uint32_t primask = __get_PRIMASK();
|
||||||
|
__disable_irq();
|
||||||
|
return primask;
|
||||||
|
}
|
||||||
|
|
||||||
|
static inline void irq_restore(uint32_t primask) {
|
||||||
|
if ((primask & 0x1) == 0) {
|
||||||
|
__enable_irq();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @brief 重置环形缓冲区状态。
|
* @brief 重置环形缓冲区状态。
|
||||||
@@ -31,7 +44,9 @@ static void uart_ring_buffer_reset_state(void) {
|
|||||||
* @ingroup RingBuffer
|
* @ingroup RingBuffer
|
||||||
*/
|
*/
|
||||||
void uart_ring_buffer_init(void) {
|
void uart_ring_buffer_init(void) {
|
||||||
|
uint32_t primask = irq_save();
|
||||||
uart_ring_buffer_reset_state();
|
uart_ring_buffer_reset_state();
|
||||||
|
irq_restore(primask);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -90,7 +105,9 @@ bool uart_ring_buffer_put(uint8_t data) {
|
|||||||
* @ingroup RingBuffer
|
* @ingroup RingBuffer
|
||||||
*/
|
*/
|
||||||
void uart_ring_buffer_clear(void) {
|
void uart_ring_buffer_clear(void) {
|
||||||
|
uint32_t primask = irq_save();
|
||||||
uart_ring_buffer_reset_state();
|
uart_ring_buffer_reset_state();
|
||||||
|
irq_restore(primask);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|||||||
@@ -1,5 +1,23 @@
|
|||||||
# You can change TOOLCHAIN_DIRECTORY if you want to use different toolchain.
|
# You can change TOOLCHAIN_DIRECTORY if you want to use different toolchain.
|
||||||
set(TOOLCHAIN_DIRECTORY "${CMAKE_SOURCE_DIR}/Toolchain/xpack-arm-none-eabi-gcc-11.3.1-1.1/bin")
|
# set(TOOLCHAIN_DIRECTORY "${CMAKE_SOURCE_DIR}/Toolchain/xpack-arm-none-eabi-gcc-11.3.1-1.1/bin")
|
||||||
|
set(TOOLCHAIN_DIRECTORY "" CACHE PATH "Path to arm-none-eabi toolchain bin directory")
|
||||||
|
|
||||||
|
if(NOT TOOLCHAIN_DIRECTORY)
|
||||||
|
# Add extra auto-detect paths in this list when using another machine.
|
||||||
|
# Example: "C:/your/path/xpack-arm-none-eabi-gcc-11.3.1-1.1/bin"
|
||||||
|
set(_TOOLCHAIN_CANDIDATES
|
||||||
|
"D:/toolchain/xpack-arm-none-eabi-gcc-11.3.1-1.1/bin"
|
||||||
|
"C:/toolchain/xpack-arm-none-eabi-gcc-11.3.1-1.1/bin"
|
||||||
|
"${CMAKE_SOURCE_DIR}/Toolchain/xpack-arm-none-eabi-gcc-11.3.1-1.1/bin"
|
||||||
|
)
|
||||||
|
|
||||||
|
foreach(_candidate IN LISTS _TOOLCHAIN_CANDIDATES)
|
||||||
|
if(EXISTS "${_candidate}/arm-none-eabi-gcc.exe" OR EXISTS "${_candidate}/arm-none-eabi-gcc")
|
||||||
|
set(TOOLCHAIN_DIRECTORY "${_candidate}")
|
||||||
|
break()
|
||||||
|
endif()
|
||||||
|
endforeach()
|
||||||
|
endif()
|
||||||
|
|
||||||
set(CMAKE_C_FLAGS_DEBUG "")
|
set(CMAKE_C_FLAGS_DEBUG "")
|
||||||
set(CMAKE_CXX_FLAGS_DEBUG "")
|
set(CMAKE_CXX_FLAGS_DEBUG "")
|
||||||
@@ -8,6 +26,10 @@ set(CMAKE_C_FLAGS_RELEASE "")
|
|||||||
set(CMAKE_CXX_FLAGS_RELEASE "")
|
set(CMAKE_CXX_FLAGS_RELEASE "")
|
||||||
set(CMAKE_ASM_FLAGS_RELEASE "")
|
set(CMAKE_ASM_FLAGS_RELEASE "")
|
||||||
|
|
||||||
|
if(NOT TOOLCHAIN_DIRECTORY)
|
||||||
|
message(FATAL_ERROR "Cannot find ARM GCC toolchain. Set TOOLCHAIN_DIRECTORY cache variable or install toolchain in one of the default locations.")
|
||||||
|
endif()
|
||||||
|
|
||||||
set(CMAKE_COLOR_DIAGNOSTICS ON)
|
set(CMAKE_COLOR_DIAGNOSTICS ON)
|
||||||
|
|
||||||
set(CMAKE_EXPORT_COMPILE_COMMANDS ON CACHE INTERNAL "")
|
set(CMAKE_EXPORT_COMPILE_COMMANDS ON CACHE INTERNAL "")
|
||||||
|
|||||||
@@ -19,6 +19,9 @@ target_compile_options(${TARGET_NAME} PRIVATE
|
|||||||
"$<$<AND:$<NOT:$<CONFIG:Debug>>,$<COMPILE_LANGUAGE:C>>:-Os>"
|
"$<$<AND:$<NOT:$<CONFIG:Debug>>,$<COMPILE_LANGUAGE:C>>:-Os>"
|
||||||
"$<$<AND:$<NOT:$<CONFIG:Debug>>,$<COMPILE_LANGUAGE:CXX>>:-Os>"
|
"$<$<AND:$<NOT:$<CONFIG:Debug>>,$<COMPILE_LANGUAGE:CXX>>:-Os>"
|
||||||
|
|
||||||
|
-ffunction-sections
|
||||||
|
-fdata-sections
|
||||||
|
|
||||||
-mcpu=cortex-m23
|
-mcpu=cortex-m23
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|||||||
@@ -1,16 +1,19 @@
|
|||||||
# Project basic info
|
# Project basic info
|
||||||
set(PROJECT_NAME "gd32e23x")
|
set(PROJECT_NAME "GD32E23x_StdPeriph_Template")
|
||||||
set(VERSION_MAJOR 1)
|
set(BOARD_TYPE_CODE 20)
|
||||||
|
set(VERSION_MAJOR 0)
|
||||||
set(VERSION_MINOR 0)
|
set(VERSION_MINOR 0)
|
||||||
set(VERSION_PATCH 0)
|
set(VERSION_PATCH 1)
|
||||||
set(VERSION "V${VERSION_MAJOR}.${VERSION_MINOR}.${VERSION_PATCH}")
|
set(VERSION "V${VERSION_MAJOR}.${VERSION_MINOR}.${VERSION_PATCH}")
|
||||||
string(TIMESTAMP BUILD_DATE "%Y-%m-%d")
|
string(TIMESTAMP BUILD_DATE "%Y-%m-%d")
|
||||||
|
|
||||||
# 编译条件(如IIC类型等)
|
# 编译条件(如IIC类型等)
|
||||||
# set(IIC_TYPE "AutoDetectDriveCurrent")
|
# Example: HW-IIC_APP / HW-IIC_Bootloader
|
||||||
set(IIC_TYPE "HW-IIC")
|
# set(BUILD_VARIANT "AutoDetectDriveCurrent")
|
||||||
|
# set(BUILD_VARIANT "HW-IIC")
|
||||||
|
set(BUILD_VARIANT "APP")
|
||||||
|
|
||||||
# 其它自定义宏
|
# 其它自定义宏
|
||||||
add_definitions(-DIIC_TYPE=${IIC_TYPE})
|
add_definitions(-DBUILD_VARIANT=${BUILD_VARIANT})
|
||||||
add_definitions(-DPROJECT_VERSION="${VERSION}")
|
add_definitions(-DPROJECT_VERSION="${VERSION}")
|
||||||
add_definitions(-DBUILD_DATE="${BUILD_DATE}")
|
add_definitions(-DBUILD_DATE="${BUILD_DATE}")
|
||||||
@@ -0,0 +1,7 @@
|
|||||||
|
#pragma once
|
||||||
|
|
||||||
|
/* Auto-generated from CMake VERSION_* variables. Do not edit directly. */
|
||||||
|
#define BOARD_TYPE_CODE @BOARD_TYPE_CODE@
|
||||||
|
#define FW_VERSION_MAJOR @VERSION_MAJOR@
|
||||||
|
#define FW_VERSION_MINOR @VERSION_MINOR@
|
||||||
|
#define FW_VERSION_PATCH @VERSION_PATCH@
|
||||||
@@ -1,3 +0,0 @@
|
|||||||
@echo off
|
|
||||||
set OPENOCD_SCRIPTS=
|
|
||||||
start "" "D:\Microsoft VS Code\Code.exe"
|
|
||||||
Reference in New Issue
Block a user