gpio.c 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925
  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. /* By default, all the pins have to be configured as GPIO pins. */
  332. PIN_FUNC_SELECT(io_reg, PIN_FUNC_GPIO);
  333. }
  334. io_num++;
  335. } while (io_num < GPIO_PIN_COUNT);
  336. return ESP_OK;
  337. }
  338. esp_err_t gpio_reset_pin(gpio_num_t gpio_num)
  339. {
  340. assert(gpio_num >= 0 && GPIO_IS_VALID_GPIO(gpio_num));
  341. gpio_config_t cfg = {
  342. .pin_bit_mask = BIT64(gpio_num),
  343. .mode = GPIO_MODE_DISABLE,
  344. //for powersave reasons, the GPIO should not be floating, select pullup
  345. .pull_up_en = true,
  346. .pull_down_en = false,
  347. .intr_type = GPIO_INTR_DISABLE,
  348. };
  349. gpio_config(&cfg);
  350. return ESP_OK;
  351. }
  352. static inline void IRAM_ATTR gpio_isr_loop(uint32_t status, const uint32_t gpio_num_start)
  353. {
  354. while (status) {
  355. int nbit = __builtin_ffs(status) - 1;
  356. status &= ~(1 << nbit);
  357. int gpio_num = gpio_num_start + nbit;
  358. if (gpio_context.gpio_isr_func[gpio_num].fn != NULL) {
  359. gpio_context.gpio_isr_func[gpio_num].fn(gpio_context.gpio_isr_func[gpio_num].args);
  360. }
  361. }
  362. }
  363. static void IRAM_ATTR gpio_intr_service(void *arg)
  364. {
  365. //GPIO intr process
  366. if (gpio_context.gpio_isr_func == NULL) {
  367. return;
  368. }
  369. //read status to get interrupt status for GPIO0-31
  370. uint32_t gpio_intr_status;
  371. gpio_hal_get_intr_status(gpio_context.gpio_hal, gpio_context.isr_core_id, &gpio_intr_status);
  372. if (gpio_intr_status) {
  373. gpio_isr_loop(gpio_intr_status, 0);
  374. gpio_hal_clear_intr_status(gpio_context.gpio_hal, gpio_intr_status);
  375. }
  376. //read status1 to get interrupt status for GPIO32-39
  377. uint32_t gpio_intr_status_h;
  378. gpio_hal_get_intr_status_high(gpio_context.gpio_hal, gpio_context.isr_core_id, &gpio_intr_status_h);
  379. if (gpio_intr_status_h) {
  380. gpio_isr_loop(gpio_intr_status_h, 32);
  381. gpio_hal_clear_intr_status_high(gpio_context.gpio_hal, gpio_intr_status_h);
  382. }
  383. }
  384. esp_err_t gpio_install_isr_service(int intr_alloc_flags)
  385. {
  386. GPIO_CHECK(gpio_context.gpio_isr_func == NULL, "GPIO isr service already installed", ESP_ERR_INVALID_STATE);
  387. esp_err_t ret;
  388. portENTER_CRITICAL(&gpio_context.gpio_spinlock);
  389. gpio_context.gpio_isr_func = (gpio_isr_func_t *) calloc(GPIO_NUM_MAX, sizeof(gpio_isr_func_t));
  390. portEXIT_CRITICAL(&gpio_context.gpio_spinlock);
  391. if (gpio_context.gpio_isr_func == NULL) {
  392. ret = ESP_ERR_NO_MEM;
  393. } else {
  394. ret = gpio_isr_register(gpio_intr_service, NULL, intr_alloc_flags, &gpio_context.gpio_isr_handle);
  395. }
  396. return ret;
  397. }
  398. esp_err_t gpio_isr_handler_add(gpio_num_t gpio_num, gpio_isr_t isr_handler, void *args)
  399. {
  400. GPIO_CHECK(gpio_context.gpio_isr_func != NULL, "GPIO isr service is not installed, call gpio_install_isr_service() first", ESP_ERR_INVALID_STATE);
  401. GPIO_CHECK(GPIO_IS_VALID_GPIO(gpio_num), "GPIO number error", ESP_ERR_INVALID_ARG);
  402. portENTER_CRITICAL(&gpio_context.gpio_spinlock);
  403. gpio_intr_disable(gpio_num);
  404. if (gpio_context.gpio_isr_func) {
  405. gpio_context.gpio_isr_func[gpio_num].fn = isr_handler;
  406. gpio_context.gpio_isr_func[gpio_num].args = args;
  407. }
  408. gpio_intr_enable_on_core (gpio_num, esp_intr_get_cpu(gpio_context.gpio_isr_handle));
  409. portEXIT_CRITICAL(&gpio_context.gpio_spinlock);
  410. return ESP_OK;
  411. }
  412. esp_err_t gpio_isr_handler_remove(gpio_num_t gpio_num)
  413. {
  414. GPIO_CHECK(gpio_context.gpio_isr_func != NULL, "GPIO isr service is not installed, call gpio_install_isr_service() first", ESP_ERR_INVALID_STATE);
  415. GPIO_CHECK(GPIO_IS_VALID_GPIO(gpio_num), "GPIO number error", ESP_ERR_INVALID_ARG);
  416. portENTER_CRITICAL(&gpio_context.gpio_spinlock);
  417. gpio_intr_disable(gpio_num);
  418. if (gpio_context.gpio_isr_func) {
  419. gpio_context.gpio_isr_func[gpio_num].fn = NULL;
  420. gpio_context.gpio_isr_func[gpio_num].args = NULL;
  421. }
  422. portEXIT_CRITICAL(&gpio_context.gpio_spinlock);
  423. return ESP_OK;
  424. }
  425. void gpio_uninstall_isr_service(void)
  426. {
  427. if (gpio_context.gpio_isr_func == NULL) {
  428. return;
  429. }
  430. portENTER_CRITICAL(&gpio_context.gpio_spinlock);
  431. esp_intr_free(gpio_context.gpio_isr_handle);
  432. free(gpio_context.gpio_isr_func);
  433. gpio_context.gpio_isr_func = NULL;
  434. gpio_context.isr_core_id = GPIO_ISR_CORE_ID_UNINIT;
  435. portEXIT_CRITICAL(&gpio_context.gpio_spinlock);
  436. return;
  437. }
  438. static void gpio_isr_register_on_core_static(void *param)
  439. {
  440. gpio_isr_alloc_t *p = (gpio_isr_alloc_t *)param;
  441. //We need to check the return value.
  442. p->ret = esp_intr_alloc(p->source, p->intr_alloc_flags, p->fn, p->arg, p->handle);
  443. }
  444. esp_err_t gpio_isr_register(void (*fn)(void *), void *arg, int intr_alloc_flags, gpio_isr_handle_t *handle)
  445. {
  446. GPIO_CHECK(fn, "GPIO ISR null", ESP_ERR_INVALID_ARG);
  447. gpio_isr_alloc_t p;
  448. p.source = ETS_GPIO_INTR_SOURCE;
  449. p.intr_alloc_flags = intr_alloc_flags;
  450. p.fn = fn;
  451. p.arg = arg;
  452. p.handle = handle;
  453. portENTER_CRITICAL(&gpio_context.gpio_spinlock);
  454. if(gpio_context.isr_core_id == GPIO_ISR_CORE_ID_UNINIT) {
  455. gpio_context.isr_core_id = xPortGetCoreID();
  456. }
  457. portEXIT_CRITICAL(&gpio_context.gpio_spinlock);
  458. esp_err_t ret;
  459. #if CONFIG_FREERTOS_UNICORE
  460. gpio_isr_register_on_core_static(&p);
  461. ret = ESP_OK;
  462. #else /* CONFIG_FREERTOS_UNICORE */
  463. ret = esp_ipc_call_blocking(gpio_context.isr_core_id, gpio_isr_register_on_core_static, (void *)&p);
  464. #endif /* !CONFIG_FREERTOS_UNICORE */
  465. if(ret != ESP_OK || p.ret != ESP_OK) {
  466. return ESP_ERR_NOT_FOUND;
  467. }
  468. return ESP_OK;
  469. }
  470. esp_err_t gpio_wakeup_enable(gpio_num_t gpio_num, gpio_int_type_t intr_type)
  471. {
  472. GPIO_CHECK(GPIO_IS_VALID_GPIO(gpio_num), "GPIO number error", ESP_ERR_INVALID_ARG);
  473. esp_err_t ret = ESP_OK;
  474. if ((intr_type == GPIO_INTR_LOW_LEVEL) || (intr_type == GPIO_INTR_HIGH_LEVEL)) {
  475. #if SOC_RTCIO_WAKE_SUPPORTED
  476. if (rtc_gpio_is_valid_gpio(gpio_num)) {
  477. ret = rtc_gpio_wakeup_enable(gpio_num, intr_type);
  478. }
  479. #endif
  480. portENTER_CRITICAL(&gpio_context.gpio_spinlock);
  481. gpio_hal_wakeup_enable(gpio_context.gpio_hal, gpio_num, intr_type);
  482. #if SOC_GPIO_SUPPORT_SLP_SWITCH && CONFIG_ESP32C3_LIGHTSLEEP_GPIO_RESET_WORKAROUND
  483. gpio_hal_sleep_sel_dis(gpio_context.gpio_hal, gpio_num);
  484. #endif
  485. portEXIT_CRITICAL(&gpio_context.gpio_spinlock);
  486. } else {
  487. ESP_LOGE(GPIO_TAG, "GPIO wakeup only supports level mode, but edge mode set. gpio_num:%u", gpio_num);
  488. ret = ESP_ERR_INVALID_ARG;
  489. }
  490. return ret;
  491. }
  492. esp_err_t gpio_wakeup_disable(gpio_num_t gpio_num)
  493. {
  494. GPIO_CHECK(GPIO_IS_VALID_GPIO(gpio_num), "GPIO number error", ESP_ERR_INVALID_ARG);
  495. esp_err_t ret = ESP_OK;
  496. #if SOC_RTCIO_WAKE_SUPPORTED
  497. if (rtc_gpio_is_valid_gpio(gpio_num)) {
  498. ret = rtc_gpio_wakeup_disable(gpio_num);
  499. }
  500. #endif
  501. portENTER_CRITICAL(&gpio_context.gpio_spinlock);
  502. gpio_hal_wakeup_disable(gpio_context.gpio_hal, gpio_num);
  503. portEXIT_CRITICAL(&gpio_context.gpio_spinlock);
  504. return ret;
  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. esp_err_t ret = ESP_OK;
  511. if (!rtc_gpio_is_valid_gpio(gpio_num) || SOC_GPIO_SUPPORT_RTC_INDEPENDENT) {
  512. portENTER_CRITICAL(&gpio_context.gpio_spinlock);
  513. gpio_hal_set_drive_capability(gpio_context.gpio_hal, gpio_num, strength);
  514. portEXIT_CRITICAL(&gpio_context.gpio_spinlock);
  515. } else {
  516. #if SOC_RTCIO_INPUT_OUTPUT_SUPPORTED
  517. ret = rtc_gpio_set_drive_capability(gpio_num, strength);
  518. #else
  519. abort(); // This should be eliminated as unreachable, unless a programming error has occured
  520. #endif
  521. }
  522. return ret;
  523. }
  524. esp_err_t gpio_get_drive_capability(gpio_num_t gpio_num, gpio_drive_cap_t *strength)
  525. {
  526. GPIO_CHECK(GPIO_IS_VALID_OUTPUT_GPIO(gpio_num), "GPIO number error", ESP_ERR_INVALID_ARG);
  527. GPIO_CHECK(strength != NULL, "GPIO drive capability pointer error", ESP_ERR_INVALID_ARG);
  528. esp_err_t ret = ESP_OK;
  529. if (!rtc_gpio_is_valid_gpio(gpio_num) || SOC_GPIO_SUPPORT_RTC_INDEPENDENT) {
  530. portENTER_CRITICAL(&gpio_context.gpio_spinlock);
  531. gpio_hal_get_drive_capability(gpio_context.gpio_hal, gpio_num, strength);
  532. portEXIT_CRITICAL(&gpio_context.gpio_spinlock);
  533. } else {
  534. #if SOC_RTCIO_INPUT_OUTPUT_SUPPORTED
  535. ret = rtc_gpio_get_drive_capability(gpio_num, strength);
  536. #else
  537. abort(); // This should be eliminated as unreachable, unless a programming error has occured
  538. #endif
  539. }
  540. return ret;
  541. }
  542. esp_err_t gpio_hold_en(gpio_num_t gpio_num)
  543. {
  544. GPIO_CHECK(GPIO_IS_VALID_OUTPUT_GPIO(gpio_num), "Only output-capable GPIO support this function", ESP_ERR_NOT_SUPPORTED);
  545. int ret = ESP_OK;
  546. if (rtc_gpio_is_valid_gpio(gpio_num)) {
  547. #if SOC_RTCIO_HOLD_SUPPORTED
  548. ret = rtc_gpio_hold_en(gpio_num);
  549. #endif
  550. } else if (GPIO_HOLD_MASK[gpio_num]) {
  551. portENTER_CRITICAL(&gpio_context.gpio_spinlock);
  552. gpio_hal_hold_en(gpio_context.gpio_hal, gpio_num);
  553. portEXIT_CRITICAL(&gpio_context.gpio_spinlock);
  554. } else {
  555. ret = ESP_ERR_NOT_SUPPORTED;
  556. }
  557. return ret;
  558. }
  559. esp_err_t gpio_hold_dis(gpio_num_t gpio_num)
  560. {
  561. GPIO_CHECK(GPIO_IS_VALID_OUTPUT_GPIO(gpio_num), "Only output-capable GPIO support this function", ESP_ERR_NOT_SUPPORTED);
  562. int ret = ESP_OK;
  563. if (rtc_gpio_is_valid_gpio(gpio_num)) {
  564. #if SOC_RTCIO_HOLD_SUPPORTED
  565. ret = rtc_gpio_hold_dis(gpio_num);
  566. #endif
  567. }else if (GPIO_HOLD_MASK[gpio_num]) {
  568. portENTER_CRITICAL(&gpio_context.gpio_spinlock);
  569. gpio_hal_hold_dis(gpio_context.gpio_hal, gpio_num);
  570. portEXIT_CRITICAL(&gpio_context.gpio_spinlock);
  571. } else {
  572. ret = ESP_ERR_NOT_SUPPORTED;
  573. }
  574. return ret;
  575. }
  576. void gpio_deep_sleep_hold_en(void)
  577. {
  578. portENTER_CRITICAL(&gpio_context.gpio_spinlock);
  579. gpio_hal_deep_sleep_hold_en(gpio_context.gpio_hal);
  580. portEXIT_CRITICAL(&gpio_context.gpio_spinlock);
  581. }
  582. void gpio_deep_sleep_hold_dis(void)
  583. {
  584. portENTER_CRITICAL(&gpio_context.gpio_spinlock);
  585. gpio_hal_deep_sleep_hold_dis(gpio_context.gpio_hal);
  586. portEXIT_CRITICAL(&gpio_context.gpio_spinlock);
  587. }
  588. #if SOC_GPIO_SUPPORT_FORCE_HOLD
  589. esp_err_t gpio_force_hold_all()
  590. {
  591. #if SOC_RTCIO_HOLD_SUPPORTED
  592. rtc_gpio_force_hold_all();
  593. #endif
  594. portENTER_CRITICAL(&gpio_context.gpio_spinlock);
  595. gpio_hal_force_hold_all(gpio_context.gpio_hal);
  596. portEXIT_CRITICAL(&gpio_context.gpio_spinlock);
  597. return ESP_OK;
  598. }
  599. esp_err_t gpio_force_unhold_all()
  600. {
  601. #if SOC_RTCIO_HOLD_SUPPORTED
  602. rtc_gpio_force_hold_dis_all();
  603. #endif
  604. portENTER_CRITICAL(&gpio_context.gpio_spinlock);
  605. gpio_hal_force_unhold_all();
  606. portEXIT_CRITICAL(&gpio_context.gpio_spinlock);
  607. return ESP_OK;
  608. }
  609. #endif
  610. void gpio_iomux_in(uint32_t gpio, uint32_t signal_idx)
  611. {
  612. gpio_hal_iomux_in(gpio_context.gpio_hal, gpio, signal_idx);
  613. }
  614. void gpio_iomux_out(uint8_t gpio_num, int func, bool oen_inv)
  615. {
  616. gpio_hal_iomux_out(gpio_context.gpio_hal, gpio_num, func, (uint32_t)oen_inv);
  617. }
  618. #if SOC_GPIO_SUPPORT_SLP_SWITCH
  619. static esp_err_t gpio_sleep_pullup_en(gpio_num_t gpio_num)
  620. {
  621. GPIO_CHECK(GPIO_IS_VALID_GPIO(gpio_num), "GPIO number error", ESP_ERR_INVALID_ARG);
  622. portENTER_CRITICAL(&gpio_context.gpio_spinlock);
  623. gpio_hal_sleep_pullup_en(gpio_context.gpio_hal, gpio_num);
  624. portEXIT_CRITICAL(&gpio_context.gpio_spinlock);
  625. return ESP_OK;
  626. }
  627. static esp_err_t gpio_sleep_pullup_dis(gpio_num_t gpio_num)
  628. {
  629. GPIO_CHECK(GPIO_IS_VALID_GPIO(gpio_num), "GPIO number error", ESP_ERR_INVALID_ARG);
  630. portENTER_CRITICAL(&gpio_context.gpio_spinlock);
  631. gpio_hal_sleep_pullup_dis(gpio_context.gpio_hal, gpio_num);
  632. portEXIT_CRITICAL(&gpio_context.gpio_spinlock);
  633. return ESP_OK;
  634. }
  635. static esp_err_t gpio_sleep_pulldown_en(gpio_num_t gpio_num)
  636. {
  637. GPIO_CHECK(GPIO_IS_VALID_GPIO(gpio_num), "GPIO number error", ESP_ERR_INVALID_ARG);
  638. portENTER_CRITICAL(&gpio_context.gpio_spinlock);
  639. gpio_hal_sleep_pulldown_en(gpio_context.gpio_hal, gpio_num);
  640. portEXIT_CRITICAL(&gpio_context.gpio_spinlock);
  641. return ESP_OK;
  642. }
  643. static esp_err_t gpio_sleep_pulldown_dis(gpio_num_t gpio_num)
  644. {
  645. GPIO_CHECK(GPIO_IS_VALID_GPIO(gpio_num), "GPIO number error", ESP_ERR_INVALID_ARG);
  646. portENTER_CRITICAL(&gpio_context.gpio_spinlock);
  647. gpio_hal_sleep_pulldown_dis(gpio_context.gpio_hal, gpio_num);
  648. portEXIT_CRITICAL(&gpio_context.gpio_spinlock);
  649. return ESP_OK;
  650. }
  651. static esp_err_t gpio_sleep_input_disable(gpio_num_t gpio_num)
  652. {
  653. GPIO_CHECK(GPIO_IS_VALID_GPIO(gpio_num), "GPIO number error", ESP_ERR_INVALID_ARG);
  654. gpio_hal_sleep_input_disable(gpio_context.gpio_hal, gpio_num);
  655. return ESP_OK;
  656. }
  657. static esp_err_t gpio_sleep_input_enable(gpio_num_t gpio_num)
  658. {
  659. GPIO_CHECK(GPIO_IS_VALID_GPIO(gpio_num), "GPIO number error", ESP_ERR_INVALID_ARG);
  660. gpio_hal_sleep_input_enable(gpio_context.gpio_hal, gpio_num);
  661. return ESP_OK;
  662. }
  663. static esp_err_t gpio_sleep_output_disable(gpio_num_t gpio_num)
  664. {
  665. GPIO_CHECK(GPIO_IS_VALID_GPIO(gpio_num), "GPIO number error", ESP_ERR_INVALID_ARG);
  666. gpio_hal_sleep_output_disable(gpio_context.gpio_hal, gpio_num);
  667. return ESP_OK;
  668. }
  669. static esp_err_t gpio_sleep_output_enable(gpio_num_t gpio_num)
  670. {
  671. GPIO_CHECK(GPIO_IS_VALID_OUTPUT_GPIO(gpio_num), "GPIO output gpio_num error", ESP_ERR_INVALID_ARG);
  672. gpio_hal_sleep_output_enable(gpio_context.gpio_hal, gpio_num);
  673. return ESP_OK;
  674. }
  675. esp_err_t gpio_sleep_set_direction(gpio_num_t gpio_num, gpio_mode_t mode)
  676. {
  677. GPIO_CHECK(GPIO_IS_VALID_GPIO(gpio_num), "GPIO number error", ESP_ERR_INVALID_ARG);
  678. if ((GPIO_IS_VALID_OUTPUT_GPIO(gpio_num) != true) && (mode & GPIO_MODE_DEF_OUTPUT)) {
  679. ESP_LOGE(GPIO_TAG, "io_num=%d can only be input", gpio_num);
  680. return ESP_ERR_INVALID_ARG;
  681. }
  682. esp_err_t ret = ESP_OK;
  683. if (mode & GPIO_MODE_DEF_INPUT) {
  684. gpio_sleep_input_enable(gpio_num);
  685. } else {
  686. gpio_sleep_input_disable(gpio_num);
  687. }
  688. if (mode & GPIO_MODE_DEF_OUTPUT) {
  689. gpio_sleep_output_enable(gpio_num);
  690. } else {
  691. gpio_sleep_output_disable(gpio_num);
  692. }
  693. return ret;
  694. }
  695. esp_err_t gpio_sleep_set_pull_mode(gpio_num_t gpio_num, gpio_pull_mode_t pull)
  696. {
  697. GPIO_CHECK(GPIO_IS_VALID_GPIO(gpio_num), "GPIO number error", ESP_ERR_INVALID_ARG);
  698. GPIO_CHECK(pull <= GPIO_FLOATING, "GPIO pull mode error", ESP_ERR_INVALID_ARG);
  699. esp_err_t ret = ESP_OK;
  700. switch (pull) {
  701. case GPIO_PULLUP_ONLY:
  702. gpio_sleep_pulldown_dis(gpio_num);
  703. gpio_sleep_pullup_en(gpio_num);
  704. break;
  705. case GPIO_PULLDOWN_ONLY:
  706. gpio_sleep_pulldown_en(gpio_num);
  707. gpio_sleep_pullup_dis(gpio_num);
  708. break;
  709. case GPIO_PULLUP_PULLDOWN:
  710. gpio_sleep_pulldown_en(gpio_num);
  711. gpio_sleep_pullup_en(gpio_num);
  712. break;
  713. case GPIO_FLOATING:
  714. gpio_sleep_pulldown_dis(gpio_num);
  715. gpio_sleep_pullup_dis(gpio_num);
  716. break;
  717. default:
  718. ESP_LOGE(GPIO_TAG, "Unknown pull up/down mode,gpio_num=%u,pull=%u", gpio_num, pull);
  719. ret = ESP_ERR_INVALID_ARG;
  720. break;
  721. }
  722. return ret;
  723. }
  724. esp_err_t gpio_sleep_sel_en(gpio_num_t gpio_num)
  725. {
  726. GPIO_CHECK(GPIO_IS_VALID_GPIO(gpio_num), "GPIO number error", ESP_ERR_INVALID_ARG);
  727. portENTER_CRITICAL(&gpio_context.gpio_spinlock);
  728. gpio_hal_sleep_sel_en(gpio_context.gpio_hal, gpio_num);
  729. portEXIT_CRITICAL(&gpio_context.gpio_spinlock);
  730. return ESP_OK;
  731. }
  732. esp_err_t gpio_sleep_sel_dis(gpio_num_t gpio_num)
  733. {
  734. GPIO_CHECK(GPIO_IS_VALID_GPIO(gpio_num), "GPIO number error", ESP_ERR_INVALID_ARG);
  735. portENTER_CRITICAL(&gpio_context.gpio_spinlock);
  736. gpio_hal_sleep_sel_dis(gpio_context.gpio_hal, gpio_num);
  737. portEXIT_CRITICAL(&gpio_context.gpio_spinlock);
  738. return ESP_OK;
  739. }
  740. #if CONFIG_GPIO_ESP32_SUPPORT_SWITCH_SLP_PULL
  741. esp_err_t gpio_sleep_pupd_config_apply(gpio_num_t gpio_num)
  742. {
  743. GPIO_CHECK(GPIO_IS_VALID_GPIO(gpio_num), "GPIO number error", ESP_ERR_INVALID_ARG);
  744. gpio_hal_sleep_pupd_config_apply(gpio_context.gpio_hal, gpio_num);
  745. return ESP_OK;
  746. }
  747. esp_err_t gpio_sleep_pupd_config_unapply(gpio_num_t gpio_num)
  748. {
  749. GPIO_CHECK(GPIO_IS_VALID_GPIO(gpio_num), "GPIO number error", ESP_ERR_INVALID_ARG);
  750. gpio_hal_sleep_pupd_config_unapply(gpio_context.gpio_hal, gpio_num);
  751. return ESP_OK;
  752. }
  753. #endif // CONFIG_GPIO_ESP32_SUPPORT_SWITCH_SLP_PULL
  754. #endif // SOC_GPIO_SUPPORT_SLP_SWITCH
  755. #if SOC_GPIO_SUPPORT_DEEPSLEEP_WAKEUP
  756. esp_err_t gpio_deep_sleep_wakeup_enable(gpio_num_t gpio_num, gpio_int_type_t intr_type)
  757. {
  758. if (!gpio_hal_is_valid_deepsleep_wakeup_gpio(gpio_num)) {
  759. ESP_LOGE(GPIO_TAG, "GPIO %d does not support deep sleep wakeup", gpio_num);
  760. return ESP_ERR_INVALID_ARG;
  761. }
  762. if ((intr_type != GPIO_INTR_LOW_LEVEL) && (intr_type != GPIO_INTR_HIGH_LEVEL)) {
  763. ESP_LOGE(GPIO_TAG, "GPIO wakeup only supports level mode, but edge mode set. gpio_num:%u", gpio_num);
  764. return ESP_ERR_INVALID_ARG;
  765. }
  766. portENTER_CRITICAL(&gpio_context.gpio_spinlock);
  767. gpio_hal_deepsleep_wakeup_enable(gpio_context.gpio_hal, gpio_num, intr_type);
  768. portEXIT_CRITICAL(&gpio_context.gpio_spinlock);
  769. return ESP_OK;
  770. }
  771. esp_err_t gpio_deep_sleep_wakeup_disable(gpio_num_t gpio_num)
  772. {
  773. if (!gpio_hal_is_valid_deepsleep_wakeup_gpio(gpio_num)) {
  774. ESP_LOGE(GPIO_TAG, "GPIO %d does not support deep sleep wakeup", gpio_num);
  775. return ESP_ERR_INVALID_ARG;
  776. }
  777. portENTER_CRITICAL(&gpio_context.gpio_spinlock);
  778. gpio_hal_deepsleep_wakeup_disable(gpio_context.gpio_hal, gpio_num);
  779. portEXIT_CRITICAL(&gpio_context.gpio_spinlock);
  780. return ESP_OK;
  781. }
  782. #endif // SOC_GPIO_SUPPORT_DEEPSLEEP_WAKEUP