Implement a secure ICS protocol targeting LoRa Node151 microcontroller for controlling irrigation.
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.
 
 
 
 
 
 

1338 lines
51 KiB

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