You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 

855 lines
28 KiB

  1. /**
  2. ******************************************************************************
  3. * @file stm32f7xx_hal_adc_ex.c
  4. * @author MCD Application Team
  5. * @version V1.0.1
  6. * @date 25-June-2015
  7. * @brief This file provides firmware functions to manage the following
  8. * functionalities of the ADC extension peripheral:
  9. * + Extended features functions
  10. *
  11. @verbatim
  12. ==============================================================================
  13. ##### How to use this driver #####
  14. ==============================================================================
  15. [..]
  16. (#)Initialize the ADC low level resources by implementing the HAL_ADC_MspInit():
  17. (##) Enable the ADC interface clock using __HAL_RCC_ADC_CLK_ENABLE()
  18. (##) ADC pins configuration
  19. (+++) Enable the clock for the ADC GPIOs using the following function:
  20. __HAL_RCC_GPIOx_CLK_ENABLE()
  21. (+++) Configure these ADC pins in analog mode using HAL_GPIO_Init()
  22. (##) In case of using interrupts (e.g. HAL_ADC_Start_IT())
  23. (+++) Configure the ADC interrupt priority using HAL_NVIC_SetPriority()
  24. (+++) Enable the ADC IRQ handler using HAL_NVIC_EnableIRQ()
  25. (+++) In ADC IRQ handler, call HAL_ADC_IRQHandler()
  26. (##) In case of using DMA to control data transfer (e.g. HAL_ADC_Start_DMA())
  27. (+++) Enable the DMAx interface clock using __HAL_RCC_DMAx_CLK_ENABLE()
  28. (+++) Configure and enable two DMA streams stream for managing data
  29. transfer from peripheral to memory (output stream)
  30. (+++) Associate the initialized DMA handle to the ADC DMA handle
  31. using __HAL_LINKDMA()
  32. (+++) Configure the priority and enable the NVIC for the transfer complete
  33. interrupt on the two DMA Streams. The output stream should have higher
  34. priority than the input stream.
  35. (#) Configure the ADC Prescaler, conversion resolution and data alignment
  36. using the HAL_ADC_Init() function.
  37. (#) Configure the ADC Injected channels group features, use HAL_ADC_Init()
  38. and HAL_ADC_ConfigChannel() functions.
  39. (#) Three operation modes are available within this driver :
  40. *** Polling mode IO operation ***
  41. =================================
  42. [..]
  43. (+) Start the ADC peripheral using HAL_ADCEx_InjectedStart()
  44. (+) Wait for end of conversion using HAL_ADC_PollForConversion(), at this stage
  45. user can specify the value of timeout according to his end application
  46. (+) To read the ADC converted values, use the HAL_ADCEx_InjectedGetValue() function.
  47. (+) Stop the ADC peripheral using HAL_ADCEx_InjectedStop()
  48. *** Interrupt mode IO operation ***
  49. ===================================
  50. [..]
  51. (+) Start the ADC peripheral using HAL_ADCEx_InjectedStart_IT()
  52. (+) Use HAL_ADC_IRQHandler() called under ADC_IRQHandler() Interrupt subroutine
  53. (+) At ADC end of conversion HAL_ADCEx_InjectedConvCpltCallback() function is executed and user can
  54. add his own code by customization of function pointer HAL_ADCEx_InjectedConvCpltCallback
  55. (+) In case of ADC Error, HAL_ADCEx_InjectedErrorCallback() function is executed and user can
  56. add his own code by customization of function pointer HAL_ADCEx_InjectedErrorCallback
  57. (+) Stop the ADC peripheral using HAL_ADCEx_InjectedStop_IT()
  58. *** DMA mode IO operation ***
  59. ==============================
  60. [..]
  61. (+) Start the ADC peripheral using HAL_ADCEx_InjectedStart_DMA(), at this stage the user specify the length
  62. of data to be transferred at each end of conversion
  63. (+) At The end of data transfer ba HAL_ADCEx_InjectedConvCpltCallback() function is executed and user can
  64. add his own code by customization of function pointer HAL_ADCEx_InjectedConvCpltCallback
  65. (+) In case of transfer Error, HAL_ADCEx_InjectedErrorCallback() function is executed and user can
  66. add his own code by customization of function pointer HAL_ADCEx_InjectedErrorCallback
  67. (+) Stop the ADC peripheral using HAL_ADCEx_InjectedStop_DMA()
  68. *** Multi mode ADCs Regular channels configuration ***
  69. ======================================================
  70. [..]
  71. (+) Select the Multi mode ADC regular channels features (dual or triple mode)
  72. and configure the DMA mode using HAL_ADCEx_MultiModeConfigChannel() functions.
  73. (+) Start the ADC peripheral using HAL_ADCEx_MultiModeStart_DMA(), at this stage the user specify the length
  74. of data to be transferred at each end of conversion
  75. (+) Read the ADCs converted values using the HAL_ADCEx_MultiModeGetValue() function.
  76. @endverbatim
  77. ******************************************************************************
  78. * @attention
  79. *
  80. * <h2><center>&copy; COPYRIGHT(c) 2015 STMicroelectronics</center></h2>
  81. *
  82. * Redistribution and use in source and binary forms, with or without modification,
  83. * are permitted provided that the following conditions are met:
  84. * 1. Redistributions of source code must retain the above copyright notice,
  85. * this list of conditions and the following disclaimer.
  86. * 2. Redistributions in binary form must reproduce the above copyright notice,
  87. * this list of conditions and the following disclaimer in the documentation
  88. * and/or other materials provided with the distribution.
  89. * 3. Neither the name of STMicroelectronics nor the names of its contributors
  90. * may be used to endorse or promote products derived from this software
  91. * without specific prior written permission.
  92. *
  93. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  94. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  95. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  96. * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
  97. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  98. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  99. * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  100. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  101. * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  102. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  103. *
  104. ******************************************************************************
  105. */
  106. /* Includes ------------------------------------------------------------------*/
  107. #include "stm32f7xx_hal.h"
  108. /** @addtogroup STM32F7xx_HAL_Driver
  109. * @{
  110. */
  111. /** @defgroup ADCEx ADCEx
  112. * @brief ADC Extended driver modules
  113. * @{
  114. */
  115. #ifdef HAL_ADC_MODULE_ENABLED
  116. /* Private typedef -----------------------------------------------------------*/
  117. /* Private define ------------------------------------------------------------*/
  118. /* Private macro -------------------------------------------------------------*/
  119. /* Private variables ---------------------------------------------------------*/
  120. /* Private function prototypes -----------------------------------------------*/
  121. /** @addtogroup ADCEx_Private_Functions
  122. * @{
  123. */
  124. static void ADC_MultiModeDMAConvCplt(DMA_HandleTypeDef *hdma);
  125. static void ADC_MultiModeDMAError(DMA_HandleTypeDef *hdma);
  126. static void ADC_MultiModeDMAHalfConvCplt(DMA_HandleTypeDef *hdma);
  127. /**
  128. * @}
  129. */
  130. /* Exported functions ---------------------------------------------------------*/
  131. /** @defgroup ADCEx_Exported_Functions ADC Exported Functions
  132. * @{
  133. */
  134. /** @defgroup ADCEx_Exported_Functions_Group1 Extended features functions
  135. * @brief Extended features functions
  136. *
  137. @verbatim
  138. ===============================================================================
  139. ##### Extended features functions #####
  140. ===============================================================================
  141. [..] This section provides functions allowing to:
  142. (+) Start conversion of injected channel.
  143. (+) Stop conversion of injected channel.
  144. (+) Start multimode and enable DMA transfer.
  145. (+) Stop multimode and disable DMA transfer.
  146. (+) Get result of injected channel conversion.
  147. (+) Get result of multimode conversion.
  148. (+) Configure injected channels.
  149. (+) Configure multimode.
  150. @endverbatim
  151. * @{
  152. */
  153. /**
  154. * @brief Enables the selected ADC software start conversion of the injected channels.
  155. * @param hadc: pointer to a ADC_HandleTypeDef structure that contains
  156. * the configuration information for the specified ADC.
  157. * @retval HAL status
  158. */
  159. HAL_StatusTypeDef HAL_ADCEx_InjectedStart(ADC_HandleTypeDef* hadc)
  160. {
  161. __IO uint32_t counter = 0;
  162. uint32_t tmp1 = 0, tmp2 = 0;
  163. /* Process locked */
  164. __HAL_LOCK(hadc);
  165. /* Check if a regular conversion is ongoing */
  166. if(hadc->State == HAL_ADC_STATE_BUSY_REG)
  167. {
  168. /* Change ADC state */
  169. hadc->State = HAL_ADC_STATE_BUSY_INJ_REG;
  170. }
  171. else
  172. {
  173. /* Change ADC state */
  174. hadc->State = HAL_ADC_STATE_BUSY_INJ;
  175. }
  176. /* Check if ADC peripheral is disabled in order to enable it and wait during
  177. Tstab time the ADC's stabilization */
  178. if((hadc->Instance->CR2 & ADC_CR2_ADON) != ADC_CR2_ADON)
  179. {
  180. /* Enable the Peripheral */
  181. __HAL_ADC_ENABLE(hadc);
  182. /* Delay for temperature sensor stabilization time */
  183. /* Compute number of CPU cycles to wait for */
  184. counter = (ADC_STAB_DELAY_US * (SystemCoreClock / 1000000));
  185. while(counter != 0)
  186. {
  187. counter--;
  188. }
  189. }
  190. /* Check if Multimode enabled */
  191. if(HAL_IS_BIT_CLR(ADC->CCR, ADC_CCR_MULTI))
  192. {
  193. tmp1 = HAL_IS_BIT_CLR(hadc->Instance->CR2, ADC_CR2_JEXTEN);
  194. tmp2 = HAL_IS_BIT_CLR(hadc->Instance->CR1, ADC_CR1_JAUTO);
  195. if(tmp1 && tmp2)
  196. {
  197. /* Enable the selected ADC software conversion for injected group */
  198. hadc->Instance->CR2 |= ADC_CR2_JSWSTART;
  199. }
  200. }
  201. else
  202. {
  203. tmp1 = HAL_IS_BIT_CLR(hadc->Instance->CR2, ADC_CR2_JEXTEN);
  204. tmp2 = HAL_IS_BIT_CLR(hadc->Instance->CR1, ADC_CR1_JAUTO);
  205. if((hadc->Instance == ADC1) && tmp1 && tmp2)
  206. {
  207. /* Enable the selected ADC software conversion for injected group */
  208. hadc->Instance->CR2 |= ADC_CR2_JSWSTART;
  209. }
  210. }
  211. /* Process unlocked */
  212. __HAL_UNLOCK(hadc);
  213. /* Return function status */
  214. return HAL_OK;
  215. }
  216. /**
  217. * @brief Enables the interrupt and starts ADC conversion of injected channels.
  218. * @param hadc: pointer to a ADC_HandleTypeDef structure that contains
  219. * the configuration information for the specified ADC.
  220. *
  221. * @retval HAL status.
  222. */
  223. HAL_StatusTypeDef HAL_ADCEx_InjectedStart_IT(ADC_HandleTypeDef* hadc)
  224. {
  225. __IO uint32_t counter = 0;
  226. uint32_t tmp1 = 0, tmp2 =0;
  227. /* Process locked */
  228. __HAL_LOCK(hadc);
  229. /* Check if a regular conversion is ongoing */
  230. if(hadc->State == HAL_ADC_STATE_BUSY_REG)
  231. {
  232. /* Change ADC state */
  233. hadc->State = HAL_ADC_STATE_BUSY_INJ_REG;
  234. }
  235. else
  236. {
  237. /* Change ADC state */
  238. hadc->State = HAL_ADC_STATE_BUSY_INJ;
  239. }
  240. /* Set ADC error code to none */
  241. hadc->ErrorCode = HAL_ADC_ERROR_NONE;
  242. /* Check if ADC peripheral is disabled in order to enable it and wait during
  243. Tstab time the ADC's stabilization */
  244. if((hadc->Instance->CR2 & ADC_CR2_ADON) != ADC_CR2_ADON)
  245. {
  246. /* Enable the Peripheral */
  247. __HAL_ADC_ENABLE(hadc);
  248. /* Delay for temperature sensor stabilization time */
  249. /* Compute number of CPU cycles to wait for */
  250. counter = (ADC_STAB_DELAY_US * (SystemCoreClock / 1000000));
  251. while(counter != 0)
  252. {
  253. counter--;
  254. }
  255. }
  256. /* Enable the ADC end of conversion interrupt for injected group */
  257. __HAL_ADC_ENABLE_IT(hadc, ADC_IT_JEOC);
  258. /* Enable the ADC overrun interrupt */
  259. __HAL_ADC_ENABLE_IT(hadc, ADC_IT_OVR);
  260. /* Check if Multimode enabled */
  261. if(HAL_IS_BIT_CLR(ADC->CCR, ADC_CCR_MULTI))
  262. {
  263. tmp1 = HAL_IS_BIT_CLR(hadc->Instance->CR2, ADC_CR2_JEXTEN);
  264. tmp2 = HAL_IS_BIT_CLR(hadc->Instance->CR1, ADC_CR1_JAUTO);
  265. if(tmp1 && tmp2)
  266. {
  267. /* Enable the selected ADC software conversion for injected group */
  268. hadc->Instance->CR2 |= ADC_CR2_JSWSTART;
  269. }
  270. }
  271. else
  272. {
  273. tmp1 = HAL_IS_BIT_CLR(hadc->Instance->CR2, ADC_CR2_JEXTEN);
  274. tmp2 = HAL_IS_BIT_CLR(hadc->Instance->CR1, ADC_CR1_JAUTO);
  275. if((hadc->Instance == ADC1) && tmp1 && tmp2)
  276. {
  277. /* Enable the selected ADC software conversion for injected group */
  278. hadc->Instance->CR2 |= ADC_CR2_JSWSTART;
  279. }
  280. }
  281. /* Process unlocked */
  282. __HAL_UNLOCK(hadc);
  283. /* Return function status */
  284. return HAL_OK;
  285. }
  286. /**
  287. * @brief Disables ADC and stop conversion of injected channels.
  288. *
  289. * @note Caution: This function will stop also regular channels.
  290. *
  291. * @param hadc: pointer to a ADC_HandleTypeDef structure that contains
  292. * the configuration information for the specified ADC.
  293. * @retval HAL status.
  294. */
  295. HAL_StatusTypeDef HAL_ADCEx_InjectedStop(ADC_HandleTypeDef* hadc)
  296. {
  297. /* Disable the Peripheral */
  298. __HAL_ADC_DISABLE(hadc);
  299. /* Change ADC state */
  300. hadc->State = HAL_ADC_STATE_READY;
  301. /* Return function status */
  302. return HAL_OK;
  303. }
  304. /**
  305. * @brief Poll for injected conversion complete
  306. * @param hadc: pointer to a ADC_HandleTypeDef structure that contains
  307. * the configuration information for the specified ADC.
  308. * @param Timeout: Timeout value in millisecond.
  309. * @retval HAL status
  310. */
  311. HAL_StatusTypeDef HAL_ADCEx_InjectedPollForConversion(ADC_HandleTypeDef* hadc, uint32_t Timeout)
  312. {
  313. uint32_t tickstart = 0;
  314. /* Get tick */
  315. tickstart = HAL_GetTick();
  316. /* Check End of conversion flag */
  317. while(!(__HAL_ADC_GET_FLAG(hadc, ADC_FLAG_JEOC)))
  318. {
  319. /* Check for the Timeout */
  320. if(Timeout != HAL_MAX_DELAY)
  321. {
  322. if((Timeout == 0)||((HAL_GetTick() - tickstart ) > Timeout))
  323. {
  324. hadc->State= HAL_ADC_STATE_TIMEOUT;
  325. /* Process unlocked */
  326. __HAL_UNLOCK(hadc);
  327. return HAL_TIMEOUT;
  328. }
  329. }
  330. }
  331. /* Check if a regular conversion is ready */
  332. if(hadc->State == HAL_ADC_STATE_EOC_REG)
  333. {
  334. /* Change ADC state */
  335. hadc->State = HAL_ADC_STATE_EOC_INJ_REG;
  336. }
  337. else
  338. {
  339. /* Change ADC state */
  340. hadc->State = HAL_ADC_STATE_EOC_INJ;
  341. }
  342. /* Return ADC state */
  343. return HAL_OK;
  344. }
  345. /**
  346. * @brief Disables the interrupt and stop ADC conversion of injected channels.
  347. *
  348. * @note Caution: This function will stop also regular channels.
  349. *
  350. * @param hadc: pointer to a ADC_HandleTypeDef structure that contains
  351. * the configuration information for the specified ADC.
  352. * @retval HAL status.
  353. */
  354. HAL_StatusTypeDef HAL_ADCEx_InjectedStop_IT(ADC_HandleTypeDef* hadc)
  355. {
  356. /* Disable the ADC end of conversion interrupt for regular group */
  357. __HAL_ADC_DISABLE_IT(hadc, ADC_IT_EOC);
  358. /* Disable the ADC end of conversion interrupt for injected group */
  359. __HAL_ADC_DISABLE_IT(hadc, ADC_CR1_JEOCIE);
  360. /* Enable the Peripheral */
  361. __HAL_ADC_DISABLE(hadc);
  362. /* Change ADC state */
  363. hadc->State = HAL_ADC_STATE_READY;
  364. /* Return function status */
  365. return HAL_OK;
  366. }
  367. /**
  368. * @brief Gets the converted value from data register of injected channel.
  369. * @param hadc: pointer to a ADC_HandleTypeDef structure that contains
  370. * the configuration information for the specified ADC.
  371. * @param InjectedRank: the ADC injected rank.
  372. * This parameter can be one of the following values:
  373. * @arg ADC_INJECTED_RANK_1: Injected Channel1 selected
  374. * @arg ADC_INJECTED_RANK_2: Injected Channel2 selected
  375. * @arg ADC_INJECTED_RANK_3: Injected Channel3 selected
  376. * @arg ADC_INJECTED_RANK_4: Injected Channel4 selected
  377. * @retval None
  378. */
  379. uint32_t HAL_ADCEx_InjectedGetValue(ADC_HandleTypeDef* hadc, uint32_t InjectedRank)
  380. {
  381. __IO uint32_t tmp = 0;
  382. /* Check the parameters */
  383. assert_param(IS_ADC_INJECTED_RANK(InjectedRank));
  384. /* Clear the ADCx's flag for injected end of conversion */
  385. __HAL_ADC_CLEAR_FLAG(hadc,ADC_FLAG_JEOC);
  386. /* Return the selected ADC converted value */
  387. switch(InjectedRank)
  388. {
  389. case ADC_INJECTED_RANK_4:
  390. {
  391. tmp = hadc->Instance->JDR4;
  392. }
  393. break;
  394. case ADC_INJECTED_RANK_3:
  395. {
  396. tmp = hadc->Instance->JDR3;
  397. }
  398. break;
  399. case ADC_INJECTED_RANK_2:
  400. {
  401. tmp = hadc->Instance->JDR2;
  402. }
  403. break;
  404. case ADC_INJECTED_RANK_1:
  405. {
  406. tmp = hadc->Instance->JDR1;
  407. }
  408. break;
  409. default:
  410. break;
  411. }
  412. return tmp;
  413. }
  414. /**
  415. * @brief Enables ADC DMA request after last transfer (Multi-ADC mode) and enables ADC peripheral
  416. *
  417. * @note Caution: This function must be used only with the ADC master.
  418. *
  419. * @param hadc: pointer to a ADC_HandleTypeDef structure that contains
  420. * the configuration information for the specified ADC.
  421. * @param pData: Pointer to buffer in which transferred from ADC peripheral to memory will be stored.
  422. * @param Length: The length of data to be transferred from ADC peripheral to memory.
  423. * @retval HAL status
  424. */
  425. HAL_StatusTypeDef HAL_ADCEx_MultiModeStart_DMA(ADC_HandleTypeDef* hadc, uint32_t* pData, uint32_t Length)
  426. {
  427. __IO uint32_t counter = 0;
  428. /* Check the parameters */
  429. assert_param(IS_FUNCTIONAL_STATE(hadc->Init.ContinuousConvMode));
  430. assert_param(IS_ADC_EXT_TRIG_EDGE(hadc->Init.ExternalTrigConvEdge));
  431. assert_param(IS_FUNCTIONAL_STATE(hadc->Init.DMAContinuousRequests));
  432. /* Process locked */
  433. __HAL_LOCK(hadc);
  434. /* Enable ADC overrun interrupt */
  435. __HAL_ADC_ENABLE_IT(hadc, ADC_IT_OVR);
  436. if (hadc->Init.DMAContinuousRequests != DISABLE)
  437. {
  438. /* Enable the selected ADC DMA request after last transfer */
  439. ADC->CCR |= ADC_CCR_DDS;
  440. }
  441. else
  442. {
  443. /* Disable the selected ADC EOC rising on each regular channel conversion */
  444. ADC->CCR &= ~ADC_CCR_DDS;
  445. }
  446. /* Set the DMA transfer complete callback */
  447. hadc->DMA_Handle->XferCpltCallback = ADC_MultiModeDMAConvCplt;
  448. /* Set the DMA half transfer complete callback */
  449. hadc->DMA_Handle->XferHalfCpltCallback = ADC_MultiModeDMAHalfConvCplt;
  450. /* Set the DMA error callback */
  451. hadc->DMA_Handle->XferErrorCallback = ADC_MultiModeDMAError ;
  452. /* Enable the DMA Stream */
  453. HAL_DMA_Start_IT(hadc->DMA_Handle, (uint32_t)&ADC->CDR, (uint32_t)pData, Length);
  454. /* Change ADC state */
  455. hadc->State = HAL_ADC_STATE_BUSY_REG;
  456. /* Check if ADC peripheral is disabled in order to enable it and wait during
  457. Tstab time the ADC's stabilization */
  458. if((hadc->Instance->CR2 & ADC_CR2_ADON) != ADC_CR2_ADON)
  459. {
  460. /* Enable the Peripheral */
  461. __HAL_ADC_ENABLE(hadc);
  462. /* Delay for temperature sensor stabilization time */
  463. /* Compute number of CPU cycles to wait for */
  464. counter = (ADC_STAB_DELAY_US * (SystemCoreClock / 1000000));
  465. while(counter != 0)
  466. {
  467. counter--;
  468. }
  469. }
  470. /* if no external trigger present enable software conversion of regular channels */
  471. if((hadc->Instance->CR2 & ADC_CR2_EXTEN) == RESET)
  472. {
  473. /* Enable the selected ADC software conversion for regular group */
  474. hadc->Instance->CR2 |= (uint32_t)ADC_CR2_SWSTART;
  475. }
  476. /* Process unlocked */
  477. __HAL_UNLOCK(hadc);
  478. /* Return function status */
  479. return HAL_OK;
  480. }
  481. /**
  482. * @brief Disables ADC DMA (multi-ADC mode) and disables ADC peripheral
  483. * @param hadc: pointer to a ADC_HandleTypeDef structure that contains
  484. * the configuration information for the specified ADC.
  485. * @retval HAL status
  486. */
  487. HAL_StatusTypeDef HAL_ADCEx_MultiModeStop_DMA(ADC_HandleTypeDef* hadc)
  488. {
  489. /* Process locked */
  490. __HAL_LOCK(hadc);
  491. /* Enable the Peripheral */
  492. __HAL_ADC_DISABLE(hadc);
  493. /* Disable ADC overrun interrupt */
  494. __HAL_ADC_DISABLE_IT(hadc, ADC_IT_OVR);
  495. /* Disable the selected ADC DMA request after last transfer */
  496. ADC->CCR &= ~ADC_CCR_DDS;
  497. /* Disable the ADC DMA Stream */
  498. HAL_DMA_Abort(hadc->DMA_Handle);
  499. /* Change ADC state */
  500. hadc->State = HAL_ADC_STATE_READY;
  501. /* Process unlocked */
  502. __HAL_UNLOCK(hadc);
  503. /* Return function status */
  504. return HAL_OK;
  505. }
  506. /**
  507. * @brief Returns the last ADC1, ADC2 and ADC3 regular conversions results
  508. * data in the selected multi mode.
  509. * @param hadc: pointer to a ADC_HandleTypeDef structure that contains
  510. * the configuration information for the specified ADC.
  511. * @retval The converted data value.
  512. */
  513. uint32_t HAL_ADCEx_MultiModeGetValue(ADC_HandleTypeDef* hadc)
  514. {
  515. /* Return the multi mode conversion value */
  516. return ADC->CDR;
  517. }
  518. /**
  519. * @brief Injected conversion complete callback in non blocking mode
  520. * @param hadc: pointer to a ADC_HandleTypeDef structure that contains
  521. * the configuration information for the specified ADC.
  522. * @retval None
  523. */
  524. __weak void HAL_ADCEx_InjectedConvCpltCallback(ADC_HandleTypeDef* hadc)
  525. {
  526. /* NOTE : This function Should not be modified, when the callback is needed,
  527. the HAL_ADC_InjectedConvCpltCallback could be implemented in the user file
  528. */
  529. }
  530. /**
  531. * @brief Configures for the selected ADC injected channel its corresponding
  532. * rank in the sequencer and its sample time.
  533. * @param hadc: pointer to a ADC_HandleTypeDef structure that contains
  534. * the configuration information for the specified ADC.
  535. * @param sConfigInjected: ADC configuration structure for injected channel.
  536. * @retval None
  537. */
  538. HAL_StatusTypeDef HAL_ADCEx_InjectedConfigChannel(ADC_HandleTypeDef* hadc, ADC_InjectionConfTypeDef* sConfigInjected)
  539. {
  540. #ifdef USE_FULL_ASSERT
  541. uint32_t tmp = 0;
  542. #endif /* USE_FULL_ASSERT */
  543. /* Check the parameters */
  544. assert_param(IS_ADC_CHANNEL(sConfigInjected->InjectedChannel));
  545. assert_param(IS_ADC_INJECTED_RANK(sConfigInjected->InjectedRank));
  546. assert_param(IS_ADC_SAMPLE_TIME(sConfigInjected->InjectedSamplingTime));
  547. assert_param(IS_ADC_EXT_INJEC_TRIG(sConfigInjected->ExternalTrigInjecConv));
  548. assert_param(IS_ADC_EXT_INJEC_TRIG_EDGE(sConfigInjected->ExternalTrigInjecConvEdge));
  549. assert_param(IS_ADC_INJECTED_LENGTH(sConfigInjected->InjectedNbrOfConversion));
  550. assert_param(IS_FUNCTIONAL_STATE(sConfigInjected->AutoInjectedConv));
  551. assert_param(IS_FUNCTIONAL_STATE(sConfigInjected->InjectedDiscontinuousConvMode));
  552. #ifdef USE_FULL_ASSERT
  553. tmp = ADC_GET_RESOLUTION(hadc);
  554. assert_param(IS_ADC_RANGE(tmp, sConfigInjected->InjectedOffset));
  555. #endif /* USE_FULL_ASSERT */
  556. /* Process locked */
  557. __HAL_LOCK(hadc);
  558. /* if ADC_Channel_10 ... ADC_Channel_18 is selected */
  559. if (sConfigInjected->InjectedChannel > ADC_CHANNEL_9)
  560. {
  561. /* Clear the old sample time */
  562. hadc->Instance->SMPR1 &= ~ADC_SMPR1(ADC_SMPR1_SMP10, sConfigInjected->InjectedChannel);
  563. /* Set the new sample time */
  564. hadc->Instance->SMPR1 |= ADC_SMPR1(sConfigInjected->InjectedSamplingTime, sConfigInjected->InjectedChannel);
  565. }
  566. else /* ADC_Channel include in ADC_Channel_[0..9] */
  567. {
  568. /* Clear the old sample time */
  569. hadc->Instance->SMPR2 &= ~ADC_SMPR2(ADC_SMPR2_SMP0, sConfigInjected->InjectedChannel);
  570. /* Set the new sample time */
  571. hadc->Instance->SMPR2 |= ADC_SMPR2(sConfigInjected->InjectedSamplingTime, sConfigInjected->InjectedChannel);
  572. }
  573. /*---------------------------- ADCx JSQR Configuration -----------------*/
  574. hadc->Instance->JSQR &= ~(ADC_JSQR_JL);
  575. hadc->Instance->JSQR |= ADC_SQR1(sConfigInjected->InjectedNbrOfConversion);
  576. /* Rank configuration */
  577. /* Clear the old SQx bits for the selected rank */
  578. hadc->Instance->JSQR &= ~ADC_JSQR(ADC_JSQR_JSQ1, sConfigInjected->InjectedRank,sConfigInjected->InjectedNbrOfConversion);
  579. /* Set the SQx bits for the selected rank */
  580. hadc->Instance->JSQR |= ADC_JSQR(sConfigInjected->InjectedChannel, sConfigInjected->InjectedRank,sConfigInjected->InjectedNbrOfConversion);
  581. /* Select external trigger to start conversion */
  582. hadc->Instance->CR2 &= ~(ADC_CR2_JEXTSEL);
  583. hadc->Instance->CR2 |= sConfigInjected->ExternalTrigInjecConv;
  584. /* Select external trigger polarity */
  585. hadc->Instance->CR2 &= ~(ADC_CR2_JEXTEN);
  586. hadc->Instance->CR2 |= sConfigInjected->ExternalTrigInjecConvEdge;
  587. if (sConfigInjected->AutoInjectedConv != DISABLE)
  588. {
  589. /* Enable the selected ADC automatic injected group conversion */
  590. hadc->Instance->CR1 |= ADC_CR1_JAUTO;
  591. }
  592. else
  593. {
  594. /* Disable the selected ADC automatic injected group conversion */
  595. hadc->Instance->CR1 &= ~(ADC_CR1_JAUTO);
  596. }
  597. if (sConfigInjected->InjectedDiscontinuousConvMode != DISABLE)
  598. {
  599. /* Enable the selected ADC injected discontinuous mode */
  600. hadc->Instance->CR1 |= ADC_CR1_JDISCEN;
  601. }
  602. else
  603. {
  604. /* Disable the selected ADC injected discontinuous mode */
  605. hadc->Instance->CR1 &= ~(ADC_CR1_JDISCEN);
  606. }
  607. switch(sConfigInjected->InjectedRank)
  608. {
  609. case 1:
  610. /* Set injected channel 1 offset */
  611. hadc->Instance->JOFR1 &= ~(ADC_JOFR1_JOFFSET1);
  612. hadc->Instance->JOFR1 |= sConfigInjected->InjectedOffset;
  613. break;
  614. case 2:
  615. /* Set injected channel 2 offset */
  616. hadc->Instance->JOFR2 &= ~(ADC_JOFR2_JOFFSET2);
  617. hadc->Instance->JOFR2 |= sConfigInjected->InjectedOffset;
  618. break;
  619. case 3:
  620. /* Set injected channel 3 offset */
  621. hadc->Instance->JOFR3 &= ~(ADC_JOFR3_JOFFSET3);
  622. hadc->Instance->JOFR3 |= sConfigInjected->InjectedOffset;
  623. break;
  624. default:
  625. /* Set injected channel 4 offset */
  626. hadc->Instance->JOFR4 &= ~(ADC_JOFR4_JOFFSET4);
  627. hadc->Instance->JOFR4 |= sConfigInjected->InjectedOffset;
  628. break;
  629. }
  630. /* if ADC1 Channel_18 is selected enable VBAT Channel */
  631. if ((hadc->Instance == ADC1) && (sConfigInjected->InjectedChannel == ADC_CHANNEL_VBAT))
  632. {
  633. /* Enable the VBAT channel*/
  634. ADC->CCR |= ADC_CCR_VBATE;
  635. }
  636. /* if ADC1 Channel_16 or Channel_17 is selected enable TSVREFE Channel(Temperature sensor and VREFINT) */
  637. if ((hadc->Instance == ADC1) && ((sConfigInjected->InjectedChannel == ADC_CHANNEL_TEMPSENSOR) || (sConfigInjected->InjectedChannel == ADC_CHANNEL_VREFINT)))
  638. {
  639. /* Enable the TSVREFE channel*/
  640. ADC->CCR |= ADC_CCR_TSVREFE;
  641. }
  642. /* Process unlocked */
  643. __HAL_UNLOCK(hadc);
  644. /* Return function status */
  645. return HAL_OK;
  646. }
  647. /**
  648. * @brief Configures the ADC multi-mode
  649. * @param hadc : pointer to a ADC_HandleTypeDef structure that contains
  650. * the configuration information for the specified ADC.
  651. * @param multimode : pointer to an ADC_MultiModeTypeDef structure that contains
  652. * the configuration information for multimode.
  653. * @retval HAL status
  654. */
  655. HAL_StatusTypeDef HAL_ADCEx_MultiModeConfigChannel(ADC_HandleTypeDef* hadc, ADC_MultiModeTypeDef* multimode)
  656. {
  657. /* Check the parameters */
  658. assert_param(IS_ADC_MODE(multimode->Mode));
  659. assert_param(IS_ADC_DMA_ACCESS_MODE(multimode->DMAAccessMode));
  660. assert_param(IS_ADC_SAMPLING_DELAY(multimode->TwoSamplingDelay));
  661. /* Process locked */
  662. __HAL_LOCK(hadc);
  663. /* Set ADC mode */
  664. ADC->CCR &= ~(ADC_CCR_MULTI);
  665. ADC->CCR |= multimode->Mode;
  666. /* Set the ADC DMA access mode */
  667. ADC->CCR &= ~(ADC_CCR_DMA);
  668. ADC->CCR |= multimode->DMAAccessMode;
  669. /* Set delay between two sampling phases */
  670. ADC->CCR &= ~(ADC_CCR_DELAY);
  671. ADC->CCR |= multimode->TwoSamplingDelay;
  672. /* Process unlocked */
  673. __HAL_UNLOCK(hadc);
  674. /* Return function status */
  675. return HAL_OK;
  676. }
  677. /**
  678. * @}
  679. */
  680. /**
  681. * @brief DMA transfer complete callback.
  682. * @param hdma: pointer to a DMA_HandleTypeDef structure that contains
  683. * the configuration information for the specified DMA module.
  684. * @retval None
  685. */
  686. static void ADC_MultiModeDMAConvCplt(DMA_HandleTypeDef *hdma)
  687. {
  688. ADC_HandleTypeDef* hadc = ( ADC_HandleTypeDef* )((DMA_HandleTypeDef* )hdma)->Parent;
  689. /* Check if an injected conversion is ready */
  690. if(hadc->State == HAL_ADC_STATE_EOC_INJ)
  691. {
  692. /* Change ADC state */
  693. hadc->State = HAL_ADC_STATE_EOC_INJ_REG;
  694. }
  695. else
  696. {
  697. /* Change ADC state */
  698. hadc->State = HAL_ADC_STATE_EOC_REG;
  699. }
  700. HAL_ADC_ConvCpltCallback(hadc);
  701. }
  702. /**
  703. * @brief DMA half transfer complete callback.
  704. * @param hdma: pointer to a DMA_HandleTypeDef structure that contains
  705. * the configuration information for the specified DMA module.
  706. * @retval None
  707. */
  708. static void ADC_MultiModeDMAHalfConvCplt(DMA_HandleTypeDef *hdma)
  709. {
  710. ADC_HandleTypeDef* hadc = ( ADC_HandleTypeDef* )((DMA_HandleTypeDef* )hdma)->Parent;
  711. /* Conversion complete callback */
  712. HAL_ADC_ConvHalfCpltCallback(hadc);
  713. }
  714. /**
  715. * @brief DMA error callback
  716. * @param hdma: pointer to a DMA_HandleTypeDef structure that contains
  717. * the configuration information for the specified DMA module.
  718. * @retval None
  719. */
  720. static void ADC_MultiModeDMAError(DMA_HandleTypeDef *hdma)
  721. {
  722. ADC_HandleTypeDef* hadc = ( ADC_HandleTypeDef* )((DMA_HandleTypeDef* )hdma)->Parent;
  723. hadc->State= HAL_ADC_STATE_ERROR;
  724. /* Set ADC error code to DMA error */
  725. hadc->ErrorCode |= HAL_ADC_ERROR_DMA;
  726. HAL_ADC_ErrorCallback(hadc);
  727. }
  728. /**
  729. * @}
  730. */
  731. #endif /* HAL_ADC_MODULE_ENABLED */
  732. /**
  733. * @}
  734. */
  735. /**
  736. * @}
  737. */
  738. /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/