stm32f1xx_hal_adc.c 89 KB

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