diff options
Diffstat (limited to 'avr/print-utils.c')
-rw-r--r-- | avr/print-utils.c | 98 |
1 files changed, 98 insertions, 0 deletions
diff --git a/avr/print-utils.c b/avr/print-utils.c new file mode 100644 index 0000000..83f86a9 --- /dev/null +++ b/avr/print-utils.c @@ -0,0 +1,98 @@ +/* + * (C) Copyright 2014 Leo C. <erbl259-lmu@yahoo.de> + * + * SPDX-License-Identifier: GPL-2.0+ + */ + +#include "common.h" +#include <stdint.h> +#include <stdio.h> +#include <ctype.h> +#include "con-utils.h" +#include "print-utils.h" + +void print_blanks(uint_fast8_t count) +{ + while(count--) + putchar(' '); +} + + +int eeprom_read_buf(uint8_t *buf, uint32_t addr, uint8_t count) +{ + eeprom_read_block((void *) buf, (const void *) (size_t) addr, count); + return 0; +} + +int ram_read_buf(uint8_t *buf, uint32_t addr, uint8_t count) +{ + while (count--) + *buf++ = *(uint8_t *) (size_t) addr++; + return 0; +} + +int flash_read_buf(uint8_t *buf, uint32_t addr, uint8_t count) +{ + while (count--) + *buf++ = *(const __memx uint8_t *) (__uint24) addr++; + return 0; +} + +int dump_mem(uint32_t address, uint32_t offset, uint32_t len, + int (*readfkt)(uint8_t *, uint32_t, uint8_t), char *title) +{ + uint8_t buf[16]; + char *indent = NULL; + uint8_t llen = 16; + uint8_t pre = offset % 16; + offset = offset & ~0x0f; + len += pre; + uint8_t i; + + if (title && *title) { + printf_P(PSTR("%s\n"),title); + indent = " "; + } + + while (len) { + if (len < 16) + llen = len; + if (readfkt(buf, address, llen - pre) != 0) + return -2; /* TODO: Error codes */ + + printf_P(PSTR("%s%.5lx:"),indent, offset); + for (i = 0; i < llen; i++) { + if ((i % 8) == 0) + putchar(' '); + if (i < pre) + printf_P(PSTR(".. ")); + else + printf_P(PSTR("%.2x "), buf[i-pre]); + } + /* 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-pre]) ? buf[i-pre] : '.'); + putchar('\n'); + + address += llen - pre; + offset += 16; + pre = 0; + len -= llen; + + if (ctrlc()) + return -1; + } + return 0; +} + +void dump_eep(uint32_t addr, unsigned int len, char *title) +{ + dump_mem(addr, addr, len, eeprom_read_buf, title); +} + +void dump_ram(uint8_t *addr, size_t offset, unsigned int len, char *title) +{ + dump_mem((uint32_t) (size_t) addr, offset, len, ram_read_buf, title); +} |