stm32h7xx_hal_adc_ex.c 101 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659
  1. /**
  2. ******************************************************************************
  3. * @file stm32h7xx_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 Converter (ADC)
  7. * peripheral:
  8. * + Peripheral Control functions
  9. * Other functions (generic functions) are available in file
  10. * "stm32h7xx_hal_adc.c".
  11. *
  12. ******************************************************************************
  13. * @attention
  14. *
  15. * Copyright (c) 2017 STMicroelectronics.
  16. * All rights reserved.
  17. *
  18. * This software is licensed under terms that can be found in the LICENSE file
  19. * in the root directory of this software component.
  20. * If no LICENSE file comes with this software, it is provided AS-IS.
  21. *
  22. ******************************************************************************
  23. @verbatim
  24. [..]
  25. (@) Sections "ADC peripheral features" and "How to use this driver" are
  26. available in file of generic functions "stm32h7xx_hal_adc.c".
  27. [..]
  28. @endverbatim
  29. ******************************************************************************
  30. */
  31. /* Includes ------------------------------------------------------------------*/
  32. #include "stm32h7xx_hal.h"
  33. /** @addtogroup STM32H7xx_HAL_Driver
  34. * @{
  35. */
  36. /** @defgroup ADCEx ADCEx
  37. * @brief ADC Extended HAL module driver
  38. * @{
  39. */
  40. #ifdef HAL_ADC_MODULE_ENABLED
  41. /* Private typedef -----------------------------------------------------------*/
  42. /* Private define ------------------------------------------------------------*/
  43. /** @defgroup ADCEx_Private_Constants ADC Extended Private Constants
  44. * @{
  45. */
  46. #define ADC_JSQR_FIELDS ((ADC_JSQR_JL | ADC_JSQR_JEXTSEL | ADC_JSQR_JEXTEN |\
  47. ADC_JSQR_JSQ1 | ADC_JSQR_JSQ2 |\
  48. ADC_JSQR_JSQ3 | ADC_JSQR_JSQ4 )) /*!< ADC_JSQR fields of parameters that can be updated anytime
  49. once the ADC is enabled */
  50. /* Fixed timeout value for ADC calibration. */
  51. /* Fixed timeout value for ADC calibration. */
  52. /* Values defined to be higher than worst cases: low clock frequency, */
  53. /* maximum prescalers. */
  54. /* Ex of profile low frequency : f_ADC at 0.125 Mhz (minimum value */
  55. /* according to Data sheet), calibration_time MAX = 165010 / f_ADC */
  56. /* 165010 / 125000 = 1.32s */
  57. /* At maximum CPU speed (480 MHz), this means */
  58. /* 1.32 * 480 MHz = 633600000 CPU cycles */
  59. #define ADC_CALIBRATION_TIMEOUT (633600000U) /*!< ADC calibration time-out value */
  60. /**
  61. * @}
  62. */
  63. /* Private macro -------------------------------------------------------------*/
  64. /* Private variables ---------------------------------------------------------*/
  65. /* Private function prototypes -----------------------------------------------*/
  66. /* Exported functions --------------------------------------------------------*/
  67. /** @defgroup ADCEx_Exported_Functions ADC Extended Exported Functions
  68. * @{
  69. */
  70. /** @defgroup ADCEx_Exported_Functions_Group1 Extended Input and Output operation functions
  71. * @brief Extended IO operation functions
  72. *
  73. @verbatim
  74. ===============================================================================
  75. ##### IO operation functions #####
  76. ===============================================================================
  77. [..] This section provides functions allowing to:
  78. (+) Perform the ADC self-calibration for single or differential ending.
  79. (+) Get calibration factors for single or differential ending.
  80. (+) Set calibration factors for single or differential ending.
  81. (+) Start conversion of ADC group injected.
  82. (+) Stop conversion of ADC group injected.
  83. (+) Poll for conversion complete on ADC group injected.
  84. (+) Get result of ADC group injected channel conversion.
  85. (+) Start conversion of ADC group injected and enable interruptions.
  86. (+) Stop conversion of ADC group injected and disable interruptions.
  87. (+) When multimode feature is available, start multimode and enable DMA transfer.
  88. (+) Stop multimode and disable ADC DMA transfer.
  89. (+) Get result of multimode conversion.
  90. @endverbatim
  91. * @{
  92. */
  93. /**
  94. * @brief Perform an ADC automatic self-calibration
  95. * Calibration prerequisite: ADC must be disabled (execute this
  96. * function before HAL_ADC_Start() or after HAL_ADC_Stop() ).
  97. * @param hadc ADC handle
  98. * @param CalibrationMode Selection of calibration offset or
  99. * linear calibration offset.
  100. * @arg ADC_CALIB_OFFSET Channel in mode calibration offset
  101. * @arg ADC_CALIB_OFFSET_LINEARITY Channel in mode linear calibration offset
  102. * @param SingleDiff Selection of single-ended or differential input
  103. * This parameter can be one of the following values:
  104. * @arg @ref ADC_SINGLE_ENDED Channel in mode input single ended
  105. * @arg @ref ADC_DIFFERENTIAL_ENDED Channel in mode input differential ended
  106. * @retval HAL status
  107. */
  108. HAL_StatusTypeDef HAL_ADCEx_Calibration_Start(ADC_HandleTypeDef *hadc, uint32_t CalibrationMode, uint32_t SingleDiff)
  109. {
  110. HAL_StatusTypeDef tmp_hal_status;
  111. __IO uint32_t wait_loop_index = 0UL;
  112. /* Check the parameters */
  113. assert_param(IS_ADC_ALL_INSTANCE(hadc->Instance));
  114. assert_param(IS_ADC_SINGLE_DIFFERENTIAL(SingleDiff));
  115. /* Process locked */
  116. __HAL_LOCK(hadc);
  117. /* Calibration prerequisite: ADC must be disabled. */
  118. /* Disable the ADC (if not already disabled) */
  119. tmp_hal_status = ADC_Disable(hadc);
  120. /* Check if ADC is effectively disabled */
  121. if (tmp_hal_status == HAL_OK)
  122. {
  123. /* Set ADC state */
  124. ADC_STATE_CLR_SET(hadc->State,
  125. HAL_ADC_STATE_REG_BUSY | HAL_ADC_STATE_INJ_BUSY,
  126. HAL_ADC_STATE_BUSY_INTERNAL);
  127. /* Start ADC calibration in mode single-ended or differential */
  128. LL_ADC_StartCalibration(hadc->Instance, CalibrationMode, SingleDiff);
  129. /* Wait for calibration completion */
  130. while (LL_ADC_IsCalibrationOnGoing(hadc->Instance) != 0UL)
  131. {
  132. wait_loop_index++;
  133. if (wait_loop_index >= ADC_CALIBRATION_TIMEOUT)
  134. {
  135. /* Update ADC state machine to error */
  136. ADC_STATE_CLR_SET(hadc->State,
  137. HAL_ADC_STATE_BUSY_INTERNAL,
  138. HAL_ADC_STATE_ERROR_INTERNAL);
  139. /* Process unlocked */
  140. __HAL_UNLOCK(hadc);
  141. return HAL_ERROR;
  142. }
  143. }
  144. /* Set ADC state */
  145. ADC_STATE_CLR_SET(hadc->State,
  146. HAL_ADC_STATE_BUSY_INTERNAL,
  147. HAL_ADC_STATE_READY);
  148. }
  149. else
  150. {
  151. SET_BIT(hadc->State, HAL_ADC_STATE_ERROR_INTERNAL);
  152. /* Note: No need to update variable "tmp_hal_status" here: already set */
  153. /* to state "HAL_ERROR" by function disabling the ADC. */
  154. }
  155. /* Process unlocked */
  156. __HAL_UNLOCK(hadc);
  157. /* Return function status */
  158. return tmp_hal_status;
  159. }
  160. /**
  161. * @brief Get the calibration factor.
  162. * @param hadc ADC handle.
  163. * @param SingleDiff This parameter can be only:
  164. * @arg @ref ADC_SINGLE_ENDED Channel in mode input single ended
  165. * @arg @ref ADC_DIFFERENTIAL_ENDED Channel in mode input differential ended
  166. * @retval Calibration value.
  167. */
  168. uint32_t HAL_ADCEx_Calibration_GetValue(ADC_HandleTypeDef *hadc, uint32_t SingleDiff)
  169. {
  170. /* Check the parameters */
  171. assert_param(IS_ADC_ALL_INSTANCE(hadc->Instance));
  172. assert_param(IS_ADC_SINGLE_DIFFERENTIAL(SingleDiff));
  173. /* Return the selected ADC calibration value */
  174. return LL_ADC_GetCalibrationOffsetFactor(hadc->Instance, SingleDiff);
  175. }
  176. /**
  177. * @brief Get the calibration factor from automatic conversion result
  178. * @param hadc ADC handle
  179. * @param LinearCalib_Buffer: Linear calibration factor
  180. * @retval HAL state
  181. */
  182. HAL_StatusTypeDef HAL_ADCEx_LinearCalibration_GetValue(ADC_HandleTypeDef *hadc, uint32_t *LinearCalib_Buffer)
  183. {
  184. uint32_t cnt;
  185. HAL_StatusTypeDef tmp_hal_status = HAL_OK;
  186. uint32_t temp_REG_IsConversionOngoing = 0UL;
  187. /* Check the parameters */
  188. assert_param(IS_ADC_ALL_INSTANCE(hadc->Instance));
  189. /* Enable the ADC ADEN = 1 to be able to read the linear calibration factor */
  190. if (LL_ADC_IsEnabled(hadc->Instance) == 0UL)
  191. {
  192. tmp_hal_status = ADC_Enable(hadc);
  193. }
  194. if (tmp_hal_status == HAL_OK)
  195. {
  196. if (LL_ADC_REG_IsConversionOngoing(hadc->Instance) != 0UL)
  197. {
  198. LL_ADC_REG_StopConversion(hadc->Instance);
  199. temp_REG_IsConversionOngoing = 1UL;
  200. }
  201. for (cnt = ADC_LINEAR_CALIB_REG_COUNT; cnt > 0UL; cnt--)
  202. {
  203. LinearCalib_Buffer[cnt - 1U] = LL_ADC_GetCalibrationLinearFactor(hadc->Instance, ADC_CR_LINCALRDYW6 >> (ADC_LINEAR_CALIB_REG_COUNT - cnt));
  204. }
  205. if (temp_REG_IsConversionOngoing != 0UL)
  206. {
  207. LL_ADC_REG_StartConversion(hadc->Instance);
  208. }
  209. }
  210. return tmp_hal_status;
  211. }
  212. /**
  213. * @brief Set the calibration factor to overwrite automatic conversion result.
  214. * ADC must be enabled and no conversion is ongoing.
  215. * @param hadc ADC handle
  216. * @param SingleDiff This parameter can be only:
  217. * @arg @ref ADC_SINGLE_ENDED Channel in mode input single ended
  218. * @arg @ref ADC_DIFFERENTIAL_ENDED Channel in mode input differential ended
  219. * @param CalibrationFactor Calibration factor On devices STM32H72xx and STM32H73xx this parameter is coded on 11 bits
  220. * maximum for ADC1/2 and on 7 bits for ADC3.
  221. * On devices STM32H74xx and STM32H75xx this parameter is coded on 11 bits.
  222. * @retval HAL state
  223. */
  224. HAL_StatusTypeDef HAL_ADCEx_Calibration_SetValue(ADC_HandleTypeDef *hadc, uint32_t SingleDiff, uint32_t CalibrationFactor)
  225. {
  226. HAL_StatusTypeDef tmp_hal_status = HAL_OK;
  227. uint32_t tmp_adc_is_conversion_on_going_regular;
  228. uint32_t tmp_adc_is_conversion_on_going_injected;
  229. /* Check the parameters */
  230. assert_param(IS_ADC_ALL_INSTANCE(hadc->Instance));
  231. assert_param(IS_ADC_SINGLE_DIFFERENTIAL(SingleDiff));
  232. #if defined(ADC_VER_V5_V90)
  233. if (hadc->Instance == ADC3)
  234. {
  235. assert_param(IS_ADC_CALFACT_ADC3(CalibrationFactor));
  236. }
  237. else
  238. {
  239. assert_param(IS_ADC_CALFACT(CalibrationFactor));
  240. }
  241. #else
  242. assert_param(IS_ADC_CALFACT(CalibrationFactor));
  243. #endif
  244. /* Process locked */
  245. __HAL_LOCK(hadc);
  246. /* Verification of hardware constraints before modifying the calibration */
  247. /* factors register: ADC must be enabled, no conversion on going. */
  248. tmp_adc_is_conversion_on_going_regular = LL_ADC_REG_IsConversionOngoing(hadc->Instance);
  249. tmp_adc_is_conversion_on_going_injected = LL_ADC_INJ_IsConversionOngoing(hadc->Instance);
  250. if ((LL_ADC_IsEnabled(hadc->Instance) != 0UL)
  251. && (tmp_adc_is_conversion_on_going_regular == 0UL)
  252. && (tmp_adc_is_conversion_on_going_injected == 0UL)
  253. )
  254. {
  255. /* Set the selected ADC calibration value */
  256. LL_ADC_SetCalibrationOffsetFactor(hadc->Instance, SingleDiff, CalibrationFactor);
  257. }
  258. else
  259. {
  260. /* Update ADC state machine */
  261. SET_BIT(hadc->State, HAL_ADC_STATE_ERROR_CONFIG);
  262. /* Update ADC error code */
  263. SET_BIT(hadc->ErrorCode, HAL_ADC_ERROR_INTERNAL);
  264. /* Update ADC state machine to error */
  265. tmp_hal_status = HAL_ERROR;
  266. }
  267. /* Process unlocked */
  268. __HAL_UNLOCK(hadc);
  269. /* Return function status */
  270. return tmp_hal_status;
  271. }
  272. /**
  273. * @brief Set the linear calibration factor
  274. * @param hadc ADC handle
  275. * @param LinearCalib_Buffer: Linear calibration factor
  276. * @retval HAL state
  277. */
  278. HAL_StatusTypeDef HAL_ADCEx_LinearCalibration_SetValue(ADC_HandleTypeDef *hadc, uint32_t *LinearCalib_Buffer)
  279. {
  280. uint32_t cnt;
  281. __IO uint32_t wait_loop_index = 0;
  282. uint32_t temp_REG_IsConversionOngoing = 0UL;
  283. /* Check the parameters */
  284. assert_param(IS_ADC_ALL_INSTANCE(hadc->Instance));
  285. /* - Exit from deep-power-down mode and ADC voltage regulator enable */
  286. /* Exit deep power down mode if still in that state */
  287. if (HAL_IS_BIT_SET(hadc->Instance->CR, ADC_CR_DEEPPWD))
  288. {
  289. /* Exit deep power down mode */
  290. CLEAR_BIT(hadc->Instance->CR, ADC_CR_DEEPPWD);
  291. /* System was in deep power down mode, calibration must
  292. be relaunched or a previously saved calibration factor
  293. re-applied once the ADC voltage regulator is enabled */
  294. }
  295. if (HAL_IS_BIT_CLR(hadc->Instance->CR, ADC_CR_ADVREGEN))
  296. {
  297. /* Enable ADC internal voltage regulator */
  298. SET_BIT(hadc->Instance->CR, ADC_CR_ADVREGEN);
  299. /* Delay for ADC stabilization time */
  300. /* Wait loop initialization and execution */
  301. /* Note: Variable divided by 2 to compensate partially */
  302. /* CPU processing cycles. */
  303. wait_loop_index = ((ADC_STAB_DELAY_US / 10UL) * ((SystemCoreClock / (100000UL * 2UL)) + 1UL));
  304. while (wait_loop_index != 0UL)
  305. {
  306. wait_loop_index--;
  307. }
  308. }
  309. /* Verification that ADC voltage regulator is correctly enabled, whether */
  310. /* or not ADC is coming from state reset (if any potential problem of */
  311. /* clocking, voltage regulator would not be enabled). */
  312. if (HAL_IS_BIT_CLR(hadc->Instance->CR, ADC_CR_ADVREGEN))
  313. {
  314. /* Update ADC state machine to error */
  315. SET_BIT(hadc->State, HAL_ADC_STATE_ERROR_INTERNAL);
  316. /* Set ADC error code to ADC peripheral internal error */
  317. SET_BIT(hadc->ErrorCode, HAL_ADC_ERROR_INTERNAL);
  318. return HAL_ERROR;
  319. }
  320. /* Enable the ADC peripheral */
  321. if (LL_ADC_IsEnabled(hadc->Instance) == 0UL) /* Enable the ADC if it is disabled */
  322. {
  323. if (ADC_Enable(hadc) != HAL_OK)
  324. {
  325. return HAL_ERROR;
  326. }
  327. else
  328. {
  329. for (cnt = ADC_LINEAR_CALIB_REG_COUNT; cnt > 0UL ; cnt--)
  330. {
  331. LL_ADC_SetCalibrationLinearFactor(hadc->Instance, ADC_CR_LINCALRDYW6 >> (ADC_LINEAR_CALIB_REG_COUNT - cnt), LinearCalib_Buffer[cnt - 1U]);
  332. }
  333. (void)ADC_Disable(hadc);
  334. }
  335. }
  336. else /* ADC is already enabled, so no need to enable it but need to stop conversion */
  337. {
  338. if (LL_ADC_REG_IsConversionOngoing(hadc->Instance) != 0UL)
  339. {
  340. LL_ADC_REG_StopConversion(hadc->Instance);
  341. temp_REG_IsConversionOngoing = 1UL;
  342. }
  343. for (cnt = ADC_LINEAR_CALIB_REG_COUNT; cnt > 0UL ; cnt--)
  344. {
  345. LL_ADC_SetCalibrationLinearFactor(hadc->Instance, ADC_CR_LINCALRDYW6 >> (ADC_LINEAR_CALIB_REG_COUNT - cnt), LinearCalib_Buffer[cnt - 1U]);
  346. }
  347. if (temp_REG_IsConversionOngoing != 0UL)
  348. {
  349. LL_ADC_REG_StartConversion(hadc->Instance);
  350. }
  351. }
  352. return HAL_OK;
  353. }
  354. /**
  355. * @brief Load the calibration factor from engi bytes
  356. * @param hadc ADC handle
  357. * @retval HAL state
  358. */
  359. HAL_StatusTypeDef HAL_ADCEx_LinearCalibration_FactorLoad(ADC_HandleTypeDef *hadc)
  360. {
  361. HAL_StatusTypeDef tmp_hal_status = HAL_OK;
  362. uint32_t cnt, FactorOffset;
  363. uint32_t LinearCalib_Buffer[ADC_LINEAR_CALIB_REG_COUNT];
  364. /* Linearity calibration is retrieved from engi bytes
  365. read values from registers and put them to the CALFACT2 register */
  366. /* If needed linearity calibration can be done in runtime using
  367. LL_ADC_GetCalibrationLinearFactor() */
  368. if (hadc->Instance == ADC1)
  369. {
  370. FactorOffset = 0UL;
  371. }
  372. else if (hadc->Instance == ADC2)
  373. {
  374. FactorOffset = 8UL;
  375. }
  376. else /*Case ADC3*/
  377. {
  378. FactorOffset = 16UL;
  379. }
  380. for (cnt = 0UL; cnt < ADC_LINEAR_CALIB_REG_COUNT; cnt++)
  381. {
  382. LinearCalib_Buffer[cnt] = *(uint32_t *)(ADC_LINEAR_CALIB_REG_1_ADDR + FactorOffset + cnt);
  383. }
  384. if (HAL_ADCEx_LinearCalibration_SetValue(hadc, (uint32_t *)LinearCalib_Buffer) != HAL_OK)
  385. {
  386. tmp_hal_status = HAL_ERROR;
  387. }
  388. return tmp_hal_status;
  389. }
  390. /**
  391. * @brief Enable ADC, start conversion of injected group.
  392. * @note Interruptions enabled in this function: None.
  393. * @note Case of multimode enabled when multimode feature is available:
  394. * HAL_ADCEx_InjectedStart() API must be called for ADC slave first,
  395. * then for ADC master.
  396. * For ADC slave, ADC is enabled only (conversion is not started).
  397. * For ADC master, ADC is enabled and multimode conversion is started.
  398. * @param hadc ADC handle.
  399. * @retval HAL status
  400. */
  401. HAL_StatusTypeDef HAL_ADCEx_InjectedStart(ADC_HandleTypeDef *hadc)
  402. {
  403. HAL_StatusTypeDef tmp_hal_status;
  404. uint32_t tmp_config_injected_queue;
  405. uint32_t tmp_multimode_config = LL_ADC_GetMultimode(__LL_ADC_COMMON_INSTANCE(hadc->Instance));
  406. /* Check the parameters */
  407. assert_param(IS_ADC_ALL_INSTANCE(hadc->Instance));
  408. if (LL_ADC_INJ_IsConversionOngoing(hadc->Instance) != 0UL)
  409. {
  410. return HAL_BUSY;
  411. }
  412. else
  413. {
  414. /* In case of software trigger detection enabled, JQDIS must be set
  415. (which can be done only if ADSTART and JADSTART are both cleared).
  416. If JQDIS is not set at that point, returns an error
  417. - since software trigger detection is disabled. User needs to
  418. resort to HAL_ADCEx_DisableInjectedQueue() API to set JQDIS.
  419. - or (if JQDIS is intentionally reset) since JEXTEN = 0 which means
  420. the queue is empty */
  421. tmp_config_injected_queue = READ_BIT(hadc->Instance->CFGR, ADC_CFGR_JQDIS);
  422. if ((READ_BIT(hadc->Instance->JSQR, ADC_JSQR_JEXTEN) == 0UL)
  423. && (tmp_config_injected_queue == 0UL)
  424. )
  425. {
  426. SET_BIT(hadc->State, HAL_ADC_STATE_ERROR_CONFIG);
  427. return HAL_ERROR;
  428. }
  429. /* Process locked */
  430. __HAL_LOCK(hadc);
  431. /* Enable the ADC peripheral */
  432. tmp_hal_status = ADC_Enable(hadc);
  433. /* Start conversion if ADC is effectively enabled */
  434. if (tmp_hal_status == HAL_OK)
  435. {
  436. /* Check if a regular conversion is ongoing */
  437. if ((hadc->State & HAL_ADC_STATE_REG_BUSY) != 0UL)
  438. {
  439. /* Reset ADC error code field related to injected conversions only */
  440. CLEAR_BIT(hadc->ErrorCode, HAL_ADC_ERROR_JQOVF);
  441. }
  442. else
  443. {
  444. /* Set ADC error code to none */
  445. ADC_CLEAR_ERRORCODE(hadc);
  446. }
  447. /* Set ADC state */
  448. /* - Clear state bitfield related to injected group conversion results */
  449. /* - Set state bitfield related to injected operation */
  450. ADC_STATE_CLR_SET(hadc->State,
  451. HAL_ADC_STATE_READY | HAL_ADC_STATE_INJ_EOC,
  452. HAL_ADC_STATE_INJ_BUSY);
  453. /* Reset HAL_ADC_STATE_MULTIMODE_SLAVE bit
  454. - if ADC instance is master or if multimode feature is not available
  455. - if multimode setting is disabled (ADC instance slave in independent mode) */
  456. if ((__LL_ADC_MULTI_INSTANCE_MASTER(hadc->Instance) == hadc->Instance)
  457. || (tmp_multimode_config == LL_ADC_MULTI_INDEPENDENT)
  458. )
  459. {
  460. CLEAR_BIT(hadc->State, HAL_ADC_STATE_MULTIMODE_SLAVE);
  461. }
  462. /* Clear ADC group injected group conversion flag */
  463. /* (To ensure of no unknown state from potential previous ADC operations) */
  464. __HAL_ADC_CLEAR_FLAG(hadc, (ADC_FLAG_JEOC | ADC_FLAG_JEOS));
  465. /* Process unlocked */
  466. /* Unlock before starting ADC conversions: in case of potential */
  467. /* interruption, to let the process to ADC IRQ Handler. */
  468. __HAL_UNLOCK(hadc);
  469. /* Enable conversion of injected group, if automatic injected conversion */
  470. /* is disabled. */
  471. /* If software start has been selected, conversion starts immediately. */
  472. /* If external trigger has been selected, conversion will start at next */
  473. /* trigger event. */
  474. /* Case of multimode enabled (when multimode feature is available): */
  475. /* if ADC is slave, */
  476. /* - ADC is enabled only (conversion is not started), */
  477. /* - if multimode only concerns regular conversion, ADC is enabled */
  478. /* and conversion is started. */
  479. /* If ADC is master or independent, */
  480. /* - ADC is enabled and conversion is started. */
  481. if ((__LL_ADC_MULTI_INSTANCE_MASTER(hadc->Instance) == hadc->Instance)
  482. || (tmp_multimode_config == LL_ADC_MULTI_INDEPENDENT)
  483. || (tmp_multimode_config == LL_ADC_MULTI_DUAL_REG_SIMULT)
  484. || (tmp_multimode_config == LL_ADC_MULTI_DUAL_REG_INTERL)
  485. )
  486. {
  487. /* ADC instance is not a multimode slave instance with multimode injected conversions enabled */
  488. if (LL_ADC_INJ_GetTrigAuto(hadc->Instance) == LL_ADC_INJ_TRIG_INDEPENDENT)
  489. {
  490. LL_ADC_INJ_StartConversion(hadc->Instance);
  491. }
  492. }
  493. else
  494. {
  495. /* ADC instance is not a multimode slave instance with multimode injected conversions enabled */
  496. SET_BIT(hadc->State, HAL_ADC_STATE_MULTIMODE_SLAVE);
  497. }
  498. }
  499. else
  500. {
  501. /* Process unlocked */
  502. __HAL_UNLOCK(hadc);
  503. }
  504. /* Return function status */
  505. return tmp_hal_status;
  506. }
  507. }
  508. /**
  509. * @brief Stop conversion of injected channels. Disable ADC peripheral if
  510. * no regular conversion is on going.
  511. * @note If ADC must be disabled and if conversion is on going on
  512. * regular group, function HAL_ADC_Stop must be used to stop both
  513. * injected and regular groups, and disable the ADC.
  514. * @note If injected group mode auto-injection is enabled,
  515. * function HAL_ADC_Stop must be used.
  516. * @note In case of multimode enabled (when multimode feature is available),
  517. * HAL_ADCEx_InjectedStop() must be called for ADC master first, then for ADC slave.
  518. * For ADC master, conversion is stopped and ADC is disabled.
  519. * For ADC slave, ADC is disabled only (conversion stop of ADC master
  520. * has already stopped conversion of ADC slave).
  521. * @param hadc ADC handle.
  522. * @retval HAL status
  523. */
  524. HAL_StatusTypeDef HAL_ADCEx_InjectedStop(ADC_HandleTypeDef *hadc)
  525. {
  526. HAL_StatusTypeDef tmp_hal_status;
  527. /* Check the parameters */
  528. assert_param(IS_ADC_ALL_INSTANCE(hadc->Instance));
  529. /* Process locked */
  530. __HAL_LOCK(hadc);
  531. /* 1. Stop potential conversion on going on injected group only. */
  532. tmp_hal_status = ADC_ConversionStop(hadc, ADC_INJECTED_GROUP);
  533. /* Disable ADC peripheral if injected conversions are effectively stopped */
  534. /* and if no conversion on regular group is on-going */
  535. if (tmp_hal_status == HAL_OK)
  536. {
  537. if (LL_ADC_REG_IsConversionOngoing(hadc->Instance) == 0UL)
  538. {
  539. /* 2. Disable the ADC peripheral */
  540. tmp_hal_status = ADC_Disable(hadc);
  541. /* Check if ADC is effectively disabled */
  542. if (tmp_hal_status == HAL_OK)
  543. {
  544. /* Set ADC state */
  545. ADC_STATE_CLR_SET(hadc->State,
  546. HAL_ADC_STATE_REG_BUSY | HAL_ADC_STATE_INJ_BUSY,
  547. HAL_ADC_STATE_READY);
  548. }
  549. }
  550. /* Conversion on injected group is stopped, but ADC not disabled since */
  551. /* conversion on regular group is still running. */
  552. else
  553. {
  554. /* Set ADC state */
  555. CLEAR_BIT(hadc->State, HAL_ADC_STATE_INJ_BUSY);
  556. }
  557. }
  558. /* Process unlocked */
  559. __HAL_UNLOCK(hadc);
  560. /* Return function status */
  561. return tmp_hal_status;
  562. }
  563. /**
  564. * @brief Wait for injected group conversion to be completed.
  565. * @param hadc ADC handle
  566. * @param Timeout Timeout value in millisecond.
  567. * @note Depending on hadc->Init.EOCSelection, JEOS or JEOC is
  568. * checked and cleared depending on AUTDLY bit status.
  569. * @retval HAL status
  570. */
  571. HAL_StatusTypeDef HAL_ADCEx_InjectedPollForConversion(ADC_HandleTypeDef *hadc, uint32_t Timeout)
  572. {
  573. uint32_t tickstart;
  574. uint32_t tmp_Flag_End;
  575. uint32_t tmp_adc_inj_is_trigger_source_sw_start;
  576. uint32_t tmp_adc_reg_is_trigger_source_sw_start;
  577. uint32_t tmp_cfgr;
  578. const ADC_TypeDef *tmpADC_Master;
  579. uint32_t tmp_multimode_config = LL_ADC_GetMultimode(__LL_ADC_COMMON_INSTANCE(hadc->Instance));
  580. /* Check the parameters */
  581. assert_param(IS_ADC_ALL_INSTANCE(hadc->Instance));
  582. /* If end of sequence selected */
  583. if (hadc->Init.EOCSelection == ADC_EOC_SEQ_CONV)
  584. {
  585. tmp_Flag_End = ADC_FLAG_JEOS;
  586. }
  587. else /* end of conversion selected */
  588. {
  589. tmp_Flag_End = ADC_FLAG_JEOC;
  590. }
  591. /* Get timeout */
  592. tickstart = HAL_GetTick();
  593. /* Wait until End of Conversion or Sequence flag is raised */
  594. while ((hadc->Instance->ISR & tmp_Flag_End) == 0UL)
  595. {
  596. /* Check if timeout is disabled (set to infinite wait) */
  597. if (Timeout != HAL_MAX_DELAY)
  598. {
  599. if (((HAL_GetTick() - tickstart) > Timeout) || (Timeout == 0UL))
  600. {
  601. if((hadc->Instance->ISR & tmp_Flag_End) == 0UL)
  602. {
  603. /* Update ADC state machine to timeout */
  604. SET_BIT(hadc->State, HAL_ADC_STATE_TIMEOUT);
  605. /* Process unlocked */
  606. __HAL_UNLOCK(hadc);
  607. return HAL_TIMEOUT;
  608. }
  609. }
  610. }
  611. }
  612. /* Retrieve ADC configuration */
  613. tmp_adc_inj_is_trigger_source_sw_start = LL_ADC_INJ_IsTriggerSourceSWStart(hadc->Instance);
  614. tmp_adc_reg_is_trigger_source_sw_start = LL_ADC_REG_IsTriggerSourceSWStart(hadc->Instance);
  615. /* Get relevant register CFGR in ADC instance of ADC master or slave */
  616. /* in function of multimode state (for devices with multimode */
  617. /* available). */
  618. if ((__LL_ADC_MULTI_INSTANCE_MASTER(hadc->Instance) == hadc->Instance)
  619. || (tmp_multimode_config == LL_ADC_MULTI_INDEPENDENT)
  620. || (tmp_multimode_config == LL_ADC_MULTI_DUAL_REG_SIMULT)
  621. || (tmp_multimode_config == LL_ADC_MULTI_DUAL_REG_INTERL)
  622. )
  623. {
  624. tmp_cfgr = READ_REG(hadc->Instance->CFGR);
  625. }
  626. else
  627. {
  628. tmpADC_Master = __LL_ADC_MULTI_INSTANCE_MASTER(hadc->Instance);
  629. tmp_cfgr = READ_REG(tmpADC_Master->CFGR);
  630. }
  631. /* Update ADC state machine */
  632. SET_BIT(hadc->State, HAL_ADC_STATE_INJ_EOC);
  633. /* Determine whether any further conversion upcoming on group injected */
  634. /* by external trigger or by automatic injected conversion */
  635. /* from group regular. */
  636. if ((tmp_adc_inj_is_trigger_source_sw_start != 0UL) ||
  637. ((READ_BIT(tmp_cfgr, ADC_CFGR_JAUTO) == 0UL) &&
  638. ((tmp_adc_reg_is_trigger_source_sw_start != 0UL) &&
  639. (READ_BIT(tmp_cfgr, ADC_CFGR_CONT) == 0UL))))
  640. {
  641. /* Check whether end of sequence is reached */
  642. if (__HAL_ADC_GET_FLAG(hadc, ADC_FLAG_JEOS))
  643. {
  644. /* Particular case if injected contexts queue is enabled: */
  645. /* when the last context has been fully processed, JSQR is reset */
  646. /* by the hardware. Even if no injected conversion is planned to come */
  647. /* (queue empty, triggers are ignored), it can start again */
  648. /* immediately after setting a new context (JADSTART is still set). */
  649. /* Therefore, state of HAL ADC injected group is kept to busy. */
  650. if (READ_BIT(tmp_cfgr, ADC_CFGR_JQM) == 0UL)
  651. {
  652. /* Set ADC state */
  653. CLEAR_BIT(hadc->State, HAL_ADC_STATE_INJ_BUSY);
  654. if ((hadc->State & HAL_ADC_STATE_REG_BUSY) == 0UL)
  655. {
  656. SET_BIT(hadc->State, HAL_ADC_STATE_READY);
  657. }
  658. }
  659. }
  660. }
  661. /* Clear polled flag */
  662. if (tmp_Flag_End == ADC_FLAG_JEOS)
  663. {
  664. /* Clear end of sequence JEOS flag of injected group if low power feature */
  665. /* "LowPowerAutoWait " is disabled, to not interfere with this feature. */
  666. /* For injected groups, no new conversion will start before JEOS is */
  667. /* cleared. */
  668. if (READ_BIT(tmp_cfgr, ADC_CFGR_AUTDLY) == 0UL)
  669. {
  670. __HAL_ADC_CLEAR_FLAG(hadc, (ADC_FLAG_JEOC | ADC_FLAG_JEOS));
  671. }
  672. }
  673. else
  674. {
  675. __HAL_ADC_CLEAR_FLAG(hadc, ADC_FLAG_JEOC);
  676. }
  677. /* Return API HAL status */
  678. return HAL_OK;
  679. }
  680. /**
  681. * @brief Enable ADC, start conversion of injected group with interruption.
  682. * @note Interruptions enabled in this function according to initialization
  683. * setting : JEOC (end of conversion) or JEOS (end of sequence)
  684. * @note Case of multimode enabled (when multimode feature is enabled):
  685. * HAL_ADCEx_InjectedStart_IT() API must be called for ADC slave first,
  686. * then for ADC master.
  687. * For ADC slave, ADC is enabled only (conversion is not started).
  688. * For ADC master, ADC is enabled and multimode conversion is started.
  689. * @param hadc ADC handle.
  690. * @retval HAL status.
  691. */
  692. HAL_StatusTypeDef HAL_ADCEx_InjectedStart_IT(ADC_HandleTypeDef *hadc)
  693. {
  694. HAL_StatusTypeDef tmp_hal_status;
  695. uint32_t tmp_config_injected_queue;
  696. uint32_t tmp_multimode_config = LL_ADC_GetMultimode(__LL_ADC_COMMON_INSTANCE(hadc->Instance));
  697. /* Check the parameters */
  698. assert_param(IS_ADC_ALL_INSTANCE(hadc->Instance));
  699. if (LL_ADC_INJ_IsConversionOngoing(hadc->Instance) != 0UL)
  700. {
  701. return HAL_BUSY;
  702. }
  703. else
  704. {
  705. /* In case of software trigger detection enabled, JQDIS must be set
  706. (which can be done only if ADSTART and JADSTART are both cleared).
  707. If JQDIS is not set at that point, returns an error
  708. - since software trigger detection is disabled. User needs to
  709. resort to HAL_ADCEx_DisableInjectedQueue() API to set JQDIS.
  710. - or (if JQDIS is intentionally reset) since JEXTEN = 0 which means
  711. the queue is empty */
  712. tmp_config_injected_queue = READ_BIT(hadc->Instance->CFGR, ADC_CFGR_JQDIS);
  713. if ((READ_BIT(hadc->Instance->JSQR, ADC_JSQR_JEXTEN) == 0UL)
  714. && (tmp_config_injected_queue == 0UL)
  715. )
  716. {
  717. SET_BIT(hadc->State, HAL_ADC_STATE_ERROR_CONFIG);
  718. return HAL_ERROR;
  719. }
  720. /* Process locked */
  721. __HAL_LOCK(hadc);
  722. /* Enable the ADC peripheral */
  723. tmp_hal_status = ADC_Enable(hadc);
  724. /* Start conversion if ADC is effectively enabled */
  725. if (tmp_hal_status == HAL_OK)
  726. {
  727. /* Check if a regular conversion is ongoing */
  728. if ((hadc->State & HAL_ADC_STATE_REG_BUSY) != 0UL)
  729. {
  730. /* Reset ADC error code field related to injected conversions only */
  731. CLEAR_BIT(hadc->ErrorCode, HAL_ADC_ERROR_JQOVF);
  732. }
  733. else
  734. {
  735. /* Set ADC error code to none */
  736. ADC_CLEAR_ERRORCODE(hadc);
  737. }
  738. /* Set ADC state */
  739. /* - Clear state bitfield related to injected group conversion results */
  740. /* - Set state bitfield related to injected operation */
  741. ADC_STATE_CLR_SET(hadc->State,
  742. HAL_ADC_STATE_READY | HAL_ADC_STATE_INJ_EOC,
  743. HAL_ADC_STATE_INJ_BUSY);
  744. /* Reset HAL_ADC_STATE_MULTIMODE_SLAVE bit
  745. - if ADC instance is master or if multimode feature is not available
  746. - if multimode setting is disabled (ADC instance slave in independent mode) */
  747. if ((__LL_ADC_MULTI_INSTANCE_MASTER(hadc->Instance) == hadc->Instance)
  748. || (tmp_multimode_config == LL_ADC_MULTI_INDEPENDENT)
  749. )
  750. {
  751. CLEAR_BIT(hadc->State, HAL_ADC_STATE_MULTIMODE_SLAVE);
  752. }
  753. /* Clear ADC group injected group conversion flag */
  754. /* (To ensure of no unknown state from potential previous ADC operations) */
  755. __HAL_ADC_CLEAR_FLAG(hadc, (ADC_FLAG_JEOC | ADC_FLAG_JEOS));
  756. /* Process unlocked */
  757. /* Unlock before starting ADC conversions: in case of potential */
  758. /* interruption, to let the process to ADC IRQ Handler. */
  759. __HAL_UNLOCK(hadc);
  760. /* Enable ADC Injected context queue overflow interrupt if this feature */
  761. /* is enabled. */
  762. if ((hadc->Instance->CFGR & ADC_CFGR_JQM) != 0UL)
  763. {
  764. __HAL_ADC_ENABLE_IT(hadc, ADC_FLAG_JQOVF);
  765. }
  766. /* Enable ADC end of conversion interrupt */
  767. switch (hadc->Init.EOCSelection)
  768. {
  769. case ADC_EOC_SEQ_CONV:
  770. __HAL_ADC_DISABLE_IT(hadc, ADC_IT_JEOC);
  771. __HAL_ADC_ENABLE_IT(hadc, ADC_IT_JEOS);
  772. break;
  773. /* case ADC_EOC_SINGLE_CONV */
  774. default:
  775. __HAL_ADC_DISABLE_IT(hadc, ADC_IT_JEOS);
  776. __HAL_ADC_ENABLE_IT(hadc, ADC_IT_JEOC);
  777. break;
  778. }
  779. /* Enable conversion of injected group, if automatic injected conversion */
  780. /* is disabled. */
  781. /* If software start has been selected, conversion starts immediately. */
  782. /* If external trigger has been selected, conversion will start at next */
  783. /* trigger event. */
  784. /* Case of multimode enabled (when multimode feature is available): */
  785. /* if ADC is slave, */
  786. /* - ADC is enabled only (conversion is not started), */
  787. /* - if multimode only concerns regular conversion, ADC is enabled */
  788. /* and conversion is started. */
  789. /* If ADC is master or independent, */
  790. /* - ADC is enabled and conversion is started. */
  791. if ((__LL_ADC_MULTI_INSTANCE_MASTER(hadc->Instance) == hadc->Instance)
  792. || (tmp_multimode_config == LL_ADC_MULTI_INDEPENDENT)
  793. || (tmp_multimode_config == LL_ADC_MULTI_DUAL_REG_SIMULT)
  794. || (tmp_multimode_config == LL_ADC_MULTI_DUAL_REG_INTERL)
  795. )
  796. {
  797. /* ADC instance is not a multimode slave instance with multimode injected conversions enabled */
  798. if (LL_ADC_INJ_GetTrigAuto(hadc->Instance) == LL_ADC_INJ_TRIG_INDEPENDENT)
  799. {
  800. LL_ADC_INJ_StartConversion(hadc->Instance);
  801. }
  802. }
  803. else
  804. {
  805. /* ADC instance is not a multimode slave instance with multimode injected conversions enabled */
  806. SET_BIT(hadc->State, HAL_ADC_STATE_MULTIMODE_SLAVE);
  807. }
  808. }
  809. else
  810. {
  811. /* Process unlocked */
  812. __HAL_UNLOCK(hadc);
  813. }
  814. /* Return function status */
  815. return tmp_hal_status;
  816. }
  817. }
  818. /**
  819. * @brief Stop conversion of injected channels, disable interruption of
  820. * end-of-conversion. Disable ADC peripheral if no regular conversion
  821. * is on going.
  822. * @note If ADC must be disabled and if conversion is on going on
  823. * regular group, function HAL_ADC_Stop must be used to stop both
  824. * injected and regular groups, and disable the ADC.
  825. * @note If injected group mode auto-injection is enabled,
  826. * function HAL_ADC_Stop must be used.
  827. * @note Case of multimode enabled (when multimode feature is available):
  828. * HAL_ADCEx_InjectedStop_IT() API must be called for ADC master first,
  829. * then for ADC slave.
  830. * For ADC master, conversion is stopped and ADC is disabled.
  831. * For ADC slave, ADC is disabled only (conversion stop of ADC master
  832. * has already stopped conversion of ADC slave).
  833. * @note In case of auto-injection mode, HAL_ADC_Stop() must be used.
  834. * @param hadc ADC handle
  835. * @retval HAL status
  836. */
  837. HAL_StatusTypeDef HAL_ADCEx_InjectedStop_IT(ADC_HandleTypeDef *hadc)
  838. {
  839. HAL_StatusTypeDef tmp_hal_status;
  840. /* Check the parameters */
  841. assert_param(IS_ADC_ALL_INSTANCE(hadc->Instance));
  842. /* Process locked */
  843. __HAL_LOCK(hadc);
  844. /* 1. Stop potential conversion on going on injected group only. */
  845. tmp_hal_status = ADC_ConversionStop(hadc, ADC_INJECTED_GROUP);
  846. /* Disable ADC peripheral if injected conversions are effectively stopped */
  847. /* and if no conversion on the other group (regular group) is intended to */
  848. /* continue. */
  849. if (tmp_hal_status == HAL_OK)
  850. {
  851. /* Disable ADC end of conversion interrupt for injected channels */
  852. __HAL_ADC_DISABLE_IT(hadc, (ADC_IT_JEOC | ADC_IT_JEOS | ADC_FLAG_JQOVF));
  853. if (LL_ADC_REG_IsConversionOngoing(hadc->Instance) == 0UL)
  854. {
  855. /* 2. Disable the ADC peripheral */
  856. tmp_hal_status = ADC_Disable(hadc);
  857. /* Check if ADC is effectively disabled */
  858. if (tmp_hal_status == HAL_OK)
  859. {
  860. /* Set ADC state */
  861. ADC_STATE_CLR_SET(hadc->State,
  862. HAL_ADC_STATE_REG_BUSY | HAL_ADC_STATE_INJ_BUSY,
  863. HAL_ADC_STATE_READY);
  864. }
  865. }
  866. /* Conversion on injected group is stopped, but ADC not disabled since */
  867. /* conversion on regular group is still running. */
  868. else
  869. {
  870. /* Set ADC state */
  871. CLEAR_BIT(hadc->State, HAL_ADC_STATE_INJ_BUSY);
  872. }
  873. }
  874. /* Process unlocked */
  875. __HAL_UNLOCK(hadc);
  876. /* Return function status */
  877. return tmp_hal_status;
  878. }
  879. /**
  880. * @brief Enable ADC, start MultiMode conversion and transfer regular results through DMA.
  881. * @note Multimode must have been previously configured using
  882. * HAL_ADCEx_MultiModeConfigChannel() function.
  883. * Interruptions enabled in this function:
  884. * overrun, DMA half transfer, DMA transfer complete.
  885. * Each of these interruptions has its dedicated callback function.
  886. * @note Case of ADC slave using its own DMA channel (typical case being both ADC instances using DMA channel
  887. * of ADC master with data concatenated): multimode must be configured without data packing and
  888. * this function must be called first with handle of ADC slave, then with handle of ADC master.
  889. * @note State field of Slave ADC handle is not updated in this configuration:
  890. * user should not rely on it for information related to Slave regular
  891. * conversions.
  892. * @param hadc ADC handle of ADC master (handle of ADC slave must not be used)
  893. * @param pData Destination Buffer address.
  894. * @param Length Length of data to be transferred from ADC peripheral to memory (in bytes).
  895. * @retval HAL status
  896. */
  897. HAL_StatusTypeDef HAL_ADCEx_MultiModeStart_DMA(ADC_HandleTypeDef *hadc, uint32_t *pData, uint32_t Length)
  898. {
  899. HAL_StatusTypeDef tmp_hal_status;
  900. ADC_HandleTypeDef tmphadcSlave;
  901. ADC_Common_TypeDef *tmpADC_Common;
  902. /* Check the parameters */
  903. assert_param(IS_ADC_MULTIMODE_MASTER_INSTANCE(hadc->Instance));
  904. assert_param(IS_FUNCTIONAL_STATE(hadc->Init.ContinuousConvMode));
  905. assert_param(IS_ADC_EXTTRIG_EDGE(hadc->Init.ExternalTrigConvEdge));
  906. if (LL_ADC_REG_IsConversionOngoing(hadc->Instance) != 0UL)
  907. {
  908. return HAL_BUSY;
  909. }
  910. else
  911. {
  912. /* Process locked */
  913. __HAL_LOCK(hadc);
  914. /* Case of ADC slave using its own DMA channel: check whether handle selected
  915. corresponds to ADC master or slave instance */
  916. if (__LL_ADC_MULTI_INSTANCE_MASTER(hadc->Instance) != hadc->Instance)
  917. {
  918. /* Case of ADC slave selected: enable ADC instance */
  919. tmp_hal_status = ADC_Enable(hadc);
  920. }
  921. else
  922. {
  923. tmphadcSlave.State = HAL_ADC_STATE_RESET;
  924. tmphadcSlave.ErrorCode = HAL_ADC_ERROR_NONE;
  925. /* Set a temporary handle of the ADC slave associated to the ADC master */
  926. ADC_MULTI_SLAVE(hadc, &tmphadcSlave);
  927. if (tmphadcSlave.Instance == NULL)
  928. {
  929. /* Set ADC state */
  930. SET_BIT(hadc->State, HAL_ADC_STATE_ERROR_CONFIG);
  931. /* Process unlocked */
  932. __HAL_UNLOCK(hadc);
  933. return HAL_ERROR;
  934. }
  935. /* Enable the ADC peripherals: master and slave (in case if not already */
  936. /* enabled previously) */
  937. tmp_hal_status = ADC_Enable(hadc);
  938. if (tmp_hal_status == HAL_OK)
  939. {
  940. tmp_hal_status = ADC_Enable(&tmphadcSlave);
  941. }
  942. }
  943. /* Start multimode conversion of ADCs pair */
  944. if (tmp_hal_status == HAL_OK)
  945. {
  946. /* Set ADC state */
  947. ADC_STATE_CLR_SET(hadc->State,
  948. (HAL_ADC_STATE_READY | HAL_ADC_STATE_REG_EOC | HAL_ADC_STATE_REG_OVR | HAL_ADC_STATE_REG_EOSMP),
  949. HAL_ADC_STATE_REG_BUSY);
  950. /* Set ADC error code to none */
  951. ADC_CLEAR_ERRORCODE(hadc);
  952. /* Set the DMA transfer complete callback */
  953. hadc->DMA_Handle->XferCpltCallback = ADC_DMAConvCplt;
  954. /* Set the DMA half transfer complete callback */
  955. hadc->DMA_Handle->XferHalfCpltCallback = ADC_DMAHalfConvCplt;
  956. /* Set the DMA error callback */
  957. hadc->DMA_Handle->XferErrorCallback = ADC_DMAError ;
  958. /* Manage ADC and DMA start: ADC overrun interruption, DMA start, ADC */
  959. /* start (in case of SW start): */
  960. /* Clear regular group conversion flag and overrun flag */
  961. /* (To ensure of no unknown state from potential previous ADC operations) */
  962. __HAL_ADC_CLEAR_FLAG(hadc, (ADC_FLAG_EOC | ADC_FLAG_EOS | ADC_FLAG_OVR));
  963. /* Process unlocked */
  964. /* Unlock before starting ADC conversions: in case of potential */
  965. /* interruption, to let the process to ADC IRQ Handler. */
  966. __HAL_UNLOCK(hadc);
  967. /* Enable ADC overrun interrupt */
  968. __HAL_ADC_ENABLE_IT(hadc, ADC_IT_OVR);
  969. /* Case of ADC slave using its own DMA channel: check whether handle selected
  970. corresponds to ADC master or slave instance */
  971. if (__LL_ADC_MULTI_INSTANCE_MASTER(hadc->Instance) != hadc->Instance)
  972. {
  973. /* Case of ADC slave selected: Start the DMA channel. */
  974. /* Note: Data transfer will start upon next call of this function using handle of ADC master */
  975. tmp_hal_status = HAL_DMA_Start_IT(hadc->DMA_Handle, (uint32_t)&hadc->Instance->DR, (uint32_t)pData, Length);
  976. }
  977. else
  978. {
  979. /* Pointer to the common control register */
  980. tmpADC_Common = __LL_ADC_COMMON_INSTANCE(hadc->Instance);
  981. /* Start the DMA channel */
  982. tmp_hal_status = HAL_DMA_Start_IT(hadc->DMA_Handle, (uint32_t)&tmpADC_Common->CDR, (uint32_t)pData, Length);
  983. /* Enable conversion of regular group. */
  984. /* If software start has been selected, conversion starts immediately. */
  985. /* If external trigger has been selected, conversion will start at next */
  986. /* trigger event. */
  987. /* Start ADC group regular conversion */
  988. LL_ADC_REG_StartConversion(hadc->Instance);
  989. }
  990. }
  991. else
  992. {
  993. /* Process unlocked */
  994. __HAL_UNLOCK(hadc);
  995. }
  996. /* Return function status */
  997. return tmp_hal_status;
  998. }
  999. }
  1000. /**
  1001. * @brief Stop multimode ADC conversion, disable ADC DMA transfer, disable ADC peripheral.
  1002. * @note Multimode is kept enabled after this function. MultiMode DMA bits
  1003. * (MDMA and DMACFG bits of common CCR register) are maintained. To disable
  1004. * Multimode (set with HAL_ADCEx_MultiModeConfigChannel()), ADC must be
  1005. * reinitialized using HAL_ADC_Init() or HAL_ADC_DeInit(), or the user can
  1006. * resort to HAL_ADCEx_DisableMultiMode() API.
  1007. * @note In case of DMA configured in circular mode, function
  1008. * HAL_ADC_Stop_DMA() must be called after this function with handle of
  1009. * ADC slave, to properly disable the DMA channel.
  1010. * @param hadc ADC handle of ADC master (handle of ADC slave must not be used)
  1011. * @retval HAL status
  1012. */
  1013. HAL_StatusTypeDef HAL_ADCEx_MultiModeStop_DMA(ADC_HandleTypeDef *hadc)
  1014. {
  1015. HAL_StatusTypeDef tmp_hal_status;
  1016. uint32_t tickstart;
  1017. ADC_HandleTypeDef tmphadcSlave;
  1018. uint32_t tmphadcSlave_conversion_on_going;
  1019. HAL_StatusTypeDef tmphadcSlave_disable_status;
  1020. /* Check the parameters */
  1021. assert_param(IS_ADC_MULTIMODE_MASTER_INSTANCE(hadc->Instance));
  1022. /* Process locked */
  1023. __HAL_LOCK(hadc);
  1024. /* 1. Stop potential multimode conversion on going, on regular and injected groups */
  1025. tmp_hal_status = ADC_ConversionStop(hadc, ADC_REGULAR_INJECTED_GROUP);
  1026. /* Disable ADC peripheral if conversions are effectively stopped */
  1027. if (tmp_hal_status == HAL_OK)
  1028. {
  1029. tmphadcSlave.State = HAL_ADC_STATE_RESET;
  1030. tmphadcSlave.ErrorCode = HAL_ADC_ERROR_NONE;
  1031. /* Set a temporary handle of the ADC slave associated to the ADC master */
  1032. ADC_MULTI_SLAVE(hadc, &tmphadcSlave);
  1033. if (tmphadcSlave.Instance == NULL)
  1034. {
  1035. /* Update ADC state machine to error */
  1036. SET_BIT(hadc->State, HAL_ADC_STATE_ERROR_CONFIG);
  1037. /* Process unlocked */
  1038. __HAL_UNLOCK(hadc);
  1039. return HAL_ERROR;
  1040. }
  1041. /* Procedure to disable the ADC peripheral: wait for conversions */
  1042. /* effectively stopped (ADC master and ADC slave), then disable ADC */
  1043. /* 1. Wait for ADC conversion completion for ADC master and ADC slave */
  1044. tickstart = HAL_GetTick();
  1045. tmphadcSlave_conversion_on_going = LL_ADC_REG_IsConversionOngoing((&tmphadcSlave)->Instance);
  1046. while ((LL_ADC_REG_IsConversionOngoing(hadc->Instance) == 1UL)
  1047. || (tmphadcSlave_conversion_on_going == 1UL)
  1048. )
  1049. {
  1050. if ((HAL_GetTick() - tickstart) > ADC_STOP_CONVERSION_TIMEOUT)
  1051. {
  1052. /* New check to avoid false timeout detection in case of preemption */
  1053. tmphadcSlave_conversion_on_going = LL_ADC_REG_IsConversionOngoing((&tmphadcSlave)->Instance);
  1054. if((LL_ADC_REG_IsConversionOngoing(hadc->Instance) == 1UL)
  1055. || (tmphadcSlave_conversion_on_going == 1UL)
  1056. )
  1057. {
  1058. /* Update ADC state machine to error */
  1059. SET_BIT(hadc->State, HAL_ADC_STATE_ERROR_INTERNAL);
  1060. /* Process unlocked */
  1061. __HAL_UNLOCK(hadc);
  1062. return HAL_ERROR;
  1063. }
  1064. }
  1065. tmphadcSlave_conversion_on_going = LL_ADC_REG_IsConversionOngoing((&tmphadcSlave)->Instance);
  1066. }
  1067. /* Disable the DMA channel (in case of DMA in circular mode or stop */
  1068. /* while DMA transfer is on going) */
  1069. /* Note: DMA channel of ADC slave should be stopped after this function */
  1070. /* with HAL_ADC_Stop_DMA() API. */
  1071. tmp_hal_status = HAL_DMA_Abort(hadc->DMA_Handle);
  1072. /* Check if DMA channel effectively disabled */
  1073. if (tmp_hal_status == HAL_ERROR)
  1074. {
  1075. /* Update ADC state machine to error */
  1076. SET_BIT(hadc->State, HAL_ADC_STATE_ERROR_DMA);
  1077. }
  1078. /* Disable ADC overrun interrupt */
  1079. __HAL_ADC_DISABLE_IT(hadc, ADC_IT_OVR);
  1080. /* 2. Disable the ADC peripherals: master and slave */
  1081. /* Update "tmp_hal_status" only if DMA channel disabling passed, to keep in */
  1082. /* memory a potential failing status. */
  1083. if (tmp_hal_status == HAL_OK)
  1084. {
  1085. tmphadcSlave_disable_status = ADC_Disable(&tmphadcSlave);
  1086. if ((ADC_Disable(hadc) == HAL_OK) &&
  1087. (tmphadcSlave_disable_status == HAL_OK))
  1088. {
  1089. tmp_hal_status = HAL_OK;
  1090. }
  1091. }
  1092. else
  1093. {
  1094. /* In case of error, attempt to disable ADC master and slave without status assert */
  1095. (void) ADC_Disable(hadc);
  1096. (void) ADC_Disable(&tmphadcSlave);
  1097. }
  1098. /* Set ADC state (ADC master) */
  1099. ADC_STATE_CLR_SET(hadc->State,
  1100. HAL_ADC_STATE_REG_BUSY | HAL_ADC_STATE_INJ_BUSY,
  1101. HAL_ADC_STATE_READY);
  1102. }
  1103. /* Process unlocked */
  1104. __HAL_UNLOCK(hadc);
  1105. /* Return function status */
  1106. return tmp_hal_status;
  1107. }
  1108. /**
  1109. * @brief Return the last ADC Master and Slave regular conversions results when in multimode configuration.
  1110. * @param hadc ADC handle of ADC Master (handle of ADC Slave must not be used)
  1111. * @retval The converted data values.
  1112. */
  1113. uint32_t HAL_ADCEx_MultiModeGetValue(ADC_HandleTypeDef *hadc)
  1114. {
  1115. const ADC_Common_TypeDef *tmpADC_Common;
  1116. /* Check the parameters */
  1117. assert_param(IS_ADC_MULTIMODE_MASTER_INSTANCE(hadc->Instance));
  1118. /* Prevent unused argument(s) compilation warning if no assert_param check */
  1119. /* and possible no usage in __LL_ADC_COMMON_INSTANCE() below */
  1120. UNUSED(hadc);
  1121. /* Pointer to the common control register */
  1122. tmpADC_Common = __LL_ADC_COMMON_INSTANCE(hadc->Instance);
  1123. /* Return the multi mode conversion value */
  1124. return tmpADC_Common->CDR;
  1125. }
  1126. /**
  1127. * @brief Get ADC injected group conversion result.
  1128. * @note Reading register JDRx automatically clears ADC flag JEOC
  1129. * (ADC group injected end of unitary conversion).
  1130. * @note This function does not clear ADC flag JEOS
  1131. * (ADC group injected end of sequence conversion)
  1132. * Occurrence of flag JEOS rising:
  1133. * - If sequencer is composed of 1 rank, flag JEOS is equivalent
  1134. * to flag JEOC.
  1135. * - If sequencer is composed of several ranks, during the scan
  1136. * sequence flag JEOC only is raised, at the end of the scan sequence
  1137. * both flags JEOC and EOS are raised.
  1138. * Flag JEOS must not be cleared by this function because
  1139. * it would not be compliant with low power features
  1140. * (feature low power auto-wait, not available on all STM32 families).
  1141. * To clear this flag, either use function:
  1142. * in programming model IT: @ref HAL_ADC_IRQHandler(), in programming
  1143. * model polling: @ref HAL_ADCEx_InjectedPollForConversion()
  1144. * or @ref __HAL_ADC_CLEAR_FLAG(&hadc, ADC_FLAG_JEOS).
  1145. * @param hadc ADC handle
  1146. * @param InjectedRank the converted ADC injected rank.
  1147. * This parameter can be one of the following values:
  1148. * @arg @ref ADC_INJECTED_RANK_1 ADC group injected rank 1
  1149. * @arg @ref ADC_INJECTED_RANK_2 ADC group injected rank 2
  1150. * @arg @ref ADC_INJECTED_RANK_3 ADC group injected rank 3
  1151. * @arg @ref ADC_INJECTED_RANK_4 ADC group injected rank 4
  1152. * @retval ADC group injected conversion data
  1153. */
  1154. uint32_t HAL_ADCEx_InjectedGetValue(ADC_HandleTypeDef *hadc, uint32_t InjectedRank)
  1155. {
  1156. uint32_t tmp_jdr;
  1157. /* Check the parameters */
  1158. assert_param(IS_ADC_ALL_INSTANCE(hadc->Instance));
  1159. assert_param(IS_ADC_INJECTED_RANK(InjectedRank));
  1160. /* Get ADC converted value */
  1161. switch (InjectedRank)
  1162. {
  1163. case ADC_INJECTED_RANK_4:
  1164. tmp_jdr = hadc->Instance->JDR4;
  1165. break;
  1166. case ADC_INJECTED_RANK_3:
  1167. tmp_jdr = hadc->Instance->JDR3;
  1168. break;
  1169. case ADC_INJECTED_RANK_2:
  1170. tmp_jdr = hadc->Instance->JDR2;
  1171. break;
  1172. case ADC_INJECTED_RANK_1:
  1173. default:
  1174. tmp_jdr = hadc->Instance->JDR1;
  1175. break;
  1176. }
  1177. /* Return ADC converted value */
  1178. return tmp_jdr;
  1179. }
  1180. /**
  1181. * @brief Injected conversion complete callback in non-blocking mode.
  1182. * @param hadc ADC handle
  1183. * @retval None
  1184. */
  1185. __weak void HAL_ADCEx_InjectedConvCpltCallback(ADC_HandleTypeDef *hadc)
  1186. {
  1187. /* Prevent unused argument(s) compilation warning */
  1188. UNUSED(hadc);
  1189. /* NOTE : This function should not be modified. When the callback is needed,
  1190. function HAL_ADCEx_InjectedConvCpltCallback must be implemented in the user file.
  1191. */
  1192. }
  1193. /**
  1194. * @brief Injected context queue overflow callback.
  1195. * @note This callback is called if injected context queue is enabled
  1196. (parameter "QueueInjectedContext" in injected channel configuration)
  1197. and if a new injected context is set when queue is full (maximum 2
  1198. contexts).
  1199. * @param hadc ADC handle
  1200. * @retval None
  1201. */
  1202. __weak void HAL_ADCEx_InjectedQueueOverflowCallback(ADC_HandleTypeDef *hadc)
  1203. {
  1204. /* Prevent unused argument(s) compilation warning */
  1205. UNUSED(hadc);
  1206. /* NOTE : This function should not be modified. When the callback is needed,
  1207. function HAL_ADCEx_InjectedQueueOverflowCallback must be implemented in the user file.
  1208. */
  1209. }
  1210. /**
  1211. * @brief Analog watchdog 2 callback in non-blocking mode.
  1212. * @param hadc ADC handle
  1213. * @retval None
  1214. */
  1215. __weak void HAL_ADCEx_LevelOutOfWindow2Callback(ADC_HandleTypeDef *hadc)
  1216. {
  1217. /* Prevent unused argument(s) compilation warning */
  1218. UNUSED(hadc);
  1219. /* NOTE : This function should not be modified. When the callback is needed,
  1220. function HAL_ADCEx_LevelOutOfWindow2Callback must be implemented in the user file.
  1221. */
  1222. }
  1223. /**
  1224. * @brief Analog watchdog 3 callback in non-blocking mode.
  1225. * @param hadc ADC handle
  1226. * @retval None
  1227. */
  1228. __weak void HAL_ADCEx_LevelOutOfWindow3Callback(ADC_HandleTypeDef *hadc)
  1229. {
  1230. /* Prevent unused argument(s) compilation warning */
  1231. UNUSED(hadc);
  1232. /* NOTE : This function should not be modified. When the callback is needed,
  1233. function HAL_ADCEx_LevelOutOfWindow3Callback must be implemented in the user file.
  1234. */
  1235. }
  1236. /**
  1237. * @brief End Of Sampling callback in non-blocking mode.
  1238. * @param hadc ADC handle
  1239. * @retval None
  1240. */
  1241. __weak void HAL_ADCEx_EndOfSamplingCallback(ADC_HandleTypeDef *hadc)
  1242. {
  1243. /* Prevent unused argument(s) compilation warning */
  1244. UNUSED(hadc);
  1245. /* NOTE : This function should not be modified. When the callback is needed,
  1246. function HAL_ADCEx_EndOfSamplingCallback must be implemented in the user file.
  1247. */
  1248. }
  1249. /**
  1250. * @brief Stop ADC conversion of regular group (and injected channels in
  1251. * case of auto_injection mode), disable ADC peripheral if no
  1252. * conversion is on going on injected group.
  1253. * @param hadc ADC handle
  1254. * @retval HAL status.
  1255. */
  1256. HAL_StatusTypeDef HAL_ADCEx_RegularStop(ADC_HandleTypeDef *hadc)
  1257. {
  1258. HAL_StatusTypeDef tmp_hal_status;
  1259. /* Check the parameters */
  1260. assert_param(IS_ADC_ALL_INSTANCE(hadc->Instance));
  1261. /* Process locked */
  1262. __HAL_LOCK(hadc);
  1263. /* 1. Stop potential regular conversion on going */
  1264. tmp_hal_status = ADC_ConversionStop(hadc, ADC_REGULAR_GROUP);
  1265. /* Disable ADC peripheral if regular conversions are effectively stopped
  1266. and if no injected conversions are on-going */
  1267. if (tmp_hal_status == HAL_OK)
  1268. {
  1269. /* Clear HAL_ADC_STATE_REG_BUSY bit */
  1270. CLEAR_BIT(hadc->State, HAL_ADC_STATE_REG_BUSY);
  1271. if (LL_ADC_INJ_IsConversionOngoing(hadc->Instance) == 0UL)
  1272. {
  1273. /* 2. Disable the ADC peripheral */
  1274. tmp_hal_status = ADC_Disable(hadc);
  1275. /* Check if ADC is effectively disabled */
  1276. if (tmp_hal_status == HAL_OK)
  1277. {
  1278. /* Set ADC state */
  1279. ADC_STATE_CLR_SET(hadc->State,
  1280. HAL_ADC_STATE_INJ_BUSY,
  1281. HAL_ADC_STATE_READY);
  1282. }
  1283. }
  1284. /* Conversion on injected group is stopped, but ADC not disabled since */
  1285. /* conversion on regular group is still running. */
  1286. else
  1287. {
  1288. SET_BIT(hadc->State, HAL_ADC_STATE_INJ_BUSY);
  1289. }
  1290. }
  1291. /* Process unlocked */
  1292. __HAL_UNLOCK(hadc);
  1293. /* Return function status */
  1294. return tmp_hal_status;
  1295. }
  1296. /**
  1297. * @brief Stop ADC conversion of ADC groups regular and injected,
  1298. * disable interrution of end-of-conversion,
  1299. * disable ADC peripheral if no conversion is on going
  1300. * on injected group.
  1301. * @param hadc ADC handle
  1302. * @retval HAL status.
  1303. */
  1304. HAL_StatusTypeDef HAL_ADCEx_RegularStop_IT(ADC_HandleTypeDef *hadc)
  1305. {
  1306. HAL_StatusTypeDef tmp_hal_status;
  1307. /* Check the parameters */
  1308. assert_param(IS_ADC_ALL_INSTANCE(hadc->Instance));
  1309. /* Process locked */
  1310. __HAL_LOCK(hadc);
  1311. /* 1. Stop potential regular conversion on going */
  1312. tmp_hal_status = ADC_ConversionStop(hadc, ADC_REGULAR_GROUP);
  1313. /* Disable ADC peripheral if conversions are effectively stopped
  1314. and if no injected conversion is on-going */
  1315. if (tmp_hal_status == HAL_OK)
  1316. {
  1317. /* Clear HAL_ADC_STATE_REG_BUSY bit */
  1318. CLEAR_BIT(hadc->State, HAL_ADC_STATE_REG_BUSY);
  1319. /* Disable all regular-related interrupts */
  1320. __HAL_ADC_DISABLE_IT(hadc, (ADC_IT_EOC | ADC_IT_EOS | ADC_IT_OVR));
  1321. /* 2. Disable ADC peripheral if no injected conversions are on-going */
  1322. if (LL_ADC_INJ_IsConversionOngoing(hadc->Instance) == 0UL)
  1323. {
  1324. tmp_hal_status = ADC_Disable(hadc);
  1325. /* if no issue reported */
  1326. if (tmp_hal_status == HAL_OK)
  1327. {
  1328. /* Set ADC state */
  1329. ADC_STATE_CLR_SET(hadc->State,
  1330. HAL_ADC_STATE_INJ_BUSY,
  1331. HAL_ADC_STATE_READY);
  1332. }
  1333. }
  1334. else
  1335. {
  1336. SET_BIT(hadc->State, HAL_ADC_STATE_INJ_BUSY);
  1337. }
  1338. }
  1339. /* Process unlocked */
  1340. __HAL_UNLOCK(hadc);
  1341. /* Return function status */
  1342. return tmp_hal_status;
  1343. }
  1344. /**
  1345. * @brief Stop ADC conversion of regular group (and injected group in
  1346. * case of auto_injection mode), disable ADC DMA transfer, disable
  1347. * ADC peripheral if no conversion is on going
  1348. * on injected group.
  1349. * @note HAL_ADCEx_RegularStop_DMA() function is dedicated to single-ADC mode only.
  1350. * For multimode (when multimode feature is available),
  1351. * HAL_ADCEx_RegularMultiModeStop_DMA() API must be used.
  1352. * @param hadc ADC handle
  1353. * @retval HAL status.
  1354. */
  1355. HAL_StatusTypeDef HAL_ADCEx_RegularStop_DMA(ADC_HandleTypeDef *hadc)
  1356. {
  1357. HAL_StatusTypeDef tmp_hal_status;
  1358. /* Check the parameters */
  1359. assert_param(IS_ADC_ALL_INSTANCE(hadc->Instance));
  1360. /* Process locked */
  1361. __HAL_LOCK(hadc);
  1362. /* 1. Stop potential regular conversion on going */
  1363. tmp_hal_status = ADC_ConversionStop(hadc, ADC_REGULAR_GROUP);
  1364. /* Disable ADC peripheral if conversions are effectively stopped
  1365. and if no injected conversion is on-going */
  1366. if (tmp_hal_status == HAL_OK)
  1367. {
  1368. /* Clear HAL_ADC_STATE_REG_BUSY bit */
  1369. CLEAR_BIT(hadc->State, HAL_ADC_STATE_REG_BUSY);
  1370. /* Disable ADC DMA (ADC DMA configuration ADC_CFGR_DMACFG is kept) */
  1371. MODIFY_REG(hadc->Instance->CFGR, ADC_CFGR_DMNGT_0 | ADC_CFGR_DMNGT_1, 0UL);
  1372. /* Disable the DMA channel (in case of DMA in circular mode or stop while */
  1373. /* while DMA transfer is on going) */
  1374. tmp_hal_status = HAL_DMA_Abort(hadc->DMA_Handle);
  1375. /* Check if DMA channel effectively disabled */
  1376. if (tmp_hal_status != HAL_OK)
  1377. {
  1378. /* Update ADC state machine to error */
  1379. SET_BIT(hadc->State, HAL_ADC_STATE_ERROR_DMA);
  1380. }
  1381. /* Disable ADC overrun interrupt */
  1382. __HAL_ADC_DISABLE_IT(hadc, ADC_IT_OVR);
  1383. /* 2. Disable the ADC peripheral */
  1384. /* Update "tmp_hal_status" only if DMA channel disabling passed, */
  1385. /* to keep in memory a potential failing status. */
  1386. if (LL_ADC_INJ_IsConversionOngoing(hadc->Instance) == 0UL)
  1387. {
  1388. if (tmp_hal_status == HAL_OK)
  1389. {
  1390. tmp_hal_status = ADC_Disable(hadc);
  1391. }
  1392. else
  1393. {
  1394. (void)ADC_Disable(hadc);
  1395. }
  1396. /* Check if ADC is effectively disabled */
  1397. if (tmp_hal_status == HAL_OK)
  1398. {
  1399. /* Set ADC state */
  1400. ADC_STATE_CLR_SET(hadc->State,
  1401. HAL_ADC_STATE_INJ_BUSY,
  1402. HAL_ADC_STATE_READY);
  1403. }
  1404. }
  1405. else
  1406. {
  1407. SET_BIT(hadc->State, HAL_ADC_STATE_INJ_BUSY);
  1408. }
  1409. }
  1410. /* Process unlocked */
  1411. __HAL_UNLOCK(hadc);
  1412. /* Return function status */
  1413. return tmp_hal_status;
  1414. }
  1415. /**
  1416. * @brief Stop DMA-based multimode ADC conversion, disable ADC DMA transfer, disable ADC peripheral if no injected conversion is on-going.
  1417. * @note Multimode is kept enabled after this function. Multimode DMA bits
  1418. * (MDMA and DMACFG bits of common CCR register) are maintained. To disable
  1419. * multimode (set with HAL_ADCEx_MultiModeConfigChannel()), ADC must be
  1420. * reinitialized using HAL_ADC_Init() or HAL_ADC_DeInit(), or the user can
  1421. * resort to HAL_ADCEx_DisableMultiMode() API.
  1422. * @note In case of DMA configured in circular mode, function
  1423. * HAL_ADCEx_RegularStop_DMA() must be called after this function with handle of
  1424. * ADC slave, to properly disable the DMA channel.
  1425. * @param hadc ADC handle of ADC master (handle of ADC slave must not be used)
  1426. * @retval HAL status
  1427. */
  1428. HAL_StatusTypeDef HAL_ADCEx_RegularMultiModeStop_DMA(ADC_HandleTypeDef *hadc)
  1429. {
  1430. HAL_StatusTypeDef tmp_hal_status;
  1431. uint32_t tickstart;
  1432. ADC_HandleTypeDef tmphadcSlave;
  1433. uint32_t tmphadcSlave_conversion_on_going;
  1434. /* Check the parameters */
  1435. assert_param(IS_ADC_MULTIMODE_MASTER_INSTANCE(hadc->Instance));
  1436. /* Process locked */
  1437. __HAL_LOCK(hadc);
  1438. /* 1. Stop potential multimode conversion on going, on regular groups */
  1439. tmp_hal_status = ADC_ConversionStop(hadc, ADC_REGULAR_GROUP);
  1440. /* Disable ADC peripheral if conversions are effectively stopped */
  1441. if (tmp_hal_status == HAL_OK)
  1442. {
  1443. /* Clear HAL_ADC_STATE_REG_BUSY bit */
  1444. CLEAR_BIT(hadc->State, HAL_ADC_STATE_REG_BUSY);
  1445. tmphadcSlave.State = HAL_ADC_STATE_RESET;
  1446. tmphadcSlave.ErrorCode = HAL_ADC_ERROR_NONE;
  1447. /* Set a temporary handle of the ADC slave associated to the ADC master */
  1448. ADC_MULTI_SLAVE(hadc, &tmphadcSlave);
  1449. if (tmphadcSlave.Instance == NULL)
  1450. {
  1451. /* Update ADC state machine to error */
  1452. SET_BIT(hadc->State, HAL_ADC_STATE_ERROR_CONFIG);
  1453. /* Process unlocked */
  1454. __HAL_UNLOCK(hadc);
  1455. return HAL_ERROR;
  1456. }
  1457. /* Procedure to disable the ADC peripheral: wait for conversions */
  1458. /* effectively stopped (ADC master and ADC slave), then disable ADC */
  1459. /* 1. Wait for ADC conversion completion for ADC master and ADC slave */
  1460. tickstart = HAL_GetTick();
  1461. tmphadcSlave_conversion_on_going = LL_ADC_REG_IsConversionOngoing((&tmphadcSlave)->Instance);
  1462. while ((LL_ADC_REG_IsConversionOngoing(hadc->Instance) == 1UL)
  1463. || (tmphadcSlave_conversion_on_going == 1UL)
  1464. )
  1465. {
  1466. if ((HAL_GetTick() - tickstart) > ADC_STOP_CONVERSION_TIMEOUT)
  1467. {
  1468. /* New check to avoid false timeout detection in case of preemption */
  1469. tmphadcSlave_conversion_on_going = LL_ADC_REG_IsConversionOngoing((&tmphadcSlave)->Instance);
  1470. if((LL_ADC_REG_IsConversionOngoing(hadc->Instance) == 1UL)
  1471. || (tmphadcSlave_conversion_on_going == 1UL)
  1472. )
  1473. {
  1474. /* Update ADC state machine to error */
  1475. SET_BIT(hadc->State, HAL_ADC_STATE_ERROR_INTERNAL);
  1476. /* Process unlocked */
  1477. __HAL_UNLOCK(hadc);
  1478. return HAL_ERROR;
  1479. }
  1480. }
  1481. tmphadcSlave_conversion_on_going = LL_ADC_REG_IsConversionOngoing((&tmphadcSlave)->Instance);
  1482. }
  1483. /* Disable the DMA channel (in case of DMA in circular mode or stop */
  1484. /* while DMA transfer is on going) */
  1485. /* Note: DMA channel of ADC slave should be stopped after this function */
  1486. /* with HAL_ADCEx_RegularStop_DMA() API. */
  1487. tmp_hal_status = HAL_DMA_Abort(hadc->DMA_Handle);
  1488. /* Check if DMA channel effectively disabled */
  1489. if (tmp_hal_status != HAL_OK)
  1490. {
  1491. /* Update ADC state machine to error */
  1492. SET_BIT(hadc->State, HAL_ADC_STATE_ERROR_DMA);
  1493. }
  1494. /* Disable ADC overrun interrupt */
  1495. __HAL_ADC_DISABLE_IT(hadc, ADC_IT_OVR);
  1496. /* 2. Disable the ADC peripherals: master and slave if no injected */
  1497. /* conversion is on-going. */
  1498. /* Update "tmp_hal_status" only if DMA channel disabling passed, to keep in */
  1499. /* memory a potential failing status. */
  1500. if (tmp_hal_status == HAL_OK)
  1501. {
  1502. if (LL_ADC_INJ_IsConversionOngoing(hadc->Instance) == 0UL)
  1503. {
  1504. tmp_hal_status = ADC_Disable(hadc);
  1505. if (tmp_hal_status == HAL_OK)
  1506. {
  1507. if (LL_ADC_INJ_IsConversionOngoing((&tmphadcSlave)->Instance) == 0UL)
  1508. {
  1509. tmp_hal_status = ADC_Disable(&tmphadcSlave);
  1510. }
  1511. }
  1512. }
  1513. if (tmp_hal_status == HAL_OK)
  1514. {
  1515. /* Both Master and Slave ADC's could be disabled. Update Master State */
  1516. /* Clear HAL_ADC_STATE_INJ_BUSY bit, set HAL_ADC_STATE_READY bit */
  1517. ADC_STATE_CLR_SET(hadc->State, HAL_ADC_STATE_INJ_BUSY, HAL_ADC_STATE_READY);
  1518. }
  1519. else
  1520. {
  1521. /* injected (Master or Slave) conversions are still on-going,
  1522. no Master State change */
  1523. }
  1524. }
  1525. }
  1526. /* Process unlocked */
  1527. __HAL_UNLOCK(hadc);
  1528. /* Return function status */
  1529. return tmp_hal_status;
  1530. }
  1531. /**
  1532. * @}
  1533. */
  1534. /** @defgroup ADCEx_Exported_Functions_Group2 ADC Extended Peripheral Control functions
  1535. * @brief ADC Extended Peripheral Control functions
  1536. *
  1537. @verbatim
  1538. ===============================================================================
  1539. ##### Peripheral Control functions #####
  1540. ===============================================================================
  1541. [..] This section provides functions allowing to:
  1542. (+) Configure channels on injected group
  1543. (+) Configure multimode when multimode feature is available
  1544. (+) Enable or Disable Injected Queue
  1545. (+) Disable ADC voltage regulator
  1546. (+) Enter ADC deep-power-down mode
  1547. @endverbatim
  1548. * @{
  1549. */
  1550. /**
  1551. * @brief Configure a channel to be assigned to ADC group injected.
  1552. * @note Possibility to update parameters on the fly:
  1553. * This function initializes injected group, following calls to this
  1554. * function can be used to reconfigure some parameters of structure
  1555. * "ADC_InjectionConfTypeDef" on the fly, without resetting the ADC.
  1556. * The setting of these parameters is conditioned to ADC state:
  1557. * Refer to comments of structure "ADC_InjectionConfTypeDef".
  1558. * @note In case of usage of internal measurement channels:
  1559. * Vbat/VrefInt/TempSensor.
  1560. * These internal paths can be disabled using function
  1561. * HAL_ADC_DeInit().
  1562. * @note Caution: For Injected Context Queue use, a context must be fully
  1563. * defined before start of injected conversion. All channels are configured
  1564. * consecutively for the same ADC instance. Therefore, the number of calls to
  1565. * HAL_ADCEx_InjectedConfigChannel() must be equal to the value of parameter
  1566. * InjectedNbrOfConversion for each context.
  1567. * - Example 1: If 1 context is intended to be used (or if there is no use of the
  1568. * Injected Queue Context feature) and if the context contains 3 injected ranks
  1569. * (InjectedNbrOfConversion = 3), HAL_ADCEx_InjectedConfigChannel() must be
  1570. * called once for each channel (i.e. 3 times) before starting a conversion.
  1571. * This function must not be called to configure a 4th injected channel:
  1572. * it would start a new context into context queue.
  1573. * - Example 2: If 2 contexts are intended to be used and each of them contains
  1574. * 3 injected ranks (InjectedNbrOfConversion = 3),
  1575. * HAL_ADCEx_InjectedConfigChannel() must be called once for each channel and
  1576. * for each context (3 channels x 2 contexts = 6 calls). Conversion can
  1577. * start once the 1st context is set, that is after the first three
  1578. * HAL_ADCEx_InjectedConfigChannel() calls. The 2nd context can be set on the fly.
  1579. * @param hadc ADC handle
  1580. * @param sConfigInjected Structure of ADC injected group and ADC channel for
  1581. * injected group.
  1582. * @retval HAL status
  1583. */
  1584. HAL_StatusTypeDef HAL_ADCEx_InjectedConfigChannel(ADC_HandleTypeDef *hadc, ADC_InjectionConfTypeDef *sConfigInjected)
  1585. {
  1586. HAL_StatusTypeDef tmp_hal_status = HAL_OK;
  1587. uint32_t tmpOffsetShifted;
  1588. uint32_t tmp_config_internal_channel;
  1589. uint32_t tmp_adc_is_conversion_on_going_regular;
  1590. uint32_t tmp_adc_is_conversion_on_going_injected;
  1591. __IO uint32_t wait_loop_index = 0;
  1592. uint32_t tmp_JSQR_ContextQueueBeingBuilt = 0U;
  1593. /* Check the parameters */
  1594. assert_param(IS_ADC_ALL_INSTANCE(hadc->Instance));
  1595. assert_param(IS_ADC_SAMPLE_TIME(sConfigInjected->InjectedSamplingTime));
  1596. assert_param(IS_ADC_SINGLE_DIFFERENTIAL(sConfigInjected->InjectedSingleDiff));
  1597. assert_param(IS_FUNCTIONAL_STATE(sConfigInjected->AutoInjectedConv));
  1598. assert_param(IS_FUNCTIONAL_STATE(sConfigInjected->QueueInjectedContext));
  1599. assert_param(IS_ADC_EXTTRIGINJEC_EDGE(sConfigInjected->ExternalTrigInjecConvEdge));
  1600. assert_param(IS_ADC_EXTTRIGINJEC(sConfigInjected->ExternalTrigInjecConv));
  1601. assert_param(IS_ADC_OFFSET_NUMBER(sConfigInjected->InjectedOffsetNumber));
  1602. assert_param(IS_FUNCTIONAL_STATE(sConfigInjected->InjecOversamplingMode));
  1603. #if defined(ADC_VER_V5_V90)
  1604. assert_param(IS_FUNCTIONAL_STATE(sConfigInjected->InjectedOffsetSaturation));
  1605. if (hadc->Instance == ADC3)
  1606. {
  1607. assert_param(IS_ADC3_OFFSET_SIGN(sConfigInjected->InjectedOffsetSign));
  1608. assert_param(IS_ADC3_RANGE(ADC_GET_RESOLUTION(hadc), sConfigInjected->InjectedOffset));
  1609. }
  1610. else
  1611. #endif /* ADC_VER_V5_V90 */
  1612. {
  1613. assert_param(IS_ADC_RANGE(ADC_GET_RESOLUTION(hadc), sConfigInjected->InjectedOffset));
  1614. }
  1615. if (hadc->Init.ScanConvMode != ADC_SCAN_DISABLE)
  1616. {
  1617. assert_param(IS_ADC_INJECTED_RANK(sConfigInjected->InjectedRank));
  1618. assert_param(IS_ADC_INJECTED_NB_CONV(sConfigInjected->InjectedNbrOfConversion));
  1619. assert_param(IS_FUNCTIONAL_STATE(sConfigInjected->InjectedDiscontinuousConvMode));
  1620. }
  1621. /* Check offset range according to oversampling setting */
  1622. if (hadc->Init.OversamplingMode == ENABLE)
  1623. {
  1624. assert_param(IS_ADC_RANGE(ADC_GET_RESOLUTION(hadc), sConfigInjected->InjectedOffset / (hadc->Init.Oversampling.Ratio + 1U)));
  1625. }
  1626. else
  1627. {
  1628. assert_param(IS_ADC_RANGE(ADC_GET_RESOLUTION(hadc), sConfigInjected->InjectedOffset));
  1629. }
  1630. #if defined(ADC_VER_V5_V90)
  1631. /* if JOVSE is set, the value of the OFFSETy_EN bit in ADCx_OFRy register is
  1632. ignored (considered as reset) */
  1633. if (hadc->Instance == ADC3)
  1634. {
  1635. assert_param(!((sConfigInjected->InjectedOffsetNumber != ADC_OFFSET_NONE) && (sConfigInjected->InjecOversamplingMode == ENABLE)));
  1636. }
  1637. #endif /* ADC_VER_V5_V90 */
  1638. /* JDISCEN and JAUTO bits can't be set at the same time */
  1639. assert_param(!((sConfigInjected->InjectedDiscontinuousConvMode == ENABLE) && (sConfigInjected->AutoInjectedConv == ENABLE)));
  1640. /* DISCEN and JAUTO bits can't be set at the same time */
  1641. assert_param(!((hadc->Init.DiscontinuousConvMode == ENABLE) && (sConfigInjected->AutoInjectedConv == ENABLE)));
  1642. /* Verification of channel number */
  1643. if (sConfigInjected->InjectedSingleDiff != ADC_DIFFERENTIAL_ENDED)
  1644. {
  1645. assert_param(IS_ADC_CHANNEL(sConfigInjected->InjectedChannel));
  1646. }
  1647. else
  1648. {
  1649. if (hadc->Instance == ADC1)
  1650. {
  1651. assert_param(IS_ADC1_DIFF_CHANNEL(sConfigInjected->InjectedChannel));
  1652. }
  1653. if (hadc->Instance == ADC2)
  1654. {
  1655. assert_param(IS_ADC2_DIFF_CHANNEL(sConfigInjected->InjectedChannel));
  1656. }
  1657. #if defined (ADC3)
  1658. if (hadc->Instance == ADC3)
  1659. {
  1660. assert_param(IS_ADC3_DIFF_CHANNEL(sConfigInjected->InjectedChannel));
  1661. }
  1662. #endif
  1663. }
  1664. /* Process locked */
  1665. __HAL_LOCK(hadc);
  1666. /* Configuration of injected group sequencer: */
  1667. /* Hardware constraint: Must fully define injected context register JSQR */
  1668. /* before make it entering into injected sequencer queue. */
  1669. /* */
  1670. /* - if scan mode is disabled: */
  1671. /* * Injected channels sequence length is set to 0x00: 1 channel */
  1672. /* converted (channel on injected rank 1) */
  1673. /* Parameter "InjectedNbrOfConversion" is discarded. */
  1674. /* * Injected context register JSQR setting is simple: register is fully */
  1675. /* defined on one call of this function (for injected rank 1) and can */
  1676. /* be entered into queue directly. */
  1677. /* - if scan mode is enabled: */
  1678. /* * Injected channels sequence length is set to parameter */
  1679. /* "InjectedNbrOfConversion". */
  1680. /* * Injected context register JSQR setting more complex: register is */
  1681. /* fully defined over successive calls of this function, for each */
  1682. /* injected channel rank. It is entered into queue only when all */
  1683. /* injected ranks have been set. */
  1684. /* Note: Scan mode is not present by hardware on this device, but used */
  1685. /* by software for alignment over all STM32 devices. */
  1686. if ((hadc->Init.ScanConvMode == ADC_SCAN_DISABLE) ||
  1687. (sConfigInjected->InjectedNbrOfConversion == 1U))
  1688. {
  1689. /* Configuration of context register JSQR: */
  1690. /* - number of ranks in injected group sequencer: fixed to 1st rank */
  1691. /* (scan mode disabled, only rank 1 used) */
  1692. /* - external trigger to start conversion */
  1693. /* - external trigger polarity */
  1694. /* - channel set to rank 1 (scan mode disabled, only rank 1 can be used) */
  1695. if (sConfigInjected->InjectedRank == ADC_INJECTED_RANK_1)
  1696. {
  1697. /* Enable external trigger if trigger selection is different of */
  1698. /* software start. */
  1699. /* Note: This configuration keeps the hardware feature of parameter */
  1700. /* ExternalTrigInjecConvEdge "trigger edge none" equivalent to */
  1701. /* software start. */
  1702. if (sConfigInjected->ExternalTrigInjecConv != ADC_INJECTED_SOFTWARE_START)
  1703. {
  1704. tmp_JSQR_ContextQueueBeingBuilt = (ADC_JSQR_RK(sConfigInjected->InjectedChannel, ADC_INJECTED_RANK_1)
  1705. | (sConfigInjected->ExternalTrigInjecConv & ADC_JSQR_JEXTSEL)
  1706. | sConfigInjected->ExternalTrigInjecConvEdge
  1707. );
  1708. }
  1709. else
  1710. {
  1711. tmp_JSQR_ContextQueueBeingBuilt = (ADC_JSQR_RK(sConfigInjected->InjectedChannel, ADC_INJECTED_RANK_1));
  1712. }
  1713. MODIFY_REG(hadc->Instance->JSQR, ADC_JSQR_FIELDS, tmp_JSQR_ContextQueueBeingBuilt);
  1714. /* For debug and informative reasons, hadc handle saves JSQR setting */
  1715. hadc->InjectionConfig.ContextQueue = tmp_JSQR_ContextQueueBeingBuilt;
  1716. }
  1717. }
  1718. else
  1719. {
  1720. /* Case of scan mode enabled, several channels to set into injected group */
  1721. /* sequencer. */
  1722. /* */
  1723. /* Procedure to define injected context register JSQR over successive */
  1724. /* calls of this function, for each injected channel rank: */
  1725. /* 1. Start new context and set parameters related to all injected */
  1726. /* channels: injected sequence length and trigger. */
  1727. /* if hadc->InjectionConfig.ChannelCount is equal to 0, this is the first */
  1728. /* call of the context under setting */
  1729. if (hadc->InjectionConfig.ChannelCount == 0U)
  1730. {
  1731. /* Initialize number of channels that will be configured on the context */
  1732. /* being built */
  1733. hadc->InjectionConfig.ChannelCount = sConfigInjected->InjectedNbrOfConversion;
  1734. /* Handle hadc saves the context under build up over each HAL_ADCEx_InjectedConfigChannel()
  1735. call, this context will be written in JSQR register at the last call.
  1736. At this point, the context is merely reset */
  1737. hadc->InjectionConfig.ContextQueue = 0x00000000U;
  1738. /* Configuration of context register JSQR: */
  1739. /* - number of ranks in injected group sequencer */
  1740. /* - external trigger to start conversion */
  1741. /* - external trigger polarity */
  1742. /* Enable external trigger if trigger selection is different of */
  1743. /* software start. */
  1744. /* Note: This configuration keeps the hardware feature of parameter */
  1745. /* ExternalTrigInjecConvEdge "trigger edge none" equivalent to */
  1746. /* software start. */
  1747. if (sConfigInjected->ExternalTrigInjecConv != ADC_INJECTED_SOFTWARE_START)
  1748. {
  1749. tmp_JSQR_ContextQueueBeingBuilt = ((sConfigInjected->InjectedNbrOfConversion - 1U)
  1750. | (sConfigInjected->ExternalTrigInjecConv & ADC_JSQR_JEXTSEL)
  1751. | sConfigInjected->ExternalTrigInjecConvEdge
  1752. );
  1753. }
  1754. else
  1755. {
  1756. tmp_JSQR_ContextQueueBeingBuilt = ((sConfigInjected->InjectedNbrOfConversion - 1U));
  1757. }
  1758. }
  1759. /* 2. Continue setting of context under definition with parameter */
  1760. /* related to each channel: channel rank sequence */
  1761. /* Clear the old JSQx bits for the selected rank */
  1762. tmp_JSQR_ContextQueueBeingBuilt &= ~ADC_JSQR_RK(ADC_SQR3_SQ10, sConfigInjected->InjectedRank);
  1763. /* Set the JSQx bits for the selected rank */
  1764. tmp_JSQR_ContextQueueBeingBuilt |= ADC_JSQR_RK(sConfigInjected->InjectedChannel, sConfigInjected->InjectedRank);
  1765. /* Decrease channel count */
  1766. hadc->InjectionConfig.ChannelCount--;
  1767. /* 3. tmp_JSQR_ContextQueueBeingBuilt is fully built for this HAL_ADCEx_InjectedConfigChannel()
  1768. call, aggregate the setting to those already built during the previous
  1769. HAL_ADCEx_InjectedConfigChannel() calls (for the same context of course) */
  1770. hadc->InjectionConfig.ContextQueue |= tmp_JSQR_ContextQueueBeingBuilt;
  1771. /* 4. End of context setting: if this is the last channel set, then write context
  1772. into register JSQR and make it enter into queue */
  1773. if (hadc->InjectionConfig.ChannelCount == 0U)
  1774. {
  1775. MODIFY_REG(hadc->Instance->JSQR, ADC_JSQR_FIELDS, hadc->InjectionConfig.ContextQueue);
  1776. }
  1777. }
  1778. /* Parameters update conditioned to ADC state: */
  1779. /* Parameters that can be updated when ADC is disabled or enabled without */
  1780. /* conversion on going on injected group: */
  1781. /* - Injected context queue: Queue disable (active context is kept) or */
  1782. /* enable (context decremented, up to 2 contexts queued) */
  1783. /* - Injected discontinuous mode: can be enabled only if auto-injected */
  1784. /* mode is disabled. */
  1785. if (LL_ADC_INJ_IsConversionOngoing(hadc->Instance) == 0UL)
  1786. {
  1787. if (!(__LL_ADC_IS_CHANNEL_INTERNAL(sConfigInjected->InjectedChannel)))
  1788. {
  1789. #if defined(ADC_VER_V5_V90)
  1790. if (hadc->Instance != ADC3)
  1791. {
  1792. /* ADC channels preselection */
  1793. hadc->Instance->PCSEL_RES0 |= (1UL << (__LL_ADC_CHANNEL_TO_DECIMAL_NB(sConfigInjected->InjectedChannel) & 0x1FUL));
  1794. }
  1795. #else
  1796. /* ADC channels preselection */
  1797. hadc->Instance->PCSEL |= (1UL << (__LL_ADC_CHANNEL_TO_DECIMAL_NB(sConfigInjected->InjectedChannel) & 0x1FUL));
  1798. #endif /* ADC_VER_V5_V90 */
  1799. }
  1800. /* If auto-injected mode is disabled: no constraint */
  1801. if (sConfigInjected->AutoInjectedConv == DISABLE)
  1802. {
  1803. MODIFY_REG(hadc->Instance->CFGR,
  1804. ADC_CFGR_JQM | ADC_CFGR_JDISCEN,
  1805. ADC_CFGR_INJECT_CONTEXT_QUEUE((uint32_t)sConfigInjected->QueueInjectedContext) |
  1806. ADC_CFGR_INJECT_DISCCONTINUOUS((uint32_t)sConfigInjected->InjectedDiscontinuousConvMode));
  1807. }
  1808. /* If auto-injected mode is enabled: Injected discontinuous setting is */
  1809. /* discarded. */
  1810. else
  1811. {
  1812. MODIFY_REG(hadc->Instance->CFGR,
  1813. ADC_CFGR_JQM | ADC_CFGR_JDISCEN,
  1814. ADC_CFGR_INJECT_CONTEXT_QUEUE((uint32_t)sConfigInjected->QueueInjectedContext));
  1815. }
  1816. }
  1817. /* Parameters update conditioned to ADC state: */
  1818. /* Parameters that can be updated when ADC is disabled or enabled without */
  1819. /* conversion on going on regular and injected groups: */
  1820. /* - Automatic injected conversion: can be enabled if injected group */
  1821. /* external triggers are disabled. */
  1822. /* - Channel sampling time */
  1823. /* - Channel offset */
  1824. tmp_adc_is_conversion_on_going_regular = LL_ADC_REG_IsConversionOngoing(hadc->Instance);
  1825. tmp_adc_is_conversion_on_going_injected = LL_ADC_INJ_IsConversionOngoing(hadc->Instance);
  1826. if ((tmp_adc_is_conversion_on_going_regular == 0UL)
  1827. && (tmp_adc_is_conversion_on_going_injected == 0UL)
  1828. )
  1829. {
  1830. /* If injected group external triggers are disabled (set to injected */
  1831. /* software start): no constraint */
  1832. if ((sConfigInjected->ExternalTrigInjecConv == ADC_INJECTED_SOFTWARE_START)
  1833. || (sConfigInjected->ExternalTrigInjecConvEdge == ADC_EXTERNALTRIGINJECCONV_EDGE_NONE))
  1834. {
  1835. if (sConfigInjected->AutoInjectedConv == ENABLE)
  1836. {
  1837. SET_BIT(hadc->Instance->CFGR, ADC_CFGR_JAUTO);
  1838. }
  1839. else
  1840. {
  1841. CLEAR_BIT(hadc->Instance->CFGR, ADC_CFGR_JAUTO);
  1842. }
  1843. }
  1844. /* If Automatic injected conversion was intended to be set and could not */
  1845. /* due to injected group external triggers enabled, error is reported. */
  1846. else
  1847. {
  1848. if (sConfigInjected->AutoInjectedConv == ENABLE)
  1849. {
  1850. /* Update ADC state machine to error */
  1851. SET_BIT(hadc->State, HAL_ADC_STATE_ERROR_CONFIG);
  1852. tmp_hal_status = HAL_ERROR;
  1853. }
  1854. else
  1855. {
  1856. CLEAR_BIT(hadc->Instance->CFGR, ADC_CFGR_JAUTO);
  1857. }
  1858. }
  1859. if (sConfigInjected->InjecOversamplingMode == ENABLE)
  1860. {
  1861. #if defined(ADC_VER_V5_V90)
  1862. if (hadc->Instance == ADC3)
  1863. {
  1864. assert_param(IS_ADC_OVERSAMPLING_RATIO_ADC3(sConfigInjected->InjecOversampling.Ratio));
  1865. }
  1866. else
  1867. {
  1868. assert_param(IS_ADC_OVERSAMPLING_RATIO(sConfigInjected->InjecOversampling.Ratio));
  1869. }
  1870. #else
  1871. assert_param(IS_ADC_OVERSAMPLING_RATIO(sConfigInjected->InjecOversampling.Ratio));
  1872. #endif
  1873. assert_param(IS_ADC_RIGHT_BIT_SHIFT(sConfigInjected->InjecOversampling.RightBitShift));
  1874. /* JOVSE must be reset in case of triggered regular mode */
  1875. assert_param(!(READ_BIT(hadc->Instance->CFGR2, ADC_CFGR2_ROVSE | ADC_CFGR2_TROVS) == (ADC_CFGR2_ROVSE | ADC_CFGR2_TROVS)));
  1876. /* Configuration of Injected Oversampler: */
  1877. /* - Oversampling Ratio */
  1878. /* - Right bit shift */
  1879. /* Enable OverSampling mode */
  1880. #if defined(ADC_VER_V5_V90)
  1881. if (hadc->Instance != ADC3)
  1882. {
  1883. MODIFY_REG(hadc->Instance->CFGR2,
  1884. ADC_CFGR2_JOVSE |
  1885. ADC_CFGR2_OVSR |
  1886. ADC_CFGR2_OVSS,
  1887. ADC_CFGR2_JOVSE |
  1888. ((sConfigInjected->InjecOversampling.Ratio - 1UL) << ADC_CFGR2_OVSR_Pos) |
  1889. sConfigInjected->InjecOversampling.RightBitShift
  1890. );
  1891. }
  1892. else
  1893. {
  1894. MODIFY_REG(hadc->Instance->CFGR2,
  1895. ADC_CFGR2_JOVSE |
  1896. ADC3_CFGR2_OVSR |
  1897. ADC_CFGR2_OVSS,
  1898. ADC_CFGR2_JOVSE |
  1899. (sConfigInjected->InjecOversampling.Ratio) |
  1900. sConfigInjected->InjecOversampling.RightBitShift
  1901. );
  1902. }
  1903. #else
  1904. MODIFY_REG(hadc->Instance->CFGR2,
  1905. ADC_CFGR2_JOVSE |
  1906. ADC_CFGR2_OVSR |
  1907. ADC_CFGR2_OVSS,
  1908. ADC_CFGR2_JOVSE |
  1909. ((sConfigInjected->InjecOversampling.Ratio - 1UL) << ADC_CFGR2_OVSR_Pos) |
  1910. sConfigInjected->InjecOversampling.RightBitShift
  1911. );
  1912. #endif
  1913. }
  1914. else
  1915. {
  1916. /* Disable Regular OverSampling */
  1917. CLEAR_BIT(hadc->Instance->CFGR2, ADC_CFGR2_JOVSE);
  1918. }
  1919. /* Set sampling time of the selected ADC channel */
  1920. LL_ADC_SetChannelSamplingTime(hadc->Instance, sConfigInjected->InjectedChannel, sConfigInjected->InjectedSamplingTime);
  1921. /* Configure the offset: offset enable/disable, channel, offset value */
  1922. /* Shift the offset with respect to the selected ADC resolution. */
  1923. /* Offset has to be left-aligned on bit 11, the LSB (right bits) are set to 0 */
  1924. #if defined(ADC_VER_V5_V90)
  1925. if (hadc->Instance == ADC3)
  1926. {
  1927. tmpOffsetShifted = ADC3_OFFSET_SHIFT_RESOLUTION(hadc, sConfigInjected->InjectedOffset);
  1928. }
  1929. else
  1930. #endif /* ADC_VER_V5_V90 */
  1931. {
  1932. tmpOffsetShifted = ADC_OFFSET_SHIFT_RESOLUTION(hadc, sConfigInjected->InjectedOffset);
  1933. }
  1934. if (sConfigInjected->InjectedOffsetNumber != ADC_OFFSET_NONE)
  1935. {
  1936. /* Set ADC selected offset number */
  1937. LL_ADC_SetOffset(hadc->Instance, sConfigInjected->InjectedOffsetNumber, sConfigInjected->InjectedChannel, tmpOffsetShifted);
  1938. #if defined(ADC_VER_V5_V90)
  1939. if (hadc->Instance == ADC3)
  1940. {
  1941. /* Set ADC selected offset sign & saturation */
  1942. LL_ADC_SetOffsetSign(hadc->Instance, sConfigInjected->InjectedOffsetNumber, sConfigInjected->InjectedOffsetSign);
  1943. LL_ADC_SetOffsetSaturation(hadc->Instance, sConfigInjected->InjectedOffsetNumber, (sConfigInjected->InjectedOffsetSaturation == ENABLE) ? LL_ADC_OFFSET_SATURATION_ENABLE : LL_ADC_OFFSET_SATURATION_DISABLE);
  1944. }
  1945. else
  1946. #endif /* ADC_VER_V5_V90 */
  1947. {
  1948. /* Set ADC selected offset signed saturation */
  1949. LL_ADC_SetOffsetSignedSaturation(hadc->Instance, sConfigInjected->InjectedOffsetNumber, (sConfigInjected->InjectedOffsetSignedSaturation == ENABLE) ? LL_ADC_OFFSET_SIGNED_SATURATION_ENABLE : LL_ADC_OFFSET_SIGNED_SATURATION_DISABLE);
  1950. }
  1951. }
  1952. else
  1953. {
  1954. #if defined(ADC_VER_V5_V90)
  1955. if (hadc->Instance == ADC3)
  1956. {
  1957. /* Scan each offset register to check if the selected channel is targeted. */
  1958. /* If this is the case, the corresponding offset number is disabled. */
  1959. if (__LL_ADC_CHANNEL_TO_DECIMAL_NB(LL_ADC_GetOffsetChannel(hadc->Instance, LL_ADC_OFFSET_1)) == __LL_ADC_CHANNEL_TO_DECIMAL_NB(sConfigInjected->InjectedChannel))
  1960. {
  1961. LL_ADC_SetOffsetState(hadc->Instance, LL_ADC_OFFSET_1, LL_ADC_OFFSET_DISABLE);
  1962. }
  1963. if (__LL_ADC_CHANNEL_TO_DECIMAL_NB(LL_ADC_GetOffsetChannel(hadc->Instance, LL_ADC_OFFSET_2)) == __LL_ADC_CHANNEL_TO_DECIMAL_NB(sConfigInjected->InjectedChannel))
  1964. {
  1965. LL_ADC_SetOffsetState(hadc->Instance, LL_ADC_OFFSET_2, LL_ADC_OFFSET_DISABLE);
  1966. }
  1967. if (__LL_ADC_CHANNEL_TO_DECIMAL_NB(LL_ADC_GetOffsetChannel(hadc->Instance, LL_ADC_OFFSET_3)) == __LL_ADC_CHANNEL_TO_DECIMAL_NB(sConfigInjected->InjectedChannel))
  1968. {
  1969. LL_ADC_SetOffsetState(hadc->Instance, LL_ADC_OFFSET_3, LL_ADC_OFFSET_DISABLE);
  1970. }
  1971. if (__LL_ADC_CHANNEL_TO_DECIMAL_NB(LL_ADC_GetOffsetChannel(hadc->Instance, LL_ADC_OFFSET_4)) == __LL_ADC_CHANNEL_TO_DECIMAL_NB(sConfigInjected->InjectedChannel))
  1972. {
  1973. LL_ADC_SetOffsetState(hadc->Instance, LL_ADC_OFFSET_4, LL_ADC_OFFSET_DISABLE);
  1974. }
  1975. }
  1976. else
  1977. #endif /* ADC_VER_V5_V90 */
  1978. {
  1979. /* Scan each offset register to check if the selected channel is targeted. */
  1980. /* If this is the case, the corresponding offset number is disabled. */
  1981. if (__LL_ADC_CHANNEL_TO_DECIMAL_NB(LL_ADC_GetOffsetChannel(hadc->Instance, LL_ADC_OFFSET_1)) == __LL_ADC_CHANNEL_TO_DECIMAL_NB(sConfigInjected->InjectedChannel))
  1982. {
  1983. LL_ADC_SetOffset(hadc->Instance, LL_ADC_OFFSET_1, sConfigInjected->InjectedChannel, LL_ADC_OFFSET_SIGNED_SATURATION_DISABLE);
  1984. }
  1985. if (__LL_ADC_CHANNEL_TO_DECIMAL_NB(LL_ADC_GetOffsetChannel(hadc->Instance, LL_ADC_OFFSET_2)) == __LL_ADC_CHANNEL_TO_DECIMAL_NB(sConfigInjected->InjectedChannel))
  1986. {
  1987. LL_ADC_SetOffset(hadc->Instance, LL_ADC_OFFSET_2, sConfigInjected->InjectedChannel, LL_ADC_OFFSET_SIGNED_SATURATION_DISABLE);
  1988. }
  1989. if (__LL_ADC_CHANNEL_TO_DECIMAL_NB(LL_ADC_GetOffsetChannel(hadc->Instance, LL_ADC_OFFSET_3)) == __LL_ADC_CHANNEL_TO_DECIMAL_NB(sConfigInjected->InjectedChannel))
  1990. {
  1991. LL_ADC_SetOffset(hadc->Instance, LL_ADC_OFFSET_4, sConfigInjected->InjectedChannel, LL_ADC_OFFSET_SIGNED_SATURATION_DISABLE);
  1992. }
  1993. if (__LL_ADC_CHANNEL_TO_DECIMAL_NB(LL_ADC_GetOffsetChannel(hadc->Instance, LL_ADC_OFFSET_4)) == __LL_ADC_CHANNEL_TO_DECIMAL_NB(sConfigInjected->InjectedChannel))
  1994. {
  1995. LL_ADC_SetOffset(hadc->Instance, LL_ADC_OFFSET_4, sConfigInjected->InjectedChannel, LL_ADC_OFFSET_SIGNED_SATURATION_DISABLE);
  1996. }
  1997. }
  1998. }
  1999. }
  2000. /* Parameters update conditioned to ADC state: */
  2001. /* Parameters that can be updated only when ADC is disabled: */
  2002. /* - Single or differential mode */
  2003. /* - Internal measurement channels: Vbat/VrefInt/TempSensor */
  2004. if (LL_ADC_IsEnabled(hadc->Instance) == 0UL)
  2005. {
  2006. /* Set mode single-ended or differential input of the selected ADC channel */
  2007. LL_ADC_SetChannelSingleDiff(hadc->Instance, sConfigInjected->InjectedChannel, sConfigInjected->InjectedSingleDiff);
  2008. /* Configuration of differential mode */
  2009. /* Note: ADC channel number masked with value "0x1F" to ensure shift value within 32 bits range */
  2010. if (sConfigInjected->InjectedSingleDiff == ADC_DIFFERENTIAL_ENDED)
  2011. {
  2012. /* Set sampling time of the selected ADC channel */
  2013. LL_ADC_SetChannelSamplingTime(hadc->Instance, (uint32_t)(__LL_ADC_DECIMAL_NB_TO_CHANNEL((__LL_ADC_CHANNEL_TO_DECIMAL_NB((uint32_t)sConfigInjected->InjectedChannel) + 1UL) & 0x1FUL)), sConfigInjected->InjectedSamplingTime);
  2014. }
  2015. /* Management of internal measurement channels: Vbat/VrefInt/TempSensor */
  2016. /* internal measurement paths enable: If internal channel selected, */
  2017. /* enable dedicated internal buffers and path. */
  2018. /* Note: these internal measurement paths can be disabled using */
  2019. /* HAL_ADC_DeInit(). */
  2020. if (__LL_ADC_IS_CHANNEL_INTERNAL(sConfigInjected->InjectedChannel))
  2021. {
  2022. /* Configuration of common ADC parameters (continuation) */
  2023. /* Software is allowed to change common parameters only when all ADCs */
  2024. /* of the common group are disabled. */
  2025. if (__LL_ADC_IS_ENABLED_ALL_COMMON_INSTANCE(__LL_ADC_COMMON_INSTANCE(hadc->Instance)) == 0UL)
  2026. {
  2027. tmp_config_internal_channel = LL_ADC_GetCommonPathInternalCh(__LL_ADC_COMMON_INSTANCE(hadc->Instance));
  2028. /* If the requested internal measurement path has already been enabled, */
  2029. /* bypass the configuration processing. */
  2030. if ((sConfigInjected->InjectedChannel == ADC_CHANNEL_TEMPSENSOR) && ((tmp_config_internal_channel & LL_ADC_PATH_INTERNAL_TEMPSENSOR) == 0UL))
  2031. {
  2032. if (ADC_TEMPERATURE_SENSOR_INSTANCE(hadc))
  2033. {
  2034. LL_ADC_SetCommonPathInternalCh(__LL_ADC_COMMON_INSTANCE(hadc->Instance), LL_ADC_PATH_INTERNAL_TEMPSENSOR | tmp_config_internal_channel);
  2035. /* Delay for temperature sensor stabilization time */
  2036. /* Wait loop initialization and execution */
  2037. /* Note: Variable divided by 2 to compensate partially */
  2038. /* CPU processing cycles, scaling in us split to not */
  2039. /* exceed 32 bits register capacity and handle low frequency. */
  2040. wait_loop_index = ((LL_ADC_DELAY_TEMPSENSOR_STAB_US / 10UL) * ((SystemCoreClock / (100000UL * 2UL)) + 1UL));
  2041. while (wait_loop_index != 0UL)
  2042. {
  2043. wait_loop_index--;
  2044. }
  2045. }
  2046. }
  2047. else if ((sConfigInjected->InjectedChannel == ADC_CHANNEL_VBAT) && ((tmp_config_internal_channel & LL_ADC_PATH_INTERNAL_VBAT) == 0UL))
  2048. {
  2049. if (ADC_BATTERY_VOLTAGE_INSTANCE(hadc))
  2050. {
  2051. LL_ADC_SetCommonPathInternalCh(__LL_ADC_COMMON_INSTANCE(hadc->Instance), LL_ADC_PATH_INTERNAL_VBAT | tmp_config_internal_channel);
  2052. }
  2053. }
  2054. else if ((sConfigInjected->InjectedChannel == ADC_CHANNEL_VREFINT) && ((tmp_config_internal_channel & LL_ADC_PATH_INTERNAL_VREFINT) == 0UL))
  2055. {
  2056. if (ADC_VREFINT_INSTANCE(hadc))
  2057. {
  2058. LL_ADC_SetCommonPathInternalCh(__LL_ADC_COMMON_INSTANCE(hadc->Instance), LL_ADC_PATH_INTERNAL_VREFINT | tmp_config_internal_channel);
  2059. }
  2060. }
  2061. else
  2062. {
  2063. /* nothing to do */
  2064. }
  2065. }
  2066. /* If the requested internal measurement path has already been enabled */
  2067. /* and other ADC of the common group are enabled, internal */
  2068. /* measurement paths cannot be enabled. */
  2069. else
  2070. {
  2071. /* Update ADC state machine to error */
  2072. SET_BIT(hadc->State, HAL_ADC_STATE_ERROR_CONFIG);
  2073. tmp_hal_status = HAL_ERROR;
  2074. }
  2075. }
  2076. }
  2077. /* Process unlocked */
  2078. __HAL_UNLOCK(hadc);
  2079. /* Return function status */
  2080. return tmp_hal_status;
  2081. }
  2082. /**
  2083. * @brief Enable ADC multimode and configure multimode parameters
  2084. * @note Possibility to update parameters on the fly:
  2085. * This function initializes multimode parameters, following
  2086. * calls to this function can be used to reconfigure some parameters
  2087. * of structure "ADC_MultiModeTypeDef" on the fly, without resetting
  2088. * the ADCs.
  2089. * The setting of these parameters is conditioned to ADC state.
  2090. * For parameters constraints, see comments of structure
  2091. * "ADC_MultiModeTypeDef".
  2092. * @note To move back configuration from multimode to single mode, ADC must
  2093. * be reset (using function HAL_ADC_Init() ).
  2094. * @param hadc Master ADC handle
  2095. * @param multimode Structure of ADC multimode configuration
  2096. * @retval HAL status
  2097. */
  2098. HAL_StatusTypeDef HAL_ADCEx_MultiModeConfigChannel(ADC_HandleTypeDef *hadc, ADC_MultiModeTypeDef *multimode)
  2099. {
  2100. HAL_StatusTypeDef tmp_hal_status = HAL_OK;
  2101. ADC_Common_TypeDef *tmpADC_Common;
  2102. ADC_HandleTypeDef tmphadcSlave;
  2103. uint32_t tmphadcSlave_conversion_on_going;
  2104. /* Check the parameters */
  2105. assert_param(IS_ADC_MULTIMODE_MASTER_INSTANCE(hadc->Instance));
  2106. assert_param(IS_ADC_MULTIMODE(multimode->Mode));
  2107. if (multimode->Mode != ADC_MODE_INDEPENDENT)
  2108. {
  2109. assert_param(IS_ADC_DUAL_DATA_MODE(multimode->DualModeData));
  2110. assert_param(IS_ADC_SAMPLING_DELAY(multimode->TwoSamplingDelay));
  2111. }
  2112. /* Process locked */
  2113. __HAL_LOCK(hadc);
  2114. tmphadcSlave.State = HAL_ADC_STATE_RESET;
  2115. tmphadcSlave.ErrorCode = HAL_ADC_ERROR_NONE;
  2116. ADC_MULTI_SLAVE(hadc, &tmphadcSlave);
  2117. if (tmphadcSlave.Instance == NULL)
  2118. {
  2119. /* Update ADC state machine to error */
  2120. SET_BIT(hadc->State, HAL_ADC_STATE_ERROR_CONFIG);
  2121. /* Process unlocked */
  2122. __HAL_UNLOCK(hadc);
  2123. return HAL_ERROR;
  2124. }
  2125. /* Parameters update conditioned to ADC state: */
  2126. /* Parameters that can be updated when ADC is disabled or enabled without */
  2127. /* conversion on going on regular group: */
  2128. /* - Multimode DATA Format configuration */
  2129. tmphadcSlave_conversion_on_going = LL_ADC_REG_IsConversionOngoing((&tmphadcSlave)->Instance);
  2130. if ((LL_ADC_REG_IsConversionOngoing(hadc->Instance) == 0UL)
  2131. && (tmphadcSlave_conversion_on_going == 0UL))
  2132. {
  2133. /* Pointer to the common control register */
  2134. tmpADC_Common = __LL_ADC_COMMON_INSTANCE(hadc->Instance);
  2135. /* If multimode is selected, configure all multimode parameters. */
  2136. /* Otherwise, reset multimode parameters (can be used in case of */
  2137. /* transition from multimode to independent mode). */
  2138. if (multimode->Mode != ADC_MODE_INDEPENDENT)
  2139. {
  2140. MODIFY_REG(tmpADC_Common->CCR, ADC_CCR_DAMDF, multimode->DualModeData);
  2141. /* Parameters that can be updated only when ADC is disabled: */
  2142. /* - Multimode mode selection */
  2143. /* - Multimode delay */
  2144. /* Note: Delay range depends on selected resolution: */
  2145. /* from 1 to 9 clock cycles for 16 bits */
  2146. /* from 1 to 9 clock cycles for 14 bits, */
  2147. /* from 1 to 8 clock cycles for 12 bits */
  2148. /* from 1 to 6 clock cycles for 10 and 8 bits */
  2149. /* If a higher delay is selected, it will be clipped to maximum delay */
  2150. /* range */
  2151. if (__LL_ADC_IS_ENABLED_ALL_COMMON_INSTANCE(__LL_ADC_COMMON_INSTANCE(hadc->Instance)) == 0UL)
  2152. {
  2153. MODIFY_REG(tmpADC_Common->CCR,
  2154. ADC_CCR_DUAL |
  2155. ADC_CCR_DELAY,
  2156. multimode->Mode |
  2157. multimode->TwoSamplingDelay
  2158. );
  2159. }
  2160. }
  2161. else /* ADC_MODE_INDEPENDENT */
  2162. {
  2163. CLEAR_BIT(tmpADC_Common->CCR, ADC_CCR_DAMDF);
  2164. /* Parameters that can be updated only when ADC is disabled: */
  2165. /* - Multimode mode selection */
  2166. /* - Multimode delay */
  2167. if (__LL_ADC_IS_ENABLED_ALL_COMMON_INSTANCE(__LL_ADC_COMMON_INSTANCE(hadc->Instance)) == 0UL)
  2168. {
  2169. CLEAR_BIT(tmpADC_Common->CCR, ADC_CCR_DUAL | ADC_CCR_DELAY);
  2170. }
  2171. }
  2172. }
  2173. /* If one of the ADC sharing the same common group is enabled, no update */
  2174. /* could be done on neither of the multimode structure parameters. */
  2175. else
  2176. {
  2177. /* Update ADC state machine to error */
  2178. SET_BIT(hadc->State, HAL_ADC_STATE_ERROR_CONFIG);
  2179. tmp_hal_status = HAL_ERROR;
  2180. }
  2181. /* Process unlocked */
  2182. __HAL_UNLOCK(hadc);
  2183. /* Return function status */
  2184. return tmp_hal_status;
  2185. }
  2186. /**
  2187. * @brief Enable Injected Queue
  2188. * @note This function resets CFGR register JQDIS bit in order to enable the
  2189. * Injected Queue. JQDIS can be written only when ADSTART and JDSTART
  2190. * are both equal to 0 to ensure that no regular nor injected
  2191. * conversion is ongoing.
  2192. * @param hadc ADC handle
  2193. * @retval HAL status
  2194. */
  2195. HAL_StatusTypeDef HAL_ADCEx_EnableInjectedQueue(ADC_HandleTypeDef *hadc)
  2196. {
  2197. HAL_StatusTypeDef tmp_hal_status;
  2198. uint32_t tmp_adc_is_conversion_on_going_regular;
  2199. uint32_t tmp_adc_is_conversion_on_going_injected;
  2200. /* Check the parameters */
  2201. assert_param(IS_ADC_ALL_INSTANCE(hadc->Instance));
  2202. tmp_adc_is_conversion_on_going_regular = LL_ADC_REG_IsConversionOngoing(hadc->Instance);
  2203. tmp_adc_is_conversion_on_going_injected = LL_ADC_INJ_IsConversionOngoing(hadc->Instance);
  2204. /* Parameter can be set only if no conversion is on-going */
  2205. if ((tmp_adc_is_conversion_on_going_regular == 0UL)
  2206. && (tmp_adc_is_conversion_on_going_injected == 0UL)
  2207. )
  2208. {
  2209. CLEAR_BIT(hadc->Instance->CFGR, ADC_CFGR_JQDIS);
  2210. /* Update state, clear previous result related to injected queue overflow */
  2211. CLEAR_BIT(hadc->State, HAL_ADC_STATE_INJ_JQOVF);
  2212. tmp_hal_status = HAL_OK;
  2213. }
  2214. else
  2215. {
  2216. tmp_hal_status = HAL_ERROR;
  2217. }
  2218. return tmp_hal_status;
  2219. }
  2220. /**
  2221. * @brief Disable Injected Queue
  2222. * @note This function sets CFGR register JQDIS bit in order to disable the
  2223. * Injected Queue. JQDIS can be written only when ADSTART and JDSTART
  2224. * are both equal to 0 to ensure that no regular nor injected
  2225. * conversion is ongoing.
  2226. * @param hadc ADC handle
  2227. * @retval HAL status
  2228. */
  2229. HAL_StatusTypeDef HAL_ADCEx_DisableInjectedQueue(ADC_HandleTypeDef *hadc)
  2230. {
  2231. HAL_StatusTypeDef tmp_hal_status;
  2232. uint32_t tmp_adc_is_conversion_on_going_regular;
  2233. uint32_t tmp_adc_is_conversion_on_going_injected;
  2234. /* Check the parameters */
  2235. assert_param(IS_ADC_ALL_INSTANCE(hadc->Instance));
  2236. tmp_adc_is_conversion_on_going_regular = LL_ADC_REG_IsConversionOngoing(hadc->Instance);
  2237. tmp_adc_is_conversion_on_going_injected = LL_ADC_INJ_IsConversionOngoing(hadc->Instance);
  2238. /* Parameter can be set only if no conversion is on-going */
  2239. if ((tmp_adc_is_conversion_on_going_regular == 0UL)
  2240. && (tmp_adc_is_conversion_on_going_injected == 0UL)
  2241. )
  2242. {
  2243. LL_ADC_INJ_SetQueueMode(hadc->Instance, LL_ADC_INJ_QUEUE_DISABLE);
  2244. tmp_hal_status = HAL_OK;
  2245. }
  2246. else
  2247. {
  2248. tmp_hal_status = HAL_ERROR;
  2249. }
  2250. return tmp_hal_status;
  2251. }
  2252. /**
  2253. * @brief Disable ADC voltage regulator.
  2254. * @note Disabling voltage regulator allows to save power. This operation can
  2255. * be carried out only when ADC is disabled.
  2256. * @note To enable again the voltage regulator, the user is expected to
  2257. * resort to HAL_ADC_Init() API.
  2258. * @param hadc ADC handle
  2259. * @retval HAL status
  2260. */
  2261. HAL_StatusTypeDef HAL_ADCEx_DisableVoltageRegulator(ADC_HandleTypeDef *hadc)
  2262. {
  2263. HAL_StatusTypeDef tmp_hal_status;
  2264. /* Check the parameters */
  2265. assert_param(IS_ADC_ALL_INSTANCE(hadc->Instance));
  2266. /* Setting of this feature is conditioned to ADC state: ADC must be ADC disabled */
  2267. if (LL_ADC_IsEnabled(hadc->Instance) == 0UL)
  2268. {
  2269. LL_ADC_DisableInternalRegulator(hadc->Instance);
  2270. tmp_hal_status = HAL_OK;
  2271. }
  2272. else
  2273. {
  2274. tmp_hal_status = HAL_ERROR;
  2275. }
  2276. return tmp_hal_status;
  2277. }
  2278. /**
  2279. * @brief Enter ADC deep-power-down mode
  2280. * @note This mode is achieved in setting DEEPPWD bit and allows to save power
  2281. * in reducing leakage currents. It is particularly interesting before
  2282. * entering stop modes.
  2283. * @note Setting DEEPPWD automatically clears ADVREGEN bit and disables the
  2284. * ADC voltage regulator. This means that this API encompasses
  2285. * HAL_ADCEx_DisableVoltageRegulator(). Additionally, the internal
  2286. * calibration is lost.
  2287. * @note To exit the ADC deep-power-down mode, the user is expected to
  2288. * resort to HAL_ADC_Init() API as well as to relaunch a calibration
  2289. * with HAL_ADCEx_Calibration_Start() API or to re-apply a previously
  2290. * saved calibration factor.
  2291. * @param hadc ADC handle
  2292. * @retval HAL status
  2293. */
  2294. HAL_StatusTypeDef HAL_ADCEx_EnterADCDeepPowerDownMode(ADC_HandleTypeDef *hadc)
  2295. {
  2296. HAL_StatusTypeDef tmp_hal_status;
  2297. /* Check the parameters */
  2298. assert_param(IS_ADC_ALL_INSTANCE(hadc->Instance));
  2299. /* Setting of this feature is conditioned to ADC state: ADC must be ADC disabled */
  2300. if (LL_ADC_IsEnabled(hadc->Instance) == 0UL)
  2301. {
  2302. LL_ADC_EnableDeepPowerDown(hadc->Instance);
  2303. tmp_hal_status = HAL_OK;
  2304. }
  2305. else
  2306. {
  2307. tmp_hal_status = HAL_ERROR;
  2308. }
  2309. return tmp_hal_status;
  2310. }
  2311. /**
  2312. * @}
  2313. */
  2314. /**
  2315. * @}
  2316. */
  2317. #endif /* HAL_ADC_MODULE_ENABLED */
  2318. /**
  2319. * @}
  2320. */
  2321. /**
  2322. * @}
  2323. */