文件
phonerail/firmware/main/slot.c
T
zhou yang和Claude Opus 5.5 7bf30e3c38 固件 0.1.0-c3:计划内重启不断电(OTA / 回滚锁住 EFUSE_EN,新固件接管;SR-6 改写)
- warm_esp.{h,c}:warm_restart() 记下开着的路(RTC_NOINIT,魔数 + 掩码 + 反码)→ gpio_hold 锁五根 EN → 重启;
  目标镜像没有能力标记(第 288 字节 PRCAPS1,.rodata_custom_desc)就不锁 —— 老固件写不动锁住的 EN。
  warm_take():软件复位 + 标记对得上才接管,一次性;再读 EN 真电平核对
- bringup:adopt 掩码;rail_en_init_keep() 接管的路写高、五根一律放开 hold(崩溃时旧锁可能还在);
  刀片不在 / mux 失联的接管路立刻关(SLOT_EMPTY arg 1);第二段已开着的路不重开、不等 PG
- pca9555_init_keep():方向寄存器是本固件写的才留 VBUS(掉过电的片子锁存器是 0xFF),键 / EDL 清零
- HAL:hal_gpio_hold();ESP 输出脚配 INPUT_OUTPUT(读得到真电平);sim 给 hold 建模
- 事件 LOG_EV_OTA arg 7 = 接管;兜底定时器回滚故意不走接管
- 顶层 CMake:phonerail.h 进 CMAKE_CONFIGURE_DEPENDS(否则镜像头版本停在旧值);-u g_phonerail_caps 防 gc
- test_bringup 加 6 例(接管 / 非计划复位全关 / 空槽 / mux 失联 / PCA 掉过电 / hold 冻结),全工程 448 例全绿
- rack:固件库识别能力标记,推送对话框按「支持 / 不支持重启不断电」给不同提示(已部署)
- 需求文档:SR-6 改为「非计划复位全关」+ 计划内重启例外;§10.9 新增「重启不断电」
- 板 B:台架镜像(空槽强制 CH1 在位,未提交)开 CH1 → OTA 带重启 → 事件 18/7 mv=1,CH1 一直开着;
  现跑 0.1.0-c3。未测:示波器看重启期间 EN / 输出毛刺(要插刀片)

Co-Authored-By: Claude Opus 5.5 <[email protected]>
2026-09-27 14:35:52 +08:00

159 行
4.3 KiB
C
原始文件 Blame 文件历史

