Skip to content
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 4 additions & 7 deletions examples/tests/adc/adc/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,6 @@

#include <libtock-sync/peripherals/adc.h>
#include <libtock-sync/services/alarm.h>
#include <libtock/interface/console.h>
#include <libtock/peripherals/syscalls/adc_syscalls.h>
#include <libtock/tock.h>

int reference_voltage;

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

// check if ADC driver exists
if (!libtock_adc_exists()) {
if (!libtocksync_adc_exists()) {
printf("No ADC driver!\n");
return -1;
}
int count;
libtock_adc_channel_count(&count);
libtocksync_adc_channel_count(&count);
printf("ADC driver exists with %d channels\n", count);

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

uint32_t resolution;
libtock_adc_command_get_resolution_bits(&resolution);
libtocksync_adc_resolution_bits(&resolution);
printf("ADC resolution %lu bits\n", resolution);

while (1) {
Expand Down
6 changes: 2 additions & 4 deletions examples/tests/adc/adc_single_samples/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -5,21 +5,19 @@

#include <libtock-sync/peripherals/adc.h>
#include <libtock-sync/services/alarm.h>
#include <libtock/interface/console.h>
#include <libtock/tock.h>


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

// check if ADC driver exists
if (!libtock_adc_exists()) {
if (!libtocksync_adc_exists()) {
printf("No ADC driver!\n");
return -1;
}

int channel_count;
libtock_adc_channel_count(&channel_count);
libtocksync_adc_channel_count(&channel_count);
printf("ADC driver exists with %d channels\n\n", channel_count);

while (1) {
Expand Down
2 changes: 1 addition & 1 deletion examples/tests/crc/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ int main(void) {
exit(1);
}

if (!libtock_crc_exists()) {
if (!libtocksync_crc_exists()) {
printf("CRC driver does not exist\n");
exit(1);
}
Expand Down
11 changes: 11 additions & 0 deletions examples/tests/gpio/gpio_sync/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
# Makefile for user application

# Specify this directory relative to the current application.
TOCK_USERLAND_BASE_DIR = ../../../..

# Which files to compile.
C_SRCS := $(wildcard *.c)

# Include userland master makefile. Contains rules and flags for actually
# building the application.
include $(TOCK_USERLAND_BASE_DIR)/AppMakefile.mk
21 changes: 21 additions & 0 deletions examples/tests/gpio/gpio_sync/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
GPIO Sync Interrupt Test App
=============

Test the libtock-sync/peripherals/gpio synchronous interrupt
event functions.

Expected Output
---------------

```
[Test] GPIO Sync Interrupt
Jump GPIO pin 0 low to start test.
Then move the jumper to VCC
tock$
Pin 0 went high
Now jumper back to ground
Pin 0 went low
Now jumper back to high
Pin 0 went back high
Success! Test done
```
34 changes: 34 additions & 0 deletions examples/tests/gpio/gpio_sync/main.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
#include <stdio.h>

#include <libtock-sync/peripherals/gpio.h>
#include <libtock/interface/console.h>
#include <libtock/tock.h>

int main(void) {
returncode_t ret;

printf("[Test] GPIO Sync Interrupt\n");
printf("Jump GPIO pin 0 low to start test.\n");
printf("Then move the jumper to VCC\n");

ret = libtocksync_gpio_wait_until_high(0, libtock_pull_down);
if (ret != RETURNCODE_SUCCESS) {
printf("[ERROR] %s\n", tock_strrcode(ret));
return -1;
}

printf("Pin 0 went high\n");
printf("Now jumper back to ground\n");

libtocksync_gpio_wait_until_low(0, libtock_pull_up);

printf("Pin 0 went low\n");
printf("Now jumper back to high\n");

libtocksync_gpio_wait_until_changed(0, libtock_pull_down);

printf("Pin 0 went back high\n");
printf("Success! Test done\n");

return 0;
}
2 changes: 1 addition & 1 deletion examples/tests/usb/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ int main(void) {
returncode_t ret;
printf("[TEST] UDP\n");

if (!libtock_usb_exists()) {
if (!libtocksync_usb_exists()) {
printf("USB test: driver is not present\n");
return -1;
}
Expand Down
88 changes: 23 additions & 65 deletions libtock-sync/peripherals/adc.c
Original file line number Diff line number Diff line change
@@ -1,90 +1,48 @@
#include <libtock/defer.h>
#include <libtock/peripherals/syscalls/adc_syscalls.h>

#include "adc.h"

// used for creating synchronous versions of functions
//
// fired - set when the callback has been called
// channel - channel that the collected sample corresponds to
// sample - collected sample value, valid if single sample operation
// length - number of collected sample values, valid if multiple sample
// operation
// buffer - pointer to buffer filled with samples, valid if multiple sample
// operation
// error - set to FAIL if an invalid callback type is detected
struct adc_data {
bool fired;
uint8_t channel;
uint16_t sample;
uint32_t length;
uint16_t* buffer;
int error;
};
#include "syscalls/adc_syscalls.h"

static struct adc_data result;


static void sample(uint8_t channel, uint16_t sample) {
result.fired = true;
result.channel = channel;
result.sample = sample;
bool libtocksync_adc_exists(void) {
return libtock_adc_driver_exists();
}

static void buffered_sample(uint8_t channel, uint32_t length, uint16_t* buffer) {
result.fired = true;
result.channel = channel;
result.length = length;
result.buffer = buffer;
returncode_t libtocksync_adc_channel_count(int* count) {
return libtock_adc_command_channel_count(count);
}

returncode_t libtocksync_adc_reference_voltage(uint32_t* reference_voltage) {
return libtock_adc_command_get_reference_voltage(reference_voltage);
}

static libtock_adc_callbacks callbacks = {
.single_sample_callback = sample,
.continuous_sample_callback = sample,
.buffered_sample_callback = buffered_sample,
.continuous_buffered_sample_callback = buffered_sample,
};


bool libtocksync_adc_exists(void) {
return libtock_adc_driver_exists();
returncode_t libtocksync_adc_resolution_bits(uint32_t* resolution) {
return libtock_adc_command_get_resolution_bits(resolution);
}

returncode_t libtocksync_adc_sample(uint8_t channel, uint16_t* sample) {
int err;
result.fired = false;
result.error = RETURNCODE_SUCCESS;
returncode_t err;

err = libtock_adc_single_sample(channel, &callbacks);
err = libtock_adc_command_single_sample(channel);
if (err != RETURNCODE_SUCCESS) return err;

// wait for callback
yield_for(&result.fired);

// copy over result
*sample = result.sample;

return result.error;
err = libtocksync_adc_yield_wait_for_single_sample(sample);
return err;
}

returncode_t libtocksync_adc_sample_buffer(uint8_t channel, uint32_t frequency, uint16_t* buffer, uint32_t length) {
returncode_t err;
result.fired = false;
result.error = RETURNCODE_SUCCESS;

err = libtock_adc_set_buffer(buffer, length);
if (err < RETURNCODE_SUCCESS) return err;
uint32_t actual_length;

err = libtock_adc_buffered_sample(channel, frequency, &callbacks);
err = libtock_adc_set_readwrite_allow_set_buffer((uint8_t*) buffer, length * 2);
if (err != RETURNCODE_SUCCESS) return err;

// wait for callback
yield_for(&result.fired);

// copy over result
if (result.buffer != buffer) {
return RETURNCODE_FAIL;
defer { libtock_adc_set_readwrite_allow_set_buffer(NULL, 0);
}

return result.error;
err = libtock_adc_command_buffered_sample(channel, frequency);
if (err != RETURNCODE_SUCCESS) return err;

err = libtocksync_adc_yield_wait_for_buffered_sample(&actual_length);
return err;
}
10 changes: 9 additions & 1 deletion libtock-sync/peripherals/adc.h
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
#pragma once

#include <libtock/peripherals/adc.h>
#include <libtock/tock.h>

#ifdef __cplusplus
Expand All @@ -9,6 +8,15 @@ extern "C" {

bool libtocksync_adc_exists(void);

// Query the number of ADC channels available.
returncode_t libtocksync_adc_channel_count(int* count);

// Query the ADC reference voltage in millivolts. Returns 0 if not available.
returncode_t libtocksync_adc_reference_voltage(uint32_t* reference_voltage);

// Query the ADC resolution in bits.
returncode_t libtocksync_adc_resolution_bits(uint32_t* resolution);

returncode_t libtocksync_adc_sample(uint8_t channel, uint16_t* sample);

returncode_t libtocksync_adc_sample_buffer(uint8_t channel, uint32_t frequency, uint16_t* buffer, uint32_t length);
Expand Down
30 changes: 8 additions & 22 deletions libtock-sync/peripherals/crc.c
Original file line number Diff line number Diff line change
@@ -1,39 +1,25 @@
#include <libtock/defer.h>
#include <libtock/peripherals/syscalls/crc_syscalls.h>

#include "crc.h"

struct crc_data {
bool fired;
int status;
uint32_t crc;
};

static struct crc_data result = {.fired = false};

static void crc_callback(returncode_t ret, uint32_t crc) {
result.fired = true;
result.status = ret;
result.crc = crc;
}
#include "syscalls/crc_syscalls.h"

bool libtocksync_crc_exists(void) {
return libtock_crc_driver_exists();
}

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

ret = libtock_crc_compute(buf, buflen, algorithm, crc_callback);
ret = libtock_crc_set_readonly_allow(buf, buflen);
if (ret != RETURNCODE_SUCCESS) return ret;
defer { libtock_crc_set_readonly_allow(NULL, 0);
}

yield_for(&result.fired);
if (result.status != RETURNCODE_SUCCESS) return result.status;

ret = libtock_crc_set_readonly_allow(NULL, 0);
ret = libtock_crc_command_request((uint32_t) algorithm, buflen);
if (ret != RETURNCODE_SUCCESS) return ret;

*crc = result.crc;

return RETURNCODE_SUCCESS;
ret = libtocksync_crc_yield_wait_for(crc);
return ret;
}
2 changes: 1 addition & 1 deletion libtock-sync/peripherals/crc.h
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#pragma once

#include <libtock/peripherals/crc.h>
#include <libtock/peripherals/crc_types.h>
#include <libtock/tock.h>

#ifdef __cplusplus
Expand Down
Loading
Loading