stm32g0xx_ll_adc.c 34 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778
  1. /**
  2. ******************************************************************************
  3. * @file stm32g0xx_ll_adc.c
  4. * @author MCD Application Team
  5. * @brief ADC LL module driver
  6. ******************************************************************************
  7. * @attention
  8. *
  9. * Copyright (c) 2018 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 "stm32g0xx_ll_adc.h"
  21. #include "stm32g0xx_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 STM32G0xx_LL_Driver
  28. * @{
  29. */
  30. #if defined (ADC1)
  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 (HSI) with prescaler 1, */
  56. /* with highest ratio CPU clock frequency vs HSI clock frequency: */
  57. /* CPU clock frequency max 56MHz, HSI frequency 16MHz: 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. /* Note: CCRDY handshake requires 1APB + 2 ADC + 3 APB cycles */
  63. /* after the channel configuration has been changed. */
  64. /* Driver timeout is approximated to 6 CPU cycles. */
  65. #define ADC_TIMEOUT_CCRDY_CPU_CYCLES (ADC_CLOCK_RATIO_VS_CPU_HIGHEST * 6UL)
  66. /**
  67. * @}
  68. */
  69. /* Private macros ------------------------------------------------------------*/
  70. /** @addtogroup ADC_LL_Private_Macros
  71. * @{
  72. */
  73. /* Check of parameters for configuration of ADC hierarchical scope: */
  74. /* common to several ADC instances. */
  75. #define IS_LL_ADC_COMMON_CLOCK(__CLOCK__) \
  76. (((__CLOCK__) == LL_ADC_CLOCK_ASYNC_DIV1) \
  77. || ((__CLOCK__) == LL_ADC_CLOCK_ASYNC_DIV2) \
  78. || ((__CLOCK__) == LL_ADC_CLOCK_ASYNC_DIV4) \
  79. || ((__CLOCK__) == LL_ADC_CLOCK_ASYNC_DIV6) \
  80. || ((__CLOCK__) == LL_ADC_CLOCK_ASYNC_DIV8) \
  81. || ((__CLOCK__) == LL_ADC_CLOCK_ASYNC_DIV10) \
  82. || ((__CLOCK__) == LL_ADC_CLOCK_ASYNC_DIV12) \
  83. || ((__CLOCK__) == LL_ADC_CLOCK_ASYNC_DIV16) \
  84. || ((__CLOCK__) == LL_ADC_CLOCK_ASYNC_DIV32) \
  85. || ((__CLOCK__) == LL_ADC_CLOCK_ASYNC_DIV64) \
  86. || ((__CLOCK__) == LL_ADC_CLOCK_ASYNC_DIV128) \
  87. || ((__CLOCK__) == LL_ADC_CLOCK_ASYNC_DIV256) \
  88. )
  89. #define IS_LL_ADC_CLOCK_FREQ_MODE(__CLOCK_FREQ_MODE__) \
  90. (((__CLOCK_FREQ_MODE__) == LL_ADC_CLOCK_FREQ_MODE_HIGH) \
  91. || ((__CLOCK_FREQ_MODE__) == LL_ADC_CLOCK_FREQ_MODE_LOW) \
  92. )
  93. /* Check of parameters for configuration of ADC hierarchical scope: */
  94. /* ADC instance. */
  95. #define IS_LL_ADC_CLOCK(__CLOCK__) \
  96. (((__CLOCK__) == LL_ADC_CLOCK_SYNC_PCLK_DIV4) \
  97. || ((__CLOCK__) == LL_ADC_CLOCK_SYNC_PCLK_DIV2) \
  98. || ((__CLOCK__) == LL_ADC_CLOCK_SYNC_PCLK_DIV1) \
  99. || ((__CLOCK__) == LL_ADC_CLOCK_ASYNC) \
  100. )
  101. #define IS_LL_ADC_RESOLUTION(__RESOLUTION__) \
  102. (((__RESOLUTION__) == LL_ADC_RESOLUTION_12B) \
  103. || ((__RESOLUTION__) == LL_ADC_RESOLUTION_10B) \
  104. || ((__RESOLUTION__) == LL_ADC_RESOLUTION_8B) \
  105. || ((__RESOLUTION__) == LL_ADC_RESOLUTION_6B) \
  106. )
  107. #define IS_LL_ADC_DATA_ALIGN(__DATA_ALIGN__) \
  108. (((__DATA_ALIGN__) == LL_ADC_DATA_ALIGN_RIGHT) \
  109. || ((__DATA_ALIGN__) == LL_ADC_DATA_ALIGN_LEFT) \
  110. )
  111. #define IS_LL_ADC_LOW_POWER(__LOW_POWER__) \
  112. (((__LOW_POWER__) == LL_ADC_LP_MODE_NONE) \
  113. || ((__LOW_POWER__) == LL_ADC_LP_AUTOWAIT) \
  114. || ((__LOW_POWER__) == LL_ADC_LP_AUTOPOWEROFF) \
  115. || ((__LOW_POWER__) == LL_ADC_LP_AUTOWAIT_AUTOPOWEROFF) \
  116. )
  117. /* Check of parameters for configuration of ADC hierarchical scope: */
  118. /* ADC group regular */
  119. #if defined(TIM15) && defined(TIM6) && defined(TIM2)
  120. #define IS_LL_ADC_REG_TRIG_SOURCE(__REG_TRIG_SOURCE__) \
  121. (((__REG_TRIG_SOURCE__) == LL_ADC_REG_TRIG_SOFTWARE) \
  122. || ((__REG_TRIG_SOURCE__) == LL_ADC_REG_TRIG_EXT_TIM1_TRGO2) \
  123. || ((__REG_TRIG_SOURCE__) == LL_ADC_REG_TRIG_EXT_TIM1_CH4 ) \
  124. || ((__REG_TRIG_SOURCE__) == LL_ADC_REG_TRIG_EXT_TIM2_TRGO) \
  125. || ((__REG_TRIG_SOURCE__) == LL_ADC_REG_TRIG_EXT_TIM3_TRGO) \
  126. || ((__REG_TRIG_SOURCE__) == LL_ADC_REG_TRIG_EXT_TIM6_TRGO) \
  127. || ((__REG_TRIG_SOURCE__) == LL_ADC_REG_TRIG_EXT_TIM15_TRGO) \
  128. || ((__REG_TRIG_SOURCE__) == LL_ADC_REG_TRIG_EXT_EXTI_LINE11) \
  129. )
  130. #elif defined(TIM15) && defined(TIM6)
  131. #define IS_LL_ADC_REG_TRIG_SOURCE(__REG_TRIG_SOURCE__) \
  132. (((__REG_TRIG_SOURCE__) == LL_ADC_REG_TRIG_SOFTWARE) \
  133. || ((__REG_TRIG_SOURCE__) == LL_ADC_REG_TRIG_EXT_TIM1_TRGO2) \
  134. || ((__REG_TRIG_SOURCE__) == LL_ADC_REG_TRIG_EXT_TIM1_CH4 ) \
  135. || ((__REG_TRIG_SOURCE__) == LL_ADC_REG_TRIG_EXT_TIM3_TRGO) \
  136. || ((__REG_TRIG_SOURCE__) == LL_ADC_REG_TRIG_EXT_TIM6_TRGO) \
  137. || ((__REG_TRIG_SOURCE__) == LL_ADC_REG_TRIG_EXT_TIM15_TRGO) \
  138. || ((__REG_TRIG_SOURCE__) == LL_ADC_REG_TRIG_EXT_EXTI_LINE11) \
  139. )
  140. #elif defined(TIM2)
  141. #define IS_LL_ADC_REG_TRIG_SOURCE(__REG_TRIG_SOURCE__) \
  142. (((__REG_TRIG_SOURCE__) == LL_ADC_REG_TRIG_SOFTWARE) \
  143. || ((__REG_TRIG_SOURCE__) == LL_ADC_REG_TRIG_EXT_TIM1_TRGO2) \
  144. || ((__REG_TRIG_SOURCE__) == LL_ADC_REG_TRIG_EXT_TIM1_CH4 ) \
  145. || ((__REG_TRIG_SOURCE__) == LL_ADC_REG_TRIG_EXT_TIM2_TRGO) \
  146. || ((__REG_TRIG_SOURCE__) == LL_ADC_REG_TRIG_EXT_TIM3_TRGO) \
  147. || ((__REG_TRIG_SOURCE__) == LL_ADC_REG_TRIG_EXT_EXTI_LINE11) \
  148. )
  149. #else
  150. #define IS_LL_ADC_REG_TRIG_SOURCE(__REG_TRIG_SOURCE__) \
  151. (((__REG_TRIG_SOURCE__) == LL_ADC_REG_TRIG_SOFTWARE) \
  152. || ((__REG_TRIG_SOURCE__) == LL_ADC_REG_TRIG_EXT_TIM1_TRGO2) \
  153. || ((__REG_TRIG_SOURCE__) == LL_ADC_REG_TRIG_EXT_TIM1_CH4 ) \
  154. || ((__REG_TRIG_SOURCE__) == LL_ADC_REG_TRIG_EXT_TIM3_TRGO) \
  155. || ((__REG_TRIG_SOURCE__) == LL_ADC_REG_TRIG_EXT_EXTI_LINE11) \
  156. )
  157. #endif /* TIM15 && TIM6 && TIM2 */
  158. #define IS_LL_ADC_REG_CONTINUOUS_MODE(__REG_CONTINUOUS_MODE__) \
  159. (((__REG_CONTINUOUS_MODE__) == LL_ADC_REG_CONV_SINGLE) \
  160. || ((__REG_CONTINUOUS_MODE__) == LL_ADC_REG_CONV_CONTINUOUS) \
  161. )
  162. #define IS_LL_ADC_REG_DMA_TRANSFER(__REG_DMA_TRANSFER__) \
  163. (((__REG_DMA_TRANSFER__) == LL_ADC_REG_DMA_TRANSFER_NONE) \
  164. || ((__REG_DMA_TRANSFER__) == LL_ADC_REG_DMA_TRANSFER_LIMITED) \
  165. || ((__REG_DMA_TRANSFER__) == LL_ADC_REG_DMA_TRANSFER_UNLIMITED) \
  166. )
  167. #define IS_LL_ADC_REG_OVR_DATA_BEHAVIOR(__REG_OVR_DATA_BEHAVIOR__) \
  168. (((__REG_OVR_DATA_BEHAVIOR__) == LL_ADC_REG_OVR_DATA_PRESERVED) \
  169. || ((__REG_OVR_DATA_BEHAVIOR__) == LL_ADC_REG_OVR_DATA_OVERWRITTEN) \
  170. )
  171. #define IS_LL_ADC_REG_SEQ_MODE(__REG_SEQ_MODE__) \
  172. (((__REG_SEQ_MODE__) == LL_ADC_REG_SEQ_FIXED) \
  173. || ((__REG_SEQ_MODE__) == LL_ADC_REG_SEQ_CONFIGURABLE) \
  174. )
  175. #define IS_LL_ADC_REG_SEQ_SCAN_LENGTH(__REG_SEQ_SCAN_LENGTH__) \
  176. (((__REG_SEQ_SCAN_LENGTH__) == LL_ADC_REG_SEQ_SCAN_DISABLE) \
  177. || ((__REG_SEQ_SCAN_LENGTH__) == LL_ADC_REG_SEQ_SCAN_ENABLE_2RANKS) \
  178. || ((__REG_SEQ_SCAN_LENGTH__) == LL_ADC_REG_SEQ_SCAN_ENABLE_3RANKS) \
  179. || ((__REG_SEQ_SCAN_LENGTH__) == LL_ADC_REG_SEQ_SCAN_ENABLE_4RANKS) \
  180. || ((__REG_SEQ_SCAN_LENGTH__) == LL_ADC_REG_SEQ_SCAN_ENABLE_5RANKS) \
  181. || ((__REG_SEQ_SCAN_LENGTH__) == LL_ADC_REG_SEQ_SCAN_ENABLE_6RANKS) \
  182. || ((__REG_SEQ_SCAN_LENGTH__) == LL_ADC_REG_SEQ_SCAN_ENABLE_7RANKS) \
  183. || ((__REG_SEQ_SCAN_LENGTH__) == LL_ADC_REG_SEQ_SCAN_ENABLE_8RANKS) \
  184. )
  185. #define IS_LL_ADC_REG_SEQ_SCAN_DISCONT_MODE(__REG_SEQ_DISCONT_MODE__) \
  186. (((__REG_SEQ_DISCONT_MODE__) == LL_ADC_REG_SEQ_DISCONT_DISABLE) \
  187. || ((__REG_SEQ_DISCONT_MODE__) == LL_ADC_REG_SEQ_DISCONT_1RANK) \
  188. )
  189. /**
  190. * @}
  191. */
  192. /* Private function prototypes -----------------------------------------------*/
  193. /* Exported functions --------------------------------------------------------*/
  194. /** @addtogroup ADC_LL_Exported_Functions
  195. * @{
  196. */
  197. /** @addtogroup ADC_LL_EF_Init
  198. * @{
  199. */
  200. /**
  201. * @brief De-initialize registers of all ADC instances belonging to
  202. * the same ADC common instance to their default reset values.
  203. * @note This function is performing a hard reset, using high level
  204. * clock source RCC ADC reset.
  205. * @param ADCxy_COMMON ADC common instance
  206. * (can be set directly from CMSIS definition or by using helper macro @ref __LL_ADC_COMMON_INSTANCE() )
  207. * @retval An ErrorStatus enumeration value:
  208. * - SUCCESS: ADC common registers are de-initialized
  209. * - ERROR: not applicable
  210. */
  211. ErrorStatus LL_ADC_CommonDeInit(ADC_Common_TypeDef *ADCxy_COMMON)
  212. {
  213. /* Check the parameters */
  214. assert_param(IS_ADC_COMMON_INSTANCE(ADCxy_COMMON));
  215. /* Prevent unused argument(s) compilation warning if no assert_param check */
  216. (void)(ADCxy_COMMON);
  217. /* Force reset of ADC clock (core clock) */
  218. LL_APB2_GRP1_ForceReset(LL_APB2_GRP1_PERIPH_ADC);
  219. /* Release reset of ADC clock (core clock) */
  220. LL_APB2_GRP1_ReleaseReset(LL_APB2_GRP1_PERIPH_ADC);
  221. return SUCCESS;
  222. }
  223. /**
  224. * @brief Initialize some features of ADC common parameters
  225. * (all ADC instances belonging to the same ADC common instance)
  226. * and multimode (for devices with several ADC instances available).
  227. * @note The setting of ADC common parameters is conditioned to
  228. * ADC instances state:
  229. * All ADC instances belonging to the same ADC common instance
  230. * must be disabled.
  231. * @param ADCxy_COMMON ADC common instance
  232. * (can be set directly from CMSIS definition or by using helper macro @ref __LL_ADC_COMMON_INSTANCE() )
  233. * @param pADC_CommonInitStruct Pointer to a @ref LL_ADC_CommonInitTypeDef structure
  234. * @retval An ErrorStatus enumeration value:
  235. * - SUCCESS: ADC common registers are initialized
  236. * - ERROR: ADC common registers are not initialized
  237. */
  238. ErrorStatus LL_ADC_CommonInit(ADC_Common_TypeDef *ADCxy_COMMON, const LL_ADC_CommonInitTypeDef *pADC_CommonInitStruct)
  239. {
  240. ErrorStatus status = SUCCESS;
  241. /* Check the parameters */
  242. assert_param(IS_ADC_COMMON_INSTANCE(ADCxy_COMMON));
  243. assert_param(IS_LL_ADC_COMMON_CLOCK(pADC_CommonInitStruct->CommonClock));
  244. /* Note: Hardware constraint (refer to description of functions */
  245. /* "LL_ADC_SetCommonXXX()": */
  246. /* On this STM32 series, setting of these features is conditioned to */
  247. /* ADC state: */
  248. /* All ADC instances of the ADC common group must be disabled. */
  249. if (__LL_ADC_IS_ENABLED_ALL_COMMON_INSTANCE(ADCxy_COMMON) == 0UL)
  250. {
  251. /* Configuration of ADC hierarchical scope: */
  252. /* - common to several ADC */
  253. /* (all ADC instances belonging to the same ADC common instance) */
  254. /* - Set ADC clock (conversion clock) */
  255. LL_ADC_SetCommonClock(ADCxy_COMMON, pADC_CommonInitStruct->CommonClock);
  256. }
  257. else
  258. {
  259. /* Initialization error: One or several ADC instances belonging to */
  260. /* the same ADC common instance are not disabled. */
  261. status = ERROR;
  262. }
  263. return status;
  264. }
  265. /**
  266. * @brief Set each @ref LL_ADC_CommonInitTypeDef field to default value.
  267. * @param pADC_CommonInitStruct Pointer to a @ref LL_ADC_CommonInitTypeDef structure
  268. * whose fields will be set to default values.
  269. * @retval None
  270. */
  271. void LL_ADC_CommonStructInit(LL_ADC_CommonInitTypeDef *pADC_CommonInitStruct)
  272. {
  273. /* Set pADC_CommonInitStruct fields to default values */
  274. /* Set fields of ADC common */
  275. /* (all ADC instances belonging to the same ADC common instance) */
  276. pADC_CommonInitStruct->CommonClock = LL_ADC_CLOCK_ASYNC_DIV2;
  277. }
  278. /**
  279. * @brief De-initialize registers of the selected ADC instance
  280. * to their default reset values.
  281. * @note To reset all ADC instances quickly (perform a hard reset),
  282. * use function @ref LL_ADC_CommonDeInit().
  283. * @note If this functions returns error status, it means that ADC instance
  284. * is in an unknown state.
  285. * In this case, perform a hard reset using high level
  286. * clock source RCC ADC reset.
  287. * Refer to function @ref LL_ADC_CommonDeInit().
  288. * @param ADCx ADC instance
  289. * @retval An ErrorStatus enumeration value:
  290. * - SUCCESS: ADC registers are de-initialized
  291. * - ERROR: ADC registers are not de-initialized
  292. */
  293. ErrorStatus LL_ADC_DeInit(ADC_TypeDef *ADCx)
  294. {
  295. ErrorStatus status = SUCCESS;
  296. __IO uint32_t timeout_cpu_cycles = 0UL;
  297. /* Check the parameters */
  298. assert_param(IS_ADC_ALL_INSTANCE(ADCx));
  299. /* Disable ADC instance if not already disabled. */
  300. if (LL_ADC_IsEnabled(ADCx) == 1UL)
  301. {
  302. /* Stop potential ADC conversion on going on ADC group regular. */
  303. LL_ADC_REG_StopConversion(ADCx);
  304. /* Wait for ADC conversions are effectively stopped */
  305. timeout_cpu_cycles = ADC_TIMEOUT_STOP_CONVERSION_CPU_CYCLES;
  306. while (LL_ADC_REG_IsStopConversionOngoing(ADCx) == 1UL)
  307. {
  308. timeout_cpu_cycles--;
  309. if (timeout_cpu_cycles == 0UL)
  310. {
  311. /* Time-out error */
  312. status = ERROR;
  313. break;
  314. }
  315. }
  316. /* Disable the ADC instance */
  317. LL_ADC_Disable(ADCx);
  318. /* Wait for ADC instance is effectively disabled */
  319. timeout_cpu_cycles = ADC_TIMEOUT_DISABLE_CPU_CYCLES;
  320. while (LL_ADC_IsDisableOngoing(ADCx) == 1UL)
  321. {
  322. timeout_cpu_cycles--;
  323. if (timeout_cpu_cycles == 0UL)
  324. {
  325. /* Time-out error */
  326. status = ERROR;
  327. break;
  328. }
  329. }
  330. }
  331. /* Check whether ADC state is compliant with expected state */
  332. if (READ_BIT(ADCx->CR,
  333. (ADC_CR_ADSTP | ADC_CR_ADSTART
  334. | ADC_CR_ADDIS | ADC_CR_ADEN)
  335. )
  336. == 0UL)
  337. {
  338. /* ========== Reset ADC registers ========== */
  339. /* Reset register IER */
  340. CLEAR_BIT(ADCx->IER,
  341. (LL_ADC_IT_ADRDY
  342. | LL_ADC_IT_EOC
  343. | LL_ADC_IT_EOS
  344. | LL_ADC_IT_OVR
  345. | LL_ADC_IT_EOSMP
  346. | LL_ADC_IT_AWD1
  347. | LL_ADC_IT_AWD2
  348. | LL_ADC_IT_AWD3
  349. | LL_ADC_IT_EOCAL
  350. | LL_ADC_IT_CCRDY
  351. )
  352. );
  353. /* Reset register ISR */
  354. SET_BIT(ADCx->ISR,
  355. (LL_ADC_FLAG_ADRDY
  356. | LL_ADC_FLAG_EOC
  357. | LL_ADC_FLAG_EOS
  358. | LL_ADC_FLAG_OVR
  359. | LL_ADC_FLAG_EOSMP
  360. | LL_ADC_FLAG_AWD1
  361. | LL_ADC_FLAG_AWD2
  362. | LL_ADC_FLAG_AWD3
  363. | LL_ADC_FLAG_EOCAL
  364. | LL_ADC_FLAG_CCRDY
  365. )
  366. );
  367. /* Reset register CR */
  368. /* Bits ADC_CR_ADCAL, ADC_CR_ADSTP, ADC_CR_ADSTART are in access mode */
  369. /* "read-set": no direct reset applicable. */
  370. CLEAR_BIT(ADCx->CR, ADC_CR_ADVREGEN);
  371. /* Reset register CFGR1 */
  372. CLEAR_BIT(ADCx->CFGR1,
  373. (ADC_CFGR1_AWD1CH | ADC_CFGR1_AWD1EN | ADC_CFGR1_AWD1SGL | ADC_CFGR1_DISCEN
  374. | ADC_CFGR1_CHSELRMOD | ADC_CFGR1_AUTOFF | ADC_CFGR1_WAIT | ADC_CFGR1_CONT | ADC_CFGR1_OVRMOD
  375. | ADC_CFGR1_EXTEN | ADC_CFGR1_EXTSEL | ADC_CFGR1_ALIGN | ADC_CFGR1_RES
  376. | ADC_CFGR1_SCANDIR | ADC_CFGR1_DMACFG | ADC_CFGR1_DMAEN)
  377. );
  378. /* Reset register SMPR */
  379. CLEAR_BIT(ADCx->SMPR, ADC_SMPR_SMP1 | ADC_SMPR_SMP2 | ADC_SMPR_SMPSEL);
  380. /* Reset register CHSELR */
  381. CLEAR_BIT(ADCx->CHSELR,
  382. (ADC_CHSELR_CHSEL18 | ADC_CHSELR_CHSEL17 | ADC_CHSELR_CHSEL16
  383. | ADC_CHSELR_CHSEL15 | ADC_CHSELR_CHSEL14 | ADC_CHSELR_CHSEL13 | ADC_CHSELR_CHSEL12
  384. | ADC_CHSELR_CHSEL11 | ADC_CHSELR_CHSEL10 | ADC_CHSELR_CHSEL9 | ADC_CHSELR_CHSEL8
  385. | ADC_CHSELR_CHSEL7 | ADC_CHSELR_CHSEL6 | ADC_CHSELR_CHSEL5 | ADC_CHSELR_CHSEL4
  386. | ADC_CHSELR_CHSEL3 | ADC_CHSELR_CHSEL2 | ADC_CHSELR_CHSEL1 | ADC_CHSELR_CHSEL0)
  387. );
  388. /* Reset register AWD1TR */
  389. MODIFY_REG(ADCx->AWD1TR, ADC_AWD1TR_HT1 | ADC_AWD1TR_LT1, ADC_AWD1TR_HT1);
  390. /* Reset register AWD2TR */
  391. MODIFY_REG(ADCx->AWD2TR, ADC_AWD2TR_HT2 | ADC_AWD2TR_LT2, ADC_AWD2TR_HT2);
  392. /* Reset register AWD3TR */
  393. MODIFY_REG(ADCx->AWD3TR, ADC_AWD3TR_HT3 | ADC_AWD3TR_LT3, ADC_AWD3TR_HT3);
  394. /* Wait for ADC channel configuration ready */
  395. timeout_cpu_cycles = ADC_TIMEOUT_CCRDY_CPU_CYCLES;
  396. while (LL_ADC_IsActiveFlag_CCRDY(ADCx) == 0UL)
  397. {
  398. timeout_cpu_cycles--;
  399. if (timeout_cpu_cycles == 0UL)
  400. {
  401. /* Time-out error */
  402. status = ERROR;
  403. break;
  404. }
  405. }
  406. /* Clear flag ADC channel configuration ready */
  407. LL_ADC_ClearFlag_CCRDY(ADCx);
  408. /* Reset register DR */
  409. /* bits in access mode read only, no direct reset applicable */
  410. /* Reset register CALFACT */
  411. CLEAR_BIT(ADCx->CALFACT, ADC_CALFACT_CALFACT);
  412. /* Reset register CFGR2 */
  413. /* Note: CFGR2 reset done at the end of de-initialization due to */
  414. /* clock source reset */
  415. /* Note: Update of ADC clock mode is conditioned to ADC state disabled: */
  416. /* already done above. */
  417. CLEAR_BIT(ADCx->CFGR2,
  418. (ADC_CFGR2_CKMODE
  419. | ADC_CFGR2_TOVS | ADC_CFGR2_OVSS | ADC_CFGR2_OVSR
  420. | ADC_CFGR2_OVSE)
  421. );
  422. }
  423. else
  424. {
  425. /* ADC instance is in an unknown state */
  426. /* Need to performing a hard reset of ADC instance, using high level */
  427. /* clock source RCC ADC reset. */
  428. /* Caution: On this STM32 series, if several ADC instances are available */
  429. /* on the selected device, RCC ADC reset will reset */
  430. /* all ADC instances belonging to the common ADC instance. */
  431. status = ERROR;
  432. }
  433. return status;
  434. }
  435. /**
  436. * @brief Initialize some features of ADC instance.
  437. * @note These parameters have an impact on ADC scope: ADC instance.
  438. * Refer to corresponding unitary functions into
  439. * @ref ADC_LL_EF_Configuration_ADC_Instance .
  440. * @note The setting of these parameters by function @ref LL_ADC_Init()
  441. * is conditioned to ADC state:
  442. * ADC instance must be disabled.
  443. * This condition is applied to all ADC features, for efficiency
  444. * and compatibility over all STM32 series. However, the different
  445. * features can be set under different ADC state conditions
  446. * (setting possible with ADC enabled without conversion on going,
  447. * ADC enabled with conversion on going, ...)
  448. * Each feature can be updated afterwards with a unitary function
  449. * and potentially with ADC in a different state than disabled,
  450. * refer to description of each function for setting
  451. * conditioned to ADC state.
  452. * @note After using this function, some other features must be configured
  453. * using LL unitary functions.
  454. * The minimum configuration remaining to be done is:
  455. * - Set ADC group regular sequencer:
  456. * Depending on the sequencer mode (refer to
  457. * function @ref LL_ADC_REG_SetSequencerConfigurable() ):
  458. * - map channel on the selected sequencer rank.
  459. * Refer to function @ref LL_ADC_REG_SetSequencerRanks();
  460. * - map channel on rank corresponding to channel number.
  461. * Refer to function @ref LL_ADC_REG_SetSequencerChannels();
  462. * - Set ADC channel sampling time
  463. * Refer to function LL_ADC_SetSamplingTimeCommonChannels();
  464. * Refer to function LL_ADC_SetChannelSamplingTime();
  465. * @param ADCx ADC instance
  466. * @param pADC_InitStruct Pointer to a @ref LL_ADC_REG_InitTypeDef structure
  467. * @retval An ErrorStatus enumeration value:
  468. * - SUCCESS: ADC registers are initialized
  469. * - ERROR: ADC registers are not initialized
  470. */
  471. ErrorStatus LL_ADC_Init(ADC_TypeDef *ADCx, const LL_ADC_InitTypeDef *pADC_InitStruct)
  472. {
  473. ErrorStatus status = SUCCESS;
  474. /* Check the parameters */
  475. assert_param(IS_ADC_ALL_INSTANCE(ADCx));
  476. assert_param(IS_LL_ADC_CLOCK(pADC_InitStruct->Clock));
  477. assert_param(IS_LL_ADC_RESOLUTION(pADC_InitStruct->Resolution));
  478. assert_param(IS_LL_ADC_DATA_ALIGN(pADC_InitStruct->DataAlignment));
  479. assert_param(IS_LL_ADC_LOW_POWER(pADC_InitStruct->LowPowerMode));
  480. /* Note: Hardware constraint (refer to description of this function): */
  481. /* ADC instance must be disabled. */
  482. if (LL_ADC_IsEnabled(ADCx) == 0UL)
  483. {
  484. /* Configuration of ADC hierarchical scope: */
  485. /* - ADC instance */
  486. /* - Set ADC data resolution */
  487. /* - Set ADC conversion data alignment */
  488. /* - Set ADC low power mode */
  489. MODIFY_REG(ADCx->CFGR1,
  490. ADC_CFGR1_RES
  491. | ADC_CFGR1_ALIGN
  492. | ADC_CFGR1_WAIT
  493. | ADC_CFGR1_AUTOFF
  494. ,
  495. pADC_InitStruct->Resolution
  496. | pADC_InitStruct->DataAlignment
  497. | pADC_InitStruct->LowPowerMode
  498. );
  499. MODIFY_REG(ADCx->CFGR2,
  500. ADC_CFGR2_CKMODE
  501. ,
  502. pADC_InitStruct->Clock
  503. );
  504. }
  505. else
  506. {
  507. /* Initialization error: ADC instance is not disabled. */
  508. status = ERROR;
  509. }
  510. return status;
  511. }
  512. /**
  513. * @brief Set each @ref LL_ADC_InitTypeDef field to default value.
  514. * @param pADC_InitStruct Pointer to a @ref LL_ADC_InitTypeDef structure
  515. * whose fields will be set to default values.
  516. * @retval None
  517. */
  518. void LL_ADC_StructInit(LL_ADC_InitTypeDef *pADC_InitStruct)
  519. {
  520. /* Set pADC_InitStruct fields to default values */
  521. /* Set fields of ADC instance */
  522. pADC_InitStruct->Clock = LL_ADC_CLOCK_SYNC_PCLK_DIV2;
  523. pADC_InitStruct->Resolution = LL_ADC_RESOLUTION_12B;
  524. pADC_InitStruct->DataAlignment = LL_ADC_DATA_ALIGN_RIGHT;
  525. pADC_InitStruct->LowPowerMode = LL_ADC_LP_MODE_NONE;
  526. }
  527. /**
  528. * @brief Initialize some features of ADC group regular.
  529. * @note These parameters have an impact on ADC scope: ADC group regular.
  530. * Refer to corresponding unitary functions into
  531. * @ref ADC_LL_EF_Configuration_ADC_Group_Regular
  532. * (functions with prefix "REG").
  533. * @note The setting of these parameters by function @ref LL_ADC_Init()
  534. * is conditioned to ADC state:
  535. * ADC instance must be disabled.
  536. * This condition is applied to all ADC features, for efficiency
  537. * and compatibility over all STM32 series. However, the different
  538. * features can be set under different ADC state conditions
  539. * (setting possible with ADC enabled without conversion on going,
  540. * ADC enabled with conversion on going, ...)
  541. * Each feature can be updated afterwards with a unitary function
  542. * and potentially with ADC in a different state than disabled,
  543. * refer to description of each function for setting
  544. * conditioned to ADC state.
  545. * @note Before using this function, ADC group regular sequencer
  546. * must be configured: refer to function
  547. * @ref LL_ADC_REG_SetSequencerConfigurable().
  548. * @note After using this function, other features must be configured
  549. * using LL unitary functions.
  550. * The minimum configuration remaining to be done is:
  551. * - Set ADC group regular sequencer:
  552. * Depending on the sequencer mode (refer to
  553. * function @ref LL_ADC_REG_SetSequencerConfigurable() ):
  554. * - map channel on the selected sequencer rank.
  555. * Refer to function @ref LL_ADC_REG_SetSequencerRanks();
  556. * - map channel on rank corresponding to channel number.
  557. * Refer to function @ref LL_ADC_REG_SetSequencerChannels();
  558. * - Set ADC channel sampling time
  559. * Refer to function LL_ADC_SetSamplingTimeCommonChannels();
  560. * Refer to function LL_ADC_SetChannelSamplingTime();
  561. * @param ADCx ADC instance
  562. * @param pADC_RegInitStruct Pointer to a @ref LL_ADC_REG_InitTypeDef structure
  563. * @retval An ErrorStatus enumeration value:
  564. * - SUCCESS: ADC registers are initialized
  565. * - ERROR: ADC registers are not initialized
  566. */
  567. ErrorStatus LL_ADC_REG_Init(ADC_TypeDef *ADCx, const LL_ADC_REG_InitTypeDef *pADC_RegInitStruct)
  568. {
  569. ErrorStatus status = SUCCESS;
  570. /* Check the parameters */
  571. assert_param(IS_ADC_ALL_INSTANCE(ADCx));
  572. assert_param(IS_LL_ADC_REG_TRIG_SOURCE(pADC_RegInitStruct->TriggerSource));
  573. assert_param(IS_LL_ADC_REG_CONTINUOUS_MODE(pADC_RegInitStruct->ContinuousMode));
  574. assert_param(IS_LL_ADC_REG_DMA_TRANSFER(pADC_RegInitStruct->DMATransfer));
  575. assert_param(IS_LL_ADC_REG_OVR_DATA_BEHAVIOR(pADC_RegInitStruct->Overrun));
  576. if (LL_ADC_REG_GetSequencerConfigurable(ADCx) != LL_ADC_REG_SEQ_FIXED)
  577. {
  578. assert_param(IS_LL_ADC_REG_SEQ_SCAN_LENGTH(pADC_RegInitStruct->SequencerLength));
  579. }
  580. if ((LL_ADC_REG_GetSequencerConfigurable(ADCx) == LL_ADC_REG_SEQ_FIXED)
  581. || (pADC_RegInitStruct->SequencerLength != LL_ADC_REG_SEQ_SCAN_DISABLE)
  582. )
  583. {
  584. assert_param(IS_LL_ADC_REG_SEQ_SCAN_DISCONT_MODE(pADC_RegInitStruct->SequencerDiscont));
  585. /* ADC group regular continuous mode and discontinuous mode */
  586. /* can not be enabled simultenaeously */
  587. assert_param((pADC_RegInitStruct->ContinuousMode == LL_ADC_REG_CONV_SINGLE)
  588. || (pADC_RegInitStruct->SequencerDiscont == LL_ADC_REG_SEQ_DISCONT_DISABLE));
  589. }
  590. /* Note: Hardware constraint (refer to description of this function): */
  591. /* ADC instance must be disabled. */
  592. if (LL_ADC_IsEnabled(ADCx) == 0UL)
  593. {
  594. /* Configuration of ADC hierarchical scope: */
  595. /* - ADC group regular */
  596. /* - Set ADC group regular trigger source */
  597. /* - Set ADC group regular sequencer length */
  598. /* - Set ADC group regular sequencer discontinuous mode */
  599. /* - Set ADC group regular continuous mode */
  600. /* - Set ADC group regular conversion data transfer: no transfer or */
  601. /* transfer by DMA, and DMA requests mode */
  602. /* - Set ADC group regular overrun behavior */
  603. /* Note: On this STM32 series, ADC trigger edge is set to value 0x0 by */
  604. /* setting of trigger source to SW start. */
  605. if ((LL_ADC_REG_GetSequencerConfigurable(ADCx) == LL_ADC_REG_SEQ_FIXED)
  606. || (pADC_RegInitStruct->SequencerLength != LL_ADC_REG_SEQ_SCAN_DISABLE)
  607. )
  608. {
  609. /* Case of sequencer mode fixed
  610. or sequencer length >= 2 ranks with sequencer mode fully configurable:
  611. discontinuous mode configured */
  612. MODIFY_REG(ADCx->CFGR1,
  613. ADC_CFGR1_EXTSEL
  614. | ADC_CFGR1_EXTEN
  615. | ADC_CFGR1_DISCEN
  616. | ADC_CFGR1_CONT
  617. | ADC_CFGR1_DMAEN
  618. | ADC_CFGR1_DMACFG
  619. | ADC_CFGR1_OVRMOD
  620. ,
  621. pADC_RegInitStruct->TriggerSource
  622. | pADC_RegInitStruct->SequencerDiscont
  623. | pADC_RegInitStruct->ContinuousMode
  624. | pADC_RegInitStruct->DMATransfer
  625. | pADC_RegInitStruct->Overrun
  626. );
  627. }
  628. else
  629. {
  630. /* Case of sequencer mode fully configurable
  631. and sequencer length 1 rank (sequencer disabled):
  632. discontinuous mode discarded (fixed to disable) */
  633. MODIFY_REG(ADCx->CFGR1,
  634. ADC_CFGR1_EXTSEL
  635. | ADC_CFGR1_EXTEN
  636. | ADC_CFGR1_DISCEN
  637. | ADC_CFGR1_CONT
  638. | ADC_CFGR1_DMAEN
  639. | ADC_CFGR1_DMACFG
  640. | ADC_CFGR1_OVRMOD
  641. ,
  642. pADC_RegInitStruct->TriggerSource
  643. | LL_ADC_REG_SEQ_DISCONT_DISABLE
  644. | pADC_RegInitStruct->ContinuousMode
  645. | pADC_RegInitStruct->DMATransfer
  646. | pADC_RegInitStruct->Overrun
  647. );
  648. }
  649. /* Set ADC group regular sequencer length */
  650. if (LL_ADC_REG_GetSequencerConfigurable(ADCx) != LL_ADC_REG_SEQ_FIXED)
  651. {
  652. LL_ADC_REG_SetSequencerLength(ADCx, pADC_RegInitStruct->SequencerLength);
  653. }
  654. }
  655. else
  656. {
  657. /* Initialization error: ADC instance is not disabled. */
  658. status = ERROR;
  659. }
  660. return status;
  661. }
  662. /**
  663. * @brief Set each @ref LL_ADC_REG_InitTypeDef field to default value.
  664. * @param pADC_RegInitStruct Pointer to a @ref LL_ADC_REG_InitTypeDef structure
  665. * whose fields will be set to default values.
  666. * @retval None
  667. */
  668. void LL_ADC_REG_StructInit(LL_ADC_REG_InitTypeDef *pADC_RegInitStruct)
  669. {
  670. /* Set pADC_RegInitStruct fields to default values */
  671. /* Set fields of ADC group regular */
  672. /* Note: On this STM32 series, ADC trigger edge is set to value 0x0 by */
  673. /* setting of trigger source to SW start. */
  674. pADC_RegInitStruct->TriggerSource = LL_ADC_REG_TRIG_SOFTWARE;
  675. pADC_RegInitStruct->SequencerLength = LL_ADC_REG_SEQ_SCAN_DISABLE;
  676. pADC_RegInitStruct->SequencerDiscont = LL_ADC_REG_SEQ_DISCONT_DISABLE;
  677. pADC_RegInitStruct->ContinuousMode = LL_ADC_REG_CONV_SINGLE;
  678. pADC_RegInitStruct->DMATransfer = LL_ADC_REG_DMA_TRANSFER_NONE;
  679. pADC_RegInitStruct->Overrun = LL_ADC_REG_OVR_DATA_OVERWRITTEN;
  680. }
  681. /**
  682. * @}
  683. */
  684. /**
  685. * @}
  686. */
  687. /**
  688. * @}
  689. */
  690. #endif /* ADC1 */
  691. /**
  692. * @}
  693. */
  694. #endif /* USE_FULL_LL_DRIVER */