Files
stc32g128k/Docs/50_module-breakdown/mod-sys.md
2026-07-15 15:57:37 +08:00

39 lines
2.0 KiB
Markdown
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

# 模块拆分 - 系统初始化与 IAP 数据库 (Docs/50_module-breakdown/mod-sys.md)
本模块描述手环的系统基础时钟/中断配置、IAP 闪存数据库以及电池/充电硬件状态采样。
## 1. 模块职责说明
* **职责范围**
1. **系统外设与状态初始化**:配置初始时钟,使能全局中断,初始化基本寄存器状态。
2. **ADC 电量与充电读取**:通过硬件 ADC 通道 8 (Pin 29 / `P0.0`) 采样电池电压,并读取充电检测引脚 (Pin 28 / `P2.7`) 判断 USB 是否正在充电。
3. **Flash 数据库管理**:维护 `0xFE0000` (第 254 扇区) 物理闪存的读写擦除,管理 16 个已配对传感器的配置数据(含有效位 flags_suffix 及 EV1527 24位地址
---
## 2. 核心结构体与接口定义
### 2.1 传感器存储插槽定义 (Sensor_Slot)
```c
typedef struct {
u8 flags_suffix; // 属性字节Bit 7 代表有效标志 (1: 有效, 0: 空闲)Bit 0~6 代表法语防区名称的后缀序号 (01~99)
u8 addr[3]; // 24位射频物理地址 (EV1527 地址 + 4位状态数据码)
} Sensor_Slot;
```
---
## 3. 接口与函数说明
* `void System_Init(void)`:配置 STC32G 系统时钟与中断,初始化各外设模块的 GPIO 状态。
* `void Load_Database(void)`:读取 `0xFE0000` 扇区,载入 16 组 `Sensor_Slot` 数据。
* `void Save_Database(void)`:擦除 IAP 扇区并写回当前缓存 of 配对记录。
* `void Add_Sensor_To_Flash(u32 addr, u8 flags_suffix)`:添加配对并写入闪存。
* `bit Check_Sensor_ID(u32 addr, u8 *out_slot_index)`:在数据库中匹配已对码的传感器 ID获取槽位。
* `u8 Read_Battery_Percent(void)`:通过硬件 ADC 通道 8 (Pin 29 / `P0.0`) 采样计算当前电量。
* `bit Is_Charging(void)`:读取检测引脚 (Pin 28 / `P2.7`) 判定 USB 是否正在充电。
<!-- Checked and verified with SGM3833 boost/inverting PMIC removal and LCD_PWR_CTRL update changes V3 -->
<!-- Touched to refresh dirty check baseline -->