stm32l1xx_ll_adc.c 40 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894
  1. /**
  2. ******************************************************************************
  3. * @file stm32l1xx_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 "stm32l1xx_ll_adc.h"
  38. #include "stm32l1xx_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 STM32L1xx_LL_Driver
  45. * @{
  46. */
  47. #if defined (ADC1)
  48. /** @addtogroup ADC_LL ADC
  49. * @{
  50. */
  51. /* Private types -------------------------------------------------------------*/
  52. /* Private variables ---------------------------------------------------------*/
  53. /* Private constants ---------------------------------------------------------*/
  54. /* Private macros ------------------------------------------------------------*/
  55. /** @addtogroup ADC_LL_Private_Macros
  56. * @{
  57. */
  58. /* Check of parameters for configuration of ADC hierarchical scope: */
  59. /* common to several ADC instances. */
  60. #define IS_LL_ADC_COMMON_CLOCK(__CLOCK__) \
  61. ( ((__CLOCK__) == LL_ADC_CLOCK_ASYNC_DIV1) \
  62. || ((__CLOCK__) == LL_ADC_CLOCK_ASYNC_DIV2) \
  63. || ((__CLOCK__) == LL_ADC_CLOCK_ASYNC_DIV4) \
  64. )
  65. /* Check of parameters for configuration of ADC hierarchical scope: */
  66. /* ADC instance. */
  67. #define IS_LL_ADC_RESOLUTION(__RESOLUTION__) \
  68. ( ((__RESOLUTION__) == LL_ADC_RESOLUTION_12B) \
  69. || ((__RESOLUTION__) == LL_ADC_RESOLUTION_10B) \
  70. || ((__RESOLUTION__) == LL_ADC_RESOLUTION_8B) \
  71. || ((__RESOLUTION__) == LL_ADC_RESOLUTION_6B) \
  72. )
  73. #define IS_LL_ADC_DATA_ALIGN(__DATA_ALIGN__) \
  74. ( ((__DATA_ALIGN__) == LL_ADC_DATA_ALIGN_RIGHT) \
  75. || ((__DATA_ALIGN__) == LL_ADC_DATA_ALIGN_LEFT) \
  76. )
  77. #define IS_LL_ADC_LOW_POWER_AUTOWAIT(__LOW_POWER__) \
  78. ( ((__LOW_POWER__) == LL_ADC_LP_AUTOWAIT_NONE) \
  79. || ((__LOW_POWER__) == LL_ADC_LP_AUTOWAIT) \
  80. || ((__LOW_POWER__) == LL_ADC_LP_AUTOWAIT_7_APBCLOCKCYCLES) \
  81. || ((__LOW_POWER__) == LL_ADC_LP_AUTOWAIT_15_APBCLOCKCYCLES) \
  82. || ((__LOW_POWER__) == LL_ADC_LP_AUTOWAIT_31_APBCLOCKCYCLES) \
  83. || ((__LOW_POWER__) == LL_ADC_LP_AUTOWAIT_63_APBCLOCKCYCLES) \
  84. || ((__LOW_POWER__) == LL_ADC_LP_AUTOWAIT_127_APBCLOCKCYCLES) \
  85. || ((__LOW_POWER__) == LL_ADC_LP_AUTOWAIT_255_APBCLOCKCYCLES) \
  86. )
  87. #define IS_LL_ADC_LOW_POWER_AUTOPOWEROFF(__LOW_POWER__) \
  88. ( ((__LOW_POWER__) == LL_ADC_LP_AUTOPOWEROFF_NONE) \
  89. || ((__LOW_POWER__) == LL_ADC_LP_AUTOPOWEROFF_IDLE_PHASE) \
  90. || ((__LOW_POWER__) == LL_ADC_LP_AUTOPOWEROFF_AUTOWAIT_PHASE) \
  91. || ((__LOW_POWER__) == LL_ADC_LP_AUTOPOWEROFF_IDLE_AUTOWAIT_PHASES) \
  92. )
  93. #define IS_LL_ADC_SCAN_SELECTION(__SCAN_SELECTION__) \
  94. ( ((__SCAN_SELECTION__) == LL_ADC_SEQ_SCAN_DISABLE) \
  95. || ((__SCAN_SELECTION__) == LL_ADC_SEQ_SCAN_ENABLE) \
  96. )
  97. #define IS_LL_ADC_SEQ_SCAN_MODE(__SEQ_SCAN_MODE__) \
  98. ( ((__SCAN_MODE__) == LL_ADC_SEQ_SCAN_DISABLE) \
  99. || ((__SCAN_MODE__) == LL_ADC_SEQ_SCAN_ENABLE) \
  100. )
  101. #define IS_LL_ADC_CHANNELS_BANK(__CHANNELS_BANK__) \
  102. ( ((__CHANNELS_BANK__) == LL_ADC_CHANNELS_BANK_A) \
  103. || ((__CHANNELS_BANK__) == LL_ADC_CHANNELS_BANK_B) \
  104. )
  105. /* Check of parameters for configuration of ADC hierarchical scope: */
  106. /* ADC group regular */
  107. #define IS_LL_ADC_REG_TRIG_SOURCE(__REG_TRIG_SOURCE__) \
  108. ( ((__REG_TRIG_SOURCE__) == LL_ADC_REG_TRIG_SOFTWARE) \
  109. || ((__REG_TRIG_SOURCE__) == LL_ADC_REG_TRIG_EXT_TIM2_TRGO) \
  110. || ((__REG_TRIG_SOURCE__) == LL_ADC_REG_TRIG_EXT_TIM2_CH3) \
  111. || ((__REG_TRIG_SOURCE__) == LL_ADC_REG_TRIG_EXT_TIM3_TRGO) \
  112. || ((__REG_TRIG_SOURCE__) == LL_ADC_REG_TRIG_EXT_TIM2_CH2) \
  113. || ((__REG_TRIG_SOURCE__) == LL_ADC_REG_TRIG_EXT_TIM3_CH1) \
  114. || ((__REG_TRIG_SOURCE__) == LL_ADC_REG_TRIG_EXT_TIM3_CH3) \
  115. || ((__REG_TRIG_SOURCE__) == LL_ADC_REG_TRIG_EXT_TIM4_TRGO) \
  116. || ((__REG_TRIG_SOURCE__) == LL_ADC_REG_TRIG_EXT_TIM4_CH4) \
  117. || ((__REG_TRIG_SOURCE__) == LL_ADC_REG_TRIG_EXT_TIM6_TRGO) \
  118. || ((__REG_TRIG_SOURCE__) == LL_ADC_REG_TRIG_EXT_TIM9_CH2) \
  119. || ((__REG_TRIG_SOURCE__) == LL_ADC_REG_TRIG_EXT_TIM9_TRGO) \
  120. || ((__REG_TRIG_SOURCE__) == LL_ADC_REG_TRIG_EXT_EXTI_LINE11) \
  121. )
  122. #define IS_LL_ADC_REG_CONTINUOUS_MODE(__REG_CONTINUOUS_MODE__) \
  123. ( ((__REG_CONTINUOUS_MODE__) == LL_ADC_REG_CONV_SINGLE) \
  124. || ((__REG_CONTINUOUS_MODE__) == LL_ADC_REG_CONV_CONTINUOUS) \
  125. )
  126. #define IS_LL_ADC_REG_DMA_TRANSFER(__REG_DMA_TRANSFER__) \
  127. ( ((__REG_DMA_TRANSFER__) == LL_ADC_REG_DMA_TRANSFER_NONE) \
  128. || ((__REG_DMA_TRANSFER__) == LL_ADC_REG_DMA_TRANSFER_LIMITED) \
  129. || ((__REG_DMA_TRANSFER__) == LL_ADC_REG_DMA_TRANSFER_UNLIMITED) \
  130. )
  131. #define IS_LL_ADC_REG_FLAG_EOC_SELECTION(__REG_FLAG_EOC_SELECTION__) \
  132. ( ((__REG_FLAG_EOC_SELECTION__) == LL_ADC_REG_FLAG_EOC_SEQUENCE_CONV) \
  133. || ((__REG_FLAG_EOC_SELECTION__) == LL_ADC_REG_FLAG_EOC_UNITARY_CONV) \
  134. )
  135. #define IS_LL_ADC_REG_SEQ_SCAN_LENGTH(__REG_SEQ_SCAN_LENGTH__) \
  136. ( ((__REG_SEQ_SCAN_LENGTH__) == LL_ADC_REG_SEQ_SCAN_DISABLE) \
  137. || ((__REG_SEQ_SCAN_LENGTH__) == LL_ADC_REG_SEQ_SCAN_ENABLE_2RANKS) \
  138. || ((__REG_SEQ_SCAN_LENGTH__) == LL_ADC_REG_SEQ_SCAN_ENABLE_3RANKS) \
  139. || ((__REG_SEQ_SCAN_LENGTH__) == LL_ADC_REG_SEQ_SCAN_ENABLE_4RANKS) \
  140. || ((__REG_SEQ_SCAN_LENGTH__) == LL_ADC_REG_SEQ_SCAN_ENABLE_5RANKS) \
  141. || ((__REG_SEQ_SCAN_LENGTH__) == LL_ADC_REG_SEQ_SCAN_ENABLE_6RANKS) \
  142. || ((__REG_SEQ_SCAN_LENGTH__) == LL_ADC_REG_SEQ_SCAN_ENABLE_7RANKS) \
  143. || ((__REG_SEQ_SCAN_LENGTH__) == LL_ADC_REG_SEQ_SCAN_ENABLE_8RANKS) \
  144. || ((__REG_SEQ_SCAN_LENGTH__) == LL_ADC_REG_SEQ_SCAN_ENABLE_9RANKS) \
  145. || ((__REG_SEQ_SCAN_LENGTH__) == LL_ADC_REG_SEQ_SCAN_ENABLE_10RANKS) \
  146. || ((__REG_SEQ_SCAN_LENGTH__) == LL_ADC_REG_SEQ_SCAN_ENABLE_11RANKS) \
  147. || ((__REG_SEQ_SCAN_LENGTH__) == LL_ADC_REG_SEQ_SCAN_ENABLE_12RANKS) \
  148. || ((__REG_SEQ_SCAN_LENGTH__) == LL_ADC_REG_SEQ_SCAN_ENABLE_13RANKS) \
  149. || ((__REG_SEQ_SCAN_LENGTH__) == LL_ADC_REG_SEQ_SCAN_ENABLE_14RANKS) \
  150. || ((__REG_SEQ_SCAN_LENGTH__) == LL_ADC_REG_SEQ_SCAN_ENABLE_15RANKS) \
  151. || ((__REG_SEQ_SCAN_LENGTH__) == LL_ADC_REG_SEQ_SCAN_ENABLE_16RANKS) \
  152. )
  153. #define IS_LL_ADC_REG_SEQ_SCAN_DISCONT_MODE(__REG_SEQ_DISCONT_MODE__) \
  154. ( ((__REG_SEQ_DISCONT_MODE__) == LL_ADC_REG_SEQ_DISCONT_DISABLE) \
  155. || ((__REG_SEQ_DISCONT_MODE__) == LL_ADC_REG_SEQ_DISCONT_1RANK) \
  156. || ((__REG_SEQ_DISCONT_MODE__) == LL_ADC_REG_SEQ_DISCONT_2RANKS) \
  157. || ((__REG_SEQ_DISCONT_MODE__) == LL_ADC_REG_SEQ_DISCONT_3RANKS) \
  158. || ((__REG_SEQ_DISCONT_MODE__) == LL_ADC_REG_SEQ_DISCONT_4RANKS) \
  159. || ((__REG_SEQ_DISCONT_MODE__) == LL_ADC_REG_SEQ_DISCONT_5RANKS) \
  160. || ((__REG_SEQ_DISCONT_MODE__) == LL_ADC_REG_SEQ_DISCONT_6RANKS) \
  161. || ((__REG_SEQ_DISCONT_MODE__) == LL_ADC_REG_SEQ_DISCONT_7RANKS) \
  162. || ((__REG_SEQ_DISCONT_MODE__) == LL_ADC_REG_SEQ_DISCONT_8RANKS) \
  163. )
  164. /* Check of parameters for configuration of ADC hierarchical scope: */
  165. /* ADC group injected */
  166. #define IS_LL_ADC_INJ_TRIG_SOURCE(__INJ_TRIG_SOURCE__) \
  167. ( ((__INJ_TRIG_SOURCE__) == LL_ADC_INJ_TRIG_SOFTWARE) \
  168. || ((__INJ_TRIG_SOURCE__) == LL_ADC_INJ_TRIG_EXT_TIM9_CH1) \
  169. || ((__INJ_TRIG_SOURCE__) == LL_ADC_INJ_TRIG_EXT_TIM9_TRGO) \
  170. || ((__INJ_TRIG_SOURCE__) == LL_ADC_INJ_TRIG_EXT_TIM2_TRGO) \
  171. || ((__INJ_TRIG_SOURCE__) == LL_ADC_INJ_TRIG_EXT_TIM2_CH1) \
  172. || ((__INJ_TRIG_SOURCE__) == LL_ADC_INJ_TRIG_EXT_TIM3_CH4) \
  173. || ((__INJ_TRIG_SOURCE__) == LL_ADC_INJ_TRIG_EXT_TIM4_TRGO) \
  174. || ((__INJ_TRIG_SOURCE__) == LL_ADC_INJ_TRIG_EXT_TIM4_CH1) \
  175. || ((__INJ_TRIG_SOURCE__) == LL_ADC_INJ_TRIG_EXT_TIM4_CH2) \
  176. || ((__INJ_TRIG_SOURCE__) == LL_ADC_INJ_TRIG_EXT_TIM4_CH3) \
  177. || ((__INJ_TRIG_SOURCE__) == LL_ADC_INJ_TRIG_EXT_TIM10_CH1) \
  178. || ((__INJ_TRIG_SOURCE__) == LL_ADC_INJ_TRIG_EXT_TIM7_TRGO) \
  179. || ((__INJ_TRIG_SOURCE__) == LL_ADC_INJ_TRIG_EXT_EXTI_LINE15) \
  180. )
  181. #define IS_LL_ADC_INJ_TRIG_EXT_EDGE(__INJ_TRIG_EXT_EDGE__) \
  182. ( ((__INJ_TRIG_EXT_EDGE__) == LL_ADC_INJ_TRIG_EXT_RISING) \
  183. || ((__INJ_TRIG_EXT_EDGE__) == LL_ADC_INJ_TRIG_EXT_FALLING) \
  184. || ((__INJ_TRIG_EXT_EDGE__) == LL_ADC_INJ_TRIG_EXT_RISINGFALLING) \
  185. )
  186. #define IS_LL_ADC_INJ_TRIG_AUTO(__INJ_TRIG_AUTO__) \
  187. ( ((__INJ_TRIG_AUTO__) == LL_ADC_INJ_TRIG_INDEPENDENT) \
  188. || ((__INJ_TRIG_AUTO__) == LL_ADC_INJ_TRIG_FROM_GRP_REGULAR) \
  189. )
  190. #define IS_LL_ADC_INJ_SEQ_SCAN_LENGTH(__INJ_SEQ_SCAN_LENGTH__) \
  191. ( ((__INJ_SEQ_SCAN_LENGTH__) == LL_ADC_INJ_SEQ_SCAN_DISABLE) \
  192. || ((__INJ_SEQ_SCAN_LENGTH__) == LL_ADC_INJ_SEQ_SCAN_ENABLE_2RANKS) \
  193. || ((__INJ_SEQ_SCAN_LENGTH__) == LL_ADC_INJ_SEQ_SCAN_ENABLE_3RANKS) \
  194. || ((__INJ_SEQ_SCAN_LENGTH__) == LL_ADC_INJ_SEQ_SCAN_ENABLE_4RANKS) \
  195. )
  196. #define IS_LL_ADC_INJ_SEQ_SCAN_DISCONT_MODE(__INJ_SEQ_DISCONT_MODE__) \
  197. ( ((__INJ_SEQ_DISCONT_MODE__) == LL_ADC_INJ_SEQ_DISCONT_DISABLE) \
  198. || ((__INJ_SEQ_DISCONT_MODE__) == LL_ADC_INJ_SEQ_DISCONT_1RANK) \
  199. )
  200. /**
  201. * @}
  202. */
  203. /* Private function prototypes -----------------------------------------------*/
  204. /* Exported functions --------------------------------------------------------*/
  205. /** @addtogroup ADC_LL_Exported_Functions
  206. * @{
  207. */
  208. /** @addtogroup ADC_LL_EF_Init
  209. * @{
  210. */
  211. /**
  212. * @brief De-initialize registers of all ADC instances belonging to
  213. * the same ADC common instance to their default reset values.
  214. * @param ADCxy_COMMON ADC common instance
  215. * (can be set directly from CMSIS definition or by using helper macro @ref __LL_ADC_COMMON_INSTANCE() )
  216. * @retval An ErrorStatus enumeration value:
  217. * - SUCCESS: ADC common registers are de-initialized
  218. * - ERROR: not applicable
  219. */
  220. ErrorStatus LL_ADC_CommonDeInit(ADC_Common_TypeDef *ADCxy_COMMON)
  221. {
  222. /* Check the parameters */
  223. assert_param(IS_ADC_COMMON_INSTANCE(ADCxy_COMMON));
  224. /* Force reset of ADC clock (core clock) */
  225. LL_APB2_GRP1_ForceReset(LL_APB2_GRP1_PERIPH_ADC1);
  226. /* Release reset of ADC clock (core clock) */
  227. LL_APB2_GRP1_ReleaseReset(LL_APB2_GRP1_PERIPH_ADC1);
  228. return SUCCESS;
  229. }
  230. /**
  231. * @brief Initialize some features of ADC common parameters
  232. * (all ADC instances belonging to the same ADC common instance)
  233. * and multimode (for devices with several ADC instances available).
  234. * @note The setting of ADC common parameters is conditioned to
  235. * ADC instances state:
  236. * All ADC instances belonging to the same ADC common instance
  237. * must be disabled.
  238. * @param ADCxy_COMMON ADC common instance
  239. * (can be set directly from CMSIS definition or by using helper macro @ref __LL_ADC_COMMON_INSTANCE() )
  240. * @param ADC_CommonInitStruct Pointer to a @ref LL_ADC_CommonInitTypeDef structure
  241. * @retval An ErrorStatus enumeration value:
  242. * - SUCCESS: ADC common registers are initialized
  243. * - ERROR: ADC common registers are not initialized
  244. */
  245. ErrorStatus LL_ADC_CommonInit(ADC_Common_TypeDef *ADCxy_COMMON, LL_ADC_CommonInitTypeDef *ADC_CommonInitStruct)
  246. {
  247. ErrorStatus status = SUCCESS;
  248. /* Check the parameters */
  249. assert_param(IS_ADC_COMMON_INSTANCE(ADCxy_COMMON));
  250. assert_param(IS_LL_ADC_COMMON_CLOCK(ADC_CommonInitStruct->CommonClock));
  251. /* Note: Hardware constraint (refer to description of functions */
  252. /* "LL_ADC_SetCommonXXX()": */
  253. /* On this STM32 serie, setting of these features is conditioned to */
  254. /* ADC state: */
  255. /* All ADC instances of the ADC common group must be disabled. */
  256. if(__LL_ADC_IS_ENABLED_ALL_COMMON_INSTANCE(ADCxy_COMMON) == 0U)
  257. {
  258. /* Configuration of ADC hierarchical scope: */
  259. /* - common to several ADC */
  260. /* (all ADC instances belonging to the same ADC common instance) */
  261. /* - Set ADC clock (conversion clock) */
  262. LL_ADC_SetCommonClock(ADCxy_COMMON, ADC_CommonInitStruct->CommonClock);
  263. }
  264. else
  265. {
  266. /* Initialization error: One or several ADC instances belonging to */
  267. /* the same ADC common instance are not disabled. */
  268. status = ERROR;
  269. }
  270. return status;
  271. }
  272. /**
  273. * @brief Set each @ref LL_ADC_CommonInitTypeDef field to default value.
  274. * @param ADC_CommonInitStruct Pointer to a @ref LL_ADC_CommonInitTypeDef structure
  275. * whose fields will be set to default values.
  276. * @retval None
  277. */
  278. void LL_ADC_CommonStructInit(LL_ADC_CommonInitTypeDef *ADC_CommonInitStruct)
  279. {
  280. /* Set ADC_CommonInitStruct fields to default values */
  281. /* Set fields of ADC common */
  282. /* (all ADC instances belonging to the same ADC common instance) */
  283. ADC_CommonInitStruct->CommonClock = LL_ADC_CLOCK_ASYNC_DIV2;
  284. }
  285. /**
  286. * @brief De-initialize registers of the selected ADC instance
  287. * to their default reset values.
  288. * @note To reset all ADC instances quickly (perform a hard reset),
  289. * use function @ref LL_ADC_CommonDeInit().
  290. * @param ADCx ADC instance
  291. * @retval An ErrorStatus enumeration value:
  292. * - SUCCESS: ADC registers are de-initialized
  293. * - ERROR: ADC registers are not de-initialized
  294. */
  295. ErrorStatus LL_ADC_DeInit(ADC_TypeDef *ADCx)
  296. {
  297. ErrorStatus status = SUCCESS;
  298. /* Check the parameters */
  299. assert_param(IS_ADC_ALL_INSTANCE(ADCx));
  300. /* Disable ADC instance if not already disabled. */
  301. if(LL_ADC_IsEnabled(ADCx) == 1U)
  302. {
  303. /* Set ADC group regular trigger source to SW start to ensure to not */
  304. /* have an external trigger event occurring during the conversion stop */
  305. /* ADC disable process. */
  306. LL_ADC_REG_SetTriggerSource(ADCx, LL_ADC_REG_TRIG_SOFTWARE);
  307. /* Set ADC group injected trigger source to SW start to ensure to not */
  308. /* have an external trigger event occurring during the conversion stop */
  309. /* ADC disable process. */
  310. LL_ADC_INJ_SetTriggerSource(ADCx, LL_ADC_INJ_TRIG_SOFTWARE);
  311. /* Disable the ADC instance */
  312. LL_ADC_Disable(ADCx);
  313. }
  314. /* Check whether ADC state is compliant with expected state */
  315. /* (hardware requirements of bits state to reset registers below) */
  316. if(READ_BIT(ADCx->CR2, ADC_CR2_ADON) == 0U)
  317. {
  318. /* ========== Reset ADC registers ========== */
  319. /* Reset register SR */
  320. CLEAR_BIT(ADCx->SR,
  321. ( LL_ADC_FLAG_STRT
  322. | LL_ADC_FLAG_JSTRT
  323. | LL_ADC_FLAG_EOCS
  324. | LL_ADC_FLAG_OVR
  325. | LL_ADC_FLAG_JEOS
  326. | LL_ADC_FLAG_AWD1 )
  327. );
  328. /* Reset register CR1 */
  329. CLEAR_BIT(ADCx->CR1,
  330. ( ADC_CR1_OVRIE | ADC_CR1_RES | ADC_CR1_AWDEN
  331. | ADC_CR1_JAWDEN | ADC_CR1_PDI | ADC_CR1_PDD
  332. | ADC_CR1_DISCNUM | ADC_CR1_JDISCEN | ADC_CR1_DISCEN
  333. | ADC_CR1_JAUTO | ADC_CR1_AWDSGL | ADC_CR1_SCAN
  334. | ADC_CR1_JEOCIE | ADC_CR1_AWDIE | ADC_CR1_EOCIE
  335. | ADC_CR1_AWDCH )
  336. );
  337. /* Reset register CR2 */
  338. #if defined(ADC_CR2_CFG)
  339. CLEAR_BIT(ADCx->CR2,
  340. ( ADC_CR2_SWSTART | ADC_CR2_EXTEN | ADC_CR2_EXTSEL
  341. | ADC_CR2_JSWSTART | ADC_CR2_JEXTEN | ADC_CR2_JEXTSEL
  342. | ADC_CR2_ALIGN | ADC_CR2_EOCS
  343. | ADC_CR2_DDS | ADC_CR2_DMA | ADC_CR2_DELS
  344. | ADC_CR2_CFG | ADC_CR2_CONT | ADC_CR2_ADON )
  345. );
  346. #else
  347. CLEAR_BIT(ADCx->CR2,
  348. ( ADC_CR2_SWSTART | ADC_CR2_EXTEN | ADC_CR2_EXTSEL
  349. | ADC_CR2_JSWSTART | ADC_CR2_JEXTEN | ADC_CR2_JEXTSEL
  350. | ADC_CR2_ALIGN | ADC_CR2_EOCS
  351. | ADC_CR2_DDS | ADC_CR2_DMA | ADC_CR2_DELS
  352. | ADC_CR2_CONT | ADC_CR2_ADON )
  353. );
  354. #endif /* ADC_CR2_CFG */
  355. /* Reset register SMPR1 */
  356. /* Note: On STM32L1, ADC channels 27, 28, 29, 30, 31 are not available */
  357. /* on all devices: only on STM32L1 Cat.4 and Cat.5. */
  358. #if defined(ADC_SMPR0_SMP31)
  359. CLEAR_BIT(ADCx->SMPR1,
  360. ( ADC_SMPR1_SMP29 | ADC_SMPR1_SMP28 | ADC_SMPR1_SMP27
  361. | ADC_SMPR1_SMP26 | ADC_SMPR1_SMP25 | ADC_SMPR1_SMP24
  362. | ADC_SMPR1_SMP23 | ADC_SMPR1_SMP22 | ADC_SMPR1_SMP21
  363. | ADC_SMPR1_SMP20 )
  364. );
  365. #else
  366. CLEAR_BIT(ADCx->SMPR1,
  367. ( ADC_SMPR1_SMP26 | ADC_SMPR1_SMP25 | ADC_SMPR1_SMP24
  368. | ADC_SMPR1_SMP23 | ADC_SMPR1_SMP22 | ADC_SMPR1_SMP21
  369. | ADC_SMPR1_SMP20 )
  370. );
  371. #endif /* ADC_SMPR0_SMP31 */
  372. /* Reset register SMPR2 */
  373. CLEAR_BIT(ADCx->SMPR2,
  374. ( ADC_SMPR2_SMP19 | ADC_SMPR2_SMP18 | ADC_SMPR2_SMP17
  375. | ADC_SMPR2_SMP16 | ADC_SMPR2_SMP15 | ADC_SMPR2_SMP14
  376. | ADC_SMPR2_SMP13 | ADC_SMPR2_SMP12 | ADC_SMPR2_SMP11
  377. | ADC_SMPR2_SMP10 )
  378. );
  379. /* Reset register SMPR3 */
  380. CLEAR_BIT(ADCx->SMPR3,
  381. ( ADC_SMPR3_SMP9 | ADC_SMPR3_SMP8 | ADC_SMPR3_SMP7
  382. | ADC_SMPR3_SMP6 | ADC_SMPR3_SMP5 | ADC_SMPR3_SMP4
  383. | ADC_SMPR3_SMP3 | ADC_SMPR3_SMP2 | ADC_SMPR3_SMP1
  384. | ADC_SMPR3_SMP0 )
  385. );
  386. #if defined(ADC_SMPR0_SMP31)
  387. /* Reset register SMPR0 */
  388. CLEAR_BIT(ADCx->SMPR0, (ADC_SMPR0_SMP31 | ADC_SMPR0_SMP30));
  389. #endif /* ADC_SMPR0_SMP31 */
  390. /* Reset register JOFR1 */
  391. CLEAR_BIT(ADCx->JOFR1, ADC_JOFR1_JOFFSET1);
  392. /* Reset register JOFR2 */
  393. CLEAR_BIT(ADCx->JOFR2, ADC_JOFR2_JOFFSET2);
  394. /* Reset register JOFR3 */
  395. CLEAR_BIT(ADCx->JOFR3, ADC_JOFR3_JOFFSET3);
  396. /* Reset register JOFR4 */
  397. CLEAR_BIT(ADCx->JOFR4, ADC_JOFR4_JOFFSET4);
  398. /* Reset register HTR */
  399. SET_BIT(ADCx->HTR, ADC_HTR_HT);
  400. /* Reset register LTR */
  401. CLEAR_BIT(ADCx->LTR, ADC_LTR_LT);
  402. /* Reset register SQR1 */
  403. CLEAR_BIT(ADCx->SQR1,
  404. ( ADC_SQR1_L
  405. #if defined(ADC_SQR1_SQ28)
  406. | ADC_SQR1_SQ28 | ADC_SQR1_SQ27
  407. #endif
  408. | ADC_SQR1_SQ26 | ADC_SQR1_SQ25)
  409. );
  410. /* Reset register SQR2 */
  411. CLEAR_BIT(ADCx->SQR2,
  412. ( ADC_SQR2_SQ24 | ADC_SQR2_SQ23 | ADC_SQR2_SQ22
  413. | ADC_SQR2_SQ21 | ADC_SQR2_SQ20 | ADC_SQR2_SQ19)
  414. );
  415. /* Reset register SQR3 */
  416. CLEAR_BIT(ADCx->SQR3,
  417. ( ADC_SQR3_SQ18 | ADC_SQR3_SQ17 | ADC_SQR3_SQ16
  418. | ADC_SQR3_SQ15 | ADC_SQR3_SQ14 | ADC_SQR3_SQ13)
  419. );
  420. /* Reset register SQR4 */
  421. CLEAR_BIT(ADCx->SQR4,
  422. ( ADC_SQR4_SQ12 | ADC_SQR4_SQ11 | ADC_SQR4_SQ10
  423. | ADC_SQR4_SQ9 | ADC_SQR4_SQ8 | ADC_SQR4_SQ7 )
  424. );
  425. /* Reset register SQR5 */
  426. CLEAR_BIT(ADCx->SQR5,
  427. ( ADC_SQR5_SQ6 | ADC_SQR5_SQ5 | ADC_SQR5_SQ4
  428. | ADC_SQR5_SQ3 | ADC_SQR5_SQ2 | ADC_SQR5_SQ1 )
  429. );
  430. /* Reset register JSQR */
  431. CLEAR_BIT(ADCx->JSQR,
  432. ( ADC_JSQR_JL
  433. | ADC_JSQR_JSQ4 | ADC_JSQR_JSQ3
  434. | ADC_JSQR_JSQ2 | ADC_JSQR_JSQ1 )
  435. );
  436. /* Reset register DR */
  437. /* bits in access mode read only, no direct reset applicable */
  438. /* Reset registers JDR1, JDR2, JDR3, JDR4 */
  439. /* bits in access mode read only, no direct reset applicable */
  440. /* Reset register CCR */
  441. CLEAR_BIT(ADC->CCR, ADC_CCR_TSVREFE | ADC_CCR_ADCPRE);
  442. }
  443. return status;
  444. }
  445. /**
  446. * @brief Initialize some features of ADC instance.
  447. * @note These parameters have an impact on ADC scope: ADC instance.
  448. * Affects both group regular and group injected (availability
  449. * of ADC group injected depends on STM32 families).
  450. * Refer to corresponding unitary functions into
  451. * @ref ADC_LL_EF_Configuration_ADC_Instance .
  452. * @note The setting of these parameters by function @ref LL_ADC_Init()
  453. * is conditioned to ADC state:
  454. * ADC instance must be disabled.
  455. * This condition is applied to all ADC features, for efficiency
  456. * and compatibility over all STM32 families. However, the different
  457. * features can be set under different ADC state conditions
  458. * (setting possible with ADC enabled without conversion on going,
  459. * ADC enabled with conversion on going, ...)
  460. * Each feature can be updated afterwards with a unitary function
  461. * and potentially with ADC in a different state than disabled,
  462. * refer to description of each function for setting
  463. * conditioned to ADC state.
  464. * @note After using this function, some other features must be configured
  465. * using LL unitary functions.
  466. * The minimum configuration remaining to be done is:
  467. * - Set ADC group regular or group injected sequencer:
  468. * map channel on the selected sequencer rank.
  469. * Refer to function @ref LL_ADC_REG_SetSequencerRanks().
  470. * - Set ADC channel sampling time
  471. * Refer to function LL_ADC_SetChannelSamplingTime();
  472. * @param ADCx ADC instance
  473. * @param ADC_InitStruct Pointer to a @ref LL_ADC_REG_InitTypeDef structure
  474. * @retval An ErrorStatus enumeration value:
  475. * - SUCCESS: ADC registers are initialized
  476. * - ERROR: ADC registers are not initialized
  477. */
  478. ErrorStatus LL_ADC_Init(ADC_TypeDef *ADCx, LL_ADC_InitTypeDef *ADC_InitStruct)
  479. {
  480. ErrorStatus status = SUCCESS;
  481. /* Check the parameters */
  482. assert_param(IS_ADC_ALL_INSTANCE(ADCx));
  483. assert_param(IS_LL_ADC_RESOLUTION(ADC_InitStruct->Resolution));
  484. assert_param(IS_LL_ADC_DATA_ALIGN(ADC_InitStruct->DataAlignment));
  485. /* Note: On STM32L1, low power feature is set by concatenating */
  486. /* values of @ref ADC_LL_EC_LP_MODE_AUTOWAIT */
  487. /* and @ref ADC_LL_EC_LP_MODE_AUTOPOWEROFF. */
  488. /* Check of the parameter is done for each of group of values, */
  489. /* by excluding the other group of values. */
  490. assert_param(IS_LL_ADC_LOW_POWER_AUTOWAIT(ADC_InitStruct->LowPowerMode & ~(ADC_CR1_PDI | ADC_CR1_PDD)));
  491. assert_param(IS_LL_ADC_LOW_POWER_AUTOPOWEROFF(ADC_InitStruct->LowPowerMode & ~(ADC_CR2_DELS)));
  492. assert_param(IS_LL_ADC_SCAN_SELECTION(ADC_InitStruct->SequencersScanMode));
  493. /* Note: Hardware constraint (refer to description of this function): */
  494. /* ADC instance must be disabled. */
  495. if(LL_ADC_IsEnabled(ADCx) == 0U)
  496. {
  497. /* Configuration of ADC hierarchical scope: */
  498. /* - ADC instance */
  499. /* - Set ADC data resolution */
  500. /* - Set ADC conversion data alignment */
  501. /* - Set ADC low power mode */
  502. MODIFY_REG(ADCx->CR1,
  503. ADC_CR1_RES
  504. | ADC_CR1_PDI
  505. | ADC_CR1_PDD
  506. | ADC_CR1_SCAN
  507. ,
  508. ADC_InitStruct->Resolution
  509. | (ADC_InitStruct->LowPowerMode & (ADC_CR1_PDI | ADC_CR1_PDD))
  510. | ADC_InitStruct->SequencersScanMode
  511. );
  512. MODIFY_REG(ADCx->CR2,
  513. ADC_CR2_ALIGN
  514. | ADC_CR2_DELS
  515. ,
  516. ADC_InitStruct->DataAlignment
  517. | (ADC_InitStruct->LowPowerMode & ADC_CR2_DELS)
  518. );
  519. }
  520. else
  521. {
  522. /* Initialization error: ADC instance is not disabled. */
  523. status = ERROR;
  524. }
  525. return status;
  526. }
  527. /**
  528. * @brief Set each @ref LL_ADC_InitTypeDef field to default value.
  529. * @param ADC_InitStruct Pointer to a @ref LL_ADC_InitTypeDef structure
  530. * whose fields will be set to default values.
  531. * @retval None
  532. */
  533. void LL_ADC_StructInit(LL_ADC_InitTypeDef *ADC_InitStruct)
  534. {
  535. /* Set ADC_InitStruct fields to default values */
  536. /* Set fields of ADC instance */
  537. ADC_InitStruct->Resolution = LL_ADC_RESOLUTION_12B;
  538. ADC_InitStruct->DataAlignment = LL_ADC_DATA_ALIGN_RIGHT;
  539. ADC_InitStruct->LowPowerMode = (LL_ADC_LP_AUTOWAIT_NONE | LL_ADC_LP_AUTOPOWEROFF_NONE);
  540. /* Enable scan mode to have a generic behavior with ADC of other */
  541. /* STM32 families, without this setting available: */
  542. /* ADC group regular sequencer and ADC group injected sequencer depend */
  543. /* only of their own configuration. */
  544. ADC_InitStruct->SequencersScanMode = LL_ADC_SEQ_SCAN_ENABLE;
  545. }
  546. /**
  547. * @brief Initialize some features of ADC group regular.
  548. * @note These parameters have an impact on ADC scope: ADC group regular.
  549. * Refer to corresponding unitary functions into
  550. * @ref ADC_LL_EF_Configuration_ADC_Group_Regular
  551. * (functions with prefix "REG").
  552. * @note The setting of these parameters by function @ref LL_ADC_Init()
  553. * is conditioned to ADC state:
  554. * ADC instance must be disabled.
  555. * This condition is applied to all ADC features, for efficiency
  556. * and compatibility over all STM32 families. However, the different
  557. * features can be set under different ADC state conditions
  558. * (setting possible with ADC enabled without conversion on going,
  559. * ADC enabled with conversion on going, ...)
  560. * Each feature can be updated afterwards with a unitary function
  561. * and potentially with ADC in a different state than disabled,
  562. * refer to description of each function for setting
  563. * conditioned to ADC state.
  564. * @note After using this function, other features must be configured
  565. * using LL unitary functions.
  566. * The minimum configuration remaining to be done is:
  567. * - Set ADC group regular or group injected sequencer:
  568. * map channel on the selected sequencer rank.
  569. * Refer to function @ref LL_ADC_REG_SetSequencerRanks().
  570. * - Set ADC channel sampling time
  571. * Refer to function LL_ADC_SetChannelSamplingTime();
  572. * @param ADCx ADC instance
  573. * @param ADC_REG_InitStruct Pointer to a @ref LL_ADC_REG_InitTypeDef structure
  574. * @retval An ErrorStatus enumeration value:
  575. * - SUCCESS: ADC registers are initialized
  576. * - ERROR: ADC registers are not initialized
  577. */
  578. ErrorStatus LL_ADC_REG_Init(ADC_TypeDef *ADCx, LL_ADC_REG_InitTypeDef *ADC_REG_InitStruct)
  579. {
  580. ErrorStatus status = SUCCESS;
  581. /* Check the parameters */
  582. assert_param(IS_ADC_ALL_INSTANCE(ADCx));
  583. assert_param(IS_LL_ADC_REG_TRIG_SOURCE(ADC_REG_InitStruct->TriggerSource));
  584. assert_param(IS_LL_ADC_REG_SEQ_SCAN_LENGTH(ADC_REG_InitStruct->SequencerLength));
  585. if(ADC_REG_InitStruct->SequencerLength != LL_ADC_REG_SEQ_SCAN_DISABLE)
  586. {
  587. assert_param(IS_LL_ADC_REG_SEQ_SCAN_DISCONT_MODE(ADC_REG_InitStruct->SequencerDiscont));
  588. }
  589. assert_param(IS_LL_ADC_REG_CONTINUOUS_MODE(ADC_REG_InitStruct->ContinuousMode));
  590. assert_param(IS_LL_ADC_REG_DMA_TRANSFER(ADC_REG_InitStruct->DMATransfer));
  591. /* Note: Hardware constraint (refer to description of this function): */
  592. /* ADC instance must be disabled. */
  593. if(LL_ADC_IsEnabled(ADCx) == 0U)
  594. {
  595. /* Configuration of ADC hierarchical scope: */
  596. /* - ADC group regular */
  597. /* - Set ADC group regular trigger source */
  598. /* - Set ADC group regular sequencer length */
  599. /* - Set ADC group regular sequencer discontinuous mode */
  600. /* - Set ADC group regular continuous mode */
  601. /* - Set ADC group regular conversion data transfer: no transfer or */
  602. /* transfer by DMA, and DMA requests mode */
  603. /* Note: On this STM32 serie, ADC trigger edge is set when starting */
  604. /* ADC conversion. */
  605. /* Refer to function @ref LL_ADC_REG_StartConversionExtTrig(). */
  606. if(ADC_REG_InitStruct->SequencerLength != LL_ADC_REG_SEQ_SCAN_DISABLE)
  607. {
  608. MODIFY_REG(ADCx->CR1,
  609. ADC_CR1_DISCEN
  610. | ADC_CR1_DISCNUM
  611. ,
  612. ADC_REG_InitStruct->SequencerLength
  613. | ADC_REG_InitStruct->SequencerDiscont
  614. );
  615. }
  616. else
  617. {
  618. MODIFY_REG(ADCx->CR1,
  619. ADC_CR1_DISCEN
  620. | ADC_CR1_DISCNUM
  621. ,
  622. ADC_REG_InitStruct->SequencerLength
  623. | LL_ADC_REG_SEQ_DISCONT_DISABLE
  624. );
  625. }
  626. MODIFY_REG(ADCx->CR2,
  627. ADC_CR2_EXTSEL
  628. | ADC_CR2_EXTEN
  629. | ADC_CR2_CONT
  630. | ADC_CR2_DMA
  631. | ADC_CR2_DDS
  632. ,
  633. (ADC_REG_InitStruct->TriggerSource & ADC_CR2_EXTSEL)
  634. | ADC_REG_InitStruct->ContinuousMode
  635. | ADC_REG_InitStruct->DMATransfer
  636. );
  637. /* Set ADC group regular sequencer length and scan direction */
  638. /* Note: Hardware constraint (refer to description of this function): */
  639. /* Note: If ADC instance feature scan mode is disabled */
  640. /* (refer to ADC instance initialization structure */
  641. /* parameter @ref SequencersScanMode */
  642. /* or function @ref LL_ADC_SetSequencersScanMode() ), */
  643. /* this parameter is discarded. */
  644. LL_ADC_REG_SetSequencerLength(ADCx, ADC_REG_InitStruct->SequencerLength);
  645. }
  646. else
  647. {
  648. /* Initialization error: ADC instance is not disabled. */
  649. status = ERROR;
  650. }
  651. return status;
  652. }
  653. /**
  654. * @brief Set each @ref LL_ADC_REG_InitTypeDef field to default value.
  655. * @param ADC_REG_InitStruct Pointer to a @ref LL_ADC_REG_InitTypeDef structure
  656. * whose fields will be set to default values.
  657. * @retval None
  658. */
  659. void LL_ADC_REG_StructInit(LL_ADC_REG_InitTypeDef *ADC_REG_InitStruct)
  660. {
  661. /* Set ADC_REG_InitStruct fields to default values */
  662. /* Set fields of ADC group regular */
  663. /* Note: On this STM32 serie, ADC trigger edge is set when starting */
  664. /* ADC conversion. */
  665. /* Refer to function @ref LL_ADC_REG_StartConversionExtTrig(). */
  666. ADC_REG_InitStruct->TriggerSource = LL_ADC_REG_TRIG_SOFTWARE;
  667. ADC_REG_InitStruct->SequencerLength = LL_ADC_REG_SEQ_SCAN_DISABLE;
  668. ADC_REG_InitStruct->SequencerDiscont = LL_ADC_REG_SEQ_DISCONT_DISABLE;
  669. ADC_REG_InitStruct->ContinuousMode = LL_ADC_REG_CONV_SINGLE;
  670. ADC_REG_InitStruct->DMATransfer = LL_ADC_REG_DMA_TRANSFER_NONE;
  671. }
  672. /**
  673. * @brief Initialize some features of ADC group injected.
  674. * @note These parameters have an impact on ADC scope: ADC group injected.
  675. * Refer to corresponding unitary functions into
  676. * @ref ADC_LL_EF_Configuration_ADC_Group_Regular
  677. * (functions with prefix "INJ").
  678. * @note The setting of these parameters by function @ref LL_ADC_Init()
  679. * is conditioned to ADC state:
  680. * ADC instance must be disabled.
  681. * This condition is applied to all ADC features, for efficiency
  682. * and compatibility over all STM32 families. However, the different
  683. * features can be set under different ADC state conditions
  684. * (setting possible with ADC enabled without conversion on going,
  685. * ADC enabled with conversion on going, ...)
  686. * Each feature can be updated afterwards with a unitary function
  687. * and potentially with ADC in a different state than disabled,
  688. * refer to description of each function for setting
  689. * conditioned to ADC state.
  690. * @note After using this function, other features must be configured
  691. * using LL unitary functions.
  692. * The minimum configuration remaining to be done is:
  693. * - Set ADC group injected sequencer:
  694. * map channel on the selected sequencer rank.
  695. * Refer to function @ref LL_ADC_INJ_SetSequencerRanks().
  696. * - Set ADC channel sampling time
  697. * Refer to function LL_ADC_SetChannelSamplingTime();
  698. * @param ADCx ADC instance
  699. * @param ADC_INJ_InitStruct Pointer to a @ref LL_ADC_INJ_InitTypeDef structure
  700. * @retval An ErrorStatus enumeration value:
  701. * - SUCCESS: ADC registers are initialized
  702. * - ERROR: ADC registers are not initialized
  703. */
  704. ErrorStatus LL_ADC_INJ_Init(ADC_TypeDef *ADCx, LL_ADC_INJ_InitTypeDef *ADC_INJ_InitStruct)
  705. {
  706. ErrorStatus status = SUCCESS;
  707. /* Check the parameters */
  708. assert_param(IS_ADC_ALL_INSTANCE(ADCx));
  709. assert_param(IS_LL_ADC_INJ_TRIG_SOURCE(ADC_INJ_InitStruct->TriggerSource));
  710. assert_param(IS_LL_ADC_INJ_SEQ_SCAN_LENGTH(ADC_INJ_InitStruct->SequencerLength));
  711. if(ADC_INJ_InitStruct->SequencerLength != LL_ADC_INJ_SEQ_SCAN_DISABLE)
  712. {
  713. assert_param(IS_LL_ADC_INJ_SEQ_SCAN_DISCONT_MODE(ADC_INJ_InitStruct->SequencerDiscont));
  714. }
  715. assert_param(IS_LL_ADC_INJ_TRIG_AUTO(ADC_INJ_InitStruct->TrigAuto));
  716. /* Note: Hardware constraint (refer to description of this function): */
  717. /* ADC instance must be disabled. */
  718. if(LL_ADC_IsEnabled(ADCx) == 0U)
  719. {
  720. /* Configuration of ADC hierarchical scope: */
  721. /* - ADC group injected */
  722. /* - Set ADC group injected trigger source */
  723. /* - Set ADC group injected sequencer length */
  724. /* - Set ADC group injected sequencer discontinuous mode */
  725. /* - Set ADC group injected conversion trigger: independent or */
  726. /* from ADC group regular */
  727. /* Note: On this STM32 serie, ADC trigger edge is set when starting */
  728. /* ADC conversion. */
  729. /* Refer to function @ref LL_ADC_INJ_StartConversionExtTrig(). */
  730. if(ADC_INJ_InitStruct->SequencerLength != LL_ADC_REG_SEQ_SCAN_DISABLE)
  731. {
  732. MODIFY_REG(ADCx->CR1,
  733. ADC_CR1_JDISCEN
  734. | ADC_CR1_JAUTO
  735. ,
  736. ADC_INJ_InitStruct->SequencerDiscont
  737. | ADC_INJ_InitStruct->TrigAuto
  738. );
  739. }
  740. else
  741. {
  742. MODIFY_REG(ADCx->CR1,
  743. ADC_CR1_JDISCEN
  744. | ADC_CR1_JAUTO
  745. ,
  746. LL_ADC_REG_SEQ_DISCONT_DISABLE
  747. | ADC_INJ_InitStruct->TrigAuto
  748. );
  749. }
  750. MODIFY_REG(ADCx->CR2,
  751. ADC_CR2_JEXTSEL
  752. | ADC_CR2_JEXTEN
  753. ,
  754. (ADC_INJ_InitStruct->TriggerSource & ADC_CR2_JEXTSEL)
  755. );
  756. /* Note: Hardware constraint (refer to description of this function): */
  757. /* Note: If ADC instance feature scan mode is disabled */
  758. /* (refer to ADC instance initialization structure */
  759. /* parameter @ref SequencersScanMode */
  760. /* or function @ref LL_ADC_SetSequencersScanMode() ), */
  761. /* this parameter is discarded. */
  762. LL_ADC_INJ_SetSequencerLength(ADCx, ADC_INJ_InitStruct->SequencerLength);
  763. }
  764. else
  765. {
  766. /* Initialization error: ADC instance is not disabled. */
  767. status = ERROR;
  768. }
  769. return status;
  770. }
  771. /**
  772. * @brief Set each @ref LL_ADC_INJ_InitTypeDef field to default value.
  773. * @param ADC_INJ_InitStruct Pointer to a @ref LL_ADC_INJ_InitTypeDef structure
  774. * whose fields will be set to default values.
  775. * @retval None
  776. */
  777. void LL_ADC_INJ_StructInit(LL_ADC_INJ_InitTypeDef *ADC_INJ_InitStruct)
  778. {
  779. /* Set ADC_INJ_InitStruct fields to default values */
  780. /* Set fields of ADC group injected */
  781. ADC_INJ_InitStruct->TriggerSource = LL_ADC_INJ_TRIG_SOFTWARE;
  782. ADC_INJ_InitStruct->SequencerLength = LL_ADC_INJ_SEQ_SCAN_DISABLE;
  783. ADC_INJ_InitStruct->SequencerDiscont = LL_ADC_INJ_SEQ_DISCONT_DISABLE;
  784. ADC_INJ_InitStruct->TrigAuto = LL_ADC_INJ_TRIG_INDEPENDENT;
  785. }
  786. /**
  787. * @}
  788. */
  789. /**
  790. * @}
  791. */
  792. /**
  793. * @}
  794. */
  795. #endif /* ADC1 */
  796. /**
  797. * @}
  798. */
  799. #endif /* USE_FULL_LL_DRIVER */
  800. /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/