]>
Commit | Line | Data |
---|---|---|
92b46605 L |
1 | #include "common.h" |
2 | #include <stdlib.h> | |
f338df2a | 3 | #include <string.h> |
92b46605 L |
4 | #include <ctype.h> |
5 | #include <avr/eeprom.h> | |
6 | ||
7 | #include "command.h" | |
e39cd2a2 | 8 | #include "print-utils.h" |
92b46605 | 9 | #include "debug.h" |
92b46605 L |
10 | /* |
11 | * Debugging | |
12 | */ | |
e39cd2a2 | 13 | |
92b46605 L |
14 | #ifdef DEBUG |
15 | ||
92b46605 | 16 | |
e39cd2a2 | 17 | void eeprom_read_buf(uint8_t *buf, uint32_t addr, uint8_t count) |
f338df2a | 18 | { |
e39cd2a2 | 19 | eeprom_read_block((void *) buf, (const void *) (size_t) addr, count); |
f338df2a | 20 | } |
92b46605 | 21 | |
e39cd2a2 | 22 | void ram_read_buf(uint8_t *buf, uint32_t addr, uint8_t count) |
f338df2a | 23 | { |
e39cd2a2 L |
24 | while (count--) |
25 | *buf++ = *(uint8_t *) (size_t) addr++; | |
f338df2a L |
26 | } |
27 | ||
e39cd2a2 L |
28 | |
29 | void dump_eep(uint32_t addr, unsigned int len, char *title) | |
92b46605 | 30 | { |
e39cd2a2 | 31 | dump_mem(addr, len, eeprom_read_buf, title); |
61b0cfe9 | 32 | } |
92b46605 | 33 | |
e39cd2a2 | 34 | void dump_ram(uint32_t addr, unsigned int len, char *title) |
61b0cfe9 | 35 | { |
e39cd2a2 | 36 | dump_mem(addr, len, ram_read_buf, title); |
61b0cfe9 | 37 | } |
92b46605 | 38 | |
92b46605 | 39 | |
f338df2a | 40 | #if 0 |
92b46605 L |
41 | void dump_heap(void) |
42 | { | |
43 | extern unsigned int __brkval; | |
44 | ||
e39cd2a2 | 45 | dump_ram(__malloc_heap_start, |
92b46605 L |
46 | __brkval - (unsigned int) __malloc_heap_start, |
47 | "=== Heap:"); | |
48 | } | |
f338df2a | 49 | #endif |
92b46605 | 50 | |
92b46605 L |
51 | |
52 | /* | |
61b0cfe9 | 53 | * Memory Display |
92b46605 L |
54 | * md addr {len} |
55 | */ | |
d0581f88 | 56 | command_ret_t do_dump_mem(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[]) |
92b46605 | 57 | { |
e39cd2a2 | 58 | void (*readhow)(uint8_t *buf, uint32_t addr, uint8_t count); |
507d25e2 | 59 | |
f338df2a | 60 | (void) cmdtp; (void) flag; |
92b46605 L |
61 | |
62 | if (argc < 2) | |
63 | return CMD_RET_USAGE; | |
64 | ||
e39cd2a2 L |
65 | uint32_t addr; |
66 | uint32_t length = 128; | |
507d25e2 | 67 | |
f338df2a | 68 | if (strchr(argv[0],'r') != NULL) |
e39cd2a2 | 69 | readhow = ram_read_buf; |
f338df2a | 70 | else if (strchr(argv[0],'e') != NULL) |
e39cd2a2 | 71 | readhow = eeprom_read_buf; |
f338df2a L |
72 | else |
73 | return CMD_RET_USAGE; | |
92b46605 | 74 | |
f338df2a | 75 | /* Address is specified since argc > 1 */ |
e39cd2a2 | 76 | addr = strtoul(argv[1], NULL, 16); |
f338df2a L |
77 | |
78 | /* If another parameter, it is the length to display. */ | |
79 | if (argc > 2) | |
80 | length = (uint16_t) strtoul(argv[2], NULL, 16); | |
92b46605 L |
81 | |
82 | /* Print the lines. */ | |
f338df2a | 83 | dump_mem(addr, length, readhow, NULL); |
92b46605 | 84 | |
d0581f88 | 85 | return CMD_RET_SUCCESS; |
92b46605 L |
86 | } |
87 | ||
d0581f88 | 88 | command_ret_t do_eep_cp(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[]) |
92b46605 L |
89 | { |
90 | uint16_t src, dest, count; | |
91 | int_fast8_t step; | |
92 | ||
93 | (void) cmdtp; | |
94 | (void) flag; | |
95 | ||
96 | if (argc != 4) | |
97 | return CMD_RET_USAGE; | |
98 | ||
99 | src = (size_t) strtoul(argv[1], NULL, 16); | |
100 | dest = (size_t) strtoul(argv[2], NULL, 16); | |
101 | count = (size_t) strtoul(argv[3], NULL, 16); | |
102 | ||
103 | if (src > E2END) { | |
104 | debug("src > EEPROM size: 0x%04x\n", src); | |
d0581f88 | 105 | return CMD_RET_FAILURE; |
92b46605 L |
106 | } |
107 | if (dest > E2END) { | |
108 | debug("dest > EEPROM size: 0x%04x\n", dest); | |
d0581f88 | 109 | return CMD_RET_FAILURE; |
92b46605 L |
110 | } |
111 | if (count > E2END+1) { | |
112 | debug("count > EEPROM size: 0x%04x\n", count); | |
d0581f88 | 113 | return CMD_RET_FAILURE; |
92b46605 L |
114 | } |
115 | if (count == 0) { | |
69988dc1 | 116 | debug("Zero length?\n"); |
d0581f88 | 117 | return CMD_RET_FAILURE; |
92b46605 L |
118 | } |
119 | ||
120 | if (dest > src) { | |
121 | src += count - 1; | |
122 | dest += count - 1; | |
123 | step = -1; | |
124 | } else | |
125 | step = 1; | |
126 | ||
127 | while (count-- > 0) { | |
128 | uint8_t data; | |
129 | data = eeprom_read_byte((uint8_t *) src); | |
130 | eeprom_write_byte((uint8_t *) dest, data); | |
131 | src += step; | |
132 | dest += step; | |
133 | ||
134 | } | |
d0581f88 | 135 | return CMD_RET_SUCCESS; |
92b46605 | 136 | } |
69988dc1 L |
137 | |
138 | /*------------------------------------------------------------------------------*/ | |
139 | ||
140 | ||
507d25e2 | 141 | #if 1 |
dea9a315 | 142 | |
69988dc1 L |
143 | struct __freelist { |
144 | size_t sz; | |
145 | struct __freelist *nx; | |
146 | }; | |
147 | ||
148 | extern char *__brkval; /* first location not yet allocated */ | |
149 | extern struct __freelist *__flp; /* freelist pointer (head of freelist) */ | |
150 | ||
151 | #define STACK_POINTER() ((char *)AVR_STACK_POINTER_REG) | |
152 | ||
153 | void | |
154 | printfreelist(const char * title) | |
155 | { | |
156 | struct __freelist *fp1; | |
157 | int i; | |
158 | unsigned int freesum = 0; | |
159 | ||
507d25e2 L |
160 | /* TODO: printf_P */ |
161 | ||
69988dc1 L |
162 | if (!__flp) { |
163 | printf("%s no free list\n", title ? title : ""); | |
164 | } else { | |
165 | printf("Free list: %s\n", title ? title : ""); | |
166 | for (i = 0, fp1 = __flp; fp1; i++, fp1 = fp1->nx) { | |
167 | printf(" entry %d @ %04x: size %4u, next ", | |
168 | i, (size_t)fp1, fp1->sz); | |
169 | if (fp1->nx) | |
170 | printf("%04x\n", (size_t)fp1->nx); | |
171 | else | |
172 | printf("NULL\n"); | |
173 | freesum += fp1->sz; | |
174 | } | |
175 | } | |
507d25e2 | 176 | |
69988dc1 L |
177 | freesum += (size_t) STACK_POINTER() - __malloc_margin - (size_t) __brkval; |
178 | ||
179 | printf("SP: %04x, __brkval: %04x, Total free: %04u\n", | |
180 | (size_t) STACK_POINTER(), (size_t) __brkval, freesum); | |
181 | } | |
182 | ||
dea9a315 | 183 | #endif |
69988dc1 | 184 | |
92b46605 | 185 | #endif /* DEBUG */ |