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