X-Git-Url: http://cloudbase.mooo.com/gitweb/z180-stamp.git/blobdiff_plain/61b0cfe9df810db4fbca78e5f880d61c5063f324..9461ecf31e8c701160059af2836337f79fc837b3:/avr/debug.c diff --git a/avr/debug.c b/avr/debug.c index 16df702..384a5ad 100644 --- a/avr/debug.c +++ b/avr/debug.c @@ -1,3 +1,10 @@ +/* + * (C) Copyright 2014 Leo C. + * + * SPDX-License-Identifier: GPL-2.0 + */ + +#include "debug.h" #include "common.h" #include #include @@ -5,78 +12,14 @@ #include #include "command.h" -#include "debug.h" +#include "cli_readline.h" +#include "print-utils.h" /* * Debugging */ -#ifdef DEBUG -static void print_blanks(uint_fast8_t count) -{ - while(count--) - putchar(' '); -} - -static uint8_t ram_read_byte(const uint8_t *p) -{ - return *p; -} - -void dump_mem(const uint8_t *startaddr, int len, - uint8_t (*readfkt)(const uint8_t *), char *title) -{ - uint8_t buf[16]; - char *indent = NULL; - uint8_t llen = 16; - uint8_t pre = (size_t) startaddr % 16; - const uint8_t *addr = (uint8_t *) ((size_t) startaddr & ~0x0f); - len += pre; - uint8_t i; - - if (title && *title) { - printf_P(PSTR("%s\n"),title); - indent = " "; - } - - while (len) { - if (len < 16) - llen = len; - - for (i = pre; i < llen; i++) - buf[i] = readfkt(addr + i); - - printf_P(PSTR("%s%04x:"),indent, addr); - for (i = 0; i < llen; i++) { - if ((i % 8) == 0) - putchar(' '); - if (i < pre) - printf_P(PSTR(".. ")); - else - printf_P(PSTR("%.2x "), buf[i]); - } - /* fill line with whitespace for nice ASCII print */ - print_blanks(3 * (16u - i) + (16u-i)/8 + 1 + pre); - /* Print data in ASCII characters */ - for (i = pre; i < llen; i++) - printf_P(PSTR("%c"), isprint(buf[i]) ? buf[i] : '.'); - putchar('\n'); - - pre = 0; - addr += 16; - len -= llen; - } -} - -void dump_eep(const uint8_t *addr, unsigned int len, char *title) -{ - dump_mem(addr, len, eeprom_read_byte, title); -} - -void dump_ram(const uint8_t *addr, unsigned int len, char *title) -{ - dump_mem(addr, len, ram_read_byte, title); -} +#ifdef DEBUG #if 0 @@ -84,7 +27,7 @@ void dump_heap(void) { extern unsigned int __brkval; - dump_ram((uint8_t *) __malloc_heap_start, + dump_ram(__malloc_heap_start, __brkval - (unsigned int) __malloc_heap_start, "=== Heap:"); } @@ -97,34 +40,39 @@ void dump_heap(void) */ command_ret_t do_dump_mem(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[]) { -// static const uint8_t *addr; -// static uint16_t length = 128; - uint8_t (*readhow)(const uint8_t *); - + int (*readwhat)(uint8_t *buf, uint32_t addr, uint8_t count); + (void) cmdtp; (void) flag; if (argc < 2) return CMD_RET_USAGE; - const uint8_t *addr; - uint16_t length = 128; - - if (strchr(argv[0],'r') != NULL) - readhow = ram_read_byte; - else if (strchr(argv[0],'e') != NULL) - readhow = eeprom_read_byte; - else + uint32_t addr; + uint32_t length = 128; + + switch (argv[0][3]) { + case 'r': + readwhat = ram_read_buf; + break; + case 'e': + readwhat = eeprom_read_buf; + break; + case 'f': + readwhat = flash_read_buf; + break; + default: return CMD_RET_USAGE; + } /* Address is specified since argc > 1 */ - addr = (const uint8_t *) (size_t) strtoul(argv[1], NULL, 16); + addr = strtoul(argv[1], NULL, 16); /* If another parameter, it is the length to display. */ if (argc > 2) length = (uint16_t) strtoul(argv[2], NULL, 16); /* Print the lines. */ - dump_mem(addr, length, readhow, NULL); + dump_mem(addr, addr, length, readwhat, NULL); return CMD_RET_SUCCESS; } @@ -179,10 +127,87 @@ command_ret_t do_eep_cp(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[ return CMD_RET_SUCCESS; } -/*------------------------------------------------------------------------------*/ +/* Modify memory. + * + * Syntax: + * !mm {addr} + * !nm {addr} + */ -#if 0 + static uint8_t *mm_last_addr; + +static command_ret_t +mod_mem_avr(cmd_tbl_t *cmdtp, int incrflag, int flag, int argc, char * const argv[]) +{ + uint8_t *addr; + uint8_t data; + int nbytes; + + (void) cmdtp; + + if (argc != 2) + return CMD_RET_USAGE; + + /* We use the last specified parameters, unless new ones are + * entered. + */ + addr = mm_last_addr; + + if ((flag & CMD_FLAG_REPEAT) == 0) { + /* New command specified. + */ + + /* Address is specified since argc > 1 + */ + addr = (uint8_t *) (size_t) strtoul(argv[1], NULL, 16); + } + + /* Print the address, followed by value. Then accept input for + * the next value. A non-converted value exits. + */ + do { + data = *addr; + printf_P(PSTR("%04x: %02x"), addr, data); + + nbytes = cli_readline(PSTR(" ? "), 0); + if (nbytes == 0 || (nbytes == 1 && console_buffer[0] == '-')) { + /* pressed as only input, don't modify current + * location and move to next. "-" pressed will go back. + */ + if (incrflag) + addr += nbytes ? -1 : 1; + nbytes = 1; + + } else { + char *endp; + data = strtoul(console_buffer, &endp, 16); + nbytes = endp - console_buffer; + if (nbytes) { + *addr = data; + if (incrflag) + addr++; + } + } + } while (nbytes); + + mm_last_addr = addr; + return CMD_RET_SUCCESS; +} + + +command_ret_t do_mem_mm_avr(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[]) +{ + return mod_mem_avr (cmdtp, 1, flag, argc, argv); +} +command_ret_t do_mem_nm_avr(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[]) +{ + return mod_mem_avr (cmdtp, 0, flag, argc, argv); +} + +/*------------------------------------------------------------------------------*/ + +#if 1 struct __freelist { size_t sz; @@ -201,6 +226,8 @@ printfreelist(const char * title) int i; unsigned int freesum = 0; +/* TODO: printf_P */ + if (!__flp) { printf("%s no free list\n", title ? title : ""); } else { @@ -215,7 +242,7 @@ printfreelist(const char * title) freesum += fp1->sz; } } - + freesum += (size_t) STACK_POINTER() - __malloc_margin - (size_t) __brkval; printf("SP: %04x, __brkval: %04x, Total free: %04u\n", @@ -225,4 +252,3 @@ printfreelist(const char * title) #endif #endif /* DEBUG */ -