ESP32 在低功耗模式下保持 GPIO 状态的方法

由于需要在低功耗(Light sleep & Deep sleep)下保持某些 GPIO 输出高电平,此篇博客用来记录 ESP32 在低功耗模式下保持 GPIO 状态的方法。分为以下三个部分:

  • 相关 API 整理
  • Light sleep 下保持 GPIO 状态
  • Deep sleep 下保持 GPIO 状态

注:此时 ESP-IDF 的 commit 为 c9646ff0beffc86d2c6d1bfbad34da16e328e0e3 (HEAD, tag: v4.3)

1 相关 API 整理

通过查阅 ESP-IDF 资料,可找到如下四个相关的 API:

/**
  * @brief Enable gpio pad hold function.
  *
  * The gpio pad hold function works in both input and output modes, but must be output-capable gpios.
  * If pad hold enabled:
  *   in output mode: the output level of the pad will be force locked and can not be changed.
  *   in input mode: the input value read will not change, regardless the changes of input signal.
  *
  * The state of digital gpio cannot be held during Deep-sleep, and it will resume the hold function
  * when the chip wakes up from Deep-sleep. If the digital gpio also needs to be held during Deep-sleep,
  * `gpio_deep_sleep_hold_en` should also be called.
  *
  * Power down or call gpio_hold_dis will disable this function.
  *
  * @param gpio_num GPIO number, only support output-capable GPIOs
  *
  * @return
  *     - ESP_OK Success
  *     - ESP_ERR_NOT_SUPPORTED Not support pad hold function
  */
esp_err_t gpio_hold_en(gpio_num_t gpio_num);

/**
  * @brief Disable gpio pad hold function.
  *
  * When the chip is woken up from Deep-sleep, the gpio will be set to the default mode, so, the gpio will output
  * the default level if this function is called. If you don't want the level changes, the gpio should be configured to
  * a known state before this function is called.
  *  e.g.
  *     If you hold gpio18 high during Deep-sleep, after the chip is woken up and `gpio_hold_dis` is called,
  *     gpio18 will output low level(because gpio18 is input mode by default). If you don't want this behavior,
  *     you should configure gpio18 as output mode and set it to hight level before calling `gpio_hold_dis`.
  *
  * @param gpio_num GPIO number, only support output-capable GPIOs
  *
  * @return
  *     - ESP_OK Success
  *     - ESP_ERR_NOT_SUPPORTED Not support pad hold function
  */
esp_err_t gpio_hold_dis(gpio_num_t gpio_num);

/**
  * @brief Enable all digital gpio pad hold function during Deep-sleep.
  *
  * When the chip is in Deep-sleep mode, all digital gpio will hold the state before sleep, and when the chip is woken up,
  * the status of digital gpio will not be held. Note that the pad hold feature only works when the chip is in Deep-sleep mode,
  * when not in sleep mode, the digital gpio state can be changed even you have called this function.
  *
  * Power down or call gpio_hold_dis will disable this function, otherwise, the digital gpio hold feature works as long as the chip enter Deep-sleep.
  */
void gpio_deep_sleep_hold_en(void);

/**
  * @brief Disable all digital gpio pad hold function during Deep-sleep.
  *
  */
void gpio_deep_sleep_hold_dis(void);

简述如下:

  1. gpio_hold_en() 使能 GPIO 状态保持功能,如果是输入模式,则读取的电平不会被改变。如果是输出模式,则输出电平会被锁定且不会被改变。

    注:如果是 Deep sleep 下保持 GPIO 状态,则需要额外调用 gpio_deep_sleep_hold_en()

  2. gpio_hold_dis() 禁用 GPIO 状态保持功能,如果之前在使能 GPIO 状态保持功能时也调用了 gpio_deep_sleep_hold_en(),此时可额外调用 gpio_deep_sleep_hold_dis()
  3. gpio_deep_sleep_hold_en() Deep-sleep 前使能 GPIO 状态保持功能,与 gpio_hold_en() 同步使用
  4. gpio_deep_sleep_hold_dis() Deep-sleep 后禁用 GPIO 状态保持功能,与 gpio_hold_dis() 同步使用

2 Light sleep 下保持 GPIO 状态

Light sleep 下可以保持任意具有输入或者输出的 GPIO 状态,进入 light sleep 前调用 gpio_hold_en(gpio_num_t gpio_num),gpio_num 为需要保持状态的 GPIO 编号。离开 light sleep 后如果想取消 GPIO 状态保持,调用 gpio_hold_dis(gpio_num_t gpio_num) 即可。

3. Deep sleep 下保持 GPIO 状态

Deep sleep 下可以保持 RTC GPIO 的状态。以下为 ESP32 对应的 RTC GPIO:
在这里插入图片描述
此时可以在进入 Deep sleep 前调用:

gpio_hold_en(GPIO_NUM);

gpio_deep_sleep_hold_en()

设置保持上图里的 GPIO 状态。如果从 Deep sleep 唤醒后取消 GPIO 状态保持,可以调用:

gpio_hold_dis(GPIO_NUM);

gpio_deep_sleep_hold_dis()
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值