温度传感器

概述

RTL8721F 提供片上温度传感器,用于测量芯片温度。Rust 驱动 ameba_hal::thermal 直接封装 PAC 寄存器块,不涉及裸位操作或 C FFI 调用。

功能特性

  • 单一实例 Thermal,通过 Thermal::steal() 获取

  • 可配置降采样率,范围从 DownSampRate::Dsr1 到 DownSampRate::Dsr512,以及 ADC 时钟分频,通过 AdcClkDiv::Div64 / AdcClkDiv::Div128 设置

  • 由内部定时器驱动的周期性自动采样,周期通过 ThermalInitParams::time_period 设置

  • 高温过温保护阈值、高温告警阈值、低温告警阈值均可独立配置,并可分别通过 ThermalInt::HIGH_WARN / ThermalInt::LOW_WARN 屏蔽

  • 通过 get_max_temp / get_min_temp 追踪历史最高/最低温度,可用 clear_max_temp / clear_min_temp 重置

  • 芯片版本为 A 时,init 过程中会自动应用出厂增益/偏移校准

  • 通过 raw_to_celsius / raw_to_fahrenheit 将原始值转换为温度,纯数学运算,不涉及寄存器访问

备注

驱动尚未将高低温告警中断封装为异步或 ISR 回调路径,需通过 get_interrupt / clear_interrupt 手动轮询。

工作模式

Thermal 只有一种使用方式:周期性轮询。

轮询模式

调用 init 后,外设会在后台按配置周期持续采样。CPU 可随时通过 get_temp_result 读取最新结果,该方法内部会自旋等待数据锁存标志,直到当前采样完成或超时,再返回原始 19 位数值。raw_to_celsius 将其转换为带符号浮点温度值。

use ameba_hal::{
    println,
    thermal::{TM_INVALID_VALUE, Thermal, ThermalInitParams, raw_to_celsius},
};
...
let thermal = unsafe { Thermal::steal() };
thermal.init(&ThermalInitParams::default());
...
let raw = thermal.get_temp_result();
if raw == TM_INVALID_VALUE {
    println!("[THERMAL] sensor not ready");
    all_pass = false;
    continue;
}

let temp_c = raw_to_celsius(raw);

完整示例见 examples/thermal/src/main.rs。

驱动尚未提供中断驱动或异步版本。需要周期性读取而不阻塞当前任务的场景,可像内置示例那样在 Embassy 定时任务里调用 get_temp_result。

应用示例

Rust SDK 在 examples/thermal/ 下提供一个示例项目:

  • thermal

    每隔 1 秒采样片上传感器一次,共 5 次,将每次原始结果转换为摄氏度并打印。若任意一次读数超出 -10-110 °C 范围则报错。

    接线:无需外部接线,传感器位于片上。

    [THERMAL] start
    [THERMAL] Temp: 35.2 C
    ...
    [PASS] thermal: OK
    

备注

编译任意示例:cargo xtask build example --name thermal。

API 参考

快速用法

use ameba_hal::thermal::{TM_INVALID_VALUE, Thermal, ThermalInitParams, raw_to_celsius};

let thermal = unsafe { Thermal::steal() };
thermal.init(&ThermalInitParams::default());

let raw = thermal.get_temp_result();
if raw != TM_INVALID_VALUE {
    let temp_c = raw_to_celsius(raw);
}

Thermal 还可通过 get_max_temp / get_min_temp 追踪历史极值,并通过 set_high_warn_threshold / set_low_warn_threshold 设置高低温告警阈值。