You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 

1081 lines
49 KiB

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