int i2c_parse(FAR struct i2ctool_s *i2ctool, int argc, char *argv[]) { FAR char *newargs[MAX_ARGUMENTS+2]; FAR char *cmd; int nargs; int index; /* Parse out the command, skipping the first argument (the program name)*/ index = 1; cmd = i2c_argument(i2ctool, argc, argv, &index); /* Check if any command was provided */ if (!cmd) { /* An empty line is not an error and an unprocessed command cannot * generate an error, but neither should they change the last * command status. */ return i2ccmd_help(i2ctool, 0, NULL); } /* Parse all of the arguments following the command name. */ newargs[0] = cmd; for (nargs = 1; nargs <= MAX_ARGUMENTS; nargs++) { newargs[nargs] = i2c_argument(i2ctool, argc, argv, &index); if (!newargs[nargs]) { break; } } newargs[nargs] = NULL; /* Then execute the command */ return i2c_execute(i2ctool, nargs, newargs); }
// read the lm75 temperature sensor // lm75 has a two-byte temperature output. The first byte is the temperature, // and the second byte's highest bit is a half-degree adder static void lm75_read(struct locl_sensor *sensor) { i2c_op *ops[2]; i2c_op read_lm75; char buf[2]; int rc; bool fault = false; const YamlDevice *device = yaml_find_device( yaml_handle, sensor->subsystem->name, sensor->yaml_sensor->device); VLOG_DBG("reading sensor '%s'", sensor->yaml_sensor->device); if (sensor->test_temp != -1) { VLOG_DBG("Test temperature override set to %d", sensor->test_temp); sensor->status = SENSOR_STATUS_NORMAL; sensor->temp = sensor->test_temp; return; } memset(buf, 0xFF, sizeof(buf)); memset(&read_lm75, 0, sizeof(read_lm75)); ops[0] = &read_lm75; ops[1] = NULL; read_lm75.direction = READ; read_lm75.device = sensor->yaml_sensor->device; read_lm75.byte_count = 2; read_lm75.data = (unsigned char *)buf; read_lm75.register_address = 0; rc = i2c_execute(yaml_handle, sensor->subsystem->name, device, ops); if (0 != rc) { fault = true; } if (true == fault) { // if we've hit the retry limit, mark it as failed if (sensor->fault_count > MAX_FAIL_RETRY) { sensor->status = SENSOR_STATUS_FAILED; } // otherwise, don't change the temp or status, but increment the retry // count sensor->fault_count++; return; } // if we succeeded in reading the temp, then clear the retry count sensor->fault_count = 0; if (sensor->status == SENSOR_STATUS_FAILED) { // we need to kick this sensor back into a working state sensor->status = SENSOR_STATUS_NORMAL; } // convert to milidegrees (C) sensor->temp = buf[0] * MILI_DEGREES; // high bit in second byte is half-degree indicator if (buf[1] < 0) { // half-degree in milidegrees sensor->temp += 500; } VLOG_DBG("%s: %4.1fc", sensor->yaml_sensor->device, ((float)sensor->temp)/MILI_DEGREES_FLOAT); }