stm32l1xx_hal_adc_ex.h 39 KB

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