py32f0xx_hal_adc.c 88 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483
  1. /**
  2. ******************************************************************************
  3. * @file py32f0xx_hal_adc.c
  4. * @author MCU Application Team
  5. * @brief This file provides firmware functions to manage the following
  6. * functionalities of the Analog to Digital Convertor (ADC)
  7. * peripheral:
  8. * + Initialization and de-initialization functions
  9. * ++ Initialization and Configuration of ADC
  10. * + Operation functions
  11. * ++ Start, stop, get result of conversions of regular
  12. * group, using 3 possible modes: polling, interruption or DMA.
  13. * + Control functions
  14. * ++ Channels configuration on regular group
  15. * ++ Analog Watchdog configuration
  16. * + State functions
  17. * ++ ADC state machine management
  18. * ++ Interrupts and flags management
  19. *
  20. @verbatim
  21. ==============================================================================
  22. ##### ADC peripheral features #####
  23. ==============================================================================
  24. [..]
  25. (+) 12-bit, 10-bit, 8-bit or 6-bit configurable resolution
  26. (+) Interrupt generation at the end of regular conversion and in case of
  27. analog watchdog or overrun events.
  28. (+) Single and continuous conversion modes.
  29. (+) Scan mode for conversion of several channels sequentially.
  30. (+) Data alignment with in-built data coherency.
  31. (+) Programmable sampling time (common for all channels)
  32. (+) ADC conversion of regular group.
  33. (+) External trigger (timer or EXTI) with configurable polarity
  34. (+) DMA request generation for transfer of conversions data of regular group.
  35. (+) ADC calibration
  36. (+) ADC supply requirements: 2.4 V to 3.6 V at full speed and down to 1.8 V at
  37. slower speed.
  38. (+) ADC input range: from Vref- (connected to Vssa) to Vref+ (connected to
  39. Vdda or to an external voltage reference).
  40. @endverbatim
  41. ******************************************************************************
  42. * @attention
  43. *
  44. * <h2><center>&copy; Copyright (c) 2023 Puya Semiconductor Co.
  45. * All rights reserved.</center></h2>
  46. *
  47. * This software component is licensed by Puya under BSD 3-Clause license,
  48. * the "License"; You may not use this file except in compliance with the
  49. * License. You may obtain a copy of the License at:
  50. * opensource.org/licenses/BSD-3-Clause
  51. *
  52. ******************************************************************************
  53. * @attention
  54. *
  55. * <h2><center>&copy; Copyright (c) 2016 STMicroelectronics.
  56. * All rights reserved.</center></h2>
  57. *
  58. * This software component is licensed by ST under BSD 3-Clause license,
  59. * the "License"; You may not use this file except in compliance with the
  60. * License. You may obtain a copy of the License at:
  61. * opensource.org/licenses/BSD-3-Clause
  62. *
  63. ******************************************************************************
  64. */
  65. /* Includes ------------------------------------------------------------------*/
  66. #include "py32f0xx_hal.h"
  67. /** @addtogroup PY32F0xx_HAL_Driver
  68. * @{
  69. */
  70. /** @defgroup ADC ADC
  71. * @brief ADC HAL module driver
  72. * @{
  73. */
  74. #ifdef HAL_ADC_MODULE_ENABLED
  75. /* Private typedef -----------------------------------------------------------*/
  76. /* Private define ------------------------------------------------------------*/
  77. /** @defgroup ADC_Private_Constants ADC Private Constants
  78. * @{
  79. */
  80. /* Fixed timeout values for ADC calibration, enable settling time, disable */
  81. /* settling time. */
  82. /* Values defined to be higher than worst cases: low clock frequency, */
  83. /* maximum prescaler. */
  84. /* Ex of profile low frequency : Clock source at 0.1 MHz, ADC clock */
  85. /* prescaler 4, sampling time 7.5 ADC clock cycles, resolution 12 bits. */
  86. /* Unit: ms */
  87. #define ADC_ENABLE_TIMEOUT ( 2U)
  88. #define ADC_DISABLE_TIMEOUT ( 2U)
  89. #define ADC_STOP_CONVERSION_TIMEOUT ( 2U)
  90. /* Delay for ADC stabilization time. */
  91. /* Maximum delay is 1us (refer to device datasheet, parameter tSTAB). */
  92. /* Unit: us */
  93. #define ADC_STAB_DELAY_US ( 1U)
  94. /* Delay for temperature sensor stabilization time. */
  95. /* Maximum delay is 10us (refer to device datasheet, parameter tSTART). */
  96. /* Unit: us */
  97. #define ADC_TEMPSENSOR_DELAY_US ( 10U)
  98. /* Fixed timeout values for ADC calibration, enable settling time, disable */
  99. /* settling time. */
  100. /* Values defined to be higher than worst cases: low clock frequency, */
  101. /* maximum prescaler. */
  102. /* Ex of profile low frequency : Clock source at 0.1 MHz, ADC clock */
  103. /* prescaler 4. */
  104. /* Unit: ms */
  105. #define ADC_CALIBRATION_TIMEOUT ( 2U)
  106. /**
  107. * @}
  108. */
  109. /* Private macro -------------------------------------------------------------*/
  110. /* Private variables ---------------------------------------------------------*/
  111. /* Private function prototypes -----------------------------------------------*/
  112. /** @defgroup ADC_Private_Functions ADC Private Functions
  113. * @{
  114. */
  115. static HAL_StatusTypeDef ADC_Enable(ADC_HandleTypeDef* hadc);
  116. static HAL_StatusTypeDef ADC_Disable(ADC_HandleTypeDef* hadc);
  117. static HAL_StatusTypeDef ADC_ConversionStop(ADC_HandleTypeDef* hadc);
  118. static void ADC_QuickSort(int32_t arr[], int32_t length);
  119. #if (defined(DMA) || defined(DMA1))
  120. static void ADC_DMAConvCplt(DMA_HandleTypeDef *hdma);
  121. static void ADC_DMAHalfConvCplt(DMA_HandleTypeDef *hdma);
  122. static void ADC_DMAError(DMA_HandleTypeDef *hdma);
  123. #endif
  124. /**
  125. * @}
  126. */
  127. /* Exported functions ---------------------------------------------------------*/
  128. /** @defgroup ADC_Exported_Functions ADC Exported Functions
  129. * @{
  130. */
  131. /** @defgroup ADC_Exported_Functions_Group1 Initialization/de-initialization functions
  132. * @brief Initialization and Configuration functions
  133. *
  134. @verbatim
  135. ===============================================================================
  136. ##### Initialization and de-initialization functions #####
  137. ===============================================================================
  138. [..] This section provides functions allowing to:
  139. (+) Initialize and configure the ADC.
  140. (+) De-initialize the ADC
  141. @endverbatim
  142. * @{
  143. */
  144. /**
  145. * @brief Initializes the ADC peripheral and regular group according to
  146. * parameters specified in structure "ADC_InitTypeDef".
  147. * @note As prerequisite, ADC clock must be configured at RCC top level
  148. * depending on both possible clock sources: APB clock or HSI clock.
  149. * See commented example code below that can be copied and uncommented
  150. * into HAL_ADC_MspInit().
  151. * @note Possibility to update parameters on the fly:
  152. * This function initializes the ADC MSP (HAL_ADC_MspInit()) only when
  153. * coming from ADC state reset. Following calls to this function can
  154. * be used to reconfigure some parameters of ADC_InitTypeDef
  155. * structure on the fly, without modifying MSP configuration. If ADC
  156. * MSP has to be modified again, HAL_ADC_DeInit() must be called
  157. * before HAL_ADC_Init().
  158. * The setting of these parameters is conditioned to ADC state.
  159. * For parameters constraints, see comments of structure
  160. * "ADC_InitTypeDef".
  161. * @note This function configures the ADC within 2 scopes: scope of entire
  162. * ADC and scope of regular group. For parameters details, see comments
  163. * of structure "ADC_InitTypeDef".
  164. * @param hadc ADC handle
  165. * @retval HAL status
  166. */
  167. HAL_StatusTypeDef HAL_ADC_Init(ADC_HandleTypeDef* hadc)
  168. {
  169. HAL_StatusTypeDef tmp_hal_status = HAL_OK;
  170. uint32_t tmpCFGR1 = 0U;
  171. /* Check ADC handle */
  172. if(hadc == NULL)
  173. {
  174. return HAL_ERROR;
  175. }
  176. /* Check the parameters */
  177. assert_param(IS_ADC_ALL_INSTANCE(hadc->Instance));
  178. assert_param(IS_ADC_CLOCKPRESCALER(hadc->Init.ClockPrescaler));
  179. assert_param(IS_ADC_RESOLUTION(hadc->Init.Resolution));
  180. assert_param(IS_ADC_DATA_ALIGN(hadc->Init.DataAlign));
  181. assert_param(IS_ADC_SCAN_MODE(hadc->Init.ScanConvMode));
  182. assert_param(IS_FUNCTIONAL_STATE(hadc->Init.ContinuousConvMode));
  183. assert_param(IS_FUNCTIONAL_STATE(hadc->Init.DiscontinuousConvMode));
  184. assert_param(IS_ADC_EXTTRIG_EDGE(hadc->Init.ExternalTrigConvEdge));
  185. assert_param(IS_ADC_EXTTRIG(hadc->Init.ExternalTrigConv));
  186. #if (defined(DMA) || defined(DMA1))
  187. assert_param(IS_FUNCTIONAL_STATE(hadc->Init.DMAContinuousRequests));
  188. #endif
  189. assert_param(IS_ADC_EOC_SELECTION(hadc->Init.EOCSelection));
  190. assert_param(IS_ADC_OVERRUN(hadc->Init.Overrun));
  191. assert_param(IS_FUNCTIONAL_STATE(hadc->Init.LowPowerAutoWait));
  192. /* As prerequisite, into HAL_ADC_MspInit(), ADC clock must be configured */
  193. /* at RCC top level depending on both possible clock sources: */
  194. /* APB clock or HSI clock. */
  195. /* Refer to header of this file for more details on clock enabling procedure*/
  196. /* Actions performed only if ADC is coming from state reset: */
  197. /* - Initialization of ADC MSP */
  198. /* - ADC voltage regulator enable */
  199. if (hadc->State == HAL_ADC_STATE_RESET)
  200. {
  201. /* Initialize ADC error code */
  202. ADC_CLEAR_ERRORCODE(hadc);
  203. /* Allocate lock resource and initialize it */
  204. hadc->Lock = HAL_UNLOCKED;
  205. #if (USE_HAL_ADC_REGISTER_CALLBACKS == 1)
  206. /* Init the ADC Callback settings */
  207. hadc->ConvCpltCallback = HAL_ADC_ConvCpltCallback; /* Legacy weak callback */
  208. hadc->ConvHalfCpltCallback = HAL_ADC_ConvHalfCpltCallback; /* Legacy weak callback */
  209. hadc->LevelOutOfWindowCallback = HAL_ADC_LevelOutOfWindowCallback; /* Legacy weak callback */
  210. hadc->ErrorCallback = HAL_ADC_ErrorCallback; /* Legacy weak callback */
  211. if (hadc->MspInitCallback == NULL)
  212. {
  213. hadc->MspInitCallback = HAL_ADC_MspInit; /* Legacy weak MspInit */
  214. }
  215. /* Init the low level hardware */
  216. hadc->MspInitCallback(hadc);
  217. #else
  218. /* Init the low level hardware */
  219. HAL_ADC_MspInit(hadc);
  220. #endif /* USE_HAL_ADC_REGISTER_CALLBACKS */
  221. }
  222. /* Configuration of ADC parameters if previous preliminary actions are */
  223. /* correctly completed. */
  224. /* and if there is no conversion on going on regular group (ADC can be */
  225. /* enabled anyway, in case of call of this function to update a parameter */
  226. /* on the fly). */
  227. if (HAL_IS_BIT_CLR(hadc->State, HAL_ADC_STATE_ERROR_INTERNAL) &&
  228. (tmp_hal_status == HAL_OK) &&
  229. (ADC_IS_CONVERSION_ONGOING_REGULAR(hadc) == RESET) )
  230. {
  231. /* Set ADC state */
  232. ADC_STATE_CLR_SET(hadc->State,
  233. HAL_ADC_STATE_REG_BUSY,
  234. HAL_ADC_STATE_BUSY_INTERNAL);
  235. /* Parameters update conditioned to ADC state: */
  236. /* Parameters that can be updated only when ADC is disabled: */
  237. /* - ADC clock mode */
  238. /* - ADC clock prescaler */
  239. /* - ADC resolution */
  240. if (ADC_IS_ENABLE(hadc) == RESET)
  241. {
  242. /* Some parameters of this register are not reset, since they are set */
  243. /* by other functions and must be kept in case of usage of this */
  244. /* function on the fly (update of a parameter of ADC_InitTypeDef */
  245. /* without needing to reconfigure all other ADC groups/channels */
  246. /* parameters): */
  247. /* - internal measurement paths: temperature sensor, Vref */
  248. /* (set into HAL_ADC_ConfigChannel() ) */
  249. /* Configuration of ADC resolution */
  250. MODIFY_REG(hadc->Instance->CFGR1,
  251. ADC_CFGR1_RESSEL,
  252. hadc->Init.Resolution );
  253. /* Configuration of ADC clock mode: clock source AHB or HSI with */
  254. /* selectable prescaler */
  255. MODIFY_REG(hadc->Instance->CFGR2,
  256. ADC_CFGR2_CKMODE,
  257. hadc->Init.ClockPrescaler );
  258. }
  259. /* Configuration of ADC: */
  260. /* - discontinuous mode */
  261. /* - LowPowerAutoWait mode */
  262. /* - continuous conversion mode */
  263. /* - overrun */
  264. /* - external trigger to start conversion */
  265. /* - external trigger polarity */
  266. /* - data alignment */
  267. /* - resolution */
  268. /* - scan direction */
  269. /* - DMA continuous request */
  270. #if (defined(DMA) || defined(DMA1))
  271. hadc->Instance->CFGR1 &= ~( ADC_CFGR1_DISCEN |
  272. ADC_CFGR1_CONT |
  273. ADC_CFGR1_OVRMOD |
  274. ADC_CFGR1_EXTSEL |
  275. ADC_CFGR1_EXTEN |
  276. ADC_CFGR1_ALIGN |
  277. ADC_CFGR1_SCANDIR |
  278. ADC_CFGR1_DMACFG );
  279. tmpCFGR1 |= (ADC_CFGR1_AUTOWAIT((uint32_t)hadc->Init.LowPowerAutoWait) |
  280. ADC_CFGR1_CONTINUOUS((uint32_t)hadc->Init.ContinuousConvMode) |
  281. ADC_CFGR1_OVERRUN(hadc->Init.Overrun) |
  282. hadc->Init.DataAlign |
  283. ADC_SCANDIR(hadc->Init.ScanConvMode) |
  284. ADC_CFGR1_DMACONTREQ((uint32_t)hadc->Init.DMAContinuousRequests) );
  285. #else
  286. hadc->Instance->CFGR1 &= ~( ADC_CFGR1_DISCEN |
  287. ADC_CFGR1_CONT |
  288. ADC_CFGR1_OVRMOD |
  289. ADC_CFGR1_EXTSEL |
  290. ADC_CFGR1_EXTEN |
  291. ADC_CFGR1_ALIGN |
  292. ADC_CFGR1_SCANDIR );
  293. tmpCFGR1 |= (ADC_CFGR1_AUTOWAIT((uint32_t)hadc->Init.LowPowerAutoWait) |
  294. ADC_CFGR1_CONTINUOUS((uint32_t)hadc->Init.ContinuousConvMode) |
  295. ADC_CFGR1_OVERRUN(hadc->Init.Overrun) |
  296. hadc->Init.DataAlign |
  297. ADC_SCANDIR(hadc->Init.ScanConvMode) );
  298. #endif
  299. /* Enable discontinuous mode only if continuous mode is disabled */
  300. if (hadc->Init.DiscontinuousConvMode == ENABLE)
  301. {
  302. if (hadc->Init.ContinuousConvMode == DISABLE)
  303. {
  304. /* Enable the selected ADC group regular discontinuous mode */
  305. tmpCFGR1 |= ADC_CFGR1_DISCEN;
  306. }
  307. else
  308. {
  309. /* ADC regular group discontinuous was intended to be enabled, */
  310. /* but ADC regular group modes continuous and sequencer discontinuous */
  311. /* cannot be enabled simultaneously. */
  312. /* Update ADC state machine to error */
  313. SET_BIT(hadc->State, HAL_ADC_STATE_ERROR_CONFIG);
  314. /* Set ADC error code to ADC IP internal error */
  315. SET_BIT(hadc->ErrorCode, HAL_ADC_ERROR_INTERNAL);
  316. }
  317. }
  318. /* Enable external trigger if trigger selection is different of software */
  319. /* start. */
  320. /* Note: This configuration keeps the hardware feature of parameter */
  321. /* ExternalTrigConvEdge "trigger edge none" equivalent to */
  322. /* software start. */
  323. if (hadc->Init.ExternalTrigConv != ADC_SOFTWARE_START)
  324. {
  325. tmpCFGR1 |= ( hadc->Init.ExternalTrigConv |
  326. hadc->Init.ExternalTrigConvEdge );
  327. }
  328. /* Update ADC configuration register with previous settings */
  329. hadc->Instance->CFGR1 |= tmpCFGR1;
  330. /* Channel sampling time configuration */
  331. /* Management of parameters "SamplingTimeCommon" and "SamplingTime" */
  332. /* (obsolete): sampling time set in this function if parameter */
  333. /* "SamplingTimeCommon" has been set to a valid sampling time. */
  334. /* Otherwise, sampling time is set into ADC channel initialization */
  335. /* structure with parameter "SamplingTime" (obsolete). */
  336. if (IS_ADC_SAMPLE_TIME(hadc->Init.SamplingTimeCommon))
  337. {
  338. /* Channel sampling time configuration */
  339. /* Clear the old sample time */
  340. hadc->Instance->SMPR &= ~(ADC_SMPR_SMP);
  341. /* Set the new sample time */
  342. hadc->Instance->SMPR |= ADC_SMPR_SET(hadc->Init.SamplingTimeCommon);
  343. }
  344. /* Check back that ADC registers have effectively been configured to */
  345. /* ensure of no potential problem of ADC core IP clocking. */
  346. /* Check through register CFGR1 (excluding analog watchdog configuration: */
  347. /* set into separate dedicated function, and bits of ADC resolution set */
  348. /* out of temporary variable 'tmpCFGR1'). */
  349. if ((hadc->Instance->CFGR1 & ~(ADC_CFGR1_AWDCH | ADC_CFGR1_AWDEN | ADC_CFGR1_AWDSGL | ADC_CFGR1_RESSEL))
  350. == tmpCFGR1)
  351. {
  352. /* Set ADC error code to none */
  353. ADC_CLEAR_ERRORCODE(hadc);
  354. /* Set the ADC state */
  355. ADC_STATE_CLR_SET(hadc->State,
  356. HAL_ADC_STATE_BUSY_INTERNAL,
  357. HAL_ADC_STATE_READY);
  358. }
  359. else
  360. {
  361. /* Update ADC state machine to error */
  362. ADC_STATE_CLR_SET(hadc->State,
  363. HAL_ADC_STATE_BUSY_INTERNAL,
  364. HAL_ADC_STATE_ERROR_INTERNAL);
  365. /* Set ADC error code to ADC IP internal error */
  366. SET_BIT(hadc->ErrorCode, HAL_ADC_ERROR_INTERNAL);
  367. tmp_hal_status = HAL_ERROR;
  368. }
  369. }
  370. else
  371. {
  372. /* Update ADC state machine to error */
  373. SET_BIT(hadc->State, HAL_ADC_STATE_ERROR_INTERNAL);
  374. tmp_hal_status = HAL_ERROR;
  375. }
  376. /* Return function status */
  377. return tmp_hal_status;
  378. }
  379. /**
  380. * @brief Deinitialize the ADC peripheral registers to their default reset
  381. * values, with deinitialization of the ADC MSP.
  382. * @note For devices with several ADCs: reset of ADC common registers is done
  383. * only if all ADCs sharing the same common group are disabled.
  384. * If this is not the case, reset of these common parameters reset is
  385. * bypassed without error reporting: it can be the intended behaviour in
  386. * case of reset of a single ADC while the other ADCs sharing the same
  387. * common group is still running.
  388. * @param hadc ADC handle
  389. * @retval HAL status
  390. */
  391. HAL_StatusTypeDef HAL_ADC_DeInit(ADC_HandleTypeDef* hadc)
  392. {
  393. HAL_StatusTypeDef tmp_hal_status = HAL_OK;
  394. /* Check ADC handle */
  395. if(hadc == NULL)
  396. {
  397. return HAL_ERROR;
  398. }
  399. /* Check the parameters */
  400. assert_param(IS_ADC_ALL_INSTANCE(hadc->Instance));
  401. /* Set ADC state */
  402. SET_BIT(hadc->State, HAL_ADC_STATE_BUSY_INTERNAL);
  403. /* Stop potential conversion on going, on regular group */
  404. tmp_hal_status = ADC_ConversionStop(hadc);
  405. /* Disable ADC peripheral if conversions are effectively stopped */
  406. if (tmp_hal_status == HAL_OK)
  407. {
  408. /* Disable the ADC peripheral */
  409. tmp_hal_status = ADC_Disable(hadc);
  410. /* Check if ADC is effectively disabled */
  411. if (tmp_hal_status != HAL_ERROR)
  412. {
  413. /* Change ADC state */
  414. hadc->State = HAL_ADC_STATE_READY;
  415. }
  416. }
  417. /* Reset ADC peripheral */
  418. __HAL_RCC_ADC_FORCE_RESET();
  419. __HAL_RCC_ADC_RELEASE_RESET();
  420. /* Configuration of ADC parameters if previous preliminary actions are */
  421. /* correctly completed. */
  422. if (tmp_hal_status != HAL_ERROR)
  423. {
  424. /* ========== Reset ADC registers ========== */
  425. /* Reset register IER */
  426. __HAL_ADC_DISABLE_IT(hadc, (ADC_IT_AWD | ADC_IT_OVR |
  427. ADC_IT_EOS | ADC_IT_EOC |
  428. ADC_IT_EOSMP /*| ADC_IT_RDY*/ ) );
  429. /* Reset register ISR */
  430. __HAL_ADC_CLEAR_FLAG(hadc, (ADC_FLAG_AWD | ADC_FLAG_OVR |
  431. ADC_FLAG_EOS | ADC_FLAG_EOC |
  432. ADC_FLAG_EOSMP /*| ADC_FLAG_RDY*/ ) );
  433. /* Reset register CR */
  434. /* Bits ADC_CR_ADCAL, ADC_CR_ADSTP, ADC_CR_ADSTART are in access mode */
  435. /* "read-set": no direct reset applicable. */
  436. /* Reset register CFGR1 */
  437. #if (defined(DMA) || defined(DMA1))
  438. hadc->Instance->CFGR1 &= ~(ADC_CFGR1_AWDCH | ADC_CFGR1_AWDEN | ADC_CFGR1_AWDSGL | ADC_CFGR1_DISCEN |
  439. /*ADC_CFGR1_AUTOFF |*/ ADC_CFGR1_WAIT | ADC_CFGR1_CONT | ADC_CFGR1_OVRMOD |
  440. ADC_CFGR1_EXTEN | ADC_CFGR1_EXTSEL | ADC_CFGR1_ALIGN | ADC_CFGR1_RESSEL |
  441. ADC_CFGR1_SCANDIR | ADC_CFGR1_DMACFG | ADC_CFGR1_DMAEN );
  442. #else
  443. hadc->Instance->CFGR1 &= ~(ADC_CFGR1_AWDCH | ADC_CFGR1_AWDEN | ADC_CFGR1_AWDSGL | ADC_CFGR1_DISCEN |
  444. /*ADC_CFGR1_AUTOFF |*/ ADC_CFGR1_WAIT | ADC_CFGR1_CONT | ADC_CFGR1_OVRMOD |
  445. ADC_CFGR1_EXTEN | ADC_CFGR1_EXTSEL | ADC_CFGR1_ALIGN | ADC_CFGR1_RESSEL |
  446. ADC_CFGR1_SCANDIR );
  447. #endif
  448. /* Reset register CFGR2 */
  449. /* Note: Update of ADC clock mode is conditioned to ADC state disabled: */
  450. /* already done above. */
  451. hadc->Instance->CFGR2 &= ~ADC_CFGR2_CKMODE;
  452. /* Reset register SMPR */
  453. hadc->Instance->SMPR &= ~ADC_SMPR_SMP;
  454. /* Reset register TR */
  455. hadc->Instance->TR &= ~(ADC_TR_HT | ADC_TR_LT);
  456. /* Reset register CHSELR */
  457. hadc->Instance->CHSELR &= ~(ADC_CHSELR_CHSEL12 | ADC_CHSELR_CHSEL11 | ADC_CHSELR_CHSEL9 | ADC_CHSELR_CHSEL8 |
  458. ADC_CHSELR_CHSEL7 | ADC_CHSELR_CHSEL6 | ADC_CHSELR_CHSEL5 | ADC_CHSELR_CHSEL4 |
  459. ADC_CHSELR_CHSEL3 | ADC_CHSELR_CHSEL2 | ADC_CHSELR_CHSEL1 | ADC_CHSELR_CHSEL0 );
  460. /* Reset register DR */
  461. /* bits in access mode read only, no direct reset applicable*/
  462. /* Reset register CCR */
  463. ADC->CCR &= ~(ADC_CCR_ALL);
  464. /* ========== Hard reset ADC peripheral ========== */
  465. /* Performs a global reset of the entire ADC peripheral: ADC state is */
  466. /* forced to a similar state after device power-on. */
  467. /* If needed, copy-paste and uncomment the following reset code into */
  468. /* function "void HAL_ADC_MspInit(ADC_HandleTypeDef* hadc)": */
  469. /* */
  470. /* __HAL_RCC_ADC1_FORCE_RESET() */
  471. /* __HAL_RCC_ADC1_RELEASE_RESET() */
  472. #if (USE_HAL_ADC_REGISTER_CALLBACKS == 1)
  473. if (hadc->MspDeInitCallback == NULL)
  474. {
  475. hadc->MspDeInitCallback = HAL_ADC_MspDeInit; /* Legacy weak MspDeInit */
  476. }
  477. /* DeInit the low level hardware */
  478. hadc->MspDeInitCallback(hadc);
  479. #else
  480. /* DeInit the low level hardware */
  481. HAL_ADC_MspDeInit(hadc);
  482. #endif /* USE_HAL_ADC_REGISTER_CALLBACKS */
  483. /* Set ADC error code to none */
  484. ADC_CLEAR_ERRORCODE(hadc);
  485. /* Set ADC state */
  486. hadc->State = HAL_ADC_STATE_RESET;
  487. }
  488. /* Process unlocked */
  489. __HAL_UNLOCK(hadc);
  490. /* Return function status */
  491. return tmp_hal_status;
  492. }
  493. /**
  494. * @brief Initializes the ADC MSP.
  495. * @param hadc ADC handle
  496. * @retval None
  497. */
  498. __weak void HAL_ADC_MspInit(ADC_HandleTypeDef* hadc)
  499. {
  500. /* Prevent unused argument(s) compilation warning */
  501. UNUSED(hadc);
  502. /* NOTE : This function should not be modified. When the callback is needed,
  503. function HAL_ADC_MspInit must be implemented in the user file.
  504. */
  505. }
  506. /**
  507. * @brief DeInitializes the ADC MSP.
  508. * @param hadc ADC handle
  509. * @retval None
  510. */
  511. __weak void HAL_ADC_MspDeInit(ADC_HandleTypeDef* hadc)
  512. {
  513. /* Prevent unused argument(s) compilation warning */
  514. UNUSED(hadc);
  515. /* NOTE : This function should not be modified. When the callback is needed,
  516. function HAL_ADC_MspDeInit must be implemented in the user file.
  517. */
  518. }
  519. #if (USE_HAL_ADC_REGISTER_CALLBACKS == 1)
  520. /**
  521. * @brief Register a User ADC Callback
  522. * To be used instead of the weak predefined callback
  523. * @param hadc Pointer to a ADC_HandleTypeDef structure that contains
  524. * the configuration information for the specified ADC.
  525. * @param CallbackID ID of the callback to be registered
  526. * This parameter can be one of the following values:
  527. * @arg @ref HAL_ADC_CONVERSION_COMPLETE_CB_ID ADC conversion complete callback ID
  528. * @arg @ref HAL_ADC_CONVERSION_HALF_CB_ID ADC conversion complete callback ID
  529. * @arg @ref HAL_ADC_LEVEL_OUT_OF_WINDOW_1_CB_ID ADC analog watchdog 1 callback ID
  530. * @arg @ref HAL_ADC_ERROR_CB_ID ADC error callback ID
  531. * @arg @ref HAL_ADC_INJ_CONVERSION_COMPLETE_CB_ID ADC group injected conversion complete callback ID
  532. * @arg @ref HAL_ADC_MSPINIT_CB_ID ADC Msp Init callback ID
  533. * @arg @ref HAL_ADC_MSPDEINIT_CB_ID ADC Msp DeInit callback ID
  534. * @arg @ref HAL_ADC_MSPINIT_CB_ID MspInit callback ID
  535. * @arg @ref HAL_ADC_MSPDEINIT_CB_ID MspDeInit callback ID
  536. * @param pCallback pointer to the Callback function
  537. * @retval HAL status
  538. */
  539. HAL_StatusTypeDef HAL_ADC_RegisterCallback(ADC_HandleTypeDef *hadc, HAL_ADC_CallbackIDTypeDef CallbackID, pADC_CallbackTypeDef pCallback)
  540. {
  541. HAL_StatusTypeDef status = HAL_OK;
  542. if (pCallback == NULL)
  543. {
  544. /* Update the error code */
  545. hadc->ErrorCode |= HAL_ADC_ERROR_INVALID_CALLBACK;
  546. return HAL_ERROR;
  547. }
  548. if ((hadc->State & HAL_ADC_STATE_READY) != 0)
  549. {
  550. switch (CallbackID)
  551. {
  552. case HAL_ADC_CONVERSION_COMPLETE_CB_ID :
  553. hadc->ConvCpltCallback = pCallback;
  554. break;
  555. case HAL_ADC_CONVERSION_HALF_CB_ID :
  556. hadc->ConvHalfCpltCallback = pCallback;
  557. break;
  558. case HAL_ADC_LEVEL_OUT_OF_WINDOW_1_CB_ID :
  559. hadc->LevelOutOfWindowCallback = pCallback;
  560. break;
  561. case HAL_ADC_ERROR_CB_ID :
  562. hadc->ErrorCallback = pCallback;
  563. break;
  564. case HAL_ADC_MSPINIT_CB_ID :
  565. hadc->MspInitCallback = pCallback;
  566. break;
  567. case HAL_ADC_MSPDEINIT_CB_ID :
  568. hadc->MspDeInitCallback = pCallback;
  569. break;
  570. default :
  571. /* Update the error code */
  572. hadc->ErrorCode |= HAL_ADC_ERROR_INVALID_CALLBACK;
  573. /* Return error status */
  574. status = HAL_ERROR;
  575. break;
  576. }
  577. }
  578. else if (HAL_ADC_STATE_RESET == hadc->State)
  579. {
  580. switch (CallbackID)
  581. {
  582. case HAL_ADC_MSPINIT_CB_ID :
  583. hadc->MspInitCallback = pCallback;
  584. break;
  585. case HAL_ADC_MSPDEINIT_CB_ID :
  586. hadc->MspDeInitCallback = pCallback;
  587. break;
  588. default :
  589. /* Update the error code */
  590. hadc->ErrorCode |= HAL_ADC_ERROR_INVALID_CALLBACK;
  591. /* Return error status */
  592. status = HAL_ERROR;
  593. break;
  594. }
  595. }
  596. else
  597. {
  598. /* Update the error code */
  599. hadc->ErrorCode |= HAL_ADC_ERROR_INVALID_CALLBACK;
  600. /* Return error status */
  601. status = HAL_ERROR;
  602. }
  603. return status;
  604. }
  605. /**
  606. * @brief Unregister a ADC Callback
  607. * ADC callback is redirected to the weak predefined callback
  608. * @param hadc Pointer to a ADC_HandleTypeDef structure that contains
  609. * the configuration information for the specified ADC.
  610. * @param CallbackID ID of the callback to be unregistered
  611. * This parameter can be one of the following values:
  612. * @arg @ref HAL_ADC_CONVERSION_COMPLETE_CB_ID ADC conversion complete callback ID
  613. * @arg @ref HAL_ADC_CONVERSION_HALF_CB_ID ADC conversion complete callback ID
  614. * @arg @ref HAL_ADC_LEVEL_OUT_OF_WINDOW_1_CB_ID ADC analog watchdog 1 callback ID
  615. * @arg @ref HAL_ADC_ERROR_CB_ID ADC error callback ID
  616. * @arg @ref HAL_ADC_INJ_CONVERSION_COMPLETE_CB_ID ADC group injected conversion complete callback ID
  617. * @arg @ref HAL_ADC_MSPINIT_CB_ID ADC Msp Init callback ID
  618. * @arg @ref HAL_ADC_MSPDEINIT_CB_ID ADC Msp DeInit callback ID
  619. * @arg @ref HAL_ADC_MSPINIT_CB_ID MspInit callback ID
  620. * @arg @ref HAL_ADC_MSPDEINIT_CB_ID MspDeInit callback ID
  621. * @retval HAL status
  622. */
  623. HAL_StatusTypeDef HAL_ADC_UnRegisterCallback(ADC_HandleTypeDef *hadc, HAL_ADC_CallbackIDTypeDef CallbackID)
  624. {
  625. HAL_StatusTypeDef status = HAL_OK;
  626. if ((hadc->State & HAL_ADC_STATE_READY) != 0)
  627. {
  628. switch (CallbackID)
  629. {
  630. case HAL_ADC_CONVERSION_COMPLETE_CB_ID :
  631. hadc->ConvCpltCallback = HAL_ADC_ConvCpltCallback;
  632. break;
  633. case HAL_ADC_CONVERSION_HALF_CB_ID :
  634. hadc->ConvHalfCpltCallback = HAL_ADC_ConvHalfCpltCallback;
  635. break;
  636. case HAL_ADC_LEVEL_OUT_OF_WINDOW_1_CB_ID :
  637. hadc->LevelOutOfWindowCallback = HAL_ADC_LevelOutOfWindowCallback;
  638. break;
  639. case HAL_ADC_ERROR_CB_ID :
  640. hadc->ErrorCallback = HAL_ADC_ErrorCallback;
  641. break;
  642. case HAL_ADC_MSPINIT_CB_ID :
  643. hadc->MspInitCallback = HAL_ADC_MspInit; /* Legacy weak MspInit */
  644. break;
  645. case HAL_ADC_MSPDEINIT_CB_ID :
  646. hadc->MspDeInitCallback = HAL_ADC_MspDeInit; /* Legacy weak MspDeInit */
  647. break;
  648. default :
  649. /* Update the error code */
  650. hadc->ErrorCode |= HAL_ADC_ERROR_INVALID_CALLBACK;
  651. /* Return error status */
  652. status = HAL_ERROR;
  653. break;
  654. }
  655. }
  656. else if (HAL_ADC_STATE_RESET == hadc->State)
  657. {
  658. switch (CallbackID)
  659. {
  660. case HAL_ADC_MSPINIT_CB_ID :
  661. hadc->MspInitCallback = HAL_ADC_MspInit; /* Legacy weak MspInit */
  662. break;
  663. case HAL_ADC_MSPDEINIT_CB_ID :
  664. hadc->MspDeInitCallback = HAL_ADC_MspDeInit; /* Legacy weak MspDeInit */
  665. break;
  666. default :
  667. /* Update the error code */
  668. hadc->ErrorCode |= HAL_ADC_ERROR_INVALID_CALLBACK;
  669. /* Return error status */
  670. status = HAL_ERROR;
  671. break;
  672. }
  673. }
  674. else
  675. {
  676. /* Update the error code */
  677. hadc->ErrorCode |= HAL_ADC_ERROR_INVALID_CALLBACK;
  678. /* Return error status */
  679. status = HAL_ERROR;
  680. }
  681. return status;
  682. }
  683. #endif /* USE_HAL_ADC_REGISTER_CALLBACKS */
  684. /**
  685. * @}
  686. */
  687. /** @defgroup ADC_Exported_Functions_Group2 IO operation functions
  688. * @brief IO operation functions
  689. *
  690. @verbatim
  691. ===============================================================================
  692. ##### IO operation functions #####
  693. ===============================================================================
  694. [..] This section provides functions allowing to:
  695. (+) Start conversion of regular group.
  696. (+) Stop conversion of regular group.
  697. (+) Poll for conversion complete on regular group.
  698. (+) Poll for conversion event.
  699. (+) Get result of regular channel conversion.
  700. (+) Start conversion of regular group and enable interruptions.
  701. (+) Stop conversion of regular group and disable interruptions.
  702. (+) Handle ADC interrupt request
  703. (+) Start conversion of regular group and enable DMA transfer.
  704. (+) Stop conversion of regular group and disable ADC DMA transfer.
  705. @endverbatim
  706. * @{
  707. */
  708. /**
  709. * @brief Enables ADC, starts conversion of regular group.
  710. * Interruptions enabled in this function: None.
  711. * @param hadc ADC handle
  712. * @retval HAL status
  713. */
  714. HAL_StatusTypeDef HAL_ADC_Start(ADC_HandleTypeDef* hadc)
  715. {
  716. HAL_StatusTypeDef tmp_hal_status = HAL_OK;
  717. /* Check the parameters */
  718. assert_param(IS_ADC_ALL_INSTANCE(hadc->Instance));
  719. /* Perform ADC enable and conversion start if no conversion is on going */
  720. if (ADC_IS_CONVERSION_ONGOING_REGULAR(hadc) == RESET)
  721. {
  722. /* Process locked */
  723. __HAL_LOCK(hadc);
  724. /* Enable the ADC peripheral */
  725. tmp_hal_status = ADC_Enable(hadc);
  726. /* Start conversion if ADC is effectively enabled */
  727. if (tmp_hal_status == HAL_OK)
  728. {
  729. /* Set ADC state */
  730. /* - Clear state bitfield related to regular group conversion results */
  731. /* - Set state bitfield related to regular operation */
  732. ADC_STATE_CLR_SET(hadc->State,
  733. HAL_ADC_STATE_READY | HAL_ADC_STATE_REG_EOC | HAL_ADC_STATE_REG_OVR | HAL_ADC_STATE_REG_EOSMP,
  734. HAL_ADC_STATE_REG_BUSY);
  735. /* Reset ADC all error code fields */
  736. ADC_CLEAR_ERRORCODE(hadc);
  737. /* Process unlocked */
  738. /* Unlock before starting ADC conversions: in case of potential */
  739. /* interruption, to let the process to ADC IRQ Handler. */
  740. __HAL_UNLOCK(hadc);
  741. /* Clear regular group conversion flag and overrun flag */
  742. /* (To ensure of no unknown state from potential previous ADC */
  743. /* operations) */
  744. __HAL_ADC_CLEAR_FLAG(hadc, (ADC_FLAG_EOC | ADC_FLAG_EOS | ADC_FLAG_OVR));
  745. /* Enable conversion of regular group. */
  746. /* If software start has been selected, conversion starts immediately. */
  747. /* If external trigger has been selected, conversion will start at next */
  748. /* trigger event. */
  749. hadc->Instance->CR |= ADC_CR_ADSTART;
  750. }
  751. }
  752. else
  753. {
  754. tmp_hal_status = HAL_BUSY;
  755. }
  756. /* Return function status */
  757. return tmp_hal_status;
  758. }
  759. /**
  760. * @brief Stop ADC conversion of regular group, disable ADC peripheral.
  761. * @param hadc ADC handle
  762. * @retval HAL status.
  763. */
  764. HAL_StatusTypeDef HAL_ADC_Stop(ADC_HandleTypeDef* hadc)
  765. {
  766. HAL_StatusTypeDef tmp_hal_status = HAL_OK;
  767. /* Check the parameters */
  768. assert_param(IS_ADC_ALL_INSTANCE(hadc->Instance));
  769. /* Process locked */
  770. __HAL_LOCK(hadc);
  771. /* 1. Stop potential conversion on going, on regular group */
  772. tmp_hal_status = ADC_ConversionStop(hadc);
  773. /* Disable ADC peripheral if conversions are effectively stopped */
  774. if (tmp_hal_status == HAL_OK)
  775. {
  776. /* 2. Disable the ADC peripheral */
  777. tmp_hal_status = ADC_Disable(hadc);
  778. /* Check if ADC is effectively disabled */
  779. if (tmp_hal_status == HAL_OK)
  780. {
  781. /* Set ADC state */
  782. ADC_STATE_CLR_SET(hadc->State,
  783. HAL_ADC_STATE_REG_BUSY,
  784. HAL_ADC_STATE_READY);
  785. }
  786. }
  787. /* Process unlocked */
  788. __HAL_UNLOCK(hadc);
  789. /* Return function status */
  790. return tmp_hal_status;
  791. }
  792. /**
  793. * @brief Wait for regular group conversion to be completed.
  794. * @note ADC conversion flags EOS (end of sequence) and EOC (end of
  795. * conversion) are cleared by this function, with an exception:
  796. * if low power feature "LowPowerAutoWait" is enabled, flags are
  797. * not cleared to not interfere with this feature until data register
  798. * is read using function HAL_ADC_GetValue().
  799. * @note This function cannot be used in a particular setup: ADC configured
  800. * in DMA mode and polling for end of each conversion (ADC init
  801. * parameter "EOCSelection" set to ADC_EOC_SINGLE_CONV).
  802. * In this case, DMA resets the flag EOC and polling cannot be
  803. * performed on each conversion. Nevertheless, polling can still
  804. * be performed on the complete sequence (ADC init
  805. * parameter "EOCSelection" set to ADC_EOC_SEQ_CONV).
  806. * @note Depending on devices and packages, DMA may not be available.
  807. * Refer to device datasheet for DMA availability.
  808. * @param hadc ADC handle
  809. * @param Timeout Timeout value in millisecond.
  810. * @retval HAL status
  811. */
  812. HAL_StatusTypeDef HAL_ADC_PollForConversion(ADC_HandleTypeDef* hadc, uint32_t Timeout)
  813. {
  814. uint32_t tickstart;
  815. uint32_t tmp_Flag_EOC;
  816. /* Check the parameters */
  817. assert_param(IS_ADC_ALL_INSTANCE(hadc->Instance));
  818. /* If end of conversion selected to end of sequence */
  819. if (hadc->Init.EOCSelection == ADC_EOC_SEQ_CONV)
  820. {
  821. tmp_Flag_EOC = ADC_FLAG_EOS;
  822. }
  823. /* If end of conversion selected to end of each conversion */
  824. else /* ADC_EOC_SINGLE_CONV */
  825. {
  826. /* Verification that ADC configuration is compliant with polling for */
  827. /* each conversion: */
  828. /* Particular case is ADC configured in DMA mode and ADC sequencer with */
  829. /* several ranks and polling for end of each conversion. */
  830. /* For code simplicity sake, this particular case is generalized to */
  831. /* ADC configured in DMA mode and and polling for end of each conversion. */
  832. #if (defined(DMA) || defined(DMA1))
  833. if (HAL_IS_BIT_SET(hadc->Instance->CFGR1, ADC_CFGR1_DMAEN))
  834. {
  835. /* Update ADC state machine to error */
  836. SET_BIT(hadc->State, HAL_ADC_STATE_ERROR_CONFIG);
  837. /* Process unlocked */
  838. __HAL_UNLOCK(hadc);
  839. return HAL_ERROR;
  840. }
  841. else
  842. {
  843. #endif
  844. tmp_Flag_EOC = (ADC_FLAG_EOC | ADC_FLAG_EOS);
  845. #if (defined(DMA) || defined(DMA1))
  846. }
  847. #endif
  848. }
  849. /* Get tick count */
  850. tickstart = HAL_GetTick();
  851. /* Wait until End of Conversion flag is raised */
  852. while(HAL_IS_BIT_CLR(hadc->Instance->ISR, tmp_Flag_EOC))
  853. {
  854. /* Check if timeout is disabled (set to infinite wait) */
  855. if(Timeout != HAL_MAX_DELAY)
  856. {
  857. if((Timeout == 0) || ((HAL_GetTick()-tickstart) > Timeout))
  858. {
  859. /* Update ADC state machine to timeout */
  860. SET_BIT(hadc->State, HAL_ADC_STATE_TIMEOUT);
  861. /* Process unlocked */
  862. __HAL_UNLOCK(hadc);
  863. return HAL_TIMEOUT;
  864. }
  865. }
  866. }
  867. /* Update ADC state machine */
  868. SET_BIT(hadc->State, HAL_ADC_STATE_REG_EOC);
  869. /* Determine whether any further conversion upcoming on group regular */
  870. /* by external trigger, continuous mode or scan sequence on going. */
  871. if(ADC_IS_SOFTWARE_START_REGULAR(hadc) &&
  872. (hadc->Init.ContinuousConvMode == DISABLE) )
  873. {
  874. /* If End of Sequence is reached, disable interrupts */
  875. if( __HAL_ADC_GET_FLAG(hadc, ADC_FLAG_EOS) )
  876. {
  877. /* Allowed to modify bits ADC_IT_EOC/ADC_IT_EOS only if bit */
  878. /* ADSTART==0 (no conversion on going) */
  879. if (ADC_IS_CONVERSION_ONGOING_REGULAR(hadc) == RESET)
  880. {
  881. /* Disable ADC end of single conversion interrupt on group regular */
  882. /* Note: Overrun interrupt was enabled with EOC interrupt in */
  883. /* HAL_Start_IT(), but is not disabled here because can be used */
  884. /* by overrun IRQ process below. */
  885. __HAL_ADC_DISABLE_IT(hadc, ADC_IT_EOC | ADC_IT_EOS);
  886. /* Set ADC state */
  887. ADC_STATE_CLR_SET(hadc->State,
  888. HAL_ADC_STATE_REG_BUSY,
  889. HAL_ADC_STATE_READY);
  890. }
  891. else
  892. {
  893. /* Change ADC state to error state */
  894. SET_BIT(hadc->State, HAL_ADC_STATE_ERROR_CONFIG);
  895. /* Set ADC error code to ADC IP internal error */
  896. SET_BIT(hadc->ErrorCode, HAL_ADC_ERROR_INTERNAL);
  897. }
  898. }
  899. }
  900. /* Clear end of conversion flag of regular group if low power feature */
  901. /* "LowPowerAutoWait " is disabled, to not interfere with this feature */
  902. /* until data register is read using function HAL_ADC_GetValue(). */
  903. if (hadc->Init.LowPowerAutoWait == DISABLE)
  904. {
  905. /* Clear regular group conversion flag */
  906. __HAL_ADC_CLEAR_FLAG(hadc, (ADC_FLAG_EOC | ADC_FLAG_EOS));
  907. }
  908. /* Return ADC state */
  909. return HAL_OK;
  910. }
  911. /**
  912. * @brief Poll for conversion event.
  913. * @param hadc ADC handle
  914. * @param EventType the ADC event type.
  915. * This parameter can be one of the following values:
  916. * @arg ADC_AWD_EVENT: ADC Analog watchdog event
  917. * @arg ADC_OVR_EVENT: ADC Overrun event
  918. * @param Timeout Timeout value in millisecond.
  919. * @retval HAL status
  920. */
  921. HAL_StatusTypeDef HAL_ADC_PollForEvent(ADC_HandleTypeDef* hadc, uint32_t EventType, uint32_t Timeout)
  922. {
  923. uint32_t tickstart=0;
  924. /* Check the parameters */
  925. assert_param(IS_ADC_ALL_INSTANCE(hadc->Instance));
  926. assert_param(IS_ADC_EVENT_TYPE(EventType));
  927. /* Get tick count */
  928. tickstart = HAL_GetTick();
  929. /* Check selected event flag */
  930. while(__HAL_ADC_GET_FLAG(hadc, EventType) == RESET)
  931. {
  932. /* Check if timeout is disabled (set to infinite wait) */
  933. if(Timeout != HAL_MAX_DELAY)
  934. {
  935. if((Timeout == 0U) || ((HAL_GetTick()-tickstart) > Timeout))
  936. {
  937. /* Update ADC state machine to timeout */
  938. SET_BIT(hadc->State, HAL_ADC_STATE_TIMEOUT);
  939. /* Process unlocked */
  940. __HAL_UNLOCK(hadc);
  941. return HAL_TIMEOUT;
  942. }
  943. }
  944. }
  945. switch(EventType)
  946. {
  947. /* Analog watchdog (level out of window) event */
  948. case ADC_AWD_EVENT:
  949. /* Set ADC state */
  950. SET_BIT(hadc->State, HAL_ADC_STATE_AWD1);
  951. /* Clear ADC analog watchdog flag */
  952. __HAL_ADC_CLEAR_FLAG(hadc, ADC_FLAG_AWD);
  953. break;
  954. /* Overrun event */
  955. default: /* Case ADC_OVR_EVENT */
  956. /* If overrun is set to overwrite previous data, overrun event is not */
  957. /* considered as an error. */
  958. /* (cf ref manual "Managing conversions without using the DMA and without */
  959. /* overrun ") */
  960. if (hadc->Init.Overrun == ADC_OVR_DATA_PRESERVED)
  961. {
  962. /* Set ADC state */
  963. SET_BIT(hadc->State, HAL_ADC_STATE_REG_OVR);
  964. /* Set ADC error code to overrun */
  965. SET_BIT(hadc->ErrorCode, HAL_ADC_ERROR_OVR);
  966. }
  967. /* Clear ADC Overrun flag */
  968. __HAL_ADC_CLEAR_FLAG(hadc, ADC_FLAG_OVR);
  969. break;
  970. }
  971. /* Return ADC state */
  972. return HAL_OK;
  973. }
  974. /**
  975. * @brief Enables ADC, starts conversion of regular group with interruption.
  976. * Interruptions enabled in this function:
  977. * - EOC (end of conversion of regular group) or EOS (end of
  978. * sequence of regular group) depending on ADC initialization
  979. * parameter "EOCSelection"
  980. * - overrun (if available)
  981. * Each of these interruptions has its dedicated callback function.
  982. * @param hadc ADC handle
  983. * @retval HAL status
  984. */
  985. HAL_StatusTypeDef HAL_ADC_Start_IT(ADC_HandleTypeDef* hadc)
  986. {
  987. HAL_StatusTypeDef tmp_hal_status = HAL_OK;
  988. /* Check the parameters */
  989. assert_param(IS_ADC_ALL_INSTANCE(hadc->Instance));
  990. /* Perform ADC enable and conversion start if no conversion is on going */
  991. if (ADC_IS_CONVERSION_ONGOING_REGULAR(hadc) == RESET)
  992. {
  993. /* Process locked */
  994. __HAL_LOCK(hadc);
  995. /* Enable the ADC peripheral */
  996. tmp_hal_status = ADC_Enable(hadc);
  997. /* Start conversion if ADC is effectively enabled */
  998. if (tmp_hal_status == HAL_OK)
  999. {
  1000. /* Set ADC state */
  1001. /* - Clear state bitfield related to regular group conversion results */
  1002. /* - Set state bitfield related to regular operation */
  1003. ADC_STATE_CLR_SET(hadc->State,
  1004. HAL_ADC_STATE_READY | HAL_ADC_STATE_REG_EOC | HAL_ADC_STATE_REG_OVR | HAL_ADC_STATE_REG_EOSMP,
  1005. HAL_ADC_STATE_REG_BUSY);
  1006. /* Reset ADC all error code fields */
  1007. ADC_CLEAR_ERRORCODE(hadc);
  1008. /* Process unlocked */
  1009. /* Unlock before starting ADC conversions: in case of potential */
  1010. /* interruption, to let the process to ADC IRQ Handler. */
  1011. __HAL_UNLOCK(hadc);
  1012. /* Clear regular group conversion flag and overrun flag */
  1013. /* (To ensure of no unknown state from potential previous ADC */
  1014. /* operations) */
  1015. __HAL_ADC_CLEAR_FLAG(hadc, (ADC_FLAG_EOC | ADC_FLAG_EOS | ADC_FLAG_OVR));
  1016. /* Enable ADC end of conversion interrupt */
  1017. /* Enable ADC overrun interrupt */
  1018. switch(hadc->Init.EOCSelection)
  1019. {
  1020. case ADC_EOC_SEQ_CONV:
  1021. __HAL_ADC_DISABLE_IT(hadc, ADC_IT_EOC);
  1022. __HAL_ADC_ENABLE_IT(hadc, (ADC_IT_EOS | ADC_IT_OVR));
  1023. break;
  1024. /* case ADC_EOC_SINGLE_CONV */
  1025. default:
  1026. __HAL_ADC_ENABLE_IT(hadc, (ADC_IT_EOC | ADC_IT_EOS | ADC_IT_OVR));
  1027. break;
  1028. }
  1029. /* Enable conversion of regular group. */
  1030. /* If software start has been selected, conversion starts immediately. */
  1031. /* If external trigger has been selected, conversion will start at next */
  1032. /* trigger event. */
  1033. hadc->Instance->CR |= ADC_CR_ADSTART;
  1034. }
  1035. }
  1036. else
  1037. {
  1038. tmp_hal_status = HAL_BUSY;
  1039. }
  1040. /* Return function status */
  1041. return tmp_hal_status;
  1042. }
  1043. /**
  1044. * @brief Stop ADC conversion of regular group, disable interruption of
  1045. * end-of-conversion, disable ADC peripheral.
  1046. * @param hadc ADC handle
  1047. * @retval HAL status.
  1048. */
  1049. HAL_StatusTypeDef HAL_ADC_Stop_IT(ADC_HandleTypeDef* hadc)
  1050. {
  1051. HAL_StatusTypeDef tmp_hal_status = HAL_OK;
  1052. /* Check the parameters */
  1053. assert_param(IS_ADC_ALL_INSTANCE(hadc->Instance));
  1054. /* Process locked */
  1055. __HAL_LOCK(hadc);
  1056. /* 1. Stop potential conversion on going, on regular group */
  1057. tmp_hal_status = ADC_ConversionStop(hadc);
  1058. /* Disable ADC peripheral if conversions are effectively stopped */
  1059. if (tmp_hal_status == HAL_OK)
  1060. {
  1061. /* Disable ADC end of conversion interrupt for regular group */
  1062. /* Disable ADC overrun interrupt */
  1063. __HAL_ADC_DISABLE_IT(hadc, (ADC_IT_EOC | ADC_IT_EOS | ADC_IT_OVR));
  1064. /* 2. Disable the ADC peripheral */
  1065. tmp_hal_status = ADC_Disable(hadc);
  1066. /* Check if ADC is effectively disabled */
  1067. if (tmp_hal_status == HAL_OK)
  1068. {
  1069. /* Set ADC state */
  1070. ADC_STATE_CLR_SET(hadc->State,
  1071. HAL_ADC_STATE_REG_BUSY,
  1072. HAL_ADC_STATE_READY);
  1073. }
  1074. }
  1075. /* Process unlocked */
  1076. __HAL_UNLOCK(hadc);
  1077. /* Return function status */
  1078. return tmp_hal_status;
  1079. }
  1080. #if (defined(DMA) || defined(DMA1))
  1081. /**
  1082. * @brief Enables ADC, starts conversion of regular group and transfers result
  1083. * through DMA.
  1084. * Interruptions enabled in this function:
  1085. * - DMA transfer complete
  1086. * - DMA half transfer
  1087. * - overrun
  1088. * Each of these interruptions has its dedicated callback function.
  1089. * @note Depending on devices and packages, DMA may not be available.
  1090. * Refer to device datasheet for DMA availability.
  1091. * @param hadc ADC handle
  1092. * @param pData The destination Buffer address.
  1093. * @param Length The length of data to be transferred from ADC peripheral to memory.
  1094. * @retval HAL status.
  1095. */
  1096. HAL_StatusTypeDef HAL_ADC_Start_DMA(ADC_HandleTypeDef* hadc, uint32_t* pData, uint32_t Length)
  1097. {
  1098. HAL_StatusTypeDef tmp_hal_status = HAL_OK;
  1099. /* Check the parameters */
  1100. assert_param(IS_ADC_ALL_INSTANCE(hadc->Instance));
  1101. /* Perform ADC enable and conversion start if no conversion is on going */
  1102. if (ADC_IS_CONVERSION_ONGOING_REGULAR(hadc) == RESET)
  1103. {
  1104. /* Process locked */
  1105. __HAL_LOCK(hadc);
  1106. /* Enable the ADC peripheral */
  1107. tmp_hal_status = ADC_Enable(hadc);
  1108. /* Start conversion if ADC is effectively enabled */
  1109. if (tmp_hal_status == HAL_OK)
  1110. {
  1111. /* Set ADC state */
  1112. /* - Clear state bitfield related to regular group conversion results */
  1113. /* - Set state bitfield related to regular operation */
  1114. ADC_STATE_CLR_SET(hadc->State,
  1115. HAL_ADC_STATE_READY | HAL_ADC_STATE_REG_EOC | HAL_ADC_STATE_REG_OVR | HAL_ADC_STATE_REG_EOSMP,
  1116. HAL_ADC_STATE_REG_BUSY);
  1117. /* Reset ADC all error code fields */
  1118. ADC_CLEAR_ERRORCODE(hadc);
  1119. /* Process unlocked */
  1120. /* Unlock before starting ADC conversions: in case of potential */
  1121. /* interruption, to let the process to ADC IRQ Handler. */
  1122. __HAL_UNLOCK(hadc);
  1123. /* Set the DMA transfer complete callback */
  1124. hadc->DMA_Handle->XferCpltCallback = ADC_DMAConvCplt;
  1125. /* Set the DMA half transfer complete callback */
  1126. hadc->DMA_Handle->XferHalfCpltCallback = ADC_DMAHalfConvCplt;
  1127. /* Set the DMA error callback */
  1128. hadc->DMA_Handle->XferErrorCallback = ADC_DMAError;
  1129. /* Manage ADC and DMA start: ADC overrun interruption, DMA start, ADC */
  1130. /* start (in case of SW start): */
  1131. /* Clear regular group conversion flag and overrun flag */
  1132. /* (To ensure of no unknown state from potential previous ADC */
  1133. /* operations) */
  1134. __HAL_ADC_CLEAR_FLAG(hadc, (ADC_FLAG_EOC | ADC_FLAG_EOS | ADC_FLAG_OVR));
  1135. /* Enable ADC overrun interrupt */
  1136. __HAL_ADC_ENABLE_IT(hadc, ADC_IT_OVR);
  1137. /* Enable ADC DMA mode */
  1138. hadc->Instance->CFGR1 |= ADC_CFGR1_DMAEN;
  1139. /* Start the DMA channel */
  1140. HAL_DMA_Start_IT(hadc->DMA_Handle, (uint32_t)&hadc->Instance->DR, (uint32_t)pData, Length);
  1141. /* Enable conversion of regular group. */
  1142. /* If software start has been selected, conversion starts immediately. */
  1143. /* If external trigger has been selected, conversion will start at next */
  1144. /* trigger event. */
  1145. hadc->Instance->CR |= ADC_CR_ADSTART;
  1146. }
  1147. }
  1148. else
  1149. {
  1150. tmp_hal_status = HAL_BUSY;
  1151. }
  1152. /* Return function status */
  1153. return tmp_hal_status;
  1154. }
  1155. /**
  1156. * @brief Stop ADC conversion of regular group, disable ADC DMA transfer, disable
  1157. * ADC peripheral.
  1158. * Each of these interruptions has its dedicated callback function.
  1159. * @note Depending on devices and packages, DMA may not be available.
  1160. * Refer to device datasheet for DMA availability.
  1161. * @param hadc ADC handle
  1162. * @retval HAL status.
  1163. */
  1164. HAL_StatusTypeDef HAL_ADC_Stop_DMA(ADC_HandleTypeDef* hadc)
  1165. {
  1166. HAL_StatusTypeDef tmp_hal_status = HAL_OK;
  1167. /* Check the parameters */
  1168. assert_param(IS_ADC_ALL_INSTANCE(hadc->Instance));
  1169. /* Process locked */
  1170. __HAL_LOCK(hadc);
  1171. /* 1. Stop potential conversion on going, on regular group */
  1172. tmp_hal_status = ADC_ConversionStop(hadc);
  1173. /* Disable ADC peripheral if conversions are effectively stopped */
  1174. if (tmp_hal_status == HAL_OK)
  1175. {
  1176. /* Disable ADC DMA (ADC DMA configuration ADC_CFGR_DMACFG is kept) */
  1177. hadc->Instance->CFGR1 &= ~ADC_CFGR1_DMAEN;
  1178. /* Disable the DMA channel (in case of DMA in circular mode or stop while */
  1179. /* while DMA transfer is on going) */
  1180. tmp_hal_status = HAL_DMA_Abort(hadc->DMA_Handle);
  1181. /* Check if DMA channel effectively disabled */
  1182. if (tmp_hal_status != HAL_OK)
  1183. {
  1184. /* Update ADC state machine to error */
  1185. SET_BIT(hadc->State, HAL_ADC_STATE_ERROR_DMA);
  1186. }
  1187. /* Disable ADC overrun interrupt */
  1188. __HAL_ADC_DISABLE_IT(hadc, ADC_IT_OVR);
  1189. /* 2. Disable the ADC peripheral */
  1190. /* Update "tmp_hal_status" only if DMA channel disabling passed, to keep */
  1191. /* in memory a potential failing status. */
  1192. if (tmp_hal_status == HAL_OK)
  1193. {
  1194. tmp_hal_status = ADC_Disable(hadc);
  1195. }
  1196. else
  1197. {
  1198. ADC_Disable(hadc);
  1199. }
  1200. /* Check if ADC is effectively disabled */
  1201. if (tmp_hal_status == HAL_OK)
  1202. {
  1203. /* Set ADC state */
  1204. ADC_STATE_CLR_SET(hadc->State,
  1205. HAL_ADC_STATE_REG_BUSY,
  1206. HAL_ADC_STATE_READY);
  1207. }
  1208. }
  1209. /* Process unlocked */
  1210. __HAL_UNLOCK(hadc);
  1211. /* Return function status */
  1212. return tmp_hal_status;
  1213. }
  1214. #endif
  1215. /**
  1216. * @brief Get ADC regular group conversion result.
  1217. * @note Reading register DR automatically clears ADC flag EOC
  1218. * (ADC group regular end of unitary conversion).
  1219. * @note This function does not clear ADC flag EOS
  1220. * (ADC group regular end of sequence conversion).
  1221. * Occurrence of flag EOS rising:
  1222. * - If sequencer is composed of 1 rank, flag EOS is equivalent
  1223. * to flag EOC.
  1224. * - If sequencer is composed of several ranks, during the scan
  1225. * sequence flag EOC only is raised, at the end of the scan sequence
  1226. * both flags EOC and EOS are raised.
  1227. * To clear this flag, either use function:
  1228. * in programming model IT: @ref HAL_ADC_IRQHandler(), in programming
  1229. * model polling: @ref HAL_ADC_PollForConversion()
  1230. * or @ref __HAL_ADC_CLEAR_FLAG(&hadc, ADC_FLAG_EOS).
  1231. * @param hadc ADC handle
  1232. * @retval ADC group regular conversion data
  1233. */
  1234. uint32_t HAL_ADC_GetValue(ADC_HandleTypeDef* hadc)
  1235. {
  1236. /* Check the parameters */
  1237. assert_param(IS_ADC_ALL_INSTANCE(hadc->Instance));
  1238. /* Note: EOC flag is not cleared here by software because automatically */
  1239. /* cleared by hardware when reading register DR. */
  1240. /* Return ADC converted value */
  1241. return hadc->Instance->DR;
  1242. }
  1243. /**
  1244. * @brief Handles ADC interrupt request.
  1245. * @param hadc ADC handle
  1246. * @retval None
  1247. */
  1248. void HAL_ADC_IRQHandler(ADC_HandleTypeDef* hadc)
  1249. {
  1250. /* Check the parameters */
  1251. assert_param(IS_ADC_ALL_INSTANCE(hadc->Instance));
  1252. assert_param(IS_FUNCTIONAL_STATE(hadc->Init.ContinuousConvMode));
  1253. assert_param(IS_ADC_EOC_SELECTION(hadc->Init.EOCSelection));
  1254. /* ========== Check End of Conversion flag for regular group ========== */
  1255. if( (__HAL_ADC_GET_FLAG(hadc, ADC_FLAG_EOC) && __HAL_ADC_GET_IT_SOURCE(hadc, ADC_IT_EOC)) ||
  1256. (__HAL_ADC_GET_FLAG(hadc, ADC_FLAG_EOS) && __HAL_ADC_GET_IT_SOURCE(hadc, ADC_IT_EOS)) )
  1257. {
  1258. /* Update state machine on conversion status if not in error state */
  1259. if (HAL_IS_BIT_CLR(hadc->State, HAL_ADC_STATE_ERROR_INTERNAL))
  1260. {
  1261. /* Set ADC state */
  1262. SET_BIT(hadc->State, HAL_ADC_STATE_REG_EOC);
  1263. }
  1264. /* Determine whether any further conversion upcoming on group regular */
  1265. /* by external trigger, continuous mode or scan sequence on going. */
  1266. if(ADC_IS_SOFTWARE_START_REGULAR(hadc) &&
  1267. (hadc->Init.ContinuousConvMode == DISABLE) )
  1268. {
  1269. /* If End of Sequence is reached, disable interrupts */
  1270. if( __HAL_ADC_GET_FLAG(hadc, ADC_FLAG_EOS) )
  1271. {
  1272. /* Allowed to modify bits ADC_IT_EOC/ADC_IT_EOS only if bit */
  1273. /* ADSTART==0 (no conversion on going) */
  1274. if (ADC_IS_CONVERSION_ONGOING_REGULAR(hadc) == RESET)
  1275. {
  1276. /* Disable ADC end of single conversion interrupt on group regular */
  1277. /* Note: Overrun interrupt was enabled with EOC interrupt in */
  1278. /* HAL_Start_IT(), but is not disabled here because can be used */
  1279. /* by overrun IRQ process below. */
  1280. __HAL_ADC_DISABLE_IT(hadc, ADC_IT_EOC | ADC_IT_EOS);
  1281. /* Set ADC state */
  1282. ADC_STATE_CLR_SET(hadc->State,
  1283. HAL_ADC_STATE_REG_BUSY,
  1284. HAL_ADC_STATE_READY);
  1285. }
  1286. else
  1287. {
  1288. /* Change ADC state to error state */
  1289. SET_BIT(hadc->State, HAL_ADC_STATE_ERROR_CONFIG);
  1290. /* Set ADC error code to ADC IP internal error */
  1291. SET_BIT(hadc->ErrorCode, HAL_ADC_ERROR_INTERNAL);
  1292. }
  1293. }
  1294. }
  1295. /* Note: into callback, to determine if conversion has been triggered */
  1296. /* from EOC or EOS, possibility to use: */
  1297. /* " if( __HAL_ADC_GET_FLAG(&hadc, ADC_FLAG_EOS)) " */
  1298. #if (USE_HAL_ADC_REGISTER_CALLBACKS == 1)
  1299. hadc->ConvCpltCallback(hadc);
  1300. #else
  1301. HAL_ADC_ConvCpltCallback(hadc);
  1302. #endif /* USE_HAL_ADC_REGISTER_CALLBACKS */
  1303. /* Clear regular group conversion flag */
  1304. /* Note: in case of overrun set to ADC_OVR_DATA_PRESERVED, end of */
  1305. /* conversion flags clear induces the release of the preserved data.*/
  1306. /* Therefore, if the preserved data value is needed, it must be */
  1307. /* read preliminarily into HAL_ADC_ConvCpltCallback(). */
  1308. __HAL_ADC_CLEAR_FLAG(hadc, (ADC_FLAG_EOC | ADC_FLAG_EOS) );
  1309. }
  1310. /* ========== Check Analog watchdog flags ========== */
  1311. if(__HAL_ADC_GET_FLAG(hadc, ADC_FLAG_AWD) && __HAL_ADC_GET_IT_SOURCE(hadc, ADC_IT_AWD))
  1312. {
  1313. /* Set ADC state */
  1314. SET_BIT(hadc->State, HAL_ADC_STATE_AWD1);
  1315. #if (USE_HAL_ADC_REGISTER_CALLBACKS == 1)
  1316. hadc->LevelOutOfWindowCallback(hadc);
  1317. #else
  1318. HAL_ADC_LevelOutOfWindowCallback(hadc);
  1319. #endif /* USE_HAL_ADC_REGISTER_CALLBACKS */
  1320. /* Clear ADC Analog watchdog flag */
  1321. __HAL_ADC_CLEAR_FLAG(hadc, ADC_FLAG_AWD);
  1322. }
  1323. /* ========== Check Overrun flag ========== */
  1324. if(__HAL_ADC_GET_FLAG(hadc, ADC_FLAG_OVR) && __HAL_ADC_GET_IT_SOURCE(hadc, ADC_IT_OVR))
  1325. {
  1326. /* If overrun is set to overwrite previous data (default setting), */
  1327. /* overrun event is not considered as an error. */
  1328. /* (cf ref manual "Managing conversions without using the DMA and without */
  1329. /* overrun ") */
  1330. /* Exception for usage with DMA overrun event always considered as an */
  1331. /* error. */
  1332. #if (defined(DMA) || defined(DMA1))
  1333. if ((hadc->Init.Overrun == ADC_OVR_DATA_PRESERVED) ||
  1334. HAL_IS_BIT_SET(hadc->Instance->CFGR1, ADC_CFGR1_DMAEN) )
  1335. #else
  1336. if (hadc->Init.Overrun == ADC_OVR_DATA_PRESERVED)
  1337. #endif
  1338. {
  1339. /* Set ADC error code to overrun */
  1340. SET_BIT(hadc->ErrorCode, HAL_ADC_ERROR_OVR);
  1341. /* Clear ADC overrun flag */
  1342. __HAL_ADC_CLEAR_FLAG(hadc, ADC_FLAG_OVR);
  1343. #if (USE_HAL_ADC_REGISTER_CALLBACKS == 1)
  1344. hadc->ErrorCallback(hadc);
  1345. #else
  1346. HAL_ADC_ErrorCallback(hadc);
  1347. #endif /* USE_HAL_ADC_REGISTER_CALLBACKS */
  1348. }
  1349. /* Clear the Overrun flag */
  1350. __HAL_ADC_CLEAR_FLAG(hadc, ADC_FLAG_OVR);
  1351. }
  1352. }
  1353. /**
  1354. * @brief Conversion complete callback in non blocking mode
  1355. * @param hadc ADC handle
  1356. * @retval None
  1357. */
  1358. __weak void HAL_ADC_ConvCpltCallback(ADC_HandleTypeDef* hadc)
  1359. {
  1360. /* Prevent unused argument(s) compilation warning */
  1361. UNUSED(hadc);
  1362. /* NOTE : This function should not be modified. When the callback is needed,
  1363. function HAL_ADC_ConvCpltCallback must be implemented in the user file.
  1364. */
  1365. }
  1366. /**
  1367. * @brief Conversion DMA half-transfer callback in non blocking mode
  1368. * @param hadc ADC handle
  1369. * @retval None
  1370. */
  1371. __weak void HAL_ADC_ConvHalfCpltCallback(ADC_HandleTypeDef* hadc)
  1372. {
  1373. /* Prevent unused argument(s) compilation warning */
  1374. UNUSED(hadc);
  1375. /* NOTE : This function should not be modified. When the callback is needed,
  1376. function HAL_ADC_ConvHalfCpltCallback must be implemented in the user file.
  1377. */
  1378. }
  1379. /**
  1380. * @brief Analog watchdog callback in non blocking mode.
  1381. * @param hadc ADC handle
  1382. * @retval None
  1383. */
  1384. __weak void HAL_ADC_LevelOutOfWindowCallback(ADC_HandleTypeDef* hadc)
  1385. {
  1386. /* Prevent unused argument(s) compilation warning */
  1387. UNUSED(hadc);
  1388. /* NOTE : This function should not be modified. When the callback is needed,
  1389. function HAL_ADC_LevelOoutOfWindowCallback must be implemented in the user file.
  1390. */
  1391. }
  1392. /**
  1393. * @brief ADC error callback in non blocking mode
  1394. * (ADC conversion with interruption or transfer by DMA)
  1395. * @param hadc ADC handle
  1396. * @retval None
  1397. */
  1398. __weak void HAL_ADC_ErrorCallback(ADC_HandleTypeDef *hadc)
  1399. {
  1400. /* Prevent unused argument(s) compilation warning */
  1401. UNUSED(hadc);
  1402. /* NOTE : This function should not be modified. When the callback is needed,
  1403. function HAL_ADC_ErrorCallback must be implemented in the user file.
  1404. */
  1405. }
  1406. /**
  1407. * @}
  1408. */
  1409. /** @defgroup ADC_Exported_Functions_Group3 Peripheral Control functions
  1410. * @brief Peripheral Control functions
  1411. *
  1412. @verbatim
  1413. ===============================================================================
  1414. ##### Peripheral Control functions #####
  1415. ===============================================================================
  1416. [..] This section provides functions allowing to:
  1417. (+) Configure channels on regular group
  1418. (+) Configure the analog watchdog
  1419. @endverbatim
  1420. * @{
  1421. */
  1422. /**
  1423. * @brief Configures the the selected channel to be linked to the regular
  1424. * group.
  1425. * @note In case of usage of internal measurement channels:
  1426. * VrefInt/TempSensor.
  1427. * Sampling time constraints must be respected (sampling time can be
  1428. * adjusted in function of ADC clock frequency and sampling time
  1429. * setting).
  1430. * Refer to device datasheet for timings values, parameters TS_vrefint,
  1431. * TS_temp (values rough order: 5us to 17us).
  1432. * These internal paths can be be disabled using function
  1433. * HAL_ADC_DeInit().
  1434. * @note Possibility to update parameters on the fly:
  1435. * This function initializes channel into regular group, following
  1436. * calls to this function can be used to reconfigure some parameters
  1437. * of structure "ADC_ChannelConfTypeDef" on the fly, without reseting
  1438. * the ADC.
  1439. * The setting of these parameters is conditioned to ADC state.
  1440. * For parameters constraints, see comments of structure
  1441. * "ADC_ChannelConfTypeDef".
  1442. * @param hadc ADC handle
  1443. * @param sConfig Structure of ADC channel for regular group.
  1444. * @retval HAL status
  1445. */
  1446. HAL_StatusTypeDef HAL_ADC_ConfigChannel(ADC_HandleTypeDef* hadc, ADC_ChannelConfTypeDef* sConfig)
  1447. {
  1448. HAL_StatusTypeDef tmp_hal_status = HAL_OK;
  1449. __IO uint32_t wait_loop_index = 0U;
  1450. /* Check the parameters */
  1451. assert_param(IS_ADC_ALL_INSTANCE(hadc->Instance));
  1452. assert_param(IS_ADC_CHANNEL(sConfig->Channel));
  1453. assert_param(IS_ADC_RANK(sConfig->Rank));
  1454. if (! IS_ADC_SAMPLE_TIME(hadc->Init.SamplingTimeCommon))
  1455. {
  1456. assert_param(IS_ADC_SAMPLE_TIME(sConfig->SamplingTime));
  1457. }
  1458. /* Process locked */
  1459. __HAL_LOCK(hadc);
  1460. /* Parameters update conditioned to ADC state: */
  1461. /* Parameters that can be updated when ADC is disabled or enabled without */
  1462. /* conversion on going on regular group: */
  1463. /* - Channel number */
  1464. /* - Channel sampling time */
  1465. /* - Management of internal measurement channels: VrefInt/TempSensor */
  1466. if (ADC_IS_CONVERSION_ONGOING_REGULAR(hadc) == RESET)
  1467. {
  1468. /* Configure channel: depending on rank setting, add it or remove it from */
  1469. /* ADC conversion sequencer. */
  1470. if (sConfig->Rank != ADC_RANK_NONE)
  1471. {
  1472. /* Regular sequence configuration */
  1473. /* Set the channel selection register from the selected channel */
  1474. hadc->Instance->CHSELR |= ADC_CHSELR_CHANNEL(sConfig->Channel);
  1475. /* Channel sampling time configuration */
  1476. /* Management of parameters "SamplingTimeCommon" and "SamplingTime" */
  1477. /* (obsolete): sampling time set in this function with */
  1478. /* parameter "SamplingTime" (obsolete) only if not already set into */
  1479. /* ADC initialization structure with parameter "SamplingTimeCommon". */
  1480. if (! IS_ADC_SAMPLE_TIME(hadc->Init.SamplingTimeCommon))
  1481. {
  1482. /* Modify sampling time if needed (not needed in case of reoccurrence */
  1483. /* for several channels programmed consecutively into the sequencer) */
  1484. if (sConfig->SamplingTime != ADC_GET_SAMPLINGTIME(hadc))
  1485. {
  1486. /* Channel sampling time configuration */
  1487. /* Clear the old sample time */
  1488. hadc->Instance->SMPR &= ~(ADC_SMPR_SMP);
  1489. /* Set the new sample time */
  1490. hadc->Instance->SMPR |= ADC_SMPR_SET(sConfig->SamplingTime);
  1491. }
  1492. }
  1493. /* Management of internal measurement channels: VrefInt/TempSensor */
  1494. /* internal measurement paths enable: If internal channel selected, */
  1495. /* enable dedicated internal buffers and path. */
  1496. /* Note: these internal measurement paths can be disabled using */
  1497. /* HAL_ADC_DeInit() or removing the channel from sequencer with */
  1498. /* channel configuration parameter "Rank". */
  1499. if(ADC_IS_CHANNEL_INTERNAL(sConfig->Channel))
  1500. {
  1501. /* If Channel_11 is selected, enable Temp. sensor measurement path. */
  1502. /* If Channel_12 is selected, enable VREFINT measurement path. */
  1503. ADC->CCR |= ADC_CHANNEL_INTERNAL_PATH(sConfig->Channel);
  1504. /* If Temp. sensor is selected, wait for stabilization delay */
  1505. if (sConfig->Channel == ADC_CHANNEL_TEMPSENSOR)
  1506. {
  1507. /* Delay for temperature sensor stabilization time */
  1508. /* Compute number of CPU cycles to wait for */
  1509. wait_loop_index = (ADC_TEMPSENSOR_DELAY_US * (SystemCoreClock / 1000000U));
  1510. while(wait_loop_index != 0U)
  1511. {
  1512. wait_loop_index--;
  1513. }
  1514. }
  1515. }
  1516. }
  1517. else
  1518. {
  1519. /* Regular sequence configuration */
  1520. /* Reset the channel selection register from the selected channel */
  1521. hadc->Instance->CHSELR &= ~ADC_CHSELR_CHANNEL(sConfig->Channel);
  1522. /* Management of internal measurement channels: VrefInt/TempSensor */
  1523. /* internal measurement paths disable: If internal channel selected, */
  1524. /* disable dedicated internal buffers and path. */
  1525. if(ADC_IS_CHANNEL_INTERNAL(sConfig->Channel))
  1526. {
  1527. ADC->CCR &= ~ADC_CHANNEL_INTERNAL_PATH(sConfig->Channel);
  1528. }
  1529. }
  1530. }
  1531. /* If a conversion is on going on regular group, no update on regular */
  1532. /* channel could be done on neither of the channel configuration structure */
  1533. /* parameters. */
  1534. else
  1535. {
  1536. /* Update ADC state machine to error */
  1537. SET_BIT(hadc->State, HAL_ADC_STATE_ERROR_CONFIG);
  1538. tmp_hal_status = HAL_ERROR;
  1539. }
  1540. /* Process unlocked */
  1541. __HAL_UNLOCK(hadc);
  1542. /* Return function status */
  1543. return tmp_hal_status;
  1544. }
  1545. /**
  1546. * @brief Configures the analog watchdog.
  1547. * @note Possibility to update parameters on the fly:
  1548. * This function initializes the selected analog watchdog, following
  1549. * calls to this function can be used to reconfigure some parameters
  1550. * of structure "ADC_AnalogWDGConfTypeDef" on the fly, without reseting
  1551. * the ADC.
  1552. * The setting of these parameters is conditioned to ADC state.
  1553. * For parameters constraints, see comments of structure
  1554. * "ADC_AnalogWDGConfTypeDef".
  1555. * @param hadc ADC handle
  1556. * @param AnalogWDGConfig Structure of ADC analog watchdog configuration
  1557. * @retval HAL status
  1558. */
  1559. HAL_StatusTypeDef HAL_ADC_AnalogWDGConfig(ADC_HandleTypeDef* hadc, ADC_AnalogWDGConfTypeDef* AnalogWDGConfig)
  1560. {
  1561. HAL_StatusTypeDef tmp_hal_status = HAL_OK;
  1562. uint32_t tmpAWDHighThresholdShifted;
  1563. uint32_t tmpAWDLowThresholdShifted;
  1564. /* Check the parameters */
  1565. assert_param(IS_ADC_ALL_INSTANCE(hadc->Instance));
  1566. assert_param(IS_ADC_ANALOG_WATCHDOG_MODE(AnalogWDGConfig->WatchdogMode));
  1567. assert_param(IS_FUNCTIONAL_STATE(AnalogWDGConfig->ITMode));
  1568. /* Verify if threshold is within the selected ADC resolution */
  1569. assert_param(IS_ADC_RANGE(ADC_GET_RESOLUTION(hadc), AnalogWDGConfig->HighThreshold));
  1570. assert_param(IS_ADC_RANGE(ADC_GET_RESOLUTION(hadc), AnalogWDGConfig->LowThreshold));
  1571. if(AnalogWDGConfig->WatchdogMode == ADC_ANALOGWATCHDOG_SINGLE_REG)
  1572. {
  1573. assert_param(IS_ADC_CHANNEL(AnalogWDGConfig->Channel));
  1574. }
  1575. /* Process locked */
  1576. __HAL_LOCK(hadc);
  1577. /* Parameters update conditioned to ADC state: */
  1578. /* Parameters that can be updated when ADC is disabled or enabled without */
  1579. /* conversion on going on regular group: */
  1580. /* - Analog watchdog channels */
  1581. /* - Analog watchdog thresholds */
  1582. if (ADC_IS_CONVERSION_ONGOING_REGULAR(hadc) == RESET)
  1583. {
  1584. /* Configuration of analog watchdog: */
  1585. /* - Set the analog watchdog enable mode: one or overall group of */
  1586. /* channels. */
  1587. /* - Set the Analog watchdog channel (is not used if watchdog */
  1588. /* mode "all channels": ADC_CFGR_AWD1SGL=0). */
  1589. hadc->Instance->CFGR1 &= ~( ADC_CFGR1_AWDSGL |
  1590. ADC_CFGR1_AWDEN |
  1591. ADC_CFGR1_AWDCH );
  1592. hadc->Instance->CFGR1 |= ( AnalogWDGConfig->WatchdogMode |
  1593. ADC_CFGR_AWDCH(AnalogWDGConfig->Channel) );
  1594. /* Shift the offset in function of the selected ADC resolution: Thresholds*/
  1595. /* have to be left-aligned on bit 11, the LSB (right bits) are set to 0 */
  1596. tmpAWDHighThresholdShifted = ADC_AWD1THRESHOLD_SHIFT_RESOLUTION(hadc, AnalogWDGConfig->HighThreshold);
  1597. tmpAWDLowThresholdShifted = ADC_AWD1THRESHOLD_SHIFT_RESOLUTION(hadc, AnalogWDGConfig->LowThreshold);
  1598. /* Set the high and low thresholds */
  1599. hadc->Instance->TR &= ~(ADC_TR_HT | ADC_TR_LT);
  1600. hadc->Instance->TR |= ( ADC_TRX_HIGHTHRESHOLD (tmpAWDHighThresholdShifted) |
  1601. tmpAWDLowThresholdShifted );
  1602. /* Clear the ADC Analog watchdog flag (in case of left enabled by */
  1603. /* previous ADC operations) to be ready to use for HAL_ADC_IRQHandler() */
  1604. /* or HAL_ADC_PollForEvent(). */
  1605. __HAL_ADC_CLEAR_FLAG(hadc, ADC_IT_AWD);
  1606. /* Configure ADC Analog watchdog interrupt */
  1607. if(AnalogWDGConfig->ITMode == ENABLE)
  1608. {
  1609. /* Enable the ADC Analog watchdog interrupt */
  1610. __HAL_ADC_ENABLE_IT(hadc, ADC_IT_AWD);
  1611. }
  1612. else
  1613. {
  1614. /* Disable the ADC Analog watchdog interrupt */
  1615. __HAL_ADC_DISABLE_IT(hadc, ADC_IT_AWD);
  1616. }
  1617. }
  1618. /* If a conversion is on going on regular group, no update could be done */
  1619. /* on neither of the AWD configuration structure parameters. */
  1620. else
  1621. {
  1622. /* Update ADC state machine to error */
  1623. SET_BIT(hadc->State, HAL_ADC_STATE_ERROR_CONFIG);
  1624. tmp_hal_status = HAL_ERROR;
  1625. }
  1626. /* Process unlocked */
  1627. __HAL_UNLOCK(hadc);
  1628. /* Return function status */
  1629. return tmp_hal_status;
  1630. }
  1631. /**
  1632. * @}
  1633. */
  1634. /** @defgroup ADC_Exported_Functions_Group4 Peripheral State functions
  1635. * @brief Peripheral State functions
  1636. *
  1637. @verbatim
  1638. ===============================================================================
  1639. ##### Peripheral State and Errors functions #####
  1640. ===============================================================================
  1641. [..]
  1642. This subsection provides functions to get in run-time the status of the
  1643. peripheral.
  1644. (+) Check the ADC state
  1645. (+) Check the ADC error code
  1646. @endverbatim
  1647. * @{
  1648. */
  1649. /**
  1650. * @brief Return the ADC state
  1651. * @note ADC state machine is managed by bitfields, ADC status must be
  1652. * compared with states bits.
  1653. * For example:
  1654. * " if (HAL_IS_BIT_SET(HAL_ADC_GetState(hadc1), HAL_ADC_STATE_REG_BUSY)) "
  1655. * " if (HAL_IS_BIT_SET(HAL_ADC_GetState(hadc1), HAL_ADC_STATE_AWD1) ) "
  1656. * @param hadc ADC handle
  1657. * @retval HAL state
  1658. */
  1659. uint32_t HAL_ADC_GetState(ADC_HandleTypeDef* hadc)
  1660. {
  1661. /* Check the parameters */
  1662. assert_param(IS_ADC_ALL_INSTANCE(hadc->Instance));
  1663. /* Return ADC state */
  1664. return hadc->State;
  1665. }
  1666. /**
  1667. * @brief Return the ADC error code
  1668. * @param hadc ADC handle
  1669. * @retval ADC Error Code
  1670. */
  1671. uint32_t HAL_ADC_GetError(ADC_HandleTypeDef *hadc)
  1672. {
  1673. return hadc->ErrorCode;
  1674. }
  1675. /**
  1676. * @}
  1677. */
  1678. /**
  1679. * @}
  1680. */
  1681. /** @defgroup ADC_Private_Functions ADC Private Functions
  1682. * @{
  1683. */
  1684. /**
  1685. * @brief Enable the selected ADC.
  1686. * @note Prerequisite condition to use this function: ADC must be disabled
  1687. * and voltage regulator must be enabled (done into HAL_ADC_Init()).
  1688. * @param hadc ADC handle
  1689. * @retval HAL status.
  1690. */
  1691. static HAL_StatusTypeDef ADC_Enable(ADC_HandleTypeDef* hadc)
  1692. {
  1693. __IO uint32_t wait_loop_index = 0U;
  1694. /* ADC enable and wait for ADC ready (in case of ADC is disabled or */
  1695. /* enabling phase not yet completed: flag ADC ready not yet set). */
  1696. /* Timeout implemented to not be stuck if ADC cannot be enabled (possible */
  1697. /* causes: ADC clock not running, ...). */
  1698. if (ADC_IS_ENABLE(hadc) == RESET)
  1699. {
  1700. /* Check if conditions to enable the ADC are fulfilled */
  1701. if (ADC_ENABLING_CONDITIONS(hadc) == RESET)
  1702. {
  1703. /* Update ADC state machine to error */
  1704. SET_BIT(hadc->State, HAL_ADC_STATE_ERROR_INTERNAL);
  1705. /* Set ADC error code to ADC IP internal error */
  1706. SET_BIT(hadc->ErrorCode, HAL_ADC_ERROR_INTERNAL);
  1707. return HAL_ERROR;
  1708. }
  1709. /* Enable the ADC peripheral */
  1710. __HAL_ADC_ENABLE(hadc);
  1711. /* Delay for ADC stabilization time */
  1712. /* Compute number of CPU cycles to wait for */
  1713. wait_loop_index = (ADC_STAB_DELAY_US * (SystemCoreClock / 1000000U));
  1714. while(wait_loop_index != 0U)
  1715. {
  1716. wait_loop_index--;
  1717. }
  1718. }
  1719. /* Return HAL status */
  1720. return HAL_OK;
  1721. }
  1722. /**
  1723. * @brief Disable the selected ADC.
  1724. * @note Prerequisite condition to use this function: ADC conversions must be
  1725. * stopped.
  1726. * @param hadc ADC handle
  1727. * @retval HAL status.
  1728. */
  1729. static HAL_StatusTypeDef ADC_Disable(ADC_HandleTypeDef* hadc)
  1730. {
  1731. uint32_t tickstart = 0U;
  1732. /* Verification if ADC is not already disabled: */
  1733. /* Note: forbidden to disable ADC if ADC is already */
  1734. /* disabled. */
  1735. if (ADC_IS_ENABLE(hadc) != RESET)
  1736. {
  1737. /* Check if conditions to disable the ADC are fulfilled */
  1738. if (ADC_DISABLING_CONDITIONS(hadc) != RESET)
  1739. {
  1740. /* Disable the ADC peripheral */
  1741. __HAL_ADC_DISABLE(hadc);
  1742. }
  1743. else
  1744. {
  1745. /* Update ADC state machine to error */
  1746. SET_BIT(hadc->State, HAL_ADC_STATE_ERROR_INTERNAL);
  1747. /* Set ADC error code to ADC IP internal error */
  1748. SET_BIT(hadc->ErrorCode, HAL_ADC_ERROR_INTERNAL);
  1749. return HAL_ERROR;
  1750. }
  1751. /* Wait for ADC effectively disabled */
  1752. /* Get tick count */
  1753. tickstart = HAL_GetTick();
  1754. while(HAL_IS_BIT_SET(hadc->Instance->CR, ADC_CR_ADEN))
  1755. {
  1756. if((HAL_GetTick() - tickstart) > ADC_DISABLE_TIMEOUT)
  1757. {
  1758. /* Update ADC state machine to error */
  1759. SET_BIT(hadc->State, HAL_ADC_STATE_ERROR_INTERNAL);
  1760. /* Set ADC error code to ADC IP internal error */
  1761. SET_BIT(hadc->ErrorCode, HAL_ADC_ERROR_INTERNAL);
  1762. return HAL_ERROR;
  1763. }
  1764. }
  1765. }
  1766. /* Return HAL status */
  1767. return HAL_OK;
  1768. }
  1769. /**
  1770. * @brief Stop ADC conversion.
  1771. * @note Prerequisite condition to use this function: ADC conversions must be
  1772. * stopped to disable the ADC.
  1773. * @param hadc ADC handle
  1774. * @retval HAL status.
  1775. */
  1776. static HAL_StatusTypeDef ADC_ConversionStop(ADC_HandleTypeDef* hadc)
  1777. {
  1778. uint32_t tickstart = 0U;
  1779. /* Check the parameters */
  1780. assert_param(IS_ADC_ALL_INSTANCE(hadc->Instance));
  1781. /* Verification if ADC is not already stopped on regular group to bypass */
  1782. /* this function if not needed. */
  1783. if (ADC_IS_CONVERSION_ONGOING_REGULAR(hadc))
  1784. {
  1785. /* Stop potential conversion on going on regular group */
  1786. /* Software is allowed to set ADSTP only when ADSTART=1 */
  1787. if (HAL_IS_BIT_SET(hadc->Instance->CR, ADC_CR_ADSTART) &&
  1788. HAL_IS_BIT_SET(hadc->Instance->CR, ADC_CR_ADEN))
  1789. {
  1790. /* Stop conversions on regular group */
  1791. hadc->Instance->CR |= ADC_CR_ADSTP;
  1792. }
  1793. /* Wait for conversion effectively stopped */
  1794. /* Get tick count */
  1795. tickstart = HAL_GetTick();
  1796. while((hadc->Instance->CR & ADC_CR_ADSTART) != RESET)
  1797. {
  1798. if((HAL_GetTick() - tickstart) > ADC_STOP_CONVERSION_TIMEOUT)
  1799. {
  1800. /* Update ADC state machine to error */
  1801. SET_BIT(hadc->State, HAL_ADC_STATE_ERROR_INTERNAL);
  1802. /* Set ADC error code to ADC IP internal error */
  1803. SET_BIT(hadc->ErrorCode, HAL_ADC_ERROR_INTERNAL);
  1804. return HAL_ERROR;
  1805. }
  1806. }
  1807. }
  1808. /* Return HAL status */
  1809. return HAL_OK;
  1810. }
  1811. #if (defined(DMA) || defined(DMA1))
  1812. /**
  1813. * @brief DMA transfer complete callback.
  1814. * @note Depending on devices and packages, DMA may not be available.
  1815. * Refer to device datasheet for DMA availability.
  1816. * @param hdma pointer to DMA handle.
  1817. * @retval None
  1818. */
  1819. static void ADC_DMAConvCplt(DMA_HandleTypeDef *hdma)
  1820. {
  1821. /* Retrieve ADC handle corresponding to current DMA handle */
  1822. ADC_HandleTypeDef* hadc = ( ADC_HandleTypeDef* )((DMA_HandleTypeDef* )hdma)->Parent;
  1823. /* Update state machine on conversion status if not in error state */
  1824. if (HAL_IS_BIT_CLR(hadc->State, HAL_ADC_STATE_ERROR_INTERNAL | HAL_ADC_STATE_ERROR_DMA))
  1825. {
  1826. /* Set ADC state */
  1827. SET_BIT(hadc->State, HAL_ADC_STATE_REG_EOC);
  1828. /* Determine whether any further conversion upcoming on group regular */
  1829. /* by external trigger, continuous mode or scan sequence on going. */
  1830. if(ADC_IS_SOFTWARE_START_REGULAR(hadc) &&
  1831. (hadc->Init.ContinuousConvMode == DISABLE) )
  1832. {
  1833. /* If End of Sequence is reached, disable interrupts */
  1834. if( __HAL_ADC_GET_FLAG(hadc, ADC_FLAG_EOS) )
  1835. {
  1836. /* Allowed to modify bits ADC_IT_EOC/ADC_IT_EOS only if bit */
  1837. /* ADSTART==0 (no conversion on going) */
  1838. if (ADC_IS_CONVERSION_ONGOING_REGULAR(hadc) == RESET)
  1839. {
  1840. /* Disable ADC end of single conversion interrupt on group regular */
  1841. /* Note: Overrun interrupt was enabled with EOC interrupt in */
  1842. /* HAL_Start_IT(), but is not disabled here because can be used */
  1843. /* by overrun IRQ process below. */
  1844. __HAL_ADC_DISABLE_IT(hadc, ADC_IT_EOC | ADC_IT_EOS);
  1845. /* Set ADC state */
  1846. ADC_STATE_CLR_SET(hadc->State,
  1847. HAL_ADC_STATE_REG_BUSY,
  1848. HAL_ADC_STATE_READY);
  1849. }
  1850. else
  1851. {
  1852. /* Change ADC state to error state */
  1853. SET_BIT(hadc->State, HAL_ADC_STATE_ERROR_CONFIG);
  1854. /* Set ADC error code to ADC IP internal error */
  1855. SET_BIT(hadc->ErrorCode, HAL_ADC_ERROR_INTERNAL);
  1856. }
  1857. }
  1858. }
  1859. /* Conversion complete callback */
  1860. #if (USE_HAL_ADC_REGISTER_CALLBACKS == 1)
  1861. hadc->ConvCpltCallback(hadc);
  1862. #else
  1863. HAL_ADC_ConvCpltCallback(hadc);
  1864. #endif /* USE_HAL_ADC_REGISTER_CALLBACKS */
  1865. }
  1866. else
  1867. {
  1868. /* Call DMA error callback */
  1869. hadc->DMA_Handle->XferErrorCallback(hdma);
  1870. }
  1871. }
  1872. /**
  1873. * @brief DMA half transfer complete callback.
  1874. * @param hdma pointer to DMA handle.
  1875. * @retval None
  1876. */
  1877. static void ADC_DMAHalfConvCplt(DMA_HandleTypeDef *hdma)
  1878. {
  1879. /* Retrieve ADC handle corresponding to current DMA handle */
  1880. ADC_HandleTypeDef* hadc = ( ADC_HandleTypeDef* )((DMA_HandleTypeDef* )hdma)->Parent;
  1881. /* Half conversion callback */
  1882. #if (USE_HAL_ADC_REGISTER_CALLBACKS == 1)
  1883. hadc->ConvHalfCpltCallback(hadc);
  1884. #else
  1885. HAL_ADC_ConvHalfCpltCallback(hadc);
  1886. #endif /* USE_HAL_ADC_REGISTER_CALLBACKS */
  1887. }
  1888. /**
  1889. * @brief DMA error callback
  1890. * @note Depending on devices and packages, DMA may not be available.
  1891. * Refer to device datasheet for DMA availability.
  1892. * @param hdma pointer to DMA handle.
  1893. * @retval None
  1894. */
  1895. static void ADC_DMAError(DMA_HandleTypeDef *hdma)
  1896. {
  1897. /* Retrieve ADC handle corresponding to current DMA handle */
  1898. ADC_HandleTypeDef* hadc = ( ADC_HandleTypeDef* )((DMA_HandleTypeDef* )hdma)->Parent;
  1899. /* Set ADC state */
  1900. SET_BIT(hadc->State, HAL_ADC_STATE_ERROR_DMA);
  1901. /* Set ADC error code to DMA error */
  1902. SET_BIT(hadc->ErrorCode, HAL_ADC_ERROR_DMA);
  1903. /* Error callback */
  1904. #if (USE_HAL_ADC_REGISTER_CALLBACKS == 1)
  1905. hadc->ErrorCallback(hadc);
  1906. #else
  1907. HAL_ADC_ErrorCallback(hadc);
  1908. #endif /* USE_HAL_ADC_REGISTER_CALLBACKS */
  1909. }
  1910. #endif
  1911. /**
  1912. * @}
  1913. */
  1914. /** @addtogroup ADC_Exported_Functions_Group2
  1915. * @{
  1916. */
  1917. /**
  1918. * @brief Config ADC Calibration Sample Time and Selection.
  1919. * @param hadc ADC handle
  1920. * @param CalibSamplingTime ADC Calibration Sample Time
  1921. * This parameter can be one of the following values:
  1922. * @arg ADC_CALIBSAMPLETIME_2CYCLES: 2 ADC CYCLES
  1923. * @arg ADC_CALIBSAMPLETIME_4CYCLES: 4 ADC CYCLES
  1924. * @arg ADC_CALIBSAMPLETIME_8CYCLES: 8 ADC CYCLES
  1925. * @arg ADC_CALIBSAMPLETIME_1CYCLE: 1 ADC CYCLE
  1926. * @param CalibSelection ADC Calibration Selection
  1927. * This parameter can be one of the following values:
  1928. * @arg ADC_CALIBSELECTION_ONLYOFFSET: Only Calibrate the OFFSET
  1929. * @arg ADC_CALIBSELECTION_OFFSET_LINEARITY: Calibrate the OFFSET and LINEARITY
  1930. * @retval HAL status
  1931. */
  1932. HAL_StatusTypeDef HAL_ADC_SetCalibration(ADC_HandleTypeDef* hadc,uint32_t CalibSamplingTime,uint32_t CalibSelection)
  1933. {
  1934. HAL_StatusTypeDef tmp_hal_status = HAL_OK;
  1935. /* Check the parameters */
  1936. assert_param(IS_ADC_ALL_INSTANCE(hadc->Instance));
  1937. assert_param(IS_ADC_CALIBRATION_SAMPLETIME(CalibSamplingTime));
  1938. assert_param(IS_ADC_CALIBRATION_SELECTION(CalibSelection));
  1939. /* Process locked */
  1940. __HAL_LOCK(hadc);
  1941. /* Calibration prerequisite: ADC must be disabled. */
  1942. if (ADC_IS_ENABLE(hadc) == RESET)
  1943. {
  1944. hadc->Instance->CCSR &= ~( ADC_CCSR_CALSMP | ADC_CCSR_CALSEL );
  1945. hadc->Instance->CCSR |= ( CalibSamplingTime | CalibSelection );
  1946. }
  1947. else
  1948. {
  1949. /* Update ADC state machine to error */
  1950. SET_BIT(hadc->State, HAL_ADC_STATE_ERROR_CONFIG);
  1951. tmp_hal_status = HAL_ERROR;
  1952. }
  1953. /* Process unlocked */
  1954. __HAL_UNLOCK(hadc);
  1955. /* Return function status */
  1956. return tmp_hal_status;
  1957. }
  1958. /**
  1959. * @brief Get ADC Calibration Status.
  1960. * @param hadc ADC handle
  1961. * @retval This parameter can be one of the following values:
  1962. * @arg ADC_CALIBSTATUS_SUCCESS: Calibration Status Succcess
  1963. * @arg ADC_CALIBSTATUS_FAIL: Calibration Status Fail
  1964. * @arg ADC_CALIBSTATUS_ONGOING: Calibration Status On going
  1965. * @arg ADC_CALIBSTATUS_INVALID: Calibration Status Invalid
  1966. */
  1967. uint32_t HAL_ADC_GetCalibStatus(ADC_HandleTypeDef* hadc)
  1968. {
  1969. uint32_t adc_calibration_status = 0U;
  1970. /* Check the parameters */
  1971. assert_param(IS_ADC_ALL_INSTANCE(hadc->Instance));
  1972. /* Process locked */
  1973. __HAL_LOCK(hadc);
  1974. adc_calibration_status= READ_BIT(hadc->Instance->CCSR, ADC_CCSR_CALFAIL | ADC_CCSR_CALON);
  1975. /* Process unlocked */
  1976. __HAL_UNLOCK(hadc);
  1977. /* Return function status */
  1978. return adc_calibration_status;
  1979. }
  1980. /**
  1981. * @brief Sort the array.
  1982. * @param arr the array
  1983. * @param length the array length
  1984. * @retval None
  1985. */
  1986. static void ADC_QuickSort(int32_t arr[], int32_t length)
  1987. {
  1988. int32_t pos,min;
  1989. for(int32_t i=0;i<length;i++)
  1990. {
  1991. min = arr[i];
  1992. pos = i;
  1993. for(int32_t j=i+1;j<length;j++)
  1994. {
  1995. if(min > arr[j])
  1996. {
  1997. min = arr[j];
  1998. pos = j;
  1999. }
  2000. }
  2001. arr[pos] = arr[i];
  2002. arr[i] = min;
  2003. }
  2004. }
  2005. /**
  2006. * @brief Perform an ADC automatic self-calibration
  2007. * Calibration prerequisite: ADC must be disabled (execute this
  2008. * function before HAL_ADC_Start() or after HAL_ADC_Stop() ).
  2009. * @note Calibration factor can be read after calibration, using function
  2010. * HAL_ADC_GetValue() (value on 7 bits: from DR[6;0]).
  2011. * @param hadc ADC handle
  2012. * @retval HAL status
  2013. */
  2014. HAL_StatusTypeDef HAL_ADC_Calibration_SingleStart(ADC_HandleTypeDef* hadc)
  2015. {
  2016. HAL_StatusTypeDef tmp_hal_status = HAL_OK;
  2017. uint32_t tickstart = 0U;
  2018. #if (defined(DMA) || defined(DMA1))
  2019. uint32_t backup_setting_adc_dma_transfer = 0; /* Note: Variable not declared as volatile because register read is already declared as volatile */
  2020. #endif
  2021. /* Check the parameters */
  2022. assert_param(IS_ADC_ALL_INSTANCE(hadc->Instance));
  2023. /* Process locked */
  2024. __HAL_LOCK(hadc);
  2025. /* Calibration prerequisite: ADC must be disabled. */
  2026. if (ADC_IS_ENABLE(hadc) == RESET)
  2027. {
  2028. /* Set ADC state */
  2029. ADC_STATE_CLR_SET(hadc->State,
  2030. HAL_ADC_STATE_REG_BUSY,
  2031. HAL_ADC_STATE_BUSY_INTERNAL);
  2032. #if (defined(DMA) || defined(DMA1))
  2033. /* Disable ADC DMA transfer request during calibration */
  2034. /* Note: Specificity of this PY32 serie: Calibration factor is */
  2035. /* available in data register and also transfered by DMA. */
  2036. /* To not insert ADC calibration factor among ADC conversion data */
  2037. /* in array variable, DMA transfer must be disabled during */
  2038. /* calibration. */
  2039. backup_setting_adc_dma_transfer = READ_BIT(hadc->Instance->CFGR1, ADC_CFGR1_DMAEN | ADC_CFGR1_DMACFG);
  2040. CLEAR_BIT(hadc->Instance->CFGR1, ADC_CFGR1_DMAEN | ADC_CFGR1_DMACFG);
  2041. #endif
  2042. /* Start ADC calibration */
  2043. hadc->Instance->CR |= ADC_CR_ADCAL;
  2044. tickstart = HAL_GetTick();
  2045. /* Wait for calibration completion */
  2046. while(HAL_IS_BIT_SET(hadc->Instance->CR, ADC_CR_ADCAL))
  2047. {
  2048. if((HAL_GetTick() - tickstart) > ADC_CALIBRATION_TIMEOUT)
  2049. {
  2050. /* Update ADC state machine to error */
  2051. ADC_STATE_CLR_SET(hadc->State,
  2052. HAL_ADC_STATE_BUSY_INTERNAL,
  2053. HAL_ADC_STATE_ERROR_INTERNAL);
  2054. /* Process unlocked */
  2055. __HAL_UNLOCK(hadc);
  2056. return HAL_ERROR;
  2057. }
  2058. }
  2059. #if (defined(DMA) || defined(DMA1))
  2060. /* Restore ADC DMA transfer request after calibration */
  2061. SET_BIT(hadc->Instance->CFGR1, backup_setting_adc_dma_transfer);
  2062. #endif
  2063. /* Set ADC state */
  2064. ADC_STATE_CLR_SET(hadc->State,
  2065. HAL_ADC_STATE_BUSY_INTERNAL,
  2066. HAL_ADC_STATE_RESET);
  2067. }
  2068. else
  2069. {
  2070. /* Update ADC state machine to error */
  2071. SET_BIT(hadc->State, HAL_ADC_STATE_ERROR_CONFIG);
  2072. tmp_hal_status = HAL_ERROR;
  2073. }
  2074. /* Process unlocked */
  2075. __HAL_UNLOCK(hadc);
  2076. /* Return function status */
  2077. return tmp_hal_status;
  2078. }
  2079. /**
  2080. * @brief Perform an ADC automatic self-calibration
  2081. * Calibration prerequisite: ADC must be disabled (execute this
  2082. * function before HAL_ADC_Start() or after HAL_ADC_Stop() ).
  2083. * @note Calibration factor can be read after calibration, using function
  2084. * HAL_ADC_GetValue() (value on 7 bits: from DR[6;0]).
  2085. * @param hadc ADC handle
  2086. * @retval HAL status
  2087. */
  2088. HAL_StatusTypeDef HAL_ADC_Calibration_Start(ADC_HandleTypeDef* hadc)
  2089. {
  2090. uint32_t ADC_CALRR1_Buff[5];
  2091. int32_t ADC_CALRR1_BuffInt[5];
  2092. for(int i=0;i<5;i++)
  2093. {
  2094. /* ADC calibration */
  2095. if (HAL_ADC_Calibration_SingleStart(hadc) != HAL_OK)
  2096. {
  2097. return HAL_ERROR;
  2098. }
  2099. ADC_CALRR1_Buff[i]= *((__IO uint32_t *)(0x40012448));
  2100. ADC_CALRR1_BuffInt[i]=(int32_t)(ADC_CALRR1_Buff[i]<<9);
  2101. }
  2102. ADC_QuickSort(ADC_CALRR1_BuffInt,5);
  2103. *((__IO uint32_t *)(0x40012450)) = (ADC_CALRR1_BuffInt[2]>>9);
  2104. *((__IO uint32_t *)(0x40012454)) = *((__IO uint32_t *)(0x4001244C));
  2105. *((__IO uint32_t *)(0x40012444)) |= 0x8000;
  2106. return HAL_OK;
  2107. }
  2108. /**
  2109. * @}
  2110. */
  2111. #endif /* HAL_ADC_MODULE_ENABLED */
  2112. /**
  2113. * @}
  2114. */
  2115. /**
  2116. * @}
  2117. */
  2118. /************************ (C) COPYRIGHT Puya *****END OF FILE****/