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