gpio.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461
  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. static const char* GPIO_TAG = "gpio";
  24. #define GPIO_CHECK(a, str, ret_val) \
  25. if (!(a)) { \
  26. ESP_LOGE(GPIO_TAG,"%s(%d): %s", __FUNCTION__, __LINE__, str); \
  27. return (ret_val); \
  28. }
  29. const uint32_t GPIO_PIN_MUX_REG[GPIO_PIN_COUNT] = {
  30. GPIO_PIN_REG_0,
  31. GPIO_PIN_REG_1,
  32. GPIO_PIN_REG_2,
  33. GPIO_PIN_REG_3,
  34. GPIO_PIN_REG_4,
  35. GPIO_PIN_REG_5,
  36. GPIO_PIN_REG_6,
  37. GPIO_PIN_REG_7,
  38. GPIO_PIN_REG_8,
  39. GPIO_PIN_REG_9,
  40. GPIO_PIN_REG_10,
  41. GPIO_PIN_REG_11,
  42. GPIO_PIN_REG_12,
  43. GPIO_PIN_REG_13,
  44. GPIO_PIN_REG_14,
  45. GPIO_PIN_REG_15,
  46. GPIO_PIN_REG_16,
  47. GPIO_PIN_REG_17,
  48. GPIO_PIN_REG_18,
  49. GPIO_PIN_REG_19,
  50. 0,
  51. GPIO_PIN_REG_21,
  52. GPIO_PIN_REG_22,
  53. GPIO_PIN_REG_23,
  54. 0,
  55. GPIO_PIN_REG_25,
  56. GPIO_PIN_REG_26,
  57. GPIO_PIN_REG_27,
  58. 0,
  59. 0,
  60. 0,
  61. 0,
  62. GPIO_PIN_REG_32,
  63. GPIO_PIN_REG_33,
  64. GPIO_PIN_REG_34,
  65. GPIO_PIN_REG_35,
  66. GPIO_PIN_REG_36,
  67. GPIO_PIN_REG_37,
  68. GPIO_PIN_REG_38,
  69. GPIO_PIN_REG_39
  70. };
  71. typedef struct {
  72. gpio_isr_t fn; /*!< isr function */
  73. void* args; /*!< isr function args */
  74. } gpio_isr_func_t;
  75. static gpio_isr_func_t* gpio_isr_func = NULL;
  76. static gpio_isr_handle_t gpio_isr_handle;
  77. static portMUX_TYPE gpio_spinlock = portMUX_INITIALIZER_UNLOCKED;
  78. esp_err_t gpio_pullup_en(gpio_num_t gpio_num)
  79. {
  80. GPIO_CHECK(GPIO_IS_VALID_GPIO(gpio_num), "GPIO number error", ESP_ERR_INVALID_ARG);
  81. if (RTC_GPIO_IS_VALID_GPIO(gpio_num)) {
  82. rtc_gpio_pullup_en(gpio_num);
  83. } else {
  84. REG_SET_BIT(GPIO_PIN_MUX_REG[gpio_num], FUN_PU);
  85. }
  86. return ESP_OK;
  87. }
  88. esp_err_t gpio_pullup_dis(gpio_num_t gpio_num)
  89. {
  90. GPIO_CHECK(GPIO_IS_VALID_GPIO(gpio_num), "GPIO number error", ESP_ERR_INVALID_ARG);
  91. if (RTC_GPIO_IS_VALID_GPIO(gpio_num)) {
  92. rtc_gpio_pullup_dis(gpio_num);
  93. } else {
  94. REG_CLR_BIT(GPIO_PIN_MUX_REG[gpio_num], FUN_PU);
  95. }
  96. return ESP_OK;
  97. }
  98. esp_err_t gpio_pulldown_en(gpio_num_t gpio_num)
  99. {
  100. GPIO_CHECK(GPIO_IS_VALID_GPIO(gpio_num), "GPIO number error", ESP_ERR_INVALID_ARG);
  101. if (RTC_GPIO_IS_VALID_GPIO(gpio_num)) {
  102. rtc_gpio_pulldown_en(gpio_num);
  103. } else {
  104. REG_SET_BIT(GPIO_PIN_MUX_REG[gpio_num], FUN_PD);
  105. }
  106. return ESP_OK;
  107. }
  108. esp_err_t gpio_pulldown_dis(gpio_num_t gpio_num)
  109. {
  110. GPIO_CHECK(GPIO_IS_VALID_GPIO(gpio_num), "GPIO number error", ESP_ERR_INVALID_ARG);
  111. if (RTC_GPIO_IS_VALID_GPIO(gpio_num)) {
  112. rtc_gpio_pulldown_dis(gpio_num);
  113. } else {
  114. REG_CLR_BIT(GPIO_PIN_MUX_REG[gpio_num], FUN_PD);
  115. }
  116. return ESP_OK;
  117. }
  118. esp_err_t gpio_set_intr_type(gpio_num_t gpio_num, gpio_int_type_t intr_type)
  119. {
  120. GPIO_CHECK(GPIO_IS_VALID_GPIO(gpio_num), "GPIO number error", ESP_ERR_INVALID_ARG);
  121. GPIO_CHECK(intr_type < GPIO_INTR_MAX, "GPIO interrupt type error", ESP_ERR_INVALID_ARG);
  122. GPIO.pin[gpio_num].int_type = intr_type;
  123. return ESP_OK;
  124. }
  125. static esp_err_t gpio_intr_enable_on_core (gpio_num_t gpio_num, uint32_t core_id)
  126. {
  127. GPIO_CHECK(GPIO_IS_VALID_GPIO(gpio_num), "GPIO number error", ESP_ERR_INVALID_ARG);
  128. if (core_id == 0) {
  129. GPIO.pin[gpio_num].int_ena = GPIO_PRO_CPU_INTR_ENA; //enable pro cpu intr
  130. } else {
  131. GPIO.pin[gpio_num].int_ena = GPIO_APP_CPU_INTR_ENA; //enable pro cpu intr
  132. }
  133. return ESP_OK;
  134. }
  135. esp_err_t gpio_intr_enable(gpio_num_t gpio_num)
  136. {
  137. return gpio_intr_enable_on_core (gpio_num, xPortGetCoreID());
  138. }
  139. esp_err_t gpio_intr_disable(gpio_num_t gpio_num)
  140. {
  141. GPIO_CHECK(GPIO_IS_VALID_GPIO(gpio_num), "GPIO number error", ESP_ERR_INVALID_ARG);
  142. GPIO.pin[gpio_num].int_ena = 0; //disable GPIO intr
  143. return ESP_OK;
  144. }
  145. static esp_err_t gpio_output_disable(gpio_num_t gpio_num)
  146. {
  147. GPIO_CHECK(GPIO_IS_VALID_GPIO(gpio_num), "GPIO number error", ESP_ERR_INVALID_ARG);
  148. if (gpio_num < 32) {
  149. GPIO.enable_w1tc = (0x1 << gpio_num);
  150. } else {
  151. GPIO.enable1_w1tc.data = (0x1 << (gpio_num - 32));
  152. }
  153. return ESP_OK;
  154. }
  155. static esp_err_t gpio_output_enable(gpio_num_t gpio_num)
  156. {
  157. GPIO_CHECK(GPIO_IS_VALID_OUTPUT_GPIO(gpio_num), "GPIO output gpio_num error", ESP_ERR_INVALID_ARG);
  158. if (gpio_num < 32) {
  159. GPIO.enable_w1ts = (0x1 << gpio_num);
  160. } else {
  161. GPIO.enable1_w1ts.data = (0x1 << (gpio_num - 32));
  162. }
  163. return ESP_OK;
  164. }
  165. esp_err_t gpio_set_level(gpio_num_t gpio_num, uint32_t level)
  166. {
  167. GPIO_CHECK(GPIO_IS_VALID_OUTPUT_GPIO(gpio_num), "GPIO output gpio_num error", ESP_ERR_INVALID_ARG);
  168. if (level) {
  169. if (gpio_num < 32) {
  170. GPIO.out_w1ts = (1 << gpio_num);
  171. } else {
  172. GPIO.out1_w1ts.data = (1 << (gpio_num - 32));
  173. }
  174. } else {
  175. if (gpio_num < 32) {
  176. GPIO.out_w1tc = (1 << gpio_num);
  177. } else {
  178. GPIO.out1_w1tc.data = (1 << (gpio_num - 32));
  179. }
  180. }
  181. return ESP_OK;
  182. }
  183. int gpio_get_level(gpio_num_t gpio_num)
  184. {
  185. if (gpio_num < 32) {
  186. return (GPIO.in >> gpio_num) & 0x1;
  187. } else {
  188. return (GPIO.in1.data >> (gpio_num - 32)) & 0x1;
  189. }
  190. }
  191. esp_err_t gpio_set_pull_mode(gpio_num_t gpio_num, gpio_pull_mode_t pull)
  192. {
  193. GPIO_CHECK(GPIO_IS_VALID_GPIO(gpio_num), "GPIO number error", ESP_ERR_INVALID_ARG);
  194. GPIO_CHECK(pull <= GPIO_FLOATING, "GPIO pull mode error", ESP_ERR_INVALID_ARG);
  195. esp_err_t ret = ESP_OK;
  196. switch (pull) {
  197. case GPIO_PULLUP_ONLY:
  198. gpio_pulldown_dis(gpio_num);
  199. gpio_pullup_en(gpio_num);
  200. break;
  201. case GPIO_PULLDOWN_ONLY:
  202. gpio_pulldown_en(gpio_num);
  203. gpio_pullup_dis(gpio_num);
  204. break;
  205. case GPIO_PULLUP_PULLDOWN:
  206. gpio_pulldown_en(gpio_num);
  207. gpio_pullup_en(gpio_num);
  208. break;
  209. case GPIO_FLOATING:
  210. gpio_pulldown_dis(gpio_num);
  211. gpio_pullup_dis(gpio_num);
  212. break;
  213. default:
  214. ESP_LOGE(GPIO_TAG, "Unknown pull up/down mode,gpio_num=%u,pull=%u", gpio_num, pull);
  215. ret = ESP_ERR_INVALID_ARG;
  216. break;
  217. }
  218. return ret;
  219. }
  220. esp_err_t gpio_set_direction(gpio_num_t gpio_num, gpio_mode_t mode)
  221. {
  222. GPIO_CHECK(GPIO_IS_VALID_GPIO(gpio_num), "GPIO number error", ESP_ERR_INVALID_ARG);
  223. if (gpio_num >= 34 && (mode & (GPIO_MODE_DEF_OUTPUT))) {
  224. ESP_LOGE(GPIO_TAG, "io_num=%d can only be input", gpio_num);
  225. return ESP_ERR_INVALID_ARG;
  226. }
  227. esp_err_t ret = ESP_OK;
  228. if (mode & GPIO_MODE_DEF_INPUT) {
  229. PIN_INPUT_ENABLE(GPIO_PIN_MUX_REG[gpio_num]);
  230. } else {
  231. PIN_INPUT_DISABLE(GPIO_PIN_MUX_REG[gpio_num]);
  232. }
  233. if (mode & GPIO_MODE_DEF_OUTPUT) {
  234. if (gpio_num < 32) {
  235. GPIO.enable_w1ts = (0x1 << gpio_num);
  236. } else {
  237. GPIO.enable1_w1ts.data = (0x1 << (gpio_num - 32));
  238. }
  239. } else {
  240. if (gpio_num < 32) {
  241. GPIO.enable_w1tc = (0x1 << gpio_num);
  242. } else {
  243. GPIO.enable1_w1tc.data = (0x1 << (gpio_num - 32));
  244. }
  245. }
  246. if (mode & GPIO_MODE_DEF_OD) {
  247. GPIO.pin[gpio_num].pad_driver = 1;
  248. } else {
  249. GPIO.pin[gpio_num].pad_driver = 0;
  250. }
  251. return ret;
  252. }
  253. esp_err_t gpio_config(gpio_config_t *pGPIOConfig)
  254. {
  255. uint64_t gpio_pin_mask = (pGPIOConfig->pin_bit_mask);
  256. uint32_t io_reg = 0;
  257. uint32_t io_num = 0;
  258. uint8_t input_en = 0;
  259. uint8_t output_en = 0;
  260. uint8_t od_en = 0;
  261. uint8_t pu_en = 0;
  262. uint8_t pd_en = 0;
  263. if (pGPIOConfig->pin_bit_mask == 0 || pGPIOConfig->pin_bit_mask >= (((uint64_t) 1) << GPIO_PIN_COUNT)) {
  264. ESP_LOGE(GPIO_TAG, "GPIO_PIN mask error ");
  265. return ESP_ERR_INVALID_ARG;
  266. }
  267. if ((pGPIOConfig->mode) & (GPIO_MODE_DEF_OUTPUT)) {
  268. //GPIO 34/35/36/37/38/39 can only be used as input mode;
  269. if ((gpio_pin_mask & ( GPIO_SEL_34 | GPIO_SEL_35 | GPIO_SEL_36 | GPIO_SEL_37 | GPIO_SEL_38 | GPIO_SEL_39))) {
  270. ESP_LOGE(GPIO_TAG, "GPIO34-39 can only be used as input mode");
  271. return ESP_ERR_INVALID_ARG;
  272. }
  273. }
  274. do {
  275. io_reg = GPIO_PIN_MUX_REG[io_num];
  276. if (((gpio_pin_mask >> io_num) & BIT(0)) && io_reg) {
  277. if(RTC_GPIO_IS_VALID_GPIO(io_num)){
  278. rtc_gpio_deinit(io_num);
  279. }
  280. if ((pGPIOConfig->mode) & GPIO_MODE_DEF_INPUT) {
  281. input_en = 1;
  282. PIN_INPUT_ENABLE(GPIO_PIN_MUX_REG[io_num]);
  283. } else {
  284. PIN_INPUT_DISABLE(GPIO_PIN_MUX_REG[io_num]);
  285. }
  286. if ((pGPIOConfig->mode) & GPIO_MODE_DEF_OD) {
  287. od_en = 1;
  288. GPIO.pin[io_num].pad_driver = 1; /*0x01 Open-drain */
  289. } else {
  290. GPIO.pin[io_num].pad_driver = 0; /*0x00 Normal gpio output */
  291. }
  292. if ((pGPIOConfig->mode) & GPIO_MODE_DEF_OUTPUT) {
  293. output_en = 1;
  294. gpio_matrix_out(io_num, SIG_GPIO_OUT_IDX, 0, 0);
  295. gpio_output_enable(io_num);
  296. } else {
  297. gpio_output_disable(io_num);
  298. }
  299. if (pGPIOConfig->pull_up_en) {
  300. pu_en = 1;
  301. gpio_pullup_en(io_num);
  302. } else {
  303. gpio_pullup_dis(io_num);
  304. }
  305. if (pGPIOConfig->pull_down_en) {
  306. pd_en = 1;
  307. gpio_pulldown_en(io_num);
  308. } else {
  309. gpio_pulldown_dis(io_num);
  310. }
  311. 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);
  312. gpio_set_intr_type(io_num, pGPIOConfig->intr_type);
  313. if (pGPIOConfig->intr_type) {
  314. gpio_intr_enable(io_num);
  315. } else {
  316. gpio_intr_disable(io_num);
  317. }
  318. PIN_FUNC_SELECT(io_reg, PIN_FUNC_GPIO); /*function number 2 is GPIO_FUNC for each pin */
  319. }
  320. io_num++;
  321. } while (io_num < GPIO_PIN_COUNT);
  322. return ESP_OK;
  323. }
  324. void IRAM_ATTR gpio_intr_service(void* arg)
  325. {
  326. //GPIO intr process
  327. uint32_t gpio_num = 0;
  328. //read status to get interrupt status for GPIO0-31
  329. uint32_t gpio_intr_status;
  330. gpio_intr_status = GPIO.status;
  331. //read status1 to get interrupt status for GPIO32-39
  332. uint32_t gpio_intr_status_h;
  333. gpio_intr_status_h = GPIO.status1.intr_st;
  334. if (gpio_isr_func == NULL) {
  335. return;
  336. }
  337. do {
  338. if (gpio_num < 32) {
  339. if (gpio_intr_status & BIT(gpio_num)) { //gpio0-gpio31
  340. if (gpio_isr_func[gpio_num].fn != NULL) {
  341. gpio_isr_func[gpio_num].fn(gpio_isr_func[gpio_num].args);
  342. }
  343. GPIO.status_w1tc = BIT(gpio_num);
  344. }
  345. } else {
  346. if (gpio_intr_status_h & BIT(gpio_num - 32)) {
  347. if (gpio_isr_func[gpio_num].fn != NULL) {
  348. gpio_isr_func[gpio_num].fn(gpio_isr_func[gpio_num].args);
  349. }
  350. GPIO.status1_w1tc.intr_st = BIT(gpio_num - 32);
  351. }
  352. }
  353. } while (++gpio_num < GPIO_PIN_COUNT);
  354. }
  355. esp_err_t gpio_isr_handler_add(gpio_num_t gpio_num, gpio_isr_t isr_handler, void* args)
  356. {
  357. GPIO_CHECK(gpio_isr_func != NULL, "GPIO isr service is not installed, call gpio_install_isr_service() first", ESP_ERR_INVALID_STATE);
  358. GPIO_CHECK(GPIO_IS_VALID_GPIO(gpio_num), "GPIO number error", ESP_ERR_INVALID_ARG);
  359. portENTER_CRITICAL(&gpio_spinlock);
  360. gpio_intr_disable(gpio_num);
  361. if (gpio_isr_func) {
  362. gpio_isr_func[gpio_num].fn = isr_handler;
  363. gpio_isr_func[gpio_num].args = args;
  364. }
  365. gpio_intr_enable_on_core (gpio_num, esp_intr_get_cpu(gpio_isr_handle));
  366. portEXIT_CRITICAL(&gpio_spinlock);
  367. return ESP_OK;
  368. }
  369. esp_err_t gpio_isr_handler_remove(gpio_num_t gpio_num)
  370. {
  371. GPIO_CHECK(gpio_isr_func != NULL, "GPIO isr service is not installed, call gpio_install_isr_service() first", ESP_ERR_INVALID_STATE);
  372. GPIO_CHECK(GPIO_IS_VALID_GPIO(gpio_num), "GPIO number error", ESP_ERR_INVALID_ARG);
  373. portENTER_CRITICAL(&gpio_spinlock);
  374. gpio_intr_disable(gpio_num);
  375. if (gpio_isr_func) {
  376. gpio_isr_func[gpio_num].fn = NULL;
  377. gpio_isr_func[gpio_num].args = NULL;
  378. }
  379. portEXIT_CRITICAL(&gpio_spinlock);
  380. return ESP_OK;
  381. }
  382. esp_err_t gpio_install_isr_service(int intr_alloc_flags)
  383. {
  384. GPIO_CHECK(gpio_isr_func == NULL, "GPIO isr service already installed", ESP_FAIL);
  385. esp_err_t ret;
  386. portENTER_CRITICAL(&gpio_spinlock);
  387. gpio_isr_func = (gpio_isr_func_t*) calloc(GPIO_NUM_MAX, sizeof(gpio_isr_func_t));
  388. if (gpio_isr_func == NULL) {
  389. ret = ESP_ERR_NO_MEM;
  390. } else {
  391. ret = gpio_isr_register(gpio_intr_service, NULL, intr_alloc_flags, &gpio_isr_handle);
  392. }
  393. portEXIT_CRITICAL(&gpio_spinlock);
  394. return ret;
  395. }
  396. void gpio_uninstall_isr_service()
  397. {
  398. if (gpio_isr_func == NULL) {
  399. return;
  400. }
  401. portENTER_CRITICAL(&gpio_spinlock);
  402. esp_intr_free(gpio_isr_handle);
  403. free(gpio_isr_func);
  404. gpio_isr_func = NULL;
  405. portEXIT_CRITICAL(&gpio_spinlock);
  406. return;
  407. }
  408. esp_err_t gpio_isr_register(void (*fn)(void*), void * arg, int intr_alloc_flags, gpio_isr_handle_t *handle)
  409. {
  410. GPIO_CHECK(fn, "GPIO ISR null", ESP_ERR_INVALID_ARG);
  411. return esp_intr_alloc(ETS_GPIO_INTR_SOURCE, intr_alloc_flags, fn, arg, handle);
  412. }
  413. /*only level interrupt can be used for wake-up function*/
  414. esp_err_t gpio_wakeup_enable(gpio_num_t gpio_num, gpio_int_type_t intr_type)
  415. {
  416. GPIO_CHECK(GPIO_IS_VALID_GPIO(gpio_num), "GPIO number error", ESP_ERR_INVALID_ARG);
  417. esp_err_t ret = ESP_OK;
  418. if (( intr_type == GPIO_INTR_LOW_LEVEL ) || ( intr_type == GPIO_INTR_HIGH_LEVEL )) {
  419. GPIO.pin[gpio_num].int_type = intr_type;
  420. GPIO.pin[gpio_num].wakeup_enable = 0x1;
  421. } else {
  422. ESP_LOGE(GPIO_TAG, "GPIO wakeup only support Level mode,but edge mode set. gpio_num:%u", gpio_num);
  423. ret = ESP_ERR_INVALID_ARG;
  424. }
  425. return ret;
  426. }
  427. esp_err_t gpio_wakeup_disable(gpio_num_t gpio_num)
  428. {
  429. GPIO_CHECK(GPIO_IS_VALID_GPIO(gpio_num), "GPIO number error", ESP_ERR_INVALID_ARG);
  430. GPIO.pin[gpio_num].wakeup_enable = 0;
  431. return ESP_OK;
  432. }