Files
blink/README.md
T
2026-07-27 20:00:06 +08:00

174 lines
5.3 KiB
Markdown
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
# MSPM0G3507 LED Blink
基于 TI MSPM0G3507 的 LED 闪烁项目,纯命令行工具链(ARM GCC + CMake + Ninja + pyOCD),不依赖任何 IDE。
## 硬件
| 项目 | 说明 |
|------|------|
| 芯片 | MSPM0G3507 (Cortex-M0+, 80MHz, 128KB Flash / 32KB SRAM) |
| 调试器 | 通用 DAPLink / CMSIS-DAP |
| LED | PB9,低有效(低电平点亮) |
## 开发环境
| 工具 | 版本 | 用途 |
|------|------|------|
| arm-none-eabi-gcc | 15.2+ | 交叉编译器 |
| CMake | 3.20+ | 构建系统 |
| Ninja | 1.10+ | 构建后端 |
| Python | 3.10+ | pyOCD 运行环境 |
| pyOCD | 0.45+ | 烧录与调试 |
| SysConfig | 1.28+ | 引脚/时钟配置生成 |
| clangd | 18+ | LSP 语言服务器 (IntelliSense) |
| MSPM0 SDK | 2.11+ | 驱动库与启动文件 |
### 快速安装
```powershell
# pyOCD(含 MSPM0 Flash 算法)
pip install pyocd
python -m pyocd pack install MSPM0G3507
```
MSPM0 SDK 和 SysConfig 需从 [TI 官网](https://www.ti.com/tool/MSPM0-SDK) 下载安装。
## 构建
```powershell
# 配置(首次或 CMakeLists.txt 变更后)
# PowerShell 中 .cmake 需引号包裹,否则会被拆开
cmake -G Ninja -S . -B build "-DCMAKE_TOOLCHAIN_FILE=gcc-arm-none-eabi.cmake" -DCMAKE_EXPORT_COMPILE_COMMANDS=ON
# 编译
ninja -C build
# 或 cd 进 build 目录后直接
cd build
ninja
```
### LSP / IntelliSense (clangd)
项目根目录已配置 `.clangd`,指向 `build/compile_commands.json`。
安装 VS Code 扩展 **clangd**(LLVM 官方)并**禁用** Microsoft C/C++ IntelliSense 即可使用。
## 烧录
一条命令搞定(自动连接、擦除、写入、校验、断开):
```powershell
python -m pyocd flash -t mspm0g3507 build/blink.elf
```
输出示例:
```
Erased 1024 bytes (1 sector), programmed 1024 bytes (1 page)
```
## 调试(GDB)
需要两个终端协作,**先启动服务器再连 GDB**:
```powershell
# 终端 1:启动 GDB 服务器(保持运行)
# DAPLink 克隆版建议降频避免通信错误
python -m pyocd gdbserver -t mspm0g3507 -f 500000
# 等看到 "GDB server listening on port 3333" 后,
# 终端 2:启动 GDB
arm-none-eabi-gdb build/blink.elf
```
GDB 内手动连接:
```gdb
target remote :3333 # 连接 pyOCD 服务器
load # 烧录固件
monitor reset # 复位芯片
continue # 开始运行
```
常用命令:
```gdb
# ── 断点 ──────────────────────────
break main # main 函数入口断点
break src/main.c:20 # 文件行号断点
info breakpoints # 列出所有断点
delete 1 # 删除 1 号断点
# ── 运行 ──────────────────────────
monitor reset # 复位芯片
continue (缩写 c) # 继续运行
step (缩写 s) # 单步进入函数
next (缩写 n) # 单步跳过函数
finish (缩写 fin) # 执行到当前函数返回
# ── 查看 ──────────────────────────
print DELAY # 查看变量/宏
print/x GPIO_LED_LED_PIN # 十六进制查看
backtrace (缩写 bt) # 调用栈
info registers # 寄存器
# ── 退出 ──────────────────────────
quit # 退出 GDB
```
### 典型调试流程
```gdb
target remote :3333
load
break main
monitor reset
continue
# → 停在 main 第一行
next # 执行 SYSCFG_DL_init()
next # 进入 while(1)
print cycle # 检查 cycle 值
continue # LED 开始闪
# 按 Ctrl+C 暂停
break src/main.c:20 # 在看 CLEAR 还是 SET 的地方断
continue # 停在断点
next # 单步看 LED ON/OFF
```
## VS Code
安装 [Cortex-Debug](https://marketplace.visualstudio.com/items?itemName=marus25.cortex-debug) 扩展后:
| 操作 | 按键 |
|------|:--:|
| 编译 | `Ctrl+Shift+B` |
| 编译 + 烧录 | `Ctrl+Shift+P` → `Run Task` → **Build & Flash** |
| 调试 | `F5`(自动启动 pyOCD → 烧录 → 停在 `main`) |
断点、单步、变量查看全在图形界面操作。首次 `F5` 会启动 pyOCD,之后每次自动复用已有服务器。
## 项目结构
```
blink/
├── .vscode/ # VS Code 配置 (纳入 Git)
│ ├── settings.json # CMake 工具链配置
│ ├── launch.json # Cortex-Debug F5 调试
│ └── tasks.json # Ctrl+Shift+B 编译 / Build & Flash
├── .clangd # clangd IntelliSense 配置
├── .gitignore
├── CMakeLists.txt # CMake 构建配置
├── gcc-arm-none-eabi.cmake # ARM 交叉编译工具链
├── blink.syscfg # SysConfig 引脚/时钟配置
├── start_pyocd.ps1 # pyOCD 服务器自动启动脚本
├── src/
│ └── main.c # 应用代码
├── include/ # 应用头文件
├── lib/ # 第三方库
└── build/ # 构建产物 (gitignore)
└── blink.elf # 固件
```