hosal_adc.h 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238
  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_ADC_H_
  31. #define __HOSAL_ADC_H_
  32. #ifdef __cplusplus
  33. extern "C" {
  34. #endif
  35. #include <stdint.h>
  36. #include "hosal_dma.h"
  37. /** @addtogroup hosal_adc ADC
  38. * HOSAL ADC API
  39. *
  40. * @{
  41. */
  42. #define HOSAL_WAIT_FOREVER 0xFFFFFFFFU /**< @brief Define the wait forever timeout macro */
  43. /**
  44. * @brief ADC interrupt events
  45. */
  46. typedef enum __ADC_INT_EVENTS__{
  47. HOSAL_ADC_INT_OV, /**< @brief Overrun error */
  48. HOSAL_ADC_INT_EOS, /**< @brief End of sample */
  49. HOSAL_ADC_INT_DMA_TRH, /**< @brief DMA transceive half */
  50. HOSAL_ADC_INT_DMA_TRC, /**< @brief DMA transceive complete */
  51. HOSAL_ADC_INT_DMA_TRE, /**< @briefDMA transceive error */
  52. } hosal_adc_event_t;
  53. /**
  54. * @brief ADC data type
  55. */
  56. typedef struct {
  57. uint32_t size; /**< @brief sampled data size */
  58. void *data; /**< @brief sampled data, aligned with resolution (until the next power of two) */
  59. } hosal_adc_data_t;
  60. /**
  61. * @brief ADC MODE type
  62. */
  63. typedef enum {
  64. HOSAL_ADC_ONE_SHOT, /**< @brief Single time sampling */
  65. HOSAL_ADC_CONTINUE /**< @brief Continuous sampling */
  66. } hosal_adc_sample_mode_t;
  67. /**
  68. * @brief Define ADC config args
  69. */
  70. typedef struct {
  71. uint32_t sampling_freq; /**< @brief sampling frequency in Hz */
  72. uint32_t pin; /**< @brief adc pin */
  73. hosal_adc_sample_mode_t mode; /**< @brief adc sampling mode */
  74. uint8_t sample_resolution; /**< @brief adc sampling resolution */
  75. } hosal_adc_config_t;
  76. /**
  77. * @brief ADC interrupt function
  78. *
  79. *@param[in] parg Set the custom parameters specified
  80. *
  81. */
  82. typedef void (*hosal_adc_irq_t)(void *parg);
  83. /**
  84. * @brief Define ADC dev hosal handle
  85. */
  86. typedef struct {
  87. uint8_t port; /**< @brief adc port */
  88. hosal_adc_config_t config; /**< @brief adc config */
  89. hosal_dma_chan_t dma_chan; /**< @brief adc dma channel */
  90. hosal_adc_irq_t cb; /**< @brief adc callback */
  91. void *p_arg; /**< @brief p_arg data */
  92. void *priv; /**< @brief priv data */
  93. } hosal_adc_dev_t;
  94. /**
  95. * @brief ADC interrupt callback
  96. *
  97. * @param[in] parg Set the custom parameters specified when the callback function is set
  98. *
  99. */
  100. typedef void (*hosal_adc_cb_t)(hosal_adc_event_t event, void *data, uint32_t size);
  101. /**
  102. * @brief Initialises an ADC interface, Prepares an ADC hardware interface for sampling
  103. *
  104. * @param[in] adc the interface which should be initialised
  105. *
  106. * @return
  107. * - 0 on success
  108. * - EIO if an error occurred with any step
  109. */
  110. int hosal_adc_init(hosal_adc_dev_t *adc);
  111. /**
  112. * @brief Add a channel to an ADC interface
  113. *
  114. * @param[in] adc the interface which should be sampled
  115. * @param[in] channel adc channel
  116. *
  117. * @return
  118. * - 0 on success
  119. * - EIO if an error occurred with any step
  120. */
  121. int hosal_adc_add_channel(hosal_adc_dev_t *adc, uint32_t channel);
  122. /**
  123. * @brief Remove a channel to an ADC interface
  124. *
  125. * @param[in] adc the interface which should be sampled
  126. * @param[in] channel adc channel
  127. *
  128. * @return
  129. * - 0 on success
  130. * - EIO if an error occurred with any step
  131. */
  132. int hosal_adc_remove_channel(hosal_adc_dev_t *adc, uint32_t channel);
  133. /**
  134. * @brief Takes adc device handle from an ADC interface
  135. *
  136. * @return
  137. * - other get adc device success
  138. * - NULL if an error occurred with any step
  139. */
  140. hosal_adc_dev_t *hosal_adc_device_get(void);
  141. /**
  142. * @brief Takes a single sample from an ADC interface
  143. *
  144. * @param[in] adc the interface which should be sampled
  145. * @param[in] channel adc channel
  146. * @param[in] timeout ms timeout
  147. *
  148. * @return
  149. * - other get adc data success
  150. * - -1 if an error occurred with any step
  151. */
  152. int hosal_adc_value_get(hosal_adc_dev_t *adc, uint32_t channel, uint32_t timeout);
  153. /**
  154. * @brief Takes a tsen sample from an ADC interface
  155. *
  156. * @param[in] adc the interface which should be sampled
  157. *
  158. * @return
  159. * - other get adc data success
  160. * - -1 if an error occurred with any step
  161. */
  162. int hosal_adc_tsen_value_get(hosal_adc_dev_t *adc);
  163. /**
  164. * @brief ADC sampling cb register
  165. *
  166. * @param [in] adc the ADC interface
  167. * @param [in] cb Non-zero pointer is the sample callback handler
  168. * NULL pointer for send unregister operation
  169. * adc in cb must be the same pointer with adc pointer passed to hosal_adc_sample_cb_reg
  170. * driver must notify upper layer by calling cb if ADC data is ready in HW or memory(DMA)
  171. *
  172. * @return
  173. * - 0 on success
  174. * - EIO if an error occurred with any step
  175. */
  176. int hosal_adc_sample_cb_reg(hosal_adc_dev_t *adc, hosal_adc_cb_t cb);
  177. /**
  178. * @brief ADC sampling start
  179. *
  180. * @param[in] adc the ADC interface
  181. * @param[in] data adc data buffer
  182. * @param[in] size data buffer size aligned with resolution (until the next power of two)
  183. *
  184. * @return
  185. * - 0 on success
  186. * - EIO if an error occurred with any step
  187. */
  188. int hosal_adc_start(hosal_adc_dev_t *adc, void *data, uint32_t size);
  189. /**
  190. * @brief ADC sampling stop
  191. *
  192. * @param[in] adc the ADC interface
  193. *
  194. * @return
  195. * - 0 on success
  196. * - EIO if an error occurred with any step
  197. */
  198. int hosal_adc_stop(hosal_adc_dev_t *adc);
  199. /**
  200. * @brief De-initialises an ADC interface, Turns off an ADC hardware interface
  201. *
  202. * @param[in] adc the interface which should be de-initialised
  203. *
  204. * @return
  205. * - 0 on success
  206. * - EIO if an error occurred with any step
  207. */
  208. int hosal_adc_finalize(hosal_adc_dev_t *adc);
  209. #ifdef __cplusplus
  210. }
  211. #endif
  212. #endif /* __HOSAL_ADC_H_ */
  213. /* end of file */