gpio.c 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934
  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. gpio_hal_iomux_func_sel(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. #if SOC_GPIO_SUPPORT_SLP_SWITCH && CONFIG_ESP32C3_LIGHTSLEEP_GPIO_RESET_WORKAROUND
  504. gpio_hal_sleep_sel_en(gpio_context.gpio_hal, gpio_num);
  505. #endif
  506. portEXIT_CRITICAL(&gpio_context.gpio_spinlock);
  507. return ret;
  508. }
  509. esp_err_t gpio_set_drive_capability(gpio_num_t gpio_num, gpio_drive_cap_t strength)
  510. {
  511. GPIO_CHECK(GPIO_IS_VALID_OUTPUT_GPIO(gpio_num), "GPIO number error", ESP_ERR_INVALID_ARG);
  512. GPIO_CHECK(strength < GPIO_DRIVE_CAP_MAX, "GPIO drive capability error", ESP_ERR_INVALID_ARG);
  513. esp_err_t ret = ESP_OK;
  514. if (!rtc_gpio_is_valid_gpio(gpio_num) || SOC_GPIO_SUPPORT_RTC_INDEPENDENT) {
  515. portENTER_CRITICAL(&gpio_context.gpio_spinlock);
  516. gpio_hal_set_drive_capability(gpio_context.gpio_hal, gpio_num, strength);
  517. portEXIT_CRITICAL(&gpio_context.gpio_spinlock);
  518. } else {
  519. #if SOC_RTCIO_INPUT_OUTPUT_SUPPORTED
  520. ret = rtc_gpio_set_drive_capability(gpio_num, strength);
  521. #else
  522. abort(); // This should be eliminated as unreachable, unless a programming error has occured
  523. #endif
  524. }
  525. return ret;
  526. }
  527. esp_err_t gpio_get_drive_capability(gpio_num_t gpio_num, gpio_drive_cap_t *strength)
  528. {
  529. GPIO_CHECK(GPIO_IS_VALID_OUTPUT_GPIO(gpio_num), "GPIO number error", ESP_ERR_INVALID_ARG);
  530. GPIO_CHECK(strength != NULL, "GPIO drive capability pointer error", ESP_ERR_INVALID_ARG);
  531. esp_err_t ret = ESP_OK;
  532. if (!rtc_gpio_is_valid_gpio(gpio_num) || SOC_GPIO_SUPPORT_RTC_INDEPENDENT) {
  533. portENTER_CRITICAL(&gpio_context.gpio_spinlock);
  534. gpio_hal_get_drive_capability(gpio_context.gpio_hal, gpio_num, strength);
  535. portEXIT_CRITICAL(&gpio_context.gpio_spinlock);
  536. } else {
  537. #if SOC_RTCIO_INPUT_OUTPUT_SUPPORTED
  538. ret = rtc_gpio_get_drive_capability(gpio_num, strength);
  539. #else
  540. abort(); // This should be eliminated as unreachable, unless a programming error has occured
  541. #endif
  542. }
  543. return ret;
  544. }
  545. esp_err_t gpio_hold_en(gpio_num_t gpio_num)
  546. {
  547. GPIO_CHECK(GPIO_IS_VALID_OUTPUT_GPIO(gpio_num), "Only output-capable GPIO support this function", ESP_ERR_NOT_SUPPORTED);
  548. int ret = ESP_OK;
  549. if (rtc_gpio_is_valid_gpio(gpio_num)) {
  550. #if SOC_RTCIO_HOLD_SUPPORTED
  551. ret = rtc_gpio_hold_en(gpio_num);
  552. #endif
  553. } else if (GPIO_HOLD_MASK[gpio_num]) {
  554. portENTER_CRITICAL(&gpio_context.gpio_spinlock);
  555. gpio_hal_hold_en(gpio_context.gpio_hal, gpio_num);
  556. portEXIT_CRITICAL(&gpio_context.gpio_spinlock);
  557. } else {
  558. ret = ESP_ERR_NOT_SUPPORTED;
  559. }
  560. return ret;
  561. }
  562. esp_err_t gpio_hold_dis(gpio_num_t gpio_num)
  563. {
  564. GPIO_CHECK(GPIO_IS_VALID_OUTPUT_GPIO(gpio_num), "Only output-capable GPIO support this function", ESP_ERR_NOT_SUPPORTED);
  565. int ret = ESP_OK;
  566. if (rtc_gpio_is_valid_gpio(gpio_num)) {
  567. #if SOC_RTCIO_HOLD_SUPPORTED
  568. ret = rtc_gpio_hold_dis(gpio_num);
  569. #endif
  570. }else if (GPIO_HOLD_MASK[gpio_num]) {
  571. portENTER_CRITICAL(&gpio_context.gpio_spinlock);
  572. gpio_hal_hold_dis(gpio_context.gpio_hal, gpio_num);
  573. portEXIT_CRITICAL(&gpio_context.gpio_spinlock);
  574. } else {
  575. ret = ESP_ERR_NOT_SUPPORTED;
  576. }
  577. return ret;
  578. }
  579. void gpio_deep_sleep_hold_en(void)
  580. {
  581. portENTER_CRITICAL(&gpio_context.gpio_spinlock);
  582. gpio_hal_deep_sleep_hold_en(gpio_context.gpio_hal);
  583. portEXIT_CRITICAL(&gpio_context.gpio_spinlock);
  584. }
  585. void gpio_deep_sleep_hold_dis(void)
  586. {
  587. portENTER_CRITICAL(&gpio_context.gpio_spinlock);
  588. gpio_hal_deep_sleep_hold_dis(gpio_context.gpio_hal);
  589. portEXIT_CRITICAL(&gpio_context.gpio_spinlock);
  590. }
  591. #if SOC_GPIO_SUPPORT_FORCE_HOLD
  592. esp_err_t gpio_force_hold_all()
  593. {
  594. #if SOC_RTCIO_HOLD_SUPPORTED
  595. rtc_gpio_force_hold_all();
  596. #endif
  597. portENTER_CRITICAL(&gpio_context.gpio_spinlock);
  598. gpio_hal_force_hold_all(gpio_context.gpio_hal);
  599. portEXIT_CRITICAL(&gpio_context.gpio_spinlock);
  600. return ESP_OK;
  601. }
  602. esp_err_t gpio_force_unhold_all()
  603. {
  604. #if SOC_RTCIO_HOLD_SUPPORTED
  605. rtc_gpio_force_hold_dis_all();
  606. #endif
  607. portENTER_CRITICAL(&gpio_context.gpio_spinlock);
  608. gpio_hal_force_unhold_all();
  609. portEXIT_CRITICAL(&gpio_context.gpio_spinlock);
  610. return ESP_OK;
  611. }
  612. #endif
  613. void gpio_iomux_in(uint32_t gpio, uint32_t signal_idx)
  614. {
  615. gpio_hal_iomux_in(gpio_context.gpio_hal, gpio, signal_idx);
  616. }
  617. void gpio_iomux_out(uint8_t gpio_num, int func, bool oen_inv)
  618. {
  619. gpio_hal_iomux_out(gpio_context.gpio_hal, gpio_num, func, (uint32_t)oen_inv);
  620. }
  621. #if SOC_GPIO_SUPPORT_SLP_SWITCH
  622. static esp_err_t gpio_sleep_pullup_en(gpio_num_t gpio_num)
  623. {
  624. GPIO_CHECK(GPIO_IS_VALID_GPIO(gpio_num), "GPIO number error", ESP_ERR_INVALID_ARG);
  625. portENTER_CRITICAL(&gpio_context.gpio_spinlock);
  626. gpio_hal_sleep_pullup_en(gpio_context.gpio_hal, gpio_num);
  627. portEXIT_CRITICAL(&gpio_context.gpio_spinlock);
  628. return ESP_OK;
  629. }
  630. static esp_err_t gpio_sleep_pullup_dis(gpio_num_t gpio_num)
  631. {
  632. GPIO_CHECK(GPIO_IS_VALID_GPIO(gpio_num), "GPIO number error", ESP_ERR_INVALID_ARG);
  633. portENTER_CRITICAL(&gpio_context.gpio_spinlock);
  634. gpio_hal_sleep_pullup_dis(gpio_context.gpio_hal, gpio_num);
  635. portEXIT_CRITICAL(&gpio_context.gpio_spinlock);
  636. return ESP_OK;
  637. }
  638. static esp_err_t gpio_sleep_pulldown_en(gpio_num_t gpio_num)
  639. {
  640. GPIO_CHECK(GPIO_IS_VALID_GPIO(gpio_num), "GPIO number error", ESP_ERR_INVALID_ARG);
  641. portENTER_CRITICAL(&gpio_context.gpio_spinlock);
  642. gpio_hal_sleep_pulldown_en(gpio_context.gpio_hal, gpio_num);
  643. portEXIT_CRITICAL(&gpio_context.gpio_spinlock);
  644. return ESP_OK;
  645. }
  646. static esp_err_t gpio_sleep_pulldown_dis(gpio_num_t gpio_num)
  647. {
  648. GPIO_CHECK(GPIO_IS_VALID_GPIO(gpio_num), "GPIO number error", ESP_ERR_INVALID_ARG);
  649. portENTER_CRITICAL(&gpio_context.gpio_spinlock);
  650. gpio_hal_sleep_pulldown_dis(gpio_context.gpio_hal, gpio_num);
  651. portEXIT_CRITICAL(&gpio_context.gpio_spinlock);
  652. return ESP_OK;
  653. }
  654. static esp_err_t gpio_sleep_input_disable(gpio_num_t gpio_num)
  655. {
  656. GPIO_CHECK(GPIO_IS_VALID_GPIO(gpio_num), "GPIO number error", ESP_ERR_INVALID_ARG);
  657. gpio_hal_sleep_input_disable(gpio_context.gpio_hal, gpio_num);
  658. return ESP_OK;
  659. }
  660. static esp_err_t gpio_sleep_input_enable(gpio_num_t gpio_num)
  661. {
  662. GPIO_CHECK(GPIO_IS_VALID_GPIO(gpio_num), "GPIO number error", ESP_ERR_INVALID_ARG);
  663. gpio_hal_sleep_input_enable(gpio_context.gpio_hal, gpio_num);
  664. return ESP_OK;
  665. }
  666. static esp_err_t gpio_sleep_output_disable(gpio_num_t gpio_num)
  667. {
  668. GPIO_CHECK(GPIO_IS_VALID_GPIO(gpio_num), "GPIO number error", ESP_ERR_INVALID_ARG);
  669. gpio_hal_sleep_output_disable(gpio_context.gpio_hal, gpio_num);
  670. return ESP_OK;
  671. }
  672. static esp_err_t gpio_sleep_output_enable(gpio_num_t gpio_num)
  673. {
  674. GPIO_CHECK(GPIO_IS_VALID_OUTPUT_GPIO(gpio_num), "GPIO output gpio_num error", ESP_ERR_INVALID_ARG);
  675. gpio_hal_sleep_output_enable(gpio_context.gpio_hal, gpio_num);
  676. return ESP_OK;
  677. }
  678. esp_err_t gpio_sleep_set_direction(gpio_num_t gpio_num, gpio_mode_t mode)
  679. {
  680. GPIO_CHECK(GPIO_IS_VALID_GPIO(gpio_num), "GPIO number error", ESP_ERR_INVALID_ARG);
  681. if ((GPIO_IS_VALID_OUTPUT_GPIO(gpio_num) != true) && (mode & GPIO_MODE_DEF_OUTPUT)) {
  682. ESP_LOGE(GPIO_TAG, "io_num=%d can only be input", gpio_num);
  683. return ESP_ERR_INVALID_ARG;
  684. }
  685. esp_err_t ret = ESP_OK;
  686. if (mode & GPIO_MODE_DEF_INPUT) {
  687. gpio_sleep_input_enable(gpio_num);
  688. } else {
  689. gpio_sleep_input_disable(gpio_num);
  690. }
  691. if (mode & GPIO_MODE_DEF_OUTPUT) {
  692. gpio_sleep_output_enable(gpio_num);
  693. } else {
  694. gpio_sleep_output_disable(gpio_num);
  695. }
  696. return ret;
  697. }
  698. esp_err_t gpio_sleep_set_pull_mode(gpio_num_t gpio_num, gpio_pull_mode_t pull)
  699. {
  700. GPIO_CHECK(GPIO_IS_VALID_GPIO(gpio_num), "GPIO number error", ESP_ERR_INVALID_ARG);
  701. GPIO_CHECK(pull <= GPIO_FLOATING, "GPIO pull mode error", ESP_ERR_INVALID_ARG);
  702. esp_err_t ret = ESP_OK;
  703. switch (pull) {
  704. case GPIO_PULLUP_ONLY:
  705. gpio_sleep_pulldown_dis(gpio_num);
  706. gpio_sleep_pullup_en(gpio_num);
  707. break;
  708. case GPIO_PULLDOWN_ONLY:
  709. gpio_sleep_pulldown_en(gpio_num);
  710. gpio_sleep_pullup_dis(gpio_num);
  711. break;
  712. case GPIO_PULLUP_PULLDOWN:
  713. gpio_sleep_pulldown_en(gpio_num);
  714. gpio_sleep_pullup_en(gpio_num);
  715. break;
  716. case GPIO_FLOATING:
  717. gpio_sleep_pulldown_dis(gpio_num);
  718. gpio_sleep_pullup_dis(gpio_num);
  719. break;
  720. default:
  721. ESP_LOGE(GPIO_TAG, "Unknown pull up/down mode,gpio_num=%u,pull=%u", gpio_num, pull);
  722. ret = ESP_ERR_INVALID_ARG;
  723. break;
  724. }
  725. return ret;
  726. }
  727. esp_err_t gpio_sleep_sel_en(gpio_num_t gpio_num)
  728. {
  729. GPIO_CHECK(GPIO_IS_VALID_GPIO(gpio_num), "GPIO number error", ESP_ERR_INVALID_ARG);
  730. portENTER_CRITICAL(&gpio_context.gpio_spinlock);
  731. gpio_hal_sleep_sel_en(gpio_context.gpio_hal, gpio_num);
  732. portEXIT_CRITICAL(&gpio_context.gpio_spinlock);
  733. return ESP_OK;
  734. }
  735. esp_err_t gpio_sleep_sel_dis(gpio_num_t gpio_num)
  736. {
  737. GPIO_CHECK(GPIO_IS_VALID_GPIO(gpio_num), "GPIO number error", ESP_ERR_INVALID_ARG);
  738. portENTER_CRITICAL(&gpio_context.gpio_spinlock);
  739. gpio_hal_sleep_sel_dis(gpio_context.gpio_hal, gpio_num);
  740. portEXIT_CRITICAL(&gpio_context.gpio_spinlock);
  741. return ESP_OK;
  742. }
  743. #if CONFIG_GPIO_ESP32_SUPPORT_SWITCH_SLP_PULL
  744. esp_err_t gpio_sleep_pupd_config_apply(gpio_num_t gpio_num)
  745. {
  746. GPIO_CHECK(GPIO_IS_VALID_GPIO(gpio_num), "GPIO number error", ESP_ERR_INVALID_ARG);
  747. gpio_hal_sleep_pupd_config_apply(gpio_context.gpio_hal, gpio_num);
  748. return ESP_OK;
  749. }
  750. esp_err_t gpio_sleep_pupd_config_unapply(gpio_num_t gpio_num)
  751. {
  752. GPIO_CHECK(GPIO_IS_VALID_GPIO(gpio_num), "GPIO number error", ESP_ERR_INVALID_ARG);
  753. gpio_hal_sleep_pupd_config_unapply(gpio_context.gpio_hal, gpio_num);
  754. return ESP_OK;
  755. }
  756. #endif // CONFIG_GPIO_ESP32_SUPPORT_SWITCH_SLP_PULL
  757. #endif // SOC_GPIO_SUPPORT_SLP_SWITCH
  758. #if SOC_GPIO_SUPPORT_DEEPSLEEP_WAKEUP
  759. esp_err_t gpio_deep_sleep_wakeup_enable(gpio_num_t gpio_num, gpio_int_type_t intr_type)
  760. {
  761. if (!gpio_hal_is_valid_deepsleep_wakeup_gpio(gpio_num)) {
  762. ESP_LOGE(GPIO_TAG, "GPIO %d does not support deep sleep wakeup", gpio_num);
  763. return ESP_ERR_INVALID_ARG;
  764. }
  765. if ((intr_type != GPIO_INTR_LOW_LEVEL) && (intr_type != GPIO_INTR_HIGH_LEVEL)) {
  766. ESP_LOGE(GPIO_TAG, "GPIO wakeup only supports level mode, but edge mode set. gpio_num:%u", gpio_num);
  767. return ESP_ERR_INVALID_ARG;
  768. }
  769. portENTER_CRITICAL(&gpio_context.gpio_spinlock);
  770. gpio_hal_deepsleep_wakeup_enable(gpio_context.gpio_hal, gpio_num, intr_type);
  771. #if SOC_GPIO_SUPPORT_SLP_SWITCH && CONFIG_ESP32C3_LIGHTSLEEP_GPIO_RESET_WORKAROUND
  772. gpio_hal_sleep_sel_dis(gpio_context.gpio_hal, gpio_num);
  773. #endif
  774. portEXIT_CRITICAL(&gpio_context.gpio_spinlock);
  775. return ESP_OK;
  776. }
  777. esp_err_t gpio_deep_sleep_wakeup_disable(gpio_num_t gpio_num)
  778. {
  779. if (!gpio_hal_is_valid_deepsleep_wakeup_gpio(gpio_num)) {
  780. ESP_LOGE(GPIO_TAG, "GPIO %d does not support deep sleep wakeup", gpio_num);
  781. return ESP_ERR_INVALID_ARG;
  782. }
  783. portENTER_CRITICAL(&gpio_context.gpio_spinlock);
  784. gpio_hal_deepsleep_wakeup_disable(gpio_context.gpio_hal, gpio_num);
  785. #if SOC_GPIO_SUPPORT_SLP_SWITCH && CONFIG_ESP32C3_LIGHTSLEEP_GPIO_RESET_WORKAROUND
  786. gpio_hal_sleep_sel_en(gpio_context.gpio_hal, gpio_num);
  787. #endif
  788. portEXIT_CRITICAL(&gpio_context.gpio_spinlock);
  789. return ESP_OK;
  790. }
  791. #endif // SOC_GPIO_SUPPORT_DEEPSLEEP_WAKEUP