Compare commits
3 Commits
750b5f3215
...
template_p
| Author | SHA1 | Date | |
|---|---|---|---|
| 1e50a94427 | |||
| 7419dec1b5 | |||
| d324d5f92a |
26
.vscode/extensions.json
vendored
26
.vscode/extensions.json
vendored
@@ -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
36
.vscode/launch.json
vendored
@@ -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
30
.vscode/settings.json
vendored
@@ -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
145
.vscode/tasks.json
vendored
@@ -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"
|
|
||||||
}
|
|
||||||
}
|
|
||||||
]
|
|
||||||
}
|
|
||||||
@@ -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)
|
||||||
@@ -45,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
|
||||||
|
|
||||||
@@ -66,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
|
||||||
|
|||||||
@@ -5,26 +5,30 @@
|
|||||||
#define GD32E23XF6 0x20
|
#define GD32E23XF6 0x20
|
||||||
#define GD32E23XF8 0x40
|
#define GD32E23XF8 0x40
|
||||||
|
|
||||||
/* >>>>>>>>>>>>>>>>>>>>[RS485 PHY DEFINE]<<<<<<<<<<<<<<<<<<<< */
|
#define PROTOCOL_BOARD_TYPE 0x01 /**< 板卡类型标识 */
|
||||||
|
|
||||||
// #define RS485_MAX13487 // RS485 PHY : MAX13487 (AutoDir)
|
#include "version.h"
|
||||||
#undef RS485_MAX13487 // RS485 PHY : SP3487 (no AutoDir)
|
|
||||||
|
|
||||||
/* >>>>>>>>>>>>>>>>>>>>[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
|
|
||||||
#undef EDDY_DRIVE_CURRENT_DETECTION // Eddy Drive Current Detection : Disable
|
|
||||||
|
|
||||||
/******************************************************************************/
|
/******************************************************************************/
|
||||||
|
|
||||||
/* Dynamic USART Configuration Structure */
|
/* Dynamic USART Configuration Structure */
|
||||||
@@ -52,8 +56,6 @@ void usart1_irq_handler(void);
|
|||||||
#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_RCU RCU_GPIOA
|
||||||
@@ -62,18 +64,47 @@ void usart1_irq_handler(void);
|
|||||||
|
|
||||||
/******************************************************************************/
|
/******************************************************************************/
|
||||||
|
|
||||||
#define RS485_RCU (g_usart_config.rcu_usart)
|
#define UART_RCU (g_usart_config.rcu_usart)
|
||||||
#define RS485_PHY (g_usart_config.usart_periph)
|
#define UART_PHY (g_usart_config.usart_periph)
|
||||||
#define RS485_IRQ (g_usart_config.irq_type)
|
#define UART_IRQ (g_usart_config.irq_type)
|
||||||
#define RS485_GPIO_RCU RCU_GPIOA
|
#define UART_GPIO_RCU RCU_GPIOA
|
||||||
#define RS485_GPIO_PORT GPIOA
|
#define UART_GPIO_PORT GPIOA
|
||||||
#define RS485_EN_PIN GPIO_PIN_1
|
#define UART_TX_PIN GPIO_PIN_2
|
||||||
#define RS485_TX_PIN GPIO_PIN_2
|
#define UART_RX_PIN GPIO_PIN_3
|
||||||
#define RS485_RX_PIN GPIO_PIN_3
|
#define UART_BAUDRATE 115200U
|
||||||
#define RS485_BAUDRATE 115200U
|
|
||||||
|
|
||||||
/******************************************************************************/
|
/******************************************************************************/
|
||||||
|
|
||||||
|
#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);
|
void mcu_detect_and_config(void);
|
||||||
uint8_t get_flash_size(void);
|
uint8_t get_flash_size(void);
|
||||||
|
|
||||||
|
|||||||
@@ -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,5 +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
|
||||||
|
|||||||
27
Inc/i2c.h
27
Inc/i2c.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,29 @@ 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);
|
||||||
|
|
||||||
|
#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 +137,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
|
||||||
|
|||||||
25
Inc/uart.h
25
Inc/uart.h
@@ -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();
|
||||||
|
|||||||
357
Src/command.c
357
Src/command.c
@@ -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,60 +439,60 @@ 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:无附加参数的基础命令
|
switch (base_cmd) {
|
||||||
if (cmd_index == cmd_len) {
|
|
||||||
// 仅基础命令,如 M1, M2, M3
|
|
||||||
switch (base_cmd) {
|
|
||||||
case 1u: // M1
|
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
|
/* ==========================================
|
||||||
// return;
|
* M888 软件重启命令
|
||||||
|
* ========================================== */
|
||||||
|
case 888u:
|
||||||
|
// 先发送确认响应
|
||||||
|
send_response(RESP_TYPE_OK, s_report_status_ok, sizeof(s_report_status_ok));
|
||||||
|
|
||||||
// case 3u:
|
// 短暂延时确保响应发送完成
|
||||||
// return;
|
delay_ms(100);
|
||||||
|
|
||||||
// case 4u:
|
// 执行软件重启
|
||||||
// return;
|
system_software_reset();
|
||||||
|
|
||||||
// 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;
|
return;
|
||||||
}
|
|
||||||
|
|
||||||
switch (base_cmd)
|
/* ==========================================
|
||||||
{
|
* M999 输出固件版本号命令
|
||||||
// case 100u:
|
* ========================================== */
|
||||||
// // set_pwm(param_value);
|
case 999u: //M999: 输出固件版本号
|
||||||
// printf("Set PWM to %u\n", param_value);
|
{
|
||||||
// return;
|
char version_str[16];
|
||||||
|
char *p = version_str;
|
||||||
|
|
||||||
|
*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));
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -307,7 +500,7 @@ void handle_command(const uint8_t *frame, uint8_t len) {
|
|||||||
* @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)并处理
|
||||||
@@ -350,6 +543,7 @@ void command_process(void) {
|
|||||||
// 防御:缓冲溢出,复位状态机
|
// 防御:缓冲溢出,复位状态机
|
||||||
cmd_len = 0;
|
cmd_len = 0;
|
||||||
expected_cmd_len = 0;
|
expected_cmd_len = 0;
|
||||||
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
// 缓存后续字节
|
// 缓存后续字节
|
||||||
@@ -393,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();
|
||||||
}
|
}
|
||||||
|
|
||||||
// 复位,等待下一帧
|
// 复位,等待下一帧
|
||||||
@@ -401,3 +600,47 @@ 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();
|
||||||
|
}
|
||||||
|
|||||||
@@ -103,10 +103,16 @@ void SysTick_Handler(void) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
void USART0_IRQHandler(void) {
|
void USART0_IRQHandler(void) {
|
||||||
// 检查当前配置是否使用USART0,并且函数指针不为空
|
// 主配置口使用 USART0 时
|
||||||
if(g_usart_config.usart_periph == USART0 && g_usart_config.irq_handler != 0) {
|
if(g_usart_config.usart_periph == USART0 && g_usart_config.irq_handler != 0) {
|
||||||
g_usart_config.irq_handler(); // 通过函数指针调用对应的处理函数
|
g_usart_config.irq_handler(); // 通过函数指针调用对应的处理函数
|
||||||
}
|
}
|
||||||
|
// 作为调试口(第二串口)时也接收数据,便于模拟上位机命令
|
||||||
|
#ifdef DEBUG_MODE
|
||||||
|
else {
|
||||||
|
usart0_irq_handler();
|
||||||
|
}
|
||||||
|
#endif
|
||||||
}
|
}
|
||||||
|
|
||||||
void USART1_IRQHandler(void) {
|
void USART1_IRQHandler(void) {
|
||||||
|
|||||||
687
Src/i2c.c
687
Src/i2c.c
@@ -34,6 +34,7 @@ void i2c_gpio_config(void) {
|
|||||||
i2c_result_t i2c_config(void) {
|
i2c_result_t i2c_config(void) {
|
||||||
/* configure I2C GPIO */
|
/* configure I2C GPIO */
|
||||||
i2c_gpio_config();
|
i2c_gpio_config();
|
||||||
|
|
||||||
/* enable I2C clock */
|
/* enable I2C clock */
|
||||||
rcu_periph_clock_enable(RCU_I2C);
|
rcu_periph_clock_enable(RCU_I2C);
|
||||||
/* configure I2C clock */
|
/* configure I2C clock */
|
||||||
@@ -90,10 +91,6 @@ i2c_result_t i2c_bus_reset(void) {
|
|||||||
gpio_bit_set(I2C_SCL_PORT, I2C_SCL_PIN); /* release SCL */
|
gpio_bit_set(I2C_SCL_PORT, I2C_SCL_PIN); /* release SCL */
|
||||||
gpio_bit_set(I2C_SDA_PORT, I2C_SDA_PIN); /* release SDA */
|
gpio_bit_set(I2C_SDA_PORT, I2C_SDA_PIN); /* release SDA */
|
||||||
|
|
||||||
#ifdef DEBUG_VERBOSE
|
|
||||||
printf("I2C bus reset: SCL = %d, SDA = %d\r\n", gpio_input_bit_get(I2C_SCL_PORT, I2C_SCL_PIN), gpio_input_bit_get(I2C_SDA_PORT, I2C_SDA_PIN));
|
|
||||||
#endif
|
|
||||||
|
|
||||||
/* 3. Double sample to confirm bus state */
|
/* 3. Double sample to confirm bus state */
|
||||||
delay_10us(1);
|
delay_10us(1);
|
||||||
bool scl_value1 = gpio_input_bit_get(I2C_SCL_PORT, I2C_SCL_PIN);
|
bool scl_value1 = gpio_input_bit_get(I2C_SCL_PORT, I2C_SCL_PIN);
|
||||||
@@ -104,27 +101,18 @@ i2c_result_t i2c_bus_reset(void) {
|
|||||||
|
|
||||||
/* 4. If SCL low -> stuck (cannot proceed) */
|
/* 4. If SCL low -> stuck (cannot proceed) */
|
||||||
if (!scl_value2) {
|
if (!scl_value2) {
|
||||||
#ifdef DEBUG_VERBOSE
|
|
||||||
printf("I2C bus reset: SCL stuck low\r\n");
|
|
||||||
#endif
|
|
||||||
return I2C_RECOVERY_SCL_STUCK_LOW;
|
return I2C_RECOVERY_SCL_STUCK_LOW;
|
||||||
}
|
}
|
||||||
|
|
||||||
/* 5. Fast path: bus idle */
|
/* 5. Fast path: bus idle */
|
||||||
if (scl_value1 && sda_value1 && scl_value2 && sda_value2) {
|
if (scl_value1 && sda_value1 && scl_value2 && sda_value2) {
|
||||||
i2c_config();
|
i2c_config();
|
||||||
#ifdef DEBUG_VERBOSE
|
|
||||||
printf("I2C bus reset: bus idle\r\n");
|
|
||||||
#endif
|
|
||||||
return I2C_RECOVERY_OK;
|
return I2C_RECOVERY_OK;
|
||||||
}
|
}
|
||||||
|
|
||||||
/* 6. SDA low: attempt to free by generating up to I2C_RECOVERY_CLOCKS pulses */
|
/* 6. SDA low: attempt to free by generating up to I2C_RECOVERY_CLOCKS pulses */
|
||||||
if (scl_value2 && !sda_value2) {
|
if (scl_value2 && !sda_value2) {
|
||||||
bool sda_released = false;
|
bool sda_released = false;
|
||||||
#ifdef DEBUG_VERBOSE
|
|
||||||
printf("I2C bus reset: SCL will try to free SDA\r\n");
|
|
||||||
#endif
|
|
||||||
for (uint8_t i = 0; i < I2C_RECOVERY_CLOCKS && !sda_released; i++) {
|
for (uint8_t i = 0; i < I2C_RECOVERY_CLOCKS && !sda_released; i++) {
|
||||||
if (!i2c_generate_scl_pulse()) {
|
if (!i2c_generate_scl_pulse()) {
|
||||||
return I2C_RECOVERY_SCL_STUCK_LOW; /* SCL failed to go high */
|
return I2C_RECOVERY_SCL_STUCK_LOW; /* SCL failed to go high */
|
||||||
@@ -137,9 +125,6 @@ i2c_result_t i2c_bus_reset(void) {
|
|||||||
return I2C_RECOVERY_SDA_STUCK_LOW;
|
return I2C_RECOVERY_SDA_STUCK_LOW;
|
||||||
}
|
}
|
||||||
/* 7. Generate a STOP condition to leave bus in idle state */
|
/* 7. Generate a STOP condition to leave bus in idle state */
|
||||||
#ifdef DEBUG_VERBOSE
|
|
||||||
printf("I2C bus reset: generating STOP condition\r\n");
|
|
||||||
#endif
|
|
||||||
gpio_bit_reset(I2C_SDA_PORT, I2C_SDA_PIN); /* SDA low */
|
gpio_bit_reset(I2C_SDA_PORT, I2C_SDA_PIN); /* SDA low */
|
||||||
delay_10us(1);
|
delay_10us(1);
|
||||||
gpio_bit_set(I2C_SCL_PORT, I2C_SCL_PIN); /* ensure SCL high */
|
gpio_bit_set(I2C_SCL_PORT, I2C_SCL_PIN); /* ensure SCL high */
|
||||||
@@ -148,9 +133,6 @@ i2c_result_t i2c_bus_reset(void) {
|
|||||||
delay_10us(1);
|
delay_10us(1);
|
||||||
}
|
}
|
||||||
|
|
||||||
#ifdef DEBUG_VERBOSE
|
|
||||||
printf("I2C bus reset: bus recovered\r\n");
|
|
||||||
#endif
|
|
||||||
/* 8. Reconfigure & enable peripheral */
|
/* 8. Reconfigure & enable peripheral */
|
||||||
i2c_config();
|
i2c_config();
|
||||||
return I2C_RECOVERY_OK;
|
return I2C_RECOVERY_OK;
|
||||||
@@ -171,10 +153,10 @@ void i2c_scan(void) {
|
|||||||
// printf("Scanning I2C bus...\r\n");
|
// printf("Scanning I2C bus...\r\n");
|
||||||
const char* msg1 = "Scanning I2C bus...\r\n";
|
const char* msg1 = "Scanning I2C bus...\r\n";
|
||||||
for (uint8_t i = 0; msg1[i] != '\0'; i++) {
|
for (uint8_t i = 0; msg1[i] != '\0'; i++) {
|
||||||
while (usart_flag_get(I2C_DEBUG_UART, USART_FLAG_TBE) == RESET) {}
|
while (usart_flag_get(UART_PHY, USART_FLAG_TBE) == RESET) {}
|
||||||
usart_data_transmit(I2C_DEBUG_UART, msg1[i]);
|
usart_data_transmit(UART_PHY, msg1[i]);
|
||||||
}
|
}
|
||||||
while (usart_flag_get(I2C_DEBUG_UART, USART_FLAG_TC) == RESET) {}
|
while (usart_flag_get(UART_PHY, USART_FLAG_TC) == RESET) {}
|
||||||
|
|
||||||
for (address = 1; address < 127; address++) {
|
for (address = 1; address < 127; address++) {
|
||||||
timeout = 0;
|
timeout = 0;
|
||||||
@@ -197,30 +179,35 @@ void i2c_scan(void) {
|
|||||||
i2c_master_addressing(I2C0, (address << 1), I2C_TRANSMITTER);
|
i2c_master_addressing(I2C0, (address << 1), I2C_TRANSMITTER);
|
||||||
timeout = 0;
|
timeout = 0;
|
||||||
|
|
||||||
// 等待地址发送完成
|
// 等待地址发送完成或错误
|
||||||
while (!i2c_flag_get(I2C0, I2C_FLAG_ADDSEND) && (timeout < I2C_TIME_OUT))
|
while (!i2c_flag_get(I2C0, I2C_FLAG_ADDSEND) && !i2c_flag_get(I2C0, I2C_FLAG_AERR) && (timeout < I2C_TIME_OUT))
|
||||||
timeout++;
|
timeout++;
|
||||||
if (timeout < I2C_TIME_OUT) {
|
|
||||||
|
if (timeout < I2C_TIME_OUT && i2c_flag_get(I2C0, I2C_FLAG_ADDSEND)) {
|
||||||
|
// 设备响应 - 清除地址标志
|
||||||
i2c_flag_clear(I2C0, I2C_FLAG_ADDSEND);
|
i2c_flag_clear(I2C0, I2C_FLAG_ADDSEND);
|
||||||
// printf("Found device at 0x%02X\r\n", address);
|
// printf("Found device at 0x%02X\r\n", address);
|
||||||
const char* msg2_prefix = "Found device at 0x";
|
const char* msg2_prefix = "Found device at 0x";
|
||||||
for (uint8_t i = 0; msg2_prefix[i] != '\0'; i++) {
|
for (uint8_t i = 0; msg2_prefix[i] != '\0'; i++) {
|
||||||
while (usart_flag_get(I2C_DEBUG_UART, USART_FLAG_TBE) == RESET) {}
|
while (usart_flag_get(UART_PHY, USART_FLAG_TBE) == RESET) {}
|
||||||
usart_data_transmit(I2C_DEBUG_UART, msg2_prefix[i]);
|
usart_data_transmit(UART_PHY, msg2_prefix[i]);
|
||||||
}
|
}
|
||||||
// 发送地址的十六进制表示
|
// 发送地址的十六进制表示
|
||||||
uint8_t hex_chars[] = "0123456789ABCDEF";
|
uint8_t hex_chars[] = "0123456789ABCDEF";
|
||||||
while (usart_flag_get(I2C_DEBUG_UART, USART_FLAG_TBE) == RESET) {}
|
while (usart_flag_get(UART_PHY, USART_FLAG_TBE) == RESET) {}
|
||||||
usart_data_transmit(I2C_DEBUG_UART, hex_chars[(address >> 4) & 0x0F]);
|
usart_data_transmit(UART_PHY, hex_chars[(address >> 4) & 0x0F]);
|
||||||
while (usart_flag_get(I2C_DEBUG_UART, USART_FLAG_TBE) == RESET) {}
|
while (usart_flag_get(UART_PHY, USART_FLAG_TBE) == RESET) {}
|
||||||
usart_data_transmit(I2C_DEBUG_UART, hex_chars[address & 0x0F]);
|
usart_data_transmit(UART_PHY, hex_chars[address & 0x0F]);
|
||||||
const char* msg2_suffix = "\r\n";
|
const char* msg2_suffix = "\r\n";
|
||||||
for (uint8_t i = 0; msg2_suffix[i] != '\0'; i++) {
|
for (uint8_t i = 0; msg2_suffix[i] != '\0'; i++) {
|
||||||
while (usart_flag_get(I2C_DEBUG_UART, USART_FLAG_TBE) == RESET) {}
|
while (usart_flag_get(UART_PHY, USART_FLAG_TBE) == RESET) {}
|
||||||
usart_data_transmit(I2C_DEBUG_UART, msg2_suffix[i]);
|
usart_data_transmit(UART_PHY, msg2_suffix[i]);
|
||||||
}
|
}
|
||||||
while (usart_flag_get(I2C_DEBUG_UART, USART_FLAG_TC) == RESET) {}
|
while (usart_flag_get(UART_PHY, USART_FLAG_TC) == RESET) {}
|
||||||
found_devices++;
|
found_devices++;
|
||||||
|
} else if (i2c_flag_get(I2C0, I2C_FLAG_AERR)) {
|
||||||
|
// 设备无响应 - 清除错误标志
|
||||||
|
i2c_flag_clear(I2C0, I2C_FLAG_AERR);
|
||||||
}
|
}
|
||||||
|
|
||||||
// 生成停止条件
|
// 生成停止条件
|
||||||
@@ -236,33 +223,80 @@ void i2c_scan(void) {
|
|||||||
// printf("No I2C devices found.\r\n");
|
// printf("No I2C devices found.\r\n");
|
||||||
const char* msg3 = "No I2C devices found.\r\n";
|
const char* msg3 = "No I2C devices found.\r\n";
|
||||||
for (uint8_t i = 0; msg3[i] != '\0'; i++) {
|
for (uint8_t i = 0; msg3[i] != '\0'; i++) {
|
||||||
while (usart_flag_get(I2C_DEBUG_UART, USART_FLAG_TBE) == RESET) {}
|
while (usart_flag_get(UART_PHY, USART_FLAG_TBE) == RESET) {}
|
||||||
usart_data_transmit(I2C_DEBUG_UART, msg3[i]);
|
usart_data_transmit(UART_PHY, msg3[i]);
|
||||||
}
|
}
|
||||||
while (usart_flag_get(I2C_DEBUG_UART, USART_FLAG_TC) == RESET) {}
|
while (usart_flag_get(UART_PHY, USART_FLAG_TC) == RESET) {}
|
||||||
} else {
|
} else {
|
||||||
// printf("Total %d I2C devices found.\r\n", found_devices);
|
// printf("Total %d I2C devices found.\r\n", found_devices);
|
||||||
const char* msg4_prefix = "Total ";
|
const char* msg4_prefix = "Total ";
|
||||||
for (uint8_t i = 0; msg4_prefix[i] != '\0'; i++) {
|
for (uint8_t i = 0; msg4_prefix[i] != '\0'; i++) {
|
||||||
while (usart_flag_get(I2C_DEBUG_UART, USART_FLAG_TBE) == RESET) {}
|
while (usart_flag_get(UART_PHY, USART_FLAG_TBE) == RESET) {}
|
||||||
usart_data_transmit(I2C_DEBUG_UART, msg4_prefix[i]);
|
usart_data_transmit(UART_PHY, msg4_prefix[i]);
|
||||||
}
|
}
|
||||||
// 发送设备数量
|
// 发送设备数量
|
||||||
if (found_devices >= 10) {
|
if (found_devices >= 10) {
|
||||||
while (usart_flag_get(I2C_DEBUG_UART, USART_FLAG_TBE) == RESET) {}
|
while (usart_flag_get(UART_PHY, USART_FLAG_TBE) == RESET) {}
|
||||||
usart_data_transmit(I2C_DEBUG_UART, '0' + (found_devices / 10));
|
usart_data_transmit(UART_PHY, '0' + (found_devices / 10));
|
||||||
}
|
}
|
||||||
while (usart_flag_get(I2C_DEBUG_UART, USART_FLAG_TBE) == RESET) {}
|
while (usart_flag_get(UART_PHY, USART_FLAG_TBE) == RESET) {}
|
||||||
usart_data_transmit(I2C_DEBUG_UART, '0' + (found_devices % 10));
|
usart_data_transmit(UART_PHY, '0' + (found_devices % 10));
|
||||||
const char* msg4_suffix = " I2C devices found.\r\n";
|
const char* msg4_suffix = " I2C devices found.\r\n";
|
||||||
for (uint8_t i = 0; msg4_suffix[i] != '\0'; i++) {
|
for (uint8_t i = 0; msg4_suffix[i] != '\0'; i++) {
|
||||||
while (usart_flag_get(I2C_DEBUG_UART, USART_FLAG_TBE) == RESET) {}
|
while (usart_flag_get(UART_PHY, USART_FLAG_TBE) == RESET) {}
|
||||||
usart_data_transmit(I2C_DEBUG_UART, msg4_suffix[i]);
|
usart_data_transmit(UART_PHY, msg4_suffix[i]);
|
||||||
}
|
}
|
||||||
while (usart_flag_get(I2C_DEBUG_UART, USART_FLAG_TC) == RESET) {}
|
while (usart_flag_get(UART_PHY, USART_FLAG_TC) == RESET) {}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @brief 内部辅助函数:等待指定的I2C标志位被设置,带有超时。
|
||||||
|
* @param[in] flag: 要等待的I2C事件标志。
|
||||||
|
* @retval i2c_result_t: I2C_RESULT_SUCCESS 或 I2C_RESULT_TIMEOUT。
|
||||||
|
*/
|
||||||
|
static i2c_result_t _i2c_wait_flag_timeout(uint32_t flag)
|
||||||
|
{
|
||||||
|
uint16_t timeout = 0;
|
||||||
|
while(!i2c_flag_get(I2C0, flag)){
|
||||||
|
if(timeout++ > I2C_TIME_OUT){
|
||||||
|
#ifdef DEBUG_VERBOSE
|
||||||
|
const char* fname = "UNKNOWN";
|
||||||
|
switch (flag) {
|
||||||
|
case I2C_FLAG_SBSEND: fname = "SBSEND"; break;
|
||||||
|
case I2C_FLAG_ADDSEND: fname = "ADDSEND"; break;
|
||||||
|
case I2C_FLAG_TBE: fname = "TBE"; break;
|
||||||
|
case I2C_FLAG_RBNE: fname = "RBNE"; break;
|
||||||
|
case I2C_FLAG_BTC: fname = "BTC"; break;
|
||||||
|
case I2C_FLAG_AERR: fname = "AERR"; break;
|
||||||
|
case I2C_FLAG_BERR: fname = "BERR"; break;
|
||||||
|
case I2C_FLAG_LOSTARB: fname = "LOSTARB"; break;
|
||||||
|
case I2C_FLAG_I2CBSY: fname = "I2CBSY"; break;
|
||||||
|
}
|
||||||
|
printf("I2C wait flag timeout: flag=0x%08X (%s)\r\n", (unsigned int)flag, fname);
|
||||||
|
#endif
|
||||||
|
return I2C_RESULT_TIMEOUT;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return I2C_RESULT_SUCCESS;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @brief 内部辅助函数:等待指定的I2C标志位被清除,带有超时。
|
||||||
|
* @param[in] flag: 要等待的I2C事件标志。
|
||||||
|
* @retval i2c_result_t: I2C_RESULT_SUCCESS 或 I2C_RESULT_TIMEOUT。
|
||||||
|
*/
|
||||||
|
static i2c_result_t _i2c_wait_flag_clear_timeout(uint32_t flag)
|
||||||
|
{
|
||||||
|
uint16_t timeout = 0;
|
||||||
|
while(i2c_flag_get(I2C0, flag)){
|
||||||
|
if(timeout++ > I2C_TIME_OUT){
|
||||||
|
return I2C_RESULT_TIMEOUT;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return I2C_RESULT_SUCCESS;
|
||||||
|
}
|
||||||
|
|
||||||
i2c_result_t i2c_write_16bits(uint8_t slave_addr, uint8_t reg_addr, uint8_t data[2]) {
|
i2c_result_t i2c_write_16bits(uint8_t slave_addr, uint8_t reg_addr, uint8_t data[2]) {
|
||||||
i2c_state_t state = I2C_STATE_START;
|
i2c_state_t state = I2C_STATE_START;
|
||||||
uint16_t timeout = 0;
|
uint16_t timeout = 0;
|
||||||
@@ -325,7 +359,7 @@ i2c_result_t i2c_write_16bits(uint8_t slave_addr, uint8_t reg_addr, uint8_t data
|
|||||||
} else {
|
} else {
|
||||||
i2c_flag_clear(I2C0, I2C_FLAG_AERR);
|
i2c_flag_clear(I2C0, I2C_FLAG_AERR);
|
||||||
timeout =0;
|
timeout =0;
|
||||||
#ifdef DEBUG_VERBOES
|
#ifdef DEBUG_VERBOSE
|
||||||
printf("IIC write failed for Error Slave Address. \n");
|
printf("IIC write failed for Error Slave Address. \n");
|
||||||
#endif
|
#endif
|
||||||
return I2C_RESULT_NACK;
|
return I2C_RESULT_NACK;
|
||||||
@@ -333,39 +367,28 @@ i2c_result_t i2c_write_16bits(uint8_t slave_addr, uint8_t reg_addr, uint8_t data
|
|||||||
|
|
||||||
case I2C_STATE_TRANSMIT_REG:
|
case I2C_STATE_TRANSMIT_REG:
|
||||||
/* wait until the transmit data buffer is empty */
|
/* wait until the transmit data buffer is empty */
|
||||||
while ((!i2c_flag_get(I2C0, I2C_FLAG_TBE)) && (timeout < I2C_TIME_OUT)) {
|
if(_i2c_wait_flag_timeout(I2C_FLAG_TBE) != I2C_RESULT_SUCCESS) {
|
||||||
timeout++;
|
|
||||||
}
|
|
||||||
if (timeout >= I2C_TIME_OUT) {
|
|
||||||
state = I2C_STATE_ERROR;
|
state = I2C_STATE_ERROR;
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
/* send register address */
|
/* send register address */
|
||||||
i2c_data_transmit(I2C0, reg_addr);
|
i2c_data_transmit(I2C0, reg_addr);
|
||||||
timeout = 0;
|
|
||||||
state = I2C_STATE_TRANSMIT_DATA;
|
state = I2C_STATE_TRANSMIT_DATA;
|
||||||
break;
|
break;
|
||||||
|
|
||||||
case I2C_STATE_TRANSMIT_DATA:
|
case I2C_STATE_TRANSMIT_DATA:
|
||||||
/* wait until the transmit data buffer is empty */
|
/* wait until the transmit data buffer is empty */
|
||||||
while ((!i2c_flag_get(I2C0, I2C_FLAG_TBE)) && (timeout < I2C_TIME_OUT)) {
|
if(_i2c_wait_flag_timeout(I2C_FLAG_TBE) != I2C_RESULT_SUCCESS) {
|
||||||
timeout++;
|
|
||||||
}
|
|
||||||
if (timeout >= I2C_TIME_OUT) {
|
|
||||||
state = I2C_STATE_ERROR;
|
state = I2C_STATE_ERROR;
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
/* send register MSB value */
|
/* send register MSB value */
|
||||||
i2c_data_transmit(I2C0, data[0]);
|
i2c_data_transmit(I2C0, data[0]);
|
||||||
timeout = 0;
|
|
||||||
|
|
||||||
/* wait until the transmit data buffer is empty */
|
/* wait until the transmit data buffer is empty */
|
||||||
while ((!i2c_flag_get(I2C0, I2C_FLAG_TBE)) && (timeout < I2C_TIME_OUT)) {
|
if(_i2c_wait_flag_timeout(I2C_FLAG_TBE) != I2C_RESULT_SUCCESS) {
|
||||||
timeout++;
|
|
||||||
}
|
|
||||||
if (timeout >= I2C_TIME_OUT) {
|
|
||||||
state = I2C_STATE_ERROR;
|
state = I2C_STATE_ERROR;
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
@@ -381,13 +404,9 @@ i2c_result_t i2c_write_16bits(uint8_t slave_addr, uint8_t reg_addr, uint8_t data
|
|||||||
|
|
||||||
/* send register LSB value */
|
/* send register LSB value */
|
||||||
i2c_data_transmit(I2C0, data[1]);
|
i2c_data_transmit(I2C0, data[1]);
|
||||||
timeout = 0;
|
|
||||||
|
|
||||||
/* wait until BTC bit is set */
|
/* wait until BTC bit is set */
|
||||||
while (!i2c_flag_get(I2C0, I2C_FLAG_BTC) && (timeout < I2C_TIME_OUT)) {
|
if(_i2c_wait_flag_timeout(I2C_FLAG_BTC) != I2C_RESULT_SUCCESS) {
|
||||||
timeout++;
|
|
||||||
}
|
|
||||||
if (timeout >= I2C_TIME_OUT) {
|
|
||||||
state = I2C_STATE_ERROR;
|
state = I2C_STATE_ERROR;
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
@@ -413,25 +432,13 @@ i2c_result_t i2c_write_16bits(uint8_t slave_addr, uint8_t reg_addr, uint8_t data
|
|||||||
return I2C_RESULT_SUCCESS;
|
return I2C_RESULT_SUCCESS;
|
||||||
|
|
||||||
case I2C_STATE_ERROR:
|
case I2C_STATE_ERROR:
|
||||||
/* send a stop condition to I2C bus */
|
/* I2C bus error, try to reset the bus and retry */
|
||||||
i2c_stop_on_bus(I2C0);
|
i2c_bus_reset();
|
||||||
|
|
||||||
timeout = 0;
|
|
||||||
while ((I2C_CTL0(I2C0) & I2C_CTL0_STOP) && (timeout < I2C_TIME_OUT)) {
|
|
||||||
timeout++;
|
|
||||||
}
|
|
||||||
if (timeout >= I2C_TIME_OUT) {
|
|
||||||
return I2C_RESULT_ERROR;
|
|
||||||
}
|
|
||||||
|
|
||||||
i2c_flag_clear(I2C0, I2C_FLAG_AERR);
|
|
||||||
i2c_flag_clear(I2C0, I2C_FLAG_BERR);
|
|
||||||
i2c_flag_clear(I2C0, I2C_FLAG_LOSTARB);
|
|
||||||
|
|
||||||
retry_count ++;
|
retry_count ++;
|
||||||
if (retry_count >= I2C_MAX_RETRY)
|
if (retry_count >= I2C_MAX_RETRY)
|
||||||
{
|
{
|
||||||
#ifdef DEBUG_VERBOES
|
#ifdef DEBUG_VERBOSE
|
||||||
printf("IIC write failed after %d retries\n", I2C_MAX_RETRY);
|
printf("IIC write failed after %d retries\n", I2C_MAX_RETRY);
|
||||||
#endif
|
#endif
|
||||||
return I2C_RESULT_ERROR;
|
return I2C_RESULT_ERROR;
|
||||||
@@ -472,11 +479,8 @@ i2c_result_t i2c_read_16bits(uint8_t slave_addr, uint8_t reg_addr, uint8_t *data
|
|||||||
case I2C_STATE_START:
|
case I2C_STATE_START:
|
||||||
timeout = 0;
|
timeout = 0;
|
||||||
|
|
||||||
// wait for bus to be idle
|
/* wait for bus to be idle */
|
||||||
while (i2c_flag_get(I2C0, I2C_FLAG_I2CBSY) && (timeout < I2C_TIME_OUT)) {
|
if(_i2c_wait_flag_clear_timeout(I2C_FLAG_I2CBSY) != I2C_RESULT_SUCCESS) {
|
||||||
timeout++;
|
|
||||||
}
|
|
||||||
if (timeout >= I2C_TIME_OUT) {
|
|
||||||
state = I2C_STATE_ERROR;
|
state = I2C_STATE_ERROR;
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
@@ -484,15 +488,11 @@ i2c_result_t i2c_read_16bits(uint8_t slave_addr, uint8_t reg_addr, uint8_t *data
|
|||||||
// send start condition
|
// send start condition
|
||||||
i2c_start_on_bus(I2C0);
|
i2c_start_on_bus(I2C0);
|
||||||
state = I2C_STATE_SEND_ADDRESS;
|
state = I2C_STATE_SEND_ADDRESS;
|
||||||
timeout = 0;
|
|
||||||
break;
|
break;
|
||||||
|
|
||||||
case I2C_STATE_SEND_ADDRESS:
|
case I2C_STATE_SEND_ADDRESS:
|
||||||
/* wait for start condition to be sent */
|
/* wait for start condition to be sent */
|
||||||
while ((!i2c_flag_get(I2C0, I2C_FLAG_SBSEND)) && (timeout < I2C_TIME_OUT)) {
|
if(_i2c_wait_flag_timeout(I2C_FLAG_SBSEND) != I2C_RESULT_SUCCESS) {
|
||||||
timeout++;
|
|
||||||
}
|
|
||||||
if (timeout >= I2C_TIME_OUT) {
|
|
||||||
state = I2C_STATE_ERROR;
|
state = I2C_STATE_ERROR;
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
@@ -540,10 +540,7 @@ i2c_result_t i2c_read_16bits(uint8_t slave_addr, uint8_t reg_addr, uint8_t *data
|
|||||||
|
|
||||||
case I2C_STATE_TRANSMIT_DATA:
|
case I2C_STATE_TRANSMIT_DATA:
|
||||||
/* wait for transmit buffer to be empty */
|
/* wait for transmit buffer to be empty */
|
||||||
while ((!i2c_flag_get(I2C0, I2C_FLAG_TBE)) && (timeout < I2C_TIME_OUT)) {
|
if(_i2c_wait_flag_timeout(I2C_FLAG_TBE) != I2C_RESULT_SUCCESS) {
|
||||||
timeout++;
|
|
||||||
}
|
|
||||||
if (timeout >= I2C_TIME_OUT) {
|
|
||||||
state = I2C_STATE_ERROR;
|
state = I2C_STATE_ERROR;
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
@@ -551,15 +548,11 @@ i2c_result_t i2c_read_16bits(uint8_t slave_addr, uint8_t reg_addr, uint8_t *data
|
|||||||
/* send register address */
|
/* send register address */
|
||||||
i2c_data_transmit(I2C0, reg_addr);
|
i2c_data_transmit(I2C0, reg_addr);
|
||||||
state = I2C_STATE_RESTART;
|
state = I2C_STATE_RESTART;
|
||||||
timeout = 0;
|
|
||||||
break;
|
break;
|
||||||
|
|
||||||
case I2C_STATE_RESTART:
|
case I2C_STATE_RESTART:
|
||||||
/* wait for byte transfer complete BTC: Bit Transfer Complete */
|
/* wait for byte transfer complete BTC: Bit Transfer Complete */
|
||||||
while ((!i2c_flag_get(I2C0, I2C_FLAG_BTC)) && (timeout < I2C_TIME_OUT)) {
|
if(_i2c_wait_flag_timeout(I2C_FLAG_BTC) != I2C_RESULT_SUCCESS) {
|
||||||
timeout++;
|
|
||||||
}
|
|
||||||
if (timeout >= I2C_TIME_OUT) {
|
|
||||||
state = I2C_STATE_ERROR;
|
state = I2C_STATE_ERROR;
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
@@ -568,11 +561,7 @@ i2c_result_t i2c_read_16bits(uint8_t slave_addr, uint8_t reg_addr, uint8_t *data
|
|||||||
i2c_start_on_bus(I2C0);
|
i2c_start_on_bus(I2C0);
|
||||||
|
|
||||||
/* wait for repeated start condition to be sent */
|
/* wait for repeated start condition to be sent */
|
||||||
timeout = 0;
|
if(_i2c_wait_flag_timeout(I2C_FLAG_SBSEND) != I2C_RESULT_SUCCESS) {
|
||||||
while ((!i2c_flag_get(I2C0, I2C_FLAG_SBSEND)) && (timeout < I2C_TIME_OUT)) {
|
|
||||||
timeout++;
|
|
||||||
}
|
|
||||||
if (timeout >= I2C_TIME_OUT) {
|
|
||||||
state = I2C_STATE_ERROR;
|
state = I2C_STATE_ERROR;
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
@@ -588,10 +577,7 @@ i2c_result_t i2c_read_16bits(uint8_t slave_addr, uint8_t reg_addr, uint8_t *data
|
|||||||
|
|
||||||
case I2C_STATE_RECEIVE_DATA:
|
case I2C_STATE_RECEIVE_DATA:
|
||||||
/* Wait for BTC (both bytes received) */
|
/* Wait for BTC (both bytes received) */
|
||||||
while ((!i2c_flag_get(I2C0, I2C_FLAG_BTC)) && (timeout < I2C_TIME_OUT)) {
|
if(_i2c_wait_flag_timeout(I2C_FLAG_BTC) != I2C_RESULT_SUCCESS) {
|
||||||
timeout++;
|
|
||||||
}
|
|
||||||
if (timeout >= I2C_TIME_OUT) {
|
|
||||||
state = I2C_STATE_ERROR;
|
state = I2C_STATE_ERROR;
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
@@ -621,13 +607,13 @@ i2c_result_t i2c_read_16bits(uint8_t slave_addr, uint8_t reg_addr, uint8_t *data
|
|||||||
return I2C_RESULT_SUCCESS;
|
return I2C_RESULT_SUCCESS;
|
||||||
|
|
||||||
case I2C_STATE_ERROR:
|
case I2C_STATE_ERROR:
|
||||||
/* send stop condition to release bus */
|
/* I2C bus error, try to reset the bus and retry */
|
||||||
i2c_stop_on_bus(I2C0);
|
i2c_bus_reset();
|
||||||
|
|
||||||
retry_count++;
|
retry_count++;
|
||||||
if (retry_count >= I2C_MAX_RETRY) {
|
if (retry_count >= I2C_MAX_RETRY) {
|
||||||
#ifdef DEBUG_VERBOES
|
#ifdef DEBUG_VERBOSE
|
||||||
printf("IIC read failed after %d retries\n", I2C_RETRY_MAX);
|
printf("IIC read failed after %d retries\n", I2C_MAX_RETRY);
|
||||||
#endif
|
#endif
|
||||||
return I2C_RESULT_ERROR;
|
return I2C_RESULT_ERROR;
|
||||||
}
|
}
|
||||||
@@ -649,6 +635,481 @@ i2c_result_t i2c_read_16bits(uint8_t slave_addr, uint8_t reg_addr, uint8_t *data
|
|||||||
return I2C_RESULT_TIMEOUT;
|
return I2C_RESULT_TIMEOUT;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/*!
|
||||||
|
\brief write data to I2C device with configurable length
|
||||||
|
\param[in] slave_addr: slave device address (7-bit)
|
||||||
|
\param[in] reg_addr: register address
|
||||||
|
\param[in] data: pointer to data buffer
|
||||||
|
\param[in] length: number of bytes to write (1-255)
|
||||||
|
\param[out] none
|
||||||
|
\retval i2c_result_t: operation result
|
||||||
|
*/
|
||||||
|
i2c_result_t i2c_write(uint8_t slave_addr, uint8_t reg_addr, uint8_t *data, uint8_t length) {
|
||||||
|
i2c_state_t state = I2C_STATE_START;
|
||||||
|
uint16_t timeout = 0;
|
||||||
|
uint8_t retry_count = 0;
|
||||||
|
uint8_t data_index = 0;
|
||||||
|
|
||||||
|
/* parameter validation */
|
||||||
|
if (data == NULL || slave_addr > 0x7F || length == 0) {
|
||||||
|
#ifdef DEBUG_VERBOSE
|
||||||
|
printf("I2C read invalid param: slave=0x%02X, len=%u, data=%p\r\n", slave_addr, length, data);
|
||||||
|
#endif
|
||||||
|
return I2C_RESULT_INVALID_PARAM;
|
||||||
|
}
|
||||||
|
|
||||||
|
while (retry_count < I2C_MAX_RETRY) {
|
||||||
|
switch (state) {
|
||||||
|
case I2C_STATE_START:
|
||||||
|
timeout = 0;
|
||||||
|
data_index = 0;
|
||||||
|
|
||||||
|
/* wait for bus to be idle */
|
||||||
|
while (i2c_flag_get(I2C0, I2C_FLAG_I2CBSY) && (timeout < I2C_TIME_OUT)) {
|
||||||
|
timeout++;
|
||||||
|
}
|
||||||
|
if (timeout >= I2C_TIME_OUT) {
|
||||||
|
state = I2C_STATE_ERROR;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
i2c_start_on_bus(I2C0);
|
||||||
|
timeout = 0;
|
||||||
|
state = I2C_STATE_SEND_ADDRESS;
|
||||||
|
break;
|
||||||
|
|
||||||
|
case I2C_STATE_SEND_ADDRESS:
|
||||||
|
/* wait for start condition to be sent. SBSEND flag */
|
||||||
|
while((!i2c_flag_get(I2C0, I2C_FLAG_SBSEND)) && (timeout < I2C_TIME_OUT)) {
|
||||||
|
timeout++;
|
||||||
|
}
|
||||||
|
if (timeout >= I2C_TIME_OUT) {
|
||||||
|
state = I2C_STATE_ERROR;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
/* send slave address */
|
||||||
|
i2c_master_addressing(I2C0, slave_addr << 1, I2C_TRANSMITTER);
|
||||||
|
timeout = 0;
|
||||||
|
state = I2C_STATE_CLEAR_ADDRESS;
|
||||||
|
break;
|
||||||
|
|
||||||
|
case I2C_STATE_CLEAR_ADDRESS:
|
||||||
|
/* wait for address to be acknowledged.ADDSEND set means i2c slave sends ACK */
|
||||||
|
while ((!i2c_flag_get(I2C0, I2C_FLAG_ADDSEND)) && (!i2c_flag_get(I2C0, I2C_FLAG_AERR)) && (timeout < I2C_TIME_OUT)) {
|
||||||
|
timeout++;
|
||||||
|
}
|
||||||
|
if (timeout >= I2C_TIME_OUT) {
|
||||||
|
state = I2C_STATE_ERROR;
|
||||||
|
break;
|
||||||
|
} else if (i2c_flag_get(I2C0, I2C_FLAG_ADDSEND)) {
|
||||||
|
i2c_flag_clear(I2C0, I2C_FLAG_ADDSEND);
|
||||||
|
timeout = 0;
|
||||||
|
state = I2C_STATE_TRANSMIT_REG;
|
||||||
|
break;
|
||||||
|
} else {
|
||||||
|
/* NACK received - address not acknowledged */
|
||||||
|
i2c_flag_clear(I2C0, I2C_FLAG_AERR);
|
||||||
|
i2c_stop_on_bus(I2C0);
|
||||||
|
timeout = 0;
|
||||||
|
#ifdef DEBUG_VERBOSE
|
||||||
|
printf("I2C write failed for Error Slave Address. \n");
|
||||||
|
#endif
|
||||||
|
return I2C_RESULT_NACK;
|
||||||
|
}
|
||||||
|
|
||||||
|
case I2C_STATE_TRANSMIT_REG:
|
||||||
|
/* wait until the transmit data buffer is empty */
|
||||||
|
if(_i2c_wait_flag_timeout(I2C_FLAG_TBE) != I2C_RESULT_SUCCESS) {
|
||||||
|
state = I2C_STATE_ERROR;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
/* send register address */
|
||||||
|
i2c_data_transmit(I2C0, reg_addr);
|
||||||
|
state = I2C_STATE_TRANSMIT_DATA;
|
||||||
|
break;
|
||||||
|
|
||||||
|
case I2C_STATE_TRANSMIT_DATA:
|
||||||
|
/* wait until the transmit data buffer is empty */
|
||||||
|
while (data_index < length) {
|
||||||
|
if(_i2c_wait_flag_timeout(I2C_FLAG_TBE) != I2C_RESULT_SUCCESS) {
|
||||||
|
state = I2C_STATE_ERROR;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
/* send data byte */
|
||||||
|
i2c_data_transmit(I2C0, data[data_index]);
|
||||||
|
data_index++;
|
||||||
|
|
||||||
|
/* check for errors */
|
||||||
|
if (i2c_flag_get(I2C0, I2C_FLAG_AERR)) {
|
||||||
|
i2c_stop_on_bus(I2C0);
|
||||||
|
return I2C_RESULT_NACK;
|
||||||
|
} else if (i2c_flag_get(I2C0, I2C_FLAG_BERR) || i2c_flag_get(I2C0, I2C_FLAG_LOSTARB)) {
|
||||||
|
i2c_stop_on_bus(I2C0);
|
||||||
|
return I2C_RESULT_ERROR;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if(state == I2C_STATE_ERROR) break;
|
||||||
|
|
||||||
|
|
||||||
|
/* check if all data has been sent */
|
||||||
|
if (data_index >= length) {
|
||||||
|
/* wait until BTC bit is set for last byte */
|
||||||
|
if(_i2c_wait_flag_timeout(I2C_FLAG_BTC) != I2C_RESULT_SUCCESS) {
|
||||||
|
state = I2C_STATE_ERROR;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
state = I2C_STATE_STOP;
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
|
||||||
|
case I2C_STATE_STOP:
|
||||||
|
/* send a stop condition to I2C bus */
|
||||||
|
i2c_stop_on_bus(I2C0);
|
||||||
|
|
||||||
|
timeout = 0;
|
||||||
|
while ((I2C_CTL0(I2C0) & I2C_CTL0_STOP) && (timeout < I2C_TIME_OUT)) {
|
||||||
|
timeout++;
|
||||||
|
}
|
||||||
|
if (timeout >= I2C_TIME_OUT) {
|
||||||
|
state = I2C_STATE_ERROR;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
/* success */
|
||||||
|
return I2C_RESULT_SUCCESS;
|
||||||
|
|
||||||
|
case I2C_STATE_ERROR:
|
||||||
|
/* I2C bus error, try to reset the bus and retry */
|
||||||
|
i2c_bus_reset();
|
||||||
|
|
||||||
|
retry_count++;
|
||||||
|
if (retry_count >= I2C_MAX_RETRY) {
|
||||||
|
#ifdef DEBUG_VERBOSE
|
||||||
|
printf("IIC write failed after %d retries\n", I2C_MAX_RETRY);
|
||||||
|
#endif
|
||||||
|
return I2C_RESULT_ERROR;
|
||||||
|
}
|
||||||
|
|
||||||
|
/* reset state machine for retry */
|
||||||
|
state = I2C_STATE_START;
|
||||||
|
timeout = 0;
|
||||||
|
|
||||||
|
/* small delay before retry */
|
||||||
|
delay_10us(10);
|
||||||
|
break;
|
||||||
|
|
||||||
|
default:
|
||||||
|
state = I2C_STATE_START;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return I2C_RESULT_TIMEOUT;
|
||||||
|
}
|
||||||
|
|
||||||
|
/*!
|
||||||
|
\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) {
|
||||||
|
i2c_state_t state = I2C_STATE_START;
|
||||||
|
uint16_t timeout = 0;
|
||||||
|
uint8_t retry_count = 0;
|
||||||
|
bool write_phase = true;
|
||||||
|
uint8_t data_index = 0;
|
||||||
|
|
||||||
|
/* parameter validation */
|
||||||
|
if (data == NULL || slave_addr > 0x7F || length == 0) {
|
||||||
|
return I2C_RESULT_INVALID_PARAM;
|
||||||
|
}
|
||||||
|
|
||||||
|
/* enable acknowledge */
|
||||||
|
i2c_ack_config(I2C0, I2C_ACK_ENABLE);
|
||||||
|
|
||||||
|
#ifdef DEBUG_VERBOSE
|
||||||
|
printf("I2C read start: slave=0x%02X reg=0x%02X len=%u\r\n", slave_addr, reg_addr, length);
|
||||||
|
#endif
|
||||||
|
|
||||||
|
while (retry_count < (uint8_t)I2C_MAX_RETRY) {
|
||||||
|
switch (state) {
|
||||||
|
case I2C_STATE_START:
|
||||||
|
timeout = 0;
|
||||||
|
data_index = 0;
|
||||||
|
|
||||||
|
/* wait for bus to be idle */
|
||||||
|
if(_i2c_wait_flag_clear_timeout(I2C_FLAG_I2CBSY) != I2C_RESULT_SUCCESS) {
|
||||||
|
state = I2C_STATE_ERROR;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
/* send start condition */
|
||||||
|
i2c_start_on_bus(I2C0);
|
||||||
|
state = I2C_STATE_SEND_ADDRESS;
|
||||||
|
break;
|
||||||
|
|
||||||
|
case I2C_STATE_SEND_ADDRESS:
|
||||||
|
/* wait for start condition to be sent */
|
||||||
|
if(_i2c_wait_flag_timeout(I2C_FLAG_SBSEND) != I2C_RESULT_SUCCESS) {
|
||||||
|
state = I2C_STATE_ERROR;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
/* send slave address */
|
||||||
|
if (write_phase) {
|
||||||
|
/* write phase: send address with write bit */
|
||||||
|
i2c_master_addressing(I2C0, (slave_addr << 1), I2C_TRANSMITTER);
|
||||||
|
} else {
|
||||||
|
/* read phase: send address with read bit */
|
||||||
|
i2c_master_addressing(I2C0, (slave_addr << 1) | 0x01, I2C_RECEIVER);
|
||||||
|
}
|
||||||
|
|
||||||
|
state = I2C_STATE_CLEAR_ADDRESS;
|
||||||
|
timeout = 0;
|
||||||
|
break;
|
||||||
|
|
||||||
|
case I2C_STATE_CLEAR_ADDRESS:
|
||||||
|
/* wait for address to be acknowledged */
|
||||||
|
while ((!i2c_flag_get(I2C0, I2C_FLAG_ADDSEND)) && (timeout < I2C_TIME_OUT)) {
|
||||||
|
timeout++;
|
||||||
|
}
|
||||||
|
if (timeout >= I2C_TIME_OUT) {
|
||||||
|
state = I2C_STATE_ERROR;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
/* debug: show address ack and error flags */
|
||||||
|
#ifdef DEBUG_VERBOSE
|
||||||
|
printf("I2C CLEAR_ADDRESS: write_phase=%d ADDSEND=%d AERR=%d\r\n",
|
||||||
|
(int)write_phase,
|
||||||
|
(int)i2c_flag_get(I2C0, I2C_FLAG_ADDSEND),
|
||||||
|
(int)i2c_flag_get(I2C0, I2C_FLAG_AERR));
|
||||||
|
#endif
|
||||||
|
|
||||||
|
if (write_phase) {
|
||||||
|
/* clear address flag (write phase) */
|
||||||
|
i2c_flag_clear(I2C0, I2C_FLAG_ADDSEND);
|
||||||
|
state = I2C_STATE_TRANSMIT_DATA;
|
||||||
|
} else {
|
||||||
|
/* READ phase: handle ACK/ACKPOS based on requested length
|
||||||
|
- length == 1: disable ACK before clearing ADDR
|
||||||
|
- length == 2: set ACKPOS_NEXT and disable ACK before clearing ADDR
|
||||||
|
- length > 2: keep ACK enabled and clear ADDR; we'll handle N-2/BTF sequence later */
|
||||||
|
if (length == 1) {
|
||||||
|
i2c_ack_config(I2C0, I2C_ACK_DISABLE);
|
||||||
|
#ifdef DEBUG_VERBOSE
|
||||||
|
printf("I2C READ phase: length=1, disabling ACK before clearing ADDSEND\r\n");
|
||||||
|
#endif
|
||||||
|
} else if (length == 2) {
|
||||||
|
i2c_ackpos_config(I2C0, I2C_ACKPOS_NEXT);
|
||||||
|
i2c_ack_config(I2C0, I2C_ACK_DISABLE);
|
||||||
|
#ifdef DEBUG_VERBOSE
|
||||||
|
printf("I2C READ phase: length=2, set ACKPOS_NEXT and disabling ACK before clearing ADDSEND\r\n");
|
||||||
|
#endif
|
||||||
|
} else {
|
||||||
|
/* length > 2: keep ACK enabled so slave will clock out data */
|
||||||
|
i2c_ack_config(I2C0, I2C_ACK_ENABLE);
|
||||||
|
#ifdef DEBUG_VERBOSE
|
||||||
|
printf("I2C READ phase: length=%u (>2), keeping ACK enabled\r\n", length);
|
||||||
|
#endif
|
||||||
|
}
|
||||||
|
|
||||||
|
/* now clear address flag to release SCL and enter data phase */
|
||||||
|
i2c_flag_clear(I2C0, I2C_FLAG_ADDSEND);
|
||||||
|
|
||||||
|
state = I2C_STATE_RECEIVE_DATA;
|
||||||
|
}
|
||||||
|
|
||||||
|
timeout = 0;
|
||||||
|
break;
|
||||||
|
|
||||||
|
case I2C_STATE_TRANSMIT_DATA:
|
||||||
|
/* wait for transmit buffer to be empty */
|
||||||
|
if(_i2c_wait_flag_timeout(I2C_FLAG_TBE) != I2C_RESULT_SUCCESS) {
|
||||||
|
state = I2C_STATE_ERROR;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
/* send register address */
|
||||||
|
i2c_data_transmit(I2C0, reg_addr);
|
||||||
|
state = I2C_STATE_RESTART;
|
||||||
|
break;
|
||||||
|
|
||||||
|
case I2C_STATE_RESTART:
|
||||||
|
/* wait for byte transfer complete */
|
||||||
|
if(_i2c_wait_flag_timeout(I2C_FLAG_BTC) != I2C_RESULT_SUCCESS) {
|
||||||
|
state = I2C_STATE_ERROR;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
#ifdef DEBUG_VERBOSE
|
||||||
|
printf("I2C RESTART: after BTC, delay 5ms and issuing repeated START\r\n");
|
||||||
|
#endif
|
||||||
|
/* small delay to allow slave device to prepare multi-byte data before repeated start */
|
||||||
|
/* increased to 5ms to improve reliability for DLPC multi-byte reads */
|
||||||
|
delay_ms(5);
|
||||||
|
|
||||||
|
/* generate repeated start condition */
|
||||||
|
i2c_start_on_bus(I2C0);
|
||||||
|
|
||||||
|
/* wait for repeated start condition to be sent */
|
||||||
|
if(_i2c_wait_flag_timeout(I2C_FLAG_SBSEND) != I2C_RESULT_SUCCESS) {
|
||||||
|
state = I2C_STATE_ERROR;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
/* send slave address with read bit */
|
||||||
|
i2c_master_addressing(I2C0, (slave_addr << 1), I2C_RECEIVER);
|
||||||
|
|
||||||
|
/* switch to read phase */
|
||||||
|
write_phase = false;
|
||||||
|
state = I2C_STATE_CLEAR_ADDRESS;
|
||||||
|
timeout = 0;
|
||||||
|
#ifdef DEBUG_VERBOSE
|
||||||
|
printf("I2C addressing sent for read (slave=0x%02X)\r\n", slave_addr);
|
||||||
|
#endif
|
||||||
|
break;
|
||||||
|
|
||||||
|
case I2C_STATE_RECEIVE_DATA:
|
||||||
|
#ifdef DEBUG_VERBOSE
|
||||||
|
printf("I2C RECEIVE_DATA: expecting %u bytes\r\n", length);
|
||||||
|
#endif
|
||||||
|
if (length == 1) {
|
||||||
|
/* single byte read */
|
||||||
|
if(_i2c_wait_flag_timeout(I2C_FLAG_RBNE) != I2C_RESULT_SUCCESS) {
|
||||||
|
state = I2C_STATE_ERROR;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
data[0] = i2c_data_receive(I2C0);
|
||||||
|
state = I2C_STATE_STOP;
|
||||||
|
} else if (length == 2) {
|
||||||
|
/* two bytes read */
|
||||||
|
if(_i2c_wait_flag_timeout(I2C_FLAG_BTC) != I2C_RESULT_SUCCESS) {
|
||||||
|
state = I2C_STATE_ERROR;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
/* send STOP before reading the last two bytes */
|
||||||
|
i2c_stop_on_bus(I2C0);
|
||||||
|
/* read the two bytes back-to-back */
|
||||||
|
data[0] = i2c_data_receive(I2C0);
|
||||||
|
data[1] = i2c_data_receive(I2C0);
|
||||||
|
state = I2C_STATE_STOP;
|
||||||
|
} else {
|
||||||
|
/* multi-byte read (length > 2) */
|
||||||
|
while (data_index < length) {
|
||||||
|
if (data_index < length - 2) {
|
||||||
|
/* normal bytes: wait for RBNE and read with ACK */
|
||||||
|
if(_i2c_wait_flag_timeout(I2C_FLAG_RBNE) != I2C_RESULT_SUCCESS) {
|
||||||
|
state = I2C_STATE_ERROR;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
data[data_index] = i2c_data_receive(I2C0);
|
||||||
|
data_index++;
|
||||||
|
} else if (data_index == length - 2) {
|
||||||
|
/* second to last byte: wait for BTF, then disable ACK and read */
|
||||||
|
if(_i2c_wait_flag_timeout(I2C_FLAG_BTC) != I2C_RESULT_SUCCESS) {
|
||||||
|
state = I2C_STATE_ERROR;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
/* disable ACK before reading N-1 byte */
|
||||||
|
i2c_ack_config(I2C0, I2C_ACK_DISABLE);
|
||||||
|
/* send STOP before reading N-1 byte */
|
||||||
|
i2c_stop_on_bus(I2C0);
|
||||||
|
/* read N-1 byte */
|
||||||
|
data[data_index] = i2c_data_receive(I2C0);
|
||||||
|
data_index++;
|
||||||
|
} else {
|
||||||
|
/* last byte: wait for RBNE and read */
|
||||||
|
if(_i2c_wait_flag_timeout(I2C_FLAG_RBNE) != I2C_RESULT_SUCCESS) {
|
||||||
|
state = I2C_STATE_ERROR;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
/* read last byte */
|
||||||
|
data[data_index] = i2c_data_receive(I2C0);
|
||||||
|
data_index++;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if(state == I2C_STATE_ERROR) break;
|
||||||
|
state = I2C_STATE_STOP;
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
|
||||||
|
case I2C_STATE_STOP:
|
||||||
|
/* wait for stop condition to complete */
|
||||||
|
while ((I2C_CTL0(I2C0) & I2C_CTL0_STOP) && (timeout < I2C_TIME_OUT)) {
|
||||||
|
timeout++;
|
||||||
|
}
|
||||||
|
if (timeout >= I2C_TIME_OUT) {
|
||||||
|
state = I2C_STATE_ERROR;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
/* reset ACK configuration for next operation */
|
||||||
|
i2c_ack_config(I2C0, I2C_ACK_ENABLE);
|
||||||
|
i2c_ackpos_config(I2C0, I2C_ACKPOS_CURRENT);
|
||||||
|
|
||||||
|
/* success */
|
||||||
|
return I2C_RESULT_SUCCESS;
|
||||||
|
|
||||||
|
case I2C_STATE_ERROR:
|
||||||
|
/* I2C bus error, try to reset the bus and retry */
|
||||||
|
#ifdef DEBUG_VERBOSE
|
||||||
|
printf("I2C_STATE_ERROR: resetting bus and retrying (retry_count=%u)\r\n", retry_count);
|
||||||
|
#endif
|
||||||
|
i2c_bus_reset();
|
||||||
|
|
||||||
|
/* reset ACK configuration */
|
||||||
|
i2c_ack_config(I2C0, I2C_ACK_ENABLE);
|
||||||
|
i2c_ackpos_config(I2C0, I2C_ACKPOS_CURRENT);
|
||||||
|
|
||||||
|
retry_count++;
|
||||||
|
if (retry_count >= I2C_MAX_RETRY) {
|
||||||
|
#ifdef DEBUG_VERBOSE
|
||||||
|
/* Print diagnostic flags to help root-cause analysis */
|
||||||
|
printf("I2C read final failure after %d retries, last_state=%d\r\n", I2C_MAX_RETRY, state);
|
||||||
|
printf(" FLAGS: AERR=%d BERR=%d LOSTARB=%d I2CBSY=%d\r\n",
|
||||||
|
(int)i2c_flag_get(I2C0, I2C_FLAG_AERR),
|
||||||
|
(int)i2c_flag_get(I2C0, I2C_FLAG_BERR),
|
||||||
|
(int)i2c_flag_get(I2C0, I2C_FLAG_LOSTARB),
|
||||||
|
(int)i2c_flag_get(I2C0, I2C_FLAG_I2CBSY));
|
||||||
|
#endif
|
||||||
|
#ifdef DEBUG_VERBOSE
|
||||||
|
printf("I2C read failed after %d retries\n", I2C_MAX_RETRY);
|
||||||
|
#endif
|
||||||
|
return I2C_RESULT_ERROR;
|
||||||
|
}
|
||||||
|
|
||||||
|
/* reset state machine for retry */
|
||||||
|
state = I2C_STATE_START;
|
||||||
|
write_phase = true;
|
||||||
|
timeout = 0;
|
||||||
|
|
||||||
|
/* small delay before retry */
|
||||||
|
delay_10us(10);
|
||||||
|
break;
|
||||||
|
|
||||||
|
default:
|
||||||
|
state = I2C_STATE_START;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
/* timeout path: provide debug info */
|
||||||
|
#ifdef DEBUG_VERBOSE
|
||||||
|
printf("I2C read timeout (end state). slave=0x%02X, len=%u\r\n", slave_addr, length);
|
||||||
|
printf(" FLAGS: AERR=%d BERR=%d LOSTARB=%d I2CBSY=%d\r\n",
|
||||||
|
(int)i2c_flag_get(I2C0, I2C_FLAG_AERR),
|
||||||
|
(int)i2c_flag_get(I2C0, I2C_FLAG_BERR),
|
||||||
|
(int)i2c_flag_get(I2C0, I2C_FLAG_LOSTARB),
|
||||||
|
(int)i2c_flag_get(I2C0, I2C_FLAG_I2CBSY));
|
||||||
|
#endif
|
||||||
|
return I2C_RESULT_TIMEOUT;
|
||||||
|
}
|
||||||
|
|
||||||
#ifdef DEBUG_VERBOSE
|
#ifdef DEBUG_VERBOSE
|
||||||
/*!
|
/*!
|
||||||
\brief get status string for debugging
|
\brief get status string for debugging
|
||||||
|
|||||||
22
Src/main.c
22
Src/main.c
@@ -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>
|
||||||
@@ -52,26 +53,31 @@ int main(void)
|
|||||||
led_init();
|
led_init();
|
||||||
mcu_detect_and_config();
|
mcu_detect_and_config();
|
||||||
|
|
||||||
setbuf(stdout, NULL);
|
// delay_ms(1000);
|
||||||
|
|
||||||
systick_config();
|
systick_config();
|
||||||
rs485_init();
|
|
||||||
|
|
||||||
printf("Flash size: %d Kbytes\n", get_flash_size());
|
uart_ring_buffer_init();
|
||||||
|
uart_init();
|
||||||
#ifdef DEBUG_VERBOSE
|
|
||||||
printf("Hello World!\r\n");
|
|
||||||
#endif
|
|
||||||
|
|
||||||
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);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -162,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(RS485_PHY, USART_FLAG_TBE) == RESET) {}
|
while (usart_flag_get(DEBUG_UART, USART_FLAG_TBE) == RESET) {}
|
||||||
usart_data_transmit(RS485_PHY, (uint8_t)ch);
|
usart_data_transmit(DEBUG_UART, (uint8_t)ch);
|
||||||
return ch;
|
return ch;
|
||||||
}
|
}
|
||||||
|
|||||||
116
Src/uart.c
116
Src/uart.c
@@ -6,71 +6,39 @@
|
|||||||
#include "uart_ring_buffer.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(RS485_IRQ, 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);
|
|
||||||
|
|
||||||
/* configure USART Tx&Rx as alternate function push-pull */
|
|
||||||
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_10MHZ, RS485_TX_PIN | RS485_RX_PIN);
|
|
||||||
|
|
||||||
/* 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
|
|
||||||
}
|
|
||||||
|
|
||||||
/******************************************************************************/
|
/******************************************************************************/
|
||||||
/* 具体的中断处理函数实现 */
|
/* 具体的中断处理函数实现 */
|
||||||
@@ -105,3 +73,37 @@ void usart1_irq_handler(void) {
|
|||||||
// 在这里添加空闲中断处理逻辑
|
// 在这里添加空闲中断处理逻辑
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/* 临时调试串口初始化 (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);
|
||||||
|
}
|
||||||
|
|||||||
@@ -6,12 +6,25 @@
|
|||||||
* @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 volatile uint8_t uart_rx_buffer[UART_RX_BUFFER_SIZE];
|
||||||
static volatile uint8_t write_index = 0;
|
static volatile uint8_t write_index = 0;
|
||||||
static volatile uint8_t read_index = 0;
|
static volatile uint8_t read_index = 0;
|
||||||
static volatile uint32_t dropped_bytes = 0;
|
static volatile 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 == 0U) {
|
||||||
|
__enable_irq();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @brief 重置环形缓冲区状态。
|
* @brief 重置环形缓冲区状态。
|
||||||
* @details 将读指针、写指针与丢弃计数清零,不清空数据区内容。
|
* @details 将读指针、写指针与丢弃计数清零,不清空数据区内容。
|
||||||
@@ -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 irq_key = irq_save();
|
||||||
uart_ring_buffer_reset_state();
|
uart_ring_buffer_reset_state();
|
||||||
|
irq_restore(irq_key);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -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 irq_key = irq_save();
|
||||||
uart_ring_buffer_reset_state();
|
uart_ring_buffer_reset_state();
|
||||||
|
irq_restore(irq_key);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|||||||
@@ -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}")
|
||||||
7
cmake/version.h.in
Normal file
7
cmake/version.h.in
Normal file
@@ -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@
|
||||||
Reference in New Issue
Block a user