1 /**
2 ******************************************************************************
3 * @file HAL_adc.c
4 * @author AE Team
5 * @version V2.0.0
6 * @date 22/08/2017
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, MindMotion 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>© COPYRIGHT 2017 MindMotion</center></h2>
19 */
20
21 /* Includes ------------------------------------------------------------------*/
22 #include "HAL_adc.h"
23 #include "HAL_rcc.h"
24
25 /** @addtogroup StdPeriph_Driver
26 * @{
27 */
28
29 /** @defgroup ADC
30 * @brief ADC driver modules
31 * @{
32 */
33
34 /** @defgroup ADC_Private_TypesDefinitions
35 * @{
36 */
37
38 /**
39 * @}
40 */
41
42 /** @defgroup ADC_Private_Defines
43 * @{
44 */
45
46
47 /* ADCR register Mask */
48 #define ADCR_CLEAR_Mask ((uint32_t)0xFFFFF180)
49
50 /* ADCFG register Mask */
51 #define ADCFG_CLEAR_Mask ((uint32_t)0xFFFFFF8F)
52 #define ADCFG_SAMCTL_Set ((uint32_t)0x1c00)
53 #define ADCFG_TSEN_Set ((uint32_t)0x00000004)
54 #define ADCFG_VSEN_Set ((uint32_t)0x00000008)
55
56 /* ADC ADEN mask */
57 #define ADCFG_ADEN_Set ((uint32_t)0x00000001)
58 #define ADCFG_ADEN_Reset ((uint32_t)0xFFFFFFFE)
59
60 /* ADC DMA mask */
61 #define ADCR_DMA_Set ((uint32_t)0x00000008)
62 #define ADCR_DMA_Reset ((uint32_t)0xFFFFFFF7)
63
64 /* ADC Software start mask */
65 #define ADCR_SWSTART_Set ((uint32_t)0x00000100)
66 #define ADCR_SWSTART_Reset ((uint32_t)0xFFFFFEFF)
67
68 /* ADC EXTTRIG mask */
69 #define ADCR_EXTTRIG_Set ((uint32_t)0x00000004)
70 #define ADCR_EXTTRIG_Reset ((uint32_t)0xFFFFFFFB)
71
72 /*seletec channle enable */
73 #define CHEN0_ENABLE ((uint32_t)0x00000001)
74 #define CHEN1_ENABLE ((uint32_t)0x00000002)
75 #define CHEN2_ENABLE ((uint32_t)0x00000004)
76 #define CHEN3_ENABLE ((uint32_t)0x00000008)
77 #define CHEN4_ENABLE ((uint32_t)0x00000010)
78 #define CHEN5_ENABLE ((uint32_t)0x00000020)
79 #define CHEN6_ENABLE ((uint32_t)0x00000040)
80 #define CHEN7_ENABLE ((uint32_t)0x00000080)
81 #define CHEN8_ENABLE ((uint32_t)0x00000100)
82 #define CHEN9_ENABLE ((uint32_t)0x00000200)
83 #define CHEN10_ENABLE ((uint32_t)0x00004000)
84 #define CHEN11_ENABLE ((uint32_t)0x00008000)
85 #define CHALL_ENABLE ((uint32_t)0x0000C3ff)
86
87 #define CHEN_DISABLE ((uint32_t)0xFFFF3C00)
88
89 /* ADC EXTSEL mask */
90 #define ADCR_EXTSEL_Reset ((uint32_t)0xFFFFFF8F)
91
92 /* ADC Analog watchdog enable mode mask */
93 #define ADCFG_AWDMode_Reset ((uint32_t)0xFFFFFFFD)
94 #define ADCFG_AWDMode_Set ((uint32_t)0x00000002)
95
96 /* ADC AWDCH mask */
97 #define ADCR_AWDCH_Reset ((uint32_t)0xFFFF0FFF)
98
99 /* ADC TSPD mask */
100 #define ADCHS_TSVREFE_Set ((uint32_t)0x00004000)
101 #define ADCHS_TSVREFE_Reset ((uint32_t)0xFFFFBFFF)
102
103 #define ADCHS_VSVREFE_Set ((uint32_t)0x00008000)
104 #define ADCHS_VSVREFE_Reset ((uint32_t)0xFFFF7FFF)
105
106 /* ADC1 DATA register base address */
107 #define ADDATA_ADDRESS ((uint32_t)0x40012400)
108 /**
109 * @}
110 */
111
112 /** @defgroup ADC_Private_Macros
113 * @{
114 */
115
116 /**
117 * @}
118 */
119
120 /** @defgroup ADC_Private_Variables
121 * @{
122 */
123
124 /**
125 * @}
126 */
127
128 /** @defgroup ADC_Private_FunctionPrototypes
129 * @{
130 */
131
132 /**
133 * @}
134 */
135
136 /** @defgroup ADC_Private_Functions
137 * @{
138 */
139
140 /**
141 * @brief Deinitializes the ADCx peripheral registers to their default
142 * reset values.
143 * @param ADCx: where x can be 1, 2 to select the ADC peripheral.
144 * @retval : None
145 */
ADC_DeInit(ADC_TypeDef * ADCx)146 void ADC_DeInit(ADC_TypeDef* ADCx)
147 {
148 /* Check the parameters */
149 assert_param(IS_ADC_ALL_PERIPH(ADCx));
150 switch (*(uint32_t*)&ADCx)
151 {
152 case ADC1_BASE:
153 /* Enable ADC1 reset state */
154 RCC_APB2PeriphResetCmd(RCC_APB2Periph_ADC1, ENABLE);
155 /* Release ADC1 from reset state */
156 RCC_APB2PeriphResetCmd(RCC_APB2Periph_ADC1, DISABLE);
157 break;
158
159 default:
160 break;
161 }
162 }
163
164 /**
165 * @brief Initializes the ADCx peripheral according to the specified parameters
166 * in the ADC_InitStruct.
167 * @param ADCx: where x can be 1, 2 or 3 to select the ADC peripheral.
168 * @param ADC_InitStruct: pointer to an ADC_InitTypeDef structure that
169 * contains the configuration information for the specified
170 * ADC peripheral.
171 * @retval : None
172 */
ADC_Init(ADC_TypeDef * ADCx,ADC_InitTypeDef * ADC_InitStruct)173 void ADC_Init(ADC_TypeDef* ADCx, ADC_InitTypeDef* ADC_InitStruct)
174 {
175 uint32_t tmpreg1 = 0;
176
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 /*---------------------------- ADCx ADCFG Configuration -----------------*/
185 /* Get the ADCx ADCFG value */
186 tmpreg1 = ADCx->ADCFG;
187 /* Clear ADCPRE bits */
188 tmpreg1 &= ADCFG_CLEAR_Mask;
189 /* Configure ADCx: AD convertion prescare*/
190 tmpreg1 |= (uint32_t)(ADC_InitStruct->ADC_PRESCARE);
191 /* Write to ADCx ADCFG */
192 ADCx->ADCFG = tmpreg1;
193
194 /*---------------------------- ADCx ADCR Configuration -----------------*/
195 /* Get the ADCx ADCR value */
196 tmpreg1 = ADCx->ADCR;
197 /* Clear ALIGN , ADMD, TRGSEL, DMAEN,TRGEN,ADWIE and ADIE bits */
198 tmpreg1 &= ADCR_CLEAR_Mask;
199 /* Configure ADCx: external trigger event and AD conversion mode and ALIGN*/
200 /* Set ALIGN bit according to ADC_DataAlign value */
201 /* Set TRGEN bits according to ADC_ContinuousConvMode value */
202 /* Set TRGSEL bits according to ADC_ExternalTrigConv value */
203 /* Set ADMD bit according to ADC_Mode value */
204 tmpreg1 |= ((uint32_t)ADC_InitStruct->ADC_DataAlign) | ADC_InitStruct->ADC_ExternalTrigConv |
205 ((uint32_t)ADC_InitStruct->ADC_Mode) ;
206
207 /* Write to ADCx ADCR */
208 ADCx->ADCR = tmpreg1;
209
210 }
211
212 /**
213 * @brief Fills each ADC_InitStruct member with its default value.
214 * @param ADC_InitStruct : pointer to an ADC_InitTypeDef structure
215 * which will be initialized.
216 * @retval : None
217 */
ADC_StructInit(ADC_InitTypeDef * ADC_InitStruct)218 void ADC_StructInit(ADC_InitTypeDef* ADC_InitStruct)
219 {
220 /* Initialize the ADC_PRESCARE values */
221 ADC_InitStruct->ADC_PRESCARE = ADC_PCLK2_PRESCARE_16;
222 /* Initialize the ADC_Mode member */
223 ADC_InitStruct->ADC_Mode = ADC_Mode_Single;
224 /* Initialize the ADC_ContinuousConvMode member */
225 ADC_InitStruct->ADC_ContinuousConvMode = DISABLE;
226 /* Initialize the ADC_ExternalTrigConv member */
227 ADC_InitStruct->ADC_ExternalTrigConv = ADC_ExternalTrigConv_T1_CC1;
228 /* Initialize the ADC_DataAlign member */
229 ADC_InitStruct->ADC_DataAlign = ADC_DataAlign_Right;
230 /* Initialize the ADC_Resolution member */
231 ADC_InitStruct->ADC_Resolution = ADC_Resolution_12b;
232 }
233
234 /**
235 * @brief Enables or disables the specified ADC peripheral.
236 * @param ADCx: where x can be 1, 2 to select the ADC peripheral.
237 * @param NewState: new state of the ADCx peripheral. This parameter
238 * can be: ENABLE or DISABLE.
239 * @retval : None
240 */
ADC_Cmd(ADC_TypeDef * ADCx,FunctionalState NewState)241 void ADC_Cmd(ADC_TypeDef* ADCx, FunctionalState NewState)
242 {
243 /* Check the parameters */
244 assert_param(IS_ADC_ALL_PERIPH(ADCx));
245 assert_param(IS_FUNCTIONAL_STATE(NewState));
246 if (NewState != DISABLE)
247 {
248 /* Set the ADEN bit */
249 ADCx->ADCFG |= ADCFG_ADEN_Set;
250 }
251 else
252 {
253 /* Disable the selected ADC peripheral */
254 ADCx->ADCFG &= ADCFG_ADEN_Reset;
255 }
256 }
257
258 /**
259 * @brief Enables or disables the specified ADC DMA request.
260 * @param ADCx: where x can be 1 or 2 to select the ADC peripheral.
261 * @param NewState: new state of the selected ADC DMA transfer.
262 * This parameter can be: ENABLE or DISABLE.
263 * @retval : None
264 */
ADC_DMACmd(ADC_TypeDef * ADCx,FunctionalState NewState)265 void ADC_DMACmd(ADC_TypeDef* ADCx, FunctionalState NewState)
266 {
267 /* Check the parameters */
268 assert_param(IS_ADC_DMA_PERIPH(ADCx));
269 assert_param(IS_FUNCTIONAL_STATE(NewState));
270 if (NewState != DISABLE)
271 {
272 /* Enable the selected ADC DMA request */
273 ADCx->ADCR |= ADCR_DMA_Set;
274 }
275 else
276 {
277 /* Disable the selected ADC DMA request */
278 ADCx->ADCR &= ADCR_DMA_Reset;
279 }
280 }
281
282 /**
283 * @brief Enables or disables the specified ADC interrupts.
284 * @param ADCx: where x can be 1, 2 to select the ADC peripheral.
285 * @param ADC_IT: specifies the ADC interrupt sources to be enabled
286 * or disabled.
287 * This parameter can be any combination of the following values:
288 * @arg ADC_IT_EOC: End of conversion interrupt mask
289 * @arg ADC_IT_AWD: Analog watchdog interrupt mask
290 * @param NewState: new state of the specified ADC interrupts.
291 * This parameter can be: ENABLE or DISABLE.
292 * @retval : None
293 */
ADC_ITConfig(ADC_TypeDef * ADCx,uint16_t ADC_IT,FunctionalState NewState)294 void ADC_ITConfig(ADC_TypeDef* ADCx, uint16_t ADC_IT, FunctionalState NewState)
295 {
296
297 /* Check the parameters */
298 assert_param(IS_ADC_ALL_PERIPH(ADCx));
299 assert_param(IS_FUNCTIONAL_STATE(NewState));
300 assert_param(IS_ADC_IT(ADC_IT));
301 /* Get the ADC IT index */
302 //itmask = (uint8_t)ADC_IT;
303 if (NewState != DISABLE)
304 {
305 /* Enable the selected ADC interrupts */
306 ADCx->ADCR |= ADC_IT;
307 }
308 else
309 {
310 /* Disable the selected ADC interrupts */
311 ADCx->ADCR &= (~(uint32_t)ADC_IT);
312 }
313 }
314
315 /**
316 * @brief Enables or disables the selected ADC software start conversion .
317 * @param ADCx: where x can be 1, 2 to select the ADC peripheral.
318 * @param NewState: new state of the selected ADC software start conversion.
319 * This parameter can be: ENABLE or DISABLE.
320 * @retval : None
321 */
ADC_SoftwareStartConvCmd(ADC_TypeDef * ADCx,FunctionalState NewState)322 void ADC_SoftwareStartConvCmd(ADC_TypeDef* ADCx, FunctionalState NewState)
323 {
324 /* Check the parameters */
325 assert_param(IS_ADC_ALL_PERIPH(ADCx));
326 assert_param(IS_FUNCTIONAL_STATE(NewState));
327 if (NewState != DISABLE)
328 {
329 /* Enable the selected ADC conversion on external event and start the selected
330 ADC conversion */
331 /*Set ADST bit*/
332 ADCx->ADCR |= ADCR_SWSTART_Set;
333 }
334 else
335 {
336 /* Disable the selected ADC conversion on external event and stop the selected
337 ADC conversion */
338 ADCx->ADCR &= ADCR_SWSTART_Reset;
339 }
340 }
341
342 /**
343 * @brief Gets the selected ADC Software start conversion Status.
344 * @param ADCx: where x can be 1, 2 to select the ADC peripheral.
345 * @retval : The new state of ADC software start conversion (SET or RESET).
346 */
ADC_GetSoftwareStartConvStatus(ADC_TypeDef * ADCx)347 FlagStatus ADC_GetSoftwareStartConvStatus(ADC_TypeDef* ADCx)
348 {
349 FlagStatus bitstatus = RESET;
350 /* Check the parameters */
351 assert_param(IS_ADC_ALL_PERIPH(ADCx));
352 /* Check the status of ADST bit */
353 if ((ADCx->ADCR & ADCR_SWSTART_Set) != (uint32_t)RESET)
354 {
355 /* ADST bit is set */
356 bitstatus = SET;
357 }
358 else
359 {
360 /* ADST bit is reset */
361 bitstatus = RESET;
362 }
363 /* Return the ADST bit status */
364 return bitstatus;
365 }
366
367 /**
368 * @brief Configures for the selected ADC channel its corresponding
369 * rank in the sequencer and its sample time.
370 * @param ADCx: where x can be 1, 2 to select the ADC peripheral.
371 * @param ADC_Channel: the ADC channel to configure.
372 * This parameter can be one of the following values:
373 * @arg ADC_Channel_0: ADC Channel0 selected
374 * @arg ADC_Channel_1: ADC Channel1 selected
375 * @arg ADC_Channel_2: ADC Channel2 selected
376 * @arg ADC_Channel_3: ADC Channel3 selected
377 * @arg ADC_Channel_4: ADC Channel4 selected
378 * @arg ADC_Channel_5: ADC Channel5 selected
379 * @arg ADC_Channel_6: ADC Channel6 selected
380 * @arg ADC_Channel_7: ADC Channel7 selected
381 * @arg ADC_Channel_8: ADC Channel8 selected
382 * @retval : None
383 */
ADC_RegularChannelConfig(ADC_TypeDef * ADCx,uint8_t ADC_Channel,uint8_t Rank,uint8_t ADC_SampleTime)384 void ADC_RegularChannelConfig(ADC_TypeDef* ADCx, uint8_t ADC_Channel, uint8_t Rank, uint8_t ADC_SampleTime)
385 {
386 uint32_t tmpreg = 0;
387 /* Check the parameters */
388 assert_param(IS_ADC_ALL_PERIPH(ADCx));
389 assert_param(IS_ADC_CHANNEL(ADC_Channel));
390 assert_param(IS_ADC_REGULAR_RANK(Rank));
391 assert_param(IS_ADC_SAMPLE_TIME(ADC_SampleTime));
392 tmpreg = ADCx->ADCFG;
393 tmpreg &= ~(ADCFG_SAMCTL_Set);
394 tmpreg|=ADC_SampleTime<<10;
395 ADCx->ADCFG = tmpreg;
396 switch(ADC_Channel)
397 {
398 /* set the CHEN0 bit for channel 0 enable*/
399 case ADC_Channel_0: ADCx->ADCHS |= CHEN0_ENABLE;
400 break;
401 /* set the CHEN1 bit for channel 1 enable*/
402 case ADC_Channel_1: ADCx->ADCHS |= CHEN1_ENABLE;
403 break;
404 /* set the CHEN2 bit for channel 2 enable*/
405 case ADC_Channel_2: ADCx->ADCHS |= CHEN2_ENABLE;
406 break;
407 /* set the CHEN3 bit for channel 3 enable*/
408 case ADC_Channel_3: ADCx->ADCHS |= CHEN3_ENABLE;
409 break;
410 /* set the CHEN4 bit for channel 4 enable*/
411 case ADC_Channel_4: ADCx->ADCHS |= CHEN4_ENABLE;
412 break;
413 /* set the CHEN5 bit for channel 5 enable*/
414 case ADC_Channel_5: ADCx->ADCHS |= CHEN5_ENABLE;
415 break;
416 /* set the CHEN6 bit for channel 6 enable*/
417 case ADC_Channel_6: ADCx->ADCHS |= CHEN6_ENABLE;
418 break;
419 /* set the CHEN7 bit for channel 7 enable*/
420 case ADC_Channel_7: ADCx->ADCHS |= CHEN7_ENABLE;
421 break;
422 /* set the CHEN8 bit for channel 8 enable*/
423 case ADC_Channel_8: ADCx->ADCHS |= CHEN8_ENABLE;
424 break;
425 case ADC_Channel_9: ADCx->ADCHS |= CHEN9_ENABLE;
426 break;
427 case ADC_Channel_10: ADCx->ADCHS |= CHEN10_ENABLE; //Temperature Sensor
428 break;
429 case ADC_Channel_11: ADCx->ADCHS |= CHEN11_ENABLE; //Voltage Sensor
430 break;
431 case ADC_Channel_All:ADCx->ADCHS |= CHALL_ENABLE;
432 break;
433 default:
434 ADCx->ADCHS &= CHEN_DISABLE;
435 break;
436
437 }
438
439 }
440
441 /**
442 * @brief Enables or disables the ADCx conversion through external trigger.
443 * @param ADCx: where x can be 1, 2 to select the ADC peripheral.
444 * @param NewState: new state of the selected ADC external trigger
445 * start of conversion.
446 * This parameter can be: ENABLE or DISABLE.
447 * @retval : None
448 */
ADC_ExternalTrigConvCmd(ADC_TypeDef * ADCx,FunctionalState NewState)449 void ADC_ExternalTrigConvCmd(ADC_TypeDef* ADCx, FunctionalState NewState)
450 {
451 /* Check the parameters */
452 assert_param(IS_ADC_ALL_PERIPH(ADCx));
453 assert_param(IS_FUNCTIONAL_STATE(NewState));
454 if (NewState != DISABLE)
455 {
456 /* Enable the selected ADC conversion on external event */
457 ADCx->ADCR |= ADCR_EXTTRIG_Set;
458 }
459 else
460 {
461 /* Disable the selected ADC conversion on external event */
462 ADCx->ADCR &= ADCR_EXTTRIG_Reset;
463 }
464 }
465
466 /**
467 * @brief Returns the last ADCx conversion result data for regular channel.
468 * @param ADCx: where x can be 1, 2 to select the ADC peripheral.
469 * @retval : The Data conversion value.
470 */
ADC_GetConversionValue(ADC_TypeDef * ADCx)471 uint16_t ADC_GetConversionValue(ADC_TypeDef* ADCx)
472 {
473 /* Check the parameters */
474 assert_param(IS_ADC_ALL_PERIPH(ADCx));
475 /* Return the selected ADC conversion value */
476 if(((ADCx->ADCFG&0x000000380)>>7)==0)
477 return (uint16_t) ADCx->ADDATA;
478 else if(((ADCx->ADCFG&0x000000380)>>7)==1)
479 return (uint16_t) (ADCx->ADDATA>>1);
480 else if(((ADCx->ADCFG&0x000000380)>>7)==2)
481 return (uint16_t) (ADCx->ADDATA>>2);
482 else if(((ADCx->ADCFG&0x000000380)>>7)==3)
483 return (uint16_t) (ADCx->ADDATA>>3);
484 else if(((ADCx->ADCFG&0x000000380)>>7)==4)
485 return (uint16_t) (ADCx->ADDATA>>4);
486 return (uint16_t) ADCx->ADDATA;
487 }
488
489
490 /**
491 * @brief Configures the ADCx external trigger for injected channels conversion.
492 * @param ADCx: where x can be 1, 2 to select the ADC peripheral.
493 * @param ADC_ExternalTrigInjecConv: specifies the ADC trigger to
494 * start injected conversion.
495 * This parameter can be one of the following values:
496 * @arg ADC_ExternalTrigConv_T1_CC1: Timer1 capture
497 * compare1 selected (for ADC1)
498 * @arg ADC_ExternalTrigConv_T1_CC2: Timer1 capture
499 * compare2 selected (for ADC1)
500 * @arg ADC_ExternalTrigConv_T1_CC3: Timer1 capture
501 * compare3 selected (for ADC1)
502 * @arg ADC_ExternalTrigConv_T2_CC2: Timer2 capture
503 * compare2 selected (for ADC1)
504 * @arg ADC_ExternalTrigConv_T3_TRGO: Timer3 TRGO event
505 * selected (for ADC1)
506 * @arg ADC_ExternalTrigConv_T3_CC1: Timer3 capture
507 * compare1 selected (for ADC1)
508 * @arg ADC_ExternalTrigConv_EXTI_11: EXTI line 11 event
509 * cselected (for ADC1)
510 * @retval : None
511 */
ADC_ExternalTrigInjectedConvConfig(ADC_TypeDef * ADCx,uint32_t ADC_ExternalTrigInjecConv)512 void ADC_ExternalTrigInjectedConvConfig(ADC_TypeDef* ADCx, uint32_t ADC_ExternalTrigInjecConv)
513 {
514 uint32_t tmpreg = 0;
515 /* Check the parameters */
516 assert_param(IS_ADC_ALL_PERIPH(ADCx));
517 assert_param(IS_ADC_EXT_INJEC_TRIG(ADC_ExternalTrigInjecConv));
518 /* Get the old register value */
519 tmpreg = ADCx->ADCR;
520 /* Clear the old external event selection for injected group */
521 tmpreg &= ADCR_EXTSEL_Reset;
522 /* Set the external event selection for injected group */
523 tmpreg |= ADC_ExternalTrigInjecConv;
524 /* Store the new register value */
525 ADCx->ADCR = tmpreg;
526 }
527
528 /**
529 * @brief Enables or disables the ADCx injected channels conversion
530 * through external trigger
531 * @param ADCx: where x can be 1, 2 to select the ADC peripheral.
532 * @param NewState: new state of the selected ADC external trigger
533 * start of injected conversion.
534 * This parameter can be: ENABLE or DISABLE.
535 * @retval : None
536 */
ADC_ExternalTrigInjectedConvCmd(ADC_TypeDef * ADCx,FunctionalState NewState)537 void ADC_ExternalTrigInjectedConvCmd(ADC_TypeDef* ADCx, FunctionalState NewState)
538 {
539 /* Check the parameters */
540 assert_param(IS_ADC_ALL_PERIPH(ADCx));
541 assert_param(IS_FUNCTIONAL_STATE(NewState));
542 if (NewState != DISABLE)
543 {
544 /* Enable the selected ADC external event selection for injected group */
545 ADCx->ADCR |= ADCR_EXTTRIG_Set;
546 }
547 else
548 {
549 /* Disable the selected ADC external event selection for injected group */
550 ADCx->ADCR &= ADCR_EXTTRIG_Reset;
551 }
552 }
553
554
555 /**
556 * @brief Enables or disables the analog watchdog on single/all regular
557 * or injected channels
558 * @param ADCx: where x can be 1, 2 to select the ADC peripheral.
559 * @param ADC_AnalogWatchdog: the ADC analog watchdog configuration.
560 * This parameter can be one of the following values:
561 * @arg ADC_AnalogWatchdog_SingleRegEnable: Analog watchdog on
562 * a single regular channel
563 * @arg ADC_AnalogWatchdog_None: No channel guarded by the
564 * analog watchdog
565 * analog watchdog
566 * @retval : None
567 */
ADC_AnalogWatchdogCmd(ADC_TypeDef * ADCx,FunctionalState NewState)568 void ADC_AnalogWatchdogCmd(ADC_TypeDef* ADCx,FunctionalState NewState)
569 {
570 /* Check the parameters */
571 assert_param(IS_ADC_ALL_PERIPH(ADCx));
572 assert_param(IS_FUNCTIONAL_STATE(NewState));
573 if (NewState != DISABLE)
574 {
575 /* Enable the selected ADC window comparison */
576 ADCx->ADCFG |= ADCFG_AWDMode_Set;
577 }
578 else
579 {
580 /* Disable the selected ADC window comparison */
581 ADCx->ADCFG &= ADCFG_AWDMode_Reset;
582 }
583 }
584
585 /**
586 * @brief Configures the high and low thresholds of the analog watchdog.
587 * @param ADCx: where x can be 1, 2 to select the ADC peripheral.
588 * @param HighThreshold: the ADC analog watchdog High threshold value.
589 * This parameter must be a 12bit value.
590 * @param LowThreshold: the ADC analog watchdog Low threshold value.
591 * This parameter must be a 12bit value.
592 * @retval : None
593 */
ADC_AnalogWatchdogThresholdsConfig(ADC_TypeDef * ADCx,uint16_t HighThreshold,uint16_t LowThreshold)594 void ADC_AnalogWatchdogThresholdsConfig(ADC_TypeDef* ADCx, uint16_t HighThreshold,
595 uint16_t LowThreshold)
596 {
597 uint32_t tempThreshold;
598 /* Check the parameters */
599 assert_param(IS_ADC_ALL_PERIPH(ADCx));
600 assert_param(IS_ADC_THRESHOLD(HighThreshold));
601 assert_param(IS_ADC_THRESHOLD(LowThreshold));
602 /* Get the ADCx high threshold */
603 tempThreshold = HighThreshold;
604 /* Set the ADCx high threshold and the ADCx low threshold */
605 ADCx->ADCMPR = (tempThreshold<<16) | LowThreshold;
606 }
607
608 /**
609 * @brief Configures the analog watchdog guarded single channel
610 * @param ADCx: where x can be 1, 2 or 3 to select the ADC peripheral.
611 * @param ADC_Channel: the ADC channel to configure for the analog
612 * watchdog.
613 * This parameter can be one of the following values:
614 * @arg ADC_Channel_0: ADC Channel0 selected
615 * @arg ADC_Channel_1: ADC Channel1 selected
616 * @arg ADC_Channel_2: ADC Channel2 selected
617 * @arg ADC_Channel_3: ADC Channel3 selected
618 * @arg ADC_Channel_4: ADC Channel4 selected
619 * @arg ADC_Channel_5: ADC Channel5 selected
620 * @arg ADC_Channel_6: ADC Channel6 selected
621 * @arg ADC_Channel_7: ADC Channel7 selected
622 * @arg ADC_Channel_8: ADC Channel8 selected
623 * @arg ADC_Channel_9: ADC Channel9 selected
624 * @arg ADC_Channel_10: ADC Channel10 selected
625 * @arg ADC_Channel_11: ADC Channel11 selected
626 * @arg ADC_Channel_All: ADC all Channel selected
627 * @retval : None
628 */
ADC_AnalogWatchdogSingleChannelConfig(ADC_TypeDef * ADCx,uint8_t ADC_Channel)629 void ADC_AnalogWatchdogSingleChannelConfig(ADC_TypeDef* ADCx, uint8_t ADC_Channel)
630 {
631 uint32_t tmpreg = 0;
632 /* Check the parameters */
633 assert_param(IS_ADC_ALL_PERIPH(ADCx));
634 assert_param(IS_ADC_CHANNEL(ADC_Channel));
635 /* Get the old register value */
636 tmpreg = ADCx->ADCR;
637 /* Clear the Analog watchdog channel select bits */
638 tmpreg &= ADCR_AWDCH_Reset;
639 /* Set the Analog watchdog channel */
640 tmpreg |= (ADC_Channel<<12);
641 /* Store the new register value */
642 ADCx->ADCR = tmpreg;
643 }
644
645 /**
646 * @brief Enables or disables the temperature sensor and Vrefint channel.
647 * @param NewState: new state of the temperature sensor.
648 * This parameter can be: ENABLE or DISABLE.
649 * @retval : None
650 */
651
ADC_TempSensorCmd(FunctionalState NewState)652 void ADC_TempSensorCmd(FunctionalState NewState)
653 {
654 /* Check the parameters */
655 assert_param(IS_FUNCTIONAL_STATE(NewState));
656 if (NewState != DISABLE)
657 {
658 /* Enable temperature sensor */
659 ADC1->ADCFG|=ADCFG_TSEN_Set;
660
661 /* Enable the temperature sensor channel*/
662 ADC1->ADCHS |= ADCHS_TSVREFE_Set ;
663 }
664 else
665 {
666 /* Disbale temperature sensor */
667 ADC1->ADCFG&=~ADCFG_TSEN_Set;
668
669 /* Disable the temperature sensor channel*/
670 ADC1->ADCHS &= ADCHS_TSVREFE_Reset;
671
672 }
673 }
674
675 /**
676 * @brief Enables or disables the temperature sensor and Vrefint channel.
677 * @param NewState: new state of the temperature sensor.
678 * This parameter can be: ENABLE or DISABLE.
679 * @retval : None
680 */
ADC_VrefintCmd(FunctionalState NewState)681 void ADC_VrefintCmd(FunctionalState NewState)
682 {
683 /* Check the parameters */
684 assert_param(IS_FUNCTIONAL_STATE(NewState));
685 if (NewState != DISABLE)
686 {
687 /* Enable temperature sensor */
688 ADC1->ADCFG|=ADCFG_VSEN_Set;
689
690 /* Enable the temperature sensor and Vrefint channel*/
691 ADC1->ADCHS |= ADCHS_VSVREFE_Set ;
692 }
693 else
694 {
695 /* Disbale temperature sensor */
696 ADC1->ADCFG&=~ADCFG_VSEN_Set;
697
698 /* Disable the temperature sensor and Vrefint channel*/
699 ADC1->ADCHS &= ADCHS_VSVREFE_Reset;
700
701 }
702 }
703
704 /**
705 * @brief Checks whether the specified ADC flag is set or not.
706 * @param ADCx: where x can be 1, 2 or 3 to select the ADC peripheral.
707 * @param ADC_FLAG: specifies the flag to check.
708 * This parameter can be one of the following values:
709 * @arg ADC_FLAG_AWD: Analog watchdog flag
710 * @arg ADC_FLAG_EOC: End of conversion flag
711 * @arg ADC_FLAG_BUSY: AD conversion busy flag
712 * @retval : The new state of ADC_FLAG (SET or RESET).
713 */
ADC_GetFlagStatus(ADC_TypeDef * ADCx,uint8_t ADC_FLAG)714 FlagStatus ADC_GetFlagStatus(ADC_TypeDef* ADCx, uint8_t ADC_FLAG)
715 {
716 FlagStatus bitstatus = RESET;
717 /* Check the parameters */
718 assert_param(IS_ADC_ALL_PERIPH(ADCx));
719 assert_param(IS_ADC_GET_FLAG(ADC_FLAG));
720 /* Check the status of the specified ADC flag */
721 if ((ADCx->ADSTA & ADC_FLAG) != (uint8_t)RESET)
722 {
723 /* ADC_FLAG is set */
724 bitstatus = SET;
725 }
726 else
727 {
728 /* ADC_FLAG is reset */
729 bitstatus = RESET;
730 }
731
732 /* Return the ADC_FLAG status */
733 return bitstatus;
734 }
735
736 /**
737 * @brief Clears the ADCx's pending flags.
738 * @param ADCx: where x can be 1, 2 to select the ADC peripheral.
739 * @param ADC_FLAG: specifies the flag to clear.
740 * This parameter can be any combination of the following values:
741 * @arg ADC_FLAG_AWD: Analog watchdog flag
742 * @arg ADC_FLAG_EOC: End of conversion flag
743 * @retval : None
744 */
ADC_ClearFlag(ADC_TypeDef * ADCx,uint8_t ADC_FLAG)745 void ADC_ClearFlag(ADC_TypeDef* ADCx, uint8_t ADC_FLAG)
746 {
747 /* Check the parameters */
748 assert_param(IS_ADC_ALL_PERIPH(ADCx));
749 assert_param(IS_ADC_CLEAR_FLAG(ADC_FLAG));
750 /* Clear the selected ADC flags */
751 ADCx->ADSTA |= ADC_FLAG;
752 }
753
754 /**
755 * @brief Checks whether the specified ADC interrupt has occurred or not.
756 * @param ADCx: where x can be 1, 2 or 3 to select the ADC peripheral.
757 * @param ADC_IT: specifies the ADC interrupt source to check.
758 * This parameter can be one of the following values:
759 * @arg ADC_IT_EOC: End of conversion interrupt mask
760 * @arg ADC_IT_AWD: Analog watchdog interrupt mask
761 * @retval : The new state of ADC_IT (SET or RESET).
762 */
ADC_GetITStatus(ADC_TypeDef * ADCx,uint16_t ADC_IT)763 ITStatus ADC_GetITStatus(ADC_TypeDef* ADCx, uint16_t ADC_IT)
764 {
765 ITStatus bitstatus = RESET;
766
767 /* Check the parameters */
768 assert_param(IS_ADC_ALL_PERIPH(ADCx));
769 assert_param(IS_ADC_GET_IT(ADC_IT));
770
771 /* Check the status of the specified ADC interrupt */
772 if (((ADCx->ADSTA & ADC_IT)) != (uint32_t)RESET)
773 {
774 /* ADC_IT is set */
775 bitstatus = SET;
776 }
777 else
778 {
779 /* ADC_IT is reset */
780 bitstatus = RESET;
781 }
782 /* Return the ADC_IT status */
783 return bitstatus;
784 }
785
786 /**
787 * @brief Clears the ADCx's interrupt pending bits.
788 * @param ADCx: where x can be 1, 2 or 3 to select the ADC peripheral.
789 * @param ADC_IT: specifies the ADC interrupt pending bit to clear.
790 * This parameter can be any combination of the following values:
791 * @arg ADC_IT_EOC: End of conversion interrupt mask
792 * @arg ADC_IT_AWD: Analog watchdog interrupt mask
793 * @retval : None
794 */
ADC_ClearITPendingBit(ADC_TypeDef * ADCx,uint16_t ADC_IT)795 void ADC_ClearITPendingBit(ADC_TypeDef* ADCx, uint16_t ADC_IT)
796 {
797 /* Check the parameters */
798 assert_param(IS_ADC_ALL_PERIPH(ADCx));
799 assert_param(IS_ADC_IT(ADC_IT));
800 /* Clear the selected ADC interrupt pending bits */
801 ADCx->ADSTA = ADC_IT;
802 }
803
804 /**
805 * @}
806 */
807
808 /**
809 * @}
810 */
811
812 /**
813 * @}
814 */
815
816 /*-------------------------(C) COPYRIGHT 2017 MindMotion ----------------------*/
817