rtc_io.c 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236
  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 __attribute__((__unused__)) *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. RTCIO_CHECK(rtc_gpio_is_valid_gpio(gpio_num), "RTCIO number error", ESP_ERR_INVALID_ARG);
  137. RTCIO_ENTER_CRITICAL();
  138. rtcio_hal_hold_enable(rtc_io_number_get(gpio_num));
  139. RTCIO_EXIT_CRITICAL();
  140. return ESP_OK;
  141. }
  142. esp_err_t rtc_gpio_hold_dis(gpio_num_t gpio_num)
  143. {
  144. RTCIO_CHECK(rtc_gpio_is_valid_gpio(gpio_num), "RTCIO number error", ESP_ERR_INVALID_ARG);
  145. RTCIO_ENTER_CRITICAL();
  146. rtcio_hal_hold_disable(rtc_io_number_get(gpio_num));
  147. RTCIO_EXIT_CRITICAL();
  148. return ESP_OK;
  149. }
  150. esp_err_t rtc_gpio_isolate(gpio_num_t gpio_num)
  151. {
  152. RTCIO_CHECK(rtc_gpio_is_valid_gpio(gpio_num), "RTCIO number error", ESP_ERR_INVALID_ARG);
  153. RTCIO_ENTER_CRITICAL();
  154. rtcio_hal_isolate(rtc_io_number_get(gpio_num));
  155. RTCIO_EXIT_CRITICAL();
  156. return ESP_OK;
  157. }
  158. esp_err_t rtc_gpio_force_hold_en_all(void)
  159. {
  160. RTCIO_ENTER_CRITICAL();
  161. rtcio_hal_hold_all();
  162. RTCIO_EXIT_CRITICAL();
  163. return ESP_OK;
  164. }
  165. esp_err_t rtc_gpio_force_hold_dis_all(void)
  166. {
  167. RTCIO_ENTER_CRITICAL();
  168. rtcio_hal_unhold_all();
  169. RTCIO_EXIT_CRITICAL();
  170. return ESP_OK;
  171. }
  172. #endif // SOC_RTCIO_HOLD_SUPPORTED
  173. #if SOC_RTCIO_WAKE_SUPPORTED
  174. esp_err_t rtc_gpio_wakeup_enable(gpio_num_t gpio_num, gpio_int_type_t intr_type)
  175. {
  176. RTCIO_CHECK(rtc_gpio_is_valid_gpio(gpio_num), "RTCIO number error", ESP_ERR_INVALID_ARG);
  177. if (intr_type == GPIO_INTR_POSEDGE || intr_type == GPIO_INTR_NEGEDGE || intr_type == GPIO_INTR_ANYEDGE) {
  178. return ESP_ERR_INVALID_ARG; // Dont support this mode.
  179. }
  180. RTCIO_ENTER_CRITICAL();
  181. rtcio_hal_wakeup_enable(rtc_io_number_get(gpio_num), intr_type);
  182. RTCIO_EXIT_CRITICAL();
  183. return ESP_OK;
  184. }
  185. esp_err_t rtc_gpio_wakeup_disable(gpio_num_t gpio_num)
  186. {
  187. RTCIO_CHECK(rtc_gpio_is_valid_gpio(gpio_num), "RTCIO number error", ESP_ERR_INVALID_ARG);
  188. RTCIO_ENTER_CRITICAL();
  189. rtcio_hal_wakeup_disable(rtc_io_number_get(gpio_num));
  190. RTCIO_EXIT_CRITICAL();
  191. return ESP_OK;
  192. }
  193. #endif // SOC_RTCIO_WAKE_SUPPORTED