py32f0xx_hal_adc.h 58 KB

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