gpio.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353
  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 "freertos/FreeRTOS.h"
  17. #include "freertos/xtensa_api.h"
  18. #include "driver/gpio.h"
  19. #include "driver/rtc_io.h"
  20. #include "soc/soc.h"
  21. #include "esp_log.h"
  22. static const char* GPIO_TAG = "GPIO";
  23. #define GPIO_CHECK(a, str, ret_val) if (!(a)) { \
  24. ESP_LOGE(GPIO_TAG,"%s:%d (%s):%s", __FILE__, __LINE__, __FUNCTION__, str); \
  25. return (ret_val); \
  26. }
  27. const uint32_t GPIO_PIN_MUX_REG[GPIO_PIN_COUNT] = {
  28. GPIO_PIN_REG_0,
  29. GPIO_PIN_REG_1,
  30. GPIO_PIN_REG_2,
  31. GPIO_PIN_REG_3,
  32. GPIO_PIN_REG_4,
  33. GPIO_PIN_REG_5,
  34. GPIO_PIN_REG_6,
  35. GPIO_PIN_REG_7,
  36. GPIO_PIN_REG_8,
  37. GPIO_PIN_REG_9,
  38. GPIO_PIN_REG_10,
  39. GPIO_PIN_REG_11,
  40. GPIO_PIN_REG_12,
  41. GPIO_PIN_REG_13,
  42. GPIO_PIN_REG_14,
  43. GPIO_PIN_REG_15,
  44. GPIO_PIN_REG_16,
  45. GPIO_PIN_REG_17,
  46. GPIO_PIN_REG_18,
  47. GPIO_PIN_REG_19,
  48. 0,
  49. GPIO_PIN_REG_21,
  50. GPIO_PIN_REG_22,
  51. GPIO_PIN_REG_23,
  52. 0,
  53. GPIO_PIN_REG_25,
  54. GPIO_PIN_REG_26,
  55. GPIO_PIN_REG_27,
  56. 0,
  57. 0,
  58. 0,
  59. 0,
  60. GPIO_PIN_REG_32,
  61. GPIO_PIN_REG_33,
  62. GPIO_PIN_REG_34,
  63. GPIO_PIN_REG_35,
  64. GPIO_PIN_REG_36,
  65. GPIO_PIN_REG_37,
  66. GPIO_PIN_REG_38,
  67. GPIO_PIN_REG_39
  68. };
  69. esp_err_t gpio_pullup_en(gpio_num_t gpio_num) {
  70. GPIO_CHECK(GPIO_IS_VALID_GPIO(gpio_num), "GPIO number error", ESP_ERR_INVALID_ARG);
  71. if(RTC_GPIO_IS_VALID_GPIO(gpio_num)){
  72. rtc_gpio_pullup_en(gpio_num);
  73. }else{
  74. REG_SET_BIT(GPIO_PIN_MUX_REG[gpio_num], FUN_PU);
  75. }
  76. return ESP_OK;
  77. }
  78. esp_err_t gpio_pullup_dis(gpio_num_t gpio_num) {
  79. GPIO_CHECK(GPIO_IS_VALID_GPIO(gpio_num), "GPIO number error", ESP_ERR_INVALID_ARG);
  80. if(RTC_GPIO_IS_VALID_GPIO(gpio_num)){
  81. rtc_gpio_pullup_dis(gpio_num);
  82. }else{
  83. REG_CLR_BIT(GPIO_PIN_MUX_REG[gpio_num], FUN_PU);
  84. }
  85. return ESP_OK;
  86. }
  87. esp_err_t gpio_pulldown_en(gpio_num_t gpio_num) {
  88. GPIO_CHECK(GPIO_IS_VALID_GPIO(gpio_num), "GPIO number error", ESP_ERR_INVALID_ARG);
  89. if(RTC_GPIO_IS_VALID_GPIO(gpio_num)){
  90. rtc_gpio_pulldown_en(gpio_num);
  91. }else{
  92. REG_SET_BIT(GPIO_PIN_MUX_REG[gpio_num], FUN_PD);
  93. }
  94. return ESP_OK;
  95. }
  96. esp_err_t gpio_pulldown_dis(gpio_num_t gpio_num) {
  97. GPIO_CHECK(GPIO_IS_VALID_GPIO(gpio_num), "GPIO number error", ESP_ERR_INVALID_ARG);
  98. if(RTC_GPIO_IS_VALID_GPIO(gpio_num)){
  99. rtc_gpio_pulldown_dis(gpio_num);
  100. }else{
  101. REG_CLR_BIT(GPIO_PIN_MUX_REG[gpio_num], FUN_PD);
  102. }
  103. return ESP_OK;
  104. }
  105. esp_err_t gpio_set_intr_type(gpio_num_t gpio_num, gpio_int_type_t intr_type)
  106. {
  107. GPIO_CHECK(GPIO_IS_VALID_GPIO(gpio_num), "GPIO number error", ESP_ERR_INVALID_ARG);
  108. GPIO_CHECK(intr_type < GPIO_INTR_MAX, "GPIO interrupt type error", ESP_ERR_INVALID_ARG);
  109. GPIO.pin[gpio_num].int_type = intr_type;
  110. return ESP_OK;
  111. }
  112. esp_err_t gpio_intr_enable(gpio_num_t gpio_num)
  113. {
  114. GPIO_CHECK(GPIO_IS_VALID_GPIO(gpio_num), "GPIO number error", ESP_ERR_INVALID_ARG);
  115. if (xPortGetCoreID() == 0) {
  116. GPIO.pin[gpio_num].int_ena = GPIO_PRO_CPU_INTR_ENA; //enable pro cpu intr
  117. } else {
  118. GPIO.pin[gpio_num].int_ena = GPIO_APP_CPU_INTR_ENA; //enable pro cpu intr
  119. }
  120. return ESP_OK;
  121. }
  122. esp_err_t gpio_intr_disable(gpio_num_t gpio_num)
  123. {
  124. GPIO_CHECK(GPIO_IS_VALID_GPIO(gpio_num), "GPIO number error", ESP_ERR_INVALID_ARG);
  125. GPIO.pin[gpio_num].int_ena = 0; //disable GPIO intr
  126. return ESP_OK;
  127. }
  128. static esp_err_t gpio_output_disable(gpio_num_t gpio_num)
  129. {
  130. GPIO_CHECK(GPIO_IS_VALID_GPIO(gpio_num), "GPIO number error", ESP_ERR_INVALID_ARG);
  131. if (gpio_num < 32) {
  132. GPIO.enable_w1tc = (0x1 << gpio_num);
  133. } else {
  134. GPIO.enable1_w1tc.data = (0x1 << (gpio_num - 32));
  135. }
  136. return ESP_OK;
  137. }
  138. static esp_err_t gpio_output_enable(gpio_num_t gpio_num)
  139. {
  140. GPIO_CHECK(GPIO_IS_VALID_OUTPUT_GPIO(gpio_num), "GPIO output gpio_num error", ESP_ERR_INVALID_ARG);
  141. if (gpio_num < 32) {
  142. GPIO.enable_w1ts = (0x1 << gpio_num);
  143. } else {
  144. GPIO.enable1_w1ts.data = (0x1 << (gpio_num - 32));
  145. }
  146. return ESP_OK;
  147. }
  148. esp_err_t gpio_set_level(gpio_num_t gpio_num, uint32_t level)
  149. {
  150. GPIO_CHECK(GPIO_IS_VALID_GPIO(gpio_num), "GPIO number error", ESP_ERR_INVALID_ARG);
  151. if (level) {
  152. if (gpio_num < 32) {
  153. GPIO.out_w1ts = (1 << gpio_num);
  154. } else {
  155. GPIO.out1_w1ts.data = (1 << (gpio_num - 32));
  156. }
  157. } else {
  158. if (gpio_num < 32) {
  159. GPIO.out_w1tc = (1 << gpio_num);
  160. } else {
  161. GPIO.out1_w1tc.data = (1 << (gpio_num - 32));
  162. }
  163. }
  164. return ESP_OK;
  165. }
  166. int gpio_get_level(gpio_num_t gpio_num)
  167. {
  168. if (gpio_num < 32) {
  169. return (GPIO.in >> gpio_num) & 0x1;
  170. } else {
  171. return (GPIO.in1.data >> (gpio_num - 32)) & 0x1;
  172. }
  173. }
  174. esp_err_t gpio_set_pull_mode(gpio_num_t gpio_num, gpio_pull_mode_t pull)
  175. {
  176. GPIO_CHECK(GPIO_IS_VALID_GPIO(gpio_num), "GPIO number error", ESP_ERR_INVALID_ARG);
  177. GPIO_CHECK(pull <= GPIO_FLOATING, "GPIO pull mode error", ESP_ERR_INVALID_ARG);
  178. esp_err_t ret = ESP_OK;
  179. switch (pull) {
  180. case GPIO_PULLUP_ONLY:
  181. gpio_pulldown_dis(gpio_num);
  182. gpio_pullup_en(gpio_num);
  183. break;
  184. case GPIO_PULLDOWN_ONLY:
  185. gpio_pulldown_en(gpio_num);
  186. gpio_pullup_dis(gpio_num);
  187. break;
  188. case GPIO_PULLUP_PULLDOWN:
  189. gpio_pulldown_en(gpio_num);
  190. gpio_pullup_en(gpio_num);
  191. break;
  192. case GPIO_FLOATING:
  193. gpio_pulldown_dis(gpio_num);
  194. gpio_pullup_dis(gpio_num);
  195. break;
  196. default:
  197. ESP_LOGE(GPIO_TAG, "Unknown pull up/down mode,gpio_num=%u,pull=%u", gpio_num, pull);
  198. ret = ESP_ERR_INVALID_ARG;
  199. break;
  200. }
  201. return ret;
  202. }
  203. esp_err_t gpio_set_direction(gpio_num_t gpio_num, gpio_mode_t mode)
  204. {
  205. GPIO_CHECK(GPIO_IS_VALID_GPIO(gpio_num), "GPIO number error", ESP_ERR_INVALID_ARG);
  206. if (gpio_num >= 34 && (mode & (GPIO_MODE_DEF_OUTPUT))) {
  207. ESP_LOGE(GPIO_TAG, "io_num=%d can only be input", gpio_num);
  208. return ESP_ERR_INVALID_ARG;
  209. }
  210. esp_err_t ret = ESP_OK;
  211. if (mode & GPIO_MODE_DEF_INPUT) {
  212. PIN_INPUT_ENABLE(GPIO_PIN_MUX_REG[gpio_num]);
  213. } else {
  214. PIN_INPUT_DISABLE(GPIO_PIN_MUX_REG[gpio_num]);
  215. }
  216. if (mode & GPIO_MODE_DEF_OUTPUT) {
  217. if (gpio_num < 32) {
  218. GPIO.enable_w1ts = (0x1 << gpio_num);
  219. } else {
  220. GPIO.enable1_w1ts.data = (0x1 << (gpio_num - 32));
  221. }
  222. } else {
  223. if (gpio_num < 32) {
  224. GPIO.enable_w1tc = (0x1 << gpio_num);
  225. } else {
  226. GPIO.enable1_w1tc.data = (0x1 << (gpio_num - 32));
  227. }
  228. }
  229. if (mode & GPIO_MODE_DEF_OD) {
  230. GPIO.pin[gpio_num].pad_driver = 1;
  231. } else {
  232. GPIO.pin[gpio_num].pad_driver = 0;
  233. }
  234. return ret;
  235. }
  236. esp_err_t gpio_config(gpio_config_t *pGPIOConfig)
  237. {
  238. uint64_t gpio_pin_mask = (pGPIOConfig->pin_bit_mask);
  239. uint32_t io_reg = 0;
  240. uint32_t io_num = 0;
  241. uint8_t input_en = 0;
  242. uint8_t output_en = 0;
  243. uint8_t od_en = 0;
  244. uint8_t pu_en = 0;
  245. uint8_t pd_en = 0;
  246. if (pGPIOConfig->pin_bit_mask == 0 || pGPIOConfig->pin_bit_mask >= (((uint64_t) 1) << GPIO_PIN_COUNT)) {
  247. ESP_LOGE(GPIO_TAG, "GPIO_PIN mask error ");
  248. return ESP_ERR_INVALID_ARG;
  249. }
  250. if ((pGPIOConfig->mode) & (GPIO_MODE_DEF_OUTPUT)) {
  251. //GPIO 34/35/36/37/38/39 can only be used as input mode;
  252. if ((gpio_pin_mask & ( GPIO_SEL_34 | GPIO_SEL_35 | GPIO_SEL_36 | GPIO_SEL_37 | GPIO_SEL_38 | GPIO_SEL_39))) {
  253. ESP_LOGE(GPIO_TAG, "GPIO34-39 can only be used as input mode");
  254. return ESP_ERR_INVALID_ARG;
  255. }
  256. }
  257. do {
  258. io_reg = GPIO_PIN_MUX_REG[io_num];
  259. if (((gpio_pin_mask >> io_num) & BIT(0)) && io_reg) {
  260. if(RTC_GPIO_IS_VALID_GPIO(io_num)){
  261. rtc_gpio_deinit(io_num);
  262. }
  263. if ((pGPIOConfig->mode) & GPIO_MODE_DEF_INPUT) {
  264. input_en = 1;
  265. PIN_INPUT_ENABLE(GPIO_PIN_MUX_REG[io_num]);
  266. } else {
  267. PIN_INPUT_DISABLE(GPIO_PIN_MUX_REG[io_num]);
  268. }
  269. if ((pGPIOConfig->mode) & GPIO_MODE_DEF_OD) {
  270. od_en = 1;
  271. GPIO.pin[io_num].pad_driver = 1; /*0x01 Open-drain */
  272. } else {
  273. GPIO.pin[io_num].pad_driver = 0; /*0x00 Normal gpio output */
  274. }
  275. if ((pGPIOConfig->mode) & GPIO_MODE_DEF_OUTPUT) {
  276. output_en = 1;
  277. gpio_output_enable(io_num);
  278. } else {
  279. gpio_output_disable(io_num);
  280. }
  281. if (pGPIOConfig->pull_up_en) {
  282. pu_en = 1;
  283. gpio_pullup_en(io_num);
  284. } else {
  285. gpio_pullup_dis(io_num);
  286. }
  287. if (pGPIOConfig->pull_down_en) {
  288. pd_en = 1;
  289. gpio_pulldown_en(io_num);
  290. } else {
  291. gpio_pulldown_dis(io_num);
  292. }
  293. 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);
  294. gpio_set_intr_type(io_num, pGPIOConfig->intr_type);
  295. if (pGPIOConfig->intr_type) {
  296. gpio_intr_enable(io_num);
  297. } else {
  298. gpio_intr_disable(io_num);
  299. }
  300. PIN_FUNC_SELECT(io_reg, PIN_FUNC_GPIO); /*function number 2 is GPIO_FUNC for each pin */
  301. }
  302. io_num++;
  303. } while (io_num < GPIO_PIN_COUNT);
  304. return ESP_OK;
  305. }
  306. esp_err_t gpio_isr_register(uint32_t gpio_intr_num, void (*fn)(void*), void * arg)
  307. {
  308. GPIO_CHECK(fn, "GPIO ISR null", ESP_ERR_INVALID_ARG);
  309. ESP_INTR_DISABLE(gpio_intr_num);
  310. intr_matrix_set(xPortGetCoreID(), ETS_GPIO_INTR_SOURCE, gpio_intr_num);
  311. xt_set_interrupt_handler(gpio_intr_num, fn, arg);
  312. ESP_INTR_ENABLE(gpio_intr_num);
  313. return ESP_OK;
  314. }
  315. /*only level interrupt can be used for wake-up function*/
  316. esp_err_t gpio_wakeup_enable(gpio_num_t gpio_num, gpio_int_type_t intr_type)
  317. {
  318. GPIO_CHECK(GPIO_IS_VALID_GPIO(gpio_num), "GPIO number error", ESP_ERR_INVALID_ARG);
  319. esp_err_t ret = ESP_OK;
  320. if ((intr_type == GPIO_INTR_LOW_LEVEL) || (intr_type == GPIO_INTR_HIGH_LEVEL)) {
  321. GPIO.pin[gpio_num].int_type = intr_type;
  322. GPIO.pin[gpio_num].wakeup_enable = 0x1;
  323. } else {
  324. ESP_LOGE(GPIO_TAG, "GPIO wakeup only support Level mode,but edge mode set. gpio_num:%u", gpio_num);
  325. ret = ESP_ERR_INVALID_ARG;
  326. }
  327. return ret;
  328. }
  329. esp_err_t gpio_wakeup_disable(gpio_num_t gpio_num)
  330. {
  331. GPIO_CHECK(GPIO_IS_VALID_GPIO(gpio_num), "GPIO number error", ESP_ERR_INVALID_ARG);
  332. GPIO.pin[gpio_num].wakeup_enable = 0;
  333. return ESP_OK;
  334. }