stm32l4xx_ll_adc.c 47 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063
  1. /**
  2. ******************************************************************************
  3. * @file stm32l4xx_ll_adc.c
  4. * @author MCD Application Team
  5. * @brief ADC LL module driver
  6. ******************************************************************************
  7. * @attention
  8. *
  9. * Copyright (c) 2017 STMicroelectronics.
  10. * All rights reserved.
  11. *
  12. * This software is licensed under terms that can be found in the LICENSE file
  13. * in the root directory of this software component.
  14. * If no LICENSE file comes with this software, it is provided AS-IS.
  15. *
  16. ******************************************************************************
  17. */
  18. #if defined(USE_FULL_LL_DRIVER)
  19. /* Includes ------------------------------------------------------------------*/
  20. #include "stm32l4xx_ll_adc.h"
  21. #include "stm32l4xx_ll_bus.h"
  22. #ifdef USE_FULL_ASSERT
  23. #include "stm32_assert.h"
  24. #else
  25. #define assert_param(expr) ((void)0U)
  26. #endif
  27. /** @addtogroup STM32L4xx_LL_Driver
  28. * @{
  29. */
  30. #if defined (ADC1) || defined (ADC2) || defined (ADC3)
  31. /** @addtogroup ADC_LL ADC
  32. * @{
  33. */
  34. /* Private types -------------------------------------------------------------*/
  35. /* Private variables ---------------------------------------------------------*/
  36. /* Private constants ---------------------------------------------------------*/
  37. /** @addtogroup ADC_LL_Private_Constants
  38. * @{
  39. */
  40. /* Definitions of ADC hardware constraints delays */
  41. /* Note: Only ADC peripheral HW delays are defined in ADC LL driver driver, */
  42. /* not timeout values: */
  43. /* Timeout values for ADC operations are dependent to device clock */
  44. /* configuration (system clock versus ADC clock), */
  45. /* and therefore must be defined in user application. */
  46. /* Refer to @ref ADC_LL_EC_HW_DELAYS for description of ADC timeout */
  47. /* values definition. */
  48. /* Note: ADC timeout values are defined here in CPU cycles to be independent */
  49. /* of device clock setting. */
  50. /* In user application, ADC timeout values should be defined with */
  51. /* temporal values, in function of device clock settings. */
  52. /* Highest ratio CPU clock frequency vs ADC clock frequency: */
  53. /* - ADC clock from synchronous clock with AHB prescaler 512, */
  54. /* APB prescaler 16, ADC prescaler 4. */
  55. /* - ADC clock from asynchronous clock (PLLSAI) with prescaler 1, */
  56. /* with highest ratio CPU clock frequency vs HSI clock frequency: */
  57. /* CPU clock frequency max 72MHz, PLLSAI freq min 26MHz: ratio 4. */
  58. /* Unit: CPU cycles. */
  59. #define ADC_CLOCK_RATIO_VS_CPU_HIGHEST (512UL * 16UL * 4UL)
  60. #define ADC_TIMEOUT_DISABLE_CPU_CYCLES (ADC_CLOCK_RATIO_VS_CPU_HIGHEST * 1UL)
  61. #define ADC_TIMEOUT_STOP_CONVERSION_CPU_CYCLES (ADC_CLOCK_RATIO_VS_CPU_HIGHEST * 1UL)
  62. /**
  63. * @}
  64. */
  65. /* Private macros ------------------------------------------------------------*/
  66. /** @addtogroup ADC_LL_Private_Macros
  67. * @{
  68. */
  69. /* Check of parameters for configuration of ADC hierarchical scope: */
  70. /* common to several ADC instances. */
  71. #define IS_LL_ADC_COMMON_CLOCK(__CLOCK__) \
  72. (((__CLOCK__) == LL_ADC_CLOCK_SYNC_PCLK_DIV1) \
  73. || ((__CLOCK__) == LL_ADC_CLOCK_SYNC_PCLK_DIV2) \
  74. || ((__CLOCK__) == LL_ADC_CLOCK_SYNC_PCLK_DIV4) \
  75. || ((__CLOCK__) == LL_ADC_CLOCK_ASYNC_DIV1) \
  76. || ((__CLOCK__) == LL_ADC_CLOCK_ASYNC_DIV2) \
  77. || ((__CLOCK__) == LL_ADC_CLOCK_ASYNC_DIV4) \
  78. || ((__CLOCK__) == LL_ADC_CLOCK_ASYNC_DIV6) \
  79. || ((__CLOCK__) == LL_ADC_CLOCK_ASYNC_DIV8) \
  80. || ((__CLOCK__) == LL_ADC_CLOCK_ASYNC_DIV10) \
  81. || ((__CLOCK__) == LL_ADC_CLOCK_ASYNC_DIV12) \
  82. || ((__CLOCK__) == LL_ADC_CLOCK_ASYNC_DIV16) \
  83. || ((__CLOCK__) == LL_ADC_CLOCK_ASYNC_DIV32) \
  84. || ((__CLOCK__) == LL_ADC_CLOCK_ASYNC_DIV64) \
  85. || ((__CLOCK__) == LL_ADC_CLOCK_ASYNC_DIV128) \
  86. || ((__CLOCK__) == LL_ADC_CLOCK_ASYNC_DIV256) \
  87. )
  88. /* Check of parameters for configuration of ADC hierarchical scope: */
  89. /* ADC instance. */
  90. #define IS_LL_ADC_RESOLUTION(__RESOLUTION__) \
  91. (((__RESOLUTION__) == LL_ADC_RESOLUTION_12B) \
  92. || ((__RESOLUTION__) == LL_ADC_RESOLUTION_10B) \
  93. || ((__RESOLUTION__) == LL_ADC_RESOLUTION_8B) \
  94. || ((__RESOLUTION__) == LL_ADC_RESOLUTION_6B) \
  95. )
  96. #define IS_LL_ADC_DATA_ALIGN(__DATA_ALIGN__) \
  97. (((__DATA_ALIGN__) == LL_ADC_DATA_ALIGN_RIGHT) \
  98. || ((__DATA_ALIGN__) == LL_ADC_DATA_ALIGN_LEFT) \
  99. )
  100. #define IS_LL_ADC_LOW_POWER(__LOW_POWER__) \
  101. (((__LOW_POWER__) == LL_ADC_LP_MODE_NONE) \
  102. || ((__LOW_POWER__) == LL_ADC_LP_AUTOWAIT) \
  103. )
  104. /* Check of parameters for configuration of ADC hierarchical scope: */
  105. /* ADC group regular */
  106. #define IS_LL_ADC_REG_TRIG_SOURCE(__REG_TRIG_SOURCE__) \
  107. (((__REG_TRIG_SOURCE__) == LL_ADC_REG_TRIG_SOFTWARE) \
  108. || ((__REG_TRIG_SOURCE__) == LL_ADC_REG_TRIG_EXT_TIM1_TRGO) \
  109. || ((__REG_TRIG_SOURCE__) == LL_ADC_REG_TRIG_EXT_TIM1_TRGO2) \
  110. || ((__REG_TRIG_SOURCE__) == LL_ADC_REG_TRIG_EXT_TIM1_CH1) \
  111. || ((__REG_TRIG_SOURCE__) == LL_ADC_REG_TRIG_EXT_TIM1_CH2) \
  112. || ((__REG_TRIG_SOURCE__) == LL_ADC_REG_TRIG_EXT_TIM1_CH3) \
  113. || ((__REG_TRIG_SOURCE__) == LL_ADC_REG_TRIG_EXT_TIM2_TRGO) \
  114. || ((__REG_TRIG_SOURCE__) == LL_ADC_REG_TRIG_EXT_TIM2_CH2) \
  115. || ((__REG_TRIG_SOURCE__) == LL_ADC_REG_TRIG_EXT_TIM3_TRGO) \
  116. || ((__REG_TRIG_SOURCE__) == LL_ADC_REG_TRIG_EXT_TIM3_CH4) \
  117. || ((__REG_TRIG_SOURCE__) == LL_ADC_REG_TRIG_EXT_TIM4_TRGO) \
  118. || ((__REG_TRIG_SOURCE__) == LL_ADC_REG_TRIG_EXT_TIM4_CH4) \
  119. || ((__REG_TRIG_SOURCE__) == LL_ADC_REG_TRIG_EXT_TIM6_TRGO) \
  120. || ((__REG_TRIG_SOURCE__) == LL_ADC_REG_TRIG_EXT_TIM8_TRGO) \
  121. || ((__REG_TRIG_SOURCE__) == LL_ADC_REG_TRIG_EXT_TIM8_TRGO2) \
  122. || ((__REG_TRIG_SOURCE__) == LL_ADC_REG_TRIG_EXT_TIM15_TRGO) \
  123. || ((__REG_TRIG_SOURCE__) == LL_ADC_REG_TRIG_EXT_EXTI_LINE11) \
  124. )
  125. #define IS_LL_ADC_REG_CONTINUOUS_MODE(__REG_CONTINUOUS_MODE__) \
  126. (((__REG_CONTINUOUS_MODE__) == LL_ADC_REG_CONV_SINGLE) \
  127. || ((__REG_CONTINUOUS_MODE__) == LL_ADC_REG_CONV_CONTINUOUS) \
  128. )
  129. #define IS_LL_ADC_REG_DMA_TRANSFER(__REG_DMA_TRANSFER__) \
  130. (((__REG_DMA_TRANSFER__) == LL_ADC_REG_DMA_TRANSFER_NONE) \
  131. || ((__REG_DMA_TRANSFER__) == LL_ADC_REG_DMA_TRANSFER_LIMITED) \
  132. || ((__REG_DMA_TRANSFER__) == LL_ADC_REG_DMA_TRANSFER_UNLIMITED) \
  133. )
  134. #define IS_LL_ADC_REG_OVR_DATA_BEHAVIOR(__REG_OVR_DATA_BEHAVIOR__) \
  135. (((__REG_OVR_DATA_BEHAVIOR__) == LL_ADC_REG_OVR_DATA_PRESERVED) \
  136. || ((__REG_OVR_DATA_BEHAVIOR__) == LL_ADC_REG_OVR_DATA_OVERWRITTEN) \
  137. )
  138. #define IS_LL_ADC_REG_SEQ_SCAN_LENGTH(__REG_SEQ_SCAN_LENGTH__) \
  139. (((__REG_SEQ_SCAN_LENGTH__) == LL_ADC_REG_SEQ_SCAN_DISABLE) \
  140. || ((__REG_SEQ_SCAN_LENGTH__) == LL_ADC_REG_SEQ_SCAN_ENABLE_2RANKS) \
  141. || ((__REG_SEQ_SCAN_LENGTH__) == LL_ADC_REG_SEQ_SCAN_ENABLE_3RANKS) \
  142. || ((__REG_SEQ_SCAN_LENGTH__) == LL_ADC_REG_SEQ_SCAN_ENABLE_4RANKS) \
  143. || ((__REG_SEQ_SCAN_LENGTH__) == LL_ADC_REG_SEQ_SCAN_ENABLE_5RANKS) \
  144. || ((__REG_SEQ_SCAN_LENGTH__) == LL_ADC_REG_SEQ_SCAN_ENABLE_6RANKS) \
  145. || ((__REG_SEQ_SCAN_LENGTH__) == LL_ADC_REG_SEQ_SCAN_ENABLE_7RANKS) \
  146. || ((__REG_SEQ_SCAN_LENGTH__) == LL_ADC_REG_SEQ_SCAN_ENABLE_8RANKS) \
  147. || ((__REG_SEQ_SCAN_LENGTH__) == LL_ADC_REG_SEQ_SCAN_ENABLE_9RANKS) \
  148. || ((__REG_SEQ_SCAN_LENGTH__) == LL_ADC_REG_SEQ_SCAN_ENABLE_10RANKS) \
  149. || ((__REG_SEQ_SCAN_LENGTH__) == LL_ADC_REG_SEQ_SCAN_ENABLE_11RANKS) \
  150. || ((__REG_SEQ_SCAN_LENGTH__) == LL_ADC_REG_SEQ_SCAN_ENABLE_12RANKS) \
  151. || ((__REG_SEQ_SCAN_LENGTH__) == LL_ADC_REG_SEQ_SCAN_ENABLE_13RANKS) \
  152. || ((__REG_SEQ_SCAN_LENGTH__) == LL_ADC_REG_SEQ_SCAN_ENABLE_14RANKS) \
  153. || ((__REG_SEQ_SCAN_LENGTH__) == LL_ADC_REG_SEQ_SCAN_ENABLE_15RANKS) \
  154. || ((__REG_SEQ_SCAN_LENGTH__) == LL_ADC_REG_SEQ_SCAN_ENABLE_16RANKS) \
  155. )
  156. #define IS_LL_ADC_REG_SEQ_SCAN_DISCONT_MODE(__REG_SEQ_DISCONT_MODE__) \
  157. (((__REG_SEQ_DISCONT_MODE__) == LL_ADC_REG_SEQ_DISCONT_DISABLE) \
  158. || ((__REG_SEQ_DISCONT_MODE__) == LL_ADC_REG_SEQ_DISCONT_1RANK) \
  159. || ((__REG_SEQ_DISCONT_MODE__) == LL_ADC_REG_SEQ_DISCONT_2RANKS) \
  160. || ((__REG_SEQ_DISCONT_MODE__) == LL_ADC_REG_SEQ_DISCONT_3RANKS) \
  161. || ((__REG_SEQ_DISCONT_MODE__) == LL_ADC_REG_SEQ_DISCONT_4RANKS) \
  162. || ((__REG_SEQ_DISCONT_MODE__) == LL_ADC_REG_SEQ_DISCONT_5RANKS) \
  163. || ((__REG_SEQ_DISCONT_MODE__) == LL_ADC_REG_SEQ_DISCONT_6RANKS) \
  164. || ((__REG_SEQ_DISCONT_MODE__) == LL_ADC_REG_SEQ_DISCONT_7RANKS) \
  165. || ((__REG_SEQ_DISCONT_MODE__) == LL_ADC_REG_SEQ_DISCONT_8RANKS) \
  166. )
  167. /* Check of parameters for configuration of ADC hierarchical scope: */
  168. /* ADC group injected */
  169. #define IS_LL_ADC_INJ_TRIG_SOURCE(__INJ_TRIG_SOURCE__) \
  170. (((__INJ_TRIG_SOURCE__) == LL_ADC_INJ_TRIG_SOFTWARE) \
  171. || ((__INJ_TRIG_SOURCE__) == LL_ADC_INJ_TRIG_EXT_TIM1_TRGO) \
  172. || ((__INJ_TRIG_SOURCE__) == LL_ADC_INJ_TRIG_EXT_TIM1_TRGO2) \
  173. || ((__INJ_TRIG_SOURCE__) == LL_ADC_INJ_TRIG_EXT_TIM1_CH4) \
  174. || ((__INJ_TRIG_SOURCE__) == LL_ADC_INJ_TRIG_EXT_TIM2_TRGO) \
  175. || ((__INJ_TRIG_SOURCE__) == LL_ADC_INJ_TRIG_EXT_TIM2_CH1) \
  176. || ((__INJ_TRIG_SOURCE__) == LL_ADC_INJ_TRIG_EXT_TIM3_TRGO) \
  177. || ((__INJ_TRIG_SOURCE__) == LL_ADC_INJ_TRIG_EXT_TIM3_CH1) \
  178. || ((__INJ_TRIG_SOURCE__) == LL_ADC_INJ_TRIG_EXT_TIM3_CH3) \
  179. || ((__INJ_TRIG_SOURCE__) == LL_ADC_INJ_TRIG_EXT_TIM3_CH4) \
  180. || ((__INJ_TRIG_SOURCE__) == LL_ADC_INJ_TRIG_EXT_TIM4_TRGO) \
  181. || ((__INJ_TRIG_SOURCE__) == LL_ADC_INJ_TRIG_EXT_TIM6_TRGO) \
  182. || ((__INJ_TRIG_SOURCE__) == LL_ADC_INJ_TRIG_EXT_TIM8_CH4) \
  183. || ((__INJ_TRIG_SOURCE__) == LL_ADC_INJ_TRIG_EXT_TIM8_TRGO) \
  184. || ((__INJ_TRIG_SOURCE__) == LL_ADC_INJ_TRIG_EXT_TIM8_TRGO2) \
  185. || ((__INJ_TRIG_SOURCE__) == LL_ADC_INJ_TRIG_EXT_TIM15_TRGO) \
  186. || ((__INJ_TRIG_SOURCE__) == LL_ADC_INJ_TRIG_EXT_EXTI_LINE15) \
  187. )
  188. #define IS_LL_ADC_INJ_TRIG_EXT_EDGE(__INJ_TRIG_EXT_EDGE__) \
  189. (((__INJ_TRIG_EXT_EDGE__) == LL_ADC_INJ_TRIG_EXT_RISING) \
  190. || ((__INJ_TRIG_EXT_EDGE__) == LL_ADC_INJ_TRIG_EXT_FALLING) \
  191. || ((__INJ_TRIG_EXT_EDGE__) == LL_ADC_INJ_TRIG_EXT_RISINGFALLING) \
  192. )
  193. #define IS_LL_ADC_INJ_TRIG_AUTO(__INJ_TRIG_AUTO__) \
  194. (((__INJ_TRIG_AUTO__) == LL_ADC_INJ_TRIG_INDEPENDENT) \
  195. || ((__INJ_TRIG_AUTO__) == LL_ADC_INJ_TRIG_FROM_GRP_REGULAR) \
  196. )
  197. #define IS_LL_ADC_INJ_SEQ_SCAN_LENGTH(__INJ_SEQ_SCAN_LENGTH__) \
  198. (((__INJ_SEQ_SCAN_LENGTH__) == LL_ADC_INJ_SEQ_SCAN_DISABLE) \
  199. || ((__INJ_SEQ_SCAN_LENGTH__) == LL_ADC_INJ_SEQ_SCAN_ENABLE_2RANKS) \
  200. || ((__INJ_SEQ_SCAN_LENGTH__) == LL_ADC_INJ_SEQ_SCAN_ENABLE_3RANKS) \
  201. || ((__INJ_SEQ_SCAN_LENGTH__) == LL_ADC_INJ_SEQ_SCAN_ENABLE_4RANKS) \
  202. )
  203. #define IS_LL_ADC_INJ_SEQ_SCAN_DISCONT_MODE(__INJ_SEQ_DISCONT_MODE__) \
  204. (((__INJ_SEQ_DISCONT_MODE__) == LL_ADC_INJ_SEQ_DISCONT_DISABLE) \
  205. || ((__INJ_SEQ_DISCONT_MODE__) == LL_ADC_INJ_SEQ_DISCONT_1RANK) \
  206. )
  207. #if defined(ADC_MULTIMODE_SUPPORT)
  208. /* Check of parameters for configuration of ADC hierarchical scope: */
  209. /* multimode. */
  210. #define IS_LL_ADC_MULTI_MODE(__MULTI_MODE__) \
  211. (((__MULTI_MODE__) == LL_ADC_MULTI_INDEPENDENT) \
  212. || ((__MULTI_MODE__) == LL_ADC_MULTI_DUAL_REG_SIMULT) \
  213. || ((__MULTI_MODE__) == LL_ADC_MULTI_DUAL_REG_INTERL) \
  214. || ((__MULTI_MODE__) == LL_ADC_MULTI_DUAL_INJ_SIMULT) \
  215. || ((__MULTI_MODE__) == LL_ADC_MULTI_DUAL_INJ_ALTERN) \
  216. || ((__MULTI_MODE__) == LL_ADC_MULTI_DUAL_REG_SIM_INJ_SIM) \
  217. || ((__MULTI_MODE__) == LL_ADC_MULTI_DUAL_REG_SIM_INJ_ALT) \
  218. || ((__MULTI_MODE__) == LL_ADC_MULTI_DUAL_REG_INT_INJ_SIM) \
  219. )
  220. #define IS_LL_ADC_MULTI_DMA_TRANSFER(__MULTI_DMA_TRANSFER__) \
  221. (((__MULTI_DMA_TRANSFER__) == LL_ADC_MULTI_REG_DMA_EACH_ADC) \
  222. || ((__MULTI_DMA_TRANSFER__) == LL_ADC_MULTI_REG_DMA_LIMIT_RES12_10B) \
  223. || ((__MULTI_DMA_TRANSFER__) == LL_ADC_MULTI_REG_DMA_LIMIT_RES8_6B) \
  224. || ((__MULTI_DMA_TRANSFER__) == LL_ADC_MULTI_REG_DMA_UNLMT_RES12_10B) \
  225. || ((__MULTI_DMA_TRANSFER__) == LL_ADC_MULTI_REG_DMA_UNLMT_RES8_6B) \
  226. )
  227. #define IS_LL_ADC_MULTI_TWOSMP_DELAY(__MULTI_TWOSMP_DELAY__) \
  228. (((__MULTI_TWOSMP_DELAY__) == LL_ADC_MULTI_TWOSMP_DELAY_1CYCLE) \
  229. || ((__MULTI_TWOSMP_DELAY__) == LL_ADC_MULTI_TWOSMP_DELAY_2CYCLES) \
  230. || ((__MULTI_TWOSMP_DELAY__) == LL_ADC_MULTI_TWOSMP_DELAY_3CYCLES) \
  231. || ((__MULTI_TWOSMP_DELAY__) == LL_ADC_MULTI_TWOSMP_DELAY_4CYCLES) \
  232. || ((__MULTI_TWOSMP_DELAY__) == LL_ADC_MULTI_TWOSMP_DELAY_5CYCLES) \
  233. || ((__MULTI_TWOSMP_DELAY__) == LL_ADC_MULTI_TWOSMP_DELAY_6CYCLES) \
  234. || ((__MULTI_TWOSMP_DELAY__) == LL_ADC_MULTI_TWOSMP_DELAY_7CYCLES) \
  235. || ((__MULTI_TWOSMP_DELAY__) == LL_ADC_MULTI_TWOSMP_DELAY_8CYCLES) \
  236. || ((__MULTI_TWOSMP_DELAY__) == LL_ADC_MULTI_TWOSMP_DELAY_9CYCLES) \
  237. || ((__MULTI_TWOSMP_DELAY__) == LL_ADC_MULTI_TWOSMP_DELAY_10CYCLES) \
  238. || ((__MULTI_TWOSMP_DELAY__) == LL_ADC_MULTI_TWOSMP_DELAY_11CYCLES) \
  239. || ((__MULTI_TWOSMP_DELAY__) == LL_ADC_MULTI_TWOSMP_DELAY_12CYCLES) \
  240. )
  241. #define IS_LL_ADC_MULTI_MASTER_SLAVE(__MULTI_MASTER_SLAVE__) \
  242. (((__MULTI_MASTER_SLAVE__) == LL_ADC_MULTI_MASTER) \
  243. || ((__MULTI_MASTER_SLAVE__) == LL_ADC_MULTI_SLAVE) \
  244. || ((__MULTI_MASTER_SLAVE__) == LL_ADC_MULTI_MASTER_SLAVE) \
  245. )
  246. #endif /* ADC_MULTIMODE_SUPPORT */
  247. /**
  248. * @}
  249. */
  250. /* Private function prototypes -----------------------------------------------*/
  251. /* Exported functions --------------------------------------------------------*/
  252. /** @addtogroup ADC_LL_Exported_Functions
  253. * @{
  254. */
  255. /** @addtogroup ADC_LL_EF_Init
  256. * @{
  257. */
  258. /**
  259. * @brief De-initialize registers of all ADC instances belonging to
  260. * the same ADC common instance to their default reset values.
  261. * @note This function is performing a hard reset, using high level
  262. * clock source RCC ADC reset.
  263. * Caution: On this STM32 series, if several ADC instances are available
  264. * on the selected device, RCC ADC reset will reset
  265. * all ADC instances belonging to the common ADC instance.
  266. * To de-initialize only 1 ADC instance, use
  267. * function @ref LL_ADC_DeInit().
  268. * @param ADCxy_COMMON ADC common instance
  269. * (can be set directly from CMSIS definition or by using helper macro @ref __LL_ADC_COMMON_INSTANCE() )
  270. * @retval An ErrorStatus enumeration value:
  271. * - SUCCESS: ADC common registers are de-initialized
  272. * - ERROR: not applicable
  273. */
  274. ErrorStatus LL_ADC_CommonDeInit(ADC_Common_TypeDef *ADCxy_COMMON)
  275. {
  276. /* Check the parameters */
  277. assert_param(IS_ADC_COMMON_INSTANCE(ADCxy_COMMON));
  278. /* Prevent unused argument compilation warning */
  279. (void)(ADCxy_COMMON);
  280. /* Force reset of ADC clock (core clock) */
  281. LL_AHB2_GRP1_ForceReset(LL_AHB2_GRP1_PERIPH_ADC);
  282. /* Release reset of ADC clock (core clock) */
  283. LL_AHB2_GRP1_ReleaseReset(LL_AHB2_GRP1_PERIPH_ADC);
  284. return SUCCESS;
  285. }
  286. /**
  287. * @brief Initialize some features of ADC common parameters
  288. * (all ADC instances belonging to the same ADC common instance)
  289. * and multimode (for devices with several ADC instances available).
  290. * @note The setting of ADC common parameters is conditioned to
  291. * ADC instances state:
  292. * All ADC instances belonging to the same ADC common instance
  293. * must be disabled.
  294. * @param ADCxy_COMMON ADC common instance
  295. * (can be set directly from CMSIS definition or by using helper macro @ref __LL_ADC_COMMON_INSTANCE() )
  296. * @param ADC_CommonInitStruct Pointer to a @ref LL_ADC_CommonInitTypeDef structure
  297. * @retval An ErrorStatus enumeration value:
  298. * - SUCCESS: ADC common registers are initialized
  299. * - ERROR: ADC common registers are not initialized
  300. */
  301. ErrorStatus LL_ADC_CommonInit(ADC_Common_TypeDef *ADCxy_COMMON, LL_ADC_CommonInitTypeDef *ADC_CommonInitStruct)
  302. {
  303. ErrorStatus status = SUCCESS;
  304. /* Check the parameters */
  305. assert_param(IS_ADC_COMMON_INSTANCE(ADCxy_COMMON));
  306. assert_param(IS_LL_ADC_COMMON_CLOCK(ADC_CommonInitStruct->CommonClock));
  307. #if defined(ADC_MULTIMODE_SUPPORT)
  308. assert_param(IS_LL_ADC_MULTI_MODE(ADC_CommonInitStruct->Multimode));
  309. if (ADC_CommonInitStruct->Multimode != LL_ADC_MULTI_INDEPENDENT)
  310. {
  311. assert_param(IS_LL_ADC_MULTI_DMA_TRANSFER(ADC_CommonInitStruct->MultiDMATransfer));
  312. assert_param(IS_LL_ADC_MULTI_TWOSMP_DELAY(ADC_CommonInitStruct->MultiTwoSamplingDelay));
  313. }
  314. #endif /* ADC_MULTIMODE_SUPPORT */
  315. /* Note: Hardware constraint (refer to description of functions */
  316. /* "LL_ADC_SetCommonXXX()" and "LL_ADC_SetMultiXXX()"): */
  317. /* On this STM32 series, setting of these features is conditioned to */
  318. /* ADC state: */
  319. /* All ADC instances of the ADC common group must be disabled. */
  320. if (__LL_ADC_IS_ENABLED_ALL_COMMON_INSTANCE(ADCxy_COMMON) == 0UL)
  321. {
  322. /* Configuration of ADC hierarchical scope: */
  323. /* - common to several ADC */
  324. /* (all ADC instances belonging to the same ADC common instance) */
  325. /* - Set ADC clock (conversion clock) */
  326. /* - multimode (if several ADC instances available on the */
  327. /* selected device) */
  328. /* - Set ADC multimode configuration */
  329. /* - Set ADC multimode DMA transfer */
  330. /* - Set ADC multimode: delay between 2 sampling phases */
  331. #if defined(ADC_MULTIMODE_SUPPORT)
  332. if (ADC_CommonInitStruct->Multimode != LL_ADC_MULTI_INDEPENDENT)
  333. {
  334. MODIFY_REG(ADCxy_COMMON->CCR,
  335. ADC_CCR_CKMODE
  336. | ADC_CCR_PRESC
  337. | ADC_CCR_DUAL
  338. | ADC_CCR_MDMA
  339. | ADC_CCR_DELAY
  340. ,
  341. ADC_CommonInitStruct->CommonClock
  342. | ADC_CommonInitStruct->Multimode
  343. | ADC_CommonInitStruct->MultiDMATransfer
  344. | ADC_CommonInitStruct->MultiTwoSamplingDelay
  345. );
  346. }
  347. else
  348. {
  349. MODIFY_REG(ADCxy_COMMON->CCR,
  350. ADC_CCR_CKMODE
  351. | ADC_CCR_PRESC
  352. | ADC_CCR_DUAL
  353. | ADC_CCR_MDMA
  354. | ADC_CCR_DELAY
  355. ,
  356. ADC_CommonInitStruct->CommonClock
  357. | LL_ADC_MULTI_INDEPENDENT
  358. );
  359. }
  360. #else
  361. LL_ADC_SetCommonClock(ADCxy_COMMON, ADC_CommonInitStruct->CommonClock);
  362. #endif
  363. }
  364. else
  365. {
  366. /* Initialization error: One or several ADC instances belonging to */
  367. /* the same ADC common instance are not disabled. */
  368. status = ERROR;
  369. }
  370. return status;
  371. }
  372. /**
  373. * @brief Set each @ref LL_ADC_CommonInitTypeDef field to default value.
  374. * @param ADC_CommonInitStruct Pointer to a @ref LL_ADC_CommonInitTypeDef structure
  375. * whose fields will be set to default values.
  376. * @retval None
  377. */
  378. void LL_ADC_CommonStructInit(LL_ADC_CommonInitTypeDef *ADC_CommonInitStruct)
  379. {
  380. /* Set ADC_CommonInitStruct fields to default values */
  381. /* Set fields of ADC common */
  382. /* (all ADC instances belonging to the same ADC common instance) */
  383. ADC_CommonInitStruct->CommonClock = LL_ADC_CLOCK_SYNC_PCLK_DIV2;
  384. #if defined(ADC_MULTIMODE_SUPPORT)
  385. /* Set fields of ADC multimode */
  386. ADC_CommonInitStruct->Multimode = LL_ADC_MULTI_INDEPENDENT;
  387. ADC_CommonInitStruct->MultiDMATransfer = LL_ADC_MULTI_REG_DMA_EACH_ADC;
  388. ADC_CommonInitStruct->MultiTwoSamplingDelay = LL_ADC_MULTI_TWOSMP_DELAY_1CYCLE;
  389. #endif /* ADC_MULTIMODE_SUPPORT */
  390. }
  391. /**
  392. * @brief De-initialize registers of the selected ADC instance
  393. * to their default reset values.
  394. * @note To reset all ADC instances quickly (perform a hard reset),
  395. * use function @ref LL_ADC_CommonDeInit().
  396. * @note If this functions returns error status, it means that ADC instance
  397. * is in an unknown state.
  398. * In this case, perform a hard reset using high level
  399. * clock source RCC ADC reset.
  400. * Caution: On this STM32 series, if several ADC instances are available
  401. * on the selected device, RCC ADC reset will reset
  402. * all ADC instances belonging to the common ADC instance.
  403. * Refer to function @ref LL_ADC_CommonDeInit().
  404. * @param ADCx ADC instance
  405. * @retval An ErrorStatus enumeration value:
  406. * - SUCCESS: ADC registers are de-initialized
  407. * - ERROR: ADC registers are not de-initialized
  408. */
  409. ErrorStatus LL_ADC_DeInit(ADC_TypeDef *ADCx)
  410. {
  411. ErrorStatus status = SUCCESS;
  412. __IO uint32_t timeout_cpu_cycles = 0UL;
  413. /* Check the parameters */
  414. assert_param(IS_ADC_ALL_INSTANCE(ADCx));
  415. /* Disable ADC instance if not already disabled. */
  416. if (LL_ADC_IsEnabled(ADCx) == 1UL)
  417. {
  418. /* Set ADC group regular trigger source to SW start to ensure to not */
  419. /* have an external trigger event occurring during the conversion stop */
  420. /* ADC disable process. */
  421. LL_ADC_REG_SetTriggerSource(ADCx, LL_ADC_REG_TRIG_SOFTWARE);
  422. /* Stop potential ADC conversion on going on ADC group regular. */
  423. if (LL_ADC_REG_IsConversionOngoing(ADCx) != 0UL)
  424. {
  425. if (LL_ADC_REG_IsStopConversionOngoing(ADCx) == 0UL)
  426. {
  427. LL_ADC_REG_StopConversion(ADCx);
  428. }
  429. }
  430. /* Set ADC group injected trigger source to SW start to ensure to not */
  431. /* have an external trigger event occurring during the conversion stop */
  432. /* ADC disable process. */
  433. LL_ADC_INJ_SetTriggerSource(ADCx, LL_ADC_INJ_TRIG_SOFTWARE);
  434. /* Stop potential ADC conversion on going on ADC group injected. */
  435. if (LL_ADC_INJ_IsConversionOngoing(ADCx) != 0UL)
  436. {
  437. if (LL_ADC_INJ_IsStopConversionOngoing(ADCx) == 0UL)
  438. {
  439. LL_ADC_INJ_StopConversion(ADCx);
  440. }
  441. }
  442. /* Wait for ADC conversions are effectively stopped */
  443. timeout_cpu_cycles = ADC_TIMEOUT_STOP_CONVERSION_CPU_CYCLES;
  444. while ((LL_ADC_REG_IsStopConversionOngoing(ADCx)
  445. | LL_ADC_INJ_IsStopConversionOngoing(ADCx)) == 1UL)
  446. {
  447. timeout_cpu_cycles--;
  448. if (timeout_cpu_cycles == 0UL)
  449. {
  450. /* Time-out error */
  451. status = ERROR;
  452. break;
  453. }
  454. }
  455. /* Flush group injected contexts queue (register JSQR): */
  456. /* Note: Bit JQM must be set to empty the contexts queue (otherwise */
  457. /* contexts queue is maintained with the last active context). */
  458. LL_ADC_INJ_SetQueueMode(ADCx, LL_ADC_INJ_QUEUE_2CONTEXTS_END_EMPTY);
  459. /* Disable the ADC instance */
  460. LL_ADC_Disable(ADCx);
  461. /* Wait for ADC instance is effectively disabled */
  462. timeout_cpu_cycles = ADC_TIMEOUT_DISABLE_CPU_CYCLES;
  463. while (LL_ADC_IsDisableOngoing(ADCx) == 1UL)
  464. {
  465. timeout_cpu_cycles--;
  466. if (timeout_cpu_cycles == 0UL)
  467. {
  468. /* Time-out error */
  469. status = ERROR;
  470. break;
  471. }
  472. }
  473. }
  474. /* Check whether ADC state is compliant with expected state */
  475. if (READ_BIT(ADCx->CR,
  476. (ADC_CR_JADSTP | ADC_CR_ADSTP | ADC_CR_JADSTART | ADC_CR_ADSTART
  477. | ADC_CR_ADDIS | ADC_CR_ADEN)
  478. )
  479. == 0UL)
  480. {
  481. /* ========== Reset ADC registers ========== */
  482. /* Reset register IER */
  483. CLEAR_BIT(ADCx->IER,
  484. (LL_ADC_IT_ADRDY
  485. | LL_ADC_IT_EOC
  486. | LL_ADC_IT_EOS
  487. | LL_ADC_IT_OVR
  488. | LL_ADC_IT_EOSMP
  489. | LL_ADC_IT_JEOC
  490. | LL_ADC_IT_JEOS
  491. | LL_ADC_IT_JQOVF
  492. | LL_ADC_IT_AWD1
  493. | LL_ADC_IT_AWD2
  494. | LL_ADC_IT_AWD3
  495. )
  496. );
  497. /* Reset register ISR */
  498. SET_BIT(ADCx->ISR,
  499. (LL_ADC_FLAG_ADRDY
  500. | LL_ADC_FLAG_EOC
  501. | LL_ADC_FLAG_EOS
  502. | LL_ADC_FLAG_OVR
  503. | LL_ADC_FLAG_EOSMP
  504. | LL_ADC_FLAG_JEOC
  505. | LL_ADC_FLAG_JEOS
  506. | LL_ADC_FLAG_JQOVF
  507. | LL_ADC_FLAG_AWD1
  508. | LL_ADC_FLAG_AWD2
  509. | LL_ADC_FLAG_AWD3
  510. )
  511. );
  512. /* Reset register CR */
  513. /* - Bits ADC_CR_JADSTP, ADC_CR_ADSTP, ADC_CR_JADSTART, ADC_CR_ADSTART, */
  514. /* ADC_CR_ADCAL, ADC_CR_ADDIS, ADC_CR_ADEN are in */
  515. /* access mode "read-set": no direct reset applicable. */
  516. /* - Reset Calibration mode to default setting (single ended). */
  517. /* - Disable ADC internal voltage regulator. */
  518. /* - Enable ADC deep power down. */
  519. /* Note: ADC internal voltage regulator disable and ADC deep power */
  520. /* down enable are conditioned to ADC state disabled: */
  521. /* already done above. */
  522. CLEAR_BIT(ADCx->CR, ADC_CR_ADVREGEN | ADC_CR_ADCALDIF);
  523. SET_BIT(ADCx->CR, ADC_CR_DEEPPWD);
  524. /* Reset register CFGR */
  525. MODIFY_REG(ADCx->CFGR,
  526. (ADC_CFGR_AWD1CH | ADC_CFGR_JAUTO | ADC_CFGR_JAWD1EN
  527. | ADC_CFGR_AWD1EN | ADC_CFGR_AWD1SGL | ADC_CFGR_JQM
  528. | ADC_CFGR_JDISCEN | ADC_CFGR_DISCNUM | ADC_CFGR_DISCEN
  529. | ADC_CFGR_AUTDLY | ADC_CFGR_CONT | ADC_CFGR_OVRMOD
  530. | ADC_CFGR_EXTEN | ADC_CFGR_EXTSEL | ADC_CFGR_ALIGN
  531. | ADC_CFGR_RES | ADC_CFGR_DMACFG | ADC_CFGR_DMAEN),
  532. ADC_CFGR_JQDIS
  533. );
  534. /* Reset register CFGR2 */
  535. CLEAR_BIT(ADCx->CFGR2,
  536. (ADC_CFGR2_ROVSM | ADC_CFGR2_TROVS | ADC_CFGR2_OVSS
  537. | ADC_CFGR2_OVSR | ADC_CFGR2_JOVSE | ADC_CFGR2_ROVSE)
  538. );
  539. /* Reset register SMPR1 */
  540. CLEAR_BIT(ADCx->SMPR1,
  541. (ADC_SMPR1_SMP9 | ADC_SMPR1_SMP8 | ADC_SMPR1_SMP7
  542. | ADC_SMPR1_SMP6 | ADC_SMPR1_SMP5 | ADC_SMPR1_SMP4
  543. | ADC_SMPR1_SMP3 | ADC_SMPR1_SMP2 | ADC_SMPR1_SMP1)
  544. );
  545. /* Reset register SMPR2 */
  546. CLEAR_BIT(ADCx->SMPR2,
  547. (ADC_SMPR2_SMP18 | ADC_SMPR2_SMP17 | ADC_SMPR2_SMP16
  548. | ADC_SMPR2_SMP15 | ADC_SMPR2_SMP14 | ADC_SMPR2_SMP13
  549. | ADC_SMPR2_SMP12 | ADC_SMPR2_SMP11 | ADC_SMPR2_SMP10)
  550. );
  551. /* Reset register TR1 */
  552. MODIFY_REG(ADCx->TR1, ADC_TR1_HT1 | ADC_TR1_LT1, ADC_TR1_HT1);
  553. /* Reset register TR2 */
  554. MODIFY_REG(ADCx->TR2, ADC_TR2_HT2 | ADC_TR2_LT2, ADC_TR2_HT2);
  555. /* Reset register TR3 */
  556. MODIFY_REG(ADCx->TR3, ADC_TR3_HT3 | ADC_TR3_LT3, ADC_TR3_HT3);
  557. /* Reset register SQR1 */
  558. CLEAR_BIT(ADCx->SQR1,
  559. (ADC_SQR1_SQ4 | ADC_SQR1_SQ3 | ADC_SQR1_SQ2
  560. | ADC_SQR1_SQ1 | ADC_SQR1_L)
  561. );
  562. /* Reset register SQR2 */
  563. CLEAR_BIT(ADCx->SQR2,
  564. (ADC_SQR2_SQ9 | ADC_SQR2_SQ8 | ADC_SQR2_SQ7
  565. | ADC_SQR2_SQ6 | ADC_SQR2_SQ5)
  566. );
  567. /* Reset register SQR3 */
  568. CLEAR_BIT(ADCx->SQR3,
  569. (ADC_SQR3_SQ14 | ADC_SQR3_SQ13 | ADC_SQR3_SQ12
  570. | ADC_SQR3_SQ11 | ADC_SQR3_SQ10)
  571. );
  572. /* Reset register SQR4 */
  573. CLEAR_BIT(ADCx->SQR4, ADC_SQR4_SQ16 | ADC_SQR4_SQ15);
  574. /* Reset register JSQR */
  575. CLEAR_BIT(ADCx->JSQR,
  576. (ADC_JSQR_JL
  577. | ADC_JSQR_JEXTSEL | ADC_JSQR_JEXTEN
  578. | ADC_JSQR_JSQ4 | ADC_JSQR_JSQ3
  579. | ADC_JSQR_JSQ2 | ADC_JSQR_JSQ1)
  580. );
  581. /* Reset register DR */
  582. /* Note: bits in access mode read only, no direct reset applicable */
  583. /* Reset register OFR1 */
  584. CLEAR_BIT(ADCx->OFR1, ADC_OFR1_OFFSET1_EN | ADC_OFR1_OFFSET1_CH | ADC_OFR1_OFFSET1);
  585. /* Reset register OFR2 */
  586. CLEAR_BIT(ADCx->OFR2, ADC_OFR2_OFFSET2_EN | ADC_OFR2_OFFSET2_CH | ADC_OFR2_OFFSET2);
  587. /* Reset register OFR3 */
  588. CLEAR_BIT(ADCx->OFR3, ADC_OFR3_OFFSET3_EN | ADC_OFR3_OFFSET3_CH | ADC_OFR3_OFFSET3);
  589. /* Reset register OFR4 */
  590. CLEAR_BIT(ADCx->OFR4, ADC_OFR4_OFFSET4_EN | ADC_OFR4_OFFSET4_CH | ADC_OFR4_OFFSET4);
  591. /* Reset registers JDR1, JDR2, JDR3, JDR4 */
  592. /* Note: bits in access mode read only, no direct reset applicable */
  593. /* Reset register AWD2CR */
  594. CLEAR_BIT(ADCx->AWD2CR, ADC_AWD2CR_AWD2CH);
  595. /* Reset register AWD3CR */
  596. CLEAR_BIT(ADCx->AWD3CR, ADC_AWD3CR_AWD3CH);
  597. /* Reset register DIFSEL */
  598. CLEAR_BIT(ADCx->DIFSEL, ADC_DIFSEL_DIFSEL);
  599. /* Reset register CALFACT */
  600. CLEAR_BIT(ADCx->CALFACT, ADC_CALFACT_CALFACT_D | ADC_CALFACT_CALFACT_S);
  601. }
  602. else
  603. {
  604. /* ADC instance is in an unknown state */
  605. /* Need to performing a hard reset of ADC instance, using high level */
  606. /* clock source RCC ADC reset. */
  607. /* Caution: On this STM32 series, if several ADC instances are available */
  608. /* on the selected device, RCC ADC reset will reset */
  609. /* all ADC instances belonging to the common ADC instance. */
  610. /* Caution: On this STM32 series, if several ADC instances are available */
  611. /* on the selected device, RCC ADC reset will reset */
  612. /* all ADC instances belonging to the common ADC instance. */
  613. status = ERROR;
  614. }
  615. return status;
  616. }
  617. /**
  618. * @brief Initialize some features of ADC instance.
  619. * @note These parameters have an impact on ADC scope: ADC instance.
  620. * Affects both group regular and group injected (availability
  621. * of ADC group injected depends on STM32 families).
  622. * Refer to corresponding unitary functions into
  623. * @ref ADC_LL_EF_Configuration_ADC_Instance .
  624. * @note The setting of these parameters by function @ref LL_ADC_Init()
  625. * is conditioned to ADC state:
  626. * ADC instance must be disabled.
  627. * This condition is applied to all ADC features, for efficiency
  628. * and compatibility over all STM32 families. However, the different
  629. * features can be set under different ADC state conditions
  630. * (setting possible with ADC enabled without conversion on going,
  631. * ADC enabled with conversion on going, ...)
  632. * Each feature can be updated afterwards with a unitary function
  633. * and potentially with ADC in a different state than disabled,
  634. * refer to description of each function for setting
  635. * conditioned to ADC state.
  636. * @note After using this function, some other features must be configured
  637. * using LL unitary functions.
  638. * The minimum configuration remaining to be done is:
  639. * - Set ADC group regular or group injected sequencer:
  640. * map channel on the selected sequencer rank.
  641. * Refer to function @ref LL_ADC_REG_SetSequencerRanks().
  642. * - Set ADC channel sampling time
  643. * Refer to function LL_ADC_SetChannelSamplingTime();
  644. * @param ADCx ADC instance
  645. * @param ADC_InitStruct Pointer to a @ref LL_ADC_REG_InitTypeDef structure
  646. * @retval An ErrorStatus enumeration value:
  647. * - SUCCESS: ADC registers are initialized
  648. * - ERROR: ADC registers are not initialized
  649. */
  650. ErrorStatus LL_ADC_Init(ADC_TypeDef *ADCx, LL_ADC_InitTypeDef *ADC_InitStruct)
  651. {
  652. ErrorStatus status = SUCCESS;
  653. /* Check the parameters */
  654. assert_param(IS_ADC_ALL_INSTANCE(ADCx));
  655. assert_param(IS_LL_ADC_RESOLUTION(ADC_InitStruct->Resolution));
  656. assert_param(IS_LL_ADC_DATA_ALIGN(ADC_InitStruct->DataAlignment));
  657. assert_param(IS_LL_ADC_LOW_POWER(ADC_InitStruct->LowPowerMode));
  658. /* Note: Hardware constraint (refer to description of this function): */
  659. /* ADC instance must be disabled. */
  660. if (LL_ADC_IsEnabled(ADCx) == 0UL)
  661. {
  662. /* Configuration of ADC hierarchical scope: */
  663. /* - ADC instance */
  664. /* - Set ADC data resolution */
  665. /* - Set ADC conversion data alignment */
  666. /* - Set ADC low power mode */
  667. MODIFY_REG(ADCx->CFGR,
  668. ADC_CFGR_RES
  669. | ADC_CFGR_ALIGN
  670. | ADC_CFGR_AUTDLY
  671. ,
  672. ADC_InitStruct->Resolution
  673. | ADC_InitStruct->DataAlignment
  674. | ADC_InitStruct->LowPowerMode
  675. );
  676. }
  677. else
  678. {
  679. /* Initialization error: ADC instance is not disabled. */
  680. status = ERROR;
  681. }
  682. return status;
  683. }
  684. /**
  685. * @brief Set each @ref LL_ADC_InitTypeDef field to default value.
  686. * @param ADC_InitStruct Pointer to a @ref LL_ADC_InitTypeDef structure
  687. * whose fields will be set to default values.
  688. * @retval None
  689. */
  690. void LL_ADC_StructInit(LL_ADC_InitTypeDef *ADC_InitStruct)
  691. {
  692. /* Set ADC_InitStruct fields to default values */
  693. /* Set fields of ADC instance */
  694. ADC_InitStruct->Resolution = LL_ADC_RESOLUTION_12B;
  695. ADC_InitStruct->DataAlignment = LL_ADC_DATA_ALIGN_RIGHT;
  696. ADC_InitStruct->LowPowerMode = LL_ADC_LP_MODE_NONE;
  697. }
  698. /**
  699. * @brief Initialize some features of ADC group regular.
  700. * @note These parameters have an impact on ADC scope: ADC group regular.
  701. * Refer to corresponding unitary functions into
  702. * @ref ADC_LL_EF_Configuration_ADC_Group_Regular
  703. * (functions with prefix "REG").
  704. * @note The setting of these parameters by function @ref LL_ADC_Init()
  705. * is conditioned to ADC state:
  706. * ADC instance must be disabled.
  707. * This condition is applied to all ADC features, for efficiency
  708. * and compatibility over all STM32 families. However, the different
  709. * features can be set under different ADC state conditions
  710. * (setting possible with ADC enabled without conversion on going,
  711. * ADC enabled with conversion on going, ...)
  712. * Each feature can be updated afterwards with a unitary function
  713. * and potentially with ADC in a different state than disabled,
  714. * refer to description of each function for setting
  715. * conditioned to ADC state.
  716. * @note After using this function, other features must be configured
  717. * using LL unitary functions.
  718. * The minimum configuration remaining to be done is:
  719. * - Set ADC group regular or group injected sequencer:
  720. * map channel on the selected sequencer rank.
  721. * Refer to function @ref LL_ADC_REG_SetSequencerRanks().
  722. * - Set ADC channel sampling time
  723. * Refer to function LL_ADC_SetChannelSamplingTime();
  724. * @param ADCx ADC instance
  725. * @param ADC_REG_InitStruct Pointer to a @ref LL_ADC_REG_InitTypeDef structure
  726. * @retval An ErrorStatus enumeration value:
  727. * - SUCCESS: ADC registers are initialized
  728. * - ERROR: ADC registers are not initialized
  729. */
  730. ErrorStatus LL_ADC_REG_Init(ADC_TypeDef *ADCx, LL_ADC_REG_InitTypeDef *ADC_REG_InitStruct)
  731. {
  732. ErrorStatus status = SUCCESS;
  733. /* Check the parameters */
  734. assert_param(IS_ADC_ALL_INSTANCE(ADCx));
  735. assert_param(IS_LL_ADC_REG_TRIG_SOURCE(ADC_REG_InitStruct->TriggerSource));
  736. assert_param(IS_LL_ADC_REG_SEQ_SCAN_LENGTH(ADC_REG_InitStruct->SequencerLength));
  737. if (ADC_REG_InitStruct->SequencerLength != LL_ADC_REG_SEQ_SCAN_DISABLE)
  738. {
  739. assert_param(IS_LL_ADC_REG_SEQ_SCAN_DISCONT_MODE(ADC_REG_InitStruct->SequencerDiscont));
  740. /* ADC group regular continuous mode and discontinuous mode */
  741. /* can not be enabled simultenaeously */
  742. assert_param((ADC_REG_InitStruct->ContinuousMode == LL_ADC_REG_CONV_SINGLE)
  743. || (ADC_REG_InitStruct->SequencerDiscont == LL_ADC_REG_SEQ_DISCONT_DISABLE));
  744. }
  745. assert_param(IS_LL_ADC_REG_CONTINUOUS_MODE(ADC_REG_InitStruct->ContinuousMode));
  746. assert_param(IS_LL_ADC_REG_DMA_TRANSFER(ADC_REG_InitStruct->DMATransfer));
  747. assert_param(IS_LL_ADC_REG_OVR_DATA_BEHAVIOR(ADC_REG_InitStruct->Overrun));
  748. /* Note: Hardware constraint (refer to description of this function): */
  749. /* ADC instance must be disabled. */
  750. if (LL_ADC_IsEnabled(ADCx) == 0UL)
  751. {
  752. /* Configuration of ADC hierarchical scope: */
  753. /* - ADC group regular */
  754. /* - Set ADC group regular trigger source */
  755. /* - Set ADC group regular sequencer length */
  756. /* - Set ADC group regular sequencer discontinuous mode */
  757. /* - Set ADC group regular continuous mode */
  758. /* - Set ADC group regular conversion data transfer: no transfer or */
  759. /* transfer by DMA, and DMA requests mode */
  760. /* - Set ADC group regular overrun behavior */
  761. /* Note: On this STM32 series, ADC trigger edge is set to value 0x0 by */
  762. /* setting of trigger source to SW start. */
  763. if (ADC_REG_InitStruct->SequencerLength != LL_ADC_REG_SEQ_SCAN_DISABLE)
  764. {
  765. MODIFY_REG(ADCx->CFGR,
  766. ADC_CFGR_EXTSEL
  767. | ADC_CFGR_EXTEN
  768. | ADC_CFGR_DISCEN
  769. | ADC_CFGR_DISCNUM
  770. | ADC_CFGR_CONT
  771. | ADC_CFGR_DMAEN
  772. | ADC_CFGR_DMACFG
  773. | ADC_CFGR_OVRMOD
  774. ,
  775. ADC_REG_InitStruct->TriggerSource
  776. | ADC_REG_InitStruct->SequencerDiscont
  777. | ADC_REG_InitStruct->ContinuousMode
  778. | ADC_REG_InitStruct->DMATransfer
  779. | ADC_REG_InitStruct->Overrun
  780. );
  781. }
  782. else
  783. {
  784. MODIFY_REG(ADCx->CFGR,
  785. ADC_CFGR_EXTSEL
  786. | ADC_CFGR_EXTEN
  787. | ADC_CFGR_DISCEN
  788. | ADC_CFGR_DISCNUM
  789. | ADC_CFGR_CONT
  790. | ADC_CFGR_DMAEN
  791. | ADC_CFGR_DMACFG
  792. | ADC_CFGR_OVRMOD
  793. ,
  794. ADC_REG_InitStruct->TriggerSource
  795. | LL_ADC_REG_SEQ_DISCONT_DISABLE
  796. | ADC_REG_InitStruct->ContinuousMode
  797. | ADC_REG_InitStruct->DMATransfer
  798. | ADC_REG_InitStruct->Overrun
  799. );
  800. }
  801. /* Set ADC group regular sequencer length and scan direction */
  802. LL_ADC_REG_SetSequencerLength(ADCx, ADC_REG_InitStruct->SequencerLength);
  803. }
  804. else
  805. {
  806. /* Initialization error: ADC instance is not disabled. */
  807. status = ERROR;
  808. }
  809. return status;
  810. }
  811. /**
  812. * @brief Set each @ref LL_ADC_REG_InitTypeDef field to default value.
  813. * @param ADC_REG_InitStruct Pointer to a @ref LL_ADC_REG_InitTypeDef structure
  814. * whose fields will be set to default values.
  815. * @retval None
  816. */
  817. void LL_ADC_REG_StructInit(LL_ADC_REG_InitTypeDef *ADC_REG_InitStruct)
  818. {
  819. /* Set ADC_REG_InitStruct fields to default values */
  820. /* Set fields of ADC group regular */
  821. /* Note: On this STM32 series, ADC trigger edge is set to value 0x0 by */
  822. /* setting of trigger source to SW start. */
  823. ADC_REG_InitStruct->TriggerSource = LL_ADC_REG_TRIG_SOFTWARE;
  824. ADC_REG_InitStruct->SequencerLength = LL_ADC_REG_SEQ_SCAN_DISABLE;
  825. ADC_REG_InitStruct->SequencerDiscont = LL_ADC_REG_SEQ_DISCONT_DISABLE;
  826. ADC_REG_InitStruct->ContinuousMode = LL_ADC_REG_CONV_SINGLE;
  827. ADC_REG_InitStruct->DMATransfer = LL_ADC_REG_DMA_TRANSFER_NONE;
  828. ADC_REG_InitStruct->Overrun = LL_ADC_REG_OVR_DATA_OVERWRITTEN;
  829. }
  830. /**
  831. * @brief Initialize some features of ADC group injected.
  832. * @note These parameters have an impact on ADC scope: ADC group injected.
  833. * Refer to corresponding unitary functions into
  834. * @ref ADC_LL_EF_Configuration_ADC_Group_Regular
  835. * (functions with prefix "INJ").
  836. * @note The setting of these parameters by function @ref LL_ADC_Init()
  837. * is conditioned to ADC state:
  838. * ADC instance must be disabled.
  839. * This condition is applied to all ADC features, for efficiency
  840. * and compatibility over all STM32 families. However, the different
  841. * features can be set under different ADC state conditions
  842. * (setting possible with ADC enabled without conversion on going,
  843. * ADC enabled with conversion on going, ...)
  844. * Each feature can be updated afterwards with a unitary function
  845. * and potentially with ADC in a different state than disabled,
  846. * refer to description of each function for setting
  847. * conditioned to ADC state.
  848. * @note After using this function, other features must be configured
  849. * using LL unitary functions.
  850. * The minimum configuration remaining to be done is:
  851. * - Set ADC group injected sequencer:
  852. * map channel on the selected sequencer rank.
  853. * Refer to function @ref LL_ADC_INJ_SetSequencerRanks().
  854. * - Set ADC channel sampling time
  855. * Refer to function LL_ADC_SetChannelSamplingTime();
  856. * @note Caution if feature ADC group injected contexts queue is enabled
  857. * (refer to with function @ref LL_ADC_INJ_SetQueueMode() ):
  858. * using successively several times this function will appear as
  859. * having no effect.
  860. * To set several features of ADC group injected, use
  861. * function @ref LL_ADC_INJ_ConfigQueueContext().
  862. * @param ADCx ADC instance
  863. * @param ADC_INJ_InitStruct Pointer to a @ref LL_ADC_INJ_InitTypeDef structure
  864. * @retval An ErrorStatus enumeration value:
  865. * - SUCCESS: ADC registers are initialized
  866. * - ERROR: ADC registers are not initialized
  867. */
  868. ErrorStatus LL_ADC_INJ_Init(ADC_TypeDef *ADCx, LL_ADC_INJ_InitTypeDef *ADC_INJ_InitStruct)
  869. {
  870. ErrorStatus status = SUCCESS;
  871. /* Check the parameters */
  872. assert_param(IS_ADC_ALL_INSTANCE(ADCx));
  873. assert_param(IS_LL_ADC_INJ_TRIG_SOURCE(ADC_INJ_InitStruct->TriggerSource));
  874. assert_param(IS_LL_ADC_INJ_SEQ_SCAN_LENGTH(ADC_INJ_InitStruct->SequencerLength));
  875. if (ADC_INJ_InitStruct->SequencerLength != LL_ADC_INJ_SEQ_SCAN_DISABLE)
  876. {
  877. assert_param(IS_LL_ADC_INJ_SEQ_SCAN_DISCONT_MODE(ADC_INJ_InitStruct->SequencerDiscont));
  878. }
  879. assert_param(IS_LL_ADC_INJ_TRIG_AUTO(ADC_INJ_InitStruct->TrigAuto));
  880. /* Note: Hardware constraint (refer to description of this function): */
  881. /* ADC instance must be disabled. */
  882. if (LL_ADC_IsEnabled(ADCx) == 0UL)
  883. {
  884. /* Configuration of ADC hierarchical scope: */
  885. /* - ADC group injected */
  886. /* - Set ADC group injected trigger source */
  887. /* - Set ADC group injected sequencer length */
  888. /* - Set ADC group injected sequencer discontinuous mode */
  889. /* - Set ADC group injected conversion trigger: independent or */
  890. /* from ADC group regular */
  891. /* Note: On this STM32 series, ADC trigger edge is set to value 0x0 by */
  892. /* setting of trigger source to SW start. */
  893. if (ADC_INJ_InitStruct->SequencerLength != LL_ADC_REG_SEQ_SCAN_DISABLE)
  894. {
  895. MODIFY_REG(ADCx->CFGR,
  896. ADC_CFGR_JDISCEN
  897. | ADC_CFGR_JAUTO
  898. ,
  899. ADC_INJ_InitStruct->SequencerDiscont
  900. | ADC_INJ_InitStruct->TrigAuto
  901. );
  902. }
  903. else
  904. {
  905. MODIFY_REG(ADCx->CFGR,
  906. ADC_CFGR_JDISCEN
  907. | ADC_CFGR_JAUTO
  908. ,
  909. LL_ADC_REG_SEQ_DISCONT_DISABLE
  910. | ADC_INJ_InitStruct->TrigAuto
  911. );
  912. }
  913. MODIFY_REG(ADCx->JSQR,
  914. ADC_JSQR_JEXTSEL
  915. | ADC_JSQR_JEXTEN
  916. | ADC_JSQR_JL
  917. ,
  918. ADC_INJ_InitStruct->TriggerSource
  919. | ADC_INJ_InitStruct->SequencerLength
  920. );
  921. }
  922. else
  923. {
  924. /* Initialization error: ADC instance is not disabled. */
  925. status = ERROR;
  926. }
  927. return status;
  928. }
  929. /**
  930. * @brief Set each @ref LL_ADC_INJ_InitTypeDef field to default value.
  931. * @param ADC_INJ_InitStruct Pointer to a @ref LL_ADC_INJ_InitTypeDef structure
  932. * whose fields will be set to default values.
  933. * @retval None
  934. */
  935. void LL_ADC_INJ_StructInit(LL_ADC_INJ_InitTypeDef *ADC_INJ_InitStruct)
  936. {
  937. /* Set ADC_INJ_InitStruct fields to default values */
  938. /* Set fields of ADC group injected */
  939. ADC_INJ_InitStruct->TriggerSource = LL_ADC_INJ_TRIG_SOFTWARE;
  940. ADC_INJ_InitStruct->SequencerLength = LL_ADC_INJ_SEQ_SCAN_DISABLE;
  941. ADC_INJ_InitStruct->SequencerDiscont = LL_ADC_INJ_SEQ_DISCONT_DISABLE;
  942. ADC_INJ_InitStruct->TrigAuto = LL_ADC_INJ_TRIG_INDEPENDENT;
  943. }
  944. /**
  945. * @}
  946. */
  947. /**
  948. * @}
  949. */
  950. /**
  951. * @}
  952. */
  953. #endif /* ADC1 || ADC2 || ADC3 */
  954. /**
  955. * @}
  956. */
  957. #endif /* USE_FULL_LL_DRIVER */