gpio.c 22 KB

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