stm32f1xx_hal_adc_ex.c 50 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324
  1. /**
  2. ******************************************************************************
  3. * @file stm32f1xx_hal_adc_ex.c
  4. * @author MCD Application Team
  5. * @brief This file provides firmware functions to manage the following
  6. * functionalities of the Analog to Digital Convertor (ADC)
  7. * peripheral:
  8. * + Operation functions
  9. * ++ Start, stop, get result of conversions of injected
  10. * group, using 2 possible modes: polling, interruption.
  11. * ++ Multimode feature (available on devices with 2 ADCs or more)
  12. * ++ Calibration (ADC automatic self-calibration)
  13. * + Control functions
  14. * ++ Channels configuration on injected group
  15. * Other functions (generic functions) are available in file
  16. * "stm32f1xx_hal_adc.c".
  17. *
  18. @verbatim
  19. [..]
  20. (@) Sections "ADC peripheral features" and "How to use this driver" are
  21. available in file of generic functions "stm32f1xx_hal_adc.c".
  22. [..]
  23. @endverbatim
  24. ******************************************************************************
  25. * @attention
  26. *
  27. * <h2><center>&copy; Copyright (c) 2016 STMicroelectronics.
  28. * All rights reserved.</center></h2>
  29. *
  30. * This software component is licensed by ST under BSD 3-Clause license,
  31. * the "License"; You may not use this file except in compliance with the
  32. * License. You may obtain a copy of the License at:
  33. * opensource.org/licenses/BSD-3-Clause
  34. *
  35. ******************************************************************************
  36. */
  37. /* Includes ------------------------------------------------------------------*/
  38. #include "stm32f1xx_hal.h"
  39. /** @addtogroup STM32F1xx_HAL_Driver
  40. * @{
  41. */
  42. /** @defgroup ADCEx ADCEx
  43. * @brief ADC Extension HAL module driver
  44. * @{
  45. */
  46. #ifdef HAL_ADC_MODULE_ENABLED
  47. /* Private typedef -----------------------------------------------------------*/
  48. /* Private define ------------------------------------------------------------*/
  49. /** @defgroup ADCEx_Private_Constants ADCEx Private Constants
  50. * @{
  51. */
  52. /* Delay for ADC calibration: */
  53. /* Hardware prerequisite before starting a calibration: the ADC must have */
  54. /* been in power-on state for at least two ADC clock cycles. */
  55. /* Unit: ADC clock cycles */
  56. #define ADC_PRECALIBRATION_DELAY_ADCCLOCKCYCLES 2U
  57. /* Timeout value for ADC calibration */
  58. /* Value defined to be higher than worst cases: low clocks freq, */
  59. /* maximum prescaler. */
  60. /* Ex of profile low frequency : Clock source at 0.1 MHz, ADC clock */
  61. /* prescaler 4, sampling time 12.5 ADC clock cycles, resolution 12 bits. */
  62. /* Unit: ms */
  63. #define ADC_CALIBRATION_TIMEOUT 10U
  64. /* Delay for temperature sensor stabilization time. */
  65. /* Maximum delay is 10us (refer to device datasheet, parameter tSTART). */
  66. /* Unit: us */
  67. #define ADC_TEMPSENSOR_DELAY_US 10U
  68. /**
  69. * @}
  70. */
  71. /* Private macro -------------------------------------------------------------*/
  72. /* Private variables ---------------------------------------------------------*/
  73. /* Private function prototypes -----------------------------------------------*/
  74. /* Private functions ---------------------------------------------------------*/
  75. /** @defgroup ADCEx_Exported_Functions ADCEx Exported Functions
  76. * @{
  77. */
  78. /** @defgroup ADCEx_Exported_Functions_Group1 Extended Extended IO operation functions
  79. * @brief Extended Extended Input and Output operation functions
  80. *
  81. @verbatim
  82. ===============================================================================
  83. ##### IO operation functions #####
  84. ===============================================================================
  85. [..] This section provides functions allowing to:
  86. (+) Start conversion of injected group.
  87. (+) Stop conversion of injected group.
  88. (+) Poll for conversion complete on injected group.
  89. (+) Get result of injected channel conversion.
  90. (+) Start conversion of injected group and enable interruptions.
  91. (+) Stop conversion of injected group and disable interruptions.
  92. (+) Start multimode and enable DMA transfer.
  93. (+) Stop multimode and disable ADC DMA transfer.
  94. (+) Get result of multimode conversion.
  95. (+) Perform the ADC self-calibration for single or differential ending.
  96. (+) Get calibration factors for single or differential ending.
  97. (+) Set calibration factors for single or differential ending.
  98. @endverbatim
  99. * @{
  100. */
  101. /**
  102. * @brief Perform an ADC automatic self-calibration
  103. * Calibration prerequisite: ADC must be disabled (execute this
  104. * function before HAL_ADC_Start() or after HAL_ADC_Stop() ).
  105. * During calibration process, ADC is enabled. ADC is let enabled at
  106. * the completion of this function.
  107. * @param hadc: ADC handle
  108. * @retval HAL status
  109. */
  110. HAL_StatusTypeDef HAL_ADCEx_Calibration_Start(ADC_HandleTypeDef* hadc)
  111. {
  112. HAL_StatusTypeDef tmp_hal_status = HAL_OK;
  113. uint32_t tickstart;
  114. __IO uint32_t wait_loop_index = 0U;
  115. /* Check the parameters */
  116. assert_param(IS_ADC_ALL_INSTANCE(hadc->Instance));
  117. /* Process locked */
  118. __HAL_LOCK(hadc);
  119. /* 1. Calibration prerequisite: */
  120. /* - ADC must be disabled for at least two ADC clock cycles in disable */
  121. /* mode before ADC enable */
  122. /* Stop potential conversion on going, on regular and injected groups */
  123. /* Disable ADC peripheral */
  124. tmp_hal_status = ADC_ConversionStop_Disable(hadc);
  125. /* Check if ADC is effectively disabled */
  126. if (tmp_hal_status == HAL_OK)
  127. {
  128. /* Set ADC state */
  129. ADC_STATE_CLR_SET(hadc->State,
  130. HAL_ADC_STATE_REG_BUSY | HAL_ADC_STATE_INJ_BUSY,
  131. HAL_ADC_STATE_BUSY_INTERNAL);
  132. /* Hardware prerequisite: delay before starting the calibration. */
  133. /* - Computation of CPU clock cycles corresponding to ADC clock cycles. */
  134. /* - Wait for the expected ADC clock cycles delay */
  135. wait_loop_index = ((SystemCoreClock
  136. / HAL_RCCEx_GetPeriphCLKFreq(RCC_PERIPHCLK_ADC))
  137. * ADC_PRECALIBRATION_DELAY_ADCCLOCKCYCLES );
  138. while(wait_loop_index != 0U)
  139. {
  140. wait_loop_index--;
  141. }
  142. /* 2. Enable the ADC peripheral */
  143. ADC_Enable(hadc);
  144. /* 3. Resets ADC calibration registers */
  145. SET_BIT(hadc->Instance->CR2, ADC_CR2_RSTCAL);
  146. tickstart = HAL_GetTick();
  147. /* Wait for calibration reset completion */
  148. while(HAL_IS_BIT_SET(hadc->Instance->CR2, ADC_CR2_RSTCAL))
  149. {
  150. if((HAL_GetTick() - tickstart) > ADC_CALIBRATION_TIMEOUT)
  151. {
  152. /* Update ADC state machine to error */
  153. ADC_STATE_CLR_SET(hadc->State,
  154. HAL_ADC_STATE_BUSY_INTERNAL,
  155. HAL_ADC_STATE_ERROR_INTERNAL);
  156. /* Process unlocked */
  157. __HAL_UNLOCK(hadc);
  158. return HAL_ERROR;
  159. }
  160. }
  161. /* 4. Start ADC calibration */
  162. SET_BIT(hadc->Instance->CR2, ADC_CR2_CAL);
  163. tickstart = HAL_GetTick();
  164. /* Wait for calibration completion */
  165. while(HAL_IS_BIT_SET(hadc->Instance->CR2, ADC_CR2_CAL))
  166. {
  167. if((HAL_GetTick() - tickstart) > ADC_CALIBRATION_TIMEOUT)
  168. {
  169. /* Update ADC state machine to error */
  170. ADC_STATE_CLR_SET(hadc->State,
  171. HAL_ADC_STATE_BUSY_INTERNAL,
  172. HAL_ADC_STATE_ERROR_INTERNAL);
  173. /* Process unlocked */
  174. __HAL_UNLOCK(hadc);
  175. return HAL_ERROR;
  176. }
  177. }
  178. /* Set ADC state */
  179. ADC_STATE_CLR_SET(hadc->State,
  180. HAL_ADC_STATE_BUSY_INTERNAL,
  181. HAL_ADC_STATE_READY);
  182. }
  183. /* Process unlocked */
  184. __HAL_UNLOCK(hadc);
  185. /* Return function status */
  186. return tmp_hal_status;
  187. }
  188. /**
  189. * @brief Enables ADC, starts conversion of injected group.
  190. * Interruptions enabled in this function: None.
  191. * @param hadc: ADC handle
  192. * @retval HAL status
  193. */
  194. HAL_StatusTypeDef HAL_ADCEx_InjectedStart(ADC_HandleTypeDef* hadc)
  195. {
  196. HAL_StatusTypeDef tmp_hal_status = HAL_OK;
  197. /* Check the parameters */
  198. assert_param(IS_ADC_ALL_INSTANCE(hadc->Instance));
  199. /* Process locked */
  200. __HAL_LOCK(hadc);
  201. /* Enable the ADC peripheral */
  202. tmp_hal_status = ADC_Enable(hadc);
  203. /* Start conversion if ADC is effectively enabled */
  204. if (tmp_hal_status == HAL_OK)
  205. {
  206. /* Set ADC state */
  207. /* - Clear state bitfield related to injected group conversion results */
  208. /* - Set state bitfield related to injected operation */
  209. ADC_STATE_CLR_SET(hadc->State,
  210. HAL_ADC_STATE_READY | HAL_ADC_STATE_INJ_EOC,
  211. HAL_ADC_STATE_INJ_BUSY);
  212. /* Case of independent mode or multimode (for devices with several ADCs): */
  213. /* Set multimode state. */
  214. if (ADC_NONMULTIMODE_OR_MULTIMODEMASTER(hadc))
  215. {
  216. CLEAR_BIT(hadc->State, HAL_ADC_STATE_MULTIMODE_SLAVE);
  217. }
  218. else
  219. {
  220. SET_BIT(hadc->State, HAL_ADC_STATE_MULTIMODE_SLAVE);
  221. }
  222. /* Check if a regular conversion is ongoing */
  223. /* Note: On this device, there is no ADC error code fields related to */
  224. /* conversions on group injected only. In case of conversion on */
  225. /* going on group regular, no error code is reset. */
  226. if (HAL_IS_BIT_CLR(hadc->State, HAL_ADC_STATE_REG_BUSY))
  227. {
  228. /* Reset ADC all error code fields */
  229. ADC_CLEAR_ERRORCODE(hadc);
  230. }
  231. /* Process unlocked */
  232. /* Unlock before starting ADC conversions: in case of potential */
  233. /* interruption, to let the process to ADC IRQ Handler. */
  234. __HAL_UNLOCK(hadc);
  235. /* Clear injected group conversion flag */
  236. /* (To ensure of no unknown state from potential previous ADC operations) */
  237. __HAL_ADC_CLEAR_FLAG(hadc, ADC_FLAG_JEOC);
  238. /* Enable conversion of injected group. */
  239. /* If software start has been selected, conversion starts immediately. */
  240. /* If external trigger has been selected, conversion will start at next */
  241. /* trigger event. */
  242. /* If automatic injected conversion is enabled, conversion will start */
  243. /* after next regular group conversion. */
  244. /* Case of multimode enabled (for devices with several ADCs): if ADC is */
  245. /* slave, ADC is enabled only (conversion is not started). If ADC is */
  246. /* master, ADC is enabled and conversion is started. */
  247. if (HAL_IS_BIT_CLR(hadc->Instance->CR1, ADC_CR1_JAUTO))
  248. {
  249. if (ADC_IS_SOFTWARE_START_INJECTED(hadc) &&
  250. ADC_NONMULTIMODE_OR_MULTIMODEMASTER(hadc) )
  251. {
  252. /* Start ADC conversion on injected group with SW start */
  253. SET_BIT(hadc->Instance->CR2, (ADC_CR2_JSWSTART | ADC_CR2_JEXTTRIG));
  254. }
  255. else
  256. {
  257. /* Start ADC conversion on injected group with external trigger */
  258. SET_BIT(hadc->Instance->CR2, ADC_CR2_JEXTTRIG);
  259. }
  260. }
  261. }
  262. else
  263. {
  264. /* Process unlocked */
  265. __HAL_UNLOCK(hadc);
  266. }
  267. /* Return function status */
  268. return tmp_hal_status;
  269. }
  270. /**
  271. * @brief Stop conversion of injected channels. Disable ADC peripheral if
  272. * no regular conversion is on going.
  273. * @note If ADC must be disabled and if conversion is on going on
  274. * regular group, function HAL_ADC_Stop must be used to stop both
  275. * injected and regular groups, and disable the ADC.
  276. * @note If injected group mode auto-injection is enabled,
  277. * function HAL_ADC_Stop must be used.
  278. * @note In case of auto-injection mode, HAL_ADC_Stop must be used.
  279. * @param hadc: ADC handle
  280. * @retval None
  281. */
  282. HAL_StatusTypeDef HAL_ADCEx_InjectedStop(ADC_HandleTypeDef* hadc)
  283. {
  284. HAL_StatusTypeDef tmp_hal_status = HAL_OK;
  285. /* Check the parameters */
  286. assert_param(IS_ADC_ALL_INSTANCE(hadc->Instance));
  287. /* Process locked */
  288. __HAL_LOCK(hadc);
  289. /* Stop potential conversion and disable ADC peripheral */
  290. /* Conditioned to: */
  291. /* - No conversion on the other group (regular group) is intended to */
  292. /* continue (injected and regular groups stop conversion and ADC disable */
  293. /* are common) */
  294. /* - In case of auto-injection mode, HAL_ADC_Stop must be used. */
  295. if(((hadc->State & HAL_ADC_STATE_REG_BUSY) == RESET) &&
  296. HAL_IS_BIT_CLR(hadc->Instance->CR1, ADC_CR1_JAUTO) )
  297. {
  298. /* Stop potential conversion on going, on regular and injected groups */
  299. /* Disable ADC peripheral */
  300. tmp_hal_status = ADC_ConversionStop_Disable(hadc);
  301. /* Check if ADC is effectively disabled */
  302. if (tmp_hal_status == HAL_OK)
  303. {
  304. /* Set ADC state */
  305. ADC_STATE_CLR_SET(hadc->State,
  306. HAL_ADC_STATE_REG_BUSY | HAL_ADC_STATE_INJ_BUSY,
  307. HAL_ADC_STATE_READY);
  308. }
  309. }
  310. else
  311. {
  312. /* Update ADC state machine to error */
  313. SET_BIT(hadc->State, HAL_ADC_STATE_ERROR_CONFIG);
  314. tmp_hal_status = HAL_ERROR;
  315. }
  316. /* Process unlocked */
  317. __HAL_UNLOCK(hadc);
  318. /* Return function status */
  319. return tmp_hal_status;
  320. }
  321. /**
  322. * @brief Wait for injected group conversion to be completed.
  323. * @param hadc: ADC handle
  324. * @param Timeout: Timeout value in millisecond.
  325. * @retval HAL status
  326. */
  327. HAL_StatusTypeDef HAL_ADCEx_InjectedPollForConversion(ADC_HandleTypeDef* hadc, uint32_t Timeout)
  328. {
  329. uint32_t tickstart;
  330. /* Variables for polling in case of scan mode enabled and polling for each */
  331. /* conversion. */
  332. __IO uint32_t Conversion_Timeout_CPU_cycles = 0U;
  333. uint32_t Conversion_Timeout_CPU_cycles_max = 0U;
  334. /* Check the parameters */
  335. assert_param(IS_ADC_ALL_INSTANCE(hadc->Instance));
  336. /* Get timeout */
  337. tickstart = HAL_GetTick();
  338. /* Polling for end of conversion: differentiation if single/sequence */
  339. /* conversion. */
  340. /* For injected group, flag JEOC is set only at the end of the sequence, */
  341. /* not for each conversion within the sequence. */
  342. /* - If single conversion for injected group (scan mode disabled or */
  343. /* InjectedNbrOfConversion ==1), flag JEOC is used to determine the */
  344. /* conversion completion. */
  345. /* - If sequence conversion for injected group (scan mode enabled and */
  346. /* InjectedNbrOfConversion >=2), flag JEOC is set only at the end of the */
  347. /* sequence. */
  348. /* To poll for each conversion, the maximum conversion time is computed */
  349. /* from ADC conversion time (selected sampling time + conversion time of */
  350. /* 12.5 ADC clock cycles) and APB2/ADC clock prescalers (depending on */
  351. /* settings, conversion time range can be from 28 to 32256 CPU cycles). */
  352. /* As flag JEOC is not set after each conversion, no timeout status can */
  353. /* be set. */
  354. if ((hadc->Instance->JSQR & ADC_JSQR_JL) == RESET)
  355. {
  356. /* Wait until End of Conversion flag is raised */
  357. while(HAL_IS_BIT_CLR(hadc->Instance->SR, ADC_FLAG_JEOC))
  358. {
  359. /* Check if timeout is disabled (set to infinite wait) */
  360. if(Timeout != HAL_MAX_DELAY)
  361. {
  362. if((Timeout == 0U) || ((HAL_GetTick() - tickstart ) > Timeout))
  363. {
  364. /* Update ADC state machine to timeout */
  365. SET_BIT(hadc->State, HAL_ADC_STATE_TIMEOUT);
  366. /* Process unlocked */
  367. __HAL_UNLOCK(hadc);
  368. return HAL_TIMEOUT;
  369. }
  370. }
  371. }
  372. }
  373. else
  374. {
  375. /* Replace polling by wait for maximum conversion time */
  376. /* - Computation of CPU clock cycles corresponding to ADC clock cycles */
  377. /* and ADC maximum conversion cycles on all channels. */
  378. /* - Wait for the expected ADC clock cycles delay */
  379. Conversion_Timeout_CPU_cycles_max = ((SystemCoreClock
  380. / HAL_RCCEx_GetPeriphCLKFreq(RCC_PERIPHCLK_ADC))
  381. * ADC_CONVCYCLES_MAX_RANGE(hadc) );
  382. while(Conversion_Timeout_CPU_cycles < Conversion_Timeout_CPU_cycles_max)
  383. {
  384. /* Check if timeout is disabled (set to infinite wait) */
  385. if(Timeout != HAL_MAX_DELAY)
  386. {
  387. if((Timeout == 0)||((HAL_GetTick() - tickstart ) > Timeout))
  388. {
  389. /* Update ADC state machine to timeout */
  390. SET_BIT(hadc->State, HAL_ADC_STATE_TIMEOUT);
  391. /* Process unlocked */
  392. __HAL_UNLOCK(hadc);
  393. return HAL_TIMEOUT;
  394. }
  395. }
  396. Conversion_Timeout_CPU_cycles ++;
  397. }
  398. }
  399. /* Clear injected group conversion flag */
  400. /* Note: On STM32F1 ADC, clear regular conversion flag raised */
  401. /* simultaneously. */
  402. __HAL_ADC_CLEAR_FLAG(hadc, ADC_FLAG_JSTRT | ADC_FLAG_JEOC | ADC_FLAG_EOC);
  403. /* Update ADC state machine */
  404. SET_BIT(hadc->State, HAL_ADC_STATE_INJ_EOC);
  405. /* Determine whether any further conversion upcoming on group injected */
  406. /* by external trigger or by automatic injected conversion */
  407. /* from group regular. */
  408. if(ADC_IS_SOFTWARE_START_INJECTED(hadc) ||
  409. (HAL_IS_BIT_CLR(hadc->Instance->CR1, ADC_CR1_JAUTO) &&
  410. (ADC_IS_SOFTWARE_START_REGULAR(hadc) &&
  411. (hadc->Init.ContinuousConvMode == DISABLE) ) ) )
  412. {
  413. /* Set ADC state */
  414. CLEAR_BIT(hadc->State, HAL_ADC_STATE_INJ_BUSY);
  415. if (HAL_IS_BIT_CLR(hadc->State, HAL_ADC_STATE_REG_BUSY))
  416. {
  417. SET_BIT(hadc->State, HAL_ADC_STATE_READY);
  418. }
  419. }
  420. /* Return ADC state */
  421. return HAL_OK;
  422. }
  423. /**
  424. * @brief Enables ADC, starts conversion of injected group with interruption.
  425. * - JEOC (end of conversion of injected group)
  426. * Each of these interruptions has its dedicated callback function.
  427. * @param hadc: ADC handle
  428. * @retval HAL status.
  429. */
  430. HAL_StatusTypeDef HAL_ADCEx_InjectedStart_IT(ADC_HandleTypeDef* hadc)
  431. {
  432. HAL_StatusTypeDef tmp_hal_status = HAL_OK;
  433. /* Check the parameters */
  434. assert_param(IS_ADC_ALL_INSTANCE(hadc->Instance));
  435. /* Process locked */
  436. __HAL_LOCK(hadc);
  437. /* Enable the ADC peripheral */
  438. tmp_hal_status = ADC_Enable(hadc);
  439. /* Start conversion if ADC is effectively enabled */
  440. if (tmp_hal_status == HAL_OK)
  441. {
  442. /* Set ADC state */
  443. /* - Clear state bitfield related to injected group conversion results */
  444. /* - Set state bitfield related to injected operation */
  445. ADC_STATE_CLR_SET(hadc->State,
  446. HAL_ADC_STATE_READY | HAL_ADC_STATE_INJ_EOC,
  447. HAL_ADC_STATE_INJ_BUSY);
  448. /* Case of independent mode or multimode (for devices with several ADCs): */
  449. /* Set multimode state. */
  450. if (ADC_NONMULTIMODE_OR_MULTIMODEMASTER(hadc))
  451. {
  452. CLEAR_BIT(hadc->State, HAL_ADC_STATE_MULTIMODE_SLAVE);
  453. }
  454. else
  455. {
  456. SET_BIT(hadc->State, HAL_ADC_STATE_MULTIMODE_SLAVE);
  457. }
  458. /* Check if a regular conversion is ongoing */
  459. /* Note: On this device, there is no ADC error code fields related to */
  460. /* conversions on group injected only. In case of conversion on */
  461. /* going on group regular, no error code is reset. */
  462. if (HAL_IS_BIT_CLR(hadc->State, HAL_ADC_STATE_REG_BUSY))
  463. {
  464. /* Reset ADC all error code fields */
  465. ADC_CLEAR_ERRORCODE(hadc);
  466. }
  467. /* Process unlocked */
  468. /* Unlock before starting ADC conversions: in case of potential */
  469. /* interruption, to let the process to ADC IRQ Handler. */
  470. __HAL_UNLOCK(hadc);
  471. /* Clear injected group conversion flag */
  472. /* (To ensure of no unknown state from potential previous ADC operations) */
  473. __HAL_ADC_CLEAR_FLAG(hadc, ADC_FLAG_JEOC);
  474. /* Enable end of conversion interrupt for injected channels */
  475. __HAL_ADC_ENABLE_IT(hadc, ADC_IT_JEOC);
  476. /* Start conversion of injected group if software start has been selected */
  477. /* and if automatic injected conversion is disabled. */
  478. /* If external trigger has been selected, conversion will start at next */
  479. /* trigger event. */
  480. /* If automatic injected conversion is enabled, conversion will start */
  481. /* after next regular group conversion. */
  482. if (HAL_IS_BIT_CLR(hadc->Instance->CR1, ADC_CR1_JAUTO))
  483. {
  484. if (ADC_IS_SOFTWARE_START_INJECTED(hadc) &&
  485. ADC_NONMULTIMODE_OR_MULTIMODEMASTER(hadc) )
  486. {
  487. /* Start ADC conversion on injected group with SW start */
  488. SET_BIT(hadc->Instance->CR2, (ADC_CR2_JSWSTART | ADC_CR2_JEXTTRIG));
  489. }
  490. else
  491. {
  492. /* Start ADC conversion on injected group with external trigger */
  493. SET_BIT(hadc->Instance->CR2, ADC_CR2_JEXTTRIG);
  494. }
  495. }
  496. }
  497. else
  498. {
  499. /* Process unlocked */
  500. __HAL_UNLOCK(hadc);
  501. }
  502. /* Return function status */
  503. return tmp_hal_status;
  504. }
  505. /**
  506. * @brief Stop conversion of injected channels, disable interruption of
  507. * end-of-conversion. Disable ADC peripheral if no regular conversion
  508. * is on going.
  509. * @note If ADC must be disabled and if conversion is on going on
  510. * regular group, function HAL_ADC_Stop must be used to stop both
  511. * injected and regular groups, and disable the ADC.
  512. * @note If injected group mode auto-injection is enabled,
  513. * function HAL_ADC_Stop must be used.
  514. * @param hadc: ADC handle
  515. * @retval None
  516. */
  517. HAL_StatusTypeDef HAL_ADCEx_InjectedStop_IT(ADC_HandleTypeDef* hadc)
  518. {
  519. HAL_StatusTypeDef tmp_hal_status = HAL_OK;
  520. /* Check the parameters */
  521. assert_param(IS_ADC_ALL_INSTANCE(hadc->Instance));
  522. /* Process locked */
  523. __HAL_LOCK(hadc);
  524. /* Stop potential conversion and disable ADC peripheral */
  525. /* Conditioned to: */
  526. /* - No conversion on the other group (regular group) is intended to */
  527. /* continue (injected and regular groups stop conversion and ADC disable */
  528. /* are common) */
  529. /* - In case of auto-injection mode, HAL_ADC_Stop must be used. */
  530. if(((hadc->State & HAL_ADC_STATE_REG_BUSY) == RESET) &&
  531. HAL_IS_BIT_CLR(hadc->Instance->CR1, ADC_CR1_JAUTO) )
  532. {
  533. /* Stop potential conversion on going, on regular and injected groups */
  534. /* Disable ADC peripheral */
  535. tmp_hal_status = ADC_ConversionStop_Disable(hadc);
  536. /* Check if ADC is effectively disabled */
  537. if (tmp_hal_status == HAL_OK)
  538. {
  539. /* Disable ADC end of conversion interrupt for injected channels */
  540. __HAL_ADC_DISABLE_IT(hadc, ADC_IT_JEOC);
  541. /* Set ADC state */
  542. ADC_STATE_CLR_SET(hadc->State,
  543. HAL_ADC_STATE_REG_BUSY | HAL_ADC_STATE_INJ_BUSY,
  544. HAL_ADC_STATE_READY);
  545. }
  546. }
  547. else
  548. {
  549. /* Update ADC state machine to error */
  550. SET_BIT(hadc->State, HAL_ADC_STATE_ERROR_CONFIG);
  551. tmp_hal_status = HAL_ERROR;
  552. }
  553. /* Process unlocked */
  554. __HAL_UNLOCK(hadc);
  555. /* Return function status */
  556. return tmp_hal_status;
  557. }
  558. #if defined (STM32F103x6) || defined (STM32F103xB) || defined (STM32F105xC) || defined (STM32F107xC) || defined (STM32F103xE) || defined (STM32F103xG)
  559. /**
  560. * @brief Enables ADC, starts conversion of regular group and transfers result
  561. * through DMA.
  562. * Multimode must have been previously configured using
  563. * HAL_ADCEx_MultiModeConfigChannel() function.
  564. * Interruptions enabled in this function:
  565. * - DMA transfer complete
  566. * - DMA half transfer
  567. * Each of these interruptions has its dedicated callback function.
  568. * @note: On STM32F1 devices, ADC slave regular group must be configured
  569. * with conversion trigger ADC_SOFTWARE_START.
  570. * @note: ADC slave can be enabled preliminarily using single-mode
  571. * HAL_ADC_Start() function.
  572. * @param hadc: ADC handle of ADC master (handle of ADC slave must not be used)
  573. * @param pData: The destination Buffer address.
  574. * @param Length: The length of data to be transferred from ADC peripheral to memory.
  575. * @retval None
  576. */
  577. HAL_StatusTypeDef HAL_ADCEx_MultiModeStart_DMA(ADC_HandleTypeDef* hadc, uint32_t* pData, uint32_t Length)
  578. {
  579. HAL_StatusTypeDef tmp_hal_status = HAL_OK;
  580. ADC_HandleTypeDef tmphadcSlave;
  581. /* Check the parameters */
  582. assert_param(IS_ADC_MULTIMODE_MASTER_INSTANCE(hadc->Instance));
  583. assert_param(IS_FUNCTIONAL_STATE(hadc->Init.ContinuousConvMode));
  584. /* Process locked */
  585. __HAL_LOCK(hadc);
  586. /* Set a temporary handle of the ADC slave associated to the ADC master */
  587. ADC_MULTI_SLAVE(hadc, &tmphadcSlave);
  588. /* On STM32F1 devices, ADC slave regular group must be configured with */
  589. /* conversion trigger ADC_SOFTWARE_START. */
  590. /* Note: External trigger of ADC slave must be enabled, it is already done */
  591. /* into function "HAL_ADC_Init()". */
  592. if(!ADC_IS_SOFTWARE_START_REGULAR(&tmphadcSlave))
  593. {
  594. /* Update ADC state machine to error */
  595. SET_BIT(hadc->State, HAL_ADC_STATE_ERROR_CONFIG);
  596. /* Process unlocked */
  597. __HAL_UNLOCK(hadc);
  598. return HAL_ERROR;
  599. }
  600. /* Enable the ADC peripherals: master and slave (in case if not already */
  601. /* enabled previously) */
  602. tmp_hal_status = ADC_Enable(hadc);
  603. if (tmp_hal_status == HAL_OK)
  604. {
  605. tmp_hal_status = ADC_Enable(&tmphadcSlave);
  606. }
  607. /* Start conversion if all ADCs of multimode are effectively enabled */
  608. if (tmp_hal_status == HAL_OK)
  609. {
  610. /* Set ADC state (ADC master) */
  611. /* - Clear state bitfield related to regular group conversion results */
  612. /* - Set state bitfield related to regular operation */
  613. ADC_STATE_CLR_SET(hadc->State,
  614. HAL_ADC_STATE_READY | HAL_ADC_STATE_REG_EOC | HAL_ADC_STATE_MULTIMODE_SLAVE,
  615. HAL_ADC_STATE_REG_BUSY);
  616. /* If conversions on group regular are also triggering group injected, */
  617. /* update ADC state. */
  618. if (READ_BIT(hadc->Instance->CR1, ADC_CR1_JAUTO) != RESET)
  619. {
  620. ADC_STATE_CLR_SET(hadc->State, HAL_ADC_STATE_INJ_EOC, HAL_ADC_STATE_INJ_BUSY);
  621. }
  622. /* Process unlocked */
  623. /* Unlock before starting ADC conversions: in case of potential */
  624. /* interruption, to let the process to ADC IRQ Handler. */
  625. __HAL_UNLOCK(hadc);
  626. /* Set ADC error code to none */
  627. ADC_CLEAR_ERRORCODE(hadc);
  628. /* Set the DMA transfer complete callback */
  629. hadc->DMA_Handle->XferCpltCallback = ADC_DMAConvCplt;
  630. /* Set the DMA half transfer complete callback */
  631. hadc->DMA_Handle->XferHalfCpltCallback = ADC_DMAHalfConvCplt;
  632. /* Set the DMA error callback */
  633. hadc->DMA_Handle->XferErrorCallback = ADC_DMAError;
  634. /* Manage ADC and DMA start: ADC overrun interruption, DMA start, ADC */
  635. /* start (in case of SW start): */
  636. /* Clear regular group conversion flag and overrun flag */
  637. /* (To ensure of no unknown state from potential previous ADC operations) */
  638. __HAL_ADC_CLEAR_FLAG(hadc, ADC_FLAG_EOC);
  639. /* Enable ADC DMA mode of ADC master */
  640. SET_BIT(hadc->Instance->CR2, ADC_CR2_DMA);
  641. /* Start the DMA channel */
  642. HAL_DMA_Start_IT(hadc->DMA_Handle, (uint32_t)&hadc->Instance->DR, (uint32_t)pData, Length);
  643. /* Start conversion of regular group if software start has been selected. */
  644. /* If external trigger has been selected, conversion will start at next */
  645. /* trigger event. */
  646. /* Note: Alternate trigger for single conversion could be to force an */
  647. /* additional set of bit ADON "hadc->Instance->CR2 |= ADC_CR2_ADON;"*/
  648. if (ADC_IS_SOFTWARE_START_REGULAR(hadc))
  649. {
  650. /* Start ADC conversion on regular group with SW start */
  651. SET_BIT(hadc->Instance->CR2, (ADC_CR2_SWSTART | ADC_CR2_EXTTRIG));
  652. }
  653. else
  654. {
  655. /* Start ADC conversion on regular group with external trigger */
  656. SET_BIT(hadc->Instance->CR2, ADC_CR2_EXTTRIG);
  657. }
  658. }
  659. else
  660. {
  661. /* Process unlocked */
  662. __HAL_UNLOCK(hadc);
  663. }
  664. /* Return function status */
  665. return tmp_hal_status;
  666. }
  667. /**
  668. * @brief Stop ADC conversion of regular group (and injected channels in
  669. * case of auto_injection mode), disable ADC DMA transfer, disable
  670. * ADC peripheral.
  671. * @note Multimode is kept enabled after this function. To disable multimode
  672. * (set with HAL_ADCEx_MultiModeConfigChannel(), ADC must be
  673. * reinitialized using HAL_ADC_Init() or HAL_ADC_ReInit().
  674. * @note In case of DMA configured in circular mode, function
  675. * HAL_ADC_Stop_DMA must be called after this function with handle of
  676. * ADC slave, to properly disable the DMA channel.
  677. * @param hadc: ADC handle of ADC master (handle of ADC slave must not be used)
  678. * @retval None
  679. */
  680. HAL_StatusTypeDef HAL_ADCEx_MultiModeStop_DMA(ADC_HandleTypeDef* hadc)
  681. {
  682. HAL_StatusTypeDef tmp_hal_status = HAL_OK;
  683. ADC_HandleTypeDef tmphadcSlave;
  684. /* Check the parameters */
  685. assert_param(IS_ADC_MULTIMODE_MASTER_INSTANCE(hadc->Instance));
  686. /* Process locked */
  687. __HAL_LOCK(hadc);
  688. /* Stop potential conversion on going, on regular and injected groups */
  689. /* Disable ADC master peripheral */
  690. tmp_hal_status = ADC_ConversionStop_Disable(hadc);
  691. /* Check if ADC is effectively disabled */
  692. if(tmp_hal_status == HAL_OK)
  693. {
  694. /* Set a temporary handle of the ADC slave associated to the ADC master */
  695. ADC_MULTI_SLAVE(hadc, &tmphadcSlave);
  696. /* Disable ADC slave peripheral */
  697. tmp_hal_status = ADC_ConversionStop_Disable(&tmphadcSlave);
  698. /* Check if ADC is effectively disabled */
  699. if(tmp_hal_status != HAL_OK)
  700. {
  701. /* Update ADC state machine to error */
  702. SET_BIT(hadc->State, HAL_ADC_STATE_ERROR_INTERNAL);
  703. /* Process unlocked */
  704. __HAL_UNLOCK(hadc);
  705. return HAL_ERROR;
  706. }
  707. /* Disable ADC DMA mode */
  708. CLEAR_BIT(hadc->Instance->CR2, ADC_CR2_DMA);
  709. /* Reset configuration of ADC DMA continuous request for dual mode */
  710. CLEAR_BIT(hadc->Instance->CR1, ADC_CR1_DUALMOD);
  711. /* Disable the DMA channel (in case of DMA in circular mode or stop while */
  712. /* while DMA transfer is on going) */
  713. tmp_hal_status = HAL_DMA_Abort(hadc->DMA_Handle);
  714. /* Change ADC state (ADC master) */
  715. ADC_STATE_CLR_SET(hadc->State,
  716. HAL_ADC_STATE_REG_BUSY | HAL_ADC_STATE_INJ_BUSY,
  717. HAL_ADC_STATE_READY);
  718. }
  719. /* Process unlocked */
  720. __HAL_UNLOCK(hadc);
  721. /* Return function status */
  722. return tmp_hal_status;
  723. }
  724. #endif /* defined STM32F103x6 || defined STM32F103xB || defined STM32F105xC || defined STM32F107xC || defined STM32F103xE || defined STM32F103xG */
  725. /**
  726. * @brief Get ADC injected group conversion result.
  727. * @note Reading register JDRx automatically clears ADC flag JEOC
  728. * (ADC group injected end of unitary conversion).
  729. * @note This function does not clear ADC flag JEOS
  730. * (ADC group injected end of sequence conversion)
  731. * Occurrence of flag JEOS rising:
  732. * - If sequencer is composed of 1 rank, flag JEOS is equivalent
  733. * to flag JEOC.
  734. * - If sequencer is composed of several ranks, during the scan
  735. * sequence flag JEOC only is raised, at the end of the scan sequence
  736. * both flags JEOC and EOS are raised.
  737. * Flag JEOS must not be cleared by this function because
  738. * it would not be compliant with low power features
  739. * (feature low power auto-wait, not available on all STM32 families).
  740. * To clear this flag, either use function:
  741. * in programming model IT: @ref HAL_ADC_IRQHandler(), in programming
  742. * model polling: @ref HAL_ADCEx_InjectedPollForConversion()
  743. * or @ref __HAL_ADC_CLEAR_FLAG(&hadc, ADC_FLAG_JEOS).
  744. * @param hadc: ADC handle
  745. * @param InjectedRank: the converted ADC injected rank.
  746. * This parameter can be one of the following values:
  747. * @arg ADC_INJECTED_RANK_1: Injected Channel1 selected
  748. * @arg ADC_INJECTED_RANK_2: Injected Channel2 selected
  749. * @arg ADC_INJECTED_RANK_3: Injected Channel3 selected
  750. * @arg ADC_INJECTED_RANK_4: Injected Channel4 selected
  751. * @retval ADC group injected conversion data
  752. */
  753. uint32_t HAL_ADCEx_InjectedGetValue(ADC_HandleTypeDef* hadc, uint32_t InjectedRank)
  754. {
  755. uint32_t tmp_jdr = 0U;
  756. /* Check the parameters */
  757. assert_param(IS_ADC_ALL_INSTANCE(hadc->Instance));
  758. assert_param(IS_ADC_INJECTED_RANK(InjectedRank));
  759. /* Get ADC converted value */
  760. switch(InjectedRank)
  761. {
  762. case ADC_INJECTED_RANK_4:
  763. tmp_jdr = hadc->Instance->JDR4;
  764. break;
  765. case ADC_INJECTED_RANK_3:
  766. tmp_jdr = hadc->Instance->JDR3;
  767. break;
  768. case ADC_INJECTED_RANK_2:
  769. tmp_jdr = hadc->Instance->JDR2;
  770. break;
  771. case ADC_INJECTED_RANK_1:
  772. default:
  773. tmp_jdr = hadc->Instance->JDR1;
  774. break;
  775. }
  776. /* Return ADC converted value */
  777. return tmp_jdr;
  778. }
  779. #if defined (STM32F103x6) || defined (STM32F103xB) || defined (STM32F105xC) || defined (STM32F107xC) || defined (STM32F103xE) || defined (STM32F103xG)
  780. /**
  781. * @brief Returns the last ADC Master&Slave regular conversions results data
  782. * in the selected multi mode.
  783. * @param hadc: ADC handle of ADC master (handle of ADC slave must not be used)
  784. * @retval The converted data value.
  785. */
  786. uint32_t HAL_ADCEx_MultiModeGetValue(ADC_HandleTypeDef* hadc)
  787. {
  788. uint32_t tmpDR = 0U;
  789. /* Check the parameters */
  790. assert_param(IS_ADC_MULTIMODE_MASTER_INSTANCE(hadc->Instance));
  791. /* Check the parameters */
  792. assert_param(IS_ADC_ALL_INSTANCE(hadc->Instance));
  793. /* Note: EOC flag is not cleared here by software because automatically */
  794. /* cleared by hardware when reading register DR. */
  795. /* On STM32F1 devices, ADC1 data register DR contains ADC2 conversions */
  796. /* only if ADC1 DMA mode is enabled. */
  797. tmpDR = hadc->Instance->DR;
  798. if (HAL_IS_BIT_CLR(ADC1->CR2, ADC_CR2_DMA))
  799. {
  800. tmpDR |= (ADC2->DR << 16U);
  801. }
  802. /* Return ADC converted value */
  803. return tmpDR;
  804. }
  805. #endif /* defined STM32F103x6 || defined STM32F103xB || defined STM32F105xC || defined STM32F107xC || defined STM32F103xE || defined STM32F103xG */
  806. /**
  807. * @brief Injected conversion complete callback in non blocking mode
  808. * @param hadc: ADC handle
  809. * @retval None
  810. */
  811. __weak void HAL_ADCEx_InjectedConvCpltCallback(ADC_HandleTypeDef* hadc)
  812. {
  813. /* Prevent unused argument(s) compilation warning */
  814. UNUSED(hadc);
  815. /* NOTE : This function Should not be modified, when the callback is needed,
  816. the HAL_ADCEx_InjectedConvCpltCallback could be implemented in the user file
  817. */
  818. }
  819. /**
  820. * @}
  821. */
  822. /** @defgroup ADCEx_Exported_Functions_Group2 Extended Peripheral Control functions
  823. * @brief Extended Peripheral Control functions
  824. *
  825. @verbatim
  826. ===============================================================================
  827. ##### Peripheral Control functions #####
  828. ===============================================================================
  829. [..] This section provides functions allowing to:
  830. (+) Configure channels on injected group
  831. (+) Configure multimode
  832. @endverbatim
  833. * @{
  834. */
  835. /**
  836. * @brief Configures the ADC injected group and the selected channel to be
  837. * linked to the injected group.
  838. * @note Possibility to update parameters on the fly:
  839. * This function initializes injected group, following calls to this
  840. * function can be used to reconfigure some parameters of structure
  841. * "ADC_InjectionConfTypeDef" on the fly, without reseting the ADC.
  842. * The setting of these parameters is conditioned to ADC state:
  843. * this function must be called when ADC is not under conversion.
  844. * @param hadc: ADC handle
  845. * @param sConfigInjected: Structure of ADC injected group and ADC channel for
  846. * injected group.
  847. * @retval None
  848. */
  849. HAL_StatusTypeDef HAL_ADCEx_InjectedConfigChannel(ADC_HandleTypeDef* hadc, ADC_InjectionConfTypeDef* sConfigInjected)
  850. {
  851. HAL_StatusTypeDef tmp_hal_status = HAL_OK;
  852. __IO uint32_t wait_loop_index = 0U;
  853. /* Check the parameters */
  854. assert_param(IS_ADC_ALL_INSTANCE(hadc->Instance));
  855. assert_param(IS_ADC_CHANNEL(sConfigInjected->InjectedChannel));
  856. assert_param(IS_ADC_SAMPLE_TIME(sConfigInjected->InjectedSamplingTime));
  857. assert_param(IS_FUNCTIONAL_STATE(sConfigInjected->AutoInjectedConv));
  858. assert_param(IS_ADC_EXTTRIGINJEC(sConfigInjected->ExternalTrigInjecConv));
  859. assert_param(IS_ADC_RANGE(sConfigInjected->InjectedOffset));
  860. if(hadc->Init.ScanConvMode != ADC_SCAN_DISABLE)
  861. {
  862. assert_param(IS_ADC_INJECTED_RANK(sConfigInjected->InjectedRank));
  863. assert_param(IS_ADC_INJECTED_NB_CONV(sConfigInjected->InjectedNbrOfConversion));
  864. assert_param(IS_FUNCTIONAL_STATE(sConfigInjected->InjectedDiscontinuousConvMode));
  865. }
  866. /* Process locked */
  867. __HAL_LOCK(hadc);
  868. /* Configuration of injected group sequencer: */
  869. /* - if scan mode is disabled, injected channels sequence length is set to */
  870. /* 0x00: 1 channel converted (channel on regular rank 1) */
  871. /* Parameter "InjectedNbrOfConversion" is discarded. */
  872. /* Note: Scan mode is present by hardware on this device and, if */
  873. /* disabled, discards automatically nb of conversions. Anyway, nb of */
  874. /* conversions is forced to 0x00 for alignment over all STM32 devices. */
  875. /* - if scan mode is enabled, injected channels sequence length is set to */
  876. /* parameter "InjectedNbrOfConversion". */
  877. if (hadc->Init.ScanConvMode == ADC_SCAN_DISABLE)
  878. {
  879. if (sConfigInjected->InjectedRank == ADC_INJECTED_RANK_1)
  880. {
  881. /* Clear the old SQx bits for all injected ranks */
  882. MODIFY_REG(hadc->Instance->JSQR ,
  883. ADC_JSQR_JL |
  884. ADC_JSQR_JSQ4 |
  885. ADC_JSQR_JSQ3 |
  886. ADC_JSQR_JSQ2 |
  887. ADC_JSQR_JSQ1 ,
  888. ADC_JSQR_RK_JL(sConfigInjected->InjectedChannel,
  889. ADC_INJECTED_RANK_1,
  890. 0x01U));
  891. }
  892. /* If another injected rank than rank1 was intended to be set, and could */
  893. /* not due to ScanConvMode disabled, error is reported. */
  894. else
  895. {
  896. /* Update ADC state machine to error */
  897. SET_BIT(hadc->State, HAL_ADC_STATE_ERROR_CONFIG);
  898. tmp_hal_status = HAL_ERROR;
  899. }
  900. }
  901. else
  902. {
  903. /* Since injected channels rank conv. order depends on total number of */
  904. /* injected conversions, selected rank must be below or equal to total */
  905. /* number of injected conversions to be updated. */
  906. if (sConfigInjected->InjectedRank <= sConfigInjected->InjectedNbrOfConversion)
  907. {
  908. /* Clear the old SQx bits for the selected rank */
  909. /* Set the SQx bits for the selected rank */
  910. MODIFY_REG(hadc->Instance->JSQR ,
  911. ADC_JSQR_JL |
  912. ADC_JSQR_RK_JL(ADC_JSQR_JSQ1,
  913. sConfigInjected->InjectedRank,
  914. sConfigInjected->InjectedNbrOfConversion) ,
  915. ADC_JSQR_JL_SHIFT(sConfigInjected->InjectedNbrOfConversion) |
  916. ADC_JSQR_RK_JL(sConfigInjected->InjectedChannel,
  917. sConfigInjected->InjectedRank,
  918. sConfigInjected->InjectedNbrOfConversion) );
  919. }
  920. else
  921. {
  922. /* Clear the old SQx bits for the selected rank */
  923. MODIFY_REG(hadc->Instance->JSQR ,
  924. ADC_JSQR_JL |
  925. ADC_JSQR_RK_JL(ADC_JSQR_JSQ1,
  926. sConfigInjected->InjectedRank,
  927. sConfigInjected->InjectedNbrOfConversion) ,
  928. 0x00000000U);
  929. }
  930. }
  931. /* Configuration of injected group */
  932. /* Parameters update conditioned to ADC state: */
  933. /* Parameters that can be updated only when ADC is disabled: */
  934. /* - external trigger to start conversion */
  935. /* Parameters update not conditioned to ADC state: */
  936. /* - Automatic injected conversion */
  937. /* - Injected discontinuous mode */
  938. /* Note: In case of ADC already enabled, caution to not launch an unwanted */
  939. /* conversion while modifying register CR2 by writing 1 to bit ADON. */
  940. if (ADC_IS_ENABLE(hadc) == RESET)
  941. {
  942. MODIFY_REG(hadc->Instance->CR2 ,
  943. ADC_CR2_JEXTSEL |
  944. ADC_CR2_ADON ,
  945. ADC_CFGR_JEXTSEL(hadc, sConfigInjected->ExternalTrigInjecConv) );
  946. }
  947. /* Configuration of injected group */
  948. /* - Automatic injected conversion */
  949. /* - Injected discontinuous mode */
  950. /* Automatic injected conversion can be enabled if injected group */
  951. /* external triggers are disabled. */
  952. if (sConfigInjected->AutoInjectedConv == ENABLE)
  953. {
  954. if (sConfigInjected->ExternalTrigInjecConv == ADC_INJECTED_SOFTWARE_START)
  955. {
  956. SET_BIT(hadc->Instance->CR1, ADC_CR1_JAUTO);
  957. }
  958. else
  959. {
  960. /* Update ADC state machine to error */
  961. SET_BIT(hadc->State, HAL_ADC_STATE_ERROR_CONFIG);
  962. tmp_hal_status = HAL_ERROR;
  963. }
  964. }
  965. /* Injected discontinuous can be enabled only if auto-injected mode is */
  966. /* disabled. */
  967. if (sConfigInjected->InjectedDiscontinuousConvMode == ENABLE)
  968. {
  969. if (sConfigInjected->AutoInjectedConv == DISABLE)
  970. {
  971. SET_BIT(hadc->Instance->CR1, ADC_CR1_JDISCEN);
  972. }
  973. else
  974. {
  975. /* Update ADC state machine to error */
  976. SET_BIT(hadc->State, HAL_ADC_STATE_ERROR_CONFIG);
  977. tmp_hal_status = HAL_ERROR;
  978. }
  979. }
  980. /* InjectedChannel sampling time configuration */
  981. /* For channels 10 to 17 */
  982. if (sConfigInjected->InjectedChannel >= ADC_CHANNEL_10)
  983. {
  984. MODIFY_REG(hadc->Instance->SMPR1 ,
  985. ADC_SMPR1(ADC_SMPR1_SMP10, sConfigInjected->InjectedChannel) ,
  986. ADC_SMPR1(sConfigInjected->InjectedSamplingTime, sConfigInjected->InjectedChannel) );
  987. }
  988. else /* For channels 0 to 9 */
  989. {
  990. MODIFY_REG(hadc->Instance->SMPR2 ,
  991. ADC_SMPR2(ADC_SMPR2_SMP0, sConfigInjected->InjectedChannel) ,
  992. ADC_SMPR2(sConfigInjected->InjectedSamplingTime, sConfigInjected->InjectedChannel) );
  993. }
  994. /* If ADC1 InjectedChannel_16 or InjectedChannel_17 is selected, enable Temperature sensor */
  995. /* and VREFINT measurement path. */
  996. if ((sConfigInjected->InjectedChannel == ADC_CHANNEL_TEMPSENSOR) ||
  997. (sConfigInjected->InjectedChannel == ADC_CHANNEL_VREFINT) )
  998. {
  999. SET_BIT(hadc->Instance->CR2, ADC_CR2_TSVREFE);
  1000. }
  1001. /* Configure the offset: offset enable/disable, InjectedChannel, offset value */
  1002. switch(sConfigInjected->InjectedRank)
  1003. {
  1004. case 1:
  1005. /* Set injected channel 1 offset */
  1006. MODIFY_REG(hadc->Instance->JOFR1,
  1007. ADC_JOFR1_JOFFSET1,
  1008. sConfigInjected->InjectedOffset);
  1009. break;
  1010. case 2:
  1011. /* Set injected channel 2 offset */
  1012. MODIFY_REG(hadc->Instance->JOFR2,
  1013. ADC_JOFR2_JOFFSET2,
  1014. sConfigInjected->InjectedOffset);
  1015. break;
  1016. case 3:
  1017. /* Set injected channel 3 offset */
  1018. MODIFY_REG(hadc->Instance->JOFR3,
  1019. ADC_JOFR3_JOFFSET3,
  1020. sConfigInjected->InjectedOffset);
  1021. break;
  1022. case 4:
  1023. default:
  1024. MODIFY_REG(hadc->Instance->JOFR4,
  1025. ADC_JOFR4_JOFFSET4,
  1026. sConfigInjected->InjectedOffset);
  1027. break;
  1028. }
  1029. /* If ADC1 Channel_16 or Channel_17 is selected, enable Temperature sensor */
  1030. /* and VREFINT measurement path. */
  1031. if ((sConfigInjected->InjectedChannel == ADC_CHANNEL_TEMPSENSOR) ||
  1032. (sConfigInjected->InjectedChannel == ADC_CHANNEL_VREFINT) )
  1033. {
  1034. /* For STM32F1 devices with several ADC: Only ADC1 can access internal */
  1035. /* measurement channels (VrefInt/TempSensor). If these channels are */
  1036. /* intended to be set on other ADC instances, an error is reported. */
  1037. if (hadc->Instance == ADC1)
  1038. {
  1039. if (READ_BIT(hadc->Instance->CR2, ADC_CR2_TSVREFE) == RESET)
  1040. {
  1041. SET_BIT(hadc->Instance->CR2, ADC_CR2_TSVREFE);
  1042. if ((sConfigInjected->InjectedChannel == ADC_CHANNEL_TEMPSENSOR))
  1043. {
  1044. /* Delay for temperature sensor stabilization time */
  1045. /* Compute number of CPU cycles to wait for */
  1046. wait_loop_index = (ADC_TEMPSENSOR_DELAY_US * (SystemCoreClock / 1000000U));
  1047. while(wait_loop_index != 0U)
  1048. {
  1049. wait_loop_index--;
  1050. }
  1051. }
  1052. }
  1053. }
  1054. else
  1055. {
  1056. /* Update ADC state machine to error */
  1057. SET_BIT(hadc->State, HAL_ADC_STATE_ERROR_CONFIG);
  1058. tmp_hal_status = HAL_ERROR;
  1059. }
  1060. }
  1061. /* Process unlocked */
  1062. __HAL_UNLOCK(hadc);
  1063. /* Return function status */
  1064. return tmp_hal_status;
  1065. }
  1066. #if defined (STM32F103x6) || defined (STM32F103xB) || defined (STM32F105xC) || defined (STM32F107xC) || defined (STM32F103xE) || defined (STM32F103xG)
  1067. /**
  1068. * @brief Enable ADC multimode and configure multimode parameters
  1069. * @note Possibility to update parameters on the fly:
  1070. * This function initializes multimode parameters, following
  1071. * calls to this function can be used to reconfigure some parameters
  1072. * of structure "ADC_MultiModeTypeDef" on the fly, without reseting
  1073. * the ADCs (both ADCs of the common group).
  1074. * The setting of these parameters is conditioned to ADC state.
  1075. * For parameters constraints, see comments of structure
  1076. * "ADC_MultiModeTypeDef".
  1077. * @note To change back configuration from multimode to single mode, ADC must
  1078. * be reset (using function HAL_ADC_Init() ).
  1079. * @param hadc: ADC handle
  1080. * @param multimode: Structure of ADC multimode configuration
  1081. * @retval HAL status
  1082. */
  1083. HAL_StatusTypeDef HAL_ADCEx_MultiModeConfigChannel(ADC_HandleTypeDef* hadc, ADC_MultiModeTypeDef* multimode)
  1084. {
  1085. HAL_StatusTypeDef tmp_hal_status = HAL_OK;
  1086. ADC_HandleTypeDef tmphadcSlave;
  1087. /* Check the parameters */
  1088. assert_param(IS_ADC_MULTIMODE_MASTER_INSTANCE(hadc->Instance));
  1089. assert_param(IS_ADC_MODE(multimode->Mode));
  1090. /* Process locked */
  1091. __HAL_LOCK(hadc);
  1092. /* Set a temporary handle of the ADC slave associated to the ADC master */
  1093. ADC_MULTI_SLAVE(hadc, &tmphadcSlave);
  1094. /* Parameters update conditioned to ADC state: */
  1095. /* Parameters that can be updated when ADC is disabled or enabled without */
  1096. /* conversion on going on regular group: */
  1097. /* - ADC master and ADC slave DMA configuration */
  1098. /* Parameters that can be updated only when ADC is disabled: */
  1099. /* - Multimode mode selection */
  1100. /* To optimize code, all multimode settings can be set when both ADCs of */
  1101. /* the common group are in state: disabled. */
  1102. if ((ADC_IS_ENABLE(hadc) == RESET) &&
  1103. (ADC_IS_ENABLE(&tmphadcSlave) == RESET) &&
  1104. (IS_ADC_MULTIMODE_MASTER_INSTANCE(hadc->Instance)) )
  1105. {
  1106. MODIFY_REG(hadc->Instance->CR1,
  1107. ADC_CR1_DUALMOD ,
  1108. multimode->Mode );
  1109. }
  1110. /* If one of the ADC sharing the same common group is enabled, no update */
  1111. /* could be done on neither of the multimode structure parameters. */
  1112. else
  1113. {
  1114. /* Update ADC state machine to error */
  1115. SET_BIT(hadc->State, HAL_ADC_STATE_ERROR_CONFIG);
  1116. tmp_hal_status = HAL_ERROR;
  1117. }
  1118. /* Process unlocked */
  1119. __HAL_UNLOCK(hadc);
  1120. /* Return function status */
  1121. return tmp_hal_status;
  1122. }
  1123. #endif /* defined STM32F103x6 || defined STM32F103xB || defined STM32F105xC || defined STM32F107xC || defined STM32F103xE || defined STM32F103xG */
  1124. /**
  1125. * @}
  1126. */
  1127. /**
  1128. * @}
  1129. */
  1130. #endif /* HAL_ADC_MODULE_ENABLED */
  1131. /**
  1132. * @}
  1133. */
  1134. /**
  1135. * @}
  1136. */
  1137. /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/