概述
由于需要在低功耗(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);
简述如下:
gpio_hold_en()
使能 GPIO 状态保持功能,如果是输入模式,则读取的电平不会被改变。如果是输出模式,则输出电平会被锁定且不会被改变。注:如果是 Deep sleep 下保持 GPIO 状态,则需要额外调用
gpio_deep_sleep_hold_en()
gpio_hold_dis()
禁用 GPIO 状态保持功能,如果之前在使能 GPIO 状态保持功能时也调用了gpio_deep_sleep_hold_en()
,此时可额外调用gpio_deep_sleep_hold_dis()
gpio_deep_sleep_hold_en()
Deep-sleep 前使能 GPIO 状态保持功能,与gpio_hold_en()
同步使用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();
最后
以上就是高兴薯片为你收集整理的ESP32 在低功耗模式下保持 GPIO 状态的方法的全部内容,希望文章能够帮你解决ESP32 在低功耗模式下保持 GPIO 状态的方法所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
发表评论 取消回复