stm32f1xx_ll_adc.c 41 KB

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