MYNEWT-678 SensorAPI: Shell: - Add support for polling at a given interval for a specific duration(Use os_cputime instead of a dedicated HW hal_timer)
"sensor read <sensor_name> <type> [-n nsamples] [-i poll_itvl] [-d duration]" 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/68265625 Tree: http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/tree/68265625 Diff: http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/diff/68265625 Branch: refs/heads/nrf_cputime Commit: 68265625d5c27ebaddd5d4b1d64acbbb30d3db90 Parents: d23fab9 Author: Vipul Rahane <vipulrah...@apache.org> Authored: Tue Mar 21 11:37:54 2017 -0700 Committer: Vipul Rahane <vipulrah...@apache.org> Committed: Tue Mar 21 11:46:30 2017 -0700 ---------------------------------------------------------------------- hw/sensor/src/sensor_shell.c | 50 +++++++++++++-------------------------- hw/sensor/syscfg.yml | 8 ------- 2 files changed, 16 insertions(+), 42 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/68265625/hw/sensor/src/sensor_shell.c ---------------------------------------------------------------------- diff --git a/hw/sensor/src/sensor_shell.c b/hw/sensor/src/sensor_shell.c index 3b6dc43..8d96445 100644 --- a/hw/sensor/src/sensor_shell.c +++ b/hw/sensor/src/sensor_shell.c @@ -40,7 +40,7 @@ #include "console/console.h" #include "shell/shell.h" #include "hal/hal_i2c.h" -#include "hal/hal_timer.h" +#include "os/os_cputime.h" static int sensor_cmd_exec(int, char **); static struct shell_cmd shell_sensor_cmd = { @@ -51,7 +51,6 @@ static struct shell_cmd shell_sensor_cmd = { struct os_sem g_sensor_shell_sem; struct hal_timer g_sensor_shell_timer; uint32_t sensor_shell_timer_arg = 0xdeadc0de; -uint8_t g_timer_is_config; struct sensor_poll_data { int spd_nsamples; @@ -277,38 +276,22 @@ void sensor_shell_timer_cb(void *arg) { int timer_arg_val; - int rc; timer_arg_val = *(uint32_t *)arg; + os_cputime_timer_relative(&g_sensor_shell_timer, timer_arg_val); os_sem_release(&g_sensor_shell_sem); - rc = hal_timer_start(&g_sensor_shell_timer, timer_arg_val); - assert(rc == 0); } -/* HAL timer configuration */ +/* os cputime timer configuration and initialization */ static void sensor_shell_config_timer(struct sensor_poll_data *spd) { - int rc; - - if (!g_timer_is_config) { - rc = hal_timer_config(MYNEWT_VAL(SENSOR_SHELL_TIMER_NUM), - MYNEWT_VAL(SENSOR_SHELL_TIMER_FREQ)); - assert(rc == 0); - g_timer_is_config = 1; - } - - sensor_shell_timer_arg = - (spd->spd_poll_itvl * 1000000) / hal_timer_get_resolution(MYNEWT_VAL(SENSOR_SHELL_TIMER_NUM)); + sensor_shell_timer_arg = os_cputime_usecs_to_ticks(spd->spd_poll_itvl * 1000); - rc = hal_timer_set_cb(MYNEWT_VAL(SENSOR_SHELL_TIMER_NUM), - &g_sensor_shell_timer, - sensor_shell_timer_cb, + os_cputime_timer_init(&g_sensor_shell_timer, sensor_shell_timer_cb, &sensor_shell_timer_arg); - assert(rc == 0); - rc = hal_timer_start(&g_sensor_shell_timer, sensor_shell_timer_arg); - assert(rc == 0); + os_cputime_timer_relative(&g_sensor_shell_timer, sensor_shell_timer_arg); } /* Check for number of samples */ @@ -318,7 +301,7 @@ sensor_shell_chk_nsamples(struct sensor_poll_data *spd, { /* Condition for number of samples */ if (spd->spd_nsamples && ctx->num_entries >= spd->spd_nsamples) { - hal_timer_stop(&g_sensor_shell_timer); + os_cputime_timer_stop(&g_sensor_shell_timer); return 0; } @@ -338,7 +321,7 @@ sensor_shell_chk_escape_seq(void) rc = console_read(&ch, 1, &newline); /* ^C or q or Q gets it out of the sampling loop */ if (rc || (ch == 3 || ch == 'q' || ch == 'Q')) { - hal_timer_stop(&g_sensor_shell_timer); + os_cputime_timer_stop(&g_sensor_shell_timer); console_printf("Sensor polling stopped rc:%u\n", rc); return 0; } @@ -351,24 +334,23 @@ sensor_shell_chk_escape_seq(void) * os_time if interval is not specified and checking duration */ static int -sensor_shell_polling_done(struct sensor_poll_data *spd, - int64_t *start_ts, - int *duration) +sensor_shell_polling_done(struct sensor_poll_data *spd, int64_t *duration, + int64_t *start_ts) { if (spd->spd_poll_duration) { if (spd->spd_poll_itvl) { - *duration += spd->spd_poll_itvl; + *duration += spd->spd_poll_itvl * 1000; } else { if (!*start_ts) { - *start_ts = os_get_uptime_usec()/1000; + *start_ts = os_get_uptime_usec(); } else { - *duration = os_get_uptime_usec()/1000 - *start_ts; + *duration = os_get_uptime_usec() - *start_ts; } } - if (*duration >= spd->spd_poll_duration) { - hal_timer_stop(&g_sensor_shell_timer); + if (*duration >= spd->spd_poll_duration * 1000) { + os_cputime_timer_stop(&g_sensor_shell_timer); console_printf("Sensor polling done\n"); return 0; } @@ -384,7 +366,7 @@ sensor_cmd_read(char *name, sensor_type_t type, struct sensor_poll_data *spd) struct sensor_listener listener; struct sensor_shell_read_ctx ctx; int rc; - int duration; + int64_t duration; int64_t start_ts; /* Look up sensor by name */ http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/68265625/hw/sensor/syscfg.yml ---------------------------------------------------------------------- diff --git a/hw/sensor/syscfg.yml b/hw/sensor/syscfg.yml index a4b6ee9..879fbb2 100644 --- a/hw/sensor/syscfg.yml +++ b/hw/sensor/syscfg.yml @@ -26,11 +26,3 @@ syscfg.defs: SENSOR_CLI: description: 'Whether or not to enable the sensor shell support' value: 1 - - SENSOR_SHELL_TIMER_FREQ: - description: 'Frequency of sensor shell timer' - value: 100000 - - SENSOR_SHELL_TIMER_NUM: - description: 'Timer number to use for sensor shell, 1 by default.' - value: 1