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.

922 lines
42 KiB

12 months ago
  1. /**
  2. ******************************************************************************
  3. * @file stm32f4xx_ll_adc.c
  4. * @author MCD Application Team
  5. * @brief ADC LL module driver
  6. ******************************************************************************
  7. * @attention
  8. *
  9. * Copyright (c) 2017 STMicroelectronics.
  10. * All rights reserved.
  11. *
  12. * This software is licensed under terms that can be found in the LICENSE file
  13. * in the root directory of this software component.
  14. * If no LICENSE file comes with this software, it is provided AS-IS.
  15. *
  16. ******************************************************************************
  17. */
  18. #if defined(USE_FULL_LL_DRIVER)
  19. /* Includes ------------------------------------------------------------------*/
  20. #include "stm32f4xx_ll_adc.h"
  21. #include "stm32f4xx_ll_bus.h"
  22. #ifdef USE_FULL_ASSERT
  23. #include "stm32_assert.h"
  24. #else
  25. #define assert_param(expr) ((void)0U)
  26. #endif
  27. /** @addtogroup STM32F4xx_LL_Driver
  28. * @{
  29. */
  30. #if defined (ADC1) || defined (ADC2) || defined (ADC3)
  31. /** @addtogroup ADC_LL ADC
  32. * @{
  33. */
  34. /* Private types -------------------------------------------------------------*/
  35. /* Private variables ---------------------------------------------------------*/
  36. /* Private constants ---------------------------------------------------------*/
  37. /* Private macros ------------------------------------------------------------*/
  38. /** @addtogroup ADC_LL_Private_Macros
  39. * @{
  40. */
  41. /* Check of parameters for configuration of ADC hierarchical scope: */
  42. /* common to several ADC instances. */
  43. #define IS_LL_ADC_COMMON_CLOCK(__CLOCK__) \
  44. ( ((__CLOCK__) == LL_ADC_CLOCK_SYNC_PCLK_DIV2) \
  45. || ((__CLOCK__) == LL_ADC_CLOCK_SYNC_PCLK_DIV4) \
  46. || ((__CLOCK__) == LL_ADC_CLOCK_SYNC_PCLK_DIV6) \
  47. || ((__CLOCK__) == LL_ADC_CLOCK_SYNC_PCLK_DIV8) \
  48. )
  49. /* Check of parameters for configuration of ADC hierarchical scope: */
  50. /* ADC instance. */
  51. #define IS_LL_ADC_RESOLUTION(__RESOLUTION__) \
  52. ( ((__RESOLUTION__) == LL_ADC_RESOLUTION_12B) \
  53. || ((__RESOLUTION__) == LL_ADC_RESOLUTION_10B) \
  54. || ((__RESOLUTION__) == LL_ADC_RESOLUTION_8B) \
  55. || ((__RESOLUTION__) == LL_ADC_RESOLUTION_6B) \
  56. )
  57. #define IS_LL_ADC_DATA_ALIGN(__DATA_ALIGN__) \
  58. ( ((__DATA_ALIGN__) == LL_ADC_DATA_ALIGN_RIGHT) \
  59. || ((__DATA_ALIGN__) == LL_ADC_DATA_ALIGN_LEFT) \
  60. )
  61. #define IS_LL_ADC_SCAN_SELECTION(__SCAN_SELECTION__) \
  62. ( ((__SCAN_SELECTION__) == LL_ADC_SEQ_SCAN_DISABLE) \
  63. || ((__SCAN_SELECTION__) == LL_ADC_SEQ_SCAN_ENABLE) \
  64. )
  65. #define IS_LL_ADC_SEQ_SCAN_MODE(__SEQ_SCAN_MODE__) \
  66. ( ((__SCAN_MODE__) == LL_ADC_SEQ_SCAN_DISABLE) \
  67. || ((__SCAN_MODE__) == LL_ADC_SEQ_SCAN_ENABLE) \
  68. )
  69. /* Check of parameters for configuration of ADC hierarchical scope: */
  70. /* ADC group regular */
  71. #define IS_LL_ADC_REG_TRIG_SOURCE(__REG_TRIG_SOURCE__) \
  72. ( ((__REG_TRIG_SOURCE__) == LL_ADC_REG_TRIG_SOFTWARE) \
  73. || ((__REG_TRIG_SOURCE__) == LL_ADC_REG_TRIG_EXT_TIM1_CH1) \
  74. || ((__REG_TRIG_SOURCE__) == LL_ADC_REG_TRIG_EXT_TIM1_CH2) \
  75. || ((__REG_TRIG_SOURCE__) == LL_ADC_REG_TRIG_EXT_TIM1_CH3) \
  76. || ((__REG_TRIG_SOURCE__) == LL_ADC_REG_TRIG_EXT_TIM2_CH2) \
  77. || ((__REG_TRIG_SOURCE__) == LL_ADC_REG_TRIG_EXT_TIM2_CH3) \
  78. || ((__REG_TRIG_SOURCE__) == LL_ADC_REG_TRIG_EXT_TIM2_CH4) \
  79. || ((__REG_TRIG_SOURCE__) == LL_ADC_REG_TRIG_EXT_TIM2_TRGO) \
  80. || ((__REG_TRIG_SOURCE__) == LL_ADC_REG_TRIG_EXT_TIM3_CH1) \
  81. || ((__REG_TRIG_SOURCE__) == LL_ADC_REG_TRIG_EXT_TIM3_TRGO) \
  82. || ((__REG_TRIG_SOURCE__) == LL_ADC_REG_TRIG_EXT_TIM4_CH4) \
  83. || ((__REG_TRIG_SOURCE__) == LL_ADC_REG_TRIG_EXT_TIM5_CH1) \
  84. || ((__REG_TRIG_SOURCE__) == LL_ADC_REG_TRIG_EXT_TIM5_CH2) \
  85. || ((__REG_TRIG_SOURCE__) == LL_ADC_REG_TRIG_EXT_TIM5_CH3) \
  86. || ((__REG_TRIG_SOURCE__) == LL_ADC_REG_TRIG_EXT_TIM8_CH1) \
  87. || ((__REG_TRIG_SOURCE__) == LL_ADC_REG_TRIG_EXT_TIM8_TRGO) \
  88. || ((__REG_TRIG_SOURCE__) == LL_ADC_REG_TRIG_EXT_EXTI_LINE11) \
  89. )
  90. #define IS_LL_ADC_REG_CONTINUOUS_MODE(__REG_CONTINUOUS_MODE__) \
  91. ( ((__REG_CONTINUOUS_MODE__) == LL_ADC_REG_CONV_SINGLE) \
  92. || ((__REG_CONTINUOUS_MODE__) == LL_ADC_REG_CONV_CONTINUOUS) \
  93. )
  94. #define IS_LL_ADC_REG_DMA_TRANSFER(__REG_DMA_TRANSFER__) \
  95. ( ((__REG_DMA_TRANSFER__) == LL_ADC_REG_DMA_TRANSFER_NONE) \
  96. || ((__REG_DMA_TRANSFER__) == LL_ADC_REG_DMA_TRANSFER_LIMITED) \
  97. || ((__REG_DMA_TRANSFER__) == LL_ADC_REG_DMA_TRANSFER_UNLIMITED) \
  98. )
  99. #define IS_LL_ADC_REG_FLAG_EOC_SELECTION(__REG_FLAG_EOC_SELECTION__) \
  100. ( ((__REG_FLAG_EOC_SELECTION__) == LL_ADC_REG_FLAG_EOC_SEQUENCE_CONV) \
  101. || ((__REG_FLAG_EOC_SELECTION__) == LL_ADC_REG_FLAG_EOC_UNITARY_CONV) \
  102. )
  103. #define IS_LL_ADC_REG_SEQ_SCAN_LENGTH(__REG_SEQ_SCAN_LENGTH__) \
  104. ( ((__REG_SEQ_SCAN_LENGTH__) == LL_ADC_REG_SEQ_SCAN_DISABLE) \
  105. || ((__REG_SEQ_SCAN_LENGTH__) == LL_ADC_REG_SEQ_SCAN_ENABLE_2RANKS) \
  106. || ((__REG_SEQ_SCAN_LENGTH__) == LL_ADC_REG_SEQ_SCAN_ENABLE_3RANKS) \
  107. || ((__REG_SEQ_SCAN_LENGTH__) == LL_ADC_REG_SEQ_SCAN_ENABLE_4RANKS) \
  108. || ((__REG_SEQ_SCAN_LENGTH__) == LL_ADC_REG_SEQ_SCAN_ENABLE_5RANKS) \
  109. || ((__REG_SEQ_SCAN_LENGTH__) == LL_ADC_REG_SEQ_SCAN_ENABLE_6RANKS) \
  110. || ((__REG_SEQ_SCAN_LENGTH__) == LL_ADC_REG_SEQ_SCAN_ENABLE_7RANKS) \
  111. || ((__REG_SEQ_SCAN_LENGTH__) == LL_ADC_REG_SEQ_SCAN_ENABLE_8RANKS) \
  112. || ((__REG_SEQ_SCAN_LENGTH__) == LL_ADC_REG_SEQ_SCAN_ENABLE_9RANKS) \
  113. || ((__REG_SEQ_SCAN_LENGTH__) == LL_ADC_REG_SEQ_SCAN_ENABLE_10RANKS) \
  114. || ((__REG_SEQ_SCAN_LENGTH__) == LL_ADC_REG_SEQ_SCAN_ENABLE_11RANKS) \
  115. || ((__REG_SEQ_SCAN_LENGTH__) == LL_ADC_REG_SEQ_SCAN_ENABLE_12RANKS) \
  116. || ((__REG_SEQ_SCAN_LENGTH__) == LL_ADC_REG_SEQ_SCAN_ENABLE_13RANKS) \
  117. || ((__REG_SEQ_SCAN_LENGTH__) == LL_ADC_REG_SEQ_SCAN_ENABLE_14RANKS) \
  118. || ((__REG_SEQ_SCAN_LENGTH__) == LL_ADC_REG_SEQ_SCAN_ENABLE_15RANKS) \
  119. || ((__REG_SEQ_SCAN_LENGTH__) == LL_ADC_REG_SEQ_SCAN_ENABLE_16RANKS) \
  120. )
  121. #define IS_LL_ADC_REG_SEQ_SCAN_DISCONT_MODE(__REG_SEQ_DISCONT_MODE__) \
  122. ( ((__REG_SEQ_DISCONT_MODE__) == LL_ADC_REG_SEQ_DISCONT_DISABLE) \
  123. || ((__REG_SEQ_DISCONT_MODE__) == LL_ADC_REG_SEQ_DISCONT_1RANK) \
  124. || ((__REG_SEQ_DISCONT_MODE__) == LL_ADC_REG_SEQ_DISCONT_2RANKS) \
  125. || ((__REG_SEQ_DISCONT_MODE__) == LL_ADC_REG_SEQ_DISCONT_3RANKS) \
  126. || ((__REG_SEQ_DISCONT_MODE__) == LL_ADC_REG_SEQ_DISCONT_4RANKS) \
  127. || ((__REG_SEQ_DISCONT_MODE__) == LL_ADC_REG_SEQ_DISCONT_5RANKS) \
  128. || ((__REG_SEQ_DISCONT_MODE__) == LL_ADC_REG_SEQ_DISCONT_6RANKS) \
  129. || ((__REG_SEQ_DISCONT_MODE__) == LL_ADC_REG_SEQ_DISCONT_7RANKS) \
  130. || ((__REG_SEQ_DISCONT_MODE__) == LL_ADC_REG_SEQ_DISCONT_8RANKS) \
  131. )
  132. /* Check of parameters for configuration of ADC hierarchical scope: */
  133. /* ADC group injected */
  134. #define IS_LL_ADC_INJ_TRIG_SOURCE(__INJ_TRIG_SOURCE__) \
  135. ( ((__INJ_TRIG_SOURCE__) == LL_ADC_INJ_TRIG_SOFTWARE) \
  136. || ((__INJ_TRIG_SOURCE__) == LL_ADC_INJ_TRIG_EXT_TIM1_CH4) \
  137. || ((__INJ_TRIG_SOURCE__) == LL_ADC_INJ_TRIG_EXT_TIM1_TRGO) \
  138. || ((__INJ_TRIG_SOURCE__) == LL_ADC_INJ_TRIG_EXT_TIM2_CH1) \
  139. || ((__INJ_TRIG_SOURCE__) == LL_ADC_INJ_TRIG_EXT_TIM2_TRGO) \
  140. || ((__INJ_TRIG_SOURCE__) == LL_ADC_INJ_TRIG_EXT_TIM3_CH2) \
  141. || ((__INJ_TRIG_SOURCE__) == LL_ADC_INJ_TRIG_EXT_TIM3_CH4) \
  142. || ((__INJ_TRIG_SOURCE__) == LL_ADC_INJ_TRIG_EXT_TIM4_CH1) \
  143. || ((__INJ_TRIG_SOURCE__) == LL_ADC_INJ_TRIG_EXT_TIM4_CH2) \
  144. || ((__INJ_TRIG_SOURCE__) == LL_ADC_INJ_TRIG_EXT_TIM4_CH3) \
  145. || ((__INJ_TRIG_SOURCE__) == LL_ADC_INJ_TRIG_EXT_TIM4_TRGO) \
  146. || ((__INJ_TRIG_SOURCE__) == LL_ADC_INJ_TRIG_EXT_TIM5_CH4) \
  147. || ((__INJ_TRIG_SOURCE__) == LL_ADC_INJ_TRIG_EXT_TIM5_TRGO) \
  148. || ((__INJ_TRIG_SOURCE__) == LL_ADC_INJ_TRIG_EXT_TIM8_CH2) \
  149. || ((__INJ_TRIG_SOURCE__) == LL_ADC_INJ_TRIG_EXT_TIM8_CH3) \
  150. || ((__INJ_TRIG_SOURCE__) == LL_ADC_INJ_TRIG_EXT_TIM8_CH4) \
  151. || ((__INJ_TRIG_SOURCE__) == LL_ADC_INJ_TRIG_EXT_EXTI_LINE15) \
  152. )
  153. #define IS_LL_ADC_INJ_TRIG_EXT_EDGE(__INJ_TRIG_EXT_EDGE__) \
  154. ( ((__INJ_TRIG_EXT_EDGE__) == LL_ADC_INJ_TRIG_EXT_RISING) \
  155. || ((__INJ_TRIG_EXT_EDGE__) == LL_ADC_INJ_TRIG_EXT_FALLING) \
  156. || ((__INJ_TRIG_EXT_EDGE__) == LL_ADC_INJ_TRIG_EXT_RISINGFALLING) \
  157. )
  158. #define IS_LL_ADC_INJ_TRIG_AUTO(__INJ_TRIG_AUTO__) \
  159. ( ((__INJ_TRIG_AUTO__) == LL_ADC_INJ_TRIG_INDEPENDENT) \
  160. || ((__INJ_TRIG_AUTO__) == LL_ADC_INJ_TRIG_FROM_GRP_REGULAR) \
  161. )
  162. #define IS_LL_ADC_INJ_SEQ_SCAN_LENGTH(__INJ_SEQ_SCAN_LENGTH__) \
  163. ( ((__INJ_SEQ_SCAN_LENGTH__) == LL_ADC_INJ_SEQ_SCAN_DISABLE) \
  164. || ((__INJ_SEQ_SCAN_LENGTH__) == LL_ADC_INJ_SEQ_SCAN_ENABLE_2RANKS) \
  165. || ((__INJ_SEQ_SCAN_LENGTH__) == LL_ADC_INJ_SEQ_SCAN_ENABLE_3RANKS) \
  166. || ((__INJ_SEQ_SCAN_LENGTH__) == LL_ADC_INJ_SEQ_SCAN_ENABLE_4RANKS) \
  167. )
  168. #define IS_LL_ADC_INJ_SEQ_SCAN_DISCONT_MODE(__INJ_SEQ_DISCONT_MODE__) \
  169. ( ((__INJ_SEQ_DISCONT_MODE__) == LL_ADC_INJ_SEQ_DISCONT_DISABLE) \
  170. || ((__INJ_SEQ_DISCONT_MODE__) == LL_ADC_INJ_SEQ_DISCONT_1RANK) \
  171. )
  172. #if defined(ADC_MULTIMODE_SUPPORT)
  173. /* Check of parameters for configuration of ADC hierarchical scope: */
  174. /* multimode. */
  175. #if defined(ADC3)
  176. #define IS_LL_ADC_MULTI_MODE(__MULTI_MODE__) \
  177. ( ((__MULTI_MODE__) == LL_ADC_MULTI_INDEPENDENT) \
  178. || ((__MULTI_MODE__) == LL_ADC_MULTI_DUAL_REG_SIMULT) \
  179. || ((__MULTI_MODE__) == LL_ADC_MULTI_DUAL_REG_INTERL) \
  180. || ((__MULTI_MODE__) == LL_ADC_MULTI_DUAL_INJ_SIMULT) \
  181. || ((__MULTI_MODE__) == LL_ADC_MULTI_DUAL_INJ_ALTERN) \
  182. || ((__MULTI_MODE__) == LL_ADC_MULTI_DUAL_REG_SIM_INJ_SIM) \
  183. || ((__MULTI_MODE__) == LL_ADC_MULTI_DUAL_REG_SIM_INJ_ALT) \
  184. || ((__MULTI_MODE__) == LL_ADC_MULTI_DUAL_REG_INT_INJ_SIM) \
  185. || ((__MULTI_MODE__) == LL_ADC_MULTI_TRIPLE_REG_SIM_INJ_SIM) \
  186. || ((__MULTI_MODE__) == LL_ADC_MULTI_TRIPLE_REG_SIM_INJ_ALT) \
  187. || ((__MULTI_MODE__) == LL_ADC_MULTI_TRIPLE_INJ_SIMULT) \
  188. || ((__MULTI_MODE__) == LL_ADC_MULTI_TRIPLE_REG_SIMULT) \
  189. || ((__MULTI_MODE__) == LL_ADC_MULTI_TRIPLE_REG_INTERL) \
  190. || ((__MULTI_MODE__) == LL_ADC_MULTI_TRIPLE_INJ_ALTERN) \
  191. )
  192. #else
  193. #define IS_LL_ADC_MULTI_MODE(__MULTI_MODE__) \
  194. ( ((__MULTI_MODE__) == LL_ADC_MULTI_INDEPENDENT) \
  195. || ((__MULTI_MODE__) == LL_ADC_MULTI_DUAL_REG_SIMULT) \
  196. || ((__MULTI_MODE__) == LL_ADC_MULTI_DUAL_REG_INTERL) \
  197. || ((__MULTI_MODE__) == LL_ADC_MULTI_DUAL_INJ_SIMULT) \
  198. || ((__MULTI_MODE__) == LL_ADC_MULTI_DUAL_INJ_ALTERN) \
  199. || ((__MULTI_MODE__) == LL_ADC_MULTI_DUAL_REG_SIM_INJ_SIM) \
  200. || ((__MULTI_MODE__) == LL_ADC_MULTI_DUAL_REG_SIM_INJ_ALT) \
  201. || ((__MULTI_MODE__) == LL_ADC_MULTI_DUAL_REG_INT_INJ_SIM) \
  202. )
  203. #endif
  204. #define IS_LL_ADC_MULTI_DMA_TRANSFER(__MULTI_DMA_TRANSFER__) \
  205. ( ((__MULTI_DMA_TRANSFER__) == LL_ADC_MULTI_REG_DMA_EACH_ADC) \
  206. || ((__MULTI_DMA_TRANSFER__) == LL_ADC_MULTI_REG_DMA_LIMIT_1) \
  207. || ((__MULTI_DMA_TRANSFER__) == LL_ADC_MULTI_REG_DMA_LIMIT_2) \
  208. || ((__MULTI_DMA_TRANSFER__) == LL_ADC_MULTI_REG_DMA_LIMIT_3) \
  209. || ((__MULTI_DMA_TRANSFER__) == LL_ADC_MULTI_REG_DMA_UNLMT_1) \
  210. || ((__MULTI_DMA_TRANSFER__) == LL_ADC_MULTI_REG_DMA_UNLMT_2) \
  211. || ((__MULTI_DMA_TRANSFER__) == LL_ADC_MULTI_REG_DMA_UNLMT_3) \
  212. )
  213. #define IS_LL_ADC_MULTI_TWOSMP_DELAY(__MULTI_TWOSMP_DELAY__) \
  214. ( ((__MULTI_TWOSMP_DELAY__) == LL_ADC_MULTI_TWOSMP_DELAY_5CYCLES) \
  215. || ((__MULTI_TWOSMP_DELAY__) == LL_ADC_MULTI_TWOSMP_DELAY_6CYCLES) \
  216. || ((__MULTI_TWOSMP_DELAY__) == LL_ADC_MULTI_TWOSMP_DELAY_7CYCLES) \
  217. || ((__MULTI_TWOSMP_DELAY__) == LL_ADC_MULTI_TWOSMP_DELAY_8CYCLES) \
  218. || ((__MULTI_TWOSMP_DELAY__) == LL_ADC_MULTI_TWOSMP_DELAY_9CYCLES) \
  219. || ((__MULTI_TWOSMP_DELAY__) == LL_ADC_MULTI_TWOSMP_DELAY_10CYCLES) \
  220. || ((__MULTI_TWOSMP_DELAY__) == LL_ADC_MULTI_TWOSMP_DELAY_11CYCLES) \
  221. || ((__MULTI_TWOSMP_DELAY__) == LL_ADC_MULTI_TWOSMP_DELAY_12CYCLES) \
  222. || ((__MULTI_TWOSMP_DELAY__) == LL_ADC_MULTI_TWOSMP_DELAY_13CYCLES) \
  223. || ((__MULTI_TWOSMP_DELAY__) == LL_ADC_MULTI_TWOSMP_DELAY_14CYCLES) \
  224. || ((__MULTI_TWOSMP_DELAY__) == LL_ADC_MULTI_TWOSMP_DELAY_15CYCLES) \
  225. || ((__MULTI_TWOSMP_DELAY__) == LL_ADC_MULTI_TWOSMP_DELAY_16CYCLES) \
  226. || ((__MULTI_TWOSMP_DELAY__) == LL_ADC_MULTI_TWOSMP_DELAY_17CYCLES) \
  227. || ((__MULTI_TWOSMP_DELAY__) == LL_ADC_MULTI_TWOSMP_DELAY_18CYCLES) \
  228. || ((__MULTI_TWOSMP_DELAY__) == LL_ADC_MULTI_TWOSMP_DELAY_19CYCLES) \
  229. || ((__MULTI_TWOSMP_DELAY__) == LL_ADC_MULTI_TWOSMP_DELAY_20CYCLES) \
  230. )
  231. #define IS_LL_ADC_MULTI_MASTER_SLAVE(__MULTI_MASTER_SLAVE__) \
  232. ( ((__MULTI_MASTER_SLAVE__) == LL_ADC_MULTI_MASTER) \
  233. || ((__MULTI_MASTER_SLAVE__) == LL_ADC_MULTI_SLAVE) \
  234. || ((__MULTI_MASTER_SLAVE__) == LL_ADC_MULTI_MASTER_SLAVE) \
  235. )
  236. #endif /* ADC_MULTIMODE_SUPPORT */
  237. /**
  238. * @}
  239. */
  240. /* Private function prototypes -----------------------------------------------*/
  241. /* Exported functions --------------------------------------------------------*/
  242. /** @addtogroup ADC_LL_Exported_Functions
  243. * @{
  244. */
  245. /** @addtogroup ADC_LL_EF_Init
  246. * @{
  247. */
  248. /**
  249. * @brief De-initialize registers of all ADC instances belonging to
  250. * the same ADC common instance to their default reset values.
  251. * @param ADCxy_COMMON ADC common instance
  252. * (can be set directly from CMSIS definition or by using helper macro @ref __LL_ADC_COMMON_INSTANCE() )
  253. * @retval An ErrorStatus enumeration value:
  254. * - SUCCESS: ADC common registers are de-initialized
  255. * - ERROR: not applicable
  256. */
  257. ErrorStatus LL_ADC_CommonDeInit(ADC_Common_TypeDef *ADCxy_COMMON)
  258. {
  259. /* Check the parameters */
  260. assert_param(IS_ADC_COMMON_INSTANCE(ADCxy_COMMON));
  261. /* Force reset of ADC clock (core clock) */
  262. LL_APB2_GRP1_ForceReset(LL_APB2_GRP1_PERIPH_ADC);
  263. /* Release reset of ADC clock (core clock) */
  264. LL_APB2_GRP1_ReleaseReset(LL_APB2_GRP1_PERIPH_ADC);
  265. return SUCCESS;
  266. }
  267. /**
  268. * @brief Initialize some features of ADC common parameters
  269. * (all ADC instances belonging to the same ADC common instance)
  270. * and multimode (for devices with several ADC instances available).
  271. * @note The setting of ADC common parameters is conditioned to
  272. * ADC instances state:
  273. * All ADC instances belonging to the same ADC common instance
  274. * must be disabled.
  275. * @param ADCxy_COMMON ADC common instance
  276. * (can be set directly from CMSIS definition or by using helper macro @ref __LL_ADC_COMMON_INSTANCE() )
  277. * @param ADC_CommonInitStruct Pointer to a @ref LL_ADC_CommonInitTypeDef structure
  278. * @retval An ErrorStatus enumeration value:
  279. * - SUCCESS: ADC common registers are initialized
  280. * - ERROR: ADC common registers are not initialized
  281. */
  282. ErrorStatus LL_ADC_CommonInit(ADC_Common_TypeDef *ADCxy_COMMON, LL_ADC_CommonInitTypeDef *ADC_CommonInitStruct)
  283. {
  284. ErrorStatus status = SUCCESS;
  285. /* Check the parameters */
  286. assert_param(IS_ADC_COMMON_INSTANCE(ADCxy_COMMON));
  287. assert_param(IS_LL_ADC_COMMON_CLOCK(ADC_CommonInitStruct->CommonClock));
  288. #if defined(ADC_MULTIMODE_SUPPORT)
  289. assert_param(IS_LL_ADC_MULTI_MODE(ADC_CommonInitStruct->Multimode));
  290. if (ADC_CommonInitStruct->Multimode != LL_ADC_MULTI_INDEPENDENT)
  291. {
  292. assert_param(IS_LL_ADC_MULTI_DMA_TRANSFER(ADC_CommonInitStruct->MultiDMATransfer));
  293. assert_param(IS_LL_ADC_MULTI_TWOSMP_DELAY(ADC_CommonInitStruct->MultiTwoSamplingDelay));
  294. }
  295. #endif /* ADC_MULTIMODE_SUPPORT */
  296. /* Note: Hardware constraint (refer to description of functions */
  297. /* "LL_ADC_SetCommonXXX()" and "LL_ADC_SetMultiXXX()"): */
  298. /* On this STM32 series, setting of these features is conditioned to */
  299. /* ADC state: */
  300. /* All ADC instances of the ADC common group must be disabled. */
  301. if (__LL_ADC_IS_ENABLED_ALL_COMMON_INSTANCE(ADCxy_COMMON) == 0UL)
  302. {
  303. /* Configuration of ADC hierarchical scope: */
  304. /* - common to several ADC */
  305. /* (all ADC instances belonging to the same ADC common instance) */
  306. /* - Set ADC clock (conversion clock) */
  307. /* - multimode (if several ADC instances available on the */
  308. /* selected device) */
  309. /* - Set ADC multimode configuration */
  310. /* - Set ADC multimode DMA transfer */
  311. /* - Set ADC multimode: delay between 2 sampling phases */
  312. #if defined(ADC_MULTIMODE_SUPPORT)
  313. if (ADC_CommonInitStruct->Multimode != LL_ADC_MULTI_INDEPENDENT)
  314. {
  315. MODIFY_REG(ADCxy_COMMON->CCR,
  316. ADC_CCR_ADCPRE
  317. | ADC_CCR_MULTI
  318. | ADC_CCR_DMA
  319. | ADC_CCR_DDS
  320. | ADC_CCR_DELAY
  321. ,
  322. ADC_CommonInitStruct->CommonClock
  323. | ADC_CommonInitStruct->Multimode
  324. | ADC_CommonInitStruct->MultiDMATransfer
  325. | ADC_CommonInitStruct->MultiTwoSamplingDelay
  326. );
  327. }
  328. else
  329. {
  330. MODIFY_REG(ADCxy_COMMON->CCR,
  331. ADC_CCR_ADCPRE
  332. | ADC_CCR_MULTI
  333. | ADC_CCR_DMA
  334. | ADC_CCR_DDS
  335. | ADC_CCR_DELAY
  336. ,
  337. ADC_CommonInitStruct->CommonClock
  338. | LL_ADC_MULTI_INDEPENDENT
  339. );
  340. }
  341. #else
  342. LL_ADC_SetCommonClock(ADCxy_COMMON, ADC_CommonInitStruct->CommonClock);
  343. #endif
  344. }
  345. else
  346. {
  347. /* Initialization error: One or several ADC instances belonging to */
  348. /* the same ADC common instance are not disabled. */
  349. status = ERROR;
  350. }
  351. return status;
  352. }
  353. /**
  354. * @brief Set each @ref LL_ADC_CommonInitTypeDef field to default value.
  355. * @param ADC_CommonInitStruct Pointer to a @ref LL_ADC_CommonInitTypeDef structure
  356. * whose fields will be set to default values.
  357. * @retval None
  358. */
  359. void LL_ADC_CommonStructInit(LL_ADC_CommonInitTypeDef *ADC_CommonInitStruct)
  360. {
  361. /* Set ADC_CommonInitStruct fields to default values */
  362. /* Set fields of ADC common */
  363. /* (all ADC instances belonging to the same ADC common instance) */
  364. ADC_CommonInitStruct->CommonClock = LL_ADC_CLOCK_SYNC_PCLK_DIV2;
  365. #if defined(ADC_MULTIMODE_SUPPORT)
  366. /* Set fields of ADC multimode */
  367. ADC_CommonInitStruct->Multimode = LL_ADC_MULTI_INDEPENDENT;
  368. ADC_CommonInitStruct->MultiDMATransfer = LL_ADC_MULTI_REG_DMA_EACH_ADC;
  369. ADC_CommonInitStruct->MultiTwoSamplingDelay = LL_ADC_MULTI_TWOSMP_DELAY_5CYCLES;
  370. #endif /* ADC_MULTIMODE_SUPPORT */
  371. }
  372. /**
  373. * @brief De-initialize registers of the selected ADC instance
  374. * to their default reset values.
  375. * @note To reset all ADC instances quickly (perform a hard reset),
  376. * use function @ref LL_ADC_CommonDeInit().
  377. * @param ADCx ADC instance
  378. * @retval An ErrorStatus enumeration value:
  379. * - SUCCESS: ADC registers are de-initialized
  380. * - ERROR: ADC registers are not de-initialized
  381. */
  382. ErrorStatus LL_ADC_DeInit(ADC_TypeDef *ADCx)
  383. {
  384. ErrorStatus status = SUCCESS;
  385. /* Check the parameters */
  386. assert_param(IS_ADC_ALL_INSTANCE(ADCx));
  387. /* Disable ADC instance if not already disabled. */
  388. if (LL_ADC_IsEnabled(ADCx) == 1UL)
  389. {
  390. /* Set ADC group regular trigger source to SW start to ensure to not */
  391. /* have an external trigger event occurring during the conversion stop */
  392. /* ADC disable process. */
  393. LL_ADC_REG_SetTriggerSource(ADCx, LL_ADC_REG_TRIG_SOFTWARE);
  394. /* Set ADC group injected trigger source to SW start to ensure to not */
  395. /* have an external trigger event occurring during the conversion stop */
  396. /* ADC disable process. */
  397. LL_ADC_INJ_SetTriggerSource(ADCx, LL_ADC_INJ_TRIG_SOFTWARE);
  398. /* Disable the ADC instance */
  399. LL_ADC_Disable(ADCx);
  400. }
  401. /* Check whether ADC state is compliant with expected state */
  402. /* (hardware requirements of bits state to reset registers below) */
  403. if (READ_BIT(ADCx->CR2, ADC_CR2_ADON) == 0UL)
  404. {
  405. /* ========== Reset ADC registers ========== */
  406. /* Reset register SR */
  407. CLEAR_BIT(ADCx->SR,
  408. (LL_ADC_FLAG_STRT
  409. | LL_ADC_FLAG_JSTRT
  410. | LL_ADC_FLAG_EOCS
  411. | LL_ADC_FLAG_OVR
  412. | LL_ADC_FLAG_JEOS
  413. | LL_ADC_FLAG_AWD1)
  414. );
  415. /* Reset register CR1 */
  416. CLEAR_BIT(ADCx->CR1,
  417. (ADC_CR1_OVRIE | ADC_CR1_RES | ADC_CR1_AWDEN
  418. | ADC_CR1_JAWDEN
  419. | ADC_CR1_DISCNUM | ADC_CR1_JDISCEN | ADC_CR1_DISCEN
  420. | ADC_CR1_JAUTO | ADC_CR1_AWDSGL | ADC_CR1_SCAN
  421. | ADC_CR1_JEOCIE | ADC_CR1_AWDIE | ADC_CR1_EOCIE
  422. | ADC_CR1_AWDCH)
  423. );
  424. /* Reset register CR2 */
  425. CLEAR_BIT(ADCx->CR2,
  426. (ADC_CR2_SWSTART | ADC_CR2_EXTEN | ADC_CR2_EXTSEL
  427. | ADC_CR2_JSWSTART | ADC_CR2_JEXTEN | ADC_CR2_JEXTSEL
  428. | ADC_CR2_ALIGN | ADC_CR2_EOCS
  429. | ADC_CR2_DDS | ADC_CR2_DMA
  430. | ADC_CR2_CONT | ADC_CR2_ADON)
  431. );
  432. /* Reset register SMPR1 */
  433. CLEAR_BIT(ADCx->SMPR1,
  434. (ADC_SMPR1_SMP18 | ADC_SMPR1_SMP17 | ADC_SMPR1_SMP16
  435. | ADC_SMPR1_SMP15 | ADC_SMPR1_SMP14 | ADC_SMPR1_SMP13
  436. | ADC_SMPR1_SMP12 | ADC_SMPR1_SMP11 | ADC_SMPR1_SMP10)
  437. );
  438. /* Reset register SMPR2 */
  439. CLEAR_BIT(ADCx->SMPR2,
  440. (ADC_SMPR2_SMP9
  441. | ADC_SMPR2_SMP8 | ADC_SMPR2_SMP7 | ADC_SMPR2_SMP6
  442. | ADC_SMPR2_SMP5 | ADC_SMPR2_SMP4 | ADC_SMPR2_SMP3
  443. | ADC_SMPR2_SMP2 | ADC_SMPR2_SMP1 | ADC_SMPR2_SMP0)
  444. );
  445. /* Reset register JOFR1 */
  446. CLEAR_BIT(ADCx->JOFR1, ADC_JOFR1_JOFFSET1);
  447. /* Reset register JOFR2 */
  448. CLEAR_BIT(ADCx->JOFR2, ADC_JOFR2_JOFFSET2);
  449. /* Reset register JOFR3 */
  450. CLEAR_BIT(ADCx->JOFR3, ADC_JOFR3_JOFFSET3);
  451. /* Reset register JOFR4 */
  452. CLEAR_BIT(ADCx->JOFR4, ADC_JOFR4_JOFFSET4);
  453. /* Reset register HTR */
  454. SET_BIT(ADCx->HTR, ADC_HTR_HT);
  455. /* Reset register LTR */
  456. CLEAR_BIT(ADCx->LTR, ADC_LTR_LT);
  457. /* Reset register SQR1 */
  458. CLEAR_BIT(ADCx->SQR1,
  459. (ADC_SQR1_L
  460. | ADC_SQR1_SQ16
  461. | ADC_SQR1_SQ15 | ADC_SQR1_SQ14 | ADC_SQR1_SQ13)
  462. );
  463. /* Reset register SQR2 */
  464. CLEAR_BIT(ADCx->SQR2,
  465. (ADC_SQR2_SQ12 | ADC_SQR2_SQ11 | ADC_SQR2_SQ10
  466. | ADC_SQR2_SQ9 | ADC_SQR2_SQ8 | ADC_SQR2_SQ7)
  467. );
  468. /* Reset register SQR3 */
  469. CLEAR_BIT(ADCx->SQR3,
  470. (ADC_SQR3_SQ6 | ADC_SQR3_SQ5 | ADC_SQR3_SQ4
  471. | ADC_SQR3_SQ3 | ADC_SQR3_SQ2 | ADC_SQR3_SQ1)
  472. );
  473. /* Reset register JSQR */
  474. CLEAR_BIT(ADCx->JSQR,
  475. (ADC_JSQR_JL
  476. | ADC_JSQR_JSQ4 | ADC_JSQR_JSQ3
  477. | ADC_JSQR_JSQ2 | ADC_JSQR_JSQ1)
  478. );
  479. /* Reset register DR */
  480. /* bits in access mode read only, no direct reset applicable */
  481. /* Reset registers JDR1, JDR2, JDR3, JDR4 */
  482. /* bits in access mode read only, no direct reset applicable */
  483. /* Reset register CCR */
  484. CLEAR_BIT(ADC->CCR, ADC_CCR_TSVREFE | ADC_CCR_ADCPRE);
  485. }
  486. return status;
  487. }
  488. /**
  489. * @brief Initialize some features of ADC instance.
  490. * @note These parameters have an impact on ADC scope: ADC instance.
  491. * Affects both group regular and group injected (availability
  492. * of ADC group injected depends on STM32 families).
  493. * Refer to corresponding unitary functions into
  494. * @ref ADC_LL_EF_Configuration_ADC_Instance .
  495. * @note The setting of these parameters by function @ref LL_ADC_Init()
  496. * is conditioned to ADC state:
  497. * ADC instance must be disabled.
  498. * This condition is applied to all ADC features, for efficiency
  499. * and compatibility over all STM32 families. However, the different
  500. * features can be set under different ADC state conditions
  501. * (setting possible with ADC enabled without conversion on going,
  502. * ADC enabled with conversion on going, ...)
  503. * Each feature can be updated afterwards with a unitary function
  504. * and potentially with ADC in a different state than disabled,
  505. * refer to description of each function for setting
  506. * conditioned to ADC state.
  507. * @note After using this function, some other features must be configured
  508. * using LL unitary functions.
  509. * The minimum configuration remaining to be done is:
  510. * - Set ADC group regular or group injected sequencer:
  511. * map channel on the selected sequencer rank.
  512. * Refer to function @ref LL_ADC_REG_SetSequencerRanks().
  513. * - Set ADC channel sampling time
  514. * Refer to function LL_ADC_SetChannelSamplingTime();
  515. * @param ADCx ADC instance
  516. * @param ADC_InitStruct Pointer to a @ref LL_ADC_REG_InitTypeDef structure
  517. * @retval An ErrorStatus enumeration value:
  518. * - SUCCESS: ADC registers are initialized
  519. * - ERROR: ADC registers are not initialized
  520. */
  521. ErrorStatus LL_ADC_Init(ADC_TypeDef *ADCx, LL_ADC_InitTypeDef *ADC_InitStruct)
  522. {
  523. ErrorStatus status = SUCCESS;
  524. /* Check the parameters */
  525. assert_param(IS_ADC_ALL_INSTANCE(ADCx));
  526. assert_param(IS_LL_ADC_RESOLUTION(ADC_InitStruct->Resolution));
  527. assert_param(IS_LL_ADC_DATA_ALIGN(ADC_InitStruct->DataAlignment));
  528. assert_param(IS_LL_ADC_SCAN_SELECTION(ADC_InitStruct->SequencersScanMode));
  529. /* Note: Hardware constraint (refer to description of this function): */
  530. /* ADC instance must be disabled. */
  531. if (LL_ADC_IsEnabled(ADCx) == 0UL)
  532. {
  533. /* Configuration of ADC hierarchical scope: */
  534. /* - ADC instance */
  535. /* - Set ADC data resolution */
  536. /* - Set ADC conversion data alignment */
  537. MODIFY_REG(ADCx->CR1,
  538. ADC_CR1_RES
  539. | ADC_CR1_SCAN
  540. ,
  541. ADC_InitStruct->Resolution
  542. | ADC_InitStruct->SequencersScanMode
  543. );
  544. MODIFY_REG(ADCx->CR2,
  545. ADC_CR2_ALIGN
  546. ,
  547. ADC_InitStruct->DataAlignment
  548. );
  549. }
  550. else
  551. {
  552. /* Initialization error: ADC instance is not disabled. */
  553. status = ERROR;
  554. }
  555. return status;
  556. }
  557. /**
  558. * @brief Set each @ref LL_ADC_InitTypeDef field to default value.
  559. * @param ADC_InitStruct Pointer to a @ref LL_ADC_InitTypeDef structure
  560. * whose fields will be set to default values.
  561. * @retval None
  562. */
  563. void LL_ADC_StructInit(LL_ADC_InitTypeDef *ADC_InitStruct)
  564. {
  565. /* Set ADC_InitStruct fields to default values */
  566. /* Set fields of ADC instance */
  567. ADC_InitStruct->Resolution = LL_ADC_RESOLUTION_12B;
  568. ADC_InitStruct->DataAlignment = LL_ADC_DATA_ALIGN_RIGHT;
  569. /* Enable scan mode to have a generic behavior with ADC of other */
  570. /* STM32 families, without this setting available: */
  571. /* ADC group regular sequencer and ADC group injected sequencer depend */
  572. /* only of their own configuration. */
  573. ADC_InitStruct->SequencersScanMode = LL_ADC_SEQ_SCAN_ENABLE;
  574. }
  575. /**
  576. * @brief Initialize some features of ADC group regular.
  577. * @note These parameters have an impact on ADC scope: ADC group regular.
  578. * Refer to corresponding unitary functions into
  579. * @ref ADC_LL_EF_Configuration_ADC_Group_Regular
  580. * (functions with prefix "REG").
  581. * @note The setting of these parameters by function @ref LL_ADC_Init()
  582. * is conditioned to ADC state:
  583. * ADC instance must be disabled.
  584. * This condition is applied to all ADC features, for efficiency
  585. * and compatibility over all STM32 families. However, the different
  586. * features can be set under different ADC state conditions
  587. * (setting possible with ADC enabled without conversion on going,
  588. * ADC enabled with conversion on going, ...)
  589. * Each feature can be updated afterwards with a unitary function
  590. * and potentially with ADC in a different state than disabled,
  591. * refer to description of each function for setting
  592. * conditioned to ADC state.
  593. * @note After using this function, other features must be configured
  594. * using LL unitary functions.
  595. * The minimum configuration remaining to be done is:
  596. * - Set ADC group regular or group injected sequencer:
  597. * map channel on the selected sequencer rank.
  598. * Refer to function @ref LL_ADC_REG_SetSequencerRanks().
  599. * - Set ADC channel sampling time
  600. * Refer to function LL_ADC_SetChannelSamplingTime();
  601. * @param ADCx ADC instance
  602. * @param ADC_REG_InitStruct Pointer to a @ref LL_ADC_REG_InitTypeDef structure
  603. * @retval An ErrorStatus enumeration value:
  604. * - SUCCESS: ADC registers are initialized
  605. * - ERROR: ADC registers are not initialized
  606. */
  607. ErrorStatus LL_ADC_REG_Init(ADC_TypeDef *ADCx, LL_ADC_REG_InitTypeDef *ADC_REG_InitStruct)
  608. {
  609. ErrorStatus status = SUCCESS;
  610. /* Check the parameters */
  611. assert_param(IS_ADC_ALL_INSTANCE(ADCx));
  612. assert_param(IS_LL_ADC_REG_TRIG_SOURCE(ADC_REG_InitStruct->TriggerSource));
  613. assert_param(IS_LL_ADC_REG_SEQ_SCAN_LENGTH(ADC_REG_InitStruct->SequencerLength));
  614. if (ADC_REG_InitStruct->SequencerLength != LL_ADC_REG_SEQ_SCAN_DISABLE)
  615. {
  616. assert_param(IS_LL_ADC_REG_SEQ_SCAN_DISCONT_MODE(ADC_REG_InitStruct->SequencerDiscont));
  617. }
  618. assert_param(IS_LL_ADC_REG_CONTINUOUS_MODE(ADC_REG_InitStruct->ContinuousMode));
  619. assert_param(IS_LL_ADC_REG_DMA_TRANSFER(ADC_REG_InitStruct->DMATransfer));
  620. /* ADC group regular continuous mode and discontinuous mode */
  621. /* can not be enabled simultenaeously */
  622. assert_param((ADC_REG_InitStruct->ContinuousMode == LL_ADC_REG_CONV_SINGLE)
  623. || (ADC_REG_InitStruct->SequencerDiscont == LL_ADC_REG_SEQ_DISCONT_DISABLE));
  624. /* Note: Hardware constraint (refer to description of this function): */
  625. /* ADC instance must be disabled. */
  626. if (LL_ADC_IsEnabled(ADCx) == 0UL)
  627. {
  628. /* Configuration of ADC hierarchical scope: */
  629. /* - ADC group regular */
  630. /* - Set ADC group regular trigger source */
  631. /* - Set ADC group regular sequencer length */
  632. /* - Set ADC group regular sequencer discontinuous mode */
  633. /* - Set ADC group regular continuous mode */
  634. /* - Set ADC group regular conversion data transfer: no transfer or */
  635. /* transfer by DMA, and DMA requests mode */
  636. /* Note: On this STM32 series, ADC trigger edge is set when starting */
  637. /* ADC conversion. */
  638. /* Refer to function @ref LL_ADC_REG_StartConversionExtTrig(). */
  639. if (ADC_REG_InitStruct->SequencerLength != LL_ADC_REG_SEQ_SCAN_DISABLE)
  640. {
  641. MODIFY_REG(ADCx->CR1,
  642. ADC_CR1_DISCEN
  643. | ADC_CR1_DISCNUM
  644. ,
  645. ADC_REG_InitStruct->SequencerDiscont
  646. );
  647. }
  648. else
  649. {
  650. MODIFY_REG(ADCx->CR1,
  651. ADC_CR1_DISCEN
  652. | ADC_CR1_DISCNUM
  653. ,
  654. LL_ADC_REG_SEQ_DISCONT_DISABLE
  655. );
  656. }
  657. MODIFY_REG(ADCx->CR2,
  658. ADC_CR2_EXTSEL
  659. | ADC_CR2_EXTEN
  660. | ADC_CR2_CONT
  661. | ADC_CR2_DMA
  662. | ADC_CR2_DDS
  663. ,
  664. (ADC_REG_InitStruct->TriggerSource & ADC_CR2_EXTSEL)
  665. | ADC_REG_InitStruct->ContinuousMode
  666. | ADC_REG_InitStruct->DMATransfer
  667. );
  668. /* Set ADC group regular sequencer length and scan direction */
  669. /* Note: Hardware constraint (refer to description of this function): */
  670. /* Note: If ADC instance feature scan mode is disabled */
  671. /* (refer to ADC instance initialization structure */
  672. /* parameter @ref SequencersScanMode */
  673. /* or function @ref LL_ADC_SetSequencersScanMode() ), */
  674. /* this parameter is discarded. */
  675. LL_ADC_REG_SetSequencerLength(ADCx, ADC_REG_InitStruct->SequencerLength);
  676. }
  677. else
  678. {
  679. /* Initialization error: ADC instance is not disabled. */
  680. status = ERROR;
  681. }
  682. return status;
  683. }
  684. /**
  685. * @brief Set each @ref LL_ADC_REG_InitTypeDef field to default value.
  686. * @param ADC_REG_InitStruct Pointer to a @ref LL_ADC_REG_InitTypeDef structure
  687. * whose fields will be set to default values.
  688. * @retval None
  689. */
  690. void LL_ADC_REG_StructInit(LL_ADC_REG_InitTypeDef *ADC_REG_InitStruct)
  691. {
  692. /* Set ADC_REG_InitStruct fields to default values */
  693. /* Set fields of ADC group regular */
  694. /* Note: On this STM32 series, ADC trigger edge is set when starting */
  695. /* ADC conversion. */
  696. /* Refer to function @ref LL_ADC_REG_StartConversionExtTrig(). */
  697. ADC_REG_InitStruct->TriggerSource = LL_ADC_REG_TRIG_SOFTWARE;
  698. ADC_REG_InitStruct->SequencerLength = LL_ADC_REG_SEQ_SCAN_DISABLE;
  699. ADC_REG_InitStruct->SequencerDiscont = LL_ADC_REG_SEQ_DISCONT_DISABLE;
  700. ADC_REG_InitStruct->ContinuousMode = LL_ADC_REG_CONV_SINGLE;
  701. ADC_REG_InitStruct->DMATransfer = LL_ADC_REG_DMA_TRANSFER_NONE;
  702. }
  703. /**
  704. * @brief Initialize some features of ADC group injected.
  705. * @note These parameters have an impact on ADC scope: ADC group injected.
  706. * Refer to corresponding unitary functions into
  707. * @ref ADC_LL_EF_Configuration_ADC_Group_Regular
  708. * (functions with prefix "INJ").
  709. * @note The setting of these parameters by function @ref LL_ADC_Init()
  710. * is conditioned to ADC state:
  711. * ADC instance must be disabled.
  712. * This condition is applied to all ADC features, for efficiency
  713. * and compatibility over all STM32 families. However, the different
  714. * features can be set under different ADC state conditions
  715. * (setting possible with ADC enabled without conversion on going,
  716. * ADC enabled with conversion on going, ...)
  717. * Each feature can be updated afterwards with a unitary function
  718. * and potentially with ADC in a different state than disabled,
  719. * refer to description of each function for setting
  720. * conditioned to ADC state.
  721. * @note After using this function, other features must be configured
  722. * using LL unitary functions.
  723. * The minimum configuration remaining to be done is:
  724. * - Set ADC group injected sequencer:
  725. * map channel on the selected sequencer rank.
  726. * Refer to function @ref LL_ADC_INJ_SetSequencerRanks().
  727. * - Set ADC channel sampling time
  728. * Refer to function LL_ADC_SetChannelSamplingTime();
  729. * @param ADCx ADC instance
  730. * @param ADC_INJ_InitStruct Pointer to a @ref LL_ADC_INJ_InitTypeDef structure
  731. * @retval An ErrorStatus enumeration value:
  732. * - SUCCESS: ADC registers are initialized
  733. * - ERROR: ADC registers are not initialized
  734. */
  735. ErrorStatus LL_ADC_INJ_Init(ADC_TypeDef *ADCx, LL_ADC_INJ_InitTypeDef *ADC_INJ_InitStruct)
  736. {
  737. ErrorStatus status = SUCCESS;
  738. /* Check the parameters */
  739. assert_param(IS_ADC_ALL_INSTANCE(ADCx));
  740. assert_param(IS_LL_ADC_INJ_TRIG_SOURCE(ADC_INJ_InitStruct->TriggerSource));
  741. assert_param(IS_LL_ADC_INJ_SEQ_SCAN_LENGTH(ADC_INJ_InitStruct->SequencerLength));
  742. if (ADC_INJ_InitStruct->SequencerLength != LL_ADC_INJ_SEQ_SCAN_DISABLE)
  743. {
  744. assert_param(IS_LL_ADC_INJ_SEQ_SCAN_DISCONT_MODE(ADC_INJ_InitStruct->SequencerDiscont));
  745. }
  746. assert_param(IS_LL_ADC_INJ_TRIG_AUTO(ADC_INJ_InitStruct->TrigAuto));
  747. /* Note: Hardware constraint (refer to description of this function): */
  748. /* ADC instance must be disabled. */
  749. if (LL_ADC_IsEnabled(ADCx) == 0UL)
  750. {
  751. /* Configuration of ADC hierarchical scope: */
  752. /* - ADC group injected */
  753. /* - Set ADC group injected trigger source */
  754. /* - Set ADC group injected sequencer length */
  755. /* - Set ADC group injected sequencer discontinuous mode */
  756. /* - Set ADC group injected conversion trigger: independent or */
  757. /* from ADC group regular */
  758. /* Note: On this STM32 series, ADC trigger edge is set when starting */
  759. /* ADC conversion. */
  760. /* Refer to function @ref LL_ADC_INJ_StartConversionExtTrig(). */
  761. if (ADC_INJ_InitStruct->SequencerLength != LL_ADC_REG_SEQ_SCAN_DISABLE)
  762. {
  763. MODIFY_REG(ADCx->CR1,
  764. ADC_CR1_JDISCEN
  765. | ADC_CR1_JAUTO
  766. ,
  767. ADC_INJ_InitStruct->SequencerDiscont
  768. | ADC_INJ_InitStruct->TrigAuto
  769. );
  770. }
  771. else
  772. {
  773. MODIFY_REG(ADCx->CR1,
  774. ADC_CR1_JDISCEN
  775. | ADC_CR1_JAUTO
  776. ,
  777. LL_ADC_REG_SEQ_DISCONT_DISABLE
  778. | ADC_INJ_InitStruct->TrigAuto
  779. );
  780. }
  781. MODIFY_REG(ADCx->CR2,
  782. ADC_CR2_JEXTSEL
  783. | ADC_CR2_JEXTEN
  784. ,
  785. (ADC_INJ_InitStruct->TriggerSource & ADC_CR2_JEXTSEL)
  786. );
  787. /* Note: Hardware constraint (refer to description of this function): */
  788. /* Note: If ADC instance feature scan mode is disabled */
  789. /* (refer to ADC instance initialization structure */
  790. /* parameter @ref SequencersScanMode */
  791. /* or function @ref LL_ADC_SetSequencersScanMode() ), */
  792. /* this parameter is discarded. */
  793. LL_ADC_INJ_SetSequencerLength(ADCx, ADC_INJ_InitStruct->SequencerLength);
  794. }
  795. else
  796. {
  797. /* Initialization error: ADC instance is not disabled. */
  798. status = ERROR;
  799. }
  800. return status;
  801. }
  802. /**
  803. * @brief Set each @ref LL_ADC_INJ_InitTypeDef field to default value.
  804. * @param ADC_INJ_InitStruct Pointer to a @ref LL_ADC_INJ_InitTypeDef structure
  805. * whose fields will be set to default values.
  806. * @retval None
  807. */
  808. void LL_ADC_INJ_StructInit(LL_ADC_INJ_InitTypeDef *ADC_INJ_InitStruct)
  809. {
  810. /* Set ADC_INJ_InitStruct fields to default values */
  811. /* Set fields of ADC group injected */
  812. ADC_INJ_InitStruct->TriggerSource = LL_ADC_INJ_TRIG_SOFTWARE;
  813. ADC_INJ_InitStruct->SequencerLength = LL_ADC_INJ_SEQ_SCAN_DISABLE;
  814. ADC_INJ_InitStruct->SequencerDiscont = LL_ADC_INJ_SEQ_DISCONT_DISABLE;
  815. ADC_INJ_InitStruct->TrigAuto = LL_ADC_INJ_TRIG_INDEPENDENT;
  816. }
  817. /**
  818. * @}
  819. */
  820. /**
  821. * @}
  822. */
  823. /**
  824. * @}
  825. */
  826. #endif /* ADC1 || ADC2 || ADC3 */
  827. /**
  828. * @}
  829. */
  830. #endif /* USE_FULL_LL_DRIVER */