stm32f10x_adc.c 46 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308
  1. /**
  2. ******************************************************************************
  3. * @file stm32f10x_adc.c
  4. * @author MCD Application Team
  5. * @version V3.5.0
  6. * @date 11-March-2011
  7. * @brief This file provides all the ADC firmware functions.
  8. ******************************************************************************
  9. * @attention
  10. *
  11. * THE PRESENT FIRMWARE WHICH IS FOR GUIDANCE ONLY AIMS AT PROVIDING CUSTOMERS
  12. * WITH CODING INFORMATION REGARDING THEIR PRODUCTS IN ORDER FOR THEM TO SAVE
  13. * TIME. AS A RESULT, STMICROELECTRONICS SHALL NOT BE HELD LIABLE FOR ANY
  14. * DIRECT, INDIRECT OR CONSEQUENTIAL DAMAGES WITH RESPECT TO ANY CLAIMS ARISING
  15. * FROM THE CONTENT OF SUCH FIRMWARE AND/OR THE USE MADE BY CUSTOMERS OF THE
  16. * CODING INFORMATION CONTAINED HEREIN IN CONNECTION WITH THEIR PRODUCTS.
  17. *
  18. * <h2><center>&copy; COPYRIGHT 2011 STMicroelectronics</center></h2>
  19. ******************************************************************************
  20. */
  21. /* Includes ------------------------------------------------------------------*/
  22. #include "stm32f10x_adc.h"
  23. #include "stm32f10x_rcc.h"
  24. /** @addtogroup STM32F10x_StdPeriph_Driver
  25. * @{
  26. */
  27. /** @defgroup ADC
  28. * @brief ADC driver modules
  29. * @{
  30. */
  31. /** @defgroup ADC_Private_TypesDefinitions
  32. * @{
  33. */
  34. /**
  35. * @}
  36. */
  37. /** @defgroup ADC_Private_Defines
  38. * @{
  39. */
  40. /* ADC DISCNUM mask */
  41. #define CR1_DISCNUM_Reset ((uint32_t)0xFFFF1FFF)
  42. /* ADC DISCEN mask */
  43. #define CR1_DISCEN_Set ((uint32_t)0x00000800)
  44. #define CR1_DISCEN_Reset ((uint32_t)0xFFFFF7FF)
  45. /* ADC JAUTO mask */
  46. #define CR1_JAUTO_Set ((uint32_t)0x00000400)
  47. #define CR1_JAUTO_Reset ((uint32_t)0xFFFFFBFF)
  48. /* ADC JDISCEN mask */
  49. #define CR1_JDISCEN_Set ((uint32_t)0x00001000)
  50. #define CR1_JDISCEN_Reset ((uint32_t)0xFFFFEFFF)
  51. /* ADC AWDCH mask */
  52. #define CR1_AWDCH_Reset ((uint32_t)0xFFFFFFE0)
  53. /* ADC Analog watchdog enable mode mask */
  54. #define CR1_AWDMode_Reset ((uint32_t)0xFF3FFDFF)
  55. /* CR1 register Mask */
  56. #define CR1_CLEAR_Mask ((uint32_t)0xFFF0FEFF)
  57. /* ADC ADON mask */
  58. #define CR2_ADON_Set ((uint32_t)0x00000001)
  59. #define CR2_ADON_Reset ((uint32_t)0xFFFFFFFE)
  60. /* ADC DMA mask */
  61. #define CR2_DMA_Set ((uint32_t)0x00000100)
  62. #define CR2_DMA_Reset ((uint32_t)0xFFFFFEFF)
  63. /* ADC RSTCAL mask */
  64. #define CR2_RSTCAL_Set ((uint32_t)0x00000008)
  65. /* ADC CAL mask */
  66. #define CR2_CAL_Set ((uint32_t)0x00000004)
  67. /* ADC SWSTART mask */
  68. #define CR2_SWSTART_Set ((uint32_t)0x00400000)
  69. /* ADC EXTTRIG mask */
  70. #define CR2_EXTTRIG_Set ((uint32_t)0x00100000)
  71. #define CR2_EXTTRIG_Reset ((uint32_t)0xFFEFFFFF)
  72. /* ADC Software start mask */
  73. #define CR2_EXTTRIG_SWSTART_Set ((uint32_t)0x00500000)
  74. #define CR2_EXTTRIG_SWSTART_Reset ((uint32_t)0xFFAFFFFF)
  75. /* ADC JEXTSEL mask */
  76. #define CR2_JEXTSEL_Reset ((uint32_t)0xFFFF8FFF)
  77. /* ADC JEXTTRIG mask */
  78. #define CR2_JEXTTRIG_Set ((uint32_t)0x00008000)
  79. #define CR2_JEXTTRIG_Reset ((uint32_t)0xFFFF7FFF)
  80. /* ADC JSWSTART mask */
  81. #define CR2_JSWSTART_Set ((uint32_t)0x00200000)
  82. /* ADC injected software start mask */
  83. #define CR2_JEXTTRIG_JSWSTART_Set ((uint32_t)0x00208000)
  84. #define CR2_JEXTTRIG_JSWSTART_Reset ((uint32_t)0xFFDF7FFF)
  85. /* ADC TSPD mask */
  86. #define CR2_TSVREFE_Set ((uint32_t)0x00800000)
  87. #define CR2_TSVREFE_Reset ((uint32_t)0xFF7FFFFF)
  88. /* CR2 register Mask */
  89. #define CR2_CLEAR_Mask ((uint32_t)0xFFF1F7FD)
  90. /* ADC SQx mask */
  91. #define SQR3_SQ_Set ((uint32_t)0x0000001F)
  92. #define SQR2_SQ_Set ((uint32_t)0x0000001F)
  93. #define SQR1_SQ_Set ((uint32_t)0x0000001F)
  94. /* SQR1 register Mask */
  95. #define SQR1_CLEAR_Mask ((uint32_t)0xFF0FFFFF)
  96. /* ADC JSQx mask */
  97. #define JSQR_JSQ_Set ((uint32_t)0x0000001F)
  98. /* ADC JL mask */
  99. #define JSQR_JL_Set ((uint32_t)0x00300000)
  100. #define JSQR_JL_Reset ((uint32_t)0xFFCFFFFF)
  101. /* ADC SMPx mask */
  102. #define SMPR1_SMP_Set ((uint32_t)0x00000007)
  103. #define SMPR2_SMP_Set ((uint32_t)0x00000007)
  104. /* ADC JDRx registers offset */
  105. #define JDR_Offset ((uint8_t)0x28)
  106. /* ADC1 DR register base address */
  107. #define DR_ADDRESS ((uint32_t)0x4001244C)
  108. /**
  109. * @}
  110. */
  111. /** @defgroup ADC_Private_Macros
  112. * @{
  113. */
  114. /**
  115. * @}
  116. */
  117. /** @defgroup ADC_Private_Variables
  118. * @{
  119. */
  120. /**
  121. * @}
  122. */
  123. /** @defgroup ADC_Private_FunctionPrototypes
  124. * @{
  125. */
  126. /**
  127. * @}
  128. */
  129. /** @defgroup ADC_Private_Functions
  130. * @{
  131. */
  132. /**
  133. * @brief Deinitializes the ADCx peripheral registers to their default reset values.
  134. * @param ADCx: where x can be 1, 2 or 3 to select the ADC peripheral.
  135. * @retval None
  136. */
  137. void ADC_DeInit(ADC_TypeDef* ADCx)
  138. {
  139. /* Check the parameters */
  140. assert_param(IS_ADC_ALL_PERIPH(ADCx));
  141. if (ADCx == ADC1)
  142. {
  143. /* Enable ADC1 reset state */
  144. RCC_APB2PeriphResetCmd(RCC_APB2Periph_ADC1, ENABLE);
  145. /* Release ADC1 from reset state */
  146. RCC_APB2PeriphResetCmd(RCC_APB2Periph_ADC1, DISABLE);
  147. }
  148. else if (ADCx == ADC2)
  149. {
  150. /* Enable ADC2 reset state */
  151. RCC_APB2PeriphResetCmd(RCC_APB2Periph_ADC2, ENABLE);
  152. /* Release ADC2 from reset state */
  153. RCC_APB2PeriphResetCmd(RCC_APB2Periph_ADC2, DISABLE);
  154. }
  155. else
  156. {
  157. if (ADCx == ADC3)
  158. {
  159. /* Enable ADC3 reset state */
  160. RCC_APB2PeriphResetCmd(RCC_APB2Periph_ADC3, ENABLE);
  161. /* Release ADC3 from reset state */
  162. RCC_APB2PeriphResetCmd(RCC_APB2Periph_ADC3, DISABLE);
  163. }
  164. }
  165. }
  166. /**
  167. * @brief Initializes the ADCx peripheral according to the specified parameters
  168. * in the ADC_InitStruct.
  169. * @param ADCx: where x can be 1, 2 or 3 to select the ADC peripheral.
  170. * @param ADC_InitStruct: pointer to an ADC_InitTypeDef structure that contains
  171. * the configuration information for the specified ADC peripheral.
  172. * @retval None
  173. */
  174. void ADC_Init(ADC_TypeDef* ADCx, ADC_InitTypeDef* ADC_InitStruct)
  175. {
  176. uint32_t tmpreg1 = 0;
  177. uint8_t tmpreg2 = 0;
  178. /* Check the parameters */
  179. assert_param(IS_ADC_ALL_PERIPH(ADCx));
  180. assert_param(IS_ADC_MODE(ADC_InitStruct->ADC_Mode));
  181. assert_param(IS_FUNCTIONAL_STATE(ADC_InitStruct->ADC_ScanConvMode));
  182. assert_param(IS_FUNCTIONAL_STATE(ADC_InitStruct->ADC_ContinuousConvMode));
  183. assert_param(IS_ADC_EXT_TRIG(ADC_InitStruct->ADC_ExternalTrigConv));
  184. assert_param(IS_ADC_DATA_ALIGN(ADC_InitStruct->ADC_DataAlign));
  185. assert_param(IS_ADC_REGULAR_LENGTH(ADC_InitStruct->ADC_NbrOfChannel));
  186. /*---------------------------- ADCx CR1 Configuration -----------------*/
  187. /* Get the ADCx CR1 value */
  188. tmpreg1 = ADCx->CR1;
  189. /* Clear DUALMOD and SCAN bits */
  190. tmpreg1 &= CR1_CLEAR_Mask;
  191. /* Configure ADCx: Dual mode and scan conversion mode */
  192. /* Set DUALMOD bits according to ADC_Mode value */
  193. /* Set SCAN bit according to ADC_ScanConvMode value */
  194. tmpreg1 |= (uint32_t)(ADC_InitStruct->ADC_Mode | ((uint32_t)ADC_InitStruct->ADC_ScanConvMode << 8));
  195. /* Write to ADCx CR1 */
  196. ADCx->CR1 = tmpreg1;
  197. /*---------------------------- ADCx CR2 Configuration -----------------*/
  198. /* Get the ADCx CR2 value */
  199. tmpreg1 = ADCx->CR2;
  200. /* Clear CONT, ALIGN and EXTSEL bits */
  201. tmpreg1 &= CR2_CLEAR_Mask;
  202. /* Configure ADCx: external trigger event and continuous conversion mode */
  203. /* Set ALIGN bit according to ADC_DataAlign value */
  204. /* Set EXTSEL bits according to ADC_ExternalTrigConv value */
  205. /* Set CONT bit according to ADC_ContinuousConvMode value */
  206. tmpreg1 |= (uint32_t)(ADC_InitStruct->ADC_DataAlign | ADC_InitStruct->ADC_ExternalTrigConv |
  207. ((uint32_t)ADC_InitStruct->ADC_ContinuousConvMode << 1));
  208. /* Write to ADCx CR2 */
  209. ADCx->CR2 = tmpreg1;
  210. /*---------------------------- ADCx SQR1 Configuration -----------------*/
  211. /* Get the ADCx SQR1 value */
  212. tmpreg1 = ADCx->SQR1;
  213. /* Clear L bits */
  214. tmpreg1 &= SQR1_CLEAR_Mask;
  215. /* Configure ADCx: regular channel sequence length */
  216. /* Set L bits according to ADC_NbrOfChannel value */
  217. tmpreg2 |= (uint8_t) (ADC_InitStruct->ADC_NbrOfChannel - (uint8_t)1);
  218. tmpreg1 |= (uint32_t)tmpreg2 << 20;
  219. /* Write to ADCx SQR1 */
  220. ADCx->SQR1 = tmpreg1;
  221. }
  222. /**
  223. * @brief Fills each ADC_InitStruct member with its default value.
  224. * @param ADC_InitStruct : pointer to an ADC_InitTypeDef structure which will be initialized.
  225. * @retval None
  226. */
  227. void ADC_StructInit(ADC_InitTypeDef* ADC_InitStruct)
  228. {
  229. /* Reset ADC init structure parameters values */
  230. /* Initialize the ADC_Mode member */
  231. ADC_InitStruct->ADC_Mode = ADC_Mode_Independent;
  232. /* initialize the ADC_ScanConvMode member */
  233. ADC_InitStruct->ADC_ScanConvMode = DISABLE;
  234. /* Initialize the ADC_ContinuousConvMode member */
  235. ADC_InitStruct->ADC_ContinuousConvMode = DISABLE;
  236. /* Initialize the ADC_ExternalTrigConv member */
  237. ADC_InitStruct->ADC_ExternalTrigConv = ADC_ExternalTrigConv_T1_CC1;
  238. /* Initialize the ADC_DataAlign member */
  239. ADC_InitStruct->ADC_DataAlign = ADC_DataAlign_Right;
  240. /* Initialize the ADC_NbrOfChannel member */
  241. ADC_InitStruct->ADC_NbrOfChannel = 1;
  242. }
  243. /**
  244. * @brief Enables or disables the specified ADC peripheral.
  245. * @param ADCx: where x can be 1, 2 or 3 to select the ADC peripheral.
  246. * @param NewState: new state of the ADCx peripheral.
  247. * This parameter can be: ENABLE or DISABLE.
  248. * @retval None
  249. */
  250. void ADC_Cmd(ADC_TypeDef* ADCx, FunctionalState NewState)
  251. {
  252. /* Check the parameters */
  253. assert_param(IS_ADC_ALL_PERIPH(ADCx));
  254. assert_param(IS_FUNCTIONAL_STATE(NewState));
  255. if (NewState != DISABLE)
  256. {
  257. /* Set the ADON bit to wake up the ADC from power down mode */
  258. ADCx->CR2 |= CR2_ADON_Set;
  259. }
  260. else
  261. {
  262. /* Disable the selected ADC peripheral */
  263. ADCx->CR2 &= CR2_ADON_Reset;
  264. }
  265. }
  266. /**
  267. * @brief Enables or disables the specified ADC DMA request.
  268. * @param ADCx: where x can be 1 or 3 to select the ADC peripheral.
  269. * Note: ADC2 hasn't a DMA capability.
  270. * @param NewState: new state of the selected ADC DMA transfer.
  271. * This parameter can be: ENABLE or DISABLE.
  272. * @retval None
  273. */
  274. void ADC_DMACmd(ADC_TypeDef* ADCx, FunctionalState NewState)
  275. {
  276. /* Check the parameters */
  277. assert_param(IS_ADC_DMA_PERIPH(ADCx));
  278. assert_param(IS_FUNCTIONAL_STATE(NewState));
  279. if (NewState != DISABLE)
  280. {
  281. /* Enable the selected ADC DMA request */
  282. ADCx->CR2 |= CR2_DMA_Set;
  283. }
  284. else
  285. {
  286. /* Disable the selected ADC DMA request */
  287. ADCx->CR2 &= CR2_DMA_Reset;
  288. }
  289. }
  290. /**
  291. * @brief Enables or disables the specified ADC interrupts.
  292. * @param ADCx: where x can be 1, 2 or 3 to select the ADC peripheral.
  293. * @param ADC_IT: specifies the ADC interrupt sources to be enabled or disabled.
  294. * This parameter can be any combination of the following values:
  295. * @arg ADC_IT_EOC: End of conversion interrupt mask
  296. * @arg ADC_IT_AWD: Analog watchdog interrupt mask
  297. * @arg ADC_IT_JEOC: End of injected conversion interrupt mask
  298. * @param NewState: new state of the specified ADC interrupts.
  299. * This parameter can be: ENABLE or DISABLE.
  300. * @retval None
  301. */
  302. void ADC_ITConfig(ADC_TypeDef* ADCx, uint16_t ADC_IT, FunctionalState NewState)
  303. {
  304. uint8_t itmask = 0;
  305. /* Check the parameters */
  306. assert_param(IS_ADC_ALL_PERIPH(ADCx));
  307. assert_param(IS_FUNCTIONAL_STATE(NewState));
  308. assert_param(IS_ADC_IT(ADC_IT));
  309. /* Get the ADC IT index */
  310. itmask = (uint8_t)ADC_IT;
  311. if (NewState != DISABLE)
  312. {
  313. /* Enable the selected ADC interrupts */
  314. ADCx->CR1 |= itmask;
  315. }
  316. else
  317. {
  318. /* Disable the selected ADC interrupts */
  319. ADCx->CR1 &= (~(uint32_t)itmask);
  320. }
  321. }
  322. /**
  323. * @brief Resets the selected ADC calibration registers.
  324. * @param ADCx: where x can be 1, 2 or 3 to select the ADC peripheral.
  325. * @retval None
  326. */
  327. void ADC_ResetCalibration(ADC_TypeDef* ADCx)
  328. {
  329. /* Check the parameters */
  330. assert_param(IS_ADC_ALL_PERIPH(ADCx));
  331. /* Resets the selected ADC calibration registers */
  332. ADCx->CR2 |= CR2_RSTCAL_Set;
  333. }
  334. /**
  335. * @brief Gets the selected ADC reset calibration registers status.
  336. * @param ADCx: where x can be 1, 2 or 3 to select the ADC peripheral.
  337. * @retval The new state of ADC reset calibration registers (SET or RESET).
  338. */
  339. FlagStatus ADC_GetResetCalibrationStatus(ADC_TypeDef* ADCx)
  340. {
  341. FlagStatus bitstatus = RESET;
  342. /* Check the parameters */
  343. assert_param(IS_ADC_ALL_PERIPH(ADCx));
  344. /* Check the status of RSTCAL bit */
  345. if ((ADCx->CR2 & CR2_RSTCAL_Set) != (uint32_t)RESET)
  346. {
  347. /* RSTCAL bit is set */
  348. bitstatus = SET;
  349. }
  350. else
  351. {
  352. /* RSTCAL bit is reset */
  353. bitstatus = RESET;
  354. }
  355. /* Return the RSTCAL bit status */
  356. return bitstatus;
  357. }
  358. /**
  359. * @brief Starts the selected ADC calibration process.
  360. * @param ADCx: where x can be 1, 2 or 3 to select the ADC peripheral.
  361. * @retval None
  362. */
  363. void ADC_StartCalibration(ADC_TypeDef* ADCx)
  364. {
  365. /* Check the parameters */
  366. assert_param(IS_ADC_ALL_PERIPH(ADCx));
  367. /* Enable the selected ADC calibration process */
  368. ADCx->CR2 |= CR2_CAL_Set;
  369. }
  370. /**
  371. * @brief Gets the selected ADC calibration status.
  372. * @param ADCx: where x can be 1, 2 or 3 to select the ADC peripheral.
  373. * @retval The new state of ADC calibration (SET or RESET).
  374. */
  375. FlagStatus ADC_GetCalibrationStatus(ADC_TypeDef* ADCx)
  376. {
  377. FlagStatus bitstatus = RESET;
  378. /* Check the parameters */
  379. assert_param(IS_ADC_ALL_PERIPH(ADCx));
  380. /* Check the status of CAL bit */
  381. if ((ADCx->CR2 & CR2_CAL_Set) != (uint32_t)RESET)
  382. {
  383. /* CAL bit is set: calibration on going */
  384. bitstatus = SET;
  385. }
  386. else
  387. {
  388. /* CAL bit is reset: end of calibration */
  389. bitstatus = RESET;
  390. }
  391. /* Return the CAL bit status */
  392. return bitstatus;
  393. }
  394. /**
  395. * @brief Enables or disables the selected ADC software start conversion .
  396. * @param ADCx: where x can be 1, 2 or 3 to select the ADC peripheral.
  397. * @param NewState: new state of the selected ADC software start conversion.
  398. * This parameter can be: ENABLE or DISABLE.
  399. * @retval None
  400. */
  401. void ADC_SoftwareStartConvCmd(ADC_TypeDef* ADCx, FunctionalState NewState)
  402. {
  403. /* Check the parameters */
  404. assert_param(IS_ADC_ALL_PERIPH(ADCx));
  405. assert_param(IS_FUNCTIONAL_STATE(NewState));
  406. if (NewState != DISABLE)
  407. {
  408. /* Enable the selected ADC conversion on external event and start the selected
  409. ADC conversion */
  410. ADCx->CR2 |= CR2_EXTTRIG_SWSTART_Set;
  411. }
  412. else
  413. {
  414. /* Disable the selected ADC conversion on external event and stop the selected
  415. ADC conversion */
  416. ADCx->CR2 &= CR2_EXTTRIG_SWSTART_Reset;
  417. }
  418. }
  419. /**
  420. * @brief Gets the selected ADC Software start conversion Status.
  421. * @param ADCx: where x can be 1, 2 or 3 to select the ADC peripheral.
  422. * @retval The new state of ADC software start conversion (SET or RESET).
  423. */
  424. FlagStatus ADC_GetSoftwareStartConvStatus(ADC_TypeDef* ADCx)
  425. {
  426. FlagStatus bitstatus = RESET;
  427. /* Check the parameters */
  428. assert_param(IS_ADC_ALL_PERIPH(ADCx));
  429. /* Check the status of SWSTART bit */
  430. if ((ADCx->CR2 & CR2_SWSTART_Set) != (uint32_t)RESET)
  431. {
  432. /* SWSTART bit is set */
  433. bitstatus = SET;
  434. }
  435. else
  436. {
  437. /* SWSTART bit is reset */
  438. bitstatus = RESET;
  439. }
  440. /* Return the SWSTART bit status */
  441. return bitstatus;
  442. }
  443. /**
  444. * @brief Configures the discontinuous mode for the selected ADC regular
  445. * group channel.
  446. * @param ADCx: where x can be 1, 2 or 3 to select the ADC peripheral.
  447. * @param Number: specifies the discontinuous mode regular channel
  448. * count value. This number must be between 1 and 8.
  449. * @retval None
  450. */
  451. void ADC_DiscModeChannelCountConfig(ADC_TypeDef* ADCx, uint8_t Number)
  452. {
  453. uint32_t tmpreg1 = 0;
  454. uint32_t tmpreg2 = 0;
  455. /* Check the parameters */
  456. assert_param(IS_ADC_ALL_PERIPH(ADCx));
  457. assert_param(IS_ADC_REGULAR_DISC_NUMBER(Number));
  458. /* Get the old register value */
  459. tmpreg1 = ADCx->CR1;
  460. /* Clear the old discontinuous mode channel count */
  461. tmpreg1 &= CR1_DISCNUM_Reset;
  462. /* Set the discontinuous mode channel count */
  463. tmpreg2 = Number - 1;
  464. tmpreg1 |= tmpreg2 << 13;
  465. /* Store the new register value */
  466. ADCx->CR1 = tmpreg1;
  467. }
  468. /**
  469. * @brief Enables or disables the discontinuous mode on regular group
  470. * channel for the specified ADC
  471. * @param ADCx: where x can be 1, 2 or 3 to select the ADC peripheral.
  472. * @param NewState: new state of the selected ADC discontinuous mode
  473. * on regular group channel.
  474. * This parameter can be: ENABLE or DISABLE.
  475. * @retval None
  476. */
  477. void ADC_DiscModeCmd(ADC_TypeDef* ADCx, FunctionalState NewState)
  478. {
  479. /* Check the parameters */
  480. assert_param(IS_ADC_ALL_PERIPH(ADCx));
  481. assert_param(IS_FUNCTIONAL_STATE(NewState));
  482. if (NewState != DISABLE)
  483. {
  484. /* Enable the selected ADC regular discontinuous mode */
  485. ADCx->CR1 |= CR1_DISCEN_Set;
  486. }
  487. else
  488. {
  489. /* Disable the selected ADC regular discontinuous mode */
  490. ADCx->CR1 &= CR1_DISCEN_Reset;
  491. }
  492. }
  493. /**
  494. * @brief Configures for the selected ADC regular channel its corresponding
  495. * rank in the sequencer and its sample time.
  496. * @param ADCx: where x can be 1, 2 or 3 to select the ADC peripheral.
  497. * @param ADC_Channel: the ADC channel to configure.
  498. * This parameter can be one of the following values:
  499. * @arg ADC_Channel_0: ADC Channel0 selected
  500. * @arg ADC_Channel_1: ADC Channel1 selected
  501. * @arg ADC_Channel_2: ADC Channel2 selected
  502. * @arg ADC_Channel_3: ADC Channel3 selected
  503. * @arg ADC_Channel_4: ADC Channel4 selected
  504. * @arg ADC_Channel_5: ADC Channel5 selected
  505. * @arg ADC_Channel_6: ADC Channel6 selected
  506. * @arg ADC_Channel_7: ADC Channel7 selected
  507. * @arg ADC_Channel_8: ADC Channel8 selected
  508. * @arg ADC_Channel_9: ADC Channel9 selected
  509. * @arg ADC_Channel_10: ADC Channel10 selected
  510. * @arg ADC_Channel_11: ADC Channel11 selected
  511. * @arg ADC_Channel_12: ADC Channel12 selected
  512. * @arg ADC_Channel_13: ADC Channel13 selected
  513. * @arg ADC_Channel_14: ADC Channel14 selected
  514. * @arg ADC_Channel_15: ADC Channel15 selected
  515. * @arg ADC_Channel_16: ADC Channel16 selected
  516. * @arg ADC_Channel_17: ADC Channel17 selected
  517. * @param Rank: The rank in the regular group sequencer. This parameter must be between 1 to 16.
  518. * @param ADC_SampleTime: The sample time value to be set for the selected channel.
  519. * This parameter can be one of the following values:
  520. * @arg ADC_SampleTime_1Cycles5: Sample time equal to 1.5 cycles
  521. * @arg ADC_SampleTime_7Cycles5: Sample time equal to 7.5 cycles
  522. * @arg ADC_SampleTime_13Cycles5: Sample time equal to 13.5 cycles
  523. * @arg ADC_SampleTime_28Cycles5: Sample time equal to 28.5 cycles
  524. * @arg ADC_SampleTime_41Cycles5: Sample time equal to 41.5 cycles
  525. * @arg ADC_SampleTime_55Cycles5: Sample time equal to 55.5 cycles
  526. * @arg ADC_SampleTime_71Cycles5: Sample time equal to 71.5 cycles
  527. * @arg ADC_SampleTime_239Cycles5: Sample time equal to 239.5 cycles
  528. * @retval None
  529. */
  530. void ADC_RegularChannelConfig(ADC_TypeDef* ADCx, uint8_t ADC_Channel, uint8_t Rank, uint8_t ADC_SampleTime)
  531. {
  532. uint32_t tmpreg1 = 0, tmpreg2 = 0;
  533. /* Check the parameters */
  534. assert_param(IS_ADC_ALL_PERIPH(ADCx));
  535. assert_param(IS_ADC_CHANNEL(ADC_Channel));
  536. assert_param(IS_ADC_REGULAR_RANK(Rank));
  537. assert_param(IS_ADC_SAMPLE_TIME(ADC_SampleTime));
  538. /* if ADC_Channel_10 ... ADC_Channel_17 is selected */
  539. if (ADC_Channel > ADC_Channel_9)
  540. {
  541. /* Get the old register value */
  542. tmpreg1 = ADCx->SMPR1;
  543. /* Calculate the mask to clear */
  544. tmpreg2 = SMPR1_SMP_Set << (3 * (ADC_Channel - 10));
  545. /* Clear the old channel sample time */
  546. tmpreg1 &= ~tmpreg2;
  547. /* Calculate the mask to set */
  548. tmpreg2 = (uint32_t)ADC_SampleTime << (3 * (ADC_Channel - 10));
  549. /* Set the new channel sample time */
  550. tmpreg1 |= tmpreg2;
  551. /* Store the new register value */
  552. ADCx->SMPR1 = tmpreg1;
  553. }
  554. else /* ADC_Channel include in ADC_Channel_[0..9] */
  555. {
  556. /* Get the old register value */
  557. tmpreg1 = ADCx->SMPR2;
  558. /* Calculate the mask to clear */
  559. tmpreg2 = SMPR2_SMP_Set << (3 * ADC_Channel);
  560. /* Clear the old channel sample time */
  561. tmpreg1 &= ~tmpreg2;
  562. /* Calculate the mask to set */
  563. tmpreg2 = (uint32_t)ADC_SampleTime << (3 * ADC_Channel);
  564. /* Set the new channel sample time */
  565. tmpreg1 |= tmpreg2;
  566. /* Store the new register value */
  567. ADCx->SMPR2 = tmpreg1;
  568. }
  569. /* For Rank 1 to 6 */
  570. if (Rank < 7)
  571. {
  572. /* Get the old register value */
  573. tmpreg1 = ADCx->SQR3;
  574. /* Calculate the mask to clear */
  575. tmpreg2 = SQR3_SQ_Set << (5 * (Rank - 1));
  576. /* Clear the old SQx bits for the selected rank */
  577. tmpreg1 &= ~tmpreg2;
  578. /* Calculate the mask to set */
  579. tmpreg2 = (uint32_t)ADC_Channel << (5 * (Rank - 1));
  580. /* Set the SQx bits for the selected rank */
  581. tmpreg1 |= tmpreg2;
  582. /* Store the new register value */
  583. ADCx->SQR3 = tmpreg1;
  584. }
  585. /* For Rank 7 to 12 */
  586. else if (Rank < 13)
  587. {
  588. /* Get the old register value */
  589. tmpreg1 = ADCx->SQR2;
  590. /* Calculate the mask to clear */
  591. tmpreg2 = SQR2_SQ_Set << (5 * (Rank - 7));
  592. /* Clear the old SQx bits for the selected rank */
  593. tmpreg1 &= ~tmpreg2;
  594. /* Calculate the mask to set */
  595. tmpreg2 = (uint32_t)ADC_Channel << (5 * (Rank - 7));
  596. /* Set the SQx bits for the selected rank */
  597. tmpreg1 |= tmpreg2;
  598. /* Store the new register value */
  599. ADCx->SQR2 = tmpreg1;
  600. }
  601. /* For Rank 13 to 16 */
  602. else
  603. {
  604. /* Get the old register value */
  605. tmpreg1 = ADCx->SQR1;
  606. /* Calculate the mask to clear */
  607. tmpreg2 = SQR1_SQ_Set << (5 * (Rank - 13));
  608. /* Clear the old SQx bits for the selected rank */
  609. tmpreg1 &= ~tmpreg2;
  610. /* Calculate the mask to set */
  611. tmpreg2 = (uint32_t)ADC_Channel << (5 * (Rank - 13));
  612. /* Set the SQx bits for the selected rank */
  613. tmpreg1 |= tmpreg2;
  614. /* Store the new register value */
  615. ADCx->SQR1 = tmpreg1;
  616. }
  617. }
  618. /**
  619. * @brief Enables or disables the ADCx conversion through external trigger.
  620. * @param ADCx: where x can be 1, 2 or 3 to select the ADC peripheral.
  621. * @param NewState: new state of the selected ADC external trigger start of conversion.
  622. * This parameter can be: ENABLE or DISABLE.
  623. * @retval None
  624. */
  625. void ADC_ExternalTrigConvCmd(ADC_TypeDef* ADCx, FunctionalState NewState)
  626. {
  627. /* Check the parameters */
  628. assert_param(IS_ADC_ALL_PERIPH(ADCx));
  629. assert_param(IS_FUNCTIONAL_STATE(NewState));
  630. if (NewState != DISABLE)
  631. {
  632. /* Enable the selected ADC conversion on external event */
  633. ADCx->CR2 |= CR2_EXTTRIG_Set;
  634. }
  635. else
  636. {
  637. /* Disable the selected ADC conversion on external event */
  638. ADCx->CR2 &= CR2_EXTTRIG_Reset;
  639. }
  640. }
  641. /**
  642. * @brief Returns the last ADCx conversion result data for regular channel.
  643. * @param ADCx: where x can be 1, 2 or 3 to select the ADC peripheral.
  644. * @retval The Data conversion value.
  645. */
  646. uint16_t ADC_GetConversionValue(ADC_TypeDef* ADCx)
  647. {
  648. /* Check the parameters */
  649. assert_param(IS_ADC_ALL_PERIPH(ADCx));
  650. /* Return the selected ADC conversion value */
  651. return (uint16_t) ADCx->DR;
  652. }
  653. /**
  654. * @brief Returns the last ADC1 and ADC2 conversion result data in dual mode.
  655. * @retval The Data conversion value.
  656. */
  657. uint32_t ADC_GetDualModeConversionValue(void)
  658. {
  659. /* Return the dual mode conversion value */
  660. return (*(__IO uint32_t *) DR_ADDRESS);
  661. }
  662. /**
  663. * @brief Enables or disables the selected ADC automatic injected group
  664. * conversion after regular one.
  665. * @param ADCx: where x can be 1, 2 or 3 to select the ADC peripheral.
  666. * @param NewState: new state of the selected ADC auto injected conversion
  667. * This parameter can be: ENABLE or DISABLE.
  668. * @retval None
  669. */
  670. void ADC_AutoInjectedConvCmd(ADC_TypeDef* ADCx, FunctionalState NewState)
  671. {
  672. /* Check the parameters */
  673. assert_param(IS_ADC_ALL_PERIPH(ADCx));
  674. assert_param(IS_FUNCTIONAL_STATE(NewState));
  675. if (NewState != DISABLE)
  676. {
  677. /* Enable the selected ADC automatic injected group conversion */
  678. ADCx->CR1 |= CR1_JAUTO_Set;
  679. }
  680. else
  681. {
  682. /* Disable the selected ADC automatic injected group conversion */
  683. ADCx->CR1 &= CR1_JAUTO_Reset;
  684. }
  685. }
  686. /**
  687. * @brief Enables or disables the discontinuous mode for injected group
  688. * channel for the specified ADC
  689. * @param ADCx: where x can be 1, 2 or 3 to select the ADC peripheral.
  690. * @param NewState: new state of the selected ADC discontinuous mode
  691. * on injected group channel.
  692. * This parameter can be: ENABLE or DISABLE.
  693. * @retval None
  694. */
  695. void ADC_InjectedDiscModeCmd(ADC_TypeDef* ADCx, FunctionalState NewState)
  696. {
  697. /* Check the parameters */
  698. assert_param(IS_ADC_ALL_PERIPH(ADCx));
  699. assert_param(IS_FUNCTIONAL_STATE(NewState));
  700. if (NewState != DISABLE)
  701. {
  702. /* Enable the selected ADC injected discontinuous mode */
  703. ADCx->CR1 |= CR1_JDISCEN_Set;
  704. }
  705. else
  706. {
  707. /* Disable the selected ADC injected discontinuous mode */
  708. ADCx->CR1 &= CR1_JDISCEN_Reset;
  709. }
  710. }
  711. /**
  712. * @brief Configures the ADCx external trigger for injected channels conversion.
  713. * @param ADCx: where x can be 1, 2 or 3 to select the ADC peripheral.
  714. * @param ADC_ExternalTrigInjecConv: specifies the ADC trigger to start injected conversion.
  715. * This parameter can be one of the following values:
  716. * @arg ADC_ExternalTrigInjecConv_T1_TRGO: Timer1 TRGO event selected (for ADC1, ADC2 and ADC3)
  717. * @arg ADC_ExternalTrigInjecConv_T1_CC4: Timer1 capture compare4 selected (for ADC1, ADC2 and ADC3)
  718. * @arg ADC_ExternalTrigInjecConv_T2_TRGO: Timer2 TRGO event selected (for ADC1 and ADC2)
  719. * @arg ADC_ExternalTrigInjecConv_T2_CC1: Timer2 capture compare1 selected (for ADC1 and ADC2)
  720. * @arg ADC_ExternalTrigInjecConv_T3_CC4: Timer3 capture compare4 selected (for ADC1 and ADC2)
  721. * @arg ADC_ExternalTrigInjecConv_T4_TRGO: Timer4 TRGO event selected (for ADC1 and ADC2)
  722. * @arg ADC_ExternalTrigInjecConv_Ext_IT15_TIM8_CC4: External interrupt line 15 or Timer8
  723. * capture compare4 event selected (for ADC1 and ADC2)
  724. * @arg ADC_ExternalTrigInjecConv_T4_CC3: Timer4 capture compare3 selected (for ADC3 only)
  725. * @arg ADC_ExternalTrigInjecConv_T8_CC2: Timer8 capture compare2 selected (for ADC3 only)
  726. * @arg ADC_ExternalTrigInjecConv_T8_CC4: Timer8 capture compare4 selected (for ADC3 only)
  727. * @arg ADC_ExternalTrigInjecConv_T5_TRGO: Timer5 TRGO event selected (for ADC3 only)
  728. * @arg ADC_ExternalTrigInjecConv_T5_CC4: Timer5 capture compare4 selected (for ADC3 only)
  729. * @arg ADC_ExternalTrigInjecConv_None: Injected conversion started by software and not
  730. * by external trigger (for ADC1, ADC2 and ADC3)
  731. * @retval None
  732. */
  733. void ADC_ExternalTrigInjectedConvConfig(ADC_TypeDef* ADCx, uint32_t ADC_ExternalTrigInjecConv)
  734. {
  735. uint32_t tmpreg = 0;
  736. /* Check the parameters */
  737. assert_param(IS_ADC_ALL_PERIPH(ADCx));
  738. assert_param(IS_ADC_EXT_INJEC_TRIG(ADC_ExternalTrigInjecConv));
  739. /* Get the old register value */
  740. tmpreg = ADCx->CR2;
  741. /* Clear the old external event selection for injected group */
  742. tmpreg &= CR2_JEXTSEL_Reset;
  743. /* Set the external event selection for injected group */
  744. tmpreg |= ADC_ExternalTrigInjecConv;
  745. /* Store the new register value */
  746. ADCx->CR2 = tmpreg;
  747. }
  748. /**
  749. * @brief Enables or disables the ADCx injected channels conversion through
  750. * external trigger
  751. * @param ADCx: where x can be 1, 2 or 3 to select the ADC peripheral.
  752. * @param NewState: new state of the selected ADC external trigger start of
  753. * injected conversion.
  754. * This parameter can be: ENABLE or DISABLE.
  755. * @retval None
  756. */
  757. void ADC_ExternalTrigInjectedConvCmd(ADC_TypeDef* ADCx, FunctionalState NewState)
  758. {
  759. /* Check the parameters */
  760. assert_param(IS_ADC_ALL_PERIPH(ADCx));
  761. assert_param(IS_FUNCTIONAL_STATE(NewState));
  762. if (NewState != DISABLE)
  763. {
  764. /* Enable the selected ADC external event selection for injected group */
  765. ADCx->CR2 |= CR2_JEXTTRIG_Set;
  766. }
  767. else
  768. {
  769. /* Disable the selected ADC external event selection for injected group */
  770. ADCx->CR2 &= CR2_JEXTTRIG_Reset;
  771. }
  772. }
  773. /**
  774. * @brief Enables or disables the selected ADC start of the injected
  775. * channels conversion.
  776. * @param ADCx: where x can be 1, 2 or 3 to select the ADC peripheral.
  777. * @param NewState: new state of the selected ADC software start injected conversion.
  778. * This parameter can be: ENABLE or DISABLE.
  779. * @retval None
  780. */
  781. void ADC_SoftwareStartInjectedConvCmd(ADC_TypeDef* ADCx, FunctionalState NewState)
  782. {
  783. /* Check the parameters */
  784. assert_param(IS_ADC_ALL_PERIPH(ADCx));
  785. assert_param(IS_FUNCTIONAL_STATE(NewState));
  786. if (NewState != DISABLE)
  787. {
  788. /* Enable the selected ADC conversion for injected group on external event and start the selected
  789. ADC injected conversion */
  790. ADCx->CR2 |= CR2_JEXTTRIG_JSWSTART_Set;
  791. }
  792. else
  793. {
  794. /* Disable the selected ADC conversion on external event for injected group and stop the selected
  795. ADC injected conversion */
  796. ADCx->CR2 &= CR2_JEXTTRIG_JSWSTART_Reset;
  797. }
  798. }
  799. /**
  800. * @brief Gets the selected ADC Software start injected conversion Status.
  801. * @param ADCx: where x can be 1, 2 or 3 to select the ADC peripheral.
  802. * @retval The new state of ADC software start injected conversion (SET or RESET).
  803. */
  804. FlagStatus ADC_GetSoftwareStartInjectedConvCmdStatus(ADC_TypeDef* ADCx)
  805. {
  806. FlagStatus bitstatus = RESET;
  807. /* Check the parameters */
  808. assert_param(IS_ADC_ALL_PERIPH(ADCx));
  809. /* Check the status of JSWSTART bit */
  810. if ((ADCx->CR2 & CR2_JSWSTART_Set) != (uint32_t)RESET)
  811. {
  812. /* JSWSTART bit is set */
  813. bitstatus = SET;
  814. }
  815. else
  816. {
  817. /* JSWSTART bit is reset */
  818. bitstatus = RESET;
  819. }
  820. /* Return the JSWSTART bit status */
  821. return bitstatus;
  822. }
  823. /**
  824. * @brief Configures for the selected ADC injected channel its corresponding
  825. * rank in the sequencer and its sample time.
  826. * @param ADCx: where x can be 1, 2 or 3 to select the ADC peripheral.
  827. * @param ADC_Channel: the ADC channel to configure.
  828. * This parameter can be one of the following values:
  829. * @arg ADC_Channel_0: ADC Channel0 selected
  830. * @arg ADC_Channel_1: ADC Channel1 selected
  831. * @arg ADC_Channel_2: ADC Channel2 selected
  832. * @arg ADC_Channel_3: ADC Channel3 selected
  833. * @arg ADC_Channel_4: ADC Channel4 selected
  834. * @arg ADC_Channel_5: ADC Channel5 selected
  835. * @arg ADC_Channel_6: ADC Channel6 selected
  836. * @arg ADC_Channel_7: ADC Channel7 selected
  837. * @arg ADC_Channel_8: ADC Channel8 selected
  838. * @arg ADC_Channel_9: ADC Channel9 selected
  839. * @arg ADC_Channel_10: ADC Channel10 selected
  840. * @arg ADC_Channel_11: ADC Channel11 selected
  841. * @arg ADC_Channel_12: ADC Channel12 selected
  842. * @arg ADC_Channel_13: ADC Channel13 selected
  843. * @arg ADC_Channel_14: ADC Channel14 selected
  844. * @arg ADC_Channel_15: ADC Channel15 selected
  845. * @arg ADC_Channel_16: ADC Channel16 selected
  846. * @arg ADC_Channel_17: ADC Channel17 selected
  847. * @param Rank: The rank in the injected group sequencer. This parameter must be between 1 and 4.
  848. * @param ADC_SampleTime: The sample time value to be set for the selected channel.
  849. * This parameter can be one of the following values:
  850. * @arg ADC_SampleTime_1Cycles5: Sample time equal to 1.5 cycles
  851. * @arg ADC_SampleTime_7Cycles5: Sample time equal to 7.5 cycles
  852. * @arg ADC_SampleTime_13Cycles5: Sample time equal to 13.5 cycles
  853. * @arg ADC_SampleTime_28Cycles5: Sample time equal to 28.5 cycles
  854. * @arg ADC_SampleTime_41Cycles5: Sample time equal to 41.5 cycles
  855. * @arg ADC_SampleTime_55Cycles5: Sample time equal to 55.5 cycles
  856. * @arg ADC_SampleTime_71Cycles5: Sample time equal to 71.5 cycles
  857. * @arg ADC_SampleTime_239Cycles5: Sample time equal to 239.5 cycles
  858. * @retval None
  859. */
  860. void ADC_InjectedChannelConfig(ADC_TypeDef* ADCx, uint8_t ADC_Channel, uint8_t Rank, uint8_t ADC_SampleTime)
  861. {
  862. uint32_t tmpreg1 = 0, tmpreg2 = 0, tmpreg3 = 0;
  863. /* Check the parameters */
  864. assert_param(IS_ADC_ALL_PERIPH(ADCx));
  865. assert_param(IS_ADC_CHANNEL(ADC_Channel));
  866. assert_param(IS_ADC_INJECTED_RANK(Rank));
  867. assert_param(IS_ADC_SAMPLE_TIME(ADC_SampleTime));
  868. /* if ADC_Channel_10 ... ADC_Channel_17 is selected */
  869. if (ADC_Channel > ADC_Channel_9)
  870. {
  871. /* Get the old register value */
  872. tmpreg1 = ADCx->SMPR1;
  873. /* Calculate the mask to clear */
  874. tmpreg2 = SMPR1_SMP_Set << (3*(ADC_Channel - 10));
  875. /* Clear the old channel sample time */
  876. tmpreg1 &= ~tmpreg2;
  877. /* Calculate the mask to set */
  878. tmpreg2 = (uint32_t)ADC_SampleTime << (3*(ADC_Channel - 10));
  879. /* Set the new channel sample time */
  880. tmpreg1 |= tmpreg2;
  881. /* Store the new register value */
  882. ADCx->SMPR1 = tmpreg1;
  883. }
  884. else /* ADC_Channel include in ADC_Channel_[0..9] */
  885. {
  886. /* Get the old register value */
  887. tmpreg1 = ADCx->SMPR2;
  888. /* Calculate the mask to clear */
  889. tmpreg2 = SMPR2_SMP_Set << (3 * ADC_Channel);
  890. /* Clear the old channel sample time */
  891. tmpreg1 &= ~tmpreg2;
  892. /* Calculate the mask to set */
  893. tmpreg2 = (uint32_t)ADC_SampleTime << (3 * ADC_Channel);
  894. /* Set the new channel sample time */
  895. tmpreg1 |= tmpreg2;
  896. /* Store the new register value */
  897. ADCx->SMPR2 = tmpreg1;
  898. }
  899. /* Rank configuration */
  900. /* Get the old register value */
  901. tmpreg1 = ADCx->JSQR;
  902. /* Get JL value: Number = JL+1 */
  903. tmpreg3 = (tmpreg1 & JSQR_JL_Set)>> 20;
  904. /* Calculate the mask to clear: ((Rank-1)+(4-JL-1)) */
  905. tmpreg2 = JSQR_JSQ_Set << (5 * (uint8_t)((Rank + 3) - (tmpreg3 + 1)));
  906. /* Clear the old JSQx bits for the selected rank */
  907. tmpreg1 &= ~tmpreg2;
  908. /* Calculate the mask to set: ((Rank-1)+(4-JL-1)) */
  909. tmpreg2 = (uint32_t)ADC_Channel << (5 * (uint8_t)((Rank + 3) - (tmpreg3 + 1)));
  910. /* Set the JSQx bits for the selected rank */
  911. tmpreg1 |= tmpreg2;
  912. /* Store the new register value */
  913. ADCx->JSQR = tmpreg1;
  914. }
  915. /**
  916. * @brief Configures the sequencer length for injected channels
  917. * @param ADCx: where x can be 1, 2 or 3 to select the ADC peripheral.
  918. * @param Length: The sequencer length.
  919. * This parameter must be a number between 1 to 4.
  920. * @retval None
  921. */
  922. void ADC_InjectedSequencerLengthConfig(ADC_TypeDef* ADCx, uint8_t Length)
  923. {
  924. uint32_t tmpreg1 = 0;
  925. uint32_t tmpreg2 = 0;
  926. /* Check the parameters */
  927. assert_param(IS_ADC_ALL_PERIPH(ADCx));
  928. assert_param(IS_ADC_INJECTED_LENGTH(Length));
  929. /* Get the old register value */
  930. tmpreg1 = ADCx->JSQR;
  931. /* Clear the old injected sequnence lenght JL bits */
  932. tmpreg1 &= JSQR_JL_Reset;
  933. /* Set the injected sequnence lenght JL bits */
  934. tmpreg2 = Length - 1;
  935. tmpreg1 |= tmpreg2 << 20;
  936. /* Store the new register value */
  937. ADCx->JSQR = tmpreg1;
  938. }
  939. /**
  940. * @brief Set the injected channels conversion value offset
  941. * @param ADCx: where x can be 1, 2 or 3 to select the ADC peripheral.
  942. * @param ADC_InjectedChannel: the ADC injected channel to set its offset.
  943. * This parameter can be one of the following values:
  944. * @arg ADC_InjectedChannel_1: Injected Channel1 selected
  945. * @arg ADC_InjectedChannel_2: Injected Channel2 selected
  946. * @arg ADC_InjectedChannel_3: Injected Channel3 selected
  947. * @arg ADC_InjectedChannel_4: Injected Channel4 selected
  948. * @param Offset: the offset value for the selected ADC injected channel
  949. * This parameter must be a 12bit value.
  950. * @retval None
  951. */
  952. void ADC_SetInjectedOffset(ADC_TypeDef* ADCx, uint8_t ADC_InjectedChannel, uint16_t Offset)
  953. {
  954. __IO uint32_t tmp = 0;
  955. /* Check the parameters */
  956. assert_param(IS_ADC_ALL_PERIPH(ADCx));
  957. assert_param(IS_ADC_INJECTED_CHANNEL(ADC_InjectedChannel));
  958. assert_param(IS_ADC_OFFSET(Offset));
  959. tmp = (uint32_t)ADCx;
  960. tmp += ADC_InjectedChannel;
  961. /* Set the selected injected channel data offset */
  962. *(__IO uint32_t *) tmp = (uint32_t)Offset;
  963. }
  964. /**
  965. * @brief Returns the ADC injected channel conversion result
  966. * @param ADCx: where x can be 1, 2 or 3 to select the ADC peripheral.
  967. * @param ADC_InjectedChannel: the converted ADC injected channel.
  968. * This parameter can be one of the following values:
  969. * @arg ADC_InjectedChannel_1: Injected Channel1 selected
  970. * @arg ADC_InjectedChannel_2: Injected Channel2 selected
  971. * @arg ADC_InjectedChannel_3: Injected Channel3 selected
  972. * @arg ADC_InjectedChannel_4: Injected Channel4 selected
  973. * @retval The Data conversion value.
  974. */
  975. uint16_t ADC_GetInjectedConversionValue(ADC_TypeDef* ADCx, uint8_t ADC_InjectedChannel)
  976. {
  977. __IO uint32_t tmp = 0;
  978. /* Check the parameters */
  979. assert_param(IS_ADC_ALL_PERIPH(ADCx));
  980. assert_param(IS_ADC_INJECTED_CHANNEL(ADC_InjectedChannel));
  981. tmp = (uint32_t)ADCx;
  982. tmp += ADC_InjectedChannel + JDR_Offset;
  983. /* Returns the selected injected channel conversion data value */
  984. return (uint16_t) (*(__IO uint32_t*) tmp);
  985. }
  986. /**
  987. * @brief Enables or disables the analog watchdog on single/all regular
  988. * or injected channels
  989. * @param ADCx: where x can be 1, 2 or 3 to select the ADC peripheral.
  990. * @param ADC_AnalogWatchdog: the ADC analog watchdog configuration.
  991. * This parameter can be one of the following values:
  992. * @arg ADC_AnalogWatchdog_SingleRegEnable: Analog watchdog on a single regular channel
  993. * @arg ADC_AnalogWatchdog_SingleInjecEnable: Analog watchdog on a single injected channel
  994. * @arg ADC_AnalogWatchdog_SingleRegOrInjecEnable: Analog watchdog on a single regular or injected channel
  995. * @arg ADC_AnalogWatchdog_AllRegEnable: Analog watchdog on all regular channel
  996. * @arg ADC_AnalogWatchdog_AllInjecEnable: Analog watchdog on all injected channel
  997. * @arg ADC_AnalogWatchdog_AllRegAllInjecEnable: Analog watchdog on all regular and injected channels
  998. * @arg ADC_AnalogWatchdog_None: No channel guarded by the analog watchdog
  999. * @retval None
  1000. */
  1001. void ADC_AnalogWatchdogCmd(ADC_TypeDef* ADCx, uint32_t ADC_AnalogWatchdog)
  1002. {
  1003. uint32_t tmpreg = 0;
  1004. /* Check the parameters */
  1005. assert_param(IS_ADC_ALL_PERIPH(ADCx));
  1006. assert_param(IS_ADC_ANALOG_WATCHDOG(ADC_AnalogWatchdog));
  1007. /* Get the old register value */
  1008. tmpreg = ADCx->CR1;
  1009. /* Clear AWDEN, AWDENJ and AWDSGL bits */
  1010. tmpreg &= CR1_AWDMode_Reset;
  1011. /* Set the analog watchdog enable mode */
  1012. tmpreg |= ADC_AnalogWatchdog;
  1013. /* Store the new register value */
  1014. ADCx->CR1 = tmpreg;
  1015. }
  1016. /**
  1017. * @brief Configures the high and low thresholds of the analog watchdog.
  1018. * @param ADCx: where x can be 1, 2 or 3 to select the ADC peripheral.
  1019. * @param HighThreshold: the ADC analog watchdog High threshold value.
  1020. * This parameter must be a 12bit value.
  1021. * @param LowThreshold: the ADC analog watchdog Low threshold value.
  1022. * This parameter must be a 12bit value.
  1023. * @retval None
  1024. */
  1025. void ADC_AnalogWatchdogThresholdsConfig(ADC_TypeDef* ADCx, uint16_t HighThreshold,
  1026. uint16_t LowThreshold)
  1027. {
  1028. /* Check the parameters */
  1029. assert_param(IS_ADC_ALL_PERIPH(ADCx));
  1030. assert_param(IS_ADC_THRESHOLD(HighThreshold));
  1031. assert_param(IS_ADC_THRESHOLD(LowThreshold));
  1032. /* Set the ADCx high threshold */
  1033. ADCx->HTR = HighThreshold;
  1034. /* Set the ADCx low threshold */
  1035. ADCx->LTR = LowThreshold;
  1036. }
  1037. /**
  1038. * @brief Configures the analog watchdog guarded single channel
  1039. * @param ADCx: where x can be 1, 2 or 3 to select the ADC peripheral.
  1040. * @param ADC_Channel: the ADC channel to configure for the analog watchdog.
  1041. * This parameter can be one of the following values:
  1042. * @arg ADC_Channel_0: ADC Channel0 selected
  1043. * @arg ADC_Channel_1: ADC Channel1 selected
  1044. * @arg ADC_Channel_2: ADC Channel2 selected
  1045. * @arg ADC_Channel_3: ADC Channel3 selected
  1046. * @arg ADC_Channel_4: ADC Channel4 selected
  1047. * @arg ADC_Channel_5: ADC Channel5 selected
  1048. * @arg ADC_Channel_6: ADC Channel6 selected
  1049. * @arg ADC_Channel_7: ADC Channel7 selected
  1050. * @arg ADC_Channel_8: ADC Channel8 selected
  1051. * @arg ADC_Channel_9: ADC Channel9 selected
  1052. * @arg ADC_Channel_10: ADC Channel10 selected
  1053. * @arg ADC_Channel_11: ADC Channel11 selected
  1054. * @arg ADC_Channel_12: ADC Channel12 selected
  1055. * @arg ADC_Channel_13: ADC Channel13 selected
  1056. * @arg ADC_Channel_14: ADC Channel14 selected
  1057. * @arg ADC_Channel_15: ADC Channel15 selected
  1058. * @arg ADC_Channel_16: ADC Channel16 selected
  1059. * @arg ADC_Channel_17: ADC Channel17 selected
  1060. * @retval None
  1061. */
  1062. void ADC_AnalogWatchdogSingleChannelConfig(ADC_TypeDef* ADCx, uint8_t ADC_Channel)
  1063. {
  1064. uint32_t tmpreg = 0;
  1065. /* Check the parameters */
  1066. assert_param(IS_ADC_ALL_PERIPH(ADCx));
  1067. assert_param(IS_ADC_CHANNEL(ADC_Channel));
  1068. /* Get the old register value */
  1069. tmpreg = ADCx->CR1;
  1070. /* Clear the Analog watchdog channel select bits */
  1071. tmpreg &= CR1_AWDCH_Reset;
  1072. /* Set the Analog watchdog channel */
  1073. tmpreg |= ADC_Channel;
  1074. /* Store the new register value */
  1075. ADCx->CR1 = tmpreg;
  1076. }
  1077. /**
  1078. * @brief Enables or disables the temperature sensor and Vrefint channel.
  1079. * @param NewState: new state of the temperature sensor.
  1080. * This parameter can be: ENABLE or DISABLE.
  1081. * @retval None
  1082. */
  1083. void ADC_TempSensorVrefintCmd(FunctionalState NewState)
  1084. {
  1085. /* Check the parameters */
  1086. assert_param(IS_FUNCTIONAL_STATE(NewState));
  1087. if (NewState != DISABLE)
  1088. {
  1089. /* Enable the temperature sensor and Vrefint channel*/
  1090. ADC1->CR2 |= CR2_TSVREFE_Set;
  1091. }
  1092. else
  1093. {
  1094. /* Disable the temperature sensor and Vrefint channel*/
  1095. ADC1->CR2 &= CR2_TSVREFE_Reset;
  1096. }
  1097. }
  1098. /**
  1099. * @brief Checks whether the specified ADC flag is set or not.
  1100. * @param ADCx: where x can be 1, 2 or 3 to select the ADC peripheral.
  1101. * @param ADC_FLAG: specifies the flag to check.
  1102. * This parameter can be one of the following values:
  1103. * @arg ADC_FLAG_AWD: Analog watchdog flag
  1104. * @arg ADC_FLAG_EOC: End of conversion flag
  1105. * @arg ADC_FLAG_JEOC: End of injected group conversion flag
  1106. * @arg ADC_FLAG_JSTRT: Start of injected group conversion flag
  1107. * @arg ADC_FLAG_STRT: Start of regular group conversion flag
  1108. * @retval The new state of ADC_FLAG (SET or RESET).
  1109. */
  1110. FlagStatus ADC_GetFlagStatus(ADC_TypeDef* ADCx, uint8_t ADC_FLAG)
  1111. {
  1112. FlagStatus bitstatus = RESET;
  1113. /* Check the parameters */
  1114. assert_param(IS_ADC_ALL_PERIPH(ADCx));
  1115. assert_param(IS_ADC_GET_FLAG(ADC_FLAG));
  1116. /* Check the status of the specified ADC flag */
  1117. if ((ADCx->SR & ADC_FLAG) != (uint8_t)RESET)
  1118. {
  1119. /* ADC_FLAG is set */
  1120. bitstatus = SET;
  1121. }
  1122. else
  1123. {
  1124. /* ADC_FLAG is reset */
  1125. bitstatus = RESET;
  1126. }
  1127. /* Return the ADC_FLAG status */
  1128. return bitstatus;
  1129. }
  1130. /**
  1131. * @brief Clears the ADCx's pending flags.
  1132. * @param ADCx: where x can be 1, 2 or 3 to select the ADC peripheral.
  1133. * @param ADC_FLAG: specifies the flag to clear.
  1134. * This parameter can be any combination of the following values:
  1135. * @arg ADC_FLAG_AWD: Analog watchdog flag
  1136. * @arg ADC_FLAG_EOC: End of conversion flag
  1137. * @arg ADC_FLAG_JEOC: End of injected group conversion flag
  1138. * @arg ADC_FLAG_JSTRT: Start of injected group conversion flag
  1139. * @arg ADC_FLAG_STRT: Start of regular group conversion flag
  1140. * @retval None
  1141. */
  1142. void ADC_ClearFlag(ADC_TypeDef* ADCx, uint8_t ADC_FLAG)
  1143. {
  1144. /* Check the parameters */
  1145. assert_param(IS_ADC_ALL_PERIPH(ADCx));
  1146. assert_param(IS_ADC_CLEAR_FLAG(ADC_FLAG));
  1147. /* Clear the selected ADC flags */
  1148. ADCx->SR = ~(uint32_t)ADC_FLAG;
  1149. }
  1150. /**
  1151. * @brief Checks whether the specified ADC interrupt has occurred or not.
  1152. * @param ADCx: where x can be 1, 2 or 3 to select the ADC peripheral.
  1153. * @param ADC_IT: specifies the ADC interrupt source to check.
  1154. * This parameter can be one of the following values:
  1155. * @arg ADC_IT_EOC: End of conversion interrupt mask
  1156. * @arg ADC_IT_AWD: Analog watchdog interrupt mask
  1157. * @arg ADC_IT_JEOC: End of injected conversion interrupt mask
  1158. * @retval The new state of ADC_IT (SET or RESET).
  1159. */
  1160. ITStatus ADC_GetITStatus(ADC_TypeDef* ADCx, uint16_t ADC_IT)
  1161. {
  1162. ITStatus bitstatus = RESET;
  1163. uint32_t itmask = 0, enablestatus = 0;
  1164. /* Check the parameters */
  1165. assert_param(IS_ADC_ALL_PERIPH(ADCx));
  1166. assert_param(IS_ADC_GET_IT(ADC_IT));
  1167. /* Get the ADC IT index */
  1168. itmask = ADC_IT >> 8;
  1169. /* Get the ADC_IT enable bit status */
  1170. enablestatus = (ADCx->CR1 & (uint8_t)ADC_IT) ;
  1171. /* Check the status of the specified ADC interrupt */
  1172. if (((ADCx->SR & itmask) != (uint32_t)RESET) && enablestatus)
  1173. {
  1174. /* ADC_IT is set */
  1175. bitstatus = SET;
  1176. }
  1177. else
  1178. {
  1179. /* ADC_IT is reset */
  1180. bitstatus = RESET;
  1181. }
  1182. /* Return the ADC_IT status */
  1183. return bitstatus;
  1184. }
  1185. /**
  1186. * @brief Clears the ADCx's interrupt pending bits.
  1187. * @param ADCx: where x can be 1, 2 or 3 to select the ADC peripheral.
  1188. * @param ADC_IT: specifies the ADC interrupt pending bit to clear.
  1189. * This parameter can be any combination of the following values:
  1190. * @arg ADC_IT_EOC: End of conversion interrupt mask
  1191. * @arg ADC_IT_AWD: Analog watchdog interrupt mask
  1192. * @arg ADC_IT_JEOC: End of injected conversion interrupt mask
  1193. * @retval None
  1194. */
  1195. void ADC_ClearITPendingBit(ADC_TypeDef* ADCx, uint16_t ADC_IT)
  1196. {
  1197. uint8_t itmask = 0;
  1198. /* Check the parameters */
  1199. assert_param(IS_ADC_ALL_PERIPH(ADCx));
  1200. assert_param(IS_ADC_IT(ADC_IT));
  1201. /* Get the ADC IT index */
  1202. itmask = (uint8_t)(ADC_IT >> 8);
  1203. /* Clear the selected ADC interrupt pending bits */
  1204. ADCx->SR = ~(uint32_t)itmask;
  1205. }
  1206. /**
  1207. * @}
  1208. */
  1209. /**
  1210. * @}
  1211. */
  1212. /**
  1213. * @}
  1214. */
  1215. /******************* (C) COPYRIGHT 2011 STMicroelectronics *****END OF FILE****/