Skip to content

Commit f534d6e

Browse files
authored
Merge pull request #574 from tock/dev/adc-no-ptr
adc: update to upcall index 1
2 parents dbe850f + b545648 commit f534d6e

11 files changed

Lines changed: 376 additions & 108 deletions

File tree

examples/tests/adc/adc_continuous/README.md

Lines changed: 25 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,13 @@
11
ADC Continuous Test App
22
=======================
33

4-
Demonstrates asynchronous continuous collection of analog samples in Tock.
5-
Manually provides buffers and callbacks to the ADC driver and then begins
6-
continuously sampling at 44.1 kHz. The provided buffers are 4410 Bytes in
7-
length, so a callback occurs every 100 milliseconds.
4+
Demonstrates asynchronous continuous collection of single analog samples in
5+
Tock. Samples at 10 Hz, and gets a sample callback every 100 ms.
86

97
On each callback, the samples are converted to millivolts and statistics about
108
the data are collected (min, max, and average). Results are printed to the
119
console.
1210

13-
Every 100 samples, the ADC is stopped and the values of an entire buffer are
14-
printed to the console synchronously. Once the printing is complete, the ADC is
15-
started again.
16-
1711
Note that the ADC is not virtualized and can currently only be used by a single
1812
application. Results are undefined if multiple applications (such as this and
1913
the `hail` app) attempt to use the ADC at once. All applications on the system
@@ -23,13 +17,29 @@ Example Output
2317
--------------
2418

2519
```
20+
Initialization complete. Entering main loop.
2621
[Tock] ADC Continuous Test
2722
ADC driver exists with 6 channels
28-
Beginning continuous sampling on channel 0 at 44100 Hz
29-
Channel: 0 Count: 4410 Avg: 2167 Min: 2119 Max: 2218
30-
Channel: 0 Count: 4410 Avg: 2167 Min: 2114 Max: 2210
31-
Channel: 0 Count: 4410 Avg: 2167 Min: 2121 Max: 2214
32-
Channel: 0 Count: 4410 Avg: 2167 Min: 2120 Max: 2211
33-
Channel: 0 Count: 4410 Avg: 2167 Min: 2116 Max: 2209
23+
Beginning continuous sampling on channel 0 at 10 Hz
24+
Channel: 0 Value: 52800
25+
Channel: 0 Value: 52800
26+
Channel: 0 Value: 52774
27+
Channel: 0 Value: 52800
28+
Channel: 0 Value: 52800
29+
Channel: 0 Value: 52800
30+
Channel: 0 Value: 52800
31+
Channel: 0 Value: 52800
32+
Channel: 0 Value: 52800
33+
Channel: 0 Value: 52800
34+
Beginning continuous sampling on channel 0 at 10 Hz
35+
Channel: 0 Value: 52800
36+
Channel: 0 Value: 52800
37+
Channel: 0 Value: 52800
38+
Channel: 0 Value: 52800
39+
Channel: 0 Value: 52800
40+
Channel: 0 Value: 52800
41+
Channel: 0 Value: 52800
42+
Channel: 0 Value: 52800
43+
Channel: 0 Value: 52800
44+
Channel: 0 Value: 52800
3445
```
35-

examples/tests/adc/adc_continuous/main.c

Lines changed: 8 additions & 79 deletions
Original file line numberDiff line numberDiff line change
@@ -10,31 +10,17 @@
1010
// Sample the first channel. On Hail, this is external pin A0 (AD0)
1111
#define ADC_CHANNEL 0
1212

13-
// Sampling frequencies
13+
// Sampling frequency
1414
#define ADC_LOWSPEED_FREQUENCY 10
15-
#define ADC_HIGHSPEED_FREQUENCY 44100
16-
17-
// Buffer size
18-
// Given a sampling frequency, we will receive callbacks every
19-
// BUF_SIZE/FREQ seconds. At 44100 Hz and 4410 samples, this is a callback
20-
// every 50 ms
21-
#define BUF_SIZE 2205
22-
23-
// data buffers
24-
static uint16_t sample_buffer1[BUF_SIZE] = {0};
25-
static uint16_t sample_buffer2[BUF_SIZE] = {0};
2615

