stm32f1xx_hal_adc_ex.c 50 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325
  1. /**
  2. ******************************************************************************
  3. * @file stm32f1xx_hal_adc_ex.c
  4. * @author MCD Application Team
  5. * @brief This file provides firmware functions to manage the following
  6. * functionalities of the Analog to Digital Convertor (ADC)
  7. * peripheral:
  8. * + Peripheral Control functions
  9. * Other functions (generic functions) are available in file
  10. * "stm32f1xx_hal_adc.c".
  11. *
  12. ******************************************************************************
  13. * @attention
  14. *
  15. * Copyright (c) 2016 STMicroelectronics.
  16. * All rights reserved.
  17. *
  18. * This software is licensed under terms that can be found in the LICENSE file
  19. * in the root directory of this software component.
  20. * If no LICENSE file comes with this software, it is provided AS-IS.
  21. *
  22. ******************************************************************************
  23. @verbatim
  24. [..]
  25. (@) Sections "ADC peripheral features" and "How to use this driver" are
  26. available in file of generic functions "stm32f1xx_hal_adc.c".
  27. [..]
  28. @endverbatim
  29. */
  30. /* Includes ------------------------------------------------------------------*/
  31. #include "stm32f1xx_hal.h"
  32. /** @addtogroup STM32F1xx_HAL_Driver
  33. * @{
  34. */
  35. /** @defgroup ADCEx ADCEx
  36. * @brief ADC Extension HAL module driver
  37. * @{
  38. */
  39. #ifdef HAL_ADC_MODULE_ENABLED
  40. /* Private typedef -----------------------------------------------------------*/
  41. /* Private define ------------------------------------------------------------*/
  42. /** @defgroup ADCEx_Private_Constants ADCEx Private Constants
  43. * @{
  44. */
  45. /* Delay for ADC calibration: */
  46. /* Hardware prerequisite before starting a calibration: the ADC must have */
  47. /* been in power-on state for at least two ADC clock cycles. */
  48. /* Unit: ADC clock cycles */
  49. #define ADC_PRECALIBRATION_DELAY_ADCCLOCKCYCLES 2U
  50. /* Timeout value for ADC calibration */
  51. /* Value defined to be higher than worst cases: low clocks freq, */
  52. /* maximum prescaler. */
  53. /* Ex of profile low frequency : Clock source at 0.1 MHz, ADC clock */
  54. /* prescaler 4, sampling time 12.5 ADC clock cycles, resolution 12 bits. */
  55. /* Unit: ms */
  56. #define ADC_CALIBRATION_TIMEOUT 10U
  57. /* Delay for temperature sensor stabilization time. */
  58. /* Maximum delay is 10us (refer to device datasheet, parameter tSTART). */
  59. /* Unit: us */
  60. #define ADC_TEMPSENSOR_DELAY_US 10U
  61. /**
  62. * @}
  63. */
  64. /* Private macro -------------------------------------------------------------*/
  65. /* Private variables ---------------------------------------------------------*/
  66. /* Private function prototypes -----------------------------------------------*/
  67. /* Private functions ---------------------------------------------------------*/
  68. /** @defgroup ADCEx_Exported_Functions ADCEx Exported Functions
  69. * @{
  70. */
  71. /** @defgroup ADCEx_Exported_Functions_Group1 Extended Extended IO operation functions
  72. * @brief Extended Extended Input and Output operation functions
  73. *
  74. @verbatim
  75. ===============================================================================
  76. ##### IO operation functions #####
  77. ===============================================================================
  78. [..] This section provides functions allowing to:
  79. (+) Start conversion of injected group.
  80. (+) Stop conversion of injected group.
  81. (+) Poll for conversion complete on injected group.
  82. (+) Get result of injected channel conversion.
  83. (+) Start conversion of injected group and enable interruptions.
  84. (+) Stop conversion of injected group and disable interruptions.
  85. (+) Start multimode and enable DMA transfer.
  86. (+) Stop multimode and disable ADC DMA transfer.
  87. (+) Get result of multimode conversion.
  88. (+) Perform the ADC self-calibration for single or differential ending.
  89. (+) Get calibration factors for single or differential ending.
  90. (+) Set calibration factors for single or differential ending.
  91. @endverbatim
  92. * @{
  93. */
  94. /**
  95. * @brief Perform an ADC automatic self-calibration
  96. * Calibration prerequisite: ADC must be disabled (execute this
  97. * function before HAL_ADC_Start() or after HAL_ADC_Stop() ).
  98. * During calibration process, ADC is enabled. ADC is let enabled at
  99. * the completion of this function.
  100. * @param hadc: ADC handle
  101. * @retval HAL status
  102. */
  103. HAL_StatusTypeDef HAL_ADCEx_Calibration_Start(ADC_HandleTypeDef* hadc)
  104. {
  105. HAL_StatusTypeDef tmp_hal_status = HAL_OK;
  106. uint32_t tickstart;
  107. __IO uint32_t wait_loop_index = 0U;
  108. /* Check the parameters */
  109. assert_param(IS_ADC_ALL_INSTANCE(hadc->Instance));
  110. /* Process locked */
  111. __HAL_LOCK(hadc);
  112. /* 1. Disable ADC peripheral */
  113. tmp_hal_status = ADC_ConversionStop_Disable(hadc);
  114. /* 2. Calibration prerequisite delay before starting the calibration. */
  115. /* - ADC must be enabled for at least two ADC clock cycles */
  116. tmp_hal_status = ADC_Enable(hadc);
  117. /* Check if ADC is effectively enabled */
  118. if (tmp_hal_status == HAL_OK)
  119. {
  120. /* Set ADC state */
  121. ADC_STATE_CLR_SET(hadc->State,
  122. HAL_ADC_STATE_REG_BUSY | HAL_ADC_STATE_INJ_BUSY,
  123. HAL_ADC_STATE_BUSY_INTERNAL);
  124. /* Hardware prerequisite: delay before starting the calibration. */
  125. /* - Computation of CPU clock cycles corresponding to ADC clock cycles. */
  126. /* - Wait for the expected ADC clock cycles delay */
  127. wait_loop_index = ((SystemCoreClock
  128. / HAL_RCCEx_GetPeriphCLKFreq(RCC_PERIPHCLK_ADC))
  129. * ADC_PRECALIBRATION_DELAY_ADCCLOCKCYCLES );
  130. while(wait_loop_index != 0U)
  131. {
  132. wait_loop_index--;
  133. }
  134. /* 3. Resets ADC calibration registers */
  135. SET_BIT(hadc->Instance->CR2, ADC_CR2_RSTCAL);
  136. tickstart = HAL_GetTick();
  137. /* Wait for calibration reset completion */
  138. while(HAL_IS_BIT_SET(hadc->Instance->CR2, ADC_CR2_RSTCAL))
  139. {
  140. if((HAL_GetTick() - tickstart) > ADC_CALIBRATION_TIMEOUT)
  141. {
  142. /* New check to avoid false timeout detection in case of preemption */
  143. if(HAL_IS_BIT_SET(hadc->Instance->CR2, ADC_CR2_RSTCAL))
  144. {
  145. /* Update ADC state machine to error */
  146. ADC_STATE_CLR_SET(hadc->State,
  147. HAL_ADC_STATE_BUSY_INTERNAL,
  148. HAL_ADC_STATE_ERROR_INTERNAL);
  149. /* Process unlocked */
  150. __HAL_UNLOCK(hadc);
  151. return HAL_ERROR;
  152. }
  153. }
  154. }
  155. /* 4. Start ADC calibration */
  156. SET_BIT(hadc->Instance->CR2, ADC_CR2_CAL);
  157. tickstart = HAL_GetTick();
  158. /* Wait for calibration completion */
  159. while(HAL_IS_BIT_SET(hadc->Instance->CR2, ADC_CR2_CAL))
  160. {
  161. if((HAL_GetTick() - tickstart) > ADC_CALIBRATION_TIMEOUT)
  162. {
  163. /* New check to avoid false timeout detection in case of preemption */
  164. if(HAL_IS_BIT_SET(hadc->Instance->CR2, ADC_CR2_CAL))
  165. {
  166. /* Update ADC state machine to error */
  167. ADC_STATE_CLR_SET(hadc->State,
  168. HAL_ADC_STATE_BUSY_INTERNAL,
  169. HAL_ADC_STATE_ERROR_INTERNAL);
  170. /* Process unlocked */
  171. __HAL_UNLOCK(hadc);
  172. return HAL_ERROR;
  173. }
  174. }
  175. }
  176. /* Set ADC state */
  177. ADC_STATE_CLR_SET(hadc->State,
  178. HAL_ADC_STATE_BUSY_INTERNAL,
  179. HAL_ADC_STATE_READY);
  180. }
  181. /* Process unlocked */
  182. __HAL_UNLOCK(hadc);
  183. /* Return function status */
  184. return tmp_hal_status;
  185. }
  186. /**
  187. * @brief Enables ADC, starts conversion of injected group.
  188. * Interruptions enabled in this function: None.
  189. * @param hadc: ADC handle
  190. * @retval HAL status
  191. */
  192. HAL_StatusTypeDef HAL_ADCEx_InjectedStart(ADC_HandleTypeDef* hadc)
  193. {
  194. HAL_StatusTypeDef tmp_hal_status = HAL_OK;
  195. /* Check the parameters */
  196. assert_param(IS_ADC_ALL_INSTANCE(hadc->Instance));
  197. /* Process locked */
  198. __HAL_LOCK(hadc);
  199. /* Enable the ADC peripheral */
  200. tmp_hal_status = ADC_Enable(hadc);
  201. /* Start conversion if ADC is effectively enabled */
  202. if (tmp_hal_status == HAL_OK)
  203. {
  204. /* Set ADC state */
  205. /* - Clear state bitfield related to injected group conversion results */
  206. /* - Set state bitfield related to injected operation */
  207. ADC_STATE_CLR_SET(hadc->State,
  208. HAL_ADC_STATE_READY | HAL_ADC_STATE_INJ_EOC,
  209. HAL_ADC_STATE_INJ_BUSY);
  210. /* Case of independent mode or multimode (for devices with several ADCs): */
  211. /* Set multimode state. */
  212. if (ADC_NONMULTIMODE_OR_MULTIMODEMASTER(hadc))
  213. {
  214. CLEAR_BIT(hadc->State, HAL_ADC_STATE_MULTIMODE_SLAVE);
  215. }
  216. else
  217. {
  218. SET_BIT(hadc->State, HAL_ADC_STATE_MULTIMODE_SLAVE);
  219. }
  220. /* Check if a regular conversion is ongoing */
  221. /* Note: On this device, there is no ADC error code fields related to */
  222. /* conversions on group injected only. In case of conversion on */
  223. /* going on group regular, no error code is reset. */
  224. if (HAL_IS_BIT_CLR(hadc->State, HAL_ADC_STATE_REG_BUSY))
  225. {
  226. /* Reset ADC all error code fields */
  227. ADC_CLEAR_ERRORCODE(hadc);
  228. }
  229. /* Process unlocked */
  230. /* Unlock before starting ADC conversions: in case of potential */
  231. /* interruption, to let the process to ADC IRQ Handler. */
  232. __HAL_UNLOCK(hadc);
  233. /* Clear injected group conversion flag */
  234. /* (To ensure of no unknown state from potential previous ADC operations) */
  235. __HAL_ADC_CLEAR_FLAG(hadc, ADC_FLAG_JEOC);
  236. /* Enable conversion of injected group. */
  237. /* If software start has been selected, conversion starts immediately. */
  238. /* If external trigger has been selected, conversion will start at next */
  239. /* trigger event. */
  240. /* If automatic injected conversion is enabled, conversion will start */
  241. /* after next regular group conversion. */
  242. /* Case of multimode enabled (for devices with several ADCs): if ADC is */
  243. /* slave, ADC is enabled only (conversion is not started). If ADC is */
  244. /* master, ADC is enabled and conversion is started. */
  245. if (HAL_IS_BIT_CLR(hadc->Instance->CR1, ADC_CR1_JAUTO))
  246. {
  247. if (ADC_IS_SOFTWARE_START_INJECTED(hadc) &&
  248. ADC_NONMULTIMODE_OR_MULTIMODEMASTER(hadc) )
  249. {
  250. /* Start ADC conversion on injected group with SW start */
  251. SET_BIT(hadc->Instance->CR2, (ADC_CR2_JSWSTART | ADC_CR2_JEXTTRIG));
  252. }
  253. else
  254. {
  255. /* Start ADC conversion on injected group with external trigger */
  256. SET_BIT(hadc->Instance->CR2, ADC_CR2_JEXTTRIG);
  257. }
  258. }
  259. }
  260. else
  261. {
  262. /* Process unlocked */
  263. __HAL_UNLOCK(hadc);
  264. }
  265. /* Return function status */
  266. return tmp_hal_status;
  267. }
  268. /**
  269. * @brief Stop conversion of injected channels. Disable ADC peripheral if
  270. * no regular conversion is on going.
  271. * @note If ADC must be disabled and if conversion is on going on
  272. * regular group, function HAL_ADC_Stop must be used to stop both
  273. * injected and regular groups, and disable the ADC.
  274. * @note If injected group mode auto-injection is enabled,
  275. * function HAL_ADC_Stop must be used.
  276. * @note In case of auto-injection mode, HAL_ADC_Stop must be used.
  277. * @param hadc: ADC handle
  278. * @retval None
  279. */
  280. HAL_StatusTypeDef HAL_ADCEx_InjectedStop(ADC_HandleTypeDef* hadc)
  281. {
  282. HAL_StatusTypeDef tmp_hal_status = HAL_OK;
  283. /* Check the parameters */
  284. assert_param(IS_ADC_ALL_INSTANCE(hadc->Instance));
  285. /* Process locked */
  286. __HAL_LOCK(hadc);
  287. /* Stop potential conversion and disable ADC peripheral */
  288. /* Conditioned to: */
  289. /* - No conversion on the other group (regular group) is intended to */
  290. /* continue (injected and regular groups stop conversion and ADC disable */
  291. /* are common) */
  292. /* - In case of auto-injection mode, HAL_ADC_Stop must be used. */
  293. if(((hadc->State & HAL_ADC_STATE_REG_BUSY) == RESET) &&
  294. HAL_IS_BIT_CLR(hadc->Instance->CR1, ADC_CR1_JAUTO) )
  295. {
  296. /* Stop potential conversion on going, on regular and injected groups */
  297. /* Disable ADC peripheral */
  298. tmp_hal_status = ADC_ConversionStop_Disable(hadc);
  299. /* Check if ADC is effectively disabled */
  300. if (tmp_hal_status == HAL_OK)
  301. {
  302. /* Set ADC state */
  303. ADC_STATE_CLR_SET(hadc->State,
  304. HAL_ADC_STATE_REG_BUSY | HAL_ADC_STATE_INJ_BUSY,
  305. HAL_ADC_STATE_READY);
  306. }
  307. }
  308. else
  309. {
  310. /* Update ADC state machine to error */
  311. SET_BIT(hadc->State, HAL_ADC_STATE_ERROR_CONFIG);
  312. tmp_hal_status = HAL_ERROR;
  313. }
  314. /* Process unlocked */
  315. __HAL_UNLOCK(hadc);
  316. /* Return function status */
  317. return tmp_hal_status;
  318. }
  319. /**
  320. * @brief Wait for injected group conversion to be completed.
  321. * @param hadc: ADC handle
  322. * @param Timeout: Timeout value in millisecond.
  323. * @retval HAL status
  324. */
  325. HAL_StatusTypeDef HAL_ADCEx_InjectedPollForConversion(ADC_HandleTypeDef* hadc, uint32_t Timeout)
  326. {
  327. uint32_t tickstart;
  328. /* Variables for polling in case of scan mode enabled and polling for each */
  329. /* conversion. */
  330. __IO uint32_t Conversion_Timeout_CPU_cycles = 0U;
  331. uint32_t Conversion_Timeout_CPU_cycles_max = 0U;
  332. /* Check the parameters */
  333. assert_param(IS_ADC_ALL_INSTANCE(hadc->Instance));
  334. /* Get timeout */
  335. tickstart = HAL_GetTick();
  336. /* Polling for end of conversion: differentiation if single/sequence */
  337. /* conversion. */
  338. /* For injected group, flag JEOC is set only at the end of the sequence, */
  339. /* not for each conversion within the sequence. */
  340. /* - If single conversion for injected group (scan mode disabled or */
  341. /* InjectedNbrOfConversion ==1), flag JEOC is used to determine the */
  342. /* conversion completion. */
  343. /* - If sequence conversion for injected group (scan mode enabled and */
  344. /* InjectedNbrOfConversion >=2), flag JEOC is set only at the end of the */
  345. /* sequence. */
  346. /* To poll for each conversion, the maximum conversion time is computed */
  347. /* from ADC conversion time (selected sampling time + conversion time of */
  348. /* 12.5 ADC clock cycles) and APB2/ADC clock prescalers (depending on */
  349. /* settings, conversion time range can be from 28 to 32256 CPU cycles). */
  350. /* As flag JEOC is not set after each conversion, no timeout status can */
  351. /* be set. */
  352. if ((hadc->Instance->JSQR & ADC_JSQR_JL) == RESET)
  353. {
  354. /* Wait until End of Conversion flag is raised */
  355. while(HAL_IS_BIT_CLR(hadc->Instance->SR, ADC_FLAG_JEOC))
  356. {
  357. /* Check if timeout is disabled (set to infinite wait) */
  358. if(Timeout != HAL_MAX_DELAY)
  359. {
  360. if((Timeout == 0U) || ((HAL_GetTick() - tickstart ) > Timeout))
  361. {
  362. /* New check to avoid false timeout detection in case of preemption */
  363. if(HAL_IS_BIT_CLR(hadc->Instance->SR, ADC_FLAG_JEOC))
  364. {
  365. /* Update ADC state machine to timeout */
  366. SET_BIT(hadc->State, HAL_ADC_STATE_TIMEOUT);
  367. /* Process unlocked */
  368. __HAL_UNLOCK(hadc);
  369. return HAL_TIMEOUT;
  370. }
  371. }
  372. }
  373. }
  374. }
  375. else
  376. {
  377. /* Replace polling by wait for maximum conversion time */
  378. /* - Computation of CPU clock cycles corresponding to ADC clock cycles */
  379. /* and ADC maximum conversion cycles on all channels. */
  380. /* - Wait for the expected ADC clock cycles delay */
  381. Conversion_Timeout_CPU_cycles_max = ((SystemCoreClock
  382. / HAL_RCCEx_GetPeriphCLKFreq(RCC_PERIPHCLK_ADC))
  383. * ADC_CONVCYCLES_MAX_RANGE(hadc) );
  384. while(Conversion_Timeout_CPU_cycles < Conversion_Timeout_CPU_cycles_max)
  385. {
  386. /* Check if timeout is disabled (set to infinite wait) */
  387. if(Timeout != HAL_MAX_DELAY)
  388. {
  389. if((Timeout == 0)||((HAL_GetTick() - tickstart ) > Timeout))
  390. {
  391. /* New check to avoid false timeout detection in case of preemption */
  392. if(Conversion_Timeout_CPU_cycles < Conversion_Timeout_CPU_cycles_max)
  393. {
  394. /* Update ADC state machine to timeout */
  395. SET_BIT(hadc->State, HAL_ADC_STATE_TIMEOUT);
  396. /* Process unlocked */
  397. __HAL_UNLOCK(hadc);
  398. return HAL_TIMEOUT;
  399. }
  400. }
  401. }
  402. Conversion_Timeout_CPU_cycles ++;
  403. }
  404. }
  405. /* Clear injected group conversion flag */
  406. /* Note: On STM32F1 ADC, clear regular conversion flag raised */
  407. /* simultaneously. */
  408. __HAL_ADC_CLEAR_FLAG(hadc, ADC_FLAG_JSTRT | ADC_FLAG_JEOC | ADC_FLAG_EOC);
  409. /* Update ADC state machine */
  410. SET_BIT(hadc->State, HAL_ADC_STATE_INJ_EOC);
  411. /* Determine whether any further conversion upcoming on group injected */
  412. /* by external trigger or by automatic injected conversion */
  413. /* from group regular. */
  414. if(ADC_IS_SOFTWARE_START_INJECTED(hadc) ||
  415. (HAL_IS_BIT_CLR(hadc->Instance->CR1, ADC_CR1_JAUTO) &&
  416. (ADC_IS_SOFTWARE_START_REGULAR(hadc) &&
  417. (hadc->Init.ContinuousConvMode == DISABLE) ) ) )
  418. {
  419. /* Set ADC state */
  420. CLEAR_BIT(hadc->State, HAL_ADC_STATE_INJ_BUSY);
  421. if (HAL_IS_BIT_CLR(hadc->State, HAL_ADC_STATE_REG_BUSY))
  422. {
  423. SET_BIT(hadc->State, HAL_ADC_STATE_READY);
  424. }
  425. }
  426. /* Return ADC state */
  427. return HAL_OK;
  428. }
  429. /**
  430. * @brief Enables ADC, starts conversion of injected group with interruption.
  431. * - JEOC (end of conversion of injected group)
  432. * Each of these interruptions has its dedicated callback function.
  433. * @param hadc: ADC handle
  434. * @retval HAL status.
  435. */
  436. HAL_StatusTypeDef HAL_ADCEx_InjectedStart_IT(ADC_HandleTypeDef* hadc)
  437. {
  438. HAL_StatusTypeDef tmp_hal_status = HAL_OK;
  439. /* Check the parameters */
  440. assert_param(IS_ADC_ALL_INSTANCE(hadc->Instance));
  441. /* Process locked */
  442. __HAL_LOCK(hadc);
  443. /* Enable the ADC peripheral */
  444. tmp_hal_status = ADC_Enable(hadc);
  445. /* Start conversion if ADC is effectively enabled */
  446. if (tmp_hal_status == HAL_OK)
  447. {
  448. /* Set ADC state */
  449. /* - Clear state bitfield related to injected group conversion results */
  450. /* - Set state bitfield related to injected operation */
  451. ADC_STATE_CLR_SET(hadc->State,
  452. HAL_ADC_STATE_READY | HAL_ADC_STATE_INJ_EOC,
  453. HAL_ADC_STATE_INJ_BUSY);
  454. /* Case of independent mode or multimode (for devices with several ADCs): */
  455. /* Set multimode state. */
  456. if (ADC_NONMULTIMODE_OR_MULTIMODEMASTER(hadc))
  457. {
  458. CLEAR_BIT(hadc->State, HAL_ADC_STATE_MULTIMODE_SLAVE);
  459. }
  460. else
  461. {
  462. SET_BIT(hadc->State, HAL_ADC_STATE_MULTIMODE_SLAVE);
  463. }
  464. /* Check if a regular conversion is ongoing */
  465. /* Note: On this device, there is no ADC error code fields related to */
  466. /* conversions on group injected only. In case of conversion on */
  467. /* going on group regular, no error code is reset. */
  468. if (HAL_IS_BIT_CLR(hadc->State, HAL_ADC_STATE_REG_BUSY))
  469. {
  470. /* Reset ADC all error code fields */
  471. ADC_CLEAR_ERRORCODE(hadc);
  472. }
  473. /* Process unlocked */
  474. /* Unlock before starting ADC conversions: in case of potential */
  475. /* interruption, to let the process to ADC IRQ Handler. */
  476. __HAL_UNLOCK(hadc);
  477. /* Clear injected group conversion flag */
  478. /* (To ensure of no unknown state from potential previous ADC operations) */
  479. __HAL_ADC_CLEAR_FLAG(hadc, ADC_FLAG_JEOC);
  480. /* Enable end of conversion interrupt for injected channels */
  481. __HAL_ADC_ENABLE_IT(hadc, ADC_IT_JEOC);
  482. /* Start conversion of injected group if software start has been selected */
  483. /* and if automatic injected conversion is disabled. */
  484. /* If external trigger has been selected, conversion will start at next */
  485. /* trigger event. */
  486. /* If automatic injected conversion is enabled, conversion will start */
  487. /* after next regular group conversion. */
  488. if (HAL_IS_BIT_CLR(hadc->Instance->CR1, ADC_CR1_JAUTO))
  489. {
  490. if (ADC_IS_SOFTWARE_START_INJECTED(hadc) &&
  491. ADC_NONMULTIMODE_OR_MULTIMODEMASTER(hadc) )
  492. {
  493. /* Start ADC conversion on injected group with SW start */
  494. SET_BIT(hadc->Instance->CR2, (ADC_CR2_JSWSTART | ADC_CR2_JEXTTRIG));
  495. }
  496. else
  497. {
  498. /* Start ADC conversion on injected group with external trigger */
  499. SET_BIT(hadc->Instance->CR2, ADC_CR2_JEXTTRIG);
  500. }
  501. }
  502. }
  503. else
  504. {
  505. /* Process unlocked */
  506. __HAL_UNLOCK(hadc);
  507. }
  508. /* Return function status */
  509. return tmp_hal_status;
  510. }
  511. /**
  512. * @brief Stop conversion of injected channels, disable interruption of
  513. * end-of-conversion. Disable ADC peripheral if no regular conversion
  514. * is on going.
  515. * @note If ADC must be disabled and if conversion is on going on
  516. * regular group, function HAL_ADC_Stop must be used to stop both
  517. * injected and regular groups, and disable the ADC.
  518. * @note If injected group mode auto-injection is enabled,
  519. * function HAL_ADC_Stop must be used.
  520. * @param hadc: ADC handle
  521. * @retval None
  522. */
  523. HAL_StatusTypeDef HAL_ADCEx_InjectedStop_IT(ADC_HandleTypeDef* hadc)
  524. {
  525. HAL_StatusTypeDef tmp_hal_status = HAL_OK;
  526. /* Check the parameters */
  527. assert_param(IS_ADC_ALL_INSTANCE(hadc->Instance));
  528. /* Process locked */
  529. __HAL_LOCK(hadc);
  530. /* Stop potential conversion and disable ADC peripheral */
  531. /* Conditioned to: */
  532. /* - No conversion on the other group (regular group) is intended to */
  533. /* continue (injected and regular groups stop conversion and ADC disable */
  534. /* are common) */
  535. /* - In case of auto-injection mode, HAL_ADC_Stop must be used. */
  536. if(((hadc->State & HAL_ADC_STATE_REG_BUSY) == RESET) &&
  537. HAL_IS_BIT_CLR(hadc->Instance->CR1, ADC_CR1_JAUTO) )
  538. {
  539. /* Stop potential conversion on going, on regular and injected groups */
  540. /* Disable ADC peripheral */
  541. tmp_hal_status = ADC_ConversionStop_Disable(hadc);
  542. /* Check if ADC is effectively disabled */
  543. if (tmp_hal_status == HAL_OK)
  544. {
  545. /* Disable ADC end of conversion interrupt for injected channels */
  546. __HAL_ADC_DISABLE_IT(hadc, ADC_IT_JEOC);
  547. /* Set ADC state */
  548. ADC_STATE_CLR_SET(hadc->State,
  549. HAL_ADC_STATE_REG_BUSY | HAL_ADC_STATE_INJ_BUSY,
  550. HAL_ADC_STATE_READY);
  551. }
  552. }
  553. else
  554. {
  555. /* Update ADC state machine to error */
  556. SET_BIT(hadc->State, HAL_ADC_STATE_ERROR_CONFIG);
  557. tmp_hal_status = HAL_ERROR;
  558. }
  559. /* Process unlocked */
  560. __HAL_UNLOCK(hadc);
  561. /* Return function status */
  562. return tmp_hal_status;
  563. }
  564. #if defined (STM32F103x6) || defined (STM32F103xB) || defined (STM32F105xC) || defined (STM32F107xC) || defined (STM32F103xE) || defined (STM32F103xG)
  565. /**
  566. * @brief Enables ADC, starts conversion of regular group and transfers result
  567. * through DMA.
  568. * Multimode must have been previously configured using
  569. * HAL_ADCEx_MultiModeConfigChannel() function.
  570. * Interruptions enabled in this function:
  571. * - DMA transfer complete
  572. * - DMA half transfer
  573. * Each of these interruptions has its dedicated callback function.
  574. * @note: On STM32F1 devices, ADC slave regular group must be configured
  575. * with conversion trigger ADC_SOFTWARE_START.
  576. * @note: ADC slave can be enabled preliminarily using single-mode
  577. * HAL_ADC_Start() function.
  578. * @param hadc: ADC handle of ADC master (handle of ADC slave must not be used)
  579. * @param pData: The destination Buffer address.
  580. * @param Length: The length of data to be transferred from ADC peripheral to memory.
  581. * @retval None
  582. */
  583. HAL_StatusTypeDef HAL_ADCEx_MultiModeStart_DMA(ADC_HandleTypeDef* hadc, uint32_t* pData, uint32_t Length)
  584. {
  585. HAL_StatusTypeDef tmp_hal_status = HAL_OK;
  586. ADC_HandleTypeDef tmphadcSlave={0};
  587. /* Check the parameters */
  588. assert_param(IS_ADC_MULTIMODE_MASTER_INSTANCE(hadc->Instance));
  589. assert_param(IS_FUNCTIONAL_STATE(hadc->Init.ContinuousConvMode));
  590. /* Process locked */
  591. __HAL_LOCK(hadc);
  592. /* Set a temporary handle of the ADC slave associated to the ADC master */
  593. ADC_MULTI_SLAVE(hadc, &tmphadcSlave);
  594. /* On STM32F1 devices, ADC slave regular group must be configured with */
  595. /* conversion trigger ADC_SOFTWARE_START. */
  596. /* Note: External trigger of ADC slave must be enabled, it is already done */
  597. /* into function "HAL_ADC_Init()". */
  598. if(!ADC_IS_SOFTWARE_START_REGULAR(&tmphadcSlave))
  599. {
  600. /* Update ADC state machine to error */
  601. SET_BIT(hadc->State, HAL_ADC_STATE_ERROR_CONFIG);
  602. /* Process unlocked */
  603. __HAL_UNLOCK(hadc);
  604. return HAL_ERROR;
  605. }
  606. /* Enable the ADC peripherals: master and slave (in case if not already */
  607. /* enabled previously) */
  608. tmp_hal_status = ADC_Enable(hadc);
  609. if (tmp_hal_status == HAL_OK)
  610. {
  611. tmp_hal_status = ADC_Enable(&tmphadcSlave);
  612. }
  613. /* Start conversion if all ADCs of multimode are effectively enabled */
  614. if (tmp_hal_status == HAL_OK)
  615. {
  616. /* Set ADC state (ADC master) */
  617. /* - Clear state bitfield related to regular group conversion results */
  618. /* - Set state bitfield related to regular operation */
  619. ADC_STATE_CLR_SET(hadc->State,
  620. HAL_ADC_STATE_READY | HAL_ADC_STATE_REG_EOC | HAL_ADC_STATE_MULTIMODE_SLAVE,
  621. HAL_ADC_STATE_REG_BUSY);
  622. /* If conversions on group regular are also triggering group injected, */
  623. /* update ADC state. */
  624. if (READ_BIT(hadc->Instance->CR1, ADC_CR1_JAUTO) != RESET)
  625. {
  626. ADC_STATE_CLR_SET(hadc->State, HAL_ADC_STATE_INJ_EOC, HAL_ADC_STATE_INJ_BUSY);
  627. }
  628. /* Process unlocked */
  629. /* Unlock before starting ADC conversions: in case of potential */
  630. /* interruption, to let the process to ADC IRQ Handler. */
  631. __HAL_UNLOCK(hadc);
  632. /* Set ADC error code to none */
  633. ADC_CLEAR_ERRORCODE(hadc);
  634. /* Set the DMA transfer complete callback */
  635. hadc->DMA_Handle->XferCpltCallback = ADC_DMAConvCplt;
  636. /* Set the DMA half transfer complete callback */
  637. hadc->DMA_Handle->XferHalfCpltCallback = ADC_DMAHalfConvCplt;
  638. /* Set the DMA error callback */
  639. hadc->DMA_Handle->XferErrorCallback = ADC_DMAError;
  640. /* Manage ADC and DMA start: ADC overrun interruption, DMA start, ADC */
  641. /* start (in case of SW start): */
  642. /* Clear regular group conversion flag and overrun flag */
  643. /* (To ensure of no unknown state from potential previous ADC operations) */
  644. __HAL_ADC_CLEAR_FLAG(hadc, ADC_FLAG_EOC);
  645. /* Enable ADC DMA mode of ADC master */
  646. SET_BIT(hadc->Instance->CR2, ADC_CR2_DMA);
  647. /* Start the DMA channel */
  648. HAL_DMA_Start_IT(hadc->DMA_Handle, (uint32_t)&hadc->Instance->DR, (uint32_t)pData, Length);
  649. /* Start conversion of regular group if software start has been selected. */
  650. /* If external trigger has been selected, conversion will start at next */
  651. /* trigger event. */
  652. /* Note: Alternate trigger for single conversion could be to force an */
  653. /* additional set of bit ADON "hadc->Instance->CR2 |= ADC_CR2_ADON;"*/
  654. if (ADC_IS_SOFTWARE_START_REGULAR(hadc))
  655. {
  656. /* Start ADC conversion on regular group with SW start */
  657. SET_BIT(hadc->Instance->CR2, (ADC_CR2_SWSTART | ADC_CR2_EXTTRIG));
  658. }
  659. else
  660. {
  661. /* Start ADC conversion on regular group with external trigger */
  662. SET_BIT(hadc->Instance->CR2, ADC_CR2_EXTTRIG);
  663. }
  664. }
  665. else
  666. {
  667. /* Process unlocked */
  668. __HAL_UNLOCK(hadc);
  669. }
  670. /* Return function status */
  671. return tmp_hal_status;
  672. }
  673. /**
  674. * @brief Stop ADC conversion of regular group (and injected channels in
  675. * case of auto_injection mode), disable ADC DMA transfer, disable
  676. * ADC peripheral.
  677. * @note Multimode is kept enabled after this function. To disable multimode
  678. * (set with HAL_ADCEx_MultiModeConfigChannel(), ADC must be
  679. * reinitialized using HAL_ADC_Init() or HAL_ADC_ReInit().
  680. * @note In case of DMA configured in circular mode, function
  681. * HAL_ADC_Stop_DMA must be called after this function with handle of
  682. * ADC slave, to properly disable the DMA channel.
  683. * @param hadc: ADC handle of ADC master (handle of ADC slave must not be used)
  684. * @retval None
  685. */
  686. HAL_StatusTypeDef HAL_ADCEx_MultiModeStop_DMA(ADC_HandleTypeDef* hadc)
  687. {
  688. HAL_StatusTypeDef tmp_hal_status = HAL_OK;
  689. ADC_HandleTypeDef tmphadcSlave={0};
  690. /* Check the parameters */
  691. assert_param(IS_ADC_MULTIMODE_MASTER_INSTANCE(hadc->Instance));
  692. /* Process locked */
  693. __HAL_LOCK(hadc);
  694. /* Stop potential conversion on going, on regular and injected groups */
  695. /* Disable ADC master peripheral */
  696. tmp_hal_status = ADC_ConversionStop_Disable(hadc);
  697. /* Check if ADC is effectively disabled */
  698. if(tmp_hal_status == HAL_OK)
  699. {
  700. /* Set a temporary handle of the ADC slave associated to the ADC master */
  701. ADC_MULTI_SLAVE(hadc, &tmphadcSlave);
  702. /* Disable ADC slave peripheral */
  703. tmp_hal_status = ADC_ConversionStop_Disable(&tmphadcSlave);
  704. /* Check if ADC is effectively disabled */
  705. if(tmp_hal_status != HAL_OK)
  706. {
  707. /* Update ADC state machine to error */
  708. SET_BIT(hadc->State, HAL_ADC_STATE_ERROR_INTERNAL);
  709. /* Process unlocked */
  710. __HAL_UNLOCK(hadc);
  711. return HAL_ERROR;
  712. }
  713. /* Disable ADC DMA mode */
  714. CLEAR_BIT(hadc->Instance->CR2, ADC_CR2_DMA);
  715. /* Reset configuration of ADC DMA continuous request for dual mode */
  716. CLEAR_BIT(hadc->Instance->CR1, ADC_CR1_DUALMOD);
  717. /* Disable the DMA channel (in case of DMA in circular mode or stop while */
  718. /* while DMA transfer is on going) */
  719. tmp_hal_status = HAL_DMA_Abort(hadc->DMA_Handle);
  720. /* Change ADC state (ADC master) */
  721. ADC_STATE_CLR_SET(hadc->State,
  722. HAL_ADC_STATE_REG_BUSY | HAL_ADC_STATE_INJ_BUSY,
  723. HAL_ADC_STATE_READY);
  724. }
  725. /* Process unlocked */
  726. __HAL_UNLOCK(hadc);
  727. /* Return function status */
  728. return tmp_hal_status;
  729. }
  730. #endif /* defined STM32F103x6 || defined STM32F103xB || defined STM32F105xC || defined STM32F107xC || defined STM32F103xE || defined STM32F103xG */
  731. /**
  732. * @brief Get ADC injected group conversion result.
  733. * @note Reading register JDRx automatically clears ADC flag JEOC
  734. * (ADC group injected end of unitary conversion).
  735. * @note This function does not clear ADC flag JEOS
  736. * (ADC group injected end of sequence conversion)
  737. * Occurrence of flag JEOS rising:
  738. * - If sequencer is composed of 1 rank, flag JEOS is equivalent
  739. * to flag JEOC.
  740. * - If sequencer is composed of several ranks, during the scan
  741. * sequence flag JEOC only is raised, at the end of the scan sequence
  742. * both flags JEOC and EOS are raised.
  743. * Flag JEOS must not be cleared by this function because
  744. * it would not be compliant with low power features
  745. * (feature low power auto-wait, not available on all STM32 families).
  746. * To clear this flag, either use function:
  747. * in programming model IT: @ref HAL_ADC_IRQHandler(), in programming
  748. * model polling: @ref HAL_ADCEx_InjectedPollForConversion()
  749. * or @ref __HAL_ADC_CLEAR_FLAG(&hadc, ADC_FLAG_JEOS).
  750. * @param hadc: ADC handle
  751. * @param InjectedRank: the converted ADC injected rank.
  752. * This parameter can be one of the following values:
  753. * @arg ADC_INJECTED_RANK_1: Injected Channel1 selected
  754. * @arg ADC_INJECTED_RANK_2: Injected Channel2 selected
  755. * @arg ADC_INJECTED_RANK_3: Injected Channel3 selected
  756. * @arg ADC_INJECTED_RANK_4: Injected Channel4 selected
  757. * @retval ADC group injected conversion data
  758. */
  759. uint32_t HAL_ADCEx_InjectedGetValue(ADC_HandleTypeDef* hadc, uint32_t InjectedRank)
  760. {
  761. uint32_t tmp_jdr = 0U;
  762. /* Check the parameters */
  763. assert_param(IS_ADC_ALL_INSTANCE(hadc->Instance));
  764. assert_param(IS_ADC_INJECTED_RANK(InjectedRank));
  765. /* Get ADC converted value */
  766. switch(InjectedRank)
  767. {
  768. case ADC_INJECTED_RANK_4:
  769. tmp_jdr = hadc->Instance->JDR4;
  770. break;
  771. case ADC_INJECTED_RANK_3:
  772. tmp_jdr = hadc->Instance->JDR3;
  773. break;
  774. case ADC_INJECTED_RANK_2:
  775. tmp_jdr = hadc->Instance->JDR2;
  776. break;
  777. case ADC_INJECTED_RANK_1:
  778. default:
  779. tmp_jdr = hadc->Instance->JDR1;
  780. break;
  781. }
  782. /* Return ADC converted value */
  783. return tmp_jdr;
  784. }
  785. #if defined (STM32F103x6) || defined (STM32F103xB) || defined (STM32F105xC) || defined (STM32F107xC) || defined (STM32F103xE) || defined (STM32F103xG)
  786. /**
  787. * @brief Returns the last ADC Master&Slave regular conversions results data
  788. * in the selected multi mode.
  789. * @param hadc: ADC handle of ADC master (handle of ADC slave must not be used)
  790. * @retval The converted data value.
  791. */
  792. uint32_t HAL_ADCEx_MultiModeGetValue(ADC_HandleTypeDef* hadc)
  793. {
  794. uint32_t tmpDR = 0U;
  795. /* Check the parameters */
  796. assert_param(IS_ADC_MULTIMODE_MASTER_INSTANCE(hadc->Instance));
  797. /* Check the parameters */
  798. assert_param(IS_ADC_ALL_INSTANCE(hadc->Instance));
  799. /* Note: EOC flag is not cleared here by software because automatically */
  800. /* cleared by hardware when reading register DR. */
  801. /* On STM32F1 devices, ADC1 data register DR contains ADC2 conversions */
  802. /* only if ADC1 DMA mode is enabled. */
  803. tmpDR = hadc->Instance->DR;
  804. if (HAL_IS_BIT_CLR(ADC1->CR2, ADC_CR2_DMA))
  805. {
  806. tmpDR |= (ADC2->DR << 16U);
  807. }
  808. /* Return ADC converted value */
  809. return tmpDR;
  810. }
  811. #endif /* defined STM32F103x6 || defined STM32F103xB || defined STM32F105xC || defined STM32F107xC || defined STM32F103xE || defined STM32F103xG */
  812. /**
  813. * @brief Injected conversion complete callback in non blocking mode
  814. * @param hadc: ADC handle
  815. * @retval None
  816. */
  817. __weak void HAL_ADCEx_InjectedConvCpltCallback(ADC_HandleTypeDef* hadc)
  818. {
  819. /* Prevent unused argument(s) compilation warning */
  820. UNUSED(hadc);
  821. /* NOTE : This function Should not be modified, when the callback is needed,
  822. the HAL_ADCEx_InjectedConvCpltCallback could be implemented in the user file
  823. */
  824. }
  825. /**
  826. * @}
  827. */
  828. /** @defgroup ADCEx_Exported_Functions_Group2 Extended Peripheral Control functions
  829. * @brief Extended Peripheral Control functions
  830. *
  831. @verbatim
  832. ===============================================================================
  833. ##### Peripheral Control functions #####
  834. ===============================================================================
  835. [..] This section provides functions allowing to:
  836. (+) Configure channels on injected group
  837. (+) Configure multimode
  838. @endverbatim
  839. * @{
  840. */
  841. /**
  842. * @brief Configures the ADC injected group and the selected channel to be
  843. * linked to the injected group.
  844. * @note Possibility to update parameters on the fly:
  845. * This function initializes injected group, following calls to this
  846. * function can be used to reconfigure some parameters of structure
  847. * "ADC_InjectionConfTypeDef" on the fly, without resetting the ADC.
  848. * The setting of these parameters is conditioned to ADC state:
  849. * this function must be called when ADC is not under conversion.
  850. * @param hadc: ADC handle
  851. * @param sConfigInjected: Structure of ADC injected group and ADC channel for
  852. * injected group.
  853. * @retval None
  854. */
  855. HAL_StatusTypeDef HAL_ADCEx_InjectedConfigChannel(ADC_HandleTypeDef* hadc, ADC_InjectionConfTypeDef* sConfigInjected)
  856. {
  857. HAL_StatusTypeDef tmp_hal_status = HAL_OK;
  858. __IO uint32_t wait_loop_index = 0U;
  859. /* Check the parameters */
  860. assert_param(IS_ADC_ALL_INSTANCE(hadc->Instance));
  861. assert_param(IS_ADC_CHANNEL(sConfigInjected->InjectedChannel));
  862. assert_param(IS_ADC_SAMPLE_TIME(sConfigInjected->InjectedSamplingTime));
  863. assert_param(IS_FUNCTIONAL_STATE(sConfigInjected->AutoInjectedConv));
  864. assert_param(IS_ADC_EXTTRIGINJEC(sConfigInjected->ExternalTrigInjecConv));
  865. assert_param(IS_ADC_RANGE(sConfigInjected->InjectedOffset));
  866. if(hadc->Init.ScanConvMode != ADC_SCAN_DISABLE)
  867. {
  868. assert_param(IS_ADC_INJECTED_RANK(sConfigInjected->InjectedRank));
  869. assert_param(IS_ADC_INJECTED_NB_CONV(sConfigInjected->InjectedNbrOfConversion));
  870. assert_param(IS_FUNCTIONAL_STATE(sConfigInjected->InjectedDiscontinuousConvMode));
  871. }
  872. /* Process locked */
  873. __HAL_LOCK(hadc);
  874. /* Configuration of injected group sequencer: */
  875. /* - if scan mode is disabled, injected channels sequence length is set to */
  876. /* 0x00: 1 channel converted (channel on regular rank 1) */
  877. /* Parameter "InjectedNbrOfConversion" is discarded. */
  878. /* Note: Scan mode is present by hardware on this device and, if */
  879. /* disabled, discards automatically nb of conversions. Anyway, nb of */
  880. /* conversions is forced to 0x00 for alignment over all STM32 devices. */
  881. /* - if scan mode is enabled, injected channels sequence length is set to */
  882. /* parameter "InjectedNbrOfConversion". */
  883. if (hadc->Init.ScanConvMode == ADC_SCAN_DISABLE)
  884. {
  885. if (sConfigInjected->InjectedRank == ADC_INJECTED_RANK_1)
  886. {
  887. /* Clear the old SQx bits for all injected ranks */
  888. MODIFY_REG(hadc->Instance->JSQR ,
  889. ADC_JSQR_JL |
  890. ADC_JSQR_JSQ4 |
  891. ADC_JSQR_JSQ3 |
  892. ADC_JSQR_JSQ2 |
  893. ADC_JSQR_JSQ1 ,
  894. ADC_JSQR_RK_JL(sConfigInjected->InjectedChannel,
  895. ADC_INJECTED_RANK_1,
  896. 0x01U));
  897. }
  898. /* If another injected rank than rank1 was intended to be set, and could */
  899. /* not due to ScanConvMode disabled, error is reported. */
  900. else
  901. {
  902. /* Update ADC state machine to error */
  903. SET_BIT(hadc->State, HAL_ADC_STATE_ERROR_CONFIG);
  904. tmp_hal_status = HAL_ERROR;
  905. }
  906. }
  907. else
  908. {
  909. /* Since injected channels rank conv. order depends on total number of */
  910. /* injected conversions, selected rank must be below or equal to total */
  911. /* number of injected conversions to be updated. */
  912. if (sConfigInjected->InjectedRank <= sConfigInjected->InjectedNbrOfConversion)
  913. {
  914. /* Clear the old SQx bits for the selected rank */
  915. /* Set the SQx bits for the selected rank */
  916. MODIFY_REG(hadc->Instance->JSQR ,
  917. ADC_JSQR_JL |
  918. ADC_JSQR_RK_JL(ADC_JSQR_JSQ1,
  919. sConfigInjected->InjectedRank,
  920. sConfigInjected->InjectedNbrOfConversion) ,
  921. ADC_JSQR_JL_SHIFT(sConfigInjected->InjectedNbrOfConversion) |
  922. ADC_JSQR_RK_JL(sConfigInjected->InjectedChannel,
  923. sConfigInjected->InjectedRank,
  924. sConfigInjected->InjectedNbrOfConversion) );
  925. }
  926. else
  927. {
  928. /* Clear the old SQx bits for the selected rank */
  929. MODIFY_REG(hadc->Instance->JSQR ,
  930. ADC_JSQR_JL |
  931. ADC_JSQR_RK_JL(ADC_JSQR_JSQ1,
  932. sConfigInjected->InjectedRank,
  933. sConfigInjected->InjectedNbrOfConversion) ,
  934. 0x00000000U);
  935. }
  936. }
  937. /* Configuration of injected group */
  938. /* Parameters update conditioned to ADC state: */
  939. /* Parameters that can be updated only when ADC is disabled: */
  940. /* - external trigger to start conversion */
  941. /* Parameters update not conditioned to ADC state: */
  942. /* - Automatic injected conversion */
  943. /* - Injected discontinuous mode */
  944. /* Note: In case of ADC already enabled, caution to not launch an unwanted */
  945. /* conversion while modifying register CR2 by writing 1 to bit ADON. */
  946. if (ADC_IS_ENABLE(hadc) == RESET)
  947. {
  948. MODIFY_REG(hadc->Instance->CR2 ,
  949. ADC_CR2_JEXTSEL |
  950. ADC_CR2_ADON ,
  951. ADC_CFGR_JEXTSEL(hadc, sConfigInjected->ExternalTrigInjecConv) );
  952. }
  953. /* Configuration of injected group */
  954. /* - Automatic injected conversion */
  955. /* - Injected discontinuous mode */
  956. /* Automatic injected conversion can be enabled if injected group */
  957. /* external triggers are disabled. */
  958. if (sConfigInjected->AutoInjectedConv == ENABLE)
  959. {
  960. if (sConfigInjected->ExternalTrigInjecConv == ADC_INJECTED_SOFTWARE_START)
  961. {
  962. SET_BIT(hadc->Instance->CR1, ADC_CR1_JAUTO);
  963. }
  964. else
  965. {
  966. /* Update ADC state machine to error */
  967. SET_BIT(hadc->State, HAL_ADC_STATE_ERROR_CONFIG);
  968. tmp_hal_status = HAL_ERROR;
  969. }
  970. }
  971. /* Injected discontinuous can be enabled only if auto-injected mode is */
  972. /* disabled. */
  973. if (sConfigInjected->InjectedDiscontinuousConvMode == ENABLE)
  974. {
  975. if (sConfigInjected->AutoInjectedConv == DISABLE)
  976. {
  977. SET_BIT(hadc->Instance->CR1, ADC_CR1_JDISCEN);
  978. }
  979. else
  980. {
  981. /* Update ADC state machine to error */
  982. SET_BIT(hadc->State, HAL_ADC_STATE_ERROR_CONFIG);
  983. tmp_hal_status = HAL_ERROR;
  984. }
  985. }
  986. /* InjectedChannel sampling time configuration */
  987. /* For channels 10 to 17 */
  988. if (sConfigInjected->InjectedChannel >= ADC_CHANNEL_10)
  989. {
  990. MODIFY_REG(hadc->Instance->SMPR1 ,
  991. ADC_SMPR1(ADC_SMPR1_SMP10, sConfigInjected->InjectedChannel) ,
  992. ADC_SMPR1(sConfigInjected->InjectedSamplingTime, sConfigInjected->InjectedChannel) );
  993. }
  994. else /* For channels 0 to 9 */
  995. {
  996. MODIFY_REG(hadc->Instance->SMPR2 ,
  997. ADC_SMPR2(ADC_SMPR2_SMP0, sConfigInjected->InjectedChannel) ,
  998. ADC_SMPR2(sConfigInjected->InjectedSamplingTime, sConfigInjected->InjectedChannel) );
  999. }
  1000. /* If ADC1 InjectedChannel_16 or InjectedChannel_17 is selected, enable Temperature sensor */
  1001. /* and VREFINT measurement path. */
  1002. if ((sConfigInjected->InjectedChannel == ADC_CHANNEL_TEMPSENSOR) ||
  1003. (sConfigInjected->InjectedChannel == ADC_CHANNEL_VREFINT) )
  1004. {
  1005. SET_BIT(hadc->Instance->CR2, ADC_CR2_TSVREFE);
  1006. }
  1007. /* Configure the offset: offset enable/disable, InjectedChannel, offset value */
  1008. switch(sConfigInjected->InjectedRank)
  1009. {
  1010. case 1:
  1011. /* Set injected channel 1 offset */
  1012. MODIFY_REG(hadc->Instance->JOFR1,
  1013. ADC_JOFR1_JOFFSET1,
  1014. sConfigInjected->InjectedOffset);
  1015. break;
  1016. case 2:
  1017. /* Set injected channel 2 offset */
  1018. MODIFY_REG(hadc->Instance->JOFR2,
  1019. ADC_JOFR2_JOFFSET2,
  1020. sConfigInjected->InjectedOffset);
  1021. break;
  1022. case 3:
  1023. /* Set injected channel 3 offset */
  1024. MODIFY_REG(hadc->Instance->JOFR3,
  1025. ADC_JOFR3_JOFFSET3,
  1026. sConfigInjected->InjectedOffset);
  1027. break;
  1028. case 4:
  1029. default:
  1030. MODIFY_REG(hadc->Instance->JOFR4,
  1031. ADC_JOFR4_JOFFSET4,
  1032. sConfigInjected->InjectedOffset);
  1033. break;
  1034. }
  1035. /* If ADC1 Channel_16 or Channel_17 is selected, enable Temperature sensor */
  1036. /* and VREFINT measurement path. */
  1037. if ((sConfigInjected->InjectedChannel == ADC_CHANNEL_TEMPSENSOR) ||
  1038. (sConfigInjected->InjectedChannel == ADC_CHANNEL_VREFINT) )
  1039. {
  1040. /* For STM32F1 devices with several ADC: Only ADC1 can access internal */
  1041. /* measurement channels (VrefInt/TempSensor). If these channels are */
  1042. /* intended to be set on other ADC instances, an error is reported. */
  1043. if (hadc->Instance == ADC1)
  1044. {
  1045. if (READ_BIT(hadc->Instance->CR2, ADC_CR2_TSVREFE) == RESET)
  1046. {
  1047. SET_BIT(hadc->Instance->CR2, ADC_CR2_TSVREFE);
  1048. if ((sConfigInjected->InjectedChannel == ADC_CHANNEL_TEMPSENSOR))
  1049. {
  1050. /* Delay for temperature sensor stabilization time */
  1051. /* Compute number of CPU cycles to wait for */
  1052. wait_loop_index = (ADC_TEMPSENSOR_DELAY_US * (SystemCoreClock / 1000000U));
  1053. while(wait_loop_index != 0U)
  1054. {
  1055. wait_loop_index--;
  1056. }
  1057. }
  1058. }
  1059. }
  1060. else
  1061. {
  1062. /* Update ADC state machine to error */
  1063. SET_BIT(hadc->State, HAL_ADC_STATE_ERROR_CONFIG);
  1064. tmp_hal_status = HAL_ERROR;
  1065. }
  1066. }
  1067. /* Process unlocked */
  1068. __HAL_UNLOCK(hadc);
  1069. /* Return function status */
  1070. return tmp_hal_status;
  1071. }
  1072. #if defined (STM32F103x6) || defined (STM32F103xB) || defined (STM32F105xC) || defined (STM32F107xC) || defined (STM32F103xE) || defined (STM32F103xG)
  1073. /**
  1074. * @brief Enable ADC multimode and configure multimode parameters
  1075. * @note Possibility to update parameters on the fly:
  1076. * This function initializes multimode parameters, following
  1077. * calls to this function can be used to reconfigure some parameters
  1078. * of structure "ADC_MultiModeTypeDef" on the fly, without resetting
  1079. * the ADCs (both ADCs of the common group).
  1080. * The setting of these parameters is conditioned to ADC state.
  1081. * For parameters constraints, see comments of structure
  1082. * "ADC_MultiModeTypeDef".
  1083. * @note To change back configuration from multimode to single mode, ADC must
  1084. * be reset (using function HAL_ADC_Init() ).
  1085. * @param hadc: ADC handle
  1086. * @param multimode: Structure of ADC multimode configuration
  1087. * @retval HAL status
  1088. */
  1089. HAL_StatusTypeDef HAL_ADCEx_MultiModeConfigChannel(ADC_HandleTypeDef* hadc, ADC_MultiModeTypeDef* multimode)
  1090. {
  1091. HAL_StatusTypeDef tmp_hal_status = HAL_OK;
  1092. ADC_HandleTypeDef tmphadcSlave={0};
  1093. /* Check the parameters */
  1094. assert_param(IS_ADC_MULTIMODE_MASTER_INSTANCE(hadc->Instance));
  1095. assert_param(IS_ADC_MODE(multimode->Mode));
  1096. /* Process locked */
  1097. __HAL_LOCK(hadc);
  1098. /* Set a temporary handle of the ADC slave associated to the ADC master */
  1099. ADC_MULTI_SLAVE(hadc, &tmphadcSlave);
  1100. /* Parameters update conditioned to ADC state: */
  1101. /* Parameters that can be updated when ADC is disabled or enabled without */
  1102. /* conversion on going on regular group: */
  1103. /* - ADC master and ADC slave DMA configuration */
  1104. /* Parameters that can be updated only when ADC is disabled: */
  1105. /* - Multimode mode selection */
  1106. /* To optimize code, all multimode settings can be set when both ADCs of */
  1107. /* the common group are in state: disabled. */
  1108. if ((ADC_IS_ENABLE(hadc) == RESET) &&
  1109. (ADC_IS_ENABLE(&tmphadcSlave) == RESET) &&
  1110. (IS_ADC_MULTIMODE_MASTER_INSTANCE(hadc->Instance)) )
  1111. {
  1112. MODIFY_REG(hadc->Instance->CR1,
  1113. ADC_CR1_DUALMOD ,
  1114. multimode->Mode );
  1115. }
  1116. /* If one of the ADC sharing the same common group is enabled, no update */
  1117. /* could be done on neither of the multimode structure parameters. */
  1118. else
  1119. {
  1120. /* Update ADC state machine to error */
  1121. SET_BIT(hadc->State, HAL_ADC_STATE_ERROR_CONFIG);
  1122. tmp_hal_status = HAL_ERROR;
  1123. }
  1124. /* Process unlocked */
  1125. __HAL_UNLOCK(hadc);
  1126. /* Return function status */
  1127. return tmp_hal_status;
  1128. }
  1129. #endif /* defined STM32F103x6 || defined STM32F103xB || defined STM32F105xC || defined STM32F107xC || defined STM32F103xE || defined STM32F103xG */
  1130. /**
  1131. * @}
  1132. */
  1133. /**
  1134. * @}
  1135. */
  1136. #endif /* HAL_ADC_MODULE_ENABLED */
  1137. /**
  1138. * @}
  1139. */
  1140. /**
  1141. * @}
  1142. */