stm32l1xx_hal_adc.c 60 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759
  1. /**
  2. ******************************************************************************
  3. * @file stm32l1xx_hal_adc.c
  4. * @author MCD Application conversion
  5. * @version V1.0.0
  6. * @date 5-September-2014
  7. * @brief This file provides firmware functions to manage the following
  8. * functionalities of the Analog to Digital Convertor (ADC)
  9. * peripheral:
  10. * + Initialization and de-initialization functions
  11. * ++ Initialization and Configuration of ADC
  12. * + Operation functions
  13. * ++ Start, stop, get result of conversions of regular
  14. * group, using 3 possible modes: polling, interruption or DMA.
  15. * + Control functions
  16. * ++ Analog Watchdog configuration
  17. * ++ Channels configuration on regular group
  18. * + State functions
  19. * ++ ADC state machine management
  20. * ++ Interrupts and flags management
  21. * Other functions (extended functions) are available in file
  22. * "stm32l1xx_hal_adc_ex.c".
  23. *
  24. @verbatim
  25. ==============================================================================
  26. ##### ADC specific features #####
  27. ==============================================================================
  28. [..]
  29. (#) 12-bit, 10-bit, 8-bit or 6-bit configurable resolution
  30. (#) Interrupt generation at the end of regular conversion, end of injected
  31. conversion, and in case of analog watchdog or overrun events.
  32. (#) Single and continuous conversion modes.
  33. (#) Scan mode for automatic conversion of channel 0 to channel 'n'.
  34. (#) Data alignment with in-built data coherency.
  35. (#) Channel-wise programmable sampling time.
  36. (#) ADC conversion Regular or Injected groups.
  37. (#) External trigger (timer or EXTI) with configurable polarity for both
  38. regular and injected groups.
  39. (#) DMA request generation for transfer of conversions data of regular group.
  40. (#) ADC calibration
  41. (#) ADC offset on injected channels
  42. (#) ADC supply requirements: 2.4 V to 3.6 V at full speed and down to 1.8 V at
  43. slower speed.
  44. (#) ADC input range: from Vref– (connected to Vssa) to Vref+ (connected to
  45. Vdda or to an external voltage reference).
  46. ##### How to use this driver #####
  47. ==============================================================================
  48. [..]
  49. (#) Enable the ADC interface
  50. As prerequisite, ADC clock must be configured at RCC top level.
  51. Two clocks settings are mandatory:
  52. - ADC clock (core clock):
  53. Example:
  54. Into HAL_ADC_MspInit() (recommended code location):
  55. __ADC1_CLK_ENABLE();
  56. - ADC clock (conversions clock):
  57. Only one possible clock source: derived from HSI RC 16MHz oscillator
  58. (HSI).
  59. Example:
  60. Into HAL_ADC_MspInit() or with main setting of RCC:
  61. RCC_OscInitTypeDef RCC_OscInitStructure;
  62. HAL_RCC_GetOscConfig(&RCC_OscInitStructure);
  63. RCC_OscInitStructure.OscillatorType = (... | RCC_OSCILLATORTYPE_HSI);
  64. RCC_OscInitStructure.HSIState = RCC_HSI_ON;
  65. RCC_OscInitStructure.HSICalibrationValue = RCC_HSICALIBRATION_DEFAULT;
  66. RCC_OscInitStructure.PLL.PLLState = RCC_PLL_NONE;
  67. RCC_OscInitStructure.PLL.PLLSource = ...
  68. RCC_OscInitStructure.PLL...
  69. HAL_RCC_OscConfig(&RCC_OscInitStructure);
  70. Note: ADC is connected directly to HSI RC 16MHz oscillator.
  71. Therefore, RCC PLL setting has no impact on ADC.
  72. PLL can be disabled (".PLL.PLLState = RCC_PLL_NONE") or
  73. enabled with HSI16 as clock source
  74. (".PLL.PLLSource = RCC_PLLSOURCE_HSI") to be used as device
  75. main clock source SYSCLK.
  76. The only mandatory setting is ".HSIState = RCC_HSI_ON"
  77. Note: ADC clock prescaler is configured at ADC level with
  78. parameter "ClockPrescaler" using function HAL_ADC_Init().
  79. (#) ADC pins configuration
  80. (++) Enable the clock for the ADC GPIOs using the following function:
  81. __GPIOx_CLK_ENABLE();
  82. (++) Configure these ADC pins in analog mode using HAL_GPIO_Init();
  83. (#) Configure the ADC parameters (conversion resolution, data alignment,
  84. continuous mode, ...) using the HAL_ADC_Init() function.
  85. (#) Activate the ADC peripheral using one of the start functions:
  86. HAL_ADC_Start(), HAL_ADC_Start_IT(), HAL_ADC_Start_DMA().
  87. *** Channels configuration to regular group ***
  88. ================================================
  89. [..]
  90. (+) To configure the ADC regular group features, use
  91. HAL_ADC_Init() and HAL_ADC_ConfigChannel() functions.
  92. (+) To read the ADC converted values, use the HAL_ADC_GetValue() function.
  93. *** DMA for regular group configuration ***
  94. ===========================================
  95. [..]
  96. (+) To enable the DMA mode for regular group, use the
  97. HAL_ADC_Start_DMA() function.
  98. (+) To enable the generation of DMA requests continuously at the end of
  99. the last DMA transfer, use the HAL_ADC_Init() function.
  100. @endverbatim
  101. ******************************************************************************
  102. * @attention
  103. *
  104. * <h2><center>&copy; COPYRIGHT(c) 2014 STMicroelectronics</center></h2>
  105. *
  106. * Redistribution and use in source and binary forms, with or without modification,
  107. * are permitted provided that the following conditions are met:
  108. * 1. Redistributions of source code must retain the above copyright notice,
  109. * this list of conditions and the following disclaimer.
  110. * 2. Redistributions in binary form must reproduce the above copyright notice,
  111. * this list of conditions and the following disclaimer in the documentation
  112. * and/or other materials provided with the distribution.
  113. * 3. Neither the name of STMicroelectronics nor the names of its contributors
  114. * may be used to endorse or promote products derived from this software
  115. * without specific prior written permission.
  116. *
  117. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  118. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  119. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  120. * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
  121. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  122. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  123. * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  124. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  125. * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  126. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  127. *
  128. ******************************************************************************
  129. */
  130. /* Includes ------------------------------------------------------------------*/
  131. #include "stm32l1xx_hal.h"
  132. /** @addtogroup STM32L1xx_HAL_Driver
  133. * @{
  134. */
  135. /** @defgroup ADC ADC
  136. * @brief ADC HAL module driver
  137. * @{
  138. */
  139. #ifdef HAL_ADC_MODULE_ENABLED
  140. /* Private typedef -----------------------------------------------------------*/
  141. /* Private define ------------------------------------------------------------*/
  142. /** @defgroup ADC_Private_Constants ADC Private Constants
  143. * @{
  144. */
  145. /* Fixed timeout values for ADC calibration, enable settling time. */
  146. /* Values defined to be higher than worst cases: low clocks freq, */
  147. /* maximum prescaler. */
  148. /* Ex of profile low frequency : Clock source at 0.1 MHz, ADC clock */
  149. /* prescaler 4, sampling time 7.5 ADC clock cycles, resolution 12 bits. */
  150. /* Unit: ms */
  151. #define ADC_ENABLE_TIMEOUT ((uint32_t) 2)
  152. #define ADC_DISABLE_TIMEOUT ((uint32_t) 2)
  153. /* Delay for ADC stabilization time. */
  154. /* Maximum delay is 3.5us (refer to device datasheet, parameter tSTAB). */
  155. /* Delay in CPU cycles, fixed to worst case: maximum CPU frequency 32MHz to */
  156. /* have the minimum number of CPU cycles to fulfill this delay. */
  157. #define ADC_STAB_DELAY_CPU_CYCLES ((uint32_t)112)
  158. /**
  159. * @}
  160. */
  161. /* Private macro -------------------------------------------------------------*/
  162. /* Private variables ---------------------------------------------------------*/
  163. /* Private function prototypes -----------------------------------------------*/
  164. /** @defgroup ADC_Private_Functions ADC Private Functions
  165. * @{
  166. */
  167. static void ADC_DMAConvCplt(DMA_HandleTypeDef *hdma);
  168. static void ADC_DMAHalfConvCplt(DMA_HandleTypeDef *hdma);
  169. static void ADC_DMAError(DMA_HandleTypeDef *hdma);
  170. /**
  171. * @}
  172. */
  173. /* Exported functions ---------------------------------------------------------*/
  174. /** @defgroup ADC_Exported_Functions ADC Exported Functions
  175. * @{
  176. */
  177. /** @defgroup ADC_Exported_Functions_Group1 Initialization/de-initialization functions
  178. * @brief Initialization and Configuration functions
  179. *
  180. @verbatim
  181. ===============================================================================
  182. ##### Initialization and de-initialization functions #####
  183. ===============================================================================
  184. [..] This section provides functions allowing to:
  185. (+) Initialize and configure the ADC.
  186. (+) De-initialize the ADC
  187. @endverbatim
  188. * @{
  189. */
  190. /**
  191. * @brief Initializes the ADC peripheral and regular group according to
  192. * parameters specified in structure "ADC_InitTypeDef".
  193. * @note As prerequisite, ADC clock must be configured at RCC top level
  194. * (clock source APB2).
  195. * See commented example code below that can be copied and uncommented
  196. * into HAL_ADC_MspInit().
  197. * @note Possibility to update parameters on the fly:
  198. * This function initializes the ADC MSP (HAL_ADC_MspInit()) only when
  199. * coming from ADC state reset. Following calls to this function can
  200. * be used to reconfigure some parameters of ADC_InitTypeDef
  201. * structure on the fly, without modifying MSP configuration. If ADC
  202. * MSP has to be modified again, HAL_ADC_DeInit() must be called
  203. * before HAL_ADC_Init().
  204. * The setting of these parameters is conditioned to ADC state.
  205. * For parameters constraints, see comments of structure
  206. * "ADC_InitTypeDef".
  207. * @note This function configures the ADC within 2 scopes: scope of entire
  208. * ADC and scope of regular group. For parameters details, see comments
  209. * of structure "ADC_InitTypeDef".
  210. * @param hadc: ADC handle
  211. * @retval HAL status
  212. */
  213. HAL_StatusTypeDef HAL_ADC_Init(ADC_HandleTypeDef* hadc)
  214. {
  215. HAL_StatusTypeDef tmpHALStatus = HAL_OK;
  216. uint32_t tmp_cr1 = 0;
  217. uint32_t tmp_cr2 = 0;
  218. /* Check ADC handle */
  219. if(hadc == NULL)
  220. {
  221. return HAL_ERROR;
  222. }
  223. /* Check the parameters */
  224. assert_param(IS_ADC_ALL_INSTANCE(hadc->Instance));
  225. assert_param(IS_ADC_CLOCKPRESCALER(hadc->Init.ClockPrescaler));
  226. assert_param(IS_ADC_RESOLUTION(hadc->Init.Resolution));
  227. assert_param(IS_ADC_DATA_ALIGN(hadc->Init.DataAlign));
  228. assert_param(IS_ADC_SCAN_MODE(hadc->Init.ScanConvMode));
  229. assert_param(IS_ADC_EOC_SELECTION(hadc->Init.EOCSelection));
  230. assert_param(IS_ADC_AUTOWAIT(hadc->Init.LowPowerAutoWait));
  231. assert_param(IS_ADC_AUTOPOWEROFF(hadc->Init.LowPowerAutoPowerOff));
  232. assert_param(IS_ADC_CHANNELSBANK(hadc->Init.ChannelsBank));
  233. assert_param(IS_FUNCTIONAL_STATE(hadc->Init.ContinuousConvMode));
  234. assert_param(IS_ADC_EXTTRIG(hadc->Init.ExternalTrigConv));
  235. assert_param(IS_FUNCTIONAL_STATE(hadc->Init.DMAContinuousRequests));
  236. if(hadc->Init.ScanConvMode != ADC_SCAN_DISABLE)
  237. {
  238. assert_param(IS_ADC_REGULAR_NB_CONV(hadc->Init.NbrOfConversion));
  239. assert_param(IS_FUNCTIONAL_STATE(hadc->Init.DiscontinuousConvMode));
  240. assert_param(IS_ADC_REGULAR_DISCONT_NUMBER(hadc->Init.NbrOfDiscConversion));
  241. }
  242. if(hadc->Init.ExternalTrigConv != ADC_SOFTWARE_START)
  243. {
  244. assert_param(IS_ADC_EXTTRIG_EDGE(hadc->Init.ExternalTrigConvEdge));
  245. }
  246. /* As prerequisite, into HAL_ADC_MspInit(), ADC clock must be configured */
  247. /* at RCC top level. */
  248. /* Refer to header of this file for more details on clock enabling */
  249. /* procedure. */
  250. /* Actions performed only if ADC is coming from state reset: */
  251. /* - Initialization of ADC MSP */
  252. if (hadc->State == HAL_ADC_STATE_RESET)
  253. {
  254. /* Enable SYSCFG clock to control the routing Interface (RI) */
  255. __SYSCFG_CLK_ENABLE();
  256. /* Init the low level hardware */
  257. HAL_ADC_MspInit(hadc);
  258. }
  259. /* Configuration of ADC parameters if previous preliminary actions are */
  260. /* correctly completed. */
  261. if (tmpHALStatus != HAL_ERROR)
  262. {
  263. /* Initialize the ADC state */
  264. hadc->State = HAL_ADC_STATE_BUSY;
  265. /* Set ADC parameters */
  266. /* Configuration of common ADC clock: clock source HSI with selectable */
  267. /* prescaler */
  268. MODIFY_REG(ADC->CCR ,
  269. ADC_CCR_ADCPRE ,
  270. hadc->Init.ClockPrescaler );
  271. /* Configuration of ADC: */
  272. /* - external trigger polarity */
  273. /* - End of conversion selection */
  274. /* - DMA continuous request */
  275. /* - Channels bank (Banks availability depends on devices categories) */
  276. /* - continuous conversion mode */
  277. tmp_cr2 |= (hadc->Init.DataAlign |
  278. hadc->Init.EOCSelection |
  279. __ADC_CR2_DMACONTREQ(hadc->Init.DMAContinuousRequests) |
  280. hadc->Init.ChannelsBank |
  281. __ADC_CR2_CONTINUOUS(hadc->Init.ContinuousConvMode) );
  282. /* Enable external trigger if trigger selection is different of software */
  283. /* start. */
  284. /* Note: This configuration keeps the hardware feature of parameter */
  285. /* ExternalTrigConvEdge "trigger edge none" equivalent to */
  286. /* software start. */
  287. if (hadc->Init.ExternalTrigConv != ADC_SOFTWARE_START)
  288. {
  289. tmp_cr2 |= ( hadc->Init.ExternalTrigConv |
  290. hadc->Init.ExternalTrigConvEdge );
  291. }
  292. /* Parameters update conditioned to ADC state: */
  293. /* Parameters that can be updated only when ADC is disabled: */
  294. /* - delay selection (LowPowerAutoWait mode) */
  295. /* - resolution */
  296. /* - auto power off (LowPowerAutoPowerOff mode) */
  297. /* - scan mode */
  298. /* - discontinuous mode disable/enable */
  299. /* - discontinuous mode number of conversions */
  300. if ((__HAL_ADC_IS_ENABLED(hadc) == RESET))
  301. {
  302. tmp_cr2 |= hadc->Init.LowPowerAutoWait;
  303. tmp_cr1 |= (hadc->Init.Resolution |
  304. hadc->Init.LowPowerAutoPowerOff |
  305. __ADC_CR1_SCAN(hadc->Init.ScanConvMode) );
  306. /* Enable discontinuous mode only if continuous mode is disabled */
  307. if ((hadc->Init.DiscontinuousConvMode == ENABLE) &&
  308. (hadc->Init.ContinuousConvMode == DISABLE) )
  309. {
  310. /* Enable discontinuous mode of regular group */
  311. /* Set the number of channels to be converted in discontinuous mode */
  312. tmp_cr1 |= ((ADC_CR1_DISCEN) |
  313. __ADC_CR1_DISCONTINUOUS_NUM(hadc->Init.NbrOfDiscConversion));
  314. }
  315. /* Update ADC configuration register CR1 with previous settings */
  316. MODIFY_REG(hadc->Instance->CR1,
  317. ADC_CR1_RES |
  318. ADC_CR1_PDI |
  319. ADC_CR1_PDD |
  320. ADC_CR1_DISCNUM |
  321. ADC_CR1_DISCEN |
  322. ADC_CR1_SCAN ,
  323. tmp_cr1 );
  324. }
  325. /* Update ADC configuration register CR2 with previous settings */
  326. MODIFY_REG(hadc->Instance->CR2 ,
  327. __ADC_CR2_MASK_ADCINIT() ,
  328. tmp_cr2 );
  329. /* Configuration of regular group sequencer: */
  330. /* - if scan mode is disabled, regular channels sequence length is set to */
  331. /* 0x00: 1 channel converted (channel on regular rank 1) */
  332. /* Parameter "NbrOfConversion" is discarded. */
  333. /* Note: Scan mode is present by hardware on this device and, if */
  334. /* disabled, discards automatically nb of conversions. Anyway, nb of */
  335. /* conversions is forced to 0x00 for alignment over all STM32 devices. */
  336. /* - if scan mode is enabled, regular channels sequence length is set to */
  337. /* parameter "NbrOfConversion" */
  338. if (hadc->Init.ScanConvMode == ADC_SCAN_ENABLE)
  339. {
  340. MODIFY_REG(hadc->Instance->SQR1 ,
  341. ADC_SQR1_L ,
  342. __ADC_SQR1_L(hadc->Init.NbrOfConversion) );
  343. }
  344. else
  345. {
  346. MODIFY_REG(hadc->Instance->SQR1,
  347. ADC_SQR1_L ,
  348. 0x00000000 );
  349. }
  350. /* Check back that ADC registers have effectively been configured to */
  351. /* ensure of no potential problem of ADC core IP clocking. */
  352. /* Check through register CR2 (excluding execution control bits ADON, */
  353. /* JSWSTART, SWSTART and injected trigger bits JEXTEN and JEXTSEL). */
  354. if ((READ_REG(hadc->Instance->CR2) & ~(ADC_CR2_ADON |
  355. ADC_CR2_SWSTART | ADC_CR2_JSWSTART |
  356. ADC_CR2_JEXTEN | ADC_CR2_JEXTSEL ))
  357. == tmp_cr2)
  358. {
  359. /* Set ADC error code to none */
  360. __HAL_ADC_CLEAR_ERRORCODE(hadc);
  361. /* Initialize the ADC state */
  362. hadc->State = HAL_ADC_STATE_READY;
  363. }
  364. else
  365. {
  366. /* Update ADC state machine to error */
  367. hadc->State = HAL_ADC_STATE_ERROR;
  368. /* Set ADC error code to ADC IP internal error */
  369. hadc->ErrorCode |= HAL_ADC_ERROR_INTERNAL;
  370. tmpHALStatus = HAL_ERROR;
  371. }
  372. }
  373. else
  374. {
  375. /* Update ADC state machine to error */
  376. hadc->State = HAL_ADC_STATE_ERROR;
  377. tmpHALStatus = HAL_ERROR;
  378. }
  379. /* Return function status */
  380. return tmpHALStatus;
  381. }
  382. /**
  383. * @brief Deinitialize the ADC peripheral registers to its default reset values.
  384. * @note To not impact other ADCs, reset of common ADC registers have been
  385. * left commented below.
  386. * If needed, the example code can be copied and uncommented into
  387. * function HAL_ADC_MspDeInit().
  388. * @param hadc: ADC handle
  389. * @retval HAL status
  390. */
  391. HAL_StatusTypeDef HAL_ADC_DeInit(ADC_HandleTypeDef* hadc)
  392. {
  393. HAL_StatusTypeDef tmpHALStatus = HAL_OK;
  394. /* Check ADC handle */
  395. if(hadc == NULL)
  396. {
  397. return HAL_ERROR;
  398. }
  399. /* Check the parameters */
  400. assert_param(IS_ADC_ALL_INSTANCE(hadc->Instance));
  401. /* Change ADC state */
  402. hadc->State = HAL_ADC_STATE_BUSY;
  403. /* Stop potential conversion on going, on regular and injected groups */
  404. /* Disable ADC peripheral */
  405. tmpHALStatus = ADC_ConversionStop_Disable(hadc);
  406. /* Configuration of ADC parameters if previous preliminary actions are */
  407. /* correctly completed. */
  408. if (tmpHALStatus != HAL_ERROR)
  409. {
  410. /* ========== Reset ADC registers ========== */
  411. /* Reset register SR */
  412. __HAL_ADC_CLEAR_FLAG(hadc, (ADC_FLAG_AWD | ADC_FLAG_JEOC | ADC_FLAG_EOC |
  413. ADC_FLAG_JSTRT | ADC_FLAG_STRT));
  414. /* Reset register CR1 */
  415. CLEAR_BIT(hadc->Instance->CR1, (ADC_CR1_OVRIE | ADC_CR1_RES | ADC_CR1_AWDEN |
  416. ADC_CR1_JAWDEN | ADC_CR1_PDI | ADC_CR1_PDD |
  417. ADC_CR1_DISCNUM | ADC_CR1_JDISCEN | ADC_CR1_DISCEN |
  418. ADC_CR1_JAUTO | ADC_CR1_AWDSGL | ADC_CR1_SCAN |
  419. ADC_CR1_JEOCIE | ADC_CR1_AWDIE | ADC_CR1_EOCIE |
  420. ADC_CR1_AWDCH ));
  421. /* Reset register CR2 */
  422. __ADC_CR2_CLEAR(hadc);
  423. /* Reset register SMPR0 */
  424. __ADC_SMPR0_CLEAR(hadc);
  425. /* Reset register SMPR1 */
  426. CLEAR_BIT(hadc->Instance->SMPR1, (ADC_SMPR1_SMP29 | ADC_SMPR1_SMP28 | ADC_SMPR1_SMP27 |
  427. ADC_SMPR1_SMP26 | ADC_SMPR1_SMP25 | ADC_SMPR1_SMP24 |
  428. ADC_SMPR1_SMP23 | ADC_SMPR1_SMP22 | ADC_SMPR1_SMP21 |
  429. ADC_SMPR1_SMP20 ));
  430. /* Reset register SMPR2 */
  431. CLEAR_BIT(hadc->Instance->SMPR2, (ADC_SMPR2_SMP19 | ADC_SMPR2_SMP18 | ADC_SMPR2_SMP17 |
  432. ADC_SMPR2_SMP16 | ADC_SMPR2_SMP15 | ADC_SMPR2_SMP14 |
  433. ADC_SMPR2_SMP13 | ADC_SMPR2_SMP12 | ADC_SMPR2_SMP11 |
  434. ADC_SMPR2_SMP10 ));
  435. /* Reset register SMPR3 */
  436. CLEAR_BIT(hadc->Instance->SMPR3, (ADC_SMPR3_SMP9 | ADC_SMPR3_SMP8 | ADC_SMPR3_SMP7 |
  437. ADC_SMPR3_SMP6 | ADC_SMPR3_SMP5 | ADC_SMPR3_SMP4 |
  438. ADC_SMPR3_SMP3 | ADC_SMPR3_SMP2 | ADC_SMPR3_SMP1 |
  439. ADC_SMPR3_SMP0 ));
  440. /* Reset register JOFR1 */
  441. CLEAR_BIT(hadc->Instance->JOFR1, ADC_JOFR1_JOFFSET1);
  442. /* Reset register JOFR2 */
  443. CLEAR_BIT(hadc->Instance->JOFR2, ADC_JOFR2_JOFFSET2);
  444. /* Reset register JOFR3 */
  445. CLEAR_BIT(hadc->Instance->JOFR3, ADC_JOFR3_JOFFSET3);
  446. /* Reset register JOFR4 */
  447. CLEAR_BIT(hadc->Instance->JOFR4, ADC_JOFR4_JOFFSET4);
  448. /* Reset register HTR */
  449. CLEAR_BIT(hadc->Instance->HTR, ADC_HTR_HT);
  450. /* Reset register LTR */
  451. CLEAR_BIT(hadc->Instance->LTR, ADC_LTR_LT);
  452. /* Reset register SQR1 */
  453. CLEAR_BIT(hadc->Instance->SQR1, (ADC_SQR1_L | __ADC_SQR1_SQXX));
  454. /* Reset register SQR2 */
  455. CLEAR_BIT(hadc->Instance->SQR2, (ADC_SQR2_SQ24 | ADC_SQR2_SQ23 | ADC_SQR2_SQ22 |
  456. ADC_SQR2_SQ21 | ADC_SQR2_SQ20 | ADC_SQR2_SQ19 ));
  457. /* Reset register SQR3 */
  458. CLEAR_BIT(hadc->Instance->SQR3, (ADC_SQR3_SQ18 | ADC_SQR3_SQ17 | ADC_SQR3_SQ16 |
  459. ADC_SQR3_SQ15 | ADC_SQR3_SQ14 | ADC_SQR3_SQ13 ));
  460. /* Reset register SQR4 */
  461. CLEAR_BIT(hadc->Instance->SQR4, (ADC_SQR4_SQ12 | ADC_SQR4_SQ11 | ADC_SQR4_SQ10 |
  462. ADC_SQR4_SQ9 | ADC_SQR4_SQ8 | ADC_SQR4_SQ7 ));
  463. /* Reset register SQR5 */
  464. CLEAR_BIT(hadc->Instance->SQR5, (ADC_SQR5_SQ6 | ADC_SQR5_SQ5 | ADC_SQR5_SQ4 |
  465. ADC_SQR5_SQ3 | ADC_SQR5_SQ2 | ADC_SQR5_SQ1 ));
  466. /* Reset register JSQR */
  467. CLEAR_BIT(hadc->Instance->JSQR, (ADC_JSQR_JL |
  468. ADC_JSQR_JSQ4 | ADC_JSQR_JSQ3 |
  469. ADC_JSQR_JSQ2 | ADC_JSQR_JSQ1 ));
  470. /* Reset register JSQR */
  471. CLEAR_BIT(hadc->Instance->JSQR, (ADC_JSQR_JL |
  472. ADC_JSQR_JSQ4 | ADC_JSQR_JSQ3 |
  473. ADC_JSQR_JSQ2 | ADC_JSQR_JSQ1 ));
  474. /* Reset register DR */
  475. /* bits in access mode read only, no direct reset applicable*/
  476. /* Reset registers JDR1, JDR2, JDR3, JDR4 */
  477. /* bits in access mode read only, no direct reset applicable*/
  478. /* Reset register CCR */
  479. CLEAR_BIT(ADC->CCR, ADC_CCR_TSVREFE);
  480. /* ========== Hard reset ADC peripheral ========== */
  481. /* Performs a global reset of the entire ADC peripheral: ADC state is */
  482. /* forced to a similar state after device power-on. */
  483. /* If needed, copy-paste and uncomment the following reset code into */
  484. /* function "void HAL_ADC_MspInit(ADC_HandleTypeDef* hadc)": */
  485. /* */
  486. /* __ADC1_FORCE_RESET() */
  487. /* __ADC1_RELEASE_RESET() */
  488. /* DeInit the low level hardware */
  489. HAL_ADC_MspDeInit(hadc);
  490. /* Set ADC error code to none */
  491. __HAL_ADC_CLEAR_ERRORCODE(hadc);
  492. /* Change ADC state */
  493. hadc->State = HAL_ADC_STATE_RESET;
  494. }
  495. /* Process unlocked */
  496. __HAL_UNLOCK(hadc);
  497. /* Return function status */
  498. return tmpHALStatus;
  499. }
  500. /**
  501. * @brief Initializes the ADC MSP.
  502. * @param hadc: ADC handle
  503. * @retval None
  504. */
  505. __weak void HAL_ADC_MspInit(ADC_HandleTypeDef* hadc)
  506. {
  507. /* NOTE : This function should not be modified. When the callback is needed,
  508. function HAL_ADC_MspInit must be implemented in the user file.
  509. */
  510. }
  511. /**
  512. * @brief DeInitializes the ADC MSP.
  513. * @param hadc: ADC handle
  514. * @retval None
  515. */
  516. __weak void HAL_ADC_MspDeInit(ADC_HandleTypeDef* hadc)
  517. {
  518. /* NOTE : This function should not be modified. When the callback is needed,
  519. function HAL_ADC_MspDeInit must be implemented in the user file.
  520. */
  521. }
  522. /**
  523. * @}
  524. */
  525. /** @defgroup ADC_Exported_Functions_Group2 IO operation functions
  526. * @brief IO operation functions
  527. *
  528. @verbatim
  529. ===============================================================================
  530. ##### IO operation functions #####
  531. ===============================================================================
  532. [..] This section provides functions allowing to:
  533. (+) Start conversion of regular group.
  534. (+) Stop conversion of regular group.
  535. (+) Poll for conversion complete on regular group.
  536. (+) Poll for conversion event.
  537. (+) Get result of regular channel conversion.
  538. (+) Start conversion of regular group and enable interruptions.
  539. (+) Stop conversion of regular group and disable interruptions.
  540. (+) Handle ADC interrupt request
  541. (+) Start conversion of regular group and enable DMA transfer.
  542. (+) Stop conversion of regular group and disable ADC DMA transfer.
  543. @endverbatim
  544. * @{
  545. */
  546. /**
  547. * @brief Enables ADC, starts conversion of regular group.
  548. * Interruptions enabled in this function: None.
  549. * @param hadc: ADC handle
  550. * @retval HAL status
  551. */
  552. HAL_StatusTypeDef HAL_ADC_Start(ADC_HandleTypeDef* hadc)
  553. {
  554. HAL_StatusTypeDef tmpHALStatus = HAL_OK;
  555. /* Check the parameters */
  556. assert_param(IS_ADC_ALL_INSTANCE(hadc->Instance));
  557. /* Process locked */
  558. __HAL_LOCK(hadc);
  559. /* Enable the ADC peripheral */
  560. tmpHALStatus = ADC_Enable(hadc);
  561. /* Start conversion if ADC is effectively enabled */
  562. if (tmpHALStatus != HAL_ERROR)
  563. {
  564. /* State machine update: Check if an injected conversion is ongoing */
  565. if(hadc->State == HAL_ADC_STATE_BUSY_INJ)
  566. {
  567. /* Change ADC state */
  568. hadc->State = HAL_ADC_STATE_BUSY_INJ_REG;
  569. }
  570. else
  571. {
  572. /* Change ADC state */
  573. hadc->State = HAL_ADC_STATE_BUSY_REG;
  574. }
  575. /* Set ADC error code to none */
  576. __HAL_ADC_CLEAR_ERRORCODE(hadc);
  577. /* Clear regular group conversion flag and overrun flag */
  578. /* (To ensure of no unknown state from potential previous ADC operations) */
  579. __HAL_ADC_CLEAR_FLAG(hadc, ADC_FLAG_EOC);
  580. /* Start conversion of regular group if software start has been selected. */
  581. /* If external trigger has been selected, conversion will start at next */
  582. /* trigger event. */
  583. if (__HAL_ADC_IS_SOFTWARE_START_REGULAR(hadc))
  584. {
  585. /* Start ADC conversion on regular group */
  586. SET_BIT(hadc->Instance->CR2, ADC_CR2_SWSTART);
  587. }
  588. }
  589. /* Process unlocked */
  590. __HAL_UNLOCK(hadc);
  591. /* Return function status */
  592. return tmpHALStatus;
  593. }
  594. /**
  595. * @brief Stop ADC conversion of regular group (and injected channels in
  596. * case of auto_injection mode), disable ADC peripheral.
  597. * @note: ADC peripheral disable is forcing interruption of potential
  598. * conversion on injected group. If injected group is under use, it
  599. * should be preliminarily stopped using HAL_ADCEx_InjectedStop function.
  600. * @param hadc: ADC handle
  601. * @retval HAL status.
  602. */
  603. HAL_StatusTypeDef HAL_ADC_Stop(ADC_HandleTypeDef* hadc)
  604. {
  605. HAL_StatusTypeDef tmpHALStatus = HAL_OK;
  606. /* Check the parameters */
  607. assert_param(IS_ADC_ALL_INSTANCE(hadc->Instance));
  608. /* Process locked */
  609. __HAL_LOCK(hadc);
  610. /* Stop potential conversion on going, on regular and injected groups */
  611. /* Disable ADC peripheral */
  612. tmpHALStatus = ADC_ConversionStop_Disable(hadc);
  613. /* Check if ADC is effectively disabled */
  614. if (tmpHALStatus != HAL_ERROR)
  615. {
  616. /* Change ADC state */
  617. hadc->State = HAL_ADC_STATE_READY;
  618. }
  619. /* Process unlocked */
  620. __HAL_UNLOCK(hadc);
  621. /* Return function status */
  622. return tmpHALStatus;
  623. }
  624. /**
  625. * @brief Wait for regular group conversion to be completed.
  626. * @param hadc: ADC handle
  627. * @param Timeout: Timeout value in millisecond.
  628. * @retval HAL status
  629. */
  630. HAL_StatusTypeDef HAL_ADC_PollForConversion(ADC_HandleTypeDef* hadc, uint32_t Timeout)
  631. {
  632. uint32_t tickstart = 0;
  633. /* Check the parameters */
  634. assert_param(IS_ADC_ALL_INSTANCE(hadc->Instance));
  635. /* Get timeout */
  636. tickstart = HAL_GetTick();
  637. /* Wait until End of Conversion flag is raised */
  638. while(HAL_IS_BIT_CLR(hadc->Instance->SR, ADC_FLAG_EOC))
  639. {
  640. /* Check if timeout is disabled (set to infinite wait) */
  641. if(Timeout != HAL_MAX_DELAY)
  642. {
  643. if((Timeout == 0)||((HAL_GetTick() - tickstart ) > Timeout))
  644. {
  645. /* Update ADC state machine to timeout */
  646. hadc->State = HAL_ADC_STATE_TIMEOUT;
  647. /* Process unlocked */
  648. __HAL_UNLOCK(hadc);
  649. return HAL_ERROR;
  650. }
  651. }
  652. }
  653. /* Clear end of conversion flag of regular group if low power feature "Auto */
  654. /* Wait" is disabled, to not interfere with this feature until data */
  655. /* register is read using function HAL_ADC_GetValue(). */
  656. if (hadc->Init.LowPowerAutoWait == DISABLE)
  657. {
  658. /* Clear regular group conversion flag */
  659. __HAL_ADC_CLEAR_FLAG(hadc, ADC_FLAG_STRT | ADC_FLAG_EOC);
  660. }
  661. /* Update state machine on conversion status if not in error state */
  662. if(hadc->State != HAL_ADC_STATE_ERROR)
  663. {
  664. /* Update ADC state machine */
  665. if(hadc->State != HAL_ADC_STATE_EOC_INJ_REG)
  666. {
  667. /* Check if a conversion is ready on injected group */
  668. if(hadc->State == HAL_ADC_STATE_EOC_INJ)
  669. {
  670. /* Change ADC state */
  671. hadc->State = HAL_ADC_STATE_EOC_INJ_REG;
  672. }
  673. else
  674. {
  675. /* Change ADC state */
  676. hadc->State = HAL_ADC_STATE_EOC_REG;
  677. }
  678. }
  679. }
  680. /* Return ADC state */
  681. return HAL_OK;
  682. }
  683. /**
  684. * @brief Poll for conversion event.
  685. * @param hadc: ADC handle
  686. * @param EventType: the ADC event type.
  687. * This parameter can be one of the following values:
  688. * @arg AWD_EVENT: ADC Analog watchdog event.
  689. * @arg OVR_EVENT: ADC Overrun event
  690. * @param Timeout: Timeout value in millisecond.
  691. * @retval HAL status
  692. */
  693. HAL_StatusTypeDef HAL_ADC_PollForEvent(ADC_HandleTypeDef* hadc, uint32_t EventType, uint32_t Timeout)
  694. {
  695. uint32_t tickstart = 0;
  696. /* Check the parameters */
  697. assert_param(IS_ADC_ALL_INSTANCE(hadc->Instance));
  698. assert_param(IS_ADC_EVENT_TYPE(EventType));
  699. /* Get timeout */
  700. tickstart = HAL_GetTick();
  701. /* Check selected event flag */
  702. while(__HAL_ADC_GET_FLAG(hadc, EventType) == RESET)
  703. {
  704. /* Check if timeout is disabled (set to infinite wait) */
  705. if(Timeout != HAL_MAX_DELAY)
  706. {
  707. if((Timeout == 0)||((HAL_GetTick() - tickstart ) > Timeout))
  708. {
  709. /* Update ADC state machine to timeout */
  710. hadc->State = HAL_ADC_STATE_TIMEOUT;
  711. /* Process unlocked */
  712. __HAL_UNLOCK(hadc);
  713. return HAL_ERROR;
  714. }
  715. }
  716. }
  717. switch(EventType)
  718. {
  719. /* Analog watchdog (level out of window) event */
  720. case AWD_EVENT:
  721. /* Change ADC state */
  722. hadc->State = HAL_ADC_STATE_AWD;
  723. /* Clear ADC analog watchdog flag */
  724. __HAL_ADC_CLEAR_FLAG(hadc, ADC_FLAG_AWD);
  725. break;
  726. /* Overrun event */
  727. default: /* Case OVR_EVENT */
  728. /* Change ADC state */
  729. hadc->State = HAL_ADC_STATE_ERROR;
  730. /* Set ADC error code to overrun */
  731. hadc->ErrorCode |= HAL_ADC_ERROR_OVR;
  732. /* Clear ADC Overrun flag */
  733. __HAL_ADC_CLEAR_FLAG(hadc, ADC_FLAG_OVR);
  734. break;
  735. }
  736. /* Return ADC state */
  737. return HAL_OK;
  738. }
  739. /**
  740. * @brief Enables ADC, starts conversion of regular group with interruption.
  741. * Interruptions enabled in this function: EOC (end of conversion),
  742. * overrun.
  743. * Each of these interruptions has its dedicated callback function.
  744. * @param hadc: ADC handle
  745. * @retval HAL status
  746. */
  747. HAL_StatusTypeDef HAL_ADC_Start_IT(ADC_HandleTypeDef* hadc)
  748. {
  749. HAL_StatusTypeDef tmpHALStatus = HAL_OK;
  750. /* Check the parameters */
  751. assert_param(IS_ADC_ALL_INSTANCE(hadc->Instance));
  752. /* Process locked */
  753. __HAL_LOCK(hadc);
  754. /* Enable the ADC peripheral */
  755. tmpHALStatus = ADC_Enable(hadc);
  756. /* Start conversion if ADC is effectively enabled */
  757. if (tmpHALStatus != HAL_ERROR)
  758. {
  759. /* State machine update: Check if an injected conversion is ongoing */
  760. if(hadc->State == HAL_ADC_STATE_BUSY_INJ)
  761. {
  762. /* Change ADC state */
  763. hadc->State = HAL_ADC_STATE_BUSY_INJ_REG;
  764. }
  765. else
  766. {
  767. /* Change ADC state */
  768. hadc->State = HAL_ADC_STATE_BUSY_REG;
  769. }
  770. /* Set ADC error code to none */
  771. __HAL_ADC_CLEAR_ERRORCODE(hadc);
  772. /* Clear regular group conversion flag and overrun flag */
  773. /* (To ensure of no unknown state from potential previous ADC operations) */
  774. __HAL_ADC_CLEAR_FLAG(hadc, ADC_FLAG_EOC);
  775. /* Enable end of conversion interrupt for regular group */
  776. __HAL_ADC_ENABLE_IT(hadc, (ADC_IT_EOC | ADC_IT_OVR));
  777. /* Start conversion of regular group if software start has been selected. */
  778. /* If external trigger has been selected, conversion will start at next */
  779. /* trigger event. */
  780. if (__HAL_ADC_IS_SOFTWARE_START_REGULAR(hadc))
  781. {
  782. /* Start ADC conversion on regular group */
  783. SET_BIT(hadc->Instance->CR2, ADC_CR2_SWSTART);
  784. }
  785. }
  786. /* Process unlocked */
  787. __HAL_UNLOCK(hadc);
  788. /* Return function status */
  789. return tmpHALStatus;
  790. }
  791. /**
  792. * @brief Stop ADC conversion of regular group (and injected group in
  793. * case of auto_injection mode), disable interrution of
  794. * end-of-conversion, disable ADC peripheral.
  795. * @param hadc: ADC handle
  796. * @retval None
  797. */
  798. HAL_StatusTypeDef HAL_ADC_Stop_IT(ADC_HandleTypeDef* hadc)
  799. {
  800. HAL_StatusTypeDef tmpHALStatus = HAL_OK;
  801. /* Check the parameters */
  802. assert_param(IS_ADC_ALL_INSTANCE(hadc->Instance));
  803. /* Process locked */
  804. __HAL_LOCK(hadc);
  805. /* Stop potential conversion on going, on regular and injected groups */
  806. /* Disable ADC peripheral */
  807. tmpHALStatus = ADC_ConversionStop_Disable(hadc);
  808. /* Check if ADC is effectively disabled */
  809. if (tmpHALStatus != HAL_ERROR)
  810. {
  811. /* Disable ADC end of conversion interrupt for regular group */
  812. __HAL_ADC_DISABLE_IT(hadc, ADC_IT_EOC);
  813. /* Change ADC state */
  814. hadc->State = HAL_ADC_STATE_READY;
  815. }
  816. /* Process unlocked */
  817. __HAL_UNLOCK(hadc);
  818. /* Return function status */
  819. return tmpHALStatus;
  820. }
  821. /**
  822. * @brief Enables ADC, starts conversion of regular group and transfers result
  823. * through DMA.
  824. * Interruptions enabled in this function:
  825. * overrun, DMA half transfer, DMA transfer complete.
  826. * Each of these interruptions has its dedicated callback function.
  827. * @param hadc: ADC handle
  828. * @param pData: The destination Buffer address.
  829. * @param Length: The length of data to be transferred from ADC peripheral to memory.
  830. * @retval None
  831. */
  832. HAL_StatusTypeDef HAL_ADC_Start_DMA(ADC_HandleTypeDef* hadc, uint32_t* pData, uint32_t Length)
  833. {
  834. HAL_StatusTypeDef tmpHALStatus = HAL_OK;
  835. /* Check the parameters */
  836. assert_param(IS_ADC_ALL_INSTANCE(hadc->Instance));
  837. /* Process locked */
  838. __HAL_LOCK(hadc);
  839. /* Enable the ADC peripheral */
  840. tmpHALStatus = ADC_Enable(hadc);
  841. /* Start conversion if ADC is effectively enabled */
  842. if (tmpHALStatus != HAL_ERROR)
  843. {
  844. /* State machine update: Check if an injected conversion is ongoing */
  845. if(hadc->State == HAL_ADC_STATE_BUSY_INJ)
  846. {
  847. /* Change ADC state */
  848. hadc->State = HAL_ADC_STATE_BUSY_INJ_REG;
  849. }
  850. else
  851. {
  852. /* Change ADC state */
  853. hadc->State = HAL_ADC_STATE_BUSY_REG;
  854. }
  855. /* Set ADC error code to none */
  856. __HAL_ADC_CLEAR_ERRORCODE(hadc);
  857. /* Set the DMA transfer complete callback */
  858. hadc->DMA_Handle->XferCpltCallback = ADC_DMAConvCplt;
  859. /* Set the DMA half transfer complete callback */
  860. hadc->DMA_Handle->XferHalfCpltCallback = ADC_DMAHalfConvCplt;
  861. /* Set the DMA error callback */
  862. hadc->DMA_Handle->XferErrorCallback = ADC_DMAError;
  863. /* Manage ADC and DMA start: ADC overrun interruption, DMA start, ADC */
  864. /* start (in case of SW start): */
  865. /* Clear regular group conversion flag and overrun flag */
  866. /* (To ensure of no unknown state from potential previous ADC operations) */
  867. __HAL_ADC_CLEAR_FLAG(hadc, ADC_FLAG_EOC);
  868. /* Enable ADC overrun interrupt */
  869. __HAL_ADC_ENABLE_IT(hadc, ADC_IT_OVR);
  870. /* Enable ADC DMA mode */
  871. hadc->Instance->CR2 |= ADC_CR2_DMA;
  872. /* Start the DMA channel */
  873. HAL_DMA_Start_IT(hadc->DMA_Handle, (uint32_t)&hadc->Instance->DR, (uint32_t)pData, Length);
  874. /* Start conversion of regular group if software start has been selected. */
  875. /* If external trigger has been selected, conversion will start at next */
  876. /* trigger event. */
  877. /* Note: Alternate trigger for single conversion could be to force an */
  878. /* additional set of bit ADON "hadc->Instance->CR2 |= ADC_CR2_ADON;"*/
  879. if (__HAL_ADC_IS_SOFTWARE_START_REGULAR(hadc))
  880. {
  881. /* Start ADC conversion on regular group */
  882. SET_BIT(hadc->Instance->CR2, ADC_CR2_SWSTART);
  883. }
  884. }
  885. /* Process unlocked */
  886. __HAL_UNLOCK(hadc);
  887. /* Return function status */
  888. return tmpHALStatus;
  889. }
  890. /**
  891. * @brief Stop ADC conversion of regular group (and injected group in
  892. * case of auto_injection mode), disable ADC DMA transfer, disable
  893. * ADC peripheral.
  894. * @note: ADC peripheral disable is forcing interruption of potential
  895. * conversion on injected group. If injected group is under use, it
  896. * should be preliminarily stopped using HAL_ADCEx_InjectedStop function.
  897. * @param hadc: ADC handle
  898. * @retval HAL status.
  899. */
  900. HAL_StatusTypeDef HAL_ADC_Stop_DMA(ADC_HandleTypeDef* hadc)
  901. {
  902. HAL_StatusTypeDef tmpHALStatus = HAL_OK;
  903. /* Check the parameters */
  904. assert_param(IS_ADC_ALL_INSTANCE(hadc->Instance));
  905. /* Process locked */
  906. __HAL_LOCK(hadc);
  907. /* Stop potential conversion on going, on regular and injected groups */
  908. /* Disable ADC peripheral */
  909. tmpHALStatus = ADC_ConversionStop_Disable(hadc);
  910. /* Check if ADC is effectively disabled */
  911. if (tmpHALStatus != HAL_ERROR)
  912. {
  913. /* Disable ADC DMA mode */
  914. hadc->Instance->CR2 &= ~ADC_CR2_DMA;
  915. /* Disable the DMA channel (in case of DMA in circular mode or stop while */
  916. /* DMA transfer is on going) */
  917. tmpHALStatus = HAL_DMA_Abort(hadc->DMA_Handle);
  918. /* Check if DMA channel effectively disabled */
  919. if (tmpHALStatus != HAL_ERROR)
  920. {
  921. /* Change ADC state */
  922. hadc->State = HAL_ADC_STATE_READY;
  923. }
  924. else
  925. {
  926. /* Update ADC state machine to error */
  927. hadc->State = HAL_ADC_STATE_ERROR;
  928. }
  929. }
  930. /* Process unlocked */
  931. __HAL_UNLOCK(hadc);
  932. /* Return function status */
  933. return tmpHALStatus;
  934. }
  935. /**
  936. * @brief Get ADC regular group conversion result.
  937. * @param hadc: ADC handle
  938. * @retval Converted value
  939. */
  940. uint32_t HAL_ADC_GetValue(ADC_HandleTypeDef* hadc)
  941. {
  942. /* Check the parameters */
  943. assert_param(IS_ADC_ALL_INSTANCE(hadc->Instance));
  944. /* Note: EOC flag is not cleared here by software because automatically */
  945. /* cleared by hardware when reading register DR. */
  946. /* Return ADC converted value */
  947. return hadc->Instance->DR;
  948. }
  949. /**
  950. * @brief DMA transfer complete callback.
  951. * @param hdma: pointer to DMA handle.
  952. * @retval None
  953. */
  954. static void ADC_DMAConvCplt(DMA_HandleTypeDef *hdma)
  955. {
  956. /* Retrieve ADC handle corresponding to current DMA handle */
  957. ADC_HandleTypeDef* hadc = ( ADC_HandleTypeDef* )((DMA_HandleTypeDef* )hdma)->Parent;
  958. /* Update state machine on conversion status if not in error state */
  959. if(hadc->State != HAL_ADC_STATE_ERROR)
  960. {
  961. /* Update ADC state machine */
  962. if(hadc->State != HAL_ADC_STATE_EOC_INJ_REG)
  963. {
  964. /* Check if a conversion is ready on injected group */
  965. if(hadc->State == HAL_ADC_STATE_EOC_INJ)
  966. {
  967. /* Change ADC state */
  968. hadc->State = HAL_ADC_STATE_EOC_INJ_REG;
  969. }
  970. else
  971. {
  972. /* Change ADC state */
  973. hadc->State = HAL_ADC_STATE_EOC_REG;
  974. }
  975. }
  976. }
  977. /* Conversion complete callback */
  978. HAL_ADC_ConvCpltCallback(hadc);
  979. }
  980. /**
  981. * @brief DMA half transfer complete callback.
  982. * @param hdma: pointer to DMA handle.
  983. * @retval None
  984. */
  985. static void ADC_DMAHalfConvCplt(DMA_HandleTypeDef *hdma)
  986. {
  987. /* Retrieve ADC handle corresponding to current DMA handle */
  988. ADC_HandleTypeDef* hadc = ( ADC_HandleTypeDef* )((DMA_HandleTypeDef* )hdma)->Parent;
  989. /* Half conversion callback */
  990. HAL_ADC_ConvHalfCpltCallback(hadc);
  991. }
  992. /**
  993. * @brief DMA error callback
  994. * @param hdma: pointer to DMA handle.
  995. * @retval None
  996. */
  997. static void ADC_DMAError(DMA_HandleTypeDef *hdma)
  998. {
  999. /* Retrieve ADC handle corresponding to current DMA handle */
  1000. ADC_HandleTypeDef* hadc = ( ADC_HandleTypeDef* )((DMA_HandleTypeDef* )hdma)->Parent;
  1001. /* Change ADC state */
  1002. hadc->State = HAL_ADC_STATE_ERROR;
  1003. /* Set ADC error code to DMA error */
  1004. hadc->ErrorCode |= HAL_ADC_ERROR_DMA;
  1005. /* Error callback */
  1006. HAL_ADC_ErrorCallback(hadc);
  1007. }
  1008. /**
  1009. * @brief Handles ADC interrupt request
  1010. * @param hadc: ADC handle
  1011. * @retval None
  1012. */
  1013. void HAL_ADC_IRQHandler(ADC_HandleTypeDef* hadc)
  1014. {
  1015. /* Check the parameters */
  1016. assert_param(IS_ADC_ALL_INSTANCE(hadc->Instance));
  1017. assert_param(IS_FUNCTIONAL_STATE(hadc->Init.ContinuousConvMode));
  1018. assert_param(IS_ADC_REGULAR_NB_CONV(hadc->Init.NbrOfConversion));
  1019. /* ========== Check End of Conversion flag for regular group ========== */
  1020. if(__HAL_ADC_GET_IT_SOURCE(hadc, ADC_IT_EOC))
  1021. {
  1022. if(__HAL_ADC_GET_FLAG(hadc, ADC_FLAG_EOC) )
  1023. {
  1024. /* Update state machine on conversion status if not in error state */
  1025. if(hadc->State != HAL_ADC_STATE_ERROR)
  1026. {
  1027. /* Update ADC state machine */
  1028. if(hadc->State != HAL_ADC_STATE_EOC_INJ_REG)
  1029. {
  1030. /* Check if a conversion is ready on injected group */
  1031. if(hadc->State == HAL_ADC_STATE_EOC_INJ)
  1032. {
  1033. /* Change ADC state */
  1034. hadc->State = HAL_ADC_STATE_EOC_INJ_REG;
  1035. }
  1036. else
  1037. {
  1038. /* Change ADC state */
  1039. hadc->State = HAL_ADC_STATE_EOC_REG;
  1040. }
  1041. }
  1042. }
  1043. /* Disable interruption if no further conversion upcoming regular */
  1044. /* external trigger or by continuous mode */
  1045. if(__HAL_ADC_IS_SOFTWARE_START_REGULAR(hadc) &&
  1046. (hadc->Init.ContinuousConvMode == DISABLE) )
  1047. {
  1048. /* Disable ADC end of single conversion interrupt */
  1049. /* Note: Overrun interrupt was enabled with EOC interrupt in */
  1050. /* HAL_ADC_Start_IT(), but is not disabled here because can be used by */
  1051. /* overrun IRQ process below. */
  1052. __HAL_ADC_DISABLE_IT(hadc, ADC_IT_EOC);
  1053. }
  1054. /* Conversion complete callback */
  1055. HAL_ADC_ConvCpltCallback(hadc);
  1056. /* Clear regular group conversion flag */
  1057. __HAL_ADC_CLEAR_FLAG(hadc, ADC_FLAG_STRT | ADC_FLAG_EOC);
  1058. }
  1059. }
  1060. /* ========== Check End of Conversion flag for injected group ========== */
  1061. if(__HAL_ADC_GET_IT_SOURCE(hadc, ADC_IT_JEOC))
  1062. {
  1063. if(__HAL_ADC_GET_FLAG(hadc, ADC_FLAG_JEOC))
  1064. {
  1065. /* Update state machine on conversion status if not in error state */
  1066. if(hadc->State != HAL_ADC_STATE_ERROR)
  1067. {
  1068. /* Update ADC state machine */
  1069. if(hadc->State != HAL_ADC_STATE_EOC_INJ_REG)
  1070. {
  1071. if(hadc->State == HAL_ADC_STATE_EOC_REG)
  1072. {
  1073. /* Change ADC state */
  1074. hadc->State = HAL_ADC_STATE_EOC_INJ_REG;
  1075. }
  1076. else
  1077. {
  1078. /* Change ADC state */
  1079. hadc->State = HAL_ADC_STATE_EOC_INJ;
  1080. }
  1081. }
  1082. }
  1083. /* Disable interruption if no further conversion upcoming injected */
  1084. /* external trigger or by automatic injected conversion with regular */
  1085. /* group having no further conversion upcoming (same conditions as */
  1086. /* regular group interruption disabling above). */
  1087. if(__HAL_ADC_IS_SOFTWARE_START_INJECTED(hadc) ||
  1088. (HAL_IS_BIT_CLR(hadc->Instance->CR1, ADC_CR1_JAUTO) &&
  1089. (__HAL_ADC_IS_SOFTWARE_START_REGULAR(hadc) &&
  1090. (hadc->Init.ContinuousConvMode == DISABLE) ) ) )
  1091. {
  1092. /* Disable ADC end of single conversion interrupt */
  1093. __HAL_ADC_DISABLE_IT(hadc, ADC_IT_JEOC);
  1094. }
  1095. /* Conversion complete callback */
  1096. HAL_ADCEx_InjectedConvCpltCallback(hadc);
  1097. /* Clear injected group conversion flag */
  1098. __HAL_ADC_CLEAR_FLAG(hadc, (ADC_FLAG_JSTRT | ADC_FLAG_JEOC));
  1099. }
  1100. }
  1101. /* ========== Check Analog watchdog flags ========== */
  1102. if(__HAL_ADC_GET_IT_SOURCE(hadc, ADC_IT_AWD))
  1103. {
  1104. if(__HAL_ADC_GET_FLAG(hadc, ADC_FLAG_AWD))
  1105. {
  1106. /* Change ADC state */
  1107. hadc->State = HAL_ADC_STATE_AWD;
  1108. /* Clear the ADCx's Analog watchdog flag */
  1109. __HAL_ADC_CLEAR_FLAG(hadc,ADC_FLAG_AWD);
  1110. /* Level out of window callback */
  1111. HAL_ADC_LevelOutOfWindowCallback(hadc);
  1112. }
  1113. }
  1114. /* ========== Check Overrun flag ========== */
  1115. if(__HAL_ADC_GET_IT_SOURCE(hadc, ADC_IT_OVR))
  1116. {
  1117. if(__HAL_ADC_GET_FLAG(hadc, ADC_FLAG_OVR))
  1118. {
  1119. /* Change ADC state to error state */
  1120. hadc->State = HAL_ADC_STATE_ERROR;
  1121. /* Set ADC error code to overrun */
  1122. hadc->ErrorCode |= HAL_ADC_ERROR_OVR;
  1123. /* Error callback */
  1124. HAL_ADC_ErrorCallback(hadc);
  1125. /* Clear the Overrun flag */
  1126. __HAL_ADC_CLEAR_FLAG(hadc, ADC_FLAG_OVR);
  1127. }
  1128. }
  1129. }
  1130. /**
  1131. * @brief Conversion complete callback in non blocking mode
  1132. * @param hadc: ADC handle
  1133. * @retval None
  1134. */
  1135. __weak void HAL_ADC_ConvCpltCallback(ADC_HandleTypeDef* hadc)
  1136. {
  1137. /* NOTE : This function should not be modified. When the callback is needed,
  1138. function HAL_ADC_ConvCpltCallback must be implemented in the user file.
  1139. */
  1140. }
  1141. /**
  1142. * @brief Conversion DMA half-transfer callback in non blocking mode
  1143. * @param hadc: ADC handle
  1144. * @retval None
  1145. */
  1146. __weak void HAL_ADC_ConvHalfCpltCallback(ADC_HandleTypeDef* hadc)
  1147. {
  1148. /* NOTE : This function should not be modified. When the callback is needed,
  1149. function HAL_ADC_ConvHalfCpltCallback must be implemented in the user file.
  1150. */
  1151. }
  1152. /**
  1153. * @brief Analog watchdog callback in non blocking mode.
  1154. * @param hadc: ADC handle
  1155. * @retval None
  1156. */
  1157. __weak void HAL_ADC_LevelOutOfWindowCallback(ADC_HandleTypeDef* hadc)
  1158. {
  1159. /* NOTE : This function should not be modified. When the callback is needed,
  1160. function HAL_ADC_LevelOutOfWindowCallback must be implemented in the user file.
  1161. */
  1162. }
  1163. /**
  1164. * @brief ADC error callback in non blocking mode
  1165. * (ADC conversion with interruption or transfer by DMA)
  1166. * @param hadc: ADC handle
  1167. * @retval None
  1168. */
  1169. __weak void HAL_ADC_ErrorCallback(ADC_HandleTypeDef *hadc)
  1170. {
  1171. /* NOTE : This function should not be modified. When the callback is needed,
  1172. function HAL_ADC_ErrorCallback must be implemented in the user file.
  1173. */
  1174. }
  1175. /**
  1176. * @}
  1177. */
  1178. /** @defgroup ADC_Exported_Functions_Group3 Peripheral Control functions
  1179. * @brief Peripheral Control functions
  1180. *
  1181. @verbatim
  1182. ===============================================================================
  1183. ##### Peripheral Control functions #####
  1184. ===============================================================================
  1185. [..] This section provides functions allowing to:
  1186. (+) Configure channels on regular group
  1187. (+) Configure the analog watchdog
  1188. @endverbatim
  1189. * @{
  1190. */
  1191. /**
  1192. * @brief Configures the the selected channel to be linked to the regular
  1193. * group.
  1194. * @note In case of usage of internal measurement channels:
  1195. * Vbat/VrefInt/TempSensor.
  1196. * These internal paths can be be disabled using function
  1197. * HAL_ADC_DeInit().
  1198. * @note Possibility to update parameters on the fly:
  1199. * This function initializes channel into regular group, following
  1200. * calls to this function can be used to reconfigure some parameters
  1201. * of structure "ADC_ChannelConfTypeDef" on the fly, without reseting
  1202. * the ADC.
  1203. * The setting of these parameters is conditioned to ADC state.
  1204. * For parameters constraints, see comments of structure
  1205. * "ADC_ChannelConfTypeDef".
  1206. * @param hadc: ADC handle
  1207. * @param sConfig: Structure of ADC channel for regular group.
  1208. * @retval HAL status
  1209. */
  1210. HAL_StatusTypeDef HAL_ADC_ConfigChannel(ADC_HandleTypeDef* hadc, ADC_ChannelConfTypeDef* sConfig)
  1211. {
  1212. HAL_StatusTypeDef tmpHALStatus = HAL_OK;
  1213. /* Check the parameters */
  1214. assert_param(IS_ADC_ALL_INSTANCE(hadc->Instance));
  1215. assert_param(IS_ADC_CHANNEL(sConfig->Channel));
  1216. assert_param(IS_ADC_REGULAR_RANK(sConfig->Rank));
  1217. assert_param(IS_ADC_SAMPLE_TIME(sConfig->SamplingTime));
  1218. /* Process locked */
  1219. __HAL_LOCK(hadc);
  1220. /* Regular sequence configuration */
  1221. /* For Rank 1 to 6 */
  1222. if (sConfig->Rank < 7)
  1223. {
  1224. MODIFY_REG(hadc->Instance->SQR5,
  1225. __ADC_SQR5_RK(ADC_SQR5_SQ1, sConfig->Rank),
  1226. __ADC_SQR5_RK(sConfig->Channel, sConfig->Rank) );
  1227. }
  1228. /* For Rank 7 to 12 */
  1229. else if (sConfig->Rank < 13)
  1230. {
  1231. MODIFY_REG(hadc->Instance->SQR4,
  1232. __ADC_SQR4_RK(ADC_SQR4_SQ7, sConfig->Rank),
  1233. __ADC_SQR4_RK(sConfig->Channel, sConfig->Rank) );
  1234. }
  1235. /* For Rank 13 to 18 */
  1236. else if (sConfig->Rank < 19)
  1237. {
  1238. MODIFY_REG(hadc->Instance->SQR3,
  1239. __ADC_SQR3_RK(ADC_SQR3_SQ13, sConfig->Rank),
  1240. __ADC_SQR3_RK(sConfig->Channel, sConfig->Rank) );
  1241. }
  1242. /* For Rank 19 to 24 */
  1243. else if (sConfig->Rank < 25)
  1244. {
  1245. MODIFY_REG(hadc->Instance->SQR2,
  1246. __ADC_SQR2_RK(ADC_SQR2_SQ19, sConfig->Rank),
  1247. __ADC_SQR2_RK(sConfig->Channel, sConfig->Rank) );
  1248. }
  1249. /* For Rank 25 to 28 */
  1250. else
  1251. {
  1252. MODIFY_REG(hadc->Instance->SQR1,
  1253. __ADC_SQR1_RK(ADC_SQR1_SQ25, sConfig->Rank),
  1254. __ADC_SQR1_RK(sConfig->Channel, sConfig->Rank) );
  1255. }
  1256. /* Channel sampling time configuration */
  1257. /* For channels 0 to 9 */
  1258. if (sConfig->Channel < ADC_CHANNEL_10)
  1259. {
  1260. MODIFY_REG(hadc->Instance->SMPR3,
  1261. __ADC_SMPR3(ADC_SMPR3_SMP0, sConfig->Channel),
  1262. __ADC_SMPR3(sConfig->SamplingTime, sConfig->Channel) );
  1263. }
  1264. /* For channels 10 to 19 */
  1265. else if (sConfig->Channel < ADC_CHANNEL_20)
  1266. {
  1267. MODIFY_REG(hadc->Instance->SMPR2,
  1268. __ADC_SMPR2(ADC_SMPR2_SMP10, sConfig->Channel),
  1269. __ADC_SMPR2(sConfig->SamplingTime, sConfig->Channel) );
  1270. }
  1271. /* For channels 20 to 26 for devices Cat.1, Cat.2, Cat.3 */
  1272. /* For channels 20 to 29 for devices Cat4, Cat.5 */
  1273. else if (sConfig->Channel <= ADC_SMPR1_CHANNEL_MAX)
  1274. {
  1275. MODIFY_REG(hadc->Instance->SMPR1,
  1276. __ADC_SMPR1(ADC_SMPR1_SMP20, sConfig->Channel),
  1277. __ADC_SMPR1(sConfig->SamplingTime, sConfig->Channel) );
  1278. }
  1279. /* For channels 30 to 31 for devices Cat4, Cat.5 */
  1280. else
  1281. {
  1282. __ADC_SMPR0_CHANNEL_SET(hadc, sConfig->SamplingTime, sConfig->Channel);
  1283. }
  1284. /* If ADC1 Channel_16 or Channel_17 is selected, enable Temperature sensor */
  1285. /* and VREFINT measurement path. */
  1286. if ((sConfig->Channel == ADC_CHANNEL_TEMPSENSOR) ||
  1287. (sConfig->Channel == ADC_CHANNEL_VREFINT) )
  1288. {
  1289. SET_BIT(ADC->CCR, ADC_CCR_TSVREFE);
  1290. }
  1291. /* Process unlocked */
  1292. __HAL_UNLOCK(hadc);
  1293. /* Return function status */
  1294. return tmpHALStatus;
  1295. }
  1296. /**
  1297. * @brief Configures the analog watchdog.
  1298. * @param hadc: ADC handle
  1299. * @param AnalogWDGConfig: Structure of ADC analog watchdog configuration
  1300. * @retval HAL status
  1301. */
  1302. HAL_StatusTypeDef HAL_ADC_AnalogWDGConfig(ADC_HandleTypeDef* hadc, ADC_AnalogWDGConfTypeDef* AnalogWDGConfig)
  1303. {
  1304. /* Check the parameters */
  1305. assert_param(IS_ADC_ALL_INSTANCE(hadc->Instance));
  1306. assert_param(IS_ADC_ANALOG_WATCHDOG_MODE(AnalogWDGConfig->WatchdogMode));
  1307. assert_param(IS_FUNCTIONAL_STATE(AnalogWDGConfig->ITMode));
  1308. assert_param(IS_ADC_RANGE(AnalogWDGConfig->HighThreshold));
  1309. assert_param(IS_ADC_RANGE(AnalogWDGConfig->LowThreshold));
  1310. if((AnalogWDGConfig->WatchdogMode == ADC_ANALOGWATCHDOG_SINGLE_REG) ||
  1311. (AnalogWDGConfig->WatchdogMode == ADC_ANALOGWATCHDOG_SINGLE_INJEC) ||
  1312. (AnalogWDGConfig->WatchdogMode == ADC_ANALOGWATCHDOG_SINGLE_REGINJEC) )
  1313. {
  1314. assert_param(IS_ADC_CHANNEL(AnalogWDGConfig->Channel));
  1315. }
  1316. /* Process locked */
  1317. __HAL_LOCK(hadc);
  1318. /* Analog watchdog configuration */
  1319. /* Configure ADC Analog watchdog interrupt */
  1320. if(AnalogWDGConfig->ITMode == ENABLE)
  1321. {
  1322. /* Enable the ADC Analog watchdog interrupt */
  1323. __HAL_ADC_ENABLE_IT(hadc, ADC_IT_AWD);
  1324. }
  1325. else
  1326. {
  1327. /* Disable the ADC Analog watchdog interrupt */
  1328. __HAL_ADC_DISABLE_IT(hadc, ADC_IT_AWD);
  1329. }
  1330. /* Configuration of analog watchdog: */
  1331. /* - Set the analog watchdog enable mode: regular and/or injected groups, */
  1332. /* one or all channels. */
  1333. /* - Set the Analog watchdog channel (is not used if watchdog */
  1334. /* mode "all channels": ADC_CFGR_AWD1SGL=0). */
  1335. hadc->Instance->CR1 &= ~( ADC_CR1_AWDSGL |
  1336. ADC_CR1_JAWDEN |
  1337. ADC_CR1_AWDEN |
  1338. ADC_CR1_AWDCH );
  1339. hadc->Instance->CR1 |= ( AnalogWDGConfig->WatchdogMode |
  1340. AnalogWDGConfig->Channel );
  1341. /* Set the high threshold */
  1342. hadc->Instance->HTR = AnalogWDGConfig->HighThreshold;
  1343. /* Set the low threshold */
  1344. hadc->Instance->LTR = AnalogWDGConfig->LowThreshold;
  1345. /* Process unlocked */
  1346. __HAL_UNLOCK(hadc);
  1347. /* Return function status */
  1348. return HAL_OK;
  1349. }
  1350. /**
  1351. * @}
  1352. */
  1353. /** @defgroup ADC_Exported_Functions_Group4 Peripheral State functions
  1354. * @brief Peripheral State functions
  1355. *
  1356. @verbatim
  1357. ===============================================================================
  1358. ##### Peripheral State and Errors functions #####
  1359. ===============================================================================
  1360. [..]
  1361. This subsection provides functions to get in run-time the status of the
  1362. peripheral.
  1363. (+) Check the ADC state
  1364. (+) Check the ADC error code
  1365. @endverbatim
  1366. * @{
  1367. */
  1368. /**
  1369. * @brief return the ADC state
  1370. * @param hadc: ADC handle
  1371. * @retval HAL state
  1372. */
  1373. HAL_ADC_StateTypeDef HAL_ADC_GetState(ADC_HandleTypeDef* hadc)
  1374. {
  1375. /* Return ADC state */
  1376. return hadc->State;
  1377. }
  1378. /**
  1379. * @brief Return the ADC error code
  1380. * @param hadc: ADC handle
  1381. * @retval ADC Error Code
  1382. */
  1383. uint32_t HAL_ADC_GetError(ADC_HandleTypeDef *hadc)
  1384. {
  1385. return hadc->ErrorCode;
  1386. }
  1387. /**
  1388. * @}
  1389. */
  1390. /**
  1391. * @}
  1392. */
  1393. /** @defgroup ADC_Private_Functions ADC Private Functions
  1394. * @{
  1395. */
  1396. /**
  1397. * @brief Enable the selected ADC.
  1398. * @note Prerequisite condition to use this function: ADC must be disabled
  1399. * and voltage regulator must be enabled (done into HAL_ADC_Init()).
  1400. * @param hadc: ADC handle
  1401. * @retval HAL status.
  1402. */
  1403. HAL_StatusTypeDef ADC_Enable(ADC_HandleTypeDef* hadc)
  1404. {
  1405. uint32_t wait_loop_index = 0;
  1406. uint32_t tickstart = 0;
  1407. /* ADC enable and wait for ADC ready (in case of ADC is disabled or */
  1408. /* enabling phase not yet completed: flag ADC ready not yet set). */
  1409. /* Timeout implemented to not be stuck if ADC cannot be enabled (possible */
  1410. /* causes: ADC clock not running, ...). */
  1411. if (__HAL_ADC_IS_ENABLED(hadc) == RESET)
  1412. {
  1413. /* Enable the Peripheral */
  1414. __ADC_ENABLE(hadc);
  1415. /* Delay for ADC stabilization time. */
  1416. /* Delay fixed to worst case: maximum CPU frequency */
  1417. while(wait_loop_index < ADC_STAB_DELAY_CPU_CYCLES)
  1418. {
  1419. wait_loop_index++;
  1420. }
  1421. /* Get timeout */
  1422. tickstart = HAL_GetTick();
  1423. /* Wait for ADC effectively enabled */
  1424. while(__HAL_ADC_IS_ENABLED(hadc) == RESET)
  1425. {
  1426. if((HAL_GetTick() - tickstart ) > ADC_ENABLE_TIMEOUT)
  1427. {
  1428. /* Update ADC state machine to error */
  1429. hadc->State = HAL_ADC_STATE_ERROR;
  1430. /* Set ADC error code to ADC IP internal error */
  1431. hadc->ErrorCode |= HAL_ADC_ERROR_INTERNAL;
  1432. /* Process unlocked */
  1433. __HAL_UNLOCK(hadc);
  1434. return HAL_ERROR;
  1435. }
  1436. }
  1437. }
  1438. /* Return HAL status */
  1439. return HAL_OK;
  1440. }
  1441. /**
  1442. * @brief Stop ADC conversion and disable the selected ADC
  1443. * @note Prerequisite condition to use this function: ADC conversions must be
  1444. * stopped to disable the ADC.
  1445. * @param hadc: ADC handle
  1446. * @retval HAL status.
  1447. */
  1448. HAL_StatusTypeDef ADC_ConversionStop_Disable(ADC_HandleTypeDef* hadc)
  1449. {
  1450. uint32_t tickstart = 0;
  1451. /* Verification if ADC is not already disabled: */
  1452. if (__HAL_ADC_IS_ENABLED(hadc) != RESET)
  1453. {
  1454. /* Disable the ADC peripheral */
  1455. __ADC_DISABLE(hadc);
  1456. /* Get timeout */
  1457. tickstart = HAL_GetTick();
  1458. /* Wait for ADC effectively disabled */
  1459. while(__HAL_ADC_IS_ENABLED(hadc) != RESET)
  1460. {
  1461. if((HAL_GetTick() - tickstart ) > ADC_DISABLE_TIMEOUT)
  1462. {
  1463. /* Update ADC state machine to error */
  1464. hadc->State = HAL_ADC_STATE_ERROR;
  1465. /* Set ADC error code to ADC IP internal error */
  1466. hadc->ErrorCode |= HAL_ADC_ERROR_INTERNAL;
  1467. return HAL_ERROR;
  1468. }
  1469. }
  1470. }
  1471. /* Return HAL status */
  1472. return HAL_OK;
  1473. }
  1474. /**
  1475. * @}
  1476. */
  1477. #endif /* HAL_ADC_MODULE_ENABLED */
  1478. /**
  1479. * @}
  1480. */
  1481. /**
  1482. * @}
  1483. */
  1484. /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/