gpio.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706
  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. #include <esp_types.h>
  15. #include "esp_err.h"
  16. #include "freertos/FreeRTOS.h"
  17. #include "driver/gpio.h"
  18. #include "driver/rtc_io.h"
  19. #include "soc/soc.h"
  20. #include "soc/periph_defs.h"
  21. #if !CONFIG_FREERTOS_UNICORE
  22. #include "esp_ipc.h"
  23. #endif
  24. #include "soc/soc_caps.h"
  25. #include "soc/gpio_periph.h"
  26. #include "esp_log.h"
  27. #include "hal/gpio_hal.h"
  28. #include "esp_rom_gpio.h"
  29. static const char *GPIO_TAG = "gpio";
  30. #define GPIO_CHECK(a, str, ret_val) \
  31. if (!(a)) { \
  32. ESP_LOGE(GPIO_TAG,"%s(%d): %s", __FUNCTION__, __LINE__, str); \
  33. return (ret_val); \
  34. }
  35. #define GPIO_ISR_CORE_ID_UNINIT (3)
  36. //default value for SOC_GPIO_SUPPORT_RTC_INDEPENDENT is 0
  37. #ifndef SOC_GPIO_SUPPORT_RTC_INDEPENDENT
  38. #define SOC_GPIO_SUPPORT_RTC_INDEPENDENT 0
  39. #endif
  40. typedef struct {
  41. gpio_isr_t fn; /*!< isr function */
  42. void *args; /*!< isr function args */
  43. } gpio_isr_func_t;
  44. // Used by the IPC call to register the interrupt service routine.
  45. typedef struct {
  46. int source; /*!< ISR source */
  47. int intr_alloc_flags; /*!< ISR alloc flag */
  48. void (*fn)(void*); /*!< ISR function */
  49. void *arg; /*!< ISR function args*/
  50. void *handle; /*!< ISR handle */
  51. esp_err_t ret;
  52. } gpio_isr_alloc_t;
  53. typedef struct {
  54. gpio_hal_context_t *gpio_hal;
  55. portMUX_TYPE gpio_spinlock;
  56. uint32_t isr_core_id;
  57. gpio_isr_func_t *gpio_isr_func;
  58. gpio_isr_handle_t gpio_isr_handle;
  59. } gpio_context_t;
  60. static gpio_hal_context_t _gpio_hal = {
  61. .dev = GPIO_HAL_GET_HW(GPIO_PORT_0)
  62. };
  63. static gpio_context_t gpio_context = {
  64. .gpio_hal = &_gpio_hal,
  65. .gpio_spinlock = portMUX_INITIALIZER_UNLOCKED,
  66. .isr_core_id = GPIO_ISR_CORE_ID_UNINIT,
  67. .gpio_isr_func = NULL,
  68. };
  69. esp_err_t gpio_pullup_en(gpio_num_t gpio_num)
  70. {
  71. GPIO_CHECK(GPIO_IS_VALID_GPIO(gpio_num), "GPIO number error", ESP_ERR_INVALID_ARG);
  72. if (!rtc_gpio_is_valid_gpio(gpio_num) || SOC_GPIO_SUPPORT_RTC_INDEPENDENT) {
  73. portENTER_CRITICAL(&gpio_context.gpio_spinlock);
  74. gpio_hal_pullup_en(gpio_context.gpio_hal, gpio_num);
  75. portEXIT_CRITICAL(&gpio_context.gpio_spinlock);
  76. } else {
  77. #if SOC_RTCIO_INPUT_OUTPUT_SUPPORTED
  78. rtc_gpio_pullup_en(gpio_num);
  79. #else
  80. abort(); // This should be eliminated as unreachable, unless a programming error has occured
  81. #endif
  82. }
  83. return ESP_OK;
  84. }
  85. esp_err_t gpio_pullup_dis(gpio_num_t gpio_num)
  86. {
  87. GPIO_CHECK(GPIO_IS_VALID_GPIO(gpio_num), "GPIO number error", ESP_ERR_INVALID_ARG);
  88. if (!rtc_gpio_is_valid_gpio(gpio_num) || SOC_GPIO_SUPPORT_RTC_INDEPENDENT) {
  89. portENTER_CRITICAL(&gpio_context.gpio_spinlock);
  90. gpio_hal_pullup_dis(gpio_context.gpio_hal, gpio_num);
  91. portEXIT_CRITICAL(&gpio_context.gpio_spinlock);
  92. } else {
  93. #if SOC_RTCIO_INPUT_OUTPUT_SUPPORTED
  94. rtc_gpio_pullup_dis(gpio_num);
  95. #else
  96. abort(); // This should be eliminated as unreachable, unless a programming error has occured
  97. #endif
  98. }
  99. return ESP_OK;
  100. }
  101. esp_err_t gpio_pulldown_en(gpio_num_t gpio_num)
  102. {
  103. GPIO_CHECK(GPIO_IS_VALID_GPIO(gpio_num), "GPIO number error", ESP_ERR_INVALID_ARG);
  104. if (!rtc_gpio_is_valid_gpio(gpio_num) || SOC_GPIO_SUPPORT_RTC_INDEPENDENT) {
  105. portENTER_CRITICAL(&gpio_context.gpio_spinlock);
  106. gpio_hal_pulldown_en(gpio_context.gpio_hal, gpio_num);
  107. portEXIT_CRITICAL(&gpio_context.gpio_spinlock);
  108. } else {
  109. #if SOC_RTCIO_INPUT_OUTPUT_SUPPORTED
  110. rtc_gpio_pulldown_en(gpio_num);
  111. #else
  112. abort(); // This should be eliminated as unreachable, unless a programming error has occured
  113. #endif
  114. }
  115. return ESP_OK;
  116. }
  117. esp_err_t gpio_pulldown_dis(gpio_num_t gpio_num)
  118. {
  119. GPIO_CHECK(GPIO_IS_VALID_GPIO(gpio_num), "GPIO number error", ESP_ERR_INVALID_ARG);
  120. if (!rtc_gpio_is_valid_gpio(gpio_num) || SOC_GPIO_SUPPORT_RTC_INDEPENDENT) {
  121. portENTER_CRITICAL(&gpio_context.gpio_spinlock);
  122. gpio_hal_pulldown_dis(gpio_context.gpio_hal, gpio_num);
  123. portEXIT_CRITICAL(&gpio_context.gpio_spinlock);
  124. } else {
  125. #if SOC_RTCIO_INPUT_OUTPUT_SUPPORTED
  126. rtc_gpio_pulldown_dis(gpio_num);
  127. #else
  128. abort(); // This should be eliminated as unreachable, unless a programming error has occured
  129. #endif
  130. }
  131. return ESP_OK;
  132. }
  133. esp_err_t gpio_set_intr_type(gpio_num_t gpio_num, gpio_int_type_t intr_type)
  134. {
  135. GPIO_CHECK(GPIO_IS_VALID_GPIO(gpio_num), "GPIO number error", ESP_ERR_INVALID_ARG);
  136. GPIO_CHECK(intr_type < GPIO_INTR_MAX, "GPIO interrupt type error", ESP_ERR_INVALID_ARG);
  137. portENTER_CRITICAL(&gpio_context.gpio_spinlock);
  138. gpio_hal_set_intr_type(gpio_context.gpio_hal, gpio_num, intr_type);
  139. portEXIT_CRITICAL(&gpio_context.gpio_spinlock);
  140. return ESP_OK;
  141. }
  142. static esp_err_t gpio_intr_enable_on_core(gpio_num_t gpio_num, uint32_t core_id)
  143. {
  144. GPIO_CHECK(GPIO_IS_VALID_GPIO(gpio_num), "GPIO number error", ESP_ERR_INVALID_ARG);
  145. gpio_hal_intr_enable_on_core(gpio_context.gpio_hal, gpio_num, core_id);
  146. return ESP_OK;
  147. }
  148. esp_err_t gpio_intr_enable(gpio_num_t gpio_num)
  149. {
  150. GPIO_CHECK(GPIO_IS_VALID_GPIO(gpio_num), "GPIO number error", ESP_ERR_INVALID_ARG);
  151. portENTER_CRITICAL(&gpio_context.gpio_spinlock);
  152. if(gpio_context.isr_core_id == GPIO_ISR_CORE_ID_UNINIT) {
  153. gpio_context.isr_core_id = xPortGetCoreID();
  154. }
  155. portEXIT_CRITICAL(&gpio_context.gpio_spinlock);
  156. return gpio_intr_enable_on_core (gpio_num, gpio_context.isr_core_id);
  157. }
  158. esp_err_t gpio_intr_disable(gpio_num_t gpio_num)
  159. {
  160. GPIO_CHECK(GPIO_IS_VALID_GPIO(gpio_num), "GPIO number error", ESP_ERR_INVALID_ARG);
  161. gpio_hal_intr_disable(gpio_context.gpio_hal, gpio_num);
  162. return ESP_OK;
  163. }
  164. static esp_err_t gpio_input_disable(gpio_num_t gpio_num)
  165. {
  166. GPIO_CHECK(GPIO_IS_VALID_GPIO(gpio_num), "GPIO number error", ESP_ERR_INVALID_ARG);
  167. gpio_hal_input_disable(gpio_context.gpio_hal, gpio_num);
  168. return ESP_OK;
  169. }
  170. static esp_err_t gpio_input_enable(gpio_num_t gpio_num)
  171. {
  172. GPIO_CHECK(GPIO_IS_VALID_GPIO(gpio_num), "GPIO number error", ESP_ERR_INVALID_ARG);
  173. gpio_hal_input_enable(gpio_context.gpio_hal, gpio_num);
  174. return ESP_OK;
  175. }
  176. static esp_err_t gpio_output_disable(gpio_num_t gpio_num)
  177. {
  178. GPIO_CHECK(GPIO_IS_VALID_GPIO(gpio_num), "GPIO number error", ESP_ERR_INVALID_ARG);
  179. gpio_hal_output_disable(gpio_context.gpio_hal, gpio_num);
  180. return ESP_OK;
  181. }
  182. static esp_err_t gpio_output_enable(gpio_num_t gpio_num)
  183. {
  184. GPIO_CHECK(GPIO_IS_VALID_OUTPUT_GPIO(gpio_num), "GPIO output gpio_num error", ESP_ERR_INVALID_ARG);
  185. gpio_hal_output_enable(gpio_context.gpio_hal, gpio_num);
  186. esp_rom_gpio_connect_out_signal(gpio_num, SIG_GPIO_OUT_IDX, false, false);
  187. return ESP_OK;
  188. }
  189. static esp_err_t gpio_od_disable(gpio_num_t gpio_num)
  190. {
  191. GPIO_CHECK(GPIO_IS_VALID_GPIO(gpio_num), "GPIO number error", ESP_ERR_INVALID_ARG);
  192. gpio_hal_od_disable(gpio_context.gpio_hal, gpio_num);
  193. return ESP_OK;
  194. }
  195. static esp_err_t gpio_od_enable(gpio_num_t gpio_num)
  196. {
  197. GPIO_CHECK(GPIO_IS_VALID_GPIO(gpio_num), "GPIO number error", ESP_ERR_INVALID_ARG);
  198. gpio_hal_od_enable(gpio_context.gpio_hal, gpio_num);
  199. return ESP_OK;
  200. }
  201. esp_err_t gpio_set_level(gpio_num_t gpio_num, uint32_t level)
  202. {
  203. GPIO_CHECK(GPIO_IS_VALID_OUTPUT_GPIO(gpio_num), "GPIO output gpio_num error", ESP_ERR_INVALID_ARG);
  204. gpio_hal_set_level(gpio_context.gpio_hal, gpio_num, level);
  205. return ESP_OK;
  206. }
  207. int gpio_get_level(gpio_num_t gpio_num)
  208. {
  209. return gpio_hal_get_level(gpio_context.gpio_hal, gpio_num);
  210. }
  211. esp_err_t gpio_set_pull_mode(gpio_num_t gpio_num, gpio_pull_mode_t pull)
  212. {
  213. GPIO_CHECK(GPIO_IS_VALID_GPIO(gpio_num), "GPIO number error", ESP_ERR_INVALID_ARG);
  214. GPIO_CHECK(pull <= GPIO_FLOATING, "GPIO pull mode error", ESP_ERR_INVALID_ARG);
  215. esp_err_t ret = ESP_OK;
  216. switch (pull) {
  217. case GPIO_PULLUP_ONLY:
  218. gpio_pulldown_dis(gpio_num);
  219. gpio_pullup_en(gpio_num);
  220. break;
  221. case GPIO_PULLDOWN_ONLY:
  222. gpio_pulldown_en(gpio_num);
  223. gpio_pullup_dis(gpio_num);
  224. break;
  225. case GPIO_PULLUP_PULLDOWN:
  226. gpio_pulldown_en(gpio_num);
  227. gpio_pullup_en(gpio_num);
  228. break;
  229. case GPIO_FLOATING:
  230. gpio_pulldown_dis(gpio_num);
  231. gpio_pullup_dis(gpio_num);
  232. break;
  233. default:
  234. ESP_LOGE(GPIO_TAG, "Unknown pull up/down mode,gpio_num=%u,pull=%u", gpio_num, pull);
  235. ret = ESP_ERR_INVALID_ARG;
  236. break;
  237. }
  238. return ret;
  239. }
  240. esp_err_t gpio_set_direction(gpio_num_t gpio_num, gpio_mode_t mode)
  241. {
  242. GPIO_CHECK(GPIO_IS_VALID_GPIO(gpio_num), "GPIO number error", ESP_ERR_INVALID_ARG);
  243. if ((GPIO_IS_VALID_OUTPUT_GPIO(gpio_num) != true) && (mode & GPIO_MODE_DEF_OUTPUT)) {
  244. ESP_LOGE(GPIO_TAG, "io_num=%d can only be input", gpio_num);
  245. return ESP_ERR_INVALID_ARG;
  246. }
  247. esp_err_t ret = ESP_OK;
  248. if (mode & GPIO_MODE_DEF_INPUT) {
  249. gpio_input_enable(gpio_num);
  250. } else {
  251. gpio_input_disable(gpio_num);
  252. }
  253. if (mode & GPIO_MODE_DEF_OUTPUT) {
  254. gpio_output_enable(gpio_num);
  255. } else {
  256. gpio_output_disable(gpio_num);
  257. }
  258. if (mode & GPIO_MODE_DEF_OD) {
  259. gpio_od_enable(gpio_num);
  260. } else {
  261. gpio_od_disable(gpio_num);
  262. }
  263. return ret;
  264. }
  265. esp_err_t gpio_config(const gpio_config_t *pGPIOConfig)
  266. {
  267. uint64_t gpio_pin_mask = (pGPIOConfig->pin_bit_mask);
  268. uint32_t io_reg = 0;
  269. uint32_t io_num = 0;
  270. uint8_t input_en = 0;
  271. uint8_t output_en = 0;
  272. uint8_t od_en = 0;
  273. uint8_t pu_en = 0;
  274. uint8_t pd_en = 0;
  275. if (pGPIOConfig->pin_bit_mask == 0 ||
  276. pGPIOConfig->pin_bit_mask & ~SOC_GPIO_VALID_GPIO_MASK) {
  277. ESP_LOGE(GPIO_TAG, "GPIO_PIN mask error ");
  278. return ESP_ERR_INVALID_ARG;
  279. }
  280. if (pGPIOConfig->mode & GPIO_MODE_DEF_OUTPUT &&
  281. pGPIOConfig->pin_bit_mask & ~SOC_GPIO_VALID_OUTPUT_GPIO_MASK) {
  282. ESP_LOGE(GPIO_TAG, "GPIO can only be used as input mode");
  283. return ESP_ERR_INVALID_ARG;
  284. }
  285. do {
  286. io_reg = GPIO_PIN_MUX_REG[io_num];
  287. if (((gpio_pin_mask >> io_num) & BIT(0))) {
  288. assert(io_reg != (intptr_t)NULL);
  289. #if SOC_RTCIO_INPUT_OUTPUT_SUPPORTED
  290. if (rtc_gpio_is_valid_gpio(io_num)) {
  291. rtc_gpio_deinit(io_num);
  292. }
  293. #endif
  294. if ((pGPIOConfig->mode) & GPIO_MODE_DEF_INPUT) {
  295. input_en = 1;
  296. gpio_input_enable(io_num);
  297. } else {
  298. gpio_input_disable(io_num);
  299. }
  300. if ((pGPIOConfig->mode) & GPIO_MODE_DEF_OD) {
  301. od_en = 1;
  302. gpio_od_enable(io_num);
  303. } else {
  304. gpio_od_disable(io_num);
  305. }
  306. if ((pGPIOConfig->mode) & GPIO_MODE_DEF_OUTPUT) {
  307. output_en = 1;
  308. gpio_output_enable(io_num);
  309. } else {
  310. gpio_output_disable(io_num);
  311. }
  312. if (pGPIOConfig->pull_up_en) {
  313. pu_en = 1;
  314. gpio_pullup_en(io_num);
  315. } else {
  316. gpio_pullup_dis(io_num);
  317. }
  318. if (pGPIOConfig->pull_down_en) {
  319. pd_en = 1;
  320. gpio_pulldown_en(io_num);
  321. } else {
  322. gpio_pulldown_dis(io_num);
  323. }
  324. ESP_LOGI(GPIO_TAG, "GPIO[%d]| InputEn: %d| OutputEn: %d| OpenDrain: %d| Pullup: %d| Pulldown: %d| Intr:%d ", io_num, input_en, output_en, od_en, pu_en, pd_en, pGPIOConfig->intr_type);
  325. gpio_set_intr_type(io_num, pGPIOConfig->intr_type);
  326. if (pGPIOConfig->intr_type) {
  327. gpio_intr_enable(io_num);
  328. } else {
  329. gpio_intr_disable(io_num);
  330. }
  331. PIN_FUNC_SELECT(io_reg, PIN_FUNC_GPIO); /*function number 2 is GPIO_FUNC for each pin */
  332. }
  333. io_num++;
  334. } while (io_num < GPIO_PIN_COUNT);
  335. return ESP_OK;
  336. }
  337. esp_err_t gpio_reset_pin(gpio_num_t gpio_num)
  338. {
  339. assert(gpio_num >= 0 && GPIO_IS_VALID_GPIO(gpio_num));
  340. gpio_config_t cfg = {
  341. .pin_bit_mask = BIT64(gpio_num),
  342. .mode = GPIO_MODE_DISABLE,
  343. //for powersave reasons, the GPIO should not be floating, select pullup
  344. .pull_up_en = true,
  345. .pull_down_en = false,
  346. .intr_type = GPIO_INTR_DISABLE,
  347. };
  348. gpio_config(&cfg);
  349. return ESP_OK;
  350. }
  351. static inline void IRAM_ATTR gpio_isr_loop(uint32_t status, const uint32_t gpio_num_start)
  352. {
  353. while (status) {
  354. int nbit = __builtin_ffs(status) - 1;
  355. status &= ~(1 << nbit);
  356. int gpio_num = gpio_num_start + nbit;
  357. if (gpio_context.gpio_isr_func[gpio_num].fn != NULL) {
  358. gpio_context.gpio_isr_func[gpio_num].fn(gpio_context.gpio_isr_func[gpio_num].args);
  359. }
  360. }
  361. }
  362. static void IRAM_ATTR gpio_intr_service(void *arg)
  363. {
  364. //GPIO intr process
  365. if (gpio_context.gpio_isr_func == NULL) {
  366. return;
  367. }
  368. //read status to get interrupt status for GPIO0-31
  369. uint32_t gpio_intr_status;
  370. gpio_hal_get_intr_status(gpio_context.gpio_hal, gpio_context.isr_core_id, &gpio_intr_status);
  371. if (gpio_intr_status) {
  372. gpio_isr_loop(gpio_intr_status, 0);
  373. gpio_hal_clear_intr_status(gpio_context.gpio_hal, gpio_intr_status);
  374. }
  375. //read status1 to get interrupt status for GPIO32-39
  376. uint32_t gpio_intr_status_h;
  377. gpio_hal_get_intr_status_high(gpio_context.gpio_hal, gpio_context.isr_core_id, &gpio_intr_status_h);
  378. if (gpio_intr_status_h) {
  379. gpio_isr_loop(gpio_intr_status_h, 32);
  380. gpio_hal_clear_intr_status_high(gpio_context.gpio_hal, gpio_intr_status_h);
  381. }
  382. }
  383. esp_err_t gpio_install_isr_service(int intr_alloc_flags)
  384. {
  385. GPIO_CHECK(gpio_context.gpio_isr_func == NULL, "GPIO isr service already installed", ESP_ERR_INVALID_STATE);
  386. esp_err_t ret;
  387. portENTER_CRITICAL(&gpio_context.gpio_spinlock);
  388. gpio_context.gpio_isr_func = (gpio_isr_func_t *) calloc(GPIO_NUM_MAX, sizeof(gpio_isr_func_t));
  389. portEXIT_CRITICAL(&gpio_context.gpio_spinlock);
  390. if (gpio_context.gpio_isr_func == NULL) {
  391. ret = ESP_ERR_NO_MEM;
  392. } else {
  393. ret = gpio_isr_register(gpio_intr_service, NULL, intr_alloc_flags, &gpio_context.gpio_isr_handle);
  394. }
  395. return ret;
  396. }
  397. esp_err_t gpio_isr_handler_add(gpio_num_t gpio_num, gpio_isr_t isr_handler, void *args)
  398. {
  399. GPIO_CHECK(gpio_context.gpio_isr_func != NULL, "GPIO isr service is not installed, call gpio_install_isr_service() first", ESP_ERR_INVALID_STATE);
  400. GPIO_CHECK(GPIO_IS_VALID_GPIO(gpio_num), "GPIO number error", ESP_ERR_INVALID_ARG);
  401. portENTER_CRITICAL(&gpio_context.gpio_spinlock);
  402. gpio_intr_disable(gpio_num);
  403. if (gpio_context.gpio_isr_func) {
  404. gpio_context.gpio_isr_func[gpio_num].fn = isr_handler;
  405. gpio_context.gpio_isr_func[gpio_num].args = args;
  406. }
  407. gpio_intr_enable_on_core (gpio_num, esp_intr_get_cpu(gpio_context.gpio_isr_handle));
  408. portEXIT_CRITICAL(&gpio_context.gpio_spinlock);
  409. return ESP_OK;
  410. }
  411. esp_err_t gpio_isr_handler_remove(gpio_num_t gpio_num)
  412. {
  413. GPIO_CHECK(gpio_context.gpio_isr_func != NULL, "GPIO isr service is not installed, call gpio_install_isr_service() first", ESP_ERR_INVALID_STATE);
  414. GPIO_CHECK(GPIO_IS_VALID_GPIO(gpio_num), "GPIO number error", ESP_ERR_INVALID_ARG);
  415. portENTER_CRITICAL(&gpio_context.gpio_spinlock);
  416. gpio_intr_disable(gpio_num);
  417. if (gpio_context.gpio_isr_func) {
  418. gpio_context.gpio_isr_func[gpio_num].fn = NULL;
  419. gpio_context.gpio_isr_func[gpio_num].args = NULL;
  420. }
  421. portEXIT_CRITICAL(&gpio_context.gpio_spinlock);
  422. return ESP_OK;
  423. }
  424. void gpio_uninstall_isr_service(void)
  425. {
  426. if (gpio_context.gpio_isr_func == NULL) {
  427. return;
  428. }
  429. portENTER_CRITICAL(&gpio_context.gpio_spinlock);
  430. esp_intr_free(gpio_context.gpio_isr_handle);
  431. free(gpio_context.gpio_isr_func);
  432. gpio_context.gpio_isr_func = NULL;
  433. gpio_context.isr_core_id = GPIO_ISR_CORE_ID_UNINIT;
  434. portEXIT_CRITICAL(&gpio_context.gpio_spinlock);
  435. return;
  436. }
  437. static void gpio_isr_register_on_core_static(void *param)
  438. {
  439. gpio_isr_alloc_t *p = (gpio_isr_alloc_t *)param;
  440. //We need to check the return value.
  441. p->ret = esp_intr_alloc(p->source, p->intr_alloc_flags, p->fn, p->arg, p->handle);
  442. }
  443. esp_err_t gpio_isr_register(void (*fn)(void *), void *arg, int intr_alloc_flags, gpio_isr_handle_t *handle)
  444. {
  445. GPIO_CHECK(fn, "GPIO ISR null", ESP_ERR_INVALID_ARG);
  446. gpio_isr_alloc_t p;
  447. p.source = ETS_GPIO_INTR_SOURCE;
  448. p.intr_alloc_flags = intr_alloc_flags;
  449. p.fn = fn;
  450. p.arg = arg;
  451. p.handle = handle;
  452. portENTER_CRITICAL(&gpio_context.gpio_spinlock);
  453. if(gpio_context.isr_core_id == GPIO_ISR_CORE_ID_UNINIT) {
  454. gpio_context.isr_core_id = xPortGetCoreID();
  455. }
  456. portEXIT_CRITICAL(&gpio_context.gpio_spinlock);
  457. esp_err_t ret;
  458. #if CONFIG_FREERTOS_UNICORE
  459. gpio_isr_register_on_core_static(&p);
  460. ret = ESP_OK;
  461. #else /* CONFIG_FREERTOS_UNICORE */
  462. ret = esp_ipc_call_blocking(gpio_context.isr_core_id, gpio_isr_register_on_core_static, (void *)&p);
  463. #endif /* !CONFIG_FREERTOS_UNICORE */
  464. if(ret != ESP_OK || p.ret != ESP_OK) {
  465. return ESP_ERR_NOT_FOUND;
  466. }
  467. return ESP_OK;
  468. }
  469. esp_err_t gpio_wakeup_enable(gpio_num_t gpio_num, gpio_int_type_t intr_type)
  470. {
  471. GPIO_CHECK(GPIO_IS_VALID_GPIO(gpio_num), "GPIO number error", ESP_ERR_INVALID_ARG);
  472. esp_err_t ret = ESP_OK;
  473. if ((intr_type == GPIO_INTR_LOW_LEVEL) || (intr_type == GPIO_INTR_HIGH_LEVEL)) {
  474. if (rtc_gpio_is_valid_gpio(gpio_num)) {
  475. ret = rtc_gpio_wakeup_enable(gpio_num, intr_type);
  476. }
  477. portENTER_CRITICAL(&gpio_context.gpio_spinlock);
  478. gpio_hal_wakeup_enable(gpio_context.gpio_hal, gpio_num, intr_type);
  479. portEXIT_CRITICAL(&gpio_context.gpio_spinlock);
  480. } else {
  481. ESP_LOGE(GPIO_TAG, "GPIO wakeup only supports level mode, but edge mode set. gpio_num:%u", gpio_num);
  482. ret = ESP_ERR_INVALID_ARG;
  483. }
  484. return ret;
  485. }
  486. esp_err_t gpio_wakeup_disable(gpio_num_t gpio_num)
  487. {
  488. GPIO_CHECK(GPIO_IS_VALID_GPIO(gpio_num), "GPIO number error", ESP_ERR_INVALID_ARG);
  489. esp_err_t ret = ESP_OK;
  490. if (rtc_gpio_is_valid_gpio(gpio_num)) {
  491. ret = rtc_gpio_wakeup_disable(gpio_num);
  492. }
  493. portENTER_CRITICAL(&gpio_context.gpio_spinlock);
  494. gpio_hal_wakeup_disable(gpio_context.gpio_hal, gpio_num);
  495. portEXIT_CRITICAL(&gpio_context.gpio_spinlock);
  496. return ret;
  497. }
  498. esp_err_t gpio_set_drive_capability(gpio_num_t gpio_num, gpio_drive_cap_t strength)
  499. {
  500. GPIO_CHECK(GPIO_IS_VALID_OUTPUT_GPIO(gpio_num), "GPIO number error", ESP_ERR_INVALID_ARG);
  501. GPIO_CHECK(strength < GPIO_DRIVE_CAP_MAX, "GPIO drive capability error", ESP_ERR_INVALID_ARG);
  502. esp_err_t ret = ESP_OK;
  503. if (!rtc_gpio_is_valid_gpio(gpio_num) || SOC_GPIO_SUPPORT_RTC_INDEPENDENT) {
  504. portENTER_CRITICAL(&gpio_context.gpio_spinlock);
  505. gpio_hal_set_drive_capability(gpio_context.gpio_hal, gpio_num, strength);
  506. portEXIT_CRITICAL(&gpio_context.gpio_spinlock);
  507. } else {
  508. #if SOC_RTCIO_INPUT_OUTPUT_SUPPORTED
  509. ret = rtc_gpio_set_drive_capability(gpio_num, strength);
  510. #else
  511. abort(); // This should be eliminated as unreachable, unless a programming error has occured
  512. #endif
  513. }
  514. return ret;
  515. }
  516. esp_err_t gpio_get_drive_capability(gpio_num_t gpio_num, gpio_drive_cap_t *strength)
  517. {
  518. GPIO_CHECK(GPIO_IS_VALID_OUTPUT_GPIO(gpio_num), "GPIO number error", ESP_ERR_INVALID_ARG);
  519. GPIO_CHECK(strength != NULL, "GPIO drive capability pointer error", ESP_ERR_INVALID_ARG);
  520. esp_err_t ret = ESP_OK;
  521. if (!rtc_gpio_is_valid_gpio(gpio_num) || SOC_GPIO_SUPPORT_RTC_INDEPENDENT) {
  522. portENTER_CRITICAL(&gpio_context.gpio_spinlock);
  523. gpio_hal_get_drive_capability(gpio_context.gpio_hal, gpio_num, strength);
  524. portEXIT_CRITICAL(&gpio_context.gpio_spinlock);
  525. } else {
  526. #if SOC_RTCIO_INPUT_OUTPUT_SUPPORTED
  527. ret = rtc_gpio_get_drive_capability(gpio_num, strength);
  528. #else
  529. abort(); // This should be eliminated as unreachable, unless a programming error has occured
  530. #endif
  531. }
  532. return ret;
  533. }
  534. esp_err_t gpio_hold_en(gpio_num_t gpio_num)
  535. {
  536. GPIO_CHECK(GPIO_IS_VALID_OUTPUT_GPIO(gpio_num), "Only output-capable GPIO support this function", ESP_ERR_NOT_SUPPORTED);
  537. int ret = ESP_OK;
  538. if (rtc_gpio_is_valid_gpio(gpio_num)) {
  539. ret = rtc_gpio_hold_en(gpio_num);
  540. } else if (GPIO_HOLD_MASK[gpio_num]) {
  541. portENTER_CRITICAL(&gpio_context.gpio_spinlock);
  542. gpio_hal_hold_en(gpio_context.gpio_hal, gpio_num);
  543. portEXIT_CRITICAL(&gpio_context.gpio_spinlock);
  544. } else {
  545. ret = ESP_ERR_NOT_SUPPORTED;
  546. }
  547. return ret;
  548. }
  549. esp_err_t gpio_hold_dis(gpio_num_t gpio_num)
  550. {
  551. GPIO_CHECK(GPIO_IS_VALID_OUTPUT_GPIO(gpio_num), "Only output-capable GPIO support this function", ESP_ERR_NOT_SUPPORTED);
  552. int ret = ESP_OK;
  553. if (rtc_gpio_is_valid_gpio(gpio_num)) {
  554. ret = rtc_gpio_hold_dis(gpio_num);
  555. }else if (GPIO_HOLD_MASK[gpio_num]) {
  556. portENTER_CRITICAL(&gpio_context.gpio_spinlock);
  557. gpio_hal_hold_dis(gpio_context.gpio_hal, gpio_num);
  558. portEXIT_CRITICAL(&gpio_context.gpio_spinlock);
  559. } else {
  560. ret = ESP_ERR_NOT_SUPPORTED;
  561. }
  562. return ret;
  563. }
  564. void gpio_deep_sleep_hold_en(void)
  565. {
  566. portENTER_CRITICAL(&gpio_context.gpio_spinlock);
  567. gpio_hal_deep_sleep_hold_en(gpio_context.gpio_hal);
  568. portEXIT_CRITICAL(&gpio_context.gpio_spinlock);
  569. }
  570. void gpio_deep_sleep_hold_dis(void)
  571. {
  572. portENTER_CRITICAL(&gpio_context.gpio_spinlock);
  573. gpio_hal_deep_sleep_hold_dis(gpio_context.gpio_hal);
  574. portEXIT_CRITICAL(&gpio_context.gpio_spinlock);
  575. }
  576. #if SOC_GPIO_SUPPORT_FORCE_HOLD
  577. esp_err_t gpio_force_hold_all()
  578. {
  579. rtc_gpio_force_hold_all();
  580. portENTER_CRITICAL(&gpio_context.gpio_spinlock);
  581. gpio_hal_force_hold_all(gpio_context.gpio_hal);
  582. portEXIT_CRITICAL(&gpio_context.gpio_spinlock);
  583. return ESP_OK;
  584. }
  585. esp_err_t gpio_force_unhold_all()
  586. {
  587. rtc_gpio_force_hold_dis_all();
  588. portENTER_CRITICAL(&gpio_context.gpio_spinlock);
  589. gpio_hal_force_unhold_all(gpio_context.gpio_hal);
  590. portEXIT_CRITICAL(&gpio_context.gpio_spinlock);
  591. return ESP_OK;
  592. }
  593. #endif
  594. void gpio_iomux_in(uint32_t gpio, uint32_t signal_idx)
  595. {
  596. gpio_hal_iomux_in(gpio_context.gpio_hal, gpio, signal_idx);
  597. }
  598. void gpio_iomux_out(uint8_t gpio_num, int func, bool oen_inv)
  599. {
  600. gpio_hal_iomux_out(gpio_context.gpio_hal, gpio_num, func, (uint32_t)oen_inv);
  601. }