MYNEWT-703 SensorAPI: Add cputime timestamps - and timeofday to SensorAPI samples - fix sensors test app sim issues - fix small typo bug with bno055
Project: http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/repo Commit: http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/commit/28531b95 Tree: http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/tree/28531b95 Diff: http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/diff/28531b95 Branch: refs/heads/master Commit: 28531b95944223fca4d23ce403153e33b3d1f055 Parents: d8b0316 Author: Vipul Rahane <vipulrah...@apache.org> Authored: Fri Mar 31 18:30:42 2017 -0700 Committer: Vipul Rahane <vipulrah...@apache.org> Committed: Fri Mar 31 18:33:43 2017 -0700 ---------------------------------------------------------------------- apps/sensors_test/src/main.c | 3 +- apps/sensors_test/syscfg.yml | 11 ++- hw/bsp/native/src/hal_bsp.c | 20 ++++ hw/drivers/sensors/bno055/src/bno055.c | 2 +- hw/drivers/sensors/sim/include/sim/sim_accel.h | 1 + hw/drivers/sensors/tsl2561/src/tsl2561.c | 1 + hw/sensor/include/sensor/sensor.h | 31 ++++-- hw/sensor/src/sensor.c | 104 ++++++++++++++++++++ hw/sensor/src/sensor_shell.c | 18 +++- 9 files changed, 170 insertions(+), 21 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/28531b95/apps/sensors_test/src/main.c ---------------------------------------------------------------------- diff --git a/apps/sensors_test/src/main.c b/apps/sensors_test/src/main.c index ac694be..42cdd10 100755 --- a/apps/sensors_test/src/main.c +++ b/apps/sensors_test/src/main.c @@ -30,6 +30,7 @@ #include <stats/stats.h> #include <config/config.h> #include <sensor/sensor.h> +#include <sim/sim_accel.h> #include <lsm303dlhc/lsm303dlhc.h> #include <tsl2561/tsl2561.h> #include <tcs34725/tcs34725.h> @@ -358,7 +359,7 @@ static int config_sensor(void) { struct os_dev *dev; - struct cfg; + struct sim_accel_cfg cfg; int rc; dev = (struct os_dev *) os_dev_open("simaccel0", OS_TIMEOUT_NEVER, NULL); http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/28531b95/apps/sensors_test/syscfg.yml ---------------------------------------------------------------------- diff --git a/apps/sensors_test/syscfg.yml b/apps/sensors_test/syscfg.yml index bdbe400..4e799f4 100644 --- a/apps/sensors_test/syscfg.yml +++ b/apps/sensors_test/syscfg.yml @@ -41,8 +41,9 @@ syscfg.vals: LOG_NEWTMGR: 1 CONFIG_NEWTMGR: 1 - TSL2561_CLI: 1 - BNO055_CLI: 1 + TSL2561_CLI: 0 + BNO055_CLI: 0 + TCS34725_CLI: 1 syscfg.defs: TSL2561_PRESENT: @@ -50,11 +51,11 @@ syscfg.defs: value : 0 LSM303DLHC_PRESENT: description: 'LSM303 is present' - value : 1 + value : 0 BNO055_PRESENT: description: 'BNO055 is present' value : 0 TCS34725_PRESENT: - description: 'BNO055 is present' - value : 0 + description: 'TCS34725 is present' + value : 1 http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/28531b95/hw/bsp/native/src/hal_bsp.c ---------------------------------------------------------------------- diff --git a/hw/bsp/native/src/hal_bsp.c b/hw/bsp/native/src/hal_bsp.c index b03d1fa..0396dee 100644 --- a/hw/bsp/native/src/hal_bsp.c +++ b/hw/bsp/native/src/hal_bsp.c @@ -30,6 +30,12 @@ #include "uart_hal/uart_hal.h" #include "mcu/native_bsp.h" #include "mcu/mcu_hal.h" +#include "syscfg/syscfg.h" + +#if MYNEWT_VAL(SIM_ACCEL_PRESENT) +#include "sim/sim_accel.h" +static struct sim_accel os_bsp_accel0; +#endif static struct uart_dev os_bsp_uart0; static struct uart_dev os_bsp_uart1; @@ -52,6 +58,14 @@ hal_bsp_power_state(int state) return (0); } +#if MYNEWT_VAL(SIM_ACCEL_PRESENT) +int +simaccel_init(struct os_dev *odev, void *arg) +{ + return 0; +} +#endif + void hal_bsp_init(void) { @@ -64,4 +78,10 @@ hal_bsp_init(void) rc = os_dev_create((struct os_dev *) &os_bsp_uart1, "uart1", OS_DEV_INIT_PRIMARY, 0, uart_hal_init, (void *) NULL); assert(rc == 0); + +#if MYNEWT_VAL(SIM_ACCEL_PRESENT) + rc = os_dev_create((struct os_dev *) &os_bsp_accel0, "simaccel0", + OS_DEV_INIT_PRIMARY, 0, simaccel_init, (void *) NULL); + assert(rc == 0); +#endif } http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/28531b95/hw/drivers/sensors/bno055/src/bno055.c ---------------------------------------------------------------------- diff --git a/hw/drivers/sensors/bno055/src/bno055.c b/hw/drivers/sensors/bno055/src/bno055.c index 0e34987..eac3600 100644 --- a/hw/drivers/sensors/bno055/src/bno055.c +++ b/hw/drivers/sensors/bno055/src/bno055.c @@ -977,7 +977,7 @@ bno055_get_vector_data(void *datastruct, int type) sad->sad_z_is_valid = 1; break; case SENSOR_TYPE_EULER: - sad = datastruct; + sed = datastruct; /* 1 degree = 16 LSB */ sed->sed_h = ((double)x)/euler_div; sed->sed_r = ((double)y)/euler_div; http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/28531b95/hw/drivers/sensors/sim/include/sim/sim_accel.h ---------------------------------------------------------------------- diff --git a/hw/drivers/sensors/sim/include/sim/sim_accel.h b/hw/drivers/sensors/sim/include/sim/sim_accel.h index 6f0f6a1..7308489 100644 --- a/hw/drivers/sensors/sim/include/sim/sim_accel.h +++ b/hw/drivers/sensors/sim/include/sim/sim_accel.h @@ -22,6 +22,7 @@ #include "os/os.h" #include "os/os_dev.h" +#include "sensor/sensor.h" #ifdef __cplusplus extern "C" { http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/28531b95/hw/drivers/sensors/tsl2561/src/tsl2561.c ---------------------------------------------------------------------- diff --git a/hw/drivers/sensors/tsl2561/src/tsl2561.c b/hw/drivers/sensors/tsl2561/src/tsl2561.c index 27cd96e..4bb5c4b 100644 --- a/hw/drivers/sensors/tsl2561/src/tsl2561.c +++ b/hw/drivers/sensors/tsl2561/src/tsl2561.c @@ -37,6 +37,7 @@ #include <assert.h> #include <stdio.h> #include <errno.h> +#include <string.h> #include "defs/error.h" #include "os/os.h" http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/28531b95/hw/sensor/include/sensor/sensor.h ---------------------------------------------------------------------- diff --git a/hw/sensor/include/sensor/sensor.h b/hw/sensor/include/sensor/sensor.h index 9dc7cc0..c3bc8fc 100644 --- a/hw/sensor/include/sensor/sensor.h +++ b/hw/sensor/include/sensor/sensor.h @@ -22,6 +22,7 @@ #include "os/os.h" #include "os/os_dev.h" +#include "syscfg/syscfg.h" #ifdef __cplusplus extern "C" { @@ -200,14 +201,20 @@ struct sensor_driver { sensor_get_config_func_t sd_get_config; }; +struct sensor_timestamp { + struct os_timeval st_ostv; + struct os_timezone st_ostz; + uint32_t st_cputime; +}; + /* * Return the OS device structure corresponding to this sensor */ #define SENSOR_GET_DEVICE(__s) ((__s)->s_dev) struct sensor { - /* The OS device this sensor inherits from, this is typically a sensor specific - * driver. + /* The OS device this sensor inherits from, this is typically a sensor + * specific driver. */ struct os_dev *s_dev; @@ -215,9 +222,9 @@ struct sensor { struct os_mutex s_lock; - /* A bit mask describing the types of sensor objects available from this sensor. - * If the bit corresponding to the sensor_type_t is set, then this sensor supports - * that variable. + /* A bit mask describing the types of sensor objects available from this + * sensor. If the bit corresponding to the sensor_type_t is set, then this + * sensor supports that variable. */ sensor_type_t s_types; /** @@ -225,15 +232,19 @@ struct sensor { */ uint32_t s_poll_rate; - /** - * The next time at which we want to poll data from this sensor - */ + /* The next time at which we want to poll data from this sensor */ os_time_t s_next_run; - /* Sensor driver specific functions, created by the device registering the sensor. + /* Sensor driver specific functions, created by the device registering the + * sensor. */ struct sensor_driver *s_funcs; - /* A list of listeners that are registered to receive data off of this sensor + + /* Sensor last reading timestamp */ + struct sensor_timestamp s_sts; + + /* A list of listeners that are registered to receive data off of this + * sensor */ SLIST_HEAD(, sensor_listener) s_listener_list; /* The next sensor in the global sensor list. */ http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/28531b95/hw/sensor/src/sensor.c ---------------------------------------------------------------------- diff --git a/hw/sensor/src/sensor.c b/hw/sensor/src/sensor.c index 89a19df..cad73a6 100644 --- a/hw/sensor/src/sensor.c +++ b/hw/sensor/src/sensor.c @@ -27,6 +27,8 @@ #include "sensor/sensor.h" #include "sensor_priv.h" +#include "os/os_time.h" +#include "os/os_cputime.h" struct { struct os_mutex mgr_lock; @@ -42,6 +44,9 @@ struct sensor_read_ctx { void *user_arg; }; +struct sensor_timestamp sensor_base_ts; +struct os_callout st_up_osco; + /** * Lock sensor manager to access the list of sensors */ @@ -228,6 +233,48 @@ done: } /** + * Event that wakes up timestamp update procedure, this updates the base + * os_timeval in the global structure along with the base cputime + * @param OS event + */ +static void +sensor_base_ts_update_event(struct os_event *ev) +{ + os_time_t ticks; + int rc; + struct os_timeval ostv; + struct os_timezone ostz; + + ticks = os_time_get(); + + rc = os_gettimeofday(&ostv, &ostz); + if (rc) { + /* There is nothing we can do here + * just reset the timer frequently if we + * fail to get time, till then we will keep using + * old timestamp values. + */ + ticks += OS_TICKS_PER_SEC * 600; + goto done; + } + + /* CPU time gets wrapped in 4295 seconds since it is uint32_t, + * hence the hardcoded value of 3600 seconds, We want to make + * sure that the cputime never gets wrapped more than once. + * os_timeval usecs value gets wrapped in 2147 secs since it is int32_t. + * Hence, we take 2146 secs so that we update before it gets wrapped. + */ + ticks += OS_TICKS_PER_SEC * 2146; + + sensor_base_ts.st_ostv = ostv; + sensor_base_ts.st_ostz = ostz; + sensor_base_ts.st_cputime = os_cputime_get32(); + +done: + os_callout_reset(&st_up_osco, ticks); +} + +/** * Get the current eventq, the system is misconfigured if there is still * no parent eventq. */ @@ -237,12 +284,23 @@ sensor_mgr_evq_get(void) return (sensor_mgr.mgr_eventq); } +void +sensor_mgr_evq_set(struct os_eventq *evq) +{ + os_eventq_designate(&sensor_mgr.mgr_eventq, evq, NULL); +} + static void sensor_mgr_init(void) { + struct os_timeval ostv; + struct os_timezone ostz; + memset(&sensor_mgr, 0, sizeof(sensor_mgr)); TAILQ_INIT(&sensor_mgr.mgr_sensor_list); + sensor_mgr_evq_set(os_eventq_dflt_get()); + /** * Initialize sensor polling callout and set it to fire on boot. */ @@ -250,6 +308,21 @@ sensor_mgr_init(void) sensor_mgr_wakeup_event, NULL); os_callout_reset(&sensor_mgr.mgr_wakeup_callout, 0); + /* Initialize sensor cputime update callout and set it to fire after an + * hour, CPU time gets wrapped in 4295 seconds, + * hence the hardcoded value of 3600 seconds, We make sure that the + * cputime never gets wrapped more than once. + */ + os_gettimeofday(&ostv, &ostz); + + sensor_base_ts.st_ostv = ostv; + sensor_base_ts.st_ostz = ostz; + sensor_base_ts.st_cputime = os_cputime_get32(); + + os_callout_init(&st_up_osco, sensor_mgr_evq_get(), + sensor_base_ts_update_event, NULL); + os_callout_reset(&st_up_osco, OS_TICKS_PER_SEC); + os_mutex_init(&sensor_mgr.mgr_lock); } @@ -535,6 +608,32 @@ sensor_read_data_func(struct sensor *sensor, void *arg, void *data) } } +static void +sensor_up_timestamp(struct sensor *sensor) +{ + uint32_t curr_ts_ticks; + uint32_t ts; + + curr_ts_ticks = os_cputime_get32(); + + ts = os_cputime_ticks_to_usecs(curr_ts_ticks - + sensor_base_ts.st_cputime); + + /* Updating cputime */ + sensor_base_ts.st_cputime = sensor->s_sts.st_cputime = curr_ts_ticks; + + /* Updating seconds */ + sensor_base_ts.st_ostv.tv_sec = sensor_base_ts.st_ostv.tv_sec + (ts + + sensor_base_ts.st_ostv.tv_usec)/1000000; + sensor->s_sts.st_ostv.tv_sec = sensor_base_ts.st_ostv.tv_sec; + + /* Updating Micro seconds */ + sensor_base_ts.st_ostv.tv_usec = + (sensor_base_ts.st_ostv.tv_usec + ts)%1000000; + sensor->s_sts.st_ostv.tv_usec = sensor_base_ts.st_ostv.tv_usec; + +} + /** * Read the data for sensor type "type," from the sensor, "sensor" and * return the result into the "value" parameter. @@ -562,8 +661,13 @@ sensor_read(struct sensor *sensor, sensor_type_t type, src.user_func = data_func; src.user_arg = arg; + sensor_up_timestamp(sensor); + rc = sensor->s_funcs->sd_read(sensor, type, sensor_read_data_func, &src, timeout); + if (rc) { + goto done; + } sensor_unlock(sensor); http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/28531b95/hw/sensor/src/sensor_shell.c ---------------------------------------------------------------------- diff --git a/hw/sensor/src/sensor_shell.c b/hw/sensor/src/sensor_shell.c index b1917ce..481e241 100644 --- a/hw/sensor/src/sensor_shell.c +++ b/hw/sensor/src/sensor_shell.c @@ -237,6 +237,11 @@ sensor_shell_read_listener(struct sensor *sensor, void *arg, void *data) ++ctx->num_entries; + console_printf("ts: [ secs: %ld usecs: %d cputime: %u ]\n", + (long int)sensor->s_sts.st_ostv.tv_sec, + (int)sensor->s_sts.st_ostv.tv_usec, + (unsigned int)sensor->s_sts.st_cputime); + if (ctx->type == SENSOR_TYPE_ACCELEROMETER || ctx->type == SENSOR_TYPE_LINEAR_ACCEL || ctx->type == SENSOR_TYPE_GRAVITY) { @@ -331,7 +336,7 @@ sensor_shell_read_listener(struct sensor *sensor, void *arg, void *data) console_printf("b = %u, ", scd->scd_b); } if (scd->scd_c_is_valid) { - console_printf("c = %u, ", scd->scd_c); + console_printf("c = %u, \n", scd->scd_c); } if (scd->scd_lux_is_valid) { console_printf("lux = %u, ", scd->scd_lux); @@ -566,6 +571,7 @@ sensor_cmd_i2cscan(int argc, char **argv) timeout = OS_TICKS_PER_SEC / 10; + rc = 0; if (sensor_shell_stol(argv[2], 0, 0xf, &i2cnum)) { console_printf("Invalid i2c interface:%s\n", argv[2]); rc = SYS_EINVAL; @@ -579,7 +585,11 @@ sensor_cmd_i2cscan(int argc, char **argv) /* Scan all valid I2C addresses (0x08..0x77) */ for (addr = 0x08; addr < 0x78; addr++) { +#ifndef ARCH_sim rc = hal_i2c_master_probe((uint8_t)i2cnum, addr, timeout); +#else + (void)timeout; +#endif /* Print addr header every 16 bytes */ if (!(addr % 16)) { console_printf("\n%02x: ", addr); @@ -629,15 +639,15 @@ sensor_cmd_exec(int argc, char **argv) i = 4; memset(&spd, 0, sizeof(struct sensor_poll_data)); - if (!strcmp(argv[i], "-n")) { + if (argv[i] && !strcmp(argv[i], "-n")) { spd.spd_nsamples = atoi(argv[++i]); i++; } - if (!strcmp(argv[i], "-i")) { + if (argv[i] && !strcmp(argv[i], "-i")) { spd.spd_poll_itvl = atoi(argv[++i]); i++; } - if (!strcmp(argv[i], "-d")) { + if (argv[i] && !strcmp(argv[i], "-d")) { spd.spd_poll_duration = atoi(argv[++i]); i++; }