【NCS随笔】如何进入system_off深度睡眠模式以及配置GPIO中断唤醒 电子说
【NCS随笔】如何进入system_off深度睡眠模式以及配置GPIO中断唤醒
本文章主要是讲解NCS下面使用nRF54L15如何进入system_off模式,以及如何配置通过按键唤醒
在prj.conf里面添加CONFIG_POWEROFF=y
在主函数文件调用如下头文件#include
即可使用进入system_off模式的函数:sys_poweroff();
进入 System OFF 前,需确保所有 EasyDMA 事务结束,HFXO 停止,且 RESETREAS 清零,否则可能无法进入
还是老规矩,使用hello_world例程,分别使用nrfx的gpio库和zephyr的库来唤醒
1、头文件调用#include
2、main函数里面添加
#define BUTTON3_PIN 4 // P0.04 对应DK的BUTTON3
// 配置 P0.04 为输入,上拉,并使能 SENSE 低电平唤醒
nrf_gpio_cfg_input(BUTTON3_PIN, NRF_GPIO_PIN_PULLUP);
nrf_gpio_cfg_sense_set(BUTTON3_PIN, NRF_GPIO_PIN_SENSE_LOW);
1、头文件调用#include
2、添加宏定义CONFIG_GPIO=y
3、主函数配置gpio唤醒
#define BUTTON_NODE DT_ALIAS(sw0)
#define BUTTON_PIN DT_GPIO_PIN(BUTTON_NODE, gpios)
#define BUTTON_FLAGS (GPIO_INPUT | DT_GPIO_FLAGS(BUTTON_NODE, gpios))
static const struct device *button_dev;
void main(void)
{
int ret;
printf("Hello World! %sn", CONFIG_BOARD_TARGET);
button_dev = DEVICE_DT_GET(DT_GPIO_CTLR(BUTTON_NODE, gpios));
if (!device_is_ready(button_dev)) {
printk("Button device not readyn");
return;
}
ret = gpio_pin_configure(button_dev, BUTTON_PIN, BUTTON_FLAGS);
if (ret < 0) {
printk("Failed to configure buttonn");
return;
}
// 配置为唤醒源
ret = gpio_pin_interrupt_configure(button_dev, BUTTON_PIN, GPIO_INT_EDGE_TO_ACTIVE | GPIO_INT_WAKEUP);
if (ret < 0) {
printk("Failed to configure button interruptn");
return;
}
printk("Waiting 5 seconds before entering System OFF...n");
k_sleep(K_SECONDS(5));
printk("Entering System OFF moden");
sys_poweroff();
// 进入System OFF后,只有唤醒源(如按键)才能唤醒,唤醒后会复位
}
4、overlay里面设置BUTTON0的sense-edge-mask寄存器
&gpio1 {
sense-edge-mask = < 0x00002000 >;
//sense-edge-mask 的每一位对应一个 GPIO pin,bit0 对应 P0.00,bit1 对应 P0.01,……,bit31 对应 P0.31所以P1,13对应0x200
};
main:
/*
* Copyright (c) 2012-2014 Wind River Systems, Inc.
*
* SPDX-License-Identifier: Apache-2.0
*/
#include < stdio.h >
#include < zephyr/kernel.h >
#include < zephyr/device.h >
#include < zephyr/drivers/gpio.h >
#include < zephyr/pm/pm.h >
#include < zephyr/pm/policy.h >
#include < zephyr/sys/printk.h >
#include < zephyr/sys/poweroff.h >
#include < hal/nrf_gpio.h >
#define BUTTON_NODE DT_ALIAS(sw0)
#define BUTTON_PIN DT_GPIO_PIN(BUTTON_NODE, gpios)
#define BUTTON_FLAGS (GPIO_INPUT | DT_GPIO_FLAGS(BUTTON_NODE, gpios))
static const struct device *button_dev;
#define BUTTON3_PIN 4 // P0.04 对应DK的BUTTON3
void main(void)
{
int ret;
printf("Hello World! %sn", CONFIG_BOARD_TARGET);
button_dev = DEVICE_DT_GET(DT_GPIO_CTLR(BUTTON_NODE, gpios));
if (!device_is_ready(button_dev)) {
printk("Button device not readyn");
return;
}
ret = gpio_pin_configure(button_dev, BUTTON_PIN, BUTTON_FLAGS);
if (ret < 0) {
printk("Failed to configure buttonn");
return;
}
// 配置为唤醒源
ret = gpio_pin_interrupt_configure(button_dev, BUTTON_PIN, GPIO_INT_EDGE_TO_ACTIVE | GPIO_INT_WAKEUP);
if (ret < 0) {
printk("Failed to configure button interruptn");
return;
}
// 配置 P0.04 为输入,上拉,并使能 SENSE 低电平唤醒
nrf_gpio_cfg_input(BUTTON3_PIN, NRF_GPIO_PIN_PULLUP);
nrf_gpio_cfg_sense_set(BUTTON3_PIN, NRF_GPIO_PIN_SENSE_LOW);
printk("Waiting 5 seconds before entering System OFF...n");
k_sleep(K_SECONDS(5));
printk("Entering System OFF moden");
sys_poweroff();
// 进入System OFF后,只有唤醒源(如按键)才能唤醒,唤醒后会复位
}
prj.conf
CONFIG_GPIO=y
CONFIG_POWEROFF=y
overlay:
&gpio1 {
sense-edge-mask = < 0x00002000 >; // 只举例,实际bit需对应你的按键引脚
};
设置上电5S进入深度休眠模式,然后通过按键唤醒:
审核编辑 黄宇
全部0条评论
快来发表一下你的评论吧 !