gpio_hal_workaround.c 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. // Copyright 2015-2019 Espressif Systems (Shanghai) PTE LTD
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. // The HAL layer for GPIO (common part)
  15. //
  16. #include "esp_attr.h"
  17. #include "soc/soc.h"
  18. #include "hal/gpio_hal.h"
  19. #include "soc/soc_caps.h"
  20. #if CONFIG_GPIO_ESP32_SUPPORT_SWITCH_SLP_PULL
  21. typedef struct gpio_slp_mode_cfg {
  22. volatile uint16_t fun_pu[((SOC_GPIO_PIN_COUNT-1) >> 4) + 1];
  23. volatile uint16_t fun_pd[((SOC_GPIO_PIN_COUNT-1) >> 4) + 1];
  24. } gpio_slp_mode_cfg_t;
  25. static void gpio_hal_sleep_mode_setup_wrapper(
  26. gpio_hal_context_t *hal,
  27. gpio_num_t gpio_num,
  28. void (*opt)(gpio_hal_context_t *, gpio_num_t, void *)
  29. )
  30. {
  31. static DRAM_ATTR gpio_slp_mode_cfg_t gpio_cfg;
  32. if (opt) {
  33. (*opt)(hal, gpio_num, (void *)&gpio_cfg);
  34. }
  35. }
  36. /**
  37. * @brief GPIO pu/pd information backup function
  38. * @param hal gpio hal
  39. * @param gpio_num gpio num
  40. * @param args pointer for bitmap to backup GPIO pu/pd information
  41. */
  42. static void gpio_hal_fun_pupd_backup(gpio_hal_context_t *hal, gpio_num_t gpio_num, void *args)
  43. {
  44. /* On ESP32, setting SLP_PU, SLP_PD couldn`t change GPIO status
  45. * from FUN_PU, FUN_PD to SLP_PU, SLP_PD at sleep.
  46. * On the ESP32S2, it does.
  47. * The following code emulates ESP32S2`s behavior:
  48. */
  49. gpio_slp_mode_cfg_t *pcfg = (gpio_slp_mode_cfg_t *)args;
  50. if (gpio_ll_sleep_sel_is_enabled(hal->dev, gpio_num)) {
  51. /* Record fun_pu and fun_pd state in bitmap */
  52. if (gpio_ll_pullup_is_enabled(hal->dev, gpio_num)) {
  53. pcfg->fun_pu[gpio_num >> 4] |= BIT(gpio_num & 0xf);
  54. } else {
  55. pcfg->fun_pu[gpio_num >> 4] &= ~BIT(gpio_num & 0xf);
  56. }
  57. if (gpio_ll_pulldown_is_enabled(hal->dev, gpio_num)) {
  58. pcfg->fun_pd[gpio_num >> 4] |= BIT(gpio_num & 0xf);
  59. } else {
  60. pcfg->fun_pd[gpio_num >> 4] &= ~BIT(gpio_num & 0xf);
  61. }
  62. if (gpio_ll_sleep_pullup_is_enabled(hal->dev, gpio_num)) {
  63. gpio_ll_pullup_en(hal->dev, gpio_num);
  64. } else {
  65. gpio_ll_pullup_dis(hal->dev, gpio_num);
  66. }
  67. if (gpio_ll_sleep_pulldown_is_enabled(hal->dev, gpio_num)) {
  68. gpio_ll_pulldown_en(hal->dev, gpio_num);
  69. } else {
  70. gpio_ll_pulldown_dis(hal->dev, gpio_num);
  71. }
  72. }
  73. }
  74. /**
  75. * @brief GPIO pu/pd information backup function
  76. * @param hal gpio hal
  77. * @param gpio_num gpio num
  78. * @param args pointer for bitmap to restore GPIO pu/pd information
  79. */
  80. static void gpio_hal_fun_pupd_restore(gpio_hal_context_t *hal, gpio_num_t gpio_num, void *args)
  81. {
  82. /* On ESP32, setting SLP_PU, SLP_PD couldn`t change GPIO status
  83. * from SLP_PU, SLP_PD to FUN_PU, FUN_PD when it wakes up.
  84. * On the ESP32S2, it does.
  85. * The following code emulates ESP32S2`s behavior:
  86. */
  87. gpio_slp_mode_cfg_t *pcfg = (gpio_slp_mode_cfg_t *)args;
  88. if (gpio_ll_sleep_sel_is_enabled(hal->dev, gpio_num)) {
  89. if (pcfg->fun_pu[gpio_num >> 4] & BIT(gpio_num & 0xf)) {
  90. gpio_ll_pullup_en(hal->dev, gpio_num);
  91. } else {
  92. gpio_ll_pullup_dis(hal->dev, gpio_num);
  93. }
  94. if (pcfg->fun_pd[gpio_num >> 4] & BIT(gpio_num & 0xf)) {
  95. gpio_ll_pulldown_en(hal->dev, gpio_num);
  96. } else {
  97. gpio_ll_pulldown_dis(hal->dev, gpio_num);
  98. }
  99. }
  100. }
  101. void gpio_hal_sleep_pupd_config_apply(gpio_hal_context_t *hal, gpio_num_t gpio_num)
  102. {
  103. gpio_hal_sleep_mode_setup_wrapper(hal, gpio_num, gpio_hal_fun_pupd_backup);
  104. }
  105. void gpio_hal_sleep_pupd_config_unapply(gpio_hal_context_t *hal, gpio_num_t gpio_num)
  106. {
  107. gpio_hal_sleep_mode_setup_wrapper(hal, gpio_num, gpio_hal_fun_pupd_restore);
  108. }
  109. #endif