py32f002b_hal_adc.h 57 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179
  1. /**
  2. ******************************************************************************
  3. * @file py32f002b_hal_adc.h
  4. * @author MCU Application Team
  5. * @brief Header file containing functions prototypes of ADC HAL library.
  6. ******************************************************************************
  7. * @attention
  8. *
  9. * <h2><center>&copy; Copyright (c) 2023 Puya Semiconductor Co.
  10. * All rights reserved.</center></h2>
  11. *
  12. * This software component is licensed by Puya under BSD 3-Clause license,
  13. * the "License"; You may not use this file except in compliance with the
  14. * License. You may obtain a copy of the License at:
  15. * opensource.org/licenses/BSD-3-Clause
  16. *
  17. ******************************************************************************
  18. * @attention
  19. *
  20. * <h2><center>&copy; Copyright (c) 2016 STMicroelectronics.
  21. * All rights reserved.</center></h2>
  22. *
  23. * This software component is licensed by ST under BSD 3-Clause license,
  24. * the "License"; You may not use this file except in compliance with the
  25. * License. You may obtain a copy of the License at:
  26. * opensource.org/licenses/BSD-3-Clause
  27. *
  28. ******************************************************************************
  29. */
  30. /* Define to prevent recursive inclusion -------------------------------------*/
  31. #ifndef __PY32F002B_HAL_ADC_H
  32. #define __PY32F002B_HAL_ADC_H
  33. #ifdef __cplusplus
  34. extern "C" {
  35. #endif
  36. /* Includes ------------------------------------------------------------------*/
  37. #include "py32f002b_hal_def.h"
  38. /** @addtogroup PY32F002B_HAL_Driver
  39. * @{
  40. */
  41. /** @addtogroup ADC
  42. * @{
  43. */
  44. /* Exported types ------------------------------------------------------------*/
  45. /** @defgroup ADC_Exported_Types ADC Exported Types
  46. * @{
  47. */
  48. /**
  49. * @brief Structure definition of ADC initialization and regular group
  50. * @note The setting of these parameters with function HAL_ADC_Init() is conditioned to ADC state.
  51. * ADC state can be either:
  52. * - For all parameters: ADC disabled (this is the only possible ADC state to modify parameter 'ClockPrescaler')
  53. * - For all parameters except 'ClockPrescaler' and 'resolution': ADC enabled without conversion on going on regular group.
  54. * If ADC is not in the appropriate state to modify some parameters, these parameters setting is bypassed
  55. * without error reporting (as it can be the expected behaviour in case of intended action to update another parameter (which fulfills the ADC state condition) on the fly).
  56. */
  57. typedef struct
  58. {
  59. uint32_t ClockPrescaler; /*!< Select ADC clock source (synchronous clock derived from APB clock or asynchronous clock derived from ADC dedicated HSI RC oscillator 14MHz) and clock prescaler.
  60. This parameter can be a value of @ref ADC_ClockPrescaler
  61. Note: In case of usage of the ADC dedicated HSI RC oscillator, it must be preliminarily enabled at RCC top level.
  62. Note: This parameter can be modified only if the ADC is disabled */
  63. uint32_t Resolution; /*!< Configures the ADC resolution.
  64. This parameter can be a value of @ref ADC_Resolution */
  65. uint32_t DataAlign; /*!< Specifies whether the ADC data alignment is left or right.
  66. This parameter can be a value of @ref ADC_Data_align */
  67. uint32_t ScanConvMode; /*!< Configures the sequencer of regular group.
  68. This parameter can be associated to parameter 'DiscontinuousConvMode' to have main sequence subdivided in successive parts.
  69. If only 1 channel is set: Conversion is performed in single mode.
  70. If several channels are set: Conversions are performed in sequence mode (ranks defined by each channel number: channel 0 fixed on rank 0, channel 1 fixed on rank1, ...).
  71. Scan direction can be set to forward (from channel 0 to channel 12) or backward (from channel 18 to channel 0).
  72. This parameter can be a value of @ref ADC_Scan_mode */
  73. uint32_t EOCSelection; /*!< Specifies what EOC (End Of Conversion) flag is used for conversion by polling and interruption: end of conversion of each rank or complete sequence.
  74. This parameter can be a value of @ref ADC_EOCSelection. */
  75. FunctionalState LowPowerAutoWait; /*!< Selects the dynamic low power Auto Delay: new conversion start only when the previous
  76. conversion (for regular group) has been treated by user software, using function HAL_ADC_GetValue().
  77. This feature automatically adapts the ADC conversions trigs to the speed of the system that reads the data. Moreover, this avoids risk of overrun for low frequency applications.
  78. This parameter can be set to ENABLE or DISABLE.
  79. Note: Do not use with interruption or DMA (HAL_ADC_Start_IT(), HAL_ADC_Start_DMA()) since they have to clear immediately the EOC flag to free the IRQ vector sequencer.
  80. Do use with polling: 1. Start conversion with HAL_ADC_Start(), 2. Later on, when conversion data is needed: use HAL_ADC_PollForConversion() to ensure that conversion is completed
  81. and use HAL_ADC_GetValue() to retrieve conversion result and trig another conversion. */
  82. FunctionalState ContinuousConvMode; /*!< Specifies whether the conversion is performed in single mode (one conversion) or continuous mode for regular group,
  83. after the selected trigger occurred (software start or external trigger).
  84. This parameter can be set to ENABLE or DISABLE. */
  85. FunctionalState DiscontinuousConvMode; /*!< Specifies whether the conversions sequence of regular group is performed in Complete-sequence/Discontinuous-sequence (main sequence subdivided in successive parts).
  86. Discontinuous mode is used only if sequencer is enabled (parameter 'ScanConvMode'). If sequencer is disabled, this parameter is discarded.
  87. Discontinuous mode can be enabled only if continuous mode is disabled. If continuous mode is enabled, this parameter setting is discarded.
  88. This parameter can be set to ENABLE or DISABLE
  89. Note: Number of discontinuous ranks increment is fixed to one-by-one. */
  90. uint32_t ExternalTrigConv; /*!< Selects the external event used to trigger the conversion start of regular group.
  91. If set to ADC_SOFTWARE_START, external triggers are disabled.
  92. This parameter can be a value of @ref ADC_External_trigger_source_Regular */
  93. uint32_t ExternalTrigConvEdge; /*!< Selects the external trigger edge of regular group.
  94. If trigger is set to ADC_SOFTWARE_START, this parameter is discarded.
  95. This parameter can be a value of @ref ADC_External_trigger_edge_Regular */
  96. #if (defined(DMA) || defined(DMA1))
  97. FunctionalState DMAContinuousRequests; /*!< Specifies whether the DMA requests are performed in one shot mode (DMA transfer stop when number of conversions is reached)
  98. or in Continuous mode (DMA transfer unlimited, whatever number of conversions).
  99. Note: In continuous mode, DMA must be configured in circular mode. Otherwise an overrun will be triggered when DMA buffer maximum pointer is reached.
  100. This parameter can be set to ENABLE or DISABLE. */
  101. #endif
  102. uint32_t Overrun; /*!< Select the behaviour in case of overrun: data preserved or overwritten
  103. This parameter has an effect on regular group only, including in DMA mode.
  104. This parameter can be a value of @ref ADC_Overrun */
  105. uint32_t SamplingTimeCommon; /*!< Sampling time value to be set for the selected channel.
  106. Unit: ADC clock cycles
  107. Conversion time is the addition of sampling time and processing time (12.5 ADC clock cycles at ADC resolution 12 bits, 10.5 cycles at 10 bits, 8.5 cycles at 8 bits, 6.5 cycles at 6 bits).
  108. This parameter can be a value of @ref ADC_sampling_times
  109. Note: In case of usage of internal measurement channels (VrefInt/TempSensor),
  110. sampling time constraints must be respected (sampling time can be adjusted in function of ADC clock frequency and sampling time setting)
  111. Refer to device datasheet for timings values, parameters TS_vrefint, TS_temp (values rough order: 5us to 17us). */
  112. } ADC_InitTypeDef;
  113. /**
  114. * @brief Structure definition of ADC channel for regular group
  115. * @note The setting of these parameters with function HAL_ADC_ConfigChannel() is conditioned to ADC state.
  116. * ADC state can be either:
  117. * - For all parameters: ADC disabled or enabled without conversion on going on regular group.
  118. * If ADC is not in the appropriate state to modify some parameters, these parameters setting is bypassed
  119. * without error reporting (as it can be the expected behaviour in case of intended action to update another parameter (which fulfills the ADC state condition) on the fly).
  120. */
  121. typedef struct
  122. {
  123. uint32_t Channel; /*!< Specifies the channel to configure into ADC regular group.
  124. This parameter can be a value of @ref ADC_channels
  125. Note: Depending on devices, some channels may not be available on package pins. Refer to device datasheet for channels availability. */
  126. uint32_t Rank; /*!< Add or remove the channel from ADC regular group sequencer.
  127. Despite the channel rank is fixed, this parameter allow an additional possibility: to remove the selected rank (selected channel) from sequencer.
  128. This parameter can be a value of @ref ADC_rank */
  129. uint32_t SamplingTime; /*!< Sampling time value to be set for the selected channel.
  130. Unit: ADC clock cycles
  131. Conversion time is the addition of sampling time and processing time (12.5 ADC clock cycles at ADC resolution 12 bits, 10.5 cycles at 10 bits, 8.5 cycles at 8 bits, 6.5 cycles at 6 bits).
  132. This parameter can be a value of @ref ADC_sampling_times
  133. Caution: this setting impacts the entire regular group. Therefore, call of HAL_ADC_ConfigChannel() to configure a channel can impact the configuration of other channels previously set.
  134. Caution: Obsolete parameter. Use parameter "SamplingTimeCommon" in ADC initialization structure.
  135. If parameter "SamplingTimeCommon" is set to a valid sampling time, parameter "SamplingTime" is discarded.
  136. Note: In case of usage of internal measurement channels (VrefInt/TempSensor),
  137. sampling time constraints must be respected (sampling time can be adjusted in function of ADC clock frequency and sampling time setting)
  138. Refer to device datasheet for timings values, parameters TS_vrefint, TS_temp (values rough order: 5us to 17us). */
  139. } ADC_ChannelConfTypeDef;
  140. /**
  141. * @brief Structure definition of ADC analog watchdog
  142. * @note The setting of these parameters with function HAL_ADC_AnalogWDGConfig() is conditioned to ADC state.
  143. * ADC state can be either: ADC disabled or ADC enabled without conversion on going on regular group.
  144. */
  145. typedef struct
  146. {
  147. uint32_t WatchdogMode; /*!< Configures the ADC analog watchdog mode: single/all/none channels.
  148. This parameter can be a value of @ref ADC_analog_watchdog_mode. */
  149. uint32_t Channel; /*!< Selects which ADC channel to monitor by analog watchdog.
  150. This parameter has an effect only if parameter 'WatchdogMode' is configured on single channel. Only 1 channel can be monitored.
  151. This parameter can be a value of @ref ADC_channels. */
  152. FunctionalState ITMode; /*!< Specifies whether the analog watchdog is configured in interrupt or polling mode.
  153. This parameter can be set to ENABLE or DISABLE */
  154. uint32_t HighThreshold; /*!< Configures the ADC analog watchdog High threshold value.
  155. Depending of ADC resolution selected (12, 10, 8 or 6 bits), this parameter must be a number between Min_Data = 0x000 and Max_Data = 0xFFF, 0x3FF, 0xFF or 0x3F respectively. */
  156. uint32_t LowThreshold; /*!< Configures the ADC analog watchdog High threshold value.
  157. Depending of ADC resolution selected (12, 10, 8 or 6 bits), this parameter must be a number between Min_Data = 0x000 and Max_Data = 0xFFF, 0x3FF, 0xFF or 0x3F respectively. */
  158. } ADC_AnalogWDGConfTypeDef;
  159. /**
  160. * @brief HAL ADC state machine: ADC states definition (bitfields)
  161. * @note ADC state machine is managed by bitfields, state must be compared
  162. * with bit by bit.
  163. * For example:
  164. * " if (HAL_IS_BIT_SET(HAL_ADC_GetState(hadc1), HAL_ADC_STATE_REG_BUSY)) "
  165. * " if (HAL_IS_BIT_SET(HAL_ADC_GetState(hadc1), HAL_ADC_STATE_AWD1) ) "
  166. */
  167. /* States of ADC global scope */
  168. #define HAL_ADC_STATE_RESET (0x00000000U) /*!< ADC not yet initialized or disabled */
  169. #define HAL_ADC_STATE_READY (0x00000001U) /*!< ADC peripheral ready for use */
  170. #define HAL_ADC_STATE_BUSY_INTERNAL (0x00000002U) /*!< ADC is busy to internal process (initialization, calibration) */
  171. #define HAL_ADC_STATE_TIMEOUT (0x00000004U) /*!< TimeOut occurrence */
  172. /* States of ADC errors */
  173. #define HAL_ADC_STATE_ERROR_INTERNAL (0x00000010U) /*!< Internal error occurrence */
  174. #define HAL_ADC_STATE_ERROR_CONFIG (0x00000020U) /*!< Configuration error occurrence */
  175. #define HAL_ADC_STATE_ERROR_DMA (0x00000040U) /*!< DMA error occurrence */
  176. /* States of ADC group regular */
  177. #define HAL_ADC_STATE_REG_BUSY (0x00000100U) /*!< A conversion on group regular is ongoing or can occur (either by continuous mode,
  178. external trigger, low power auto power-on, multimode ADC master control) */
  179. #define HAL_ADC_STATE_REG_EOC (0x00000200U) /*!< Conversion data available on group regular */
  180. #define HAL_ADC_STATE_REG_OVR (0x00000400U) /*!< Overrun occurrence */
  181. #define HAL_ADC_STATE_REG_EOSMP (0x00000800U)
  182. /* States of ADC group injected */
  183. #define HAL_ADC_STATE_INJ_BUSY (0x00001000U)
  184. #define HAL_ADC_STATE_INJ_EOC (0x00002000U)
  185. #define HAL_ADC_STATE_INJ_JQOVF (0x00004000U)
  186. /* States of ADC analog watchdogs */
  187. #define HAL_ADC_STATE_AWD1 (0x00010000U) /*!< Out-of-window occurrence of analog watchdog 1 */
  188. #define HAL_ADC_STATE_AWD2 (0x00020000U)
  189. #define HAL_ADC_STATE_AWD3 (0x00040000U)
  190. /* States of ADC multi-mode */
  191. #define HAL_ADC_STATE_MULTIMODE_SLAVE (0x00100000U)
  192. /* ADC Temperature Scale Value */
  193. #define HAL_ADC_TSCAL1 (*(uint32_t *)(0x1fff0114)) /*!< Temperature Scale1 */
  194. #define HAL_ADC_TSCAL2 (*(uint32_t *)(0x1fff0118)) /*!< Temperature Scale2 */
  195. /**
  196. * @brief ADC handle Structure definition
  197. */
  198. typedef struct __ADC_HandleTypeDef
  199. {
  200. ADC_TypeDef *Instance; /*!< Register base address */
  201. ADC_InitTypeDef Init; /*!< ADC required parameters */
  202. #if (defined(DMA) || defined(DMA1))
  203. DMA_HandleTypeDef *DMA_Handle; /*!< Pointer DMA Handler */
  204. #endif
  205. HAL_LockTypeDef Lock; /*!< ADC locking object */
  206. __IO uint32_t State; /*!< ADC communication state (bitmap of ADC states) */
  207. __IO uint32_t ErrorCode; /*!< ADC Error code */
  208. #if (USE_HAL_ADC_REGISTER_CALLBACKS == 1)
  209. void (* ConvCpltCallback)(struct __ADC_HandleTypeDef *hadc); /*!< ADC conversion complete callback */
  210. void (* ConvHalfCpltCallback)(struct __ADC_HandleTypeDef *hadc); /*!< ADC conversion DMA half-transfer callback */
  211. void (* LevelOutOfWindowCallback)(struct __ADC_HandleTypeDef *hadc); /*!< ADC analog watchdog 1 callback */
  212. void (* ErrorCallback)(struct __ADC_HandleTypeDef *hadc); /*!< ADC error callback */
  213. void (* MspInitCallback)(struct __ADC_HandleTypeDef *hadc); /*!< ADC Msp Init callback */
  214. void (* MspDeInitCallback)(struct __ADC_HandleTypeDef *hadc); /*!< ADC Msp DeInit callback */
  215. #endif /* USE_HAL_ADC_REGISTER_CALLBACKS */
  216. } ADC_HandleTypeDef;
  217. #if (USE_HAL_ADC_REGISTER_CALLBACKS == 1)
  218. /**
  219. * @brief HAL ADC Callback ID enumeration definition
  220. */
  221. typedef enum
  222. {
  223. HAL_ADC_CONVERSION_COMPLETE_CB_ID = 0x00U, /*!< ADC conversion complete callback ID */
  224. HAL_ADC_CONVERSION_HALF_CB_ID = 0x01U, /*!< ADC conversion DMA half-transfer callback ID */
  225. HAL_ADC_LEVEL_OUT_OF_WINDOW_1_CB_ID = 0x02U, /*!< ADC analog watchdog 1 callback ID */
  226. HAL_ADC_ERROR_CB_ID = 0x03U, /*!< ADC error callback ID */
  227. HAL_ADC_INJ_CONVERSION_COMPLETE_CB_ID = 0x04U, /*!< ADC group injected conversion complete callback ID */
  228. HAL_ADC_MSPINIT_CB_ID = 0x09U, /*!< ADC Msp Init callback ID */
  229. HAL_ADC_MSPDEINIT_CB_ID = 0x0AU /*!< ADC Msp DeInit callback ID */
  230. } HAL_ADC_CallbackIDTypeDef;
  231. /**
  232. * @brief HAL ADC Callback pointer definition
  233. */
  234. typedef void (*pADC_CallbackTypeDef)(ADC_HandleTypeDef *hadc); /*!< pointer to a ADC callback function */
  235. #endif /* USE_HAL_ADC_REGISTER_CALLBACKS */
  236. /**
  237. * @}
  238. */
  239. /* Exported constants --------------------------------------------------------*/
  240. /** @defgroup ADC_Exported_Constants ADC Exported Constants
  241. * @{
  242. */
  243. /** @defgroup ADC_Error_Code ADC Error Code
  244. * @{
  245. */
  246. #define HAL_ADC_ERROR_NONE (0x00U) /*!< No error */
  247. #define HAL_ADC_ERROR_INTERNAL (0x01U) /*!< ADC IP internal error: if problem of clocking,
  248. enable/disable, erroneous state */
  249. #define HAL_ADC_ERROR_OVR (0x02U) /*!< Overrun error */
  250. #if (defined(DMA) || defined(DMA1))
  251. #define HAL_ADC_ERROR_DMA (0x04U) /*!< DMA transfer error */
  252. #endif
  253. #if (USE_HAL_ADC_REGISTER_CALLBACKS == 1)
  254. #define HAL_ADC_ERROR_INVALID_CALLBACK (0x10U) /*!< Invalid Callback error */
  255. #endif /* USE_HAL_ADC_REGISTER_CALLBACKS */
  256. typedef enum
  257. {
  258. HAL_ADCCALIBOK = 0x00U,
  259. HAL_ADCCALIBERROR = 0x01U,
  260. HAL_ADCCALIBOFFFAIL = 0x02U,
  261. HAL_ADCCALIBCAPFAIL = 0x03U,
  262. } HAL_ADCCalibStatusTypeDef;
  263. /**
  264. * @}
  265. */
  266. /** @defgroup ADC_ClockPrescaler ADC ClockPrescaler
  267. * @{
  268. */
  269. #define ADC_CLOCK_SYNC_PCLK_DIV1 (0x00000000U) /*!< ADC synchronous clock derived from APB clock divided by a prescaler of 1 */
  270. #define ADC_CLOCK_SYNC_PCLK_DIV2 ((uint32_t)ADC_CFGR2_CKMODE_0) /*!< ADC synchronous clock derived from APB clock divided by a prescaler of 2 */
  271. #define ADC_CLOCK_SYNC_PCLK_DIV4 ((uint32_t)ADC_CFGR2_CKMODE_1) /*!< ADC synchronous clock derived from APB clock divided by a prescaler of 4 */
  272. #define ADC_CLOCK_SYNC_PCLK_DIV8 ((uint32_t)(ADC_CFGR2_CKMODE_0 | ADC_CFGR2_CKMODE_1)) /*!< ADC synchronous clock derived from APB clock divided by a prescaler of 8 */
  273. #define ADC_CLOCK_SYNC_PCLK_DIV16 ((uint32_t)ADC_CFGR2_CKMODE_2) /*!< ADC synchronous clock derived from APB clock divided by a prescaler of 16 */
  274. #define ADC_CLOCK_SYNC_PCLK_DIV32 ((uint32_t)(ADC_CFGR2_CKMODE_0 | ADC_CFGR2_CKMODE_2)) /*!< ADC synchronous clock derived from APB clock divided by a prescaler of 32 */
  275. #define ADC_CLOCK_SYNC_PCLK_DIV64 ((uint32_t)(ADC_CFGR2_CKMODE_1 | ADC_CFGR2_CKMODE_2)) /*!< ADC synchronous clock derived from APB clock divided by a prescaler of 64 */
  276. #define ADC_CLOCK_ASYNC_HSI_DIV1 ((uint32_t)ADC_CFGR2_CKMODE_3) /*!< ADC asynchronous clock derived from ADC dedicated HSI */
  277. #define ADC_CLOCK_ASYNC_HSI_DIV2 ((uint32_t)(ADC_CFGR2_CKMODE_0 | ADC_CFGR2_CKMODE_3)) /*!< ADC asynchronous clock derived from HSI clock divided by a prescaler of 2 */
  278. #define ADC_CLOCK_ASYNC_HSI_DIV4 ((uint32_t)(ADC_CFGR2_CKMODE_1 | ADC_CFGR2_CKMODE_3)) /*!< ADC asynchronous clock derived from HSI clock divided by a prescaler of 4 */
  279. #define ADC_CLOCK_ASYNC_HSI_DIV8 ((uint32_t)(ADC_CFGR2_CKMODE_0 | ADC_CFGR2_CKMODE_1 | ADC_CFGR2_CKMODE_3)) /*!< ADC asynchronous clock derived from HSI clock divided by a prescaler of 8 */
  280. #define ADC_CLOCK_ASYNC_HSI_DIV16 ((uint32_t)(ADC_CFGR2_CKMODE_2 | ADC_CFGR2_CKMODE_3)) /*!< ADC asynchronous clock derived from HSI clock divided by a prescaler of 16 */
  281. #define ADC_CLOCK_ASYNC_HSI_DIV32 ((uint32_t)(ADC_CFGR2_CKMODE_0 | ADC_CFGR2_CKMODE_2 | ADC_CFGR2_CKMODE_3)) /*!< ADC asynchronous clock derived from HSI clock divided by a prescaler of 32 */
  282. #define ADC_CLOCK_ASYNC_HSI_DIV64 ((uint32_t)(ADC_CFGR2_CKMODE_1 | ADC_CFGR2_CKMODE_2 | ADC_CFGR2_CKMODE_3)) /*!< ADC asynchronous clock derived from HSI clock divided by a prescaler of 64 */
  283. /**
  284. * @}
  285. */
  286. /** @defgroup ADC_Resolution ADC Resolution
  287. * @{
  288. */
  289. #define ADC_RESOLUTION_12B (0x00000000U) /*!< ADC 12-bit resolution */
  290. #define ADC_RESOLUTION_10B ((uint32_t)ADC_CFGR1_RESSEL_0) /*!< ADC 10-bit resolution */
  291. #define ADC_RESOLUTION_8B ((uint32_t)ADC_CFGR1_RESSEL_1) /*!< ADC 8-bit resolution */
  292. #define ADC_RESOLUTION_6B ((uint32_t)ADC_CFGR1_RESSEL) /*!< ADC 6-bit resolution */
  293. /**
  294. * @}
  295. */
  296. /** @defgroup ADC_Data_align ADC Data_align
  297. * @{
  298. */
  299. #define ADC_DATAALIGN_RIGHT (0x00000000U)
  300. #define ADC_DATAALIGN_LEFT ((uint32_t)ADC_CFGR1_ALIGN)
  301. /**
  302. * @}
  303. */
  304. /** @defgroup ADC_Scan_mode ADC Scan mode
  305. * @{
  306. */
  307. #define ADC_SCAN_DIRECTION_FORWARD (0x00000001U) /*!< Scan direction forward: from channel 0 to channel 10 */
  308. #define ADC_SCAN_DIRECTION_BACKWARD (0x00000002U) /*!< Scan direction backward: from channel 10 to channel 0 */
  309. #define ADC_SCAN_ENABLE ADC_SCAN_DIRECTION_FORWARD
  310. /**
  311. * @}
  312. */
  313. /** @defgroup ADC_External_trigger_edge_Regular ADC External trigger edge Regular
  314. * @{
  315. */
  316. #define ADC_EXTERNALTRIGCONVEDGE_NONE (0x00000000U)
  317. #define ADC_EXTERNALTRIGCONVEDGE_RISING ((uint32_t)ADC_CFGR1_EXTEN_0)
  318. #define ADC_EXTERNALTRIGCONVEDGE_FALLING ((uint32_t)ADC_CFGR1_EXTEN_1)
  319. #define ADC_EXTERNALTRIGCONVEDGE_RISINGFALLING ((uint32_t)ADC_CFGR1_EXTEN)
  320. /**
  321. * @}
  322. */
  323. /** @defgroup ADC_EOCSelection ADC EOCSelection
  324. * @{
  325. */
  326. #define ADC_EOC_SINGLE_CONV ((uint32_t) ADC_ISR_EOC)
  327. #define ADC_EOC_SEQ_CONV ((uint32_t) ADC_ISR_EOSEQ)
  328. /**
  329. * @}
  330. */
  331. /** @defgroup ADC_Overrun ADC Overrun
  332. * @{
  333. */
  334. #define ADC_OVR_DATA_OVERWRITTEN (0x00000000U)
  335. #define ADC_OVR_DATA_PRESERVED (0x00000001U)
  336. /**
  337. * @}
  338. */
  339. /** @defgroup ADC_rank ADC rank
  340. * @{
  341. */
  342. #define ADC_RANK_CHANNEL_NUMBER (0x00001000U) /*!< Enable the rank of the selected channels. Number of ranks in the sequence is defined by number of channels enabled, rank of each channel is defined by channel number (channel 0 fixed on rank 0, channel 1 fixed on rank1, ...) */
  343. #define ADC_RANK_NONE (0x00001001U) /*!< Disable the selected rank (selected channel) from sequencer */
  344. /**
  345. * @}
  346. */
  347. /** @defgroup ADC_sampling_times ADC sampling times
  348. * @{
  349. */
  350. #define ADC_SAMPLETIME_3CYCLES_5 (0x10000000U) /*!< Sampling time 3.5 ADC clock cycles */
  351. #define ADC_SAMPLETIME_5CYCLES_5 ((uint32_t) ADC_SMPR_SMP_0) /*!< Sampling time 5.5 ADC clock cycles */
  352. #define ADC_SAMPLETIME_7CYCLES_5 ((uint32_t) ADC_SMPR_SMP_1) /*!< Sampling time 7.5 ADC clock cycles */
  353. #define ADC_SAMPLETIME_13CYCLES_5 ((uint32_t) (ADC_SMPR_SMP_1 | ADC_SMPR_SMP_0)) /*!< Sampling time 13.5 ADC clock cycles */
  354. #define ADC_SAMPLETIME_28CYCLES_5 ((uint32_t) ADC_SMPR_SMP_2) /*!< Sampling time 28.5 ADC clock cycles */
  355. #define ADC_SAMPLETIME_41CYCLES_5 ((uint32_t) (ADC_SMPR_SMP_2 | ADC_SMPR_SMP_0)) /*!< Sampling time 41.5 ADC clock cycles */
  356. #define ADC_SAMPLETIME_71CYCLES_5 ((uint32_t)(ADC_SMPR_SMP_2 | ADC_SMPR_SMP_1)) /*!< Sampling time 71.5 ADC clock cycles */
  357. #define ADC_SAMPLETIME_239CYCLES_5 ((uint32_t) ADC_SMPR_SMP) /*!< Sampling time 239.5 ADC clock cycles */
  358. /**
  359. * @}
  360. */
  361. /** @defgroup ADC_VrefBuf ADC VrefBuf
  362. * @{
  363. */
  364. #define ADC_VREFBUF_VCCA (0x00000000U) /*!< VREFBUF VCCA */
  365. #define ADC_VREFBUF_1P5V ((uint32_t)(ADC_CR_VREF_BUFFERE)) /*!< VREFBUF 1.5V */
  366. /**
  367. * @}
  368. */
  369. /** @defgroup ADC_analog_watchdog_mode ADC analog watchdog mode
  370. * @{
  371. */
  372. #define ADC_ANALOGWATCHDOG_NONE ( 0x00000000U)
  373. #define ADC_ANALOGWATCHDOG_SINGLE_REG ((uint32_t)(ADC_CFGR1_AWDSGL | ADC_CFGR1_AWDEN))
  374. #define ADC_ANALOGWATCHDOG_ALL_REG ((uint32_t) ADC_CFGR1_AWDEN)
  375. /**
  376. * @}
  377. */
  378. /** @defgroup ADC_Event_type ADC Event type
  379. * @{
  380. */
  381. #define ADC_AWD_EVENT ((uint32_t)ADC_FLAG_AWD) /*!< ADC Analog watchdog 1 event */
  382. #define ADC_OVR_EVENT ((uint32_t)ADC_FLAG_OVR) /*!< ADC overrun event */
  383. /**
  384. * @}
  385. */
  386. /** @defgroup ADC_interrupts_definition ADC interrupts definition
  387. * @{
  388. */
  389. #define ADC_IT_AWD ADC_IER_AWDIE /*!< ADC Analog watchdog interrupt source */
  390. #define ADC_IT_OVR ADC_IER_OVRIE /*!< ADC overrun interrupt source */
  391. #define ADC_IT_EOS ADC_IER_EOSEQIE /*!< ADC End of Regular sequence of Conversions interrupt source */
  392. #define ADC_IT_EOC ADC_IER_EOCIE /*!< ADC End of Regular Conversion interrupt source */
  393. #define ADC_IT_EOSMP ADC_IER_EOSMPIE /*!< ADC End of Sampling interrupt source */
  394. /**
  395. * @}
  396. */
  397. /** @defgroup ADC_flags_definition ADC flags definition
  398. * @{
  399. */
  400. #define ADC_FLAG_AWD ADC_ISR_AWD /*!< ADC Analog watchdog flag */
  401. #define ADC_FLAG_OVR ADC_ISR_OVR /*!< ADC overrun flag */
  402. #define ADC_FLAG_EOS ADC_ISR_EOSEQ /*!< ADC End of Regular sequence of Conversions flag */
  403. #define ADC_FLAG_EOC ADC_ISR_EOC /*!< ADC End of Regular Conversion flag */
  404. #define ADC_FLAG_EOSMP ADC_ISR_EOSMP /*!< ADC End of Sampling flag */
  405. /**
  406. * @}
  407. */
  408. /** @defgroup ADC_Calibration_sampling_times ADC Calibration sampling times
  409. * @{
  410. */
  411. #define ADC_CALIBSAMPLETIME_1CYCLE (0x00000000U) /*!< Calibration Sampling time 1 ADC clock cycle */
  412. #define ADC_CALIBSAMPLETIME_2CYCLES ((uint32_t) ADC_CCSR_CALSMP_0) /*!< Calibration Sampling time 2 ADC clock cycles */
  413. #define ADC_CALIBSAMPLETIME_4CYCLES ((uint32_t) ADC_CCSR_CALSMP_1) /*!< Calibration Sampling time 4 ADC clock cycles */
  414. #define ADC_CALIBSAMPLETIME_8CYCLES ((uint32_t) (ADC_CCSR_CALSMP_1 | ADC_CCSR_CALSMP_0)) /*!< Calibration Sampling time 8 ADC clock cycles */
  415. /**
  416. * @}
  417. */
  418. /** @defgroup ADC_Calibration_Selection ADC Contents Selection
  419. * @{
  420. */
  421. #define ADC_CALIBSELECTION_ONLYOFFSET (0x00000000U) /*!< Calibration Selection Only Offset*/
  422. #define ADC_CALIBSELECTION_OFFSET_CAPACITANCE ((uint32_t) ADC_CCSR_CALSEL) /*!< Calibration Selection Offset and CAPACITANCE */
  423. /**
  424. * @}
  425. */
  426. /**
  427. * @}
  428. */
  429. /* Private constants ---------------------------------------------------------*/
  430. /** @addtogroup ADC_Private_Constants ADC Private Constants
  431. * @{
  432. */
  433. /** @defgroup ADC_Internal_HAL_driver_Ext_trig_src_Regular ADC Internal HAL driver Ext trig src Regular
  434. * @{
  435. */
  436. /* List of external triggers of regular group for ADC1: */
  437. /* (used internally by HAL driver. To not use into HAL structure parameters) */
  438. #define ADC1_2_EXTERNALTRIG_T1_TRGO (0x00000000U)
  439. #define ADC1_2_EXTERNALTRIG_T1_CC4 ((uint32_t)ADC_CFGR1_EXTSEL_0)
  440. /**
  441. * @}
  442. */
  443. /* Combination of all post-conversion flags bits: EOC/EOS, OVR, AWD */
  444. #define ADC_FLAG_POSTCONV_ALL (ADC_FLAG_AWD | ADC_FLAG_OVR | ADC_FLAG_EOS | ADC_FLAG_EOC)
  445. /**
  446. * @}
  447. */
  448. /** @defgroup ADC_Exported_Constants ADC Exported Constants
  449. * @{
  450. */
  451. #define ADC_CCR_ALL (ADC_CCR_TSEN | ADC_CCR_VREFEN)
  452. /** @defgroup ADC_channels ADC channels
  453. * @{
  454. */
  455. /* Note: Depending on devices, some channels may not be available on package */
  456. /* pins. Refer to device datasheet for channels availability. */
  457. /* Note: Channels are used by bitfields for setting of channel selection */
  458. /* (register ADC_CHSELR) and used by number for setting of analog */
  459. /* watchdog channel (bits AWDCH in register ADC_CFGR1). */
  460. /* Channels are defined with decimal numbers and converted them to */
  461. /* bitfields when needed. */
  462. #define ADC_CHANNEL_0 (0x00000000U)
  463. #define ADC_CHANNEL_1 (0x00000001U)
  464. #define ADC_CHANNEL_2 (0x00000002U)
  465. #define ADC_CHANNEL_3 (0x00000003U)
  466. #define ADC_CHANNEL_4 (0x00000004U)
  467. #define ADC_CHANNEL_5 (0x00000005U)
  468. #define ADC_CHANNEL_6 (0x00000006U)
  469. #define ADC_CHANNEL_7 (0x00000007U)
  470. #define ADC_CHANNEL_8 (0x00000008U)
  471. #define ADC_CHANNEL_9 (0x00000009U)
  472. #if defined(ADC_CHSELR_CHSEL10)
  473. #define ADC_CHANNEL_10 (0x0000000AU)
  474. #endif
  475. #define ADC_CHANNEL_TEMPSENSOR ADC_CHANNEL_8
  476. #define ADC_CHANNEL_VREFINT ADC_CHANNEL_9
  477. #if defined(ADC_CHSELR_CHSEL10)
  478. #define ADC_CHANNEL_VCCA ADC_CHANNEL_10
  479. #endif
  480. /**
  481. * @}
  482. */
  483. /** @defgroup ADC_External_trigger_source_Regular ADC External trigger source Regular
  484. * @{
  485. */
  486. /* List of external triggers with generic trigger name, sorted by trigger */
  487. /* name: */
  488. /* External triggers of regular group for ADC1 */
  489. #define ADC_EXTERNALTRIGCONV_T1_TRGO ADC1_2_EXTERNALTRIG_T1_TRGO
  490. #define ADC_EXTERNALTRIGCONV_T1_CC4 ADC1_2_EXTERNALTRIG_T1_CC4
  491. #define ADC_SOFTWARE_START (ADC_CFGR1_EXTSEL + 1U)
  492. /**
  493. * @}
  494. */
  495. /**
  496. * @}
  497. */
  498. /* Exported macro ------------------------------------------------------------*/
  499. /** @defgroup ADC_Exported_Macros ADC Exported Macros
  500. * @{
  501. */
  502. /* Macro for internal HAL driver usage, and possibly can be used into code of */
  503. /* final user. */
  504. /**
  505. * @brief Enable the ADC peripheral
  506. * @param __HANDLE__ ADC handle
  507. * @retval None
  508. */
  509. #define __HAL_ADC_ENABLE(__HANDLE__) \
  510. ((__HANDLE__)->Instance->CR |= ADC_CR_ADEN)
  511. /**
  512. * @brief Disable the ADC peripheral
  513. * @param __HANDLE__ ADC handle
  514. * @retval None
  515. */
  516. #define __HAL_ADC_DISABLE(__HANDLE__) \
  517. do{ \
  518. (__HANDLE__)->Instance->CR |= ADC_CR_ADDIS; \
  519. __HAL_ADC_CLEAR_FLAG((__HANDLE__), (ADC_FLAG_EOSMP)); \
  520. } while(0)
  521. /**
  522. * @brief Enable the ADC end of conversion interrupt.
  523. * @param __HANDLE__ ADC handle
  524. * @param __INTERRUPT__ ADC Interrupt
  525. * This parameter can be any combination of the following values:
  526. * @arg ADC_IT_EOC: ADC End of Regular Conversion interrupt source
  527. * @arg ADC_IT_EOS: ADC End of Regular sequence of Conversions interrupt source
  528. * @arg ADC_IT_AWD: ADC Analog watchdog interrupt source
  529. * @arg ADC_IT_OVR: ADC overrun interrupt source
  530. * @arg ADC_IT_EOSMP: ADC End of Sampling interrupt source
  531. * @retval None
  532. */
  533. #define __HAL_ADC_ENABLE_IT(__HANDLE__, __INTERRUPT__) \
  534. (((__HANDLE__)->Instance->IER) |= (__INTERRUPT__))
  535. /**
  536. * @brief Disable the ADC end of conversion interrupt.
  537. * @param __HANDLE__ ADC handle
  538. * @param __INTERRUPT__ ADC Interrupt
  539. * This parameter can be any combination of the following values:
  540. * @arg ADC_IT_EOC: ADC End of Regular Conversion interrupt source
  541. * @arg ADC_IT_EOS: ADC End of Regular sequence of Conversions interrupt source
  542. * @arg ADC_IT_AWD: ADC Analog watchdog interrupt source
  543. * @arg ADC_IT_OVR: ADC overrun interrupt source
  544. * @arg ADC_IT_EOSMP: ADC End of Sampling interrupt source
  545. * @retval None
  546. */
  547. #define __HAL_ADC_DISABLE_IT(__HANDLE__, __INTERRUPT__) \
  548. (((__HANDLE__)->Instance->IER) &= ~(__INTERRUPT__))
  549. /** @brief Checks if the specified ADC interrupt source is enabled or disabled.
  550. * @param __HANDLE__ ADC handle
  551. * @param __INTERRUPT__ ADC interrupt source to check
  552. * This parameter can be any combination of the following values:
  553. * @arg ADC_IT_EOC: ADC End of Regular Conversion interrupt source
  554. * @arg ADC_IT_EOS: ADC End of Regular sequence of Conversions interrupt source
  555. * @arg ADC_IT_AWD: ADC Analog watchdog interrupt source
  556. * @arg ADC_IT_OVR: ADC overrun interrupt source
  557. * @arg ADC_IT_EOSMP: ADC End of Sampling interrupt source
  558. * @retval State ofinterruption (SET or RESET)
  559. */
  560. #define __HAL_ADC_GET_IT_SOURCE(__HANDLE__, __INTERRUPT__) \
  561. (((__HANDLE__)->Instance->IER & (__INTERRUPT__)) == (__INTERRUPT__))
  562. /**
  563. * @brief Get the selected ADC's flag status.
  564. * @param __HANDLE__ ADC handle
  565. * @param __FLAG__ ADC flag
  566. * This parameter can be any combination of the following values:
  567. * @arg ADC_FLAG_EOC: ADC End of Regular conversion flag
  568. * @arg ADC_FLAG_EOS: ADC End of Regular sequence of Conversions flag
  569. * @arg ADC_FLAG_AWD: ADC Analog watchdog flag
  570. * @arg ADC_FLAG_OVR: ADC overrun flag
  571. * @arg ADC_FLAG_EOSMP: ADC End of Sampling flag
  572. * @retval None
  573. */
  574. #define __HAL_ADC_GET_FLAG(__HANDLE__, __FLAG__) \
  575. ((((__HANDLE__)->Instance->ISR) & (__FLAG__)) == (__FLAG__))
  576. /**
  577. * @brief Clear the ADC's pending flags
  578. * @param __HANDLE__ ADC handle
  579. * @param __FLAG__ ADC flag
  580. * This parameter can be any combination of the following values:
  581. * @arg ADC_FLAG_EOC: ADC End of Regular conversion flag
  582. * @arg ADC_FLAG_EOS: ADC End of Regular sequence of Conversions flag
  583. * @arg ADC_FLAG_AWD: ADC Analog watchdog flag
  584. * @arg ADC_FLAG_OVR: ADC overrun flag
  585. * @arg ADC_FLAG_EOSMP: ADC End of Sampling flag
  586. * @retval None
  587. */
  588. /* Note: bit cleared bit by writing 1 (writing 0 has no effect on any bit of register ISR) */
  589. #define __HAL_ADC_CLEAR_FLAG(__HANDLE__, __FLAG__) \
  590. (((__HANDLE__)->Instance->ISR) = (__FLAG__))
  591. /** @brief Reset ADC handle state
  592. * @param __HANDLE__ ADC handle
  593. * @retval None
  594. */
  595. #if (USE_HAL_ADC_REGISTER_CALLBACKS == 1)
  596. #define __HAL_ADC_RESET_HANDLE_STATE(__HANDLE__) \
  597. do{ \
  598. (__HANDLE__)->State = HAL_ADC_STATE_RESET; \
  599. (__HANDLE__)->MspInitCallback = NULL; \
  600. (__HANDLE__)->MspDeInitCallback = NULL; \
  601. } while(0)
  602. #else
  603. #define __HAL_ADC_RESET_HANDLE_STATE(__HANDLE__) \
  604. ((__HANDLE__)->State = HAL_ADC_STATE_RESET)
  605. #endif
  606. /**
  607. * @}
  608. */
  609. /* Private macro -------------------------------------------------------------*/
  610. /** @defgroup ADC_Private_Macros ADC Private Macros
  611. * @{
  612. */
  613. /* Macro reserved for internal HAL driver usage, not intended to be used in */
  614. /* code of final user. */
  615. /**
  616. * @brief Verification of hardware constraints before ADC can be enabled
  617. * @param __HANDLE__ ADC handle
  618. * @retval SET (ADC can be enabled) or RESET (ADC cannot be enabled)
  619. */
  620. #define ADC_ENABLING_CONDITIONS(__HANDLE__) \
  621. (( ( ((__HANDLE__)->Instance->CR) & \
  622. (ADC_CR_ADCAL | ADC_CR_ADSTP | ADC_CR_ADSTART | ADC_CR_ADDIS| ADC_CR_ADEN) \
  623. ) == RESET \
  624. ) ? SET : RESET)
  625. /**
  626. * @brief Verification of hardware constraints before ADC can be disabled
  627. * @param __HANDLE__ ADC handle
  628. * @retval SET (ADC can be disabled) or RESET (ADC cannot be disabled)
  629. */
  630. #define ADC_DISABLING_CONDITIONS(__HANDLE__) \
  631. (( ( ((__HANDLE__)->Instance->CR) & \
  632. (ADC_CR_ADSTART | ADC_CR_ADEN)) == ADC_CR_ADEN \
  633. ) ? SET : RESET)
  634. /**
  635. * @brief Verification of ADC state: enabled or disabled
  636. * @param __HANDLE__ ADC handle
  637. * @retval SET (ADC enabled) or RESET (ADC disabled)
  638. */
  639. #define ADC_IS_ENABLE(__HANDLE__) ((( ((((__HANDLE__)->Instance->CR) & (ADC_CR_ADEN)) == ADC_CR_ADEN) ) ) ? SET : RESET)
  640. /**
  641. * @brief Test if conversion trigger of regular group is software start
  642. * or external trigger.
  643. * @param __HANDLE__ ADC handle
  644. * @retval SET (software start) or RESET (external trigger)
  645. */
  646. #define ADC_IS_SOFTWARE_START_REGULAR(__HANDLE__) \
  647. (((__HANDLE__)->Instance->CFGR1 & ADC_CFGR1_EXTEN) == RESET)
  648. /**
  649. * @brief Check if no conversion on going on regular group
  650. * @param __HANDLE__ ADC handle
  651. * @retval SET (conversion is on going) or RESET (no conversion is on going)
  652. */
  653. #define ADC_IS_CONVERSION_ONGOING_REGULAR(__HANDLE__) \
  654. (( (((__HANDLE__)->Instance->CR) & ADC_CR_ADSTART) == RESET \
  655. ) ? RESET : SET)
  656. /**
  657. * @brief Returns resolution bits in CFGR1 register: RES[1:0].
  658. * Returned value is among parameters to @ref ADC_Resolution.
  659. * @param __HANDLE__ ADC handle
  660. * @retval None
  661. */
  662. #define ADC_GET_RESOLUTION(__HANDLE__) \
  663. (((__HANDLE__)->Instance->CFGR1) & ADC_CFGR1_RESSEL)
  664. /**
  665. * @brief Returns ADC sample time bits in SMPR register: SMP[2:0].
  666. * Returned value is among parameters to @ref ADC_Resolution.
  667. * @param __HANDLE__ ADC handle
  668. * @retval None
  669. */
  670. #define ADC_GET_SAMPLINGTIME(__HANDLE__) \
  671. (((__HANDLE__)->Instance->SMPR) & ADC_SMPR_SMP)
  672. /**
  673. * @brief Simultaneously clears and sets specific bits of the handle State
  674. * @note: ADC_STATE_CLR_SET() macro is merely aliased to generic macro MODIFY_REG(),
  675. * the first parameter is the ADC handle State, the second parameter is the
  676. * bit field to clear, the third and last parameter is the bit field to set.
  677. * @retval None
  678. */
  679. #define ADC_STATE_CLR_SET MODIFY_REG
  680. /**
  681. * @brief Clear ADC error code (set it to error code: "no error")
  682. * @param __HANDLE__ ADC handle
  683. * @retval None
  684. */
  685. #define ADC_CLEAR_ERRORCODE(__HANDLE__) \
  686. ((__HANDLE__)->ErrorCode = HAL_ADC_ERROR_NONE)
  687. /**
  688. * @brief Configure the channel number into channel selection register
  689. * @param _CHANNEL_ ADC Channel
  690. * @retval None
  691. */
  692. #define ADC_CHSELR_CHANNEL(_CHANNEL_) \
  693. ( 1U << (_CHANNEL_))
  694. /**
  695. * @brief Set the ADC's sample time
  696. * @param _SAMPLETIME_ Sample time parameter.
  697. * @retval None
  698. */
  699. #define ADC_SMPR_SET(_SAMPLETIME_) \
  700. ((_SAMPLETIME_) & (ADC_SMPR_SMP))
  701. /**
  702. * @brief Set the Analog Watchdog 1 channel.
  703. * @param _CHANNEL_ channel to be monitored by Analog Watchdog 1.
  704. * @retval None
  705. */
  706. #define ADC_CFGR_AWDCH(_CHANNEL_) \
  707. ((_CHANNEL_-1) << 26U)
  708. /**
  709. * @brief Enable ADC discontinuous conversion mode for regular group
  710. * @param _REG_DISCONTINUOUS_MODE_ Regular discontinuous mode.
  711. * @retval None
  712. */
  713. #define ADC_CFGR1_REG_DISCCONTINUOUS(_REG_DISCONTINUOUS_MODE_) \
  714. ((_REG_DISCONTINUOUS_MODE_) << 16U)
  715. /**
  716. * @brief Enable the ADC auto delay mode.
  717. * @param _AUTOWAIT_ Auto delay bit enable or disable.
  718. * @retval None
  719. */
  720. #define ADC_CFGR1_AUTOWAIT(_AUTOWAIT_) \
  721. ((_AUTOWAIT_) << 14U)
  722. /**
  723. * @brief Enable ADC continuous conversion mode.
  724. * @param _CONTINUOUS_MODE_ Continuous mode.
  725. * @retval None
  726. */
  727. #define ADC_CFGR1_CONTINUOUS(_CONTINUOUS_MODE_) \
  728. ((_CONTINUOUS_MODE_) << 13U)
  729. /**
  730. * @brief Enable ADC overrun mode.
  731. * @param _OVERRUN_MODE_ Overrun mode.
  732. * @retval Overun bit setting to be programmed into CFGR register
  733. */
  734. #define ADC_CFGR1_OVERRUN(_OVERRUN_MODE_) \
  735. ( ( (_OVERRUN_MODE_) != (ADC_OVR_DATA_PRESERVED) \
  736. )? (ADC_CFGR1_OVRMOD) : (0x00000000) \
  737. )
  738. /**
  739. * @brief Enable ADC scan mode to convert multiple ranks with sequencer.
  740. * @param _SCAN_MODE_ Scan conversion mode.
  741. * @retval None
  742. */
  743. #define ADC_SCANDIR(_SCAN_MODE_) \
  744. ( ( (_SCAN_MODE_) == (ADC_SCAN_DIRECTION_BACKWARD) \
  745. )? (ADC_CFGR1_SCANDIR) : (0x00000000) \
  746. )
  747. #if (defined(DMA) || defined(DMA1))
  748. /**
  749. * @brief Enable the ADC DMA continuous request.
  750. * @param _DMACONTREQ_MODE_ DMA continuous request mode.
  751. * @retval None
  752. */
  753. #define ADC_CFGR1_DMACONTREQ(_DMACONTREQ_MODE_) \
  754. ((_DMACONTREQ_MODE_) << 1U)
  755. #endif
  756. /**
  757. * @brief Configure the analog watchdog high threshold into register TR.
  758. * @param _Threshold_ Threshold value
  759. * @retval None
  760. */
  761. #define ADC_TRX_HIGHTHRESHOLD(_Threshold_) \
  762. ((_Threshold_) << 16U)
  763. /**
  764. * @}
  765. */
  766. /** @defgroup ADCEx_Private_Macros ADCEx Private Macros
  767. * @{
  768. */
  769. /* Macro reserved for internal HAL driver usage, not intended to be used in */
  770. /* code of final user. */
  771. /**
  772. * @brief Test if the selected ADC channel is an internal channel
  773. * VrefInt/TempSensor
  774. * Note:
  775. * @param __CHANNEL__ ADC channel
  776. * @retval None
  777. */
  778. #define ADC_IS_CHANNEL_INTERNAL(__CHANNEL__) \
  779. (((__CHANNEL__) == ADC_CHANNEL_TEMPSENSOR) || \
  780. ((__CHANNEL__) == ADC_CHANNEL_VREFINT) \
  781. )
  782. /**
  783. * @brief Select the internal measurement path to be enabled/disabled
  784. * corresponding to the selected ADC internal channel
  785. * VrefInt/TempSensor.
  786. * Note:
  787. * @param __CHANNEL__ ADC channel
  788. * @retval Bit of register ADC_CCR
  789. */
  790. #define ADC_CHANNEL_INTERNAL_PATH(__CHANNEL__) \
  791. (( (__CHANNEL__) == ADC_CHANNEL_TEMPSENSOR \
  792. )? \
  793. (ADC_CCR_TSEN) \
  794. : \
  795. (ADC_CCR_VREFEN) \
  796. )
  797. /**
  798. * @brief Shift the AWD threshold in function of the selected ADC resolution.
  799. * Thresholds have to be left-aligned on bit 11, the LSB (right bits) are set to 0.
  800. * If resolution 12 bits, no shift.
  801. * If resolution 10 bits, shift of 2 ranks on the left.
  802. * If resolution 8 bits, shift of 4 ranks on the left.
  803. * If resolution 6 bits, shift of 6 ranks on the left.
  804. * therefore, shift = (12 - resolution) = 12 - (12- (((RES[1:0]) >> 3)*2))
  805. * @param __HANDLE__ ADC handle
  806. * @param _Threshold_ Value to be shifted
  807. * @retval None
  808. */
  809. #define ADC_AWD1THRESHOLD_SHIFT_RESOLUTION(__HANDLE__, _Threshold_) \
  810. ((_Threshold_) << ((((__HANDLE__)->Instance->CFGR1 & ADC_CFGR1_RESSEL) >> 3U)*2))
  811. #define IS_ADC_CLOCKPRESCALER(ADC_CLOCK) (((ADC_CLOCK) == ADC_CLOCK_SYNC_PCLK_DIV1) || \
  812. ((ADC_CLOCK) == ADC_CLOCK_SYNC_PCLK_DIV2) || \
  813. ((ADC_CLOCK) == ADC_CLOCK_SYNC_PCLK_DIV4) || \
  814. ((ADC_CLOCK) == ADC_CLOCK_SYNC_PCLK_DIV8) || \
  815. ((ADC_CLOCK) == ADC_CLOCK_SYNC_PCLK_DIV16) || \
  816. ((ADC_CLOCK) == ADC_CLOCK_SYNC_PCLK_DIV32) || \
  817. ((ADC_CLOCK) == ADC_CLOCK_SYNC_PCLK_DIV64) || \
  818. ((ADC_CLOCK) == ADC_CLOCK_ASYNC_HSI_DIV1) || \
  819. ((ADC_CLOCK) == ADC_CLOCK_ASYNC_HSI_DIV2) || \
  820. ((ADC_CLOCK) == ADC_CLOCK_ASYNC_HSI_DIV4) || \
  821. ((ADC_CLOCK) == ADC_CLOCK_ASYNC_HSI_DIV8) || \
  822. ((ADC_CLOCK) == ADC_CLOCK_ASYNC_HSI_DIV16) || \
  823. ((ADC_CLOCK) == ADC_CLOCK_ASYNC_HSI_DIV32) || \
  824. ((ADC_CLOCK) == ADC_CLOCK_ASYNC_HSI_DIV64) )
  825. #define IS_ADC_RESOLUTION(RESOLUTION) (((RESOLUTION) == ADC_RESOLUTION_12B) || \
  826. ((RESOLUTION) == ADC_RESOLUTION_10B) || \
  827. ((RESOLUTION) == ADC_RESOLUTION_8B) || \
  828. ((RESOLUTION) == ADC_RESOLUTION_6B) )
  829. #define IS_ADC_DATA_ALIGN(ALIGN) (((ALIGN) == ADC_DATAALIGN_RIGHT) || \
  830. ((ALIGN) == ADC_DATAALIGN_LEFT) )
  831. #define IS_ADC_SCAN_MODE(SCAN_MODE) (((SCAN_MODE) == ADC_SCAN_DIRECTION_FORWARD) || \
  832. ((SCAN_MODE) == ADC_SCAN_DIRECTION_BACKWARD)|| \
  833. ((SCAN_MODE) == 0))
  834. #define IS_ADC_EXTTRIG_EDGE(EDGE) (((EDGE) == ADC_EXTERNALTRIGCONVEDGE_NONE) || \
  835. ((EDGE) == ADC_EXTERNALTRIGCONVEDGE_RISING) || \
  836. ((EDGE) == ADC_EXTERNALTRIGCONVEDGE_FALLING) || \
  837. ((EDGE) == ADC_EXTERNALTRIGCONVEDGE_RISINGFALLING) )
  838. #define IS_ADC_EOC_SELECTION(EOC_SELECTION) (((EOC_SELECTION) == ADC_EOC_SINGLE_CONV) || \
  839. ((EOC_SELECTION) == ADC_EOC_SEQ_CONV) )
  840. #define IS_ADC_OVERRUN(OVR) (((OVR) == ADC_OVR_DATA_PRESERVED) || \
  841. ((OVR) == ADC_OVR_DATA_OVERWRITTEN) )
  842. #define IS_ADC_RANK(WATCHDOG) (((WATCHDOG) == ADC_RANK_CHANNEL_NUMBER) || \
  843. ((WATCHDOG) == ADC_RANK_NONE) )
  844. #define IS_ADC_SAMPLE_TIME(TIME) (((TIME) == ADC_SAMPLETIME_3CYCLES_5) || \
  845. ((TIME) == ADC_SAMPLETIME_5CYCLES_5) || \
  846. ((TIME) == ADC_SAMPLETIME_7CYCLES_5) || \
  847. ((TIME) == ADC_SAMPLETIME_13CYCLES_5) || \
  848. ((TIME) == ADC_SAMPLETIME_28CYCLES_5) || \
  849. ((TIME) == ADC_SAMPLETIME_41CYCLES_5) || \
  850. ((TIME) == ADC_SAMPLETIME_71CYCLES_5) || \
  851. ((TIME) == ADC_SAMPLETIME_239CYCLES_5) )
  852. #define IS_ADC_ANALOG_WATCHDOG_MODE(WATCHDOG) (((WATCHDOG) == ADC_ANALOGWATCHDOG_NONE) || \
  853. ((WATCHDOG) == ADC_ANALOGWATCHDOG_SINGLE_REG) || \
  854. ((WATCHDOG) == ADC_ANALOGWATCHDOG_ALL_REG) )
  855. #define IS_ADC_EVENT_TYPE(EVENT) (((EVENT) == ADC_AWD_EVENT) || \
  856. ((EVENT) == ADC_OVR_EVENT) )
  857. #define IS_ADC_EXTTRIG(REGTRIG) (((REGTRIG) == ADC_EXTERNALTRIGCONV_T1_TRGO) || \
  858. ((REGTRIG) == ADC_EXTERNALTRIGCONV_T1_CC4) || \
  859. ((REGTRIG) == ADC_SOFTWARE_START))
  860. #if defined(ADC_CHSELR_CHSEL10)
  861. #define IS_ADC_CHANNEL(CHANNEL) (((CHANNEL) == ADC_CHANNEL_0) || \
  862. ((CHANNEL) == ADC_CHANNEL_1) || \
  863. ((CHANNEL) == ADC_CHANNEL_2) || \
  864. ((CHANNEL) == ADC_CHANNEL_3) || \
  865. ((CHANNEL) == ADC_CHANNEL_4) || \
  866. ((CHANNEL) == ADC_CHANNEL_5) || \
  867. ((CHANNEL) == ADC_CHANNEL_6) || \
  868. ((CHANNEL) == ADC_CHANNEL_7) || \
  869. ((CHANNEL) == ADC_CHANNEL_8) || \
  870. ((CHANNEL) == ADC_CHANNEL_9) || \
  871. ((CHANNEL) == ADC_CHANNEL_10) || \
  872. ((CHANNEL) == ADC_CHANNEL_TEMPSENSOR) || \
  873. ((CHANNEL) == ADC_CHANNEL_VREFINT) || \
  874. ((CHANNEL) == ADC_CHANNEL_VCCA) )
  875. #else
  876. #define IS_ADC_CHANNEL(CHANNEL) (((CHANNEL) == ADC_CHANNEL_0) || \
  877. ((CHANNEL) == ADC_CHANNEL_1) || \
  878. ((CHANNEL) == ADC_CHANNEL_2) || \
  879. ((CHANNEL) == ADC_CHANNEL_3) || \
  880. ((CHANNEL) == ADC_CHANNEL_4) || \
  881. ((CHANNEL) == ADC_CHANNEL_5) || \
  882. ((CHANNEL) == ADC_CHANNEL_6) || \
  883. ((CHANNEL) == ADC_CHANNEL_7) || \
  884. ((CHANNEL) == ADC_CHANNEL_8) || \
  885. ((CHANNEL) == ADC_CHANNEL_9) || \
  886. ((CHANNEL) == ADC_CHANNEL_TEMPSENSOR) || \
  887. ((CHANNEL) == ADC_CHANNEL_VREFINT) )
  888. #endif
  889. #define IS_ADC_VREFBUF(VREFBUF) (((VREFBUF) == ADC_VREFBUF_VCCA) || \
  890. ((VREFBUF) == ADC_VREFBUF_1P5V) )
  891. #define IS_ADC_CALIBRATION_SAMPLETIME(TIME) (((TIME) == ADC_CALIBSAMPLETIME_1CYCLE) || \
  892. ((TIME) == ADC_CALIBSAMPLETIME_2CYCLES) || \
  893. ((TIME) == ADC_CALIBSAMPLETIME_4CYCLES) || \
  894. ((TIME) == ADC_CALIBSAMPLETIME_8CYCLES) )
  895. #define IS_ADC_CALIBRATION_SELECTION(SELECTION) (((SELECTION) == ADC_CALIBSELECTION_ONLYOFFSET) || \
  896. ((SELECTION) == ADC_CALIBSELECTION_OFFSET_CAPACITANCE) )
  897. /** @defgroup ADC_range_verification ADC range verification
  898. * in function of ADC resolution selected (12, 10, 8 or 6 bits)
  899. * @{
  900. */
  901. #define IS_ADC_RANGE(RESOLUTION, ADC_VALUE) \
  902. ((((RESOLUTION) == ADC_RESOLUTION_12B) && ((ADC_VALUE) <= (0x0FFFU))) || \
  903. (((RESOLUTION) == ADC_RESOLUTION_10B) && ((ADC_VALUE) <= (0x03FFU))) || \
  904. (((RESOLUTION) == ADC_RESOLUTION_8B) && ((ADC_VALUE) <= (0x00FFU))) || \
  905. (((RESOLUTION) == ADC_RESOLUTION_6B) && ((ADC_VALUE) <= (0x003FU))) )
  906. /**
  907. * @}
  908. */
  909. /** @defgroup ADC_regular_rank_verification ADC regular rank verification
  910. * @{
  911. */
  912. #define IS_ADC_REGULAR_RANK(RANK) (((RANK) >= (1U)) && ((RANK) <= (16U)))
  913. /**
  914. * @}
  915. */
  916. /**
  917. * @}
  918. */
  919. /* Exported functions --------------------------------------------------------*/
  920. /** @addtogroup ADC_Exported_Functions
  921. * @{
  922. */
  923. /** @addtogroup ADC_Exported_Functions_Group1
  924. * @{
  925. */
  926. /* Initialization and de-initialization functions **********************************/
  927. HAL_StatusTypeDef HAL_ADC_Init(ADC_HandleTypeDef* hadc);
  928. HAL_StatusTypeDef HAL_ADC_DeInit(ADC_HandleTypeDef *hadc);
  929. void HAL_ADC_MspInit(ADC_HandleTypeDef* hadc);
  930. void HAL_ADC_MspDeInit(ADC_HandleTypeDef* hadc);
  931. #if (USE_HAL_ADC_REGISTER_CALLBACKS == 1)
  932. /* Callbacks Register/UnRegister functions ***********************************/
  933. HAL_StatusTypeDef HAL_ADC_RegisterCallback(ADC_HandleTypeDef *hadc, HAL_ADC_CallbackIDTypeDef CallbackID, pADC_CallbackTypeDef pCallback);
  934. HAL_StatusTypeDef HAL_ADC_UnRegisterCallback(ADC_HandleTypeDef *hadc, HAL_ADC_CallbackIDTypeDef CallbackID);
  935. #endif /* USE_HAL_ADC_REGISTER_CALLBACKS */
  936. /**
  937. * @}
  938. */
  939. /* IO operation functions *****************************************************/
  940. /** @addtogroup ADC_Exported_Functions_Group2
  941. * @{
  942. */
  943. /* Blocking mode: Polling */
  944. HAL_StatusTypeDef HAL_ADC_Start(ADC_HandleTypeDef* hadc);
  945. HAL_StatusTypeDef HAL_ADC_Stop(ADC_HandleTypeDef* hadc);
  946. HAL_StatusTypeDef HAL_ADC_PollForConversion(ADC_HandleTypeDef* hadc, uint32_t Timeout);
  947. HAL_StatusTypeDef HAL_ADC_PollForEvent(ADC_HandleTypeDef* hadc, uint32_t EventType, uint32_t Timeout);
  948. /* Non-blocking mode: Interruption */
  949. HAL_StatusTypeDef HAL_ADC_Start_IT(ADC_HandleTypeDef* hadc);
  950. HAL_StatusTypeDef HAL_ADC_Stop_IT(ADC_HandleTypeDef* hadc);
  951. #if (defined(DMA) || defined(DMA1))
  952. /* Non-blocking mode: DMA */
  953. HAL_StatusTypeDef HAL_ADC_Start_DMA(ADC_HandleTypeDef* hadc, uint32_t* pData, uint32_t Length);
  954. HAL_StatusTypeDef HAL_ADC_Stop_DMA(ADC_HandleTypeDef* hadc);
  955. #endif
  956. /* ADC retrieve conversion value intended to be used with polling or interruption */
  957. uint32_t HAL_ADC_GetValue(ADC_HandleTypeDef* hadc);
  958. /* ADC IRQHandler and Callbacks used in non-blocking modes (Interruption and DMA) */
  959. void HAL_ADC_IRQHandler(ADC_HandleTypeDef* hadc);
  960. void HAL_ADC_ConvCpltCallback(ADC_HandleTypeDef* hadc);
  961. void HAL_ADC_ConvHalfCpltCallback(ADC_HandleTypeDef* hadc);
  962. void HAL_ADC_LevelOutOfWindowCallback(ADC_HandleTypeDef* hadc);
  963. void HAL_ADC_ErrorCallback(ADC_HandleTypeDef *hadc);
  964. /* ADC Calibration */
  965. HAL_StatusTypeDef HAL_ADC_Calibration_SetAndStart(ADC_HandleTypeDef* hadc,uint32_t CalibSamplingTime,uint32_t CalibSelection);
  966. HAL_StatusTypeDef HAL_ADC_Calibration_Start(ADC_HandleTypeDef* hadc);
  967. HAL_ADCCalibStatusTypeDef HAL_ADC_Calibration_GetStatus(ADC_HandleTypeDef* hadc);
  968. #define HAL_ADCEx_Calibration_Start HAL_ADC_Calibration_Start
  969. #define HAL_ADCEx_Calibration_SetAndStart HAL_ADC_Calibration_SetAndStart
  970. #define HAL_ADCEx_Calibration_GetStatus HAL_ADC_Calibration_GetStatus
  971. /**
  972. * @}
  973. */
  974. /* Peripheral Control functions ***********************************************/
  975. /** @addtogroup ADC_Exported_Functions_Group3
  976. * @{
  977. */
  978. HAL_StatusTypeDef HAL_ADC_ConfigChannel(ADC_HandleTypeDef* hadc, ADC_ChannelConfTypeDef* sConfig);
  979. HAL_StatusTypeDef HAL_ADC_ConfigVrefBuf(ADC_HandleTypeDef* hadc, uint32_t VrefBuf);
  980. HAL_StatusTypeDef HAL_ADC_AnalogWDGConfig(ADC_HandleTypeDef* hadc, ADC_AnalogWDGConfTypeDef* AnalogWDGConfig);
  981. /**
  982. * @}
  983. */
  984. /* Peripheral State functions *************************************************/
  985. /** @addtogroup ADC_Exported_Functions_Group4
  986. * @{
  987. */
  988. uint32_t HAL_ADC_GetState(ADC_HandleTypeDef* hadc);
  989. uint32_t HAL_ADC_GetError(ADC_HandleTypeDef *hadc);
  990. /**
  991. * @}
  992. */
  993. /**
  994. * @}
  995. */
  996. /**
  997. * @}
  998. */
  999. /**
  1000. * @}
  1001. */
  1002. #ifdef __cplusplus
  1003. }
  1004. #endif
  1005. #endif /* __PY32F002B_HAL_ADC_H */
  1006. /************************ (C) COPYRIGHT Puya *****END OF FILE****/