-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathadc.c
100 lines (85 loc) · 2.91 KB
/
adc.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
#include "config.h"
#include "adc.h"
adcsample_t samples[CONFIG_ADC_NUM_INPUTS * ADC_GRP_BUF_DEPTH];
volatile uint16_t adc_val[CONFIG_ADC_NUM_INPUTS];
volatile bool adc_error = false;
/* Unfortunately this struct must be constant, therefore the ADC cannot be configured during runtime */
ADCConversionGroup adcgrpcfg = {
TRUE, /* buffer circular mode */
CONFIG_ADC_NUM_INPUTS, /* number of channels */
adc_callback, /* conversion end callback */
adc_error_callback, /* conversion error callback */
0, 0, /* CR1, CR2 */
ADC_SMPR1_SMP_AN11(ADC_SAMPLE_41P5) | ADC_SMPR1_SMP_AN10(ADC_SAMPLE_41P5), /* SMPR1 */
0, /* SMPR2 */
ADC_SQR1_NUM_CH(CONFIG_ADC_NUM_INPUTS), /* SQR1: Conversion group sequence 13...16 + sequence length */
#if CONFIG_ADC_NUM_INPUTS > 9
ADC_SQR2_SQ10_N(ADC_CHANNEL_IN9)| /* SQR2: Conversion group sequence 7...12 */
#endif
#if CONFIG_ADC_NUM_INPUTS > 8
ADC_SQR2_SQ9_N(ADC_CHANNEL_IN8) |
#endif
#if CONFIG_ADC_NUM_INPUTS > 7
ADC_SQR2_SQ8_N(ADC_CHANNEL_IN7) |
#endif
#if CONFIG_ADC_NUM_INPUTS > 6
ADC_SQR2_SQ7_N(ADC_CHANNEL_IN6) |
#endif
0, /* End SQR2 */
#if CONFIG_ADC_NUM_INPUTS > 5
ADC_SQR3_SQ6_N(ADC_CHANNEL_IN5) | /* SQR3: Conversion group sequence 1...6 */
#endif
#if CONFIG_ADC_NUM_INPUTS > 4
ADC_SQR3_SQ5_N(ADC_CHANNEL_IN4) |
#endif
#if CONFIG_ADC_NUM_INPUTS > 3
ADC_SQR3_SQ4_N(ADC_CHANNEL_IN3) |
#endif
#if CONFIG_ADC_NUM_INPUTS > 2
ADC_SQR3_SQ3_N(ADC_CHANNEL_IN2) |
#endif
#if CONFIG_ADC_NUM_INPUTS > 1
ADC_SQR3_SQ2_N(ADC_CHANNEL_IN1) |
#endif
#if CONFIG_ADC_NUM_INPUTS > 0
ADC_SQR3_SQ1_N(ADC_CHANNEL_IN0) |
#endif
0, /* End SQR3 */
};
/*
* ADC streaming callback.
*/
void adc_callback(ADCDriver *adcp, adcsample_t *buffer, size_t n)
{
(void)adcp;
uint8_t i, j;
uint32_t avg[CONFIG_ADC_NUM_INPUTS];
for (i = 0; i < CONFIG_ADC_NUM_INPUTS; i++) {
avg[i] = 0;
}
for (j = 0; j < n; j++) {
for (i = 0; i < CONFIG_ADC_NUM_INPUTS; i++) {
avg[i] += *buffer++;
}
}
for (i = 0; i < CONFIG_ADC_NUM_INPUTS; i++) {
adc_val[i] = avg[i] / n;
}
}
void adc_error_callback(ADCDriver *adcp, adcerror_t err)
{
(void)adcp;
(void)err;
adc_error = true;
}
void adc_init()
{
#if defined(CONFIG_SENSOR_ADC) && CONFIG_ADC_NUM_INPUTS > 0
adc_error = false;
// Activate the ADC1 driver
adcStart(&ADCD1, NULL);
// Starts ADC continuous conversion
adcStartConversion(&ADCD1, &adcgrpcfg, samples, ADC_GRP_BUF_DEPTH);
#endif
return;
}