2716
static void continuous_sample_cb(uint8_t channel,
2817
uint16_t sample);
29-
static void continuous_buffered_sample_cb(uint8_t channel,
30-
uint32_t length,
31-
uint16_t* buf_ptr);
3218

3319
static libtock_adc_callbacks callbacks = {
3420
.single_sample_callback = NULL,
3521
.continuous_sample_callback = continuous_sample_cb,
3622
.buffered_sample_callback = NULL,
37-
.continuous_buffered_sample_callback = continuous_buffered_sample_cb,
23+
.continuous_buffered_sample_callback = NULL,
3824
};
3925

4026

@@ -62,52 +48,6 @@ static void continuous_sample_cb(uint8_t channel,
6248
}
6349

6450
// start buffered sampling
65-
printf("Beginning buffered sampling on channel %d at %d Hz\n",
66-
ADC_CHANNEL, ADC_HIGHSPEED_FREQUENCY);
67-
err = libtock_adc_continuous_buffered_sample(ADC_CHANNEL, ADC_HIGHSPEED_FREQUENCY, &callbacks);
68-
if (err < RETURNCODE_SUCCESS) {
69-
printf("continuous buffered sample error: %d\n", err);
70-
return;
71-
}
72-
}
73-
}
74-
75-
static void continuous_buffered_sample_cb(uint8_t channel,
76-
uint32_t length,
77-
uint16_t* buf_ptr) {
78-
// buffer of ADC samples is ready
79-
static uint8_t counter = 0;
80-
81-
// calculate and print statistics about the data
82-
uint32_t sum = 0;
83-
uint16_t min = 0xFFFF;
84-
uint16_t max = 0;
85-
for (uint32_t i = 0; i < length; i++) {
86-
uint16_t sample = (buf_ptr[i] * 3300 / 4095);
87-
sum += sample;
88-
if (sample < min) {
89-
min = sample;
90-
}
91-
if (sample > max) {
92-
max = sample;
93-
}
94-
}
95-
printf("Channel: %u\tCount: %d\tAvg: %lu\tMin: %u\tMax: %u\n",
96-
channel, BUF_SIZE, sum / BUF_SIZE, min, max);
97-
98-
// determine when to switch sampling method
99-
counter++;
100-
if (counter == 10) {
101-
counter = 0;
102-
103-
// stop single sampling
104-
int err = libtock_adc_stop_sampling();
105-
if (err < RETURNCODE_SUCCESS) {
106-
printf("Failed to stop sampling: %d\n", err);
107-
return;
108-
}
109-
110-
// start single sampling
11151
printf("Beginning continuous sampling on channel %d at %d Hz\n",
11252
ADC_CHANNEL, ADC_LOWSPEED_FREQUENCY);
11353
err = libtock_adc_continuous_sample(ADC_CHANNEL, ADC_LOWSPEED_FREQUENCY, &callbacks);
@@ -118,6 +58,7 @@ static void continuous_buffered_sample_cb(uint8_t channel,
11858
}
11959
}
12060

61+
12162
int main(void) {
12263
int err;
12364
printf("[Tock] ADC Continuous Test\n");
@@ -131,21 +72,6 @@ int main(void) {
13172
libtock_adc_channel_count(&count);
13273
printf("ADC driver exists with %d channels\n", count);
13374

134-
// set main buffer for ADC samples
135-
err = libtock_adc_set_buffer(sample_buffer1, BUF_SIZE);
136-
if (err < RETURNCODE_SUCCESS) {
137-
printf("set buffer error: %d\n", err);
138-
return -1;
139-
}
140-
141-
// set secondary buffer for ADC samples. In continuous mode, the ADC will
142-
// automatically switch between the two each callback
143-
err = libtock_adc_set_double_buffer(sample_buffer2, BUF_SIZE);
144-
if (err < RETURNCODE_SUCCESS) {
145-
printf("set double buffer error: %d\n", err);
146-
return -1;
147-
}
148-
14975
// begin continuous sampling
15076
printf("Beginning continuous sampling on channel %d at %d Hz\n",
15177
ADC_CHANNEL, ADC_LOWSPEED_FREQUENCY);
@@ -155,7 +81,10 @@ int main(void) {
15581
return -1;
15682
}
15783

158-
// return successfully. The system automatically calls `yield` continuously
159-
// for us
84+
// loop forever
85+
while (1) {
86+
yield();
87+
}
88+
16089
return 0;
16190
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
# Makefile for user application
2+
3+
# Specify this directory relative to the current application.
4+
TOCK_USERLAND_BASE_DIR = ../../../..
5+
6+
# Which files to compile.
7+
C_SRCS := $(wildcard *.c)
8+
9+
# Include userland master makefile. Contains rules and flags for actually
10+
# building the application.
11+
include $(TOCK_USERLAND_BASE_DIR)/AppMakefile.mk
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
ADC Double Buffered Test App
2+
============================
3+
4+
Demonstrates asynchronous continuous collection of analog samples in Tock.
5+
Manually provides buffers and callbacks to the ADC driver and then begins
6+
continuously sampling at 44.1 kHz. The provided buffers are 4410 Bytes in
7+
length, so a callback occurs every 100 milliseconds.
8+
9+
On each callback, the samples are converted to millivolts and statistics about
10+
the data are collected (min, max, and average). Results are printed to the
11+
console.
12+
13+
Note that the ADC is not virtualized and can currently only be used by a single
14+
application. Results are undefined if multiple applications (such as this and
15+
the `hail` app) attempt to use the ADC at once. All applications on the system
16+
can be erased with `tockloader erase-apps`.
17+
18+
Example Output
19+
--------------
20+
21+
```
22+
Initialization complete. Entering main loop.
23+
[Tock] ADC Double Buffered Test
24+
ADC driver exists with 6 channels
25+
Beginning buffered sampling on channel 0 at 44100 Hz
26+
Channel: 0 Count: 2205 Avg: 50037 Min: 49060 Max: 50878
27+
Channel: 0 Count: 2205 Avg: 50036 Min: 49176 Max: 50917
28+
Channel: 0 Count: 2205 Avg: 50039 Min: 49112 Max: 50788
29+
Channel: 0 Count: 2205 Avg: 50028 Min: 49176 Max: 50969
30+
Channel: 0 Count: 2205 Avg: 50033 Min: 49138 Max: 50827
31+
Channel: 0 Count: 2205 Avg: 50035 Min: 49254 Max: 50865
32+
Channel: 0 Count: 2205 Avg: 50033 Min: 49305 Max: 50981
33+
Channel: 0 Count: 2205 Avg: 50042 Min: 49280 Max: 50827
34+
Channel: 0 Count: 2205 Avg: 50037 Min: 49202 Max: 50865
35+
Channel: 0 Count: 2205 Avg: 50037 Min: 49112 Max: 50878
36+
Channel: 0 Count: 2205 Avg: 50038 Min: 49047 Max: 50917
37+
```
Lines changed: 124 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,124 @@
1+
#include <inttypes.h>
2+
#include <stdio.h>
3+
#include <stdlib.h>
4+
#include <string.h>
5+
#include <unistd.h>
6+
7+
#include <libtock/interface/console.h>
8+
#include <libtock/peripherals/adc.h>
9+
#include <libtock/tock.h>
10+
11+
// Sample the first channel. On Hail, this is external pin A0 (AD0)
12+
#define ADC_CHANNEL 0
13+
14+
// Sampling frequency
15+
#define ADC_HIGHSPEED_FREQUENCY 44100
16+
17+
// Buffer size
18+
// Given a sampling frequency, we will receive callbacks every
19+
// BUF_SIZE/FREQ seconds. At 44100 Hz and 4410 samples, this is a callback
20+
// every 50 ms
21+
#define BUF_SIZE 2205
22+
23+
// data buffers
24+
static uint16_t sample_buffer1[BUF_SIZE] = {0};
25+
static uint16_t sample_buffer2[BUF_SIZE] = {0};
26+
27+
28+
static void continuous_buffered_sample_cb(uint8_t channel,
29+
uint32_t length,
30+
uint8_t buffer_index);
31+
32+
static libtock_adc_callbacks callbacks = {
33+
.single_sample_callback = NULL,
34+
.continuous_sample_callback = NULL,
35+
.buffered_sample_callback = NULL,
36+
.continuous_buffered_sample_callback = continuous_buffered_sample_cb,
37+
};
38+
39+
40+
41+
static void continuous_buffered_sample_cb(uint8_t channel,
42+
uint32_t length,
43+
uint8_t buffer_index) {
44+
45+
uint16_t* buf_ptr;
46+
47+
// retrieve the filled buffer
48+
if (buffer_index == 0) {
49+
libtock_adc_set_buffer(NULL, 0);
50+
buf_ptr = sample_buffer1;
51+
} else {
52+
libtock_adc_set_double_buffer(NULL, 0);
53+
buf_ptr = sample_buffer2;
54+
}
55+
56+
// calculate and print statistics about the data
57+
uint32_t sum = 0;
58+
uint16_t min = 0xFFFF;
59+
uint16_t max = 0;
60+
for (uint32_t i = 0; i < length; i++) {
61+
uint16_t sample = (buf_ptr[i] * 3300 / 4095);
62+
sum += sample;
63+
if (sample < min) {
64+
min = sample;
65+
}
66+
if (sample > max) {
67+
max = sample;
68+
}
69+
}
70+
printf("Channel: %u\tCount: %d\tAvg: %" PRIu32 "\tMin: %u\tMax: %u\n",
71+
channel, BUF_SIZE, sum / BUF_SIZE, min, max);
72+
73+
// re-allow the missing buffer
74+
if (buffer_index == 0) {
75+
libtock_adc_set_buffer(sample_buffer1, BUF_SIZE);
76+
} else {
77+
libtock_adc_set_double_buffer(sample_buffer2, BUF_SIZE);
78+
}
79+
}
80+
81+
int main(void) {
82+
int err;
83+
printf("[Tock] ADC Double Buffered Test\n");
84+
85+
// check if ADC driver exists
86+
if (!libtock_adc_exists()) {
87+
printf("No ADC driver!\n");
88+
exit(-1);
89+
}
90+
int count;
91+
libtock_adc_channel_count(&count);
92+
printf("ADC driver exists with %d channels\n", count);
93+
94+
// set main buffer for ADC samples
95+
err = libtock_adc_set_buffer(sample_buffer1, BUF_SIZE);
96+
if (err < RETURNCODE_SUCCESS) {
97+
printf("set buffer error: %d\n", err);
98+
exit(-1);
99+
}
100+
101+
// set secondary buffer for ADC samples. In buffered mode, the ADC will
102+
// automatically switch between the two each callback
103+
err = libtock_adc_set_double_buffer(sample_buffer2, BUF_SIZE);
104+
if (err < RETURNCODE_SUCCESS) {
105+
printf("set double buffer error: %d\n", err);
106+
exit(-1);
107+
}
108+
109+
// start buffered sampling
110+
printf("Beginning buffered sampling on channel %d at %d Hz\n",
111+
ADC_CHANNEL, ADC_HIGHSPEED_FREQUENCY);
112+
err = libtock_adc_continuous_buffered_sample(ADC_CHANNEL, ADC_HIGHSPEED_FREQUENCY, &callbacks);
113+
if (err < RETURNCODE_SUCCESS) {
114+
printf("continuous buffered sample error: %d\n", err);
115+
exit(-1);
116+
}
117+
118+
// loop forever
119+
while (1) {
120+
yield();
121+
}
122+
123+
return 0;
124+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
# Makefile for user application
2+
3+
# Specify this directory relative to the current application.
4+
TOCK_USERLAND_BASE_DIR = ../../../..
5+
6+
# Which files to compile.
7+
C_SRCS := $(wildcard *.c)
8+
9+
# Include userland master makefile. Contains rules and flags for actually
10+
# building the application.
11+
include $(TOCK_USERLAND_BASE_DIR)/AppMakefile.mk

0 commit comments

Comments
 (0)