gpio.c 18 KB

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