1 /*!
2 * @file apm32s10x_adc.c
3 *
4 * @brief This file provides all the ADC firmware functions
5 *
6 * @version V1.0.1
7 *
8 * @date 2022-12-31
9 *
10 * @attention
11 *
12 * Copyright (C) 2022-2023 Geehy Semiconductor
13 *
14 * You may not use this file except in compliance with the
15 * GEEHY COPYRIGHT NOTICE (GEEHY SOFTWARE PACKAGE LICENSE).
16 *
17 * The program is only for reference, which is distributed in the hope
18 * that it will be usefull and instructional for customers to develop
19 * their software. Unless required by applicable law or agreed to in
20 * writing, the program is distributed on an "AS IS" BASIS, WITHOUT
21 * ANY WARRANTY OR CONDITIONS OF ANY KIND, either express or implied.
22 * See the GEEHY SOFTWARE PACKAGE LICENSE for the governing permissions
23 * and limitations under the License.
24 */
25
26 /* Includes */
27 #include "apm32s10x_adc.h"
28 #include "apm32s10x_rcm.h"
29
30 /** @addtogroup APM32S10x_StdPeriphDriver
31 @{
32 */
33
34 /** @addtogroup ADC_Driver ADC Driver
35 @{
36 */
37
38 /** @defgroup ADC_Functions Functions
39 @{
40 */
41
42 /*!
43 * @brief Reset ADC peripheral registers to their default reset values.
44 *
45 * @param adc: Select ADC peripheral.
46 *
47 * @retval None
48 *
49 * @note adc can be ADC1, ADC2.
50 */
ADC_Reset(ADC_T * adc)51 void ADC_Reset(ADC_T* adc)
52 {
53 if (adc == ADC1)
54 {
55 RCM_EnableAPB2PeriphReset(RCM_APB2_PERIPH_ADC1);
56 RCM_DisableAPB2PeriphReset(RCM_APB2_PERIPH_ADC1);
57 }
58 else if (adc == ADC2)
59 {
60 RCM_EnableAPB2PeriphReset(RCM_APB2_PERIPH_ADC2);
61 RCM_DisableAPB2PeriphReset(RCM_APB2_PERIPH_ADC2);
62 }
63 }
64
65 /*!
66 * @brief Config the ADC peripheral according to the specified parameters in the adcConfig.
67 *
68 * @param adc: Select ADC peripheral.
69 *
70 * @param adcConfig: pointer to a ADC_Config_T structure.
71 *
72 * @retval None
73 *
74 * @note adc can be ADC1, ADC2.
75 */
ADC_Config(ADC_T * adc,ADC_Config_T * adcConfig)76 void ADC_Config(ADC_T* adc, ADC_Config_T* adcConfig)
77 {
78 uint32_t reg;
79
80 reg = adc->CTRL1;
81 reg &= 0xFFF0FEFF;
82 reg |= (uint32_t)((adcConfig->mode) | ((uint32_t)adcConfig->scanConvMode << 8));
83 adc->CTRL1 = reg;
84
85 reg = adc->CTRL2;
86 reg &= 0xFFF1F7FD;
87 reg |= (uint32_t)adcConfig->dataAlign | \
88 (uint32_t)adcConfig->externalTrigConv | \
89 ((uint32_t)adcConfig->continuosConvMode << 1);
90
91 adc->CTRL2 = reg;
92
93 reg = adc->REGSEQ1;
94 reg &= 0xFF0FFFFF;
95 reg |= (uint32_t)((adcConfig->nbrOfChannel - (uint8_t)1) << 20);
96 adc->REGSEQ1 = reg;
97 }
98
99 /*!
100 * @brief Fills each ADC_Config_T member with its default value.
101 *
102 * @param adcConfig: pointer to a ADC_Config_T structure which will be initialized.
103 *
104 * @retval None
105 */
ADC_ConfigStructInit(ADC_Config_T * adcConfig)106 void ADC_ConfigStructInit(ADC_Config_T* adcConfig)
107 {
108 adcConfig->mode = ADC_MODE_INDEPENDENT;
109 adcConfig->scanConvMode = DISABLE;
110 adcConfig->continuosConvMode = DISABLE;
111 adcConfig->externalTrigConv = ADC_EXT_TRIG_CONV_TMR1_CC1;
112 adcConfig->dataAlign = ADC_DATA_ALIGN_RIGHT;
113 adcConfig->nbrOfChannel = 1;
114 }
115
116 /*!
117 * @brief Enables the specified ADC peripheral.
118 *
119 * @param adc: Select the ADC peripheral.
120 *
121 * @retval None
122 *
123 * @note adc can be ADC1, ADC2.
124 */
ADC_Enable(ADC_T * adc)125 void ADC_Enable(ADC_T* adc)
126 {
127 adc->CTRL2_B.ADCEN = BIT_SET;
128 }
129
130 /*!
131 * @brief Disable the specified ADC peripheral.
132 *
133 * @param adc: Select the ADC peripheral.
134 *
135 * @retval None
136 *
137 * @note adc can be ADC1, ADC2.
138 */
ADC_Disable(ADC_T * adc)139 void ADC_Disable(ADC_T* adc)
140 {
141 adc->CTRL2_B.ADCEN = BIT_RESET;
142 }
143
144 /*!
145 * @brief Disable the specified ADC DMA request.
146 *
147 * @param adc: Select the ADC peripheral.
148 *
149 * @retval None
150 *
151 * @note adc can be ADC1, ADC2.
152 */
ADC_EnableDMA(ADC_T * adc)153 void ADC_EnableDMA(ADC_T* adc)
154 {
155 adc->CTRL2_B.DMAEN = BIT_SET;
156 }
157
158 /*!
159 * @brief Disable the specified ADC DMA request.
160 *
161 * @param adc: Select the ADC peripheral.
162 *
163 * @retval None
164 *
165 * @note adc can be ADC1, ADC2.
166 */
ADC_DisableDMA(ADC_T * adc)167 void ADC_DisableDMA(ADC_T* adc)
168 {
169 adc->CTRL2_B.DMAEN = BIT_RESET;
170 }
171
172 /*!
173 * @brief Reset the specified ADC calibration registers.
174 *
175 * @param adc: Select the ADC peripheral.
176 *
177 * @retval None
178 *
179 * @note adc can be ADC1, ADC2.
180 */
ADC_ResetCalibration(ADC_T * adc)181 void ADC_ResetCalibration(ADC_T* adc)
182 {
183 adc->CTRL2_B.CALRST = BIT_SET;
184 }
185
186 /*!
187 * @brief Reads the specified ADC calibration reset status.
188 *
189 * @param adc: Select the ADC peripheral.
190 *
191 * @retval The status of ADC calibration reset.
192 *
193 * @note adc can be ADC1, ADC2.
194 */
ADC_ReadResetCalibrationStatus(ADC_T * adc)195 uint8_t ADC_ReadResetCalibrationStatus(ADC_T* adc)
196 {
197 uint8_t ret;
198 ret = (adc->CTRL2_B.CALRST) ? BIT_SET : BIT_RESET;
199 return ret;
200 }
201
202 /*!
203 * @brief Starts the specified ADC calibration.
204 *
205 * @param adc: Select the ADC peripheral.
206 *
207 * @retval None
208 *
209 * @note adc can be ADC1, ADC2.
210 */
ADC_StartCalibration(ADC_T * adc)211 void ADC_StartCalibration(ADC_T* adc)
212 {
213 adc->CTRL2_B.CAL = BIT_SET;
214 }
215
216 /*!
217 * @brief Reads the specified ADC calibration start flag.
218 *
219 * @param adc: Select the ADC peripheral.
220 *
221 * @retval The status of ADC calibration start.
222 *
223 * @note adc can be ADC1, ADC2.
224 */
ADC_ReadCalibrationStartFlag(ADC_T * adc)225 uint8_t ADC_ReadCalibrationStartFlag(ADC_T* adc)
226 {
227 uint8_t ret;
228 ret = (adc->CTRL2_B.CAL) ? BIT_SET : BIT_RESET;
229 return ret;
230 }
231
232 /*!
233 * @brief Enables the specified ADC software start conversion.
234 *
235 * @param adc: Select the ADC peripheral.
236 *
237 * @retval None
238 *
239 * @note adc can be ADC1, ADC2.
240 */
ADC_EnableSoftwareStartConv(ADC_T * adc)241 void ADC_EnableSoftwareStartConv(ADC_T* adc)
242 {
243 adc->CTRL2 |= 0x00500000;
244 }
245
246 /*!
247 * @brief Disable the specified ADC software start conversion.
248 *
249 * @param adc: Select the ADC peripheral.
250 *
251 * @retval None
252 *
253 * @note adc can be ADC1, ADC2.
254 */
ADC_DisableSoftwareStartConv(ADC_T * adc)255 void ADC_DisableSoftwareStartConv(ADC_T* adc)
256 {
257 adc->CTRL2 &= 0xFFAFFFFF;
258 }
259
260 /*!
261 * @brief Reads the specified ADC Software start conversion Status.
262 *
263 * @param adc: Select the ADC peripheral.
264 *
265 * @retval The status of ADC Software start conversion registers.
266 *
267 * @note adc can be ADC1, ADC2.
268 */
ADC_ReadSoftwareStartConvStatus(ADC_T * adc)269 uint8_t ADC_ReadSoftwareStartConvStatus(ADC_T* adc)
270 {
271 uint8_t ret;
272 ret = (adc->CTRL2_B.REGSWSC) ? BIT_SET : BIT_RESET;
273 return ret;
274 }
275
276 /*!
277 * @brief Configures the specified ADC regular discontinuous mode.
278 *
279 * @param adc: Select the ADC peripheral.
280 *
281 * @param number: The number of the discontinuous mode regular channels.
282 * This parameter can be between 1 and 8.
283 *
284 * @retval None
285 *
286 * @note adc can be ADC1, ADC2.
287 */
ADC_ConfigDiscMode(ADC_T * adc,uint8_t number)288 void ADC_ConfigDiscMode(ADC_T* adc, uint8_t number)
289 {
290 adc->CTRL1_B.DISCNUMCFG |= number - 1;
291 }
292
293 /*!
294 * @brief Enable the specified ADC regular discontinuous mode.
295 *
296 * @param adc: Select the ADC peripheral.
297 *
298 * @retval None
299 *
300 * @note adc can be ADC1, ADC2.
301 */
ADC_EnableDiscMode(ADC_T * adc)302 void ADC_EnableDiscMode(ADC_T* adc)
303 {
304 adc->CTRL1_B.REGDISCEN = BIT_SET;
305 }
306
307 /*!
308 * @brief Disable the specified ADC regular discontinuous mode.
309 *
310 * @param adc: Select the ADC peripheral.
311 *
312 * @retval None
313 *
314 * @note adc can be ADC1, ADC2.
315 */
ADC_DisableDiscMode(ADC_T * adc)316 void ADC_DisableDiscMode(ADC_T* adc)
317 {
318 adc->CTRL1_B.REGDISCEN = BIT_RESET;
319 }
320
321 /*!
322 * @brief Configures the specified ADC regular channel.
323 *
324 * @param adc: Select the ADC peripheral.
325 *
326 * @param channel: Select the ADC channel.
327 * This parameter can be one of the following values:
328 * @arg ADC_CHANNEL_0: ADC channel 0
329 * @arg ADC_CHANNEL_1: ADC channel 1
330 * @arg ADC_CHANNEL_2: ADC channel 2
331 * @arg ADC_CHANNEL_3: ADC channel 3
332 * @arg ADC_CHANNEL_4: ADC channel 4
333 * @arg ADC_CHANNEL_5: ADC channel 5
334 * @arg ADC_CHANNEL_6: ADC channel 6
335 * @arg ADC_CHANNEL_7: ADC channel 7
336 * @arg ADC_CHANNEL_8: ADC channel 8
337 * @arg ADC_CHANNEL_9: ADC channel 9
338 * @arg ADC_CHANNEL_10: ADC channel 10
339 * @arg ADC_CHANNEL_11: ADC channel 11
340 * @arg ADC_CHANNEL_12: ADC channel 12
341 * @arg ADC_CHANNEL_13: ADC channel 13
342 * @arg ADC_CHANNEL_14: ADC channel 14
343 * @arg ADC_CHANNEL_15: ADC channel 15
344 * @arg ADC_CHANNEL_16: ADC channel 16 which is connected to TempSensor
345 * @arg ADC_CHANNEL_17: ADC channel 17 which is connected to Vrefint
346 *
347 * @param rank: The rank in the regular group sequencer
348 * This parameter must be between 1 to 16.
349 *
350 * @param sampleTime: the specified ADC channel SampleTime
351 * The parameter can be one of following values:
352 * @arg ADC_SAMPLETIME_1CYCLES5: ADC 1.5 clock cycles
353 * @arg ADC_SAMPLETIME_7CYCLES5: ADC 7.5 clock cycles
354 * @arg ADC_SAMPLETIME_13CYCLES5: ADC 13.5 clock cycles
355 * @arg ADC_SAMPLETIME_28CYCLES5: ADC 28.5 clock cycles
356 * @arg ADC_SAMPLETIME_41CYCLES5: ADC 41.5 clock cycles
357 * @arg ADC_SAMPLETIME_55CYCLES5: ADC 55.5 clock cycles
358 * @arg ADC_SAMPLETIME_71CYCLES5: ADC 71.5 clock cycles
359 * @arg ADC_SAMPLETIME_239CYCLES5: ADC 239.5 clock cycles
360 *
361 * @retval None
362 *
363 * @note adc can be ADC1, ADC2.
364 */
ADC_ConfigRegularChannel(ADC_T * adc,uint8_t channel,uint8_t rank,uint8_t sampleTime)365 void ADC_ConfigRegularChannel(ADC_T* adc, uint8_t channel, uint8_t rank, uint8_t sampleTime)
366 {
367 uint32_t temp1 = 0;
368 uint32_t temp2 = 0;
369 if (channel > ADC_CHANNEL_9)
370 {
371 temp1 = adc->SMPTIM1;
372 temp2 = SMPCYCCFG_SET_SMPTIM1 << (3 * (channel - 10));
373 temp1 &= ~temp2;
374 temp2 = (uint32_t)sampleTime << (3 * (channel - 10));
375 temp1 |= temp2;
376 adc->SMPTIM1 = temp1;
377 }
378 else
379 {
380 temp1 = adc->SMPTIM2;
381 temp2 = SMPCYCCFG_SET_SMPTIM2 << (3 * channel);
382 temp1 &= ~temp2;
383 temp2 = (uint32_t)sampleTime << (3 * channel);
384 temp1 |= temp2;
385 adc->SMPTIM2 = temp1;
386 }
387
388 if (rank < 7)
389 {
390 temp1 = adc->REGSEQ3;
391 temp2 = REGSEQC_SET_REGSEQ3 << (5 * (rank - 1));
392 temp1 &= ~temp2;
393 temp2 = (uint32_t)channel << (5 * (rank - 1));
394 temp1 |= temp2;
395 adc->REGSEQ3 = temp1;
396 }
397 else if (rank < 13)
398 {
399 temp1 = adc->REGSEQ2;
400 temp2 = REGSEQC_SET_REGSEQ2 << (5 * (rank - 7));
401 temp1 &= ~temp2;
402 temp2 = (uint32_t)channel << (5 * (rank - 7));
403 temp1 |= temp2;
404 adc->REGSEQ2 = temp1;
405 }
406 else
407 {
408 temp1 = adc->REGSEQ1;
409 temp2 = REGSEQC_SET_REGSEQ1 << (5 * (rank - 13));
410 temp1 &= ~temp2;
411 temp2 = (uint32_t)channel << (5 * (rank - 13));
412 temp1 |= temp2;
413 adc->REGSEQ1 = temp1;
414 }
415 }
416
417 /*!
418 * @brief Enable the specified ADC regular channel external trigger.
419 *
420 * @param adc: Select the ADC peripheral.
421 *
422 * @retval None
423 *
424 * @note adc can be ADC1, ADC2.
425 */
ADC_EnableExternalTrigConv(ADC_T * adc)426 void ADC_EnableExternalTrigConv(ADC_T* adc)
427 {
428 adc->CTRL2_B.REGEXTTRGEN = BIT_SET;
429 }
430
431 /*!
432 * @brief Disable the specified ADC regular channel external trigger.
433 *
434 * @param adc: Select the ADC peripheral.
435 *
436 * @retval None
437 *
438 * @note adc can be ADC1, ADC2.
439 */
ADC_DisableExternalTrigConv(ADC_T * adc)440 void ADC_DisableExternalTrigConv(ADC_T* adc)
441 {
442 adc->CTRL2_B.REGEXTTRGEN = BIT_RESET;
443 }
444
445 /*!
446 * @brief Reads the specified ADC conversion result data.
447 *
448 * @param adc: Select the ADC peripheral.
449 *
450 * @retval The Data conversion value.
451 *
452 * @note adc can be ADC1, ADC2.
453 */
ADC_ReadConversionValue(ADC_T * adc)454 uint16_t ADC_ReadConversionValue(ADC_T* adc)
455 {
456 return (uint16_t) adc->REGDATA;
457 }
458
459 /*!
460 * @brief Reads the specified ADC conversion result data in dual mode.
461 *
462 * @param adc: Select the ADC peripheral.
463 *
464 * @retval The Data conversion value.
465 *
466 * @note adc can be ADC1, ADC2.
467 */
ADC_ReadDualModeConversionValue(ADC_T * adc)468 uint32_t ADC_ReadDualModeConversionValue(ADC_T* adc)
469 {
470 return (*(__IOM uint32_t*) RDG_ADDRESS);
471 }
472
473 /*!
474 * @brief Enable the specified ADC automatic injected group.
475 *
476 * @param adc: Select the ADC peripheral.
477 *
478 * @retval None
479 *
480 * @note adc can be ADC1, ADC2.
481 */
ADC_EnableAutoInjectedConv(ADC_T * adc)482 void ADC_EnableAutoInjectedConv(ADC_T* adc)
483 {
484 adc->CTRL1_B.INJGACEN = BIT_SET;
485 }
486
487 /*!
488 * @brief Disable the specified ADC automatic injected group.
489 *
490 * @param adc: Select the ADC peripheral.
491 *
492 * @retval None
493 *
494 * @note adc can be ADC1, ADC2.
495 */
ADC_DisableAutoInjectedConv(ADC_T * adc)496 void ADC_DisableAutoInjectedConv(ADC_T* adc)
497 {
498 adc->CTRL1_B.INJGACEN = BIT_RESET;
499 }
500
501 /*!
502 * @brief Enable the specified ADC discontinuous mode for injected group.
503 *
504 * @param adc: Select the ADC peripheral.
505 *
506 * @retval None
507 *
508 * @note adc can be ADC1, ADC2.
509 */
ADC_EnableInjectedDiscMode(ADC_T * adc)510 void ADC_EnableInjectedDiscMode(ADC_T* adc)
511 {
512 adc->CTRL1_B.INJDISCEN = BIT_SET;
513 }
514
515 /*!
516 * @brief Disable the specified ADC discontinuous mode for injected group.
517 *
518 * @param adc: Select the ADC peripheral.
519 *
520 * @retval None
521 *
522 * @note adc can be ADC1, ADC2.
523 */
ADC_DisableInjectedDiscMode(ADC_T * adc)524 void ADC_DisableInjectedDiscMode(ADC_T* adc)
525 {
526 adc->CTRL1_B.INJDISCEN = BIT_RESET;
527 }
528
529 /*!
530 * @brief Configures the specified ADC external trigger for injected channels conversion
531 *
532 * @param adc: Select the ADC peripheral
533 *
534 * @param extTrigInjecConv: Select the ADC trigger to start injected conversion
535 * This parameter can be one of the following values:
536 * @arg ADC_EXT_TRIG_INJEC_CONV_TMR1_TRGO : Select Timer1 TRGO event
537 * @arg ADC_EXT_TRIG_INJEC_CONV_TMR1_CC4 : Select Timer1 capture compare4
538 * @arg ADC_EXT_TRIG_INJEC_CONV_TMR2_TRGO : Select Timer2 TRGO event
539 * @arg ADC_EXT_TRIG_INJEC_CONV_TMR2_CC1 : Select Timer2 capture compare1
540 * @arg ADC_EXT_TRIG_INJEC_CONV_TMR3_CC4 : Select Timer3 capture compare4
541 * @arg ADC_EXT_TRIG_INJEC_CONV_TMR4_TRGO : Select Timer4 TRGO event selected
542 * @arg ADC_EXT_TRIG_INJEC_CONV_EINT15 : External interrupt line 15
543 * @arg ADC_EXT_TRIG_INJEC_CONV_NONE : Injected conversion started by software instead of external trigger
544 *
545 * @retval None
546 *
547 * @note adc can be ADC1, ADC2.
548 */
ADC_ConfigExternalTrigInjectedConv(ADC_T * adc,ADC_EXT_TRIG_INJEC_CONV_T extTrigInjecConv)549 void ADC_ConfigExternalTrigInjectedConv(ADC_T* adc, ADC_EXT_TRIG_INJEC_CONV_T extTrigInjecConv)
550 {
551 adc->CTRL2_B.INJGEXTTRGSEL = RESET;
552 adc->CTRL2_B.INJGEXTTRGSEL |= extTrigInjecConv;
553 }
554
555 /*!
556 * @brief Ensable the specified ADC injected channels conversion through
557 *
558 * @param adc: Select the ADC peripheral
559 *
560 * @retval None
561 *
562 * @note adc can be ADC1, ADC2.
563 */
ADC_EnableExternalTrigInjectedConv(ADC_T * adc)564 void ADC_EnableExternalTrigInjectedConv(ADC_T* adc)
565 {
566 adc->CTRL2_B.INJEXTTRGEN = BIT_SET;
567 }
568
569 /*!
570 * @brief Disable the specified ADC injected channels conversion through
571 *
572 * @param adc: Select the ADC peripheral
573 *
574 * @retval None
575 *
576 * @note adc can be ADC1, ADC2.
577 */
ADC_DisableExternalTrigInjectedConv(ADC_T * adc)578 void ADC_DisableExternalTrigInjectedConv(ADC_T* adc)
579 {
580 adc->CTRL2_B.INJEXTTRGEN = BIT_RESET;
581 }
582
583 /*!
584 * @brief Enable the specified ADC start of the injected
585 *
586 * @param adc: Select the ADC peripheral
587 *
588 * @retval None
589 *
590 * @note adc can be ADC1, ADC2.
591 */
ADC_EnableSoftwareStartInjectedConv(ADC_T * adc)592 void ADC_EnableSoftwareStartInjectedConv(ADC_T* adc)
593 {
594 adc->CTRL2_B.INJEXTTRGEN = BIT_SET;
595 adc->CTRL2_B.INJSWSC = BIT_SET;
596 }
597
598 /*!
599 * @brief Disable the specified ADC start of the injected
600 *
601 * @param adc: Select the ADC peripheral
602 *
603 * @retval None
604 *
605 * @note adc can be ADC1, ADC2.
606 */
ADC_DisableSoftwareStartInjectedConv(ADC_T * adc)607 void ADC_DisableSoftwareStartInjectedConv(ADC_T* adc)
608 {
609 adc->CTRL2_B.INJEXTTRGEN = BIT_RESET;
610 adc->CTRL2_B.INJSWSC = BIT_RESET;
611 }
612
613 /*!
614 * @brief Reads the specified ADC Software start injected conversion Status
615 *
616 * @param adc: Select the ADC peripheral
617 *
618 * @retval The status of ADC Software start injected conversion
619 *
620 * @note adc can be ADC1, ADC2.
621 */
ADC_ReadSoftwareStartInjectedConvStatus(ADC_T * adc)622 uint8_t ADC_ReadSoftwareStartInjectedConvStatus(ADC_T* adc)
623 {
624 uint8_t ret;
625 ret = (adc->CTRL2_B.INJSWSC) ? BIT_SET : BIT_RESET;
626 return ret;
627 }
628
629 /*!
630 * @brief Configures the specified ADC injected channel.
631 *
632 * @param adc: Select the ADC peripheral.
633 *
634 * @param channel: Select the ADC injected channel.
635 * This parameter can be one of the following values:
636 * @arg ADC_CHANNEL_0: ADC channel 0
637 * @arg ADC_CHANNEL_1: ADC channel 1
638 * @arg ADC_CHANNEL_2: ADC channel 2
639 * @arg ADC_CHANNEL_3: ADC channel 3
640 * @arg ADC_CHANNEL_4: ADC channel 4
641 * @arg ADC_CHANNEL_5: ADC channel 5
642 * @arg ADC_CHANNEL_6: ADC channel 6
643 * @arg ADC_CHANNEL_7: ADC channel 7
644 * @arg ADC_CHANNEL_8: ADC channel 8
645 * @arg ADC_CHANNEL_9: ADC channel 9
646 * @arg ADC_CHANNEL_10: ADC channel 10
647 * @arg ADC_CHANNEL_11: ADC channel 11
648 * @arg ADC_CHANNEL_12: ADC channel 12
649 * @arg ADC_CHANNEL_13: ADC channel 13
650 * @arg ADC_CHANNEL_14: ADC channel 14
651 * @arg ADC_CHANNEL_15: ADC channel 15
652 * @arg ADC_CHANNEL_16: ADC channel 16 which is connected to TempSensor
653 * @arg ADC_CHANNEL_17: ADC channel 17 which is connected to Vrefint
654 *
655 * @param rank: The rank in the injected group sequencer.
656 * This parameter must be between 1 to 4.
657 *
658 * @param sampleTime: the specified ADC channel SampleTime
659 * The parameter can be one of following values:
660 * @arg ADC_SAMPLETIME_1CYCLES5: ADC 1.5 clock cycles
661 * @arg ADC_SAMPLETIME_7CYCLES5: ADC 7.5 clock cycles
662 * @arg ADC_SAMPLETIME_13CYCLES5: ADC 13.5 clock cycles
663 * @arg ADC_SAMPLETIME_28CYCLES5: ADC 28.5 clock cycles
664 * @arg ADC_SAMPLETIME_41CYCLES5: ADC 41.5 clock cycles
665 * @arg ADC_SAMPLETIME_55CYCLES5: ADC 55.5 clock cycles
666 * @arg ADC_SAMPLETIME_71CYCLES5: ADC 71.5 clock cycles
667 * @arg ADC_SAMPLETIME_239CYCLES5: ADC 239.5 clock cycles
668 *
669 * @retval None
670 *
671 * @note adc can be ADC1, ADC2.
672 */
ADC_ConfigInjectedChannel(ADC_T * adc,uint8_t channel,uint8_t rank,uint8_t sampleTime)673 void ADC_ConfigInjectedChannel(ADC_T* adc, uint8_t channel, uint8_t rank, uint8_t sampleTime)
674 {
675 uint32_t temp1 = 0;
676 uint32_t temp2 = 0;
677 uint32_t temp3 = 0;
678 if (channel > ADC_CHANNEL_9)
679 {
680 temp1 = adc->SMPTIM1;
681 temp2 = SMPCYCCFG_SET_SMPTIM1 << (3 * (channel - 10));
682 temp1 &= ~temp2;
683 temp2 = (uint32_t)sampleTime << (3 * (channel - 10));
684 temp1 |= temp2;
685 adc->SMPTIM1 = temp1;
686 }
687 else
688 {
689 temp1 = adc->SMPTIM2;
690 temp2 = SMPCYCCFG_SET_SMPTIM2 << (3 * channel);
691 temp1 &= ~temp2;
692 temp2 = (uint32_t)sampleTime << (3 * channel);
693 temp1 |= temp2;
694 adc->SMPTIM2 = temp1;
695 }
696 temp1 = adc->INJSEQ;
697 temp3 = (temp1 & INJSEQ_SET_INJSEQLEN) >> 20;
698 temp2 = INJSEQ_SET_INJSEQC << (5 * (uint8_t)((rank + 3) - (temp3 + 1)));
699 temp1 &= ~temp2;
700 temp2 = (uint32_t)channel << (5 * (uint8_t)((rank + 3) - (temp3 + 1)));
701 temp1 |= temp2;
702 adc->INJSEQ = temp1;
703 }
704
705 /*!
706 * @brief Configures the specified ADC injected channel.
707 *
708 * @param adc: Select the ADC peripheral.
709 *
710 * @param length: The sequencer length.
711 * This parameter must be a number between 1 to 4.
712 *
713 * @retval None
714 *
715 * @note adc can be ADC1, ADC2.
716 */
ADC_ConfigInjectedSequencerLength(ADC_T * adc,uint8_t length)717 void ADC_ConfigInjectedSequencerLength(ADC_T* adc, uint8_t length)
718 {
719 adc->INJSEQ_B.INJSEQLEN = RESET;
720 adc->INJSEQ_B.INJSEQLEN |= length - 1;
721 }
722
723 /*!
724 * @brief Configures the specified ADC injected channel conversion value offset.
725 *
726 * @param adc: Select the ADC peripheral.
727 *
728 * @param channel: Select the ADC injected channel.
729 * This parameter can be one of the following values:
730 * @arg ADC_INJEC_CHANNEL_1: select Injected Channel 1
731 * @arg ADC_INJEC_CHANNEL_2: select Injected Channel 2
732 * @arg ADC_INJEC_CHANNEL_3: select Injected Channel 3
733 * @arg ADC_INJEC_CHANNEL_4: select Injected Channel 4
734 *
735 * @param offSet: The specified ADC injected channel offset.
736 * This parameter must be a 12bit value.
737 *
738 * @retval None
739 *
740 * @note adc can be ADC1, ADC2.
741 */
ADC_ConfigInjectedOffset(ADC_T * adc,ADC_INJEC_CHANNEL_T channel,uint16_t offSet)742 void ADC_ConfigInjectedOffset(ADC_T* adc, ADC_INJEC_CHANNEL_T channel, uint16_t offSet)
743 {
744 __IOM uint32_t tmp = 0;
745
746 tmp = (uint32_t)adc;
747 tmp += channel;
748
749 *(__IOM uint32_t*) tmp = (uint32_t)offSet;
750 }
751
752 /*!
753 * @brief Reads the ADC injected channel conversion value.
754 *
755 * @param adc: Select the ADC peripheral.
756 *
757 * @param channel: Select the ADC injected channel.
758 * This parameter can be one of the following values:
759 * @arg ADC_INJEC_CHANNEL_1: select Injected Channel 1
760 * @arg ADC_INJEC_CHANNEL_2: select Injected Channel 2
761 * @arg ADC_INJEC_CHANNEL_3: select Injected Channel 3
762 * @arg ADC_INJEC_CHANNEL_4: select Injected Channel 4
763 *
764 * @retval The Data of conversion value.
765 *
766 * @note adc can be ADC1, ADC2.
767 */
ADC_ReadInjectedConversionValue(ADC_T * adc,ADC_INJEC_CHANNEL_T channel)768 uint16_t ADC_ReadInjectedConversionValue(ADC_T* adc, ADC_INJEC_CHANNEL_T channel)
769 {
770 __IOM uint32_t temp = 0;
771
772 temp = (uint32_t)adc;
773 temp += channel + INJDATA_OFFSET;
774
775 return (uint16_t)(*(__IOM uint32_t*) temp);
776 }
777
778 /*!
779 * @brief Enable the specified ADC analog watchdog.
780 *
781 * @param adc: Select the ADC peripheral.
782 *
783 * @param analogWatchdog: The ADC analog watchdog configuration
784 * This parameter can be one of the following values:
785 * @arg ADC_ANALOG_WATCHDOG_SINGLE_REG : Analog watchdog on a single regular channel
786 * @arg ADC_ANALOG_WATCHDOG_SINGLE_INJEC : Analog watchdog on a single injected channel
787 * @arg ADC_ANALOG_WATCHDOG_SINGLE_REG_INJEC : Analog watchdog on a single regular or injected channel
788 * @arg ADC_ANALOG_WATCHDOG_ALL_REG : Analog watchdog on all regular channel
789 * @arg ADC_ANALOG_WATCHDOG_ALL_INJEC : Analog watchdog on all injected channel
790 * @arg ADC_ANALOG_WATCHDOG_ALL_REG_ALL_INJEC : Analog watchdog on all regular and injected channels
791 * @arg ADC_ANALOG_WATCHDOG_NONE : No channel guarded by the analog watchdog
792 *
793 * @retval None
794 *
795 * @note adc can be ADC1, ADC2.
796 */
ADC_EnableAnalogWatchdog(ADC_T * adc,uint32_t analogWatchdog)797 void ADC_EnableAnalogWatchdog(ADC_T* adc, uint32_t analogWatchdog)
798 {
799 adc->CTRL1 &= 0xFF3FFDFF;
800 adc->CTRL1 |= analogWatchdog;
801 }
802
803 /*!
804 * @brief Disable the specified ADC analog watchdog.
805 *
806 * @param adc: Select the ADC peripheral.
807 *
808 * @retval None
809 *
810 * @note adc can be ADC1, ADC2.
811 */
ADC_DisableAnalogWatchdog(ADC_T * adc)812 void ADC_DisableAnalogWatchdog(ADC_T* adc)
813 {
814 adc->CTRL1 &= 0xFF3FFDFF;
815 }
816
817 /*!
818 * @brief Configures the specified ADC high and low thresholds of the analog watchdog.
819 *
820 * @param adc: Select the ADC peripheral.
821 *
822 * @param highThreshold: The ADC analog watchdog High threshold value.
823 * This parameter must be a 12bit value.
824 *
825 * @param lowThreshold: The ADC analog watchdog Low threshold value.
826 * This parameter must be a 12bit value.
827 *
828 * @retval None
829 *
830 * @note adc can be ADC1, ADC2.
831 */
ADC_ConfigAnalogWatchdogThresholds(ADC_T * adc,uint16_t highThreshold,uint16_t lowThreshold)832 void ADC_ConfigAnalogWatchdogThresholds(ADC_T* adc, uint16_t highThreshold, uint16_t lowThreshold)
833 {
834 adc->AWDHT = highThreshold;
835 adc->AWDLT = lowThreshold;
836 }
837
838 /*!
839 * @brief Configures the specified ADC analog watchdog guarded single channel
840 *
841 * @param adc: Select the ADC peripheral
842 *
843 * @param channel: Select the ADC channel
844 * This parameter can be one of the following values:
845 * @arg ADC_Channel_0: Select ADC Channel 0
846 * @arg ADC_Channel_1: Select ADC Channel 1
847 * @arg ADC_Channel_2: Select ADC Channel 2
848 * @arg ADC_Channel_3: Select ADC Channel 3
849 * @arg ADC_Channel_4: Select ADC Channel 4
850 * @arg ADC_Channel_5: Select ADC Channel 5
851 * @arg ADC_Channel_6: Select ADC Channel 6
852 * @arg ADC_Channel_7: Select ADC Channel 7
853 * @arg ADC_Channel_8: Select ADC Channel 8
854 * @arg ADC_Channel_9: Select ADC Channel 9
855 * @arg ADC_Channel_10: Select ADC Channel 10
856 * @arg ADC_Channel_11: Select ADC Channel 11
857 * @arg ADC_Channel_12: Select ADC Channel 12
858 * @arg ADC_Channel_13: Select ADC Channel 13
859 * @arg ADC_Channel_14: Select ADC Channel 14
860 * @arg ADC_Channel_15: Select ADC Channel 15
861 * @arg ADC_Channel_16: Select ADC Channel 16 which is connected to TempSensor
862 * @arg ADC_Channel_17: Select ADC Channel 17 which is connected to Vrefint
863 *
864 * @retval None
865 *
866 * @note adc can be ADC1, ADC2.
867 */
ADC_ConfigAnalogWatchdogSingleChannel(ADC_T * adc,uint8_t channel)868 void ADC_ConfigAnalogWatchdogSingleChannel(ADC_T* adc, uint8_t channel)
869 {
870 adc->CTRL1_B.AWDCHSEL = BIT_RESET;
871 adc->CTRL1 |= channel;
872 }
873
874 /*!
875 * @brief Enable the specified ADC temperature sensor and Vrefint channel.
876 *
877 * @param adc: Select the ADC peripheral.
878 *
879 * @retval None
880 *
881 * @note adc can be ADC1, ADC2.
882 */
ADC_EnableTempSensorVrefint(ADC_T * adc)883 void ADC_EnableTempSensorVrefint(ADC_T* adc)
884 {
885 adc->CTRL2_B.TSVREFEN = BIT_SET;
886 }
887
888 /*!
889 * @brief Disable the specified ADC temperature sensor and Vrefint channel.
890 *
891 * @param adc: Select the ADC peripheral
892 *
893 * @retval None
894 *
895 * @note adc can be ADC1, ADC2.
896 */
ADC_DisableTempSensorVrefint(ADC_T * adc)897 void ADC_DisableTempSensorVrefint(ADC_T* adc)
898 {
899 adc->CTRL2_B.TSVREFEN = BIT_RESET;
900 }
901
902 /*!
903 * @brief Enable the specified ADC interrupt.
904 *
905 * @param adc: Select the ADC peripheral.
906 *
907 * @param interrupt: Select the ADC interrupt sources
908 * This parameter can be any combination of the following values:
909 * @arg ADC_INT_AWD : Enable Analog watchdog interrupt
910 * @arg ADC_INT_EOC : Enable End of conversion interrupt
911 * @arg ADC_INT_INJEOC : Enable End of injected conversion interrupt
912 *
913 * @retval None
914 *
915 * @note adc can be ADC1, ADC2.
916 */
ADC_EnableInterrupt(ADC_T * adc,uint16_t interrupt)917 void ADC_EnableInterrupt(ADC_T* adc, uint16_t interrupt)
918 {
919 uint8_t mask;
920
921 mask = (uint8_t)interrupt;
922 adc->CTRL1 |= (uint8_t)mask;
923 }
924
925 /*!
926 * @brief Disable the specified ADC interrupt.
927 *
928 * @param adc: Select the ADC peripheral.
929 *
930 * @param interrupt: Select the ADC interrupt sources
931 * This parameter can be any combination of the following values:
932 * @arg ADC_INT_AWD : Disable Analog watchdog interrupt
933 * @arg ADC_INT_EOC : Disable End of conversion interrupt
934 * @arg ADC_INT_INJEOC : Disable End of injected conversion interrupt
935 *
936 * @retval None
937 *
938 * @note adc can be ADC1, ADC2.
939 */
ADC_DisableInterrupt(ADC_T * adc,uint16_t interrupt)940 void ADC_DisableInterrupt(ADC_T* adc, uint16_t interrupt)
941 {
942 uint8_t mask;
943
944 mask = (uint8_t)interrupt;
945 adc->CTRL1 &= (~(uint32_t)mask);
946 }
947
948 /*!
949 * @brief Reads the specified ADC flag
950 *
951 * @param adc: Select the ADC peripheral
952 *
953 * @param flag: Select the flag to check
954 * This parameter can be one of the following values:
955 * @arg ADC_FLAG_AWD : Analog watchdog flag
956 * @arg ADC_FLAG_EOC : End of conversion flag
957 * @arg ADC_FLAG_INJEOC: End of injected group conversion flag
958 * @arg ADC_FLAG_INJCS : Injected group conversion Start flag
959 * @arg ADC_FLAG_REGCS : Regular group conversion Start flag
960 *
961 * @retval The status of ADC flag
962 *
963 * @note adc can be ADC1, ADC2.
964 */
ADC_ReadStatusFlag(ADC_T * adc,ADC_FLAG_T flag)965 uint8_t ADC_ReadStatusFlag(ADC_T* adc, ADC_FLAG_T flag)
966 {
967 return (adc->STS & flag) ? SET : RESET;
968 }
969
970 /*!
971 * @brief Clears the specified ADC flag
972 *
973 * @param adc: Select the ADC peripheral
974 *
975 * @param flag: Select the flag to clear
976 * This parameter can be any combination of the following values:
977 * @arg ADC_FLAG_AWD : Analog watchdog flag
978 * @arg ADC_FLAG_EOC : End of conversion flag
979 * @arg ADC_FLAG_INJEOC: End of injected group conversion flag
980 * @arg ADC_FLAG_INJCS : Injected group conversion Start flag
981 * @arg ADC_FLAG_REGCS : Regular group conversion Start flag
982 *
983 * @retval None
984 *
985 * @note adc can be ADC1, ADC2.
986 */
ADC_ClearStatusFlag(ADC_T * adc,uint8_t flag)987 void ADC_ClearStatusFlag(ADC_T* adc, uint8_t flag)
988 {
989 adc->STS = ~(uint32_t)flag;
990 }
991
992 /*!
993 * @brief Reads the specified ADC Interrupt flag.
994 *
995 * @param adc: Select the ADC peripheral.
996 *
997 * @param interrupt: Select the ADC interrupt source.
998 * This parameter can be one of the following values:
999 * @arg ADC_INT_AWD : Enable Analog watchdog interrupt
1000 * @arg ADC_INT_EOC : Enable End of conversion interrupt
1001 * @arg ADC_INT_INJEOC : Enable End of injected conversion interrupt
1002 *
1003 * @retval The status of ADC interrupt
1004 *
1005 * @note adc can be ADC1, ADC2.
1006 */
ADC_ReadIntFlag(ADC_T * adc,ADC_INT_T flag)1007 uint8_t ADC_ReadIntFlag(ADC_T* adc, ADC_INT_T flag)
1008 {
1009 uint8_t bitStatus = RESET;
1010 uint32_t itmask = 0;
1011 uint32_t enableStatus = 0;
1012
1013 itmask = flag >> 8;
1014 enableStatus = (adc->CTRL1 & (uint8_t)flag);
1015
1016 if (((adc->STS & itmask) != (uint32_t)RESET) && enableStatus)
1017 {
1018 bitStatus = SET;
1019 }
1020 else
1021 {
1022 bitStatus = RESET;
1023 }
1024 return bitStatus;
1025 }
1026
1027 /*!
1028 * @brief Clears the specified ADC Interrupt pending bits.
1029 *
1030 * @param adc: Select the ADC peripheral.
1031 *
1032 * @param interrupt: Select the ADC interrupt source.
1033 * This parameter can be any combination of the following values:
1034 * @arg ADC_INT_AWD : Enable Analog watchdog interrupt
1035 * @arg ADC_INT_EOC : Enable End of conversion interrupt
1036 * @arg ADC_INT_INJEOC : Enable End of injected conversion interrupt
1037 *
1038 * @retval None
1039 *
1040 * @note adc can be ADC1, ADC2.
1041 */
ADC_ClearIntFlag(ADC_T * adc,uint16_t flag)1042 void ADC_ClearIntFlag(ADC_T* adc, uint16_t flag)
1043 {
1044 uint8_t mask = 0;
1045
1046 mask = (uint8_t)(flag >> 8);
1047 adc->STS = ~(uint32_t)mask;
1048 }
1049
1050 /**@} end of group ADC_Functions */
1051 /**@} end of group ADC_Driver */
1052 /**@} end of group APM32S10x_StdPeriphDriver */
1053