diff options
author | Frans Meulenbroeks <fransmeulenbroeks@gmail.com> | 2010-03-26 09:46:41 +0100 |
---|---|---|
committer | Heiko Schocher <hs@denx.de> | 2010-03-29 13:28:57 +0200 |
commit | 2c0dc990202c69a6231122ec2463c4e3076a16f3 (patch) | |
tree | 2cd1d1e4314b04e83f1c7bd0e274ca563e4a7a91 | |
parent | a266fe955a55bb7a03a67f3c91033068f317b337 (diff) |
cmd_i2c: introduced get_alen helper function
The code to parse alen appeared 6 times in the function.
Factored this out in a small helper function
Signed-off-by: Frans Meulenbroeks <fransmeulenbroeks@gmail.com>
-rw-r--r-- | common/cmd_i2c.c | 119 |
1 files changed, 47 insertions, 72 deletions
diff --git a/common/cmd_i2c.c b/common/cmd_i2c.c index bf7f6d352ce..e98b35c7470 100644 --- a/common/cmd_i2c.c +++ b/common/cmd_i2c.c @@ -151,6 +151,29 @@ int i2c_set_bus_speed(unsigned int) __attribute__((weak, alias("__def_i2c_set_bus_speed"))); /* + * get_alen: small parser helper function to get address length + * returns the address length,or 0 on error + */ +static uint get_alen(char *arg) +{ + int j; + int alen; + + alen = 1; + for (j = 0; j < 8; j++) { + if (arg[j] == '.') { + alen = arg[j+1] - '0'; + if (alen > 3) { + return 0; + } + break; + } else if (arg[j] == '\0') + break; + } + return alen; +} + +/* * Syntax: * i2c read {i2c_chip} {devaddr}{.0, .1, .2} {len} {memaddr} */ @@ -160,7 +183,6 @@ static int do_i2c_read ( cmd_tbl_t *cmdtp, int flag, int argc, char *argv[]) u_char chip; uint devaddr, alen, length; u_char *memaddr; - int j; if (argc != 5) { cmd_usage(cmdtp); @@ -177,17 +199,10 @@ static int do_i2c_read ( cmd_tbl_t *cmdtp, int flag, int argc, char *argv[]) * 2 bytes long. Some day it might be 3 bytes long :-). */ devaddr = simple_strtoul(argv[2], NULL, 16); - alen = 1; - for (j = 0; j < 8; j++) { - if (argv[2][j] == '.') { - alen = argv[2][j+1] - '0'; - if (alen > 3) { - cmd_usage(cmdtp); - return 1; - } - break; - } else if (argv[2][j] == '\0') - break; + alen = get_alen(argv[2]); + if (alen == 0) { + cmd_usage(cmdtp); + return 1; } /* @@ -234,7 +249,6 @@ static int do_i2c_md ( cmd_tbl_t *cmdtp, int flag, int argc, char *argv[]) /* * New command specified. */ - alen = 1; /* * I2C chip address @@ -246,17 +260,10 @@ static int do_i2c_md ( cmd_tbl_t *cmdtp, int flag, int argc, char *argv[]) * 2 bytes long. Some day it might be 3 bytes long :-). */ addr = simple_strtoul(argv[2], NULL, 16); - alen = 1; - for (j = 0; j < 8; j++) { - if (argv[2][j] == '.') { - alen = argv[2][j+1] - '0'; - if (alen > 3) { - cmd_usage(cmdtp); - return 1; - } - break; - } else if (argv[2][j] == '\0') - break; + alen = get_alen(argv[2]); + if (alen == 0) { + cmd_usage(cmdtp); + return 1; } /* @@ -324,7 +331,6 @@ static int do_i2c_mw ( cmd_tbl_t *cmdtp, int flag, int argc, char *argv[]) uint alen; uchar byte; int count; - int j; if ((argc < 4) || (argc > 5)) { cmd_usage(cmdtp); @@ -340,17 +346,10 @@ static int do_i2c_mw ( cmd_tbl_t *cmdtp, int flag, int argc, char *argv[]) * Address is always specified. */ addr = simple_strtoul(argv[2], NULL, 16); - alen = 1; - for (j = 0; j < 8; j++) { - if (argv[2][j] == '.') { - alen = argv[2][j+1] - '0'; - if (alen > 3) { - cmd_usage(cmdtp); - return 1; - } - break; - } else if (argv[2][j] == '\0') - break; + alen = get_alen(argv[2]); + if (alen == 0) { + cmd_usage(cmdtp); + return 1; } /* @@ -398,7 +397,6 @@ static int do_i2c_crc (cmd_tbl_t *cmdtp, int flag, int argc, char *argv[]) uchar byte; ulong crc; ulong err; - int j; if (argc < 4) { cmd_usage(cmdtp); @@ -414,17 +412,10 @@ static int do_i2c_crc (cmd_tbl_t *cmdtp, int flag, int argc, char *argv[]) * Address is always specified. */ addr = simple_strtoul(argv[2], NULL, 16); - alen = 1; - for (j = 0; j < 8; j++) { - if (argv[2][j] == '.') { - alen = argv[2][j+1] - '0'; - if (alen > 3) { - cmd_usage(cmdtp); - return 1; - } - break; - } else if (argv[2][j] == '\0') - break; + alen = get_alen(argv[2]); + if (alen == 0) { + cmd_usage(cmdtp); + return 1; } /* @@ -469,7 +460,6 @@ mod_i2c_mem(cmd_tbl_t *cmdtp, int incrflag, int flag, int argc, char *argv[]) ulong data; int size = 1; int nbytes; - int j; extern char console_buffer[]; if (argc != 3) { @@ -504,17 +494,10 @@ mod_i2c_mem(cmd_tbl_t *cmdtp, int incrflag, int flag, int argc, char *argv[]) * Address is always specified. */ addr = simple_strtoul(argv[2], NULL, 16); - alen = 1; - for (j = 0; j < 8; j++) { - if (argv[2][j] == '.') { - alen = argv[2][j+1] - '0'; - if (alen > 3) { - cmd_usage(cmdtp); - return 1; - } - break; - } else if (argv[2][j] == '\0') - break; + alen = get_alen(argv[2]); + if (alen == 0) { + cmd_usage(cmdtp); + return 1; } } @@ -644,7 +627,6 @@ static int do_i2c_loop(cmd_tbl_t *cmdtp, int flag, int argc, char *argv[]) uint length; u_char bytes[16]; int delay; - int j; if (argc < 3) { cmd_usage(cmdtp); @@ -660,17 +642,10 @@ static int do_i2c_loop(cmd_tbl_t *cmdtp, int flag, int argc, char *argv[]) * Address is always specified. */ addr = simple_strtoul(argv[2], NULL, 16); - alen = 1; - for (j = 0; j < 8; j++) { - if (argv[2][j] == '.') { - alen = argv[2][j+1] - '0'; - if (alen > 3) { - cmd_usage(cmdtp); - return 1; - } - break; - } else if (argv[2][j] == '\0') - break; + alen = get_alen(argv[2]); + if (alen == 0) { + cmd_usage(cmdtp); + return 1; } /* |