int i2ccmd_bus(FAR struct i2ctool_s *i2ctool, int argc, char **argv) { FAR struct i2c_dev_s *dev; int i; i2ctool_printf(i2ctool, " BUS EXISTS?\n"); for (i = CONFIG_I2CTOOL_MINBUS; i <= CONFIG_I2CTOOL_MAXBUS; i++) { dev = up_i2cinitialize(i); if (dev) { i2ctool_printf(i2ctool, "Bus %d: YES\n", i); (void)up_i2cuninitialize(dev); } else { i2ctool_printf(i2ctool, "Bus %d: NO\n", i); } } return OK; }
static int i2ccmd_unrecognized(FAR struct i2ctool_s *i2ctool, int argc, char **argv) { i2ctool_printf(i2ctool, g_i2ccmdnotfound, argv[0]); return ERROR; }
static int i2ccmd_help(FAR struct i2ctool_s *i2ctool, int argc, char **argv) { const struct cmdmap_s *ptr; i2ctool_printf(i2ctool, "Usage: i2c <cmd> [arguments]\n"); i2ctool_printf(i2ctool, "Where <cmd> is one of:\n\n"); for (ptr = g_i2ccmds; ptr->cmd; ptr++) { if (ptr->usage) { i2ctool_printf(i2ctool, " %s: %s %s\n", ptr->desc, ptr->cmd, ptr->usage); } else { i2ctool_printf(i2ctool, " %s: %s\n", ptr->desc, ptr->cmd); } } i2ctool_printf(i2ctool, "\nWhere common \"sticky\" OPTIONS include:\n"); i2ctool_printf(i2ctool, " [-a addr] is the I2C device address (hex). " "Default: %02x Current: %02x\n", CONFIG_I2CTOOL_MINADDR, i2ctool->addr); i2ctool_printf(i2ctool, " [-b bus] is the I2C bus number (decimal). " "Default: %d Current: %d\n", CONFIG_I2CTOOL_MINBUS, i2ctool->bus); i2ctool_printf(i2ctool, " [-r regaddr] is the I2C device register address (hex). " "Default: 00 Current: %02x\n", i2ctool->regaddr); i2ctool_printf(i2ctool, " [-w width] is the data width (8 or 16 decimal). " "Default: 8 Current: %d\n", i2ctool->width); i2ctool_printf(i2ctool, " [-s|n], send/don't send start between command and data. " "Default: -n Current: %s\n", i2ctool->start ? "-s" : "-n"); i2ctool_printf(i2ctool, " [-i|j], Auto increment|don't increment regaddr on repititions. " "Default: NO Current: %s\n", i2ctool->autoincr ? "YES" : "NO"); i2ctool_printf(i2ctool, " [-f freq] I2C frequency. " "Default: %d Current: %d\n", CONFIG_I2CTOOL_DEFFREQ, i2ctool->freq); i2ctool_printf(i2ctool, "\nNOTES:\n"); #ifndef CONFIG_DISABLE_ENVIRON i2ctool_printf(i2ctool, "o An environment variable like $PATH may be used for any argument.\n"); #endif i2ctool_printf(i2ctool, "o Arguments are \"sticky\". For example, once the I2C address is\n"); i2ctool_printf(i2ctool, " specified, that address will be re-used until it is changed.\n"); i2ctool_printf(i2ctool, "\nWARNING:\n"); i2ctool_printf(i2ctool, "o The I2C dev command may have bad side effects on your I2C devices.\n"); i2ctool_printf(i2ctool, " Use only at your own risk.\n"); return OK; }
int i2ccmd_verf(FAR struct i2ctool_s *i2ctool, int argc, FAR char **argv) { FAR struct i2c_dev_s *dev; FAR char *ptr; uint16_t rdvalue; uint8_t regaddr; bool addrinaddr; long wrvalue; long repititions; int nargs; int argndx; int ret; int i; /* Parse any command line arguments */ for (argndx = 1; argndx < argc; ) { /* Break out of the look when the last option has been parsed */ ptr = argv[argndx]; if (*ptr != '-') { break; } /* Otherwise, check for common options */ nargs = common_args(i2ctool, &argv[argndx]); if (nargs < 0) { return ERROR; } argndx += nargs; } /* The options may be followed by the optional wrvalue to be written. If omitted, then * the register address will be used as the wrvalue, providing an address-in-address * test. */ addrinaddr = true; wrvalue = 0; if (argndx < argc) { wrvalue = strtol(argv[argndx], NULL, 16); if (i2ctool->width == 8) { if (wrvalue < 0 || wrvalue > 255) { i2ctool_printf(i2ctool, g_i2cargrange, argv[0]); return ERROR; } } else if (wrvalue < 0 || wrvalue > 65535) { i2ctool_printf(i2ctool, g_i2cargrange, argv[0]); return ERROR; } addrinaddr = false; argndx++; } /* There may be one more thing on the command line: The repitition * count. */ repititions = 1; if (argndx < argc) { repititions = strtol(argv[argndx], NULL, 16); if (repititions < 1) { i2ctool_printf(i2ctool, g_i2cargrange, argv[0]); return ERROR; } argndx++; } if (argndx != argc) { i2ctool_printf(i2ctool, g_i2ctoomanyargs, argv[0]); return ERROR; } /* Get a handle to the I2C bus */ dev = up_i2cinitialize(i2ctool->bus); if (!dev) { i2ctool_printf(i2ctool, "Failed to get bus %d\n", i2ctool->bus); return ERROR; } /* Set the frequency and the address (NOTE: Only 7-bit address supported now) */ I2C_SETFREQUENCY(dev, i2ctool->freq); I2C_SETADDRESS(dev, i2ctool->addr, 7); /* Loop for the requested number of repititions */ regaddr = i2ctool->regaddr; ret = OK; for (i = 0; i < repititions; i++) { /* If we are performing an address-in-address test, then use the register * address as the value to write. */ if (addrinaddr) { wrvalue = regaddr; } /* Write to the I2C bus */ ret = i2ctool_set(i2ctool, dev, regaddr, (uint16_t)wrvalue); if (ret == OK) { /* Read the value back from the I2C bus */ ret = i2ctool_get(i2ctool, dev, regaddr, &rdvalue); } /* Display the result */ if (ret == OK) { i2ctool_printf(i2ctool, "VERIFY Bus: %d Addr: %02x Subaddr: %02x Wrote: ", i2ctool->bus, i2ctool->addr, i2ctool->regaddr); if (i2ctool->width == 8) { i2ctool_printf(i2ctool, "%02x Read: %02x", (int)wrvalue, (int)rdvalue); } else { i2ctool_printf(i2ctool, "%04x Read: %04x", (int)wrvalue, (int)rdvalue); } if (wrvalue != rdvalue) { i2ctool_printf(i2ctool, " <<< FAILURE\n"); } else { i2ctool_printf(i2ctool, "\n"); } } else { i2ctool_printf(i2ctool, g_i2cxfrerror, argv[0], -ret); break; } /* Auto-increment the address if so configured */ if (i2ctool->autoincr) { regaddr++; } } (void)up_i2cuninitialize(dev); return ret; }
int common_args(FAR struct i2ctool_s *i2ctool, FAR char **arg) { FAR char *ptr = *arg; long value; int ret; if (ptr[0] != '-') { goto invalid_argument; } switch (ptr[1]) { case 'a': ret = arg_hex(arg, &value); if (value < CONFIG_I2CTOOL_MINADDR || value > CONFIG_I2CTOOL_MAXADDR) { goto out_of_range; } i2ctool->addr = (uint8_t) value; return ret; case 'b': ret = arg_decimal(arg, &value); if (value < CONFIG_I2CTOOL_MINBUS || value > CONFIG_I2CTOOL_MAXBUS) { goto out_of_range; } i2ctool->bus = (uint8_t) value; return ret; case 'f': ret = arg_decimal(arg, &value); if (value == 0) { goto out_of_range; } i2ctool->freq = value; return ret; case 'i': i2ctool->autoincr = true; return 1; case 'j': i2ctool->autoincr = false; return 1; case 'n': i2ctool->start = false; return 1; case 'r': ret = arg_hex(arg, &value); if (value < 0 || value > CONFIG_I2CTOOL_MAXREGADDR) { goto out_of_range; } i2ctool->regaddr = (uint8_t) value; return ret; case 's': i2ctool->start = true; return 1; case 'w': ret = arg_decimal(arg, &value); if (value != 8 && value != 16) { goto out_of_range; } i2ctool->width = (uint8_t) value; return ret; default: goto invalid_argument; } invalid_argument: i2ctool_printf(i2ctool, g_i2carginvalid, ptr); return ERROR; out_of_range: i2ctool_printf(i2ctool, g_i2cargrange, ptr); return ERROR; }
int i2ccmd_get(FAR struct i2ctool_s *i2ctool, int argc, FAR char **argv) { FAR char *ptr; uint16_t result; uint8_t regaddr; long repititions; int nargs; int argndx; int ret; int fd; int i; /* Parse any command line arguments */ for (argndx = 1; argndx < argc; ) { /* Break out of the look when the last option has been parsed */ ptr = argv[argndx]; if (*ptr != '-') { break; } /* Otherwise, check for common options */ nargs = common_args(i2ctool, &argv[argndx]); if (nargs < 0) { return ERROR; } argndx += nargs; } /* There may be one more thing on the command line: The repitition * count. */ repititions = 1; if (argndx < argc) { repititions = strtol(argv[argndx], NULL, 16); if (repititions < 1) { i2ctool_printf(i2ctool, g_i2cargrange, argv[0]); return ERROR; } argndx++; } if (argndx != argc) { i2ctool_printf(i2ctool, g_i2ctoomanyargs, argv[0]); return ERROR; } /* Get a handle to the I2C bus */ fd = i2cdev_open(i2ctool->bus); if (fd < 0) { i2ctool_printf(i2ctool, "Failed to get bus %d\n", i2ctool->bus); return ERROR; } /* Loop for the requested number of repititions */ regaddr = i2ctool->regaddr; ret = OK; for (i = 0; i < repititions; i++) { /* Read from the I2C bus */ ret = i2ctool_get(i2ctool, fd, regaddr, &result); /* Display the result */ if (ret == OK) { i2ctool_printf(i2ctool, "READ Bus: %d Addr: %02x Subaddr: %02x Value: ", i2ctool->bus, i2ctool->addr, regaddr); if (i2ctool->width == 8) { i2ctool_printf(i2ctool, "%02x\n", result); } else { i2ctool_printf(i2ctool, "%04x\n", result); } } else { i2ctool_printf(i2ctool, g_i2cxfrerror, argv[0], -ret); break; } /* Auto-increment the address if so configured */ if (i2ctool->autoincr) { regaddr++; } } (void)close(fd); return ret; }