rtc_io.c 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262
  1. // Copyright 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. #include <string.h>
  15. #include "esp_log.h"
  16. #include "esp_err.h"
  17. #include "freertos/FreeRTOS.h"
  18. #include "freertos/semphr.h"
  19. #include "freertos/timers.h"
  20. #include "driver/rtc_io.h"
  21. #include "hal/rtc_io_hal.h"
  22. static const char *RTCIO_TAG = "RTCIO";
  23. #define RTCIO_CHECK(a, str, ret_val) ({ \
  24. if (!(a)) { \
  25. ESP_LOGE(RTCIO_TAG,"%s(%d): %s", __FUNCTION__, __LINE__, str); \
  26. return (ret_val); \
  27. } \
  28. })
  29. extern portMUX_TYPE rtc_spinlock; //TODO: Will be placed in the appropriate position after the rtc module is finished.
  30. #define RTCIO_ENTER_CRITICAL() portENTER_CRITICAL(&rtc_spinlock)
  31. #define RTCIO_EXIT_CRITICAL() portEXIT_CRITICAL(&rtc_spinlock)
  32. #if SOC_RTCIO_INPUT_OUTPUT_SUPPORTED
  33. /*---------------------------------------------------------------
  34. RTC IO
  35. ---------------------------------------------------------------*/
  36. esp_err_t rtc_gpio_init(gpio_num_t gpio_num)
  37. {
  38. RTCIO_CHECK(rtc_gpio_is_valid_gpio(gpio_num), "RTCIO number error", ESP_ERR_INVALID_ARG);
  39. RTCIO_ENTER_CRITICAL();
  40. rtcio_hal_function_select(rtc_io_number_get(gpio_num), RTCIO_FUNC_RTC);
  41. RTCIO_EXIT_CRITICAL();
  42. return ESP_OK;
  43. }
  44. esp_err_t rtc_gpio_deinit(gpio_num_t gpio_num)
  45. {
  46. RTCIO_CHECK(rtc_gpio_is_valid_gpio(gpio_num), "RTCIO number error", ESP_ERR_INVALID_ARG);
  47. RTCIO_ENTER_CRITICAL();
  48. // Select Gpio as Digital Gpio
  49. rtcio_hal_function_select(rtc_io_number_get(gpio_num), RTCIO_FUNC_DIGITAL);
  50. RTCIO_EXIT_CRITICAL();
  51. return ESP_OK;
  52. }
  53. esp_err_t rtc_gpio_set_level(gpio_num_t gpio_num, uint32_t level)
  54. {
  55. RTCIO_CHECK(rtc_gpio_is_valid_gpio(gpio_num), "RTCIO number error", ESP_ERR_INVALID_ARG);
  56. RTCIO_ENTER_CRITICAL();
  57. rtcio_hal_set_level(rtc_io_number_get(gpio_num), level);
  58. RTCIO_EXIT_CRITICAL();
  59. return ESP_OK;
  60. }
  61. uint32_t rtc_gpio_get_level(gpio_num_t gpio_num)
  62. {
  63. RTCIO_CHECK(rtc_gpio_is_valid_gpio(gpio_num), "RTCIO number error", ESP_ERR_INVALID_ARG);
  64. return rtcio_hal_get_level(rtc_io_number_get(gpio_num));
  65. }
  66. esp_err_t rtc_gpio_set_drive_capability(gpio_num_t gpio_num, gpio_drive_cap_t strength)
  67. {
  68. RTCIO_CHECK(rtc_gpio_is_valid_gpio(gpio_num), "RTCIO number error", ESP_ERR_INVALID_ARG);
  69. RTCIO_CHECK(GPIO_IS_VALID_OUTPUT_GPIO(gpio_num), "Output pad only", ESP_ERR_INVALID_ARG);
  70. RTCIO_CHECK(strength < GPIO_DRIVE_CAP_MAX, "RTCIO drive capability error", ESP_ERR_INVALID_ARG);
  71. RTCIO_ENTER_CRITICAL();
  72. rtcio_hal_set_drive_capability(rtc_io_number_get(gpio_num), strength);
  73. RTCIO_EXIT_CRITICAL();
  74. return ESP_OK;
  75. }
  76. esp_err_t rtc_gpio_get_drive_capability(gpio_num_t gpio_num, gpio_drive_cap_t *strength)
  77. {
  78. RTCIO_CHECK(rtc_gpio_is_valid_gpio(gpio_num), "RTCIO number error", ESP_ERR_INVALID_ARG);
  79. RTCIO_CHECK(GPIO_IS_VALID_OUTPUT_GPIO(gpio_num), "Output pad only", ESP_ERR_INVALID_ARG);
  80. RTCIO_CHECK(strength != NULL, "GPIO drive pointer error", ESP_ERR_INVALID_ARG);
  81. *strength = (gpio_drive_cap_t)rtcio_hal_get_drive_capability(rtc_io_number_get(gpio_num));
  82. return ESP_OK;
  83. }
  84. esp_err_t rtc_gpio_set_direction(gpio_num_t gpio_num, rtc_gpio_mode_t mode)
  85. {
  86. RTCIO_CHECK(rtc_gpio_is_valid_gpio(gpio_num), "RTCIO number error", ESP_ERR_INVALID_ARG);
  87. RTCIO_ENTER_CRITICAL();
  88. rtcio_hal_set_direction(rtc_io_number_get(gpio_num), mode);
  89. RTCIO_EXIT_CRITICAL();
  90. return ESP_OK;
  91. }
  92. esp_err_t rtc_gpio_set_direction_in_sleep(gpio_num_t gpio_num, rtc_gpio_mode_t mode)
  93. {
  94. RTCIO_CHECK(rtc_gpio_is_valid_gpio(gpio_num), "RTCIO number error", ESP_ERR_INVALID_ARG);
  95. RTCIO_ENTER_CRITICAL();
  96. rtcio_hal_set_direction_in_sleep(rtc_io_number_get(gpio_num), mode);
  97. RTCIO_EXIT_CRITICAL();
  98. return ESP_OK;
  99. }
  100. esp_err_t rtc_gpio_pullup_en(gpio_num_t gpio_num)
  101. {
  102. RTCIO_CHECK(rtc_gpio_is_valid_gpio(gpio_num), "RTCIO number error", ESP_ERR_INVALID_ARG);
  103. RTCIO_ENTER_CRITICAL();
  104. rtcio_hal_pullup_enable(rtc_io_number_get(gpio_num));
  105. RTCIO_EXIT_CRITICAL();
  106. return ESP_OK;
  107. }
  108. esp_err_t rtc_gpio_pullup_dis(gpio_num_t gpio_num)
  109. {
  110. RTCIO_CHECK(rtc_gpio_is_valid_gpio(gpio_num), "RTCIO number error", ESP_ERR_INVALID_ARG);
  111. RTCIO_ENTER_CRITICAL();
  112. rtcio_hal_pullup_disable(rtc_io_number_get(gpio_num));
  113. RTCIO_EXIT_CRITICAL();
  114. return ESP_OK;
  115. }
  116. esp_err_t rtc_gpio_pulldown_en(gpio_num_t gpio_num)
  117. {
  118. RTCIO_CHECK(rtc_gpio_is_valid_gpio(gpio_num), "RTCIO number error", ESP_ERR_INVALID_ARG);
  119. RTCIO_ENTER_CRITICAL();
  120. rtcio_hal_pulldown_enable(rtc_io_number_get(gpio_num));
  121. RTCIO_EXIT_CRITICAL();
  122. return ESP_OK;
  123. }
  124. esp_err_t rtc_gpio_pulldown_dis(gpio_num_t gpio_num)
  125. {
  126. RTCIO_CHECK(rtc_gpio_is_valid_gpio(gpio_num), "RTCIO number error", ESP_ERR_INVALID_ARG);
  127. RTCIO_ENTER_CRITICAL();
  128. rtcio_hal_pulldown_disable(rtc_io_number_get(gpio_num));
  129. RTCIO_EXIT_CRITICAL();
  130. return ESP_OK;
  131. }
  132. #endif // SOC_RTCIO_INPUT_OUTPUT_SUPPORTED
  133. #if SOC_RTCIO_HOLD_SUPPORTED
  134. esp_err_t rtc_gpio_hold_en(gpio_num_t gpio_num)
  135. {
  136. #ifdef CONFIG_IDF_TARGET_ESP32C3 // should use HAL here, TODO ESP32-C3 IDF-2511
  137. RTCIO_CHECK(gpio_num <= GPIO_NUM_5, "RTCIO number error", ESP_ERR_INVALID_ARG);
  138. REG_SET_BIT(RTC_CNTL_PAD_HOLD_REG, BIT(gpio_num));
  139. #else
  140. RTCIO_CHECK(rtc_gpio_is_valid_gpio(gpio_num), "RTCIO number error", ESP_ERR_INVALID_ARG);
  141. RTCIO_ENTER_CRITICAL();
  142. rtcio_hal_hold_enable(rtc_io_number_get(gpio_num));
  143. RTCIO_EXIT_CRITICAL();
  144. #endif
  145. return ESP_OK;
  146. }
  147. esp_err_t rtc_gpio_hold_dis(gpio_num_t gpio_num)
  148. {
  149. #ifdef CONFIG_IDF_TARGET_ESP32C3 // should use HAL here, TODO ESP32-C3 IDF-2511
  150. RTCIO_CHECK(gpio_num <= GPIO_NUM_5, "RTCIO number error", ESP_ERR_INVALID_ARG);
  151. REG_CLR_BIT(RTC_CNTL_PAD_HOLD_REG, BIT(gpio_num));
  152. #else
  153. RTCIO_CHECK(rtc_gpio_is_valid_gpio(gpio_num), "RTCIO number error", ESP_ERR_INVALID_ARG);
  154. RTCIO_ENTER_CRITICAL();
  155. rtcio_hal_hold_disable(rtc_io_number_get(gpio_num));
  156. RTCIO_EXIT_CRITICAL();
  157. #endif
  158. return ESP_OK;
  159. }
  160. esp_err_t rtc_gpio_isolate(gpio_num_t gpio_num)
  161. {
  162. RTCIO_CHECK(rtc_gpio_is_valid_gpio(gpio_num), "RTCIO number error", ESP_ERR_INVALID_ARG);
  163. RTCIO_ENTER_CRITICAL();
  164. rtcio_hal_isolate(rtc_io_number_get(gpio_num));
  165. RTCIO_EXIT_CRITICAL();
  166. return ESP_OK;
  167. }
  168. esp_err_t rtc_gpio_force_hold_en_all(void)
  169. {
  170. RTCIO_ENTER_CRITICAL();
  171. rtcio_hal_hold_all();
  172. RTCIO_EXIT_CRITICAL();
  173. return ESP_OK;
  174. }
  175. esp_err_t rtc_gpio_force_hold_dis_all(void)
  176. {
  177. RTCIO_ENTER_CRITICAL();
  178. rtcio_hal_unhold_all();
  179. RTCIO_EXIT_CRITICAL();
  180. return ESP_OK;
  181. }
  182. #endif // SOC_RTCIO_HOLD_SUPPORTED
  183. #if SOC_RTCIO_WAKE_SUPPORTED
  184. esp_err_t rtc_gpio_wakeup_enable(gpio_num_t gpio_num, gpio_int_type_t intr_type)
  185. {
  186. #ifdef CONFIG_IDF_TARGET_ESP32C3 // should use HAL here, TODO ESP32-C3 IDF-2511
  187. RTCIO_CHECK(gpio_num <= GPIO_NUM_5, "RTCIO number error", ESP_ERR_INVALID_ARG);
  188. REG_SET_BIT(RTC_CNTL_GPIO_WAKEUP_REG, RTC_CNTL_GPIO_PIN0_WAKEUP_ENABLE_M >> gpio_num);
  189. uint32_t reg = REG_READ(RTC_CNTL_GPIO_WAKEUP_REG);
  190. reg &= (~(RTC_CNTL_GPIO_PIN0_INT_TYPE_V << (RTC_CNTL_GPIO_PIN0_INT_TYPE_S - gpio_num * 3)));
  191. reg |= (intr_type << (RTC_CNTL_GPIO_PIN0_INT_TYPE_S - gpio_num * 3));
  192. REG_WRITE(RTC_CNTL_GPIO_WAKEUP_REG, reg);
  193. ESP_LOGD(RTCIO_TAG, "gpio wake up 0x%08x", REG_READ(RTC_CNTL_GPIO_WAKEUP_REG));
  194. #else
  195. RTCIO_CHECK(rtc_gpio_is_valid_gpio(gpio_num), "RTCIO number error", ESP_ERR_INVALID_ARG);
  196. if (intr_type == GPIO_INTR_POSEDGE || intr_type == GPIO_INTR_NEGEDGE || intr_type == GPIO_INTR_ANYEDGE) {
  197. return ESP_ERR_INVALID_ARG; // Dont support this mode.
  198. }
  199. RTCIO_ENTER_CRITICAL();
  200. rtcio_hal_wakeup_enable(rtc_io_number_get(gpio_num), intr_type);
  201. RTCIO_EXIT_CRITICAL();
  202. #endif // CONFIG_IDF_TARGET_ESP32C3
  203. return ESP_OK;
  204. }
  205. esp_err_t rtc_gpio_wakeup_disable(gpio_num_t gpio_num)
  206. {
  207. #ifdef CONFIG_IDF_TARGET_ESP32C3 // should use HAL here, TODO ESP32-C3 IDF-2511
  208. RTCIO_CHECK(gpio_num <= GPIO_NUM_5, "RTCIO number error", ESP_ERR_INVALID_ARG);
  209. REG_CLR_BIT(RTC_CNTL_GPIO_WAKEUP_REG, RTC_CNTL_GPIO_PIN0_WAKEUP_ENABLE_M >> gpio_num);
  210. #else
  211. RTCIO_CHECK(rtc_gpio_is_valid_gpio(gpio_num), "RTCIO number error", ESP_ERR_INVALID_ARG);
  212. RTCIO_ENTER_CRITICAL();
  213. rtcio_hal_wakeup_disable(rtc_io_number_get(gpio_num));
  214. RTCIO_EXIT_CRITICAL();
  215. #endif
  216. return ESP_OK;
  217. }
  218. #endif // SOC_RTCIO_WAKE_SUPPORTED