diff options
Diffstat (limited to 'lib/chibios-contrib/testhal/NRF51/NRF51822/ADC/main.c')
-rw-r--r-- | lib/chibios-contrib/testhal/NRF51/NRF51822/ADC/main.c | 139 |
1 files changed, 139 insertions, 0 deletions
diff --git a/lib/chibios-contrib/testhal/NRF51/NRF51822/ADC/main.c b/lib/chibios-contrib/testhal/NRF51/NRF51822/ADC/main.c new file mode 100644 index 000000000..c4d4c76b5 --- /dev/null +++ b/lib/chibios-contrib/testhal/NRF51/NRF51822/ADC/main.c | |||
@@ -0,0 +1,139 @@ | |||
1 | /* | ||
2 | Copyright (C) 2015 Stephen Caudle | ||
3 | |||
4 | Licensed under the Apache License, Version 2.0 (the "License"); | ||
5 | you may not use this file except in compliance with the License. | ||
6 | You may obtain a copy of the License at | ||
7 | |||
8 | http://www.apache.org/licenses/LICENSE-2.0 | ||
9 | |||
10 | Unless required by applicable law or agreed to in writing, software | ||
11 | distributed under the License is distributed on an "AS IS" BASIS, | ||
12 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
13 | See the License for the specific language governing permissions and | ||
14 | limitations under the License. | ||
15 | */ | ||
16 | |||
17 | #include "ch.h" | ||
18 | #include "hal.h" | ||
19 | |||
20 | #define ADC_GRP1_NUM_CHANNELS 1 | ||
21 | #define ADC_GRP1_BUF_DEPTH 8 | ||
22 | |||
23 | #define ADC_GRP2_NUM_CHANNELS 1 | ||
24 | #define ADC_GRP2_BUF_DEPTH 16 | ||
25 | |||
26 | static adcsample_t samples1[ADC_GRP1_NUM_CHANNELS * ADC_GRP1_BUF_DEPTH]; | ||
27 | static adcsample_t samples2[ADC_GRP2_NUM_CHANNELS * ADC_GRP2_BUF_DEPTH]; | ||
28 | |||
29 | /* | ||
30 | * ADC streaming callback. | ||
31 | */ | ||
32 | size_t nx = 0, ny = 0; | ||
33 | static void adccallback(ADCDriver *adcp, adcsample_t *buffer, size_t n) { | ||
34 | |||
35 | (void)adcp; | ||
36 | if (samples2 == buffer) { | ||
37 | nx += n; | ||
38 | } | ||
39 | else { | ||
40 | ny += n; | ||
41 | } | ||
42 | } | ||
43 | |||
44 | /* | ||
45 | * ADC conversion group. | ||
46 | * Mode: Linear buffer, 8 samples of 1 channel, SW triggered. | ||
47 | * Channels: AIN3 (prescaled by 1/3). | ||
48 | */ | ||
49 | static const ADCConversionGroup adcgrpcfg1 = { | ||
50 | FALSE, | ||
51 | ADC_GRP1_NUM_CHANNELS, | ||
52 | NULL, | ||
53 | (1U << (3)), /* AIN3 */ | ||
54 | (ADC_CONFIG_RES_8bit << ADC_CONFIG_RES_Pos) | | ||
55 | (ADC_CONFIG_INPSEL_AnalogInputOneThirdPrescaling << ADC_CONFIG_INPSEL_Pos) | | ||
56 | (ADC_CONFIG_REFSEL_VBG << ADC_CONFIG_REFSEL_Pos) | | ||
57 | (ADC_CONFIG_EXTREFSEL_None << ADC_CONFIG_EXTREFSEL_Pos) | ||
58 | }; | ||
59 | |||
60 | /* | ||
61 | * ADC conversion group. | ||
62 | * Mode: Continuous, 16 samples of 1 channel, SW triggered. | ||
63 | * Channels: VDD (prescaled by 1/3). | ||
64 | */ | ||
65 | static const ADCConversionGroup adcgrpcfg2 = { | ||
66 | TRUE, | ||
67 | ADC_GRP2_NUM_CHANNELS, | ||
68 | adccallback, | ||
69 | 0, /* Use 0 for VDD */ | ||
70 | (ADC_CONFIG_RES_10bit << ADC_CONFIG_RES_Pos) | | ||
71 | (ADC_CONFIG_INPSEL_SupplyOneThirdPrescaling << ADC_CONFIG_INPSEL_Pos) | | ||
72 | (ADC_CONFIG_REFSEL_VBG << ADC_CONFIG_REFSEL_Pos) | | ||
73 | (ADC_CONFIG_EXTREFSEL_None << ADC_CONFIG_EXTREFSEL_Pos) | ||
74 | }; | ||
75 | |||
76 | /* | ||
77 | * LED blinker thread, times are in milliseconds. | ||
78 | */ | ||
79 | static THD_WORKING_AREA(waThread1, 128); | ||
80 | static THD_FUNCTION(Thread1, arg) { | ||
81 | |||
82 | (void)arg; | ||
83 | chRegSetThreadName("blinker"); | ||
84 | while (true) { | ||
85 | palTogglePad(IOPORT1, LED0); | ||
86 | chThdSleepMilliseconds(500); | ||
87 | } | ||
88 | } | ||
89 | |||
90 | /* | ||
91 | * Application entry point. | ||
92 | */ | ||
93 | int main(void) { | ||
94 | |||
95 | /* | ||
96 | * System initializations. | ||
97 | * - HAL initialization, this also initializes the configured device drivers | ||
98 | * and performs the board-specific initializations. | ||
99 | * - Kernel initialization, the main() function becomes a thread and the | ||
100 | * RTOS is active. | ||
101 | */ | ||
102 | halInit(); | ||
103 | chSysInit(); | ||
104 | |||
105 | /* | ||
106 | * Setting up analog input used by the demo (P0.02, SMBA). | ||
107 | */ | ||
108 | palSetPadMode(IOPORT1, 2, PAL_MODE_INPUT_ANALOG); | ||
109 | |||
110 | /* | ||
111 | * Creates the blinker thread. | ||
112 | */ | ||
113 | chThdCreateStatic(waThread1, sizeof(waThread1), NORMALPRIO, Thread1, NULL); | ||
114 | |||
115 | /* | ||
116 | * Activates the ADC1 driver and the temperature sensor. | ||
117 | */ | ||
118 | adcStart(&ADCD1, NULL); | ||
119 | |||
120 | /* | ||
121 | * Linear conversion. | ||
122 | */ | ||
123 | adcConvert(&ADCD1, &adcgrpcfg1, samples1, ADC_GRP1_BUF_DEPTH); | ||
124 | chThdSleepMilliseconds(1000); | ||
125 | |||
126 | /* | ||
127 | * Starts an ADC continuous conversion. | ||
128 | */ | ||
129 | adcStartConversion(&ADCD1, &adcgrpcfg2, samples2, ADC_GRP2_BUF_DEPTH); | ||
130 | |||
131 | /* | ||
132 | * Normal main() thread activity, in this demo it does nothing. | ||
133 | */ | ||
134 | while (true) { | ||
135 | if (palReadPad(IOPORT1, KEY1) == 0) | ||
136 | adcStopConversion(&ADCD1); | ||
137 | chThdSleepMilliseconds(500); | ||
138 | } | ||
139 | } | ||