generated from hulk/gd32e23x_template_cmake_vscode
Compare commits
6
Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
c7433a8df7 | ||
|
|
722b02e755 | ||
|
|
0022afb751 | ||
|
|
f556d1f590 | ||
|
|
8dcbfa9f99 | ||
|
|
4af24a4dfb |
@@ -1,30 +1,38 @@
|
|||||||
# AGENTS.md — ldc1612_cmake_vscode 项目约定与进度
|
# AGENTS.md — CeramixNano EDDY(ldc1612_cmake_vscode)项目约定与进度
|
||||||
|
|
||||||
## 项目概述
|
## 项目概述
|
||||||
|
|
||||||
GD32E230 电涡流传感器节点固件:LDC1612(电涡流/电感数字转换器)+ TMP112(板载温度),
|
CeramixNano EDDY 电涡流传感器单板固件(工程目录名沿用 ldc1612_cmake_vscode):
|
||||||
通过 RS485 串口命令协议(`D5 03 LEN [cmd] CRC`)与上位机通信,支持温度补偿与标定。
|
LDC1612(电涡流/电感数字转换器)+ TMP112(板载温度),RS485 串口命令协议
|
||||||
|
(`D5 03 LEN [cmd] CRC`,板卡类型 0x03),支持温度补偿与标定。
|
||||||
|
|
||||||
- 协议文档:`CommunicationProtocol.md`;寄存器速查:`LDC1612_REG_LIST.md`
|
- 协议文档:`CommunicationProtocol.md`;寄存器速查:`LDC1612_REG_LIST.md`
|
||||||
- 版本策略:`Inc/version.h`(BOARD_TYPE_CODE.主.次.修订,当前 23.1.0.1)
|
- 命名/版本规则(2026-10-24 起):产物名 `CeramixNano-EDDY_*`,板卡码 **26**
|
||||||
|
(M999 应答 `v26.主.次.修订`,与 MB 的 25 同源规则);版本维护在
|
||||||
|
`cmake/project_config.cmake`(编译时生成 version.h,`Inc/version.h` 说法已过时)
|
||||||
|
|
||||||
## 工具链与构建
|
## 工具链与构建
|
||||||
|
|
||||||
- 语言/环境:C99,GD32E23x 标准库(`SDK/`)
|
- 语言/环境:C99,GD32E23x 标准库(`SDK/`),MCU GD32E230F8(64K Flash/8K RAM)
|
||||||
- 编译器:`arm-none-eabi-gcc`(D:\toolchain\xpack-arm-none-eabi-gcc-15.2.1-1.1)
|
- 编译器:xpack-arm-none-eabi-gcc 15.2.1(`GD32_GCC_BIN` 环境变量解析)
|
||||||
- 构建:CMake + Ninja,preset `Debug` / `Release`,输出 `Build/<preset>/Application/Application.elf`
|
- 构建:CMake + Ninja,preset `Debug` / `Release`,输出 `Build/<preset>/Application/`
|
||||||
- 命令行构建:`cmake --build Build\Debug`(需 arm-none-eabi-gcc/cmake/ninja 在 PATH)
|
及 `Build/<preset>/CeramixNano-EDDY_<版本>_<variant>_<日期>.bin/.hex` 等
|
||||||
|
(variant:dev 线 `APP`,golden 线 `golden`)
|
||||||
- 烧录/调试(.vscode/tasks.json、launch.json):
|
- 烧录/调试(.vscode/tasks.json、launch.json):
|
||||||
- OpenOCD:`GD32_OPENOCD_BIN` 指向定制构建(含 gd32e23x flash 驱动,CMSIS-DAP HID 后端)
|
- OpenOCD:`GD32_OPENOCD_BIN` 指向定制构建(含 gd32e23x flash 驱动,CMSIS-DAP 后端)
|
||||||
- `Flash MCU (DAPLink)` 任务 + `.vscode/openocd_daplink_gd32e23x.cfg`:
|
- `Flash MCU (DAPLink)` 任务 + `.vscode/openocd_daplink_gd32e23x.cfg`:
|
||||||
适配 DAPLink 探针(无 connect_assert_srst,当前共享 cfg 的 connect_assert_srst
|
适配 DAPLink 探针(无 connect_assert_srst;该选项在此 DAPLink 上会导致
|
||||||
会导致 Cortex-M23 识别失败);烧录命令 `program Application.elf verify reset exit`
|
examination 失败,但 SRST 线是接通的,见下文排障)
|
||||||
- J-Link 任务需 SEGGER 探针 + PATH 中 JLink.exe(当前接的是 DAPLink,不适用)
|
- J-Link 任务需 SEGGER 探针 + PATH 中 JLink.exe(当前接的是 DAPLink,不适用)
|
||||||
- 调试器探针历史问题(2026-10-02 已解决):DAPLink 的 CMSIS-DAP v1 接口曾被 Zadig
|
- 调试器探针历史问题(2026-10-02 已解决):DAPLink 的 CMSIS-DAP v1 接口曾被 Zadig
|
||||||
装上 libwdi WinUSB 驱动(oem244.inf),导致 OpenOCD 枚举不到探针;删除该驱动包后
|
装上 libwdi WinUSB 驱动(oem244.inf),导致 OpenOCD 枚举不到探针;删除该驱动包后
|
||||||
恢复 HID。若复发:管理员运行 `pnputil /delete-driver <oemNN.inf> /uninstall`。
|
恢复 HID。若复发:管理员运行 `pnputil /delete-driver <oemNN.inf> /uninstall`。
|
||||||
- 串口验证:RS485 转 USB(FTDI,COM15),115200-8N1,**需同时置位 RTS+DTR**
|
- 串口验证:RS485 转 USB(FTDI,COM15),115200-8N1,**需同时置位 RTS+DTR**
|
||||||
(转换器方向控制),否则命令发不出去;DAPLink 虚拟串口为 COM8。
|
(转换器方向控制),否则命令发不出去;DAPLink 虚拟串口为 COM8。
|
||||||
|
- 本地测试工具(git 不跟踪,在 `Toolchain/eddy_test/`):`eddy_cmd.py`(D5 帧命令行)、
|
||||||
|
`soc_ota.py`(PC 模拟 SOC 完整 OTA/Ymodem,支持 `--abort-after`/`--no-ca` 中止模式)、
|
||||||
|
`swd.py`(OpenOCD 封装:probe/image/boot/erasepage/read/dump)、`verify_pack.py`、
|
||||||
|
`verify_dump.py`(独立 CRC 复算与读回比对)。
|
||||||
|
|
||||||
## 硬件/板级约定(Inc/board_config.h)
|
## 硬件/板级约定(Inc/board_config.h)
|
||||||
|
|
||||||
@@ -35,46 +43,115 @@ GD32E230 电涡流传感器节点固件:LDC1612(电涡流/电感数字转换
|
|||||||
- 传感器:LDC1612 @0x2B(40MHz 外部时钟),TMP112 @0x49(ADD0=GND)
|
- 传感器:LDC1612 @0x2B(40MHz 外部时钟),TMP112 @0x49(ADD0=GND)
|
||||||
- 调试宏(均默认关闭):DEBUG_VERBOSE / COM_DEBUG / LDC_DEBUG / EDDY_DRIVE_CURRENT_DETECTION
|
- 调试宏(均默认关闭):DEBUG_VERBOSE / COM_DEBUG / LDC_DEBUG / EDDY_DRIVE_CURRENT_DETECTION
|
||||||
|
|
||||||
## 内存布局(仓库默认 = 正式发布布局)
|
## 双固件布局(2026-10-24 移植完成,与 CeramixNano-MB 完全相同的 Flash 分配)
|
||||||
|
|
||||||
- **APP 链接在 0x08002000(54K),VECT_TAB_OFFSET=0x2000(Src/system_gd32e23x.c),
|
```
|
||||||
前置 8KB Bootloader**;OTA/Ymodem 打包(_UPDATE.bin/_BL.bin)基于此布局,
|
0x08000000 ┌─────────────────┐
|
||||||
详见 hulk-mcu-firmware skill
|
│ Bootloader │ 8K 页 0~7 专属分支 bootloader_CeramixNano_EDDY(V0.1.0,4,040B)
|
||||||
- 调试阶段如需脱离 Bootloader 裸机运行(直接烧 0x08000000):临时把
|
0x08002000 ├─────────────────┤
|
||||||
`LD/gd32e23x_flash.ld` 的 ORIGIN 改为 0x08000000、VECT_TAB_OFFSET 改为 0x00,
|
│ FW_A 运行槽 │ 36K 页 8~43 OTA/Ymodem 接收区;锚点 A 记录@0x0800AFFC
|
||||||
**仅限本地临时修改,不要提交推送**(否则烧 _BL 镜像时 Bootloader 会覆盖 APP 头部)
|
0x0800B000 ├─────────────────┤
|
||||||
- 修改布局需同时改 `LD/gd32e23x_flash.ld` 的 ORIGIN 与 VECT_TAB_OFFSET
|
│ FW_B 出厂保底槽│ 19K 页 44~62 golden 线构建;B 记录@0x0800FBF8
|
||||||
|
0x0800FC00 ├─────────────────┤
|
||||||
|
│ 末页标志页 │ 1K 页 63 legacy 0xEEEE@0x0800FFFC
|
||||||
|
0x08010000 └─────────────────┘
|
||||||
|
```
|
||||||
|
|
||||||
|
- **BL = 并列板卡分支 `bootloader_CeramixNano_EDDY`**(BL 仓库,2026-10-24 自 MB 分支
|
||||||
|
创建;板卡宏 CERAMIXNANO_EDDY,**LED=PB1**,Ymodem 每成功烧录一包翻转一次,逻辑
|
||||||
|
同 MB 分支;版本独立编号 V0.1.0 = 4,040B,MD5 1dcc17bf…)。RS485 配置与 MB 逐宏
|
||||||
|
相同;**PA14 不被 BL 占用 ⇒ 升级会话期间 SWD 保持可用**(MB 分支 BL 的 LED=PA14
|
||||||
|
会占 SWCLK,勿混烧)。
|
||||||
|
- **A 线 = dev/main**:链接 0x08002000 / **LENGTH 36K**(LD 上限防溢出 B 槽),
|
||||||
|
VTOR 0x2000;版本从 V1.0.3 起(首个双固件 A 版本,码 26)。
|
||||||
|
- **B 线 = release/golden**:链接 0x0800B000 / LENGTH 0x4BF8(19K−8B,B 记录由打包
|
||||||
|
工具写入,链接区不含尾 8B——与 MB 的 .fw_tail 方案等价),VTOR 0xB000;
|
||||||
|
**版本冻结 V1.0.3 = 生产实装版原样复制(2026-10-25 起,见开发进度)**。保底判定值
|
||||||
|
= M999 返回 `v26.1.0.3` ⇔ 在跑 FW_B;`v26.1.0.2`(旧 golden)与 `v26.1.0.3` 均永不
|
||||||
|
再用于 OTA 升级包版本,dev 线后续 A 版本必须 > V1.0.3。此分支改动不得合回 dev/main。
|
||||||
|
- 打包:`hulk-mcu-firmware` skill / merge tool profile **`CeramixNano_EDDY_Dual_FW`**
|
||||||
|
(布局与 `CeramixNano_MB_Dual_FW` 逐字节相同),产出 `_UPDATE/_FULL/_FACTORY + MD5`。
|
||||||
|
- 升级链路:上位机(原 SOC 职责)—RS485—> BL(Ymodem);APP 用 `M9999` 软复位进
|
||||||
|
升级模式(SWRSTF=1 ⇒ BL 直留升级模式)。握手:BL 发 `ok` → `0x30` → BL `ok` →
|
||||||
|
`0x31` → 预擦 A+末页 → `'C'` 周期 → Ymodem(文件头 ACK 后 **ACK+'C' 连发**,
|
||||||
|
发送器须一次等齐)→ EOT → ACK×2 → 跳新固件。
|
||||||
|
- 完全体方案文档:`E:\Hulk-Coding\capsule_mb_camke_vscode\doc\双固件方案_完全体说明.md`。
|
||||||
|
|
||||||
## 开发进度
|
## 开发进度
|
||||||
|
|
||||||
### 已完成
|
### 已完成
|
||||||
|
|
||||||
- [x] LDC1612 驱动(初始化/单通道配置/原始值读取/状态检查)
|
- LDC1612 驱动(初始化/单通道配置/原始值读取/状态检查)
|
||||||
- [x] TMP112 驱动(初始化/温度读取/阈值/单次转换)
|
- TMP112 驱动(初始化/温度读取/阈值/单次转换)
|
||||||
- [x] RS485 命令体系:M1/M2(周期上报开关)、M3(涡流原始值)、M4(温度×10000)、
|
- RS485 命令体系:M1/M2(周期上报开关)、M3(涡流原始值)、M4(温度×10000)、
|
||||||
M5(温度补偿涡流值)、M6(标定数据:涡流+温度)、M999(版本)、M9999(复位)
|
M5(温度补偿涡流值)、M6(标定数据:涡流+温度)、M999(版本)、M9999(复位)
|
||||||
- [x] v1.0.1:涡流传感器温度标定方法与标定命令(M6)
|
- v1.0.1:涡流传感器温度标定方法与标定命令(M6)
|
||||||
- [x] 2026-10-02:从模板 gd32e23x_template_cmake_vscode 导入 IIC 驱动改进:
|
- 2026-10-02:IIC 驱动改进导入(总线恢复重试/超时辅助/read_raw/scan AERR 处理);
|
||||||
错误路径改为 i2c_bus_reset() 总线恢复+重试、_i2c_wait_flag_timeout 系列带超时
|
硬件全命令实测通过;DAPLink 烧录问题解决;发布 V1.0.2(Gitea Release + tag)
|
||||||
辅助函数、i2c_scan 增加 AERR 处理、新增 i2c_read_raw(无子地址读取)、
|
- **2026-10-24:双固件(出厂救援)方案移植完成并全链路实测通过**(详见下节)
|
||||||
i2c_write/i2c_read 改为 16 位寄存器地址(DLPC 风格,本项目传感器仍走
|
- BL 仓库新建并列板卡分支 `bootloader_CeramixNano_EDDY`(LED=PB1,V0.1.0 = 4,040B,
|
||||||
i2c_write_16bits/i2c_read_16bits 8 位寄存器路径);移除未使用的
|
每收包翻转一次;此前短暂采用"复用 MB bin"方案,因 LED 引脚不同已废弃并重打包)
|
||||||
i2c_read/write_8bits、_32bits、i2c_read_display_params 显示面板便捷函数;
|
- dev:LD 54K→36K、命名 CeramixNano-EDDY、板卡码 26、V1.0.3;
|
||||||
tmp112.c 读取改回 i2c_read_16bits
|
golden:V1.0.2 冻结命名 + variant=golden
|
||||||
- [x] 2026-10-02:硬件验证通过——全部命令实测:M3 涡流原始值 ~0x01ADBADC
|
- 两线 CMake 补齐 `--gap-fill=0xFF` 与 `LD LINK_DEPENDS`
|
||||||
(室温)/0x01B094BA(板子被手捂热后,随温度同步上移,无错误位)、
|
- fw_pack 新增 `CeramixNano_EDDY_Dual_FW` profile(55 项回归过,skill 缓存已同步)
|
||||||
M4 温度 27.25→30.45→29.95°C(跟踪板温变化合理)、M5 补偿值(系数为 0 时≈M3)、
|
- 产物(`Build/test/dualfw_2026-10-24/`):UPDATE(12 页,trailer 0x5C09000C)、
|
||||||
M1 周期上报(~10ms/帧)开启、M2 停止("ok" 确认)、M6 组合数据、
|
FULL(EDDY BL + A=V1.0.3/B=V1.0.2,19 项校验过)、FACTORY(12 项校验过)+ MD5 清单;
|
||||||
M999 版本 v23.1.0.1、M9999 复位(复位后 M999 恢复应答);
|
独立 python XMODEM 复算全部命中
|
||||||
所有响应 CRC 抽查校验正确;Release 预设编译通过(text 11.4KB)
|
- **硬件实测矩阵(SWD 探针 PC 采样 + 485 命令双重手段,全部 PASS;先用 MB bin 实测,
|
||||||
- [x] 2026-10-02:解决 DAPLink 烧录器识别问题(Zadig WinUSB 驱动冲突);
|
换 EDDY 专属 BL 后 FULL/FACTORY/OTA 全周期复测通过)**:
|
||||||
新增 DAPLink 专用烧录 cfg 与任务
|
FACTORY→跳B(v26.1.0.2) / FULL→记录路径跳A(v26.1.0.3) / PC 模拟 SOC 完整 OTA
|
||||||
- [x] 2026-10-02:发布 V1.0.2(Gitea Release + tag,基于 0x08002000 Bootloader
|
44s 成功+读回整片逐字节与包一致(B 区在真实 OTA 中毫发无损)/
|
||||||
布局打包 _UPDATE.bin/_BL.bin;dev 顶端另提交调试布局 0x08000000 供日常烧录)
|
擦锚点页→legacy 跳A / 锚点在位毁A体→CRC 拦截直落B(防降级)/
|
||||||
|
再毁B体→拒跳坏固件停留 BL(PC=0x080001B6) / OTA 第4包 CA 中止→落B /
|
||||||
|
非整页裸 bin 包→头部预检 CA×2 拒收→落B / M3/M4/M6 功能全通
|
||||||
|
|
||||||
|
- **2026-10-25:golden 托底线整体替换为生产实装版原样复制**(用户决策:生产版已在
|
||||||
|
机器上实装,托底必须与其同源,不做任何"顺手改进";IIC 新驱动等留给后续 A 线新版本):
|
||||||
|
- 来源:生产仓库(GitLab `ceramixnano_eddy_cmake_vscode`,dev@151828f,v26.1.0.3,
|
||||||
|
2026-03-23 已含驱动电流调参 GEN(18)→GEN(8));复制 8 文件(Src: command.c/i2c.c/
|
||||||
|
ldc1612.c/tmp112.c;Inc: command.h/i2c.h/ldc1612.h;CommunicationProtocol.md),
|
||||||
|
复制后 `fc /b` 逐字节核验一致
|
||||||
|
- golden 从此与 dev 线的功能差异(**属预期,勿"修复"**):LDC1612 驱动电流 **GEN(8)**
|
||||||
|
(dev 线仍为 GEN(18),后续 A 线新版本需评估回合生产调参);命令集 **M1~M4 +
|
||||||
|
M999/M9999(无 M5/M6)**;M4 温度为生产版无符号算法;i2c 为旧 8 位寄存器地址 API
|
||||||
|
驱动(与 tmp112.c/ldc1612.c 配套,整组生效)
|
||||||
|
- golden 保留项(B 槽专属构建):LD 0x0800B000/0x4BF8、VTOR 0xB000、gap-fill 0xFF、
|
||||||
|
BUILD_VARIANT=golden;仅 VERSION_PATCH 2→3 与生产对齐
|
||||||
|
- **保底判定值改为 `v26.1.0.3`**;注意 dev 线当前同为 V1.0.3(10-24 的 FULL 测试包
|
||||||
|
A=V1.0.3)——后续 A 线正式版本必须升到 V1.0.4 以上,否则 M999 无法区分保底
|
||||||
|
|
||||||
### 计划 / TODO
|
### 计划 / TODO
|
||||||
|
|
||||||
- [ ] 正式发布前切回 Bootloader 布局(0x08002000/54K)并适配 OTA/Ymodem 打包流程
|
- dev → main 合并待发布节奏(V1.0.3 为首个双固件 A 版本,main 仍是 54K 单槽布局)
|
||||||
- [ ] M5 温度补偿系数目前为 0(TEMP_COMP_COEFF_*),待标定后填入实际温漂系数
|
- M5 温度补偿系数目前为 0(TEMP_COMP_COEFF_*),待标定后填入实际温漂系数
|
||||||
- [ ] 模板侧(gd32e23x_template_cmake_vscode)的 SEGGER RTT / CFG_ENABLE 宏体系
|
- 上位机/SOC 侧需预存保底判定值 `v26.1.0.3`(跑 B ⇒ 升级未生效重推;2026-10-25 起判定值随生产版复制从 v26.1.0.2 改为 v26.1.0.3)
|
||||||
尚未同步到本项目(board_config.h 仍是 define/undef 风格),如需对齐另起任务
|
- (可选)模板侧 SEGGER RTT / CFG_ENABLE 宏体系同步
|
||||||
|
|
||||||
|
### 已删除的历史分支(2026-10-24 清理,均为开发过程产物)
|
||||||
|
|
||||||
|
- `dev_more_information`(远端):零独有提交,内容早已全部并入 main/dev。
|
||||||
|
- `dev_feature_iic_more_byte`(远端):仅 2 个 2025-08-25 的孤立实验提交,从未接入构建——
|
||||||
|
`75ea93c`(iic_new.c 975 行并行驱动实验,实际采用的是后来模板导入 i2c.c 的改进)、
|
||||||
|
`dbb6569`("dlpc3421 driver from kimi",DLPC3421 为 MB 光机芯片,与本板无关)。
|
||||||
|
如需找回:本地对象库在 gc 前仍可 `git show <hash>`。
|
||||||
|
|
||||||
|
### 教训(避免复发)
|
||||||
|
|
||||||
|
- **SWRSTF 残留**:M9999/调试器软复位置位的 SWRSTF **不随 NRST/SRST 清除**(仅 POR
|
||||||
|
或 RSTFC 清),此后任何复位 BL 都直进升级模式、不跳 APP——表现为"设备沉默,
|
||||||
|
M999 无响应,但 0x30 能回 ok"。排障/测试时须 SWD `mww 0x40021024 0x01000000`
|
||||||
|
清标志后再放跑(`swd.py boot` 已内置)。
|
||||||
|
- **(历史)误烧 MB 分支 BL 会让 SWD 在 0x30 后失联**:MB 分支 BL 的 LED=PA14
|
||||||
|
(SWCLK),0x30 应答后切输出 ⇒ SWD 死。**本板专用 `bootloader_CeramixNano_EDDY`
|
||||||
|
分支 LED=PB1,PA14 不占用,升级会话期间 SWD 全程可用**(2026-10-24 起所有镜像
|
||||||
|
均为 EDDY BL,此坑仅存档)。若真误烧:`M9999` 复位(有 APP 时)或用
|
||||||
|
connect_assert_srst 变体 cfg(`Toolchain/eddy_test/daplink_srst.cfg`)脉冲 SRST
|
||||||
|
复位恢复——SRST 线已验证接通,但该 DAPLink 下 examination 必失败属正常,
|
||||||
|
复位本身生效。
|
||||||
|
- **Ymodem 文件头 ACK 后是 ACK+'C' 连发**:发送器若分两次等会永远等不到第二个 'C'
|
||||||
|
(首次 OTA 调试即栽在此,BL 无超时会卡死;随后发 5×CA 可解)。
|
||||||
|
- OpenOCD `program`/`dump_image` 的文件路径必须转正斜杠,否则 Tcl 吃掉 `\l` `\t`。
|
||||||
|
- legacy 路径(锚点擦除态+flag 在)**没有 CRC 安全网**:实测毁 A 体后仍会跳进坏 A
|
||||||
|
导致 HardFault(xPSR=0x41000003)——这正是 v5 记录机制存在的意义,量产镜像必须
|
||||||
|
带锚点(FULL/OTA 正常路径均带)。
|
||||||
|
|||||||
+4
-1
@@ -62,6 +62,9 @@ target_link_options(${PROJECT_NAME} PRIVATE
|
|||||||
-T${CMAKE_SOURCE_DIR}/LD/gd32e23x_flash.ld -Xlinker
|
-T${CMAKE_SOURCE_DIR}/LD/gd32e23x_flash.ld -Xlinker
|
||||||
-L${CMAKE_SOURCE_DIR}/LD
|
-L${CMAKE_SOURCE_DIR}/LD
|
||||||
)
|
)
|
||||||
|
# LD 变更时触发重链接(改布局不重编也要重新生成 bin)
|
||||||
|
set_target_properties(${PROJECT_NAME} PROPERTIES LINK_DEPENDS
|
||||||
|
${CMAKE_SOURCE_DIR}/LD/gd32e23x_flash.ld)
|
||||||
|
|
||||||
# 生成 map 文件等
|
# 生成 map 文件等
|
||||||
target_link_options(${PROJECT_NAME} PRIVATE
|
target_link_options(${PROJECT_NAME} PRIVATE
|
||||||
@@ -80,7 +83,7 @@ add_custom_command(TARGET ${PROJECT_NAME}
|
|||||||
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 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 --gap-fill=0xFF $<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
|
||||||
COMMAND ${CMAKE_SIZE} --format=berkeley $<TARGET_FILE:${PROJECT_NAME}> > ${CMAKE_CURRENT_BINARY_DIR}/${OUTPUT_PREFIX}.bsz
|
COMMAND ${CMAKE_SIZE} --format=berkeley $<TARGET_FILE:${PROJECT_NAME}> > ${CMAKE_CURRENT_BINARY_DIR}/${OUTPUT_PREFIX}.bsz
|
||||||
|
|||||||
@@ -58,28 +58,5 @@
|
|||||||
|
|
||||||
### 4. 单次读取并发送板载温度传感器数据
|
### 4. 单次读取并发送板载温度传感器数据
|
||||||
|
|
||||||
- M4 指令 单次读取并发送板载温度传感器数据(TMP112,返回温度值×10000,4 字节有符号大端)
|
- M3 指令 单次读取并发送板载温度传感器数据
|
||||||
- `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`
|
|
||||||
|
|||||||
@@ -105,8 +105,4 @@ void eddy_current_report(void);
|
|||||||
|
|
||||||
void temperature_raw_value_report(void);
|
void temperature_raw_value_report(void);
|
||||||
|
|
||||||
void eddy_current_compensated_report(void);
|
|
||||||
|
|
||||||
void calibration_data_report(void);
|
|
||||||
|
|
||||||
#endif // COMMAND_H
|
#endif // COMMAND_H
|
||||||
|
|||||||
@@ -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 (8-bit register address + 2 data bytes)
|
\brief write 16-bit data to I2C device
|
||||||
\param[in] slave_addr: 7-bit slave address
|
\param[in] slave_addr: 7-bit slave address
|
||||||
\param[in] reg_addr: register address (8-bit)
|
\param[in] reg_addr: register address
|
||||||
\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 (8-bit register address + 2 data bytes)
|
\brief read 16-bit data from I2C device
|
||||||
\param[in] slave_addr: 7-bit slave address
|
\param[in] slave_addr: 7-bit slave address
|
||||||
\param[in] reg_addr: register address (8-bit)
|
\param[in] reg_addr: register address
|
||||||
\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,33 +111,70 @@ 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 (16-bit, high byte first)
|
\param[in] reg_addr: register address
|
||||||
\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, uint16_t reg_addr, uint8_t *data, uint8_t length);
|
i2c_result_t i2c_write(uint8_t slave_addr, uint8_t reg_addr, uint8_t *data, uint8_t length);
|
||||||
|
|
||||||
/*!
|
/*!
|
||||||
\brief read data from I2C device with configurable length
|
\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 (16-bit, high byte first)
|
\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-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, uint16_t reg_addr, uint8_t *data, uint8_t length);
|
i2c_result_t i2c_read(uint8_t slave_addr, uint8_t reg_addr, uint8_t *data, uint8_t length);
|
||||||
|
|
||||||
/* Pure read response without sub-address phase (for commands that already sent read request parameters) */
|
/* Convenience functions for common operations */
|
||||||
/*!
|
/*!
|
||||||
\brief read data from I2C device without register/sub-address phase
|
\brief write single byte to I2C device
|
||||||
\param[in] slave_addr: slave device address (7-bit)
|
\param[in] slave_addr: slave device address (7-bit)
|
||||||
\param[out] data: pointer to data buffer
|
\param[in] reg_addr: register address
|
||||||
\param[in] length: number of bytes to read (1-255)
|
\param[in] data: data byte to write
|
||||||
\retval i2c_result_t: operation result
|
\retval i2c_result_t: operation result
|
||||||
*/
|
*/
|
||||||
i2c_result_t i2c_read_raw(uint8_t slave_addr, uint8_t *data, uint8_t length);
|
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[in] length: number of bytes to read (1-13)
|
||||||
|
\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);
|
||||||
|
|
||||||
#ifdef DEBUG_VERBOSE
|
#ifdef DEBUG_VERBOSE
|
||||||
/*!
|
/*!
|
||||||
|
|||||||
+1
-1
@@ -445,7 +445,7 @@
|
|||||||
// 参数 idrive: 0-31之间的整数
|
// 参数 idrive: 0-31之间的整数
|
||||||
#define LDC1612_DRIVE_CURRENT_GEN(idrive) ( (uint16_t)(idrive) << 11 )
|
#define LDC1612_DRIVE_CURRENT_GEN(idrive) ( (uint16_t)(idrive) << 11 )
|
||||||
|
|
||||||
#define LDC1612_DRIVE_CURRENT LDC1612_DRIVE_CURRENT_GEN(18) // 0x9000
|
#define LDC1612_DRIVE_CURRENT LDC1612_DRIVE_CURRENT_GEN(8) // 0x9000
|
||||||
|
|
||||||
/**************************IDs (Read Only 0x7E 0x7F)***********************************/
|
/**************************IDs (Read Only 0x7E 0x7F)***********************************/
|
||||||
|
|
||||||
|
|||||||
@@ -10,9 +10,11 @@ _Min_Heap_Size = 0x200; /* required amount of heap */
|
|||||||
_Min_Stack_Size = 0x400; /* required amount of stack */
|
_Min_Stack_Size = 0x400; /* required amount of stack */
|
||||||
|
|
||||||
/* Memories definition */
|
/* Memories definition */
|
||||||
|
/* FW_B 保底槽布局:0x0800B000~0x0800FC00 共 19K,末 8B(0x0800FBF8 起)为 B 记录
|
||||||
|
* {magic16,pages16,crc16,2B 保留},由打包工具写入,链接区上限 = 槽 - 8B = 0x4BF8 */
|
||||||
MEMORY
|
MEMORY
|
||||||
{
|
{
|
||||||
FLASH (rx) : ORIGIN = 0x08002000, LENGTH = 54K
|
FLASH (rx) : ORIGIN = 0x0800B000, LENGTH = 0x4BF8
|
||||||
RAM (xrw) : ORIGIN = 0x20000000, LENGTH = 8K
|
RAM (xrw) : ORIGIN = 0x20000000, LENGTH = 8K
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -1,6 +1,6 @@
|
|||||||
# LDC1612 传感器固件
|
# CeramixNano-EDDY(LDC1612)传感器固件
|
||||||
|
|
||||||
本仓库基于 GD32E23x CMake + VSCode 工程模板(`gd32e23x_template_cmake_vscode`),包含 LDC1612 电感采样、TMP112 温度采集及补偿、RS485 命令处理。当前版本为 V1.0.1。固件采用 Bootloader + APP 布局:APP 链接在 `0x08002000`(54KB Flash / 8KB RAM),启动时自设向量表偏移;开发进度与审查资料见 `doc/` 目录。
|
本仓库基于 GD32E23x CMake + VSCode 工程模板(`gd32e23x_template_cmake_vscode`),包含 LDC1612 电感采样、TMP112 温度采集及补偿、RS485 命令处理。**版本按分支区分**:`release/golden` = 出厂保底线 **V1.0.3(冻结,生产实装版原样复制)**;`dev` = 双固件 A 线 **V1.0.3(开发中)**;`main` = 旧单槽布局线(LDC1612 旧名,V1.0.2)。固件采用双固件(出厂救援)布局:dev 线 APP 链接在 `0x08002000`(36KB A 槽 / 8KB RAM),golden 线链接 `0x0800B000`(B 槽),启动时自设向量表偏移;布局详情见下文「固件布局与 Bootloader」,开发进度见 `AGENTS.md`。
|
||||||
|
|
||||||
## 本地编译
|
## 本地编译
|
||||||
|
|
||||||
@@ -16,7 +16,7 @@ cmake --preset Release
|
|||||||
cmake --build --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)。
|
输出位于 `Build/Debug` 或 `Build/Release`:`Application/Application.elf` 用于调试,`CeramixNano-EDDY_<版本>_<BUILD_VARIANT>_<日期>.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” 重新配置。需要独立验证目录时可运行:
|
如果切换 ARM GCC 版本,请使用新的构建目录,或通过 CMake Tools 的 “Delete Cache and Reconfigure” 重新配置。需要独立验证目录时可运行:
|
||||||
|
|
||||||
@@ -41,23 +41,27 @@ cmake --build Build/local-check/Debug
|
|||||||
|
|
||||||
---
|
---
|
||||||
|
|
||||||
## 固件布局与 Bootloader
|
## 固件布局与 Bootloader(双固件,2026-10-24 起)
|
||||||
|
|
||||||
GD32E230F8(64KB Flash)按 Bootloader + APP 方式分区:
|
GD32E230F8(64KB Flash)采用与 CeramixNano-MB 相同的双固件(出厂救援)布局:
|
||||||
|
|
||||||
| 区域 | 地址范围 | 说明 |
|
| 区域 | 地址范围 | 说明 |
|
||||||
|---|---|---|
|
|---|---|---|
|
||||||
| Bootloader | `0x08000000` - `0x08001FFF`(8KB) | 随板提供,独立固件(`gd32e230f8_bootloader_hulk.bin`),负责 RS485 Ymodem OTA 与跳转 |
|
| Bootloader | `0x08000000` - `0x08001FFF`(8KB,页 0~7) | **专属分支 `bootloader_CeramixNano_EDDY`**(V0.1.0,4,040B,LED=PB1,Ymodem 每烧录一包翻转一次;与 MB 分支并列、版本独立编号)——启动判定链 + RS485 Ymodem OTA;PA14 不占用,升级会话期间 SWD 可用 |
|
||||||
| APP(本仓库) | `0x08002000` - `0x0800F7FF`(54KB) | 链接区 `FLASH ORIGIN=0x08002000, LENGTH=54K` |
|
| FW_A 运行槽(本仓库 dev/main) | `0x08002000` - `0x0800AFFF`(36KB,页 8~43) | 唯一 OTA/Ymodem 接收区;链接 `ORIGIN=0x08002000, LENGTH=36K`(上限防溢出 B 槽);槽尾锚点 A 记录 @`0x0800AFFC` |
|
||||||
| 温度标定页(预留) | `0x0800F800` - `0x0800FBFF` | 预留存放标定数据;当前 APP 不访问,仅靠 54KB 链接上限保证不越界覆盖 |
|
| FW_B 出厂保底槽(release/golden 分支) | `0x0800B000` - `0x0800FBFF`(19KB,页 44~62) | 出厂救援固件(版本冻结 V1.0.3,生产实装版原样复制),OTA 永不擦写;B 记录 @`0x0800FBF8` |
|
||||||
| 有效标志 | `0x0800FFFC` | Bootloader OTA 完成后自写 `0x0000EEEE`,跳转前校验 |
|
| 末页标志页 | `0x0800FC00` - `0x0800FFFF`(1KB,页 63) | legacy 标志 `0xEEEE` @`0x0800FFFC` |
|
||||||
|
|
||||||
工程侧的两个适配点(缺一不可):
|
启动判定:A 记录在位 ⇒ trailer 比对 + 固件体 CRC 复算 + 向量校验 ⇒ 跳 A(不过直落 B,防降级);锚点擦除态 ⇒ legacy flag ⇒ 跳 A;B 记录 CRC 复算通过 ⇒ 跳 B;否则留 BL 等 Ymodem。OTA 中途断电/中止 ⇒ 重启落 B 跑出厂固件,**设备不变砖**。
|
||||||
|
|
||||||
- `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`。
|
- `LD/gd32e23x_flash.ld`:FLASH `ORIGIN = 0x08002000, LENGTH = 36K`;
|
||||||
|
- `Src/system_gd32e23x.c`:`VECT_TAB_OFFSET = 0x2000`——Bootloader 不写 VTOR,APP 必须自己把向量表指到 `0x08002000`,否则中断会落回 Bootloader 向量表(现象:收到第一个 RS485 字节即死机);
|
||||||
|
- 出厂保底固件在 `release/golden` 分支构建(`ORIGIN=0x0800B000 / LENGTH=0x4BF8`、`VECT_TAB_OFFSET=0xB000`,版本冻结 V1.0.3 = **生产实装版原样复制**——命令集 M1~M4 + M999/M9999、LDC1612 驱动电流 `GEN(8)`、旧 8 位寄存器地址 i2c 驱动;判定值 `v26.1.0.3`);
|
||||||
|
- 打包(`_UPDATE/_FULL/_FACTORY` + MD5)用 merge tool / `hulk-mcu-firmware` skill,profile `CeramixNano_EDDY_Dual_FW`;产物命名 `CeramixNano-EDDY_*`(板卡码 26)。
|
||||||
|
|
||||||
|
⚠️ 与旧版 54K 单槽布局不兼容:需整片烧录一次(FACTORY/FULL 镜像)。RS485 协议、命令集(M1-M6/M999/M9999)与回包格式无任何变化。方案完全体文档见 MB 仓库 `doc/双固件方案_完全体说明.md`;开发约定与实测记录见本项目 `AGENTS.md`。
|
||||||
|
|
||||||
---
|
---
|
||||||
|
|
||||||
|
|||||||
+10
-92
@@ -74,15 +74,6 @@
|
|||||||
#define RESP_TYPE_LEN_ERR 0xF4 /**< 长度错误 */
|
#define RESP_TYPE_LEN_ERR 0xF4 /**< 长度错误 */
|
||||||
/** @} */
|
/** @} */
|
||||||
|
|
||||||
/* 温度补偿参数定义
|
|
||||||
* TEMP_COMP_REF_TEMP_X10000: 参考温度,单位 0.0001°C
|
|
||||||
* TEMP_COMP_COEFF_NUMERATOR / TEMP_COMP_COEFF_DENOMINATOR: 温漂系数,默认表示 counts/°C
|
|
||||||
* 补偿公式: compensated = raw - (temp - ref) * coeff
|
|
||||||
*/
|
|
||||||
#define TEMP_COMP_REF_TEMP_X10000 (250000L)
|
|
||||||
#define TEMP_COMP_COEFF_NUMERATOR (0L)
|
|
||||||
#define TEMP_COMP_COEFF_DENOMINATOR (10000L)
|
|
||||||
|
|
||||||
/* ============================================================================
|
/* ============================================================================
|
||||||
* 模块内部变量
|
* 模块内部变量
|
||||||
* ============================================================================ */
|
* ============================================================================ */
|
||||||
@@ -104,9 +95,6 @@ static const uint8_t s_report_status_ok[] = { 'o', 'k' }; /**< 成功响应
|
|||||||
static const uint8_t s_report_status_err[] = { 'e','r','r' }; /**< 错误响应数据 */
|
static const uint8_t s_report_status_err[] = { 'e','r','r' }; /**< 错误响应数据 */
|
||||||
/** @} */
|
/** @} */
|
||||||
|
|
||||||
static int32_t tmp112_get_temperature_x10000(void);
|
|
||||||
static int64_t calculate_temperature_compensation_offset(int32_t temperature_x10000);
|
|
||||||
|
|
||||||
/* ============================================================================
|
/* ============================================================================
|
||||||
* 公共接口函数
|
* 公共接口函数
|
||||||
* ============================================================================ */
|
* ============================================================================ */
|
||||||
@@ -344,14 +332,6 @@ void handle_command(const uint8_t *frame, uint8_t len) {
|
|||||||
temperature_raw_value_report();
|
temperature_raw_value_report();
|
||||||
return;
|
return;
|
||||||
|
|
||||||
case 5u:
|
|
||||||
eddy_current_compensated_report();
|
|
||||||
return;
|
|
||||||
|
|
||||||
case 6u:
|
|
||||||
calibration_data_report();
|
|
||||||
return;
|
|
||||||
|
|
||||||
// case 201u: // M201命令
|
// case 201u: // M201命令
|
||||||
// send_response(RESP_TYPE_OK, s_report_status_ok, sizeof(s_report_status_ok));
|
// send_response(RESP_TYPE_OK, s_report_status_ok, sizeof(s_report_status_ok));
|
||||||
// return;
|
// return;
|
||||||
@@ -533,82 +513,20 @@ void eddy_current_report(void) {
|
|||||||
send_response(RESP_TYPE_OK, sensor_data, sizeof(sensor_data));
|
send_response(RESP_TYPE_OK, sensor_data, sizeof(sensor_data));
|
||||||
}
|
}
|
||||||
|
|
||||||
static int32_t tmp112_get_temperature_x10000(void)
|
|
||||||
{
|
|
||||||
uint8_t value[2] = {0};
|
|
||||||
int16_t temp_raw;
|
|
||||||
|
|
||||||
tmp112a_get_raw_temperature_value(value);
|
|
||||||
|
|
||||||
temp_raw = (int16_t)(((uint16_t)value[0] << 8) | value[1]);
|
|
||||||
temp_raw >>= 4;
|
|
||||||
|
|
||||||
return (int32_t)temp_raw * 625;
|
|
||||||
}
|
|
||||||
|
|
||||||
static int64_t calculate_temperature_compensation_offset(int32_t temperature_x10000)
|
|
||||||
{
|
|
||||||
int32_t delta_temp_x10000 = temperature_x10000 - TEMP_COMP_REF_TEMP_X10000;
|
|
||||||
|
|
||||||
return ((int64_t)delta_temp_x10000 * TEMP_COMP_COEFF_NUMERATOR) /
|
|
||||||
TEMP_COMP_COEFF_DENOMINATOR;
|
|
||||||
}
|
|
||||||
|
|
||||||
void temperature_raw_value_report(void) {
|
void temperature_raw_value_report(void) {
|
||||||
|
// if (!g_temperature_sensor_report_enabled) return;
|
||||||
uint8_t raw_result[4];
|
uint8_t raw_result[4];
|
||||||
int32_t raw_value = tmp112_get_temperature_x10000();
|
uint8_t value[2] = {0};
|
||||||
|
uint32_t raw_value = 0;
|
||||||
|
|
||||||
raw_result[0] = (uint8_t)((uint32_t)raw_value >> 24);
|
// i2c_read_16bits(TMP112A_ADDR, TMP112A_TEMP_REG, value);
|
||||||
raw_result[1] = (uint8_t)((uint32_t)raw_value >> 16);
|
tmp112a_get_raw_temperature_value(value);
|
||||||
raw_result[2] = (uint8_t)((uint32_t)raw_value >> 8);
|
raw_value = (uint32_t)((uint16_t) (value[0] << 4) | (value[1]>>4)) * 625;
|
||||||
raw_result[3] = (uint8_t)((uint32_t)raw_value & 0xFF);
|
raw_result[0] = (uint8_t)(raw_value >> 24);
|
||||||
|
raw_result[1] = (uint8_t)(raw_value >> 16);
|
||||||
|
raw_result[2] = (uint8_t)(raw_value >> 8);
|
||||||
|
raw_result[3] = (uint8_t)(raw_value & 0xFF);
|
||||||
|
|
||||||
send_response(RESP_TYPE_OK, raw_result, sizeof(raw_result));
|
send_response(RESP_TYPE_OK, raw_result, sizeof(raw_result));
|
||||||
}
|
}
|
||||||
|
|
||||||
void eddy_current_compensated_report(void)
|
|
||||||
{
|
|
||||||
uint8_t sensor_data[4];
|
|
||||||
uint32_t raw_result = ldc1612_get_raw_channel_result(CHANNEL_0);
|
|
||||||
|
|
||||||
if ((raw_result & 0xF0000000u) == 0u) {
|
|
||||||
int32_t temperature_x10000 = tmp112_get_temperature_x10000();
|
|
||||||
int64_t compensated_value = (int64_t)raw_result -
|
|
||||||
calculate_temperature_compensation_offset(temperature_x10000);
|
|
||||||
|
|
||||||
if (compensated_value < 0) {
|
|
||||||
raw_result = 0;
|
|
||||||
} else if (compensated_value > 0x0FFFFFFFLL) {
|
|
||||||
raw_result = 0x0FFFFFFFu;
|
|
||||||
} else {
|
|
||||||
raw_result = (uint32_t)compensated_value;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
sensor_data[0] = (uint8_t)(raw_result >> 24);
|
|
||||||
sensor_data[1] = (uint8_t)(raw_result >> 16);
|
|
||||||
sensor_data[2] = (uint8_t)(raw_result >> 8);
|
|
||||||
sensor_data[3] = (uint8_t)(raw_result & 0xFF);
|
|
||||||
|
|
||||||
send_response(RESP_TYPE_OK, sensor_data, sizeof(sensor_data));
|
|
||||||
}
|
|
||||||
|
|
||||||
void calibration_data_report(void)
|
|
||||||
{
|
|
||||||
uint8_t payload[9];
|
|
||||||
uint32_t eddy_raw_value = ldc1612_get_raw_channel_result(CHANNEL_0);
|
|
||||||
int32_t temperature_raw_value = tmp112_get_temperature_x10000();
|
|
||||||
|
|
||||||
payload[0] = (uint8_t)(eddy_raw_value >> 24);
|
|
||||||
payload[1] = (uint8_t)(eddy_raw_value >> 16);
|
|
||||||
payload[2] = (uint8_t)(eddy_raw_value >> 8);
|
|
||||||
payload[3] = (uint8_t)(eddy_raw_value & 0xFF);
|
|
||||||
payload[4] = 0xFF;
|
|
||||||
payload[5] = (uint8_t)((uint32_t)temperature_raw_value >> 24);
|
|
||||||
payload[6] = (uint8_t)((uint32_t)temperature_raw_value >> 16);
|
|
||||||
payload[7] = (uint8_t)((uint32_t)temperature_raw_value >> 8);
|
|
||||||
payload[8] = (uint8_t)((uint32_t)temperature_raw_value & 0xFF);
|
|
||||||
|
|
||||||
send_response(RESP_TYPE_OK, payload, sizeof(payload));
|
|
||||||
}
|
|
||||||
|
|
||||||
@@ -34,7 +34,6 @@ 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 */
|
||||||
@@ -198,12 +197,10 @@ 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) && !i2c_flag_get(I2C0, I2C_FLAG_AERR) && (timeout < I2C_TIME_OUT))
|
while (!i2c_flag_get(I2C0, I2C_FLAG_ADDSEND) && (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";
|
||||||
@@ -224,9 +221,6 @@ 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);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// 生成停止条件
|
// 生成停止条件
|
||||||
@@ -269,53 +263,6 @@ 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;
|
||||||
@@ -378,7 +325,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_VERBOSE
|
#ifdef DEBUG_VERBOES
|
||||||
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;
|
||||||
@@ -386,28 +333,39 @@ 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 */
|
||||||
if(_i2c_wait_flag_timeout(I2C_FLAG_TBE) != I2C_RESULT_SUCCESS) {
|
while ((!i2c_flag_get(I2C0, I2C_FLAG_TBE)) && (timeout < I2C_TIME_OUT)) {
|
||||||
|
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 */
|
||||||
if(_i2c_wait_flag_timeout(I2C_FLAG_TBE) != I2C_RESULT_SUCCESS) {
|
while ((!i2c_flag_get(I2C0, I2C_FLAG_TBE)) && (timeout < I2C_TIME_OUT)) {
|
||||||
|
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 */
|
||||||
if(_i2c_wait_flag_timeout(I2C_FLAG_TBE) != I2C_RESULT_SUCCESS) {
|
while ((!i2c_flag_get(I2C0, I2C_FLAG_TBE)) && (timeout < I2C_TIME_OUT)) {
|
||||||
|
timeout++;
|
||||||
|
}
|
||||||
|
if (timeout >= I2C_TIME_OUT) {
|
||||||
state = I2C_STATE_ERROR;
|
state = I2C_STATE_ERROR;
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
@@ -423,9 +381,13 @@ 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 */
|
||||||
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;
|
||||||
}
|
}
|
||||||
@@ -451,13 +413,25 @@ 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:
|
||||||
/* I2C bus error, try to reset the bus and retry */
|
/* send a stop condition to I2C bus */
|
||||||
i2c_bus_reset();
|
i2c_stop_on_bus(I2C0);
|
||||||
|
|
||||||
|
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_VERBOSE
|
#ifdef DEBUG_VERBOES
|
||||||
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;
|
||||||
@@ -498,8 +472,11 @@ 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
|
||||||
if(_i2c_wait_flag_clear_timeout(I2C_FLAG_I2CBSY) != I2C_RESULT_SUCCESS) {
|
while (i2c_flag_get(I2C0, I2C_FLAG_I2CBSY) && (timeout < I2C_TIME_OUT)) {
|
||||||
|
timeout++;
|
||||||
|
}
|
||||||
|
if (timeout >= I2C_TIME_OUT) {
|
||||||
state = I2C_STATE_ERROR;
|
state = I2C_STATE_ERROR;
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
@@ -507,11 +484,15 @@ 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 */
|
||||||
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;
|
||||||
}
|
}
|
||||||
@@ -559,7 +540,10 @@ 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 */
|
||||||
if(_i2c_wait_flag_timeout(I2C_FLAG_TBE) != I2C_RESULT_SUCCESS) {
|
while ((!i2c_flag_get(I2C0, I2C_FLAG_TBE)) && (timeout < I2C_TIME_OUT)) {
|
||||||
|
timeout++;
|
||||||
|
}
|
||||||
|
if (timeout >= I2C_TIME_OUT) {
|
||||||
state = I2C_STATE_ERROR;
|
state = I2C_STATE_ERROR;
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
@@ -567,11 +551,15 @@ 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 */
|
||||||
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;
|
||||||
}
|
}
|
||||||
@@ -580,7 +568,11 @@ i2c_result_t i2c_read_16bits(uint8_t slave_addr, uint8_t reg_addr, uint8_t *data
|
|||||||
i2c_start_on_bus(I2C0);
|
i2c_start_on_bus(I2C0);
|
||||||
|
|
||||||
/* wait for repeated start condition to be sent */
|
/* wait for repeated start condition to be sent */
|
||||||
if(_i2c_wait_flag_timeout(I2C_FLAG_SBSEND) != I2C_RESULT_SUCCESS) {
|
timeout = 0;
|
||||||
|
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;
|
||||||
}
|
}
|
||||||
@@ -596,7 +588,10 @@ 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) */
|
||||||
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;
|
||||||
}
|
}
|
||||||
@@ -626,13 +621,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:
|
||||||
/* I2C bus error, try to reset the bus and retry */
|
/* send stop condition to release bus */
|
||||||
i2c_bus_reset();
|
i2c_stop_on_bus(I2C0);
|
||||||
|
|
||||||
retry_count++;
|
retry_count++;
|
||||||
if (retry_count >= I2C_MAX_RETRY) {
|
if (retry_count >= I2C_MAX_RETRY) {
|
||||||
#ifdef DEBUG_VERBOSE
|
#ifdef DEBUG_VERBOES
|
||||||
printf("IIC read failed after %d retries\n", I2C_MAX_RETRY);
|
printf("IIC read failed after %d retries\n", I2C_RETRY_MAX);
|
||||||
#endif
|
#endif
|
||||||
return I2C_RESULT_ERROR;
|
return I2C_RESULT_ERROR;
|
||||||
}
|
}
|
||||||
@@ -657,13 +652,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 (16-bit, high byte first)
|
\param[in] reg_addr: register address
|
||||||
\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, uint16_t reg_addr, uint8_t *data, uint8_t length) {
|
i2c_result_t i2c_write(uint8_t slave_addr, uint8_t reg_addr, uint8_t *data, uint8_t length) {
|
||||||
i2c_state_t state = I2C_STATE_START;
|
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;
|
||||||
@@ -671,9 +666,6 @@ i2c_result_t i2c_write(uint8_t slave_addr, uint16_t reg_addr, uint8_t *data, uin
|
|||||||
|
|
||||||
/* 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;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -727,42 +719,36 @@ i2c_result_t i2c_write(uint8_t slave_addr, uint16_t reg_addr, uint8_t *data, uin
|
|||||||
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("I2C 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;
|
||||||
}
|
}
|
||||||
|
|
||||||
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 */
|
||||||
if(_i2c_wait_flag_timeout(I2C_FLAG_TBE) != I2C_RESULT_SUCCESS) {
|
while ((!i2c_flag_get(I2C0, I2C_FLAG_TBE)) && (timeout < I2C_TIME_OUT)) {
|
||||||
|
timeout++;
|
||||||
|
}
|
||||||
|
if (timeout >= I2C_TIME_OUT) {
|
||||||
state = I2C_STATE_ERROR;
|
state = I2C_STATE_ERROR;
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
/* send register address, high byte first */
|
/* send register address */
|
||||||
if(_i2c_wait_flag_timeout(I2C_FLAG_TBE) != I2C_RESULT_SUCCESS) {
|
i2c_data_transmit(I2C0, reg_addr);
|
||||||
state = I2C_STATE_ERROR;
|
timeout = 0;
|
||||||
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 (data_index < length) {
|
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;
|
||||||
}
|
}
|
||||||
@@ -779,19 +765,21 @@ i2c_result_t i2c_write(uint8_t slave_addr, uint16_t reg_addr, uint8_t *data, uin
|
|||||||
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 */
|
||||||
if(_i2c_wait_flag_timeout(I2C_FLAG_BTC) != I2C_RESULT_SUCCESS) {
|
timeout = 0;
|
||||||
|
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:
|
||||||
@@ -811,8 +799,17 @@ i2c_result_t i2c_write(uint8_t slave_addr, uint16_t reg_addr, uint8_t *data, uin
|
|||||||
return I2C_RESULT_SUCCESS;
|
return I2C_RESULT_SUCCESS;
|
||||||
|
|
||||||
case I2C_STATE_ERROR:
|
case I2C_STATE_ERROR:
|
||||||
/* I2C bus error, try to reset the bus and retry */
|
/* send a stop condition to I2C bus */
|
||||||
i2c_bus_reset();
|
i2c_stop_on_bus(I2C0);
|
||||||
|
|
||||||
|
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) {
|
||||||
@@ -841,12 +838,12 @@ i2c_result_t i2c_write(uint8_t slave_addr, uint16_t reg_addr, uint8_t *data, uin
|
|||||||
/*!
|
/*!
|
||||||
\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 (16-bit, high byte first)
|
\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-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, uint16_t reg_addr, uint8_t *data, uint8_t length) {
|
i2c_result_t i2c_read(uint8_t slave_addr, uint8_t reg_addr, uint8_t *data, uint8_t length) {
|
||||||
i2c_state_t state = I2C_STATE_START;
|
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;
|
||||||
@@ -861,10 +858,6 @@ i2c_result_t i2c_read(uint8_t slave_addr, uint16_t reg_addr, uint8_t *data, uint
|
|||||||
/* 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:
|
||||||
@@ -872,7 +865,10 @@ i2c_result_t i2c_read(uint8_t slave_addr, uint16_t reg_addr, uint8_t *data, uint
|
|||||||
data_index = 0;
|
data_index = 0;
|
||||||
|
|
||||||
/* wait for bus to be idle */
|
/* wait for bus to be idle */
|
||||||
if(_i2c_wait_flag_clear_timeout(I2C_FLAG_I2CBSY) != I2C_RESULT_SUCCESS) {
|
while (i2c_flag_get(I2C0, I2C_FLAG_I2CBSY) && (timeout < I2C_TIME_OUT)) {
|
||||||
|
timeout++;
|
||||||
|
}
|
||||||
|
if (timeout >= I2C_TIME_OUT) {
|
||||||
state = I2C_STATE_ERROR;
|
state = I2C_STATE_ERROR;
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
@@ -880,11 +876,15 @@ i2c_result_t i2c_read(uint8_t slave_addr, uint16_t reg_addr, uint8_t *data, uint
|
|||||||
/* 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 */
|
||||||
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;
|
||||||
}
|
}
|
||||||
@@ -912,92 +912,67 @@ i2c_result_t i2c_read(uint8_t slave_addr, uint16_t reg_addr, uint8_t *data, uint
|
|||||||
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: handle ACK/ACKPOS based on requested length
|
/* read phase setup based on 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);
|
||||||
#ifdef DEBUG_VERBOSE
|
i2c_flag_clear(I2C0, I2C_FLAG_ADDSEND);
|
||||||
printf("I2C READ phase: length=1, disabling ACK before clearing ADDSEND\r\n");
|
/* send STOP immediately after clearing ADDR for single byte */
|
||||||
#endif
|
i2c_stop_on_bus(I2C0);
|
||||||
} 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);
|
||||||
#ifdef DEBUG_VERBOSE
|
|
||||||
printf("I2C READ phase: length=2, set ACKPOS_NEXT and disabling ACK before clearing ADDSEND\r\n");
|
|
||||||
#endif
|
|
||||||
} else {
|
|
||||||
/* length > 2: keep ACK enabled so slave will clock out data */
|
|
||||||
i2c_ack_config(I2C0, I2C_ACK_ENABLE);
|
|
||||||
#ifdef DEBUG_VERBOSE
|
|
||||||
printf("I2C READ phase: length=%u (>2), keeping ACK enabled\r\n", length);
|
|
||||||
#endif
|
|
||||||
}
|
|
||||||
|
|
||||||
/* now clear address flag to release SCL and enter data phase */
|
|
||||||
i2c_flag_clear(I2C0, I2C_FLAG_ADDSEND);
|
i2c_flag_clear(I2C0, I2C_FLAG_ADDSEND);
|
||||||
|
} else {
|
||||||
|
/* multi-byte read: clear ADDR with ACK enabled */
|
||||||
|
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 */
|
||||||
if(_i2c_wait_flag_timeout(I2C_FLAG_TBE) != I2C_RESULT_SUCCESS) {
|
while ((!i2c_flag_get(I2C0, I2C_FLAG_TBE)) && (timeout < I2C_TIME_OUT)) {
|
||||||
|
timeout++;
|
||||||
|
}
|
||||||
|
if (timeout >= I2C_TIME_OUT) {
|
||||||
state = I2C_STATE_ERROR;
|
state = I2C_STATE_ERROR;
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
/* send register address, high byte first */
|
/* send register address */
|
||||||
if(_i2c_wait_flag_timeout(I2C_FLAG_TBE) != I2C_RESULT_SUCCESS) {
|
i2c_data_transmit(I2C0, reg_addr);
|
||||||
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 */
|
||||||
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;
|
||||||
}
|
}
|
||||||
|
|
||||||
#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 */
|
||||||
if(_i2c_wait_flag_timeout(I2C_FLAG_SBSEND) != I2C_RESULT_SUCCESS) {
|
timeout = 0;
|
||||||
|
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;
|
||||||
}
|
}
|
||||||
@@ -1009,18 +984,15 @@ i2c_result_t i2c_read(uint8_t slave_addr, uint16_t reg_addr, uint8_t *data, uint
|
|||||||
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 */
|
||||||
if(_i2c_wait_flag_timeout(I2C_FLAG_RBNE) != I2C_RESULT_SUCCESS) {
|
while ((!i2c_flag_get(I2C0, I2C_FLAG_RBNE)) && (timeout < I2C_TIME_OUT)) {
|
||||||
|
timeout++;
|
||||||
|
}
|
||||||
|
if (timeout >= I2C_TIME_OUT) {
|
||||||
state = I2C_STATE_ERROR;
|
state = I2C_STATE_ERROR;
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
@@ -1028,7 +1000,10 @@ i2c_result_t i2c_read(uint8_t slave_addr, uint16_t reg_addr, uint8_t *data, uint
|
|||||||
state = I2C_STATE_STOP;
|
state = I2C_STATE_STOP;
|
||||||
} else if (length == 2) {
|
} else if (length == 2) {
|
||||||
/* two bytes read */
|
/* two bytes read */
|
||||||
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;
|
||||||
}
|
}
|
||||||
@@ -1041,39 +1016,40 @@ i2c_result_t i2c_read(uint8_t slave_addr, uint16_t reg_addr, uint8_t *data, uint
|
|||||||
} else {
|
} else {
|
||||||
/* multi-byte read (length > 2) */
|
/* multi-byte read (length > 2) */
|
||||||
while (data_index < length) {
|
while (data_index < length) {
|
||||||
if (data_index < length - 2) {
|
/* wait for RBNE (receive buffer not empty) */
|
||||||
/* normal bytes: wait for RBNE and read with ACK */
|
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;
|
||||||
}
|
}
|
||||||
data[data_index] = i2c_data_receive(I2C0);
|
|
||||||
data_index++;
|
/* special handling for last 3 bytes */
|
||||||
} else if (data_index == length - 2) {
|
if (data_index == length - 3) {
|
||||||
/* second to last byte: wait for BTF, then disable ACK and read */
|
/* wait for BTF (byte transfer finished) before reading N-2 */
|
||||||
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;
|
||||||
}
|
}
|
||||||
/* disable ACK before reading N-1 byte */
|
/* disable ACK for last 2 bytes */
|
||||||
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);
|
timeout = 0;
|
||||||
data_index++;
|
|
||||||
}
|
}
|
||||||
}
|
|
||||||
if(state == I2C_STATE_ERROR) break;
|
|
||||||
state = I2C_STATE_STOP;
|
state = I2C_STATE_STOP;
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
@@ -1096,11 +1072,8 @@ i2c_result_t i2c_read(uint8_t slave_addr, uint16_t reg_addr, uint8_t *data, uint
|
|||||||
return I2C_RESULT_SUCCESS;
|
return I2C_RESULT_SUCCESS;
|
||||||
|
|
||||||
case I2C_STATE_ERROR:
|
case I2C_STATE_ERROR:
|
||||||
/* I2C bus error, try to reset the bus and retry */
|
/* send stop condition to release bus */
|
||||||
#ifdef DEBUG_VERBOSE
|
i2c_stop_on_bus(I2C0);
|
||||||
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);
|
||||||
@@ -1109,13 +1082,7 @@ i2c_result_t i2c_read(uint8_t slave_addr, uint16_t reg_addr, uint8_t *data, uint
|
|||||||
retry_count++;
|
retry_count++;
|
||||||
if (retry_count >= I2C_MAX_RETRY) {
|
if (retry_count >= I2C_MAX_RETRY) {
|
||||||
#ifdef DEBUG_VERBOSE
|
#ifdef DEBUG_VERBOSE
|
||||||
/* Print diagnostic flags to help root-cause analysis */
|
printf("I2C read failed after %d retries\n", I2C_MAX_RETRY);
|
||||||
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;
|
||||||
}
|
}
|
||||||
@@ -1134,179 +1101,39 @@ i2c_result_t i2c_read(uint8_t slave_addr, uint16_t reg_addr, uint8_t *data, uint
|
|||||||
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 */
|
||||||
\brief read data from I2C device without register/sub-address phase
|
i2c_result_t i2c_write_8bits(uint8_t slave_addr, uint8_t reg_addr, uint8_t data) {
|
||||||
\param[in] slave_addr: slave device address (7-bit)
|
return i2c_write(slave_addr, reg_addr, &data, 1);
|
||||||
\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;
|
|
||||||
|
|
||||||
if (data == NULL || slave_addr > 0x7F || length == 0) {
|
i2c_result_t i2c_read_8bits(uint8_t slave_addr, uint8_t reg_addr, uint8_t *data) {
|
||||||
|
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_result_t value
|
\param[in] status: i2c_status_t value
|
||||||
\param[out] none
|
\param[out] none
|
||||||
\retval const char* status string
|
\retval const char* status string
|
||||||
*/
|
*/
|
||||||
|
|||||||
+8
-20
@@ -148,17 +148,11 @@ 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: 确保传感器处于睡眠模式 - 配置前必须 */
|
||||||
@@ -167,34 +161,28 @@ ldc1612_status_t ldc1612_config_single_channel(uint8_t channel) {
|
|||||||
delay_ms(10);
|
delay_ms(10);
|
||||||
|
|
||||||
/* Step 2: 配置频率分频 - 必须在其他配置之前 */
|
/* Step 2: 配置频率分频 - 必须在其他配置之前 */
|
||||||
status = ldc1612_write_register(SET_FREQ_REG_START + channel, freq_divider);
|
uint16_t freq_divider = ldc1612_calculate_freq_divider(channel);
|
||||||
if (status != I2C_RESULT_SUCCESS) return LDC1612_STATUS_ERROR;
|
ldc1612_write_register(SET_FREQ_REG_START + channel, freq_divider);
|
||||||
delay_ms(5);
|
delay_ms(5);
|
||||||
|
|
||||||
/* Step 3: 配置LC稳定时间 - 影响测量精度 */
|
/* Step 3: 配置LC稳定时间 - 影响测量精度 */
|
||||||
status = ldc1612_write_register(SET_SETTLECOUNT_REG_START + channel, LDC1612_SETTLECOUNT_CH0);
|
ldc1612_write_register(SET_SETTLECOUNT_REG_START + channel, LDC1612_SETTLECOUNT_CH0);
|
||||||
if (status != I2C_RESULT_SUCCESS) return LDC1612_STATUS_ERROR;
|
|
||||||
|
|
||||||
/* Step 4: 配置转换时间 - 影响测量速度和精度 */
|
/* Step 4: 配置转换时间 - 影响测量速度和精度 */
|
||||||
status = ldc1612_write_register(SET_CONVERSION_TIME_REG_START + channel, LDC1612_RCOUNT_TIME_CH0);
|
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: 配置转换偏移 */
|
||||||
status = ldc1612_write_register(SET_CONVERSION_OFFSET_REG_START + channel, SET_CONVERSION_OFFSET_CH0);
|
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: 配置驱动电流 - 影响传感器灵敏度 */
|
||||||
status = ldc1612_write_register(SET_DRIVER_CURRENT_REG + channel, LDC1612_DRIVE_CURRENT);
|
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);
|
||||||
status = ldc1612_write_register(MUX_CONFIG_REG, LDC1612_MUX_CONFIG);
|
ldc1612_write_register(MUX_CONFIG_REG, LDC1612_MUX_CONFIG);
|
||||||
if (status != I2C_RESULT_SUCCESS) return LDC1612_STATUS_ERROR;
|
|
||||||
|
|
||||||
/* Step 8: 配置错误输出 */
|
/* Step 8: 配置错误输出 */
|
||||||
status = ldc1612_write_register(ERROR_CONFIG_REG, LDC1612_ERROR_CONFIG_DEFAULT);
|
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);
|
||||||
|
|||||||
@@ -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)0x2000 /* vector table base offset */
|
#define VECT_TAB_OFFSET (uint32_t)0xB000 /* vector table base offset, FW_B slot 0x0800B000 */
|
||||||
|
|
||||||
/* 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)
|
||||||
|
|||||||
+2
-1
@@ -86,7 +86,8 @@ 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,9 +1,12 @@
|
|||||||
# Project basic info
|
# Project basic info
|
||||||
set(PROJECT_NAME "LDC1612")
|
# golden 出厂保底线:生产实装版原样复制(GitLab ceramixnano_eddy_cmake_vscode dev@151828f,v26.1.0.3,2026-10-25)
|
||||||
set(BOARD_TYPE_CODE 23)
|
# 保底判定值 = M999 返回 v26.1.0.3 ⇔ 在跑 FW_B;v26.1.0.2(旧 golden)与 v26.1.0.3 均永不再用于 OTA 升级包版本
|
||||||
|
# 此分支改动不得合回 dev/main(链接基址 0x0800B000 为 B 槽专属)
|
||||||
|
set(PROJECT_NAME "CeramixNano-EDDY")
|
||||||
|
set(BOARD_TYPE_CODE 26)
|
||||||
set(VERSION_MAJOR 1)
|
set(VERSION_MAJOR 1)
|
||||||
set(VERSION_MINOR 0)
|
set(VERSION_MINOR 0)
|
||||||
set(VERSION_PATCH 2)
|
set(VERSION_PATCH 3)
|
||||||
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")
|
||||||
|
|
||||||
@@ -11,7 +14,7 @@ string(TIMESTAMP BUILD_DATE "%Y-%m-%d")
|
|||||||
# Example: HW-IIC_APP / HW-IIC_Bootloader
|
# Example: HW-IIC_APP / HW-IIC_Bootloader
|
||||||
# set(BUILD_VARIANT "AutoDetectDriveCurrent")
|
# set(BUILD_VARIANT "AutoDetectDriveCurrent")
|
||||||
# set(BUILD_VARIANT "HW-IIC")
|
# set(BUILD_VARIANT "HW-IIC")
|
||||||
set(BUILD_VARIANT "APP")
|
set(BUILD_VARIANT "golden")
|
||||||
|
|
||||||
# 其它自定义宏
|
# 其它自定义宏
|
||||||
add_definitions(-DBUILD_VARIANT=${BUILD_VARIANT})
|
add_definitions(-DBUILD_VARIANT=${BUILD_VARIANT})
|
||||||
|
|||||||
Reference in New Issue
Block a user