Skip to content

Commit 08e9adf

Browse files
authored
Merge pull request #566 from tock/dev/ywf-peripherals
YWF: Peripherals
2 parents a420707 + 359e1d1 commit 08e9adf

45 files changed

Lines changed: 550 additions & 321 deletions

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

examples/tests/adc/adc/main.c

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,6 @@
55

66
#include <libtock-sync/peripherals/adc.h>
77
#include <libtock-sync/services/alarm.h>
8-
#include <libtock/interface/console.h>
9-
#include <libtock/peripherals/syscalls/adc_syscalls.h>
10-
#include <libtock/tock.h>
118

129
int reference_voltage;
1310

@@ -51,15 +48,15 @@ int main(void) {
5148
printf("[Tock] ADC Test\n");
5249

5350
// check if ADC driver exists
54-
if (!libtock_adc_exists()) {
51+
if (!libtocksync_adc_exists()) {
5552
printf("No ADC driver!\n");
5653
return -1;
5754
}
5855
int count;
59-
libtock_adc_channel_count(&count);
56+
libtocksync_adc_channel_count(&count);
6057
printf("ADC driver exists with %d channels\n", count);
6158

62-
libtock_adc_command_get_reference_voltage((uint32_t*) &reference_voltage);
59+
libtocksync_adc_reference_voltage((uint32_t*) &reference_voltage);
6360
if (reference_voltage > 0) {
6461
printf("ADC reference voltage %d.%dV\n", reference_voltage / 1000, reference_voltage % 1000);
6562
} else {
@@ -68,7 +65,7 @@ int main(void) {
6865
}
6966

7067
uint32_t resolution;
71-
libtock_adc_command_get_resolution_bits(&resolution);
68+
libtocksync_adc_resolution_bits(&resolution);
7269
printf("ADC resolution %lu bits\n", resolution);
7370

7471
while (1) {

examples/tests/adc/adc_single_samples/main.c

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,21 +5,19 @@
55

66
#include <libtock-sync/peripherals/adc.h>
77
#include <libtock-sync/services/alarm.h>
8-
#include <libtock/interface/console.h>
9-
#include <libtock/tock.h>
108

119

1210
int main(void) {
1311
printf("[Tock] ADC Sample All Channels Test\n");
1412

1513
// check if ADC driver exists
16-
if (!libtock_adc_exists()) {
14+
if (!libtocksync_adc_exists()) {
1715
printf("No ADC driver!\n");
1816
return -1;
1917
}
2018

2119
int channel_count;
22-
libtock_adc_channel_count(&channel_count);
20+
libtocksync_adc_channel_count(&channel_count);
2321
printf("ADC driver exists with %d channels\n\n", channel_count);
2422

2523
while (1) {

examples/tests/crc/main.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ int main(void) {
4040
exit(1);
4141
}
4242

43-
if (!libtock_crc_exists()) {
43+
if (!libtocksync_crc_exists()) {
4444
printf("CRC driver does not exist\n");
4545
exit(1);
4646
}
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: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
GPIO Sync Interrupt Test App
2+
=============
3+
4+
Test the libtock-sync/peripherals/gpio synchronous interrupt
5+
event functions.
6+
7+
Expected Output
8+
---------------
9+
10+
```
11+
[Test] GPIO Sync Interrupt
12+
Jump GPIO pin 0 low to start test.
13+
Then move the jumper to VCC
14+
tock$
15+
Pin 0 went high
16+
Now jumper back to ground
17+
Pin 0 went low
18+
Now jumper back to high
19+
Pin 0 went back high
20+
Success! Test done
21+
```
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
#include <stdio.h>
2+
3+
#include <libtock-sync/peripherals/gpio.h>
4+
#include <libtock/interface/console.h>
5+
#include <libtock/tock.h>
6+
7+
int main(void) {
8+
returncode_t ret;
9+
10+
printf("[Test] GPIO Sync Interrupt\n");
11+
printf("Jump GPIO pin 0 low to start test.\n");
12+
printf("Then move the jumper to VCC\n");
13+
14+
ret = libtocksync_gpio_wait_until_high(0, libtock_pull_down);
15+
if (ret != RETURNCODE_SUCCESS) {
16+
printf("[ERROR] %s\n", tock_strrcode(ret));
17+
return -1;
18+
}
19+
20+
printf("Pin 0 went high\n");
21+
printf("Now jumper back to ground\n");
22+
23+
libtocksync_gpio_wait_until_low(0, libtock_pull_up);
24+
25+
printf("Pin 0 went low\n");
26+
printf("Now jumper back to high\n");
27+
28+
libtocksync_gpio_wait_until_changed(0, libtock_pull_down);
29+
30+
printf("Pin 0 went back high\n");
31+
printf("Success! Test done\n");
32+
33+
return 0;
34+
}

examples/tests/usb/main.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ int main(void) {
66
returncode_t ret;
77
printf("[TEST] UDP\n");
88

9-
if (!libtock_usb_exists()) {
9+
if (!libtocksync_usb_exists()) {
1010
printf("USB test: driver is not present\n");
1111
return -1;
1212
}

libtock-sync/peripherals/adc.c

Lines changed: 23 additions & 65 deletions
Original file line numberDiff line numberDiff line change
@@ -1,90 +1,48 @@
1+
#include <libtock/defer.h>
12
#include <libtock/peripherals/syscalls/adc_syscalls.h>
23

34
#include "adc.h"
45

5-
// used for creating synchronous versions of functions
6-
//
7-
// fired - set when the callback has been called
8-
// channel - channel that the collected sample corresponds to
9-
// sample - collected sample value, valid if single sample operation
10-
// length - number of collected sample values, valid if multiple sample
11-
// operation
12-
// buffer - pointer to buffer filled with samples, valid if multiple sample
13-
// operation
14-
// error - set to FAIL if an invalid callback type is detected
15-
struct adc_data {
16-
bool fired;
17-
uint8_t channel;
18-
uint16_t sample;
19-
uint32_t length;
20-
uint16_t* buffer;
21-
int error;
22-
};
6+
#include "syscalls/adc_syscalls.h"
237

24-
static struct adc_data result;
25-
26-
27-
static void sample(uint8_t channel, uint16_t sample) {
28-
result.fired = true;
29-
result.channel = channel;
30-
result.sample = sample;
8+
bool libtocksync_adc_exists(void) {
9+
return libtock_adc_driver_exists();
3110
}
3211

33-
static void buffered_sample(uint8_t channel, uint32_t length, uint16_t* buffer) {
34-
result.fired = true;
35-
result.channel = channel;
36-
result.length = length;
37-
result.buffer = buffer;
12+
returncode_t libtocksync_adc_channel_count(int* count) {
13+
return libtock_adc_command_channel_count(count);
3814
}
3915

16+
returncode_t libtocksync_adc_reference_voltage(uint32_t* reference_voltage) {
17+
return libtock_adc_command_get_reference_voltage(reference_voltage);
18+
}
4019

41-
static libtock_adc_callbacks callbacks = {
42-
.single_sample_callback = sample,
43-
.continuous_sample_callback = sample,
44-
.buffered_sample_callback = buffered_sample,
45-
.continuous_buffered_sample_callback = buffered_sample,
46-
};
47-
48-
49-
bool libtocksync_adc_exists(void) {
50-
return libtock_adc_driver_exists();
20+
returncode_t libtocksync_adc_resolution_bits(uint32_t* resolution) {
21+
return libtock_adc_command_get_resolution_bits(resolution);
5122
}
5223

5324
returncode_t libtocksync_adc_sample(uint8_t channel, uint16_t* sample) {
54-
int err;
55-
result.fired = false;
56-
result.error = RETURNCODE_SUCCESS;
25+
returncode_t err;
5726

58-
err = libtock_adc_single_sample(channel, &callbacks);
27+
err = libtock_adc_command_single_sample(channel);
5928
if (err != RETURNCODE_SUCCESS) return err;
6029

61-
// wait for callback
62-
yield_for(&result.fired);
63-
64-
// copy over result
65-
*sample = result.sample;
66-
67-
return result.error;
30+
err = libtocksync_adc_yield_wait_for_single_sample(sample);
31+
return err;
6832
}
6933

7034
returncode_t libtocksync_adc_sample_buffer(uint8_t channel, uint32_t frequency, uint16_t* buffer, uint32_t length) {
7135
returncode_t err;
72-
result.fired = false;
73-
result.error = RETURNCODE_SUCCESS;
74-
75-
err = libtock_adc_set_buffer(buffer, length);
76-
if (err < RETURNCODE_SUCCESS) return err;
36+
uint32_t actual_length;
7737

78-
err = libtock_adc_buffered_sample(channel, frequency, &callbacks);
38+
err = libtock_adc_set_readwrite_allow_set_buffer((uint8_t*) buffer, length * 2);
7939
if (err != RETURNCODE_SUCCESS) return err;
80-
81-
// wait for callback
82-
yield_for(&result.fired);
83-
84-
// copy over result
85-
if (result.buffer != buffer) {
86-
return RETURNCODE_FAIL;
40+
defer { libtock_adc_set_readwrite_allow_set_buffer(NULL, 0);
8741
}
8842

89-
return result.error;
43+
err = libtock_adc_command_buffered_sample(channel, frequency);
44+
if (err != RETURNCODE_SUCCESS) return err;
45+
46+
err = libtocksync_adc_yield_wait_for_buffered_sample(&actual_length);
47+
return err;
9048
}

libtock-sync/peripherals/adc.h

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
#pragma once
22

3-
#include <libtock/peripherals/adc.h>
43
#include <libtock/tock.h>
54

65
#ifdef __cplusplus
@@ -9,6 +8,15 @@ extern "C" {
98

109
bool libtocksync_adc_exists(void);
1110

11+
// Query the number of ADC channels available.
12+
returncode_t libtocksync_adc_channel_count(int* count);
13+
14+
// Query the ADC reference voltage in millivolts. Returns 0 if not available.
15+
returncode_t libtocksync_adc_reference_voltage(uint32_t* reference_voltage);
16+
17+
// Query the ADC resolution in bits.
18+
returncode_t libtocksync_adc_resolution_bits(uint32_t* resolution);
19+
1220
returncode_t libtocksync_adc_sample(uint8_t channel, uint16_t* sample);
1321

1422
returncode_t libtocksync_adc_sample_buffer(uint8_t channel, uint32_t frequency, uint16_t* buffer, uint32_t length);

libtock-sync/peripherals/crc.c

Lines changed: 8 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,39 +1,25 @@
1+
#include <libtock/defer.h>
12
#include <libtock/peripherals/syscalls/crc_syscalls.h>
23

34
#include "crc.h"
45

5-
struct crc_data {
6-
bool fired;
7-
int status;
8-
uint32_t crc;
9-
};
10-
11-
static struct crc_data result = {.fired = false};
12-
13-
static void crc_callback(returncode_t ret, uint32_t crc) {
14-
result.fired = true;
15-
result.status = ret;
16-
result.crc = crc;
17-
}
6+
#include "syscalls/crc_syscalls.h"
187

198
bool libtocksync_crc_exists(void) {
209
return libtock_crc_driver_exists();
2110
}
2211

2312
returncode_t libtocksync_crc_compute(const uint8_t* buf, size_t buflen, libtock_crc_alg_t algorithm, uint32_t* crc) {
2413
returncode_t ret;
25-
result.fired = false;
2614

27-
ret = libtock_crc_compute(buf, buflen, algorithm, crc_callback);
15+
ret = libtock_crc_set_readonly_allow(buf, buflen);
2816
if (ret != RETURNCODE_SUCCESS) return ret;
17+
defer { libtock_crc_set_readonly_allow(NULL, 0);
18+
}
2919

30-
yield_for(&result.fired);
31-
if (result.status != RETURNCODE_SUCCESS) return result.status;
32-
33-
ret = libtock_crc_set_readonly_allow(NULL, 0);
20+
ret = libtock_crc_command_request((uint32_t) algorithm, buflen);
3421
if (ret != RETURNCODE_SUCCESS) return ret;
3522

36-
*crc = result.crc;
37-
38-
return RETURNCODE_SUCCESS;
23+
ret = libtocksync_crc_yield_wait_for(crc);
24+
return ret;
3925
}

0 commit comments

Comments
 (0)