stm32l4xx_ll_adc.c 48 KB

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