gpio.h 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. /*
  2. * ESPRESSIF MIT License
  3. *
  4. * Copyright (c) 2016 <ESPRESSIF SYSTEMS (SHANGHAI) PTE LTD>
  5. *
  6. * Permission is hereby granted for use on ESPRESSIF SYSTEMS ESP8266 only, in which case,
  7. * it is free of charge, to any person obtaining a copy of this software and associated
  8. * documentation files (the "Software"), to deal in the Software without restriction, including
  9. * without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense,
  10. * and/or sell copies of the Software, and to permit persons to whom the Software is furnished
  11. * to do so, subject to the following conditions:
  12. *
  13. * The above copyright notice and this permission notice shall be included in all copies or
  14. * substantial portions of the Software.
  15. *
  16. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  17. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
  18. * FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
  19. * COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
  20. * IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
  21. * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  22. *
  23. */
  24. #ifndef _GPIO_H_
  25. #define _GPIO_H_
  26. #define GPIO_PIN_ADDR(i) (GPIO_PIN0_ADDRESS + i*4)
  27. #define GPIO_ID_IS_PIN_REGISTER(reg_id) \
  28. ((reg_id >= GPIO_ID_PIN0) && (reg_id <= GPIO_ID_PIN(GPIO_PIN_COUNT-1)))
  29. #define GPIO_REGID_TO_PINIDX(reg_id) ((reg_id) - GPIO_ID_PIN0)
  30. typedef enum {
  31. GPIO_PIN_INTR_DISABLE = 0,
  32. GPIO_PIN_INTR_POSEDGE = 1,
  33. GPIO_PIN_INTR_NEGEDGE = 2,
  34. GPIO_PIN_INTR_ANYEDGE = 3,
  35. GPIO_PIN_INTR_LOLEVEL = 4,
  36. GPIO_PIN_INTR_HILEVEL = 5
  37. } GPIO_INT_TYPE;
  38. #define GPIO_OUTPUT_SET(gpio_no, bit_value) \
  39. gpio_output_set((bit_value)<<gpio_no, ((~(bit_value))&0x01)<<gpio_no, 1<<gpio_no,0)
  40. #define GPIO_DIS_OUTPUT(gpio_no) gpio_output_set(0,0,0, 1<<gpio_no)
  41. #define GPIO_INPUT_GET(gpio_no) ((gpio_input_get()>>gpio_no)&BIT0)
  42. /* GPIO interrupt handler, registered through gpio_intr_handler_register */
  43. typedef void (* gpio_intr_handler_fn_t)(uint32 intr_mask, void *arg);
  44. /*
  45. * Initialize GPIO. This includes reading the GPIO Configuration DataSet
  46. * to initialize "output enables" and pin configurations for each gpio pin.
  47. * Must be called once during startup.
  48. */
  49. void gpio_init(void);
  50. /*
  51. * Change GPIO pin output by setting, clearing, or disabling pins.
  52. * In general, it is expected that a bit will be set in at most one
  53. * of these masks. If a bit is clear in all masks, the output state
  54. * remains unchanged.
  55. *
  56. * There is no particular ordering guaranteed; so if the order of
  57. * writes is significant, calling code should divide a single call
  58. * into multiple calls.
  59. */
  60. void gpio_output_set(uint32 set_mask,
  61. uint32 clear_mask,
  62. uint32 enable_mask,
  63. uint32 disable_mask);
  64. /*
  65. * Sample the value of GPIO input pins and returns a bitmask.
  66. */
  67. uint32 gpio_input_get(void);
  68. /*
  69. * Set the specified GPIO register to the specified value.
  70. * This is a very general and powerful interface that is not
  71. * expected to be used during normal operation. It is intended
  72. * mainly for debug, or for unusual requirements.
  73. */
  74. void gpio_register_set(uint32 reg_id, uint32 value);
  75. /* Get the current value of the specified GPIO register. */
  76. uint32 gpio_register_get(uint32 reg_id);
  77. /*
  78. * Register an application-specific interrupt handler for GPIO pin
  79. * interrupts. Once the interrupt handler is called, it will not
  80. * be called again until after a call to gpio_intr_ack. Any GPIO
  81. * interrupts that occur during the interim are masked.
  82. *
  83. * The application-specific handler is called with a mask of
  84. * pending GPIO interrupts. After processing pin interrupts, the
  85. * application-specific handler may wish to use gpio_intr_pending
  86. * to check for any additional pending interrupts before it returns.
  87. */
  88. void gpio_intr_handler_register(gpio_intr_handler_fn_t fn, void *arg);
  89. /* Determine which GPIO interrupts are pending. */
  90. uint32 gpio_intr_pending(void);
  91. /*
  92. * Acknowledge GPIO interrupts.
  93. * Intended to be called from the gpio_intr_handler_fn.
  94. */
  95. void gpio_intr_ack(uint32 ack_mask);
  96. void gpio_pin_wakeup_enable(uint32 i, GPIO_INT_TYPE intr_state);
  97. void gpio_pin_wakeup_disable();
  98. void gpio_pin_intr_state_set(uint32 i, GPIO_INT_TYPE intr_state);
  99. #endif // _GPIO_H_