py32f0xx_ll_adc.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590
  1. /**
  2. ******************************************************************************
  3. * @file py32f0xx_ll_adc.c
  4. * @author MCU Application Team
  5. * @brief ADC LL module driver.
  6. ******************************************************************************
  7. * @attention
  8. *
  9. * <h2><center>&copy; Copyright (c) 2023 Puya Semiconductor Co.
  10. * All rights reserved.</center></h2>
  11. *
  12. * This software component is licensed by Puya under BSD 3-Clause license,
  13. * the "License"; You may not use this file except in compliance with the
  14. * License. You may obtain a copy of the License at:
  15. * opensource.org/licenses/BSD-3-Clause
  16. *
  17. ******************************************************************************
  18. * @attention
  19. *
  20. * <h2><center>&copy; Copyright (c) 2016 STMicroelectronics.
  21. * All rights reserved.</center></h2>
  22. *
  23. * This software component is licensed by ST under BSD 3-Clause license,
  24. * the "License"; You may not use this file except in compliance with the
  25. * License. You may obtain a copy of the License at:
  26. * opensource.org/licenses/BSD-3-Clause
  27. *
  28. ******************************************************************************
  29. */
  30. #if defined(USE_FULL_LL_DRIVER)
  31. /* Includes ------------------------------------------------------------------*/
  32. #include "py32f0xx_ll_adc.h"
  33. #include "py32f0xx_ll_bus.h"
  34. #ifdef USE_FULL_ASSERT
  35. #include "PY32_assert.h"
  36. #else
  37. #define assert_param(expr) ((void)0U)
  38. #endif
  39. /** @addtogroup PY32F0xx_LL_Driver
  40. * @{
  41. */
  42. #if defined (ADC1)
  43. /** @addtogroup ADC_LL ADC
  44. * @{
  45. */
  46. /* Private types -------------------------------------------------------------*/
  47. /* Private variables ---------------------------------------------------------*/
  48. /* Private constants ---------------------------------------------------------*/
  49. /** @addtogroup ADC_LL_Private_Constants
  50. * @{
  51. */
  52. /* Definitions of ADC hardware constraints delays */
  53. /* Note: Only ADC IP HW delays are defined in ADC LL driver driver, */
  54. /* not timeout values: */
  55. /* Timeout values for ADC operations are dependent to device clock */
  56. /* configuration (system clock versus ADC clock), */
  57. /* and therefore must be defined in user application. */
  58. /* Refer to @ref ADC_LL_EC_HW_DELAYS for description of ADC timeout */
  59. /* values definition. */
  60. /* Unit: CPU cycles. */
  61. #define ADC_CLOCK_RATIO_VS_CPU_HIGHEST ((uint32_t) 512U * 16U * 4U)
  62. #define ADC_TIMEOUT_DISABLE_CPU_CYCLES (ADC_CLOCK_RATIO_VS_CPU_HIGHEST * 1U)
  63. #define ADC_TIMEOUT_STOP_CONVERSION_CPU_CYCLES (ADC_CLOCK_RATIO_VS_CPU_HIGHEST * 1U)
  64. /**
  65. * @}
  66. */
  67. /* Private macros ------------------------------------------------------------*/
  68. /** @addtogroup ADC_LL_Private_Macros ADC Private Macros
  69. * @{
  70. */
  71. /* Check of parameters for configuration of ADC hierarchical scope: */
  72. /* common to several ADC instances. */
  73. /* Check of parameters for configuration of ADC hierarchical scope: */
  74. /* ADC instance. */
  75. #define IS_LL_ADC_CLOCK(__CLOCK__) \
  76. ( ((__CLOCK__) == LL_ADC_CLOCK_SYNC_PCLK_DIV64) \
  77. || ((__CLOCK__) == LL_ADC_CLOCK_SYNC_PCLK_DIV32) \
  78. || ((__CLOCK__) == LL_ADC_CLOCK_SYNC_PCLK_DIV16) \
  79. || ((__CLOCK__) == LL_ADC_CLOCK_SYNC_PCLK_DIV8) \
  80. || ((__CLOCK__) == LL_ADC_CLOCK_SYNC_PCLK_DIV4) \
  81. || ((__CLOCK__) == LL_ADC_CLOCK_SYNC_PCLK_DIV2) \
  82. || ((__CLOCK__) == LL_ADC_CLOCK_SYNC_PCLK_DIV1) \
  83. || ((__CLOCK__) == LL_ADC_CLOCK_ASYNC_HSI_DIV64) \
  84. || ((__CLOCK__) == LL_ADC_CLOCK_ASYNC_HSI_DIV32) \
  85. || ((__CLOCK__) == LL_ADC_CLOCK_ASYNC_HSI_DIV16) \
  86. || ((__CLOCK__) == LL_ADC_CLOCK_ASYNC_HSI_DIV8) \
  87. || ((__CLOCK__) == LL_ADC_CLOCK_ASYNC_HSI_DIV4) \
  88. || ((__CLOCK__) == LL_ADC_CLOCK_ASYNC_HSI_DIV2) \
  89. || ((__CLOCK__) == LL_ADC_CLOCK_ASYNC_HSI_DIV1) \
  90. )
  91. #define IS_LL_ADC_RESOLUTION(__RESOLUTION__) \
  92. ( ((__RESOLUTION__) == LL_ADC_RESOLUTION_12B) \
  93. || ((__RESOLUTION__) == LL_ADC_RESOLUTION_10B) \
  94. || ((__RESOLUTION__) == LL_ADC_RESOLUTION_8B) \
  95. || ((__RESOLUTION__) == LL_ADC_RESOLUTION_6B) \
  96. )
  97. #define IS_LL_ADC_DATA_ALIGN(__DATA_ALIGN__) \
  98. ( ((__DATA_ALIGN__) == LL_ADC_DATA_ALIGN_RIGHT) \
  99. || ((__DATA_ALIGN__) == LL_ADC_DATA_ALIGN_LEFT) \
  100. )
  101. #define IS_LL_ADC_LOW_POWER(__LOW_POWER__) \
  102. ( ((__LOW_POWER__) == LL_ADC_LP_MODE_NONE) \
  103. || ((__LOW_POWER__) == LL_ADC_LP_AUTOWAIT) \
  104. )
  105. /* Check of parameters for configuration of ADC hierarchical scope: */
  106. /* ADC group regular */
  107. #define IS_LL_ADC_REG_TRIG_SOURCE(__REG_TRIG_SOURCE__) \
  108. ( ((__REG_TRIG_SOURCE__) == LL_ADC_REG_TRIG_SOFTWARE) \
  109. || ((__REG_TRIG_SOURCE__) == LL_ADC_REG_TRIG_EXT_TIM1_TRGO) \
  110. || ((__REG_TRIG_SOURCE__) == LL_ADC_REG_TRIG_EXT_TIM1_CH4) \
  111. || ((__REG_TRIG_SOURCE__) == LL_ADC_REG_TRIG_EXT_TIM3_TRGO) \
  112. )
  113. #define IS_LL_ADC_REG_CONTINUOUS_MODE(__REG_CONTINUOUS_MODE__) \
  114. ( ((__REG_CONTINUOUS_MODE__) == LL_ADC_REG_CONV_SINGLE) \
  115. || ((__REG_CONTINUOUS_MODE__) == LL_ADC_REG_CONV_CONTINUOUS) \
  116. )
  117. #if (defined(DMA) || defined(DMA1))
  118. #define IS_LL_ADC_REG_DMA_TRANSFER(__REG_DMA_TRANSFER__) \
  119. ( ((__REG_DMA_TRANSFER__) == LL_ADC_REG_DMA_TRANSFER_NONE) \
  120. || ((__REG_DMA_TRANSFER__) == LL_ADC_REG_DMA_TRANSFER_LIMITED) \
  121. || ((__REG_DMA_TRANSFER__) == LL_ADC_REG_DMA_TRANSFER_UNLIMITED) \
  122. )
  123. #endif
  124. #define IS_LL_ADC_REG_OVR_DATA_BEHAVIOR(__REG_OVR_DATA_BEHAVIOR__) \
  125. ( ((__REG_OVR_DATA_BEHAVIOR__) == LL_ADC_REG_OVR_DATA_PRESERVED) \
  126. || ((__REG_OVR_DATA_BEHAVIOR__) == LL_ADC_REG_OVR_DATA_OVERWRITTEN) \
  127. )
  128. #define IS_LL_ADC_REG_SEQ_SCAN_DISCONT_MODE(__REG_SEQ_DISCONT_MODE__) \
  129. ( ((__REG_SEQ_DISCONT_MODE__) == LL_ADC_REG_SEQ_DISCONT_DISABLE) \
  130. || ((__REG_SEQ_DISCONT_MODE__) == LL_ADC_REG_SEQ_DISCONT_1RANK) \
  131. )
  132. /**
  133. * @}
  134. */
  135. /* Private function prototypes -----------------------------------------------*/
  136. /* Exported functions --------------------------------------------------------*/
  137. /** @addtogroup ADC_LL_Exported_Functions
  138. * @{
  139. */
  140. /** @addtogroup ADC_LL_EF_Init
  141. * @{
  142. */
  143. /**
  144. * @brief De-initialize registers of all ADC instances belonging to
  145. * the same ADC common instance to their default reset values.
  146. * @note This function is performing a hard reset, using high level
  147. * clock source RCC ADC reset.
  148. * @param ADCxy_COMMON ADC common instance
  149. * (can be set directly from CMSIS definition or by using helper macro @ref __LL_ADC_COMMON_INSTANCE() )
  150. * @retval An ErrorStatus enumeration value:
  151. * - SUCCESS: ADC common registers are de-initialized
  152. * - ERROR: not applicable
  153. */
  154. ErrorStatus LL_ADC_CommonDeInit(ADC_Common_TypeDef *ADCxy_COMMON)
  155. {
  156. /* Check the parameters */
  157. assert_param(IS_ADC_COMMON_INSTANCE(ADCxy_COMMON));
  158. /* Force reset of ADC clock (core clock) */
  159. LL_APB1_GRP2_ForceReset(LL_APB1_GRP2_PERIPH_ADC1);
  160. /* Release reset of ADC clock (core clock) */
  161. LL_APB1_GRP2_ReleaseReset(LL_APB1_GRP2_PERIPH_ADC1);
  162. return SUCCESS;
  163. }
  164. /**
  165. * @brief De-initialize registers of the selected ADC instance
  166. * to their default reset values.
  167. * @note To reset all ADC instances quickly (perform a hard reset),
  168. * use function @ref LL_ADC_CommonDeInit().
  169. * @note If this functions returns error status, it means that ADC instance
  170. * is in an unknown state.
  171. * In this case, perform a hard reset using high level
  172. * clock source RCC ADC reset.
  173. * Refer to function @ref LL_ADC_CommonDeInit().
  174. * @param ADCx ADC instance
  175. * @retval An ErrorStatus enumeration value:
  176. * - SUCCESS: ADC registers are de-initialized
  177. * - ERROR: ADC registers are not de-initialized
  178. */
  179. ErrorStatus LL_ADC_DeInit(ADC_TypeDef *ADCx)
  180. {
  181. ErrorStatus status = SUCCESS;
  182. __IO uint32_t timeout_cpu_cycles = 0U;
  183. /* Check the parameters */
  184. assert_param(IS_ADC_ALL_INSTANCE(ADCx));
  185. /* Disable ADC instance if not already disabled. */
  186. if(LL_ADC_IsEnabled(ADCx) == 1U)
  187. {
  188. /* Set ADC group regular trigger source to SW start to ensure to not */
  189. /* have an external trigger event occurring during the conversion stop */
  190. /* ADC disable process. */
  191. LL_ADC_REG_SetTriggerSource(ADCx, LL_ADC_REG_TRIG_SOFTWARE);
  192. /* Stop potential ADC conversion on going on ADC group regular. */
  193. if(LL_ADC_REG_IsConversionOngoing(ADCx) != 0U)
  194. {
  195. if(LL_ADC_REG_IsStopConversionOngoing(ADCx) == 0U)
  196. {
  197. LL_ADC_REG_StopConversion(ADCx);
  198. }
  199. }
  200. else
  201. {
  202. (ADCx->CFGR1)|=(0x7<<6);
  203. (ADCx->CFGR1)|=(0x3<<10);
  204. __disable_irq();
  205. LL_ADC_REG_StartConversion(ADCx);
  206. LL_ADC_REG_StopConversion(ADCx);
  207. __enable_irq();
  208. }
  209. /* Wait for ADC conversions are effectively stopped */
  210. timeout_cpu_cycles = ADC_TIMEOUT_STOP_CONVERSION_CPU_CYCLES;
  211. while (LL_ADC_REG_IsStopConversionOngoing(ADCx) == 1U)
  212. {
  213. if(timeout_cpu_cycles-- == 0U)
  214. {
  215. /* Time-out error */
  216. status = ERROR;
  217. }
  218. }
  219. /* Wait for ADC instance is effectively disabled */
  220. timeout_cpu_cycles = ADC_TIMEOUT_DISABLE_CPU_CYCLES;
  221. while (LL_ADC_IsEnabled(ADCx) == 1U)
  222. {
  223. if(timeout_cpu_cycles-- == 0U)
  224. {
  225. /* Time-out error */
  226. status = ERROR;
  227. }
  228. }
  229. }
  230. /* Check whether ADC state is compliant with expected state */
  231. if(READ_BIT(ADCx->CR,(ADC_CR_ADSTP | ADC_CR_ADSTART | ADC_CR_ADEN ))== 0U)
  232. {
  233. /* ========== Reset ADC registers ========== */
  234. /* Reset register IER */
  235. CLEAR_BIT(ADCx->IER,
  236. ( LL_ADC_IT_EOC
  237. | LL_ADC_IT_EOS
  238. | LL_ADC_IT_OVR
  239. | LL_ADC_IT_EOSMP
  240. | LL_ADC_IT_AWD )
  241. );
  242. /* Reset register ISR */
  243. SET_BIT(ADCx->ISR,
  244. ( LL_ADC_FLAG_EOC
  245. | LL_ADC_FLAG_EOS
  246. | LL_ADC_FLAG_OVR
  247. | LL_ADC_FLAG_EOSMP
  248. | LL_ADC_FLAG_AWD )
  249. );
  250. /* Reset register CR */
  251. /* Bits ADC_CR_ADCAL, ADC_CR_ADSTP, ADC_CR_ADSTART are in access mode */
  252. /* "read-set": no direct reset applicable. */
  253. /* No action on register CR */
  254. /* Reset register CFGR1 */
  255. #if (defined(DMA) || defined(DMA1))
  256. CLEAR_BIT(ADCx->CFGR1,
  257. ( ADC_CFGR1_AWDCH | ADC_CFGR1_AWDEN | ADC_CFGR1_AWDSGL | ADC_CFGR1_DISCEN
  258. | ADC_CFGR1_WAIT | ADC_CFGR1_CONT | ADC_CFGR1_OVRMOD
  259. | ADC_CFGR1_EXTEN | ADC_CFGR1_EXTSEL | ADC_CFGR1_ALIGN | ADC_CFGR1_RESSEL
  260. | ADC_CFGR1_SCANDIR | ADC_CFGR1_DMACFG | ADC_CFGR1_DMAEN )
  261. );
  262. #else
  263. CLEAR_BIT(ADCx->CFGR1,
  264. ( ADC_CFGR1_AWDCH | ADC_CFGR1_AWDEN | ADC_CFGR1_AWDSGL | ADC_CFGR1_DISCEN
  265. | ADC_CFGR1_WAIT | ADC_CFGR1_CONT | ADC_CFGR1_OVRMOD
  266. | ADC_CFGR1_EXTEN | ADC_CFGR1_EXTSEL | ADC_CFGR1_ALIGN | ADC_CFGR1_RESSEL
  267. | ADC_CFGR1_SCANDIR )
  268. );
  269. #endif
  270. /* Reset register CFGR2 */
  271. /* Note: Update of ADC clock mode is conditioned to ADC state disabled: */
  272. /* already done above. */
  273. CLEAR_BIT(ADCx->CFGR2, ADC_CFGR2_CKMODE);
  274. /* Reset register SMPR */
  275. CLEAR_BIT(ADCx->SMPR, ADC_SMPR_SMP);
  276. /* Reset register TR */
  277. MODIFY_REG(ADCx->TR, ADC_TR_HT | ADC_TR_LT, ADC_TR_HT);
  278. /* Reset register CHSELR */
  279. CLEAR_BIT(ADCx->CHSELR,
  280. ( ADC_CHSELR_CHSEL12 | ADC_CHSELR_CHSEL11 | ADC_CHSELR_CHSEL9 | ADC_CHSELR_CHSEL8
  281. | ADC_CHSELR_CHSEL7 | ADC_CHSELR_CHSEL6 | ADC_CHSELR_CHSEL5 | ADC_CHSELR_CHSEL4
  282. | ADC_CHSELR_CHSEL3 | ADC_CHSELR_CHSEL2 | ADC_CHSELR_CHSEL1 | ADC_CHSELR_CHSEL0 )
  283. );
  284. /* Reset register CCR */
  285. CLEAR_BIT(__LL_ADC_COMMON_INSTANCE(ADC1)->CCR,ADC_CCR_TSEN | ADC_CCR_VREFEN);
  286. /* Reset register DR */
  287. /* bits in access mode read only, no direct reset applicable */
  288. }
  289. else
  290. {
  291. /* ADC instance is in an unknown state */
  292. /* Need to performing a hard reset of ADC instance, using high level */
  293. /* clock source RCC ADC reset. */
  294. /* Caution: On this PY32 serie, if several ADC instances are available */
  295. /* on the selected device, RCC ADC reset will reset */
  296. /* all ADC instances belonging to the common ADC instance. */
  297. status = ERROR;
  298. }
  299. return status;
  300. }
  301. /**
  302. * @brief Initialize some features of ADC instance.
  303. * @note These parameters have an impact on ADC scope: ADC instance.
  304. * Refer to corresponding unitary functions into
  305. * @ref ADC_LL_EF_Configuration_ADC_Instance .
  306. * @note The setting of these parameters by function @ref LL_ADC_Init()
  307. * is conditioned to ADC state:
  308. * ADC instance must be disabled.
  309. * This condition is applied to all ADC features, for efficiency
  310. * and compatibility over all PY32 families. However, the different
  311. * features can be set under different ADC state conditions
  312. * (setting possible with ADC enabled without conversion on going,
  313. * ADC enabled with conversion on going, ...)
  314. * Each feature can be updated afterwards with a unitary function
  315. * and potentially with ADC in a different state than disabled,
  316. * refer to description of each function for setting
  317. * conditioned to ADC state.
  318. * @note After using this function, some other features must be configured
  319. * using LL unitary functions.
  320. * The minimum configuration remaining to be done is:
  321. * - Set ADC group regular sequencer:
  322. * map channel on rank corresponding to channel number.
  323. * Refer to function @ref LL_ADC_REG_SetSequencerChannels();
  324. * - Set ADC channel sampling time
  325. * Refer to function LL_ADC_SetChannelSamplingTime();
  326. * @param ADCx ADC instance
  327. * @param ADC_InitStruct Pointer to a @ref LL_ADC_REG_InitTypeDef structure
  328. * @retval An ErrorStatus enumeration value:
  329. * - SUCCESS: ADC registers are initialized
  330. * - ERROR: ADC registers are not initialized
  331. */
  332. ErrorStatus LL_ADC_Init(ADC_TypeDef *ADCx, LL_ADC_InitTypeDef *ADC_InitStruct)
  333. {
  334. ErrorStatus status = SUCCESS;
  335. /* Check the parameters */
  336. assert_param(IS_ADC_ALL_INSTANCE(ADCx));
  337. assert_param(IS_LL_ADC_CLOCK(ADC_InitStruct->Clock));
  338. assert_param(IS_LL_ADC_RESOLUTION(ADC_InitStruct->Resolution));
  339. assert_param(IS_LL_ADC_DATA_ALIGN(ADC_InitStruct->DataAlignment));
  340. assert_param(IS_LL_ADC_LOW_POWER(ADC_InitStruct->LowPowerMode));
  341. /* Note: Hardware constraint (refer to description of this function): */
  342. /* ADC instance must be disabled. */
  343. if(LL_ADC_IsEnabled(ADCx) == 0U)
  344. {
  345. /* Configuration of ADC hierarchical scope: */
  346. /* - ADC instance */
  347. /* - Set ADC data resolution */
  348. /* - Set ADC conversion data alignment */
  349. /* - Set ADC low power mode */
  350. MODIFY_REG(ADCx->CFGR1,
  351. ADC_CFGR1_RESSEL
  352. | ADC_CFGR1_ALIGN
  353. | ADC_CFGR1_WAIT
  354. ,
  355. ADC_InitStruct->Resolution
  356. | ADC_InitStruct->DataAlignment
  357. | ADC_InitStruct->LowPowerMode
  358. );
  359. MODIFY_REG(ADCx->CFGR2,
  360. ADC_CFGR2_CKMODE
  361. ,
  362. ADC_InitStruct->Clock
  363. );
  364. }
  365. else
  366. {
  367. /* Initialization error: ADC instance is not disabled. */
  368. status = ERROR;
  369. }
  370. return status;
  371. }
  372. /**
  373. * @brief Set each @ref LL_ADC_InitTypeDef field to default value.
  374. * @param ADC_InitStruct Pointer to a @ref LL_ADC_InitTypeDef structure
  375. * whose fields will be set to default values.
  376. * @retval None
  377. */
  378. void LL_ADC_StructInit(LL_ADC_InitTypeDef *ADC_InitStruct)
  379. {
  380. /* Set ADC_InitStruct fields to default values */
  381. /* Set fields of ADC instance */
  382. ADC_InitStruct->Clock = LL_ADC_CLOCK_SYNC_PCLK_DIV2;
  383. ADC_InitStruct->Resolution = LL_ADC_RESOLUTION_12B;
  384. ADC_InitStruct->DataAlignment = LL_ADC_DATA_ALIGN_RIGHT;
  385. ADC_InitStruct->LowPowerMode = LL_ADC_LP_MODE_NONE;
  386. }
  387. /**
  388. * @brief Initialize some features of ADC group regular.
  389. * @note These parameters have an impact on ADC scope: ADC group regular.
  390. * Refer to corresponding unitary functions into
  391. * @ref ADC_LL_EF_Configuration_ADC_Group_Regular
  392. * (functions with prefix "REG").
  393. * @note The setting of these parameters by function @ref LL_ADC_Init()
  394. * is conditioned to ADC state:
  395. * ADC instance must be disabled.
  396. * This condition is applied to all ADC features, for efficiency
  397. * and compatibility over all PY32 families. However, the different
  398. * features can be set under different ADC state conditions
  399. * (setting possible with ADC enabled without conversion on going,
  400. * ADC enabled with conversion on going, ...)
  401. * Each feature can be updated afterwards with a unitary function
  402. * and potentially with ADC in a different state than disabled,
  403. * refer to description of each function for setting
  404. * conditioned to ADC state.
  405. * @note After using this function, other features must be configured
  406. * using LL unitary functions.
  407. * The minimum configuration remaining to be done is:
  408. * - Set ADC group regular sequencer:
  409. * map channel on rank corresponding to channel number.
  410. * Refer to function @ref LL_ADC_REG_SetSequencerChannels();
  411. * - Set ADC channel sampling time
  412. * Refer to function LL_ADC_SetChannelSamplingTime();
  413. * @note Depending on devices and packages, DMA may not be available.
  414. * Refer to device datasheet for DMA availability.
  415. * @param ADCx ADC instance
  416. * @param ADC_REG_InitStruct Pointer to a @ref LL_ADC_REG_InitTypeDef structure
  417. * @retval An ErrorStatus enumeration value:
  418. * - SUCCESS: ADC registers are initialized
  419. * - ERROR: ADC registers are not initialized
  420. */
  421. ErrorStatus LL_ADC_REG_Init(ADC_TypeDef *ADCx, LL_ADC_REG_InitTypeDef *ADC_REG_InitStruct)
  422. {
  423. ErrorStatus status = SUCCESS;
  424. /* Check the parameters */
  425. assert_param(IS_ADC_ALL_INSTANCE(ADCx));
  426. assert_param(IS_LL_ADC_REG_TRIG_SOURCE(ADC_REG_InitStruct->TriggerSource));
  427. assert_param(IS_LL_ADC_REG_SEQ_SCAN_DISCONT_MODE(ADC_REG_InitStruct->SequencerDiscont));
  428. assert_param(IS_LL_ADC_REG_CONTINUOUS_MODE(ADC_REG_InitStruct->ContinuousMode));
  429. #if (defined(DMA) || defined(DMA1))
  430. assert_param(IS_LL_ADC_REG_DMA_TRANSFER(ADC_REG_InitStruct->DMATransfer));
  431. #endif
  432. assert_param(IS_LL_ADC_REG_OVR_DATA_BEHAVIOR(ADC_REG_InitStruct->Overrun));
  433. /* Note: Hardware constraint (refer to description of this function): */
  434. /* ADC instance must be disabled. */
  435. if(LL_ADC_IsEnabled(ADCx) == 0U)
  436. {
  437. /* Configuration of ADC hierarchical scope: */
  438. /* - ADC group regular */
  439. /* - Set ADC group regular trigger source */
  440. /* - Set ADC group regular sequencer discontinuous mode */
  441. /* - Set ADC group regular continuous mode */
  442. /* - Set ADC group regular conversion data transfer: no transfer or */
  443. /* transfer by DMA, and DMA requests mode */
  444. /* - Set ADC group regular overrun behavior */
  445. /* Note: On this PY32 serie, ADC trigger edge is set to value 0x0 by */
  446. /* setting of trigger source to SW start. */
  447. #if (defined(DMA) || defined(DMA1))
  448. MODIFY_REG(ADCx->CFGR1,
  449. ADC_CFGR1_EXTSEL
  450. | ADC_CFGR1_EXTEN
  451. | ADC_CFGR1_DISCEN
  452. | ADC_CFGR1_CONT
  453. | ADC_CFGR1_DMAEN
  454. | ADC_CFGR1_DMACFG
  455. | ADC_CFGR1_OVRMOD
  456. ,
  457. ADC_REG_InitStruct->TriggerSource
  458. | ADC_REG_InitStruct->SequencerDiscont
  459. | ADC_REG_InitStruct->ContinuousMode
  460. | ADC_REG_InitStruct->DMATransfer
  461. | ADC_REG_InitStruct->Overrun
  462. );
  463. #else
  464. MODIFY_REG(ADCx->CFGR1,
  465. ADC_CFGR1_EXTSEL
  466. | ADC_CFGR1_EXTEN
  467. | ADC_CFGR1_DISCEN
  468. | ADC_CFGR1_CONT
  469. | ADC_CFGR1_OVRMOD
  470. ,
  471. ADC_REG_InitStruct->TriggerSource
  472. | ADC_REG_InitStruct->SequencerDiscont
  473. | ADC_REG_InitStruct->ContinuousMode
  474. | ADC_REG_InitStruct->Overrun
  475. );
  476. #endif
  477. }
  478. else
  479. {
  480. /* Initialization error: ADC instance is not disabled. */
  481. status = ERROR;
  482. }
  483. return status;
  484. }
  485. /**
  486. * @brief Set each @ref LL_ADC_REG_InitTypeDef field to default value.
  487. * @note Depending on devices and packages, DMA may not be available.
  488. * Refer to device datasheet for DMA availability.
  489. * @param ADC_REG_InitStruct Pointer to a @ref LL_ADC_REG_InitTypeDef structure
  490. * whose fields will be set to default values.
  491. * @retval None
  492. */
  493. void LL_ADC_REG_StructInit(LL_ADC_REG_InitTypeDef *ADC_REG_InitStruct)
  494. {
  495. /* Set ADC_REG_InitStruct fields to default values */
  496. /* Set fields of ADC group regular */
  497. /* Note: On this PY32 serie, ADC trigger edge is set to value 0x0 by */
  498. /* setting of trigger source to SW start. */
  499. ADC_REG_InitStruct->TriggerSource = LL_ADC_REG_TRIG_SOFTWARE;
  500. ADC_REG_InitStruct->SequencerDiscont = LL_ADC_REG_SEQ_DISCONT_DISABLE;
  501. ADC_REG_InitStruct->ContinuousMode = LL_ADC_REG_CONV_SINGLE;
  502. #if (defined(DMA) || defined(DMA1))
  503. ADC_REG_InitStruct->DMATransfer = LL_ADC_REG_DMA_TRANSFER_NONE;
  504. #endif
  505. ADC_REG_InitStruct->Overrun = LL_ADC_REG_OVR_DATA_OVERWRITTEN;
  506. }
  507. /**
  508. * @}
  509. */
  510. /**
  511. * @}
  512. */
  513. /**
  514. * @}
  515. */
  516. #endif /* ADC1 */
  517. /**
  518. * @}
  519. */
  520. #endif /* USE_FULL_LL_DRIVER */
  521. /************************ (C) COPYRIGHT Puya *****END OF FILE****/