]>
Commit | Line | Data |
---|---|---|
92b46605 L |
1 | #include "common.h" |
2 | #include <stdlib.h> | |
3 | #include <ctype.h> | |
4 | #include <avr/eeprom.h> | |
5 | ||
6 | #include "command.h" | |
7 | #include "debug.h" | |
8 | ||
9 | /* | |
10 | * Debugging | |
11 | */ | |
12 | #ifdef DEBUG | |
13 | ||
14 | ||
15 | static void print_blanks(uint_fast8_t count) | |
16 | { | |
17 | while(count--) | |
18 | putchar(' '); | |
19 | } | |
20 | ||
21 | ||
22 | void dump_ram(const uint8_t *startaddr, int len, char *title) | |
23 | { | |
24 | uint8_t llen = 16; | |
25 | uint8_t pre = (size_t) startaddr % 16; | |
26 | const uint8_t *addr = (uint8_t *) ((size_t) startaddr & ~0x0f); | |
27 | len += pre; | |
28 | uint8_t i; | |
29 | ||
30 | if (title && *title) | |
31 | printf_P(PSTR("%s\n"),title); | |
32 | ||
33 | while (len) { | |
34 | if (len < 16) | |
35 | llen = len; | |
36 | ||
37 | printf_P(PSTR(" %.4x:"), (size_t) addr); | |
38 | print_blanks(3 * pre); | |
39 | for (i = pre; i < llen; i++) | |
40 | printf_P(PSTR(" %.2x"), addr[i]); | |
41 | print_blanks(3 * (16 - i + 1) + pre); | |
42 | for (i = pre; i < llen; i++) | |
43 | printf_P(PSTR("%c"), isprint(addr[i]) ? addr[i] : '.'); | |
44 | putchar('\n'); | |
45 | ||
46 | pre = 0; | |
47 | addr += 16; | |
48 | len -= llen; | |
49 | } | |
50 | } | |
51 | ||
52 | void dump_heap(void) | |
53 | { | |
54 | extern unsigned int __brkval; | |
55 | ||
56 | dump_ram((uint8_t *) __malloc_heap_start, | |
57 | __brkval - (unsigned int) __malloc_heap_start, | |
58 | "=== Heap:"); | |
59 | } | |
60 | ||
61 | /* TODO: combine with dump_ram() */ | |
62 | void dump_eep(const uint8_t *addr, unsigned int len) | |
63 | { | |
64 | uint_fast8_t i; | |
65 | uint8_t buf[16]; | |
66 | ||
67 | printf_P(PSTR("eeprom dump:")); | |
68 | while (len) { | |
69 | printf_P(PSTR("\n 0x%.4x:"), (unsigned int) addr); | |
70 | for (i = 0; i<16; i++) | |
71 | buf[i] = eeprom_read_byte(addr + i); | |
72 | for (i = 0; i<16; i++) | |
73 | printf_P(PSTR(" %.2x"), buf[i]); | |
74 | printf_P(PSTR(" ")); | |
75 | for (i = 0; i<16; i++) | |
76 | printf_P(PSTR("%c"), isprint(buf[i]) ? buf[i] : '.'); | |
77 | ||
78 | addr += 16; | |
79 | len -= len > 16 ? 16 : len; | |
80 | } | |
81 | putchar('\n'); | |
82 | } | |
83 | ||
84 | /* | |
85 | * EEPROM Display | |
86 | * md addr {len} | |
87 | */ | |
88 | int do_dump_eep(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[]) | |
89 | { | |
90 | const uint8_t *addr; | |
91 | uint16_t length; | |
92 | ||
93 | (void) cmdtp; | |
94 | ||
95 | ||
96 | /* We use the last specified parameters, unless new ones are | |
97 | * entered. | |
98 | */ | |
99 | addr = 0; | |
100 | length = 128; | |
101 | ||
102 | if (argc < 2) | |
103 | return CMD_RET_USAGE; | |
104 | ||
105 | if ((flag & CMD_FLAG_REPEAT) == 0) { | |
106 | /* Address is specified since argc > 1 */ | |
107 | addr = (const uint8_t *) (size_t) strtoul(argv[1], NULL, 16); | |
108 | ||
109 | /* If another parameter, it is the length to display. */ | |
110 | if (argc > 2) | |
111 | length = (uint16_t) strtoul(argv[2], NULL, 16); | |
112 | } | |
113 | ||
114 | /* Print the lines. */ | |
115 | dump_eep(addr, length); | |
116 | ||
117 | return 0; | |
118 | } | |
119 | ||
120 | int do_eep_cp(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[]) | |
121 | { | |
122 | uint16_t src, dest, count; | |
123 | int_fast8_t step; | |
124 | ||
125 | (void) cmdtp; | |
126 | (void) flag; | |
127 | ||
128 | if (argc != 4) | |
129 | return CMD_RET_USAGE; | |
130 | ||
131 | src = (size_t) strtoul(argv[1], NULL, 16); | |
132 | dest = (size_t) strtoul(argv[2], NULL, 16); | |
133 | count = (size_t) strtoul(argv[3], NULL, 16); | |
134 | ||
135 | if (src > E2END) { | |
136 | debug("src > EEPROM size: 0x%04x\n", src); | |
137 | return 1; | |
138 | } | |
139 | if (dest > E2END) { | |
140 | debug("dest > EEPROM size: 0x%04x\n", dest); | |
141 | return 1; | |
142 | } | |
143 | if (count > E2END+1) { | |
144 | debug("count > EEPROM size: 0x%04x\n", count); | |
145 | return 1; | |
146 | } | |
147 | if (count == 0) { | |
148 | debug("Zero length ???\n"); | |
149 | return 1; | |
150 | } | |
151 | ||
152 | if (dest > src) { | |
153 | src += count - 1; | |
154 | dest += count - 1; | |
155 | step = -1; | |
156 | } else | |
157 | step = 1; | |
158 | ||
159 | while (count-- > 0) { | |
160 | uint8_t data; | |
161 | data = eeprom_read_byte((uint8_t *) src); | |
162 | eeprom_write_byte((uint8_t *) dest, data); | |
163 | src += step; | |
164 | dest += step; | |
165 | ||
166 | } | |
167 | return 0; | |
168 | } | |
169 | #endif /* DEBUG */ | |
170 |