diff --git a/samples/boards/sensortile_box/CMakeLists.txt b/samples/boards/sensortile_box/CMakeLists.txt new file mode 100644 index 00000000000..41bccd9a5e8 --- /dev/null +++ b/samples/boards/sensortile_box/CMakeLists.txt @@ -0,0 +1,10 @@ +# Copyright (c) 2019 STMicroelectronics +# +# SPDX-License-Identifier: Apache-2.0 +# +cmake_minimum_required(VERSION 3.13.1) + +include($ENV{ZEPHYR_BASE}/cmake/app/boilerplate.cmake NO_POLICY_SCOPE) +project(sensortile_box) + +target_sources(app PRIVATE src/main.c) diff --git a/samples/boards/sensortile_box/README.rst b/samples/boards/sensortile_box/README.rst new file mode 100644 index 00000000000..3692a0cd3ff --- /dev/null +++ b/samples/boards/sensortile_box/README.rst @@ -0,0 +1,83 @@ +.. _sensortile_box sensors: + +ST SensorTile.box +################# + +Overview +******** +This sample provides an example of how to read sensors data +from the SensorTile.box board. + +This sample enables all sensors of SensorTile.box board, and then +periodically reads and displays data on the console from the following +sensors: + +- HTS221: ambient temperature and relative humidity +- LPS22HH: ambient temperature and atmospheric pressure +- LIS2DW12: 3-Axis acceleration +- LSM6DSOX: 6-Axis acceleration and angular velocity +- STTS751: temperature sensor + +Requirements +************ + +The application requires a SensorTile.box board connected to the PC +through USB. The board declares itself as a USB CDC class device. + +References +********** + +- :ref:`sensortile_box` + +Building and Running +******************** + +Build and flash the sample in the following way: + +.. zephyr-app-commands:: + :zephyr-app: samples/boards/sensortile_box + :board: sensortile_box + :goals: build flash + +Please note that flashing the board requires a few preliminary steps described +in :ref:`sensortile_box`. + +Then, power cycle the board by disconnecting and reconnecting the USB cable. +Run your favorite terminal program to listen for output. + +.. code-block:: console + + $ minicom -D -b 115200 + +Replace :code:`` with the port where the SensorTile.box board +can be found. For example, under Linux, :code:`/dev/ttyUSB0`. +The ``-b`` option sets baud rate ignoring the value from config. + +Sample Output +============= + +The sample code outputs sensors data on the SensorTile.box console. + + .. code-block:: console + + SensorTile.box dashboard + + HTS221: Temperature: 26.4 C + HTS221: Relative Humidity: 60.5% + LPS22HH: Temperature: 28.4 C + LPS22HH: Pressure:99.694 kpa + LIS2DW12: Accel (m.s-2): x: 0.306, y: -0.459, z: 10.031 + IIS3DHHC: Accel (m.s-2): x: -0.581, y: 0.880, z: -9.933 + LSM6DSOX: Accel (m.s-2): x: -0.158, y: 0.158, z: 9.811 + LSM6DSOX: GYro (dps): x: 0.003, y: 0.000, z: -0.005 + STTS751: Temperature: 27.0 C + 1:: lps22hh trig 206 + 1:: lis2dw12 trig 410 + 1:: lsm6dsox acc trig 836 + 1:: lsm6dsox gyr trig 836 + 1:: iis3dhhc trig 2422 + + + +If you move the board around or put your finger on the temperature +sensor, you will see the accelerometer, gyro, and temperature values change. diff --git a/samples/boards/sensortile_box/prj.conf b/samples/boards/sensortile_box/prj.conf new file mode 100644 index 00000000000..45167e925ec --- /dev/null +++ b/samples/boards/sensortile_box/prj.conf @@ -0,0 +1,36 @@ +CONFIG_LOG=y +CONFIG_PRINTK=y +CONFIG_SPI=y +CONFIG_I2C=y +CONFIG_GPIO=y + +# config sensors +CONFIG_SENSOR=y +CONFIG_SENSOR_LOG_LEVEL_DBG=y +CONFIG_HTS221=y +CONFIG_HTS221_TRIGGER_NONE=y +CONFIG_LPS22HH=y +CONFIG_LPS22HH_TRIGGER_OWN_THREAD=y +CONFIG_LIS2DW12=y +CONFIG_LIS2DW12_TRIGGER_OWN_THREAD=y +CONFIG_LSM6DSO=y +CONFIG_LSM6DSO_TRIGGER_OWN_THREAD=y +CONFIG_LSM6DSO_ENABLE_TEMP=n +CONFIG_LSM6DSO_INT_PIN_1=y +CONFIG_STTS751=y +CONFIG_STTS751_TRIGGER_NONE=y +CONFIG_IIS3DHHC=y +CONFIG_IIS3DHHC_TRIGGER_OWN_THREAD=y + +# config USB and USB console +CONFIG_USB=y +CONFIG_USB_DEVICE_VID=0x0483 +CONFIG_USB_DEVICE_PID=0x1234 +CONFIG_USB_DEVICE_STACK=y +CONFIG_USB_DEVICE_PRODUCT="Zephyr CDC SensorTile.box" + +CONFIG_USB_UART_CONSOLE=y +CONFIG_UART_INTERRUPT_DRIVEN=y +CONFIG_UART_LINE_CTRL=y +CONFIG_UART_CONSOLE_ON_DEV_NAME="CDC_ACM_0" +CONFIG_USB_UART_DTR_WAIT=y diff --git a/samples/boards/sensortile_box/sample.yaml b/samples/boards/sensortile_box/sample.yaml new file mode 100644 index 00000000000..01f4cbf165e --- /dev/null +++ b/samples/boards/sensortile_box/sample.yaml @@ -0,0 +1,9 @@ +sample: + description: SensorTile.box board testing + name: SensorTile.box test +tests: + sample.board.sensortile_box.sensors: + harness: sensor + platform_whitelist: sensortile_box + tags: sensors + depends_on: i2c spi gpio diff --git a/samples/boards/sensortile_box/src/main.c b/samples/boards/sensortile_box/src/main.c new file mode 100644 index 00000000000..d9525099040 --- /dev/null +++ b/samples/boards/sensortile_box/src/main.c @@ -0,0 +1,448 @@ +/* + * Copyright (c) 2018 STMicroelectronics + * + * SPDX-License-Identifier: Apache-2.0 + */ + +#include +#include + +#include +#include +#include +#include +#include + +#include + +#define WHOAMI_REG 0x0F +#define WHOAMI_ALT_REG 0x4F + +#ifdef CONFIG_LPS22HH_TRIGGER +static int lps22hh_trig_cnt; + +static void lps22hh_trigger_handler(struct device *dev, + struct sensor_trigger *trig) +{ + sensor_sample_fetch_chan(dev, SENSOR_CHAN_PRESS); + lps22hh_trig_cnt++; +} +#endif + +#ifdef CONFIG_LIS2DW12_TRIGGER +static int lis2dw12_trig_cnt; + +static void lis2dw12_trigger_handler(struct device *dev, + struct sensor_trigger *trig) +{ + sensor_sample_fetch_chan(dev, SENSOR_CHAN_ACCEL_XYZ); + lis2dw12_trig_cnt++; +} +#endif + +#ifdef CONFIG_LSM6DSO_TRIGGER +static int lsm6dso_acc_trig_cnt; +static int lsm6dso_gyr_trig_cnt; +static int lsm6dso_temp_trig_cnt; + +static void lsm6dso_acc_trig_handler(struct device *dev, + struct sensor_trigger *trig) +{ + sensor_sample_fetch_chan(dev, SENSOR_CHAN_ACCEL_XYZ); + lsm6dso_acc_trig_cnt++; +} + +static void lsm6dso_gyr_trig_handler(struct device *dev, + struct sensor_trigger *trig) +{ + sensor_sample_fetch_chan(dev, SENSOR_CHAN_GYRO_XYZ); + lsm6dso_gyr_trig_cnt++; +} + +static void lsm6dso_temp_trig_handler(struct device *dev, + struct sensor_trigger *trig) +{ + sensor_sample_fetch_chan(dev, SENSOR_CHAN_DIE_TEMP); + lsm6dso_temp_trig_cnt++; +} +#endif + +#ifdef CONFIG_STTS751_TRIGGER +static int stts751_trig_cnt; + +static void stts751_trigger_handler(struct device *dev, + struct sensor_trigger *trig) +{ + stts751_trig_cnt++; +} +#endif + +#ifdef CONFIG_IIS3DHHC_TRIGGER +static int iis3dhhc_trig_cnt; + +static void iis3dhhc_trigger_handler(struct device *dev, + struct sensor_trigger *trig) +{ + sensor_sample_fetch_chan(dev, SENSOR_CHAN_ACCEL_XYZ); + iis3dhhc_trig_cnt++; +} +#endif + +static void lps22hh_config(struct device *lps22hh) +{ + struct sensor_value odr_attr; + + /* set LPS22HH sampling frequency to 50 Hz */ + odr_attr.val1 = 50; + odr_attr.val2 = 0; + + if (sensor_attr_set(lps22hh, SENSOR_CHAN_ALL, + SENSOR_ATTR_SAMPLING_FREQUENCY, &odr_attr) < 0) { + printk("Cannot set sampling frequency for LPS22HH\n"); + return; + } + +#ifdef CONFIG_LPS22HH_TRIGGER + struct sensor_trigger trig; + + trig.type = SENSOR_TRIG_DATA_READY; + trig.chan = SENSOR_CHAN_ALL; + sensor_trigger_set(lps22hh, &trig, lps22hh_trigger_handler); +#endif +} + +static void lis2dw12_config(struct device *lis2dw12) +{ + struct sensor_value odr_attr, fs_attr; + + /* set LIS2DW12 accel/gyro sampling frequency to 100 Hz */ + odr_attr.val1 = 100; + odr_attr.val2 = 0; + + if (sensor_attr_set(lis2dw12, SENSOR_CHAN_ACCEL_XYZ, + SENSOR_ATTR_SAMPLING_FREQUENCY, &odr_attr) < 0) { + printk("Cannot set sampling frequency for LIS2DW12 accel\n"); + return; + } + + sensor_g_to_ms2(16, &fs_attr); + + if (sensor_attr_set(lis2dw12, SENSOR_CHAN_ACCEL_XYZ, + SENSOR_ATTR_FULL_SCALE, &fs_attr) < 0) { + printk("Cannot set sampling frequency for LIS2DW12 gyro\n"); + return; + } + +#ifdef CONFIG_LIS2DW12_TRIGGER + struct sensor_trigger trig; + + trig.type = SENSOR_TRIG_DATA_READY; + trig.chan = SENSOR_CHAN_ACCEL_XYZ; + sensor_trigger_set(lis2dw12, &trig, lis2dw12_trigger_handler); +#endif +} + +static void lsm6dso_config(struct device *lsm6dso) +{ + struct sensor_value odr_attr, fs_attr; + + /* set LSM6DSO accel sampling frequency to 208 Hz */ + odr_attr.val1 = 208; + odr_attr.val2 = 0; + + if (sensor_attr_set(lsm6dso, SENSOR_CHAN_ACCEL_XYZ, + SENSOR_ATTR_SAMPLING_FREQUENCY, &odr_attr) < 0) { + printk("Cannot set sampling frequency for LSM6DSO accel\n"); + return; + } + + sensor_g_to_ms2(16, &fs_attr); + + if (sensor_attr_set(lsm6dso, SENSOR_CHAN_ACCEL_XYZ, + SENSOR_ATTR_FULL_SCALE, &fs_attr) < 0) { + printk("Cannot set fs for LSM6DSO accel\n"); + return; + } + + /* set LSM6DSO gyro sampling frequency to 208 Hz */ + odr_attr.val1 = 208; + odr_attr.val2 = 0; + + if (sensor_attr_set(lsm6dso, SENSOR_CHAN_GYRO_XYZ, + SENSOR_ATTR_SAMPLING_FREQUENCY, &odr_attr) < 0) { + printk("Cannot set sampling frequency for LSM6DSO gyro\n"); + return; + } + + sensor_degrees_to_rad(250, &fs_attr); + + if (sensor_attr_set(lsm6dso, SENSOR_CHAN_GYRO_XYZ, + SENSOR_ATTR_FULL_SCALE, &fs_attr) < 0) { + printk("Cannot set fs for LSM6DSO gyro\n"); + return; + } + +#ifdef CONFIG_LSM6DSO_TRIGGER + struct sensor_trigger trig; + + trig.type = SENSOR_TRIG_DATA_READY; + trig.chan = SENSOR_CHAN_ACCEL_XYZ; + sensor_trigger_set(lsm6dso, &trig, lsm6dso_acc_trig_handler); + + trig.type = SENSOR_TRIG_DATA_READY; + trig.chan = SENSOR_CHAN_GYRO_XYZ; + sensor_trigger_set(lsm6dso, &trig, lsm6dso_gyr_trig_handler); + + trig.type = SENSOR_TRIG_DATA_READY; + trig.chan = SENSOR_CHAN_DIE_TEMP; + sensor_trigger_set(lsm6dso, &trig, lsm6dso_temp_trig_handler); +#endif +} + +static void stts751_config(struct device *stts751) +{ + struct sensor_value odr_attr; + + /* set STTS751 conversion rate to 16 Hz */ + odr_attr.val1 = 16; + odr_attr.val2 = 0; + + if (sensor_attr_set(stts751, SENSOR_CHAN_ALL, + SENSOR_ATTR_SAMPLING_FREQUENCY, &odr_attr) < 0) { + printk("Cannot set sampling frequency for STTS751\n"); + return; + } + +#ifdef CONFIG_STTS751_TRIGGER + struct sensor_trigger trig; + + trig.type = SENSOR_TRIG_THRESHOLD; + trig.chan = SENSOR_CHAN_ALL; + sensor_trigger_set(stts751, &trig, stts751_trigger_handler); +#endif +} + +static void iis3dhhc_config(struct device *iis3dhhc) +{ + struct sensor_value odr_attr; + + /* enable IIS3DHHC conversion */ + odr_attr.val1 = 1000; /* enable sensor at 1KHz */ + odr_attr.val2 = 0; + + if (sensor_attr_set(iis3dhhc, SENSOR_CHAN_ALL, + SENSOR_ATTR_SAMPLING_FREQUENCY, &odr_attr) < 0) { + printk("Cannot set sampling frequency for IIS3DHHC\n"); + return; + } + +#ifdef CONFIG_IIS3DHHC_TRIGGER + struct sensor_trigger trig; + + trig.type = SENSOR_TRIG_DATA_READY; + trig.chan = SENSOR_CHAN_ACCEL_XYZ; + sensor_trigger_set(iis3dhhc, &trig, iis3dhhc_trigger_handler); +#endif +} + +void main(void) +{ + static struct device *led0, *led1; + int i, on = 1; + int cnt = 1; + + led0 = device_get_binding(DT_ALIAS_LED0_GPIOS_CONTROLLER); + gpio_pin_configure(led0, DT_ALIAS_LED0_GPIOS_PIN, GPIO_DIR_OUT); + + led1 = device_get_binding(DT_ALIAS_LED1_GPIOS_CONTROLLER); + gpio_pin_configure(led1, DT_ALIAS_LED1_GPIOS_PIN, GPIO_DIR_OUT); + + for (i = 0; i < 6; i++) { + gpio_pin_write(led0, DT_ALIAS_LED0_GPIOS_PIN, on); + gpio_pin_write(led1, DT_ALIAS_LED1_GPIOS_PIN, !on); + k_sleep(100); + on = (on == 1) ? 0 : 1; + } + + gpio_pin_write(led0, DT_ALIAS_LED0_GPIOS_PIN, 0); + gpio_pin_write(led1, DT_ALIAS_LED1_GPIOS_PIN, 1); + + printk("SensorTile.box test!!\n"); + + struct device *hts221 = device_get_binding(DT_INST_0_ST_HTS221_LABEL); + struct device *lis2dw12 = device_get_binding(DT_INST_0_ST_LIS2DW12_LABEL); + struct device *lps22hh = device_get_binding(DT_INST_0_ST_LPS22HH_LABEL); + struct device *lsm6dso = device_get_binding(DT_INST_0_ST_LSM6DSO_LABEL); + struct device *stts751 = device_get_binding(DT_INST_0_ST_STTS751_LABEL); + struct device *iis3dhhc = device_get_binding(DT_INST_0_ST_IIS3DHHC_LABEL); + + if (!hts221) { + printk("Could not get pointer to %s sensor\n", + DT_INST_0_ST_HTS221_LABEL); + return; + } + + if (!lis2dw12) { + printf("Could not get LIS2DW12 device\n"); + return; + } + + if (lps22hh == NULL) { + printf("Could not get LPS22HH device\n"); + return; + } + + if (lsm6dso == NULL) { + printf("Could not get LSM6DSO device\n"); + return; + } + + if (stts751 == NULL) { + printf("Could not get STTS751 device\n"); + return; + } + + if (iis3dhhc == NULL) { + printf("Could not get IIS3DHHC device\n"); + return; + } + + lis2dw12_config(lis2dw12); + lps22hh_config(lps22hh); + lsm6dso_config(lsm6dso); + stts751_config(stts751); + iis3dhhc_config(iis3dhhc); + + while (1) { + struct sensor_value hts221_hum, hts221_temp; + struct sensor_value lps22hh_press, lps22hh_temp; + struct sensor_value lis2dw12_accel[3]; + struct sensor_value iis3dhhc_accel[3]; + struct sensor_value lsm6dso_accel[3], lsm6dso_gyro[3]; + struct sensor_value stts751_temp; + + /* handle HTS221 sensor */ + if (sensor_sample_fetch(hts221) < 0) { + printf("HTS221 Sensor sample update error\n"); + return; + } + +#ifndef CONFIG_LIS2DW12_TRIGGER + /* handle LIS2DW12 sensor */ + if (sensor_sample_fetch(lis2dw12) < 0) { + printf("LIS2DW12 Sensor sample update error\n"); + return; + } +#endif + +#ifndef CONFIG_LSM6DSO_TRIGGER + if (sensor_sample_fetch(lsm6dso) < 0) { + printf("LSM6DSO Sensor sample update error\n"); + return; + } +#endif + +#ifndef CONFIG_LPS22HH_TRIGGER + if (sensor_sample_fetch(lps22hh) < 0) { + printf("LPS22HH Sensor sample update error\n"); + return; + } +#endif + +#ifndef CONFIG_STTS751_TRIGGER + if (sensor_sample_fetch(stts751) < 0) { + printf("STTS751 Sensor sample update error\n"); + return; + } +#endif + +#ifndef CONFIG_IIS3DHHC_TRIGGER + if (sensor_sample_fetch(iis3dhhc) < 0) { + printf("IIS3DHHC Sensor sample update error\n"); + return; + } +#endif + + sensor_channel_get(hts221, SENSOR_CHAN_HUMIDITY, &hts221_hum); + sensor_channel_get(hts221, SENSOR_CHAN_AMBIENT_TEMP, &hts221_temp); + sensor_channel_get(lis2dw12, SENSOR_CHAN_ACCEL_XYZ, lis2dw12_accel); + sensor_channel_get(lps22hh, SENSOR_CHAN_AMBIENT_TEMP, &lps22hh_temp); + sensor_channel_get(lps22hh, SENSOR_CHAN_PRESS, &lps22hh_press); + sensor_channel_get(lsm6dso, SENSOR_CHAN_ACCEL_XYZ, lsm6dso_accel); + sensor_channel_get(lsm6dso, SENSOR_CHAN_GYRO_XYZ, lsm6dso_gyro); + sensor_channel_get(stts751, SENSOR_CHAN_AMBIENT_TEMP, &stts751_temp); + sensor_channel_get(iis3dhhc, SENSOR_CHAN_ACCEL_XYZ, iis3dhhc_accel); + + /* Display sensor data */ + + /* Erase previous */ + printf("\0033\014"); + + printf("SensorTile.box dashboard\n\n"); + + /* HTS221 temperature */ + printf("HTS221: Temperature: %.1f C\n", + sensor_value_to_double(&hts221_temp)); + + /* HTS221 humidity */ + printf("HTS221: Relative Humidity: %.1f%%\n", + sensor_value_to_double(&hts221_hum)); + + /* temperature */ + printf("LPS22HH: Temperature: %.1f C\n", + sensor_value_to_double(&lps22hh_temp)); + + /* pressure */ + printf("LPS22HH: Pressure:%.3f kpa\n", + sensor_value_to_double(&lps22hh_press)); + + printf("LIS2DW12: Accel (m.s-2): x: %.3f, y: %.3f, z: %.3f\n", + sensor_value_to_double(&lis2dw12_accel[0]), + sensor_value_to_double(&lis2dw12_accel[1]), + sensor_value_to_double(&lis2dw12_accel[2])); + + printf("IIS3DHHC: Accel (m.s-2): x: %.3f, y: %.3f, z: %.3f\n", + sensor_value_to_double(&iis3dhhc_accel[0]), + sensor_value_to_double(&iis3dhhc_accel[1]), + sensor_value_to_double(&iis3dhhc_accel[2])); + + printf("LSM6DSOX: Accel (m.s-2): x: %.3f, y: %.3f, z: %.3f\n", + sensor_value_to_double(&lsm6dso_accel[0]), + sensor_value_to_double(&lsm6dso_accel[1]), + sensor_value_to_double(&lsm6dso_accel[2])); + + printf("LSM6DSOX: GYro (dps): x: %.3f, y: %.3f, z: %.3f\n", + sensor_value_to_double(&lsm6dso_gyro[0]), + sensor_value_to_double(&lsm6dso_gyro[1]), + sensor_value_to_double(&lsm6dso_gyro[2])); + + /* temperature */ + printf("STTS751: Temperature: %.1f C\n", + sensor_value_to_double(&stts751_temp)); + +#if defined(CONFIG_LPS22HH_TRIGGER) + printk("%d:: lps22hh trig %d\n", cnt, lps22hh_trig_cnt); +#endif + +#ifdef CONFIG_LIS2DW12_TRIGGER + printk("%d:: lis2dw12 trig %d\n", cnt, lis2dw12_trig_cnt); +#endif + +#ifdef CONFIG_LSM6DSO_TRIGGER + printk("%d:: lsm6dsox acc trig %d\n", cnt, lsm6dso_acc_trig_cnt); + printk("%d:: lsm6dsox gyr trig %d\n", cnt, lsm6dso_gyr_trig_cnt); +#endif + +#if defined(CONFIG_STTS751_TRIGGER) + printk("%d:: stts751 trig %d\n", cnt, stts751_trig_cnt); +#endif + +#if defined(CONFIG_IIS3DHHC_TRIGGER) + printk("%d:: iis3dhhc trig %d\n", cnt, iis3dhhc_trig_cnt); +#endif + + k_sleep(2000); + } +} + +