From 9da00855c144e5bf4718157ec28538c7f8e7c014 Mon Sep 17 00:00:00 2001 From: Olaf Rempel Date: Sat, 25 Dec 2010 13:08:02 +0100 Subject: [PATCH] some cleanup --- linux/filedata.c | 2 +- linux/filedata.h | 19 +++++++++++++++ linux/twb.c | 28 +++++++++++++++++++++- linux/{twiboot.h => twb.h} | 22 ++++------------- linux/twiboot.c | 49 +++++++++++++++----------------------- 5 files changed, 70 insertions(+), 50 deletions(-) create mode 100644 linux/filedata.h rename linux/{twiboot.h => twb.h} (55%) diff --git a/linux/filedata.c b/linux/filedata.c index 6854b66..8c6543e 100644 --- a/linux/filedata.c +++ b/linux/filedata.c @@ -25,7 +25,7 @@ #include #include -#include "twiboot.h" +#include "filedata.h" #define FILETYPE_UNKNOWN 0 #define FILETYPE_BINARY 1 diff --git a/linux/filedata.h b/linux/filedata.h new file mode 100644 index 0000000..f11fef1 --- /dev/null +++ b/linux/filedata.h @@ -0,0 +1,19 @@ +#ifndef _FILEDATA_H_ +#define _FILEDATA_H_ + +#include + +struct databuf { + uint32_t size; // allocation size + uint32_t length; // used size + uint8_t data[0]; +}; + +int dbuf_alloc(struct databuf **dbuf, uint32_t size); +void dbuf_free(struct databuf *dbuf); + +int file_getsize(const char *filename, uint32_t *size); +int file_read(const char *filename, struct databuf *dbuf); +int file_write(const char *filename, struct databuf *dbuf); + +#endif /* _FILEDATA_H_ */ diff --git a/linux/twb.c b/linux/twb.c index 2da46be..e5f7a4c 100644 --- a/linux/twb.c +++ b/linux/twb.c @@ -31,10 +31,12 @@ #include #include +#include "filedata.h" #include "list.h" -#include "twiboot.h" +#include "twb.h" #define MIN(a, b) ((a) < (b) ? (a) : (b)) +#define ARRAY_SIZE(x) (sizeof(x) / sizeof(*x)) #define READ_BLOCK_SIZE 128 /* bytes in one flash/eeprom read request */ #define WRITE_BLOCK_SIZE 16 /* bytes in one eeprom write request */ @@ -58,6 +60,28 @@ #define MEMTYPE_EEPROM 0x02 #define MEMTYPE_PARAMETERS 0x03 /* only in APP */ +struct chipinfo { + uint8_t sig[3]; + const char name[16]; +}; + +static struct chipinfo chips[] = { + { { 0x1E, 0x93, 0x07 }, "AVR Mega 8" }, + { { 0x1E, 0x93, 0x0A }, "AVR Mega 88" }, + { { 0x1E, 0x94, 0x06 }, "AVR Mega 168" }, +}; + +static const char * twb_get_chipname(uint8_t *sig) +{ + int i; + for (i = 0; i < ARRAY_SIZE(chips); i++) { + struct chipinfo *chip = &chips[i]; + if (chip->sig[0] == sig[0] && chip->sig[1] == sig[1] && chip->sig[2] == sig[2]) + return chip->name; + } + + return "unknown"; +} static int twb_switch_application(struct twiboot *twb, uint8_t application) { @@ -206,6 +230,8 @@ int twb_open(struct twiboot *twb) } memcpy(twb->signature, chipinfo, sizeof(twb->signature)); + twb->chipname = twb_get_chipname(twb->signature); + twb->pagesize = chipinfo[3]; twb->flashsize = (chipinfo[4] << 8) + chipinfo[5]; twb->eepromsize = (chipinfo[6] << 8) + chipinfo[7]; diff --git a/linux/twiboot.h b/linux/twb.h similarity index 55% rename from linux/twiboot.h rename to linux/twb.h index 9035155..c26c48f 100644 --- a/linux/twiboot.h +++ b/linux/twb.h @@ -1,21 +1,8 @@ -#ifndef _TWIBOOT_H_ -#define _TWIBOOT_H_ +#ifndef _TWB_H_ +#define _TWB_H_ #include -struct databuf { - uint32_t size; // allocation size - uint32_t length; // used size - uint8_t data[0]; -}; - -int dbuf_alloc(struct databuf **dbuf, uint32_t size); -void dbuf_free(struct databuf *dbuf); - -int file_getsize(const char *filename, uint32_t *size); -int file_read(const char *filename, struct databuf *dbuf); -int file_write(const char *filename, struct databuf *dbuf); - struct twiboot { char *device; uint8_t address; @@ -24,6 +11,8 @@ struct twiboot { char version[16]; uint8_t signature[3]; + const char *chipname; + uint8_t pagesize; uint16_t flashsize; uint16_t eepromsize; @@ -35,9 +24,6 @@ struct twiboot { int twb_open(struct twiboot *twb); int twb_close(struct twiboot *twb); -#define DATATYPE_FLASH 0x01 -#define DATATYPE_EEPROM 0x02 - int twb_read(struct twiboot *twb, struct databuf *dbuf, int memtype); int twb_verify(struct twiboot *twb, struct databuf *dbuf, int memtype); int twb_write(struct twiboot *twb, struct databuf *dbuf, int memtype); diff --git a/linux/twiboot.c b/linux/twiboot.c index 75adc34..396d247 100644 --- a/linux/twiboot.c +++ b/linux/twiboot.c @@ -23,11 +23,14 @@ #include +#include "filedata.h" #include "list.h" -#include "twiboot.h" +#include "twb.h" -#define OP_READ 0x01 -#define OP_WRITE 0x02 +#define OP_MODE_READ 0x01 +#define OP_MODE_WRITE 0x02 +#define OP_TYPE_FLASH 0x01 +#define OP_TYPE_EEPROM 0x02 struct operation { struct list_head list; @@ -61,11 +64,11 @@ static struct operation * alloc_operation(const char *arg) } if (strncmp(arg, "flash:", 6) == 0) { - op->memtype = DATATYPE_FLASH; + op->memtype = OP_TYPE_FLASH; op->filename = strdup(arg + 6); } else if (strncmp(arg, "eeprom:", 7) == 0) { - op->memtype = DATATYPE_EEPROM; + op->memtype = OP_TYPE_EEPROM; op->filename = strdup(arg + 7); } else { @@ -76,20 +79,6 @@ static struct operation * alloc_operation(const char *arg) return op; } -static char * check_signature(uint8_t *sig) -{ - if (sig[0] == 0x1E && sig[1] == 0x93 && sig[2] == 0x07) - return "AVR Mega 8"; - - if (sig[0] == 0x1E && sig[1] == 0x93 && sig[2] == 0x0A) - return "AVR Mega 88"; - - if (sig[0] == 0x1E && sig[1] == 0x94 && sig[2] == 0x06) - return "AVR Mega 168"; - - return "unknown"; -} - static void progress_cb(const char *msg, int pos, int size) { if (pos != -1 && size != -1) { @@ -149,7 +138,7 @@ int main(int argc, char *argv[]) { struct operation *op = alloc_operation(optarg); if (op != NULL) { - op->mode = OP_READ; + op->mode = OP_MODE_READ; list_add_tail(&op->list, &operation_list); } else { @@ -162,7 +151,7 @@ int main(int argc, char *argv[]) { struct operation *op = alloc_operation(optarg); if (op != NULL) { - op->mode = OP_WRITE; + op->mode = OP_MODE_WRITE; list_add_tail(&op->list, &operation_list); } else { @@ -219,7 +208,7 @@ int main(int argc, char *argv[]) if (!abort) { printf("device : %-16s (address: 0x%02x)\n", twb.device, twb.address); - printf("version : %-16s (sig: 0x%02x 0x%02x 0x%02x => %s)\n", twb.version, twb.signature[0], twb.signature[1], twb.signature[2], check_signature(twb.signature)); + printf("version : %-16s (sig: 0x%02x 0x%02x 0x%02x => %s)\n", twb.version, twb.signature[0], twb.signature[1], twb.signature[2], twb.chipname); printf("flash size : 0x%04x (0x%02x bytes/page)\n", twb.flashsize, twb.pagesize); printf("eeprom size : 0x%04x\n", twb.eepromsize); @@ -231,14 +220,14 @@ int main(int argc, char *argv[]) struct operation *op; list_for_each_entry(op, &operation_list, list) { abort = 1; - if (op->mode == OP_READ) { + if (op->mode == OP_MODE_READ) { struct databuf *dbuf; int result; - if (op->memtype == DATATYPE_FLASH) { + if (op->memtype == OP_TYPE_FLASH) { twb.progress_msg = "reading flash"; result = dbuf_alloc(&dbuf, twb.flashsize); - } else if (op->memtype == DATATYPE_EEPROM) { + } else if (op->memtype == OP_TYPE_EEPROM) { twb.progress_msg = "reading eeprom"; result = dbuf_alloc(&dbuf, twb.eepromsize); } @@ -262,7 +251,7 @@ int main(int argc, char *argv[]) dbuf_free(dbuf); - } else if (op->mode == OP_WRITE) { + } else if (op->mode == OP_MODE_WRITE) { struct databuf *dbuf; unsigned int size; int result; @@ -282,7 +271,7 @@ int main(int argc, char *argv[]) break; } - if (op->memtype == DATATYPE_FLASH) { + if (op->memtype == OP_TYPE_FLASH) { twb.progress_msg = "writing flash"; if (dbuf->length > twb.flashsize) { @@ -291,7 +280,7 @@ int main(int argc, char *argv[]) break; } - } else if (op->memtype == DATATYPE_EEPROM) { + } else if (op->memtype == OP_TYPE_EEPROM) { twb.progress_msg = "writing eeprom"; if (dbuf->length > twb.eepromsize) { @@ -309,9 +298,9 @@ int main(int argc, char *argv[]) } if (verify) { - if (op->memtype == DATATYPE_FLASH) { + if (op->memtype == OP_TYPE_FLASH) { twb.progress_msg = "verifing flash"; - } else if (op->memtype == DATATYPE_EEPROM) { + } else if (op->memtype == OP_TYPE_EEPROM) { twb.progress_msg = "verifing eeprom"; }