stm8l15x_adc.c 40 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988
  1. /**
  2. ******************************************************************************
  3. * @file stm8l15x_adc.c
  4. * @author MCD Application Team
  5. * @version V1.6.1
  6. * @date 30-September-2014
  7. * @brief This file provides firmware functions to manage the following
  8. * functionalities of the Analog to Digital Convertor (ADC) peripheral:
  9. * - Initialization and Configuration
  10. * - Power saving
  11. * - Analog Watchdog configuration
  12. * - Temperature Sensor & Vrefint (Voltage Reference internal) management
  13. * - Regular Channels Configuration
  14. * - Regular Channels DMA Configuration
  15. * - Injected channels Configuration
  16. * - Interrupts and flags management
  17. *
  18. * @verbatim
  19. *
  20. * ===================================================================
  21. * How to use this driver
  22. * ===================================================================
  23. * 1. Enable The ADC controller clock using CLK_PeripheralClockConfig()
  24. * function : CLK_PeripheralClockConfig(CLK_Peripheral_ADC1, ENABLE).
  25. *
  26. * 2. Configure the ADC Prescaler, conversion resolution and data
  27. * alignment using the ADC_Init() function.
  28. *
  29. * 3. Activate the ADC peripheral using ADC_Cmd() function.
  30. *
  31. * Regular channels group configuration
  32. * ====================================
  33. * - To configure the ADC regular channels group features, use
  34. * ADC_Init() and ADC_RegularChannelConfig() functions.
  35. * - To activate the continuous mode, use the ADC_continuousModeCmd()
  36. * function.
  37. * - To configure and activate the Discontinuous mode, use the
  38. * ADC_DiscModeChannelCountConfig() and ADC_DiscModeCmd() functions.
  39. * - To read the ADC converted values, use the ADC_GetConversionValue()
  40. * function.
  41. *
  42. * DMA for Regular channels group features configuration
  43. * ======================================================
  44. * - To enable the DMA mode for regular channels group, use the
  45. * ADC_DMACmd() function.
  46. * - To enable the generation of DMA requests continuously at the end
  47. * of the last DMA transfer, use the ADC_DMARequestAfterLastTransferCmd()
  48. * function.
  49. * Injected channels group configuration
  50. * =====================================
  51. * - To configure the ADC Injected channels group features, use
  52. * ADC_InjectedChannelConfig() and ADC_InjectedSequencerLengthConfig()
  53. * functions.
  54. * - To activate the continuous mode, use the ADC_continuousModeCmd()
  55. * function.
  56. * - To activate the Injected Discontinuous mode, use the
  57. * ADC_InjectedDiscModeCmd() function.
  58. * - To activate the AutoInjected mode, use the ADC_AutoInjectedConvCmd()
  59. * function.
  60. * - To read the ADC converted values, use the ADC_GetInjectedConversionValue()
  61. * function.
  62. *
  63. * @endverbatim
  64. *
  65. ******************************************************************************
  66. * @attention
  67. *
  68. * <h2><center>&copy; COPYRIGHT 2014 STMicroelectronics</center></h2>
  69. *
  70. * Licensed under MCD-ST Liberty SW License Agreement V2, (the "License");
  71. * You may not use this file except in compliance with the License.
  72. * You may obtain a copy of the License at:
  73. *
  74. * http://www.st.com/software_license_agreement_liberty_v2
  75. *
  76. * Unless required by applicable law or agreed to in writing, software
  77. * distributed under the License is distributed on an "AS IS" BASIS,
  78. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  79. * See the License for the specific language governing permissions and
  80. * limitations under the License.
  81. *
  82. ******************************************************************************
  83. */
  84. /* Includes ------------------------------------------------------------------*/
  85. #include "stm8l15x_adc.h"
  86. /** @addtogroup STM8L15x_StdPeriph_Driver
  87. * @{
  88. */
  89. /** @defgroup ADC
  90. * @brief ADC driver modules
  91. * @{
  92. */
  93. /* Private typedef -----------------------------------------------------------*/
  94. /* Private define ------------------------------------------------------------*/
  95. /* Private macro -------------------------------------------------------------*/
  96. /* Private variables ---------------------------------------------------------*/
  97. /* Private function prototypes -----------------------------------------------*/
  98. /* Private functions ---------------------------------------------------------*/
  99. /** @defgroup ADC_Private_Functions
  100. * @{
  101. */
  102. /** @defgroup ADC_Group1 Initialization and Configuration functions
  103. * @brief Initialization and Configuration functions
  104. *
  105. @verbatim
  106. ===============================================================================
  107. Initialization and Configuration functions
  108. ===============================================================================
  109. This section provides functions allowing to:
  110. - Enable or disable the ADC peripheral,
  111. - Initialize and configure the ADC Prescaler, ADC Conversion Resolution
  112. (12bit..6bit), ADC Continuous Conversion Mode (Continuous or Single
  113. conversion),
  114. - Configure External trigger Sensitivity and source,
  115. - Start ADC conversion, by software trigger.
  116. @endverbatim
  117. * @{
  118. */
  119. /**
  120. * @brief Deinitializes the ADC peripheral registers to their default reset
  121. * values.
  122. * @param ADCx where x can be 1 to select the specified ADC peripheral.
  123. * @retval None
  124. */
  125. void ADC_DeInit(ADC_TypeDef* ADCx)
  126. {
  127. /* Set the Configuration registers to their reset values */
  128. ADCx->CR1 = ADC_CR1_RESET_VALUE;
  129. ADCx->CR2 = ADC_CR2_RESET_VALUE;
  130. ADCx->CR3 = ADC_CR3_RESET_VALUE;
  131. /* Set the status registers to their reset values */
  132. ADCx->SR = (uint8_t)~ADC_SR_RESET_VALUE;
  133. /* Set the High threshold registers to their reset values */
  134. ADCx->HTRH = ADC_HTRH_RESET_VALUE;
  135. ADCx->HTRL = ADC_HTRL_RESET_VALUE;
  136. /* Set the low threshold registers to their reset values */
  137. ADCx->LTRH = ADC_LTRH_RESET_VALUE;
  138. ADCx->LTRL = ADC_LTRL_RESET_VALUE;
  139. /* Set the channels sequence registers to their reset values */
  140. ADCx->SQR[0] = ADC_SQR1_RESET_VALUE;
  141. ADCx->SQR[1] = ADC_SQR2_RESET_VALUE;
  142. ADCx->SQR[2] = ADC_SQR3_RESET_VALUE;
  143. ADCx->SQR[3] = ADC_SQR4_RESET_VALUE;
  144. /* Set the channels Trigger registers to their reset values */
  145. ADCx->TRIGR[0] = ADC_TRIGR1_RESET_VALUE;
  146. ADCx->TRIGR[1] = ADC_TRIGR2_RESET_VALUE;
  147. ADCx->TRIGR[2] = ADC_TRIGR3_RESET_VALUE;
  148. ADCx->TRIGR[3] = ADC_TRIGR4_RESET_VALUE;
  149. }
  150. /**
  151. * @brief Initializes the specified ADC peripheral according to the specified
  152. * parameters.
  153. * @param ADCx where x can be 1 to select the specified ADC peripheral.
  154. * @param ADC_ConversionMode : specifies the ADC conversion mode,
  155. * This parameter can be one of the following values:
  156. * @arg ADC_ConversionMode_Single: Single Conversion Mode
  157. * @arg ADC_ConversionMode_Continuous: Continuous Conversion Mode
  158. * @param ADC_Resolution : specifies the ADC Data resolution,
  159. * This parameter can be one of the following values:
  160. * @arg ADC_Resolution_12Bit: 12 bit resolution
  161. * @arg ADC_Resolution_10Bit: 10 bit resolution
  162. * @arg ADC_Resolution_8Bit: 8 bit resolution
  163. * @arg ADC_Resolution_6Bit: 6 bit resolution
  164. * @param ADC_Prescaler : specifies the ADC Prescaler,
  165. * This parameter can be one of the following values:
  166. * @arg ADC_Prescaler_1: ADC Clock frequency is divided by 1
  167. * @arg ADC_Prescaler_2: ADC Clock frequency is divided by 2
  168. * @retval None
  169. */
  170. void ADC_Init(ADC_TypeDef* ADCx,
  171. ADC_ConversionMode_TypeDef ADC_ConversionMode,
  172. ADC_Resolution_TypeDef ADC_Resolution,
  173. ADC_Prescaler_TypeDef ADC_Prescaler)
  174. {
  175. /* Check the parameters */
  176. assert_param(IS_ADC_CONVERSION_MODE(ADC_ConversionMode));
  177. assert_param(IS_ADC_RESOLUTION(ADC_Resolution));
  178. assert_param(IS_ADC_PRESCALER(ADC_Prescaler));
  179. /*clear CR1 register */
  180. ADCx->CR1 &= (uint8_t)~(ADC_CR1_CONT | ADC_CR1_RES);
  181. /* set the resolution and the conversion mode */
  182. ADCx->CR1 |= (uint8_t)((uint8_t)ADC_ConversionMode | (uint8_t)ADC_Resolution);
  183. /*clear CR2 register */
  184. ADCx->CR2 &= (uint8_t)~(ADC_CR2_PRESC);
  185. /* set the Prescaler */
  186. ADCx->CR2 |= (uint8_t) ADC_Prescaler;
  187. }
  188. /**
  189. * @brief Enables or disables the selected ADC peripheral.
  190. * @param ADCx where x can be 1 to select the specified ADC peripheral.
  191. * @param NewState : new state of the specified ADC peripheral.
  192. * This parameter can be: ENABLE or DISABLE.
  193. * @retval None
  194. */
  195. void ADC_Cmd(ADC_TypeDef* ADCx,
  196. FunctionalState NewState)
  197. {
  198. /* Check the parameters */
  199. assert_param(IS_FUNCTIONAL_STATE(NewState));
  200. if (NewState != DISABLE)
  201. {
  202. /* Set the ADON bit to wake up the specified ADC from power down mode */
  203. ADCx->CR1 |= ADC_CR1_ADON;
  204. }
  205. else
  206. {
  207. /* Disable the selected ADC peripheral */
  208. ADCx->CR1 &= (uint8_t)~ADC_CR1_ADON;
  209. }
  210. }
  211. /**
  212. * @brief Starts ADC conversion, by software trigger.
  213. * @param ADCx where x can be 1 to select the specified ADC peripheral.
  214. * @retval None
  215. */
  216. void ADC_SoftwareStartConv(ADC_TypeDef* ADCx)
  217. {
  218. /* Start the ADC software conversion */
  219. ADCx->CR1 |= ADC_CR1_START;
  220. }
  221. /**
  222. * @brief Configures the ADC conversion through external trigger.
  223. * @param ADCx where x can be 1 to select the specified ADC peripheral.
  224. * @param ADC_ExtEventSelection : Specifies the external trigger.
  225. * This parameter can be one of the following values:
  226. * @arg ADC_ExtEventSelection_None: Conversion starts only by software start
  227. * @arg ADC_ExtEventSelection_Trigger1: Trigger 1 Enables conversion
  228. * @arg ADC_ExtEventSelection_Trigger2: Trigger 2 Enables conversion
  229. * @arg ADC_ExtEventSelection_Trigger3: Trigger 3 Enables conversion
  230. * @param ADC_ExtTRGSensitivity : Specifies the external trigger sensitivity.
  231. * This parameter can be one of the following values:
  232. * @arg ADC_ExtTRGSensitivity_Rising: External Trigger Sensitivity is Rising Edge
  233. * @arg ADC_ExtTRGSensitivity_Falling: External Trigger Sensitivity is Falling Edge
  234. * @arg ADC_ExtTRGSensitivity_All: External Trigger Sensitivity is Falling and Rising Edge
  235. * @retval None
  236. */
  237. void ADC_ExternalTrigConfig(ADC_TypeDef* ADCx,
  238. ADC_ExtEventSelection_TypeDef ADC_ExtEventSelection,
  239. ADC_ExtTRGSensitivity_TypeDef ADC_ExtTRGSensitivity)
  240. {
  241. /* Check the parameters */
  242. assert_param(IS_ADC_EXT_EVENT_SELECTION(ADC_ExtEventSelection));
  243. assert_param(IS_ADC_EXT_TRG_SENSITIVITY(ADC_ExtTRGSensitivity));
  244. /*clear old configuration of CR2 register */
  245. ADCx->CR2 &= (uint8_t)~(ADC_CR2_TRIGEDGE | ADC_CR2_EXTSEL);
  246. /* set the External Trigger Edge Sensitivity and the external event
  247. selection */
  248. ADCx->CR2 |= (uint8_t)( (uint8_t)ADC_ExtTRGSensitivity | \
  249. (uint8_t)ADC_ExtEventSelection);
  250. }
  251. /**
  252. * @}
  253. */
  254. /** @defgroup ADC_Group2 Analog Watchdog configuration functions
  255. * @brief Analog Watchdog configuration functions
  256. *
  257. @verbatim
  258. ===============================================================================
  259. Analog Watchdog configuration functions
  260. ===============================================================================
  261. This section provides functions allowing to configure the Analog Watchdog
  262. (AWD) feature in the ADC.
  263. A typical configuration Analog Watchdog is done following these steps :
  264. 1. the ADC guarded channel is selected using the
  265. ADC_AnalogWatchdogChannelSelect() function.
  266. 2. The Analog watchdog lower and higher threshold are configured using the
  267. ADC_AnalogWatchdogThresholdsConfig() function.
  268. Note : Both AWD selection and thresholds can be configured with one unique
  269. function ADC_AnalogWatchdogConfig(), which is kept for firmware
  270. compatibility reason.
  271. @endverbatim
  272. * @{
  273. */
  274. /**
  275. * @brief Configures the channel to be checked by the Analog watchdog.
  276. * @param ADCx where x can be 1 to select the specified ADC peripheral.
  277. * @param ADC_AnalogWatchdogSelection : Specifies the channel to be checked
  278. * by the Analog watchdog.
  279. * This parameter can be one of the following values:
  280. * @arg ADC_AnalogWatchdogSelection_Channel0: AWD affected to Channel 0
  281. * @arg ADC_AnalogWatchdogSelection_Channel1: AWD affected to Channel 1
  282. * @arg ADC_AnalogWatchdogSelection_Channel2: AWD affected to Channel 2
  283. * @arg ADC_AnalogWatchdogSelection_Channel3: AWD affected to Channel 3
  284. * @arg ADC_AnalogWatchdogSelection_Channel4: AWD affected to Channel 4
  285. * @arg ADC_AnalogWatchdogSelection_Channel5: AWD affected to Channel 5
  286. * @arg ADC_AnalogWatchdogSelection_Channel6: AWD affected to Channel 6
  287. * @arg ADC_AnalogWatchdogSelection_Channel7: AWD affected to Channel 7
  288. * @arg ADC_AnalogWatchdogSelection_Channel8: AWD affected to Channel 8
  289. * @arg ADC_AnalogWatchdogSelection_Channel9: AWD affected to Channel 9
  290. * @arg ADC_AnalogWatchdogSelection_Channel10: AWD affected to Channel 10
  291. * @arg ADC_AnalogWatchdogSelection_Channel11: AWD affected to Channel 11
  292. * @arg ADC_AnalogWatchdogSelection_Channel12: AWD affected to Channel 12
  293. * @arg ADC_AnalogWatchdogSelection_Channel13: AWD affected to Channel 13
  294. * @arg ADC_AnalogWatchdogSelection_Channel14: AWD affected to Channel 14
  295. * @arg ADC_AnalogWatchdogSelection_Channel15: AWD affected to Channel 15
  296. * @arg ADC_AnalogWatchdogSelection_Channel16: AWD affected to Channel 16
  297. * @arg ADC_AnalogWatchdogSelection_Channel17: AWD affected to Channel 17
  298. * @arg ADC_AnalogWatchdogSelection_Channel18: AWD affected to Channel 18
  299. * @arg ADC_AnalogWatchdogSelection_Channel19: AWD affected to Channel 19
  300. * @arg ADC_AnalogWatchdogSelection_Channel20: AWD affected to Channel 20
  301. * @arg ADC_AnalogWatchdogSelection_Channel21: AWD affected to Channel 21
  302. * @arg ADC_AnalogWatchdogSelection_Channel22: AWD affected to Channel 22
  303. * @arg ADC_AnalogWatchdogSelection_Channel23: AWD affected to Channel 23
  304. * @ref ADC_AnalogWatchdogSelection_TypeDef enumeration.
  305. * @retval None
  306. */
  307. void ADC_AnalogWatchdogChannelSelect(ADC_TypeDef* ADCx,
  308. ADC_AnalogWatchdogSelection_TypeDef ADC_AnalogWatchdogSelection)
  309. {
  310. /* Check the parameters */
  311. assert_param(IS_ADC_ANALOGWATCHDOG_SELECTION(ADC_AnalogWatchdogSelection));
  312. /* Reset the CHSEL bits */
  313. ADCx->CR3 &= ((uint8_t)~ADC_CR3_CHSEL);
  314. /* Select the channel to be checked by the Analog watchdog */
  315. ADCx->CR3 |= (uint8_t)ADC_AnalogWatchdogSelection;
  316. }
  317. /**
  318. * @brief Configures the high and low thresholds of the Analog watchdog.
  319. * @param ADCx where x can be 1 to select the specified ADC peripheral.
  320. * @param HighThreshold: Analog watchdog High threshold value.
  321. * This parameter must be a 12bit value.
  322. * @param LowThreshold: Analog watchdog Low threshold value.
  323. * This parameter must be a 12bit value.
  324. * @retval None
  325. */
  326. void ADC_AnalogWatchdogThresholdsConfig(ADC_TypeDef* ADCx, uint16_t HighThreshold, uint16_t LowThreshold)
  327. {
  328. /* Check the parameters */
  329. assert_param(IS_ADC_THRESHOLD(HighThreshold));
  330. assert_param(IS_ADC_THRESHOLD(LowThreshold));
  331. /* Set the ADC high threshold */
  332. ADCx->HTRH = (uint8_t)(HighThreshold >> 8);
  333. ADCx->HTRL = (uint8_t)(HighThreshold);
  334. /* Set the ADC low threshold */
  335. ADCx->LTRH = (uint8_t)(LowThreshold >> 8);
  336. ADCx->LTRL = (uint8_t)(LowThreshold);
  337. }
  338. /**
  339. * @brief Configures the Analog watchdog.
  340. * @param ADCx where x can be 1 to select the specified ADC peripheral.
  341. * @param ADC_AnalogWatchdogSelection : Specifies the channel to be checked
  342. * by the Analog watchdog.
  343. * This parameter can be one of the following values:
  344. * @arg ADC_AnalogWatchdogSelection_Channel0: AWD affected to Channel 0
  345. * @arg ADC_AnalogWatchdogSelection_Channel1: AWD affected to Channel 1
  346. * @arg ADC_AnalogWatchdogSelection_Channel2: AWD affected to Channel 2
  347. * @arg ADC_AnalogWatchdogSelection_Channel3: AWD affected to Channel 3
  348. * @arg ADC_AnalogWatchdogSelection_Channel4: AWD affected to Channel 4
  349. * @arg ADC_AnalogWatchdogSelection_Channel5: AWD affected to Channel 5
  350. * @arg ADC_AnalogWatchdogSelection_Channel6: AWD affected to Channel 6
  351. * @arg ADC_AnalogWatchdogSelection_Channel7: AWD affected to Channel 7
  352. * @arg ADC_AnalogWatchdogSelection_Channel8: AWD affected to Channel 8
  353. * @arg ADC_AnalogWatchdogSelection_Channel9: AWD affected to Channel 9
  354. * @arg ADC_AnalogWatchdogSelection_Channel10: AWD affected to Channel 10
  355. * @arg ADC_AnalogWatchdogSelection_Channel11: AWD affected to Channel 11
  356. * @arg ADC_AnalogWatchdogSelection_Channel12: AWD affected to Channel 12
  357. * @arg ADC_AnalogWatchdogSelection_Channel13: AWD affected to Channel 13
  358. * @arg ADC_AnalogWatchdogSelection_Channel14: AWD affected to Channel 14
  359. * @arg ADC_AnalogWatchdogSelection_Channel15: AWD affected to Channel 15
  360. * @arg ADC_AnalogWatchdogSelection_Channel16: AWD affected to Channel 16
  361. * @arg ADC_AnalogWatchdogSelection_Channel17: AWD affected to Channel 17
  362. * @arg ADC_AnalogWatchdogSelection_Channel18: AWD affected to Channel 18
  363. * @arg ADC_AnalogWatchdogSelection_Channel19: AWD affected to Channel 19
  364. * @arg ADC_AnalogWatchdogSelection_Channel20: AWD affected to Channel 20
  365. * @arg ADC_AnalogWatchdogSelection_Channel21: AWD affected to Channel 21
  366. * @arg ADC_AnalogWatchdogSelection_Channel22: AWD affected to Channel 22
  367. * @arg ADC_AnalogWatchdogSelection_Channel23: AWD affected to Channel 23
  368. * @param HighThreshold: Analog watchdog High threshold value.
  369. * This parameter must be a 12bit value.
  370. * @param LowThreshold: Analog watchdog Low threshold value.
  371. * This parameter must be a 12bit value.
  372. * @retval None
  373. */
  374. void ADC_AnalogWatchdogConfig(ADC_TypeDef* ADCx,
  375. ADC_AnalogWatchdogSelection_TypeDef ADC_AnalogWatchdogSelection,
  376. uint16_t HighThreshold,
  377. uint16_t LowThreshold)
  378. {
  379. /* Check the parameters */
  380. assert_param(IS_ADC_ANALOGWATCHDOG_SELECTION(ADC_AnalogWatchdogSelection));
  381. assert_param(IS_ADC_THRESHOLD(HighThreshold));
  382. assert_param(IS_ADC_THRESHOLD(LowThreshold));
  383. /*Reset the CHSEL bits */
  384. ADCx->CR3 &= ((uint8_t)~ADC_CR3_CHSEL);
  385. /* Select the channel to be checked by the Analog watchdog.*/
  386. ADCx->CR3 |= (uint8_t)ADC_AnalogWatchdogSelection;
  387. /* Set the ADC high threshold */
  388. ADCx->HTRH = (uint8_t)(HighThreshold >> 8);
  389. ADCx->HTRL = (uint8_t)(HighThreshold);
  390. /* Set the ADC low threshold */
  391. ADCx->LTRH = (uint8_t)(LowThreshold >> 8);
  392. ADCx->LTRL = (uint8_t)LowThreshold;
  393. }
  394. /**
  395. * @}
  396. */
  397. /** @defgroup ADC_Group3 Temperature Sensor & Vrefint (Voltage Reference
  398. * internal) management functions
  399. * @brief Temperature Sensor & Vrefint (Voltage Reference internal)
  400. * management functions
  401. *
  402. @verbatim
  403. ===============================================================================
  404. Temperature Sensor & Vrefint (Voltage Reference internal) management functions
  405. ===============================================================================
  406. This section provides functions allowing to enable/ disable the internal
  407. connections between the ADC and the Temperature Sensor and the Vrefint source.
  408. A typical configuration to get the Temperature sensor or/and Vrefint channels
  409. voltages is done following these steps :
  410. 1. Enable the internal connection of Temperature sensor or/and Vrefint sources
  411. with the ADC channels:
  412. - for the Temperature sensor using ADC_TempSensorCmd() function.
  413. - for the Internal Voltage reference using ADC_VrefintCmd() function.
  414. 2. Enable the ADC_Channel_TempSensor and/or ADC_Channel_Vrefint channels
  415. using ADC_ChannelCmd()function.
  416. 3. Get the voltage values, using ADC_GetConversionValue().
  417. @endverbatim
  418. * @{
  419. */
  420. /**
  421. * @brief Enables or disables the Temperature sensor internal reference.
  422. * @param NewState : new state of the Temperature sensor internal reference.
  423. * This parameter can be: ENABLE or DISABLE.
  424. * @retval None
  425. */
  426. void ADC_TempSensorCmd(FunctionalState NewState)
  427. {
  428. /* Check the parameters */
  429. assert_param(IS_FUNCTIONAL_STATE(NewState));
  430. if (NewState != DISABLE)
  431. {
  432. /*Enable the Temperature sensor internal reference.*/
  433. ADC1->TRIGR[0] |= (uint8_t)(ADC_TRIGR1_TSON);
  434. }
  435. else
  436. {
  437. /*Disable the Temperature sensor internal reference.*/
  438. ADC1->TRIGR[0] &= (uint8_t)(~ADC_TRIGR1_TSON);
  439. }
  440. }
  441. /**
  442. * @brief Enables or disables the Internal Voltage reference.
  443. * @param NewState : new state of the Internal Voltage reference.
  444. * This parameter can be: ENABLE or DISABLE.
  445. * @retval None
  446. */
  447. void ADC_VrefintCmd(FunctionalState NewState)
  448. {
  449. /* Check the parameters */
  450. assert_param(IS_FUNCTIONAL_STATE(NewState));
  451. if (NewState != DISABLE)
  452. {
  453. /* Enable the Internal Voltage reference.*/
  454. ADC1->TRIGR[0] |= (uint8_t)(ADC_TRIGR1_VREFINTON);
  455. }
  456. else
  457. {
  458. /* Disable the Internal Voltage reference.*/
  459. ADC1->TRIGR[0] &= (uint8_t)(~ADC_TRIGR1_VREFINTON);
  460. }
  461. }
  462. /**
  463. * @}
  464. */
  465. /** @defgroup ADC_Group4 Channels Configuration functions
  466. * @brief Channels Configuration functions
  467. *
  468. @verbatim
  469. ===============================================================================
  470. Channels Configuration functions
  471. ===============================================================================
  472. This section provides functions allowing to:
  473. - Enable or disable the ADC channel using ADC_ChannelCmd() function,
  474. - Configure the channels sampling times using ADC_SamplingTimeConfig()
  475. function.
  476. Note: there are 2 sampling times configuration values :
  477. - 1st Group value : for channels 0..23
  478. - 2nd Group value : for channels 24..27 (depending on the MCU
  479. package density) and Temperature Sensor and Vrefint channels.
  480. - Configure the channels Schmitt Trigger for each channel using
  481. ADC_SchmittTriggerConfig() function.
  482. - Get the current ADC conversion value.
  483. @endverbatim
  484. * @{
  485. */
  486. /**
  487. * @brief Enables or disables the selected ADC channel(s).
  488. * @param ADCx where x can be 1 to select the specified ADC peripheral.
  489. * @param ADC_Channels: specifies the ADC channels to be initialized
  490. * This parameter can be one of the following values:
  491. * @arg ADC_Channel_0: Channel 0
  492. * @arg ADC_Channel_1: Channel 1
  493. * @arg ADC_Channel_2: Channel 2
  494. * @arg ADC_Channel_3: Channel 3
  495. * @arg ADC_Channel_4: Channel 4
  496. * @arg ADC_Channel_5: Channel 5
  497. * @arg ADC_Channel_6: Channel 6
  498. * @arg ADC_Channel_7: Channel 7
  499. * @arg ADC_Channel_8: Channel 8
  500. * @arg ADC_Channel_9: Channel 9
  501. * @arg ADC_Channel_10: Channel 10
  502. * @arg ADC_Channel_11: Channel 11
  503. * @arg ADC_Channel_12: Channel 12
  504. * @arg ADC_Channel_13: Channel 13
  505. * @arg ADC_Channel_14: Channel 14
  506. * @arg ADC_Channel_15: Channel 15
  507. * @arg ADC_Channel_16: Channel 16
  508. * @arg ADC_Channel_17: Channel 17
  509. * @arg ADC_Channel_18: Channel 18
  510. * @arg ADC_Channel_19: Channel 19
  511. * @arg ADC_Channel_20: Channel 20
  512. * @arg ADC_Channel_21: Channel 21
  513. * @arg ADC_Channel_22: Channel 22
  514. * @arg ADC_Channel_23: Channel 23
  515. * @arg ADC_Channel_24: Channel 24
  516. * @arg ADC_Channel_25: Channel 25
  517. * @arg ADC_Channel_26: Channel 26
  518. * @arg ADC_Channel_27: Channel 27
  519. * @arg ADC_Channel_Vrefint: Vrefint Channel
  520. * @arg ADC_Channel_TempSensor: Temperature sensor Channel
  521. * @arg ADC_Channel_00To07: select from channel00 to channel07
  522. * @arg ADC_Channel_08To15: select from channel08 to channel15
  523. * @arg ADC_Channel_16To23: select from channel16 to channel23
  524. * @arg ADC_Channel_24To27: select from channel24 to channel27
  525. * @param NewState : new state of the specified ADC channel(s).
  526. * This parameter can be: ENABLE or DISABLE.
  527. * @retval None
  528. */
  529. void ADC_ChannelCmd(ADC_TypeDef* ADCx, ADC_Channel_TypeDef ADC_Channels, FunctionalState NewState)
  530. {
  531. uint8_t regindex = 0;
  532. /* Check the parameters */
  533. assert_param(IS_FUNCTIONAL_STATE(NewState));
  534. regindex = (uint8_t)((uint16_t)ADC_Channels >> 8);
  535. if (NewState != DISABLE)
  536. {
  537. /* Enable the selected ADC channel(s). */
  538. ADCx->SQR[regindex] |= (uint8_t)(ADC_Channels);
  539. }
  540. else
  541. {
  542. /* Disable the selected ADC channel(s). */
  543. ADCx->SQR[regindex] &= (uint8_t)(~(uint8_t)(ADC_Channels));
  544. }
  545. }
  546. /**
  547. * @brief Configures the sampling time for the selected ADC channel group.
  548. * @param ADCx where x can be 1 to select the specified ADC peripheral.
  549. * @param ADC_GroupChannels : ADC channel group to configure.
  550. * This parameter can be one of the following values:
  551. * @arg ADC_Group_SlowChannels: Slow Channels group
  552. * @arg ADC_Group_FastChannels: Fast Channels group
  553. * @note The channels of 1st ADC Group can be channels 0..23
  554. * @note The channels of 2nd ADC Group can be channels 24..27 (depending on the MCU
  555. * package density) and Temperature Sensor and Vrefint channels.
  556. * @param ADC_SamplingTime : Specifies the sample time value
  557. * This parameter can be one of the following values:
  558. * @arg ADC_SamplingTime_4Cycles: Sampling Time Cycles is 4
  559. * @arg ADC_SamplingTime_9Cycles: Sampling Time Cycles is 9
  560. * @arg ADC_SamplingTime_16Cycles: Sampling Time Cycles is 16
  561. * @arg ADC_SamplingTime_24Cycles: Sampling Time Cycles is 24
  562. * @arg ADC_SamplingTime_48Cycles: Sampling Time Cycles is 48
  563. * @arg ADC_SamplingTime_96Cycles: Sampling Time Cycles is 96
  564. * @arg ADC_SamplingTime_192Cycles: Sampling Time Cycles is 192
  565. * @arg ADC_SamplingTime_384Cycles: Sampling Time Cycles is 384
  566. * @retval None
  567. */
  568. void ADC_SamplingTimeConfig(ADC_TypeDef* ADCx,
  569. ADC_Group_TypeDef ADC_GroupChannels,
  570. ADC_SamplingTime_TypeDef ADC_SamplingTime)
  571. {
  572. /* Check the parameters */
  573. assert_param(IS_ADC_GROUP(ADC_GroupChannels));
  574. assert_param(IS_ADC_SAMPLING_TIME_CYCLES(ADC_SamplingTime));
  575. if ( ADC_GroupChannels != ADC_Group_SlowChannels)
  576. {
  577. /* Configures the sampling time for the Fast ADC channel group. */
  578. ADCx->CR3 &= (uint8_t)~ADC_CR3_SMPT2;
  579. ADCx->CR3 |= (uint8_t)(ADC_SamplingTime << 5);
  580. }
  581. else
  582. {
  583. /* Configures the sampling time for the Slow ADC channel group. */
  584. ADCx->CR2 &= (uint8_t)~ADC_CR2_SMPT1;
  585. ADCx->CR2 |= (uint8_t)ADC_SamplingTime;
  586. }
  587. }
  588. /**
  589. * @brief Configures the status of the Schmitt Trigger for the selected ADC
  590. * channel(s).
  591. * @param ADCx where x can be 1 to select the specified ADC peripheral.
  592. * @param ADC_Channels: specifies the ADC channels to be initialized,
  593. * This parameter can be one of the following values:
  594. * @arg ADC_Channel_0: Channel 0
  595. * @arg ADC_Channel_1: Channel 1
  596. * @arg ADC_Channel_2: Channel 2
  597. * @arg ADC_Channel_3: Channel 3
  598. * @arg ADC_Channel_4: Channel 4
  599. * @arg ADC_Channel_5: Channel 5
  600. * @arg ADC_Channel_6: Channel 6
  601. * @arg ADC_Channel_7: Channel 7
  602. * @arg ADC_Channel_8: Channel 8
  603. * @arg ADC_Channel_9: Channel 9
  604. * @arg ADC_Channel_10: Channel 10
  605. * @arg ADC_Channel_11: Channel 11
  606. * @arg ADC_Channel_12: Channel 12
  607. * @arg ADC_Channel_13: Channel 13
  608. * @arg ADC_Channel_14: Channel 14
  609. * @arg ADC_Channel_15: Channel 15
  610. * @arg ADC_Channel_16: Channel 16
  611. * @arg ADC_Channel_17: Channel 17
  612. * @arg ADC_Channel_18: Channel 18
  613. * @arg ADC_Channel_19: Channel 19
  614. * @arg ADC_Channel_20: Channel 20
  615. * @arg ADC_Channel_21: Channel 21
  616. * @arg ADC_Channel_22: Channel 22
  617. * @arg ADC_Channel_23: Channel 23
  618. * @arg ADC_Channel_24: Channel 24
  619. * @arg ADC_Channel_25: Channel 25
  620. * @arg ADC_Channel_26: Channel 26
  621. * @arg ADC_Channel_27: Channel 27
  622. * @arg ADC_Channel_Vrefint: Vrefint Channel
  623. * @arg ADC_Channel_TempSensor: Temperature sensor Channel
  624. * @arg ADC_Channel_00To07: select from channel00 to channel07
  625. * @arg ADC_Channel_08To15: select from channel08 to channel15
  626. * @arg ADC_Channel_16To23: select from channel16 to channel23
  627. * @arg ADC_Channel_24To27: select from channel24 to channel27
  628. * @param NewState : new state of the Schmitt Trigger
  629. * This parameter can be: ENABLE or DISABLE.
  630. * @retval None
  631. */
  632. void ADC_SchmittTriggerConfig(ADC_TypeDef* ADCx, ADC_Channel_TypeDef ADC_Channels,
  633. FunctionalState NewState)
  634. {
  635. uint8_t regindex = 0;
  636. /* Check the parameters */
  637. assert_param(IS_FUNCTIONAL_STATE(NewState));
  638. regindex = (uint8_t)((uint16_t)ADC_Channels >> 8);
  639. if (NewState != DISABLE)
  640. {
  641. /* Enable the Schmitt Trigger for the selected ADC channel(s).*/
  642. ADCx->TRIGR[regindex] &= (uint8_t)(~(uint8_t)ADC_Channels);
  643. }
  644. else
  645. {
  646. /* Disable the Schmitt Trigger for the selected ADC channel(s).*/
  647. ADCx->TRIGR[regindex] |= (uint8_t)(ADC_Channels);
  648. }
  649. }
  650. /**
  651. * @brief Returns the last ADC converted data.
  652. * @param ADCx where x can be 1 to select the specified ADC peripheral.
  653. * @retval The Data conversion value.
  654. */
  655. uint16_t ADC_GetConversionValue(ADC_TypeDef* ADCx)
  656. {
  657. uint16_t tmpreg = 0;
  658. /* Get last ADC converted data.*/
  659. tmpreg = (uint16_t)(ADCx->DRH);
  660. tmpreg = (uint16_t)((uint16_t)((uint16_t)tmpreg << 8) | ADCx->DRL);
  661. /* Return the selected ADC conversion value */
  662. return (uint16_t)(tmpreg) ;
  663. }
  664. /**
  665. * @}
  666. */
  667. /** @defgroup ADC_Group5 ADC Channels DMA Configuration function
  668. * @brief ADC Channels DMA Configuration function
  669. *
  670. @verbatim
  671. ===============================================================================
  672. ADC Channels DMA Configuration function
  673. ===============================================================================
  674. This section provides a function allowing to configure the DMA for ADC
  675. channel.
  676. Since converted channel values are stored into a unique data register,
  677. it is useful to use DMA for conversion of more than one channel. This
  678. avoids the loss of the data already stored in the ADC Data register.
  679. When the DMA mode is enabled (using the ADC_DMACmd() function), after each
  680. conversion of a channel, a DMA request is generated.
  681. @endverbatim
  682. * @{
  683. */
  684. /**
  685. * @brief Enables or disables the specified ADC DMA request.
  686. * @param ADCx where x can be 1 to select the specified ADC peripheral.
  687. * @param NewState : new state of the specified ADC DMA transfer.
  688. * This parameter can be: ENABLE or DISABLE.
  689. * @retval None
  690. */
  691. void ADC_DMACmd(ADC_TypeDef* ADCx, FunctionalState NewState)
  692. {
  693. /* Check the parameters */
  694. assert_param(IS_FUNCTIONAL_STATE(NewState));
  695. if (NewState != DISABLE)
  696. {
  697. /* Enable the specified ADC DMA request */
  698. ADCx->SQR[0] &= (uint8_t)~ADC_SQR1_DMAOFF;
  699. }
  700. else
  701. {
  702. /* Disable the specified ADC DMA request */
  703. ADCx->SQR[0] |= ADC_SQR1_DMAOFF;
  704. }
  705. }
  706. /**
  707. * @}
  708. */
  709. /** @defgroup ADC_Group6 Interrupts and flags management functions
  710. * @brief Interrupts and flags management functions
  711. *
  712. @verbatim
  713. ===============================================================================
  714. Interrupts and flags management functions
  715. ===============================================================================
  716. This section provides functions allowing to configure the ADC Interrupts and
  717. get the status and clear flags and Interrupts pending bits.
  718. The ADC provides 3 Interrupt sources and 3 Flags:
  719. Flags :
  720. ----------
  721. 1. ADC_FLAG_OVR : Overrun detection when ADC channel converted data is lost
  722. 2. ADC_FLAG_EOC : End of conversion - to indicate the end of a regular
  723. CHANNEL conversion or a GROUP conversions, depending of the
  724. ADC Continuous Conversion Mode (Continuous or Single
  725. conversion) and of the DMA usage.
  726. Note : if DMA is used, EOC occurs at the end of the sequence
  727. conversion, else it occurs after each conversion
  728. 3. ADC_FLAG_AWD: to indicate if the converted voltage crosses the
  729. programmed Analog watchdog thresholds values.
  730. Interrupts :
  731. ------------
  732. 1. ADC_IT_OVR : specifies the interrupt source for the Overrun detection event.
  733. 2. ADC_IT_EOC : specifies the interrupt source for the End of conversion event.
  734. 3. ADC_IT_AWD : specifies the interrupt source for the Analog watchdog event.
  735. @endverbatim
  736. * @{
  737. */
  738. /**
  739. * @brief Enables or disables the specified ADC interrupts.
  740. * @param ADCx where x can be 1 to select the specified ADC peripheral.
  741. * @param ADC_IT : specifies the ADC interrupt sources to be enabled or
  742. * disabled.
  743. * This parameter can be one of the following values:
  744. * @arg ADC_IT_EOC: End of Conversion Interrupt
  745. * @arg ADC_IT_AWD: Analog Watchdog Interrupt
  746. * @arg ADC_IT_OVER: Over Run Interrupt
  747. * @param NewState : new state of the specified ADC interrupts.
  748. * This parameter can be: ENABLE or DISABLE.
  749. * @retval None
  750. */
  751. void ADC_ITConfig(ADC_TypeDef* ADCx, ADC_IT_TypeDef ADC_IT, FunctionalState NewState)
  752. {
  753. /* Check the parameters */
  754. assert_param(IS_FUNCTIONAL_STATE(NewState));
  755. assert_param(IS_ADC_IT(ADC_IT));
  756. if (NewState != DISABLE)
  757. {
  758. /* Enable the selected ADC interrupts */
  759. ADCx->CR1 |= (uint8_t) ADC_IT;
  760. }
  761. else
  762. {
  763. /* Disable the selected ADC interrupts */
  764. ADCx->CR1 &= (uint8_t)(~ADC_IT);
  765. }
  766. }
  767. /**
  768. * @brief Checks whether the specified ADC flag is set or not.
  769. * @param ADCx where x can be 1 to select the specified ADC peripheral.
  770. * @param ADC_FLAG: specifies the flag to check.
  771. * This parameter can be one of the following values:
  772. * @arg ADC_FLAG_EOC: End of Conversion flag
  773. * @arg ADC_FLAG_AWD: Analog Watchdog flag
  774. * @arg ADC_FLAG_OVER: Over Run flag
  775. * @retval The new state of ADC_FLAG (SET or RESET).
  776. */
  777. FlagStatus ADC_GetFlagStatus(ADC_TypeDef* ADCx, ADC_FLAG_TypeDef ADC_FLAG)
  778. {
  779. FlagStatus flagstatus = RESET;
  780. /* Check the parameters */
  781. assert_param(IS_ADC_GET_FLAG(ADC_FLAG));
  782. /* Check the status of the specified ADC flag */
  783. if ((ADCx->SR & ADC_FLAG) != (uint8_t)RESET)
  784. {
  785. /* ADC_FLAG is set */
  786. flagstatus = SET;
  787. }
  788. else
  789. {
  790. /* ADC_FLAG is reset */
  791. flagstatus = RESET;
  792. }
  793. /* Return the ADC_FLAG status */
  794. return flagstatus;
  795. }
  796. /**
  797. * @brief Clears the ADC's pending flags.
  798. * @param ADCx where x can be 1 to select the specified ADC peripheral.
  799. * @param ADC_FLAG: specifies the flag to clear.
  800. * This parameter can be one of the following values:
  801. * @arg ADC_FLAG_EOC: End of Conversion flag
  802. * @arg ADC_FLAG_AWD: Analog Watchdog flag
  803. * @arg ADC_FLAG_OVER: Over Run flag
  804. * @retval None
  805. */
  806. void ADC_ClearFlag(ADC_TypeDef* ADCx,
  807. ADC_FLAG_TypeDef ADC_FLAG)
  808. {
  809. /* Check the parameters */
  810. assert_param(IS_ADC_CLEAR_FLAG(ADC_FLAG));
  811. /* Clear the selected ADC flags */
  812. ADCx->SR = (uint8_t)~ADC_FLAG;
  813. }
  814. /**
  815. * @brief Checks whether the specified ADC interrupt has occurred or not.
  816. * @param ADCx where x can be 1 to select the specified ADC peripheral.
  817. * @param ADC_IT: specifies the ADC interrupt source to check.
  818. * This parameter can be one of the following values:
  819. * @arg ADC_IT_EOC: End of Conversion Interrupt
  820. * @arg ADC_IT_AWD: Analog Watchdog Interrupt
  821. * @arg ADC_IT_OVER: Over Run Interrupt
  822. * @retval Status of ADC_IT (SET or RESET).
  823. */
  824. ITStatus ADC_GetITStatus(ADC_TypeDef* ADCx,
  825. ADC_IT_TypeDef ADC_IT)
  826. {
  827. ITStatus itstatus = RESET;
  828. uint8_t itmask = 0, enablestatus = 0;
  829. /* Check the parameters */
  830. assert_param(IS_ADC_GET_IT(ADC_IT));
  831. /* Get the ADC IT index */
  832. itmask = (uint8_t)(ADC_IT >> 3);
  833. itmask = (uint8_t)((uint8_t)((uint8_t)(itmask & (uint8_t)0x10) >> 2) | \
  834. (uint8_t)(itmask & (uint8_t)0x03));
  835. /* Get the ADC_IT enable bit status */
  836. enablestatus = (uint8_t)(ADCx->CR1 & (uint8_t)ADC_IT) ;
  837. /* Check the status of the specified ADC interrupt */
  838. if (((ADCx->SR & itmask) != (uint8_t)RESET) && enablestatus)
  839. {
  840. /* ADC_IT is set */
  841. itstatus = SET;
  842. }
  843. else
  844. {
  845. /* ADC_IT is reset */
  846. itstatus = RESET;
  847. }
  848. /* Return the ADC_IT status */
  849. return itstatus;
  850. }
  851. /**
  852. * @brief Clears the ADC’s interrupt pending bits.
  853. * @param ADCx where x can be 1 to select the specified ADC peripheral.
  854. * @param ADC_IT: specifies the ADC interrupt pending bit to clear.
  855. * This parameter can be one of the following values:
  856. * @arg ADC_IT_EOC: End of Conversion Interrupt
  857. * @arg ADC_IT_AWD: Analog Watchdog Interrupt
  858. * @arg ADC_IT_OVER: Over Run Interrupt
  859. * @retval None
  860. */
  861. void ADC_ClearITPendingBit(ADC_TypeDef* ADCx,
  862. ADC_IT_TypeDef ADC_IT)
  863. {
  864. uint8_t itmask = 0;
  865. /* Check the parameters */
  866. assert_param(IS_ADC_IT(ADC_IT));
  867. /* Get the ADC IT index */
  868. itmask = (uint8_t)(ADC_IT >> 3);
  869. itmask = (uint8_t)((uint8_t)(((uint8_t)(itmask & (uint8_t)0x10)) >> 2) | \
  870. (uint8_t)(itmask & (uint8_t)0x03));
  871. /* Clear the selected ADC interrupt pending bits */
  872. ADCx->SR = (uint8_t)~itmask;
  873. }
  874. /**
  875. * @}
  876. */
  877. /**
  878. * @}
  879. */
  880. /**
  881. * @}
  882. */
  883. /**
  884. * @}
  885. */
  886. /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/