]>
Commit | Line | Data |
---|---|---|
35edb766 L |
1 | /* |
2 | * (C) Copyright 2014 Leo C. <erbl259-lmu@yahoo.de> | |
3 | * | |
4 | * SPDX-License-Identifier: GPL-2.0+ | |
5 | */ | |
6 | ||
e39cd2a2 | 7 | #include "common.h" |
c748023e | 8 | #include <stdint.h> |
8f23e84c | 9 | #include <stdio.h> |
e39cd2a2 L |
10 | #include <ctype.h> |
11 | #include "con-utils.h" | |
8f23e84c L |
12 | #include "print-utils.h" |
13 | ||
14 | void print_blanks(uint_fast8_t count) | |
15 | { | |
16 | while(count--) | |
17 | putchar(' '); | |
18 | } | |
19 | ||
20 | ||
d9d8de47 | 21 | int eeprom_read_buf(uint8_t *buf, uint32_t addr, uint8_t count) |
fc454c83 L |
22 | { |
23 | eeprom_read_block((void *) buf, (const void *) (size_t) addr, count); | |
d9d8de47 | 24 | return 0; |
fc454c83 L |
25 | } |
26 | ||
d9d8de47 | 27 | int ram_read_buf(uint8_t *buf, uint32_t addr, uint8_t count) |
fc454c83 | 28 | { |
d9d8de47 L |
29 | while (count--) |
30 | *buf++ = *(uint8_t *) (size_t) addr++; | |
31 | return 0; | |
fc454c83 L |
32 | } |
33 | ||
d9d8de47 | 34 | int flash_read_buf(uint8_t *buf, uint32_t addr, uint8_t count) |
c748023e | 35 | { |
d9d8de47 L |
36 | while (count--) |
37 | *buf++ = *(const __memx uint8_t *) (__uint24) addr++; | |
38 | return 0; | |
c748023e L |
39 | } |
40 | ||
fc454c83 | 41 | int dump_mem(uint32_t address, uint32_t offset, uint32_t len, |
d9d8de47 | 42 | int (*readfkt)(uint8_t *, uint32_t, uint8_t), char *title) |
e39cd2a2 L |
43 | { |
44 | uint8_t buf[16]; | |
45 | char *indent = NULL; | |
46 | uint8_t llen = 16; | |
fc454c83 L |
47 | uint8_t pre = offset % 16; |
48 | offset = offset & ~0x0f; | |
e39cd2a2 L |
49 | len += pre; |
50 | uint8_t i; | |
51 | ||
52 | if (title && *title) { | |
53 | printf_P(PSTR("%s\n"),title); | |
54 | indent = " "; | |
55 | } | |
56 | ||
57 | while (len) { | |
58 | if (len < 16) | |
59 | llen = len; | |
d9d8de47 L |
60 | if (readfkt(buf, address, llen - pre) != 0) |
61 | return -2; /* TODO: Error codes */ | |
e39cd2a2 | 62 | |
fc454c83 | 63 | printf_P(PSTR("%s%.5lx:"),indent, offset); |
e39cd2a2 L |
64 | for (i = 0; i < llen; i++) { |
65 | if ((i % 8) == 0) | |
66 | putchar(' '); | |
67 | if (i < pre) | |
68 | printf_P(PSTR(".. ")); | |
69 | else | |
70 | printf_P(PSTR("%.2x "), buf[i-pre]); | |
71 | } | |
72 | /* fill line with whitespace for nice ASCII print */ | |
73 | print_blanks(3 * (16u - i) + (16u-i)/8 + 1 + pre); | |
74 | /* Print data in ASCII characters */ | |
75 | for (i = pre; i < llen; i++) | |
76 | printf_P(PSTR("%c"), isprint(buf[i-pre]) ? buf[i-pre] : '.'); | |
77 | putchar('\n'); | |
78 | ||
fc454c83 L |
79 | address += llen - pre; |
80 | offset += 16; | |
e39cd2a2 | 81 | pre = 0; |
e39cd2a2 L |
82 | len -= llen; |
83 | ||
84 | if (ctrlc()) | |
85 | return -1; | |
86 | } | |
87 | return 0; | |
88 | } | |
fc454c83 L |
89 | |
90 | void dump_eep(uint32_t addr, unsigned int len, char *title) | |
91 | { | |
92 | dump_mem(addr, addr, len, eeprom_read_buf, title); | |
93 | } | |
94 | ||
cca42593 | 95 | void dump_ram(uint8_t *addr, size_t offset, unsigned int len, char *title) |
fc454c83 | 96 | { |
cca42593 | 97 | dump_mem((uint32_t) (size_t) addr, offset, len, ram_read_buf, title); |
fc454c83 | 98 | } |