stm32f1xx_hal_adc_ex.c 53 KB

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