stm32f1xx_hal_adc.h 51 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000
  1. /**
  2. ******************************************************************************
  3. * @file stm32f1xx_hal_adc.h
  4. * @author MCD Application Team
  5. * @brief Header file containing functions prototypes of ADC HAL library.
  6. ******************************************************************************
  7. * @attention
  8. *
  9. *
  10. * Copyright (c) 2016 STMicroelectronics.
  11. * All rights reserved.
  12. *
  13. * This software is licensed under terms that can be found in the LICENSE file
  14. * in the root directory of this software component.
  15. * If no LICENSE file comes with this software, it is provided AS-IS.
  16. *
  17. ******************************************************************************
  18. */
  19. /* Define to prevent recursive inclusion -------------------------------------*/
  20. #ifndef __STM32F1xx_HAL_ADC_H
  21. #define __STM32F1xx_HAL_ADC_H
  22. #ifdef __cplusplus
  23. extern "C" {
  24. #endif
  25. /* Includes ------------------------------------------------------------------*/
  26. #include "stm32f1xx_hal_def.h"
  27. /** @addtogroup STM32F1xx_HAL_Driver
  28. * @{
  29. */
  30. /** @addtogroup ADC
  31. * @{
  32. */
  33. /* Exported types ------------------------------------------------------------*/
  34. /** @defgroup ADC_Exported_Types ADC Exported Types
  35. * @{
  36. */
  37. /**
  38. * @brief Structure definition of ADC and regular group initialization
  39. * @note Parameters of this structure are shared within 2 scopes:
  40. * - Scope entire ADC (affects regular and injected groups): DataAlign, ScanConvMode.
  41. * - Scope regular group: ContinuousConvMode, NbrOfConversion, DiscontinuousConvMode, NbrOfDiscConversion, ExternalTrigConvEdge, ExternalTrigConv.
  42. * @note The setting of these parameters with function HAL_ADC_Init() is conditioned to ADC state.
  43. * ADC can be either disabled or enabled without conversion on going on regular group.
  44. */
  45. typedef struct
  46. {
  47. uint32_t DataAlign; /*!< Specifies ADC data alignment to right (MSB on register bit 11 and LSB on register bit 0) (default setting)
  48. or to left (if regular group: MSB on register bit 15 and LSB on register bit 4, if injected group (MSB kept as signed value due to potential negative value after offset application): MSB on register bit 14 and LSB on register bit 3).
  49. This parameter can be a value of @ref ADC_Data_align */
  50. uint32_t ScanConvMode; /*!< Configures the sequencer of regular and injected groups.
  51. This parameter can be associated to parameter 'DiscontinuousConvMode' to have main sequence subdivided in successive parts.
  52. If disabled: Conversion is performed in single mode (one channel converted, the one defined in rank 1).
  53. Parameters 'NbrOfConversion' and 'InjectedNbrOfConversion' are discarded (equivalent to set to 1).
  54. If enabled: Conversions are performed in sequence mode (multiple ranks defined by 'NbrOfConversion'/'InjectedNbrOfConversion' and each channel rank).
  55. Scan direction is upward: from rank1 to rank 'n'.
  56. This parameter can be a value of @ref ADC_Scan_mode
  57. Note: For regular group, this parameter should be enabled in conversion either by polling (HAL_ADC_Start with Discontinuous mode and NbrOfDiscConversion=1)
  58. or by DMA (HAL_ADC_Start_DMA), but not by interruption (HAL_ADC_Start_IT): in scan mode, interruption is triggered only on the
  59. the last conversion of the sequence. All previous conversions would be overwritten by the last one.
  60. Injected group used with scan mode has not this constraint: each rank has its own result register, no data is overwritten. */
  61. FunctionalState ContinuousConvMode; /*!< Specifies whether the conversion is performed in single mode (one conversion) or continuous mode for regular group,
  62. after the selected trigger occurred (software start or external trigger).
  63. This parameter can be set to ENABLE or DISABLE. */
  64. uint32_t NbrOfConversion; /*!< Specifies the number of ranks that will be converted within the regular group sequencer.
  65. To use regular group sequencer and convert several ranks, parameter 'ScanConvMode' must be enabled.
  66. This parameter must be a number between Min_Data = 1 and Max_Data = 16. */
  67. FunctionalState DiscontinuousConvMode; /*!< Specifies whether the conversions sequence of regular group is performed in Complete-sequence/Discontinuous-sequence (main sequence subdivided in successive parts).
  68. Discontinuous mode is used only if sequencer is enabled (parameter 'ScanConvMode'). If sequencer is disabled, this parameter is discarded.
  69. Discontinuous mode can be enabled only if continuous mode is disabled. If continuous mode is enabled, this parameter setting is discarded.
  70. This parameter can be set to ENABLE or DISABLE. */
  71. uint32_t NbrOfDiscConversion; /*!< Specifies the number of discontinuous conversions in which the main sequence of regular group (parameter NbrOfConversion) will be subdivided.
  72. If parameter 'DiscontinuousConvMode' is disabled, this parameter is discarded.
  73. This parameter must be a number between Min_Data = 1 and Max_Data = 8. */
  74. uint32_t ExternalTrigConv; /*!< Selects the external event used to trigger the conversion start of regular group.
  75. If set to ADC_SOFTWARE_START, external triggers are disabled.
  76. If set to external trigger source, triggering is on event rising edge.
  77. This parameter can be a value of @ref ADC_External_trigger_source_Regular */
  78. }ADC_InitTypeDef;
  79. /**
  80. * @brief Structure definition of ADC channel for regular group
  81. * @note The setting of these parameters with function HAL_ADC_ConfigChannel() is conditioned to ADC state.
  82. * ADC can be either disabled or enabled without conversion on going on regular group.
  83. */
  84. typedef struct
  85. {
  86. uint32_t Channel; /*!< Specifies the channel to configure into ADC regular group.
  87. This parameter can be a value of @ref ADC_channels
  88. Note: Depending on devices, some channels may not be available on package pins. Refer to device datasheet for channels availability.
  89. Note: On STM32F1 devices with several ADC: Only ADC1 can access internal measurement channels (VrefInt/TempSensor)
  90. Note: On STM32F10xx8 and STM32F10xxB devices: A low-amplitude voltage glitch may be generated (on ADC input 0) on the PA0 pin, when the ADC is converting with injection trigger.
  91. It is advised to distribute the analog channels so that Channel 0 is configured as an injected channel.
  92. Refer to errata sheet of these devices for more details. */
  93. uint32_t Rank; /*!< Specifies the rank in the regular group sequencer
  94. This parameter can be a value of @ref ADC_regular_rank
  95. Note: In case of need to disable a channel or change order of conversion sequencer, rank containing a previous channel setting can be overwritten by the new channel setting (or parameter number of conversions can be adjusted) */
  96. uint32_t SamplingTime; /*!< Sampling time value to be set for the selected channel.
  97. Unit: ADC clock cycles
  98. Conversion time is the addition of sampling time and processing time (12.5 ADC clock cycles at ADC resolution 12 bits).
  99. This parameter can be a value of @ref ADC_sampling_times
  100. Caution: This parameter updates the parameter property of the channel, that can be used into regular and/or injected groups.
  101. If this same channel has been previously configured in the other group (regular/injected), it will be updated to last setting.
  102. Note: In case of usage of internal measurement channels (VrefInt/TempSensor),
  103. sampling time constraints must be respected (sampling time can be adjusted in function of ADC clock frequency and sampling time setting)
  104. Refer to device datasheet for timings values, parameters TS_vrefint, TS_temp (values rough order: 5us to 17.1us min). */
  105. }ADC_ChannelConfTypeDef;
  106. /**
  107. * @brief ADC Configuration analog watchdog definition
  108. * @note The setting of these parameters with function is conditioned to ADC state.
  109. * ADC state can be either disabled or enabled without conversion on going on regular and injected groups.
  110. */
  111. typedef struct
  112. {
  113. uint32_t WatchdogMode; /*!< Configures the ADC analog watchdog mode: single/all channels, regular/injected group.
  114. This parameter can be a value of @ref ADC_analog_watchdog_mode. */
  115. uint32_t Channel; /*!< Selects which ADC channel to monitor by analog watchdog.
  116. This parameter has an effect only if watchdog mode is configured on single channel (parameter WatchdogMode)
  117. This parameter can be a value of @ref ADC_channels. */
  118. FunctionalState ITMode; /*!< Specifies whether the analog watchdog is configured in interrupt or polling mode.
  119. This parameter can be set to ENABLE or DISABLE */
  120. uint32_t HighThreshold; /*!< Configures the ADC analog watchdog High threshold value.
  121. This parameter must be a number between Min_Data = 0x000 and Max_Data = 0xFFF. */
  122. uint32_t LowThreshold; /*!< Configures the ADC analog watchdog High threshold value.
  123. This parameter must be a number between Min_Data = 0x000 and Max_Data = 0xFFF. */
  124. uint32_t WatchdogNumber; /*!< Reserved for future use, can be set to 0 */
  125. }ADC_AnalogWDGConfTypeDef;
  126. /**
  127. * @brief HAL ADC state machine: ADC states definition (bitfields)
  128. */
  129. /* States of ADC global scope */
  130. #define HAL_ADC_STATE_RESET 0x00000000U /*!< ADC not yet initialized or disabled */
  131. #define HAL_ADC_STATE_READY 0x00000001U /*!< ADC peripheral ready for use */
  132. #define HAL_ADC_STATE_BUSY_INTERNAL 0x00000002U /*!< ADC is busy to internal process (initialization, calibration) */
  133. #define HAL_ADC_STATE_TIMEOUT 0x00000004U /*!< TimeOut occurrence */
  134. /* States of ADC errors */
  135. #define HAL_ADC_STATE_ERROR_INTERNAL 0x00000010U /*!< Internal error occurrence */
  136. #define HAL_ADC_STATE_ERROR_CONFIG 0x00000020U /*!< Configuration error occurrence */
  137. #define HAL_ADC_STATE_ERROR_DMA 0x00000040U /*!< DMA error occurrence */
  138. /* States of ADC group regular */
  139. #define HAL_ADC_STATE_REG_BUSY 0x00000100U /*!< A conversion on group regular is ongoing or can occur (either by continuous mode,
  140. external trigger, low power auto power-on, multimode ADC master control) */
  141. #define HAL_ADC_STATE_REG_EOC 0x00000200U /*!< Conversion data available on group regular */
  142. #define HAL_ADC_STATE_REG_OVR 0x00000400U /*!< Not available on STM32F1 device: Overrun occurrence */
  143. #define HAL_ADC_STATE_REG_EOSMP 0x00000800U /*!< Not available on STM32F1 device: End Of Sampling flag raised */
  144. /* States of ADC group injected */
  145. #define HAL_ADC_STATE_INJ_BUSY 0x00001000U /*!< A conversion on group injected is ongoing or can occur (either by auto-injection mode,
  146. external trigger, low power auto power-on, multimode ADC master control) */
  147. #define HAL_ADC_STATE_INJ_EOC 0x00002000U /*!< Conversion data available on group injected */
  148. #define HAL_ADC_STATE_INJ_JQOVF 0x00004000U /*!< Not available on STM32F1 device: Injected queue overflow occurrence */
  149. /* States of ADC analog watchdogs */
  150. #define HAL_ADC_STATE_AWD1 0x00010000U /*!< Out-of-window occurrence of analog watchdog 1 */
  151. #define HAL_ADC_STATE_AWD2 0x00020000U /*!< Not available on STM32F1 device: Out-of-window occurrence of analog watchdog 2 */
  152. #define HAL_ADC_STATE_AWD3 0x00040000U /*!< Not available on STM32F1 device: Out-of-window occurrence of analog watchdog 3 */
  153. /* States of ADC multi-mode */
  154. #define HAL_ADC_STATE_MULTIMODE_SLAVE 0x00100000U /*!< ADC in multimode slave state, controlled by another ADC master ( */
  155. /**
  156. * @brief ADC handle Structure definition
  157. */
  158. typedef struct __ADC_HandleTypeDef
  159. {
  160. ADC_TypeDef *Instance; /*!< Register base address */
  161. ADC_InitTypeDef Init; /*!< ADC required parameters */
  162. DMA_HandleTypeDef *DMA_Handle; /*!< Pointer DMA Handler */
  163. HAL_LockTypeDef Lock; /*!< ADC locking object */
  164. __IO uint32_t State; /*!< ADC communication state (bitmap of ADC states) */
  165. __IO uint32_t ErrorCode; /*!< ADC Error code */
  166. #if (USE_HAL_ADC_REGISTER_CALLBACKS == 1)
  167. void (* ConvCpltCallback)(struct __ADC_HandleTypeDef *hadc); /*!< ADC conversion complete callback */
  168. void (* ConvHalfCpltCallback)(struct __ADC_HandleTypeDef *hadc); /*!< ADC conversion DMA half-transfer callback */
  169. void (* LevelOutOfWindowCallback)(struct __ADC_HandleTypeDef *hadc); /*!< ADC analog watchdog 1 callback */
  170. void (* ErrorCallback)(struct __ADC_HandleTypeDef *hadc); /*!< ADC error callback */
  171. void (* InjectedConvCpltCallback)(struct __ADC_HandleTypeDef *hadc); /*!< ADC group injected conversion complete callback */ /*!< ADC end of sampling callback */
  172. void (* MspInitCallback)(struct __ADC_HandleTypeDef *hadc); /*!< ADC Msp Init callback */
  173. void (* MspDeInitCallback)(struct __ADC_HandleTypeDef *hadc); /*!< ADC Msp DeInit callback */
  174. #endif /* USE_HAL_ADC_REGISTER_CALLBACKS */
  175. }ADC_HandleTypeDef;
  176. #if (USE_HAL_ADC_REGISTER_CALLBACKS == 1)
  177. /**
  178. * @brief HAL ADC Callback ID enumeration definition
  179. */
  180. typedef enum
  181. {
  182. HAL_ADC_CONVERSION_COMPLETE_CB_ID = 0x00U, /*!< ADC conversion complete callback ID */
  183. HAL_ADC_CONVERSION_HALF_CB_ID = 0x01U, /*!< ADC conversion DMA half-transfer callback ID */
  184. HAL_ADC_LEVEL_OUT_OF_WINDOW_1_CB_ID = 0x02U, /*!< ADC analog watchdog 1 callback ID */
  185. HAL_ADC_ERROR_CB_ID = 0x03U, /*!< ADC error callback ID */
  186. HAL_ADC_INJ_CONVERSION_COMPLETE_CB_ID = 0x04U, /*!< ADC group injected conversion complete callback ID */
  187. HAL_ADC_MSPINIT_CB_ID = 0x09U, /*!< ADC Msp Init callback ID */
  188. HAL_ADC_MSPDEINIT_CB_ID = 0x0AU /*!< ADC Msp DeInit callback ID */
  189. } HAL_ADC_CallbackIDTypeDef;
  190. /**
  191. * @brief HAL ADC Callback pointer definition
  192. */
  193. typedef void (*pADC_CallbackTypeDef)(ADC_HandleTypeDef *hadc); /*!< pointer to a ADC callback function */
  194. #endif /* USE_HAL_ADC_REGISTER_CALLBACKS */
  195. /**
  196. * @}
  197. */
  198. /* Exported constants --------------------------------------------------------*/
  199. /** @defgroup ADC_Exported_Constants ADC Exported Constants
  200. * @{
  201. */
  202. /** @defgroup ADC_Error_Code ADC Error Code
  203. * @{
  204. */
  205. #define HAL_ADC_ERROR_NONE 0x00U /*!< No error */
  206. #define HAL_ADC_ERROR_INTERNAL 0x01U /*!< ADC IP internal error: if problem of clocking,
  207. enable/disable, erroneous state */
  208. #define HAL_ADC_ERROR_OVR 0x02U /*!< Overrun error */
  209. #define HAL_ADC_ERROR_DMA 0x04U /*!< DMA transfer error */
  210. #if (USE_HAL_ADC_REGISTER_CALLBACKS == 1)
  211. #define HAL_ADC_ERROR_INVALID_CALLBACK (0x10U) /*!< Invalid Callback error */
  212. #endif /* USE_HAL_ADC_REGISTER_CALLBACKS */
  213. /**
  214. * @}
  215. */
  216. /** @defgroup ADC_Data_align ADC data alignment
  217. * @{
  218. */
  219. #define ADC_DATAALIGN_RIGHT 0x00000000U
  220. #define ADC_DATAALIGN_LEFT ((uint32_t)ADC_CR2_ALIGN)
  221. /**
  222. * @}
  223. */
  224. /** @defgroup ADC_Scan_mode ADC scan mode
  225. * @{
  226. */
  227. /* Note: Scan mode values are not among binary choices ENABLE/DISABLE for */
  228. /* compatibility with other STM32 devices having a sequencer with */
  229. /* additional options. */
  230. #define ADC_SCAN_DISABLE 0x00000000U
  231. #define ADC_SCAN_ENABLE ((uint32_t)ADC_CR1_SCAN)
  232. /**
  233. * @}
  234. */
  235. /** @defgroup ADC_External_trigger_edge_Regular ADC external trigger enable for regular group
  236. * @{
  237. */
  238. #define ADC_EXTERNALTRIGCONVEDGE_NONE 0x00000000U
  239. #define ADC_EXTERNALTRIGCONVEDGE_RISING ((uint32_t)ADC_CR2_EXTTRIG)
  240. /**
  241. * @}
  242. */
  243. /** @defgroup ADC_channels ADC channels
  244. * @{
  245. */
  246. /* Note: Depending on devices, some channels may not be available on package */
  247. /* pins. Refer to device datasheet for channels availability. */
  248. #define ADC_CHANNEL_0 0x00000000U
  249. #define ADC_CHANNEL_1 ((uint32_t)( ADC_SQR3_SQ1_0))
  250. #define ADC_CHANNEL_2 ((uint32_t)( ADC_SQR3_SQ1_1 ))
  251. #define ADC_CHANNEL_3 ((uint32_t)( ADC_SQR3_SQ1_1 | ADC_SQR3_SQ1_0))
  252. #define ADC_CHANNEL_4 ((uint32_t)( ADC_SQR3_SQ1_2 ))
  253. #define ADC_CHANNEL_5 ((uint32_t)( ADC_SQR3_SQ1_2 | ADC_SQR3_SQ1_0))
  254. #define ADC_CHANNEL_6 ((uint32_t)( ADC_SQR3_SQ1_2 | ADC_SQR3_SQ1_1 ))
  255. #define ADC_CHANNEL_7 ((uint32_t)( ADC_SQR3_SQ1_2 | ADC_SQR3_SQ1_1 | ADC_SQR3_SQ1_0))
  256. #define ADC_CHANNEL_8 ((uint32_t)( ADC_SQR3_SQ1_3 ))
  257. #define ADC_CHANNEL_9 ((uint32_t)( ADC_SQR3_SQ1_3 | ADC_SQR3_SQ1_0))
  258. #define ADC_CHANNEL_10 ((uint32_t)( ADC_SQR3_SQ1_3 | ADC_SQR3_SQ1_1 ))
  259. #define ADC_CHANNEL_11 ((uint32_t)( ADC_SQR3_SQ1_3 | ADC_SQR3_SQ1_1 | ADC_SQR3_SQ1_0))
  260. #define ADC_CHANNEL_12 ((uint32_t)( ADC_SQR3_SQ1_3 | ADC_SQR3_SQ1_2 ))
  261. #define ADC_CHANNEL_13 ((uint32_t)( ADC_SQR3_SQ1_3 | ADC_SQR3_SQ1_2 | ADC_SQR3_SQ1_0))
  262. #define ADC_CHANNEL_14 ((uint32_t)( ADC_SQR3_SQ1_3 | ADC_SQR3_SQ1_2 | ADC_SQR3_SQ1_1 ))
  263. #define ADC_CHANNEL_15 ((uint32_t)( ADC_SQR3_SQ1_3 | ADC_SQR3_SQ1_2 | ADC_SQR3_SQ1_1 | ADC_SQR3_SQ1_0))
  264. #define ADC_CHANNEL_16 ((uint32_t)(ADC_SQR3_SQ1_4 ))
  265. #define ADC_CHANNEL_17 ((uint32_t)(ADC_SQR3_SQ1_4 | ADC_SQR3_SQ1_0))
  266. #define ADC_CHANNEL_TEMPSENSOR ADC_CHANNEL_16 /* ADC internal channel (no connection on device pin) */
  267. #define ADC_CHANNEL_VREFINT ADC_CHANNEL_17 /* ADC internal channel (no connection on device pin) */
  268. /**
  269. * @}
  270. */
  271. /** @defgroup ADC_sampling_times ADC sampling times
  272. * @{
  273. */
  274. #define ADC_SAMPLETIME_1CYCLE_5 0x00000000U /*!< Sampling time 1.5 ADC clock cycle */
  275. #define ADC_SAMPLETIME_7CYCLES_5 ((uint32_t)( ADC_SMPR2_SMP0_0)) /*!< Sampling time 7.5 ADC clock cycles */
  276. #define ADC_SAMPLETIME_13CYCLES_5 ((uint32_t)( ADC_SMPR2_SMP0_1 )) /*!< Sampling time 13.5 ADC clock cycles */
  277. #define ADC_SAMPLETIME_28CYCLES_5 ((uint32_t)( ADC_SMPR2_SMP0_1 | ADC_SMPR2_SMP0_0)) /*!< Sampling time 28.5 ADC clock cycles */
  278. #define ADC_SAMPLETIME_41CYCLES_5 ((uint32_t)(ADC_SMPR2_SMP0_2 )) /*!< Sampling time 41.5 ADC clock cycles */
  279. #define ADC_SAMPLETIME_55CYCLES_5 ((uint32_t)(ADC_SMPR2_SMP0_2 | ADC_SMPR2_SMP0_0)) /*!< Sampling time 55.5 ADC clock cycles */
  280. #define ADC_SAMPLETIME_71CYCLES_5 ((uint32_t)(ADC_SMPR2_SMP0_2 | ADC_SMPR2_SMP0_1 )) /*!< Sampling time 71.5 ADC clock cycles */
  281. #define ADC_SAMPLETIME_239CYCLES_5 ((uint32_t)(ADC_SMPR2_SMP0_2 | ADC_SMPR2_SMP0_1 | ADC_SMPR2_SMP0_0)) /*!< Sampling time 239.5 ADC clock cycles */
  282. /**
  283. * @}
  284. */
  285. /** @defgroup ADC_regular_rank ADC rank into regular group
  286. * @{
  287. */
  288. #define ADC_REGULAR_RANK_1 0x00000001U
  289. #define ADC_REGULAR_RANK_2 0x00000002U
  290. #define ADC_REGULAR_RANK_3 0x00000003U
  291. #define ADC_REGULAR_RANK_4 0x00000004U
  292. #define ADC_REGULAR_RANK_5 0x00000005U
  293. #define ADC_REGULAR_RANK_6 0x00000006U
  294. #define ADC_REGULAR_RANK_7 0x00000007U
  295. #define ADC_REGULAR_RANK_8 0x00000008U
  296. #define ADC_REGULAR_RANK_9 0x00000009U
  297. #define ADC_REGULAR_RANK_10 0x0000000AU
  298. #define ADC_REGULAR_RANK_11 0x0000000BU
  299. #define ADC_REGULAR_RANK_12 0x0000000CU
  300. #define ADC_REGULAR_RANK_13 0x0000000DU
  301. #define ADC_REGULAR_RANK_14 0x0000000EU
  302. #define ADC_REGULAR_RANK_15 0x0000000FU
  303. #define ADC_REGULAR_RANK_16 0x00000010U
  304. /**
  305. * @}
  306. */
  307. /** @defgroup ADC_analog_watchdog_mode ADC analog watchdog mode
  308. * @{
  309. */
  310. #define ADC_ANALOGWATCHDOG_NONE 0x00000000U
  311. #define ADC_ANALOGWATCHDOG_SINGLE_REG ((uint32_t)(ADC_CR1_AWDSGL | ADC_CR1_AWDEN))
  312. #define ADC_ANALOGWATCHDOG_SINGLE_INJEC ((uint32_t)(ADC_CR1_AWDSGL | ADC_CR1_JAWDEN))
  313. #define ADC_ANALOGWATCHDOG_SINGLE_REGINJEC ((uint32_t)(ADC_CR1_AWDSGL | ADC_CR1_AWDEN | ADC_CR1_JAWDEN))
  314. #define ADC_ANALOGWATCHDOG_ALL_REG ((uint32_t)ADC_CR1_AWDEN)
  315. #define ADC_ANALOGWATCHDOG_ALL_INJEC ((uint32_t)ADC_CR1_JAWDEN)
  316. #define ADC_ANALOGWATCHDOG_ALL_REGINJEC ((uint32_t)(ADC_CR1_AWDEN | ADC_CR1_JAWDEN))
  317. /**
  318. * @}
  319. */
  320. /** @defgroup ADC_conversion_group ADC conversion group
  321. * @{
  322. */
  323. #define ADC_REGULAR_GROUP ((uint32_t)(ADC_FLAG_EOC))
  324. #define ADC_INJECTED_GROUP ((uint32_t)(ADC_FLAG_JEOC))
  325. #define ADC_REGULAR_INJECTED_GROUP ((uint32_t)(ADC_FLAG_EOC | ADC_FLAG_JEOC))
  326. /**
  327. * @}
  328. */
  329. /** @defgroup ADC_Event_type ADC Event type
  330. * @{
  331. */
  332. #define ADC_AWD_EVENT ((uint32_t)ADC_FLAG_AWD) /*!< ADC Analog watchdog event */
  333. #define ADC_AWD1_EVENT ADC_AWD_EVENT /*!< ADC Analog watchdog 1 event: Alternate naming for compatibility with other STM32 devices having several analog watchdogs */
  334. /**
  335. * @}
  336. */
  337. /** @defgroup ADC_interrupts_definition ADC interrupts definition
  338. * @{
  339. */
  340. #define ADC_IT_EOC ADC_CR1_EOCIE /*!< ADC End of Regular Conversion interrupt source */
  341. #define ADC_IT_JEOC ADC_CR1_JEOCIE /*!< ADC End of Injected Conversion interrupt source */
  342. #define ADC_IT_AWD ADC_CR1_AWDIE /*!< ADC Analog watchdog interrupt source */
  343. /**
  344. * @}
  345. */
  346. /** @defgroup ADC_flags_definition ADC flags definition
  347. * @{
  348. */
  349. #define ADC_FLAG_STRT ADC_SR_STRT /*!< ADC Regular group start flag */
  350. #define ADC_FLAG_JSTRT ADC_SR_JSTRT /*!< ADC Injected group start flag */
  351. #define ADC_FLAG_EOC ADC_SR_EOC /*!< ADC End of Regular conversion flag */
  352. #define ADC_FLAG_JEOC ADC_SR_JEOC /*!< ADC End of Injected conversion flag */
  353. #define ADC_FLAG_AWD ADC_SR_AWD /*!< ADC Analog watchdog flag */
  354. /**
  355. * @}
  356. */
  357. /**
  358. * @}
  359. */
  360. /* Private constants ---------------------------------------------------------*/
  361. /** @addtogroup ADC_Private_Constants ADC Private Constants
  362. * @{
  363. */
  364. /** @defgroup ADC_conversion_cycles ADC conversion cycles
  365. * @{
  366. */
  367. /* ADC conversion cycles (unit: ADC clock cycles) */
  368. /* (selected sampling time + conversion time of 12.5 ADC clock cycles, with */
  369. /* resolution 12 bits) */
  370. #define ADC_CONVERSIONCLOCKCYCLES_SAMPLETIME_1CYCLE5 14U
  371. #define ADC_CONVERSIONCLOCKCYCLES_SAMPLETIME_7CYCLES5 20U
  372. #define ADC_CONVERSIONCLOCKCYCLES_SAMPLETIME_13CYCLES5 26U
  373. #define ADC_CONVERSIONCLOCKCYCLES_SAMPLETIME_28CYCLES5 41U
  374. #define ADC_CONVERSIONCLOCKCYCLES_SAMPLETIME_41CYCLES5 54U
  375. #define ADC_CONVERSIONCLOCKCYCLES_SAMPLETIME_55CYCLES5 68U
  376. #define ADC_CONVERSIONCLOCKCYCLES_SAMPLETIME_71CYCLES5 84U
  377. #define ADC_CONVERSIONCLOCKCYCLES_SAMPLETIME_239CYCLES5 252U
  378. /**
  379. * @}
  380. */
  381. /** @defgroup ADC_sampling_times_all_channels ADC sampling times all channels
  382. * @{
  383. */
  384. #define ADC_SAMPLETIME_ALLCHANNELS_SMPR2BIT2 \
  385. (ADC_SMPR2_SMP9_2 | ADC_SMPR2_SMP8_2 | ADC_SMPR2_SMP7_2 | ADC_SMPR2_SMP6_2 | \
  386. ADC_SMPR2_SMP5_2 | ADC_SMPR2_SMP4_2 | ADC_SMPR2_SMP3_2 | ADC_SMPR2_SMP2_2 | \
  387. ADC_SMPR2_SMP1_2 | ADC_SMPR2_SMP0_2)
  388. #define ADC_SAMPLETIME_ALLCHANNELS_SMPR1BIT2 \
  389. (ADC_SMPR1_SMP17_2 | ADC_SMPR1_SMP16_2 | ADC_SMPR1_SMP15_2 | ADC_SMPR1_SMP14_2 | \
  390. ADC_SMPR1_SMP13_2 | ADC_SMPR1_SMP12_2 | ADC_SMPR1_SMP11_2 | ADC_SMPR1_SMP10_2 )
  391. #define ADC_SAMPLETIME_ALLCHANNELS_SMPR2BIT1 \
  392. (ADC_SMPR2_SMP9_1 | ADC_SMPR2_SMP8_1 | ADC_SMPR2_SMP7_1 | ADC_SMPR2_SMP6_1 | \
  393. ADC_SMPR2_SMP5_1 | ADC_SMPR2_SMP4_1 | ADC_SMPR2_SMP3_1 | ADC_SMPR2_SMP2_1 | \
  394. ADC_SMPR2_SMP1_1 | ADC_SMPR2_SMP0_1)
  395. #define ADC_SAMPLETIME_ALLCHANNELS_SMPR1BIT1 \
  396. (ADC_SMPR1_SMP17_1 | ADC_SMPR1_SMP16_1 | ADC_SMPR1_SMP15_1 | ADC_SMPR1_SMP14_1 | \
  397. ADC_SMPR1_SMP13_1 | ADC_SMPR1_SMP12_1 | ADC_SMPR1_SMP11_1 | ADC_SMPR1_SMP10_1 )
  398. #define ADC_SAMPLETIME_ALLCHANNELS_SMPR2BIT0 \
  399. (ADC_SMPR2_SMP9_0 | ADC_SMPR2_SMP8_0 | ADC_SMPR2_SMP7_0 | ADC_SMPR2_SMP6_0 | \
  400. ADC_SMPR2_SMP5_0 | ADC_SMPR2_SMP4_0 | ADC_SMPR2_SMP3_0 | ADC_SMPR2_SMP2_0 | \
  401. ADC_SMPR2_SMP1_0 | ADC_SMPR2_SMP0_0)
  402. #define ADC_SAMPLETIME_ALLCHANNELS_SMPR1BIT0 \
  403. (ADC_SMPR1_SMP17_0 | ADC_SMPR1_SMP16_0 | ADC_SMPR1_SMP15_0 | ADC_SMPR1_SMP14_0 | \
  404. ADC_SMPR1_SMP13_0 | ADC_SMPR1_SMP12_0 | ADC_SMPR1_SMP11_0 | ADC_SMPR1_SMP10_0 )
  405. #define ADC_SAMPLETIME_1CYCLE5_SMPR2ALLCHANNELS 0x00000000U
  406. #define ADC_SAMPLETIME_7CYCLES5_SMPR2ALLCHANNELS (ADC_SAMPLETIME_ALLCHANNELS_SMPR2BIT0)
  407. #define ADC_SAMPLETIME_13CYCLES5_SMPR2ALLCHANNELS (ADC_SAMPLETIME_ALLCHANNELS_SMPR2BIT1)
  408. #define ADC_SAMPLETIME_28CYCLES5_SMPR2ALLCHANNELS (ADC_SAMPLETIME_ALLCHANNELS_SMPR2BIT1 | ADC_SAMPLETIME_ALLCHANNELS_SMPR2BIT0)
  409. #define ADC_SAMPLETIME_41CYCLES5_SMPR2ALLCHANNELS (ADC_SAMPLETIME_ALLCHANNELS_SMPR2BIT2)
  410. #define ADC_SAMPLETIME_55CYCLES5_SMPR2ALLCHANNELS (ADC_SAMPLETIME_ALLCHANNELS_SMPR2BIT2 | ADC_SAMPLETIME_ALLCHANNELS_SMPR2BIT0)
  411. #define ADC_SAMPLETIME_71CYCLES5_SMPR2ALLCHANNELS (ADC_SAMPLETIME_ALLCHANNELS_SMPR2BIT2 | ADC_SAMPLETIME_ALLCHANNELS_SMPR2BIT1)
  412. #define ADC_SAMPLETIME_239CYCLES5_SMPR2ALLCHANNELS (ADC_SAMPLETIME_ALLCHANNELS_SMPR2BIT2 | ADC_SAMPLETIME_ALLCHANNELS_SMPR2BIT1 | ADC_SAMPLETIME_ALLCHANNELS_SMPR2BIT0)
  413. #define ADC_SAMPLETIME_1CYCLE5_SMPR1ALLCHANNELS 0x00000000U
  414. #define ADC_SAMPLETIME_7CYCLES5_SMPR1ALLCHANNELS (ADC_SAMPLETIME_ALLCHANNELS_SMPR1BIT0)
  415. #define ADC_SAMPLETIME_13CYCLES5_SMPR1ALLCHANNELS (ADC_SAMPLETIME_ALLCHANNELS_SMPR1BIT1)
  416. #define ADC_SAMPLETIME_28CYCLES5_SMPR1ALLCHANNELS (ADC_SAMPLETIME_ALLCHANNELS_SMPR1BIT1 | ADC_SAMPLETIME_ALLCHANNELS_SMPR1BIT0)
  417. #define ADC_SAMPLETIME_41CYCLES5_SMPR1ALLCHANNELS (ADC_SAMPLETIME_ALLCHANNELS_SMPR1BIT2)
  418. #define ADC_SAMPLETIME_55CYCLES5_SMPR1ALLCHANNELS (ADC_SAMPLETIME_ALLCHANNELS_SMPR1BIT2 | ADC_SAMPLETIME_ALLCHANNELS_SMPR1BIT0)
  419. #define ADC_SAMPLETIME_71CYCLES5_SMPR1ALLCHANNELS (ADC_SAMPLETIME_ALLCHANNELS_SMPR1BIT2 | ADC_SAMPLETIME_ALLCHANNELS_SMPR1BIT1)
  420. #define ADC_SAMPLETIME_239CYCLES5_SMPR1ALLCHANNELS (ADC_SAMPLETIME_ALLCHANNELS_SMPR1BIT2 | ADC_SAMPLETIME_ALLCHANNELS_SMPR1BIT1 | ADC_SAMPLETIME_ALLCHANNELS_SMPR1BIT0)
  421. /**
  422. * @}
  423. */
  424. /* Combination of all post-conversion flags bits: EOC/EOS, JEOC/JEOS, OVR, AWDx */
  425. #define ADC_FLAG_POSTCONV_ALL (ADC_FLAG_EOC | ADC_FLAG_JEOC | ADC_FLAG_AWD )
  426. /**
  427. * @}
  428. */
  429. /* Exported macro ------------------------------------------------------------*/
  430. /** @defgroup ADC_Exported_Macros ADC Exported Macros
  431. * @{
  432. */
  433. /* Macro for internal HAL driver usage, and possibly can be used into code of */
  434. /* final user. */
  435. /**
  436. * @brief Enable the ADC peripheral
  437. * @note ADC enable requires a delay for ADC stabilization time
  438. * (refer to device datasheet, parameter tSTAB)
  439. * @note On STM32F1, if ADC is already enabled this macro trigs a conversion
  440. * SW start on regular group.
  441. * @param __HANDLE__: ADC handle
  442. * @retval None
  443. */
  444. #define __HAL_ADC_ENABLE(__HANDLE__) \
  445. (SET_BIT((__HANDLE__)->Instance->CR2, (ADC_CR2_ADON)))
  446. /**
  447. * @brief Disable the ADC peripheral
  448. * @param __HANDLE__: ADC handle
  449. * @retval None
  450. */
  451. #define __HAL_ADC_DISABLE(__HANDLE__) \
  452. (CLEAR_BIT((__HANDLE__)->Instance->CR2, (ADC_CR2_ADON)))
  453. /** @brief Enable the ADC end of conversion interrupt.
  454. * @param __HANDLE__: ADC handle
  455. * @param __INTERRUPT__: ADC Interrupt
  456. * This parameter can be any combination of the following values:
  457. * @arg ADC_IT_EOC: ADC End of Regular Conversion interrupt source
  458. * @arg ADC_IT_JEOC: ADC End of Injected Conversion interrupt source
  459. * @arg ADC_IT_AWD: ADC Analog watchdog interrupt source
  460. * @retval None
  461. */
  462. #define __HAL_ADC_ENABLE_IT(__HANDLE__, __INTERRUPT__) \
  463. (SET_BIT((__HANDLE__)->Instance->CR1, (__INTERRUPT__)))
  464. /** @brief Disable the ADC end of conversion interrupt.
  465. * @param __HANDLE__: ADC handle
  466. * @param __INTERRUPT__: ADC Interrupt
  467. * This parameter can be any combination of the following values:
  468. * @arg ADC_IT_EOC: ADC End of Regular Conversion interrupt source
  469. * @arg ADC_IT_JEOC: ADC End of Injected Conversion interrupt source
  470. * @arg ADC_IT_AWD: ADC Analog watchdog interrupt source
  471. * @retval None
  472. */
  473. #define __HAL_ADC_DISABLE_IT(__HANDLE__, __INTERRUPT__) \
  474. (CLEAR_BIT((__HANDLE__)->Instance->CR1, (__INTERRUPT__)))
  475. /** @brief Checks if the specified ADC interrupt source is enabled or disabled.
  476. * @param __HANDLE__: ADC handle
  477. * @param __INTERRUPT__: ADC interrupt source to check
  478. * This parameter can be any combination of the following values:
  479. * @arg ADC_IT_EOC: ADC End of Regular Conversion interrupt source
  480. * @arg ADC_IT_JEOC: ADC End of Injected Conversion interrupt source
  481. * @arg ADC_IT_AWD: ADC Analog watchdog interrupt source
  482. * @retval None
  483. */
  484. #define __HAL_ADC_GET_IT_SOURCE(__HANDLE__, __INTERRUPT__) \
  485. (((__HANDLE__)->Instance->CR1 & (__INTERRUPT__)) == (__INTERRUPT__))
  486. /** @brief Get the selected ADC's flag status.
  487. * @param __HANDLE__: ADC handle
  488. * @param __FLAG__: ADC flag
  489. * This parameter can be any combination of the following values:
  490. * @arg ADC_FLAG_STRT: ADC Regular group start flag
  491. * @arg ADC_FLAG_JSTRT: ADC Injected group start flag
  492. * @arg ADC_FLAG_EOC: ADC End of Regular conversion flag
  493. * @arg ADC_FLAG_JEOC: ADC End of Injected conversion flag
  494. * @arg ADC_FLAG_AWD: ADC Analog watchdog flag
  495. * @retval None
  496. */
  497. #define __HAL_ADC_GET_FLAG(__HANDLE__, __FLAG__) \
  498. ((((__HANDLE__)->Instance->SR) & (__FLAG__)) == (__FLAG__))
  499. /** @brief Clear the ADC's pending flags
  500. * @param __HANDLE__: ADC handle
  501. * @param __FLAG__: ADC flag
  502. * This parameter can be any combination of the following values:
  503. * @arg ADC_FLAG_STRT: ADC Regular group start flag
  504. * @arg ADC_FLAG_JSTRT: ADC Injected group start flag
  505. * @arg ADC_FLAG_EOC: ADC End of Regular conversion flag
  506. * @arg ADC_FLAG_JEOC: ADC End of Injected conversion flag
  507. * @arg ADC_FLAG_AWD: ADC Analog watchdog flag
  508. * @retval None
  509. */
  510. #define __HAL_ADC_CLEAR_FLAG(__HANDLE__, __FLAG__) \
  511. (WRITE_REG((__HANDLE__)->Instance->SR, ~(__FLAG__)))
  512. /** @brief Reset ADC handle state
  513. * @param __HANDLE__: ADC handle
  514. * @retval None
  515. */
  516. #if (USE_HAL_ADC_REGISTER_CALLBACKS == 1)
  517. #define __HAL_ADC_RESET_HANDLE_STATE(__HANDLE__) \
  518. do{ \
  519. (__HANDLE__)->State = HAL_ADC_STATE_RESET; \
  520. (__HANDLE__)->MspInitCallback = NULL; \
  521. (__HANDLE__)->MspDeInitCallback = NULL; \
  522. } while(0)
  523. #else
  524. #define __HAL_ADC_RESET_HANDLE_STATE(__HANDLE__) \
  525. ((__HANDLE__)->State = HAL_ADC_STATE_RESET)
  526. #endif
  527. /**
  528. * @}
  529. */
  530. /* Private macro ------------------------------------------------------------*/
  531. /** @defgroup ADC_Private_Macros ADC Private Macros
  532. * @{
  533. */
  534. /* Macro reserved for internal HAL driver usage, not intended to be used in */
  535. /* code of final user. */
  536. /**
  537. * @brief Verification of ADC state: enabled or disabled
  538. * @param __HANDLE__: ADC handle
  539. * @retval SET (ADC enabled) or RESET (ADC disabled)
  540. */
  541. #define ADC_IS_ENABLE(__HANDLE__) \
  542. ((( ((__HANDLE__)->Instance->CR2 & ADC_CR2_ADON) == ADC_CR2_ADON ) \
  543. ) ? SET : RESET)
  544. /**
  545. * @brief Test if conversion trigger of regular group is software start
  546. * or external trigger.
  547. * @param __HANDLE__: ADC handle
  548. * @retval SET (software start) or RESET (external trigger)
  549. */
  550. #define ADC_IS_SOFTWARE_START_REGULAR(__HANDLE__) \
  551. (READ_BIT((__HANDLE__)->Instance->CR2, ADC_CR2_EXTSEL) == ADC_SOFTWARE_START)
  552. /**
  553. * @brief Test if conversion trigger of injected group is software start
  554. * or external trigger.
  555. * @param __HANDLE__: ADC handle
  556. * @retval SET (software start) or RESET (external trigger)
  557. */
  558. #define ADC_IS_SOFTWARE_START_INJECTED(__HANDLE__) \
  559. (READ_BIT((__HANDLE__)->Instance->CR2, ADC_CR2_JEXTSEL) == ADC_INJECTED_SOFTWARE_START)
  560. /**
  561. * @brief Simultaneously clears and sets specific bits of the handle State
  562. * @note: ADC_STATE_CLR_SET() macro is merely aliased to generic macro MODIFY_REG(),
  563. * the first parameter is the ADC handle State, the second parameter is the
  564. * bit field to clear, the third and last parameter is the bit field to set.
  565. * @retval None
  566. */
  567. #define ADC_STATE_CLR_SET MODIFY_REG
  568. /**
  569. * @brief Clear ADC error code (set it to error code: "no error")
  570. * @param __HANDLE__: ADC handle
  571. * @retval None
  572. */
  573. #define ADC_CLEAR_ERRORCODE(__HANDLE__) \
  574. ((__HANDLE__)->ErrorCode = HAL_ADC_ERROR_NONE)
  575. /**
  576. * @brief Set ADC number of conversions into regular channel sequence length.
  577. * @param _NbrOfConversion_: Regular channel sequence length
  578. * @retval None
  579. */
  580. #define ADC_SQR1_L_SHIFT(_NbrOfConversion_) \
  581. (((_NbrOfConversion_) - (uint8_t)1) << ADC_SQR1_L_Pos)
  582. /**
  583. * @brief Set the ADC's sample time for channel numbers between 10 and 18.
  584. * @param _SAMPLETIME_: Sample time parameter.
  585. * @param _CHANNELNB_: Channel number.
  586. * @retval None
  587. */
  588. #define ADC_SMPR1(_SAMPLETIME_, _CHANNELNB_) \
  589. ((_SAMPLETIME_) << (ADC_SMPR1_SMP11_Pos * ((_CHANNELNB_) - 10)))
  590. /**
  591. * @brief Set the ADC's sample time for channel numbers between 0 and 9.
  592. * @param _SAMPLETIME_: Sample time parameter.
  593. * @param _CHANNELNB_: Channel number.
  594. * @retval None
  595. */
  596. #define ADC_SMPR2(_SAMPLETIME_, _CHANNELNB_) \
  597. ((_SAMPLETIME_) << (ADC_SMPR2_SMP1_Pos * (_CHANNELNB_)))
  598. /**
  599. * @brief Set the selected regular channel rank for rank between 1 and 6.
  600. * @param _CHANNELNB_: Channel number.
  601. * @param _RANKNB_: Rank number.
  602. * @retval None
  603. */
  604. #define ADC_SQR3_RK(_CHANNELNB_, _RANKNB_) \
  605. ((_CHANNELNB_) << (ADC_SQR3_SQ2_Pos * ((_RANKNB_) - 1)))
  606. /**
  607. * @brief Set the selected regular channel rank for rank between 7 and 12.
  608. * @param _CHANNELNB_: Channel number.
  609. * @param _RANKNB_: Rank number.
  610. * @retval None
  611. */
  612. #define ADC_SQR2_RK(_CHANNELNB_, _RANKNB_) \
  613. ((_CHANNELNB_) << (ADC_SQR2_SQ8_Pos * ((_RANKNB_) - 7)))
  614. /**
  615. * @brief Set the selected regular channel rank for rank between 13 and 16.
  616. * @param _CHANNELNB_: Channel number.
  617. * @param _RANKNB_: Rank number.
  618. * @retval None
  619. */
  620. #define ADC_SQR1_RK(_CHANNELNB_, _RANKNB_) \
  621. ((_CHANNELNB_) << (ADC_SQR1_SQ14_Pos * ((_RANKNB_) - 13)))
  622. /**
  623. * @brief Set the injected sequence length.
  624. * @param _JSQR_JL_: Sequence length.
  625. * @retval None
  626. */
  627. #define ADC_JSQR_JL_SHIFT(_JSQR_JL_) \
  628. (((_JSQR_JL_) -1) << ADC_JSQR_JL_Pos)
  629. /**
  630. * @brief Set the selected injected channel rank
  631. * Note: on STM32F1 devices, channel rank position in JSQR register
  632. * is depending on total number of ranks selected into
  633. * injected sequencer (ranks sequence starting from 4-JL)
  634. * @param _CHANNELNB_: Channel number.
  635. * @param _RANKNB_: Rank number.
  636. * @param _JSQR_JL_: Sequence length.
  637. * @retval None
  638. */
  639. #define ADC_JSQR_RK_JL(_CHANNELNB_, _RANKNB_, _JSQR_JL_) \
  640. ((_CHANNELNB_) << (ADC_JSQR_JSQ2_Pos * ((4 - ((_JSQR_JL_) - (_RANKNB_))) - 1)))
  641. /**
  642. * @brief Enable ADC continuous conversion mode.
  643. * @param _CONTINUOUS_MODE_: Continuous mode.
  644. * @retval None
  645. */
  646. #define ADC_CR2_CONTINUOUS(_CONTINUOUS_MODE_) \
  647. ((_CONTINUOUS_MODE_) << ADC_CR2_CONT_Pos)
  648. /**
  649. * @brief Configures the number of discontinuous conversions for the regular group channels.
  650. * @param _NBR_DISCONTINUOUS_CONV_: Number of discontinuous conversions.
  651. * @retval None
  652. */
  653. #define ADC_CR1_DISCONTINUOUS_NUM(_NBR_DISCONTINUOUS_CONV_) \
  654. (((_NBR_DISCONTINUOUS_CONV_) - 1) << ADC_CR1_DISCNUM_Pos)
  655. /**
  656. * @brief Enable ADC scan mode to convert multiple ranks with sequencer.
  657. * @param _SCAN_MODE_: Scan conversion mode.
  658. * @retval None
  659. */
  660. /* Note: Scan mode is compared to ENABLE for legacy purpose, this parameter */
  661. /* is equivalent to ADC_SCAN_ENABLE. */
  662. #define ADC_CR1_SCAN_SET(_SCAN_MODE_) \
  663. (( ((_SCAN_MODE_) == ADC_SCAN_ENABLE) || ((_SCAN_MODE_) == ENABLE) \
  664. )? (ADC_SCAN_ENABLE) : (ADC_SCAN_DISABLE) \
  665. )
  666. /**
  667. * @brief Get the maximum ADC conversion cycles on all channels.
  668. * Returns the selected sampling time + conversion time (12.5 ADC clock cycles)
  669. * Approximation of sampling time within 4 ranges, returns the highest value:
  670. * below 7.5 cycles {1.5 cycle; 7.5 cycles},
  671. * between 13.5 cycles and 28.5 cycles {13.5 cycles; 28.5 cycles}
  672. * between 41.5 cycles and 71.5 cycles {41.5 cycles; 55.5 cycles; 71.5cycles}
  673. * equal to 239.5 cycles
  674. * Unit: ADC clock cycles
  675. * @param __HANDLE__: ADC handle
  676. * @retval ADC conversion cycles on all channels
  677. */
  678. #define ADC_CONVCYCLES_MAX_RANGE(__HANDLE__) \
  679. (( (((__HANDLE__)->Instance->SMPR2 & ADC_SAMPLETIME_ALLCHANNELS_SMPR2BIT2) == RESET) && \
  680. (((__HANDLE__)->Instance->SMPR1 & ADC_SAMPLETIME_ALLCHANNELS_SMPR1BIT2) == RESET) ) ? \
  681. \
  682. (( (((__HANDLE__)->Instance->SMPR2 & ADC_SAMPLETIME_ALLCHANNELS_SMPR2BIT1) == RESET) && \
  683. (((__HANDLE__)->Instance->SMPR1 & ADC_SAMPLETIME_ALLCHANNELS_SMPR1BIT1) == RESET) ) ? \
  684. ADC_CONVERSIONCLOCKCYCLES_SAMPLETIME_7CYCLES5 : ADC_CONVERSIONCLOCKCYCLES_SAMPLETIME_28CYCLES5) \
  685. : \
  686. ((((((__HANDLE__)->Instance->SMPR2 & ADC_SAMPLETIME_ALLCHANNELS_SMPR2BIT1) == RESET) && \
  687. (((__HANDLE__)->Instance->SMPR1 & ADC_SAMPLETIME_ALLCHANNELS_SMPR1BIT1) == RESET)) || \
  688. ((((__HANDLE__)->Instance->SMPR2 & ADC_SAMPLETIME_ALLCHANNELS_SMPR1BIT0) == RESET) && \
  689. (((__HANDLE__)->Instance->SMPR1 & ADC_SAMPLETIME_ALLCHANNELS_SMPR1BIT0) == RESET))) ? \
  690. ADC_CONVERSIONCLOCKCYCLES_SAMPLETIME_71CYCLES5 : ADC_CONVERSIONCLOCKCYCLES_SAMPLETIME_239CYCLES5) \
  691. )
  692. #define IS_ADC_DATA_ALIGN(ALIGN) (((ALIGN) == ADC_DATAALIGN_RIGHT) || \
  693. ((ALIGN) == ADC_DATAALIGN_LEFT) )
  694. #define IS_ADC_SCAN_MODE(SCAN_MODE) (((SCAN_MODE) == ADC_SCAN_DISABLE) || \
  695. ((SCAN_MODE) == ADC_SCAN_ENABLE) )
  696. #define IS_ADC_EXTTRIG_EDGE(EDGE) (((EDGE) == ADC_EXTERNALTRIGCONVEDGE_NONE) || \
  697. ((EDGE) == ADC_EXTERNALTRIGCONVEDGE_RISING) )
  698. #define IS_ADC_CHANNEL(CHANNEL) (((CHANNEL) == ADC_CHANNEL_0) || \
  699. ((CHANNEL) == ADC_CHANNEL_1) || \
  700. ((CHANNEL) == ADC_CHANNEL_2) || \
  701. ((CHANNEL) == ADC_CHANNEL_3) || \
  702. ((CHANNEL) == ADC_CHANNEL_4) || \
  703. ((CHANNEL) == ADC_CHANNEL_5) || \
  704. ((CHANNEL) == ADC_CHANNEL_6) || \
  705. ((CHANNEL) == ADC_CHANNEL_7) || \
  706. ((CHANNEL) == ADC_CHANNEL_8) || \
  707. ((CHANNEL) == ADC_CHANNEL_9) || \
  708. ((CHANNEL) == ADC_CHANNEL_10) || \
  709. ((CHANNEL) == ADC_CHANNEL_11) || \
  710. ((CHANNEL) == ADC_CHANNEL_12) || \
  711. ((CHANNEL) == ADC_CHANNEL_13) || \
  712. ((CHANNEL) == ADC_CHANNEL_14) || \
  713. ((CHANNEL) == ADC_CHANNEL_15) || \
  714. ((CHANNEL) == ADC_CHANNEL_16) || \
  715. ((CHANNEL) == ADC_CHANNEL_17) )
  716. #define IS_ADC_SAMPLE_TIME(TIME) (((TIME) == ADC_SAMPLETIME_1CYCLE_5) || \
  717. ((TIME) == ADC_SAMPLETIME_7CYCLES_5) || \
  718. ((TIME) == ADC_SAMPLETIME_13CYCLES_5) || \
  719. ((TIME) == ADC_SAMPLETIME_28CYCLES_5) || \
  720. ((TIME) == ADC_SAMPLETIME_41CYCLES_5) || \
  721. ((TIME) == ADC_SAMPLETIME_55CYCLES_5) || \
  722. ((TIME) == ADC_SAMPLETIME_71CYCLES_5) || \
  723. ((TIME) == ADC_SAMPLETIME_239CYCLES_5) )
  724. #define IS_ADC_REGULAR_RANK(CHANNEL) (((CHANNEL) == ADC_REGULAR_RANK_1 ) || \
  725. ((CHANNEL) == ADC_REGULAR_RANK_2 ) || \
  726. ((CHANNEL) == ADC_REGULAR_RANK_3 ) || \
  727. ((CHANNEL) == ADC_REGULAR_RANK_4 ) || \
  728. ((CHANNEL) == ADC_REGULAR_RANK_5 ) || \
  729. ((CHANNEL) == ADC_REGULAR_RANK_6 ) || \
  730. ((CHANNEL) == ADC_REGULAR_RANK_7 ) || \
  731. ((CHANNEL) == ADC_REGULAR_RANK_8 ) || \
  732. ((CHANNEL) == ADC_REGULAR_RANK_9 ) || \
  733. ((CHANNEL) == ADC_REGULAR_RANK_10) || \
  734. ((CHANNEL) == ADC_REGULAR_RANK_11) || \
  735. ((CHANNEL) == ADC_REGULAR_RANK_12) || \
  736. ((CHANNEL) == ADC_REGULAR_RANK_13) || \
  737. ((CHANNEL) == ADC_REGULAR_RANK_14) || \
  738. ((CHANNEL) == ADC_REGULAR_RANK_15) || \
  739. ((CHANNEL) == ADC_REGULAR_RANK_16) )
  740. #define IS_ADC_ANALOG_WATCHDOG_MODE(WATCHDOG) (((WATCHDOG) == ADC_ANALOGWATCHDOG_NONE) || \
  741. ((WATCHDOG) == ADC_ANALOGWATCHDOG_SINGLE_REG) || \
  742. ((WATCHDOG) == ADC_ANALOGWATCHDOG_SINGLE_INJEC) || \
  743. ((WATCHDOG) == ADC_ANALOGWATCHDOG_SINGLE_REGINJEC) || \
  744. ((WATCHDOG) == ADC_ANALOGWATCHDOG_ALL_REG) || \
  745. ((WATCHDOG) == ADC_ANALOGWATCHDOG_ALL_INJEC) || \
  746. ((WATCHDOG) == ADC_ANALOGWATCHDOG_ALL_REGINJEC) )
  747. #define IS_ADC_CONVERSION_GROUP(CONVERSION) (((CONVERSION) == ADC_REGULAR_GROUP) || \
  748. ((CONVERSION) == ADC_INJECTED_GROUP) || \
  749. ((CONVERSION) == ADC_REGULAR_INJECTED_GROUP) )
  750. #define IS_ADC_EVENT_TYPE(EVENT) ((EVENT) == ADC_AWD_EVENT)
  751. /** @defgroup ADC_range_verification ADC range verification
  752. * For a unique ADC resolution: 12 bits
  753. * @{
  754. */
  755. #define IS_ADC_RANGE(ADC_VALUE) ((ADC_VALUE) <= 0x0FFFU)
  756. /**
  757. * @}
  758. */
  759. /** @defgroup ADC_regular_nb_conv_verification ADC regular nb conv verification
  760. * @{
  761. */
  762. #define IS_ADC_REGULAR_NB_CONV(LENGTH) (((LENGTH) >= 1U) && ((LENGTH) <= 16U))
  763. /**
  764. * @}
  765. */
  766. /** @defgroup ADC_regular_discontinuous_mode_number_verification ADC regular discontinuous mode number verification
  767. * @{
  768. */
  769. #define IS_ADC_REGULAR_DISCONT_NUMBER(NUMBER) (((NUMBER) >= 1U) && ((NUMBER) <= 8U))
  770. /**
  771. * @}
  772. */
  773. /**
  774. * @}
  775. */
  776. /* Include ADC HAL Extension module */
  777. #include "stm32f1xx_hal_adc_ex.h"
  778. /* Exported functions --------------------------------------------------------*/
  779. /** @addtogroup ADC_Exported_Functions
  780. * @{
  781. */
  782. /** @addtogroup ADC_Exported_Functions_Group1
  783. * @{
  784. */
  785. /* Initialization and de-initialization functions **********************************/
  786. HAL_StatusTypeDef HAL_ADC_Init(ADC_HandleTypeDef* hadc);
  787. HAL_StatusTypeDef HAL_ADC_DeInit(ADC_HandleTypeDef *hadc);
  788. void HAL_ADC_MspInit(ADC_HandleTypeDef* hadc);
  789. void HAL_ADC_MspDeInit(ADC_HandleTypeDef* hadc);
  790. #if (USE_HAL_ADC_REGISTER_CALLBACKS == 1)
  791. /* Callbacks Register/UnRegister functions ***********************************/
  792. HAL_StatusTypeDef HAL_ADC_RegisterCallback(ADC_HandleTypeDef *hadc, HAL_ADC_CallbackIDTypeDef CallbackID, pADC_CallbackTypeDef pCallback);
  793. HAL_StatusTypeDef HAL_ADC_UnRegisterCallback(ADC_HandleTypeDef *hadc, HAL_ADC_CallbackIDTypeDef CallbackID);
  794. #endif /* USE_HAL_ADC_REGISTER_CALLBACKS */
  795. /**
  796. * @}
  797. */
  798. /* IO operation functions *****************************************************/
  799. /** @addtogroup ADC_Exported_Functions_Group2
  800. * @{
  801. */
  802. /* Blocking mode: Polling */
  803. HAL_StatusTypeDef HAL_ADC_Start(ADC_HandleTypeDef* hadc);
  804. HAL_StatusTypeDef HAL_ADC_Stop(ADC_HandleTypeDef* hadc);
  805. HAL_StatusTypeDef HAL_ADC_PollForConversion(ADC_HandleTypeDef* hadc, uint32_t Timeout);
  806. HAL_StatusTypeDef HAL_ADC_PollForEvent(ADC_HandleTypeDef* hadc, uint32_t EventType, uint32_t Timeout);
  807. /* Non-blocking mode: Interruption */
  808. HAL_StatusTypeDef HAL_ADC_Start_IT(ADC_HandleTypeDef* hadc);
  809. HAL_StatusTypeDef HAL_ADC_Stop_IT(ADC_HandleTypeDef* hadc);
  810. /* Non-blocking mode: DMA */
  811. HAL_StatusTypeDef HAL_ADC_Start_DMA(ADC_HandleTypeDef* hadc, uint32_t* pData, uint32_t Length);
  812. HAL_StatusTypeDef HAL_ADC_Stop_DMA(ADC_HandleTypeDef* hadc);
  813. /* ADC retrieve conversion value intended to be used with polling or interruption */
  814. uint32_t HAL_ADC_GetValue(ADC_HandleTypeDef* hadc);
  815. /* ADC IRQHandler and Callbacks used in non-blocking modes (Interruption and DMA) */
  816. void HAL_ADC_IRQHandler(ADC_HandleTypeDef* hadc);
  817. void HAL_ADC_ConvCpltCallback(ADC_HandleTypeDef* hadc);
  818. void HAL_ADC_ConvHalfCpltCallback(ADC_HandleTypeDef* hadc);
  819. void HAL_ADC_LevelOutOfWindowCallback(ADC_HandleTypeDef* hadc);
  820. void HAL_ADC_ErrorCallback(ADC_HandleTypeDef *hadc);
  821. /**
  822. * @}
  823. */
  824. /* Peripheral Control functions ***********************************************/
  825. /** @addtogroup ADC_Exported_Functions_Group3
  826. * @{
  827. */
  828. HAL_StatusTypeDef HAL_ADC_ConfigChannel(ADC_HandleTypeDef* hadc, ADC_ChannelConfTypeDef* sConfig);
  829. HAL_StatusTypeDef HAL_ADC_AnalogWDGConfig(ADC_HandleTypeDef* hadc, ADC_AnalogWDGConfTypeDef* AnalogWDGConfig);
  830. /**
  831. * @}
  832. */
  833. /* Peripheral State functions *************************************************/
  834. /** @addtogroup ADC_Exported_Functions_Group4
  835. * @{
  836. */
  837. uint32_t HAL_ADC_GetState(ADC_HandleTypeDef* hadc);
  838. uint32_t HAL_ADC_GetError(ADC_HandleTypeDef *hadc);
  839. /**
  840. * @}
  841. */
  842. /**
  843. * @}
  844. */
  845. /* Internal HAL driver functions **********************************************/
  846. /** @addtogroup ADC_Private_Functions
  847. * @{
  848. */
  849. HAL_StatusTypeDef ADC_Enable(ADC_HandleTypeDef* hadc);
  850. HAL_StatusTypeDef ADC_ConversionStop_Disable(ADC_HandleTypeDef* hadc);
  851. void ADC_StabilizationTime(uint32_t DelayUs);
  852. void ADC_DMAConvCplt(DMA_HandleTypeDef *hdma);
  853. void ADC_DMAHalfConvCplt(DMA_HandleTypeDef *hdma);
  854. void ADC_DMAError(DMA_HandleTypeDef *hdma);
  855. /**
  856. * @}
  857. */
  858. /**
  859. * @}
  860. */
  861. /**
  862. * @}
  863. */
  864. #ifdef __cplusplus
  865. }
  866. #endif
  867. #endif /* __STM32F1xx_HAL_ADC_H */