rtc_io.c 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228
  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):%s", __FILE__, __LINE__, __FUNCTION__, 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. /*---------------------------------------------------------------
  33. RTC IO
  34. ---------------------------------------------------------------*/
  35. esp_err_t rtc_gpio_init(gpio_num_t gpio_num)
  36. {
  37. RTCIO_CHECK(rtc_gpio_is_valid_gpio(gpio_num), "RTCIO number error", ESP_ERR_INVALID_ARG);
  38. RTCIO_ENTER_CRITICAL();
  39. rtcio_hal_function_select(rtc_io_number_get(gpio_num), RTCIO_FUNC_RTC);
  40. RTCIO_EXIT_CRITICAL();
  41. return ESP_OK;
  42. }
  43. esp_err_t rtc_gpio_deinit(gpio_num_t gpio_num)
  44. {
  45. RTCIO_CHECK(rtc_gpio_is_valid_gpio(gpio_num), "RTCIO number error", ESP_ERR_INVALID_ARG);
  46. RTCIO_ENTER_CRITICAL();
  47. // Select Gpio as Digital Gpio
  48. rtcio_hal_function_select(rtc_io_number_get(gpio_num), RTCIO_FUNC_DIGITAL);
  49. RTCIO_EXIT_CRITICAL();
  50. return ESP_OK;
  51. }
  52. esp_err_t rtc_gpio_set_level(gpio_num_t gpio_num, uint32_t level)
  53. {
  54. RTCIO_CHECK(rtc_gpio_is_valid_gpio(gpio_num), "RTCIO number error", ESP_ERR_INVALID_ARG);
  55. RTCIO_ENTER_CRITICAL();
  56. rtcio_hal_set_level(rtc_io_number_get(gpio_num), level);
  57. RTCIO_EXIT_CRITICAL();
  58. return ESP_OK;
  59. }
  60. uint32_t rtc_gpio_get_level(gpio_num_t gpio_num)
  61. {
  62. RTCIO_CHECK(rtc_gpio_is_valid_gpio(gpio_num), "RTCIO number error", ESP_ERR_INVALID_ARG);
  63. return rtcio_hal_get_level(rtc_io_number_get(gpio_num));
  64. }
  65. esp_err_t rtc_gpio_set_drive_capability(gpio_num_t gpio_num, gpio_drive_cap_t strength)
  66. {
  67. RTCIO_CHECK(rtc_gpio_is_valid_gpio(gpio_num), "RTCIO number error", ESP_ERR_INVALID_ARG);
  68. RTCIO_CHECK(GPIO_IS_VALID_OUTPUT_GPIO(gpio_num), "Output pad only", ESP_ERR_INVALID_ARG);
  69. RTCIO_CHECK(strength < GPIO_DRIVE_CAP_MAX, "RTCIO drive capability error", ESP_ERR_INVALID_ARG);
  70. RTCIO_ENTER_CRITICAL();
  71. rtcio_hal_set_drive_capability(rtc_io_number_get(gpio_num), strength);
  72. RTCIO_EXIT_CRITICAL();
  73. return ESP_OK;
  74. }
  75. esp_err_t rtc_gpio_get_drive_capability(gpio_num_t gpio_num, gpio_drive_cap_t *strength)
  76. {
  77. RTCIO_CHECK(rtc_gpio_is_valid_gpio(gpio_num), "RTCIO number error", ESP_ERR_INVALID_ARG);
  78. RTCIO_CHECK(GPIO_IS_VALID_OUTPUT_GPIO(gpio_num), "Output pad only", ESP_ERR_INVALID_ARG);
  79. RTCIO_CHECK(strength != NULL, "GPIO drive pointer error", ESP_ERR_INVALID_ARG);
  80. *strength = (gpio_drive_cap_t)rtcio_hal_get_drive_capability(rtc_io_number_get(gpio_num));
  81. return ESP_OK;
  82. }
  83. esp_err_t rtc_gpio_set_direction(gpio_num_t gpio_num, rtc_gpio_mode_t mode)
  84. {
  85. RTCIO_CHECK(rtc_gpio_is_valid_gpio(gpio_num), "RTCIO number error", ESP_ERR_INVALID_ARG);
  86. RTCIO_ENTER_CRITICAL();
  87. rtcio_hal_set_direction(rtc_io_number_get(gpio_num), mode);
  88. RTCIO_EXIT_CRITICAL();
  89. return ESP_OK;
  90. }
  91. esp_err_t rtc_gpio_set_direction_in_sleep(gpio_num_t gpio_num, rtc_gpio_mode_t mode)
  92. {
  93. RTCIO_CHECK(rtc_gpio_is_valid_gpio(gpio_num), "RTCIO number error", ESP_ERR_INVALID_ARG);
  94. RTCIO_ENTER_CRITICAL();
  95. rtcio_hal_set_direction_in_sleep(rtc_io_number_get(gpio_num), mode);
  96. RTCIO_EXIT_CRITICAL();
  97. return ESP_OK;
  98. }
  99. esp_err_t rtc_gpio_pullup_en(gpio_num_t gpio_num)
  100. {
  101. RTCIO_CHECK(rtc_gpio_is_valid_gpio(gpio_num), "RTCIO number error", ESP_ERR_INVALID_ARG);
  102. RTCIO_ENTER_CRITICAL();
  103. rtcio_hal_pullup_enable(rtc_io_number_get(gpio_num));
  104. RTCIO_EXIT_CRITICAL();
  105. return ESP_OK;
  106. }
  107. esp_err_t rtc_gpio_pullup_dis(gpio_num_t gpio_num)
  108. {
  109. RTCIO_CHECK(rtc_gpio_is_valid_gpio(gpio_num), "RTCIO number error", ESP_ERR_INVALID_ARG);
  110. RTCIO_ENTER_CRITICAL();
  111. rtcio_hal_pullup_disable(rtc_io_number_get(gpio_num));
  112. RTCIO_EXIT_CRITICAL();
  113. return ESP_OK;
  114. }
  115. esp_err_t rtc_gpio_pulldown_en(gpio_num_t gpio_num)
  116. {
  117. RTCIO_CHECK(rtc_gpio_is_valid_gpio(gpio_num), "RTCIO number error", ESP_ERR_INVALID_ARG);
  118. RTCIO_ENTER_CRITICAL();
  119. rtcio_hal_pulldown_enable(rtc_io_number_get(gpio_num));
  120. RTCIO_EXIT_CRITICAL();
  121. return ESP_OK;
  122. }
  123. esp_err_t rtc_gpio_pulldown_dis(gpio_num_t gpio_num)
  124. {
  125. RTCIO_CHECK(rtc_gpio_is_valid_gpio(gpio_num), "RTCIO number error", ESP_ERR_INVALID_ARG);
  126. RTCIO_ENTER_CRITICAL();
  127. rtcio_hal_pulldown_disable(rtc_io_number_get(gpio_num));
  128. RTCIO_EXIT_CRITICAL();
  129. return ESP_OK;
  130. }
  131. esp_err_t rtc_gpio_hold_en(gpio_num_t gpio_num)
  132. {
  133. RTCIO_CHECK(rtc_gpio_is_valid_gpio(gpio_num), "RTCIO number error", ESP_ERR_INVALID_ARG);
  134. RTCIO_ENTER_CRITICAL();
  135. rtcio_hal_hold_enable(rtc_io_number_get(gpio_num));
  136. RTCIO_EXIT_CRITICAL();
  137. return ESP_OK;
  138. }
  139. esp_err_t rtc_gpio_hold_dis(gpio_num_t gpio_num)
  140. {
  141. RTCIO_CHECK(rtc_gpio_is_valid_gpio(gpio_num), "RTCIO number error", ESP_ERR_INVALID_ARG);
  142. RTCIO_ENTER_CRITICAL();
  143. rtcio_hal_hold_disable(rtc_io_number_get(gpio_num));
  144. RTCIO_EXIT_CRITICAL();
  145. return ESP_OK;
  146. }
  147. esp_err_t rtc_gpio_isolate(gpio_num_t gpio_num)
  148. {
  149. RTCIO_CHECK(rtc_gpio_is_valid_gpio(gpio_num), "RTCIO number error", ESP_ERR_INVALID_ARG);
  150. RTCIO_ENTER_CRITICAL();
  151. rtcio_hal_isolate(rtc_io_number_get(gpio_num));
  152. RTCIO_EXIT_CRITICAL();
  153. return ESP_OK;
  154. }
  155. esp_err_t rtc_gpio_force_hold_en_all(void)
  156. {
  157. RTCIO_ENTER_CRITICAL();
  158. rtcio_hal_hold_all();
  159. RTCIO_EXIT_CRITICAL();
  160. return ESP_OK;
  161. }
  162. esp_err_t rtc_gpio_force_hold_dis_all(void)
  163. {
  164. RTCIO_ENTER_CRITICAL();
  165. rtcio_hal_unhold_all();
  166. RTCIO_EXIT_CRITICAL();
  167. return ESP_OK;
  168. }
  169. esp_err_t rtc_gpio_wakeup_enable(gpio_num_t gpio_num, gpio_int_type_t intr_type)
  170. {
  171. RTCIO_CHECK(rtc_gpio_is_valid_gpio(gpio_num), "RTCIO number error", ESP_ERR_INVALID_ARG);
  172. if (intr_type == GPIO_INTR_POSEDGE || intr_type == GPIO_INTR_NEGEDGE || intr_type == GPIO_INTR_ANYEDGE) {
  173. return ESP_ERR_INVALID_ARG; // Dont support this mode.
  174. }
  175. RTCIO_ENTER_CRITICAL();
  176. rtcio_hal_wakeup_enable(rtc_io_number_get(gpio_num), intr_type);
  177. RTCIO_EXIT_CRITICAL();
  178. return ESP_OK;
  179. }
  180. esp_err_t rtc_gpio_wakeup_disable(gpio_num_t gpio_num)
  181. {
  182. RTCIO_CHECK(rtc_gpio_is_valid_gpio(gpio_num), "RTCIO number error", ESP_ERR_INVALID_ARG);
  183. RTCIO_ENTER_CRITICAL();
  184. rtcio_hal_wakeup_disable(rtc_io_number_get(gpio_num));
  185. RTCIO_EXIT_CRITICAL();
  186. return ESP_OK;
  187. }