stm32f10x_adc.c 46 KB

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