stm32l1xx_hal_adc_ex.h 37 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588
  1. /**
  2. ******************************************************************************
  3. * @file stm32l1xx_hal_adc_ex.h
  4. * @author MCD Application Team
  5. * @brief Header file of ADC HAL Extension module.
  6. ******************************************************************************
  7. * @attention
  8. *
  9. * <h2><center>&copy; COPYRIGHT(c) 2017 STMicroelectronics</center></h2>
  10. *
  11. * Redistribution and use in source and binary forms, with or without modification,
  12. * are permitted provided that the following conditions are met:
  13. * 1. Redistributions of source code must retain the above copyright notice,
  14. * this list of conditions and the following disclaimer.
  15. * 2. Redistributions in binary form must reproduce the above copyright notice,
  16. * this list of conditions and the following disclaimer in the documentation
  17. * and/or other materials provided with the distribution.
  18. * 3. Neither the name of STMicroelectronics nor the names of its contributors
  19. * may be used to endorse or promote products derived from this software
  20. * without specific prior written permission.
  21. *
  22. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  23. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  24. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  25. * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
  26. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  27. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  28. * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  29. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  30. * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  31. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  32. *
  33. ******************************************************************************
  34. */
  35. /* Define to prevent recursive inclusion -------------------------------------*/
  36. #ifndef __STM32L1xx_HAL_ADC_EX_H
  37. #define __STM32L1xx_HAL_ADC_EX_H
  38. #ifdef __cplusplus
  39. extern "C" {
  40. #endif
  41. /* Includes ------------------------------------------------------------------*/
  42. #include "stm32l1xx_hal_def.h"
  43. /** @addtogroup STM32L1xx_HAL_Driver
  44. * @{
  45. */
  46. /** @addtogroup ADCEx
  47. * @{
  48. */
  49. /* Exported types ------------------------------------------------------------*/
  50. /** @defgroup ADCEx_Exported_Types ADCEx Exported Types
  51. * @{
  52. */
  53. /**
  54. * @brief ADC Configuration injected Channel structure definition
  55. * @note Parameters of this structure are shared within 2 scopes:
  56. * - Scope channel: InjectedChannel, InjectedRank, InjectedSamplingTime, InjectedOffset
  57. * - Scope injected group (affects all channels of injected group): InjectedNbrOfConversion, InjectedDiscontinuousConvMode,
  58. * AutoInjectedConv, ExternalTrigInjecConvEdge, ExternalTrigInjecConv.
  59. * @note The setting of these parameters with function HAL_ADCEx_InjectedConfigChannel() is conditioned to ADC state.
  60. * ADC state can be either:
  61. * - For all parameters: ADC disabled
  62. * - For all except parameters 'InjectedDiscontinuousConvMode' and 'AutoInjectedConv': ADC enabled without conversion on going on injected group.
  63. * - For parameters 'ExternalTrigInjecConv' and 'ExternalTrigInjecConvEdge': ADC enabled, even with conversion on going on injected group.
  64. */
  65. typedef struct
  66. {
  67. uint32_t InjectedChannel; /*!< Selection of ADC channel to configure
  68. This parameter can be a value of @ref ADC_channels
  69. Note: Depending on devices, some channels may not be available on package pins. Refer to device datasheet for channels availability. */
  70. uint32_t InjectedRank; /*!< Rank in the injected group sequencer
  71. This parameter must be a value of @ref ADCEx_injected_rank
  72. Note: In case of need to disable a channel or change order of conversion sequencer, rank containing a previous channel setting can be overwritten by the new channel setting (or parameter number of conversions can be adjusted) */
  73. uint32_t InjectedSamplingTime; /*!< Sampling time value to be set for the selected channel.
  74. Unit: ADC clock cycles
  75. Conversion time is the addition of sampling time and processing time (12 ADC clock cycles at ADC resolution 12 bits, 11 cycles at 10 bits, 9 cycles at 8 bits, 7 cycles at 6 bits).
  76. This parameter can be a value of @ref ADC_sampling_times
  77. Caution: This parameter updates the parameter property of the channel, that can be used into regular and/or injected groups.
  78. If this same channel has been previously configured in the other group (regular/injected), it will be updated to last setting.
  79. Note: In case of usage of internal measurement channels (VrefInt/Vbat/TempSensor),
  80. sampling time constraints must be respected (sampling time can be adjusted in function of ADC clock frequency and sampling time setting)
  81. Refer to device datasheet for timings values, parameters TS_vrefint, TS_temp (values rough order: 4us min). */
  82. uint32_t InjectedOffset; /*!< Defines the offset to be subtracted from the raw converted data (for channels set on injected group only).
  83. Offset value must be a positive number.
  84. Depending of ADC resolution selected (12, 10, 8 or 6 bits),
  85. this parameter must be a number between Min_Data = 0x000 and Max_Data = 0xFFF, 0x3FF, 0xFF or 0x3F respectively. */
  86. uint32_t InjectedNbrOfConversion; /*!< Specifies the number of ranks that will be converted within the injected group sequencer.
  87. To use the injected group sequencer and convert several ranks, parameter 'ScanConvMode' must be enabled.
  88. This parameter must be a number between Min_Data = 1 and Max_Data = 4.
  89. Caution: this setting impacts the entire injected group. Therefore, call of HAL_ADCEx_InjectedConfigChannel() to
  90. configure a channel on injected group can impact the configuration of other channels previously set. */
  91. uint32_t InjectedDiscontinuousConvMode; /*!< Specifies whether the conversions sequence of injected group is performed in Complete-sequence/Discontinuous-sequence (main sequence subdivided in successive parts).
  92. Discontinuous mode is used only if sequencer is enabled (parameter 'ScanConvMode'). If sequencer is disabled, this parameter is discarded.
  93. Discontinuous mode can be enabled only if continuous mode is disabled. If continuous mode is enabled, this parameter setting is discarded.
  94. This parameter can be set to ENABLE or DISABLE.
  95. Note: For injected group, number of discontinuous ranks increment is fixed to one-by-one.
  96. Caution: this setting impacts the entire injected group. Therefore, call of HAL_ADCEx_InjectedConfigChannel() to
  97. configure a channel on injected group can impact the configuration of other channels previously set. */
  98. uint32_t AutoInjectedConv; /*!< Enables or disables the selected ADC automatic injected group conversion after regular one
  99. This parameter can be set to ENABLE or DISABLE.
  100. Note: To use Automatic injected conversion, discontinuous mode must be disabled ('DiscontinuousConvMode' and 'InjectedDiscontinuousConvMode' set to DISABLE)
  101. Note: To use Automatic injected conversion, injected group external triggers must be disabled ('ExternalTrigInjecConv' set to ADC_SOFTWARE_START)
  102. Note: In case of DMA used with regular group: if DMA configured in normal mode (single shot) JAUTO will be stopped upon DMA transfer complete.
  103. To maintain JAUTO always enabled, DMA must be configured in circular mode.
  104. Caution: this setting impacts the entire injected group. Therefore, call of HAL_ADCEx_InjectedConfigChannel() to
  105. configure a channel on injected group can impact the configuration of other channels previously set. */
  106. uint32_t ExternalTrigInjecConv; /*!< Selects the external event used to trigger the conversion start of injected group.
  107. If set to ADC_INJECTED_SOFTWARE_START, external triggers are disabled.
  108. If set to external trigger source, triggering is on event rising edge.
  109. This parameter can be a value of @ref ADCEx_External_trigger_source_Injected
  110. Note: This parameter must be modified when ADC is disabled (before ADC start conversion or after ADC stop conversion).
  111. If ADC is enabled, this parameter setting is bypassed without error reporting (as it can be the expected behaviour in case of another parameter update on the fly)
  112. Caution: this setting impacts the entire injected group. Therefore, call of HAL_ADCEx_InjectedConfigChannel() to
  113. configure a channel on injected group can impact the configuration of other channels previously set. */
  114. uint32_t ExternalTrigInjecConvEdge; /*!< Selects the external trigger edge of injected group.
  115. This parameter can be a value of @ref ADCEx_External_trigger_edge_Injected.
  116. If trigger is set to ADC_INJECTED_SOFTWARE_START, this parameter is discarded.
  117. Caution: this setting impacts the entire injected group. Therefore, call of HAL_ADCEx_InjectedConfigChannel() to
  118. configure a channel on injected group can impact the configuration of other channels previously set. */
  119. }ADC_InjectionConfTypeDef;
  120. /**
  121. * @}
  122. */
  123. /* Exported constants --------------------------------------------------------*/
  124. /** @defgroup ADCEx_Exported_Constants ADCEx Exported Constants
  125. * @{
  126. */
  127. /** @defgroup ADCEx_injected_rank ADCEx rank into injected group
  128. * @{
  129. */
  130. #define ADC_INJECTED_RANK_1 (0x00000001U)
  131. #define ADC_INJECTED_RANK_2 (0x00000002U)
  132. #define ADC_INJECTED_RANK_3 (0x00000003U)
  133. #define ADC_INJECTED_RANK_4 (0x00000004U)
  134. /**
  135. * @}
  136. */
  137. /** @defgroup ADCEx_External_trigger_edge_Injected ADCEx external trigger enable for injected group
  138. * @{
  139. */
  140. #define ADC_EXTERNALTRIGINJECCONV_EDGE_NONE (0x00000000U)
  141. #define ADC_EXTERNALTRIGINJECCONV_EDGE_RISING ((uint32_t)ADC_CR2_JEXTEN_0)
  142. #define ADC_EXTERNALTRIGINJECCONV_EDGE_FALLING ((uint32_t)ADC_CR2_JEXTEN_1)
  143. #define ADC_EXTERNALTRIGINJECCONV_EDGE_RISINGFALLING ((uint32_t)ADC_CR2_JEXTEN)
  144. /**
  145. * @}
  146. */
  147. /** @defgroup ADCEx_External_trigger_source_Injected ADCEx External trigger source Injected
  148. * @{
  149. */
  150. /* External triggers for injected groups of ADC1 */
  151. #define ADC_EXTERNALTRIGINJECCONV_T2_CC1 ADC_EXTERNALTRIGINJEC_T2_CC1
  152. #define ADC_EXTERNALTRIGINJECCONV_T2_TRGO ADC_EXTERNALTRIGINJEC_T2_TRGO
  153. #define ADC_EXTERNALTRIGINJECCONV_T3_CC4 ADC_EXTERNALTRIGINJEC_T3_CC4
  154. #define ADC_EXTERNALTRIGINJECCONV_T4_TRGO ADC_EXTERNALTRIGINJEC_T4_TRGO
  155. #define ADC_EXTERNALTRIGINJECCONV_T4_CC1 ADC_EXTERNALTRIGINJEC_T4_CC1
  156. #define ADC_EXTERNALTRIGINJECCONV_T4_CC2 ADC_EXTERNALTRIGINJEC_T4_CC2
  157. #define ADC_EXTERNALTRIGINJECCONV_T4_CC3 ADC_EXTERNALTRIGINJEC_T4_CC3
  158. #define ADC_EXTERNALTRIGINJECCONV_T7_TRGO ADC_EXTERNALTRIGINJEC_T7_TRGO
  159. #define ADC_EXTERNALTRIGINJECCONV_T9_CC1 ADC_EXTERNALTRIGINJEC_T9_CC1
  160. #define ADC_EXTERNALTRIGINJECCONV_T9_TRGO ADC_EXTERNALTRIGINJEC_T9_TRGO
  161. #define ADC_EXTERNALTRIGINJECCONV_T10_CC1 ADC_EXTERNALTRIGINJEC_T10_CC1
  162. #define ADC_EXTERNALTRIGINJECCONV_EXT_IT15 ADC_EXTERNALTRIGINJEC_EXT_IT15
  163. #define ADC_INJECTED_SOFTWARE_START (0x00000010U)
  164. /**
  165. * @}
  166. */
  167. /**
  168. * @}
  169. */
  170. /* Private constants ---------------------------------------------------------*/
  171. /** @addtogroup ADCEx_Private_Constants ADCEx Private Constants
  172. * @{
  173. */
  174. /** @defgroup ADCEx_Internal_HAL_driver_Ext_trig_src_Injected ADCEx Internal HAL driver Ext trig src Injected
  175. * @{
  176. */
  177. /* List of external triggers of injected group for ADC1: */
  178. /* (used internally by HAL driver. To not use into HAL structure parameters) */
  179. #define ADC_EXTERNALTRIGINJEC_T9_CC1 (0x00000000U)
  180. #define ADC_EXTERNALTRIGINJEC_T9_TRGO ((uint32_t)( ADC_CR2_JEXTSEL_0))
  181. #define ADC_EXTERNALTRIGINJEC_T2_TRGO ((uint32_t)( ADC_CR2_JEXTSEL_1 ))
  182. #define ADC_EXTERNALTRIGINJEC_T2_CC1 ((uint32_t)( ADC_CR2_JEXTSEL_1 | ADC_CR2_JEXTSEL_0))
  183. #define ADC_EXTERNALTRIGINJEC_T3_CC4 ((uint32_t)( ADC_CR2_JEXTSEL_2 ))
  184. #define ADC_EXTERNALTRIGINJEC_T4_TRGO ((uint32_t)( ADC_CR2_JEXTSEL_2 | ADC_CR2_JEXTSEL_0))
  185. #define ADC_EXTERNALTRIGINJEC_T4_CC1 ((uint32_t)( ADC_CR2_JEXTSEL_2 | ADC_CR2_JEXTSEL_1 ))
  186. #define ADC_EXTERNALTRIGINJEC_T4_CC2 ((uint32_t)( ADC_CR2_JEXTSEL_2 | ADC_CR2_JEXTSEL_1 | ADC_CR2_JEXTSEL_0))
  187. #define ADC_EXTERNALTRIGINJEC_T4_CC3 ((uint32_t)(ADC_CR2_JEXTSEL_3 ))
  188. #define ADC_EXTERNALTRIGINJEC_T10_CC1 ((uint32_t)(ADC_CR2_JEXTSEL_3 | ADC_CR2_JEXTSEL_0))
  189. #define ADC_EXTERNALTRIGINJEC_T7_TRGO ((uint32_t)(ADC_CR2_JEXTSEL_3 | ADC_CR2_JEXTSEL_1 ))
  190. #define ADC_EXTERNALTRIGINJEC_EXT_IT15 ((uint32_t)(ADC_CR2_JEXTSEL_3 | ADC_CR2_JEXTSEL_2 | ADC_CR2_JEXTSEL_1 | ADC_CR2_JEXTSEL_0))
  191. /**
  192. * @}
  193. */
  194. /**
  195. * @}
  196. */
  197. /* Exported macro ------------------------------------------------------------*/
  198. /** @defgroup ADCEx_Exported_Macros ADCEx Exported Macros
  199. * @{
  200. */
  201. /* Macro for internal HAL driver usage, and possibly can be used into code of */
  202. /* final user. */
  203. /**
  204. * @brief Selection of channels bank.
  205. * Note: Banks availability depends on devices categories.
  206. * This macro is intended to change bank selection quickly on the fly,
  207. * without going through ADC init structure update and execution of function
  208. * 'HAL_ADC_Init()'.
  209. * @param __HANDLE__: ADC handle
  210. * @param __BANK__: Bank selection. This parameter can be a value of @ref ADC_ChannelsBank.
  211. * @retval None
  212. */
  213. #define __HAL_ADC_CHANNELS_BANK(__HANDLE__, __BANK__) \
  214. MODIFY_REG((__HANDLE__)->Instance->CR2, ADC_CR2_CFG, (__BANK__))
  215. #if defined(STM32L100xC) || defined (STM32L151xC) || defined (STM32L152xC) || defined (STM32L162xC) || defined(STM32L151xCA) || defined (STM32L151xD) || defined (STM32L152xCA) || defined (STM32L152xD) || defined (STM32L162xCA) || defined (STM32L162xD) || defined(STM32L151xE) || defined(STM32L151xDX) || defined (STM32L152xE) || defined (STM32L152xDX) || defined (STM32L162xE) || defined (STM32L162xDX)
  216. /**
  217. * @brief Configures the ADC channels speed.
  218. * Limited to channels 3, 8, 13 and to devices category Cat.3, Cat.4, Cat.5.
  219. * - For ADC_CHANNEL_3: Used as ADC direct channel (fast channel) if OPAMP1 is
  220. * in power down mode.
  221. * - For ADC_CHANNEL_8: Used as ADC direct channel (fast channel) if OPAMP2 is
  222. * in power down mode.
  223. * - For ADC_CHANNEL_13: Used as ADC re-routed channel if OPAMP3 is in
  224. * power down mode. Otherwise, channel 13 is connected to OPAMP3 output and
  225. * routed through switches COMP1_SW1 and VCOMP to ADC switch matrix.
  226. * (Note: OPAMP3 is available on STM32L1 Cat.4 only).
  227. * @param __CHANNEL__: ADC channel
  228. * This parameter can be one of the following values:
  229. * @arg ADC_CHANNEL_3: Channel 3 is selected.
  230. * @arg ADC_CHANNEL_8: Channel 8 is selected.
  231. * @arg ADC_CHANNEL_13: Channel 13 is selected.
  232. * @retval None
  233. */
  234. #define __HAL_ADC_CHANNEL_SPEED_FAST(__CHANNEL__) \
  235. ( ( ((__CHANNEL__) == ADC_CHANNEL_3) \
  236. )? \
  237. (SET_BIT(COMP->CSR, COMP_CSR_FCH3)) \
  238. : \
  239. ( ( ((__CHANNEL__) == ADC_CHANNEL_8) \
  240. )? \
  241. (SET_BIT(COMP->CSR, COMP_CSR_FCH8)) \
  242. : \
  243. ( ( ((__CHANNEL__) == ADC_CHANNEL_13) \
  244. )? \
  245. (SET_BIT(COMP->CSR, COMP_CSR_RCH13)) \
  246. : \
  247. (SET_BIT(COMP->CSR, 0x00000000)) \
  248. ) \
  249. ) \
  250. )
  251. #define __HAL_ADC_CHANNEL_SPEED_SLOW(__CHANNEL__) \
  252. ( ( ((__CHANNEL__) == ADC_CHANNEL_3) \
  253. )? \
  254. (CLEAR_BIT(COMP->CSR, COMP_CSR_FCH3)) \
  255. : \
  256. ( ( ((__CHANNEL__) == ADC_CHANNEL_8) \
  257. )? \
  258. (CLEAR_BIT(COMP->CSR, COMP_CSR_FCH8)) \
  259. : \
  260. ( ( ((__CHANNEL__) == ADC_CHANNEL_13) \
  261. )? \
  262. (CLEAR_BIT(COMP->CSR, COMP_CSR_RCH13)) \
  263. : \
  264. (SET_BIT(COMP->CSR, 0x00000000)) \
  265. ) \
  266. ) \
  267. )
  268. #endif /* STM32L100xC || STM32L151xC || STM32L152xC || STM32L162xC || STM32L151xCA || STM32L151xD || STM32L152xCA || STM32L152xD || STM32L162xCA || STM32L162xD || STM32L151xE || STM32L151xDX || STM32L152xE || STM32L152xDX || STM32L162xE || STM32L162xDX */
  269. /**
  270. * @}
  271. */
  272. /* Private macro ------------------------------------------------------------*/
  273. /** @defgroup ADCEx_Private_Macro ADCEx Private Macro
  274. * @{
  275. */
  276. /* Macro reserved for internal HAL driver usage, not intended to be used in */
  277. /* code of final user. */
  278. /**
  279. * @brief Set ADC ranks available in register SQR1.
  280. * Register SQR1 bits availability depends on device category.
  281. * @param _NbrOfConversion_: Regular channel sequence length
  282. * @retval None
  283. */
  284. #if defined(STM32L100xC) || defined (STM32L151xC) || defined (STM32L152xC) || defined (STM32L162xC) || defined(STM32L151xCA) || defined (STM32L151xD) || defined (STM32L152xCA) || defined (STM32L152xD) || defined (STM32L162xCA) || defined (STM32L162xD) || defined(STM32L151xE) || defined(STM32L151xDX) || defined (STM32L152xE) || defined (STM32L152xDX) || defined (STM32L162xE) || defined (STM32L162xDX)
  285. #define __ADC_SQR1_SQXX (ADC_SQR1_SQ28 | ADC_SQR1_SQ27 | ADC_SQR1_SQ26 | ADC_SQR1_SQ25)
  286. #else
  287. #define __ADC_SQR1_SQXX (ADC_SQR1_SQ27 | ADC_SQR1_SQ26 | ADC_SQR1_SQ25)
  288. #endif /* STM32L100xC || STM32L151xC || STM32L152xC || STM32L162xC || STM32L151xCA || STM32L151xD || STM32L152xCA || STM32L152xD || STM32L162xCA || STM32L162xD || STM32L151xE || STM32L151xDX || STM32L152xE || STM32L152xDX || STM32L162xE || STM32L162xDX */
  289. /**
  290. * @brief Set the ADC's sample time for channel numbers between 30 and 31.
  291. * Register SMPR0 availability depends on device category. If register is not
  292. * available on the current device, this macro does nothing.
  293. * @retval None
  294. * @param _SAMPLETIME_: Sample time parameter.
  295. * @param _CHANNELNB_: Channel number.
  296. * @retval None
  297. */
  298. #if defined(STM32L151xCA) || defined (STM32L151xD) || defined (STM32L152xCA) || defined (STM32L152xD) || defined (STM32L162xCA) || defined (STM32L162xD) || defined(STM32L151xE) || defined(STM32L151xDX) || defined (STM32L152xE) || defined (STM32L152xDX) || defined (STM32L162xE) || defined (STM32L162xDX)
  299. #define ADC_SMPR0(_SAMPLETIME_, _CHANNELNB_) \
  300. ((_SAMPLETIME_) << (3 * ((_CHANNELNB_) - 30)))
  301. #else
  302. #define ADC_SMPR0(_SAMPLETIME_, _CHANNELNB_) \
  303. (0x00000000U)
  304. #endif /* STM32L151xCA || STM32L151xD || STM32L152xCA || STM32L152xD || STM32L162xCA || STM32L162xD || STM32L151xE || STM32L151xDX || STM32L152xE || STM32L152xDX || STM32L162xE || STM32L162xDX */
  305. #if defined(STM32L151xCA) || defined (STM32L151xD) || defined (STM32L152xCA) || defined (STM32L152xD) || defined (STM32L162xCA) || defined (STM32L162xD) || defined(STM32L151xE) || defined(STM32L151xDX) || defined (STM32L152xE) || defined (STM32L152xDX) || defined (STM32L162xE) || defined (STM32L162xDX)
  306. /**
  307. * @brief Set the ADC's sample time for channel numbers between 20 and 29.
  308. * @param _SAMPLETIME_: Sample time parameter.
  309. * @param _CHANNELNB_: Channel number.
  310. * @retval None
  311. */
  312. #define ADC_SMPR1(_SAMPLETIME_, _CHANNELNB_) \
  313. ((_SAMPLETIME_) << (3 * ((_CHANNELNB_) - 20)))
  314. #else
  315. /**
  316. * @brief Set the ADC's sample time for channel numbers between 20 and 26.
  317. * @param _SAMPLETIME_: Sample time parameter.
  318. * @param _CHANNELNB_: Channel number.
  319. * @retval None
  320. */
  321. #define ADC_SMPR1(_SAMPLETIME_, _CHANNELNB_) \
  322. ((_SAMPLETIME_) << (3 * ((_CHANNELNB_) - 20)))
  323. #endif /* STM32L151xCA || STM32L151xD || STM32L152xCA || STM32L152xD || STM32L162xCA || STM32L162xD || STM32L151xE || STM32L151xDX || STM32L152xE || STM32L152xDX || STM32L162xE || STM32L162xDX */
  324. /**
  325. * @brief Defines the highest channel available in register SMPR1. Channels
  326. * availability depends on device category:
  327. * Highest channel in register SMPR1 is channel 26 for devices Cat.1, Cat.2, Cat.3
  328. * Highest channel in register SMPR1 is channel 29 for devices Cat.4, Cat.5
  329. * @param None
  330. * @retval None
  331. */
  332. #if defined(STM32L151xCA) || defined (STM32L151xD) || defined (STM32L152xCA) || defined (STM32L152xD) || defined (STM32L162xCA) || defined (STM32L162xD) || defined(STM32L151xE) || defined(STM32L151xDX) || defined (STM32L152xE) || defined (STM32L152xDX) || defined (STM32L162xE) || defined (STM32L162xDX)
  333. #define ADC_SMPR1_CHANNEL_MAX ADC_CHANNEL_29
  334. #else
  335. #define ADC_SMPR1_CHANNEL_MAX ADC_CHANNEL_26
  336. #endif /* STM32L151xCA || STM32L151xD || STM32L152xCA || STM32L152xD || STM32L162xCA || STM32L162xD || STM32L151xE || STM32L151xDX || STM32L152xE || STM32L152xDX || STM32L162xE || STM32L162xDX */
  337. /**
  338. * @brief Define mask of configuration bits of ADC and regular group in
  339. * register CR2 (bits of ADC enable, conversion start and injected group are
  340. * excluded of this mask).
  341. * @retval None
  342. */
  343. #if defined (STM32L100xC) || defined (STM32L151xC) || defined (STM32L152xC) || defined (STM32L162xC) || defined (STM32L151xCA) || defined (STM32L151xD) || defined (STM32L152xCA) || defined (STM32L152xD) || defined (STM32L162xCA) || defined (STM32L162xD) || defined (STM32L151xE) || defined (STM32L151xDX) || defined (STM32L152xE) || defined (STM32L152xDX) || defined (STM32L162xE) || defined (STM32L162xDX)
  344. #define ADC_CR2_MASK_ADCINIT() \
  345. (ADC_CR2_EXTEN | ADC_CR2_EXTSEL | ADC_CR2_ALIGN | ADC_CR2_EOCS | ADC_CR2_DDS | ADC_CR2_DELS | ADC_CR2_CFG | ADC_CR2_CONT)
  346. #else
  347. #define ADC_CR2_MASK_ADCINIT() \
  348. (ADC_CR2_EXTEN | ADC_CR2_EXTSEL | ADC_CR2_ALIGN | ADC_CR2_EOCS | ADC_CR2_DDS | ADC_CR2_DELS | ADC_CR2_CONT)
  349. #endif
  350. /**
  351. * @brief Get the maximum ADC conversion cycles on all channels.
  352. * Returns the selected sampling time + conversion time (12.5 ADC clock cycles)
  353. * Approximation of sampling time within 2 ranges, returns the highest value:
  354. * below 24 cycles {4 cycles; 9 cycles; 16 cycles; 24 cycles}
  355. * between 48 cycles and 384 cycles {48 cycles; 96 cycles; 192 cycles; 384 cycles}
  356. * Unit: ADC clock cycles
  357. * @param __HANDLE__: ADC handle
  358. * @retval ADC conversion cycles on all channels
  359. */
  360. #if defined(STM32L151xCA) || defined (STM32L151xD) || defined (STM32L152xCA) || defined (STM32L152xD) || defined (STM32L162xCA) || defined (STM32L162xD) || defined(STM32L151xE) || defined(STM32L151xDX) || defined (STM32L152xE) || defined (STM32L152xDX) || defined (STM32L162xE) || defined (STM32L162xDX)
  361. #define ADC_CONVCYCLES_MAX_RANGE(__HANDLE__) \
  362. (( (((__HANDLE__)->Instance->SMPR3 & ADC_SAMPLETIME_ALLCHANNELS_SMPR3BIT2) == RESET) && \
  363. (((__HANDLE__)->Instance->SMPR2 & ADC_SAMPLETIME_ALLCHANNELS_SMPR2BIT2) == RESET) && \
  364. (((__HANDLE__)->Instance->SMPR1 & ADC_SAMPLETIME_ALLCHANNELS_SMPR1BIT2) == RESET) && \
  365. (((__HANDLE__)->Instance->SMPR0 & ADC_SAMPLETIME_ALLCHANNELS_SMPR0BIT2) == RESET) ) ? \
  366. \
  367. ADC_CONVERSIONCLOCKCYCLES_SAMPLETIME_24CYCLES : ADC_CONVERSIONCLOCKCYCLES_SAMPLETIME_384CYCLES \
  368. )
  369. #else
  370. #define ADC_CONVCYCLES_MAX_RANGE(__HANDLE__) \
  371. (( (((__HANDLE__)->Instance->SMPR3 & ADC_SAMPLETIME_ALLCHANNELS_SMPR3BIT2) == RESET) && \
  372. (((__HANDLE__)->Instance->SMPR2 & ADC_SAMPLETIME_ALLCHANNELS_SMPR2BIT2) == RESET) && \
  373. (((__HANDLE__)->Instance->SMPR1 & ADC_SAMPLETIME_ALLCHANNELS_SMPR1BIT2) == RESET) ) ? \
  374. \
  375. ADC_CONVERSIONCLOCKCYCLES_SAMPLETIME_24CYCLES : ADC_CONVERSIONCLOCKCYCLES_SAMPLETIME_384CYCLES \
  376. )
  377. #endif /* STM32L151xCA || STM32L151xD || STM32L152xCA || STM32L152xD || STM32L162xCA || STM32L162xD || STM32L151xE || STM32L151xDX || STM32L152xE || STM32L152xDX || STM32L162xE || STM32L162xDX */
  378. /**
  379. * @brief Get the ADC clock prescaler from ADC common control register
  380. * and convert it to its decimal number setting (refer to reference manual)
  381. * @retval None
  382. */
  383. #define ADC_GET_CLOCK_PRESCALER_DECIMAL(__HANDLE__) \
  384. ((0x01) << ((ADC->CCR & ADC_CCR_ADCPRE) >> POSITION_VAL(ADC_CCR_ADCPRE)))
  385. /**
  386. * @brief Clear register SMPR0.
  387. * Register SMPR0 availability depends on device category. If register is not
  388. * available on the current device, this macro performs no action.
  389. * @param __HANDLE__: ADC handle
  390. * @retval None
  391. */
  392. #if defined(STM32L151xCA) || defined (STM32L151xD) || defined (STM32L152xCA) || defined (STM32L152xD) || defined (STM32L162xCA) || defined (STM32L162xD) || defined(STM32L151xE) || defined(STM32L151xDX) || defined (STM32L152xE) || defined (STM32L152xDX) || defined (STM32L162xE) || defined (STM32L162xDX)
  393. #define ADC_SMPR1_CLEAR(__HANDLE__) \
  394. CLEAR_BIT((__HANDLE__)->Instance->SMPR1, (ADC_SMPR1_SMP29 | ADC_SMPR1_SMP28 | ADC_SMPR1_SMP27 | \
  395. ADC_SMPR1_SMP26 | ADC_SMPR1_SMP25 | ADC_SMPR1_SMP24 | \
  396. ADC_SMPR1_SMP23 | ADC_SMPR1_SMP22 | ADC_SMPR1_SMP21 | \
  397. ADC_SMPR1_SMP20 ))
  398. #define ADC_SMPR0_CLEAR(__HANDLE__) \
  399. (CLEAR_BIT((__HANDLE__)->Instance->SMPR0, (ADC_SMPR0_SMP31 | ADC_SMPR0_SMP30)))
  400. #else
  401. #define ADC_SMPR1_CLEAR(__HANDLE__) \
  402. CLEAR_BIT((__HANDLE__)->Instance->SMPR1, (ADC_SMPR1_SMP26 | ADC_SMPR1_SMP25 | ADC_SMPR1_SMP24 | \
  403. ADC_SMPR1_SMP23 | ADC_SMPR1_SMP22 | ADC_SMPR1_SMP21 | \
  404. ADC_SMPR1_SMP20 ))
  405. #define ADC_SMPR0_CLEAR(__HANDLE__) __NOP()
  406. #endif /* STM32L151xCA || STM32L151xD || STM32L152xCA || STM32L152xD || STM32L162xCA || STM32L162xD || STM32L151xE || STM32L151xDX || STM32L152xE || STM32L152xDX || STM32L162xE || STM32L162xDX */
  407. /**
  408. * @brief Clear register CR2.
  409. * @param __HANDLE__: ADC handle
  410. * @retval None
  411. */
  412. #if defined(STM32L151xCA) || defined (STM32L151xD) || defined (STM32L152xCA) || defined (STM32L152xD) || defined (STM32L162xCA) || defined (STM32L162xD) || defined(STM32L151xE) || defined(STM32L151xDX) || defined (STM32L152xE) || defined (STM32L152xDX) || defined (STM32L162xE) || defined (STM32L162xDX)
  413. #define ADC_CR2_CLEAR(__HANDLE__) \
  414. (CLEAR_BIT((__HANDLE__)->Instance->CR2, (ADC_CR2_SWSTART | ADC_CR2_EXTEN | ADC_CR2_EXTSEL | \
  415. ADC_CR2_JSWSTART | ADC_CR2_JEXTEN | ADC_CR2_JEXTSEL | \
  416. ADC_CR2_ALIGN | ADC_CR2_EOCS | ADC_CR2_DDS | \
  417. ADC_CR2_DMA | ADC_CR2_DELS | ADC_CR2_CFG | \
  418. ADC_CR2_CONT | ADC_CR2_ADON )) \
  419. )
  420. #else
  421. #define ADC_CR2_CLEAR(__HANDLE__) \
  422. (CLEAR_BIT((__HANDLE__)->Instance->CR2, (ADC_CR2_SWSTART | ADC_CR2_EXTEN | ADC_CR2_EXTSEL | \
  423. ADC_CR2_JSWSTART | ADC_CR2_JEXTEN | ADC_CR2_JEXTSEL | \
  424. ADC_CR2_ALIGN | ADC_CR2_EOCS | ADC_CR2_DDS | \
  425. ADC_CR2_DMA | ADC_CR2_DELS | \
  426. ADC_CR2_CONT | ADC_CR2_ADON )) \
  427. )
  428. #endif /* STM32L151xCA || STM32L151xD || STM32L152xCA || STM32L152xD || STM32L162xCA || STM32L162xD || STM32L151xE || STM32L151xDX || STM32L152xE || STM32L152xDX || STM32L162xE || STM32L162xDX */
  429. /**
  430. * @brief Set the sampling time of selected channel on register SMPR0
  431. * Register SMPR0 availability depends on device category. If register is not
  432. * available on the current device, this macro performs no action.
  433. * @param __HANDLE__: ADC handle
  434. * @param _SAMPLETIME_: Sample time parameter.
  435. * @param __CHANNEL__: Channel number.
  436. * @retval None
  437. */
  438. #if defined(STM32L151xCA) || defined (STM32L151xD) || defined (STM32L152xCA) || defined (STM32L152xD) || defined (STM32L162xCA) || defined (STM32L162xD) || defined(STM32L151xE) || defined(STM32L151xDX) || defined (STM32L152xE) || defined (STM32L152xDX) || defined (STM32L162xE) || defined (STM32L162xDX)
  439. #define ADC_SMPR0_CHANNEL_SET(__HANDLE__, _SAMPLETIME_, __CHANNEL__) \
  440. MODIFY_REG((__HANDLE__)->Instance->SMPR0, \
  441. ADC_SMPR0(ADC_SMPR0_SMP30, (__CHANNEL__)), \
  442. ADC_SMPR0((_SAMPLETIME_), (__CHANNEL__)) )
  443. #else
  444. #define ADC_SMPR0_CHANNEL_SET(__HANDLE__, _SAMPLETIME_, __CHANNEL__) __NOP()
  445. #endif /* STM32L151xCA || STM32L151xD || STM32L152xCA || STM32L152xD || STM32L162xCA || STM32L162xD || STM32L151xE || STM32L151xDX || STM32L152xE || STM32L152xDX || STM32L162xE || STM32L162xDX */
  446. #define IS_ADC_INJECTED_RANK(CHANNEL) (((CHANNEL) == ADC_INJECTED_RANK_1) || \
  447. ((CHANNEL) == ADC_INJECTED_RANK_2) || \
  448. ((CHANNEL) == ADC_INJECTED_RANK_3) || \
  449. ((CHANNEL) == ADC_INJECTED_RANK_4) )
  450. #define IS_ADC_EXTTRIGINJEC_EDGE(EDGE) (((EDGE) == ADC_EXTERNALTRIGINJECCONV_EDGE_NONE) || \
  451. ((EDGE) == ADC_EXTERNALTRIGINJECCONV_EDGE_RISING) || \
  452. ((EDGE) == ADC_EXTERNALTRIGINJECCONV_EDGE_FALLING) || \
  453. ((EDGE) == ADC_EXTERNALTRIGINJECCONV_EDGE_RISINGFALLING) )
  454. #define IS_ADC_EXTTRIGINJEC(REGTRIG) (((REGTRIG) == ADC_EXTERNALTRIGINJECCONV_T2_CC1) || \
  455. ((REGTRIG) == ADC_EXTERNALTRIGINJECCONV_T2_TRGO) || \
  456. ((REGTRIG) == ADC_EXTERNALTRIGINJECCONV_T3_CC4) || \
  457. ((REGTRIG) == ADC_EXTERNALTRIGINJECCONV_T4_TRGO) || \
  458. ((REGTRIG) == ADC_EXTERNALTRIGINJECCONV_T4_CC1) || \
  459. ((REGTRIG) == ADC_EXTERNALTRIGINJECCONV_T4_CC2) || \
  460. ((REGTRIG) == ADC_EXTERNALTRIGINJECCONV_T4_CC3) || \
  461. ((REGTRIG) == ADC_EXTERNALTRIGINJECCONV_T7_TRGO) || \
  462. ((REGTRIG) == ADC_EXTERNALTRIGINJECCONV_T9_CC1) || \
  463. ((REGTRIG) == ADC_EXTERNALTRIGINJECCONV_T9_TRGO) || \
  464. ((REGTRIG) == ADC_EXTERNALTRIGINJECCONV_T10_CC1) || \
  465. ((REGTRIG) == ADC_EXTERNALTRIGINJECCONV_EXT_IT15) || \
  466. ((REGTRIG) == ADC_SOFTWARE_START) )
  467. /** @defgroup ADCEx_injected_nb_conv_verification ADCEx injected nb conv verification
  468. * @{
  469. */
  470. #define IS_ADC_INJECTED_NB_CONV(LENGTH) (((LENGTH) >= (1U)) && ((LENGTH) <= (4U)))
  471. /**
  472. * @}
  473. */
  474. /**
  475. * @}
  476. */
  477. /* Exported functions --------------------------------------------------------*/
  478. /** @addtogroup ADCEx_Exported_Functions
  479. * @{
  480. */
  481. /* IO operation functions *****************************************************/
  482. /** @addtogroup ADCEx_Exported_Functions_Group1
  483. * @{
  484. */
  485. /* Blocking mode: Polling */
  486. HAL_StatusTypeDef HAL_ADCEx_InjectedStart(ADC_HandleTypeDef* hadc);
  487. HAL_StatusTypeDef HAL_ADCEx_InjectedStop(ADC_HandleTypeDef* hadc);
  488. HAL_StatusTypeDef HAL_ADCEx_InjectedPollForConversion(ADC_HandleTypeDef* hadc, uint32_t Timeout);
  489. /* Non-blocking mode: Interruption */
  490. HAL_StatusTypeDef HAL_ADCEx_InjectedStart_IT(ADC_HandleTypeDef* hadc);
  491. HAL_StatusTypeDef HAL_ADCEx_InjectedStop_IT(ADC_HandleTypeDef* hadc);
  492. /* ADC retrieve conversion value intended to be used with polling or interruption */
  493. uint32_t HAL_ADCEx_InjectedGetValue(ADC_HandleTypeDef* hadc, uint32_t InjectedRank);
  494. /* ADC IRQHandler and Callbacks used in non-blocking modes (Interruption) */
  495. void HAL_ADCEx_InjectedConvCpltCallback(ADC_HandleTypeDef* hadc);
  496. /**
  497. * @}
  498. */
  499. /* Peripheral Control functions ***********************************************/
  500. /** @addtogroup ADCEx_Exported_Functions_Group2
  501. * @{
  502. */
  503. HAL_StatusTypeDef HAL_ADCEx_InjectedConfigChannel(ADC_HandleTypeDef* hadc,ADC_InjectionConfTypeDef* sConfigInjected);
  504. /**
  505. * @}
  506. */
  507. /**
  508. * @}
  509. */
  510. /**
  511. * @}
  512. */
  513. /**
  514. * @}
  515. */
  516. #ifdef __cplusplus
  517. }
  518. #endif
  519. #endif /* __STM32L1xx_HAL_ADC_EX_H */
  520. /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/