stm32f1xx_hal_adc_ex.c 52 KB

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