SensorAPI: move i2cscan cmd to sensor shell - Moving from bno055 driver to the sensor shell because it really doesn't belong in the driver but is cmd that would be useful for multiple sensors
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/c3b41745 Tree: http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/tree/c3b41745 Diff: http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/diff/c3b41745 Branch: refs/heads/master Commit: c3b417456815272ff586ef644c52a0de6cea2589 Parents: bc1c683 Author: Vipul Rahane <[email protected]> Authored: Mon Mar 6 12:36:37 2017 -0800 Committer: Vipul Rahane <[email protected]> Committed: Mon Mar 6 12:39:47 2017 -0800 ---------------------------------------------------------------------- hw/drivers/sensors/bno055/src/bno055.c | 8 +++ hw/drivers/sensors/bno055/src/bno055_shell.c | 77 +++-------------------- hw/sensor/include/sensor/sensor.h | 4 ++ hw/sensor/src/sensor_shell.c | 67 ++++++++++++++++++++ 4 files changed, 87 insertions(+), 69 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/c3b41745/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 6773650..dbf0288 100644 --- a/hw/drivers/sensors/bno055/src/bno055.c +++ b/hw/drivers/sensors/bno055/src/bno055.c @@ -806,6 +806,14 @@ bno055_config(struct bno055 *bno055, struct bno055_cfg *cfg) bno055->cfg.bc_opr_mode = cfg->bc_opr_mode; + rc = bno055_acc_cfg(cfg); + if (rc) { + goto err; + } + + bno055->cfg.bc_acc_range = cfg->bc_acc_range; + bno055->cfg.bc_acc_bw = cfg->bc_acc_bw; + return 0; err: return rc; http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/c3b41745/hw/drivers/sensors/bno055/src/bno055_shell.c ---------------------------------------------------------------------- diff --git a/hw/drivers/sensors/bno055/src/bno055_shell.c b/hw/drivers/sensors/bno055/src/bno055_shell.c index 6404245..dd9ee08 100644 --- a/hw/drivers/sensors/bno055/src/bno055_shell.c +++ b/hw/drivers/sensors/bno055/src/bno055_shell.c @@ -42,23 +42,6 @@ static struct shell_cmd bno055_shell_cmd_struct = { }; static int -bno055_shell_stol(char *param_val, long min, long max, long *output) -{ - char *endptr; - long lval; - - lval = strtol(param_val, &endptr, 10); /* Base 10 */ - if (param_val != '\0' && *endptr == '\0' && - lval >= min && lval <= max) { - *output = lval; - } else { - return EINVAL; - } - - return 0; -} - -static int bno055_shell_err_too_many_args(char *cmd_name) { console_printf("Error: too many arguments for command \"%s\"\n", @@ -140,7 +123,7 @@ bno055_shell_cmd_sensor_offsets(int argc, char **argv) tok = strtok(argv[2], ":"); i = 0; do { - if (bno055_shell_stol(tok, 0, UINT16_MAX, &val)) { + if (sensor_shell_stol(tok, 0, UINT16_MAX, &val)) { return bno055_shell_err_invalid_arg(argv[2]); } offsetdata[i] = val; @@ -238,16 +221,17 @@ bno055_shell_cmd_read(int argc, char **argv) /* Since this is the biggest struct, malloc space for it */ databuf = malloc(sizeof(*sqd)); + assert(databuf != NULL); /* Check if more than one sample requested */ if (argc == 4) { - if (bno055_shell_stol(argv[2], 1, UINT16_MAX, &val)) { + if (sensor_shell_stol(argv[2], 1, UINT16_MAX, &val)) { return bno055_shell_err_invalid_arg(argv[2]); } samples = (uint16_t)val; - if (bno055_shell_stol(argv[3], 0, UINT16_MAX, &val)) { + if (sensor_shell_stol(argv[3], 0, UINT16_MAX, &val)) { return bno055_shell_err_invalid_arg(argv[2]); } type = (int)(1 << val); @@ -331,7 +315,7 @@ bno055_shell_cmd_opr_mode(int argc, char **argv) /* Update the mode */ if (argc == 3) { - if (bno055_shell_stol(argv[2], 0, 16, &val)) { + if (sensor_shell_stol(argv[2], 0, BNO055_OPR_MODE_NDOF, &val)) { return bno055_shell_err_invalid_arg(argv[2]); } /* Make sure mode is valid */ @@ -371,7 +355,7 @@ bno055_shell_cmd_pwr_mode(int argc, char **argv) /* Update the mode */ if (argc == 3) { - if (bno055_shell_stol(argv[2], 0, 16, &val)) { + if (sensor_shell_stol(argv[2], 0, BNO055_PWR_MODE_SUSPEND, &val)) { return bno055_shell_err_invalid_arg(argv[2]); } /* Make sure mode is valid */ @@ -419,7 +403,7 @@ bno055_shell_units_cmd(int argc, char **argv) /* Update the units */ if (argc == 3) { - if (bno055_shell_stol(argv[2], 0, UINT8_MAX, &val)) { + if (sensor_shell_stol(argv[2], 0, UINT8_MAX, &val)) { return bno055_shell_err_invalid_arg(argv[2]); } @@ -441,7 +425,7 @@ bno055_shell_cmd_dumpreg(int argc, char **argv) uint8_t val; int rc; - if (bno055_shell_stol(argv[2], 0, UINT8_MAX, &addr)) { + if (sensor_shell_stol(argv[2], 0, UINT8_MAX, &addr)) { return bno055_shell_err_invalid_arg(argv[2]); } rc = bno055_read8((uint8_t)addr, &val); @@ -456,47 +440,6 @@ err: } static int -shell_i2cscan_cmd(int argc, char **argv) -{ - uint8_t addr; - int32_t timeout; - uint8_t dev_count = 0; - long i2cnum; - int rc; - - timeout = OS_TICKS_PER_SEC / 10; - - if (bno055_shell_stol(argv[2], 0, 0xf, &i2cnum)) { - return bno055_shell_err_invalid_arg(argv[2]); - } - - console_printf("Scanning I2C bus %u\n" - " 0 1 2 3 4 5 6 7 8 9 a b c d e f\n" - "00: ", (uint8_t)i2cnum); - - - /* Scan all valid I2C addresses (0x08..0x77) */ - for (addr = 0x08; addr < 0x78; addr++) { - rc = hal_i2c_master_probe((uint8_t)i2cnum, addr, timeout); - /* Print addr header every 16 bytes */ - if (!(addr % 16)) { - console_printf("\n%02x: ", addr); - } - /* Display the addr if a response was received */ - if (!rc) { - console_printf("%02x ", addr); - dev_count++; - } else { - console_printf("-- "); - } - os_time_delay(OS_TICKS_PER_SEC/1000 * 20); - } - console_printf("\nFound %u devices on I2C bus %u\n", dev_count, (uint8_t)i2cnum); - - return 0; -} - -static int bno055_shell_cmd_reset(int argc, char **argv) { int rc; @@ -552,10 +495,6 @@ bno055_shell_cmd(int argc, char **argv) return bno055_shell_cmd_dumpreg(argc, argv); } - if (argc > 1 && strcmp(argv[1], "i2cscan") == 0) { - return shell_i2cscan_cmd(argc, argv); - } - if (argc > 1 && strcmp(argv[1], "units") == 0) { return bno055_shell_units_cmd(argc, argv); } http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/c3b41745/hw/sensor/include/sensor/sensor.h ---------------------------------------------------------------------- diff --git a/hw/sensor/include/sensor/sensor.h b/hw/sensor/include/sensor/sensor.h index 769a148..813634b 100644 --- a/hw/sensor/include/sensor/sensor.h +++ b/hw/sensor/include/sensor/sensor.h @@ -325,8 +325,12 @@ struct sensor *sensor_mgr_find_next(sensor_mgr_compare_func_t, void *, struct sensor *sensor_mgr_find_next_bytype(sensor_type_t, struct sensor *); struct sensor *sensor_mgr_find_next_bydevname(char *, struct sensor *); +#if MYNEWT_VAL(SENSOR_CLI) char* sensor_ftostr(float num, char *fltstr, int len); +int +sensor_shell_stol(char *param_val, long min, long max, long *output); +#endif /** * }@ http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/c3b41745/hw/sensor/src/sensor_shell.c ---------------------------------------------------------------------- diff --git a/hw/sensor/src/sensor_shell.c b/hw/sensor/src/sensor_shell.c index 545653f..8a9b969 100644 --- a/hw/sensor/src/sensor_shell.c +++ b/hw/sensor/src/sensor_shell.c @@ -38,6 +38,7 @@ #include "sensor/euler.h" #include "console/console.h" #include "shell/shell.h" +#include "hal/hal_i2c.h" static int sensor_cmd_exec(int, char **); static struct shell_cmd shell_sensor_cmd = { @@ -239,6 +240,67 @@ err: return; } +int +sensor_shell_stol(char *param_val, long min, long max, long *output) +{ + char *endptr; + long lval; + + lval = strtol(param_val, &endptr, 10); /* Base 10 */ + if (param_val != '\0' && *endptr == '\0' && + lval >= min && lval <= max) { + *output = lval; + } else { + return EINVAL; + } + + return 0; +} + +static int +sensor_cmd_i2cscan(int argc, char **argv) +{ + uint8_t addr; + int32_t timeout; + uint8_t dev_count = 0; + long i2cnum; + int rc; + + timeout = OS_TICKS_PER_SEC / 10; + + if (sensor_shell_stol(argv[2], 0, 0xf, &i2cnum)) { + console_printf("Invalid i2c interface:%s\n", argv[2]); + rc = SYS_EINVAL; + goto err; + } + + console_printf("Scanning I2C bus %u\n" + " 0 1 2 3 4 5 6 7 8 9 a b c d e f\n" + "00: ", (uint8_t)i2cnum); + + + /* Scan all valid I2C addresses (0x08..0x77) */ + for (addr = 0x08; addr < 0x78; addr++) { + rc = hal_i2c_master_probe((uint8_t)i2cnum, addr, timeout); + /* Print addr header every 16 bytes */ + if (!(addr % 16)) { + console_printf("\n%02x: ", addr); + } + /* Display the addr if a response was received */ + if (!rc) { + console_printf("%02x ", addr); + dev_count++; + } else { + console_printf("-- "); + } + os_time_delay(OS_TICKS_PER_SEC/1000 * 20); + } + console_printf("\nFound %u devices on I2C bus %u\n", dev_count, (uint8_t)i2cnum); + + return 0; +err: + return rc; +} static int sensor_cmd_exec(int argc, char **argv) @@ -264,6 +326,11 @@ sensor_cmd_exec(int argc, char **argv) goto err; } sensor_cmd_read(argv[2], (sensor_type_t) atoi(argv[3]), atoi(argv[4])); + } else if (!strcmp(argv[1], "i2cscan")) { + rc = sensor_cmd_i2cscan(argc, argv); + if (rc) { + goto err; + } } else { console_printf("Unknown sensor command %s\n", subcmd); rc = SYS_EINVAL;
