HAL_adc.h 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440
  1. /**
  2. ******************************************************************************
  3. * @file HAL_adc.h
  4. * @author IC Applications Department
  5. * @version V0.8
  6. * @date 2019_08_02
  7. * @brief This file contains all the functions prototypes for the ADC firmware
  8. * library.
  9. ******************************************************************************
  10. * @copy
  11. *
  12. * THE PRESENT FIRMWARE WHICH IS FOR GUIDANCE ONLY AIMS AT PROVIDING CUSTOMERS
  13. * WITH CODING INFORMATION REGARDING THEIR PRODUCTS IN ORDER FOR THEM TO SAVE
  14. * TIME. AS A RESULT, HOLOCENE SHALL NOT BE HELD LIABLE FOR ANY
  15. * DIRECT, INDIRECT OR CONSEQUENTIAL DAMAGES WITH RESPECT TO ANY CLAIMS ARISING
  16. * FROM THE CONTENT OF SUCH FIRMWARE AND/OR THE USE MADE BY CUSTOMERS OF THE
  17. * CODING INFORMATION CONTAINED HEREIN IN CONNECTION WITH THEIR PRODUCTS.
  18. *
  19. * <h2><center>&copy; COPYRIGHT 2016 HOLOCENE</center></h2>
  20. */
  21. /* Define to prevent recursive inclusion -------------------------------------*/
  22. #ifndef __HAL_ADC_H
  23. #define __HAL_ADC_H
  24. /* Includes ------------------------------------------------------------------*/
  25. #include "HAL_device.h"
  26. /** @addtogroup StdPeriph_Driver
  27. * @{
  28. */
  29. /** @addtogroup ADC
  30. * @{
  31. */
  32. /** @defgroup ADC_Exported_Types
  33. * @{
  34. */
  35. /**
  36. * @brief ADC Init structure definition
  37. */
  38. /*
  39. typedef struct
  40. {
  41. uint32_t ADC_Mode;
  42. FunctionalState ADC_ScanConvMode;
  43. FunctionalState ADC_ContinuousConvMode;
  44. uint32_t ADC_ExternalTrigConv;
  45. uint32_t ADC_DataAlign;
  46. uint8_t ADC_NbrOfChannel;
  47. }ADC_InitTypeDef;
  48. */
  49. typedef struct
  50. {
  51. uint32_t ADC_Resolution;
  52. uint32_t ADC_PRESCARE;
  53. uint32_t ADC_Mode;
  54. FunctionalState ADC_ContinuousConvMode;
  55. uint32_t ADC_TRGEN;
  56. uint32_t ADC_ExternalTrigConv;
  57. uint32_t ADC_DataAlign;
  58. }ADC_InitTypeDef;
  59. /**
  60. * @}
  61. */
  62. /** @defgroup ADC_Exported_Constants
  63. * @{
  64. */
  65. #define IS_ADC_ALL_PERIPH(PERIPH) (((*(uint32_t*)&(PERIPH)) == ADC1_BASE) || \
  66. ((*(uint32_t*)&(PERIPH)) == ADC2_BASE))
  67. #define IS_ADC_DMA_PERIPH(PERIPH) (((*(uint32_t*)&(PERIPH)) == ADC1_BASE) || \
  68. ((*(uint32_t*)&(PERIPH)) == ADC2_BASE))
  69. /** @defgroup ADC_Resolution
  70. * @{
  71. */
  72. #define ADC_Resolution_12b ((uint32_t)0x00000000)
  73. #define ADC_Resolution_11b ((uint32_t)0x00000080)
  74. #define ADC_Resolution_10b ((uint32_t)0x00000100)
  75. #define ADC_Resolution_9b ((uint32_t)0x00000180)
  76. #define ADC_Resolution_8b ((uint32_t)0x00000200)
  77. #define IS_ADC_RESOLUTION(RESOLUTION) (((RESOLUTION) == ADC_Resolution_12b) || \
  78. ((RESOLUTION) == ADC_Resolution_10b) || \
  79. ((RESOLUTION) == ADC_Resolution_8b) || \
  80. ((RESOLUTION) == ADC_Resolution_6b))
  81. /**
  82. * @brief for ADC1, ADC2
  83. */
  84. #define ADC_PCLK2_PRESCARE_2 ((uint32_t)0x00000000)
  85. #define ADC_PCLK2_PRESCARE_4 ((uint32_t)0x00000010)
  86. #define ADC_PCLK2_PRESCARE_6 ((uint32_t)0x00000020)
  87. #define ADC_PCLK2_PRESCARE_8 ((uint32_t)0x00000030)
  88. #define ADC_PCLK2_PRESCARE_10 ((uint32_t)0x00000040)
  89. #define ADC_PCLK2_PRESCARE_12 ((uint32_t)0x00000050)
  90. #define ADC_PCLK2_PRESCARE_14 ((uint32_t)0x00000060)
  91. #define ADC_PCLK2_PRESCARE_16 ((uint32_t)0x00000070)
  92. /** @defgroup ADC_dual_mode
  93. * @{
  94. */
  95. #define ADC_Mode_Single ((uint32_t)0x00000000)
  96. #define ADC_Mode_Single_Period ((uint32_t)0x00000200)
  97. #define ADC_Mode_Continuous_Scan ((uint32_t)0x00000400)
  98. #define IS_ADC_MODE(MODE) (((MODE) == ADC_Mode_Single) || \
  99. ((MODE) == ADC_Mode_Single_Period) || \
  100. ((MODE) == ADC_Mode_Continuous_Scan))
  101. /**
  102. * @}
  103. */
  104. #define ADC_TRG_Disable ((uint32_t)0xfffffffB)
  105. #define ADC_TRG_Enable ((uint32_t)0x00000004)
  106. /** @defgroup ADC_extrenal_trigger_sources_for_regular_channels_conversion
  107. * @{
  108. */
  109. /**
  110. * @brief for ADC1
  111. */
  112. #define ADC_ExternalTrigConv_T1_CC1 ((uint32_t)0x00000000)
  113. #define ADC_ExternalTrigConv_T1_CC2 ((uint32_t)0x00000010)
  114. #define ADC_ExternalTrigConv_T1_CC3 ((uint32_t)0x00000020)
  115. #define ADC_ExternalTrigConv_T2_CC2 ((uint32_t)0x00000030)
  116. #define ADC_ExternalTrigConv_T3_TRGO ((uint32_t)0x00000040)
  117. #define ADC_ExternalTrigConv_T4_CC4 ((uint32_t)0x00000050)
  118. #define ADC_ExternalTrigConv_T3_CC1 ((uint32_t)0x00000060)
  119. #define ADC_ExternalTrigConv_EXTI_11 ((uint32_t)0x00000070)
  120. /**
  121. * @brief for ADC2
  122. */
  123. #define ADC_ExternalTrigConv_T1_TRGO ((uint32_t)0x00000000)
  124. #define ADC_ExternalTrigConv_T1_CC4 ((uint32_t)0x00000010)
  125. #define ADC_ExternalTrigConv_T2_TRGO ((uint32_t)0x00000020)
  126. #define ADC_ExternalTrigConv_T2_CC1 ((uint32_t)0x00000030)
  127. #define ADC_ExternalTrigConv_T3_CC4 ((uint32_t)0x00000040)
  128. #define ADC_ExternalTrigConv_T4_TRGO ((uint32_t)0x00000050)
  129. #define ADC_ExternalTrigConv_T3_CC1 ((uint32_t)0x00000060)
  130. #define ADC_ExternalTrigConv_EXTI_15 ((uint32_t)0x00000070)
  131. #define IS_ADC_EXT_TRIG(REGTRIG) (((REGTRIG) == ADC_ExternalTrigConv_T1_CC1) || \
  132. ((REGTRIG) == ADC_ExternalTrigConv_T1_CC2) || \
  133. ((REGTRIG) == ADC_ExternalTrigConv_T1_CC3) || \
  134. ((REGTRIG) == ADC_ExternalTrigConv_T2_CC2) || \
  135. ((REGTRIG) == ADC_ExternalTrigConv_T3_TRGO) || \
  136. ((REGTRIG) == ADC_ExternalTrigConv_T4_CC4) || \
  137. ((REGTRIG) == ADC_ExternalTrigConv_T3_CC1) || \
  138. ((REGTRIG) == ADC_ExternalTrigConv_EXTI_11) || \
  139. ((REGTRIG) == ADC_ExternalTrigConv_T1_TRGO) || \
  140. ((REGTRIG) == ADC_ExternalTrigConv_T1_CC4) || \
  141. ((REGTRIG) == ADC_ExternalTrigConv_T2_TRGO) || \
  142. ((REGTRIG) == ADC_ExternalTrigConv_T2_CC1) || \
  143. ((REGTRIG) == ADC_ExternalTrigConv_T3_CC4) || \
  144. ((REGTRIG) == ADC_ExternalTrigConv_T4_TRGO) || \
  145. ((REGTRIG) == ADC_ExternalTrigConv_T3_CC1) || \
  146. ((REGTRIG) == ADC_ExternalTrigConv_EXTI_15))
  147. /**
  148. * @}
  149. */
  150. /** @defgroup ADC_data_align
  151. * @{
  152. */
  153. #define ADC_DataAlign_Right ((uint32_t)0x00000000)
  154. #define ADC_DataAlign_Left ((uint32_t)0x00000800)
  155. #define IS_ADC_DATA_ALIGN(ALIGN) (((ALIGN) == ADC_DataAlign_Right) || \
  156. ((ALIGN) == ADC_DataAlign_Left))
  157. /**
  158. * @}
  159. */
  160. /** @defgroup ADC_channels
  161. * @{
  162. */
  163. #define ADC_Channel_0 ((uint8_t)0x00)
  164. #define ADC_Channel_1 ((uint8_t)0x01)
  165. #define ADC_Channel_2 ((uint8_t)0x02)
  166. #define ADC_Channel_3 ((uint8_t)0x03)
  167. #define ADC_Channel_4 ((uint8_t)0x04)
  168. #define ADC_Channel_5 ((uint8_t)0x05)
  169. #define ADC_Channel_6 ((uint8_t)0x06)
  170. #define ADC_Channel_7 ((uint8_t)0x07)
  171. #define ADC_Channel_8 ((uint8_t)0x08)
  172. #define ADC_Channel_All ((uint8_t)0x0f)
  173. #define IS_ADC_CHANNEL(CHANNEL) (((CHANNEL) == ADC_Channel_0) || ((CHANNEL) == ADC_Channel_1) || \
  174. ((CHANNEL) == ADC_Channel_2) || ((CHANNEL) == ADC_Channel_3) || \
  175. ((CHANNEL) == ADC_Channel_4) || ((CHANNEL) == ADC_Channel_5) || \
  176. ((CHANNEL) == ADC_Channel_6) || ((CHANNEL) == ADC_Channel_7) || \
  177. ((CHANNEL) == ADC_Channel_8) || ((CHANNEL) == ADC_Channel_All))
  178. /**
  179. * @}
  180. */
  181. /**
  182. * @}
  183. */
  184. #define ADC_SMPR_SMP ((uint32_t)0x00000007) /*!< SMP[2:0] bits (Sampling time selection) */
  185. #define ADC_SMPR_SMP_0 ((uint32_t)0x00000001) /*!< Bit 0 */
  186. #define ADC_SMPR_SMP_1 ((uint32_t)0x00000002) /*!< Bit 1 */
  187. #define ADC_SMPR_SMP_2 ((uint32_t)0x00000004) /*!< Bit 2 */
  188. /** @defgroup ADC_sampling_times
  189. * @{
  190. */
  191. #define ADC_SampleTime_1_5Cycles ((uint32_t)0x00000000)
  192. #define ADC_SampleTime_7_5Cycles ((uint32_t)0x00000001)
  193. #define ADC_SampleTime_13_5Cycles ((uint32_t)0x00000002)
  194. #define ADC_SampleTime_28_5Cycles ((uint32_t)0x00000003)
  195. #define ADC_SampleTime_41_5Cycles ((uint32_t)0x00000004)
  196. #define ADC_SampleTime_55_5Cycles ((uint32_t)0x00000005)
  197. #define ADC_SampleTime_71_5Cycles ((uint32_t)0x00000006)
  198. #define ADC_SampleTime_239_5Cycles ((uint32_t)0x00000007)
  199. #define IS_ADC_SAMPLE_TIME(TIME) (((TIME) == ADC_SampleTime_1_5Cycles) || \
  200. ((TIME) == ADC_SampleTime_7_5Cycles) || \
  201. ((TIME) == ADC_SampleTime_13_5Cycles) || \
  202. ((TIME) == ADC_SampleTime_28_5Cycles) || \
  203. ((TIME) == ADC_SampleTime_41_5Cycles) || \
  204. ((TIME) == ADC_SampleTime_55_5Cycles) || \
  205. ((TIME) == ADC_SampleTime_71_5Cycles) || \
  206. ((TIME) == ADC_SampleTime_239_5Cycles))
  207. /** @defgroup ADC_injected_channel_selection
  208. * @{
  209. */
  210. #define ADC_InjectedChannel_0 ((uint8_t)0x18)
  211. #define ADC_InjectedChannel_1 ((uint8_t)0x1C)
  212. #define ADC_InjectedChannel_2 ((uint8_t)0x20)
  213. #define ADC_InjectedChannel_3 ((uint8_t)0x24)
  214. #define ADC_InjectedChannel_4 ((uint8_t)0x28)
  215. #define ADC_InjectedChannel_5 ((uint8_t)0x2C)
  216. #define ADC_InjectedChannel_6 ((uint8_t)0x30)
  217. #define ADC_InjectedChannel_7 ((uint8_t)0x34)
  218. #define ADC_InjectedChannel_8 ((uint8_t)0x38)
  219. #define IS_ADC_INJECTED_CHANNEL(CHANNEL) (((CHANNEL) == ADC_InjectedChannel_1) || \
  220. ((CHANNEL) == ADC_InjectedChannel_2) || \
  221. ((CHANNEL) == ADC_InjectedChannel_3) || \
  222. ((CHANNEL) == ADC_InjectedChannel_4) || \
  223. ((CHANNEL) == ADC_InjectedChannel_5) || \
  224. ((CHANNEL) == ADC_InjectedChannel_6) || \
  225. ((CHANNEL) == ADC_InjectedChannel_7) || \
  226. ((CHANNEL) == ADC_InjectedChannel_8))
  227. /**
  228. * @}
  229. */
  230. /** @defgroup ADC_analog_watchdog_selection
  231. * @{
  232. */
  233. #define ADC_AnalogWatchdog_SingleRegEnable ((uint32_t)0x00000002)
  234. #define ADC_AnalogWatchdog_None ((uint32_t)0x00000000)
  235. #define IS_ADC_ANALOG_WATCHDOG(WATCHDOG) (((WATCHDOG) == ADC_AnalogWatchdog_SingleRegEnable) || \
  236. ((WATCHDOG) == ADC_AnalogWatchdog_None))
  237. /**
  238. * @}
  239. */
  240. /** @defgroup ADC_interrupts_definition
  241. * @{
  242. */
  243. #define ADC_IT_EOC ((uint16_t)0x0001)
  244. #define ADC_IT_AWD ((uint16_t)0x0002)
  245. #define IS_ADC_IT(IT) ((((IT) & (uint16_t)0xFFFC) == 0x00) && ((IT) != 0x00))
  246. #define IS_ADC_GET_IT(IT) (((IT) == ADC_IT_EOC) || ((IT) == ADC_IT_AWD))
  247. /**
  248. * @}
  249. */
  250. /** @defgroup ADC_flags_definition
  251. * @{
  252. */
  253. #define ADC_FLAG_AWD ((uint8_t)0x02) //ADWIF 比较标志位
  254. #define ADC_FLAG_EOC ((uint8_t)0x01) //ADIF 转换结束标志位
  255. #define IS_ADC_CLEAR_FLAG(FLAG) ((((FLAG) & (uint8_t)0xF0) == 0x00) && ((FLAG) != 0x00))
  256. #define IS_ADC_GET_FLAG(FLAG) (((FLAG) == ADC_FLAG_AWD) || ((FLAG) == ADC_FLAG_EOC))
  257. /**
  258. * @}
  259. */
  260. /** @defgroup ADC_thresholds
  261. * @{
  262. */
  263. #define IS_ADC_THRESHOLD(THRESHOLD) ((THRESHOLD) <= 0xFFF)
  264. /**
  265. * @}
  266. */
  267. /** @defgroup ADC_injected_offset
  268. * @{
  269. */
  270. #define IS_ADC_OFFSET(OFFSET) ((OFFSET) <= 0xFFF)
  271. /**
  272. * @}
  273. */
  274. /** @defgroup ADC_injected_length
  275. * @{
  276. */
  277. #define IS_ADC_INJECTED_LENGTH(LENGTH) (((LENGTH) >= 0x1) && ((LENGTH) <= 0x4))
  278. /**
  279. * @}
  280. */
  281. /** @defgroup ADC_injected_rank
  282. * @{
  283. */
  284. #define IS_ADC_INJECTED_RANK(RANK) (((RANK) >= 0x1) && ((RANK) <= 0x4))
  285. /**
  286. * @}
  287. */
  288. /** @defgroup ADC_regular_length
  289. * @{
  290. */
  291. #define IS_ADC_REGULAR_LENGTH(LENGTH) (((LENGTH) >= 0x1) && ((LENGTH) <= 0x10))
  292. /**
  293. * @}
  294. */
  295. /** @defgroup ADC_regular_rank
  296. * @{
  297. */
  298. #define IS_ADC_REGULAR_RANK(RANK) (((RANK) >= 0x1) && ((RANK) <= 0x10))
  299. /**
  300. * @}
  301. */
  302. /** @defgroup ADC_regular_discontinuous_mode_number
  303. * @{
  304. */
  305. #define IS_ADC_REGULAR_DISC_NUMBER(NUMBER) (((NUMBER) >= 0x1) && ((NUMBER) <= 0x8))
  306. /**
  307. * @}
  308. */
  309. /**
  310. * @}
  311. */
  312. /** @defgroup ADC_Exported_Macros
  313. * @{
  314. */
  315. /**
  316. * @}
  317. */
  318. /** @defgroup ADC_Exported_Functions
  319. * @{
  320. */
  321. void ADC_DeInit(ADC_TypeDef* ADCx);
  322. void ADC_Init(ADC_TypeDef* ADCx, ADC_InitTypeDef* ADC_InitStruct);
  323. void ADC_StructInit(ADC_InitTypeDef* ADC_InitStruct);
  324. void ADC_Cmd(ADC_TypeDef* ADCx, FunctionalState NewState);
  325. void ADC_DMACmd(ADC_TypeDef* ADCx, FunctionalState NewState);
  326. void ADC_ITConfig(ADC_TypeDef* ADCx, uint16_t ADC_IT, FunctionalState NewState);
  327. void ADC_SoftwareStartConvCmd(ADC_TypeDef* ADCx, FunctionalState NewState);
  328. FlagStatus ADC_GetSoftwareStartConvStatus(ADC_TypeDef* ADCx);
  329. void ADC_RegularChannelConfig(ADC_TypeDef* ADCx, uint8_t ADC_Channel, uint8_t Rank, uint8_t ADC_SampleTime);
  330. void ADC_ExternalTrigConvCmd(ADC_TypeDef* ADCx, FunctionalState NewState);
  331. uint16_t ADC_GetConversionValue(ADC_TypeDef* ADCx);
  332. void ADC_ExternalTrigInjectedConvConfig(ADC_TypeDef* ADCx, uint32_t ADC_ExternalTrigInjecConv);
  333. void ADC_ExternalTrigInjectedConvCmd(ADC_TypeDef* ADCx, FunctionalState NewState);
  334. void ADC_SoftwareStartInjectedConvCmd(ADC_TypeDef* ADCx, FunctionalState NewState);
  335. void ADC_AnalogWatchdogCmd(ADC_TypeDef* ADCx, uint32_t ADC_AnalogWatchdog);
  336. void ADC_AnalogWatchdogThresholdsConfig(ADC_TypeDef* ADCx, uint16_t HighThreshold, uint16_t LowThreshold);
  337. void ADC_AnalogWatchdogSingleChannelConfig(ADC_TypeDef* ADCx, uint8_t ADC_Channel);
  338. void ADC_TempSensorVrefintCmd(FunctionalState NewState);
  339. FlagStatus ADC_GetFlagStatus(ADC_TypeDef* ADCx, uint8_t ADC_FLAG);
  340. void ADC_ClearFlag(ADC_TypeDef* ADCx, uint8_t ADC_FLAG);
  341. ITStatus ADC_GetITStatus(ADC_TypeDef* ADCx, uint16_t ADC_IT);
  342. void ADC_ClearITPendingBit(ADC_TypeDef* ADCx, uint16_t ADC_IT);
  343. #endif /*__HAL_ADC_H */
  344. /**
  345. * @}
  346. */
  347. /**
  348. * @}
  349. */
  350. /**
  351. * @}
  352. */
  353. /*-------------------------(C) COPYRIGHT 2016 HOLOCENE ----------------------*/