X-Git-Url: http://cloudbase.mooo.com/gitweb/z180-stamp.git/blobdiff_plain/31033ab3f82c4d00e12fb72a65c2ba614da8ad2f..cb4fb1ed02b59f798368a76f2596d7c24125e68a:/avr/cmd_loadihex.c diff --git a/avr/cmd_loadihex.c b/avr/cmd_loadihex.c index 01fb6b0..18d5331 100644 --- a/avr/cmd_loadihex.c +++ b/avr/cmd_loadihex.c @@ -12,7 +12,7 @@ #include "command.h" #include "con-utils.h" #include "z80-if.h" -//#include "debug.h" +#include "debug.h" uint32_t detect_ramsize(void) @@ -43,14 +43,6 @@ uint32_t detect_ramsize(void) return addr; } -static uint32_t min(uint32_t a, uint32_t b) -{ - if (a < b) - return a; - return b; -} - - typedef enum { IHX_OK, IHX_BROKEN, @@ -93,74 +85,6 @@ int get_hexbyte(void) { return -1; } -#if 0 - -static -ihex_t ihex_get_record() { - - int i; - uint8_t sum, c; - ihex_t rec = { IHX_BROKEN, 0, 0, 0, 0 }; - - - while ((c = my_getchar(0)) != ':') - if (c == 0x03) - return rec; - - if ((i = get_hexbyte()) < 0) /* Start code */ - return rec; - sum = i; - rec.len = i; - if ((i = get_hexbyte()) < 0) /* Byte Count */ - return rec; - sum += i; - rec.address = i * 256; - if ((i = get_hexbyte()) < 0) /* Address */ - return rec; - sum += i; - rec.address += i; - if ((i = get_hexbyte()) < 0) /* Record type */ - return rec; - sum += i; - rec.type = i; - - if (rec.len) { /* Record Data */ - uint8_t *p; int k; - if ((rec.data = malloc(rec.len)) == 0) - return rec; - - for (p=rec.data, k=rec.len; k; k--) { - if ((i = get_hexbyte()) < 0) - break; - sum += i; - *p++ = i; - } - - if (k) { - free(rec.data); rec.data = 0; - return rec; - } - } - - i = get_hexbyte(); /* Check sum */ - - if (i >= 0) { - sum += i; - if (sum == 0) - rec.status = IHX_OK; - else - rec.status = IHX_CHKSUMERR; - } - - if (rec.status != IHX_OK) { - free(rec.data); - rec.data = 0; - } - - return rec; -} - -#else static int ihex_get_record(ihex_t *rec) { @@ -169,11 +93,17 @@ int ihex_get_record(ihex_t *rec) { uint8_t sum; rec->status = IHX_BROKEN; + rec->len = 0; + rec->type = 0xff; - - while ((c = my_getchar(0)) != ':') + while ((c = my_getchar(0)) != ':') { if (c == 0x03) - return -1; + return -1; /* Control-C */ + if (c == 0x04) { + rec->status = IHX_OK; + return 0; /*Control-D, EOF */ + } + } if ((c = get_hexbyte()) < 0) /* Start code */ return -1; @@ -220,7 +150,6 @@ int ihex_get_record(ihex_t *rec) { return rec->len; } -#endif command_ret_t do_loadihex(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[]) { @@ -231,22 +160,26 @@ command_ret_t do_loadihex(cmd_tbl_t *cmdtp, int flag, int argc, char * const arg uint32_t address_low = address_max; command_ret_t rcode = CMD_RET_FAILURE; ihex_t rec; + bool firstrec = true; (void) cmdtp; (void) flag; - printf_P(PSTR("RAM Size: 0x%.5lX\n"), address_max); - - if (argc > 1) offset = strtol(argv[1], NULL, 16); + printf_P(PSTR("Waiting for Intel Hex Records...\n")); + while (ihex_get_record(&rec) > 0 && rec.status == IHX_OK && rec.type != 1 ) { switch (rec.type) { case 0: /* Data record */ + if (firstrec) { + printf_P(PSTR("Loading: 0x.....")); + firstrec = false; + } if (rec.len) { uint32_t addr = base_address + rec.address + offset; if (addr < address_low) @@ -254,10 +187,9 @@ command_ret_t do_loadihex(cmd_tbl_t *cmdtp, int flag, int argc, char * const arg if (addr+rec.len > address_high) address_high = addr + rec.len; - -printf_P(PSTR("low: 0x%.5lX, high: 0x%.5lX, max: 0x%.5lX, addr: 0x%.5lX, len: %d\n"), - address_low, address_high, address_max, addr, rec.len); - +// debug("low: 0x%.5lX, high: 0x%.5lX, max: 0x%.5lX, addr: 0x%.5lX, len: %d\n", +// address_low, address_high, address_max, addr, rec.len); + printf_P(PSTR("\b\b\b\b\b%.5lX"), addr); if (addr < address_max) { uint32_t tmplen = address_max - addr; if (rec.len > tmplen) @@ -280,7 +212,7 @@ printf_P(PSTR("low: 0x%.5lX, high: 0x%.5lX, max: 0x%.5lX, addr: 0x%.5lX, len: %d break; #endif case 2: /* Extended Segment Address Record */ - base_address = ((rec.data[0] << 8) + rec.data[1]) << 4; + base_address = (uint32_t)((rec.data[0] << 8) + rec.data[1]) << 4; break; case 4: /* Extended Linear Address Record */ @@ -305,16 +237,25 @@ printf_P(PSTR("low: 0x%.5lX, high: 0x%.5lX, max: 0x%.5lX, addr: 0x%.5lX, len: %d break; } - printf_P(PSTR("Data loaded: ")); - if (address_low >= min(address_high, address_max)) + + for (uint_fast8_t i=0; i<100; ++i) { + /* flush input buffer */ + while (my_getchar(0) > 0) + ; + udelay(1000); + } + + + printf_P(PSTR("\nData loaded: ")); + if (address_low >= MIN(address_high, address_max)) printf_P(PSTR("None.\n")); else printf_P(PSTR("low: 0x%.5lX high: 0x%.5lX\n"), address_low, - min(address_high, address_max) - 1); + MIN(address_high, address_max) - 1); if (address_high > address_max) printf_P(PSTR("Data above highest RAM address " - "(Range 0x%.5lX...0x%.5lX) ignored!\n"), address_max, address_high - 1); + "(in range 0x%.5lX - 0x%.5lX) ignored!\n"), address_max, address_high - 1); return rcode; }