stm32l1xx_hal_adc_ex.c 35 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884
  1. /**
  2. ******************************************************************************
  3. * @file stm32l1xx_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. * + Operation functions
  9. * ++ Start, stop, get result of conversions of injected
  10. * group, using 2 possible modes: polling, interruption.
  11. * ++ Calibration (ADC automatic self-calibration)
  12. * + Control functions
  13. * ++ Channels configuration on injected group
  14. * Other functions (generic functions) are available in file
  15. * "stm32l1xx_hal_adc.c".
  16. *
  17. @verbatim
  18. [..]
  19. (@) Sections "ADC peripheral features" and "How to use this driver" are
  20. available in file of generic functions "stm32l1xx_hal_adc.c".
  21. [..]
  22. @endverbatim
  23. ******************************************************************************
  24. * @attention
  25. *
  26. * <h2><center>&copy; COPYRIGHT(c) 2017 STMicroelectronics</center></h2>
  27. *
  28. * Redistribution and use in source and binary forms, with or without modification,
  29. * are permitted provided that the following conditions are met:
  30. * 1. Redistributions of source code must retain the above copyright notice,
  31. * this list of conditions and the following disclaimer.
  32. * 2. Redistributions in binary form must reproduce the above copyright notice,
  33. * this list of conditions and the following disclaimer in the documentation
  34. * and/or other materials provided with the distribution.
  35. * 3. Neither the name of STMicroelectronics nor the names of its contributors
  36. * may be used to endorse or promote products derived from this software
  37. * without specific prior written permission.
  38. *
  39. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  40. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  41. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  42. * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
  43. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  44. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  45. * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  46. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  47. * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  48. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  49. *
  50. ******************************************************************************
  51. */
  52. /* Includes ------------------------------------------------------------------*/
  53. #include "stm32l1xx_hal.h"
  54. /** @addtogroup STM32L1xx_HAL_Driver
  55. * @{
  56. */
  57. /** @defgroup ADCEx ADCEx
  58. * @brief ADC Extension HAL module driver
  59. * @{
  60. */
  61. #ifdef HAL_ADC_MODULE_ENABLED
  62. /* Private typedef -----------------------------------------------------------*/
  63. /* Private define ------------------------------------------------------------*/
  64. /** @defgroup ADCEx_Private_Constants ADCEx Private Constants
  65. * @{
  66. */
  67. /* ADC conversion cycles (unit: ADC clock cycles) */
  68. /* (selected sampling time + conversion time of 12 ADC clock cycles, with */
  69. /* resolution 12 bits) */
  70. #define ADC_CONVERSIONCLOCKCYCLES_SAMPLETIME_4CYCLE5 ( 16U)
  71. #define ADC_CONVERSIONCLOCKCYCLES_SAMPLETIME_9CYCLES ( 21U)
  72. #define ADC_CONVERSIONCLOCKCYCLES_SAMPLETIME_16CYCLES ( 28U)
  73. #define ADC_CONVERSIONCLOCKCYCLES_SAMPLETIME_24CYCLES ( 36U)
  74. #define ADC_CONVERSIONCLOCKCYCLES_SAMPLETIME_48CYCLES ( 60U)
  75. #define ADC_CONVERSIONCLOCKCYCLES_SAMPLETIME_96CYCLES (108U)
  76. #define ADC_CONVERSIONCLOCKCYCLES_SAMPLETIME_192CYCLES (204U)
  77. #define ADC_CONVERSIONCLOCKCYCLES_SAMPLETIME_384CYCLES (396U)
  78. /* Delay for temperature sensor stabilization time. */
  79. /* Maximum delay is 10us (refer to device datasheet, parameter tSTART). */
  80. /* Unit: us */
  81. #define ADC_TEMPSENSOR_DELAY_US (10U)
  82. /**
  83. * @}
  84. */
  85. /* Private macro -------------------------------------------------------------*/
  86. /* Private variables ---------------------------------------------------------*/
  87. /* Private function prototypes -----------------------------------------------*/
  88. /* Private functions ---------------------------------------------------------*/
  89. /** @defgroup ADCEx_Exported_Functions ADCEx Exported Functions
  90. * @{
  91. */
  92. /** @defgroup ADCEx_Exported_Functions_Group1 ADC Extended IO operation functions
  93. * @brief ADC Extended Input and Output operation functions
  94. *
  95. @verbatim
  96. ===============================================================================
  97. ##### IO operation functions #####
  98. ===============================================================================
  99. [..] This section provides functions allowing to:
  100. (+) Start conversion of injected group.
  101. (+) Stop conversion of injected group.
  102. (+) Poll for conversion complete on injected group.
  103. (+) Get result of injected channel conversion.
  104. (+) Start conversion of injected group and enable interruptions.
  105. (+) Stop conversion of injected group and disable interruptions.
  106. @endverbatim
  107. * @{
  108. */
  109. /**
  110. * @brief Enables ADC, starts conversion of injected group.
  111. * Interruptions enabled in this function: None.
  112. * @param hadc: ADC handle
  113. * @retval HAL status
  114. */
  115. HAL_StatusTypeDef HAL_ADCEx_InjectedStart(ADC_HandleTypeDef* hadc)
  116. {
  117. HAL_StatusTypeDef tmp_hal_status = HAL_OK;
  118. /* Check the parameters */
  119. assert_param(IS_ADC_ALL_INSTANCE(hadc->Instance));
  120. /* Process locked */
  121. __HAL_LOCK(hadc);
  122. /* Enable the ADC peripheral */
  123. tmp_hal_status = ADC_Enable(hadc);
  124. /* Start conversion if ADC is effectively enabled */
  125. if (tmp_hal_status == HAL_OK)
  126. {
  127. /* Set ADC state */
  128. /* - Clear state bitfield related to injected group conversion results */
  129. /* - Set state bitfield related to injected operation */
  130. ADC_STATE_CLR_SET(hadc->State,
  131. HAL_ADC_STATE_READY | HAL_ADC_STATE_INJ_EOC,
  132. HAL_ADC_STATE_INJ_BUSY);
  133. /* Check if a regular conversion is ongoing */
  134. /* Note: On this device, there is no ADC error code fields related to */
  135. /* conversions on group injected only. In case of conversion on */
  136. /* going on group regular, no error code is reset. */
  137. if (HAL_IS_BIT_CLR(hadc->State, HAL_ADC_STATE_REG_BUSY))
  138. {
  139. /* Reset ADC all error code fields */
  140. ADC_CLEAR_ERRORCODE(hadc);
  141. }
  142. /* Process unlocked */
  143. /* Unlock before starting ADC conversions: in case of potential */
  144. /* interruption, to let the process to ADC IRQ Handler. */
  145. __HAL_UNLOCK(hadc);
  146. /* Clear injected group conversion flag */
  147. /* (To ensure of no unknown state from potential previous ADC operations) */
  148. __HAL_ADC_CLEAR_FLAG(hadc, ADC_FLAG_JEOC);
  149. /* Enable conversion of injected group. */
  150. /* If software start has been selected, conversion starts immediately. */
  151. /* If external trigger has been selected, conversion will start at next */
  152. /* trigger event. */
  153. /* If automatic injected conversion is enabled, conversion will start */
  154. /* after next regular group conversion. */
  155. if (ADC_IS_SOFTWARE_START_INJECTED(hadc) &&
  156. HAL_IS_BIT_CLR(hadc->Instance->CR1, ADC_CR1_JAUTO) )
  157. {
  158. /* Enable ADC software conversion for injected channels */
  159. SET_BIT(hadc->Instance->CR2, ADC_CR2_JSWSTART);
  160. }
  161. }
  162. /* Return function status */
  163. return tmp_hal_status;
  164. }
  165. /**
  166. * @brief Stop conversion of injected channels. Disable ADC peripheral if
  167. * no regular conversion is on going.
  168. * @note If ADC must be disabled and if conversion is on going on
  169. * regular group, function HAL_ADC_Stop must be used to stop both
  170. * injected and regular groups, and disable the ADC.
  171. * @note If injected group mode auto-injection is enabled,
  172. * function HAL_ADC_Stop must be used.
  173. * @note In case of auto-injection mode, HAL_ADC_Stop must be used.
  174. * @param hadc: ADC handle
  175. * @retval None
  176. */
  177. HAL_StatusTypeDef HAL_ADCEx_InjectedStop(ADC_HandleTypeDef* hadc)
  178. {
  179. HAL_StatusTypeDef tmp_hal_status = HAL_OK;
  180. /* Check the parameters */
  181. assert_param(IS_ADC_ALL_INSTANCE(hadc->Instance));
  182. /* Process locked */
  183. __HAL_LOCK(hadc);
  184. /* Stop potential conversion and disable ADC peripheral */
  185. /* Conditioned to: */
  186. /* - No conversion on the other group (regular group) is intended to */
  187. /* continue (injected and regular groups stop conversion and ADC disable */
  188. /* are common) */
  189. /* - In case of auto-injection mode, HAL_ADC_Stop must be used. */
  190. if(((hadc->State & HAL_ADC_STATE_REG_BUSY) == RESET) &&
  191. HAL_IS_BIT_CLR(hadc->Instance->CR1, ADC_CR1_JAUTO) )
  192. {
  193. /* Stop potential conversion on going, on regular and injected groups */
  194. /* Disable ADC peripheral */
  195. tmp_hal_status = ADC_ConversionStop_Disable(hadc);
  196. /* Check if ADC is effectively disabled */
  197. if (tmp_hal_status == HAL_OK)
  198. {
  199. /* Set ADC state */
  200. ADC_STATE_CLR_SET(hadc->State,
  201. HAL_ADC_STATE_REG_BUSY | HAL_ADC_STATE_INJ_BUSY,
  202. HAL_ADC_STATE_READY);
  203. }
  204. }
  205. else
  206. {
  207. /* Update ADC state machine to error */
  208. SET_BIT(hadc->State, HAL_ADC_STATE_ERROR_CONFIG);
  209. tmp_hal_status = HAL_ERROR;
  210. }
  211. /* Process unlocked */
  212. __HAL_UNLOCK(hadc);
  213. /* Return function status */
  214. return tmp_hal_status;
  215. }
  216. /**
  217. * @brief Wait for injected group conversion to be completed.
  218. * @param hadc: ADC handle
  219. * @param Timeout: Timeout value in millisecond.
  220. * @retval HAL status
  221. */
  222. HAL_StatusTypeDef HAL_ADCEx_InjectedPollForConversion(ADC_HandleTypeDef* hadc, uint32_t Timeout)
  223. {
  224. uint32_t tickstart;
  225. /* Variables for polling in case of scan mode enabled and polling for each */
  226. /* conversion. */
  227. /* Note: Variable "conversion_timeout_cpu_cycles" set to offset 28 CPU */
  228. /* cycles to compensate number of CPU cycles for processing of variable */
  229. /* "conversion_timeout_cpu_cycles_max" */
  230. uint32_t conversion_timeout_cpu_cycles = 28;
  231. uint32_t conversion_timeout_cpu_cycles_max = 0;
  232. /* Check the parameters */
  233. assert_param(IS_ADC_ALL_INSTANCE(hadc->Instance));
  234. /* Get timeout */
  235. tickstart = HAL_GetTick();
  236. /* Polling for end of conversion: differentiation if single/sequence */
  237. /* conversion. */
  238. /* For injected group, flag JEOC is set only at the end of the sequence, */
  239. /* not for each conversion within the sequence. */
  240. /* If setting "EOCSelection" is set to poll for each single conversion, */
  241. /* management of polling depends on setting of injected group sequencer: */
  242. /* - If single conversion for injected group (scan mode disabled or */
  243. /* InjectedNbrOfConversion ==1), flag JEOC is used to determine the */
  244. /* conversion completion. */
  245. /* - If sequence conversion for injected group (scan mode enabled and */
  246. /* InjectedNbrOfConversion >=2), flag JEOC is set only at the end of the */
  247. /* sequence. */
  248. /* To poll for each conversion, the maximum conversion time is computed */
  249. /* from ADC conversion time (selected sampling time + conversion time of */
  250. /* 12 ADC clock cycles) and APB2/ADC clock prescalers (depending on */
  251. /* settings, conversion time range can vary from 8 to several thousands */
  252. /* of CPU cycles). */
  253. /* Note: On STM32L1, setting "EOCSelection" is related to regular group */
  254. /* only, by hardware. For compatibility with other STM32 devices, */
  255. /* this setting is related also to injected group by software. */
  256. if (((hadc->Instance->JSQR & ADC_JSQR_JL) == RESET) ||
  257. (hadc->Init.EOCSelection != ADC_EOC_SINGLE_CONV) )
  258. {
  259. /* Wait until End of Conversion flag is raised */
  260. while(HAL_IS_BIT_CLR(hadc->Instance->SR, ADC_FLAG_JEOC))
  261. {
  262. /* Check if timeout is disabled (set to infinite wait) */
  263. if(Timeout != HAL_MAX_DELAY)
  264. {
  265. if((Timeout == 0) || ((HAL_GetTick() - tickstart ) > Timeout))
  266. {
  267. /* Update ADC state machine to timeout */
  268. SET_BIT(hadc->State, HAL_ADC_STATE_TIMEOUT);
  269. /* Process unlocked */
  270. __HAL_UNLOCK(hadc);
  271. return HAL_TIMEOUT;
  272. }
  273. }
  274. }
  275. }
  276. else
  277. {
  278. /* Computation of CPU cycles corresponding to ADC conversion cycles. */
  279. /* Retrieve ADC clock prescaler and ADC maximum conversion cycles on all */
  280. /* channels. */
  281. conversion_timeout_cpu_cycles_max = ADC_GET_CLOCK_PRESCALER_DECIMAL(hadc);
  282. conversion_timeout_cpu_cycles_max *= ADC_CONVCYCLES_MAX_RANGE(hadc);
  283. /* Poll with maximum conversion time */
  284. while(conversion_timeout_cpu_cycles < conversion_timeout_cpu_cycles_max)
  285. {
  286. /* Check if timeout is disabled (set to infinite wait) */
  287. if(Timeout != HAL_MAX_DELAY)
  288. {
  289. if((Timeout == 0) || ((HAL_GetTick() - tickstart ) > Timeout))
  290. {
  291. /* Update ADC state machine to timeout */
  292. SET_BIT(hadc->State, HAL_ADC_STATE_TIMEOUT);
  293. /* Process unlocked */
  294. __HAL_UNLOCK(hadc);
  295. return HAL_TIMEOUT;
  296. }
  297. }
  298. conversion_timeout_cpu_cycles ++;
  299. }
  300. }
  301. /* Clear end of conversion flag of injected group if low power feature */
  302. /* "Auto Wait" is disabled, to not interfere with this feature until data */
  303. /* register is read using function HAL_ADCEx_InjectedGetValue(). */
  304. if (hadc->Init.LowPowerAutoWait == DISABLE)
  305. {
  306. /* Clear injected group conversion flag */
  307. __HAL_ADC_CLEAR_FLAG(hadc, ADC_FLAG_JSTRT | ADC_FLAG_JEOC);
  308. }
  309. /* Update ADC state machine */
  310. SET_BIT(hadc->State, HAL_ADC_STATE_INJ_EOC);
  311. /* Determine whether any further conversion upcoming on group injected */
  312. /* by external trigger, continuous mode or scan sequence on going. */
  313. /* Note: On STM32L1, there is no independent flag of end of sequence. */
  314. /* The test of scan sequence on going is done either with scan */
  315. /* sequence disabled or with end of conversion flag set to */
  316. /* of end of sequence. */
  317. if(ADC_IS_SOFTWARE_START_INJECTED(hadc) &&
  318. (HAL_IS_BIT_CLR(hadc->Instance->JSQR, ADC_JSQR_JL) ||
  319. HAL_IS_BIT_CLR(hadc->Instance->CR2, ADC_CR2_EOCS) ) &&
  320. (HAL_IS_BIT_CLR(hadc->Instance->CR1, ADC_CR1_JAUTO) &&
  321. (ADC_IS_SOFTWARE_START_REGULAR(hadc) &&
  322. (hadc->Init.ContinuousConvMode == DISABLE) ) ) )
  323. {
  324. /* Set ADC state */
  325. CLEAR_BIT(hadc->State, HAL_ADC_STATE_INJ_BUSY);
  326. if (HAL_IS_BIT_CLR(hadc->State, HAL_ADC_STATE_REG_BUSY))
  327. {
  328. SET_BIT(hadc->State, HAL_ADC_STATE_READY);
  329. }
  330. }
  331. /* Return ADC state */
  332. return HAL_OK;
  333. }
  334. /**
  335. * @brief Enables ADC, starts conversion of injected group with interruption.
  336. * - JEOC (end of conversion of injected group)
  337. * Each of these interruptions has its dedicated callback function.
  338. * @param hadc: ADC handle
  339. * @retval HAL status.
  340. */
  341. HAL_StatusTypeDef HAL_ADCEx_InjectedStart_IT(ADC_HandleTypeDef* hadc)
  342. {
  343. HAL_StatusTypeDef tmp_hal_status = HAL_OK;
  344. /* Check the parameters */
  345. assert_param(IS_ADC_ALL_INSTANCE(hadc->Instance));
  346. /* Process locked */
  347. __HAL_LOCK(hadc);
  348. /* Enable the ADC peripheral */
  349. tmp_hal_status = ADC_Enable(hadc);
  350. /* Start conversion if ADC is effectively enabled */
  351. if (tmp_hal_status == HAL_OK)
  352. {
  353. /* Set ADC state */
  354. /* - Clear state bitfield related to injected group conversion results */
  355. /* - Set state bitfield related to injected operation */
  356. ADC_STATE_CLR_SET(hadc->State,
  357. HAL_ADC_STATE_READY | HAL_ADC_STATE_INJ_EOC,
  358. HAL_ADC_STATE_INJ_BUSY);
  359. /* Check if a regular conversion is ongoing */
  360. /* Note: On this device, there is no ADC error code fields related to */
  361. /* conversions on group injected only. In case of conversion on */
  362. /* going on group regular, no error code is reset. */
  363. if (HAL_IS_BIT_CLR(hadc->State, HAL_ADC_STATE_REG_BUSY))
  364. {
  365. /* Reset ADC all error code fields */
  366. ADC_CLEAR_ERRORCODE(hadc);
  367. }
  368. /* Process unlocked */
  369. /* Unlock before starting ADC conversions: in case of potential */
  370. /* interruption, to let the process to ADC IRQ Handler. */
  371. __HAL_UNLOCK(hadc);
  372. /* Clear injected group conversion flag */
  373. /* (To ensure of no unknown state from potential previous ADC operations) */
  374. __HAL_ADC_CLEAR_FLAG(hadc, ADC_FLAG_JEOC);
  375. /* Enable end of conversion interrupt for injected channels */
  376. __HAL_ADC_ENABLE_IT(hadc, ADC_IT_JEOC);
  377. /* Enable conversion of injected group. */
  378. /* If software start has been selected, conversion starts immediately. */
  379. /* If external trigger has been selected, conversion will start at next */
  380. /* trigger event. */
  381. /* If automatic injected conversion is enabled, conversion will start */
  382. /* after next regular group conversion. */
  383. if (ADC_IS_SOFTWARE_START_INJECTED(hadc) &&
  384. HAL_IS_BIT_CLR(hadc->Instance->CR1, ADC_CR1_JAUTO) )
  385. {
  386. /* Enable ADC software conversion for injected channels */
  387. SET_BIT(hadc->Instance->CR2, ADC_CR2_JSWSTART);
  388. }
  389. }
  390. /* Return function status */
  391. return tmp_hal_status;
  392. }
  393. /**
  394. * @brief Stop conversion of injected channels, disable interruption of
  395. * end-of-conversion. Disable ADC peripheral if no regular conversion
  396. * is on going.
  397. * @note If ADC must be disabled and if conversion is on going on
  398. * regular group, function HAL_ADC_Stop must be used to stop both
  399. * injected and regular groups, and disable the ADC.
  400. * @note If injected group mode auto-injection is enabled,
  401. * function HAL_ADC_Stop must be used.
  402. * @param hadc: ADC handle
  403. * @retval None
  404. */
  405. HAL_StatusTypeDef HAL_ADCEx_InjectedStop_IT(ADC_HandleTypeDef* hadc)
  406. {
  407. HAL_StatusTypeDef tmp_hal_status = HAL_OK;
  408. /* Check the parameters */
  409. assert_param(IS_ADC_ALL_INSTANCE(hadc->Instance));
  410. /* Process locked */
  411. __HAL_LOCK(hadc);
  412. /* Stop potential conversion and disable ADC peripheral */
  413. /* Conditioned to: */
  414. /* - No conversion on the other group (regular group) is intended to */
  415. /* continue (injected and regular groups stop conversion and ADC disable */
  416. /* are common) */
  417. /* - In case of auto-injection mode, HAL_ADC_Stop must be used. */
  418. if(((hadc->State & HAL_ADC_STATE_REG_BUSY) == RESET) &&
  419. HAL_IS_BIT_CLR(hadc->Instance->CR1, ADC_CR1_JAUTO) )
  420. {
  421. /* Stop potential conversion on going, on regular and injected groups */
  422. /* Disable ADC peripheral */
  423. tmp_hal_status = ADC_ConversionStop_Disable(hadc);
  424. /* Check if ADC is effectively disabled */
  425. if (tmp_hal_status == HAL_OK)
  426. {
  427. /* Disable ADC end of conversion interrupt for injected channels */
  428. __HAL_ADC_DISABLE_IT(hadc, ADC_IT_JEOC);
  429. /* Set ADC state */
  430. ADC_STATE_CLR_SET(hadc->State,
  431. HAL_ADC_STATE_REG_BUSY | HAL_ADC_STATE_INJ_BUSY,
  432. HAL_ADC_STATE_READY);
  433. }
  434. }
  435. else
  436. {
  437. /* Update ADC state machine to error */
  438. SET_BIT(hadc->State, HAL_ADC_STATE_ERROR_CONFIG);
  439. tmp_hal_status = HAL_ERROR;
  440. }
  441. /* Process unlocked */
  442. __HAL_UNLOCK(hadc);
  443. /* Return function status */
  444. return tmp_hal_status;
  445. }
  446. /**
  447. * @brief Get ADC injected group conversion result.
  448. * @note Reading register JDRx automatically clears ADC flag JEOC
  449. * (ADC group injected end of unitary conversion).
  450. * @note This function does not clear ADC flag JEOS
  451. * (ADC group injected end of sequence conversion)
  452. * Occurrence of flag JEOS rising:
  453. * - If sequencer is composed of 1 rank, flag JEOS is equivalent
  454. * to flag JEOC.
  455. * - If sequencer is composed of several ranks, during the scan
  456. * sequence flag JEOC only is raised, at the end of the scan sequence
  457. * both flags JEOC and EOS are raised.
  458. * Flag JEOS must not be cleared by this function because
  459. * it would not be compliant with low power features
  460. * (feature low power auto-wait, not available on all STM32 families).
  461. * To clear this flag, either use function:
  462. * in programming model IT: @ref HAL_ADC_IRQHandler(), in programming
  463. * model polling: @ref HAL_ADCEx_InjectedPollForConversion()
  464. * or @ref __HAL_ADC_CLEAR_FLAG(&hadc, ADC_FLAG_JEOS).
  465. * @param hadc: ADC handle
  466. * @param InjectedRank: the converted ADC injected rank.
  467. * This parameter can be one of the following values:
  468. * @arg ADC_INJECTED_RANK_1: Injected Channel1 selected
  469. * @arg ADC_INJECTED_RANK_2: Injected Channel2 selected
  470. * @arg ADC_INJECTED_RANK_3: Injected Channel3 selected
  471. * @arg ADC_INJECTED_RANK_4: Injected Channel4 selected
  472. * @retval ADC group injected conversion data
  473. */
  474. uint32_t HAL_ADCEx_InjectedGetValue(ADC_HandleTypeDef* hadc, uint32_t InjectedRank)
  475. {
  476. uint32_t tmp_jdr = 0;
  477. /* Check the parameters */
  478. assert_param(IS_ADC_ALL_INSTANCE(hadc->Instance));
  479. assert_param(IS_ADC_INJECTED_RANK(InjectedRank));
  480. /* Get ADC converted value */
  481. switch(InjectedRank)
  482. {
  483. case ADC_INJECTED_RANK_4:
  484. tmp_jdr = hadc->Instance->JDR4;
  485. break;
  486. case ADC_INJECTED_RANK_3:
  487. tmp_jdr = hadc->Instance->JDR3;
  488. break;
  489. case ADC_INJECTED_RANK_2:
  490. tmp_jdr = hadc->Instance->JDR2;
  491. break;
  492. case ADC_INJECTED_RANK_1:
  493. default:
  494. tmp_jdr = hadc->Instance->JDR1;
  495. break;
  496. }
  497. /* Return ADC converted value */
  498. return tmp_jdr;
  499. }
  500. /**
  501. * @brief Injected conversion complete callback in non blocking mode
  502. * @param hadc: ADC handle
  503. * @retval None
  504. */
  505. __weak void HAL_ADCEx_InjectedConvCpltCallback(ADC_HandleTypeDef* hadc)
  506. {
  507. /* Prevent unused argument(s) compilation warning */
  508. UNUSED(hadc);
  509. /* NOTE : This function Should not be modified, when the callback is needed,
  510. the HAL_ADCEx_InjectedConvCpltCallback could be implemented in the user file
  511. */
  512. }
  513. /**
  514. * @}
  515. */
  516. /** @defgroup ADCEx_Exported_Functions_Group2 ADC Extended Peripheral Control functions
  517. * @brief ADC Extended Peripheral Control functions
  518. *
  519. @verbatim
  520. ===============================================================================
  521. ##### Peripheral Control functions #####
  522. ===============================================================================
  523. [..] This section provides functions allowing to:
  524. (+) Configure channels on injected group
  525. @endverbatim
  526. * @{
  527. */
  528. /**
  529. * @brief Configures the ADC injected group and the selected channel to be
  530. * linked to the injected group.
  531. * @note Possibility to update parameters on the fly:
  532. * This function initializes injected group, following calls to this
  533. * function can be used to reconfigure some parameters of structure
  534. * "ADC_InjectionConfTypeDef" on the fly, without reseting the ADC.
  535. * The setting of these parameters is conditioned to ADC state:
  536. * this function must be called when ADC is not under conversion.
  537. * @param hadc: ADC handle
  538. * @param sConfigInjected: Structure of ADC injected group and ADC channel for
  539. * injected group.
  540. * @retval None
  541. */
  542. HAL_StatusTypeDef HAL_ADCEx_InjectedConfigChannel(ADC_HandleTypeDef* hadc, ADC_InjectionConfTypeDef* sConfigInjected)
  543. {
  544. HAL_StatusTypeDef tmp_hal_status = HAL_OK;
  545. __IO uint32_t wait_loop_index = 0;
  546. /* Check the parameters */
  547. assert_param(IS_ADC_ALL_INSTANCE(hadc->Instance));
  548. assert_param(IS_ADC_CHANNEL(sConfigInjected->InjectedChannel));
  549. assert_param(IS_ADC_SAMPLE_TIME(sConfigInjected->InjectedSamplingTime));
  550. assert_param(IS_FUNCTIONAL_STATE(sConfigInjected->AutoInjectedConv));
  551. assert_param(IS_ADC_EXTTRIGINJEC(sConfigInjected->ExternalTrigInjecConv));
  552. assert_param(IS_ADC_RANGE(ADC_RESOLUTION_12B, sConfigInjected->InjectedOffset));
  553. if(hadc->Init.ScanConvMode != ADC_SCAN_DISABLE)
  554. {
  555. assert_param(IS_ADC_INJECTED_RANK(sConfigInjected->InjectedRank));
  556. assert_param(IS_ADC_INJECTED_NB_CONV(sConfigInjected->InjectedNbrOfConversion));
  557. assert_param(IS_FUNCTIONAL_STATE(sConfigInjected->InjectedDiscontinuousConvMode));
  558. }
  559. if(sConfigInjected->ExternalTrigInjecConv != ADC_INJECTED_SOFTWARE_START)
  560. {
  561. assert_param(IS_ADC_EXTTRIGINJEC_EDGE(sConfigInjected->ExternalTrigInjecConvEdge));
  562. }
  563. /* Process locked */
  564. __HAL_LOCK(hadc);
  565. /* Configuration of injected group sequencer: */
  566. /* - if scan mode is disabled, injected channels sequence length is set to */
  567. /* 0x00: 1 channel converted (channel on regular rank 1) */
  568. /* Parameter "InjectedNbrOfConversion" is discarded. */
  569. /* Note: Scan mode is present by hardware on this device and, if */
  570. /* disabled, discards automatically nb of conversions. Anyway, nb of */
  571. /* conversions is forced to 0x00 for alignment over all STM32 devices. */
  572. /* - if scan mode is enabled, injected channels sequence length is set to */
  573. /* parameter ""InjectedNbrOfConversion". */
  574. if (hadc->Init.ScanConvMode == ADC_SCAN_DISABLE)
  575. {
  576. if (sConfigInjected->InjectedRank == ADC_INJECTED_RANK_1)
  577. {
  578. /* Clear the old SQx bits for all injected ranks */
  579. MODIFY_REG(hadc->Instance->JSQR ,
  580. ADC_JSQR_JL |
  581. ADC_JSQR_JSQ4 |
  582. ADC_JSQR_JSQ3 |
  583. ADC_JSQR_JSQ2 |
  584. ADC_JSQR_JSQ1 ,
  585. ADC_JSQR_RK_JL(sConfigInjected->InjectedChannel,
  586. ADC_INJECTED_RANK_1,
  587. 0x01) );
  588. }
  589. /* If another injected rank than rank1 was intended to be set, and could */
  590. /* not due to ScanConvMode disabled, error is reported. */
  591. else
  592. {
  593. /* Update ADC state machine to error */
  594. SET_BIT(hadc->State, HAL_ADC_STATE_ERROR_CONFIG);
  595. tmp_hal_status = HAL_ERROR;
  596. }
  597. }
  598. else
  599. {
  600. /* Since injected channels rank conv. order depends on total number of */
  601. /* injected conversions, selected rank must be below or equal to total */
  602. /* number of injected conversions to be updated. */
  603. if (sConfigInjected->InjectedRank <= sConfigInjected->InjectedNbrOfConversion)
  604. {
  605. /* Clear the old SQx bits for the selected rank */
  606. /* Set the SQx bits for the selected rank */
  607. MODIFY_REG(hadc->Instance->JSQR ,
  608. ADC_JSQR_JL |
  609. ADC_JSQR_RK_JL(ADC_JSQR_JSQ1,
  610. sConfigInjected->InjectedRank,
  611. sConfigInjected->InjectedNbrOfConversion) ,
  612. ADC_JSQR_JL_SHIFT(sConfigInjected->InjectedNbrOfConversion) |
  613. ADC_JSQR_RK_JL(sConfigInjected->InjectedChannel,
  614. sConfigInjected->InjectedRank,
  615. sConfigInjected->InjectedNbrOfConversion) );
  616. }
  617. else
  618. {
  619. /* Clear the old SQx bits for the selected rank */
  620. MODIFY_REG(hadc->Instance->JSQR ,
  621. ADC_JSQR_JL |
  622. ADC_JSQR_RK_JL(ADC_JSQR_JSQ1,
  623. sConfigInjected->InjectedRank,
  624. sConfigInjected->InjectedNbrOfConversion) ,
  625. 0x00000000 );
  626. }
  627. }
  628. /* Enable external trigger if trigger selection is different of software */
  629. /* start. */
  630. /* Note: This configuration keeps the hardware feature of parameter */
  631. /* ExternalTrigConvEdge "trigger edge none" equivalent to */
  632. /* software start. */
  633. if (sConfigInjected->ExternalTrigInjecConv != ADC_INJECTED_SOFTWARE_START)
  634. {
  635. MODIFY_REG(hadc->Instance->CR2 ,
  636. ADC_CR2_JEXTEN |
  637. ADC_CR2_JEXTSEL ,
  638. sConfigInjected->ExternalTrigInjecConv |
  639. sConfigInjected->ExternalTrigInjecConvEdge );
  640. }
  641. else
  642. {
  643. MODIFY_REG(hadc->Instance->CR2,
  644. ADC_CR2_JEXTEN |
  645. ADC_CR2_JEXTSEL ,
  646. 0x00000000 );
  647. }
  648. /* Configuration of injected group */
  649. /* Parameters update conditioned to ADC state: */
  650. /* Parameters that can be updated only when ADC is disabled: */
  651. /* - Automatic injected conversion */
  652. /* - Injected discontinuous mode */
  653. if ((ADC_IS_ENABLE(hadc) == RESET))
  654. {
  655. hadc->Instance->CR1 &= ~(ADC_CR1_JAUTO |
  656. ADC_CR1_JDISCEN );
  657. /* Automatic injected conversion can be enabled if injected group */
  658. /* external triggers are disabled. */
  659. if (sConfigInjected->AutoInjectedConv == ENABLE)
  660. {
  661. if (sConfigInjected->ExternalTrigInjecConv == ADC_INJECTED_SOFTWARE_START)
  662. {
  663. SET_BIT(hadc->Instance->CR1, ADC_CR1_JAUTO);
  664. }
  665. else
  666. {
  667. /* Update ADC state machine to error */
  668. SET_BIT(hadc->State, HAL_ADC_STATE_ERROR_CONFIG);
  669. tmp_hal_status = HAL_ERROR;
  670. }
  671. }
  672. /* Injected discontinuous can be enabled only if auto-injected mode is */
  673. /* disabled. */
  674. if (sConfigInjected->InjectedDiscontinuousConvMode == ENABLE)
  675. {
  676. if (sConfigInjected->AutoInjectedConv == DISABLE)
  677. {
  678. SET_BIT(hadc->Instance->CR1, ADC_CR1_JDISCEN);
  679. }
  680. else
  681. {
  682. /* Update ADC state machine to error */
  683. SET_BIT(hadc->State, HAL_ADC_STATE_ERROR_CONFIG);
  684. tmp_hal_status = HAL_ERROR;
  685. }
  686. }
  687. }
  688. /* Channel sampling time configuration */
  689. /* For InjectedChannels 0 to 9 */
  690. if (sConfigInjected->InjectedChannel < ADC_CHANNEL_10)
  691. {
  692. MODIFY_REG(hadc->Instance->SMPR3,
  693. ADC_SMPR3(ADC_SMPR3_SMP0, sConfigInjected->InjectedChannel),
  694. ADC_SMPR3(sConfigInjected->InjectedSamplingTime, sConfigInjected->InjectedChannel) );
  695. }
  696. /* For InjectedChannels 10 to 19 */
  697. else if (sConfigInjected->InjectedChannel < ADC_CHANNEL_20)
  698. {
  699. MODIFY_REG(hadc->Instance->SMPR2,
  700. ADC_SMPR2(ADC_SMPR2_SMP10, sConfigInjected->InjectedChannel),
  701. ADC_SMPR2(sConfigInjected->InjectedSamplingTime, sConfigInjected->InjectedChannel) );
  702. }
  703. /* For InjectedChannels 20 to 26 for devices Cat.1, Cat.2, Cat.3 */
  704. /* For InjectedChannels 20 to 29 for devices Cat4, Cat.5 */
  705. else if (sConfigInjected->InjectedChannel <= ADC_SMPR1_CHANNEL_MAX)
  706. {
  707. MODIFY_REG(hadc->Instance->SMPR1,
  708. ADC_SMPR1(ADC_SMPR1_SMP20, sConfigInjected->InjectedChannel),
  709. ADC_SMPR1(sConfigInjected->InjectedSamplingTime, sConfigInjected->InjectedChannel) );
  710. }
  711. /* For InjectedChannels 30 to 31 for devices Cat4, Cat.5 */
  712. else
  713. {
  714. ADC_SMPR0_CHANNEL_SET(hadc, sConfigInjected->InjectedSamplingTime, sConfigInjected->InjectedChannel);
  715. }
  716. /* Configure the offset: offset enable/disable, InjectedChannel, offset value */
  717. switch(sConfigInjected->InjectedRank)
  718. {
  719. case 1:
  720. /* Set injected channel 1 offset */
  721. MODIFY_REG(hadc->Instance->JOFR1,
  722. ADC_JOFR1_JOFFSET1,
  723. sConfigInjected->InjectedOffset);
  724. break;
  725. case 2:
  726. /* Set injected channel 2 offset */
  727. MODIFY_REG(hadc->Instance->JOFR2,
  728. ADC_JOFR2_JOFFSET2,
  729. sConfigInjected->InjectedOffset);
  730. break;
  731. case 3:
  732. /* Set injected channel 3 offset */
  733. MODIFY_REG(hadc->Instance->JOFR3,
  734. ADC_JOFR3_JOFFSET3,
  735. sConfigInjected->InjectedOffset);
  736. break;
  737. case 4:
  738. default:
  739. MODIFY_REG(hadc->Instance->JOFR4,
  740. ADC_JOFR4_JOFFSET4,
  741. sConfigInjected->InjectedOffset);
  742. break;
  743. }
  744. /* If ADC1 Channel_16 or Channel_17 is selected, enable Temperature sensor */
  745. /* and VREFINT measurement path. */
  746. if ((sConfigInjected->InjectedChannel == ADC_CHANNEL_TEMPSENSOR) ||
  747. (sConfigInjected->InjectedChannel == ADC_CHANNEL_VREFINT) )
  748. {
  749. SET_BIT(ADC->CCR, ADC_CCR_TSVREFE);
  750. if ((sConfigInjected->InjectedChannel == ADC_CHANNEL_TEMPSENSOR))
  751. {
  752. /* Delay for temperature sensor stabilization time */
  753. /* Compute number of CPU cycles to wait for */
  754. wait_loop_index = (ADC_TEMPSENSOR_DELAY_US * (SystemCoreClock / 1000000));
  755. while(wait_loop_index != 0)
  756. {
  757. wait_loop_index--;
  758. }
  759. }
  760. }
  761. /* Process unlocked */
  762. __HAL_UNLOCK(hadc);
  763. /* Return function status */
  764. return tmp_hal_status;
  765. }
  766. /**
  767. * @}
  768. */
  769. /**
  770. * @}
  771. */
  772. #endif /* HAL_ADC_MODULE_ENABLED */
  773. /**
  774. * @}
  775. */
  776. /**
  777. * @}
  778. */
  779. /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/