forked from raspberrypi/pico-examples
-
Notifications
You must be signed in to change notification settings - Fork 0
/
adc_console.c
100 lines (93 loc) · 2.97 KB
/
adc_console.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 <stdio.h>
#include "pico/stdlib.h"
#include "hardware/gpio.h"
#include "hardware/adc.h"
#define N_SAMPLES 1000
uint16_t sample_buf[N_SAMPLES];
void printhelp() {
puts("\nCommands:");
puts("c0, ...\t: Select ADC channel n");
puts("s\t: Sample once");
puts("S\t: Sample many");
puts("w\t: Wiggle pins");
}
void __not_in_flash_func(adc_capture)(uint16_t *buf, size_t count) {
adc_fifo_setup(true, false, 0, false, false);
adc_run(true);
for (int i = 0; i < count; i = i + 1)
buf[i] = adc_fifo_get_blocking();
adc_run(false);
adc_fifo_drain();
}
int main(void) {
stdio_init_all();
adc_init();
adc_set_temp_sensor_enabled(true);
// Set all pins to input (as far as SIO is concerned)
gpio_set_dir_all_bits(0);
for (int i = 2; i < 30; ++i) {
gpio_set_function(i, GPIO_FUNC_SIO);
if (i >= 26) {
gpio_disable_pulls(i);
gpio_set_input_enabled(i, false);
}
}
printf("\n===========================\n");
printf("RP2040 ADC and Test Console\n");
printf("===========================\n");
printhelp();
while (1) {
char c = getchar();
printf("%c", c);
switch (c) {
case 'c':
c = getchar();
printf("%c\n", c);
if (c < '0' || c > '7') {
printf("Unknown input channel\n");
printhelp();
} else {
adc_select_input(c - '0');
printf("Switched to channel %c\n", c);
}
break;
case 's': {
uint32_t result = adc_read();
const float conversion_factor = 3.3f / (1 << 12);
printf("\n0x%03x -> %f V\n", result, result * conversion_factor);
break;
}
case 'S': {
printf("\nStarting capture\n");
adc_capture(sample_buf, N_SAMPLES);
printf("Done\n");
for (int i = 0; i < N_SAMPLES; i = i + 1)
printf("%03x\n", sample_buf[i]);
break;
}
case 'w':
printf("\nPress any key to stop wiggling\n");
int i = 1;
gpio_set_dir_all_bits(-1);
while (getchar_timeout_us(0) == PICO_ERROR_TIMEOUT) {
// Pattern: Flash all pins for a cycle,
// Then scan along pins for one cycle each
i = i ? i << 1 : 1;
gpio_put_all(i ? i : ~0);
}
gpio_set_dir_all_bits(0);
printf("Wiggling halted.\n");
break;
case '\n':
case '\r':
break;
case 'h':
printhelp();
break;
default:
printf("\nUnrecognised command: %c\n", c);
printhelp();
break;
}
}
}