Skip to content

Commit

Permalink
libtock: sensors: temp: update to new format
Browse files Browse the repository at this point in the history
  • Loading branch information
bradjc committed Feb 9, 2024
1 parent 2c18e78 commit c7d056a
Show file tree
Hide file tree
Showing 5 changed files with 71 additions and 54 deletions.
1 change: 1 addition & 0 deletions libtock/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ $(LIBNAME)_SRCS += $(wildcard $($(LIBNAME)_DIR)/kernel/*.c)
$(LIBNAME)_SRCS += $(wildcard $($(LIBNAME)_DIR)/net/*.c)
$(LIBNAME)_SRCS += $(wildcard $($(LIBNAME)_DIR)/peripherals/*.c)
$(LIBNAME)_SRCS += $(wildcard $($(LIBNAME)_DIR)/sensors/*.c)
$(LIBNAME)_SRCS += $(wildcard $($(LIBNAME)_DIR)/sensors/internal/*.c)
$(LIBNAME)_SRCS += $(wildcard $($(LIBNAME)_DIR)/storage/*.c)

include $(TOCK_USERLAND_BASE_DIR)/TockLibrary.mk
16 changes: 16 additions & 0 deletions libtock/sensors/internal/temperature.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
#include "temperature.h"
#include "tock.h"

bool temperature_exists(void) {
return driver_exists(DRIVER_NUM_TEMPERATURE);
}

int temperature_set_upcall(subscribe_upcall callback, void* callback_args) {
subscribe_return_t sval = subscribe(DRIVER_NUM_TEMPERATURE, 0, callback, callback_args);
return tock_subscribe_return_to_returncode(sval);
}

int temperature_command_read(void) {
syscall_return_t cval = command(DRIVER_NUM_TEMPERATURE, 1, 0, 0);
return tock_command_return_novalue_to_returncode(cval);
}
31 changes: 31 additions & 0 deletions libtock/sensors/internal/temperature_internal.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
#pragma once

#include "tock.h"

#ifdef __cplusplus
extern "C" {
#endif

#define DRIVER_NUM_TEMPERATURE 0x60000

// Check if temperature sensor exists.
bool temperature_exists(void);

// units: temperature in hundredths of degrees centigrade.

// Set the callback function to be called when the temperature measurement is
// finished.
//
// callback - pointer to function to be called
// callback_args - pointer to data provided to the callback
int temperature_set_upcall(subscribe_upcall callback, void* callback_args);

// initiate an ambient temperature measurement used both for synchronous and asynchronous readings
int temperature_command_read(void);




#ifdef __cplusplus
}
#endif
54 changes: 15 additions & 39 deletions libtock/sensors/temperature.c
Original file line number Diff line number Diff line change
@@ -1,51 +1,27 @@
#include "temperature.h"
#include "tock.h"

struct data {
bool fired;
int temp;
};

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

// Internal upcall for faking synchronous reads
static void temp_upcall(int temp,
__attribute__ ((unused)) int unused,
__attribute__ ((unused)) int unused1,
void* ud) {
struct data* data = (struct data*) ud;
data->temp = temp;
data->fired = true;
}
#include "temperature.h"
#include "temperature_internal.h"

bool temperature_exists(void) {
return driver_exists(DRIVER_NUM_TEMPERATURE);
}

int temperature_set_callback(subscribe_upcall callback, void* callback_args) {
subscribe_return_t sval = subscribe(DRIVER_NUM_TEMPERATURE, 0, callback, callback_args);
return tock_subscribe_return_to_returncode(sval);
}

int temperature_read(void) {
syscall_return_t cval = command(DRIVER_NUM_TEMPERATURE, 1, 0, 0);
return tock_command_return_novalue_to_returncode(cval);
// Internal upcall for passing to the syscall driver.
static void temperature_temp_upcall(int temp,
__attribute__ ((unused)) int unused,
__attribute__ ((unused)) int unused1,
void* ud) {
temperature_callback cb = (temperature_callback) ud;
cb(RETURNCODE_SUCCESS, temp);
}

int temperature_read_sync(int* temperature) {
int err;
result.fired = false;

err = temperature_set_callback(temp_upcall, (void*) &result);
if (err < 0) return err;

err = temperature_read();
if (err < 0) return err;

// Wait for the callback.
yield_for(&result.fired);
int temperature_read(temperature_callback cb) {
int ret;

*temperature = result.temp;
ret = temperature_set_upcall(temperature_temp_upcall, cb);
if (ret != RETURNCODE_SUCCESS) return ret;

return RETURNCODE_SUCCESS;
ret = temperature_command_read();
return ret;
}
23 changes: 8 additions & 15 deletions libtock/sensors/temperature.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,27 +6,20 @@
extern "C" {
#endif

#define DRIVER_NUM_TEMPERATURE 0x60000

// check if temperature sensor exists
bool temperature_exists(void);

// units: temperature in hundredths of degrees centigrade.

// function to be called when the temperature measurement is finished
// Function signature for temperature data callback.
//
// callback - pointer to function to be called
// callback_args - pointer to data provided to the callback
int temperature_set_callback (subscribe_upcall callback, void* callback_args);
// - `arg1` (`int`): Returncode indicating status from sampling the sensor.
// - `arg2` (`int`): Temperature reading in hundredths of degrees centigrade.
typedef void (*temperature_callback)(returncode_t, int);

// initiate an ambient temperature measurement used both for synchronous and asynchronous readings
int temperature_read(void);


// initiate a synchronous ambient temperature measurement
//
// temperature - pointer/address where the result of the temperature reading should be stored
int temperature_read_sync (int* temperature);
// Initiate an ambient temperature measurement and return results via the `cb`.
int temperature_read(temperature_callback cb);



#ifdef __cplusplus
}
Expand Down

0 comments on commit c7d056a

Please sign in to comment.