diff --git a/drivers/sensor/CMakeLists.txt b/drivers/sensor/CMakeLists.txt index 8cbf1f704cf6a..7db6aa7f61af6 100644 --- a/drivers/sensor/CMakeLists.txt +++ b/drivers/sensor/CMakeLists.txt @@ -41,6 +41,7 @@ add_subdirectory_ifdef(CONFIG_PMS7003 pms7003) add_subdirectory_ifdef(CONFIG_QDEC_NRFX qdec_nrfx) add_subdirectory_ifdef(CONFIG_TEMP_NRF5 nrf5) add_subdirectory_ifdef(CONFIG_SHT3XD sht3xd) +add_subdirectory_ifdef(CONFIG_SIMACCEL simaccel) add_subdirectory_ifdef(CONFIG_SX9500 sx9500) add_subdirectory_ifdef(CONFIG_TH02 th02) add_subdirectory_ifdef(CONFIG_TMP007 tmp007) diff --git a/drivers/sensor/simaccel/CMakeList.txt b/drivers/sensor/simaccel/CMakeList.txt new file mode 100644 index 0000000000000..fd365c104124f --- /dev/null +++ b/drivers/sensor/simaccel/CMakeList.txt @@ -0,0 +1,3 @@ +zephyr_library() + +zephyr_library_sources_ifdef(CONFIG_SIMACCEL simaccel.c) diff --git a/drivers/sensor/simaccel/Kconfig b/drivers/sensor/simaccel/Kconfig new file mode 100644 index 0000000000000..75d7746fc6a5e --- /dev/null +++ b/drivers/sensor/simaccel/Kconfig @@ -0,0 +1,36 @@ +# Kconfig - SIMACCEL simulated 3-axis accelerometer sensor driver + +# +# Copyright (c) 2019 Kevin Townsend +# +# SPDX-License-Identifier: Apache-2.0 +# + +menuconfig SIMACCEL + bool "SIMACCEL simulated accelerometer sensor driver" + help + Enable simulated 3-axis accelerometer sensor driver. + +if SIMACCEL + +choice + prompt "Measurements per second" + default SIMACCEL_MPS_1 + help + Number of measurements per second. + +config SIMACCEL_MPS_1 + bool "1" + +config SIMACCEL_MPS_10 + bool "10" + +config SIMACCEL_MPS_100 + bool "100" + +config SIMACCEL_MPS_1000 + bool "1000" + +endchoice + +endif # SIMACCEL diff --git a/drivers/sensor/simaccel/simaccel.c b/drivers/sensor/simaccel/simaccel.c new file mode 100644 index 0000000000000..29f712c613aa9 --- /dev/null +++ b/drivers/sensor/simaccel/simaccel.c @@ -0,0 +1,81 @@ +/* simaccel.c - Simulated driver for a 3-axis accelerometer. */ + +/* + * Copyright (c) 2019 Kevin Townsend. + * + * SPDX-License-Identifier: Apache-2.0 + */ + +#include +#include +#include +#include +#include + +#include "simaccel.h" + +/** + * @brief Retrieves a set of data samples from the sensor. + * + * @param dev Pointer to the `device` used when executing the read attempt. + * @param chan The `sensor_channel` to request data from. This should + * normally be `SENSOR_CHAN_ALL`. + * + * @return 0 on success, otherwise an appropriate error code. + */ +static int simaccel_sample_fetch(struct device *dev, enum sensor_channel chan) +{ + struct simaccel_data *data = dev->driver_data; + u8_t buf[6] = { 0 }; + int ret; + + __ASSERT_NO_MSG(chan == SENSOR_CHAN_ALL); + + /* TODO: Generate sensible random data or use a known lookup table. */ + data->range = 2; + data->xdata = (s16_t)buf[1] << 8 | buf[0]; + data->ydata = (s16_t)buf[3] << 8 | buf[2]; + data->zdata = (s16_t)buf[5] << 8 | buf[4]; + + return 0; +} + +static int simaccel_channel_get(struct device *dev, + enum sensor_channel chan, + struct sensor_value *val) +{ + struct simaccel_data *data = dev->driver_data; + + switch (chan) { + default: + return -EINVAL; + } + + return 0; +} + +static const struct sensor_driver_api simaccel_api_funcs = { + .sample_fetch = simaccel_sample_fetch, + .channel_get = simaccel_channel_get, +}; + +/** + * Attempts to initialise a simaccel device. + * + * @param dev The 'device' to bind the driver instance to. + * + * @return 0 on success, otherwise an appropriate error code. + */ +int simaccel_init(struct device *dev) +{ + struct simaccel_data *data = dev->driver_data; + + return 0; +} + +static struct simaccel_data simaccel_data; + +DEVICE_AND_API_INIT(simaccel, "simaccel", + simaccel_init, &simaccel_data, + NULL, POST_KERNEL, CONFIG_SENSOR_INIT_PRIORITY, + &simaccel_api_funcs); diff --git a/drivers/sensor/simaccel/simaccel.h b/drivers/sensor/simaccel/simaccel.h new file mode 100644 index 0000000000000..75e4682f1b480 --- /dev/null +++ b/drivers/sensor/simaccel/simaccel.h @@ -0,0 +1,24 @@ +/* + * Copyright (c) 2019 Kevin Townsend + * + * SPDX-License-Identifier: Apache-2.0 + */ + +#ifndef ZEPHYR_DRIVERS_SENSOR_SIMACCEL_SIMACCEL_H_ +#define ZEPHYR_DRIVERS_SENSOR_SIMACCEL_SIMACCEL_H_ + +#include +#include + +struct simaccel_data { + /** Accelerometer measurement (+/-G). Valid values are 2, 4, 8 or 16. */ + u8_t range; + /** Raw x-axis data. */ + s16_t xdata; + /** Raw y-axis data. */ + s16_t ydata; + /** Raw z-axis data. */ + s16_t zdata; +}; + +#endif /* ZEPHYR_DRIVERS_SENSOR_SIMACCEL_SIMACCEL_H_ */