stm32l4xx_hal_adc_ex.c 90 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251
  1. /**
  2. ******************************************************************************
  3. * @file stm32l4xx_hal_adc_ex.c
  4. * @author MCD Application Team
  5. * @brief This file provides firmware functions to manage the following
  6. * functionalities of the Analog to Digital Convertor (ADC)
  7. * peripheral:
  8. * + Operation functions
  9. * ++ Start, stop, get result of conversions of ADC group injected,
  10. * using 2 possible modes: polling, interruption.
  11. * ++ Calibration
  12. * +++ ADC automatic self-calibration
  13. * +++ Calibration factors get or set
  14. * ++ Multimode feature when available
  15. * + Control functions
  16. * ++ Channels configuration on ADC group injected
  17. * + State functions
  18. * ++ ADC group injected contexts queue management
  19. * Other functions (generic functions) are available in file
  20. * "stm32l4xx_hal_adc.c".
  21. *
  22. @verbatim
  23. [..]
  24. (@) Sections "ADC peripheral features" and "How to use this driver" are
  25. available in file of generic functions "stm32l4xx_hal_adc.c".
  26. [..]
  27. @endverbatim
  28. ******************************************************************************
  29. * @attention
  30. *
  31. * <h2><center>&copy; COPYRIGHT(c) 2017 STMicroelectronics</center></h2>
  32. *
  33. * Redistribution and use in source and binary forms, with or without modification,
  34. * are permitted provided that the following conditions are met:
  35. * 1. Redistributions of source code must retain the above copyright notice,
  36. * this list of conditions and the following disclaimer.
  37. * 2. Redistributions in binary form must reproduce the above copyright notice,
  38. * this list of conditions and the following disclaimer in the documentation
  39. * and/or other materials provided with the distribution.
  40. * 3. Neither the name of STMicroelectronics nor the names of its contributors
  41. * may be used to endorse or promote products derived from this software
  42. * without specific prior written permission.
  43. *
  44. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  45. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  46. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  47. * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
  48. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  49. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  50. * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  51. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  52. * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  53. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  54. *
  55. ******************************************************************************
  56. */
  57. /* Includes ------------------------------------------------------------------*/
  58. #include "stm32l4xx_hal.h"
  59. /** @addtogroup STM32L4xx_HAL_Driver
  60. * @{
  61. */
  62. /** @defgroup ADCEx ADCEx
  63. * @brief ADC Extended HAL module driver
  64. * @{
  65. */
  66. #ifdef HAL_ADC_MODULE_ENABLED
  67. /* Private typedef -----------------------------------------------------------*/
  68. /* Private define ------------------------------------------------------------*/
  69. /** @defgroup ADCEx_Private_Constants ADC Extended Private Constants
  70. * @{
  71. */
  72. #define ADC_JSQR_FIELDS ((uint32_t)(ADC_JSQR_JL | ADC_JSQR_JEXTSEL | ADC_JSQR_JEXTEN |\
  73. ADC_JSQR_JSQ1 | ADC_JSQR_JSQ2 |\
  74. ADC_JSQR_JSQ3 | ADC_JSQR_JSQ4 )) /*!< ADC_JSQR fields of parameters that can be updated anytime
  75. once the ADC is enabled */
  76. /* Fixed timeout value for ADC calibration. */
  77. /* Values defined to be higher than worst cases: low clock frequency, */
  78. /* maximum prescalers. */
  79. /* Ex of profile low frequency : f_ADC at 0.14 MHz (minimum value */
  80. /* according to Data sheet), calibration_time MAX = 112 / f_ADC */
  81. /* 112 / 140,000 = 0.8 ms */
  82. /* At maximum CPU speed (80 MHz), this means */
  83. /* 0.8 ms * 80 MHz = 64000 CPU cycles */
  84. #define ADC_CALIBRATION_TIMEOUT (64000U) /*!< ADC calibration time-out value */
  85. /**
  86. * @}
  87. */
  88. /* Private macro -------------------------------------------------------------*/
  89. /* Private variables ---------------------------------------------------------*/
  90. /* Private function prototypes -----------------------------------------------*/
  91. /* Exported functions --------------------------------------------------------*/
  92. /** @defgroup ADCEx_Exported_Functions ADC Extended Exported Functions
  93. * @{
  94. */
  95. /** @defgroup ADCEx_Exported_Functions_Group1 Extended Input and Output operation functions
  96. * @brief Extended IO operation functions
  97. *
  98. @verbatim
  99. ===============================================================================
  100. ##### IO operation functions #####
  101. ===============================================================================
  102. [..] This section provides functions allowing to:
  103. (+) Perform the ADC self-calibration for single or differential ending.
  104. (+) Get calibration factors for single or differential ending.
  105. (+) Set calibration factors for single or differential ending.
  106. (+) Start conversion of ADC group injected.
  107. (+) Stop conversion of ADC group injected.
  108. (+) Poll for conversion complete on ADC group injected.
  109. (+) Get result of ADC group injected channel conversion.
  110. (+) Start conversion of ADC group injected and enable interruptions.
  111. (+) Stop conversion of ADC group injected and disable interruptions.
  112. (+) When multimode feature is available, start multimode and enable DMA transfer.
  113. (+) Stop multimode and disable ADC DMA transfer.
  114. (+) Get result of multimode conversion.
  115. @endverbatim
  116. * @{
  117. */
  118. /**
  119. * @brief Perform an ADC automatic self-calibration
  120. * Calibration prerequisite: ADC must be disabled (execute this
  121. * function before HAL_ADC_Start() or after HAL_ADC_Stop() ).
  122. * @param hadc ADC handle
  123. * @param SingleDiff Selection of single-ended or differential input
  124. * This parameter can be one of the following values:
  125. * @arg @ref ADC_SINGLE_ENDED Channel in mode input single ended
  126. * @arg @ref ADC_DIFFERENTIAL_ENDED Channel in mode input differential ended
  127. * @retval HAL status
  128. */
  129. HAL_StatusTypeDef HAL_ADCEx_Calibration_Start(ADC_HandleTypeDef* hadc, uint32_t SingleDiff)
  130. {
  131. HAL_StatusTypeDef tmp_hal_status = HAL_OK;
  132. uint32_t WaitLoopIndex = 0;
  133. /* Check the parameters */
  134. assert_param(IS_ADC_ALL_INSTANCE(hadc->Instance));
  135. assert_param(IS_ADC_SINGLE_DIFFERENTIAL(SingleDiff));
  136. /* Process locked */
  137. __HAL_LOCK(hadc);
  138. /* Calibration prerequisite: ADC must be disabled. */
  139. /* Disable the ADC (if not already disabled) */
  140. tmp_hal_status = ADC_Disable(hadc);
  141. /* Check if ADC is effectively disabled */
  142. if (tmp_hal_status == HAL_OK)
  143. {
  144. /* Set ADC state */
  145. ADC_STATE_CLR_SET(hadc->State,
  146. HAL_ADC_STATE_REG_BUSY | HAL_ADC_STATE_INJ_BUSY,
  147. HAL_ADC_STATE_BUSY_INTERNAL);
  148. /* Select calibration mode single ended or differential ended */
  149. MODIFY_REG(hadc->Instance->CR, ADC_CR_ADCALDIF, SingleDiff);
  150. /* Start ADC calibration */
  151. SET_BIT(hadc->Instance->CR, ADC_CR_ADCAL);
  152. /* Wait for calibration completion */
  153. while(HAL_IS_BIT_SET(hadc->Instance->CR, ADC_CR_ADCAL))
  154. {
  155. WaitLoopIndex++;
  156. if (WaitLoopIndex >= ADC_CALIBRATION_TIMEOUT)
  157. {
  158. /* Update ADC state machine to error */
  159. ADC_STATE_CLR_SET(hadc->State,
  160. HAL_ADC_STATE_BUSY_INTERNAL,
  161. HAL_ADC_STATE_ERROR_INTERNAL);
  162. /* Process unlocked */
  163. __HAL_UNLOCK(hadc);
  164. return HAL_ERROR;
  165. }
  166. }
  167. /* Set ADC state */
  168. ADC_STATE_CLR_SET(hadc->State,
  169. HAL_ADC_STATE_BUSY_INTERNAL,
  170. HAL_ADC_STATE_READY);
  171. }
  172. else
  173. {
  174. SET_BIT(hadc->State, HAL_ADC_STATE_ERROR_INTERNAL);
  175. /* Note: No need to update variable "tmp_hal_status" here: already set */
  176. /* to state "HAL_ERROR" by function disabling the ADC. */
  177. }
  178. /* Process unlocked */
  179. __HAL_UNLOCK(hadc);
  180. /* Return function status */
  181. return tmp_hal_status;
  182. }
  183. /**
  184. * @brief Get the calibration factor.
  185. * @param hadc ADC handle.
  186. * @param SingleDiff This parameter can be only:
  187. * @arg @ref ADC_SINGLE_ENDED Channel in mode input single ended
  188. * @arg @ref ADC_DIFFERENTIAL_ENDED Channel in mode input differential ended
  189. * @retval Calibration value.
  190. */
  191. uint32_t HAL_ADCEx_Calibration_GetValue(ADC_HandleTypeDef* hadc, uint32_t SingleDiff)
  192. {
  193. /* Check the parameters */
  194. assert_param(IS_ADC_ALL_INSTANCE(hadc->Instance));
  195. assert_param(IS_ADC_SINGLE_DIFFERENTIAL(SingleDiff));
  196. /* Return the selected ADC calibration value */
  197. if (SingleDiff == ADC_DIFFERENTIAL_ENDED)
  198. {
  199. return ADC_CALFACT_DIFF_GET(hadc->Instance->CALFACT);
  200. }
  201. else
  202. {
  203. return ((hadc->Instance->CALFACT) & ADC_CALFACT_CALFACT_S);
  204. }
  205. }
  206. /**
  207. * @brief Set the calibration factor to overwrite automatic conversion result.
  208. * ADC must be enabled and no conversion is ongoing.
  209. * @param hadc ADC handle
  210. * @param SingleDiff This parameter can be only:
  211. * @arg @ref ADC_SINGLE_ENDED Channel in mode input single ended
  212. * @arg @ref ADC_DIFFERENTIAL_ENDED Channel in mode input differential ended
  213. * @param CalibrationFactor Calibration factor (coded on 7 bits maximum)
  214. * @retval HAL state
  215. */
  216. HAL_StatusTypeDef HAL_ADCEx_Calibration_SetValue(ADC_HandleTypeDef* hadc, uint32_t SingleDiff, uint32_t CalibrationFactor)
  217. {
  218. HAL_StatusTypeDef tmp_hal_status = HAL_OK;
  219. /* Check the parameters */
  220. assert_param(IS_ADC_ALL_INSTANCE(hadc->Instance));
  221. assert_param(IS_ADC_SINGLE_DIFFERENTIAL(SingleDiff));
  222. assert_param(IS_ADC_CALFACT(CalibrationFactor));
  223. /* Process locked */
  224. __HAL_LOCK(hadc);
  225. /* Verification of hardware constraints before modifying the calibration */
  226. /* factors register: ADC must be enabled, no conversion on going. */
  227. if ( (ADC_IS_ENABLE(hadc) != RESET) &&
  228. (ADC_IS_CONVERSION_ONGOING_REGULAR_INJECTED(hadc) == RESET) )
  229. {
  230. /* Set the selected ADC calibration value */
  231. if (SingleDiff == ADC_DIFFERENTIAL_ENDED)
  232. {
  233. MODIFY_REG(hadc->Instance->CALFACT, ADC_CALFACT_CALFACT_D, ADC_CALFACT_DIFF_SET(CalibrationFactor));
  234. }
  235. else
  236. {
  237. MODIFY_REG(hadc->Instance->CALFACT, ADC_CALFACT_CALFACT_S, CalibrationFactor);
  238. }
  239. }
  240. else
  241. {
  242. /* Update ADC state machine */
  243. SET_BIT(hadc->State, HAL_ADC_STATE_ERROR_CONFIG);
  244. /* Update ADC error code */
  245. SET_BIT(hadc->ErrorCode, HAL_ADC_ERROR_INTERNAL);
  246. /* Update ADC state machine to error */
  247. tmp_hal_status = HAL_ERROR;
  248. }
  249. /* Process unlocked */
  250. __HAL_UNLOCK(hadc);
  251. /* Return function status */
  252. return tmp_hal_status;
  253. }
  254. /**
  255. * @brief Enable ADC, start conversion of injected group.
  256. * @note Interruptions enabled in this function: None.
  257. * @note Case of multimode enabled when multimode feature is available:
  258. * HAL_ADCEx_InjectedStart() API must be called for ADC slave first,
  259. * then for ADC master.
  260. * For ADC slave, ADC is enabled only (conversion is not started).
  261. * For ADC master, ADC is enabled and multimode conversion is started.
  262. * @param hadc ADC handle.
  263. * @retval HAL status
  264. */
  265. HAL_StatusTypeDef HAL_ADCEx_InjectedStart(ADC_HandleTypeDef* hadc)
  266. {
  267. HAL_StatusTypeDef tmp_hal_status = HAL_OK;
  268. /* Check the parameters */
  269. assert_param(IS_ADC_ALL_INSTANCE(hadc->Instance));
  270. if (ADC_IS_CONVERSION_ONGOING_INJECTED(hadc))
  271. {
  272. return HAL_BUSY;
  273. }
  274. else
  275. {
  276. /* In case of software trigger detection enabled, JQDIS must be set
  277. (which can be done only if ADSTART and JADSTART are both cleared).
  278. If JQDIS is not set at that point, returns an error
  279. - since software trigger detection is disabled. User needs to
  280. resort to HAL_ADCEx_DisableInjectedQueue() API to set JQDIS.
  281. - or (if JQDIS is intentionally reset) since JEXTEN = 0 which means
  282. the queue is empty */
  283. if ((READ_BIT(hadc->Instance->JSQR, ADC_JSQR_JEXTEN) == RESET)
  284. && (READ_BIT(hadc->Instance->CFGR, ADC_CFGR_JQDIS) == RESET))
  285. {
  286. SET_BIT(hadc->State, HAL_ADC_STATE_ERROR_CONFIG);
  287. return HAL_ERROR;
  288. }
  289. /* Process locked */
  290. __HAL_LOCK(hadc);
  291. /* Enable the ADC peripheral */
  292. tmp_hal_status = ADC_Enable(hadc);
  293. /* Start conversion if ADC is effectively enabled */
  294. if (tmp_hal_status == HAL_OK)
  295. {
  296. /* Check if a regular conversion is ongoing */
  297. if (HAL_IS_BIT_SET(hadc->State, HAL_ADC_STATE_REG_BUSY))
  298. {
  299. /* Reset ADC error code field related to injected conversions only */
  300. CLEAR_BIT(hadc->ErrorCode, HAL_ADC_ERROR_JQOVF);
  301. }
  302. else
  303. {
  304. /* Set ADC error code to none */
  305. ADC_CLEAR_ERRORCODE(hadc);
  306. }
  307. /* Set ADC state */
  308. /* - Clear state bitfield related to injected group conversion results */
  309. /* - Set state bitfield related to injected operation */
  310. ADC_STATE_CLR_SET(hadc->State,
  311. HAL_ADC_STATE_READY | HAL_ADC_STATE_INJ_EOC,
  312. HAL_ADC_STATE_INJ_BUSY);
  313. /* Reset HAL_ADC_STATE_MULTIMODE_SLAVE bit
  314. - by default if ADC is Master or Independent or if multimode feature is not available
  315. - if multimode setting is set to independent mode (no dual regular or injected conversions are configured) */
  316. if (ADC_NONMULTIMODE_OR_MULTIMODEMASTER(hadc))
  317. {
  318. CLEAR_BIT(hadc->State, HAL_ADC_STATE_MULTIMODE_SLAVE);
  319. }
  320. /* Clear ADC group injected group conversion flag */
  321. /* (To ensure of no unknown state from potential previous ADC operations) */
  322. __HAL_ADC_CLEAR_FLAG(hadc, (ADC_FLAG_JEOC | ADC_FLAG_JEOS));
  323. /* Process unlocked */
  324. /* Unlock before starting ADC conversions: in case of potential */
  325. /* interruption, to let the process to ADC IRQ Handler. */
  326. __HAL_UNLOCK(hadc);
  327. /* Enable conversion of injected group, if automatic injected conversion */
  328. /* is disabled. */
  329. /* If software start has been selected, conversion starts immediately. */
  330. /* If external trigger has been selected, conversion will start at next */
  331. /* trigger event. */
  332. /* Case of multimode enabled (when multimode feature is available): */
  333. /* if ADC is slave, */
  334. /* - ADC is enabled only (conversion is not started). */
  335. /* - if multimode only concerns regular conversion, ADC is enabled */
  336. /* and conversion is started. */
  337. /* If ADC is master or independent, */
  338. /* - ADC is enabled and conversion is started. */
  339. /* Are injected conversions that of a dual Slave ? */
  340. if (ADC_INDEPENDENT_OR_NONMULTIMODEINJECTED_SLAVE(hadc))
  341. {
  342. /* hadc is not the handle of a Slave ADC with dual injected conversions enabled:
  343. set ADSTART only if JAUTO is cleared */
  344. if (HAL_IS_BIT_CLR(hadc->Instance->CFGR, ADC_CFGR_JAUTO))
  345. {
  346. SET_BIT(hadc->Instance->CR, ADC_CR_JADSTART) ;
  347. }
  348. }
  349. else
  350. {
  351. /* hadc is the handle of a Slave ADC with dual injected conversions enabled:
  352. ADSTART is not set */
  353. SET_BIT(hadc->State, HAL_ADC_STATE_MULTIMODE_SLAVE);
  354. }
  355. }
  356. else
  357. {
  358. /* Process unlocked */
  359. __HAL_UNLOCK(hadc);
  360. }
  361. /* Return function status */
  362. return tmp_hal_status;
  363. }
  364. }
  365. /**
  366. * @brief Stop conversion of injected channels. Disable ADC peripheral if
  367. * no regular conversion is on going.
  368. * @note If ADC must be disabled and if conversion is on going on
  369. * regular group, function HAL_ADC_Stop must be used to stop both
  370. * injected and regular groups, and disable the ADC.
  371. * @note If injected group mode auto-injection is enabled,
  372. * function HAL_ADC_Stop must be used.
  373. * @note In case of multimode enabled (when multimode feature is available),
  374. * HAL_ADCEx_InjectedStop() must be called for ADC master first, then for ADC slave.
  375. * For ADC master, conversion is stopped and ADC is disabled.
  376. * For ADC slave, ADC is disabled only (conversion stop of ADC master
  377. * has already stopped conversion of ADC slave).
  378. * @param hadc ADC handle.
  379. * @retval HAL status
  380. */
  381. HAL_StatusTypeDef HAL_ADCEx_InjectedStop(ADC_HandleTypeDef* hadc)
  382. {
  383. HAL_StatusTypeDef tmp_hal_status = HAL_OK;
  384. /* Check the parameters */
  385. assert_param(IS_ADC_ALL_INSTANCE(hadc->Instance));
  386. /* Process locked */
  387. __HAL_LOCK(hadc);
  388. /* 1. Stop potential conversion on going on injected group only. */
  389. tmp_hal_status = ADC_ConversionStop(hadc, ADC_INJECTED_GROUP);
  390. /* Disable ADC peripheral if injected conversions are effectively stopped */
  391. /* and if no conversion on regular group is on-going */
  392. if (tmp_hal_status == HAL_OK)
  393. {
  394. if (ADC_IS_CONVERSION_ONGOING_REGULAR(hadc) == RESET)
  395. {
  396. /* 2. Disable the ADC peripheral */
  397. tmp_hal_status = ADC_Disable(hadc);
  398. /* Check if ADC is effectively disabled */
  399. if (tmp_hal_status == HAL_OK)
  400. {
  401. /* Set ADC state */
  402. ADC_STATE_CLR_SET(hadc->State,
  403. HAL_ADC_STATE_REG_BUSY | HAL_ADC_STATE_INJ_BUSY,
  404. HAL_ADC_STATE_READY);
  405. }
  406. }
  407. /* Conversion on injected group is stopped, but ADC not disabled since */
  408. /* conversion on regular group is still running. */
  409. else
  410. {
  411. /* Set ADC state */
  412. CLEAR_BIT(hadc->State, HAL_ADC_STATE_INJ_BUSY);
  413. }
  414. }
  415. /* Process unlocked */
  416. __HAL_UNLOCK(hadc);
  417. /* Return function status */
  418. return tmp_hal_status;
  419. }
  420. /**
  421. * @brief Wait for injected group conversion to be completed.
  422. * @param hadc ADC handle
  423. * @param Timeout Timeout value in millisecond.
  424. * @note Depending on hadc->Init.EOCSelection, JEOS or JEOC is
  425. * checked and cleared depending on AUTDLY bit status.
  426. * @retval HAL status
  427. */
  428. HAL_StatusTypeDef HAL_ADCEx_InjectedPollForConversion(ADC_HandleTypeDef* hadc, uint32_t Timeout)
  429. {
  430. uint32_t tickstart = 0U;
  431. uint32_t tmp_Flag_End = 0U;
  432. uint32_t tmp_cfgr = 0U;
  433. ADC_TypeDef *tmpADC_Master;
  434. /* Check the parameters */
  435. assert_param(IS_ADC_ALL_INSTANCE(hadc->Instance));
  436. /* If end of sequence selected */
  437. if (hadc->Init.EOCSelection == ADC_EOC_SEQ_CONV)
  438. {
  439. tmp_Flag_End = ADC_FLAG_JEOS;
  440. }
  441. else /* end of conversion selected */
  442. {
  443. tmp_Flag_End = ADC_FLAG_JEOC;
  444. }
  445. /* Get timeout */
  446. tickstart = HAL_GetTick();
  447. /* Wait until End of Conversion or Sequence flag is raised */
  448. while(HAL_IS_BIT_CLR(hadc->Instance->ISR, tmp_Flag_End))
  449. {
  450. /* Check if timeout is disabled (set to infinite wait) */
  451. if(Timeout != HAL_MAX_DELAY)
  452. {
  453. if((Timeout == 0) || ((HAL_GetTick()-tickstart) > Timeout))
  454. {
  455. /* Update ADC state machine to timeout */
  456. SET_BIT(hadc->State, HAL_ADC_STATE_TIMEOUT);
  457. /* Process unlocked */
  458. __HAL_UNLOCK(hadc);
  459. return HAL_TIMEOUT;
  460. }
  461. }
  462. }
  463. /* Get relevant register CFGR in ADC instance of ADC master or slave */
  464. /* in function of multimode state (for devices with multimode */
  465. /* available). */
  466. if (ADC_INDEPENDENT_OR_NONMULTIMODEINJECTED_SLAVE(hadc) == SET)
  467. {
  468. tmp_cfgr = READ_REG(hadc->Instance->CFGR);
  469. }
  470. else
  471. {
  472. tmpADC_Master = ADC_MASTER_REGISTER(hadc);
  473. tmp_cfgr = READ_REG(tmpADC_Master->CFGR);
  474. }
  475. /* Update ADC state machine */
  476. SET_BIT(hadc->State, HAL_ADC_STATE_INJ_EOC);
  477. /* Determine whether any further conversion upcoming on group injected */
  478. /* by external trigger or by automatic injected conversion */
  479. /* from group regular. */
  480. if(ADC_IS_SOFTWARE_START_INJECTED(hadc) ||
  481. ((READ_BIT (tmp_cfgr, ADC_CFGR_JAUTO) == RESET) &&
  482. (ADC_IS_SOFTWARE_START_REGULAR(hadc) &&
  483. (READ_BIT (tmp_cfgr, ADC_CFGR_CONT) == RESET) ) ) )
  484. {
  485. /* Check whether end of sequence is reached */
  486. if( __HAL_ADC_GET_FLAG(hadc, ADC_FLAG_JEOS) )
  487. {
  488. /* Particular case if injected contexts queue is enabled: */
  489. /* when the last context has been fully processed, JSQR is reset */
  490. /* by the hardware. Even if no injected conversion is planned to come */
  491. /* (queue empty, triggers are ignored), it can start again */
  492. /* immediately after setting a new context (JADSTART is still set). */
  493. /* Therefore, state of HAL ADC injected group is kept to busy. */
  494. if(READ_BIT(tmp_cfgr, ADC_CFGR_JQM) == RESET)
  495. {
  496. /* Set ADC state */
  497. CLEAR_BIT(hadc->State, HAL_ADC_STATE_INJ_BUSY);
  498. if (HAL_IS_BIT_CLR(hadc->State, HAL_ADC_STATE_REG_BUSY))
  499. {
  500. SET_BIT(hadc->State, HAL_ADC_STATE_READY);
  501. }
  502. }
  503. }
  504. }
  505. /* Clear polled flag */
  506. if (tmp_Flag_End == ADC_FLAG_JEOS)
  507. {
  508. /* Clear end of sequence JEOS flag of injected group if low power feature */
  509. /* "LowPowerAutoWait " is disabled, to not interfere with this feature. */
  510. /* For injected groups, no new conversion will start before JEOS is */
  511. /* cleared. */
  512. if (READ_BIT (tmp_cfgr, ADC_CFGR_AUTDLY) == RESET)
  513. {
  514. __HAL_ADC_CLEAR_FLAG(hadc, (ADC_FLAG_JEOC | ADC_FLAG_JEOS));
  515. }
  516. }
  517. else
  518. {
  519. __HAL_ADC_CLEAR_FLAG(hadc, ADC_FLAG_JEOC);
  520. }
  521. /* Return API HAL status */
  522. return HAL_OK;
  523. }
  524. /**
  525. * @brief Enable ADC, start conversion of injected group with interruption.
  526. * @note Interruptions enabled in this function according to initialization
  527. * setting : JEOC (end of conversion) or JEOS (end of sequence)
  528. * @note Case of multimode enabled (when multimode feature is enabled):
  529. * HAL_ADCEx_InjectedStart_IT() API must be called for ADC slave first,
  530. * then for ADC master.
  531. * For ADC slave, ADC is enabled only (conversion is not started).
  532. * For ADC master, ADC is enabled and multimode conversion is started.
  533. * @param hadc ADC handle.
  534. * @retval HAL status.
  535. */
  536. HAL_StatusTypeDef HAL_ADCEx_InjectedStart_IT(ADC_HandleTypeDef* hadc)
  537. {
  538. HAL_StatusTypeDef tmp_hal_status = HAL_OK;
  539. /* Check the parameters */
  540. assert_param(IS_ADC_ALL_INSTANCE(hadc->Instance));
  541. if (ADC_IS_CONVERSION_ONGOING_INJECTED(hadc))
  542. {
  543. return HAL_BUSY;
  544. }
  545. else
  546. {
  547. /* In case of software trigger detection enabled, JQDIS must be set
  548. (which can be done only if ADSTART and JADSTART are both cleared).
  549. If JQDIS is not set at that point, returns an error
  550. - since software trigger detection is disabled. User needs to
  551. resort to HAL_ADCEx_DisableInjectedQueue() API to set JQDIS.
  552. - or (if JQDIS is intentionally reset) since JEXTEN = 0 which means
  553. the queue is empty */
  554. if ((READ_BIT(hadc->Instance->JSQR, ADC_JSQR_JEXTEN) == RESET)
  555. && (READ_BIT(hadc->Instance->CFGR, ADC_CFGR_JQDIS) == RESET))
  556. {
  557. SET_BIT(hadc->State, HAL_ADC_STATE_ERROR_CONFIG);
  558. return HAL_ERROR;
  559. }
  560. /* Process locked */
  561. __HAL_LOCK(hadc);
  562. /* Enable the ADC peripheral */
  563. tmp_hal_status = ADC_Enable(hadc);
  564. /* Start conversion if ADC is effectively enabled */
  565. if (tmp_hal_status == HAL_OK)
  566. {
  567. /* Check if a regular conversion is ongoing */
  568. if (HAL_IS_BIT_SET(hadc->State, HAL_ADC_STATE_REG_BUSY))
  569. {
  570. /* Reset ADC error code field related to injected conversions only */
  571. CLEAR_BIT(hadc->ErrorCode, HAL_ADC_ERROR_JQOVF);
  572. }
  573. else
  574. {
  575. /* Set ADC error code to none */
  576. ADC_CLEAR_ERRORCODE(hadc);
  577. }
  578. /* Set ADC state */
  579. /* - Clear state bitfield related to injected group conversion results */
  580. /* - Set state bitfield related to injected operation */
  581. ADC_STATE_CLR_SET(hadc->State,
  582. HAL_ADC_STATE_READY | HAL_ADC_STATE_INJ_EOC,
  583. HAL_ADC_STATE_INJ_BUSY);
  584. /* Reset HAL_ADC_STATE_MULTIMODE_SLAVE bit
  585. - by default if ADC is Master or Independent or if multimode feature is not available
  586. - if multimode setting is set to independent mode (no dual regular or injected conversions are configured) */
  587. if (ADC_NONMULTIMODE_OR_MULTIMODEMASTER(hadc))
  588. {
  589. CLEAR_BIT(hadc->State, HAL_ADC_STATE_MULTIMODE_SLAVE);
  590. }
  591. /* Clear ADC group injected group conversion flag */
  592. /* (To ensure of no unknown state from potential previous ADC operations) */
  593. __HAL_ADC_CLEAR_FLAG(hadc, (ADC_FLAG_JEOC | ADC_FLAG_JEOS));
  594. /* Process unlocked */
  595. /* Unlock before starting ADC conversions: in case of potential */
  596. /* interruption, to let the process to ADC IRQ Handler. */
  597. __HAL_UNLOCK(hadc);
  598. /* Enable ADC Injected context queue overflow interrupt if this feature */
  599. /* is enabled. */
  600. if ((hadc->Instance->CFGR & ADC_CFGR_JQM) != RESET)
  601. {
  602. __HAL_ADC_ENABLE_IT(hadc, ADC_FLAG_JQOVF);
  603. }
  604. /* Enable ADC end of conversion interrupt */
  605. switch(hadc->Init.EOCSelection)
  606. {
  607. case ADC_EOC_SEQ_CONV:
  608. __HAL_ADC_DISABLE_IT(hadc, ADC_IT_JEOC);
  609. __HAL_ADC_ENABLE_IT(hadc, ADC_IT_JEOS);
  610. break;
  611. /* case ADC_EOC_SINGLE_CONV */
  612. default:
  613. __HAL_ADC_DISABLE_IT(hadc, ADC_IT_JEOS);
  614. __HAL_ADC_ENABLE_IT(hadc, ADC_IT_JEOC);
  615. break;
  616. }
  617. /* Enable conversion of injected group, if automatic injected conversion */
  618. /* is disabled. */
  619. /* If software start has been selected, conversion starts immediately. */
  620. /* If external trigger has been selected, conversion will start at next */
  621. /* trigger event. */
  622. /* Case of multimode enabled (when multimode feature is available): */
  623. /* if ADC is slave, */
  624. /* - ADC is enabled only (conversion is not started), */
  625. /* - if multimode only concerns regular conversion, ADC is enabled */
  626. /* and conversion is started. */
  627. /* If ADC is master or independent, */
  628. /* - ADC is enabled and conversion is started. */
  629. /* Are injected conversions that of a dual Slave ? */
  630. if (ADC_INDEPENDENT_OR_NONMULTIMODEINJECTED_SLAVE(hadc))
  631. {
  632. /* hadc is not the handle of a Slave ADC with dual injected conversions enabled:
  633. set ADSTART only if JAUTO is cleared */
  634. if (HAL_IS_BIT_CLR(hadc->Instance->CFGR, ADC_CFGR_JAUTO))
  635. {
  636. SET_BIT(hadc->Instance->CR, ADC_CR_JADSTART) ;
  637. }
  638. }
  639. else
  640. {
  641. /* hadc is the handle of a Slave ADC with dual injected conversions enabled:
  642. ADSTART is not set */
  643. SET_BIT(hadc->State, HAL_ADC_STATE_MULTIMODE_SLAVE);
  644. }
  645. }
  646. else
  647. {
  648. /* Process unlocked */
  649. __HAL_UNLOCK(hadc);
  650. }
  651. /* Return function status */
  652. return tmp_hal_status;
  653. }
  654. }
  655. /**
  656. * @brief Stop conversion of injected channels, disable interruption of
  657. * end-of-conversion. Disable ADC peripheral if no regular conversion
  658. * is on going.
  659. * @note If ADC must be disabled and if conversion is on going on
  660. * regular group, function HAL_ADC_Stop must be used to stop both
  661. * injected and regular groups, and disable the ADC.
  662. * @note If injected group mode auto-injection is enabled,
  663. * function HAL_ADC_Stop must be used.
  664. * @note Case of multimode enabled (when multimode feature is available):
  665. * HAL_ADCEx_InjectedStop_IT() API must be called for ADC master first,
  666. * then for ADC slave.
  667. * For ADC master, conversion is stopped and ADC is disabled.
  668. * For ADC slave, ADC is disabled only (conversion stop of ADC master
  669. * has already stopped conversion of ADC slave).
  670. * @note In case of auto-injection mode, HAL_ADC_Stop() must be used.
  671. * @param hadc ADC handle
  672. * @retval HAL status
  673. */
  674. HAL_StatusTypeDef HAL_ADCEx_InjectedStop_IT(ADC_HandleTypeDef* hadc)
  675. {
  676. HAL_StatusTypeDef tmp_hal_status = HAL_OK;
  677. /* Check the parameters */
  678. assert_param(IS_ADC_ALL_INSTANCE(hadc->Instance));
  679. /* Process locked */
  680. __HAL_LOCK(hadc);
  681. /* 1. Stop potential conversion on going on injected group only. */
  682. tmp_hal_status = ADC_ConversionStop(hadc, ADC_INJECTED_GROUP);
  683. /* Disable ADC peripheral if injected conversions are effectively stopped */
  684. /* and if no conversion on the other group (regular group) is intended to */
  685. /* continue. */
  686. if (tmp_hal_status == HAL_OK)
  687. {
  688. /* Disable ADC end of conversion interrupt for injected channels */
  689. __HAL_ADC_DISABLE_IT(hadc, (ADC_IT_JEOC | ADC_IT_JEOS | ADC_FLAG_JQOVF));
  690. if (ADC_IS_CONVERSION_ONGOING_REGULAR(hadc) == RESET)
  691. {
  692. /* 2. Disable the ADC peripheral */
  693. tmp_hal_status = ADC_Disable(hadc);
  694. /* Check if ADC is effectively disabled */
  695. if (tmp_hal_status == HAL_OK)
  696. {
  697. /* Set ADC state */
  698. ADC_STATE_CLR_SET(hadc->State,
  699. HAL_ADC_STATE_REG_BUSY | HAL_ADC_STATE_INJ_BUSY,
  700. HAL_ADC_STATE_READY);
  701. }
  702. }
  703. /* Conversion on injected group is stopped, but ADC not disabled since */
  704. /* conversion on regular group is still running. */
  705. else
  706. {
  707. /* Set ADC state */
  708. CLEAR_BIT(hadc->State, HAL_ADC_STATE_INJ_BUSY);
  709. }
  710. }
  711. /* Process unlocked */
  712. __HAL_UNLOCK(hadc);
  713. /* Return function status */
  714. return tmp_hal_status;
  715. }
  716. #if defined(ADC_MULTIMODE_SUPPORT)
  717. /**
  718. * @brief Enable ADC, start MultiMode conversion and transfer regular results through DMA.
  719. * @note Multimode must have been previously configured using
  720. * HAL_ADCEx_MultiModeConfigChannel() function.
  721. * Interruptions enabled in this function:
  722. * overrun, DMA half transfer, DMA transfer complete.
  723. * Each of these interruptions has its dedicated callback function.
  724. * @note State field of Slave ADC handle is not updated in this configuration:
  725. * user should not rely on it for information related to Slave regular
  726. * conversions.
  727. * @param hadc ADC handle of ADC master (handle of ADC slave must not be used)
  728. * @param pData Destination Buffer address.
  729. * @param Length Length of data to be transferred from ADC peripheral to memory (in bytes).
  730. * @retval HAL status
  731. */
  732. HAL_StatusTypeDef HAL_ADCEx_MultiModeStart_DMA(ADC_HandleTypeDef* hadc, uint32_t* pData, uint32_t Length)
  733. {
  734. HAL_StatusTypeDef tmp_hal_status = HAL_OK;
  735. ADC_HandleTypeDef tmphadcSlave;
  736. ADC_Common_TypeDef *tmpADC_Common;
  737. /* Check the parameters */
  738. assert_param(IS_ADC_MULTIMODE_MASTER_INSTANCE(hadc->Instance));
  739. assert_param(IS_FUNCTIONAL_STATE(hadc->Init.ContinuousConvMode));
  740. assert_param(IS_ADC_EXTTRIG_EDGE(hadc->Init.ExternalTrigConvEdge));
  741. assert_param(IS_FUNCTIONAL_STATE(hadc->Init.DMAContinuousRequests));
  742. if (ADC_IS_CONVERSION_ONGOING_REGULAR(hadc))
  743. {
  744. return HAL_BUSY;
  745. }
  746. else
  747. {
  748. /* Process locked */
  749. __HAL_LOCK(hadc);
  750. /* Set a temporary handle of the ADC slave associated to the ADC master */
  751. ADC_MULTI_SLAVE(hadc, &tmphadcSlave);
  752. if (tmphadcSlave.Instance == NULL)
  753. {
  754. /* Set ADC state */
  755. SET_BIT(hadc->State, HAL_ADC_STATE_ERROR_CONFIG);
  756. /* Process unlocked */
  757. __HAL_UNLOCK(hadc);
  758. return HAL_ERROR;
  759. }
  760. /* Enable the ADC peripherals: master and slave (in case if not already */
  761. /* enabled previously) */
  762. tmp_hal_status = ADC_Enable(hadc);
  763. if (tmp_hal_status == HAL_OK)
  764. {
  765. tmp_hal_status = ADC_Enable(&tmphadcSlave);
  766. }
  767. /* Start multimode conversion of ADCs pair */
  768. if (tmp_hal_status == HAL_OK)
  769. {
  770. /* Set ADC state */
  771. ADC_STATE_CLR_SET(hadc->State,
  772. (HAL_ADC_STATE_READY | HAL_ADC_STATE_REG_EOC | HAL_ADC_STATE_REG_OVR | HAL_ADC_STATE_REG_EOSMP),
  773. HAL_ADC_STATE_REG_BUSY);
  774. /* Set ADC error code to none */
  775. ADC_CLEAR_ERRORCODE(hadc);
  776. /* Set the DMA transfer complete callback */
  777. hadc->DMA_Handle->XferCpltCallback = ADC_DMAConvCplt;
  778. /* Set the DMA half transfer complete callback */
  779. hadc->DMA_Handle->XferHalfCpltCallback = ADC_DMAHalfConvCplt;
  780. /* Set the DMA error callback */
  781. hadc->DMA_Handle->XferErrorCallback = ADC_DMAError ;
  782. /* Pointer to the common control register */
  783. tmpADC_Common = __LL_ADC_COMMON_INSTANCE(hadc->Instance);
  784. /* Manage ADC and DMA start: ADC overrun interruption, DMA start, ADC */
  785. /* start (in case of SW start): */
  786. /* Clear regular group conversion flag and overrun flag */
  787. /* (To ensure of no unknown state from potential previous ADC operations) */
  788. __HAL_ADC_CLEAR_FLAG(hadc, (ADC_FLAG_EOC | ADC_FLAG_EOS | ADC_FLAG_OVR));
  789. /* Process unlocked */
  790. /* Unlock before starting ADC conversions: in case of potential */
  791. /* interruption, to let the process to ADC IRQ Handler. */
  792. __HAL_UNLOCK(hadc);
  793. /* Enable ADC overrun interrupt */
  794. __HAL_ADC_ENABLE_IT(hadc, ADC_IT_OVR);
  795. /* Start the DMA channel */
  796. HAL_DMA_Start_IT(hadc->DMA_Handle, (uint32_t)&tmpADC_Common->CDR, (uint32_t)pData, Length);
  797. /* Enable conversion of regular group. */
  798. /* If software start has been selected, conversion starts immediately. */
  799. /* If external trigger has been selected, conversion will start at next */
  800. /* trigger event. */
  801. /* Start ADC group regular conversion */
  802. LL_ADC_REG_StartConversion(hadc->Instance);
  803. }
  804. else
  805. {
  806. /* Process unlocked */
  807. __HAL_UNLOCK(hadc);
  808. }
  809. /* Return function status */
  810. return tmp_hal_status;
  811. }
  812. }
  813. /**
  814. * @brief Stop multimode ADC conversion, disable ADC DMA transfer, disable ADC peripheral.
  815. * @note Multimode is kept enabled after this function. MultiMode DMA bits
  816. * (MDMA and DMACFG bits of common CCR register) are maintained. To disable
  817. * Multimode (set with HAL_ADCEx_MultiModeConfigChannel()), ADC must be
  818. * reinitialized using HAL_ADC_Init() or HAL_ADC_DeInit(), or the user can
  819. * resort to HAL_ADCEx_DisableMultiMode() API.
  820. * @note In case of DMA configured in circular mode, function
  821. * HAL_ADC_Stop_DMA() must be called after this function with handle of
  822. * ADC slave, to properly disable the DMA channel.
  823. * @param hadc ADC handle of ADC master (handle of ADC slave must not be used)
  824. * @retval HAL status
  825. */
  826. HAL_StatusTypeDef HAL_ADCEx_MultiModeStop_DMA(ADC_HandleTypeDef* hadc)
  827. {
  828. HAL_StatusTypeDef tmp_hal_status = HAL_OK;
  829. uint32_t tickstart;
  830. ADC_HandleTypeDef tmphadcSlave;
  831. /* Check the parameters */
  832. assert_param(IS_ADC_MULTIMODE_MASTER_INSTANCE(hadc->Instance));
  833. /* Process locked */
  834. __HAL_LOCK(hadc);
  835. /* 1. Stop potential multimode conversion on going, on regular and injected groups */
  836. tmp_hal_status = ADC_ConversionStop(hadc, ADC_REGULAR_INJECTED_GROUP);
  837. /* Disable ADC peripheral if conversions are effectively stopped */
  838. if (tmp_hal_status == HAL_OK)
  839. {
  840. /* Set a temporary handle of the ADC slave associated to the ADC master */
  841. ADC_MULTI_SLAVE(hadc, &tmphadcSlave);
  842. if (tmphadcSlave.Instance == NULL)
  843. {
  844. /* Update ADC state machine to error */
  845. SET_BIT(hadc->State, HAL_ADC_STATE_ERROR_CONFIG);
  846. /* Process unlocked */
  847. __HAL_UNLOCK(hadc);
  848. return HAL_ERROR;
  849. }
  850. /* Procedure to disable the ADC peripheral: wait for conversions */
  851. /* effectively stopped (ADC master and ADC slave), then disable ADC */
  852. /* 1. Wait until ADSTP=0 for ADC master and ADC slave*/
  853. tickstart = HAL_GetTick();
  854. while(ADC_IS_CONVERSION_ONGOING_REGULAR(hadc) ||
  855. ADC_IS_CONVERSION_ONGOING_REGULAR(&tmphadcSlave) )
  856. {
  857. if((HAL_GetTick()-tickstart) > ADC_STOP_CONVERSION_TIMEOUT)
  858. {
  859. /* Update ADC state machine to error */
  860. SET_BIT(hadc->State, HAL_ADC_STATE_ERROR_INTERNAL);
  861. /* Process unlocked */
  862. __HAL_UNLOCK(hadc);
  863. return HAL_ERROR;
  864. }
  865. }
  866. /* Disable the DMA channel (in case of DMA in circular mode or stop */
  867. /* while DMA transfer is on going) */
  868. /* Note: DMA channel of ADC slave should be stopped after this function */
  869. /* with HAL_ADC_Stop_DMA() API. */
  870. tmp_hal_status = HAL_DMA_Abort(hadc->DMA_Handle);
  871. /* Check if DMA channel effectively disabled */
  872. if (tmp_hal_status == HAL_ERROR)
  873. {
  874. /* Update ADC state machine to error */
  875. SET_BIT(hadc->State, HAL_ADC_STATE_ERROR_DMA);
  876. }
  877. /* Disable ADC overrun interrupt */
  878. __HAL_ADC_DISABLE_IT(hadc, ADC_IT_OVR);
  879. /* 2. Disable the ADC peripherals: master and slave */
  880. /* Update "tmp_hal_status" only if DMA channel disabling passed, to keep in */
  881. /* memory a potential failing status. */
  882. if (tmp_hal_status == HAL_OK)
  883. {
  884. /* Check if ADC are effectively disabled */
  885. if ((ADC_Disable(hadc) == HAL_OK) &&
  886. (ADC_Disable(&tmphadcSlave) == HAL_OK) )
  887. {
  888. tmp_hal_status = HAL_OK;
  889. }
  890. }
  891. else
  892. {
  893. ADC_Disable(hadc);
  894. ADC_Disable(&tmphadcSlave);
  895. }
  896. /* Set ADC state (ADC master) */
  897. ADC_STATE_CLR_SET(hadc->State,
  898. HAL_ADC_STATE_REG_BUSY | HAL_ADC_STATE_INJ_BUSY,
  899. HAL_ADC_STATE_READY);
  900. }
  901. /* Process unlocked */
  902. __HAL_UNLOCK(hadc);
  903. /* Return function status */
  904. return tmp_hal_status;
  905. }
  906. /**
  907. * @brief Return the last ADC Master and Slave regular conversions results when in multimode configuration.
  908. * @param hadc ADC handle of ADC Master (handle of ADC Slave must not be used)
  909. * @retval The converted data values.
  910. */
  911. uint32_t HAL_ADCEx_MultiModeGetValue(ADC_HandleTypeDef* hadc)
  912. {
  913. ADC_Common_TypeDef *tmpADC_Common;
  914. /* Check the parameters */
  915. assert_param(IS_ADC_MULTIMODE_MASTER_INSTANCE(hadc->Instance));
  916. /* Pointer to the common control register */
  917. tmpADC_Common = __LL_ADC_COMMON_INSTANCE(hadc->Instance);
  918. /* Return the multi mode conversion value */
  919. return tmpADC_Common->CDR;
  920. }
  921. #endif /* ADC_MULTIMODE_SUPPORT */
  922. /**
  923. * @brief Get ADC injected group conversion result.
  924. * @note Reading register JDRx automatically clears ADC flag JEOC
  925. * (ADC group injected end of unitary conversion).
  926. * @note This function does not clear ADC flag JEOS
  927. * (ADC group injected end of sequence conversion)
  928. * Occurrence of flag JEOS rising:
  929. * - If sequencer is composed of 1 rank, flag JEOS is equivalent
  930. * to flag JEOC.
  931. * - If sequencer is composed of several ranks, during the scan
  932. * sequence flag JEOC only is raised, at the end of the scan sequence
  933. * both flags JEOC and EOS are raised.
  934. * Flag JEOS must not be cleared by this function because
  935. * it would not be compliant with low power features
  936. * (feature low power auto-wait, not available on all STM32 families).
  937. * To clear this flag, either use function:
  938. * in programming model IT: @ref HAL_ADC_IRQHandler(), in programming
  939. * model polling: @ref HAL_ADCEx_InjectedPollForConversion()
  940. * or @ref __HAL_ADC_CLEAR_FLAG(&hadc, ADC_FLAG_JEOS).
  941. * @param hadc ADC handle
  942. * @param InjectedRank the converted ADC injected rank.
  943. * This parameter can be one of the following values:
  944. * @arg @ref ADC_INJECTED_RANK_1 ADC group injected rank 1
  945. * @arg @ref ADC_INJECTED_RANK_2 ADC group injected rank 2
  946. * @arg @ref ADC_INJECTED_RANK_3 ADC group injected rank 3
  947. * @arg @ref ADC_INJECTED_RANK_4 ADC group injected rank 4
  948. * @retval ADC group injected conversion data
  949. */
  950. uint32_t HAL_ADCEx_InjectedGetValue(ADC_HandleTypeDef* hadc, uint32_t InjectedRank)
  951. {
  952. uint32_t tmp_jdr = 0;
  953. /* Check the parameters */
  954. assert_param(IS_ADC_ALL_INSTANCE(hadc->Instance));
  955. assert_param(IS_ADC_INJECTED_RANK(InjectedRank));
  956. /* Get ADC converted value */
  957. switch(InjectedRank)
  958. {
  959. case ADC_INJECTED_RANK_4:
  960. tmp_jdr = hadc->Instance->JDR4;
  961. break;
  962. case ADC_INJECTED_RANK_3:
  963. tmp_jdr = hadc->Instance->JDR3;
  964. break;
  965. case ADC_INJECTED_RANK_2:
  966. tmp_jdr = hadc->Instance->JDR2;
  967. break;
  968. case ADC_INJECTED_RANK_1:
  969. default:
  970. tmp_jdr = hadc->Instance->JDR1;
  971. break;
  972. }
  973. /* Return ADC converted value */
  974. return tmp_jdr;
  975. }
  976. /**
  977. * @brief Injected conversion complete callback in non-blocking mode.
  978. * @param hadc ADC handle
  979. * @retval None
  980. */
  981. __weak void HAL_ADCEx_InjectedConvCpltCallback(ADC_HandleTypeDef* hadc)
  982. {
  983. /* Prevent unused argument(s) compilation warning */
  984. UNUSED(hadc);
  985. /* NOTE : This function should not be modified. When the callback is needed,
  986. function HAL_ADCEx_InjectedConvCpltCallback must be implemented in the user file.
  987. */
  988. }
  989. /**
  990. * @brief Injected context queue overflow callback.
  991. * @note This callback is called if injected context queue is enabled
  992. (parameter "QueueInjectedContext" in injected channel configuration)
  993. and if a new injected context is set when queue is full (maximum 2
  994. contexts).
  995. * @param hadc ADC handle
  996. * @retval None
  997. */
  998. __weak void HAL_ADCEx_InjectedQueueOverflowCallback(ADC_HandleTypeDef* hadc)
  999. {
  1000. /* Prevent unused argument(s) compilation warning */
  1001. UNUSED(hadc);
  1002. /* NOTE : This function should not be modified. When the callback is needed,
  1003. function HAL_ADCEx_InjectedQueueOverflowCallback must be implemented in the user file.
  1004. */
  1005. }
  1006. /**
  1007. * @brief Analog watchdog 2 callback in non-blocking mode.
  1008. * @param hadc ADC handle
  1009. * @retval None
  1010. */
  1011. __weak void HAL_ADCEx_LevelOutOfWindow2Callback(ADC_HandleTypeDef* hadc)
  1012. {
  1013. /* Prevent unused argument(s) compilation warning */
  1014. UNUSED(hadc);
  1015. /* NOTE : This function should not be modified. When the callback is needed,
  1016. function HAL_ADCEx_LevelOutOfWindow2Callback must be implemented in the user file.
  1017. */
  1018. }
  1019. /**
  1020. * @brief Analog watchdog 3 callback in non-blocking mode.
  1021. * @param hadc ADC handle
  1022. * @retval None
  1023. */
  1024. __weak void HAL_ADCEx_LevelOutOfWindow3Callback(ADC_HandleTypeDef* hadc)
  1025. {
  1026. /* Prevent unused argument(s) compilation warning */
  1027. UNUSED(hadc);
  1028. /* NOTE : This function should not be modified. When the callback is needed,
  1029. function HAL_ADCEx_LevelOutOfWindow3Callback must be implemented in the user file.
  1030. */
  1031. }
  1032. /**
  1033. * @brief End Of Sampling callback in non-blocking mode.
  1034. * @param hadc ADC handle
  1035. * @retval None
  1036. */
  1037. __weak void HAL_ADCEx_EndOfSamplingCallback(ADC_HandleTypeDef* hadc)
  1038. {
  1039. /* Prevent unused argument(s) compilation warning */
  1040. UNUSED(hadc);
  1041. /* NOTE : This function should not be modified. When the callback is needed,
  1042. function HAL_ADCEx_EndOfSamplingCallback must be implemented in the user file.
  1043. */
  1044. }
  1045. /**
  1046. * @brief Stop ADC conversion of regular group (and injected channels in
  1047. * case of auto_injection mode), disable ADC peripheral if no
  1048. * conversion is on going on injected group.
  1049. * @param hadc ADC handle
  1050. * @retval HAL status.
  1051. */
  1052. HAL_StatusTypeDef HAL_ADCEx_RegularStop(ADC_HandleTypeDef* hadc)
  1053. {
  1054. HAL_StatusTypeDef tmp_hal_status = HAL_OK;
  1055. /* Check the parameters */
  1056. assert_param(IS_ADC_ALL_INSTANCE(hadc->Instance));
  1057. /* Process locked */
  1058. __HAL_LOCK(hadc);
  1059. /* 1. Stop potential regular conversion on going */
  1060. tmp_hal_status = ADC_ConversionStop(hadc, ADC_REGULAR_GROUP);
  1061. /* Disable ADC peripheral if regular conversions are effectively stopped
  1062. and if no injected conversions are on-going */
  1063. if (tmp_hal_status == HAL_OK)
  1064. {
  1065. /* Clear HAL_ADC_STATE_REG_BUSY bit */
  1066. CLEAR_BIT(hadc->State, HAL_ADC_STATE_REG_BUSY);
  1067. if (ADC_IS_CONVERSION_ONGOING_INJECTED(hadc) == RESET)
  1068. {
  1069. /* 2. Disable the ADC peripheral */
  1070. tmp_hal_status = ADC_Disable(hadc);
  1071. /* Check if ADC is effectively disabled */
  1072. if (tmp_hal_status == HAL_OK)
  1073. {
  1074. /* Set ADC state */
  1075. ADC_STATE_CLR_SET(hadc->State,
  1076. HAL_ADC_STATE_INJ_BUSY,
  1077. HAL_ADC_STATE_READY);
  1078. }
  1079. }
  1080. /* Conversion on injected group is stopped, but ADC not disabled since */
  1081. /* conversion on regular group is still running. */
  1082. else
  1083. {
  1084. SET_BIT(hadc->State, HAL_ADC_STATE_INJ_BUSY);
  1085. }
  1086. }
  1087. /* Process unlocked */
  1088. __HAL_UNLOCK(hadc);
  1089. /* Return function status */
  1090. return tmp_hal_status;
  1091. }
  1092. /**
  1093. * @brief Stop ADC conversion of ADC groups regular and injected,
  1094. * disable interrution of end-of-conversion,
  1095. * disable ADC peripheral if no conversion is on going
  1096. * on injected group.
  1097. * @param hadc ADC handle
  1098. * @retval HAL status.
  1099. */
  1100. HAL_StatusTypeDef HAL_ADCEx_RegularStop_IT(ADC_HandleTypeDef* hadc)
  1101. {
  1102. HAL_StatusTypeDef tmp_hal_status = HAL_OK;
  1103. /* Check the parameters */
  1104. assert_param(IS_ADC_ALL_INSTANCE(hadc->Instance));
  1105. /* Process locked */
  1106. __HAL_LOCK(hadc);
  1107. /* 1. Stop potential regular conversion on going */
  1108. tmp_hal_status = ADC_ConversionStop(hadc, ADC_REGULAR_GROUP);
  1109. /* Disable ADC peripheral if conversions are effectively stopped
  1110. and if no injected conversion is on-going */
  1111. if (tmp_hal_status == HAL_OK)
  1112. {
  1113. /* Clear HAL_ADC_STATE_REG_BUSY bit */
  1114. CLEAR_BIT(hadc->State, HAL_ADC_STATE_REG_BUSY);
  1115. /* Disable all regular-related interrupts */
  1116. __HAL_ADC_DISABLE_IT(hadc, (ADC_IT_EOC | ADC_IT_EOS | ADC_IT_OVR));
  1117. /* 2. Disable ADC peripheral if no injected conversions are on-going */
  1118. if (ADC_IS_CONVERSION_ONGOING_INJECTED(hadc) == RESET)
  1119. {
  1120. tmp_hal_status = ADC_Disable(hadc);
  1121. /* if no issue reported */
  1122. if (tmp_hal_status == HAL_OK)
  1123. {
  1124. /* Set ADC state */
  1125. ADC_STATE_CLR_SET(hadc->State,
  1126. HAL_ADC_STATE_INJ_BUSY,
  1127. HAL_ADC_STATE_READY);
  1128. }
  1129. }
  1130. else
  1131. {
  1132. SET_BIT(hadc->State, HAL_ADC_STATE_INJ_BUSY);
  1133. }
  1134. }
  1135. /* Process unlocked */
  1136. __HAL_UNLOCK(hadc);
  1137. /* Return function status */
  1138. return tmp_hal_status;
  1139. }
  1140. /**
  1141. * @brief Stop ADC conversion of regular group (and injected group in
  1142. * case of auto_injection mode), disable ADC DMA transfer, disable
  1143. * ADC peripheral if no conversion is on going
  1144. * on injected group.
  1145. * @note HAL_ADCEx_RegularStop_DMA() function is dedicated to single-ADC mode only.
  1146. * For multimode (when multimode feature is available),
  1147. * HAL_ADCEx_RegularMultiModeStop_DMA() API must be used.
  1148. * @param hadc ADC handle
  1149. * @retval HAL status.
  1150. */
  1151. HAL_StatusTypeDef HAL_ADCEx_RegularStop_DMA(ADC_HandleTypeDef* hadc)
  1152. {
  1153. HAL_StatusTypeDef tmp_hal_status = HAL_OK;
  1154. /* Check the parameters */
  1155. assert_param(IS_ADC_ALL_INSTANCE(hadc->Instance));
  1156. /* Process locked */
  1157. __HAL_LOCK(hadc);
  1158. /* 1. Stop potential regular conversion on going */
  1159. tmp_hal_status = ADC_ConversionStop(hadc, ADC_REGULAR_GROUP);
  1160. /* Disable ADC peripheral if conversions are effectively stopped
  1161. and if no injected conversion is on-going */
  1162. if (tmp_hal_status == HAL_OK)
  1163. {
  1164. /* Clear HAL_ADC_STATE_REG_BUSY bit */
  1165. CLEAR_BIT(hadc->State, HAL_ADC_STATE_REG_BUSY);
  1166. /* Disable ADC DMA (ADC DMA configuration ADC_CFGR_DMACFG is kept) */
  1167. CLEAR_BIT(hadc->Instance->CFGR, ADC_CFGR_DMAEN);
  1168. /* Disable the DMA channel (in case of DMA in circular mode or stop while */
  1169. /* while DMA transfer is on going) */
  1170. tmp_hal_status = HAL_DMA_Abort(hadc->DMA_Handle);
  1171. /* Check if DMA channel effectively disabled */
  1172. if (tmp_hal_status != HAL_OK)
  1173. {
  1174. /* Update ADC state machine to error */
  1175. SET_BIT(hadc->State, HAL_ADC_STATE_ERROR_DMA);
  1176. }
  1177. /* Disable ADC overrun interrupt */
  1178. __HAL_ADC_DISABLE_IT(hadc, ADC_IT_OVR);
  1179. /* 2. Disable the ADC peripheral */
  1180. /* Update "tmp_hal_status" only if DMA channel disabling passed, to keep in */
  1181. /* memory a potential failing status. */
  1182. if (ADC_IS_CONVERSION_ONGOING_INJECTED(hadc) == RESET)
  1183. {
  1184. if (tmp_hal_status == HAL_OK)
  1185. {
  1186. tmp_hal_status = ADC_Disable(hadc);
  1187. }
  1188. else
  1189. {
  1190. ADC_Disable(hadc);
  1191. }
  1192. /* Check if ADC is effectively disabled */
  1193. if (tmp_hal_status == HAL_OK)
  1194. {
  1195. /* Set ADC state */
  1196. ADC_STATE_CLR_SET(hadc->State,
  1197. HAL_ADC_STATE_INJ_BUSY,
  1198. HAL_ADC_STATE_READY);
  1199. }
  1200. }
  1201. else
  1202. {
  1203. SET_BIT(hadc->State, HAL_ADC_STATE_INJ_BUSY);
  1204. }
  1205. }
  1206. /* Process unlocked */
  1207. __HAL_UNLOCK(hadc);
  1208. /* Return function status */
  1209. return tmp_hal_status;
  1210. }
  1211. #if defined(ADC_MULTIMODE_SUPPORT)
  1212. /**
  1213. * @brief Stop DMA-based multimode ADC conversion, disable ADC DMA transfer, disable ADC peripheral if no injected conversion is on-going.
  1214. * @note Multimode is kept enabled after this function. Multimode DMA bits
  1215. * (MDMA and DMACFG bits of common CCR register) are maintained. To disable
  1216. * multimode (set with HAL_ADCEx_MultiModeConfigChannel()), ADC must be
  1217. * reinitialized using HAL_ADC_Init() or HAL_ADC_DeInit(), or the user can
  1218. * resort to HAL_ADCEx_DisableMultiMode() API.
  1219. * @note In case of DMA configured in circular mode, function
  1220. * HAL_ADCEx_RegularStop_DMA() must be called after this function with handle of
  1221. * ADC slave, to properly disable the DMA channel.
  1222. * @param hadc ADC handle of ADC master (handle of ADC slave must not be used)
  1223. * @retval HAL status
  1224. */
  1225. HAL_StatusTypeDef HAL_ADCEx_RegularMultiModeStop_DMA(ADC_HandleTypeDef* hadc)
  1226. {
  1227. HAL_StatusTypeDef tmp_hal_status = HAL_OK;
  1228. uint32_t tickstart;
  1229. ADC_HandleTypeDef tmphadcSlave;
  1230. /* Check the parameters */
  1231. assert_param(IS_ADC_MULTIMODE_MASTER_INSTANCE(hadc->Instance));
  1232. /* Process locked */
  1233. __HAL_LOCK(hadc);
  1234. /* 1. Stop potential multimode conversion on going, on regular groups */
  1235. tmp_hal_status = ADC_ConversionStop(hadc, ADC_REGULAR_GROUP);
  1236. /* Disable ADC peripheral if conversions are effectively stopped */
  1237. if (tmp_hal_status == HAL_OK)
  1238. {
  1239. /* Clear HAL_ADC_STATE_REG_BUSY bit */
  1240. CLEAR_BIT(hadc->State, HAL_ADC_STATE_REG_BUSY);
  1241. /* Set a temporary handle of the ADC slave associated to the ADC master */
  1242. ADC_MULTI_SLAVE(hadc, &tmphadcSlave);
  1243. if (tmphadcSlave.Instance == NULL)
  1244. {
  1245. /* Update ADC state machine to error */
  1246. SET_BIT(hadc->State, HAL_ADC_STATE_ERROR_CONFIG);
  1247. /* Process unlocked */
  1248. __HAL_UNLOCK(hadc);
  1249. return HAL_ERROR;
  1250. }
  1251. /* Procedure to disable the ADC peripheral: wait for conversions */
  1252. /* effectively stopped (ADC master and ADC slave), then disable ADC */
  1253. /* 1. Wait until ADSTP=0 for ADC master and ADC slave*/
  1254. tickstart = HAL_GetTick();
  1255. while(ADC_IS_CONVERSION_ONGOING_REGULAR(hadc) ||
  1256. ADC_IS_CONVERSION_ONGOING_REGULAR(&tmphadcSlave) )
  1257. {
  1258. if((HAL_GetTick()-tickstart) > ADC_STOP_CONVERSION_TIMEOUT)
  1259. {
  1260. /* Update ADC state machine to error */
  1261. SET_BIT(hadc->State, HAL_ADC_STATE_ERROR_INTERNAL);
  1262. /* Process unlocked */
  1263. __HAL_UNLOCK(hadc);
  1264. return HAL_ERROR;
  1265. }
  1266. }
  1267. /* Disable the DMA channel (in case of DMA in circular mode or stop */
  1268. /* while DMA transfer is on going) */
  1269. /* Note: DMA channel of ADC slave should be stopped after this function */
  1270. /* with HAL_ADCEx_RegularStop_DMA() API. */
  1271. tmp_hal_status = HAL_DMA_Abort(hadc->DMA_Handle);
  1272. /* Check if DMA channel effectively disabled */
  1273. if (tmp_hal_status != HAL_OK)
  1274. {
  1275. /* Update ADC state machine to error */
  1276. SET_BIT(hadc->State, HAL_ADC_STATE_ERROR_DMA);
  1277. }
  1278. /* Disable ADC overrun interrupt */
  1279. __HAL_ADC_DISABLE_IT(hadc, ADC_IT_OVR);
  1280. /* 2. Disable the ADC peripherals: master and slave if no injected */
  1281. /* conversion is on-going. */
  1282. /* Update "tmp_hal_status" only if DMA channel disabling passed, to keep in */
  1283. /* memory a potential failing status. */
  1284. if (tmp_hal_status == HAL_OK)
  1285. {
  1286. if (ADC_IS_CONVERSION_ONGOING_INJECTED(hadc) == RESET)
  1287. {
  1288. tmp_hal_status = ADC_Disable(hadc);
  1289. if (tmp_hal_status == HAL_OK)
  1290. {
  1291. if (ADC_IS_CONVERSION_ONGOING_INJECTED(&tmphadcSlave) == RESET)
  1292. {
  1293. tmp_hal_status = ADC_Disable(&tmphadcSlave);
  1294. }
  1295. }
  1296. }
  1297. if (tmp_hal_status == HAL_OK)
  1298. {
  1299. /* Both Master and Slave ADC's could be disabled. Update Master State */
  1300. /* Clear HAL_ADC_STATE_INJ_BUSY bit, set HAL_ADC_STATE_READY bit */
  1301. ADC_STATE_CLR_SET(hadc->State, HAL_ADC_STATE_INJ_BUSY, HAL_ADC_STATE_READY);
  1302. }
  1303. else
  1304. {
  1305. /* injected (Master or Slave) conversions are still on-going,
  1306. no Master State change */
  1307. }
  1308. }
  1309. }
  1310. /* Process unlocked */
  1311. __HAL_UNLOCK(hadc);
  1312. /* Return function status */
  1313. return tmp_hal_status;
  1314. }
  1315. #endif /* ADC_MULTIMODE_SUPPORT */
  1316. /**
  1317. * @}
  1318. */
  1319. /** @defgroup ADCEx_Exported_Functions_Group2 ADC Extended Peripheral Control functions
  1320. * @brief ADC Extended Peripheral Control functions
  1321. *
  1322. @verbatim
  1323. ===============================================================================
  1324. ##### Peripheral Control functions #####
  1325. ===============================================================================
  1326. [..] This section provides functions allowing to:
  1327. (+) Configure channels on injected group
  1328. (+) Configure multimode when multimode feature is available
  1329. (+) Enable or Disable Injected Queue
  1330. (+) Disable ADC voltage regulator
  1331. (+) Enter ADC deep-power-down mode
  1332. @endverbatim
  1333. * @{
  1334. */
  1335. /**
  1336. * @brief Configure a channel to be assigned to ADC group injected.
  1337. * @note Possibility to update parameters on the fly:
  1338. * This function initializes injected group, following calls to this
  1339. * function can be used to reconfigure some parameters of structure
  1340. * "ADC_InjectionConfTypeDef" on the fly, without resetting the ADC.
  1341. * The setting of these parameters is conditioned to ADC state:
  1342. * Refer to comments of structure "ADC_InjectionConfTypeDef".
  1343. * @note In case of usage of internal measurement channels:
  1344. * Vbat/VrefInt/TempSensor.
  1345. * These internal paths can be disabled using function
  1346. * HAL_ADC_DeInit().
  1347. * @note Caution: For Injected Context Queue use, a context must be fully
  1348. * defined before start of injected conversion. All channels are configured
  1349. * consecutively for the same ADC instance. Therefore, the number of calls to
  1350. * HAL_ADCEx_InjectedConfigChannel() must be equal to the value of parameter
  1351. * InjectedNbrOfConversion for each context.
  1352. * - Example 1: If 1 context is intended to be used (or if there is no use of the
  1353. * Injected Queue Context feature) and if the context contains 3 injected ranks
  1354. * (InjectedNbrOfConversion = 3), HAL_ADCEx_InjectedConfigChannel() must be
  1355. * called once for each channel (i.e. 3 times) before starting a conversion.
  1356. * This function must not be called to configure a 4th injected channel:
  1357. * it would start a new context into context queue.
  1358. * - Example 2: If 2 contexts are intended to be used and each of them contains
  1359. * 3 injected ranks (InjectedNbrOfConversion = 3),
  1360. * HAL_ADCEx_InjectedConfigChannel() must be called once for each channel and
  1361. * for each context (3 channels x 2 contexts = 6 calls). Conversion can
  1362. * start once the 1st context is set, that is after the first three
  1363. * HAL_ADCEx_InjectedConfigChannel() calls. The 2nd context can be set on the fly.
  1364. * @param hadc ADC handle
  1365. * @param sConfigInjected Structure of ADC injected group and ADC channel for
  1366. * injected group.
  1367. * @retval HAL status
  1368. */
  1369. HAL_StatusTypeDef HAL_ADCEx_InjectedConfigChannel(ADC_HandleTypeDef* hadc, ADC_InjectionConfTypeDef* sConfigInjected)
  1370. {
  1371. HAL_StatusTypeDef tmp_hal_status = HAL_OK;
  1372. uint32_t tmpOffsetShifted;
  1373. uint32_t wait_loop_index = 0U;
  1374. uint32_t tmp_JSQR_ContextQueueBeingBuilt = 0U;
  1375. /* Check the parameters */
  1376. assert_param(IS_ADC_ALL_INSTANCE(hadc->Instance));
  1377. assert_param(IS_ADC_SAMPLE_TIME(sConfigInjected->InjectedSamplingTime));
  1378. assert_param(IS_ADC_SINGLE_DIFFERENTIAL(sConfigInjected->InjectedSingleDiff));
  1379. assert_param(IS_FUNCTIONAL_STATE(sConfigInjected->AutoInjectedConv));
  1380. assert_param(IS_FUNCTIONAL_STATE(sConfigInjected->QueueInjectedContext));
  1381. assert_param(IS_ADC_EXTTRIGINJEC_EDGE(sConfigInjected->ExternalTrigInjecConvEdge));
  1382. assert_param(IS_ADC_EXTTRIGINJEC(hadc, sConfigInjected->ExternalTrigInjecConv));
  1383. assert_param(IS_ADC_OFFSET_NUMBER(sConfigInjected->InjectedOffsetNumber));
  1384. assert_param(IS_ADC_RANGE(ADC_GET_RESOLUTION(hadc), sConfigInjected->InjectedOffset));
  1385. assert_param(IS_FUNCTIONAL_STATE(sConfigInjected->InjecOversamplingMode));
  1386. if(hadc->Init.ScanConvMode != ADC_SCAN_DISABLE)
  1387. {
  1388. assert_param(IS_ADC_INJECTED_RANK(sConfigInjected->InjectedRank));
  1389. assert_param(IS_ADC_INJECTED_NB_CONV(sConfigInjected->InjectedNbrOfConversion));
  1390. assert_param(IS_FUNCTIONAL_STATE(sConfigInjected->InjectedDiscontinuousConvMode));
  1391. }
  1392. /* if JOVSE is set, the value of the OFFSETy_EN bit in ADCx_OFRy register is
  1393. ignored (considered as reset) */
  1394. assert_param(!((sConfigInjected->InjectedOffsetNumber != ADC_OFFSET_NONE) && (sConfigInjected->InjecOversamplingMode == ENABLE)));
  1395. /* JDISCEN and JAUTO bits can't be set at the same time */
  1396. assert_param(!((sConfigInjected->InjectedDiscontinuousConvMode == ENABLE) && (sConfigInjected->AutoInjectedConv == ENABLE)));
  1397. /* DISCEN and JAUTO bits can't be set at the same time */
  1398. assert_param(!((hadc->Init.DiscontinuousConvMode == ENABLE) && (sConfigInjected->AutoInjectedConv == ENABLE)));
  1399. /* Verification of channel number */
  1400. if (sConfigInjected->InjectedSingleDiff != ADC_DIFFERENTIAL_ENDED)
  1401. {
  1402. assert_param(IS_ADC_CHANNEL(hadc, sConfigInjected->InjectedChannel));
  1403. }
  1404. else
  1405. {
  1406. assert_param(IS_ADC_DIFF_CHANNEL(hadc, sConfigInjected->InjectedChannel));
  1407. }
  1408. /* Process locked */
  1409. __HAL_LOCK(hadc);
  1410. /* Configuration of injected group sequencer: */
  1411. /* Hardware constraint: Must fully define injected context register JSQR */
  1412. /* before make it entering into injected sequencer queue. */
  1413. /* */
  1414. /* - if scan mode is disabled: */
  1415. /* * Injected channels sequence length is set to 0x00: 1 channel */
  1416. /* converted (channel on injected rank 1) */
  1417. /* Parameter "InjectedNbrOfConversion" is discarded. */
  1418. /* * Injected context register JSQR setting is simple: register is fully */
  1419. /* defined on one call of this function (for injected rank 1) and can */
  1420. /* be entered into queue directly. */
  1421. /* - if scan mode is enabled: */
  1422. /* * Injected channels sequence length is set to parameter */
  1423. /* "InjectedNbrOfConversion". */
  1424. /* * Injected context register JSQR setting more complex: register is */
  1425. /* fully defined over successive calls of this function, for each */
  1426. /* injected channel rank. It is entered into queue only when all */
  1427. /* injected ranks have been set. */
  1428. /* Note: Scan mode is not present by hardware on this device, but used */
  1429. /* by software for alignment over all STM32 devices. */
  1430. if ((hadc->Init.ScanConvMode == ADC_SCAN_DISABLE) ||
  1431. (sConfigInjected->InjectedNbrOfConversion == 1U) )
  1432. {
  1433. /* Configuration of context register JSQR: */
  1434. /* - number of ranks in injected group sequencer: fixed to 1st rank */
  1435. /* (scan mode disabled, only rank 1 used) */
  1436. /* - external trigger to start conversion */
  1437. /* - external trigger polarity */
  1438. /* - channel set to rank 1 (scan mode disabled, only rank 1 can be used) */
  1439. if (sConfigInjected->InjectedRank == ADC_INJECTED_RANK_1)
  1440. {
  1441. /* Enable external trigger if trigger selection is different of */
  1442. /* software start. */
  1443. /* Note: This configuration keeps the hardware feature of parameter */
  1444. /* ExternalTrigInjecConvEdge "trigger edge none" equivalent to */
  1445. /* software start. */
  1446. if (sConfigInjected->ExternalTrigInjecConv != ADC_INJECTED_SOFTWARE_START)
  1447. {
  1448. tmp_JSQR_ContextQueueBeingBuilt = ( ADC_JSQR_RK(sConfigInjected->InjectedChannel, ADC_INJECTED_RANK_1)
  1449. | (sConfigInjected->ExternalTrigInjecConv & ADC_JSQR_JEXTSEL)
  1450. | sConfigInjected->ExternalTrigInjecConvEdge
  1451. );
  1452. }
  1453. else
  1454. {
  1455. tmp_JSQR_ContextQueueBeingBuilt = ( ADC_JSQR_RK(sConfigInjected->InjectedChannel, ADC_INJECTED_RANK_1) );
  1456. }
  1457. MODIFY_REG(hadc->Instance->JSQR, ADC_JSQR_FIELDS, tmp_JSQR_ContextQueueBeingBuilt);
  1458. /* For debug and informative reasons, hadc handle saves JSQR setting */
  1459. hadc->InjectionConfig.ContextQueue = tmp_JSQR_ContextQueueBeingBuilt;
  1460. }
  1461. }
  1462. else
  1463. {
  1464. /* Case of scan mode enabled, several channels to set into injected group */
  1465. /* sequencer. */
  1466. /* */
  1467. /* Procedure to define injected context register JSQR over successive */
  1468. /* calls of this function, for each injected channel rank: */
  1469. /* 1. Start new context and set parameters related to all injected */
  1470. /* channels: injected sequence length and trigger. */
  1471. /* if hadc->InjectionConfig.ChannelCount is equal to 0, this is the first */
  1472. /* call of the context under setting */
  1473. if (hadc->InjectionConfig.ChannelCount == 0U)
  1474. {
  1475. /* Initialize number of channels that will be configured on the context */
  1476. /* being built */
  1477. hadc->InjectionConfig.ChannelCount = sConfigInjected->InjectedNbrOfConversion;
  1478. /* Handle hadc saves the context under build up over each HAL_ADCEx_InjectedConfigChannel()
  1479. call, this context will be written in JSQR register at the last call.
  1480. At this point, the context is merely reset */
  1481. hadc->InjectionConfig.ContextQueue = 0x00000000U;
  1482. /* Configuration of context register JSQR: */
  1483. /* - number of ranks in injected group sequencer */
  1484. /* - external trigger to start conversion */
  1485. /* - external trigger polarity */
  1486. /* Enable external trigger if trigger selection is different of */
  1487. /* software start. */
  1488. /* Note: This configuration keeps the hardware feature of parameter */
  1489. /* ExternalTrigInjecConvEdge "trigger edge none" equivalent to */
  1490. /* software start. */
  1491. if (sConfigInjected->ExternalTrigInjecConv != ADC_INJECTED_SOFTWARE_START)
  1492. {
  1493. tmp_JSQR_ContextQueueBeingBuilt = ( (sConfigInjected->InjectedNbrOfConversion - 1U)
  1494. | (sConfigInjected->ExternalTrigInjecConv & ADC_JSQR_JEXTSEL)
  1495. | sConfigInjected->ExternalTrigInjecConvEdge
  1496. );
  1497. }
  1498. else
  1499. {
  1500. tmp_JSQR_ContextQueueBeingBuilt = ((sConfigInjected->InjectedNbrOfConversion - 1U) );
  1501. }
  1502. }
  1503. /* 2. Continue setting of context under definition with parameter */
  1504. /* related to each channel: channel rank sequence */
  1505. /* Clear the old JSQx bits for the selected rank */
  1506. tmp_JSQR_ContextQueueBeingBuilt &= ~ADC_JSQR_RK(ADC_SQR3_SQ10, sConfigInjected->InjectedRank);
  1507. /* Set the JSQx bits for the selected rank */
  1508. tmp_JSQR_ContextQueueBeingBuilt |= ADC_JSQR_RK(sConfigInjected->InjectedChannel, sConfigInjected->InjectedRank);
  1509. /* Decrease channel count */
  1510. hadc->InjectionConfig.ChannelCount--;
  1511. /* 3. tmp_JSQR_ContextQueueBeingBuilt is fully built for this HAL_ADCEx_InjectedConfigChannel()
  1512. call, aggregate the setting to those already built during the previous
  1513. HAL_ADCEx_InjectedConfigChannel() calls (for the same context of course) */
  1514. hadc->InjectionConfig.ContextQueue |= tmp_JSQR_ContextQueueBeingBuilt;
  1515. /* 4. End of context setting: if this is the last channel set, then write context
  1516. into register JSQR and make it enter into queue */
  1517. if (hadc->InjectionConfig.ChannelCount == 0U)
  1518. {
  1519. MODIFY_REG(hadc->Instance->JSQR, ADC_JSQR_FIELDS, hadc->InjectionConfig.ContextQueue);
  1520. }
  1521. }
  1522. /* Parameters update conditioned to ADC state: */
  1523. /* Parameters that can be updated when ADC is disabled or enabled without */
  1524. /* conversion on going on injected group: */
  1525. /* - Injected context queue: Queue disable (active context is kept) or */
  1526. /* enable (context decremented, up to 2 contexts queued) */
  1527. /* - Injected discontinuous mode: can be enabled only if auto-injected */
  1528. /* mode is disabled. */
  1529. if (ADC_IS_CONVERSION_ONGOING_INJECTED(hadc) == RESET)
  1530. {
  1531. /* If auto-injected mode is disabled: no constraint */
  1532. if (sConfigInjected->AutoInjectedConv == DISABLE)
  1533. {
  1534. MODIFY_REG(hadc->Instance->CFGR, ADC_CFGR_JQM | ADC_CFGR_JDISCEN,
  1535. ADC_CFGR_INJECT_CONTEXT_QUEUE(sConfigInjected->QueueInjectedContext) |
  1536. ADC_CFGR_INJECT_DISCCONTINUOUS(sConfigInjected->InjectedDiscontinuousConvMode) );
  1537. }
  1538. /* If auto-injected mode is enabled: Injected discontinuous setting is */
  1539. /* discarded. */
  1540. else
  1541. {
  1542. MODIFY_REG(hadc->Instance->CFGR, ADC_CFGR_JQM | ADC_CFGR_JDISCEN,
  1543. ADC_CFGR_INJECT_CONTEXT_QUEUE(sConfigInjected->QueueInjectedContext) );
  1544. }
  1545. }
  1546. /* Parameters update conditioned to ADC state: */
  1547. /* Parameters that can be updated when ADC is disabled or enabled without */
  1548. /* conversion on going on regular and injected groups: */
  1549. /* - Automatic injected conversion: can be enabled if injected group */
  1550. /* external triggers are disabled. */
  1551. /* - Channel sampling time */
  1552. /* - Channel offset */
  1553. if (ADC_IS_CONVERSION_ONGOING_REGULAR_INJECTED(hadc) == RESET)
  1554. {
  1555. /* If injected group external triggers are disabled (set to injected */
  1556. /* software start): no constraint */
  1557. if ((sConfigInjected->ExternalTrigInjecConv == ADC_INJECTED_SOFTWARE_START)
  1558. || (sConfigInjected->ExternalTrigInjecConvEdge == ADC_EXTERNALTRIGINJECCONV_EDGE_NONE))
  1559. {
  1560. if (sConfigInjected->AutoInjectedConv == ENABLE)
  1561. {
  1562. SET_BIT(hadc->Instance->CFGR, ADC_CFGR_JAUTO);
  1563. }
  1564. else
  1565. {
  1566. CLEAR_BIT(hadc->Instance->CFGR, ADC_CFGR_JAUTO);
  1567. }
  1568. }
  1569. /* If Automatic injected conversion was intended to be set and could not */
  1570. /* due to injected group external triggers enabled, error is reported. */
  1571. else
  1572. {
  1573. if (sConfigInjected->AutoInjectedConv == ENABLE)
  1574. {
  1575. /* Update ADC state machine to error */
  1576. SET_BIT(hadc->State, HAL_ADC_STATE_ERROR_CONFIG);
  1577. tmp_hal_status = HAL_ERROR;
  1578. }
  1579. else
  1580. {
  1581. CLEAR_BIT(hadc->Instance->CFGR, ADC_CFGR_JAUTO);
  1582. }
  1583. }
  1584. if (sConfigInjected->InjecOversamplingMode == ENABLE)
  1585. {
  1586. assert_param(IS_ADC_OVERSAMPLING_RATIO(sConfigInjected->InjecOversampling.Ratio));
  1587. assert_param(IS_ADC_RIGHT_BIT_SHIFT(sConfigInjected->InjecOversampling.RightBitShift));
  1588. /* JOVSE must be reset in case of triggered regular mode */
  1589. assert_param(!(READ_BIT(hadc->Instance->CFGR2, ADC_CFGR2_ROVSE|ADC_CFGR2_TROVS) == (ADC_CFGR2_ROVSE|ADC_CFGR2_TROVS)));
  1590. /* Configuration of Injected Oversampler: */
  1591. /* - Oversampling Ratio */
  1592. /* - Right bit shift */
  1593. /* Enable OverSampling mode */
  1594. MODIFY_REG(hadc->Instance->CFGR2,
  1595. ADC_CFGR2_JOVSE |
  1596. ADC_CFGR2_OVSR |
  1597. ADC_CFGR2_OVSS,
  1598. ADC_CFGR2_JOVSE |
  1599. sConfigInjected->InjecOversampling.Ratio |
  1600. sConfigInjected->InjecOversampling.RightBitShift
  1601. );
  1602. }
  1603. else
  1604. {
  1605. /* Disable Regular OverSampling */
  1606. CLEAR_BIT( hadc->Instance->CFGR2, ADC_CFGR2_JOVSE);
  1607. }
  1608. #if defined(ADC_SMPR1_SMPPLUS)
  1609. /* Manage specific case of sampling time 3.5 cycles replacing 2.5 cyles */
  1610. if(sConfigInjected->InjectedSamplingTime == ADC_SAMPLETIME_3CYCLES_5)
  1611. {
  1612. /* Set sampling time of the selected ADC channel */
  1613. LL_ADC_SetChannelSamplingTime(hadc->Instance, sConfigInjected->InjectedChannel, LL_ADC_SAMPLINGTIME_2CYCLES_5);
  1614. /* Set ADC sampling time common configuration */
  1615. LL_ADC_SetSamplingTimeCommonConfig(hadc->Instance, LL_ADC_SAMPLINGTIME_COMMON_3C5_REPL_2C5);
  1616. }
  1617. else
  1618. {
  1619. /* Set sampling time of the selected ADC channel */
  1620. LL_ADC_SetChannelSamplingTime(hadc->Instance, sConfigInjected->InjectedChannel, sConfigInjected->InjectedSamplingTime);
  1621. /* Set ADC sampling time common configuration */
  1622. LL_ADC_SetSamplingTimeCommonConfig(hadc->Instance, LL_ADC_SAMPLINGTIME_COMMON_DEFAULT);
  1623. }
  1624. #else
  1625. /* Set sampling time of the selected ADC channel */
  1626. LL_ADC_SetChannelSamplingTime(hadc->Instance, sConfigInjected->InjectedChannel, sConfigInjected->InjectedSamplingTime);
  1627. #endif
  1628. /* Configure the offset: offset enable/disable, channel, offset value */
  1629. /* Shift the offset with respect to the selected ADC resolution. */
  1630. /* Offset has to be left-aligned on bit 11, the LSB (right bits) are set to 0 */
  1631. tmpOffsetShifted = ADC_OFFSET_SHIFT_RESOLUTION(hadc, sConfigInjected->InjectedOffset);
  1632. if(sConfigInjected->InjectedOffsetNumber != ADC_OFFSET_NONE)
  1633. {
  1634. /* Set ADC selected offset number */
  1635. LL_ADC_SetOffset(hadc->Instance, sConfigInjected->InjectedOffsetNumber, sConfigInjected->InjectedChannel, tmpOffsetShifted);
  1636. }
  1637. else
  1638. {
  1639. /* Scan each offset register to check if the selected channel is targeted. */
  1640. /* If this is the case, the corresponding offset number is disabled. */
  1641. if(__LL_ADC_CHANNEL_TO_DECIMAL_NB(LL_ADC_GetOffsetChannel(hadc->Instance, LL_ADC_OFFSET_1)) == __LL_ADC_CHANNEL_TO_DECIMAL_NB(sConfigInjected->InjectedChannel))
  1642. {
  1643. LL_ADC_SetOffsetState(hadc->Instance, LL_ADC_OFFSET_1, LL_ADC_OFFSET_DISABLE);
  1644. }
  1645. if(__LL_ADC_CHANNEL_TO_DECIMAL_NB(LL_ADC_GetOffsetChannel(hadc->Instance, LL_ADC_OFFSET_2)) == __LL_ADC_CHANNEL_TO_DECIMAL_NB(sConfigInjected->InjectedChannel))
  1646. {
  1647. LL_ADC_SetOffsetState(hadc->Instance, LL_ADC_OFFSET_2, LL_ADC_OFFSET_DISABLE);
  1648. }
  1649. if(__LL_ADC_CHANNEL_TO_DECIMAL_NB(LL_ADC_GetOffsetChannel(hadc->Instance, LL_ADC_OFFSET_3)) == __LL_ADC_CHANNEL_TO_DECIMAL_NB(sConfigInjected->InjectedChannel))
  1650. {
  1651. LL_ADC_SetOffsetState(hadc->Instance, LL_ADC_OFFSET_3, LL_ADC_OFFSET_DISABLE);
  1652. }
  1653. if(__LL_ADC_CHANNEL_TO_DECIMAL_NB(LL_ADC_GetOffsetChannel(hadc->Instance, LL_ADC_OFFSET_4)) == __LL_ADC_CHANNEL_TO_DECIMAL_NB(sConfigInjected->InjectedChannel))
  1654. {
  1655. LL_ADC_SetOffsetState(hadc->Instance, LL_ADC_OFFSET_4, LL_ADC_OFFSET_DISABLE);
  1656. }
  1657. }
  1658. }
  1659. /* Parameters update conditioned to ADC state: */
  1660. /* Parameters that can be updated only when ADC is disabled: */
  1661. /* - Single or differential mode */
  1662. /* - Internal measurement channels: Vbat/VrefInt/TempSensor */
  1663. if (ADC_IS_ENABLE(hadc) == RESET)
  1664. {
  1665. /* Set mode single-ended or differential input of the selected ADC channel */
  1666. LL_ADC_SetChannelSingleDiff(hadc->Instance, sConfigInjected->InjectedChannel, sConfigInjected->InjectedSingleDiff);
  1667. /* Configuration of differential mode */
  1668. if (sConfigInjected->InjectedSingleDiff == ADC_DIFFERENTIAL_ENDED)
  1669. {
  1670. /* Set sampling time of the selected ADC channel */
  1671. LL_ADC_SetChannelSamplingTime(hadc->Instance, __LL_ADC_DECIMAL_NB_TO_CHANNEL(__LL_ADC_CHANNEL_TO_DECIMAL_NB(sConfigInjected->InjectedChannel) + 1), sConfigInjected->InjectedSamplingTime);
  1672. }
  1673. /* Management of internal measurement channels: Vbat/VrefInt/TempSensor */
  1674. /* internal measurement paths enable: If internal channel selected, */
  1675. /* enable dedicated internal buffers and path. */
  1676. /* Note: these internal measurement paths can be disabled using */
  1677. /* HAL_ADC_DeInit(). */
  1678. /* Configuration of common ADC parameters */
  1679. /* If the requested internal measurement path has already been enabled, */
  1680. /* bypass the configuration processing. */
  1681. if (( (sConfigInjected->InjectedChannel == ADC_CHANNEL_TEMPSENSOR) &&
  1682. ((LL_ADC_GetCommonPathInternalCh(__LL_ADC_COMMON_INSTANCE(hadc->Instance)) & LL_ADC_PATH_INTERNAL_TEMPSENSOR) == 0U)) ||
  1683. ( (sConfigInjected->InjectedChannel == ADC_CHANNEL_VBAT) &&
  1684. ((LL_ADC_GetCommonPathInternalCh(__LL_ADC_COMMON_INSTANCE(hadc->Instance)) & LL_ADC_PATH_INTERNAL_VBAT) == 0U)) ||
  1685. ( (sConfigInjected->InjectedChannel == ADC_CHANNEL_VREFINT) &&
  1686. ((LL_ADC_GetCommonPathInternalCh(__LL_ADC_COMMON_INSTANCE(hadc->Instance)) & LL_ADC_PATH_INTERNAL_VREFINT) == 0U))
  1687. )
  1688. {
  1689. /* Configuration of common ADC parameters (continuation) */
  1690. /* Software is allowed to change common parameters only when all ADCs */
  1691. /* of the common group are disabled. */
  1692. if ((ADC_IS_ENABLE(hadc) == RESET) &&
  1693. (ADC_ANY_OTHER_ENABLED(hadc) == RESET) )
  1694. {
  1695. if (sConfigInjected->InjectedChannel == ADC_CHANNEL_TEMPSENSOR)
  1696. {
  1697. if (ADC_TEMPERATURE_SENSOR_INSTANCE(hadc))
  1698. {
  1699. LL_ADC_SetCommonPathInternalCh(__LL_ADC_COMMON_INSTANCE(hadc->Instance), LL_ADC_PATH_INTERNAL_TEMPSENSOR | LL_ADC_GetCommonPathInternalCh(__LL_ADC_COMMON_INSTANCE(hadc->Instance)));
  1700. /* Delay for temperature sensor stabilization time */
  1701. /* Compute number of CPU cycles to wait for */
  1702. wait_loop_index = (LL_ADC_DELAY_TEMPSENSOR_STAB_US * (SystemCoreClock / 1000000));
  1703. while(wait_loop_index != 0)
  1704. {
  1705. wait_loop_index--;
  1706. }
  1707. }
  1708. }
  1709. else if (sConfigInjected->InjectedChannel == ADC_CHANNEL_VBAT)
  1710. {
  1711. if (ADC_BATTERY_VOLTAGE_INSTANCE(hadc))
  1712. {
  1713. LL_ADC_SetCommonPathInternalCh(__LL_ADC_COMMON_INSTANCE(hadc->Instance), LL_ADC_PATH_INTERNAL_VBAT | LL_ADC_GetCommonPathInternalCh(__LL_ADC_COMMON_INSTANCE(hadc->Instance)));
  1714. }
  1715. }
  1716. else if (sConfigInjected->InjectedChannel == ADC_CHANNEL_VREFINT)
  1717. {
  1718. if (ADC_VREFINT_INSTANCE(hadc))
  1719. {
  1720. LL_ADC_SetCommonPathInternalCh(__LL_ADC_COMMON_INSTANCE(hadc->Instance), LL_ADC_PATH_INTERNAL_VREFINT | LL_ADC_GetCommonPathInternalCh(__LL_ADC_COMMON_INSTANCE(hadc->Instance)));
  1721. }
  1722. }
  1723. }
  1724. /* If the requested internal measurement path has already been enabled */
  1725. /* and other ADC of the common group are enabled, internal */
  1726. /* measurement paths cannot be enabled. */
  1727. else
  1728. {
  1729. /* Update ADC state machine to error */
  1730. SET_BIT(hadc->State, HAL_ADC_STATE_ERROR_CONFIG);
  1731. tmp_hal_status = HAL_ERROR;
  1732. }
  1733. }
  1734. }
  1735. /* Process unlocked */
  1736. __HAL_UNLOCK(hadc);
  1737. /* Return function status */
  1738. return tmp_hal_status;
  1739. }
  1740. #if defined(ADC_MULTIMODE_SUPPORT)
  1741. /**
  1742. * @brief Enable ADC multimode and configure multimode parameters
  1743. * @note Possibility to update parameters on the fly:
  1744. * This function initializes multimode parameters, following
  1745. * calls to this function can be used to reconfigure some parameters
  1746. * of structure "ADC_MultiModeTypeDef" on the fly, without resetting
  1747. * the ADCs.
  1748. * The setting of these parameters is conditioned to ADC state.
  1749. * For parameters constraints, see comments of structure
  1750. * "ADC_MultiModeTypeDef".
  1751. * @note To move back configuration from multimode to single mode, ADC must
  1752. * be reset (using function HAL_ADC_Init() ).
  1753. * @param hadc Master ADC handle
  1754. * @param multimode Structure of ADC multimode configuration
  1755. * @retval HAL status
  1756. */
  1757. HAL_StatusTypeDef HAL_ADCEx_MultiModeConfigChannel(ADC_HandleTypeDef* hadc, ADC_MultiModeTypeDef* multimode)
  1758. {
  1759. HAL_StatusTypeDef tmp_hal_status = HAL_OK;
  1760. ADC_Common_TypeDef *tmpADC_Common;
  1761. ADC_HandleTypeDef tmphadcSlave;
  1762. /* Check the parameters */
  1763. assert_param(IS_ADC_MULTIMODE_MASTER_INSTANCE(hadc->Instance));
  1764. assert_param(IS_ADC_MULTIMODE(multimode->Mode));
  1765. if(multimode->Mode != ADC_MODE_INDEPENDENT)
  1766. {
  1767. assert_param(IS_ADC_DMA_ACCESS_MULTIMODE(multimode->DMAAccessMode));
  1768. assert_param(IS_ADC_SAMPLING_DELAY(multimode->TwoSamplingDelay));
  1769. }
  1770. /* Process locked */
  1771. __HAL_LOCK(hadc);
  1772. ADC_MULTI_SLAVE(hadc, &tmphadcSlave);
  1773. if (tmphadcSlave.Instance == NULL)
  1774. {
  1775. /* Update ADC state machine to error */
  1776. SET_BIT(hadc->State, HAL_ADC_STATE_ERROR_CONFIG);
  1777. /* Process unlocked */
  1778. __HAL_UNLOCK(hadc);
  1779. return HAL_ERROR;
  1780. }
  1781. /* Parameters update conditioned to ADC state: */
  1782. /* Parameters that can be updated when ADC is disabled or enabled without */
  1783. /* conversion on going on regular group: */
  1784. /* - Multimode DMA configuration */
  1785. /* - Multimode DMA mode */
  1786. if ( (ADC_IS_CONVERSION_ONGOING_REGULAR(hadc) == RESET)
  1787. && (ADC_IS_CONVERSION_ONGOING_REGULAR(&tmphadcSlave) == RESET) )
  1788. {
  1789. /* Pointer to the common control register */
  1790. tmpADC_Common = __LL_ADC_COMMON_INSTANCE(hadc->Instance);
  1791. /* If multimode is selected, configure all multimode paramaters. */
  1792. /* Otherwise, reset multimode parameters (can be used in case of */
  1793. /* transition from multimode to independent mode). */
  1794. if(multimode->Mode != ADC_MODE_INDEPENDENT)
  1795. {
  1796. MODIFY_REG(tmpADC_Common->CCR, ADC_CCR_MDMA | ADC_CCR_DMACFG,
  1797. multimode->DMAAccessMode |
  1798. ADC_CCR_MULTI_DMACONTREQ(hadc->Init.DMAContinuousRequests));
  1799. /* Parameters that can be updated only when ADC is disabled: */
  1800. /* - Multimode mode selection */
  1801. /* - Multimode delay */
  1802. /* Note: Delay range depends on selected resolution: */
  1803. /* from 1 to 12 clock cycles for 12 bits */
  1804. /* from 1 to 10 clock cycles for 10 bits, */
  1805. /* from 1 to 8 clock cycles for 8 bits */
  1806. /* from 1 to 6 clock cycles for 6 bits */
  1807. /* If a higher delay is selected, it will be clipped to maximum delay */
  1808. /* range */
  1809. if ((ADC_IS_ENABLE(hadc) == RESET) &&
  1810. (ADC_IS_ENABLE(&tmphadcSlave) == RESET) )
  1811. {
  1812. MODIFY_REG(tmpADC_Common->CCR,
  1813. ADC_CCR_DUAL |
  1814. ADC_CCR_DELAY,
  1815. multimode->Mode |
  1816. multimode->TwoSamplingDelay
  1817. );
  1818. }
  1819. }
  1820. else /* ADC_MODE_INDEPENDENT */
  1821. {
  1822. CLEAR_BIT(tmpADC_Common->CCR, ADC_CCR_MDMA | ADC_CCR_DMACFG);
  1823. /* Parameters that can be updated only when ADC is disabled: */
  1824. /* - Multimode mode selection */
  1825. /* - Multimode delay */
  1826. if ((ADC_IS_ENABLE(hadc) == RESET) &&
  1827. (ADC_IS_ENABLE(&tmphadcSlave) == RESET) )
  1828. {
  1829. CLEAR_BIT(tmpADC_Common->CCR, ADC_CCR_DUAL | ADC_CCR_DELAY);
  1830. }
  1831. }
  1832. }
  1833. /* If one of the ADC sharing the same common group is enabled, no update */
  1834. /* could be done on neither of the multimode structure parameters. */
  1835. else
  1836. {
  1837. /* Update ADC state machine to error */
  1838. SET_BIT(hadc->State, HAL_ADC_STATE_ERROR_CONFIG);
  1839. tmp_hal_status = HAL_ERROR;
  1840. }
  1841. /* Process unlocked */
  1842. __HAL_UNLOCK(hadc);
  1843. /* Return function status */
  1844. return tmp_hal_status;
  1845. }
  1846. #endif /* ADC_MULTIMODE_SUPPORT */
  1847. /**
  1848. * @brief Enable Injected Queue
  1849. * @note This function resets CFGR register JQDIS bit in order to enable the
  1850. * Injected Queue. JQDIS can be written only when ADSTART and JDSTART
  1851. * are both equal to 0 to ensure that no regular nor injected
  1852. * conversion is ongoing.
  1853. * @param hadc ADC handle
  1854. * @retval HAL status
  1855. */
  1856. HAL_StatusTypeDef HAL_ADCEx_EnableInjectedQueue(ADC_HandleTypeDef* hadc)
  1857. {
  1858. /* Parameter can be set only if no conversion is on-going */
  1859. if (ADC_IS_CONVERSION_ONGOING_REGULAR_INJECTED(hadc) == RESET)
  1860. {
  1861. CLEAR_BIT(hadc->Instance->CFGR, ADC_CFGR_JQDIS);
  1862. /* Update state, clear previous result related to injected queue overflow */
  1863. CLEAR_BIT(hadc->State, HAL_ADC_STATE_INJ_JQOVF);
  1864. return HAL_OK;
  1865. }
  1866. else
  1867. {
  1868. return HAL_ERROR;
  1869. }
  1870. }
  1871. /**
  1872. * @brief Disable Injected Queue
  1873. * @note This function sets CFGR register JQDIS bit in order to disable the
  1874. * Injected Queue. JQDIS can be written only when ADSTART and JDSTART
  1875. * are both equal to 0 to ensure that no regular nor injected
  1876. * conversion is ongoing.
  1877. * @param hadc ADC handle
  1878. * @retval HAL status
  1879. */
  1880. HAL_StatusTypeDef HAL_ADCEx_DisableInjectedQueue(ADC_HandleTypeDef* hadc)
  1881. {
  1882. /* Parameter can be set only if no conversion is on-going */
  1883. if (ADC_IS_CONVERSION_ONGOING_REGULAR_INJECTED(hadc) == RESET)
  1884. {
  1885. SET_BIT(hadc->Instance->CFGR, ADC_CFGR_JQDIS);
  1886. return HAL_OK;
  1887. }
  1888. else
  1889. {
  1890. return HAL_ERROR;
  1891. }
  1892. }
  1893. /**
  1894. * @brief Disable ADC voltage regulator.
  1895. * @note Disabling voltage regulator allows to save power. This operation can
  1896. * be carried out only when ADC is disabled.
  1897. * @note To enable again the voltage regulator, the user is expected to
  1898. * resort to HAL_ADC_Init() API.
  1899. * @param hadc ADC handle
  1900. * @retval HAL status
  1901. */
  1902. HAL_StatusTypeDef HAL_ADCEx_DisableVoltageRegulator(ADC_HandleTypeDef* hadc)
  1903. {
  1904. /* ADVREGEN can be written only when the ADC is disabled */
  1905. if (ADC_IS_ENABLE(hadc) == RESET)
  1906. {
  1907. CLEAR_BIT(hadc->Instance->CR, ADC_CR_ADVREGEN);
  1908. return HAL_OK;
  1909. }
  1910. else
  1911. {
  1912. return HAL_ERROR;
  1913. }
  1914. }
  1915. /**
  1916. * @brief Enter ADC deep-power-down mode
  1917. * @note This mode is achieved in setting DEEPPWD bit and allows to save power
  1918. * in reducing leakage currents. It is particularly interesting before
  1919. * entering stop modes.
  1920. * @note Setting DEEPPWD automatically clears ADVREGEN bit and disables the
  1921. * ADC voltage regulator. This means that this API encompasses
  1922. * HAL_ADCEx_DisableVoltageRegulator(). Additionally, the internal
  1923. * calibration is lost.
  1924. * @note To exit the ADC deep-power-down mode, the user is expected to
  1925. * resort to HAL_ADC_Init() API as well as to relaunch a calibration
  1926. * with HAL_ADCEx_Calibration_Start() API or to re-apply a previously
  1927. * saved calibration factor.
  1928. * @param hadc ADC handle
  1929. * @retval HAL status
  1930. */
  1931. HAL_StatusTypeDef HAL_ADCEx_EnterADCDeepPowerDownMode(ADC_HandleTypeDef* hadc)
  1932. {
  1933. /* DEEPPWD can be written only when the ADC is disabled */
  1934. if (ADC_IS_ENABLE(hadc) == RESET)
  1935. {
  1936. SET_BIT(hadc->Instance->CR, ADC_CR_DEEPPWD);
  1937. return HAL_OK;
  1938. }
  1939. else
  1940. {
  1941. return HAL_ERROR;
  1942. }
  1943. }
  1944. /**
  1945. * @}
  1946. */
  1947. /**
  1948. * @}
  1949. */
  1950. #endif /* HAL_ADC_MODULE_ENABLED */
  1951. /**
  1952. * @}
  1953. */
  1954. /**
  1955. * @}
  1956. */
  1957. /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/