diff options
author | Daniel Friesel <derf@finalrewind.org> | 2016-01-02 15:05:24 +0100 |
---|---|---|
committer | Daniel Friesel <derf@finalrewind.org> | 2016-01-02 15:05:24 +0100 |
commit | 7768c72f7c6180a4517b0b6966af0b92c0067008 (patch) | |
tree | 0514073334419d0051272c75ea6938e5d32f274b | |
parent | 82f57f4b2d1307452ce840571b301b8317336c1e (diff) |
i2cget, i2cset: properly terminate I2C connection in case of errors
-rw-r--r-- | commandline/i2cget.c | 13 | ||||
-rw-r--r-- | commandline/i2cset.c | 11 |
2 files changed, 20 insertions, 4 deletions
diff --git a/commandline/i2cget.c b/commandline/i2cget.c index c7cd18f..9ed8bbd 100644 --- a/commandline/i2cget.c +++ b/commandline/i2cget.c @@ -11,8 +11,6 @@ int main(int argc, char **argv) char *conv_err; i2c_getopt(argc, argv); - i2c_init(); - i2c_start(); if (argc < 3) { fputs("Usage: vusb-i2cget <address> <register ...> ", stderr); @@ -26,8 +24,13 @@ int main(int argc, char **argv) return 2; } + i2c_init(); + i2c_start(); + if (!i2c_tx_byte((address << 1) | 0)) { fprintf(stderr, "Received NAK from slave 0x%02x, aborting\n", address); + i2c_stop(); + i2c_deinit(); return 3; } @@ -35,10 +38,14 @@ int main(int argc, char **argv) cmdbuf = strtol(argv[i], &conv_err, 0); if (conv_err && *conv_err) { fprintf(stderr, "read command: Conversion error at '%s'\n", conv_err); + i2c_stop(); + i2c_deinit(); return 2; } if (!i2c_tx_byte(cmdbuf)) { fprintf(stderr, "Received NAK after byte %d (0x%02x)\n", i-1, cmdbuf); + i2c_stop(); + i2c_deinit(); return 4; } } @@ -46,6 +53,8 @@ int main(int argc, char **argv) i2c_start(); if (!i2c_tx_byte((address << 1) | 1)) { fprintf(stderr, "Received NAK after reSTART from slave 0x%02x, aborting\n", address); + i2c_stop(); + i2c_deinit(); return 3; } diff --git a/commandline/i2cset.c b/commandline/i2cset.c index 5b2662c..2d7567f 100644 --- a/commandline/i2cset.c +++ b/commandline/i2cset.c @@ -10,8 +10,6 @@ int main(int argc, char **argv) char *conv_err; i2c_getopt(argc, argv); - i2c_init(); - i2c_start(); if (argc < 3) { fputs("Usage: i2cset <address> <data ...>", stderr); @@ -25,8 +23,13 @@ int main(int argc, char **argv) return 2; } + i2c_init(); + i2c_start(); + if (!i2c_tx_byte(address << 1)) { fprintf(stderr, "Received NAK from slave 0x%02x, aborting\n", address); + i2c_stop(); + i2c_deinit(); return 3; } @@ -34,10 +37,14 @@ int main(int argc, char **argv) cmdbuf = strtol(argv[i], &conv_err, 0); if (conv_err && *conv_err) { fprintf(stderr, "write command: conversion error at '%s'\n", conv_err); + i2c_stop(); + i2c_deinit(); return 2; } if (!i2c_tx_byte(cmdbuf)) { fprintf(stderr, "Received NAK after byte %d (0x%02x)\n", i-1, cmdbuf); + i2c_stop(); + i2c_deinit(); return 4; } } |