hosal_gpio.h 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  1. /*
  2. * Copyright (c) 2016-2022 Bouffalolab.
  3. *
  4. * This file is part of
  5. * *** Bouffalolab Software Dev Kit ***
  6. * (see www.bouffalolab.com).
  7. *
  8. * Redistribution and use in source and binary forms, with or without modification,
  9. * are permitted provided that the following conditions are met:
  10. * 1. Redistributions of source code must retain the above copyright notice,
  11. * this list of conditions and the following disclaimer.
  12. * 2. Redistributions in binary form must reproduce the above copyright notice,
  13. * this list of conditions and the following disclaimer in the documentation
  14. * and/or other materials provided with the distribution.
  15. * 3. Neither the name of Bouffalo Lab nor the names of its contributors
  16. * may be used to endorse or promote products derived from this software
  17. * without specific prior written permission.
  18. *
  19. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  20. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  21. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  22. * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
  23. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  24. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  25. * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  26. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  27. * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  28. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  29. */
  30. #ifndef __HOSAL_GPIO_H_
  31. #define __HOSAL_GPIO_H_
  32. #ifdef __cplusplus
  33. extern "C" {
  34. #endif
  35. #include <stdint.h>
  36. /** @addtogroup hosal_gpio GPIO
  37. * HOSAL GPIO API
  38. *
  39. * @{
  40. */
  41. /**
  42. * @brief gpio config struct
  43. */
  44. typedef enum {
  45. ANALOG_MODE, /**< @brief Used as a function pin, input and output analog */
  46. INPUT_PULL_UP, /**< @brief Input with an internal pull-up resistor - use with devices that actively drive the signal low - e.g. button connected to ground */
  47. INPUT_PULL_DOWN, /**< @brief Input with an internal pull-down resistor - use with devices that actively drive the signal high - e.g. button connected to a power rail */
  48. INPUT_HIGH_IMPEDANCE, /**< @brief Input - must always be driven, either actively or by an external pullup resistor */
  49. OUTPUT_PUSH_PULL, /**< @brief Output actively driven high and actively driven low - must not be connected to other active outputs - e.g. LED output */
  50. OUTPUT_OPEN_DRAIN_NO_PULL, /**< @brief Output actively driven low but is high-impedance when set high - can be connected to other open-drain/open-collector outputs. Needs an external pull-up resistor */
  51. OUTPUT_OPEN_DRAIN_PULL_UP, /**< @brief Output actively driven low and is pulled high with an internal resistor when set high - can be connected to other open-drain/open-collector outputs. */
  52. OUTPUT_OPEN_DRAIN_AF, /**< @brief Alternate Function Open Drain Mode. */
  53. OUTPUT_PUSH_PULL_AF, /**< @brief Alternate Function Push Pull Mode. */
  54. } hosal_gpio_config_t;
  55. /**
  56. * @brief GPIO interrupt trigger
  57. */
  58. typedef enum {
  59. HOSAL_IRQ_TRIG_NEG_PULSE, /**< @brief GPIO negedge pulse trigger interrupt */
  60. HOSAL_IRQ_TRIG_POS_PULSE, /**< @brief GPIO posedge pulse trigger interrupt */
  61. HOSAL_IRQ_TRIG_NEG_LEVEL, /**< @brief GPIO negedge level trigger interrupt (32k 3T)*/
  62. HOSAL_IRQ_TRIG_POS_LEVEL, /**< @brief GPIO posedge level trigger interrupt (32k 3T)*/
  63. } hosal_gpio_irq_trigger_t;
  64. /**
  65. * @brief GPIO interrupt callback handler
  66. *
  67. *@param[in] parg :Set the custom parameters specified
  68. */
  69. typedef void (*hosal_gpio_irq_handler_t)(void *arg);
  70. /**
  71. * @brief hosal gpio ctx, use for multi gpio irq
  72. */
  73. typedef struct hosal_gpio_ctx {
  74. struct hosal_gpio_ctx *next;
  75. hosal_gpio_irq_handler_t handle;
  76. void *arg;
  77. uint8_t pin;
  78. uint8_t intCtrlMod;
  79. uint8_t intTrigMod;
  80. }hosal_gpio_ctx_t;
  81. /**
  82. * @brief GPIO dev struct
  83. */
  84. typedef struct {
  85. uint8_t port; /**< @brief gpio port */
  86. hosal_gpio_config_t config; /**< @brief gpio config */
  87. void *priv; /**< @brief priv data */
  88. } hosal_gpio_dev_t;
  89. /**
  90. * @brief Initialises a GPIO pin
  91. *
  92. * @note Prepares a GPIO pin for use.
  93. *
  94. * @param[in] gpio the gpio pin which should be initialised
  95. *
  96. * @return
  97. * - 0 on success
  98. * - EIO if an error occurred with any step
  99. */
  100. int hosal_gpio_init(hosal_gpio_dev_t *gpio);
  101. /**
  102. * @brief Set GPIO output high or low
  103. *
  104. * @note Using this function on a gpio pin which is set to input mode is undefined.
  105. *
  106. * @param[in] gpio the gpio pin which should be set
  107. * @param[in] value 0 : output low | >0 : output high
  108. *
  109. * @return
  110. * - 0 on success
  111. * - EIO if an error occurred with any step
  112. */
  113. int hosal_gpio_output_set(hosal_gpio_dev_t *gpio, uint8_t value);
  114. /**
  115. * @brief Get the state of an input GPIO pin. Using this function on a
  116. * gpio pin which is set to output mode will return an undefined value.
  117. *
  118. * @param[in] gpio the gpio pin which should be read
  119. * @param[out] value gpio value
  120. *
  121. * @return
  122. * - 0 on success
  123. * - EIO if an error occurred with any step
  124. */
  125. int hosal_gpio_input_get(hosal_gpio_dev_t *gpio, uint8_t *value);
  126. /**
  127. * @brief Enables an interrupt trigger for an input GPIO pin.
  128. * Using this function on a gpio pin which is set to
  129. * output mode is undefined.
  130. *
  131. * @param[in] gpio the gpio pin which will provide the interrupt trigger
  132. * @param[in] trigger the type of trigger (rising/falling edge or both)
  133. * @param[in] handler a function pointer to the interrupt handler
  134. * @param[in] arg an argument that will be passed to the interrupt handler
  135. *
  136. * @return
  137. * - 0 on success
  138. * - EIO if an error occurred with any step
  139. */
  140. int hosal_gpio_irq_set(hosal_gpio_dev_t *gpio, hosal_gpio_irq_trigger_t trigger_type, hosal_gpio_irq_handler_t handler, void *arg);
  141. /**
  142. * @brief Clear an interrupt status for an input GPIO pin.
  143. * Using this function on a gpio pin which has generated a interrupt.
  144. *
  145. * @param[in] gpio the gpio pin which provided the interrupt trigger
  146. * @param[in] mask 0 : mask | 1 : umask
  147. *
  148. * @return
  149. * - 0 on success
  150. * - EIO if an error occurred with any step
  151. */
  152. int hosal_gpio_irq_mask(hosal_gpio_dev_t *gpio, uint8_t mask);
  153. /**
  154. * @brief Set a GPIO pin in default state.
  155. *
  156. * @param[in] gpio the gpio pin which should be deinitialised
  157. *
  158. * @return
  159. * - 0 on success
  160. * - EIO if an error occurred with any step
  161. */
  162. int hosal_gpio_finalize(hosal_gpio_dev_t *gpio);
  163. /** @} */
  164. #ifdef __cplusplus
  165. }
  166. #endif
  167. #endif /* HAL_GPIO_H */