此文件含有模棱两可的 Unicode 字符
此文件含有可能会与其他字符混淆的 Unicode 字符。 如果您是想特意这样的,可以安全地忽略该警告。 使用 Escape 按钮显示他们。
/* 一片刀片的 I/O 门面。契约见 slot.h。 */
#include "slot.h"
#include <assert.h>
#include <stddef.h>
#include "hal_gpio.h"
#include "i2cbus.h"
#include "rail_cfg.h"
/* `PRSNT#` 的缓存(见 slot.h:读失败保留上一次的值)。 */
static bool s_present[PHONERAIL_RAIL_COUNT] = { true, true, true, true, true };
static bool rail_valid(int rail) { return rail >= 1 && rail <= PHONERAIL_RAIL_COUNT; }
int slot_mux_ch(int rail)
{
assert(rail_valid(rail));
return (int)rail_cfg(rail)->mux_ch;
}
/* ---- PRSNT# -------------------------------------------------------------- */
hal_err_t slot_prsnt_init(void)
{
for (int rail = 1; rail <= PHONERAIL_RAIL_COUNT; rail++) {
const hal_err_t err =
hal_gpio_config(rail_cfg(rail)->prsnt_n, HAL_GPIO_INPUT_PULLUP);
if (err != HAL_OK) {
return err;
}
}
slot_prsnt_poll();
return HAL_OK;
}
void slot_prsnt_poll(void)
{
for (int rail = 1; rail <= PHONERAIL_RAIL_COUNT; rail++) {
int level = 0;
if (hal_gpio_read(rail_cfg(rail)->prsnt_n, &level) == HAL_OK) {
s_present[rail - 1] = (level == 0); /* 低 = 在位 */
}
/* 读失败:保留上一次的值(slot.h 里的理由)。 */
}
}
bool slot_present(int rail)
{
return rail_valid(rail) && s_present[rail - 1];
}
/* ---- 一次 xact 的正文们 --------------------------------------------------
每个都是「通道已选好、锁已持有」的状态下被调一次。 */
typedef struct {
int rail;
uint8_t mask;
uint8_t value;
} out0_arg_t;
static hal_err_t do_out0(void *arg)
{
const out0_arg_t *a = (const out0_arg_t *)arg;
return pca9555_write_out0(a->rail - 1, a->mask, a->value);
}
static hal_err_t do_init(void *arg)
{
const int rail = *(const int *)arg;
return pca9555_init(rail - 1);
}
static hal_err_t do_init_keep_vbus(void *arg)
{
const int rail = *(const int *)arg;
return pca9555_init_keep(rail - 1, (uint8_t)(1u << PCA9555_BIT_VBUS_EN));
}
/* ---- 输出 ---------------------------------------------------------------- */
/* 已持锁版本:影子下标与通道由调用方保证一致 —— 调用方只可能是
`t_safety`,而它是拿 `slot_mux_ch(rail)` 去开的 `xact`。 */
static hal_err_t out0_locked(int rail, uint8_t mask, uint8_t value)
{
if (!rail_valid(rail)) {
return HAL_ERR_ARG;
}
return pca9555_write_out0(rail - 1, mask, value);
}
static hal_err_t out0_xact(int rail, uint8_t mask, uint8_t value)
{
if (!rail_valid(rail)) {
return HAL_ERR_ARG;
}
out0_arg_t a = { rail, mask, value };
return i2cbus_xact(slot_mux_ch(rail), SLOT_LOCK_TIMEOUT_MS, do_out0, &a);
}
hal_err_t slot_init(int rail)
{
if (!rail_valid(rail)) {
return HAL_ERR_ARG;
}
int r = rail;
return i2cbus_xact(slot_mux_ch(rail), SLOT_LOCK_TIMEOUT_MS, do_init, &r);
}
hal_err_t slot_init_keep_vbus(int rail)
{
if (!rail_valid(rail)) {
return HAL_ERR_ARG;
}
int r = rail;
return i2cbus_xact(slot_mux_ch(rail), SLOT_LOCK_TIMEOUT_MS, do_init_keep_vbus, &r);
}
hal_err_t slot_out0_write(int rail, uint8_t mask, uint8_t value)
{
return out0_xact(rail, mask, value);
}
uint8_t slot_shadow(int rail)
{
assert(rail_valid(rail));
return pca9555_shadow(rail - 1);
}
/* VBUS 的极性:**高 = 通**(TPS22918 的 ON 脚,板上 R701 100 kΩ 下拉 ⇒
PCA9555 复位为高阻时 VBUS 必断)。极性只在这两个函数里各出现一次。 */
#define SLOT_VBUS_BIT ((uint8_t)(1u << PCA9555_BIT_VBUS_EN))
hal_err_t slot_vbus_enable(int rail, bool on)
{
return out0_xact(rail, SLOT_VBUS_BIT, on ? SLOT_VBUS_BIT : 0u);
}
hal_err_t slot_vbus_enable_locked(int rail, bool on)
{
return out0_locked(rail, SLOT_VBUS_BIT, on ? SLOT_VBUS_BIT : 0u);
}
bool slot_vbus_is_on(int rail)
{
/* 影子就在驱动里(写成功才更新),这里不另存一份 —— 两处影子迟早会分家。 */
return rail_valid(rail) && (pca9555_shadow(rail - 1) & SLOT_VBUS_BIT) != 0;
}
/* ---- 输入 ---------------------------------------------------------------- */
hal_err_t slot_read_inputs_locked(int rail, pca9555_in_t *out)
{
if (!rail_valid(rail) || out == NULL) {
return HAL_ERR_ARG;
}
return pca9555_read_inputs(out);
}