HAL_adc.h 13 KB

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