From 7eeb76dbdf4038cda70817a1852b7737cdad36bc Mon Sep 17 00:00:00 2001 From: cjiio Date: Wed, 2 Sep 2026 13:07:26 +0800 Subject: [PATCH] feat(battery): load custom CW2017 profile --- components/bsp/include/bsp_battery.h | 2 +- components/bsp/src/bsp_battery.c | 138 +++++++++++++++++- docs/CHANGELOG.md | 2 + docs/CHANGELOG.zh_CN.md | 2 + .../AI_HARDWARE_DEVELOPMENT_GUIDE.md | 2 +- .../AI_HARDWARE_DEVELOPMENT_GUIDE.zh_CN.md | 2 +- 6 files changed, 140 insertions(+), 8 deletions(-) diff --git a/components/bsp/include/bsp_battery.h b/components/bsp/include/bsp_battery.h index cebd1d9..2a48ae7 100644 --- a/components/bsp/include/bsp_battery.h +++ b/components/bsp/include/bsp_battery.h @@ -1,6 +1,6 @@ // components/bsp/include/bsp_battery.h // CellWise CW2017 电量计:I2C 0x63,与 ES8311 共用总线。 -// 芯片自带 Li-Poly profile,直接给 SOC%,无需外部分压电阻与查表。 +// BSP 写入自定义电池 profile,芯片直接给 SOC%,无需外部分压电阻与查表。 #pragma once #include "esp_err.h" diff --git a/components/bsp/src/bsp_battery.c b/components/bsp/src/bsp_battery.c index 4feac2b..b7138f1 100644 --- a/components/bsp/src/bsp_battery.c +++ b/components/bsp/src/bsp_battery.c @@ -1,12 +1,12 @@ // components/bsp/src/bsp_battery.c -// 移植自 trae_card/components/platform/platform_esp32/src/battery_cw2017.c -// (去掉了电池 profile 写入部分:开源硬件用户电池各异,用芯片自带 Li-Poly profile 更通用) +// CW2017 电量计驱动。 #include "bsp_battery.h" #include "bsp_i2c.h" #include "bsp_pins.h" #include "esp_log.h" #include "freertos/FreeRTOS.h" #include "freertos/task.h" +#include static const char *TAG = "bsp_batt"; @@ -14,6 +14,30 @@ static const char *TAG = "bsp_batt"; #define CW_REG_VCELL_H 0x02 // 14bit 电压,V(uV) = raw * 312.5 #define CW_REG_SOC_H 0x04 // 高字节 = 整数百分比;低字节(0x05)= 1/256 % #define CW_REG_CONFIG 0x08 // 0xF0=睡眠 / 0x30=复位态 / 0x00=正常 +#define CW_REG_SOC_ALERT 0x0B // bit7=profile UPDATE_FLAG;bit6:0=SOC 告警阈值 +#define CW_REG_PROFILE 0x10 // 80 字节电池 profile 起始地址 + +#define CW_CONFIG_ACTIVE 0x00 +#define CW_CONFIG_RESTART 0x30 +#define CW_CONFIG_SLEEP 0xF0 +#define CW_UPDATE_FLAG 0x80 +#define CW_PROFILE_SIZE 80 + +static const uint8_t s_battery_profile[CW_PROFILE_SIZE] = { + 0x64, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0xAD, 0xC7, 0xC8, 0xCA, 0xBD, 0xB1, 0xC1, 0x94, + 0x88, 0xD1, 0xBD, 0x97, 0x88, 0x66, 0x56, 0x4A, + 0x3F, 0x33, 0x26, 0x5C, 0x37, 0xD1, 0x27, 0xD8, + 0xCC, 0xB7, 0xCF, 0xB3, 0xB2, 0xAE, 0xA6, 0x9E, + 0x99, 0x97, 0x9B, 0x86, 0x47, 0x1E, 0x17, 0x26, + 0x49, 0x96, 0xD9, 0xE1, 0xDD, 0xDC, 0xD4, 0x59, + 0x00, 0x00, 0x90, 0x02, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x64, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x5C, +}; + +_Static_assert(sizeof(s_battery_profile) == CW_PROFILE_SIZE, + "CW2017 battery profile must contain exactly 80 bytes"); static i2c_master_dev_handle_t s_dev; @@ -28,6 +52,76 @@ static int cw_write(uint8_t reg, uint8_t val) { return i2c_master_transmit(s_dev, b, 2, 100) == ESP_OK ? 0 : -1; } +// CONFIG 的低 4 bit 为保留位。复位、睡眠和激活均按芯片规定的时序切换。 +static int cw_enter_sleep(void) { + if (cw_write(CW_REG_CONFIG, CW_CONFIG_RESTART) != 0) return -1; + vTaskDelay(pdMS_TO_TICKS(20)); + if (cw_write(CW_REG_CONFIG, CW_CONFIG_SLEEP) != 0) return -1; + vTaskDelay(pdMS_TO_TICKS(10)); + return 0; +} + +static int cw_enter_active(void) { + if (cw_write(CW_REG_CONFIG, CW_CONFIG_RESTART) != 0) return -1; + vTaskDelay(pdMS_TO_TICKS(20)); + if (cw_write(CW_REG_CONFIG, CW_CONFIG_ACTIVE) != 0) return -1; + vTaskDelay(pdMS_TO_TICKS(10)); + return 0; +} + +// 同时检查 UPDATE_FLAG 和 80 字节内容,避免只凭标志误用旧电芯参数。 +static int cw_profile_matches(bool *matches) { + uint8_t val = 0; + *matches = false; + + if (cw_read(CW_REG_SOC_ALERT, &val, 1) != 0) return -1; + if ((val & CW_UPDATE_FLAG) == 0) return 0; + + for (size_t i = 0; i < CW_PROFILE_SIZE; i++) { + if (cw_read((uint8_t)(CW_REG_PROFILE + i), &val, 1) != 0) return -1; + if (val != s_battery_profile[i]) return 0; + } + + *matches = true; + return 0; +} + +// Profile 必须在睡眠态逐字节写入,读回校验后置 UPDATE_FLAG,再重启计算。 +static int cw_update_profile(void) { + uint8_t val = 0; + if (cw_enter_sleep() != 0) return -1; + + for (size_t i = 0; i < CW_PROFILE_SIZE; i++) { + if (cw_write((uint8_t)(CW_REG_PROFILE + i), s_battery_profile[i]) != 0) { + ESP_LOGE(TAG, "写 profile 失败:index=%u", (unsigned)i); + return -1; + } + } + + for (size_t i = 0; i < CW_PROFILE_SIZE; i++) { + if (cw_read((uint8_t)(CW_REG_PROFILE + i), &val, 1) != 0) return -1; + if (val != s_battery_profile[i]) { + ESP_LOGE(TAG, "profile 校验失败:index=%u expected=0x%02X actual=0x%02X", + (unsigned)i, s_battery_profile[i], val); + return -1; + } + } + + if (cw_read(CW_REG_SOC_ALERT, &val, 1) != 0) return -1; + if (cw_write(CW_REG_SOC_ALERT, val | CW_UPDATE_FLAG) != 0) return -1; + return cw_enter_active(); +} + +// 首次计算期间 SOC 可能暂时大于 100;最多等待 5 秒再判定初始化失败。 +static int cw_wait_soc_ready(void) { + for (int retry = 0; retry < 50; retry++) { + uint8_t soc = 0; + vTaskDelay(pdMS_TO_TICKS(100)); + if (cw_read(CW_REG_SOC_H, &soc, 1) == 0 && soc <= 100) return 0; + } + return -1; +} + esp_err_t bsp_battery_init(void) { if (s_dev) return ESP_OK; @@ -52,11 +146,45 @@ esp_err_t bsp_battery_init(void) { } ESP_LOGI(TAG, "检测到 CW2017 VERSION=0x%02X", ver); - // 确保处于正常工作模式(非睡眠/复位态)。用芯片自带 Li-Poly profile,不写自定义 profile。 - cw_write(CW_REG_CONFIG, 0x00); - vTaskDelay(pdMS_TO_TICKS(100)); // 等首次 SOC 计算完成 + bool profile_matches = false; + if (cw_profile_matches(&profile_matches) != 0) { + ESP_LOGE(TAG, "读取电池 profile 失败"); + e = ESP_FAIL; + goto fail; + } + + if (!profile_matches) { + ESP_LOGI(TAG, "写入优特利 520mAh 自定义 profile"); + if (cw_update_profile() != 0) { + ESP_LOGE(TAG, "自定义 profile 写入失败"); + e = ESP_FAIL; + goto fail; + } + } else { + uint8_t config = 0; + if (cw_read(CW_REG_CONFIG, &config, 1) != 0) { + e = ESP_FAIL; + goto fail; + } + if (config != CW_CONFIG_ACTIVE && cw_enter_active() != 0) { + e = ESP_FAIL; + goto fail; + } + ESP_LOGI(TAG, "优特利 520mAh profile 已匹配"); + } + + if (cw_wait_soc_ready() != 0) { + ESP_LOGE(TAG, "等待首次 SOC 计算超时"); + e = ESP_ERR_TIMEOUT; + goto fail; + } return ESP_OK; + +fail: + i2c_master_bus_rm_device(s_dev); + s_dev = NULL; + return e; } int bsp_battery_soc(void) { diff --git a/docs/CHANGELOG.md b/docs/CHANGELOG.md index d42f77c..f9ca474 100644 --- a/docs/CHANGELOG.md +++ b/docs/CHANGELOG.md @@ -6,6 +6,8 @@ ## Unreleased +- Added the supplied 80-byte CW2017 profile for the specified 520 mAh cell, including content/update-flag checks, verified writes, the required restart sequence, and bounded SOC-readiness polling. + - Reorganized the documentation by function area with a dual entry point: the root `AGENTS.md` is now a thin router (hard constraints + task routing only) and the detailed AI workflow lives in `docs/development/ai-guide.md`; `agent-guide.md` was folded in. `docs/development/` gained a second level (`engineering/`, `ci/`, `release/`), and the `plays/` application archive and `experiences/` moved into a `docs/reference/` area with a dedicated README. Removed `docs/software-design/` (empty scaffold); folded the three `assets/{fonts,images,music}/README` leaves into the `assets/` README; flattened the six `project-completion` sub-documents into a single file; and unified each directory to a single README, eliminating every `INDEX` file and a duplicated experience index. All cross-references and bibliographic links were updated; no content was dropped. - Made mini-program BLE install compatibility a template-level invariant: fixed diff --git a/docs/CHANGELOG.zh_CN.md b/docs/CHANGELOG.zh_CN.md index 836c0f6..aabe11d 100644 --- a/docs/CHANGELOG.zh_CN.md +++ b/docs/CHANGELOG.zh_CN.md @@ -6,6 +6,8 @@ ## Unreleased +- 加入厂家为优特利 520mAh 电芯生成的 80 字节 CW2017 profile,并实现内容与更新标志检查、写入后校验、规定的重启时序以及有上限的 SOC 就绪等待。 + - 按功能域整理文档并采用双入口:根目录 `AGENTS.md` 变为薄路由(只保留硬约束与任务路由),详细的 AI 开发工作流下沉到 `docs/development/ai-guide.md`,`agent-guide.md` 并入其中。为 `docs/development/` 增加二级分区(`engineering/`、`ci/`、`release/`),把 `plays/` 应用档案与 `experiences/` 移入带专属 README 的 `docs/reference/` 参考区;删除 `docs/software-design/`(空脚手架);把 `assets/{fonts,images,music}/README` 三个叶子 README 并入 `assets/` README;把 `project-completion` 的六个子文档压平为单文件;并把每个目录统一为单一 README,消除所有 `INDEX` 文件与一处重复经验索引。所有交叉引用与文献链接已更新;未丢弃任何内容。 - 将小程序 BLE 安装兼容提升为二创模板强制契约:固定保护 `cardid`/Recovery 分区, diff --git a/docs/hardware-design/AI_HARDWARE_DEVELOPMENT_GUIDE.md b/docs/hardware-design/AI_HARDWARE_DEVELOPMENT_GUIDE.md index 183903e..57ce773 100644 --- a/docs/hardware-design/AI_HARDWARE_DEVELOPMENT_GUIDE.md +++ b/docs/hardware-design/AI_HARDWARE_DEVELOPMENT_GUIDE.md @@ -156,7 +156,7 @@ The audio demo's three-second recording buffer is about 96 KB and is the largest ## 9. CW2017 fuel gauge -Initialization reads VERSION, writes CONFIG `0x00`, waits 100 ms, and uses the chip's built-in Li-Poly profile. The repository intentionally does not write a custom cell profile. +Initialization reads VERSION and checks the profile update flag plus all 80 profile bytes. When needed, it puts the gauge to sleep, writes and verifies the supplied profile for the specified 520 mAh cell, sets the update flag, restarts the gauge with the required `0x30` to `0x00` sequence, and waits up to five seconds for a valid SOC. Replacing the cell requires a matching vendor-generated profile and renewed charge/discharge validation. - SOC uses registers `0x04–0x05`; values above 100 are treated as not ready and return `-1`. - Voltage uses the 14-bit value at `0x02–0x03`, converted as `raw × 312.5 µV`, and returned in mV. diff --git a/docs/hardware-design/AI_HARDWARE_DEVELOPMENT_GUIDE.zh_CN.md b/docs/hardware-design/AI_HARDWARE_DEVELOPMENT_GUIDE.zh_CN.md index 09eeef3..be8bc7d 100644 --- a/docs/hardware-design/AI_HARDWARE_DEVELOPMENT_GUIDE.zh_CN.md +++ b/docs/hardware-design/AI_HARDWARE_DEVELOPMENT_GUIDE.zh_CN.md @@ -215,7 +215,7 @@ Audio demo 使用独立 4 KB 栈任务:OK 播放 1 秒 1 kHz 方波,UP 录 3 ## 9. CW2017 电池计 -CW2017 在共享 I2C 地址 0x63。初始化读取 VERSION 确认在线,将 CONFIG 写为 0x00 进入正常模式,等待 100 ms 后使用芯片自带 Li-Poly profile。仓库刻意不写自定义电池 profile,因为开源用户的电池可能不同。 +CW2017 在共享 I2C 地址 0x63。初始化读取 VERSION 确认在线,并检查 profile 更新标志以及全部 80 字节内容。需要更新时,驱动让电量计进入睡眠态,写入并读回校验优特利 520mAh 电芯的厂家 profile,置更新标志,再按 `0x30` 到 `0x00` 的规定时序重启,最长等待 5 秒取得有效 SOC。更换电芯时必须取得匹配的厂家 profile,并重新完成充放电验证。 - SOC:读 0x04–0x05,仅返回高字节整数百分比;大于 100 视为未就绪并返回 `-1`。 - 电压:读 0x02–0x03 的 14 bit 值,换算为 `raw × 312.5 µV`,API 返回 mV。