drv_gpio.h 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  1. /* Copyright (C) 2018 RDA Technologies Limited and/or its affiliates("RDA").
  2. * All rights reserved.
  3. *
  4. * This software is supplied "AS IS" without any warranties.
  5. * RDA assumes no responsibility or liability for the use of the software,
  6. * conveys no license or title under any patent, copyright, or mask work
  7. * right to the product. RDA reserves the right to make changes in the
  8. * software without notification. RDA also make no representation or
  9. * warranty that such application will be suitable for the specified use
  10. * without further testing or modification.
  11. */
  12. #ifndef _DRV_GPIO_H_
  13. #define _DRV_GPIO_H_
  14. #include "osi_api.h"
  15. #include "quec_common.h"
  16. #ifdef __cplusplus
  17. extern "C" {
  18. #endif
  19. struct drvGpio;
  20. /**
  21. * opaque data struct for GPIO instance
  22. */
  23. typedef struct drvGpio drvGpio_t;
  24. /**
  25. * GPIO interrupt callback
  26. *
  27. * It will be called when GPIO interrupt occurs. Due to it is executed in
  28. * ISR, the callback should follow ISR programming guides.
  29. */
  30. typedef void (*drvGpioIntrCB_t)(void *ctx);
  31. typedef enum
  32. {
  33. DRV_GPIO_INPUT,
  34. DRV_GPIO_OUTPUT
  35. } drvGpioMode_t;
  36. /**
  37. * GPIO configuration
  38. */
  39. typedef struct
  40. {
  41. drvGpioMode_t mode; ///< GPIO mode
  42. bool out_level; ///< level to be set for GPIO output
  43. bool intr_enabled; ///< interrupt enabled, only for GPIO input
  44. bool intr_level; ///< true for level interrupt, false for edge interrupt
  45. bool debounce; ///< debounce enabled
  46. bool rising; ///< rising edge or level high interrupt enabled
  47. bool falling; ///< falling edge or level low interrupt enabled
  48. } drvGpioConfig_t;
  49. /**
  50. * @brief GPIO module initialization
  51. *
  52. * It just initialize GPIO module, and won't touch any GPIO. It should be
  53. * called before any \p drvGpioOpen.
  54. */
  55. void drvGpioInit(void);
  56. /**
  57. * @brief open a GPIO
  58. *
  59. * IOMUX will be set to GPIO mode at open.
  60. *
  61. * Each GPIO sould be opened only once. When it is already opened, this
  62. * API will return NULL.
  63. *
  64. * If the specified GPIO can't support the specified mode, this API will
  65. * return NULL.
  66. *
  67. * If the specified GPIO can't support input interrupt, and interrupt is
  68. * enabled, this API will return NULL.
  69. *
  70. * The returned instance is dynamic allocated, caller should free it after
  71. * \p drvGpioClose is called.
  72. *
  73. * @param id GPIO id. The range may be different among chips.
  74. * @param cfg GPIO configuration
  75. * @param cb callback at interrupt
  76. * @param cb_ctx context pointer for callback
  77. * @return
  78. * - GPIO instance pointer
  79. * - NULL if parameter is invalid
  80. */
  81. drvGpio_t *drvGpioOpen(uint32_t id, const drvGpioConfig_t *cfg, drvGpioIntrCB_t cb, void *cb_ctx);
  82. /**
  83. * @brief close a GPIO
  84. *
  85. * IOMUX will be kept, and the GPIO will be set to input mode.
  86. *
  87. * @param d GPIO instance
  88. */
  89. void drvGpioClose(drvGpio_t *d);
  90. /**
  91. * @brief reconfigure the opened GPIO
  92. *
  93. * When the configuration is invalid, the current configuration will
  94. * be kept.
  95. *
  96. * @param d GPIO instance
  97. * @param cfg GPIO configuration
  98. * @return
  99. * - true if GPIO configuration is valid
  100. * - false if GPIO configuration is invalid
  101. */
  102. bool drvGpioReconfig(drvGpio_t *d, const drvGpioConfig_t *cfg);
  103. /**
  104. * @brief read the level of GPIO
  105. *
  106. * It can be called for both GPIO input and output. For GPIO output,
  107. * it is the level set by software.
  108. *
  109. * @param d GPIO instance
  110. * @return
  111. * - true for level high
  112. * - false for level low
  113. */
  114. bool drvGpioRead(drvGpio_t *d);
  115. /**
  116. * @brief write level for GPIO
  117. *
  118. * When it is called for GPIO input, it will do nothing.
  119. *
  120. * When it is called for GPIO output, set the output level.
  121. *
  122. * @param d GPIO instance
  123. * @param level GPIO level to be set,
  124. * - true for level high
  125. * - false for level low
  126. */
  127. void drvGpioWrite(drvGpio_t *d, bool level);
  128. /**
  129. * @brief GPIO suspend callback
  130. *
  131. * Explicit GPIO suspend and resume call will be used, in case the order
  132. * of GPIO suspend/resume is sensitive.
  133. *
  134. * @param mode suspend mode
  135. */
  136. void drvGpioSuspend(osiSuspendMode_t mode);
  137. /**
  138. * @brief GPIO resume callback
  139. *
  140. * Explicit GPIO suspend and resume call will be used, in case the order
  141. * of GPIO suspend/resume is sensitive.
  142. *
  143. * @param mode suspend mode
  144. * @param source wakeup source
  145. */
  146. void drvGpioResume(osiSuspendMode_t mode, uint32_t source);
  147. #ifdef CONFIG_QUEC_PROJECT_FEATURE_GPIO
  148. void quec_drvGpioOpen(uint32_t id, drvGpioIntrCB_t cb, void *cb_ctx);
  149. void quec_drvGpioClose(uint32_t id);
  150. void quec_drvGpioInit(uint32_t gpio_set1, uint32_t gpio_set2);
  151. #endif
  152. #ifdef __cplusplus
  153. }
  154. #endif
  155. #endif