stm32l1xx_hal_adc_ex.c 32 KB

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