HAL_adc.h 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420
  1. /**
  2. ******************************************************************************
  3. * @file HAL_adc.h
  4. * @author AE Team
  5. * @version V1.0.0
  6. * @date 28/7/2017
  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, MindMotion 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 2017 MindMotion</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_ExternalTrigConv;
  56. uint32_t ADC_DataAlign;
  57. }ADC_InitTypeDef;
  58. /**
  59. * @}
  60. */
  61. /** @defgroup ADC_Exported_Constants
  62. * @{
  63. */
  64. #define IS_ADC_ALL_PERIPH(PERIPH) (((*(uint32_t*)&(PERIPH)) == ADC1_BASE) || \
  65. ((*(uint32_t*)&(PERIPH)) == ADC2_BASE))
  66. #define IS_ADC_DMA_PERIPH(PERIPH) (((*(uint32_t*)&(PERIPH)) == ADC1_BASE) || \
  67. ((*(uint32_t*)&(PERIPH)) == ADC2_BASE))
  68. /** @defgroup ADC_Resolution
  69. * @{
  70. */
  71. #define ADC_Resolution_12b ((uint32_t)0x00000000)
  72. #define ADC_Resolution_11b ((uint32_t)0x00000080)
  73. #define ADC_Resolution_10b ((uint32_t)0x00000100)
  74. #define ADC_Resolution_9b ((uint32_t)0x00000180)
  75. #define ADC_Resolution_8b ((uint32_t)0x00000200)
  76. /**
  77. * @brief for ADC1, ADC2
  78. */
  79. #define ADC_PCLK2_PRESCARE_2 ((uint32_t)0x00000000)
  80. #define ADC_PCLK2_PRESCARE_4 ((uint32_t)0x00000008)
  81. #define ADC_PCLK2_PRESCARE_6 ((uint32_t)0x00000010)
  82. #define ADC_PCLK2_PRESCARE_8 ((uint32_t)0x00000018)
  83. #define ADC_PCLK2_PRESCARE_10 ((uint32_t)0x00000020)
  84. #define ADC_PCLK2_PRESCARE_12 ((uint32_t)0x00000028)
  85. #define ADC_PCLK2_PRESCARE_14 ((uint32_t)0x00000030)
  86. #define ADC_PCLK2_PRESCARE_16 ((uint32_t)0x00000038)
  87. #define ADC_PCLK2_PRESCARE_32 ((uint32_t)0x00000040)
  88. #define ADC_PCLK2_PRESCARE_64 ((uint32_t)0x00000048)
  89. #define ADC_PCLK2_PRESCARE_128 ((uint32_t)0x00000050)
  90. #define ADC_PCLK2_PRESCARE_256 ((uint32_t)0x00000058)
  91. #define ADC_PCLK2_PRESCARE_512 ((uint32_t)0x00000060)
  92. #define ADC_PCLK2_PRESCARE_1024 ((uint32_t)0x00000068)
  93. #define ADC_PCLK2_PRESCARE_2048 ((uint32_t)0x00000070)
  94. #define ADC_PCLK2_PRESCARE_4096 ((uint32_t)0x00000078)
  95. /** @defgroup ADC_dual_mode
  96. * @{
  97. */
  98. #define ADC_Mode_Single ((uint32_t)0x00000000)
  99. #define ADC_Mode_Single_Period ((uint32_t)0x00000200)
  100. #define ADC_Mode_Continuous_Scan ((uint32_t)0x00000400)
  101. #define IS_ADC_MODE(MODE) (((MODE) == ADC_Mode_Single) || \
  102. ((MODE) == ADC_Mode_Single_Period) || \
  103. ((MODE) == ADC_Mode_Continuous_Scan))
  104. /**
  105. * @}
  106. */
  107. /** @defgroup ADC_extrenal_trigger_sources_for_regular_channels_conversion
  108. * @{
  109. */
  110. /**
  111. * @brief for ADC1
  112. */
  113. #define ADC_ExternalTrigConv_T1_CC1 ((uint32_t)0x00000000)
  114. #define ADC_ExternalTrigConv_T1_CC2 ((uint32_t)0x00000010)
  115. #define ADC_ExternalTrigConv_T1_CC3 ((uint32_t)0x00000020)
  116. #define ADC_ExternalTrigConv_T2_CC2 ((uint32_t)0x00000030)
  117. #define ADC_ExternalTrigConv_T3_TRGO ((uint32_t)0x00000040)
  118. #define ADC_ExternalTrigConv_T4_CC4 ((uint32_t)0x00000050)
  119. #define ADC_ExternalTrigConv_T3_CC1 ((uint32_t)0x00000060)
  120. #define ADC_ExternalTrigConv_EXTI_11 ((uint32_t)0x00000070)
  121. /**
  122. * @brief for ADC2
  123. */
  124. #define ADC_ExternalTrigConv_T1_TRGO ((uint32_t)0x00000000)
  125. #define ADC_ExternalTrigConv_T1_CC4 ((uint32_t)0x00000010)
  126. #define ADC_ExternalTrigConv_T2_TRGO ((uint32_t)0x00000020)
  127. #define ADC_ExternalTrigConv_T2_CC1 ((uint32_t)0x00000030)
  128. #define ADC_ExternalTrigConv_T3_CC4 ((uint32_t)0x00000040)
  129. #define ADC_ExternalTrigConv_T4_TRGO ((uint32_t)0x00000050)
  130. #define ADC_ExternalTrigConv_T3_CC1 ((uint32_t)0x00000060)
  131. #define ADC_ExternalTrigConv_EXTI_15 ((uint32_t)0x00000070)
  132. #define IS_ADC_EXT_TRIG(REGTRIG) (((REGTRIG) == ADC_ExternalTrigConv_T1_CC1) || \
  133. ((REGTRIG) == ADC_ExternalTrigConv_T1_CC2) || \
  134. ((REGTRIG) == ADC_ExternalTrigConv_T1_CC3) || \
  135. ((REGTRIG) == ADC_ExternalTrigConv_T2_CC2) || \
  136. ((REGTRIG) == ADC_ExternalTrigConv_T3_TRGO) || \
  137. ((REGTRIG) == ADC_ExternalTrigConv_T4_CC4) || \
  138. ((REGTRIG) == ADC_ExternalTrigConv_T3_CC1) || \
  139. ((REGTRIG) == ADC_ExternalTrigConv_EXTI_11) || \
  140. ((REGTRIG) == ADC_ExternalTrigConv_T1_TRGO) || \
  141. ((REGTRIG) == ADC_ExternalTrigConv_T1_CC4) || \
  142. ((REGTRIG) == ADC_ExternalTrigConv_T2_TRGO) || \
  143. ((REGTRIG) == ADC_ExternalTrigConv_T2_CC1) || \
  144. ((REGTRIG) == ADC_ExternalTrigConv_T3_CC4) || \
  145. ((REGTRIG) == ADC_ExternalTrigConv_T4_TRGO) || \
  146. ((REGTRIG) == ADC_ExternalTrigConv_T3_CC1) || \
  147. ((REGTRIG) == ADC_ExternalTrigConv_EXTI_15))
  148. /**
  149. * @}
  150. */
  151. /** @defgroup ADC_data_align
  152. * @{
  153. */
  154. #define ADC_DataAlign_Right ((uint32_t)0x00000000)
  155. #define ADC_DataAlign_Left ((uint32_t)0x00000800)
  156. #define IS_ADC_DATA_ALIGN(ALIGN) (((ALIGN) == ADC_DataAlign_Right) || \
  157. ((ALIGN) == ADC_DataAlign_Left))
  158. /**
  159. * @}
  160. */
  161. /** @defgroup ADC_channels
  162. * @{
  163. */
  164. #define ADC_Channel_0 ((uint8_t)0x00)
  165. #define ADC_Channel_1 ((uint8_t)0x01)
  166. #define ADC_Channel_2 ((uint8_t)0x02)
  167. #define ADC_Channel_3 ((uint8_t)0x03)
  168. #define ADC_Channel_4 ((uint8_t)0x04)
  169. #define ADC_Channel_5 ((uint8_t)0x05)
  170. #define ADC_Channel_6 ((uint8_t)0x06)
  171. #define ADC_Channel_7 ((uint8_t)0x07)
  172. #define ADC_Channel_8 ((uint8_t)0x08)
  173. #define ADC_Channel_All ((uint8_t)0x0f)
  174. #define IS_ADC_CHANNEL(CHANNEL) (((CHANNEL) == ADC_Channel_0) || ((CHANNEL) == ADC_Channel_1) || \
  175. ((CHANNEL) == ADC_Channel_2) || ((CHANNEL) == ADC_Channel_3) || \
  176. ((CHANNEL) == ADC_Channel_4) || ((CHANNEL) == ADC_Channel_5) || \
  177. ((CHANNEL) == ADC_Channel_6) || ((CHANNEL) == ADC_Channel_7) || \
  178. ((CHANNEL) == ADC_Channel_8) || ((CHANNEL) == ADC_Channel_All))
  179. /**
  180. * @}
  181. */
  182. #define ADC_SMPR_SMP ((uint32_t)0x00000007) /*!< SMP[2:0] bits (Sampling time selection) */
  183. #define ADC_SMPR_SMP_0 ((uint32_t)0x00000001) /*!< Bit 0 */
  184. #define ADC_SMPR_SMP_1 ((uint32_t)0x00000002) /*!< Bit 1 */
  185. #define ADC_SMPR_SMP_2 ((uint32_t)0x00000004) /*!< Bit 2 */
  186. /** @defgroup ADC_sampling_times
  187. * @{
  188. */
  189. #define ADC_SampleTime_1_5Cycles ((uint32_t)0x00000000)
  190. #define ADC_SampleTime_7_5Cycles ((uint32_t)0x00000001)
  191. #define ADC_SampleTime_13_5Cycles ((uint32_t)0x00000002)
  192. #define ADC_SampleTime_28_5Cycles ((uint32_t)0x00000003)
  193. #define ADC_SampleTime_41_5Cycles ((uint32_t)0x00000004)
  194. #define ADC_SampleTime_55_5Cycles ((uint32_t)0x00000005)
  195. #define ADC_SampleTime_71_5Cycles ((uint32_t)0x00000006)
  196. #define ADC_SampleTime_239_5Cycles ((uint32_t)0x00000007)
  197. /** @defgroup ADC_injected_channel_selection
  198. * @{
  199. */
  200. #define ADC_InjectedChannel_0 ((uint8_t)0x18)
  201. #define ADC_InjectedChannel_1 ((uint8_t)0x1C)
  202. #define ADC_InjectedChannel_2 ((uint8_t)0x20)
  203. #define ADC_InjectedChannel_3 ((uint8_t)0x24)
  204. #define ADC_InjectedChannel_4 ((uint8_t)0x28)
  205. #define ADC_InjectedChannel_5 ((uint8_t)0x2C)
  206. #define ADC_InjectedChannel_6 ((uint8_t)0x30)
  207. #define ADC_InjectedChannel_7 ((uint8_t)0x34)
  208. #define ADC_InjectedChannel_8 ((uint8_t)0x38)
  209. #define IS_ADC_INJECTED_CHANNEL(CHANNEL) (((CHANNEL) == ADC_InjectedChannel_1) || \
  210. ((CHANNEL) == ADC_InjectedChannel_2) || \
  211. ((CHANNEL) == ADC_InjectedChannel_3) || \
  212. ((CHANNEL) == ADC_InjectedChannel_4) || \
  213. ((CHANNEL) == ADC_InjectedChannel_5) || \
  214. ((CHANNEL) == ADC_InjectedChannel_6) || \
  215. ((CHANNEL) == ADC_InjectedChannel_7) || \
  216. ((CHANNEL) == ADC_InjectedChannel_8))
  217. /**
  218. * @}
  219. */
  220. /** @defgroup ADC_analog_watchdog_selection
  221. * @{
  222. */
  223. #define ADC_AnalogWatchdog_SingleRegEnable ((uint32_t)0x00000002)
  224. #define ADC_AnalogWatchdog_None ((uint32_t)0x00000000)
  225. #define IS_ADC_ANALOG_WATCHDOG(WATCHDOG) (((WATCHDOG) == ADC_AnalogWatchdog_SingleRegEnable) || \
  226. ((WATCHDOG) == ADC_AnalogWatchdog_None))
  227. /**
  228. * @}
  229. */
  230. /** @defgroup ADC_interrupts_definition
  231. * @{
  232. */
  233. #define ADC_IT_EOC ((uint16_t)0x0001)
  234. #define ADC_IT_AWD ((uint16_t)0x0002)
  235. #define IS_ADC_IT(IT) ((((IT) & (uint16_t)0xFFFC) == 0x00) && ((IT) != 0x00))
  236. #define IS_ADC_GET_IT(IT) (((IT) == ADC_IT_EOC) || ((IT) == ADC_IT_AWD))
  237. /**
  238. * @}
  239. */
  240. /** @defgroup ADC_flags_definition
  241. * @{
  242. */
  243. #define ADC_FLAG_AWD ((uint8_t)0x02) //ADWIF ±È½Ï±ê־λ
  244. #define ADC_FLAG_EOC ((uint8_t)0x01) //ADIF ת»»½áÊø±ê־λ
  245. #define IS_ADC_CLEAR_FLAG(FLAG) ((((FLAG) & (uint8_t)0xF0) == 0x00) && ((FLAG) != 0x00))
  246. #define IS_ADC_GET_FLAG(FLAG) (((FLAG) == ADC_FLAG_AWD) || ((FLAG) == ADC_FLAG_EOC))
  247. /**
  248. * @}
  249. */
  250. /** @defgroup ADC_thresholds
  251. * @{
  252. */
  253. #define IS_ADC_THRESHOLD(THRESHOLD) ((THRESHOLD) <= 0xFFF)
  254. /**
  255. * @}
  256. */
  257. /** @defgroup ADC_injected_offset
  258. * @{
  259. */
  260. #define IS_ADC_OFFSET(OFFSET) ((OFFSET) <= 0xFFF)
  261. /**
  262. * @}
  263. */
  264. /** @defgroup ADC_injected_length
  265. * @{
  266. */
  267. #define IS_ADC_INJECTED_LENGTH(LENGTH) (((LENGTH) >= 0x1) && ((LENGTH) <= 0x4))
  268. /**
  269. * @}
  270. */
  271. /** @defgroup ADC_injected_rank
  272. * @{
  273. */
  274. #define IS_ADC_INJECTED_RANK(RANK) (((RANK) >= 0x1) && ((RANK) <= 0x4))
  275. /**
  276. * @}
  277. */
  278. /** @defgroup ADC_regular_length
  279. * @{
  280. */
  281. #define IS_ADC_REGULAR_LENGTH(LENGTH) (((LENGTH) >= 0x1) && ((LENGTH) <= 0x10))
  282. /**
  283. * @}
  284. */
  285. /** @defgroup ADC_regular_rank
  286. * @{
  287. */
  288. #define IS_ADC_REGULAR_RANK(RANK) (((RANK) >= 0x1) && ((RANK) <= 0x10))
  289. /**
  290. * @}
  291. */
  292. /** @defgroup ADC_regular_discontinuous_mode_number
  293. * @{
  294. */
  295. #define IS_ADC_REGULAR_DISC_NUMBER(NUMBER) (((NUMBER) >= 0x1) && ((NUMBER) <= 0x8))
  296. /**
  297. * @}
  298. */
  299. /**
  300. * @}
  301. */
  302. /** @defgroup ADC_Exported_Macros
  303. * @{
  304. */
  305. /**
  306. * @}
  307. */
  308. /** @defgroup ADC_Exported_Functions
  309. * @{
  310. */
  311. void ADC_DeInit(ADC_TypeDef* ADCx);
  312. void ADC_Init(ADC_TypeDef* ADCx, ADC_InitTypeDef* ADC_InitStruct);
  313. void ADC_StructInit(ADC_InitTypeDef* ADC_InitStruct);
  314. void ADC_Cmd(ADC_TypeDef* ADCx, FunctionalState NewState);
  315. void ADC_DMACmd(ADC_TypeDef* ADCx, FunctionalState NewState);
  316. void ADC_ITConfig(ADC_TypeDef* ADCx, uint16_t ADC_IT, FunctionalState NewState);
  317. void ADC_SoftwareStartConvCmd(ADC_TypeDef* ADCx, FunctionalState NewState);
  318. FlagStatus ADC_GetSoftwareStartConvStatus(ADC_TypeDef* ADCx);
  319. void ADC_RegularChannelConfig(ADC_TypeDef* ADCx, uint8_t ADC_Channel, uint8_t Rank, uint8_t ADC_SampleTime);
  320. void ADC_ExternalTrigConvCmd(ADC_TypeDef* ADCx, FunctionalState NewState);
  321. uint16_t ADC_GetConversionValue(ADC_TypeDef* ADCx);
  322. void ADC_AnalogWatchdogCmd(ADC_TypeDef* ADCx, uint32_t ADC_AnalogWatchdog);
  323. void ADC_AnalogWatchdogThresholdsConfig(ADC_TypeDef* ADCx, uint16_t HighThreshold, uint16_t LowThreshold);
  324. void ADC_AnalogWatchdogSingleChannelConfig(ADC_TypeDef* ADCx, uint8_t ADC_Channel);
  325. void ADC_TempSensorVrefintCmd(FunctionalState NewState);
  326. FlagStatus ADC_GetFlagStatus(ADC_TypeDef* ADCx, uint8_t ADC_FLAG);
  327. void ADC_ClearFlag(ADC_TypeDef* ADCx, uint8_t ADC_FLAG);
  328. ITStatus ADC_GetITStatus(ADC_TypeDef* ADCx, uint16_t ADC_IT);
  329. void ADC_ClearITPendingBit(ADC_TypeDef* ADCx, uint16_t ADC_IT);
  330. #endif /*__HAL_ADC_H */
  331. /**
  332. * @}
  333. */
  334. /**
  335. * @}
  336. */
  337. /**
  338. * @}
  339. */
  340. /*-------------------------(C) COPYRIGHT 2017 MindMotion ----------------------*/