From ccf09c3b393d51107ddc1c8061485f86147a7045 Mon Sep 17 00:00:00 2001 From: kooho <2229179028@qq.com> Date: Tue, 3 Apr 2018 22:09:30 +0800 Subject: [PATCH] driver(gpio): Add api support digital pad hold function. --- components/driver/gpio.c | 65 +++++++++++++++++++++++++ components/driver/include/driver/gpio.h | 29 +++++++++++ 2 files changed, 94 insertions(+) diff --git a/components/driver/gpio.c b/components/driver/gpio.c index 02c9e64f4..e8f6c632c 100644 --- a/components/driver/gpio.c +++ b/components/driver/gpio.c @@ -482,3 +482,68 @@ esp_err_t gpio_get_drive_capability(gpio_num_t gpio_num, gpio_drive_cap_t* stren } return ESP_OK; } + +static const uint32_t GPIO_HOLD_MASK[34] = { + 0, + GPIO_SEL_1, + 0, + GPIO_SEL_0, + 0, + GPIO_SEL_8, + GPIO_SEL_2, + GPIO_SEL_3, + GPIO_SEL_4, + GPIO_SEL_5, + GPIO_SEL_6, + GPIO_SEL_7, + 0, + 0, + 0, + 0, + GPIO_SEL_9, + GPIO_SEL_10, + GPIO_SEL_11, + GPIO_SEL_12, + 0, + GPIO_SEL_14, + GPIO_SEL_15, + GPIO_SEL_16, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, +}; + +esp_err_t gpio_hold_en(gpio_num_t gpio_num) +{ + GPIO_CHECK(GPIO_IS_VALID_OUTPUT_GPIO(gpio_num), "Only output-capable GPIO support this function", ESP_ERR_NOT_SUPPORTED); + esp_err_t r = ESP_OK; + if (RTC_GPIO_IS_VALID_GPIO(gpio_num)) { + r = rtc_gpio_hold_en(gpio_num); + } else if (GPIO_HOLD_MASK[gpio_num]) { + SET_PERI_REG_MASK(RTC_IO_DIG_PAD_HOLD_REG, GPIO_HOLD_MASK[gpio_num]); + } else { + r = ESP_ERR_NOT_SUPPORTED; + } + return r == ESP_OK ? ESP_OK : ESP_ERR_NOT_SUPPORTED; +} + +esp_err_t gpio_hold_dis(gpio_num_t gpio_num) +{ + GPIO_CHECK(GPIO_IS_VALID_OUTPUT_GPIO(gpio_num), "Only output-capable GPIO support this function", ESP_ERR_NOT_SUPPORTED); + esp_err_t r = ESP_OK; + if (RTC_GPIO_IS_VALID_GPIO(gpio_num)) { + r = rtc_gpio_hold_dis(gpio_num); + } else if (GPIO_HOLD_MASK[gpio_num]) { + CLEAR_PERI_REG_MASK(RTC_IO_DIG_PAD_HOLD_REG, GPIO_HOLD_MASK[gpio_num]); + } else { + r = ESP_ERR_NOT_SUPPORTED; + } + return r == ESP_OK ? ESP_OK : ESP_ERR_NOT_SUPPORTED; +} \ No newline at end of file diff --git a/components/driver/include/driver/gpio.h b/components/driver/include/driver/gpio.h index 8246c415e..2f272ef5e 100644 --- a/components/driver/include/driver/gpio.h +++ b/components/driver/include/driver/gpio.h @@ -518,6 +518,35 @@ esp_err_t gpio_set_drive_capability(gpio_num_t gpio_num, gpio_drive_cap_t streng */ esp_err_t gpio_get_drive_capability(gpio_num_t gpio_num, gpio_drive_cap_t* strength); +/** + * @brief Set 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. + * + * 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 Unset gpio pad hold 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_dis(gpio_num_t gpio_num); + #ifdef __cplusplus } #endif