stm32g4xx_hal_adc_ex.c 92 KB

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