cmem7_adc.h 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269
  1. /**
  2. *****************************************************************************
  3. * @file cmem7_adc.h
  4. *
  5. * @brief CMEM7 ADC header file
  6. *
  7. *
  8. * @version V1.0
  9. * @date 3. September 2013
  10. *
  11. * @note
  12. *
  13. *****************************************************************************
  14. * @attention
  15. *
  16. * THE PRESENT FIRMWARE WHICH IS FOR GUIDANCE ONLY AIMS AT PROVIDING CUSTOMERS
  17. * WITH CODING INFORMATION REGARDING THEIR PRODUCTS IN ORDER FOR THEM TO SAVE
  18. * TIME. AS A RESULT, CAPITAL-MICRO SHALL NOT BE HELD LIABLE FOR ANY DIRECT,
  19. * INDIRECT OR CONSEQUENTIAL DAMAGES WITH RESPECT TO ANY CLAIMS ARISING
  20. * FROM THE CONTENT OF SUCH FIRMWARE AND/OR THE USE MADE BY CUSTOMERS OF THE
  21. * CODING INFORMATION CONTAINED HEREIN IN CONNECTION WITH THEIR PRODUCTS.
  22. *
  23. * <h2><center>&copy; COPYRIGHT 2013 Capital-micro </center></h2>
  24. *****************************************************************************
  25. */
  26. #ifndef __CMEM7_ADC_H
  27. #define __CMEM7_ADC_H
  28. #ifdef __cplusplus
  29. extern "C" {
  30. #endif
  31. #include "cmem7.h"
  32. #include "cmem7_conf.h"
  33. /** @defgroup ADC_PERIPH
  34. * @{
  35. */
  36. typedef enum {
  37. ADC_PERIPH_1,
  38. ADC_PERIPH_2,
  39. } ADC_PERIPH;
  40. #define IS_ADC_ALL_PERIPH(PERIPH) (((PERIPH) == ADC_PERIPH_1) || \
  41. ((PERIPH) == ADC_PERIPH_2))
  42. /**
  43. * @}
  44. */
  45. /** @defgroup ADC_VSEN
  46. * @{
  47. */
  48. #define ADC_VSEN_VDDCORE 1
  49. #define ADC_VSEN_VDDIO 2
  50. #define ADC_VSEN_VDDIO2 4
  51. #define IS_ADC_VSEN(VSEN) (((VSEN) == ADC_VSEN_VDDCORE) || \
  52. ((VSEN) == ADC_VSEN_VDDIO) || \
  53. ((VSEN) == ADC_VSEN_VDDIO2))
  54. /**
  55. * @}
  56. */
  57. /** @defgroup ADC_PHASE_CTRL
  58. * @{
  59. */
  60. #define ADC_PHASE_CTRL_0DEG_RISE_EDGE 0 /* ADC-1 and ADC-2 CLK are 0DEG Phase Difference(Rising Edge) */
  61. #define ADC_PHASE_CTRL_90DEG_AHEAD 1 /* ADC-1 90DEG ahead of ADC-2 */
  62. #define ADC_PHASE_CTRL_90DEG_LAG 2 /* ADC-1 90DEG lag of ADC-2 */
  63. #define ADC_PHASE_CTRL_0DEG_FALL_EDGE 3 /* ADC-1 and ADC-2 CLK are 0DEG Phase Difference(falling Edge) */
  64. #define IS_ADC_PHASE_CTRL(CTRL) (((CTRL) == ADC_PHASE_CTRL_0DEG_RISE_EDGE) || \
  65. ((CTRL) == ADC_PHASE_CTRL_90DEG_AHEAD) || \
  66. ((CTRL) == ADC_PHASE_CTRL_90DEG_LAG) || \
  67. ((CTRL) == ADC_PHASE_CTRL_0DEG_FALL_EDGE))
  68. /**
  69. * @}
  70. */
  71. /** @defgroup ADC_CONVERSION
  72. * @{
  73. */
  74. #define ADC_SYSTEM_MODE_SINGLE_CONV 1
  75. #define ADC_SYSTEM_MODE_CONTINUOUS_CONV 2
  76. #define IS_ADC_CONVERSION(CONV) (((CONV) == ADC_SYSTEM_MODE_SINGLE_CONV) || \
  77. ((CONV) == ADC_SYSTEM_MODE_CONTINUOUS_CONV))
  78. /**
  79. * @}
  80. */
  81. /** @defgroup ADC_CALIBRATION
  82. * @{
  83. */
  84. #define ADC_CALIBRATION_OFFSET 3
  85. #define ADC_CALIBRATION_NEGTIVE_GAIN 4
  86. #define ADC_CALIBRATION_POSTIVE_GAIN 5
  87. #define IS_ADC_CALIBRATION(CALIB) (((CALIB) == ADC_CALIBRATION_OFFSET) || \
  88. ((CALIB) == ADC_CALIBRATION_NEGTIVE_GAIN) || \
  89. ((CALIB) == ADC_CALIBRATION_POSTIVE_GAIN))
  90. /**
  91. * @}
  92. */
  93. /** @defgroup ADC_CHANNEL
  94. * @{
  95. */
  96. #define ADC_CHANNEL_CALIBRATION 0x0
  97. /**
  98. * @}
  99. */
  100. /** @defgroup ADC1_CHANNEL
  101. * @{
  102. */
  103. #define ADC1_CHANNEL_VIP 0x1
  104. #define ADC1_CHANNEL_VSEN 0x2
  105. #define ADC1_CHANNEL_VADIO_0 0x4
  106. #define ADC1_CHANNEL_VADIO_1 0x8
  107. #define ADC1_CHANNEL_VADIO_2 0x10
  108. #define ADC1_CHANNEL_VADIO_3 0x20
  109. #define ADC1_CHANNEL_VADIO_4 0x40
  110. #define ADC1_CHANNEL_VADIO_5 0x80
  111. #define ADC1_CHANNEL_ALL 0xFF
  112. #define IS_ADC1_CHANNEL(CHANNEL) (((CHANNEL) != 0) && ((CHANNEL) & ~ADC1_CHANNEL_ALL) == 0)
  113. /**
  114. * @}
  115. */
  116. /** @defgroup ADC2_CHANNEL
  117. * @{
  118. */
  119. #define ADC2_CHANNEL_VIN 0x1
  120. #define ADC2_CHANNEL_VTMP 0x2
  121. #define ADC2_CHANNEL_VADIO_6 0x4
  122. #define ADC2_CHANNEL_VADIO_7 0x8
  123. #define ADC2_CHANNEL_VADIO_8 0x10
  124. #define ADC2_CHANNEL_VADIO_9 0x20
  125. #define ADC2_CHANNEL_VADIO_10 0x40
  126. #define ADC2_CHANNEL_VADIO_11 0x80
  127. #define ADC2_CHANNEL_ALL 0xFF
  128. #define IS_ADC2_CHANNEL(CHANNEL) (((CHANNEL) != 0) && ((CHANNEL) & ~ADC2_CHANNEL_ALL) == 0)
  129. /**
  130. * @}
  131. */
  132. /** @defgroup ADC_INT
  133. * @{
  134. */
  135. #define ADC1_INT_ALMOST_FULL 0x1
  136. #define ADC2_INT_ALMOST_FULL 0x8
  137. #define ADC_INT_ALL 0x9
  138. #define IS_ADC_INT(INT) (((INT) != 0) && (((INT) & ~ADC_INT_ALL) == 0))
  139. /**
  140. * @}
  141. */
  142. /**
  143. * @brief ADC collection data structure
  144. */
  145. typedef struct {
  146. uint8_t channel; /*!< The channel of collected data, is a value of
  147. @ref ADC_CHANNEL, @ref ADC1_CHANNEL or @ref ADC2_CHANNEL */
  148. uint16_t data; /*!< collected data */
  149. } ADC_Data;
  150. /**
  151. * @brief ADC initialization structure
  152. */
  153. typedef struct
  154. {
  155. uint8_t ADC_PhaseCtrl; /*!< Phase between ADC1 and ADC2, is a value of @ref ADC_PHASE_CTRL */
  156. uint8_t ADC_VsenSelection; /*!< ADC1 VSEN selection, is a value of @ref ADC_VSEN */
  157. } ADC_InitTypeDef;
  158. /**
  159. * @brief ADC initialization
  160. * @note This function should be called at first before any other interfaces.
  161. * @param[in] init A pointer to structure ADC_InitTypeDef
  162. * @retval None
  163. */
  164. void ADC_Init(ADC_InitTypeDef* init);
  165. /**
  166. * @brief Enable or disable ADC.
  167. * @param[in] adc ADC peripheral, which is a value of @ref ADC_PERIPH
  168. * @param[in] Enable The bit indicates if the specific ADC is enable or not
  169. * @retval None
  170. */
  171. void ADC_Enable(uint8_t adc, BOOL enable);
  172. /**
  173. * @brief Enable or disable ADC interrupt.
  174. * @param[in] Int interrupt mask bits, which can be a combination of @ref ADC_INT
  175. * @param[in] Enable The bit indicates if specific interrupts are enable or not
  176. * @retval None
  177. */
  178. void ADC_EnableInt(uint32_t Int, BOOL enable);
  179. /**
  180. * @brief Check specific interrupts are set or not
  181. * @param[in] Int interrupt mask bits, which can be a combination of @ref ADC_INT
  182. * @retval BOOL The bit indicates if the specific interrupts are set or not
  183. */
  184. BOOL ADC_GetIntStatus(uint32_t Int);
  185. /**
  186. * @brief Clear specific interrupts
  187. * @param[in] Int interrupt mask bits, which can be a value of @ref ADC_INT
  188. * @retval None
  189. */
  190. void ADC_ClearInt(uint32_t Int);
  191. /**
  192. * @brief ADC starts to convert data
  193. * @param[in] adc ADC peripheral, which is a value of @ref ADC_PERIPH
  194. * @param[in] convMode It should be a value of @ref ADC_CONVERSION
  195. * @param[in] channel It should be the value of @ref ADC1_CHANNEL
  196. * or @ref ADC2_CHANNEL according to parameter 'adc'
  197. * @retval BOOL The bit indicates if the specific ADC starts to convert data
  198. */
  199. BOOL ADC_StartConversion(uint8_t adc, uint8_t convMode, uint32_t channel);
  200. /**
  201. * @brief ADC starts to calibrate and produces one sample
  202. * @param[in] adc ADC peripheral, which is a value of @ref ADC_PERIPH
  203. * @param[in] convMode It should be a value of @ref ADC_CALIBRATION
  204. * @retval BOOL The bit indicates if the specific ADC starts to convert data
  205. */
  206. BOOL ADC_StartCalibration(uint8_t adc, uint8_t calibration);
  207. /**
  208. * @brief ADC stops conversion or calibration
  209. * @param[in] adc ADC peripheral, which is a value of @ref ADC_PERIPH
  210. * @retval NULL
  211. */
  212. void ADC_Stop(uint8_t adc);
  213. /**
  214. * @brief Check if ADC is busy or not
  215. * @param[in] adc ADC peripheral, which is a value of @ref ADC_PERIPH
  216. * @retval BOOL The bit indicates if the specific ADC is busy or not
  217. */
  218. BOOL ADC_IsBusy(uint8_t adc);
  219. /**
  220. * @brief Read data from ADC
  221. * @param[in] adc ADC peripheral, which is a value of @ref ADC_PERIPH
  222. * @param[in] Size Expected data size to be read
  223. * @param[out] data A user-allocated buffer to fetch data to be read
  224. * @retval uint8_t Actual read data size
  225. */
  226. uint8_t ADC_Read(uint8_t adc, uint8_t size, ADC_Data* data);
  227. #ifdef __cplusplus
  228. }
  229. #endif
  230. #endif /*__CMEM7_ADC_H */