gpio.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613
  1. // Copyright 2015-2016 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. // http://www.apache.org/licenses/LICENSE-2.0
  7. //
  8. // Unless required by applicable law or agreed to in writing, software
  9. // distributed under the License is distributed on an "AS IS" BASIS,
  10. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  11. // See the License for the specific language governing permissions and
  12. // limitations under the License.
  13. #include <esp_types.h>
  14. #include "esp_err.h"
  15. #include "freertos/FreeRTOS.h"
  16. #include "freertos/xtensa_api.h"
  17. #include "driver/gpio.h"
  18. #include "driver/rtc_io.h"
  19. #include "soc/soc.h"
  20. #include "soc/gpio_periph.h"
  21. #include "esp_log.h"
  22. #if !CONFIG_FREERTOS_UNICORE
  23. #include "esp_ipc.h"
  24. #endif
  25. #define GPIO_CHECK(a, str, ret_val) \
  26. if (!(a)) { \
  27. ESP_LOGE(GPIO_TAG,"%s(%d): %s", __FUNCTION__, __LINE__, str); \
  28. return (ret_val); \
  29. }
  30. #define GPIO_ISR_CORE_ID_UNINIT (3)
  31. typedef struct {
  32. gpio_isr_t fn; /*!< isr function */
  33. void* args; /*!< isr function args */
  34. } gpio_isr_func_t;
  35. // Used by the IPC call to register the interrupt service routine.
  36. typedef struct {
  37. int source; /*!< ISR source */
  38. int intr_alloc_flags; /*!< ISR alloc flag */
  39. void (*fn)(void*); /*!< ISR function */
  40. void *arg; /*!< ISR function args*/
  41. void *handle; /*!< ISR handle */
  42. esp_err_t ret;
  43. } gpio_isr_alloc_t;
  44. static const char* GPIO_TAG = "gpio";
  45. static gpio_isr_func_t* gpio_isr_func = NULL;
  46. static gpio_isr_handle_t gpio_isr_handle;
  47. static uint32_t isr_core_id = GPIO_ISR_CORE_ID_UNINIT;
  48. static portMUX_TYPE gpio_spinlock = portMUX_INITIALIZER_UNLOCKED;
  49. esp_err_t gpio_pullup_en(gpio_num_t gpio_num)
  50. {
  51. GPIO_CHECK(GPIO_IS_VALID_GPIO(gpio_num), "GPIO number error", ESP_ERR_INVALID_ARG);
  52. if (RTC_GPIO_IS_VALID_GPIO(gpio_num)) {
  53. rtc_gpio_pullup_en(gpio_num);
  54. } else {
  55. REG_SET_BIT(GPIO_PIN_MUX_REG[gpio_num], FUN_PU);
  56. }
  57. return ESP_OK;
  58. }
  59. esp_err_t gpio_pullup_dis(gpio_num_t gpio_num)
  60. {
  61. GPIO_CHECK(GPIO_IS_VALID_GPIO(gpio_num), "GPIO number error", ESP_ERR_INVALID_ARG);
  62. if (RTC_GPIO_IS_VALID_GPIO(gpio_num)) {
  63. rtc_gpio_pullup_dis(gpio_num);
  64. } else {
  65. REG_CLR_BIT(GPIO_PIN_MUX_REG[gpio_num], FUN_PU);
  66. }
  67. return ESP_OK;
  68. }
  69. esp_err_t gpio_pulldown_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)) {
  73. rtc_gpio_pulldown_en(gpio_num);
  74. } else {
  75. REG_SET_BIT(GPIO_PIN_MUX_REG[gpio_num], FUN_PD);
  76. }
  77. return ESP_OK;
  78. }
  79. esp_err_t gpio_pulldown_dis(gpio_num_t gpio_num)
  80. {
  81. GPIO_CHECK(GPIO_IS_VALID_GPIO(gpio_num), "GPIO number error", ESP_ERR_INVALID_ARG);
  82. if (RTC_GPIO_IS_VALID_GPIO(gpio_num)) {
  83. rtc_gpio_pulldown_dis(gpio_num);
  84. } else {
  85. REG_CLR_BIT(GPIO_PIN_MUX_REG[gpio_num], FUN_PD);
  86. }
  87. return ESP_OK;
  88. }
  89. esp_err_t gpio_set_intr_type(gpio_num_t gpio_num, gpio_int_type_t intr_type)
  90. {
  91. GPIO_CHECK(GPIO_IS_VALID_GPIO(gpio_num), "GPIO number error", ESP_ERR_INVALID_ARG);
  92. GPIO_CHECK(intr_type < GPIO_INTR_MAX, "GPIO interrupt type error", ESP_ERR_INVALID_ARG);
  93. GPIO.pin[gpio_num].int_type = intr_type;
  94. return ESP_OK;
  95. }
  96. static void gpio_intr_status_clr(gpio_num_t gpio_num)
  97. {
  98. if (gpio_num < 32) {
  99. GPIO.status_w1tc = BIT(gpio_num);
  100. } else {
  101. GPIO.status1_w1tc.intr_st = BIT(gpio_num - 32);
  102. }
  103. }
  104. static esp_err_t gpio_intr_enable_on_core (gpio_num_t gpio_num, uint32_t core_id)
  105. {
  106. gpio_intr_status_clr(gpio_num);
  107. if (core_id == 0) {
  108. GPIO.pin[gpio_num].int_ena = GPIO_PRO_CPU_INTR_ENA; //enable pro cpu intr
  109. } else {
  110. GPIO.pin[gpio_num].int_ena = GPIO_APP_CPU_INTR_ENA; //enable pro cpu intr
  111. }
  112. return ESP_OK;
  113. }
  114. esp_err_t gpio_intr_enable(gpio_num_t gpio_num)
  115. {
  116. GPIO_CHECK(GPIO_IS_VALID_GPIO(gpio_num), "GPIO number error", ESP_ERR_INVALID_ARG);
  117. portENTER_CRITICAL(&gpio_spinlock);
  118. if(isr_core_id == GPIO_ISR_CORE_ID_UNINIT) {
  119. isr_core_id = xPortGetCoreID();
  120. }
  121. portEXIT_CRITICAL(&gpio_spinlock);
  122. return gpio_intr_enable_on_core (gpio_num, isr_core_id);
  123. }
  124. esp_err_t gpio_intr_disable(gpio_num_t gpio_num)
  125. {
  126. GPIO_CHECK(GPIO_IS_VALID_GPIO(gpio_num), "GPIO number error", ESP_ERR_INVALID_ARG);
  127. GPIO.pin[gpio_num].int_ena = 0; //disable GPIO intr
  128. gpio_intr_status_clr(gpio_num);
  129. return ESP_OK;
  130. }
  131. static esp_err_t gpio_output_disable(gpio_num_t gpio_num)
  132. {
  133. GPIO_CHECK(GPIO_IS_VALID_GPIO(gpio_num), "GPIO number error", ESP_ERR_INVALID_ARG);
  134. if (gpio_num < 32) {
  135. GPIO.enable_w1tc = (0x1 << gpio_num);
  136. } else {
  137. GPIO.enable1_w1tc.data = (0x1 << (gpio_num - 32));
  138. }
  139. // Ensure no other output signal is routed via GPIO matrix to this pin
  140. REG_WRITE(GPIO_FUNC0_OUT_SEL_CFG_REG + (gpio_num * 4),
  141. SIG_GPIO_OUT_IDX);
  142. return ESP_OK;
  143. }
  144. static esp_err_t gpio_output_enable(gpio_num_t gpio_num)
  145. {
  146. GPIO_CHECK(GPIO_IS_VALID_OUTPUT_GPIO(gpio_num), "GPIO output gpio_num error", ESP_ERR_INVALID_ARG);
  147. if (gpio_num < 32) {
  148. GPIO.enable_w1ts = (0x1 << gpio_num);
  149. } else {
  150. GPIO.enable1_w1ts.data = (0x1 << (gpio_num - 32));
  151. }
  152. gpio_matrix_out(gpio_num, SIG_GPIO_OUT_IDX, false, false);
  153. return ESP_OK;
  154. }
  155. esp_err_t gpio_set_level(gpio_num_t gpio_num, uint32_t level)
  156. {
  157. GPIO_CHECK(GPIO_IS_VALID_OUTPUT_GPIO(gpio_num), "GPIO output gpio_num error", ESP_ERR_INVALID_ARG);
  158. if (level) {
  159. if (gpio_num < 32) {
  160. GPIO.out_w1ts = (1 << gpio_num);
  161. } else {
  162. GPIO.out1_w1ts.data = (1 << (gpio_num - 32));
  163. }
  164. } else {
  165. if (gpio_num < 32) {
  166. GPIO.out_w1tc = (1 << gpio_num);
  167. } else {
  168. GPIO.out1_w1tc.data = (1 << (gpio_num - 32));
  169. }
  170. }
  171. return ESP_OK;
  172. }
  173. int gpio_get_level(gpio_num_t gpio_num)
  174. {
  175. if (gpio_num < 32) {
  176. return (GPIO.in >> gpio_num) & 0x1;
  177. } else {
  178. return (GPIO.in1.data >> (gpio_num - 32)) & 0x1;
  179. }
  180. }
  181. esp_err_t gpio_set_pull_mode(gpio_num_t gpio_num, gpio_pull_mode_t pull)
  182. {
  183. GPIO_CHECK(GPIO_IS_VALID_GPIO(gpio_num), "GPIO number error", ESP_ERR_INVALID_ARG);
  184. GPIO_CHECK(pull <= GPIO_FLOATING, "GPIO pull mode error", ESP_ERR_INVALID_ARG);
  185. esp_err_t ret = ESP_OK;
  186. switch (pull) {
  187. case GPIO_PULLUP_ONLY:
  188. gpio_pulldown_dis(gpio_num);
  189. gpio_pullup_en(gpio_num);
  190. break;
  191. case GPIO_PULLDOWN_ONLY:
  192. gpio_pulldown_en(gpio_num);
  193. gpio_pullup_dis(gpio_num);
  194. break;
  195. case GPIO_PULLUP_PULLDOWN:
  196. gpio_pulldown_en(gpio_num);
  197. gpio_pullup_en(gpio_num);
  198. break;
  199. case GPIO_FLOATING:
  200. gpio_pulldown_dis(gpio_num);
  201. gpio_pullup_dis(gpio_num);
  202. break;
  203. default:
  204. ESP_LOGE(GPIO_TAG, "Unknown pull up/down mode,gpio_num=%u,pull=%u", gpio_num, pull);
  205. ret = ESP_ERR_INVALID_ARG;
  206. break;
  207. }
  208. return ret;
  209. }
  210. esp_err_t gpio_set_direction(gpio_num_t gpio_num, gpio_mode_t mode)
  211. {
  212. GPIO_CHECK(GPIO_IS_VALID_GPIO(gpio_num), "GPIO number error", ESP_ERR_INVALID_ARG);
  213. if (gpio_num >= 34 && (mode & GPIO_MODE_DEF_OUTPUT)) {
  214. ESP_LOGE(GPIO_TAG, "io_num=%d can only be input", gpio_num);
  215. return ESP_ERR_INVALID_ARG;
  216. }
  217. esp_err_t ret = ESP_OK;
  218. if (mode & GPIO_MODE_DEF_INPUT) {
  219. PIN_INPUT_ENABLE(GPIO_PIN_MUX_REG[gpio_num]);
  220. } else {
  221. PIN_INPUT_DISABLE(GPIO_PIN_MUX_REG[gpio_num]);
  222. }
  223. if (mode & GPIO_MODE_DEF_OUTPUT) {
  224. gpio_output_enable(gpio_num);
  225. } else {
  226. gpio_output_disable(gpio_num);
  227. }
  228. if (mode & GPIO_MODE_DEF_OD) {
  229. GPIO.pin[gpio_num].pad_driver = 1;
  230. } else {
  231. GPIO.pin[gpio_num].pad_driver = 0;
  232. }
  233. return ret;
  234. }
  235. esp_err_t gpio_config(const gpio_config_t *pGPIOConfig)
  236. {
  237. uint64_t gpio_pin_mask = (pGPIOConfig->pin_bit_mask);
  238. uint32_t io_reg = 0;
  239. uint32_t io_num = 0;
  240. uint8_t input_en = 0;
  241. uint8_t output_en = 0;
  242. uint8_t od_en = 0;
  243. uint8_t pu_en = 0;
  244. uint8_t pd_en = 0;
  245. if (pGPIOConfig->pin_bit_mask == 0 || pGPIOConfig->pin_bit_mask >= (((uint64_t) 1) << GPIO_PIN_COUNT)) {
  246. ESP_LOGE(GPIO_TAG, "GPIO_PIN mask error ");
  247. return ESP_ERR_INVALID_ARG;
  248. }
  249. if ((pGPIOConfig->mode) & (GPIO_MODE_DEF_OUTPUT)) {
  250. //GPIO 34/35/36/37/38/39 can only be used as input mode;
  251. if ((gpio_pin_mask & ( GPIO_SEL_34 | GPIO_SEL_35 | GPIO_SEL_36 | GPIO_SEL_37 | GPIO_SEL_38 | GPIO_SEL_39))) {
  252. ESP_LOGE(GPIO_TAG, "GPIO34-39 can only be used as input mode");
  253. return ESP_ERR_INVALID_ARG;
  254. }
  255. }
  256. do {
  257. io_reg = GPIO_PIN_MUX_REG[io_num];
  258. if (((gpio_pin_mask >> io_num) & BIT(0))) {
  259. if (!io_reg) {
  260. ESP_LOGE(GPIO_TAG, "IO%d is not a valid GPIO",io_num);
  261. return ESP_ERR_INVALID_ARG;
  262. }
  263. if(RTC_GPIO_IS_VALID_GPIO(io_num)){
  264. rtc_gpio_deinit(io_num);
  265. }
  266. if ((pGPIOConfig->mode) & GPIO_MODE_DEF_INPUT) {
  267. input_en = 1;
  268. PIN_INPUT_ENABLE(GPIO_PIN_MUX_REG[io_num]);
  269. } else {
  270. PIN_INPUT_DISABLE(GPIO_PIN_MUX_REG[io_num]);
  271. }
  272. if ((pGPIOConfig->mode) & GPIO_MODE_DEF_OD) {
  273. od_en = 1;
  274. GPIO.pin[io_num].pad_driver = 1; /*0x01 Open-drain */
  275. } else {
  276. GPIO.pin[io_num].pad_driver = 0; /*0x00 Normal gpio output */
  277. }
  278. if ((pGPIOConfig->mode) & GPIO_MODE_DEF_OUTPUT) {
  279. output_en = 1;
  280. gpio_output_enable(io_num);
  281. } else {
  282. gpio_output_disable(io_num);
  283. }
  284. if (pGPIOConfig->pull_up_en) {
  285. pu_en = 1;
  286. gpio_pullup_en(io_num);
  287. } else {
  288. gpio_pullup_dis(io_num);
  289. }
  290. if (pGPIOConfig->pull_down_en) {
  291. pd_en = 1;
  292. gpio_pulldown_en(io_num);
  293. } else {
  294. gpio_pulldown_dis(io_num);
  295. }
  296. 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);
  297. gpio_set_intr_type(io_num, pGPIOConfig->intr_type);
  298. if (pGPIOConfig->intr_type) {
  299. gpio_intr_enable(io_num);
  300. } else {
  301. gpio_intr_disable(io_num);
  302. }
  303. PIN_FUNC_SELECT(io_reg, PIN_FUNC_GPIO); /*function number 2 is GPIO_FUNC for each pin */
  304. }
  305. io_num++;
  306. } while (io_num < GPIO_PIN_COUNT);
  307. return ESP_OK;
  308. }
  309. esp_err_t gpio_reset_pin(gpio_num_t gpio_num)
  310. {
  311. assert(gpio_num >= 0 && GPIO_IS_VALID_GPIO(gpio_num));
  312. gpio_config_t cfg = {
  313. .pin_bit_mask = BIT64(gpio_num),
  314. .mode = GPIO_MODE_DISABLE,
  315. //for powersave reasons, the GPIO should not be floating, select pullup
  316. .pull_up_en = true,
  317. .pull_down_en = false,
  318. .intr_type = GPIO_INTR_DISABLE,
  319. };
  320. gpio_config(&cfg);
  321. return ESP_OK;
  322. }
  323. static inline void IRAM_ATTR gpio_isr_loop(uint32_t status, const uint32_t gpio_num_start) {
  324. while (status) {
  325. int nbit = __builtin_ffs(status) - 1;
  326. status &= ~(1 << nbit);
  327. int gpio_num = gpio_num_start + nbit;
  328. if (gpio_isr_func[gpio_num].fn != NULL) {
  329. gpio_isr_func[gpio_num].fn(gpio_isr_func[gpio_num].args);
  330. }
  331. }
  332. }
  333. static void IRAM_ATTR gpio_intr_service(void* arg)
  334. {
  335. //GPIO intr process
  336. if (gpio_isr_func == NULL) {
  337. return;
  338. }
  339. //read status to get interrupt status for GPIO0-31
  340. const uint32_t gpio_intr_status = (isr_core_id == 0) ? GPIO.pcpu_int : GPIO.acpu_int;
  341. if (gpio_intr_status) {
  342. gpio_isr_loop(gpio_intr_status, 0);
  343. GPIO.status_w1tc = gpio_intr_status;
  344. }
  345. //read status1 to get interrupt status for GPIO32-39
  346. const uint32_t gpio_intr_status_h = (isr_core_id == 0) ? GPIO.pcpu_int1.intr : GPIO.acpu_int1.intr;
  347. if (gpio_intr_status_h) {
  348. gpio_isr_loop(gpio_intr_status_h, 32);
  349. GPIO.status1_w1tc.intr_st = gpio_intr_status_h;
  350. }
  351. }
  352. esp_err_t gpio_isr_handler_add(gpio_num_t gpio_num, gpio_isr_t isr_handler, void* args)
  353. {
  354. GPIO_CHECK(gpio_isr_func != NULL, "GPIO isr service is not installed, call gpio_install_isr_service() first", ESP_ERR_INVALID_STATE);
  355. GPIO_CHECK(GPIO_IS_VALID_GPIO(gpio_num), "GPIO number error", ESP_ERR_INVALID_ARG);
  356. portENTER_CRITICAL(&gpio_spinlock);
  357. gpio_intr_disable(gpio_num);
  358. if (gpio_isr_func) {
  359. gpio_isr_func[gpio_num].fn = isr_handler;
  360. gpio_isr_func[gpio_num].args = args;
  361. }
  362. gpio_intr_enable_on_core (gpio_num, esp_intr_get_cpu(gpio_isr_handle));
  363. portEXIT_CRITICAL(&gpio_spinlock);
  364. return ESP_OK;
  365. }
  366. esp_err_t gpio_isr_handler_remove(gpio_num_t gpio_num)
  367. {
  368. GPIO_CHECK(gpio_isr_func != NULL, "GPIO isr service is not installed, call gpio_install_isr_service() first", ESP_ERR_INVALID_STATE);
  369. GPIO_CHECK(GPIO_IS_VALID_GPIO(gpio_num), "GPIO number error", ESP_ERR_INVALID_ARG);
  370. portENTER_CRITICAL(&gpio_spinlock);
  371. gpio_intr_disable(gpio_num);
  372. if (gpio_isr_func) {
  373. gpio_isr_func[gpio_num].fn = NULL;
  374. gpio_isr_func[gpio_num].args = NULL;
  375. }
  376. portEXIT_CRITICAL(&gpio_spinlock);
  377. return ESP_OK;
  378. }
  379. esp_err_t gpio_install_isr_service(int intr_alloc_flags)
  380. {
  381. GPIO_CHECK(gpio_isr_func == NULL, "GPIO isr service already installed", ESP_ERR_INVALID_STATE);
  382. esp_err_t ret;
  383. portENTER_CRITICAL(&gpio_spinlock);
  384. gpio_isr_func = (gpio_isr_func_t*) calloc(GPIO_NUM_MAX, sizeof(gpio_isr_func_t));
  385. portEXIT_CRITICAL(&gpio_spinlock);
  386. if (gpio_isr_func == NULL) {
  387. ret = ESP_ERR_NO_MEM;
  388. } else {
  389. ret = gpio_isr_register(gpio_intr_service, NULL, intr_alloc_flags, &gpio_isr_handle);
  390. }
  391. return ret;
  392. }
  393. void gpio_uninstall_isr_service()
  394. {
  395. if (gpio_isr_func == NULL) {
  396. return;
  397. }
  398. portENTER_CRITICAL(&gpio_spinlock);
  399. esp_intr_free(gpio_isr_handle);
  400. free(gpio_isr_func);
  401. gpio_isr_func = NULL;
  402. isr_core_id = GPIO_ISR_CORE_ID_UNINIT;
  403. portEXIT_CRITICAL(&gpio_spinlock);
  404. return;
  405. }
  406. static void gpio_isr_register_on_core_static(void *param)
  407. {
  408. gpio_isr_alloc_t *p = (gpio_isr_alloc_t *)param;
  409. //We need to check the return value.
  410. p->ret = esp_intr_alloc(p->source, p->intr_alloc_flags, p->fn, p->arg, p->handle);
  411. }
  412. esp_err_t gpio_isr_register(void (*fn)(void*), void * arg, int intr_alloc_flags, gpio_isr_handle_t *handle)
  413. {
  414. GPIO_CHECK(fn, "GPIO ISR null", ESP_ERR_INVALID_ARG);
  415. gpio_isr_alloc_t p;
  416. p.source = ETS_GPIO_INTR_SOURCE;
  417. p.intr_alloc_flags = intr_alloc_flags;
  418. p.fn = fn;
  419. p.arg = arg;
  420. p.handle = handle;
  421. portENTER_CRITICAL(&gpio_spinlock);
  422. if(isr_core_id == GPIO_ISR_CORE_ID_UNINIT) {
  423. isr_core_id = xPortGetCoreID();
  424. }
  425. portEXIT_CRITICAL(&gpio_spinlock);
  426. esp_err_t ret;
  427. #if CONFIG_FREERTOS_UNICORE
  428. gpio_isr_register_on_core_static(&p);
  429. ret = ESP_OK;
  430. #else /* CONFIG_FREERTOS_UNICORE */
  431. ret = esp_ipc_call_blocking(isr_core_id, gpio_isr_register_on_core_static, (void *)&p);
  432. #endif /* !CONFIG_FREERTOS_UNICORE */
  433. if(ret != ESP_OK || p.ret != ESP_OK) {
  434. return ESP_ERR_NOT_FOUND;
  435. }
  436. return ESP_OK;
  437. }
  438. esp_err_t gpio_wakeup_enable(gpio_num_t gpio_num, gpio_int_type_t intr_type)
  439. {
  440. GPIO_CHECK(GPIO_IS_VALID_GPIO(gpio_num), "GPIO number error", ESP_ERR_INVALID_ARG);
  441. esp_err_t ret = ESP_OK;
  442. if (( intr_type == GPIO_INTR_LOW_LEVEL ) || ( intr_type == GPIO_INTR_HIGH_LEVEL )) {
  443. if (RTC_GPIO_IS_VALID_GPIO(gpio_num)) {
  444. ret = rtc_gpio_wakeup_enable(gpio_num, intr_type);
  445. } else {
  446. GPIO.pin[gpio_num].int_type = intr_type;
  447. GPIO.pin[gpio_num].wakeup_enable = 0x1;
  448. }
  449. } else {
  450. ESP_LOGE(GPIO_TAG, "GPIO wakeup only supports level mode, but edge mode set. gpio_num:%u", gpio_num);
  451. ret = ESP_ERR_INVALID_ARG;
  452. }
  453. return ret;
  454. }
  455. esp_err_t gpio_wakeup_disable(gpio_num_t gpio_num)
  456. {
  457. GPIO_CHECK(GPIO_IS_VALID_GPIO(gpio_num), "GPIO number error", ESP_ERR_INVALID_ARG);
  458. GPIO.pin[gpio_num].wakeup_enable = 0;
  459. return ESP_OK;
  460. }
  461. esp_err_t gpio_set_drive_capability(gpio_num_t gpio_num, gpio_drive_cap_t strength)
  462. {
  463. GPIO_CHECK(GPIO_IS_VALID_OUTPUT_GPIO(gpio_num), "GPIO number error", ESP_ERR_INVALID_ARG);
  464. GPIO_CHECK(strength < GPIO_DRIVE_CAP_MAX, "GPIO drive capability error", ESP_ERR_INVALID_ARG);
  465. if (RTC_GPIO_IS_VALID_GPIO(gpio_num)) {
  466. rtc_gpio_set_drive_capability(gpio_num, strength);
  467. } else {
  468. SET_PERI_REG_BITS(GPIO_PIN_MUX_REG[gpio_num], FUN_DRV_V, strength, FUN_DRV_S);
  469. }
  470. return ESP_OK;
  471. }
  472. esp_err_t gpio_get_drive_capability(gpio_num_t gpio_num, gpio_drive_cap_t* strength)
  473. {
  474. GPIO_CHECK(GPIO_IS_VALID_OUTPUT_GPIO(gpio_num), "GPIO number error", ESP_ERR_INVALID_ARG);
  475. GPIO_CHECK(strength != NULL, "GPIO drive capability pointer error", ESP_ERR_INVALID_ARG);
  476. if (RTC_GPIO_IS_VALID_GPIO(gpio_num)) {
  477. return rtc_gpio_get_drive_capability(gpio_num, strength);
  478. } else {
  479. *strength = GET_PERI_REG_BITS2(GPIO_PIN_MUX_REG[gpio_num], FUN_DRV_V, FUN_DRV_S);
  480. }
  481. return ESP_OK;
  482. }
  483. static const uint32_t GPIO_HOLD_MASK[34] = {
  484. 0,
  485. GPIO_SEL_1,
  486. 0,
  487. GPIO_SEL_0,
  488. 0,
  489. GPIO_SEL_8,
  490. GPIO_SEL_2,
  491. GPIO_SEL_3,
  492. GPIO_SEL_4,
  493. GPIO_SEL_5,
  494. GPIO_SEL_6,
  495. GPIO_SEL_7,
  496. 0,
  497. 0,
  498. 0,
  499. 0,
  500. GPIO_SEL_9,
  501. GPIO_SEL_10,
  502. GPIO_SEL_11,
  503. GPIO_SEL_12,
  504. 0,
  505. GPIO_SEL_14,
  506. GPIO_SEL_15,
  507. GPIO_SEL_16,
  508. 0,
  509. 0,
  510. 0,
  511. 0,
  512. 0,
  513. 0,
  514. 0,
  515. 0,
  516. 0,
  517. 0,
  518. };
  519. esp_err_t gpio_hold_en(gpio_num_t gpio_num)
  520. {
  521. GPIO_CHECK(GPIO_IS_VALID_OUTPUT_GPIO(gpio_num), "Only output-capable GPIO support this function", ESP_ERR_NOT_SUPPORTED);
  522. esp_err_t r = ESP_OK;
  523. if (RTC_GPIO_IS_VALID_GPIO(gpio_num)) {
  524. r = rtc_gpio_hold_en(gpio_num);
  525. } else if (GPIO_HOLD_MASK[gpio_num]) {
  526. SET_PERI_REG_MASK(RTC_IO_DIG_PAD_HOLD_REG, GPIO_HOLD_MASK[gpio_num]);
  527. } else {
  528. r = ESP_ERR_NOT_SUPPORTED;
  529. }
  530. return r == ESP_OK ? ESP_OK : ESP_ERR_NOT_SUPPORTED;
  531. }
  532. esp_err_t gpio_hold_dis(gpio_num_t gpio_num)
  533. {
  534. GPIO_CHECK(GPIO_IS_VALID_OUTPUT_GPIO(gpio_num), "Only output-capable GPIO support this function", ESP_ERR_NOT_SUPPORTED);
  535. esp_err_t r = ESP_OK;
  536. if (RTC_GPIO_IS_VALID_GPIO(gpio_num)) {
  537. r = rtc_gpio_hold_dis(gpio_num);
  538. } else if (GPIO_HOLD_MASK[gpio_num]) {
  539. CLEAR_PERI_REG_MASK(RTC_IO_DIG_PAD_HOLD_REG, GPIO_HOLD_MASK[gpio_num]);
  540. } else {
  541. r = ESP_ERR_NOT_SUPPORTED;
  542. }
  543. return r == ESP_OK ? ESP_OK : ESP_ERR_NOT_SUPPORTED;
  544. }
  545. void gpio_deep_sleep_hold_en(void)
  546. {
  547. portENTER_CRITICAL(&gpio_spinlock);
  548. SET_PERI_REG_MASK(RTC_CNTL_DIG_ISO_REG, RTC_CNTL_DG_PAD_AUTOHOLD_EN_M);
  549. portEXIT_CRITICAL(&gpio_spinlock);
  550. }
  551. void gpio_deep_sleep_hold_dis(void)
  552. {
  553. portENTER_CRITICAL(&gpio_spinlock);
  554. CLEAR_PERI_REG_MASK(RTC_CNTL_DIG_ISO_REG, RTC_CNTL_DG_PAD_AUTOHOLD_EN_M);
  555. portEXIT_CRITICAL(&gpio_spinlock);
  556. }
  557. void gpio_iomux_in(uint32_t gpio, uint32_t signal_idx)
  558. {
  559. GPIO.func_in_sel_cfg[signal_idx].sig_in_sel = 0;
  560. PIN_INPUT_ENABLE(GPIO_PIN_MUX_REG[gpio]);
  561. }
  562. void gpio_iomux_out(uint8_t gpio_num, int func, bool oen_inv)
  563. {
  564. GPIO.func_out_sel_cfg[gpio_num].oen_sel = 0;
  565. GPIO.func_out_sel_cfg[gpio_num].oen_inv_sel = oen_inv;
  566. PIN_FUNC_SELECT(GPIO_PIN_MUX_REG[gpio_num], func);
  567. }