stm32l4xx_ll_adc.c 49 KB

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