From c7d056ad6d719abab93a7238fa5d7ee9465291ab Mon Sep 17 00:00:00 2001 From: Brad Campbell Date: Fri, 9 Feb 2024 16:12:43 -0500 Subject: [PATCH] libtock: sensors: temp: update to new format --- libtock/Makefile | 1 + libtock/sensors/internal/temperature.c | 16 ++++++ .../sensors/internal/temperature_internal.h | 31 +++++++++++ libtock/sensors/temperature.c | 54 ++++++------------- libtock/sensors/temperature.h | 23 +++----- 5 files changed, 71 insertions(+), 54 deletions(-) create mode 100644 libtock/sensors/internal/temperature.c create mode 100644 libtock/sensors/internal/temperature_internal.h diff --git a/libtock/Makefile b/libtock/Makefile index 7383ac713..9c8978ad7 100644 --- a/libtock/Makefile +++ b/libtock/Makefile @@ -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 diff --git a/libtock/sensors/internal/temperature.c b/libtock/sensors/internal/temperature.c new file mode 100644 index 000000000..d99f03fe5 --- /dev/null +++ b/libtock/sensors/internal/temperature.c @@ -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); +} diff --git a/libtock/sensors/internal/temperature_internal.h b/libtock/sensors/internal/temperature_internal.h new file mode 100644 index 000000000..56bb935aa --- /dev/null +++ b/libtock/sensors/internal/temperature_internal.h @@ -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 diff --git a/libtock/sensors/temperature.c b/libtock/sensors/temperature.c index 3da58b414..ecf130696 100644 --- a/libtock/sensors/temperature.c +++ b/libtock/sensors/temperature.c @@ -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; } diff --git a/libtock/sensors/temperature.h b/libtock/sensors/temperature.h index 58379ac75..730ca2d69 100644 --- a/libtock/sensors/temperature.h +++ b/libtock/sensors/temperature.h @@ -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 }