Lines Matching defs:i2c_opt
315 write_offset(int fd, struct options i2c_opt, struct iiccmd *cmd)
318 if (i2c_opt.off_len > 0) {
319 cmd->count = i2c_opt.off_len;
320 cmd->buf = (void*)i2c_opt.off_buf;
327 i2c_write(int fd, struct options i2c_opt, uint8_t *i2c_buf)
330 char buf[i2c_opt.off_len + i2c_opt.count];
333 cmd.slave = i2c_opt.addr;
338 switch(i2c_opt.mode) {
340 if (write_offset(fd, i2c_opt, &cmd))
352 cmd.count = i2c_opt.count;
360 if (write_offset(fd, i2c_opt, &cmd))
369 cmd.count = i2c_opt.count;
378 memcpy(buf, i2c_opt.off_buf, i2c_opt.off_len);
379 memcpy(buf + i2c_opt.off_len, i2c_buf, i2c_opt.count);
383 cmd.count = i2c_opt.off_len + i2c_opt.count;
395 i2c_read(int fd, struct options i2c_opt, uint8_t *i2c_buf)
401 cmd.slave = i2c_opt.addr;
403 if (i2c_opt.off_len) {
410 if (write_offset(fd, i2c_opt, &cmd))
413 if (i2c_opt.mode == I2C_MODE_STOP_START && i2c_do_stop(fd))
416 cmd.slave = i2c_opt.addr | 1;
420 if (i2c_opt.mode == I2C_MODE_STOP_START || i2c_opt.off_len == 0) {
423 } else if (i2c_opt.mode == I2C_MODE_REPEATED_START) {
428 cmd.count = i2c_opt.count;
449 i2c_rdwr_transfer(int fd, struct options i2c_opt, uint8_t *i2c_buf)
455 if (i2c_opt.off_len) {
457 msgp->slave = i2c_opt.addr;
458 msgp->buf = i2c_opt.off_buf;
459 msgp->len = i2c_opt.off_len;
471 if (i2c_opt.dir == 'w')
475 msgp->slave = i2c_opt.addr;
476 msgp->len = i2c_opt.count;
490 access_bus(int fd, struct options i2c_opt)
492 uint8_t i2c_buf[i2c_opt.count];
499 if (i2c_opt.dir == 'w') {
500 if (i2c_opt.verbose && !i2c_opt.binary)
502 i2c_opt.count);
503 if (fread(i2c_buf, 1, i2c_opt.count, stdin) != i2c_opt.count)
507 if (i2c_opt.mode == I2C_MODE_TRANSFER)
508 error = i2c_rdwr_transfer(fd, i2c_opt, i2c_buf);
509 else if (i2c_opt.dir == 'w')
510 error = i2c_write(fd, i2c_opt, i2c_buf);
512 error = i2c_read(fd, i2c_opt, i2c_buf);
515 if (i2c_opt.dir == 'r' && i2c_opt.binary) {
516 (void)fwrite(i2c_buf, 1, i2c_opt.count, stdout);
517 } else if (i2c_opt.verbose || i2c_opt.dir == 'r') {
518 if (i2c_opt.verbose)
520 i2c_opt.dir == 'r' ? "read" : "written");
522 for (u = 0; u < i2c_opt.count; u++) {
545 command_bus(struct options i2c_opt, char *cmd)
570 i2c_opt.dir = 'r';
574 i2c_opt.dir = 'w';
592 i2c_opt.addr = strtoul(cmd, &cmd, 0);
593 if (i2c_opt.addr == 0 && errno == EINVAL) {
597 if (i2c_opt.addr < 1 || i2c_opt.addr > 0x7f) {
598 fprintf(stderr, "Invalid device (0x%x)\n", i2c_opt.addr);
601 i2c_opt.addr <<= 1;
628 i2c_opt.off_buf, &i2c_opt.off_len);
634 if (i2c_opt.dir == 'r') {
635 i2c_opt.count = strtoul(cmd, &cmd, 0);
636 if (i2c_opt.count == 0 && errno == EINVAL) {
641 i2c_opt.count = 0;
651 dbuf[i2c_opt.count] = digittoint(*cmd++) << 4;
663 dbuf[i2c_opt.count++] |= digittoint(*cmd++);
679 error = i2c_rdwr_transfer(fd, i2c_opt, dbuf);
683 if (i2c_opt.dir == 'r') {
684 for (u = 0; u < i2c_opt.count; u++)
692 exec_bus(struct options i2c_opt, char *cmd)
700 error = command_bus(i2c_opt, cmd);
701 if (i2c_opt.verbose) {
713 instruct_bus(struct options i2c_opt, int argc, char **argv)
723 error = exec_bus(i2c_opt, *argv);
732 error = exec_bus(i2c_opt, buf);
742 struct options i2c_opt;
753 i2c_opt.off = 0;
754 i2c_opt.addr = 0;
755 i2c_opt.verbose = 0;
756 i2c_opt.dir = 'r'; /* direction = read */
757 i2c_opt.width = "8";
758 i2c_opt.count = 1;
759 i2c_opt.binary = 0; /* ASCII text output */
760 i2c_opt.skip = NULL; /* scan all addresses */
761 i2c_opt.mode = I2C_MODE_TRANSFER;
800 i2c_opt.addr = strtoul(optarg, 0, 16);
801 if (i2c_opt.addr == 0 && errno == EINVAL)
803 if (i2c_opt.addr == 0 || i2c_opt.addr > 0x7f)
805 i2c_opt.addr <<= 1;
808 i2c_opt.binary = 1;
811 i2c_opt.count = (strtoul(optarg, 0, 10));
812 if (i2c_opt.count == 0 && errno == EINVAL)
818 i2c_opt.dir = optarg[0];
826 i2c_opt.mode = I2C_MODE_NONE;
828 i2c_opt.mode = I2C_MODE_STOP_START;
830 i2c_opt.mode = I2C_MODE_REPEATED_START;
832 i2c_opt.mode = I2C_MODE_TRANSFER;
837 i2c_opt.skip = optarg;
840 i2c_opt.off = strtoul(optarg, 0, 16);
841 if (i2c_opt.off == 0 && errno == EINVAL)
847 i2c_opt.verbose = 1;
850 i2c_opt.width = optarg; // checked later.
860 return(instruct_bus(i2c_opt, argc, argv));
865 if (i2c_opt.mode == I2C_MODE_NOTSET) {
866 if (i2c_opt.dir == 'r')
867 i2c_opt.mode = I2C_MODE_STOP_START;
868 else if (i2c_opt.dir == 'w')
869 i2c_opt.mode = I2C_MODE_NONE;
872 err_msg = encode_offset(i2c_opt.width, i2c_opt.off,
873 i2c_opt.off_buf, &i2c_opt.off_len);
888 if (i2c_opt.verbose)
891 i2c_opt.addr >> 1, i2c_opt.dir, i2c_opt.off,
892 i2c_opt.width, i2c_opt.count);
893 error = access_bus(fd, i2c_opt);
896 error = scan_bus(dev, fd, i2c_opt.skip, i2c_opt.verbose);
899 error = reset_bus(dev, fd, i2c_opt.verbose);