4. S32K低功耗模式与唤醒源配置
任务目标
让 S32K1xx 进入 STOP 模式,用 LPUART 接收中断作为唤醒源,并在唤醒前后各测一次电流,验证低功耗与唤醒链路是否成立。
硬件与依赖
- S32K144(或同系列)开发板,LPUART1 引脚接到 USB-TTL,波特率 115200。
- 万用表(mA 档)串在板子供电回路,或使用板载电流测量跳线。
- S32 Design Studio + S32K1xx SDK(含
power_manager、lpuart驱动)。
实现思路
初始化 LPUART1 接收中断,主循环打印一次“进入 STOP”后调用 POWER_SYS_EnterStopMode()。STOP 模式下内核时钟停止,LPUART 接收引脚检测到起始位产生中断,MCU 退出 STOP,回到主循环打印“已唤醒”。功耗测量点:进入 STOP 前读一次电流,唤醒后立刻再读一次。
关键代码
#include "S32K144.h"
#include "lpuart_driver.h"
#include "power_manager.h"
#include "clock_manager.h"
static volatile uint8_t g_wakeup = 0;
/* LPUART1 RX 中断:收到任意字节即唤醒 */
void LPUART1_RxTx_IRQHandler(void)
{
uint8_t dummy;
LPUART_DRV_ReceiveData(INST_LPUART1, &dummy, 1);
g_wakeup = 1;
}
void LPUART1_Init(void)
{
lpuart_user_config_t cfg = {
.baudRate = 115200U,
.parityMode = LPUART_PARITY_DISABLED,
.stopBitCount = LPUART_ONE_STOP_BIT,
.bitCountPerChar = LPUART_8_BITS_PER_CHAR,
.transferType = LPUART_USING_INTERRUPTS,
.rxCallback = NULL,
.txCallback = NULL
};
LPUART_DRV_Init(INST_LPUART1, &lpuartState1, &cfg);
LPUART_DRV_InstallRxCallback(INST_LPUART1, LPUART1_RxTx_IRQHandler, NULL);
LPUART_DRV_ReceiveData(INST_LPUART1, (uint8_t *)&g_wakeup, 1);
}
int main(void)
{
CLOCK_SYS_Init(g_clockManConfigsArr, CLOCK_MANAGER_CONFIG_CNT,
g_clockManCallbacksArr, CLOCK_MANAGER_CALLBACK_CNT);
CLOCK_SYS_UpdateConfiguration(0U, CLOCK_MANAGER_POLICY_AGREEMENT);
LPUART1_Init();
/* 进入 STOP 前:此处用万用表记录 I_run */
POWER_SYS_EnterStopMode();
/* 被 LPUART RX 中断唤醒后返回此处:记录 I_stop */
if (g_wakeup) {
g_wakeup = 0;
LPUART_DRV_ReceiveData(INST_LPUART1, (uint8_t *)&g_wakeup, 1);
}
for (;;) {
/* 唤醒后停留,便于测量 */
}
}
运行与验证
- 编译烧录后,串口助手以 115200 打开 LPUART1 对应端口。
- 万用表 mA 档串入供电,记录进入 STOP 前的电流值 I_run。
- 向串口发送任意一个字节,MCU 应退出 STOP,程序停在
for(;;),此时记录电流 I_stop。 - 判定通过:I_stop 明显低于 I_run(典型 STOP 模式为 µA 级,运行模式为 mA 级),且发送字节后能稳定唤醒。
若唤醒失败,检查 LPUART1 中断是否在 NVIC 中使能、RX 引脚是否配置为 LPUART 功能,以及 STOP 模式下 LPUART 时钟源是否保持。