generated from hulk/gd32e23x_template_cmake_vscode
Compare commits
11
Commits
59f6e917fd
...
main
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
b09d6fb040 | ||
|
|
b6da450f8a | ||
|
|
1cad4e44b5 | ||
|
|
de6afa1717 | ||
|
|
57963d179a | ||
|
|
8d5c33236d | ||
|
|
a40c2ec793 | ||
|
|
e81c902341 | ||
|
|
07b631b254 | ||
|
|
89e7191cd5 | ||
|
|
a4f585bbaa |
Vendored
+2
@@ -0,0 +1,2 @@
|
|||||||
|
erase
|
||||||
|
exit
|
||||||
Vendored
+7
-25
@@ -1,26 +1,8 @@
|
|||||||
{
|
{
|
||||||
"recommendations": [
|
"recommendations": [
|
||||||
"ms-vscode.cmake-tools",
|
"marus25.cortex-debug",
|
||||||
"ms-vscode.cpptools",
|
"ms-vscode.cmake-tools",
|
||||||
"ms-vscode.cpptools-extension-pack",
|
"twxs.cmake",
|
||||||
"ms-vscode.cpptools-themes",
|
"ms-vscode.cpptools"
|
||||||
"ms-vscode.vscode-embedded-tools",
|
]
|
||||||
"ms-vscode.hexeditor",
|
}
|
||||||
"ms-vscode.notepadplusplus-keybindings",
|
|
||||||
"twxs.cmake",
|
|
||||||
"xaver.clang-format",
|
|
||||||
"marus25.cortex-debug",
|
|
||||||
"cheshirekow.cmake-format",
|
|
||||||
"mcu-debug.debug-tracker-vscode",
|
|
||||||
"mcu-debug.memory-view",
|
|
||||||
"mcu-debug.peripheral-viewer",
|
|
||||||
"mcu-debug.rtos-views",
|
|
||||||
"trond-snekvik.gnu-mapfiles",
|
|
||||||
"zixuanwang.linkerscript",
|
|
||||||
"gurumukhi.selected-lines-count",
|
|
||||||
"gruntfuggly.todo-tree",
|
|
||||||
"vscode-icons-team.vscode-icons",
|
|
||||||
"jeff-hykin.better-cpp-syntax",
|
|
||||||
"dan-c-underwood.arm"
|
|
||||||
]
|
|
||||||
}
|
|
||||||
|
|||||||
Vendored
+4
@@ -0,0 +1,4 @@
|
|||||||
|
loadfile Application.hex
|
||||||
|
r
|
||||||
|
go
|
||||||
|
exit
|
||||||
Vendored
+32
-10
@@ -9,20 +9,19 @@
|
|||||||
"type": "cortex-debug",
|
"type": "cortex-debug",
|
||||||
"runToEntryPoint": "main",
|
"runToEntryPoint": "main",
|
||||||
"showDevDebugOutput": "none",
|
"showDevDebugOutput": "none",
|
||||||
"gdbPath": "${workspaceFolder}/Toolchain/xpack-arm-none-eabi-gcc-11.3.1-1.1/bin/arm-none-eabi-gdb.exe",
|
"gdbPath": "${env:GD32_GCC_BIN}/arm-none-eabi-gdb.exe",
|
||||||
"servertype": "openocd",
|
"servertype": "openocd",
|
||||||
"serverpath": "${workspaceFolder}/Toolchain/xpack-openocd-0.11.0-3/bin/openocd.exe",
|
"serverpath": "${env:GD32_OPENOCD_BIN}/openocd.exe",
|
||||||
"svdFile": "${workspaceFolder}/GD32E230.svd",
|
"svdFile": "${workspaceFolder}/GD32E230.svd",
|
||||||
"liveWatch": {
|
|
||||||
"enabled": true,
|
|
||||||
"samplesPerSecond": 1
|
|
||||||
},
|
|
||||||
"configFiles": [
|
"configFiles": [
|
||||||
"${workspaceFolder}/Toolchain/xpack-openocd-0.11.0-3/scripts/target/openocd_gdlink_gd32e23x.cfg"
|
"${env:GD32_OPENOCD_SCRIPTS}/${config:gd32.openocd.targetConfig}"
|
||||||
],
|
],
|
||||||
"serverArgs": [
|
"serverArgs": [
|
||||||
"-s", "${workspaceFolder}/Toolchain/xpack-openocd-0.11.0-3/scripts"
|
"-s", "${env:GD32_OPENOCD_SCRIPTS}"
|
||||||
],
|
],
|
||||||
|
"env": {
|
||||||
|
"OPENOCD_SCRIPTS": "${env:GD32_OPENOCD_SCRIPTS}"
|
||||||
|
},
|
||||||
"searchDir": [
|
"searchDir": [
|
||||||
"${workspaceFolder}"
|
"${workspaceFolder}"
|
||||||
],
|
],
|
||||||
@@ -30,7 +29,30 @@
|
|||||||
"preRestartCommands": [
|
"preRestartCommands": [
|
||||||
"load",
|
"load",
|
||||||
"continue"
|
"continue"
|
||||||
],
|
]
|
||||||
},
|
},
|
||||||
|
{
|
||||||
|
"cwd": "${workspaceFolder}",
|
||||||
|
"executable": "${workspaceFolder}/Build/Debug/Application/Application.elf",
|
||||||
|
"name": "Debug with J-Link",
|
||||||
|
"request": "launch",
|
||||||
|
"type": "cortex-debug",
|
||||||
|
"runToEntryPoint": "main",
|
||||||
|
"showDevDebugOutput": "none",
|
||||||
|
"gdbPath": "${env:GD32_GCC_BIN}/arm-none-eabi-gdb.exe",
|
||||||
|
"servertype": "jlink",
|
||||||
|
"device": "${config:gd32.jlink.device}",
|
||||||
|
"interface": "${config:gd32.jlink.interface}",
|
||||||
|
"svdFile": "${workspaceFolder}/GD32E230.svd",
|
||||||
|
"liveWatch": {
|
||||||
|
"enabled": true,
|
||||||
|
"samplesPerSecond": 1
|
||||||
|
},
|
||||||
|
"preLaunchTask": "Build",
|
||||||
|
"preRestartCommands": [
|
||||||
|
"load",
|
||||||
|
"continue"
|
||||||
|
]
|
||||||
|
}
|
||||||
]
|
]
|
||||||
}
|
}
|
||||||
|
|||||||
Vendored
+23
@@ -0,0 +1,23 @@
|
|||||||
|
# DAPLink-friendly GD32E23x flash/debug config
|
||||||
|
# Difference vs target/openocd_gdlink_gd32e23x.cfg:
|
||||||
|
# - no connect_assert_srst (breaks target examination with the current DAPLink)
|
||||||
|
adapter driver cmsis-dap
|
||||||
|
transport select swd
|
||||||
|
|
||||||
|
source [find target/swj-dp.tcl]
|
||||||
|
source [find mem_helper.tcl]
|
||||||
|
|
||||||
|
set _CHIPNAME gd32e23x
|
||||||
|
swj_newdap $_CHIPNAME cpu -irlen 4 -ircapture 0x1 -irmask 0xf -expected-id 0x0BF11477
|
||||||
|
dap create $_CHIPNAME.dap -chain-position $_CHIPNAME.cpu
|
||||||
|
|
||||||
|
set _TARGETNAME $_CHIPNAME.cpu
|
||||||
|
target create $_TARGETNAME cortex_m -dap $_CHIPNAME.dap
|
||||||
|
$_TARGETNAME configure -work-area-phys 0x20000000 -work-area-size 0x1000 -work-area-backup 0
|
||||||
|
|
||||||
|
flash bank $_CHIPNAME.flash gd32e23x 0x08000000 0 0 0 $_TARGETNAME
|
||||||
|
|
||||||
|
adapter speed 1000
|
||||||
|
reset_config srst_only srst_nogate
|
||||||
|
|
||||||
|
cortex_m reset_config sysresetreq
|
||||||
Vendored
+2
@@ -0,0 +1,2 @@
|
|||||||
|
r
|
||||||
|
exit
|
||||||
Vendored
+11
-26
@@ -1,30 +1,15 @@
|
|||||||
{
|
{
|
||||||
"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.configureOnOpen": true,
|
||||||
"cmake.buildDirectory": "${workspaceFolder}/Build",
|
"cmake.useCMakePresets": "always",
|
||||||
"vcpkg.storageLocation": "C:\\Dev\\Tools\\vcpkg",
|
"C_Cpp.default.configurationProvider": "ms-vscode.cmake-tools",
|
||||||
|
"cortex-debug.variableUseNaturalFormat": true,
|
||||||
|
"gd32.openocd.targetConfig": "target/openocd_gdlink_gd32e23x.cfg",
|
||||||
|
"gd32.openocd.massEraseCommand": "gd32e23x mass_erase 0",
|
||||||
|
"gd32.jlink.device": "GD32E230F8",
|
||||||
|
"gd32.jlink.interface": "SWD",
|
||||||
|
"gd32.jlink.speed": "4000",
|
||||||
"files.associations": {
|
"files.associations": {
|
||||||
"*.h": "c",
|
"*.h": "c",
|
||||||
"*.c": "c",
|
"*.c": "c"
|
||||||
"array": "c",
|
}
|
||||||
"string": "c",
|
}
|
||||||
"string_view": "c",
|
|
||||||
"ranges": "c",
|
|
||||||
"span": "c"
|
|
||||||
},
|
|
||||||
"cortex-debug.variableUseNaturalFormat": true,
|
|
||||||
}
|
|
||||||
|
|||||||
Vendored
+221
-112
@@ -1,116 +1,6 @@
|
|||||||
{
|
{
|
||||||
"version": "2.0.0",
|
"version": "2.0.0",
|
||||||
"tasks": [
|
"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",
|
"label": "Build",
|
||||||
"type": "cmake",
|
"type": "cmake",
|
||||||
@@ -130,7 +20,8 @@
|
|||||||
],
|
],
|
||||||
"options": {
|
"options": {
|
||||||
"environment": {
|
"environment": {
|
||||||
"CLICOLOR_FORCE": "1"
|
"CLICOLOR_FORCE": "1",
|
||||||
|
"OPENOCD_SCRIPTS": "${env:GD32_OPENOCD_SCRIPTS}"
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
"presentation": {
|
"presentation": {
|
||||||
@@ -140,6 +31,224 @@
|
|||||||
"id": "code",
|
"id": "code",
|
||||||
"tooltip": "Build"
|
"tooltip": "Build"
|
||||||
}
|
}
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"label": "Build and Flash",
|
||||||
|
"dependsOn": [
|
||||||
|
"Build",
|
||||||
|
"Flash MCU"
|
||||||
|
],
|
||||||
|
"dependsOrder": "sequence",
|
||||||
|
"group": "build",
|
||||||
|
"icon": {
|
||||||
|
"id": "insert",
|
||||||
|
"tooltip": "Build and Flash"
|
||||||
|
}
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"label": "Flash MCU",
|
||||||
|
"type": "process",
|
||||||
|
"command": "${env:GD32_OPENOCD_BIN}/openocd.exe",
|
||||||
|
"args": [
|
||||||
|
"-s", "${env:GD32_OPENOCD_SCRIPTS}",
|
||||||
|
"-f", "${env:GD32_OPENOCD_SCRIPTS}/${config:gd32.openocd.targetConfig}",
|
||||||
|
"-c", "init; reset halt; flash write_image erase ${command:cmake.launchTargetFilename}; reset; exit"
|
||||||
|
],
|
||||||
|
"group": "build",
|
||||||
|
"problemMatcher": [],
|
||||||
|
"options": {
|
||||||
|
"cwd": "${command:cmake.buildDirectory}/Application",
|
||||||
|
"environment": {
|
||||||
|
"OPENOCD_SCRIPTS": "${env:GD32_OPENOCD_SCRIPTS}"
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"presentation": {
|
||||||
|
"clear": true
|
||||||
|
},
|
||||||
|
"icon": {
|
||||||
|
"id": "gather",
|
||||||
|
"tooltip": "Flash MCU"
|
||||||
|
}
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"label": "Flash MCU (DAPLink)",
|
||||||
|
"type": "process",
|
||||||
|
"command": "${env:GD32_OPENOCD_BIN}/openocd.exe",
|
||||||
|
"args": [
|
||||||
|
"-s", "${env:GD32_OPENOCD_SCRIPTS}",
|
||||||
|
"-f", "${workspaceFolder}/.vscode/openocd_daplink_gd32e23x.cfg",
|
||||||
|
"-c", "program Application.elf verify reset exit"
|
||||||
|
],
|
||||||
|
"group": "build",
|
||||||
|
"problemMatcher": [],
|
||||||
|
"options": {
|
||||||
|
"cwd": "${command:cmake.buildDirectory}/Application",
|
||||||
|
"environment": {
|
||||||
|
"OPENOCD_SCRIPTS": "${env:GD32_OPENOCD_SCRIPTS}"
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"presentation": {
|
||||||
|
"clear": true
|
||||||
|
},
|
||||||
|
"icon": {
|
||||||
|
"id": "zap",
|
||||||
|
"tooltip": "Flash MCU via DAPLink (no connect_assert_srst)"
|
||||||
|
}
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"label": "Reset MCU",
|
||||||
|
"type": "process",
|
||||||
|
"command": "${env:GD32_OPENOCD_BIN}/openocd.exe",
|
||||||
|
"args": [
|
||||||
|
"-s", "${env:GD32_OPENOCD_SCRIPTS}",
|
||||||
|
"-f", "${env:GD32_OPENOCD_SCRIPTS}/${config:gd32.openocd.targetConfig}",
|
||||||
|
"-c", "init; reset; exit"
|
||||||
|
],
|
||||||
|
"group": "build",
|
||||||
|
"problemMatcher": [],
|
||||||
|
"options": {
|
||||||
|
"cwd": "${command:cmake.buildDirectory}/Application",
|
||||||
|
"environment": {
|
||||||
|
"OPENOCD_SCRIPTS": "${env:GD32_OPENOCD_SCRIPTS}"
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"presentation": {
|
||||||
|
"clear": true
|
||||||
|
},
|
||||||
|
"icon": {
|
||||||
|
"id": "discard",
|
||||||
|
"tooltip": "Reset MCU"
|
||||||
|
}
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"label": "Mass Erase MCU",
|
||||||
|
"type": "process",
|
||||||
|
"command": "${env:GD32_OPENOCD_BIN}/openocd.exe",
|
||||||
|
"args": [
|
||||||
|
"-s", "${env:GD32_OPENOCD_SCRIPTS}",
|
||||||
|
"-f", "${env:GD32_OPENOCD_SCRIPTS}/${config:gd32.openocd.targetConfig}",
|
||||||
|
"-c", "init; reset halt; ${config:gd32.openocd.massEraseCommand}; exit"
|
||||||
|
],
|
||||||
|
"group": "build",
|
||||||
|
"problemMatcher": [],
|
||||||
|
"options": {
|
||||||
|
"cwd": "${command:cmake.buildDirectory}/Application",
|
||||||
|
"environment": {
|
||||||
|
"OPENOCD_SCRIPTS": "${env:GD32_OPENOCD_SCRIPTS}"
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"presentation": {
|
||||||
|
"clear": true
|
||||||
|
},
|
||||||
|
"icon": {
|
||||||
|
"id": "clear-all",
|
||||||
|
"tooltip": "Erase MCU"
|
||||||
|
}
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"label": "OpenOCD Server",
|
||||||
|
"type": "process",
|
||||||
|
"command": "${env:GD32_OPENOCD_BIN}/openocd.exe",
|
||||||
|
"args": [
|
||||||
|
"-s", "${env:GD32_OPENOCD_SCRIPTS}",
|
||||||
|
"-f", "${env:GD32_OPENOCD_SCRIPTS}/${config:gd32.openocd.targetConfig}"
|
||||||
|
],
|
||||||
|
"group": "build",
|
||||||
|
"problemMatcher": [],
|
||||||
|
"options": {
|
||||||
|
"cwd": "${command:cmake.buildDirectory}/Application",
|
||||||
|
"environment": {
|
||||||
|
"OPENOCD_SCRIPTS": "${env:GD32_OPENOCD_SCRIPTS}"
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"presentation": {
|
||||||
|
"clear": true
|
||||||
|
}
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"label": "Build and Flash (Jlink)",
|
||||||
|
"dependsOn": [
|
||||||
|
"Build",
|
||||||
|
"Flash MCU (Jlink)"
|
||||||
|
],
|
||||||
|
"dependsOrder": "sequence",
|
||||||
|
"group": "build",
|
||||||
|
"icon": {
|
||||||
|
"id": "insert",
|
||||||
|
"tooltip": "Build and Flash (Jlink)"
|
||||||
|
}
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"label": "Flash MCU (Jlink)",
|
||||||
|
"type": "process",
|
||||||
|
"command": "JLink.exe",
|
||||||
|
"args": [
|
||||||
|
"-device", "${config:gd32.jlink.device}",
|
||||||
|
"-if", "${config:gd32.jlink.interface}",
|
||||||
|
"-speed", "${config:gd32.jlink.speed}",
|
||||||
|
"-autoconnect", "1",
|
||||||
|
"-CommanderScript", "${workspaceFolder}/.vscode/flash.jlink"
|
||||||
|
],
|
||||||
|
"group": "build",
|
||||||
|
"problemMatcher": [],
|
||||||
|
"options": {
|
||||||
|
"cwd": "${command:cmake.buildDirectory}/Application"
|
||||||
|
},
|
||||||
|
"presentation": {
|
||||||
|
"clear": true
|
||||||
|
},
|
||||||
|
"icon": {
|
||||||
|
"id": "gather",
|
||||||
|
"tooltip": "Flash MCU (Jlink)"
|
||||||
|
}
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"label": "Reset MCU (Jlink)",
|
||||||
|
"type": "process",
|
||||||
|
"command": "JLink.exe",
|
||||||
|
"args": [
|
||||||
|
"-device", "${config:gd32.jlink.device}",
|
||||||
|
"-if", "${config:gd32.jlink.interface}",
|
||||||
|
"-speed", "${config:gd32.jlink.speed}",
|
||||||
|
"-autoconnect", "1",
|
||||||
|
"-CommanderScript", "${workspaceFolder}/.vscode/reset.jlink"
|
||||||
|
],
|
||||||
|
"group": "build",
|
||||||
|
"problemMatcher": [],
|
||||||
|
"options": {
|
||||||
|
"cwd": "${workspaceFolder}"
|
||||||
|
},
|
||||||
|
"presentation": {
|
||||||
|
"clear": true
|
||||||
|
},
|
||||||
|
"icon": {
|
||||||
|
"id": "discard",
|
||||||
|
"tooltip": "Reset MCU (Jlink)"
|
||||||
|
}
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"label": "Mass Erase MCU (Jlink)",
|
||||||
|
"type": "process",
|
||||||
|
"command": "JLink.exe",
|
||||||
|
"args": [
|
||||||
|
"-device", "${config:gd32.jlink.device}",
|
||||||
|
"-if", "${config:gd32.jlink.interface}",
|
||||||
|
"-speed", "${config:gd32.jlink.speed}",
|
||||||
|
"-autoconnect", "1",
|
||||||
|
"-CommanderScript", "${workspaceFolder}/.vscode/erase.jlink"
|
||||||
|
],
|
||||||
|
"group": "build",
|
||||||
|
"problemMatcher": [],
|
||||||
|
"options": {
|
||||||
|
"cwd": "${workspaceFolder}"
|
||||||
|
},
|
||||||
|
"presentation": {
|
||||||
|
"clear": true
|
||||||
|
},
|
||||||
|
"icon": {
|
||||||
|
"id": "clear-all",
|
||||||
|
"tooltip": "Erase MCU (Jlink)"
|
||||||
|
}
|
||||||
}
|
}
|
||||||
]
|
]
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -0,0 +1,80 @@
|
|||||||
|
# AGENTS.md — ldc1612_cmake_vscode 项目约定与进度
|
||||||
|
|
||||||
|
## 项目概述
|
||||||
|
|
||||||
|
GD32E230 电涡流传感器节点固件:LDC1612(电涡流/电感数字转换器)+ TMP112(板载温度),
|
||||||
|
通过 RS485 串口命令协议(`D5 03 LEN [cmd] CRC`)与上位机通信,支持温度补偿与标定。
|
||||||
|
|
||||||
|
- 协议文档:`CommunicationProtocol.md`;寄存器速查:`LDC1612_REG_LIST.md`
|
||||||
|
- 版本策略:`Inc/version.h`(BOARD_TYPE_CODE.主.次.修订,当前 23.1.0.1)
|
||||||
|
|
||||||
|
## 工具链与构建
|
||||||
|
|
||||||
|
- 语言/环境:C99,GD32E23x 标准库(`SDK/`)
|
||||||
|
- 编译器:`arm-none-eabi-gcc`(D:\toolchain\xpack-arm-none-eabi-gcc-15.2.1-1.1)
|
||||||
|
- 构建:CMake + Ninja,preset `Debug` / `Release`,输出 `Build/<preset>/Application/Application.elf`
|
||||||
|
- 命令行构建:`cmake --build Build\Debug`(需 arm-none-eabi-gcc/cmake/ninja 在 PATH)
|
||||||
|
- 烧录/调试(.vscode/tasks.json、launch.json):
|
||||||
|
- OpenOCD:`GD32_OPENOCD_BIN` 指向定制构建(含 gd32e23x flash 驱动,CMSIS-DAP HID 后端)
|
||||||
|
- `Flash MCU (DAPLink)` 任务 + `.vscode/openocd_daplink_gd32e23x.cfg`:
|
||||||
|
适配 DAPLink 探针(无 connect_assert_srst,当前共享 cfg 的 connect_assert_srst
|
||||||
|
会导致 Cortex-M23 识别失败);烧录命令 `program Application.elf verify reset exit`
|
||||||
|
- J-Link 任务需 SEGGER 探针 + PATH 中 JLink.exe(当前接的是 DAPLink,不适用)
|
||||||
|
- 调试器探针历史问题(2026-10-02 已解决):DAPLink 的 CMSIS-DAP v1 接口曾被 Zadig
|
||||||
|
装上 libwdi WinUSB 驱动(oem244.inf),导致 OpenOCD 枚举不到探针;删除该驱动包后
|
||||||
|
恢复 HID。若复发:管理员运行 `pnputil /delete-driver <oemNN.inf> /uninstall`。
|
||||||
|
- 串口验证:RS485 转 USB(FTDI,COM15),115200-8N1,**需同时置位 RTS+DTR**
|
||||||
|
(转换器方向控制),否则命令发不出去;DAPLink 虚拟串口为 COM8。
|
||||||
|
|
||||||
|
## 硬件/板级约定(Inc/board_config.h)
|
||||||
|
|
||||||
|
- MCU:GD32E230(自动探测 Flash 容量,64KB → USART1 作 RS485)
|
||||||
|
- RS485:SP3487(无自动方向),USART1 PA2(TX)/PA3(RX),DE=PA1(USART 硬件 DE,AF1)
|
||||||
|
- I2C0:PF0(SDA)/PF1(SCL),硬件 IIC 100kHz(`SOFTWARE_IIC` 可切换软模拟,默认关闭)
|
||||||
|
- LED:PB1
|
||||||
|
- 传感器:LDC1612 @0x2B(40MHz 外部时钟),TMP112 @0x49(ADD0=GND)
|
||||||
|
- 调试宏(均默认关闭):DEBUG_VERBOSE / COM_DEBUG / LDC_DEBUG / EDDY_DRIVE_CURRENT_DETECTION
|
||||||
|
|
||||||
|
## 内存布局(仓库默认 = 正式发布布局)
|
||||||
|
|
||||||
|
- **APP 链接在 0x08002000(54K),VECT_TAB_OFFSET=0x2000(Src/system_gd32e23x.c),
|
||||||
|
前置 8KB Bootloader**;OTA/Ymodem 打包(_UPDATE.bin/_BL.bin)基于此布局,
|
||||||
|
详见 hulk-mcu-firmware skill
|
||||||
|
- 调试阶段如需脱离 Bootloader 裸机运行(直接烧 0x08000000):临时把
|
||||||
|
`LD/gd32e23x_flash.ld` 的 ORIGIN 改为 0x08000000、VECT_TAB_OFFSET 改为 0x00,
|
||||||
|
**仅限本地临时修改,不要提交推送**(否则烧 _BL 镜像时 Bootloader 会覆盖 APP 头部)
|
||||||
|
- 修改布局需同时改 `LD/gd32e23x_flash.ld` 的 ORIGIN 与 VECT_TAB_OFFSET
|
||||||
|
|
||||||
|
## 开发进度
|
||||||
|
|
||||||
|
### 已完成
|
||||||
|
|
||||||
|
- [x] LDC1612 驱动(初始化/单通道配置/原始值读取/状态检查)
|
||||||
|
- [x] TMP112 驱动(初始化/温度读取/阈值/单次转换)
|
||||||
|
- [x] RS485 命令体系:M1/M2(周期上报开关)、M3(涡流原始值)、M4(温度×10000)、
|
||||||
|
M5(温度补偿涡流值)、M6(标定数据:涡流+温度)、M999(版本)、M9999(复位)
|
||||||
|
- [x] v1.0.1:涡流传感器温度标定方法与标定命令(M6)
|
||||||
|
- [x] 2026-10-02:从模板 gd32e23x_template_cmake_vscode 导入 IIC 驱动改进:
|
||||||
|
错误路径改为 i2c_bus_reset() 总线恢复+重试、_i2c_wait_flag_timeout 系列带超时
|
||||||
|
辅助函数、i2c_scan 增加 AERR 处理、新增 i2c_read_raw(无子地址读取)、
|
||||||
|
i2c_write/i2c_read 改为 16 位寄存器地址(DLPC 风格,本项目传感器仍走
|
||||||
|
i2c_write_16bits/i2c_read_16bits 8 位寄存器路径);移除未使用的
|
||||||
|
i2c_read/write_8bits、_32bits、i2c_read_display_params 显示面板便捷函数;
|
||||||
|
tmp112.c 读取改回 i2c_read_16bits
|
||||||
|
- [x] 2026-10-02:硬件验证通过——全部命令实测:M3 涡流原始值 ~0x01ADBADC
|
||||||
|
(室温)/0x01B094BA(板子被手捂热后,随温度同步上移,无错误位)、
|
||||||
|
M4 温度 27.25→30.45→29.95°C(跟踪板温变化合理)、M5 补偿值(系数为 0 时≈M3)、
|
||||||
|
M1 周期上报(~10ms/帧)开启、M2 停止("ok" 确认)、M6 组合数据、
|
||||||
|
M999 版本 v23.1.0.1、M9999 复位(复位后 M999 恢复应答);
|
||||||
|
所有响应 CRC 抽查校验正确;Release 预设编译通过(text 11.4KB)
|
||||||
|
- [x] 2026-10-02:解决 DAPLink 烧录器识别问题(Zadig WinUSB 驱动冲突);
|
||||||
|
新增 DAPLink 专用烧录 cfg 与任务
|
||||||
|
- [x] 2026-10-02:发布 V1.0.2(Gitea Release + tag,基于 0x08002000 Bootloader
|
||||||
|
布局打包 _UPDATE.bin/_BL.bin;dev 顶端另提交调试布局 0x08000000 供日常烧录)
|
||||||
|
|
||||||
|
### 计划 / TODO
|
||||||
|
|
||||||
|
- [ ] 正式发布前切回 Bootloader 布局(0x08002000/54K)并适配 OTA/Ymodem 打包流程
|
||||||
|
- [ ] M5 温度补偿系数目前为 0(TEMP_COMP_COEFF_*),待标定后填入实际温漂系数
|
||||||
|
- [ ] 模板侧(gd32e23x_template_cmake_vscode)的 SEGGER RTT / CFG_ENABLE 宏体系
|
||||||
|
尚未同步到本项目(board_config.h 仍是 define/undef 风格),如需对齐另起任务
|
||||||
@@ -79,6 +79,7 @@ add_custom_command(TARGET ${PROJECT_NAME}
|
|||||||
POST_BUILD
|
POST_BUILD
|
||||||
COMMAND echo -- Running Post Build Commands
|
COMMAND echo -- Running Post Build Commands
|
||||||
COMMAND ${CMAKE_OBJCOPY} -O ihex $<TARGET_FILE:${PROJECT_NAME}> ${CMAKE_CURRENT_BINARY_DIR}/${OUTPUT_PREFIX}.hex
|
COMMAND ${CMAKE_OBJCOPY} -O ihex $<TARGET_FILE:${PROJECT_NAME}> ${CMAKE_CURRENT_BINARY_DIR}/${OUTPUT_PREFIX}.hex
|
||||||
|
COMMAND ${CMAKE_OBJCOPY} -O ihex $<TARGET_FILE:${PROJECT_NAME}> $<TARGET_FILE_DIR:${PROJECT_NAME}>/Application.hex
|
||||||
COMMAND ${CMAKE_OBJCOPY} -O binary $<TARGET_FILE:${PROJECT_NAME}> ${CMAKE_CURRENT_BINARY_DIR}/${OUTPUT_PREFIX}.bin
|
COMMAND ${CMAKE_OBJCOPY} -O binary $<TARGET_FILE:${PROJECT_NAME}> ${CMAKE_CURRENT_BINARY_DIR}/${OUTPUT_PREFIX}.bin
|
||||||
COMMAND ${CMAKE_SIZE} $<TARGET_FILE:${PROJECT_NAME}>
|
COMMAND ${CMAKE_SIZE} $<TARGET_FILE:${PROJECT_NAME}>
|
||||||
COMMAND ${CMAKE_OBJDUMP} -h -S $<TARGET_FILE:${PROJECT_NAME}> > ${CMAKE_CURRENT_BINARY_DIR}/${OUTPUT_PREFIX}.list
|
COMMAND ${CMAKE_OBJDUMP} -h -S $<TARGET_FILE:${PROJECT_NAME}> > ${CMAKE_CURRENT_BINARY_DIR}/${OUTPUT_PREFIX}.list
|
||||||
|
|||||||
@@ -58,5 +58,28 @@
|
|||||||
|
|
||||||
### 4. 单次读取并发送板载温度传感器数据
|
### 4. 单次读取并发送板载温度传感器数据
|
||||||
|
|
||||||
- M3 指令 单次读取并发送板载温度传感器数据
|
- M4 指令 单次读取并发送板载温度传感器数据(TMP112,返回温度值×10000,4 字节有符号大端)
|
||||||
- `D5 03 02 4D 34 86`
|
- `D5 03 02 4D 34 86`
|
||||||
|
- 示例回复:`B5 F0 04 00 04 28 74 94`,有效数据 `0x00042874` = 272500,即 `27.25°C`
|
||||||
|
|
||||||
|
### 5. 单次读取并发送温度补偿后的涡流传感器数据
|
||||||
|
|
||||||
|
- M5 指令 单次读取并发送温度补偿后的涡流传感器数据(补偿系数默认为 0,与 M3 等价)
|
||||||
|
- `D5 03 02 4D 35 87`
|
||||||
|
|
||||||
|
### 6. 单次读取并发送标定数据(涡流原始值 + 温度值)
|
||||||
|
|
||||||
|
- M6 指令 单次读取涡流原始值和板载温度值,用于温度标定
|
||||||
|
- `D5 03 02 4D 36 88`
|
||||||
|
- 回复 9 字节:涡流原始值(4字节大端) + 0xFF + 温度值×10000(4字节大端)
|
||||||
|
- 示例回复:`B5 F0 09 01 AD BA DC FF 00 04 28 74 DC`
|
||||||
|
|
||||||
|
### 7. 查询固件版本号
|
||||||
|
|
||||||
|
- M999 指令 返回固件版本号字符串,如 `v23.1.0.1`(板卡类型.主版本.次版本.修订号)
|
||||||
|
- `D5 03 04 4D 39 39 39 FF`
|
||||||
|
|
||||||
|
### 8. 复位 MCU
|
||||||
|
|
||||||
|
- M9999 指令 立即复位 MCU(无回复)
|
||||||
|
- `D5 03 05 4D 39 39 39 39 39`
|
||||||
|
|||||||
@@ -89,9 +89,9 @@ i2c_result_t i2c_bus_reset(void);
|
|||||||
void i2c_scan(void);
|
void i2c_scan(void);
|
||||||
|
|
||||||
/*!
|
/*!
|
||||||
\brief write 16-bit data to I2C device
|
\brief write 16-bit data to I2C device (8-bit register address + 2 data bytes)
|
||||||
\param[in] slave_addr: 7-bit slave address
|
\param[in] slave_addr: 7-bit slave address
|
||||||
\param[in] reg_addr: register address
|
\param[in] reg_addr: register address (8-bit)
|
||||||
\param[in] data: pointer to 2-byte data array
|
\param[in] data: pointer to 2-byte data array
|
||||||
\param[out] none
|
\param[out] none
|
||||||
\retval i2c_result_t
|
\retval i2c_result_t
|
||||||
@@ -99,9 +99,9 @@ void i2c_scan(void);
|
|||||||
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]);
|
||||||
|
|
||||||
/*!
|
/*!
|
||||||
\brief read 16-bit data from I2C device
|
\brief read 16-bit data from I2C device (8-bit register address + 2 data bytes)
|
||||||
\param[in] slave_addr: 7-bit slave address
|
\param[in] slave_addr: 7-bit slave address
|
||||||
\param[in] reg_addr: register address
|
\param[in] reg_addr: register address (8-bit)
|
||||||
\param[out] data: pointer to 2-byte data buffer
|
\param[out] data: pointer to 2-byte data buffer
|
||||||
\retval i2c_result_t
|
\retval i2c_result_t
|
||||||
*/
|
*/
|
||||||
@@ -111,70 +111,33 @@ i2c_result_t i2c_read_16bits(uint8_t slave_addr, uint8_t reg_addr, uint8_t *data
|
|||||||
/*!
|
/*!
|
||||||
\brief write data to I2C device with configurable length
|
\brief write data to I2C device with configurable length
|
||||||
\param[in] slave_addr: slave device address (7-bit)
|
\param[in] slave_addr: slave device address (7-bit)
|
||||||
\param[in] reg_addr: register address
|
\param[in] reg_addr: register address (16-bit, high byte first)
|
||||||
\param[in] data: pointer to data buffer
|
\param[in] data: pointer to data buffer
|
||||||
\param[in] length: number of bytes to write (1-255)
|
\param[in] length: number of bytes to write (1-255)
|
||||||
\param[out] none
|
\param[out] none
|
||||||
\retval i2c_result_t: operation result
|
\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_result_t i2c_write(uint8_t slave_addr, uint16_t reg_addr, uint8_t *data, uint8_t length);
|
||||||
|
|
||||||
/*!
|
/*!
|
||||||
\brief read data from I2C device with configurable length
|
\brief read data from I2C device with configurable length
|
||||||
\param[in] slave_addr: slave device address (7-bit)
|
\param[in] slave_addr: slave device address (7-bit)
|
||||||
\param[in] reg_addr: register address
|
\param[in] reg_addr: register address (16-bit, high byte first)
|
||||||
\param[out] data: pointer to data buffer
|
\param[out] data: pointer to data buffer
|
||||||
\param[in] length: number of bytes to read (1-255)
|
\param[in] length: number of bytes to read (1-255)
|
||||||
\retval i2c_result_t: operation result
|
\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_result_t i2c_read(uint8_t slave_addr, uint16_t reg_addr, uint8_t *data, uint8_t length);
|
||||||
|
|
||||||
/* Convenience functions for common operations */
|
/* Pure read response without sub-address phase (for commands that already sent read request parameters) */
|
||||||
/*!
|
/*!
|
||||||
\brief write single byte to I2C device
|
\brief read data from I2C device without register/sub-address phase
|
||||||
\param[in] slave_addr: slave device address (7-bit)
|
\param[in] slave_addr: slave device address (7-bit)
|
||||||
\param[in] reg_addr: register address
|
|
||||||
\param[in] data: data byte to write
|
|
||||||
\retval i2c_result_t: operation result
|
|
||||||
*/
|
|
||||||
i2c_result_t i2c_write_8bits(uint8_t slave_addr, uint8_t reg_addr, uint8_t data);
|
|
||||||
|
|
||||||
/*!
|
|
||||||
\brief read single byte from I2C device
|
|
||||||
\param[in] slave_addr: slave device address (7-bit)
|
|
||||||
\param[in] reg_addr: register address
|
|
||||||
\param[out] data: pointer to data byte
|
|
||||||
\retval i2c_result_t: operation result
|
|
||||||
*/
|
|
||||||
i2c_result_t i2c_read_8bits(uint8_t slave_addr, uint8_t reg_addr, uint8_t *data);
|
|
||||||
|
|
||||||
/*!
|
|
||||||
\brief write 32-bit data to I2C device
|
|
||||||
\param[in] slave_addr: slave device address (7-bit)
|
|
||||||
\param[in] reg_addr: register address
|
|
||||||
\param[in] data: pointer to 4-byte data array
|
|
||||||
\retval i2c_result_t: operation result
|
|
||||||
*/
|
|
||||||
i2c_result_t i2c_write_32bits(uint8_t slave_addr, uint8_t reg_addr, uint8_t data[4]);
|
|
||||||
|
|
||||||
/*!
|
|
||||||
\brief read 32-bit data from I2C device
|
|
||||||
\param[in] slave_addr: slave device address (7-bit)
|
|
||||||
\param[in] reg_addr: register address
|
|
||||||
\param[out] data: pointer to 4-byte data buffer
|
|
||||||
\retval i2c_result_t: operation result
|
|
||||||
*/
|
|
||||||
i2c_result_t i2c_read_32bits(uint8_t slave_addr, uint8_t reg_addr, uint8_t *data);
|
|
||||||
|
|
||||||
/*!
|
|
||||||
\brief read display panel parameters (multi-byte)
|
|
||||||
\param[in] slave_addr: slave device address (7-bit)
|
|
||||||
\param[in] reg_addr: register address
|
|
||||||
\param[out] data: pointer to data buffer
|
\param[out] data: pointer to data buffer
|
||||||
\param[in] length: number of bytes to read (1-13)
|
\param[in] length: number of bytes to read (1-255)
|
||||||
\retval i2c_result_t: operation result
|
\retval i2c_result_t: operation result
|
||||||
*/
|
*/
|
||||||
i2c_result_t i2c_read_display_params(uint8_t slave_addr, uint8_t reg_addr, uint8_t *data, uint8_t length);
|
i2c_result_t i2c_read_raw(uint8_t slave_addr, uint8_t *data, uint8_t length);
|
||||||
|
|
||||||
#ifdef DEBUG_VERBOSE
|
#ifdef DEBUG_VERBOSE
|
||||||
/*!
|
/*!
|
||||||
|
|||||||
@@ -12,7 +12,7 @@ _Min_Stack_Size = 0x400; /* required amount of stack */
|
|||||||
/* Memories definition */
|
/* Memories definition */
|
||||||
MEMORY
|
MEMORY
|
||||||
{
|
{
|
||||||
FLASH (rx) : ORIGIN = 0x08000000, LENGTH = 32K
|
FLASH (rx) : ORIGIN = 0x08002000, LENGTH = 54K
|
||||||
RAM (xrw) : ORIGIN = 0x20000000, LENGTH = 8K
|
RAM (xrw) : ORIGIN = 0x20000000, LENGTH = 8K
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -1,11 +1,36 @@
|
|||||||
# GD32E23x 工程模板
|
# LDC1612 传感器固件
|
||||||
|
|
||||||
本仓库为兆易创新 GD32E23x 系列 MCU 的 CMake + VSCode 工程模板,适合嵌入式开发快速上手和团队协作。
|
本仓库基于 GD32E23x CMake + VSCode 工程模板(`gd32e23x_template_cmake_vscode`),包含 LDC1612 电感采样、TMP112 温度采集及补偿、RS485 命令处理。当前版本为 V1.0.1。固件采用 Bootloader + APP 布局:APP 链接在 `0x08002000`(54KB Flash / 8KB RAM),启动时自设向量表偏移;开发进度与审查资料见 `doc/` 目录。
|
||||||
|
|
||||||
|
## 本地编译
|
||||||
|
|
||||||
|
需要 CMake 3.20 或更新版本、Ninja 和 ARM GCC。CMake、Ninja 应在 `PATH` 中;ARM GCC 通过 `GD32_GCC_BIN` 指定,也可在配置时传入 `-DTOOLCHAIN_DIRECTORY="D:/toolchain/arm-gcc/bin"`,该参数优先于环境变量。工具链路径支持包根目录或 `bin` 目录。
|
||||||
|
|
||||||
|
在工程根目录运行 PowerShell(工具链路径请替换为本机安装位置):
|
||||||
|
|
||||||
|
```powershell
|
||||||
|
$env:GD32_GCC_BIN = 'D:\toolchain\xpack-arm-none-eabi-gcc-15.2.1-1.1\bin'
|
||||||
|
cmake --preset Debug
|
||||||
|
cmake --build --preset Debug
|
||||||
|
cmake --preset Release
|
||||||
|
cmake --build --preset Release
|
||||||
|
```
|
||||||
|
|
||||||
|
输出位于 `Build/Debug` 或 `Build/Release`:`Application/Application.elf` 用于调试,`LDC1612_V1.0.1_APP_<日期>.bin` / `.hex` 为应用固件;`Application/Application.hex` 供 J-Link 烧录任务使用。应用固件不含 Bootloader:单独烧录需写到 `0x08002000`;整片烧录(Bootloader + APP + 标志页)用配套的固件合并工具(`gd32e230f8_firmware_merge_tool` 仓库)处理。详见[固件布局与 Bootloader](#固件布局与-bootloader)。
|
||||||
|
|
||||||
|
如果切换 ARM GCC 版本,请使用新的构建目录,或通过 CMake Tools 的 “Delete Cache and Reconfigure” 重新配置。需要独立验证目录时可运行:
|
||||||
|
|
||||||
|
```powershell
|
||||||
|
cmake --preset Debug -B Build/local-check/Debug
|
||||||
|
cmake --build Build/local-check/Debug
|
||||||
|
```
|
||||||
|
|
||||||
---
|
---
|
||||||
|
|
||||||
## 目录
|
## 目录
|
||||||
|
|
||||||
|
- [本地编译](#本地编译)
|
||||||
|
- [固件布局与 Bootloader](#固件布局与-bootloader)
|
||||||
- [适用范围](#适用范围)
|
- [适用范围](#适用范围)
|
||||||
- [默认配置](#默认配置)
|
- [默认配置](#默认配置)
|
||||||
- [工具链准备](#工具链准备)
|
- [工具链准备](#工具链准备)
|
||||||
@@ -16,6 +41,26 @@
|
|||||||
|
|
||||||
---
|
---
|
||||||
|
|
||||||
|
## 固件布局与 Bootloader
|
||||||
|
|
||||||
|
GD32E230F8(64KB Flash)按 Bootloader + APP 方式分区:
|
||||||
|
|
||||||
|
| 区域 | 地址范围 | 说明 |
|
||||||
|
|---|---|---|
|
||||||
|
| Bootloader | `0x08000000` - `0x08001FFF`(8KB) | 随板提供,独立固件(`gd32e230f8_bootloader_hulk.bin`),负责 RS485 Ymodem OTA 与跳转 |
|
||||||
|
| APP(本仓库) | `0x08002000` - `0x0800F7FF`(54KB) | 链接区 `FLASH ORIGIN=0x08002000, LENGTH=54K` |
|
||||||
|
| 温度标定页(预留) | `0x0800F800` - `0x0800FBFF` | 预留存放标定数据;当前 APP 不访问,仅靠 54KB 链接上限保证不越界覆盖 |
|
||||||
|
| 有效标志 | `0x0800FFFC` | Bootloader OTA 完成后自写 `0x0000EEEE`,跳转前校验 |
|
||||||
|
|
||||||
|
工程侧的两个适配点(缺一不可):
|
||||||
|
|
||||||
|
- `LD/gd32e23x_flash.ld`:FLASH `ORIGIN = 0x08002000, LENGTH = 54K`;
|
||||||
|
- `Src/system_gd32e23x.c`:`VECT_TAB_OFFSET = 0x2000`——Bootloader 不写 VTOR,APP 必须自己把向量表指到 `0x08002000`,否则中断会落回 Bootloader 向量表(现象:收到第一个 RS485 字节即死机)。
|
||||||
|
|
||||||
|
⚠️ 与旧版布局(APP 链接在 `0x08000000` 的独立镜像)不兼容:从旧版升级需整片烧录一次。RS485 协议、命令集(M1-M6/M999/M9999)与回包格式无任何变化。布局依据与 Bootloader 反汇编分析见 `doc/LDC1612_工程审查与实验方案_2026-09-26.md`。
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
## 适用范围
|
## 适用范围
|
||||||
|
|
||||||
- 适用于兆易创新 GD32E23x 系列 Cortex-M23 内核单片机
|
- 适用于兆易创新 GD32E23x 系列 Cortex-M23 内核单片机
|
||||||
@@ -34,37 +79,49 @@
|
|||||||
|
|
||||||
### 1. xPack GNU Arm Embedded GCC Toolchain
|
### 1. xPack GNU Arm Embedded GCC Toolchain
|
||||||
|
|
||||||
- **版本**:xpack-arm-none-eabi-gcc-11.3.1-1.1
|
- **原模板版本**:xpack-arm-none-eabi-gcc-11.3.1-1.1;本机编译验证使用 15.2.1-1.1。
|
||||||
- **建议解压路径**:Tools/xpack-arm-none-eabi-gcc-11.3.1-1.1
|
- **安装位置**:由本机统一管理,不提交到仓库。
|
||||||
- **官方下载地址**:https://github.com/xpack-dev-tools/arm-none-eabi-gcc-xpack/releases
|
- **官方下载地址**:https://github.com/xpack-dev-tools/arm-none-eabi-gcc-xpack/releases
|
||||||
- **路径自定义说明**:
|
- **路径自定义说明**:
|
||||||
如需自定义工具链路径,请同步修改以下文件:
|
编译器与 GDB 均通过 `GD32_GCC_BIN` 环境变量定位(或配置时传 `-DTOOLCHAIN_DIRECTORY`),CMake 文件与 `.vscode` 均无需修改路径。
|
||||||
- `Projects/<BoardName>/<ProjectName>/cmake/arm-none-eabi-gcc.cmake`(第2行)
|
|
||||||
- `Projects/<BoardName>/<ProjectName>/.vscode/launch.json`(第12行)
|
|
||||||
|
|
||||||
### 2. OpenOCD
|
### 2. OpenOCD(GD-Link 支持)
|
||||||
|
|
||||||
- **版本**:xpack-openocd-0.11.0-3
|
- **版本**:xpack-openocd-0.11.0-3
|
||||||
- **建议解压路径**:Tools/xpack-openocd-0.11.0-3
|
|
||||||
- **获取地址**:https://github.com/burakenez/gd32-tools-xpack-openocd/tree/v0.11.0-3
|
- **获取地址**:https://github.com/burakenez/gd32-tools-xpack-openocd/tree/v0.11.0-3
|
||||||
- **说明**:
|
- **说明**:
|
||||||
- 本版本提取自 Embedded Builder V1.4.1.23782。
|
- 本版本提取自 Embedded Builder V1.4.1.23782。
|
||||||
- ⚠️ 请勿随意更换版本,因 GD32 MCU 支持有限,推荐严格使用此版本。
|
- ⚠️ 请勿随意更换版本,因 GD32 MCU 支持有限,推荐严格使用此版本。
|
||||||
- **路径自定义说明**:
|
- **路径自定义说明**:
|
||||||
如需自定义 OpenOCD 路径,请同步修改以下文件:
|
通过两个环境变量定位,安装位置任意:
|
||||||
- `Projects/<BoardName>/<ProjectName>/.vscode/launch.json`(第14、17行)
|
- `GD32_OPENOCD_BIN`:`openocd.exe` 所在 `bin` 目录;
|
||||||
- `Projects/<BoardName>/<ProjectName>/.vscode/task.json` 中所有相关路径
|
- `GD32_OPENOCD_SCRIPTS`:OpenOCD `scripts` 目录。
|
||||||
|
`.vscode` 的调试配置与烧录任务全部读取这两个变量,无需修改任何文件。
|
||||||
|
|
||||||
---
|
---
|
||||||
|
|
||||||
## 使用说明
|
## 使用说明
|
||||||
|
|
||||||
1. **准备工具链**
|
1. **配置环境变量**(用户级,按本机安装位置设置一次即可)
|
||||||
- 按上述说明下载并解压 ARM GCC 和 OpenOCD 到 Tools 目录。
|
|
||||||
- Toolchain 目录内容不会被 git 跟踪,需自行维护。
|
|
||||||
|
|
||||||
2. **烧录固件**
|
| 环境变量 | 指向 | 用途 |
|
||||||
- 可直接使用 VSCode 任务栏的 Flash MCU 任务,或命令行运行 OpenOCD。
|
|---|---|---|
|
||||||
|
| `GD32_GCC_BIN` | ARM GCC 的 `bin` 目录 | 编译、GDB 调试 |
|
||||||
|
| `GD32_OPENOCD_BIN` | OpenOCD 的 `bin` 目录 | 烧录、调试 |
|
||||||
|
| `GD32_OPENOCD_SCRIPTS` | OpenOCD 的 `scripts` 目录 | 烧录、调试 |
|
||||||
|
|
||||||
|
设置后重开终端 / VSCode 生效。`cmd` 示例:`setx GD32_GCC_BIN "D:\toolchain\xpack-arm-none-eabi-gcc-15.2.1-1.1\bin"`。
|
||||||
|
|
||||||
|
2. **编译**
|
||||||
|
命令行见上文[本地编译](#本地编译),或直接用 VSCode 的 CMake Tools 选择 Debug / Release preset 构建。
|
||||||
|
|
||||||
|
3. **烧录固件**
|
||||||
|
- **OpenOCD(GD-Link)**:VSCode 任务 `Flash MCU`(连带编译用 `Build and Flash`);另有 `Reset MCU`、`Mass Erase MCU`、`OpenOCD Server` 任务。
|
||||||
|
- **J-Link**:任务 `Flash MCU (Jlink)` 等,依赖构建生成的 `Build/<preset>/Application/Application.hex`。
|
||||||
|
- 器件与接口在 `.vscode/settings.json` 的 `gd32.jlink.*` / `gd32.openocd.*` 中配置,当前为 GD32E230F8 / SWD。
|
||||||
|
|
||||||
|
4. **调试**
|
||||||
|
VSCode 调试配置有两个:`Debug with OpenOCD` 与 `Debug with J-Link`(均使用 cortex-debug,SVD 文件 `GD32E230.svd`,支持外设寄存器查看)。烧录调试器以 GD-Link 为例;J-Link 直接选用对应配置即可。
|
||||||
|
|
||||||
---
|
---
|
||||||
|
|
||||||
@@ -129,7 +186,6 @@
|
|||||||
|
|
||||||
- **快速上手示例**:如 main.c 的最小点灯/串口输出代码片段。
|
- **快速上手示例**:如 main.c 的最小点灯/串口输出代码片段。
|
||||||
- **常见问题与解答**:如构建失败、烧录失败的排查建议。
|
- **常见问题与解答**:如构建失败、烧录失败的排查建议。
|
||||||
- **调试说明**:如何用 VSCode 调试、断点、查看寄存器等。
|
|
||||||
- **多板卡适配说明**:如有多种硬件,如何切换 BoardName。
|
- **多板卡适配说明**:如有多种硬件,如何切换 BoardName。
|
||||||
- **贡献指南**:如何提交 PR、代码风格约定等。
|
- **贡献指南**:如何提交 PR、代码风格约定等。
|
||||||
- **License 说明**:开源协议和版权声明。
|
- **License 说明**:开源协议和版权声明。
|
||||||
|
|||||||
@@ -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 */
|
||||||
@@ -149,7 +150,7 @@ i2c_result_t i2c_bus_reset(void) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
#ifdef DEBUG_VERBOSE
|
#ifdef DEBUG_VERBOSE
|
||||||
printf("I2C bus reset: bus recovered\r\n");
|
printf("I2C bus reset: bus recovered\r\n");
|
||||||
#endif
|
#endif
|
||||||
/* 8. Reconfigure & enable peripheral */
|
/* 8. Reconfigure & enable peripheral */
|
||||||
i2c_config();
|
i2c_config();
|
||||||
@@ -197,10 +198,12 @@ 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";
|
||||||
@@ -221,6 +224,9 @@ void i2c_scan(void) {
|
|||||||
}
|
}
|
||||||
while (usart_flag_get(I2C_DEBUG_UART, USART_FLAG_TC) == RESET) {}
|
while (usart_flag_get(I2C_DEBUG_UART, USART_FLAG_TC) == RESET) {}
|
||||||
found_devices++;
|
found_devices++;
|
||||||
|
} else if (i2c_flag_get(I2C0, I2C_FLAG_AERR)) {
|
||||||
|
// 设备无响应 - 清除错误标志
|
||||||
|
i2c_flag_clear(I2C0, I2C_FLAG_AERR);
|
||||||
}
|
}
|
||||||
|
|
||||||
// 生成停止条件
|
// 生成停止条件
|
||||||
@@ -263,6 +269,53 @@ void i2c_scan(void) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @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 +378,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 +386,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 +423,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 +451,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;
|
||||||
@@ -444,7 +470,7 @@ i2c_result_t i2c_write_16bits(uint8_t slave_addr, uint8_t reg_addr, uint8_t data
|
|||||||
/* small delay before retry */
|
/* small delay before retry */
|
||||||
delay_10us(10);
|
delay_10us(10);
|
||||||
break;
|
break;
|
||||||
|
|
||||||
default:
|
default:
|
||||||
state = I2C_STATE_START;
|
state = I2C_STATE_START;
|
||||||
break;
|
break;
|
||||||
@@ -472,11 +498,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 +507,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 +559,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,28 +567,20 @@ 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;
|
||||||
}
|
}
|
||||||
|
|
||||||
/* generate repeated start condition */
|
/* generate repeated start condition */
|
||||||
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 +596,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 +626,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;
|
||||||
}
|
}
|
||||||
@@ -652,13 +657,13 @@ i2c_result_t i2c_read_16bits(uint8_t slave_addr, uint8_t reg_addr, uint8_t *data
|
|||||||
/*!
|
/*!
|
||||||
\brief write data to I2C device with configurable length
|
\brief write data to I2C device with configurable length
|
||||||
\param[in] slave_addr: slave device address (7-bit)
|
\param[in] slave_addr: slave device address (7-bit)
|
||||||
\param[in] reg_addr: register address
|
\param[in] reg_addr: register address (16-bit, high byte first)
|
||||||
\param[in] data: pointer to data buffer
|
\param[in] data: pointer to data buffer
|
||||||
\param[in] length: number of bytes to write (1-255)
|
\param[in] length: number of bytes to write (1-255)
|
||||||
\param[out] none
|
\param[out] none
|
||||||
\retval i2c_result_t: operation result
|
\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_result_t i2c_write(uint8_t slave_addr, uint16_t reg_addr, uint8_t *data, uint8_t length) {
|
||||||
i2c_state_t state = I2C_STATE_START;
|
i2c_state_t state = I2C_STATE_START;
|
||||||
uint16_t timeout = 0;
|
uint16_t timeout = 0;
|
||||||
uint8_t retry_count = 0;
|
uint8_t retry_count = 0;
|
||||||
@@ -666,6 +671,9 @@ i2c_result_t i2c_write(uint8_t slave_addr, uint8_t reg_addr, uint8_t *data, uint
|
|||||||
|
|
||||||
/* parameter validation */
|
/* parameter validation */
|
||||||
if (data == NULL || slave_addr > 0x7F || length == 0) {
|
if (data == NULL || slave_addr > 0x7F || length == 0) {
|
||||||
|
#ifdef DEBUG_VERBOSE
|
||||||
|
printf("I2C write invalid param: slave=0x%02X, len=%u, data=%p\r\n", slave_addr, length, data);
|
||||||
|
#endif
|
||||||
return I2C_RESULT_INVALID_PARAM;
|
return I2C_RESULT_INVALID_PARAM;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -719,67 +727,71 @@ i2c_result_t i2c_write(uint8_t slave_addr, uint8_t reg_addr, uint8_t *data, uint
|
|||||||
state = I2C_STATE_TRANSMIT_REG;
|
state = I2C_STATE_TRANSMIT_REG;
|
||||||
break;
|
break;
|
||||||
} else {
|
} else {
|
||||||
|
/* NACK received - address not acknowledged */
|
||||||
i2c_flag_clear(I2C0, I2C_FLAG_AERR);
|
i2c_flag_clear(I2C0, I2C_FLAG_AERR);
|
||||||
|
i2c_stop_on_bus(I2C0);
|
||||||
timeout = 0;
|
timeout = 0;
|
||||||
#ifdef DEBUG_VERBOSE
|
#ifdef DEBUG_VERBOSE
|
||||||
printf("IIC write failed for Error Slave Address. \n");
|
printf("I2C write failed for Error Slave Address. \n");
|
||||||
#endif
|
#endif
|
||||||
return I2C_RESULT_NACK;
|
return I2C_RESULT_NACK;
|
||||||
}
|
}
|
||||||
|
|
||||||
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, high byte first */
|
||||||
i2c_data_transmit(I2C0, reg_addr);
|
if(_i2c_wait_flag_timeout(I2C_FLAG_TBE) != I2C_RESULT_SUCCESS) {
|
||||||
timeout = 0;
|
state = I2C_STATE_ERROR;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
i2c_data_transmit(I2C0, (uint8_t)(reg_addr >> 8));
|
||||||
|
|
||||||
|
if(_i2c_wait_flag_timeout(I2C_FLAG_TBE) != I2C_RESULT_SUCCESS) {
|
||||||
|
state = I2C_STATE_ERROR;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
i2c_data_transmit(I2C0, (uint8_t)(reg_addr & 0xFF));
|
||||||
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)) {
|
while (data_index < length) {
|
||||||
timeout++;
|
if(_i2c_wait_flag_timeout(I2C_FLAG_TBE) != I2C_RESULT_SUCCESS) {
|
||||||
}
|
state = I2C_STATE_ERROR;
|
||||||
if (timeout >= I2C_TIME_OUT) {
|
break;
|
||||||
state = I2C_STATE_ERROR;
|
}
|
||||||
break;
|
|
||||||
}
|
|
||||||
|
|
||||||
/* send data byte */
|
/* send data byte */
|
||||||
i2c_data_transmit(I2C0, data[data_index]);
|
i2c_data_transmit(I2C0, data[data_index]);
|
||||||
data_index++;
|
data_index++;
|
||||||
|
|
||||||
/* check for errors */
|
/* check for errors */
|
||||||
if (i2c_flag_get(I2C0, I2C_FLAG_AERR)) {
|
if (i2c_flag_get(I2C0, I2C_FLAG_AERR)) {
|
||||||
i2c_stop_on_bus(I2C0);
|
i2c_stop_on_bus(I2C0);
|
||||||
return I2C_RESULT_NACK;
|
return I2C_RESULT_NACK;
|
||||||
} else if (i2c_flag_get(I2C0, I2C_FLAG_BERR) || i2c_flag_get(I2C0, I2C_FLAG_LOSTARB)) {
|
} else if (i2c_flag_get(I2C0, I2C_FLAG_BERR) || i2c_flag_get(I2C0, I2C_FLAG_LOSTARB)) {
|
||||||
i2c_stop_on_bus(I2C0);
|
i2c_stop_on_bus(I2C0);
|
||||||
return I2C_RESULT_ERROR;
|
return I2C_RESULT_ERROR;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
if(state == I2C_STATE_ERROR) break;
|
||||||
|
|
||||||
|
|
||||||
/* check if all data has been sent */
|
/* check if all data has been sent */
|
||||||
if (data_index >= length) {
|
if (data_index >= length) {
|
||||||
/* wait until BTC bit is set for last byte */
|
/* wait until BTC bit is set for last byte */
|
||||||
timeout = 0;
|
if(_i2c_wait_flag_timeout(I2C_FLAG_BTC) != I2C_RESULT_SUCCESS) {
|
||||||
while (!i2c_flag_get(I2C0, I2C_FLAG_BTC) && (timeout < I2C_TIME_OUT)) {
|
|
||||||
timeout++;
|
|
||||||
}
|
|
||||||
if (timeout >= I2C_TIME_OUT) {
|
|
||||||
state = I2C_STATE_ERROR;
|
state = I2C_STATE_ERROR;
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
state = I2C_STATE_STOP;
|
state = I2C_STATE_STOP;
|
||||||
}
|
}
|
||||||
timeout = 0;
|
|
||||||
break;
|
break;
|
||||||
|
|
||||||
case I2C_STATE_STOP:
|
case I2C_STATE_STOP:
|
||||||
@@ -799,17 +811,8 @@ i2c_result_t i2c_write(uint8_t slave_addr, uint8_t reg_addr, uint8_t *data, uint
|
|||||||
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++;
|
|
||||||
}
|
|
||||||
|
|
||||||
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) {
|
||||||
@@ -826,7 +829,7 @@ i2c_result_t i2c_write(uint8_t slave_addr, uint8_t reg_addr, uint8_t *data, uint
|
|||||||
/* small delay before retry */
|
/* small delay before retry */
|
||||||
delay_10us(10);
|
delay_10us(10);
|
||||||
break;
|
break;
|
||||||
|
|
||||||
default:
|
default:
|
||||||
state = I2C_STATE_START;
|
state = I2C_STATE_START;
|
||||||
break;
|
break;
|
||||||
@@ -838,12 +841,12 @@ i2c_result_t i2c_write(uint8_t slave_addr, uint8_t reg_addr, uint8_t *data, uint
|
|||||||
/*!
|
/*!
|
||||||
\brief read data from I2C device with configurable length
|
\brief read data from I2C device with configurable length
|
||||||
\param[in] slave_addr: slave device address (7-bit)
|
\param[in] slave_addr: slave device address (7-bit)
|
||||||
\param[in] reg_addr: register address
|
\param[in] reg_addr: register address (16-bit, high byte first)
|
||||||
\param[out] data: pointer to data buffer
|
\param[out] data: pointer to data buffer
|
||||||
\param[in] length: number of bytes to read (1-255)
|
\param[in] length: number of bytes to read (1-255)
|
||||||
\retval i2c_result_t: operation result
|
\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_result_t i2c_read(uint8_t slave_addr, uint16_t reg_addr, uint8_t *data, uint8_t length) {
|
||||||
i2c_state_t state = I2C_STATE_START;
|
i2c_state_t state = I2C_STATE_START;
|
||||||
uint16_t timeout = 0;
|
uint16_t timeout = 0;
|
||||||
uint8_t retry_count = 0;
|
uint8_t retry_count = 0;
|
||||||
@@ -858,6 +861,10 @@ i2c_result_t i2c_read(uint8_t slave_addr, uint8_t reg_addr, uint8_t *data, uint8
|
|||||||
/* enable acknowledge */
|
/* enable acknowledge */
|
||||||
i2c_ack_config(I2C0, I2C_ACK_ENABLE);
|
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) {
|
while (retry_count < (uint8_t)I2C_MAX_RETRY) {
|
||||||
switch (state) {
|
switch (state) {
|
||||||
case I2C_STATE_START:
|
case I2C_STATE_START:
|
||||||
@@ -865,10 +872,7 @@ i2c_result_t i2c_read(uint8_t slave_addr, uint8_t reg_addr, uint8_t *data, uint8
|
|||||||
data_index = 0;
|
data_index = 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;
|
||||||
}
|
}
|
||||||
@@ -876,15 +880,11 @@ i2c_result_t i2c_read(uint8_t slave_addr, uint8_t reg_addr, uint8_t *data, uint8
|
|||||||
/* 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;
|
||||||
}
|
}
|
||||||
@@ -912,67 +912,92 @@ i2c_result_t i2c_read(uint8_t slave_addr, uint8_t reg_addr, uint8_t *data, uint8
|
|||||||
break;
|
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) {
|
if (write_phase) {
|
||||||
/* clear address flag (write phase) */
|
/* clear address flag (write phase) */
|
||||||
i2c_flag_clear(I2C0, I2C_FLAG_ADDSEND);
|
i2c_flag_clear(I2C0, I2C_FLAG_ADDSEND);
|
||||||
state = I2C_STATE_TRANSMIT_DATA;
|
state = I2C_STATE_TRANSMIT_DATA;
|
||||||
} else {
|
} else {
|
||||||
/* read phase setup based on length */
|
/* 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) {
|
if (length == 1) {
|
||||||
/* single byte read: disable ACK before clearing ADDR */
|
|
||||||
i2c_ack_config(I2C0, I2C_ACK_DISABLE);
|
i2c_ack_config(I2C0, I2C_ACK_DISABLE);
|
||||||
i2c_flag_clear(I2C0, I2C_FLAG_ADDSEND);
|
#ifdef DEBUG_VERBOSE
|
||||||
/* send STOP immediately after clearing ADDR for single byte */
|
printf("I2C READ phase: length=1, disabling ACK before clearing ADDSEND\r\n");
|
||||||
i2c_stop_on_bus(I2C0);
|
#endif
|
||||||
} else if (length == 2) {
|
} else if (length == 2) {
|
||||||
/* two bytes read: set POS=NEXT and disable ACK before clearing ADDR */
|
|
||||||
i2c_ackpos_config(I2C0, I2C_ACKPOS_NEXT);
|
i2c_ackpos_config(I2C0, I2C_ACKPOS_NEXT);
|
||||||
i2c_ack_config(I2C0, I2C_ACK_DISABLE);
|
i2c_ack_config(I2C0, I2C_ACK_DISABLE);
|
||||||
i2c_flag_clear(I2C0, I2C_FLAG_ADDSEND);
|
#ifdef DEBUG_VERBOSE
|
||||||
|
printf("I2C READ phase: length=2, set ACKPOS_NEXT and disabling ACK before clearing ADDSEND\r\n");
|
||||||
|
#endif
|
||||||
} else {
|
} else {
|
||||||
/* multi-byte read: clear ADDR with ACK enabled */
|
/* length > 2: keep ACK enabled so slave will clock out data */
|
||||||
i2c_flag_clear(I2C0, I2C_FLAG_ADDSEND);
|
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;
|
state = I2C_STATE_RECEIVE_DATA;
|
||||||
}
|
}
|
||||||
|
|
||||||
timeout = 0;
|
timeout = 0;
|
||||||
break;
|
break;
|
||||||
|
|
||||||
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;
|
||||||
}
|
}
|
||||||
|
|
||||||
/* send register address */
|
/* send register address, high byte first */
|
||||||
i2c_data_transmit(I2C0, reg_addr);
|
if(_i2c_wait_flag_timeout(I2C_FLAG_TBE) != I2C_RESULT_SUCCESS) {
|
||||||
|
state = I2C_STATE_ERROR;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
i2c_data_transmit(I2C0, (uint8_t)(reg_addr >> 8));
|
||||||
|
|
||||||
|
if(_i2c_wait_flag_timeout(I2C_FLAG_TBE) != I2C_RESULT_SUCCESS) {
|
||||||
|
state = I2C_STATE_ERROR;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
i2c_data_transmit(I2C0, (uint8_t)(reg_addr & 0xFF));
|
||||||
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 */
|
/* wait for byte 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;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#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 */
|
/* generate repeated start condition */
|
||||||
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;
|
||||||
}
|
}
|
||||||
@@ -984,15 +1009,18 @@ i2c_result_t i2c_read(uint8_t slave_addr, uint8_t reg_addr, uint8_t *data, uint8
|
|||||||
write_phase = false;
|
write_phase = false;
|
||||||
state = I2C_STATE_CLEAR_ADDRESS;
|
state = I2C_STATE_CLEAR_ADDRESS;
|
||||||
timeout = 0;
|
timeout = 0;
|
||||||
|
#ifdef DEBUG_VERBOSE
|
||||||
|
printf("I2C addressing sent for read (slave=0x%02X)\r\n", slave_addr);
|
||||||
|
#endif
|
||||||
break;
|
break;
|
||||||
|
|
||||||
case I2C_STATE_RECEIVE_DATA:
|
case I2C_STATE_RECEIVE_DATA:
|
||||||
|
#ifdef DEBUG_VERBOSE
|
||||||
|
printf("I2C RECEIVE_DATA: expecting %u bytes\r\n", length);
|
||||||
|
#endif
|
||||||
if (length == 1) {
|
if (length == 1) {
|
||||||
/* single byte read */
|
/* single byte read */
|
||||||
while ((!i2c_flag_get(I2C0, I2C_FLAG_RBNE)) && (timeout < I2C_TIME_OUT)) {
|
if(_i2c_wait_flag_timeout(I2C_FLAG_RBNE) != I2C_RESULT_SUCCESS) {
|
||||||
timeout++;
|
|
||||||
}
|
|
||||||
if (timeout >= I2C_TIME_OUT) {
|
|
||||||
state = I2C_STATE_ERROR;
|
state = I2C_STATE_ERROR;
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
@@ -1000,10 +1028,7 @@ i2c_result_t i2c_read(uint8_t slave_addr, uint8_t reg_addr, uint8_t *data, uint8
|
|||||||
state = I2C_STATE_STOP;
|
state = I2C_STATE_STOP;
|
||||||
} else if (length == 2) {
|
} else if (length == 2) {
|
||||||
/* two bytes read */
|
/* two bytes read */
|
||||||
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;
|
||||||
}
|
}
|
||||||
@@ -1016,40 +1041,39 @@ i2c_result_t i2c_read(uint8_t slave_addr, uint8_t reg_addr, uint8_t *data, uint8
|
|||||||
} else {
|
} else {
|
||||||
/* multi-byte read (length > 2) */
|
/* multi-byte read (length > 2) */
|
||||||
while (data_index < length) {
|
while (data_index < length) {
|
||||||
/* wait for RBNE (receive buffer not empty) */
|
if (data_index < length - 2) {
|
||||||
while ((!i2c_flag_get(I2C0, I2C_FLAG_RBNE)) && (timeout < I2C_TIME_OUT)) {
|
/* normal bytes: wait for RBNE and read with ACK */
|
||||||
timeout++;
|
if(_i2c_wait_flag_timeout(I2C_FLAG_RBNE) != I2C_RESULT_SUCCESS) {
|
||||||
}
|
|
||||||
if (timeout >= I2C_TIME_OUT) {
|
|
||||||
state = I2C_STATE_ERROR;
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
|
|
||||||
/* special handling for last 3 bytes */
|
|
||||||
if (data_index == length - 3) {
|
|
||||||
/* wait for BTF (byte transfer finished) before reading N-2 */
|
|
||||||
while ((!i2c_flag_get(I2C0, I2C_FLAG_BTC)) && (timeout < I2C_TIME_OUT)) {
|
|
||||||
timeout++;
|
|
||||||
}
|
|
||||||
if (timeout >= I2C_TIME_OUT) {
|
|
||||||
state = I2C_STATE_ERROR;
|
state = I2C_STATE_ERROR;
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
/* disable ACK for last 2 bytes */
|
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);
|
i2c_ack_config(I2C0, I2C_ACK_DISABLE);
|
||||||
}
|
/* send STOP before reading N-1 byte */
|
||||||
|
|
||||||
/* read data byte */
|
|
||||||
data[data_index] = i2c_data_receive(I2C0);
|
|
||||||
data_index++;
|
|
||||||
|
|
||||||
/* send STOP after reading N-1 byte */
|
|
||||||
if (data_index == length - 1) {
|
|
||||||
i2c_stop_on_bus(I2C0);
|
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++;
|
||||||
}
|
}
|
||||||
|
|
||||||
timeout = 0;
|
|
||||||
}
|
}
|
||||||
|
if(state == I2C_STATE_ERROR) break;
|
||||||
state = I2C_STATE_STOP;
|
state = I2C_STATE_STOP;
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
@@ -1072,8 +1096,11 @@ i2c_result_t i2c_read(uint8_t slave_addr, uint8_t reg_addr, uint8_t *data, uint8
|
|||||||
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);
|
#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 */
|
/* reset ACK configuration */
|
||||||
i2c_ack_config(I2C0, I2C_ACK_ENABLE);
|
i2c_ack_config(I2C0, I2C_ACK_ENABLE);
|
||||||
@@ -1082,7 +1109,13 @@ i2c_result_t i2c_read(uint8_t slave_addr, uint8_t reg_addr, uint8_t *data, uint8
|
|||||||
retry_count++;
|
retry_count++;
|
||||||
if (retry_count >= I2C_MAX_RETRY) {
|
if (retry_count >= I2C_MAX_RETRY) {
|
||||||
#ifdef DEBUG_VERBOSE
|
#ifdef DEBUG_VERBOSE
|
||||||
printf("I2C read failed after %d retries\n", I2C_MAX_RETRY);
|
/* 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
|
#endif
|
||||||
return I2C_RESULT_ERROR;
|
return I2C_RESULT_ERROR;
|
||||||
}
|
}
|
||||||
@@ -1101,39 +1134,179 @@ i2c_result_t i2c_read(uint8_t slave_addr, uint8_t reg_addr, uint8_t *data, uint8
|
|||||||
break;
|
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;
|
return I2C_RESULT_TIMEOUT;
|
||||||
}
|
}
|
||||||
|
|
||||||
/* convenience functions for common operations */
|
/*!
|
||||||
i2c_result_t i2c_write_8bits(uint8_t slave_addr, uint8_t reg_addr, uint8_t data) {
|
\brief read data from I2C device without register/sub-address phase
|
||||||
return i2c_write(slave_addr, reg_addr, &data, 1);
|
\param[in] slave_addr: slave device address (7-bit)
|
||||||
}
|
\param[out] data: pointer to data buffer
|
||||||
|
\param[in] length: number of bytes to read (1-255)
|
||||||
|
\retval i2c_result_t: operation result
|
||||||
|
*/
|
||||||
|
i2c_result_t i2c_read_raw(uint8_t slave_addr, uint8_t *data, uint8_t length)
|
||||||
|
{
|
||||||
|
i2c_state_t state = I2C_STATE_START;
|
||||||
|
uint16_t timeout = 0;
|
||||||
|
uint8_t retry_count = 0;
|
||||||
|
uint8_t data_index = 0;
|
||||||
|
|
||||||
i2c_result_t i2c_read_8bits(uint8_t slave_addr, uint8_t reg_addr, uint8_t *data) {
|
if (data == NULL || slave_addr > 0x7F || length == 0) {
|
||||||
return i2c_read(slave_addr, reg_addr, data, 1);
|
|
||||||
}
|
|
||||||
|
|
||||||
i2c_result_t i2c_write_32bits(uint8_t slave_addr, uint8_t reg_addr, uint8_t data[4]) {
|
|
||||||
return i2c_write(slave_addr, reg_addr, data, 4);
|
|
||||||
}
|
|
||||||
|
|
||||||
i2c_result_t i2c_read_32bits(uint8_t slave_addr, uint8_t reg_addr, uint8_t *data) {
|
|
||||||
return i2c_read(slave_addr, reg_addr, data, 4);
|
|
||||||
}
|
|
||||||
|
|
||||||
/* 显示面板专用函数 - 支持读取显示界面参数 */
|
|
||||||
i2c_result_t i2c_read_display_params(uint8_t slave_addr, uint8_t reg_addr, uint8_t *data, uint8_t length) {
|
|
||||||
/* 针对显示面板的多字节读取,支持13字节的完整参数读取 */
|
|
||||||
if (length > 13) {
|
|
||||||
return I2C_RESULT_INVALID_PARAM;
|
return I2C_RESULT_INVALID_PARAM;
|
||||||
}
|
}
|
||||||
return i2c_read(slave_addr, reg_addr, data, length);
|
|
||||||
|
i2c_ack_config(I2C0, I2C_ACK_ENABLE);
|
||||||
|
|
||||||
|
while (retry_count < (uint8_t)I2C_MAX_RETRY) {
|
||||||
|
switch (state) {
|
||||||
|
case I2C_STATE_START:
|
||||||
|
timeout = 0;
|
||||||
|
data_index = 0;
|
||||||
|
|
||||||
|
if (_i2c_wait_flag_clear_timeout(I2C_FLAG_I2CBSY) != I2C_RESULT_SUCCESS) {
|
||||||
|
state = I2C_STATE_ERROR;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
i2c_start_on_bus(I2C0);
|
||||||
|
state = I2C_STATE_SEND_ADDRESS;
|
||||||
|
break;
|
||||||
|
|
||||||
|
case I2C_STATE_SEND_ADDRESS:
|
||||||
|
if (_i2c_wait_flag_timeout(I2C_FLAG_SBSEND) != I2C_RESULT_SUCCESS) {
|
||||||
|
state = I2C_STATE_ERROR;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
i2c_master_addressing(I2C0, (slave_addr << 1), I2C_RECEIVER);
|
||||||
|
state = I2C_STATE_CLEAR_ADDRESS;
|
||||||
|
timeout = 0;
|
||||||
|
break;
|
||||||
|
|
||||||
|
case I2C_STATE_CLEAR_ADDRESS:
|
||||||
|
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;
|
||||||
|
}
|
||||||
|
if (i2c_flag_get(I2C0, I2C_FLAG_AERR)) {
|
||||||
|
i2c_flag_clear(I2C0, I2C_FLAG_AERR);
|
||||||
|
i2c_stop_on_bus(I2C0);
|
||||||
|
return I2C_RESULT_NACK;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (length == 1) {
|
||||||
|
i2c_ack_config(I2C0, I2C_ACK_DISABLE);
|
||||||
|
} else if (length == 2) {
|
||||||
|
i2c_ackpos_config(I2C0, I2C_ACKPOS_NEXT);
|
||||||
|
i2c_ack_config(I2C0, I2C_ACK_DISABLE);
|
||||||
|
} else {
|
||||||
|
i2c_ack_config(I2C0, I2C_ACK_ENABLE);
|
||||||
|
}
|
||||||
|
|
||||||
|
i2c_flag_clear(I2C0, I2C_FLAG_ADDSEND);
|
||||||
|
state = I2C_STATE_RECEIVE_DATA;
|
||||||
|
timeout = 0;
|
||||||
|
break;
|
||||||
|
|
||||||
|
case I2C_STATE_RECEIVE_DATA:
|
||||||
|
if (length == 1) {
|
||||||
|
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) {
|
||||||
|
if (_i2c_wait_flag_timeout(I2C_FLAG_BTC) != I2C_RESULT_SUCCESS) {
|
||||||
|
state = I2C_STATE_ERROR;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
i2c_stop_on_bus(I2C0);
|
||||||
|
data[0] = i2c_data_receive(I2C0);
|
||||||
|
data[1] = i2c_data_receive(I2C0);
|
||||||
|
state = I2C_STATE_STOP;
|
||||||
|
} else {
|
||||||
|
while (data_index < length) {
|
||||||
|
if (data_index < length - 2) {
|
||||||
|
if (_i2c_wait_flag_timeout(I2C_FLAG_RBNE) != I2C_RESULT_SUCCESS) {
|
||||||
|
state = I2C_STATE_ERROR;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
data[data_index++] = i2c_data_receive(I2C0);
|
||||||
|
} else if (data_index == length - 2) {
|
||||||
|
if (_i2c_wait_flag_timeout(I2C_FLAG_BTC) != I2C_RESULT_SUCCESS) {
|
||||||
|
state = I2C_STATE_ERROR;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
i2c_ack_config(I2C0, I2C_ACK_DISABLE);
|
||||||
|
i2c_stop_on_bus(I2C0);
|
||||||
|
data[data_index++] = i2c_data_receive(I2C0);
|
||||||
|
} else {
|
||||||
|
if (_i2c_wait_flag_timeout(I2C_FLAG_RBNE) != I2C_RESULT_SUCCESS) {
|
||||||
|
state = I2C_STATE_ERROR;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
data[data_index++] = i2c_data_receive(I2C0);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (state == I2C_STATE_ERROR) break;
|
||||||
|
state = I2C_STATE_STOP;
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
|
||||||
|
case I2C_STATE_STOP:
|
||||||
|
while ((I2C_CTL0(I2C0) & I2C_CTL0_STOP) && (timeout < I2C_TIME_OUT)) {
|
||||||
|
timeout++;
|
||||||
|
}
|
||||||
|
if (timeout >= I2C_TIME_OUT) {
|
||||||
|
state = I2C_STATE_ERROR;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
i2c_ack_config(I2C0, I2C_ACK_ENABLE);
|
||||||
|
i2c_ackpos_config(I2C0, I2C_ACKPOS_CURRENT);
|
||||||
|
return I2C_RESULT_SUCCESS;
|
||||||
|
|
||||||
|
case I2C_STATE_ERROR:
|
||||||
|
i2c_bus_reset();
|
||||||
|
i2c_ack_config(I2C0, I2C_ACK_ENABLE);
|
||||||
|
i2c_ackpos_config(I2C0, I2C_ACKPOS_CURRENT);
|
||||||
|
|
||||||
|
retry_count++;
|
||||||
|
if (retry_count >= I2C_MAX_RETRY) {
|
||||||
|
return I2C_RESULT_ERROR;
|
||||||
|
}
|
||||||
|
|
||||||
|
state = I2C_STATE_START;
|
||||||
|
timeout = 0;
|
||||||
|
delay_10us(10);
|
||||||
|
break;
|
||||||
|
|
||||||
|
default:
|
||||||
|
state = I2C_STATE_START;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return I2C_RESULT_TIMEOUT;
|
||||||
}
|
}
|
||||||
|
|
||||||
#ifdef DEBUG_VERBOSE
|
#ifdef DEBUG_VERBOSE
|
||||||
/*!
|
/*!
|
||||||
\brief get status string for debugging
|
\brief get status string for debugging
|
||||||
\param[in] status: i2c_status_t value
|
\param[in] status: i2c_result_t value
|
||||||
\param[out] none
|
\param[out] none
|
||||||
\retval const char* status string
|
\retval const char* status string
|
||||||
*/
|
*/
|
||||||
|
|||||||
+21
-9
@@ -148,11 +148,17 @@ ldc1612_status_t ldc1612_init(void) {
|
|||||||
*/
|
*/
|
||||||
ldc1612_status_t ldc1612_config_single_channel(uint8_t channel) {
|
ldc1612_status_t ldc1612_config_single_channel(uint8_t channel) {
|
||||||
i2c_result_t status;
|
i2c_result_t status;
|
||||||
|
uint16_t freq_divider;
|
||||||
|
|
||||||
if (channel > 1) {
|
if (channel > 1) {
|
||||||
return LDC1612_STATUS_INVALID_PARAM;
|
return LDC1612_STATUS_INVALID_PARAM;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
freq_divider = ldc1612_calculate_freq_divider(channel);
|
||||||
|
if (freq_divider == 0) {
|
||||||
|
return LDC1612_STATUS_INVALID_PARAM;
|
||||||
|
}
|
||||||
|
|
||||||
/* 配置顺序严格按照TI官方文档要求 */
|
/* 配置顺序严格按照TI官方文档要求 */
|
||||||
|
|
||||||
/* Step 1: 确保传感器处于睡眠模式 - 配置前必须 */
|
/* Step 1: 确保传感器处于睡眠模式 - 配置前必须 */
|
||||||
@@ -161,28 +167,34 @@ ldc1612_status_t ldc1612_config_single_channel(uint8_t channel) {
|
|||||||
delay_ms(10);
|
delay_ms(10);
|
||||||
|
|
||||||
/* Step 2: 配置频率分频 - 必须在其他配置之前 */
|
/* Step 2: 配置频率分频 - 必须在其他配置之前 */
|
||||||
uint16_t freq_divider = ldc1612_calculate_freq_divider(channel);
|
status = ldc1612_write_register(SET_FREQ_REG_START + channel, freq_divider);
|
||||||
ldc1612_write_register(SET_FREQ_REG_START + channel, freq_divider);
|
if (status != I2C_RESULT_SUCCESS) return LDC1612_STATUS_ERROR;
|
||||||
delay_ms(5);
|
delay_ms(5);
|
||||||
|
|
||||||
/* Step 3: 配置LC稳定时间 - 影响测量精度 */
|
/* Step 3: 配置LC稳定时间 - 影响测量精度 */
|
||||||
ldc1612_write_register(SET_SETTLECOUNT_REG_START + channel, LDC1612_SETTLECOUNT_CH0);
|
status = ldc1612_write_register(SET_SETTLECOUNT_REG_START + channel, LDC1612_SETTLECOUNT_CH0);
|
||||||
|
if (status != I2C_RESULT_SUCCESS) return LDC1612_STATUS_ERROR;
|
||||||
|
|
||||||
/* Step 4: 配置转换时间 - 影响测量速度和精度 */
|
/* Step 4: 配置转换时间 - 影响测量速度和精度 */
|
||||||
ldc1612_write_register(SET_CONVERSION_TIME_REG_START + channel, LDC1612_RCOUNT_TIME_CH0);
|
status = ldc1612_write_register(SET_CONVERSION_TIME_REG_START + channel, LDC1612_RCOUNT_TIME_CH0);
|
||||||
|
if (status != I2C_RESULT_SUCCESS) return LDC1612_STATUS_ERROR;
|
||||||
|
|
||||||
/* Step 5: 配置转换偏移 */
|
/* Step 5: 配置转换偏移 */
|
||||||
ldc1612_write_register(SET_CONVERSION_OFFSET_REG_START + channel, SET_CONVERSION_OFFSET_CH0);
|
status = ldc1612_write_register(SET_CONVERSION_OFFSET_REG_START + channel, SET_CONVERSION_OFFSET_CH0);
|
||||||
|
if (status != I2C_RESULT_SUCCESS) return LDC1612_STATUS_ERROR;
|
||||||
|
|
||||||
/* Step 6: 配置驱动电流 - 影响传感器灵敏度 */
|
/* Step 6: 配置驱动电流 - 影响传感器灵敏度 */
|
||||||
ldc1612_write_register(SET_DRIVER_CURRENT_REG + channel, LDC1612_DRIVE_CURRENT);
|
status = ldc1612_write_register(SET_DRIVER_CURRENT_REG + channel, LDC1612_DRIVE_CURRENT);
|
||||||
|
if (status != I2C_RESULT_SUCCESS) return LDC1612_STATUS_ERROR;
|
||||||
|
|
||||||
/* Step 7: 配置多路复用器 - 设置通道选择和滤波 */
|
/* Step 7: 配置多路复用器 - 设置通道选择和滤波 */
|
||||||
// ldc1612_configure_mux_register(LDC1612_MUX_AUTOSCAN_DISABLE, LDC1612_MUX_RR_SEQUENCE_0, LDC1612_MUX_FILTER_ALL_LOW, LDC1612_MUX_FILTER_NONE);
|
// ldc1612_configure_mux_register(LDC1612_MUX_AUTOSCAN_DISABLE, LDC1612_MUX_RR_SEQUENCE_0, LDC1612_MUX_FILTER_ALL_LOW, LDC1612_MUX_FILTER_NONE);
|
||||||
ldc1612_write_register(MUX_CONFIG_REG, LDC1612_MUX_CONFIG);
|
status = ldc1612_write_register(MUX_CONFIG_REG, LDC1612_MUX_CONFIG);
|
||||||
|
if (status != I2C_RESULT_SUCCESS) return LDC1612_STATUS_ERROR;
|
||||||
|
|
||||||
/* Step 8: 配置错误输出 */
|
/* Step 8: 配置错误输出 */
|
||||||
ldc1612_write_register(ERROR_CONFIG_REG, LDC1612_ERROR_CONFIG_DEFAULT);
|
status = ldc1612_write_register(ERROR_CONFIG_REG, LDC1612_ERROR_CONFIG_DEFAULT);
|
||||||
|
if (status != I2C_RESULT_SUCCESS) return LDC1612_STATUS_ERROR;
|
||||||
|
|
||||||
/* Step 9: 最后启动传感器 - 必须最后一步 */
|
/* Step 9: 最后启动传感器 - 必须最后一步 */
|
||||||
status = ldc1612_write_register(SENSOR_CONFIG_REG, LDC1612_SENSOR_CONFIG_CH0);
|
status = ldc1612_write_register(SENSOR_CONFIG_REG, LDC1612_SENSOR_CONFIG_CH0);
|
||||||
@@ -332,4 +344,4 @@ uint16_t ldc1612_check_status_and_log_errors(void) {
|
|||||||
|
|
||||||
// 读取STATUS寄存器会自动清除错误标志,但不会清除DRDY和UNREADCONV标志
|
// 读取STATUS寄存器会自动清除错误标志,但不会清除DRDY和UNREADCONV标志
|
||||||
return status;
|
return status;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -41,7 +41,7 @@
|
|||||||
#define __HXTAL (HXTAL_VALUE) /* high speed crystal oscillator frequency */
|
#define __HXTAL (HXTAL_VALUE) /* high speed crystal oscillator frequency */
|
||||||
#define __SYS_OSC_CLK (__IRC8M) /* main oscillator frequency */
|
#define __SYS_OSC_CLK (__IRC8M) /* main oscillator frequency */
|
||||||
|
|
||||||
#define VECT_TAB_OFFSET (uint32_t)0x00 /* vector table base offset */
|
#define VECT_TAB_OFFSET (uint32_t)0x2000 /* vector table base offset */
|
||||||
|
|
||||||
/* select a system clock by uncommenting the following line */
|
/* select a system clock by uncommenting the following line */
|
||||||
//#define __SYSTEM_CLOCK_8M_HXTAL (__HXTAL)
|
//#define __SYSTEM_CLOCK_8M_HXTAL (__HXTAL)
|
||||||
|
|||||||
+1
-2
@@ -86,8 +86,7 @@ tmp112a_status_t tmp112a_read_temperature(tmp112a_result_t *result) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
void tmp112a_get_raw_temperature_value(uint8_t *value) {
|
void tmp112a_get_raw_temperature_value(uint8_t *value) {
|
||||||
// i2c_read_16bits(TMP112A_ADDR, TMP112A_TEMP_REG, value);
|
i2c_read_16bits(TMP112A_ADDR, TMP112A_TEMP_REG, value);
|
||||||
i2c_read(TMP112A_ADDR, TMP112A_TEMP_REG, value, 2);
|
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -1,5 +1,61 @@
|
|||||||
# You can change TOOLCHAIN_DIRECTORY if you want to use different toolchain.
|
# The project never stores a compiler package. Each machine supplies a path.
|
||||||
set(TOOLCHAIN_DIRECTORY "${CMAKE_SOURCE_DIR}/Toolchain/xpack-arm-none-eabi-gcc-11.3.1-1.1/bin")
|
# TOOLCHAIN_DIRECTORY (CMake cache) has priority over the environment variable.
|
||||||
|
# GD32_GCC_ENV_VAR lets a workstation use a different variable name if needed.
|
||||||
|
set(TOOLCHAIN_DIRECTORY "" CACHE PATH "Path to arm-none-eabi toolchain bin directory")
|
||||||
|
set(GD32_GCC_ENV_VAR "GD32_GCC_BIN" CACHE STRING "Environment variable containing the ARM GCC path")
|
||||||
|
set(CMAKE_TRY_COMPILE_PLATFORM_VARIABLES TOOLCHAIN_DIRECTORY GD32_GCC_ENV_VAR)
|
||||||
|
|
||||||
|
if(GD32_GCC_ENV_VAR AND NOT GD32_GCC_ENV_VAR MATCHES "^GD32_[A-Za-z0-9_]+$")
|
||||||
|
message(FATAL_ERROR "GD32_GCC_ENV_VAR must use the GD32_ prefix: ${GD32_GCC_ENV_VAR}")
|
||||||
|
endif()
|
||||||
|
|
||||||
|
if(NOT TOOLCHAIN_DIRECTORY AND GD32_GCC_ENV_VAR)
|
||||||
|
if(DEFINED ENV{${GD32_GCC_ENV_VAR}} AND NOT "$ENV{${GD32_GCC_ENV_VAR}}" STREQUAL "")
|
||||||
|
file(TO_CMAKE_PATH "$ENV{${GD32_GCC_ENV_VAR}}" TOOLCHAIN_DIRECTORY)
|
||||||
|
endif()
|
||||||
|
endif()
|
||||||
|
|
||||||
|
if(NOT TOOLCHAIN_DIRECTORY)
|
||||||
|
message(FATAL_ERROR
|
||||||
|
"Cannot find ARM GCC toolchain. Set ${GD32_GCC_ENV_VAR} in the machine environment "
|
||||||
|
"or pass -DTOOLCHAIN_DIRECTORY=<toolchain-bin>.")
|
||||||
|
endif()
|
||||||
|
|
||||||
|
file(TO_CMAKE_PATH "${TOOLCHAIN_DIRECTORY}" TOOLCHAIN_DIRECTORY)
|
||||||
|
get_filename_component(TOOLCHAIN_DIRECTORY "${TOOLCHAIN_DIRECTORY}" ABSOLUTE)
|
||||||
|
|
||||||
|
if(WIN32)
|
||||||
|
set(TOOLCHAIN_SUFFIX ".exe")
|
||||||
|
else()
|
||||||
|
set(TOOLCHAIN_SUFFIX "")
|
||||||
|
endif()
|
||||||
|
|
||||||
|
# Accept either the package root or its bin directory.
|
||||||
|
if(NOT EXISTS "${TOOLCHAIN_DIRECTORY}/arm-none-eabi-gcc${TOOLCHAIN_SUFFIX}" AND
|
||||||
|
EXISTS "${TOOLCHAIN_DIRECTORY}/bin/arm-none-eabi-gcc${TOOLCHAIN_SUFFIX}")
|
||||||
|
set(TOOLCHAIN_DIRECTORY "${TOOLCHAIN_DIRECTORY}/bin")
|
||||||
|
endif()
|
||||||
|
|
||||||
|
set(_ARM_GCC_EXECUTABLE "${TOOLCHAIN_DIRECTORY}/arm-none-eabi-gcc${TOOLCHAIN_SUFFIX}")
|
||||||
|
|
||||||
|
set(_TOOLCHAIN_BINARIES
|
||||||
|
arm-none-eabi-gcc
|
||||||
|
arm-none-eabi-g++
|
||||||
|
arm-none-eabi-gdb
|
||||||
|
arm-none-eabi-ar
|
||||||
|
arm-none-eabi-ranlib
|
||||||
|
arm-none-eabi-as
|
||||||
|
arm-none-eabi-ld
|
||||||
|
arm-none-eabi-objcopy
|
||||||
|
arm-none-eabi-objdump
|
||||||
|
arm-none-eabi-size
|
||||||
|
)
|
||||||
|
foreach(_binary IN LISTS _TOOLCHAIN_BINARIES)
|
||||||
|
if(NOT EXISTS "${TOOLCHAIN_DIRECTORY}/${_binary}${TOOLCHAIN_SUFFIX}")
|
||||||
|
message(FATAL_ERROR
|
||||||
|
"ARM GCC toolchain is incomplete; missing ${_binary}${TOOLCHAIN_SUFFIX} in ${TOOLCHAIN_DIRECTORY}")
|
||||||
|
endif()
|
||||||
|
endforeach()
|
||||||
|
|
||||||
set(CMAKE_C_FLAGS_DEBUG "")
|
set(CMAKE_C_FLAGS_DEBUG "")
|
||||||
set(CMAKE_CXX_FLAGS_DEBUG "")
|
set(CMAKE_CXX_FLAGS_DEBUG "")
|
||||||
@@ -12,10 +68,6 @@ set(CMAKE_COLOR_DIAGNOSTICS ON)
|
|||||||
|
|
||||||
set(CMAKE_EXPORT_COMPILE_COMMANDS ON CACHE INTERNAL "")
|
set(CMAKE_EXPORT_COMPILE_COMMANDS ON CACHE INTERNAL "")
|
||||||
|
|
||||||
if(WIN32)
|
|
||||||
set(TOOLCHAIN_SUFFIX ".exe")
|
|
||||||
endif()
|
|
||||||
|
|
||||||
set(CMAKE_SYSTEM_NAME Generic)
|
set(CMAKE_SYSTEM_NAME Generic)
|
||||||
set(CMAKE_SYSTEM_PROCESSOR arm)
|
set(CMAKE_SYSTEM_PROCESSOR arm)
|
||||||
|
|
||||||
@@ -28,21 +80,48 @@ set(FLAGS "-mcpu=cortex-m23 -std=gnu11 -fstack-usage -
|
|||||||
set(ASM_FLAGS "-x assembler-with-cpp")
|
set(ASM_FLAGS "-x assembler-with-cpp")
|
||||||
set(CPP_FLAGS "-fno-rtti -fno-exceptions -fno-threadsafe-statics")
|
set(CPP_FLAGS "-fno-rtti -fno-exceptions -fno-threadsafe-statics")
|
||||||
|
|
||||||
set(CMAKE_C_COMPILER ${TOOLCHAIN_PREFIX}gcc${TOOLCHAIN_SUFFIX})
|
set(_REQUESTED_C_COMPILER "${TOOLCHAIN_PREFIX}gcc${TOOLCHAIN_SUFFIX}")
|
||||||
set(CMAKE_CXX_COMPILER ${TOOLCHAIN_PREFIX}g++${TOOLCHAIN_SUFFIX})
|
if(DEFINED CMAKE_C_COMPILER AND CMAKE_C_COMPILER AND
|
||||||
set(CMAKE_ASM_COMPILER ${TOOLCHAIN_PREFIX}gcc${TOOLCHAIN_SUFFIX})
|
IS_ABSOLUTE "${CMAKE_C_COMPILER}" AND
|
||||||
|
NOT CMAKE_C_COMPILER STREQUAL "${_REQUESTED_C_COMPILER}")
|
||||||
|
message(FATAL_ERROR
|
||||||
|
"The build directory is already configured for a different ARM GCC compiler:\n"
|
||||||
|
" cached: ${CMAKE_C_COMPILER}\n"
|
||||||
|
" selected: ${_REQUESTED_C_COMPILER}\n"
|
||||||
|
"Delete the build directory or run CMake Tools 'Delete Cache and Reconfigure'.")
|
||||||
|
endif()
|
||||||
|
|
||||||
|
set(CMAKE_C_COMPILER "${_REQUESTED_C_COMPILER}")
|
||||||
|
set(CMAKE_CXX_COMPILER "${TOOLCHAIN_PREFIX}g++${TOOLCHAIN_SUFFIX}")
|
||||||
|
set(CMAKE_ASM_COMPILER "${TOOLCHAIN_PREFIX}gcc${TOOLCHAIN_SUFFIX}")
|
||||||
|
|
||||||
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} ${FLAGS}")
|
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} ${FLAGS}")
|
||||||
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${FLAGS} ${CPP_FLAGS}")
|
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${FLAGS} ${CPP_FLAGS}")
|
||||||
set(CMAKE_ASM_FLAGS "${CMAKE_ASM_FLAGS} ${FLAGS} ${ASM_FLAGS}")
|
set(CMAKE_ASM_FLAGS "${CMAKE_ASM_FLAGS} ${FLAGS} ${ASM_FLAGS}")
|
||||||
set(CMAKE_OBJCOPY ${TOOLCHAIN_PREFIX}objcopy${TOOLCHAIN_SUFFIX})
|
set(CMAKE_AR "${TOOLCHAIN_PREFIX}ar${TOOLCHAIN_SUFFIX}")
|
||||||
set(CMAKE_SIZE ${TOOLCHAIN_PREFIX}size${TOOLCHAIN_SUFFIX})
|
set(CMAKE_RANLIB "${TOOLCHAIN_PREFIX}ranlib${TOOLCHAIN_SUFFIX}")
|
||||||
set(CMAKE_OBJDUMP ${TOOLCHAIN_PREFIX}objdump${TOOLCHAIN_SUFFIX})
|
set(CMAKE_OBJCOPY "${TOOLCHAIN_PREFIX}objcopy${TOOLCHAIN_SUFFIX}")
|
||||||
set(CMAKE_AS ${TOOLCHAIN_PREFIX}as${TOOLCHAIN_SUFFIX})
|
set(CMAKE_SIZE "${TOOLCHAIN_PREFIX}size${TOOLCHAIN_SUFFIX}")
|
||||||
set(CMAKE_LD ${TOOLCHAIN_PREFIX}ld${TOOLCHAIN_SUFFIX})
|
set(CMAKE_OBJDUMP "${TOOLCHAIN_PREFIX}objdump${TOOLCHAIN_SUFFIX}")
|
||||||
|
set(CMAKE_AS "${TOOLCHAIN_PREFIX}as${TOOLCHAIN_SUFFIX}")
|
||||||
|
set(CMAKE_LD "${TOOLCHAIN_PREFIX}ld${TOOLCHAIN_SUFFIX}")
|
||||||
|
set(CMAKE_LINKER "${TOOLCHAIN_PREFIX}ld${TOOLCHAIN_SUFFIX}")
|
||||||
|
|
||||||
|
execute_process(
|
||||||
|
COMMAND "${_ARM_GCC_EXECUTABLE}" --version
|
||||||
|
OUTPUT_VARIABLE _ARM_GCC_VERSION_OUTPUT
|
||||||
|
ERROR_VARIABLE _ARM_GCC_VERSION_ERROR
|
||||||
|
RESULT_VARIABLE _ARM_GCC_VERSION_RESULT
|
||||||
|
OUTPUT_STRIP_TRAILING_WHITESPACE
|
||||||
|
)
|
||||||
|
if(NOT _ARM_GCC_VERSION_RESULT EQUAL 0)
|
||||||
|
message(FATAL_ERROR "Unable to execute ${_ARM_GCC_EXECUTABLE}: ${_ARM_GCC_VERSION_ERROR}")
|
||||||
|
endif()
|
||||||
|
string(REGEX MATCH "[0-9]+\\.[0-9]+\\.[0-9]+" _ARM_GCC_VERSION "${_ARM_GCC_VERSION_OUTPUT}")
|
||||||
|
message(STATUS "Using ARM GCC ${_ARM_GCC_VERSION} from ${TOOLCHAIN_DIRECTORY}")
|
||||||
|
|
||||||
set(CMAKE_EXECUTABLE_SUFFIX_ASM ".elf")
|
set(CMAKE_EXECUTABLE_SUFFIX_ASM ".elf")
|
||||||
set(CMAKE_EXECUTABLE_SUFFIX_C ".elf")
|
set(CMAKE_EXECUTABLE_SUFFIX_C ".elf")
|
||||||
set(CMAKE_EXECUTABLE_SUFFIX_CXX ".elf")
|
set(CMAKE_EXECUTABLE_SUFFIX_CXX ".elf")
|
||||||
|
|
||||||
set(CMAKE_TRY_COMPILE_TARGET_TYPE STATIC_LIBRARY)
|
set(CMAKE_TRY_COMPILE_TARGET_TYPE STATIC_LIBRARY)
|
||||||
|
|||||||
@@ -3,7 +3,7 @@ set(PROJECT_NAME "LDC1612")
|
|||||||
set(BOARD_TYPE_CODE 23)
|
set(BOARD_TYPE_CODE 23)
|
||||||
set(VERSION_MAJOR 1)
|
set(VERSION_MAJOR 1)
|
||||||
set(VERSION_MINOR 0)
|
set(VERSION_MINOR 0)
|
||||||
set(VERSION_PATCH 1)
|
set(VERSION_PATCH 2)
|
||||||
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")
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user