stm32l4xx_ll_adc.c 47 KB

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