stm32wbxx_hal_adc_ex.c 70 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826
  1. /**
  2. ******************************************************************************
  3. * @file stm32wbxx_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. * + Operation functions
  9. * ++ Start, stop, get result of conversions of ADC group injected,
  10. * using 2 possible modes: polling, interruption (not available on devices: STM32WB10xx, STM32WB15xx, STM32WB1Mxx ).
  11. * ++ Calibration
  12. * +++ ADC automatic self-calibration
  13. * +++ Calibration factors get or set
  14. * + Control functions
  15. * ++ Channels configuration on ADC group injected (not available on devices: STM32WB10xx, STM32WB15xx, STM32WB1Mxx )
  16. * + State functions
  17. * ++ ADC group injected contexts queue management (not available on devices: STM32WB10xx, STM32WB15xx, STM32WB1Mxx )
  18. * Other functions (generic functions) are available in file
  19. * "stm32wbxx_hal_adc.c".
  20. ******************************************************************************
  21. * @attention
  22. *
  23. * Copyright (c) 2019 STMicroelectronics.
  24. * All rights reserved.
  25. *
  26. * This software is licensed under terms that can be found in the LICENSE file
  27. * in the root directory of this software component.
  28. * If no LICENSE file comes with this software, it is provided AS-IS.
  29. *
  30. ******************************************************************************
  31. @verbatim
  32. [..]
  33. (@) Sections "ADC peripheral features" and "How to use this driver" are
  34. available in file of generic functions "stm32wbxx_hal_adc.c".
  35. [..]
  36. @endverbatim
  37. ******************************************************************************
  38. */
  39. /* Includes ------------------------------------------------------------------*/
  40. #include "stm32wbxx_hal.h"
  41. /** @addtogroup STM32WBxx_HAL_Driver
  42. * @{
  43. */
  44. /** @defgroup ADCEx ADCEx
  45. * @brief ADC Extended HAL module driver
  46. * @{
  47. */
  48. #ifdef HAL_ADC_MODULE_ENABLED
  49. /* Private typedef -----------------------------------------------------------*/
  50. /* Private define ------------------------------------------------------------*/
  51. /** @defgroup ADCEx_Private_Constants ADC Extended Private Constants
  52. * @{
  53. */
  54. #define ADC_JSQR_FIELDS ((ADC_JSQR_JL | ADC_JSQR_JEXTSEL | ADC_JSQR_JEXTEN |\
  55. ADC_JSQR_JSQ1 | ADC_JSQR_JSQ2 |\
  56. ADC_JSQR_JSQ3 | ADC_JSQR_JSQ4 )) /*!< ADC_JSQR fields of parameters that can be updated anytime
  57. once the ADC is enabled */
  58. /* Fixed timeout value for ADC calibration. */
  59. /* Values defined to be higher than worst cases: maximum ratio between ADC */
  60. /* and CPU clock frequencies. */
  61. /* Example of profile low frequency : ADC frequency at 46.9kHz (ADC clock */
  62. /* source PLL SAI 12MHz, ADC clock prescaler 256), CPU frequency 64MHz. */
  63. /* Calibration time max = 116 / fADC (refer to datasheet) */
  64. /* = 158 379 CPU cycles */
  65. #define ADC_CALIBRATION_TIMEOUT (158379UL) /*!< ADC calibration time-out value (unit: CPU cycles) */
  66. #define ADC_DISABLE_TIMEOUT (2UL)
  67. /**
  68. * @}
  69. */
  70. /* Private macro -------------------------------------------------------------*/
  71. /* Private variables ---------------------------------------------------------*/
  72. /* Private function prototypes -----------------------------------------------*/
  73. /* Exported functions --------------------------------------------------------*/
  74. /** @defgroup ADCEx_Exported_Functions ADC Extended Exported Functions
  75. * @{
  76. */
  77. /** @defgroup ADCEx_Exported_Functions_Group1 Extended Input and Output operation functions
  78. * @brief Extended IO operation functions
  79. *
  80. @verbatim
  81. ===============================================================================
  82. ##### IO operation functions #####
  83. ===============================================================================
  84. [..] This section provides functions allowing to:
  85. (+) Perform the ADC self-calibration for single or differential ending.
  86. (+) Get calibration factors for single or differential ending.
  87. (+) Set calibration factors for single or differential ending.
  88. (+) Start conversion of ADC group injected (not available on devices: STM32WB10xx, STM32WB15xx, STM32WB1Mxx ).
  89. (+) Stop conversion of ADC group injected (not available on devices: STM32WB10xx, STM32WB15xx, STM32WB1Mxx ).
  90. (+) Poll for conversion complete on ADC group injected (not available on devices: STM32WB10xx, STM32WB15xx, STM32WB1Mxx ).
  91. (+) Get result of ADC group injected channel conversion (not available on devices: STM32WB10xx, STM32WB15xx, STM32WB1Mxx ).
  92. (+) Start conversion of ADC group injected and enable interruptions (not available on devices: STM32WB10xx, STM32WB15xx, STM32WB1Mxx ).
  93. (+) Stop conversion of ADC group injected and disable interruptions (not available on devices: STM32WB10xx, STM32WB15xx, STM32WB1Mxx ).
  94. @endverbatim
  95. * @{
  96. */
  97. /**
  98. * @brief Perform an ADC automatic self-calibration
  99. * Calibration prerequisite: ADC must be disabled (execute this
  100. * function before HAL_ADC_Start() or after HAL_ADC_Stop() ).
  101. * @param hadc ADC handle
  102. * @param SingleDiff Selection of single-ended or differential input
  103. * This parameter can be one of the following values:
  104. * @arg @ref ADC_SINGLE_ENDED Channel in mode input single ended
  105. * @arg @ref ADC_DIFFERENTIAL_ENDED Channel in mode input differential ended (1)
  106. *
  107. * (1) On STM32WB series, parameter not available on devices: STM32WB10xx, STM32WB15xx, STM32WB1Mxx.
  108. * @retval HAL status
  109. */
  110. HAL_StatusTypeDef HAL_ADCEx_Calibration_Start(ADC_HandleTypeDef *hadc, uint32_t SingleDiff)
  111. {
  112. #if defined(ADC_SUPPORT_2_5_MSPS)
  113. UNUSED(SingleDiff);
  114. uint32_t calibration_index;
  115. uint32_t calibration_factor_accumulated = 0;
  116. uint32_t backup_setting_cfgr1;
  117. uint32_t tickstart;
  118. uint32_t adc_clk_async_presc;
  119. __IO uint32_t delay_cpu_cycles;
  120. #endif /* ADC_SUPPORT_2_5_MSPS */
  121. HAL_StatusTypeDef tmp_hal_status;
  122. __IO uint32_t wait_loop_index = 0UL;
  123. /* Check the parameters */
  124. assert_param(IS_ADC_ALL_INSTANCE(hadc->Instance));
  125. assert_param(IS_ADC_SINGLE_DIFFERENTIAL(SingleDiff));
  126. /* Process locked */
  127. __HAL_LOCK(hadc);
  128. /* Calibration prerequisite: ADC must be disabled. */
  129. /* Disable the ADC (if not already disabled) */
  130. tmp_hal_status = ADC_Disable(hadc);
  131. /* Check if ADC is effectively disabled */
  132. if (tmp_hal_status == HAL_OK)
  133. {
  134. /* Set ADC state */
  135. #if defined(ADC_SUPPORT_2_5_MSPS)
  136. ADC_STATE_CLR_SET(hadc->State,
  137. HAL_ADC_STATE_REG_BUSY,
  138. HAL_ADC_STATE_BUSY_INTERNAL);
  139. #else
  140. ADC_STATE_CLR_SET(hadc->State,
  141. HAL_ADC_STATE_REG_BUSY | HAL_ADC_STATE_INJ_BUSY,
  142. HAL_ADC_STATE_BUSY_INTERNAL);
  143. #endif /* ADC_SUPPORT_2_5_MSPS */
  144. /* Start ADC calibration in mode single-ended or differential */
  145. #if defined(ADC_SUPPORT_2_5_MSPS)
  146. /* Manage settings impacting calibration */
  147. /* - Disable ADC mode auto power-off */
  148. /* - Disable ADC DMA transfer request during calibration */
  149. /* Note: Specificity of this STM32 series: Calibration factor is */
  150. /* available in data register and also transferred by DMA. */
  151. /* To not insert ADC calibration factor among ADC conversion data */
  152. /* in array variable, DMA transfer must be disabled during */
  153. /* calibration. */
  154. backup_setting_cfgr1 = READ_BIT(hadc->Instance->CFGR1, ADC_CFGR1_DMAEN | ADC_CFGR1_DMACFG | ADC_CFGR1_AUTOFF);
  155. CLEAR_BIT(hadc->Instance->CFGR1, ADC_CFGR1_DMAEN | ADC_CFGR1_DMACFG | ADC_CFGR1_AUTOFF);
  156. /* ADC calibration procedure */
  157. /* Note: Perform an averaging of 8 calibrations for optimized accuracy */
  158. for (calibration_index = 0UL; calibration_index < 8UL; calibration_index++)
  159. {
  160. /* Start ADC calibration */
  161. LL_ADC_StartCalibration(hadc->Instance);
  162. #else
  163. LL_ADC_StartCalibration(hadc->Instance, SingleDiff);
  164. #endif /* ADC_SUPPORT_2_5_MSPS */
  165. /* Wait for calibration completion */
  166. while (LL_ADC_IsCalibrationOnGoing(hadc->Instance) != 0UL)
  167. {
  168. wait_loop_index++;
  169. if (wait_loop_index >= ADC_CALIBRATION_TIMEOUT)
  170. {
  171. /* Update ADC state machine to error */
  172. ADC_STATE_CLR_SET(hadc->State,
  173. HAL_ADC_STATE_BUSY_INTERNAL,
  174. HAL_ADC_STATE_ERROR_INTERNAL);
  175. /* Process unlocked */
  176. __HAL_UNLOCK(hadc);
  177. return HAL_ERROR;
  178. }
  179. }
  180. #if defined(ADC_SUPPORT_2_5_MSPS)
  181. calibration_factor_accumulated += LL_ADC_GetCalibrationFactor(hadc->Instance);
  182. }
  183. /* Compute average */
  184. calibration_factor_accumulated /= calibration_index;
  185. /* Apply calibration factor (requires ADC enable and disable process) */
  186. LL_ADC_Enable(hadc->Instance);
  187. /* Case of ADC clocked at low frequency: Delay required between ADC enable and disable actions */
  188. if(LL_ADC_GetClock(hadc->Instance) == LL_ADC_CLOCK_ASYNC)
  189. {
  190. adc_clk_async_presc = LL_ADC_GetCommonClock(__LL_ADC_COMMON_INSTANCE(hadc->Instance));
  191. if(adc_clk_async_presc >= LL_ADC_CLOCK_ASYNC_DIV16)
  192. {
  193. /* Delay loop initialization and execution */
  194. /* Delay depends on ADC clock prescaler: Compute ADC clock asynchronous prescaler to decimal format */
  195. delay_cpu_cycles = (1U << ((adc_clk_async_presc >> ADC_CCR_PRESC_Pos) - 3U));
  196. /* Divide variable by 2 to compensate partially CPU processing cycles */
  197. delay_cpu_cycles >>= 1U;
  198. while(delay_cpu_cycles != 0)
  199. {
  200. delay_cpu_cycles--;
  201. }
  202. }
  203. }
  204. LL_ADC_SetCalibrationFactor(hadc->Instance, calibration_factor_accumulated);
  205. LL_ADC_Disable(hadc->Instance);
  206. /* Wait for ADC effectively disabled before changing configuration */
  207. /* Get tick count */
  208. tickstart = HAL_GetTick();
  209. while (LL_ADC_IsEnabled(hadc->Instance) != 0UL)
  210. {
  211. if ((HAL_GetTick() - tickstart) > ADC_DISABLE_TIMEOUT)
  212. {
  213. /* New check to avoid false timeout detection in case of preemption */
  214. if (LL_ADC_IsEnabled(hadc->Instance) != 0UL)
  215. {
  216. /* Update ADC state machine to error */
  217. SET_BIT(hadc->State, HAL_ADC_STATE_ERROR_INTERNAL);
  218. /* Set ADC error code to ADC peripheral internal error */
  219. SET_BIT(hadc->ErrorCode, HAL_ADC_ERROR_INTERNAL);
  220. return HAL_ERROR;
  221. }
  222. }
  223. }
  224. /* Restore configuration after calibration */
  225. SET_BIT(hadc->Instance->CFGR1, backup_setting_cfgr1);
  226. #endif /* ADC_SUPPORT_2_5_MSPS */
  227. /* Set ADC state */
  228. ADC_STATE_CLR_SET(hadc->State,
  229. HAL_ADC_STATE_BUSY_INTERNAL,
  230. HAL_ADC_STATE_READY);
  231. }
  232. else
  233. {
  234. SET_BIT(hadc->State, HAL_ADC_STATE_ERROR_INTERNAL);
  235. /* Note: No need to update variable "tmp_hal_status" here: already set */
  236. /* to state "HAL_ERROR" by function disabling the ADC. */
  237. }
  238. /* Process unlocked */
  239. __HAL_UNLOCK(hadc);
  240. /* Return function status */
  241. return tmp_hal_status;
  242. }
  243. /**
  244. * @brief Get the calibration factor.
  245. * @param hadc ADC handle.
  246. * @param SingleDiff This parameter can be only:
  247. * @arg @ref ADC_SINGLE_ENDED Channel in mode input single ended
  248. * @arg @ref ADC_DIFFERENTIAL_ENDED Channel in mode input differential ended (1)
  249. *
  250. * (1) On STM32WB series, parameter not available on devices: STM32WB10xx, STM32WB15xx, STM32WB1Mxx.
  251. * @retval Calibration value.
  252. */
  253. uint32_t HAL_ADCEx_Calibration_GetValue(const ADC_HandleTypeDef *hadc, uint32_t SingleDiff)
  254. {
  255. #if defined(ADC_SUPPORT_2_5_MSPS)
  256. UNUSED(SingleDiff);
  257. #endif /* ADC_SUPPORT_2_5_MSPS */
  258. /* Check the parameters */
  259. assert_param(IS_ADC_ALL_INSTANCE(hadc->Instance));
  260. assert_param(IS_ADC_SINGLE_DIFFERENTIAL(SingleDiff));
  261. /* Return the selected ADC calibration value */
  262. #if defined(ADC_SUPPORT_2_5_MSPS)
  263. return LL_ADC_GetCalibrationFactor(hadc->Instance);
  264. #else
  265. return LL_ADC_GetCalibrationFactor(hadc->Instance, SingleDiff);
  266. #endif /* ADC_SUPPORT_2_5_MSPS */
  267. }
  268. /**
  269. * @brief Set the calibration factor to overwrite automatic conversion result.
  270. * ADC must be enabled and no conversion is ongoing.
  271. * @param hadc ADC handle
  272. * @param SingleDiff This parameter can be only:
  273. * @arg @ref ADC_SINGLE_ENDED Channel in mode input single ended
  274. * @arg @ref ADC_DIFFERENTIAL_ENDED Channel in mode input differential ended (1)
  275. *
  276. * (1) On STM32WB series, parameter not available on devices: STM32WB10xx, STM32WB15xx, STM32WB1Mxx.
  277. * @param CalibrationFactor Calibration factor (coded on 7 bits maximum)
  278. * @retval HAL state
  279. */
  280. HAL_StatusTypeDef HAL_ADCEx_Calibration_SetValue(ADC_HandleTypeDef *hadc, uint32_t SingleDiff, uint32_t CalibrationFactor)
  281. {
  282. #if defined(ADC_SUPPORT_2_5_MSPS)
  283. UNUSED(SingleDiff);
  284. #endif /* ADC_SUPPORT_2_5_MSPS */
  285. HAL_StatusTypeDef tmp_hal_status = HAL_OK;
  286. uint32_t tmp_adc_is_conversion_on_going_regular;
  287. #if defined(ADC_SUPPORT_2_5_MSPS)
  288. /* Feature "ADC group injected" not available on ADC peripheral of this STM32WB device */
  289. #else
  290. uint32_t tmp_adc_is_conversion_on_going_injected;
  291. #endif /* ADC_SUPPORT_2_5_MSPS */
  292. /* Check the parameters */
  293. assert_param(IS_ADC_ALL_INSTANCE(hadc->Instance));
  294. assert_param(IS_ADC_SINGLE_DIFFERENTIAL(SingleDiff));
  295. assert_param(IS_ADC_CALFACT(CalibrationFactor));
  296. /* Process locked */
  297. __HAL_LOCK(hadc);
  298. /* Verification of hardware constraints before modifying the calibration */
  299. /* factors register: ADC must be enabled, no conversion on going. */
  300. tmp_adc_is_conversion_on_going_regular = LL_ADC_REG_IsConversionOngoing(hadc->Instance);
  301. #if defined(ADC_SUPPORT_2_5_MSPS)
  302. /* Feature "ADC group injected" not available on ADC peripheral of this STM32WB device */
  303. #else
  304. tmp_adc_is_conversion_on_going_injected = LL_ADC_INJ_IsConversionOngoing(hadc->Instance);
  305. #endif /* ADC_SUPPORT_2_5_MSPS */
  306. if ((LL_ADC_IsEnabled(hadc->Instance) != 0UL)
  307. && (tmp_adc_is_conversion_on_going_regular == 0UL)
  308. #if defined(ADC_SUPPORT_2_5_MSPS)
  309. /* Feature "ADC group injected" not available on ADC peripheral of this STM32WB device */
  310. #else
  311. && (tmp_adc_is_conversion_on_going_injected == 0UL)
  312. #endif /* ADC_SUPPORT_2_5_MSPS */
  313. )
  314. {
  315. /* Set the selected ADC calibration value */
  316. #if defined(ADC_SUPPORT_2_5_MSPS)
  317. LL_ADC_SetCalibrationFactor(hadc->Instance, CalibrationFactor);
  318. #else
  319. LL_ADC_SetCalibrationFactor(hadc->Instance, SingleDiff, CalibrationFactor);
  320. #endif /* ADC_SUPPORT_2_5_MSPS */
  321. }
  322. else
  323. {
  324. /* Update ADC state machine */
  325. SET_BIT(hadc->State, HAL_ADC_STATE_ERROR_CONFIG);
  326. /* Update ADC error code */
  327. SET_BIT(hadc->ErrorCode, HAL_ADC_ERROR_INTERNAL);
  328. /* Update ADC state machine to error */
  329. tmp_hal_status = HAL_ERROR;
  330. }
  331. /* Process unlocked */
  332. __HAL_UNLOCK(hadc);
  333. /* Return function status */
  334. return tmp_hal_status;
  335. }
  336. #if defined(ADC_SUPPORT_2_5_MSPS)
  337. /* Feature "ADC group injected" not available on ADC peripheral of this STM32WB device */
  338. #else
  339. /**
  340. * @brief Enable ADC, start conversion of injected group.
  341. * @note Interruptions enabled in this function: None.
  342. * @param hadc ADC handle.
  343. * @retval HAL status
  344. */
  345. HAL_StatusTypeDef HAL_ADCEx_InjectedStart(ADC_HandleTypeDef *hadc)
  346. {
  347. HAL_StatusTypeDef tmp_hal_status;
  348. uint32_t tmp_config_injected_queue;
  349. /* Check the parameters */
  350. assert_param(IS_ADC_ALL_INSTANCE(hadc->Instance));
  351. if (LL_ADC_INJ_IsConversionOngoing(hadc->Instance) != 0UL)
  352. {
  353. return HAL_BUSY;
  354. }
  355. else
  356. {
  357. /* In case of software trigger detection enabled, JQDIS must be set
  358. (which can be done only if ADSTART and JADSTART are both cleared).
  359. If JQDIS is not set at that point, returns an error
  360. - since software trigger detection is disabled. User needs to
  361. resort to HAL_ADCEx_DisableInjectedQueue() API to set JQDIS.
  362. - or (if JQDIS is intentionally reset) since JEXTEN = 0 which means
  363. the queue is empty */
  364. tmp_config_injected_queue = READ_BIT(hadc->Instance->CFGR, ADC_CFGR_JQDIS);
  365. if ((READ_BIT(hadc->Instance->JSQR, ADC_JSQR_JEXTEN) == 0UL)
  366. && (tmp_config_injected_queue == 0UL)
  367. )
  368. {
  369. SET_BIT(hadc->State, HAL_ADC_STATE_ERROR_CONFIG);
  370. return HAL_ERROR;
  371. }
  372. /* Process locked */
  373. __HAL_LOCK(hadc);
  374. /* Enable the ADC peripheral */
  375. tmp_hal_status = ADC_Enable(hadc);
  376. /* Start conversion if ADC is effectively enabled */
  377. if (tmp_hal_status == HAL_OK)
  378. {
  379. /* Check if a regular conversion is ongoing */
  380. if ((hadc->State & HAL_ADC_STATE_REG_BUSY) != 0UL)
  381. {
  382. /* Reset ADC error code field related to injected conversions only */
  383. CLEAR_BIT(hadc->ErrorCode, HAL_ADC_ERROR_JQOVF);
  384. }
  385. else
  386. {
  387. /* Set ADC error code to none */
  388. ADC_CLEAR_ERRORCODE(hadc);
  389. }
  390. /* Set ADC state */
  391. /* - Clear state bitfield related to injected group conversion results */
  392. /* - Set state bitfield related to injected operation */
  393. ADC_STATE_CLR_SET(hadc->State,
  394. HAL_ADC_STATE_READY | HAL_ADC_STATE_INJ_EOC,
  395. HAL_ADC_STATE_INJ_BUSY);
  396. /* Clear ADC group injected group conversion flag */
  397. /* (To ensure of no unknown state from potential previous ADC operations) */
  398. __HAL_ADC_CLEAR_FLAG(hadc, (ADC_FLAG_JEOC | ADC_FLAG_JEOS));
  399. /* Process unlocked */
  400. /* Unlock before starting ADC conversions: in case of potential */
  401. /* interruption, to let the process to ADC IRQ Handler. */
  402. __HAL_UNLOCK(hadc);
  403. /* Enable conversion of injected group, if automatic injected conversion */
  404. /* is disabled. */
  405. /* If software start has been selected, conversion starts immediately. */
  406. /* If external trigger has been selected, conversion will start at next */
  407. /* trigger event. */
  408. if(LL_ADC_INJ_GetTrigAuto(hadc->Instance) == LL_ADC_INJ_TRIG_INDEPENDENT)
  409. {
  410. /* Start ADC group injected conversion */
  411. LL_ADC_INJ_StartConversion(hadc->Instance);
  412. }
  413. }
  414. else
  415. {
  416. /* Process unlocked */
  417. __HAL_UNLOCK(hadc);
  418. }
  419. /* Return function status */
  420. return tmp_hal_status;
  421. }
  422. }
  423. /**
  424. * @brief Stop conversion of injected channels. Disable ADC peripheral if
  425. * no regular conversion is on going.
  426. * @note If ADC must be disabled and if conversion is on going on
  427. * regular group, function HAL_ADC_Stop must be used to stop both
  428. * injected and regular groups, and disable the ADC.
  429. * @note If injected group mode auto-injection is enabled,
  430. * function HAL_ADC_Stop must be used.
  431. * @param hadc ADC handle.
  432. * @retval HAL status
  433. */
  434. HAL_StatusTypeDef HAL_ADCEx_InjectedStop(ADC_HandleTypeDef *hadc)
  435. {
  436. HAL_StatusTypeDef tmp_hal_status;
  437. /* Check the parameters */
  438. assert_param(IS_ADC_ALL_INSTANCE(hadc->Instance));
  439. /* Process locked */
  440. __HAL_LOCK(hadc);
  441. /* 1. Stop potential conversion on going on injected group only. */
  442. tmp_hal_status = ADC_ConversionStop(hadc, ADC_INJECTED_GROUP);
  443. /* Disable ADC peripheral if injected conversions are effectively stopped */
  444. /* and if no conversion on regular group is on-going */
  445. if (tmp_hal_status == HAL_OK)
  446. {
  447. if (LL_ADC_REG_IsConversionOngoing(hadc->Instance) == 0UL)
  448. {
  449. /* 2. Disable the ADC peripheral */
  450. tmp_hal_status = ADC_Disable(hadc);
  451. /* Check if ADC is effectively disabled */
  452. if (tmp_hal_status == HAL_OK)
  453. {
  454. /* Set ADC state */
  455. ADC_STATE_CLR_SET(hadc->State,
  456. HAL_ADC_STATE_REG_BUSY | HAL_ADC_STATE_INJ_BUSY,
  457. HAL_ADC_STATE_READY);
  458. }
  459. }
  460. /* Conversion on injected group is stopped, but ADC not disabled since */
  461. /* conversion on regular group is still running. */
  462. else
  463. {
  464. /* Set ADC state */
  465. CLEAR_BIT(hadc->State, HAL_ADC_STATE_INJ_BUSY);
  466. }
  467. }
  468. /* Process unlocked */
  469. __HAL_UNLOCK(hadc);
  470. /* Return function status */
  471. return tmp_hal_status;
  472. }
  473. /**
  474. * @brief Wait for injected group conversion to be completed.
  475. * @param hadc ADC handle
  476. * @param Timeout Timeout value in millisecond.
  477. * @note Depending on hadc->Init.EOCSelection, JEOS or JEOC is
  478. * checked and cleared depending on AUTDLY bit status.
  479. * @retval HAL status
  480. */
  481. HAL_StatusTypeDef HAL_ADCEx_InjectedPollForConversion(ADC_HandleTypeDef *hadc, uint32_t Timeout)
  482. {
  483. uint32_t tickstart;
  484. uint32_t tmp_Flag_End;
  485. uint32_t tmp_adc_inj_is_trigger_source_sw_start;
  486. uint32_t tmp_adc_reg_is_trigger_source_sw_start;
  487. uint32_t tmp_cfgr;
  488. /* Check the parameters */
  489. assert_param(IS_ADC_ALL_INSTANCE(hadc->Instance));
  490. /* If end of sequence selected */
  491. if (hadc->Init.EOCSelection == ADC_EOC_SEQ_CONV)
  492. {
  493. tmp_Flag_End = ADC_FLAG_JEOS;
  494. }
  495. else /* end of conversion selected */
  496. {
  497. tmp_Flag_End = ADC_FLAG_JEOC;
  498. }
  499. /* Get timeout */
  500. tickstart = HAL_GetTick();
  501. /* Wait until End of Conversion or Sequence flag is raised */
  502. while ((hadc->Instance->ISR & tmp_Flag_End) == 0UL)
  503. {
  504. /* Check if timeout is disabled (set to infinite wait) */
  505. if (Timeout != HAL_MAX_DELAY)
  506. {
  507. if (((HAL_GetTick() - tickstart) > Timeout) || (Timeout == 0UL))
  508. {
  509. /* New check to avoid false timeout detection in case of preemption */
  510. if ((hadc->Instance->ISR & tmp_Flag_End) == 0UL)
  511. {
  512. /* Update ADC state machine to timeout */
  513. SET_BIT(hadc->State, HAL_ADC_STATE_TIMEOUT);
  514. /* Process unlocked */
  515. __HAL_UNLOCK(hadc);
  516. return HAL_TIMEOUT;
  517. }
  518. }
  519. }
  520. }
  521. /* Retrieve ADC configuration */
  522. tmp_adc_inj_is_trigger_source_sw_start = LL_ADC_INJ_IsTriggerSourceSWStart(hadc->Instance);
  523. tmp_adc_reg_is_trigger_source_sw_start = LL_ADC_REG_IsTriggerSourceSWStart(hadc->Instance);
  524. tmp_cfgr = READ_REG(hadc->Instance->CFGR);
  525. /* Update ADC state machine */
  526. SET_BIT(hadc->State, HAL_ADC_STATE_INJ_EOC);
  527. /* Determine whether any further conversion upcoming on group injected */
  528. /* by external trigger or by automatic injected conversion */
  529. /* from group regular. */
  530. if ((tmp_adc_inj_is_trigger_source_sw_start != 0UL) ||
  531. ((READ_BIT(tmp_cfgr, ADC_CFGR_JAUTO) == 0UL) &&
  532. ((tmp_adc_reg_is_trigger_source_sw_start != 0UL) &&
  533. (READ_BIT(tmp_cfgr, ADC_CFGR_CONT) == 0UL))))
  534. {
  535. /* Check whether end of sequence is reached */
  536. if (__HAL_ADC_GET_FLAG(hadc, ADC_FLAG_JEOS))
  537. {
  538. /* Particular case if injected contexts queue is enabled: */
  539. /* when the last context has been fully processed, JSQR is reset */
  540. /* by the hardware. Even if no injected conversion is planned to come */
  541. /* (queue empty, triggers are ignored), it can start again */
  542. /* immediately after setting a new context (JADSTART is still set). */
  543. /* Therefore, state of HAL ADC injected group is kept to busy. */
  544. if (READ_BIT(hadc->Instance->CFGR, ADC_CFGR_JQM) == 0UL)
  545. {
  546. /* Set ADC state */
  547. CLEAR_BIT(hadc->State, HAL_ADC_STATE_INJ_BUSY);
  548. if ((hadc->State & HAL_ADC_STATE_REG_BUSY) == 0UL)
  549. {
  550. SET_BIT(hadc->State, HAL_ADC_STATE_READY);
  551. }
  552. }
  553. }
  554. }
  555. /* Clear polled flag */
  556. if (tmp_Flag_End == ADC_FLAG_JEOS)
  557. {
  558. /* Clear end of sequence JEOS flag of injected group if low power feature */
  559. /* "LowPowerAutoWait " is disabled, to not interfere with this feature. */
  560. /* For injected groups, no new conversion will start before JEOS is */
  561. /* cleared. */
  562. if (READ_BIT(hadc->Instance->CFGR, ADC_CFGR_AUTDLY) == 0UL)
  563. {
  564. __HAL_ADC_CLEAR_FLAG(hadc, (ADC_FLAG_JEOC | ADC_FLAG_JEOS));
  565. }
  566. }
  567. else
  568. {
  569. __HAL_ADC_CLEAR_FLAG(hadc, ADC_FLAG_JEOC);
  570. }
  571. /* Return API HAL status */
  572. return HAL_OK;
  573. }
  574. /**
  575. * @brief Enable ADC, start conversion of injected group with interruption.
  576. * @note Interruptions enabled in this function according to initialization
  577. * setting : JEOC (end of conversion) or JEOS (end of sequence)
  578. * @param hadc ADC handle.
  579. * @retval HAL status.
  580. */
  581. HAL_StatusTypeDef HAL_ADCEx_InjectedStart_IT(ADC_HandleTypeDef *hadc)
  582. {
  583. HAL_StatusTypeDef tmp_hal_status;
  584. uint32_t tmp_config_injected_queue;
  585. /* Check the parameters */
  586. assert_param(IS_ADC_ALL_INSTANCE(hadc->Instance));
  587. if (LL_ADC_INJ_IsConversionOngoing(hadc->Instance) != 0UL)
  588. {
  589. return HAL_BUSY;
  590. }
  591. else
  592. {
  593. /* In case of software trigger detection enabled, JQDIS must be set
  594. (which can be done only if ADSTART and JADSTART are both cleared).
  595. If JQDIS is not set at that point, returns an error
  596. - since software trigger detection is disabled. User needs to
  597. resort to HAL_ADCEx_DisableInjectedQueue() API to set JQDIS.
  598. - or (if JQDIS is intentionally reset) since JEXTEN = 0 which means
  599. the queue is empty */
  600. tmp_config_injected_queue = READ_BIT(hadc->Instance->CFGR, ADC_CFGR_JQDIS);
  601. if ((READ_BIT(hadc->Instance->JSQR, ADC_JSQR_JEXTEN) == 0UL)
  602. && (tmp_config_injected_queue == 0UL)
  603. )
  604. {
  605. SET_BIT(hadc->State, HAL_ADC_STATE_ERROR_CONFIG);
  606. return HAL_ERROR;
  607. }
  608. /* Process locked */
  609. __HAL_LOCK(hadc);
  610. /* Enable the ADC peripheral */
  611. tmp_hal_status = ADC_Enable(hadc);
  612. /* Start conversion if ADC is effectively enabled */
  613. if (tmp_hal_status == HAL_OK)
  614. {
  615. /* Check if a regular conversion is ongoing */
  616. if ((hadc->State & HAL_ADC_STATE_REG_BUSY) != 0UL)
  617. {
  618. /* Reset ADC error code field related to injected conversions only */
  619. CLEAR_BIT(hadc->ErrorCode, HAL_ADC_ERROR_JQOVF);
  620. }
  621. else
  622. {
  623. /* Set ADC error code to none */
  624. ADC_CLEAR_ERRORCODE(hadc);
  625. }
  626. /* Set ADC state */
  627. /* - Clear state bitfield related to injected group conversion results */
  628. /* - Set state bitfield related to injected operation */
  629. ADC_STATE_CLR_SET(hadc->State,
  630. HAL_ADC_STATE_READY | HAL_ADC_STATE_INJ_EOC,
  631. HAL_ADC_STATE_INJ_BUSY);
  632. /* Clear ADC group injected group conversion flag */
  633. /* (To ensure of no unknown state from potential previous ADC operations) */
  634. __HAL_ADC_CLEAR_FLAG(hadc, (ADC_FLAG_JEOC | ADC_FLAG_JEOS));
  635. /* Process unlocked */
  636. /* Unlock before starting ADC conversions: in case of potential */
  637. /* interruption, to let the process to ADC IRQ Handler. */
  638. __HAL_UNLOCK(hadc);
  639. /* Enable ADC Injected context queue overflow interrupt if this feature */
  640. /* is enabled. */
  641. if ((hadc->Instance->CFGR & ADC_CFGR_JQM) != 0UL)
  642. {
  643. __HAL_ADC_ENABLE_IT(hadc, ADC_FLAG_JQOVF);
  644. }
  645. /* Enable ADC end of conversion interrupt */
  646. switch (hadc->Init.EOCSelection)
  647. {
  648. case ADC_EOC_SEQ_CONV:
  649. __HAL_ADC_DISABLE_IT(hadc, ADC_IT_JEOC);
  650. __HAL_ADC_ENABLE_IT(hadc, ADC_IT_JEOS);
  651. break;
  652. /* case ADC_EOC_SINGLE_CONV */
  653. default:
  654. __HAL_ADC_DISABLE_IT(hadc, ADC_IT_JEOS);
  655. __HAL_ADC_ENABLE_IT(hadc, ADC_IT_JEOC);
  656. break;
  657. }
  658. /* Enable conversion of injected group, if automatic injected conversion */
  659. /* is disabled. */
  660. /* If software start has been selected, conversion starts immediately. */
  661. /* If external trigger has been selected, conversion will start at next */
  662. /* trigger event. */
  663. if(LL_ADC_INJ_GetTrigAuto(hadc->Instance) == LL_ADC_INJ_TRIG_INDEPENDENT)
  664. {
  665. /* Start ADC group injected conversion */
  666. LL_ADC_INJ_StartConversion(hadc->Instance);
  667. }
  668. }
  669. else
  670. {
  671. /* Process unlocked */
  672. __HAL_UNLOCK(hadc);
  673. }
  674. /* Return function status */
  675. return tmp_hal_status;
  676. }
  677. }
  678. /**
  679. * @brief Stop conversion of injected channels, disable interruption of
  680. * end-of-conversion. Disable ADC peripheral if no regular conversion
  681. * is on going.
  682. * @note If ADC must be disabled and if conversion is on going on
  683. * regular group, function HAL_ADC_Stop must be used to stop both
  684. * injected and regular groups, and disable the ADC.
  685. * @note If injected group mode auto-injection is enabled,
  686. * function HAL_ADC_Stop must be used.
  687. * @note In case of auto-injection mode, HAL_ADC_Stop() must be used.
  688. * @param hadc ADC handle
  689. * @retval HAL status
  690. */
  691. HAL_StatusTypeDef HAL_ADCEx_InjectedStop_IT(ADC_HandleTypeDef *hadc)
  692. {
  693. HAL_StatusTypeDef tmp_hal_status;
  694. /* Check the parameters */
  695. assert_param(IS_ADC_ALL_INSTANCE(hadc->Instance));
  696. /* Process locked */
  697. __HAL_LOCK(hadc);
  698. /* 1. Stop potential conversion on going on injected group only. */
  699. tmp_hal_status = ADC_ConversionStop(hadc, ADC_INJECTED_GROUP);
  700. /* Disable ADC peripheral if injected conversions are effectively stopped */
  701. /* and if no conversion on the other group (regular group) is intended to */
  702. /* continue. */
  703. if (tmp_hal_status == HAL_OK)
  704. {
  705. /* Disable ADC end of conversion interrupt for injected channels */
  706. __HAL_ADC_DISABLE_IT(hadc, (ADC_IT_JEOC | ADC_IT_JEOS | ADC_FLAG_JQOVF));
  707. if (LL_ADC_REG_IsConversionOngoing(hadc->Instance) == 0UL)
  708. {
  709. /* 2. Disable the ADC peripheral */
  710. tmp_hal_status = ADC_Disable(hadc);
  711. /* Check if ADC is effectively disabled */
  712. if (tmp_hal_status == HAL_OK)
  713. {
  714. /* Set ADC state */
  715. ADC_STATE_CLR_SET(hadc->State,
  716. HAL_ADC_STATE_REG_BUSY | HAL_ADC_STATE_INJ_BUSY,
  717. HAL_ADC_STATE_READY);
  718. }
  719. }
  720. /* Conversion on injected group is stopped, but ADC not disabled since */
  721. /* conversion on regular group is still running. */
  722. else
  723. {
  724. /* Set ADC state */
  725. CLEAR_BIT(hadc->State, HAL_ADC_STATE_INJ_BUSY);
  726. }
  727. }
  728. /* Process unlocked */
  729. __HAL_UNLOCK(hadc);
  730. /* Return function status */
  731. return tmp_hal_status;
  732. }
  733. #endif /* ADC_SUPPORT_2_5_MSPS */
  734. #if defined(ADC_SUPPORT_2_5_MSPS)
  735. /* Feature "ADC group injected" not available on ADC peripheral of this STM32WB device */
  736. #else
  737. /**
  738. * @brief Get ADC injected group conversion result.
  739. * @note Reading register JDRx automatically clears ADC flag JEOC
  740. * (ADC group injected end of unitary conversion).
  741. * @note This function does not clear ADC flag JEOS
  742. * (ADC group injected end of sequence conversion)
  743. * Occurrence of flag JEOS rising:
  744. * - If sequencer is composed of 1 rank, flag JEOS is equivalent
  745. * to flag JEOC.
  746. * - If sequencer is composed of several ranks, during the scan
  747. * sequence flag JEOC only is raised, at the end of the scan sequence
  748. * both flags JEOC and EOS are raised.
  749. * Flag JEOS must not be cleared by this function because
  750. * it would not be compliant with low power features
  751. * (feature low power auto-wait, not available on all STM32 families).
  752. * To clear this flag, either use function:
  753. * in programming model IT: @ref HAL_ADC_IRQHandler(), in programming
  754. * model polling: @ref HAL_ADCEx_InjectedPollForConversion()
  755. * or @ref __HAL_ADC_CLEAR_FLAG(&hadc, ADC_FLAG_JEOS).
  756. * @param hadc ADC handle
  757. * @param InjectedRank the converted ADC injected rank.
  758. * This parameter can be one of the following values:
  759. * @arg @ref ADC_INJECTED_RANK_1 ADC group injected rank 1
  760. * @arg @ref ADC_INJECTED_RANK_2 ADC group injected rank 2
  761. * @arg @ref ADC_INJECTED_RANK_3 ADC group injected rank 3
  762. * @arg @ref ADC_INJECTED_RANK_4 ADC group injected rank 4
  763. * @retval ADC group injected conversion data
  764. */
  765. uint32_t HAL_ADCEx_InjectedGetValue(const ADC_HandleTypeDef *hadc, uint32_t InjectedRank)
  766. {
  767. uint32_t tmp_jdr;
  768. /* Check the parameters */
  769. assert_param(IS_ADC_ALL_INSTANCE(hadc->Instance));
  770. assert_param(IS_ADC_INJECTED_RANK(InjectedRank));
  771. /* Get ADC converted value */
  772. switch (InjectedRank)
  773. {
  774. case ADC_INJECTED_RANK_4:
  775. tmp_jdr = hadc->Instance->JDR4;
  776. break;
  777. case ADC_INJECTED_RANK_3:
  778. tmp_jdr = hadc->Instance->JDR3;
  779. break;
  780. case ADC_INJECTED_RANK_2:
  781. tmp_jdr = hadc->Instance->JDR2;
  782. break;
  783. case ADC_INJECTED_RANK_1:
  784. default:
  785. tmp_jdr = hadc->Instance->JDR1;
  786. break;
  787. }
  788. /* Return ADC converted value */
  789. return tmp_jdr;
  790. }
  791. /**
  792. * @brief Injected conversion complete callback in non-blocking mode.
  793. * @param hadc ADC handle
  794. * @retval None
  795. */
  796. __weak void HAL_ADCEx_InjectedConvCpltCallback(ADC_HandleTypeDef *hadc)
  797. {
  798. /* Prevent unused argument(s) compilation warning */
  799. UNUSED(hadc);
  800. /* NOTE : This function should not be modified. When the callback is needed,
  801. function HAL_ADCEx_InjectedConvCpltCallback must be implemented in the user file.
  802. */
  803. }
  804. /**
  805. * @brief Injected context queue overflow callback.
  806. * @note This callback is called if injected context queue is enabled
  807. (parameter "QueueInjectedContext" in injected channel configuration)
  808. and if a new injected context is set when queue is full (maximum 2
  809. contexts).
  810. * @param hadc ADC handle
  811. * @retval None
  812. */
  813. __weak void HAL_ADCEx_InjectedQueueOverflowCallback(ADC_HandleTypeDef *hadc)
  814. {
  815. /* Prevent unused argument(s) compilation warning */
  816. UNUSED(hadc);
  817. /* NOTE : This function should not be modified. When the callback is needed,
  818. function HAL_ADCEx_InjectedQueueOverflowCallback must be implemented in the user file.
  819. */
  820. }
  821. #endif /* ADC_SUPPORT_2_5_MSPS */
  822. /**
  823. * @brief Analog watchdog 2 callback in non-blocking mode.
  824. * @param hadc ADC handle
  825. * @retval None
  826. */
  827. __weak void HAL_ADCEx_LevelOutOfWindow2Callback(ADC_HandleTypeDef *hadc)
  828. {
  829. /* Prevent unused argument(s) compilation warning */
  830. UNUSED(hadc);
  831. /* NOTE : This function should not be modified. When the callback is needed,
  832. function HAL_ADCEx_LevelOutOfWindow2Callback must be implemented in the user file.
  833. */
  834. }
  835. /**
  836. * @brief Analog watchdog 3 callback in non-blocking mode.
  837. * @param hadc ADC handle
  838. * @retval None
  839. */
  840. __weak void HAL_ADCEx_LevelOutOfWindow3Callback(ADC_HandleTypeDef *hadc)
  841. {
  842. /* Prevent unused argument(s) compilation warning */
  843. UNUSED(hadc);
  844. /* NOTE : This function should not be modified. When the callback is needed,
  845. function HAL_ADCEx_LevelOutOfWindow3Callback must be implemented in the user file.
  846. */
  847. }
  848. /**
  849. * @brief End Of Sampling callback in non-blocking mode.
  850. * @param hadc ADC handle
  851. * @retval None
  852. */
  853. __weak void HAL_ADCEx_EndOfSamplingCallback(ADC_HandleTypeDef *hadc)
  854. {
  855. /* Prevent unused argument(s) compilation warning */
  856. UNUSED(hadc);
  857. /* NOTE : This function should not be modified. When the callback is needed,
  858. function HAL_ADCEx_EndOfSamplingCallback must be implemented in the user file.
  859. */
  860. }
  861. #if defined(ADC_SUPPORT_2_5_MSPS)
  862. /* Feature "ADC group injected" not available on ADC peripheral of this STM32WB device */
  863. #else
  864. /**
  865. * @brief Stop ADC conversion of regular group (and injected channels in
  866. * case of auto_injection mode), disable ADC peripheral if no
  867. * conversion is on going on injected group.
  868. * @param hadc ADC handle
  869. * @retval HAL status.
  870. */
  871. HAL_StatusTypeDef HAL_ADCEx_RegularStop(ADC_HandleTypeDef *hadc)
  872. {
  873. HAL_StatusTypeDef tmp_hal_status;
  874. /* Check the parameters */
  875. assert_param(IS_ADC_ALL_INSTANCE(hadc->Instance));
  876. /* Process locked */
  877. __HAL_LOCK(hadc);
  878. /* 1. Stop potential regular conversion on going */
  879. tmp_hal_status = ADC_ConversionStop(hadc, ADC_REGULAR_GROUP);
  880. /* Disable ADC peripheral if regular conversions are effectively stopped
  881. and if no injected conversions are on-going */
  882. if (tmp_hal_status == HAL_OK)
  883. {
  884. /* Clear HAL_ADC_STATE_REG_BUSY bit */
  885. CLEAR_BIT(hadc->State, HAL_ADC_STATE_REG_BUSY);
  886. if (LL_ADC_INJ_IsConversionOngoing(hadc->Instance) == 0UL)
  887. {
  888. /* 2. Disable the ADC peripheral */
  889. tmp_hal_status = ADC_Disable(hadc);
  890. /* Check if ADC is effectively disabled */
  891. if (tmp_hal_status == HAL_OK)
  892. {
  893. /* Set ADC state */
  894. ADC_STATE_CLR_SET(hadc->State,
  895. HAL_ADC_STATE_INJ_BUSY,
  896. HAL_ADC_STATE_READY);
  897. }
  898. }
  899. /* Conversion on injected group is stopped, but ADC not disabled since */
  900. /* conversion on regular group is still running. */
  901. else
  902. {
  903. SET_BIT(hadc->State, HAL_ADC_STATE_INJ_BUSY);
  904. }
  905. }
  906. /* Process unlocked */
  907. __HAL_UNLOCK(hadc);
  908. /* Return function status */
  909. return tmp_hal_status;
  910. }
  911. /**
  912. * @brief Stop ADC conversion of ADC groups regular and injected,
  913. * disable interrution of end-of-conversion,
  914. * disable ADC peripheral if no conversion is on going
  915. * on injected group.
  916. * @param hadc ADC handle
  917. * @retval HAL status.
  918. */
  919. HAL_StatusTypeDef HAL_ADCEx_RegularStop_IT(ADC_HandleTypeDef *hadc)
  920. {
  921. HAL_StatusTypeDef tmp_hal_status;
  922. /* Check the parameters */
  923. assert_param(IS_ADC_ALL_INSTANCE(hadc->Instance));
  924. /* Process locked */
  925. __HAL_LOCK(hadc);
  926. /* 1. Stop potential regular conversion on going */
  927. tmp_hal_status = ADC_ConversionStop(hadc, ADC_REGULAR_GROUP);
  928. /* Disable ADC peripheral if conversions are effectively stopped
  929. and if no injected conversion is on-going */
  930. if (tmp_hal_status == HAL_OK)
  931. {
  932. /* Clear HAL_ADC_STATE_REG_BUSY bit */
  933. CLEAR_BIT(hadc->State, HAL_ADC_STATE_REG_BUSY);
  934. /* Disable all regular-related interrupts */
  935. __HAL_ADC_DISABLE_IT(hadc, (ADC_IT_EOC | ADC_IT_EOS | ADC_IT_OVR));
  936. /* 2. Disable ADC peripheral if no injected conversions are on-going */
  937. if (LL_ADC_INJ_IsConversionOngoing(hadc->Instance) == 0UL)
  938. {
  939. tmp_hal_status = ADC_Disable(hadc);
  940. /* if no issue reported */
  941. if (tmp_hal_status == HAL_OK)
  942. {
  943. /* Set ADC state */
  944. ADC_STATE_CLR_SET(hadc->State,
  945. HAL_ADC_STATE_INJ_BUSY,
  946. HAL_ADC_STATE_READY);
  947. }
  948. }
  949. else
  950. {
  951. SET_BIT(hadc->State, HAL_ADC_STATE_INJ_BUSY);
  952. }
  953. }
  954. /* Process unlocked */
  955. __HAL_UNLOCK(hadc);
  956. /* Return function status */
  957. return tmp_hal_status;
  958. }
  959. /**
  960. * @brief Stop ADC conversion of regular group (and injected group in
  961. * case of auto_injection mode), disable ADC DMA transfer, disable
  962. * ADC peripheral if no conversion is on going
  963. * on injected group.
  964. * @param hadc ADC handle
  965. * @retval HAL status.
  966. */
  967. HAL_StatusTypeDef HAL_ADCEx_RegularStop_DMA(ADC_HandleTypeDef *hadc)
  968. {
  969. HAL_StatusTypeDef tmp_hal_status;
  970. /* Check the parameters */
  971. assert_param(IS_ADC_ALL_INSTANCE(hadc->Instance));
  972. /* Process locked */
  973. __HAL_LOCK(hadc);
  974. /* 1. Stop potential regular conversion on going */
  975. tmp_hal_status = ADC_ConversionStop(hadc, ADC_REGULAR_GROUP);
  976. /* Disable ADC peripheral if conversions are effectively stopped
  977. and if no injected conversion is on-going */
  978. if (tmp_hal_status == HAL_OK)
  979. {
  980. /* Clear HAL_ADC_STATE_REG_BUSY bit */
  981. CLEAR_BIT(hadc->State, HAL_ADC_STATE_REG_BUSY);
  982. /* Disable ADC DMA (ADC DMA configuration ADC_CFGR_DMACFG is kept) */
  983. CLEAR_BIT(hadc->Instance->CFGR, ADC_CFGR_DMAEN);
  984. /* Disable the DMA channel (in case of DMA in circular mode or stop while */
  985. /* while DMA transfer is on going) */
  986. tmp_hal_status = HAL_DMA_Abort(hadc->DMA_Handle);
  987. /* Check if DMA channel effectively disabled */
  988. if (tmp_hal_status != HAL_OK)
  989. {
  990. /* Update ADC state machine to error */
  991. SET_BIT(hadc->State, HAL_ADC_STATE_ERROR_DMA);
  992. }
  993. /* Disable ADC overrun interrupt */
  994. __HAL_ADC_DISABLE_IT(hadc, ADC_IT_OVR);
  995. /* 2. Disable the ADC peripheral */
  996. /* Update "tmp_hal_status" only if DMA channel disabling passed, */
  997. /* to keep in memory a potential failing status. */
  998. if (LL_ADC_INJ_IsConversionOngoing(hadc->Instance) == 0UL)
  999. {
  1000. if (tmp_hal_status == HAL_OK)
  1001. {
  1002. tmp_hal_status = ADC_Disable(hadc);
  1003. }
  1004. else
  1005. {
  1006. (void)ADC_Disable(hadc);
  1007. }
  1008. /* Check if ADC is effectively disabled */
  1009. if (tmp_hal_status == HAL_OK)
  1010. {
  1011. /* Set ADC state */
  1012. ADC_STATE_CLR_SET(hadc->State,
  1013. HAL_ADC_STATE_INJ_BUSY,
  1014. HAL_ADC_STATE_READY);
  1015. }
  1016. }
  1017. else
  1018. {
  1019. SET_BIT(hadc->State, HAL_ADC_STATE_INJ_BUSY);
  1020. }
  1021. }
  1022. /* Process unlocked */
  1023. __HAL_UNLOCK(hadc);
  1024. /* Return function status */
  1025. return tmp_hal_status;
  1026. }
  1027. #endif /* ADC_SUPPORT_2_5_MSPS */
  1028. /**
  1029. * @}
  1030. */
  1031. #if defined(ADC_SUPPORT_2_5_MSPS)
  1032. /* Feature "ADC group injected" not available on ADC peripheral of this STM32WB device */
  1033. #else
  1034. /** @defgroup ADCEx_Exported_Functions_Group2 ADC Extended Peripheral Control functions
  1035. * @brief ADC Extended Peripheral Control functions
  1036. *
  1037. @verbatim
  1038. ===============================================================================
  1039. ##### Peripheral Control functions #####
  1040. ===============================================================================
  1041. [..] This section provides functions allowing to:
  1042. (+) Configure channels on injected group
  1043. (+) Enable or Disable Injected Queue
  1044. (+) Disable ADC voltage regulator
  1045. (+) Enter ADC deep-power-down mode
  1046. @endverbatim
  1047. * @{
  1048. */
  1049. /**
  1050. * @brief Configure a channel to be assigned to ADC group injected.
  1051. * @note Possibility to update parameters on the fly:
  1052. * This function initializes injected group, following calls to this
  1053. * function can be used to reconfigure some parameters of structure
  1054. * "ADC_InjectionConfTypeDef" on the fly, without resetting the ADC.
  1055. * The setting of these parameters is conditioned to ADC state:
  1056. * Refer to comments of structure "ADC_InjectionConfTypeDef".
  1057. * @note In case of usage of internal measurement channels:
  1058. * Vbat/VrefInt/TempSensor.
  1059. * These internal paths can be disabled using function
  1060. * HAL_ADC_DeInit().
  1061. * @note Caution: For Injected Context Queue use, a context must be fully
  1062. * defined before start of injected conversion. All channels are configured
  1063. * consecutively for the same ADC instance. Therefore, the number of calls to
  1064. * HAL_ADCEx_InjectedConfigChannel() must be equal to the value of parameter
  1065. * InjectedNbrOfConversion for each context.
  1066. * - Example 1: If 1 context is intended to be used (or if there is no use of the
  1067. * Injected Queue Context feature) and if the context contains 3 injected ranks
  1068. * (InjectedNbrOfConversion = 3), HAL_ADCEx_InjectedConfigChannel() must be
  1069. * called once for each channel (i.e. 3 times) before starting a conversion.
  1070. * This function must not be called to configure a 4th injected channel:
  1071. * it would start a new context into context queue.
  1072. * - Example 2: If 2 contexts are intended to be used and each of them contains
  1073. * 3 injected ranks (InjectedNbrOfConversion = 3),
  1074. * HAL_ADCEx_InjectedConfigChannel() must be called once for each channel and
  1075. * for each context (3 channels x 2 contexts = 6 calls). Conversion can
  1076. * start once the 1st context is set, that is after the first three
  1077. * HAL_ADCEx_InjectedConfigChannel() calls. The 2nd context can be set on the fly.
  1078. * @param hadc ADC handle
  1079. * @param sConfigInjected Structure of ADC injected group and ADC channel for
  1080. * injected group.
  1081. * @retval HAL status
  1082. */
  1083. HAL_StatusTypeDef HAL_ADCEx_InjectedConfigChannel(ADC_HandleTypeDef *hadc, const ADC_InjectionConfTypeDef *sConfigInjected)
  1084. {
  1085. HAL_StatusTypeDef tmp_hal_status = HAL_OK;
  1086. uint32_t tmpOffsetShifted;
  1087. uint32_t tmp_config_internal_channel;
  1088. uint32_t tmp_adc_is_conversion_on_going_regular;
  1089. uint32_t tmp_adc_is_conversion_on_going_injected;
  1090. __IO uint32_t wait_loop_index = 0;
  1091. uint32_t tmp_JSQR_ContextQueueBeingBuilt = 0U;
  1092. /* Check the parameters */
  1093. assert_param(IS_ADC_ALL_INSTANCE(hadc->Instance));
  1094. assert_param(IS_ADC_SAMPLE_TIME(sConfigInjected->InjectedSamplingTime));
  1095. assert_param(IS_ADC_SINGLE_DIFFERENTIAL(sConfigInjected->InjectedSingleDiff));
  1096. assert_param(IS_FUNCTIONAL_STATE(sConfigInjected->AutoInjectedConv));
  1097. assert_param(IS_FUNCTIONAL_STATE(sConfigInjected->QueueInjectedContext));
  1098. assert_param(IS_ADC_EXTTRIGINJEC_EDGE(sConfigInjected->ExternalTrigInjecConvEdge));
  1099. assert_param(IS_ADC_EXTTRIGINJEC(hadc, sConfigInjected->ExternalTrigInjecConv));
  1100. assert_param(IS_ADC_OFFSET_NUMBER(sConfigInjected->InjectedOffsetNumber));
  1101. assert_param(IS_ADC_RANGE(ADC_GET_RESOLUTION(hadc), sConfigInjected->InjectedOffset));
  1102. assert_param(IS_FUNCTIONAL_STATE(sConfigInjected->InjecOversamplingMode));
  1103. if (hadc->Init.ScanConvMode != ADC_SCAN_DISABLE)
  1104. {
  1105. assert_param(IS_ADC_INJECTED_RANK(sConfigInjected->InjectedRank));
  1106. assert_param(IS_ADC_INJECTED_NB_CONV(sConfigInjected->InjectedNbrOfConversion));
  1107. assert_param(IS_FUNCTIONAL_STATE(sConfigInjected->InjectedDiscontinuousConvMode));
  1108. }
  1109. /* if JOVSE is set, the value of the OFFSETy_EN bit in ADCx_OFRy register is
  1110. ignored (considered as reset) */
  1111. assert_param(!((sConfigInjected->InjectedOffsetNumber != ADC_OFFSET_NONE) && (sConfigInjected->InjecOversamplingMode == ENABLE)));
  1112. /* JDISCEN and JAUTO bits can't be set at the same time */
  1113. assert_param(!((sConfigInjected->InjectedDiscontinuousConvMode == ENABLE) && (sConfigInjected->AutoInjectedConv == ENABLE)));
  1114. /* DISCEN and JAUTO bits can't be set at the same time */
  1115. assert_param(!((hadc->Init.DiscontinuousConvMode == ENABLE) && (sConfigInjected->AutoInjectedConv == ENABLE)));
  1116. /* Verification of channel number */
  1117. if (sConfigInjected->InjectedSingleDiff != ADC_DIFFERENTIAL_ENDED)
  1118. {
  1119. assert_param(IS_ADC_CHANNEL(hadc, sConfigInjected->InjectedChannel));
  1120. }
  1121. else
  1122. {
  1123. assert_param(IS_ADC_DIFF_CHANNEL(hadc, sConfigInjected->InjectedChannel));
  1124. }
  1125. /* Process locked */
  1126. __HAL_LOCK(hadc);
  1127. /* Configuration of injected group sequencer: */
  1128. /* Hardware constraint: Must fully define injected context register JSQR */
  1129. /* before make it entering into injected sequencer queue. */
  1130. /* */
  1131. /* - if scan mode is disabled: */
  1132. /* * Injected channels sequence length is set to 0x00: 1 channel */
  1133. /* converted (channel on injected rank 1) */
  1134. /* Parameter "InjectedNbrOfConversion" is discarded. */
  1135. /* * Injected context register JSQR setting is simple: register is fully */
  1136. /* defined on one call of this function (for injected rank 1) and can */
  1137. /* be entered into queue directly. */
  1138. /* - if scan mode is enabled: */
  1139. /* * Injected channels sequence length is set to parameter */
  1140. /* "InjectedNbrOfConversion". */
  1141. /* * Injected context register JSQR setting more complex: register is */
  1142. /* fully defined over successive calls of this function, for each */
  1143. /* injected channel rank. It is entered into queue only when all */
  1144. /* injected ranks have been set. */
  1145. /* Note: Scan mode is not present by hardware on this device, but used */
  1146. /* by software for alignment over all STM32 devices. */
  1147. if ((hadc->Init.ScanConvMode == ADC_SCAN_DISABLE) ||
  1148. (sConfigInjected->InjectedNbrOfConversion == 1U))
  1149. {
  1150. /* Configuration of context register JSQR: */
  1151. /* - number of ranks in injected group sequencer: fixed to 1st rank */
  1152. /* (scan mode disabled, only rank 1 used) */
  1153. /* - external trigger to start conversion */
  1154. /* - external trigger polarity */
  1155. /* - channel set to rank 1 (scan mode disabled, only rank 1 can be used) */
  1156. if (sConfigInjected->InjectedRank == ADC_INJECTED_RANK_1)
  1157. {
  1158. /* Enable external trigger if trigger selection is different of */
  1159. /* software start. */
  1160. /* Note: This configuration keeps the hardware feature of parameter */
  1161. /* ExternalTrigInjecConvEdge "trigger edge none" equivalent to */
  1162. /* software start. */
  1163. if (sConfigInjected->ExternalTrigInjecConv != ADC_INJECTED_SOFTWARE_START)
  1164. {
  1165. tmp_JSQR_ContextQueueBeingBuilt = (ADC_JSQR_RK(sConfigInjected->InjectedChannel, ADC_INJECTED_RANK_1)
  1166. | (sConfigInjected->ExternalTrigInjecConv & ADC_JSQR_JEXTSEL)
  1167. | sConfigInjected->ExternalTrigInjecConvEdge
  1168. );
  1169. }
  1170. else
  1171. {
  1172. tmp_JSQR_ContextQueueBeingBuilt = (ADC_JSQR_RK(sConfigInjected->InjectedChannel, ADC_INJECTED_RANK_1));
  1173. }
  1174. MODIFY_REG(hadc->Instance->JSQR, ADC_JSQR_FIELDS, tmp_JSQR_ContextQueueBeingBuilt);
  1175. /* For debug and informative reasons, hadc handle saves JSQR setting */
  1176. hadc->InjectionConfig.ContextQueue = tmp_JSQR_ContextQueueBeingBuilt;
  1177. }
  1178. }
  1179. else
  1180. {
  1181. /* Case of scan mode enabled, several channels to set into injected group */
  1182. /* sequencer. */
  1183. /* */
  1184. /* Procedure to define injected context register JSQR over successive */
  1185. /* calls of this function, for each injected channel rank: */
  1186. /* 1. Start new context and set parameters related to all injected */
  1187. /* channels: injected sequence length and trigger. */
  1188. /* if hadc->InjectionConfig.ChannelCount is equal to 0, this is the first */
  1189. /* call of the context under setting */
  1190. if (hadc->InjectionConfig.ChannelCount == 0U)
  1191. {
  1192. /* Initialize number of channels that will be configured on the context */
  1193. /* being built */
  1194. hadc->InjectionConfig.ChannelCount = sConfigInjected->InjectedNbrOfConversion;
  1195. /* Handle hadc saves the context under build up over each HAL_ADCEx_InjectedConfigChannel()
  1196. call, this context will be written in JSQR register at the last call.
  1197. At this point, the context is merely reset */
  1198. hadc->InjectionConfig.ContextQueue = 0x00000000U;
  1199. /* Configuration of context register JSQR: */
  1200. /* - number of ranks in injected group sequencer */
  1201. /* - external trigger to start conversion */
  1202. /* - external trigger polarity */
  1203. /* Enable external trigger if trigger selection is different of */
  1204. /* software start. */
  1205. /* Note: This configuration keeps the hardware feature of parameter */
  1206. /* ExternalTrigInjecConvEdge "trigger edge none" equivalent to */
  1207. /* software start. */
  1208. if (sConfigInjected->ExternalTrigInjecConv != ADC_INJECTED_SOFTWARE_START)
  1209. {
  1210. tmp_JSQR_ContextQueueBeingBuilt = ((sConfigInjected->InjectedNbrOfConversion - 1U)
  1211. | (sConfigInjected->ExternalTrigInjecConv & ADC_JSQR_JEXTSEL)
  1212. | sConfigInjected->ExternalTrigInjecConvEdge
  1213. );
  1214. }
  1215. else
  1216. {
  1217. tmp_JSQR_ContextQueueBeingBuilt = ((sConfigInjected->InjectedNbrOfConversion - 1U));
  1218. }
  1219. }
  1220. /* 2. Continue setting of context under definition with parameter */
  1221. /* related to each channel: channel rank sequence */
  1222. /* Clear the old JSQx bits for the selected rank */
  1223. tmp_JSQR_ContextQueueBeingBuilt &= ~ADC_JSQR_RK(ADC_SQR3_SQ10, sConfigInjected->InjectedRank);
  1224. /* Set the JSQx bits for the selected rank */
  1225. tmp_JSQR_ContextQueueBeingBuilt |= ADC_JSQR_RK(sConfigInjected->InjectedChannel, sConfigInjected->InjectedRank);
  1226. /* Decrease channel count */
  1227. hadc->InjectionConfig.ChannelCount--;
  1228. /* 3. tmp_JSQR_ContextQueueBeingBuilt is fully built for this HAL_ADCEx_InjectedConfigChannel()
  1229. call, aggregate the setting to those already built during the previous
  1230. HAL_ADCEx_InjectedConfigChannel() calls (for the same context of course) */
  1231. hadc->InjectionConfig.ContextQueue |= tmp_JSQR_ContextQueueBeingBuilt;
  1232. /* 4. End of context setting: if this is the last channel set, then write context
  1233. into register JSQR and make it enter into queue */
  1234. if (hadc->InjectionConfig.ChannelCount == 0U)
  1235. {
  1236. MODIFY_REG(hadc->Instance->JSQR, ADC_JSQR_FIELDS, hadc->InjectionConfig.ContextQueue);
  1237. }
  1238. }
  1239. /* Parameters update conditioned to ADC state: */
  1240. /* Parameters that can be updated when ADC is disabled or enabled without */
  1241. /* conversion on going on injected group: */
  1242. /* - Injected context queue: Queue disable (active context is kept) or */
  1243. /* enable (context decremented, up to 2 contexts queued) */
  1244. /* - Injected discontinuous mode: can be enabled only if auto-injected */
  1245. /* mode is disabled. */
  1246. if (LL_ADC_INJ_IsConversionOngoing(hadc->Instance) == 0UL)
  1247. {
  1248. /* If auto-injected mode is disabled: no constraint */
  1249. if (sConfigInjected->AutoInjectedConv == DISABLE)
  1250. {
  1251. MODIFY_REG(hadc->Instance->CFGR,
  1252. ADC_CFGR_JQM | ADC_CFGR_JDISCEN,
  1253. ADC_CFGR_INJECT_CONTEXT_QUEUE((uint32_t)sConfigInjected->QueueInjectedContext) |
  1254. ADC_CFGR_INJECT_DISCCONTINUOUS((uint32_t)sConfigInjected->InjectedDiscontinuousConvMode));
  1255. }
  1256. /* If auto-injected mode is enabled: Injected discontinuous setting is */
  1257. /* discarded. */
  1258. else
  1259. {
  1260. MODIFY_REG(hadc->Instance->CFGR,
  1261. ADC_CFGR_JQM | ADC_CFGR_JDISCEN,
  1262. ADC_CFGR_INJECT_CONTEXT_QUEUE((uint32_t)sConfigInjected->QueueInjectedContext));
  1263. }
  1264. }
  1265. /* Parameters update conditioned to ADC state: */
  1266. /* Parameters that can be updated when ADC is disabled or enabled without */
  1267. /* conversion on going on regular and injected groups: */
  1268. /* - Automatic injected conversion: can be enabled if injected group */
  1269. /* external triggers are disabled. */
  1270. /* - Channel sampling time */
  1271. /* - Channel offset */
  1272. tmp_adc_is_conversion_on_going_regular = LL_ADC_REG_IsConversionOngoing(hadc->Instance);
  1273. tmp_adc_is_conversion_on_going_injected = LL_ADC_INJ_IsConversionOngoing(hadc->Instance);
  1274. if ((tmp_adc_is_conversion_on_going_regular == 0UL)
  1275. && (tmp_adc_is_conversion_on_going_injected == 0UL)
  1276. )
  1277. {
  1278. /* If injected group external triggers are disabled (set to injected */
  1279. /* software start): no constraint */
  1280. if ((sConfigInjected->ExternalTrigInjecConv == ADC_INJECTED_SOFTWARE_START)
  1281. || (sConfigInjected->ExternalTrigInjecConvEdge == ADC_EXTERNALTRIGINJECCONV_EDGE_NONE))
  1282. {
  1283. if (sConfigInjected->AutoInjectedConv == ENABLE)
  1284. {
  1285. SET_BIT(hadc->Instance->CFGR, ADC_CFGR_JAUTO);
  1286. }
  1287. else
  1288. {
  1289. CLEAR_BIT(hadc->Instance->CFGR, ADC_CFGR_JAUTO);
  1290. }
  1291. }
  1292. /* If Automatic injected conversion was intended to be set and could not */
  1293. /* due to injected group external triggers enabled, error is reported. */
  1294. else
  1295. {
  1296. if (sConfigInjected->AutoInjectedConv == ENABLE)
  1297. {
  1298. /* Update ADC state machine to error */
  1299. SET_BIT(hadc->State, HAL_ADC_STATE_ERROR_CONFIG);
  1300. tmp_hal_status = HAL_ERROR;
  1301. }
  1302. else
  1303. {
  1304. CLEAR_BIT(hadc->Instance->CFGR, ADC_CFGR_JAUTO);
  1305. }
  1306. }
  1307. if (sConfigInjected->InjecOversamplingMode == ENABLE)
  1308. {
  1309. assert_param(IS_ADC_OVERSAMPLING_RATIO(sConfigInjected->InjecOversampling.Ratio));
  1310. assert_param(IS_ADC_RIGHT_BIT_SHIFT(sConfigInjected->InjecOversampling.RightBitShift));
  1311. /* JOVSE must be reset in case of triggered regular mode */
  1312. assert_param(!(READ_BIT(hadc->Instance->CFGR2, ADC_CFGR2_ROVSE | ADC_CFGR2_TROVS) == (ADC_CFGR2_ROVSE | ADC_CFGR2_TROVS)));
  1313. /* Configuration of Injected Oversampler: */
  1314. /* - Oversampling Ratio */
  1315. /* - Right bit shift */
  1316. /* Enable OverSampling mode */
  1317. MODIFY_REG(hadc->Instance->CFGR2,
  1318. ADC_CFGR2_JOVSE |
  1319. ADC_CFGR2_OVSR |
  1320. ADC_CFGR2_OVSS,
  1321. ADC_CFGR2_JOVSE |
  1322. sConfigInjected->InjecOversampling.Ratio |
  1323. sConfigInjected->InjecOversampling.RightBitShift
  1324. );
  1325. }
  1326. else
  1327. {
  1328. /* Disable Regular OverSampling */
  1329. CLEAR_BIT(hadc->Instance->CFGR2, ADC_CFGR2_JOVSE);
  1330. }
  1331. /* Set sampling time of the selected ADC channel */
  1332. LL_ADC_SetChannelSamplingTime(hadc->Instance, sConfigInjected->InjectedChannel, sConfigInjected->InjectedSamplingTime);
  1333. /* Configure the offset: offset enable/disable, channel, offset value */
  1334. /* Shift the offset with respect to the selected ADC resolution. */
  1335. /* Offset has to be left-aligned on bit 11, the LSB (right bits) are set to 0 */
  1336. tmpOffsetShifted = ADC_OFFSET_SHIFT_RESOLUTION(hadc, sConfigInjected->InjectedOffset);
  1337. if (sConfigInjected->InjectedOffsetNumber != ADC_OFFSET_NONE)
  1338. {
  1339. /* Set ADC selected offset number */
  1340. LL_ADC_SetOffset(hadc->Instance, sConfigInjected->InjectedOffsetNumber, sConfigInjected->InjectedChannel,
  1341. tmpOffsetShifted);
  1342. }
  1343. else
  1344. {
  1345. /* Scan each offset register to check if the selected channel is targeted. */
  1346. /* If this is the case, the corresponding offset number is disabled. */
  1347. if(__LL_ADC_CHANNEL_TO_DECIMAL_NB(LL_ADC_GetOffsetChannel(hadc->Instance, LL_ADC_OFFSET_1)) == __LL_ADC_CHANNEL_TO_DECIMAL_NB(sConfigInjected->InjectedChannel))
  1348. {
  1349. LL_ADC_SetOffsetState(hadc->Instance, LL_ADC_OFFSET_1, LL_ADC_OFFSET_DISABLE);
  1350. }
  1351. if(__LL_ADC_CHANNEL_TO_DECIMAL_NB(LL_ADC_GetOffsetChannel(hadc->Instance, LL_ADC_OFFSET_2)) == __LL_ADC_CHANNEL_TO_DECIMAL_NB(sConfigInjected->InjectedChannel))
  1352. {
  1353. LL_ADC_SetOffsetState(hadc->Instance, LL_ADC_OFFSET_2, LL_ADC_OFFSET_DISABLE);
  1354. }
  1355. if(__LL_ADC_CHANNEL_TO_DECIMAL_NB(LL_ADC_GetOffsetChannel(hadc->Instance, LL_ADC_OFFSET_3)) == __LL_ADC_CHANNEL_TO_DECIMAL_NB(sConfigInjected->InjectedChannel))
  1356. {
  1357. LL_ADC_SetOffsetState(hadc->Instance, LL_ADC_OFFSET_3, LL_ADC_OFFSET_DISABLE);
  1358. }
  1359. if(__LL_ADC_CHANNEL_TO_DECIMAL_NB(LL_ADC_GetOffsetChannel(hadc->Instance, LL_ADC_OFFSET_4)) == __LL_ADC_CHANNEL_TO_DECIMAL_NB(sConfigInjected->InjectedChannel))
  1360. {
  1361. LL_ADC_SetOffsetState(hadc->Instance, LL_ADC_OFFSET_4, LL_ADC_OFFSET_DISABLE);
  1362. }
  1363. }
  1364. }
  1365. /* Parameters update conditioned to ADC state: */
  1366. /* Parameters that can be updated only when ADC is disabled: */
  1367. /* - Single or differential mode */
  1368. /* - Internal measurement channels: Vbat/VrefInt/TempSensor */
  1369. if (LL_ADC_IsEnabled(hadc->Instance) == 0UL)
  1370. {
  1371. /* Set mode single-ended or differential input of the selected ADC channel */
  1372. LL_ADC_SetChannelSingleDiff(hadc->Instance, sConfigInjected->InjectedChannel, sConfigInjected->InjectedSingleDiff);
  1373. /* Configuration of differential mode */
  1374. /* Note: ADC channel number masked with value "0x1F" to ensure shift value within 32 bits range */
  1375. if (sConfigInjected->InjectedSingleDiff == ADC_DIFFERENTIAL_ENDED)
  1376. {
  1377. /* Set sampling time of the selected ADC channel */
  1378. LL_ADC_SetChannelSamplingTime(hadc->Instance, (uint32_t)(__LL_ADC_DECIMAL_NB_TO_CHANNEL((__LL_ADC_CHANNEL_TO_DECIMAL_NB((uint32_t)sConfigInjected->InjectedChannel) + 1UL) & 0x1FUL)), sConfigInjected->InjectedSamplingTime);
  1379. }
  1380. /* Management of internal measurement channels: Vbat/VrefInt/TempSensor */
  1381. /* internal measurement paths enable: If internal channel selected, */
  1382. /* enable dedicated internal buffers and path. */
  1383. /* Note: these internal measurement paths can be disabled using */
  1384. /* HAL_ADC_DeInit(). */
  1385. if(__LL_ADC_IS_CHANNEL_INTERNAL(sConfigInjected->InjectedChannel))
  1386. {
  1387. /* Configuration of common ADC parameters (continuation) */
  1388. /* Software is allowed to change common parameters only when all ADCs */
  1389. /* of the common group are disabled. */
  1390. if (__LL_ADC_IS_ENABLED_ALL_COMMON_INSTANCE(__LL_ADC_COMMON_INSTANCE(hadc->Instance)) == 0UL)
  1391. {
  1392. tmp_config_internal_channel = LL_ADC_GetCommonPathInternalCh(__LL_ADC_COMMON_INSTANCE(hadc->Instance));
  1393. /* If the requested internal measurement path has already been enabled, */
  1394. /* bypass the configuration processing. */
  1395. if ((sConfigInjected->InjectedChannel == ADC_CHANNEL_TEMPSENSOR) && ((tmp_config_internal_channel & LL_ADC_PATH_INTERNAL_TEMPSENSOR) == 0UL))
  1396. {
  1397. if (ADC_TEMPERATURE_SENSOR_INSTANCE(hadc))
  1398. {
  1399. LL_ADC_SetCommonPathInternalCh(__LL_ADC_COMMON_INSTANCE(hadc->Instance), LL_ADC_PATH_INTERNAL_TEMPSENSOR | tmp_config_internal_channel);
  1400. /* Delay for temperature sensor stabilization time */
  1401. /* Wait loop initialization and execution */
  1402. /* Note: Variable divided by 2 to compensate partially */
  1403. /* CPU processing cycles, scaling in us split to not */
  1404. /* exceed 32 bits register capacity and handle low frequency. */
  1405. wait_loop_index = ((LL_ADC_DELAY_TEMPSENSOR_STAB_US / 10UL) * (SystemCoreClock / (100000UL * 2UL)));
  1406. while(wait_loop_index != 0UL)
  1407. {
  1408. wait_loop_index--;
  1409. }
  1410. }
  1411. }
  1412. else if ((sConfigInjected->InjectedChannel == ADC_CHANNEL_VBAT) && ((tmp_config_internal_channel & LL_ADC_PATH_INTERNAL_VBAT) == 0UL))
  1413. {
  1414. if (ADC_BATTERY_VOLTAGE_INSTANCE(hadc))
  1415. {
  1416. LL_ADC_SetCommonPathInternalCh(__LL_ADC_COMMON_INSTANCE(hadc->Instance), LL_ADC_PATH_INTERNAL_VBAT | tmp_config_internal_channel);
  1417. }
  1418. }
  1419. else if ((sConfigInjected->InjectedChannel == ADC_CHANNEL_VREFINT) && ((tmp_config_internal_channel & LL_ADC_PATH_INTERNAL_VREFINT) == 0UL))
  1420. {
  1421. if (ADC_VREFINT_INSTANCE(hadc))
  1422. {
  1423. LL_ADC_SetCommonPathInternalCh(__LL_ADC_COMMON_INSTANCE(hadc->Instance), LL_ADC_PATH_INTERNAL_VREFINT | tmp_config_internal_channel);
  1424. }
  1425. }
  1426. else
  1427. {
  1428. /* nothing to do */
  1429. }
  1430. }
  1431. /* If the requested internal measurement path has already been enabled */
  1432. /* and other ADC of the common group are enabled, internal */
  1433. /* measurement paths cannot be enabled. */
  1434. else
  1435. {
  1436. /* Update ADC state machine to error */
  1437. SET_BIT(hadc->State, HAL_ADC_STATE_ERROR_CONFIG);
  1438. tmp_hal_status = HAL_ERROR;
  1439. }
  1440. }
  1441. }
  1442. /* Process unlocked */
  1443. __HAL_UNLOCK(hadc);
  1444. /* Return function status */
  1445. return tmp_hal_status;
  1446. }
  1447. #endif /* ADC_SUPPORT_2_5_MSPS */
  1448. #if defined(ADC_SUPPORT_2_5_MSPS)
  1449. /* Feature "ADC group injected" not available on ADC peripheral of this STM32WB device */
  1450. #else
  1451. /**
  1452. * @brief Enable Injected Queue
  1453. * @note This function resets CFGR register JQDIS bit in order to enable the
  1454. * Injected Queue. JQDIS can be written only when ADSTART and JDSTART
  1455. * are both equal to 0 to ensure that no regular nor injected
  1456. * conversion is ongoing.
  1457. * @param hadc ADC handle
  1458. * @retval HAL status
  1459. */
  1460. HAL_StatusTypeDef HAL_ADCEx_EnableInjectedQueue(ADC_HandleTypeDef *hadc)
  1461. {
  1462. HAL_StatusTypeDef tmp_hal_status;
  1463. uint32_t tmp_adc_is_conversion_on_going_regular;
  1464. uint32_t tmp_adc_is_conversion_on_going_injected;
  1465. /* Check the parameters */
  1466. assert_param(IS_ADC_ALL_INSTANCE(hadc->Instance));
  1467. tmp_adc_is_conversion_on_going_regular = LL_ADC_REG_IsConversionOngoing(hadc->Instance);
  1468. tmp_adc_is_conversion_on_going_injected = LL_ADC_INJ_IsConversionOngoing(hadc->Instance);
  1469. /* Parameter can be set only if no conversion is on-going */
  1470. if ((tmp_adc_is_conversion_on_going_regular == 0UL)
  1471. && (tmp_adc_is_conversion_on_going_injected == 0UL)
  1472. )
  1473. {
  1474. CLEAR_BIT(hadc->Instance->CFGR, ADC_CFGR_JQDIS);
  1475. /* Update state, clear previous result related to injected queue overflow */
  1476. CLEAR_BIT(hadc->State, HAL_ADC_STATE_INJ_JQOVF);
  1477. tmp_hal_status = HAL_OK;
  1478. }
  1479. else
  1480. {
  1481. tmp_hal_status = HAL_ERROR;
  1482. }
  1483. return tmp_hal_status;
  1484. }
  1485. /**
  1486. * @brief Disable Injected Queue
  1487. * @note This function sets CFGR register JQDIS bit in order to disable the
  1488. * Injected Queue. JQDIS can be written only when ADSTART and JDSTART
  1489. * are both equal to 0 to ensure that no regular nor injected
  1490. * conversion is ongoing.
  1491. * @param hadc ADC handle
  1492. * @retval HAL status
  1493. */
  1494. HAL_StatusTypeDef HAL_ADCEx_DisableInjectedQueue(ADC_HandleTypeDef *hadc)
  1495. {
  1496. HAL_StatusTypeDef tmp_hal_status;
  1497. uint32_t tmp_adc_is_conversion_on_going_regular;
  1498. uint32_t tmp_adc_is_conversion_on_going_injected;
  1499. /* Check the parameters */
  1500. assert_param(IS_ADC_ALL_INSTANCE(hadc->Instance));
  1501. tmp_adc_is_conversion_on_going_regular = LL_ADC_REG_IsConversionOngoing(hadc->Instance);
  1502. tmp_adc_is_conversion_on_going_injected = LL_ADC_INJ_IsConversionOngoing(hadc->Instance);
  1503. /* Parameter can be set only if no conversion is on-going */
  1504. if ((tmp_adc_is_conversion_on_going_regular == 0UL)
  1505. && (tmp_adc_is_conversion_on_going_injected == 0UL)
  1506. )
  1507. {
  1508. LL_ADC_INJ_SetQueueMode(hadc->Instance, LL_ADC_INJ_QUEUE_DISABLE);
  1509. tmp_hal_status = HAL_OK;
  1510. }
  1511. else
  1512. {
  1513. tmp_hal_status = HAL_ERROR;
  1514. }
  1515. return tmp_hal_status;
  1516. }
  1517. #endif /* ADC_SUPPORT_2_5_MSPS */
  1518. /**
  1519. * @brief Disable ADC voltage regulator.
  1520. * @note Disabling voltage regulator allows to save power. This operation can
  1521. * be carried out only when ADC is disabled.
  1522. * @note To enable again the voltage regulator, the user is expected to
  1523. * resort to HAL_ADC_Init() API.
  1524. * @param hadc ADC handle
  1525. * @retval HAL status
  1526. */
  1527. HAL_StatusTypeDef HAL_ADCEx_DisableVoltageRegulator(ADC_HandleTypeDef *hadc)
  1528. {
  1529. HAL_StatusTypeDef tmp_hal_status;
  1530. /* Check the parameters */
  1531. assert_param(IS_ADC_ALL_INSTANCE(hadc->Instance));
  1532. /* Setting of this feature is conditioned to ADC state: ADC must be ADC disabled */
  1533. if (LL_ADC_IsEnabled(hadc->Instance) == 0UL)
  1534. {
  1535. LL_ADC_DisableInternalRegulator(hadc->Instance);
  1536. tmp_hal_status = HAL_OK;
  1537. }
  1538. else
  1539. {
  1540. tmp_hal_status = HAL_ERROR;
  1541. }
  1542. return tmp_hal_status;
  1543. }
  1544. #if defined(ADC_SUPPORT_2_5_MSPS)
  1545. /* Feature " ADC deep power-down" not available on ADC peripheral of this STM32WB device */
  1546. #else
  1547. /**
  1548. * @brief Enter ADC deep power-down mode
  1549. * @note This mode is achieved in setting DEEPPWD bit and allows to save power
  1550. * in reducing leakage currents. It is particularly interesting before
  1551. * entering stop modes.
  1552. * @note Setting DEEPPWD automatically clears ADVREGEN bit and disables the
  1553. * ADC voltage regulator. This means that this API encompasses
  1554. * HAL_ADCEx_DisableVoltageRegulator(). Additionally, the internal
  1555. * calibration is lost.
  1556. * @note To exit the ADC deep-power-down mode, the user is expected to
  1557. * resort to HAL_ADC_Init() API as well as to relaunch a calibration
  1558. * with HAL_ADCEx_Calibration_Start() API or to re-apply a previously
  1559. * saved calibration factor.
  1560. * @param hadc ADC handle
  1561. * @retval HAL status
  1562. */
  1563. HAL_StatusTypeDef HAL_ADCEx_EnterADCDeepPowerDownMode(ADC_HandleTypeDef *hadc)
  1564. {
  1565. HAL_StatusTypeDef tmp_hal_status;
  1566. /* Check the parameters */
  1567. assert_param(IS_ADC_ALL_INSTANCE(hadc->Instance));
  1568. /* Setting of this feature is conditioned to ADC state: ADC must be ADC disabled */
  1569. if (LL_ADC_IsEnabled(hadc->Instance) == 0UL)
  1570. {
  1571. LL_ADC_EnableDeepPowerDown(hadc->Instance);
  1572. tmp_hal_status = HAL_OK;
  1573. }
  1574. else
  1575. {
  1576. tmp_hal_status = HAL_ERROR;
  1577. }
  1578. return tmp_hal_status;
  1579. }
  1580. #endif /* ADC_SUPPORT_2_5_MSPS */
  1581. /**
  1582. * @}
  1583. */
  1584. /**
  1585. * @}
  1586. */
  1587. #endif /* HAL_ADC_MODULE_ENABLED */
  1588. /**
  1589. * @}
  1590. */
  1591. /**
  1592. * @}
  1593. */