1 ////////////////////////////////////////////////////////////////////////////////
2 /// @file hal_adc.c
3 /// @author AE TEAM
4 /// @brief THIS FILE PROVIDES ALL THE ADC FIRMWARE FUNCTIONS.
5 ////////////////////////////////////////////////////////////////////////////////
6 /// @attention
7 ///
8 /// THE EXISTING FIRMWARE IS ONLY FOR REFERENCE, WHICH IS DESIGNED TO PROVIDE
9 /// CUSTOMERS WITH CODING INFORMATION ABOUT THEIR PRODUCTS SO THEY CAN SAVE
10 /// TIME. THEREFORE, MINDMOTION SHALL NOT BE LIABLE FOR ANY DIRECT, INDIRECT OR
11 /// CONSEQUENTIAL DAMAGES ABOUT ANY CLAIMS ARISING OUT OF THE CONTENT OF SUCH
12 /// HARDWARE AND/OR THE USE OF THE CODING INFORMATION CONTAINED HEREIN IN
13 /// CONNECTION WITH PRODUCTS MADE BY CUSTOMERS.
14 ///
15 /// <H2><CENTER>© COPYRIGHT MINDMOTION </CENTER></H2>
16 ////////////////////////////////////////////////////////////////////////////////
17
18 // Define to prevent recursive inclusion
19 #define _HAL_ADC_C_
20
21 // Files includes
22 #include "hal_adc.h"
23 #include "hal_rcc.h"
24
25 ////////////////////////////////////////////////////////////////////////////////
26 /// @addtogroup MM32_Hardware_Abstract_Layer
27 /// @{
28
29 ////////////////////////////////////////////////////////////////////////////////
30 /// @addtogroup ADC_HAL
31 /// @{
32
33 ////////////////////////////////////////////////////////////////////////////////
34 /// @addtogroup ADC_Exported_Functions
35 /// @{
36
37 ////////////////////////////////////////////////////////////////////////////////
38 /// @brief Deinitializes the adc peripheral registers to their default
39 /// reset values.
40 /// @param adc: select the ADC peripheral.
41 /// @retval None.
42 ////////////////////////////////////////////////////////////////////////////////
ADC_DeInit(ADC_TypeDef * adc)43 void ADC_DeInit(ADC_TypeDef* adc)
44 {
45
46 switch (*(vu32*)&adc) {
47
48 case ADC1_BASE:
49 exRCC_APB2PeriphReset(RCC_APB2ENR_ADC1);
50 break;
51 case ADC2_BASE:
52 exRCC_APB2PeriphReset(RCC_APB2ENR_ADC2);
53 break;
54 case ADC3_BASE:
55 exRCC_APB2PeriphReset(RCC_APB2ENR_ADC3);
56 break;
57 default:
58 break;
59 }
60 }
61
62 ////////////////////////////////////////////////////////////////////////////////
63 /// @brief Initializes the adc peripheral according to the specified parameters
64 /// in the init_struct, Please use this function if you want to be
65 /// compatible with older versions of the library.
66 /// @param adc: select the ADC peripheral.
67 /// @param init_struct: pointer to an ADC_InitTypeDef structure that contains
68 /// the configuration information for the specified ADC peripheral.
69 /// @retval None.
70 ////////////////////////////////////////////////////////////////////////////////
ADC_Init(ADC_TypeDef * adc,ADC_InitTypeDef * init_struct)71 void ADC_Init(ADC_TypeDef* adc, ADC_InitTypeDef* init_struct)
72 {
73 adc->ADCFG &= ~(ADC_CFGR_PRE | ADC_CFGR_RSLTCTL);
74 adc->ADCFG |= (u32)(init_struct->ADC_PRESCARE) | init_struct->ADC_Resolution;
75
76 adc->ADCR &= ~(ADC_CR_ALIGN | ADC_CR_MODE | ADC_CR_TRGSEL);
77 adc->ADCR |= ((u32)init_struct->ADC_DataAlign) | init_struct->ADC_ExternalTrigConv | ((u32)init_struct->ADC_Mode);
78 }
79
80 ////////////////////////////////////////////////////////////////////////////////
81 /// @brief Fills each init_struct member with its default value.
82 /// @param init_struct : pointer to an ADC_InitTypeDef structure which will be
83 /// initialized.
84 /// @retval None.
85 ////////////////////////////////////////////////////////////////////////////////
ADC_StructInit(ADC_InitTypeDef * init_struct)86 void ADC_StructInit(ADC_InitTypeDef* init_struct)
87 {
88 init_struct->ADC_Resolution = ADC_Resolution_12b;
89 init_struct->ADC_PRESCARE = ADC_PCLK2_PRESCARE_2;
90 init_struct->ADC_Mode = ADC_CR_IMM; //ADC_Mode_Single;
91 init_struct->ADC_ContinuousConvMode = DISABLE; // useless
92 init_struct->ADC_ExternalTrigConv = ADC1_ExternalTrigConv_T1_CC1;
93 init_struct->ADC_DataAlign = ADC_DataAlign_Right;
94 }
95
96 ////////////////////////////////////////////////////////////////////////////////
97 /// @brief Enables or disables the specified ADC peripheral.
98 /// @param adc:select the ADC peripheral.
99 /// @param state: new state of the adc peripheral.
100 /// @retval None.
101 ////////////////////////////////////////////////////////////////////////////////
ADC_Cmd(ADC_TypeDef * adc,FunctionalState state)102 void ADC_Cmd(ADC_TypeDef* adc, FunctionalState state)
103 {
104 (state) ? (adc->ADCFG |= ADC_CFGR_ADEN) : (adc->ADCFG &= ~ADC_CFGR_ADEN);
105 }
106 ////////////////////////////////////////////////////////////////////////////////
107 /// @brief Enables or disables the specified ADC DMA request.
108 /// @param adc: select the ADC peripheral.
109 /// @param state: New state of the selected ADC DMA transfer.
110 /// @retval None.
111 ////////////////////////////////////////////////////////////////////////////////
ADC_DMACmd(ADC_TypeDef * adc,FunctionalState state)112 void ADC_DMACmd(ADC_TypeDef* adc, FunctionalState state)
113 {
114 (state) ? (adc->ADCR |= ADC_CR_DMAEN) : (adc->ADCR &= ~ADC_CR_DMAEN);
115 }
116 ////////////////////////////////////////////////////////////////////////////////
117 /// @brief Enables or disables the specified ADC interrupts.
118 /// @param adc: select the ADC peripheral.
119 /// @param adc_interrupt: specifies the ADC interrupt sources to be enabled or disabled.
120 /// @param state: New state of the specified ADC interrupts.
121 /// @retval None.
122 ////////////////////////////////////////////////////////////////////////////////
ADC_ITConfig(ADC_TypeDef * adc,ADCFLAG_TypeDef adc_interrupt,FunctionalState state)123 void ADC_ITConfig(ADC_TypeDef* adc, ADCFLAG_TypeDef adc_interrupt, FunctionalState state)
124 {
125 if (adc_interrupt == ADC_IT_EOC)
126 (state) ? (adc->ADCR |= ADC_CR_ADIE) : (adc->ADCR &= ~ADC_CR_ADIE);
127 else
128 (state) ? (adc->ADCR |= ADC_CR_ADWIE) : (adc->ADCR &= ~ADC_CR_ADWIE);
129 }
130
131 ////////////////////////////////////////////////////////////////////////////////
132 /// @brief Enables or disables the selected ADC software start conversion .
133 /// @param adc: select the ADC peripheral.
134 /// @param state: New state of the selected ADC software start conversion.
135 /// @retval None.
136 ////////////////////////////////////////////////////////////////////////////////
ADC_SoftwareStartConvCmd(ADC_TypeDef * adc,FunctionalState state)137 void ADC_SoftwareStartConvCmd(ADC_TypeDef* adc, FunctionalState state)
138 {
139 (state) ? (adc->ADCR |= ADC_CR_ADST) : (adc->ADCR &= ~ADC_CR_ADST);
140 }
141
142 ////////////////////////////////////////////////////////////////////////////////
143 /// @brief Gets the selected ADC Software start conversion Status.
144 /// @param adc: select the ADC peripheral.
145 /// @retval The new state of ADC software start conversion (SET or RESET).
146 ////////////////////////////////////////////////////////////////////////////////
ADC_GetSoftwareStartConvStatus(ADC_TypeDef * adc)147 FlagStatus ADC_GetSoftwareStartConvStatus(ADC_TypeDef* adc)
148 {
149 return (((adc->ADCR & ADC_CR_ADST) != (u32)RESET) ? SET : RESET);
150 }
151 ////////////////////////////////////////////////////////////////////////////////
152 /// @brief Enable the selected ADC channel and configure its sample time. Please
153 /// use this function if you want to be compatible with older versions
154 /// of the library.
155 /// @param adc: select the ADC peripheral.
156 /// @param channel: the ADC channel to configure.
157 /// @param sample_time: the ADC Channel n Sample time to configure.
158 /// @retval None.
159 ////////////////////////////////////////////////////////////////////////////////
ADC_RegularChannelConfig(ADC_TypeDef * adc,u32 channel,u8 rank,u32 sample_time)160 void ADC_RegularChannelConfig(ADC_TypeDef* adc, u32 channel, u8 rank, u32 sample_time) //ADCSAM_TypeDef
161 {
162
163 u32 tempchan;
164 sample_time = sample_time & 0xF;
165 tempchan = channel;
166 if(tempchan > 8) {
167 tempchan = tempchan & 0xF;
168 tempchan = tempchan - 8;
169 adc->SMPR2 &= ~(0xF << tempchan);
170 adc->SMPR2 |= (sample_time << tempchan);
171 }
172 else {
173 adc->SMPR1 &= ~(0xF << tempchan);
174 adc->SMPR1 |= (sample_time << tempchan);
175 }
176 adc->ADCHS &= ~(1 << channel);
177 adc->ADCHS |= (1 << channel);
178
179 if (channel & ADC_CHSR_CHT)
180 ADC_TempSensorVrefintCmd(ENABLE);
181 else if (channel & ADC_CHSR_CHV)
182 ADC_TempSensorVrefintCmd(ENABLE);
183 }
184
185 ////////////////////////////////////////////////////////////////////////////////
186 /// @brief Enables or disables the adc conversion through external trigger.
187 /// @param adc: select the ADC peripheral.
188 /// @param state: New state of the selected ADC external trigger.
189 /// @retval None.
190 ////////////////////////////////////////////////////////////////////////////////
ADC_ExternalTrigConvCmd(ADC_TypeDef * adc,FunctionalState state)191 void ADC_ExternalTrigConvCmd(ADC_TypeDef* adc, FunctionalState state)
192 {
193 (state) ? (adc->ADCR |= ADC_CR_TRGEN) : (adc->ADCR &= ~ADC_CR_TRGEN);
194 }
195 ////////////////////////////////////////////////////////////////////////////////
196 /// @brief Configures the adc external trigger for injected channels conversion.
197 /// @param adc: select the ADC peripheral.
198 /// @param adc_external_trig_source: Configuring the external trigger source
199 /// for the ADC.
200 /// @retval None.
201 ////////////////////////////////////////////////////////////////////////////////
ADC_ExternalTrigConvConfig(ADC_TypeDef * adc,EXTERTRIG_TypeDef adc_external_trig_source)202 void ADC_ExternalTrigConvConfig(ADC_TypeDef* adc, EXTERTRIG_TypeDef adc_external_trig_source)
203 {
204 adc->ADCR &= ~ADC_CR_TRGSEL;
205 adc->ADCR |= adc_external_trig_source;
206 }
207
208
209 ////////////////////////////////////////////////////////////////////////////////
210 /// @brief Returns the last adc conversion result data for regular channel.
211 /// @param adc: select the ADC peripheral.
212 /// @retval The data conversion value.
213 ////////////////////////////////////////////////////////////////////////////////
ADC_GetConversionValue(ADC_TypeDef * adc)214 u16 ADC_GetConversionValue(ADC_TypeDef* adc)
215 {
216 return (u16)adc->ADDATA;
217 }
218
219 ////////////////////////////////////////////////////////////////////////////////
220 /// @brief Returns the last ADC conversion result data in dual mode.
221 /// @param None
222 /// @retval The Data conversion value.
223 ////////////////////////////////////////////////////////////////////////////////
ADC_GetDualModeConversionValue()224 u32 ADC_GetDualModeConversionValue()
225 {
226 return (*(vu32*)ADC1_BASE);
227 }
228
229
230
231 ////////////////////////////////////////////////////////////////////////////////
232 /// @brief Enables or disables the analog watchdog.
233 /// @param adc: to select the ADC peripheral.
234 /// @param state: New state of the selected ADC analog watchdog.
235 /// @retval None.
236 ////////////////////////////////////////////////////////////////////////////////
ADC_AnalogWatchdogCmd(ADC_TypeDef * adc,FunctionalState state)237 void ADC_AnalogWatchdogCmd(ADC_TypeDef* adc, FunctionalState state)
238 {
239 (state) ? (adc->ADCFG |= ADC_CFGR_ADWEN) : (adc->ADCFG &= ~ADC_CFGR_ADWEN);
240 }
241
242 ////////////////////////////////////////////////////////////////////////////////
243 /// @brief Configures the high and low thresholds of the analog watchdog.
244 /// @param adc: select the ADC peripheral.
245 /// @param high_threshold: the ADC analog watchdog High threshold value.
246 /// This parameter must be a 12bit value.
247 /// @param low_threshold: the ADC analog watchdog Low threshold value.
248 /// This parameter must be a 12bit value.
249 /// @retval None.
250 ////////////////////////////////////////////////////////////////////////////////
ADC_AnalogWatchdogThresholdsConfig(ADC_TypeDef * adc,u16 high_threshold,u16 low_threshold)251 void ADC_AnalogWatchdogThresholdsConfig(ADC_TypeDef* adc, u16 high_threshold, u16 low_threshold)
252 {
253 u32 tempThreshold;
254 tempThreshold = high_threshold;
255 adc->ADCMPR = (tempThreshold << 16) | low_threshold;
256 }
257
258 ////////////////////////////////////////////////////////////////////////////////
259 /// @brief Configures the analog watchdog guarded single channel
260 /// @param adc: select the ADC peripheral.
261 /// @param channel: the ADC channel to configure for the analog watchdog.
262 /// @retval None.
263 ////////////////////////////////////////////////////////////////////////////////
ADC_AnalogWatchdogSingleChannelConfig(ADC_TypeDef * adc,ADCCHANNEL_TypeDef channel)264 void ADC_AnalogWatchdogSingleChannelConfig(ADC_TypeDef* adc, ADCCHANNEL_TypeDef channel)
265 {
266 adc->ADCR &= ~ADC_CR_CMPCH;
267 adc->ADCR |= (channel << ADC_CR_CMPCH_Pos);
268 }
269
270 ////////////////////////////////////////////////////////////////////////////////
271 /// @brief Enables or disables the temperature sensor and Vrefint channel.
272 /// @param state: New state of the temperature sensor.
273 /// @retval None.
274 ////////////////////////////////////////////////////////////////////////////////
ADC_TempSensorVrefintCmd(FunctionalState state)275 void ADC_TempSensorVrefintCmd(FunctionalState state)
276 {
277 (state) ? (ADC1->ADCFG |= (ADC_CFGR_TEN | ADC_CFGR_VEN))
278 : (ADC1->ADCFG &= ~(ADC_CFGR_TEN | ADC_CFGR_VEN));
279 }
280
281
282 ////////////////////////////////////////////////////////////////////////////////
283 /// @brief Enables or disables the temperature sensor .
284 /// @param state: New state of the temperature sensor.
285 /// @retval None.
286 ////////////////////////////////////////////////////////////////////////////////
ADC_TempSensorCmd(FunctionalState state)287 void ADC_TempSensorCmd(FunctionalState state)
288 {
289 ADC_TempSensorVrefintCmd(state);
290 }
291 ////////////////////////////////////////////////////////////////////////////////
292 /// @brief Enables or disables the Vrefint channel.
293 /// @param state: New state of the Vrefint channel.
294 /// @retval None.
295 ////////////////////////////////////////////////////////////////////////////////
ADC_VrefintCmd(FunctionalState state)296 void ADC_VrefintCmd(FunctionalState state)
297 {
298 ADC_TempSensorVrefintCmd(state);
299 }
300 ////////////////////////////////////////////////////////////////////////////////
301 /// @brief Enables or disables the temperature sensor and Vrefint channel.
302 /// @param chs: temperature sensor bit & Vrefint bit.
303 /// @param state: New state of the temperature sensor.
304 /// @retval None.
305 ////////////////////////////////////////////////////////////////////////////////
exADC_TempSensorVrefintCmd(u32 chs,FunctionalState state)306 void exADC_TempSensorVrefintCmd(u32 chs, FunctionalState state)
307 {
308 if (chs & ADC_CHSR_CHT) {
309 (state) ? (ADC1->ADCFG |= ADC_CFGR_TEN)
310 : (ADC1->ADCFG &= ~ADC_CFGR_TEN);
311 }
312 else if (chs & ADC_CHSR_CHV) {
313 (state) ? (ADC1->ADCFG |= ADC_CFGR_VEN)
314 : (ADC1->ADCFG &= ~ADC_CFGR_VEN);
315 }
316
317 }
318
319 ////////////////////////////////////////////////////////////////////////////////
320 /// @brief Checks whether the specified ADC flag is set or not.
321 /// @param adc: select the ADC peripheral.
322 /// @param adc_flag: specifies the flag to check.
323 /// @retval The New state of adc_flag (SET or RESET).
324 ////////////////////////////////////////////////////////////////////////////////
ADC_GetFlagStatus(ADC_TypeDef * adc,ADCFLAG_TypeDef adc_flag)325 FlagStatus ADC_GetFlagStatus(ADC_TypeDef* adc, ADCFLAG_TypeDef adc_flag)
326 {
327 return (adc_flag == ADC_IT_EOC) ? ((adc->ADSTA & ADC_SR_ADIF) ? SET : RESET) : ((adc->ADSTA & ADC_SR_ADWIF) ? SET : RESET);
328 }
329
330 ////////////////////////////////////////////////////////////////////////////////
331 /// @brief Clears the adc's pending flags.
332 /// @param adc: select the ADC peripheral.
333 /// @param adc_flag: specifies the flag to clear.
334 /// @retval None.
335 ////////////////////////////////////////////////////////////////////////////////
ADC_ClearFlag(ADC_TypeDef * adc,ADCFLAG_TypeDef adc_flag)336 void ADC_ClearFlag(ADC_TypeDef* adc, ADCFLAG_TypeDef adc_flag)
337 {
338 (adc_flag == ADC_IT_EOC) ? (adc->ADSTA |= ADC_SR_ADIF) : (adc->ADSTA |= ADC_SR_ADWIF);
339 }
340
341 ////////////////////////////////////////////////////////////////////////////////
342 /// @brief Checks whether the specified adc's interrupt has occurred or not.
343 /// @param adc: select the ADC peripheral.
344 /// @param adc_interrupt: specifies the ADC interrupt source to check.
345 /// @retval The new state of adc_interrupt (SET or RESET).
346 ////////////////////////////////////////////////////////////////////////////////
ADC_GetITStatus(ADC_TypeDef * adc,ADCFLAG_TypeDef adc_interrupt)347 ITStatus ADC_GetITStatus(ADC_TypeDef* adc, ADCFLAG_TypeDef adc_interrupt)
348 {
349 return (adc_interrupt == ADC_IT_EOC) ? ((adc->ADSTA & ADC_SR_ADIF) ? SET : RESET) : ((adc->ADSTA & ADC_SR_ADWIF) ? SET : RESET);
350 }
351
352 ////////////////////////////////////////////////////////////////////////////////
353 /// @brief Clears the adc's interrupt pending bits.
354 /// @param adc: select the ADC peripheral.
355 /// @param adc_interrupt: specifies the ADC interrupt pending bit to clear.
356 /// @retval None.
357 ////////////////////////////////////////////////////////////////////////////////
ADC_ClearITPendingBit(ADC_TypeDef * adc,ADCFLAG_TypeDef adc_interrupt)358 void ADC_ClearITPendingBit(ADC_TypeDef* adc, ADCFLAG_TypeDef adc_interrupt)
359 {
360 (adc_interrupt == ADC_IT_EOC) ? (adc->ADSTA |= ADC_SR_ADIF) : (adc->ADSTA |= ADC_SR_ADWIF);
361 }
362 ////////////////////////////////////////////////////////////////////////////////
363 /// @brief Configures the adc any channels conversion rank and channel.
364 /// @param adc: select the ADC peripheral.
365 /// @param rank: rank can be 0x0~0xf for the convert sequence.
366 /// @param adc_channel: Configuring the target channel to be converted.
367 /// @retval None.
368 ////////////////////////////////////////////////////////////////////////////////
ADC_ANY_CH_Config(ADC_TypeDef * adc,u8 rank,ADCCHANNEL_TypeDef adc_channel)369 void ADC_ANY_CH_Config(ADC_TypeDef* adc, u8 rank, ADCCHANNEL_TypeDef adc_channel)
370 {
371 rank = rank & 0xF;
372 if(rank < 8) {
373 adc->CHANY0 &= ~(0x0F << (4 * rank));
374 adc->CHANY0 |= (adc_channel << (4 * rank));
375 }
376 else {
377 adc->CHANY1 &= ~(0x0F << (4 * (rank - 8)));
378 adc->CHANY1 |= (adc_channel << (4 * (rank - 8)));
379 }
380 }
381 ////////////////////////////////////////////////////////////////////////////////
382 /// @brief Configures the adc any channels conversion Max rank number
383 /// @param adc: select the ADC peripheral.
384 /// @param num: Configuring the max rank number for the ADC.
385 /// @retval None.
386 ////////////////////////////////////////////////////////////////////////////////
ADC_ANY_NUM_Config(ADC_TypeDef * adc,u8 num)387 void ADC_ANY_NUM_Config(ADC_TypeDef* adc, u8 num)
388 {
389 if(num > 15) num = 15; //15 ? 16 need to be confirmed
390 adc->ANYCFG = num;
391 }
392 ////////////////////////////////////////////////////////////////////////////////
393 /// @brief Enables or disables the ANY channel converter.
394 /// @param state: enable or disable the ANY channel converter mode.
395 /// @retval None.
396 ////////////////////////////////////////////////////////////////////////////////
ADC_ANY_Cmd(ADC_TypeDef * adc,FunctionalState state)397 void ADC_ANY_Cmd(ADC_TypeDef* adc, FunctionalState state)
398 {
399 (state) ? (adc->ANYCR |= ADC1_CHANY_CR_MDEN) : (adc->ANYCR &= ~ADC1_CHANY_CR_MDEN);
400 }
401 ////////////////////////////////////////////////////////////////////////////////
402 /// @brief Enables or disables the selected ADC automatic injected group
403 /// conversion after regular one.
404 /// @param adc: where x can be 1, 2 or 3 to select the ADC peripheral.
405 /// @param state: new state of the selected ADC auto injected conversion
406 /// This parameter can be: ENABLE or DISABLE.
407 /// @retval None
408 ////////////////////////////////////////////////////////////////////////////////
ADC_AutoInjectedConvCmd(ADC_TypeDef * adc,FunctionalState state)409 void ADC_AutoInjectedConvCmd(ADC_TypeDef* adc, FunctionalState state)
410 {
411 (state) ? (adc->ANYCR |= ADC_ANY_CR_JAUTO) : (adc->ANYCR &= ~ADC_ANY_CR_JAUTO);
412 }
413
414 ////////////////////////////////////////////////////////////////////////////////
415 /// @brief Configures the adc external trigger for injected channels conversion.
416 /// @param adc: where x can be 1, 2 or 3 to select the ADC peripheral.
417 /// @param ADC_ExtInjTrigSource: specifies the ADC trigger to start injected conversion.
418
419 /// @retval None
420 ////////////////////////////////////////////////////////////////////////////////
ADC_ExternalTrigInjectedConvertConfig(ADC_TypeDef * adc,EXTER_INJ_TRIG_TypeDef ADC_ExtInjTrigSource)421 void ADC_ExternalTrigInjectedConvertConfig(ADC_TypeDef* adc, EXTER_INJ_TRIG_TypeDef ADC_ExtInjTrigSource)
422 {
423 u32 tmpreg = 0;
424 // Get the old register value
425 tmpreg = adc->ANYCR;
426 // Clear the old external event selection for injected group
427 tmpreg &= ADC_ANY_CR_JTRGSEL;
428 // Set the external event selection for injected group
429 tmpreg |= ADC_ExtInjTrigSource;
430 // Store the new register value
431 adc->ANYCR = tmpreg;
432 }
433
434
435 ////////////////////////////////////////////////////////////////////////////////
436 /// @brief Enables or disables the adc injected channels conversion through
437 /// external trigger
438 /// @param adc: where x can be 1, 2 or 3 to select the ADC peripheral.
439 /// @param state: new state of the selected ADC external trigger start of
440 /// injected conversion.
441 /// This parameter can be: ENABLE or DISABLE.
442 /// @retval None
443 ////////////////////////////////////////////////////////////////////////////////
ADC_ExternalTrigInjectedConvCmd(ADC_TypeDef * adc,FunctionalState state)444 void ADC_ExternalTrigInjectedConvCmd(ADC_TypeDef* adc, FunctionalState state)
445 {
446 (state) ? (adc->ANYCR |= ADC_ANY_CR_JTRGEN) : (adc->ANYCR &= ~ADC_ANY_CR_JTRGEN);
447 }
448 ////////////////////////////////////////////////////////////////////////////////
449 /// @brief Enables or disables the selected ADC start of the injected
450 /// channels conversion.
451 /// @param adc: where x can be 1, 2 or 3 to select the ADC peripheral.
452 /// @param state: new state of the selected ADC software start injected conversion.
453 /// This parameter can be: ENABLE or DISABLE.
454 /// @retval None
455 ////////////////////////////////////////////////////////////////////////////////
ADC_InjectedConvCmd(ADC_TypeDef * adc,FunctionalState state)456 void ADC_InjectedConvCmd(ADC_TypeDef* adc, FunctionalState state)
457 {
458 (state) ? (adc->ANYCR |= ADC_ANY_CR_JCEN) : (adc->ANYCR &= ~ADC_ANY_CR_JCEN);
459 }
460 ////////////////////////////////////////////////////////////////////////////////
461 /// @brief Enables or disables the selected ADC start of the injected
462 /// channels conversion.
463 /// @param adc: where x can be 1, 2 or 3 to select the ADC peripheral.
464 /// @param NewState: new state of the selected ADC software start injected conversion.
465 /// This parameter can be: ENABLE or DISABLE.
466 /// @retval None
467 ////////////////////////////////////////////////////////////////////////////////
ADC_SoftwareStartInjectedConvCmd(ADC_TypeDef * adc,FunctionalState state)468 void ADC_SoftwareStartInjectedConvCmd(ADC_TypeDef* adc, FunctionalState state)
469 {
470 (state) ? (adc->ANYCR |= ADC_ANY_CR_JADST) : (adc->ANYCR &= ~ADC_ANY_CR_JADST);
471 }
472
473
474 ////////////////////////////////////////////////////////////////////////////////
475 /// @brief Enable the selected ADC channel and configure its sample time. Please
476 /// use this function if you want to be compatible with older versions
477 /// of the library.
478 /// @param adc: select the ADC peripheral.
479 /// @param event: the ADC external event to configure.
480 /// @param sample_time: the ADC Channel n Sample time to configure.
481 /// @retval None.
482 ////////////////////////////////////////////////////////////////////////////////
ADC_InjectedSequencerConfig(ADC_TypeDef * adc,u32 event,u32 sample_time)483 void ADC_InjectedSequencerConfig(ADC_TypeDef* adc, u32 event, u32 sample_time)
484 {
485 adc->ANYCR &= ~(ADC_ANY_CR_JCEN | ADC_ANY_CR_CHANY_MDEN | ADC_ANY_CR_JTRGSEL_EXTI12 \
486 | ADC_ANY_CR_JTRGSHIFT_512 | ADC_ANY_CR_JTRGEN);
487 adc->ANYCR |= (ADC_ANY_CR_JCEN | ADC_ANY_CR_CHANY_MDEN | sample_time | event | ADC_ANY_CR_JTRGEN);
488 }
489 ////////////////////////////////////////////////////////////////////////////////
490 /// @brief Injection channel length configuration.
491 /// @param adc: select the ADC peripheral.
492 /// @param Length: Injection channel length.
493 /// @retval None.
494 ////////////////////////////////////////////////////////////////////////////////
ADC_InjectedSequencerLengthConfig(ADC_TypeDef * adc,ADC_INJ_SEQ_LEN_TypeDef Length)495 void ADC_InjectedSequencerLengthConfig(ADC_TypeDef* adc, ADC_INJ_SEQ_LEN_TypeDef Length)
496 {
497 adc->JSQR &= ~(0x03 << ADC_JSQR_JL_Pos);
498 adc->JSQR |= Length << ADC_JSQR_JL_Pos;
499 }
500 ////////////////////////////////////////////////////////////////////////////////
501 /// @brief Injection channel configuration.
502 /// @param adc : select the ADC peripheral.
503 /// @param off_addr : Injection channel offset address.
504 /// @param channel: The sampling channel.
505 /// @retval None.
506 ////////////////////////////////////////////////////////////////////////////////
ADC_InjectedSequencerChannelConfig(ADC_TypeDef * adc,ADC_INJ_SEQ_Channel_TypeDef off_addr,ADCCHANNEL_TypeDef channel)507 void ADC_InjectedSequencerChannelConfig(ADC_TypeDef* adc, ADC_INJ_SEQ_Channel_TypeDef off_addr, ADCCHANNEL_TypeDef channel)
508 {
509 adc->JSQR &= ~(0x1F << (off_addr >> 2) * 5);
510 adc->JSQR |= (channel << (off_addr >> 2) * 5);
511 }
512 ////////////////////////////////////////////////////////////////////////////////
513 /// @brief Injection channel converted value.
514 /// @param adc : select the ADC peripheral.
515 /// @param off_addr : Injection channel offset address.
516 /// @retval value.
517 ////////////////////////////////////////////////////////////////////////////////
ADC_GetInjectedConversionValue(ADC_TypeDef * adc,ADC_INJ_SEQ_Channel_TypeDef off_addr)518 u16 ADC_GetInjectedConversionValue(ADC_TypeDef* adc, ADC_INJ_SEQ_Channel_TypeDef off_addr)
519 {
520 u32 value;
521 value = (*(vu32*)(*(vu32*)&adc + 0xB0 + off_addr)) - (*(vu32*)(*(vu32*)&adc + 0x7C + off_addr));
522
523 return (u16)value;
524 }
525 ////////////////////////////////////////////////////////////////////////////////
526 /// @brief Injection current converted value.
527 /// @param adc : select the ADC peripheral.
528 /// @retval value. Returns the last adc conversion result data for injected channel.
529 ////////////////////////////////////////////////////////////////////////////////
ADC_GetInjectedCurrentConvertedValue(ADC_TypeDef * adc)530 u16 ADC_GetInjectedCurrentConvertedValue(ADC_TypeDef* adc)
531 {
532 return (u16)adc->JDATA;
533 }
534 ////////////////////////////////////////////////////////////////////////////////
535 /// @brief Injection channel compensation configuration.
536 /// @param adc : select the ADC peripheral.
537 /// @param off_addr : Injection channel.
538 /// @param value : compensation value.
539 /// @retval None.
540 ////////////////////////////////////////////////////////////////////////////////
ADC_SetInjectedOffset(ADC_TypeDef * adc,ADC_INJ_SEQ_Channel_TypeDef off_addr,u16 value)541 void ADC_SetInjectedOffset(ADC_TypeDef* adc, ADC_INJ_SEQ_Channel_TypeDef off_addr, u16 value)
542 {
543 *(vu32*)(*(vu32*)&adc + 0x7C + off_addr) = value;
544 }
545
546
547 ////////////////////////////////////////////////////////////////////////////////
548 /// @brief Get channel convertion result.
549 /// @param adc : select the ADC peripheral.
550 /// @param channel : Converted channel.
551 /// @retval The Data conversion value.
552 ////////////////////////////////////////////////////////////////////////////////
ADC_GetChannelConvertedValue(ADC_TypeDef * adc,ADCCHANNEL_TypeDef channel)553 u16 ADC_GetChannelConvertedValue(ADC_TypeDef* adc, ADCCHANNEL_TypeDef channel)
554 {
555 return (u16)(*(vu32*) ((u32)adc + 0x18 + ((u32)channel << 2)));
556 }
557
558 /// @}
559
560 /// @}
561
562 /// @}
563
564