]> cloudbase.mooo.com Git - z180-stamp.git/blob - avr/debug.c
fcbd2c107011760654cf12a862573b4353335207
[z180-stamp.git] / avr / debug.c
1 #include "common.h"
2 #include <stdlib.h>
3 #include <string.h>
4 #include <ctype.h>
5 #include <avr/eeprom.h>
6
7 #include "command.h"
8 #include "debug.h"
9
10 /*
11 * Debugging
12 */
13 #ifdef DEBUG
14
15 //uint8_t eeprom_read_byte (const uint8_t *__p)
16
17 static void print_blanks(uint_fast8_t count)
18 {
19 while(count--)
20 putchar(' ');
21 }
22
23 static uint8_t ram_read_byte(const uint8_t *p)
24 {
25 return *p;
26 }
27
28 void dump_mem(const uint8_t *startaddr, int len,
29 uint8_t (*readfkt)(const uint8_t *), char *title)
30 {
31 uint8_t buf[16];
32 uint8_t llen = 16;
33 uint8_t pre = (size_t) startaddr % 16;
34 const uint8_t *addr = (uint8_t *) ((size_t) startaddr & ~0x0f);
35 len += pre;
36 uint8_t i;
37
38 if (title && *title)
39 printf_P(PSTR("%s\n"),title);
40
41 while (len) {
42 if (len < 16)
43 llen = len;
44
45 for (i = pre; i < llen; i++)
46 buf[i] = readfkt(addr + i);
47
48 printf_P(PSTR("%04x:"), addr);
49 for (i = 0; i < llen; i++) {
50 if ((i % 8) == 0)
51 putchar(' ');
52 if (i < pre)
53 printf_P(PSTR(".. "));
54 else
55 printf_P(PSTR("%.2x "), buf[i]);
56 }
57 /* fill line with whitespace for nice ASCII print */
58 print_blanks(3 * (16u - i) + (16u-i)/8 + 1 + pre);
59 /* Print data in ASCII characters */
60 for (i = pre; i < llen; i++)
61 printf_P(PSTR("%c"), isprint(buf[i]) ? buf[i] : '.');
62 putchar('\n');
63
64 pre = 0;
65 addr += 16;
66 len -= llen;
67 }
68 }
69
70 #if 0
71 void dump_ram(const uint8_t *startaddr, int len, char *title)
72 {
73 uint8_t llen = 16;
74 uint8_t pre = (size_t) startaddr % 16;
75 const uint8_t *addr = (uint8_t *) ((size_t) startaddr & ~0x0f);
76 len += pre;
77 uint8_t i;
78
79 if (title && *title)
80 printf_P(PSTR("%s\n"),title);
81
82 while (len) {
83 if (len < 16)
84 llen = len;
85
86 printf_P(PSTR(" %.4x:"), (size_t) addr);
87 print_blanks(3 * pre);
88 for (i = pre; i < llen; i++)
89 printf_P(PSTR(" %.2x"), addr[i]);
90 print_blanks(3 * (16 - i + 1) + pre);
91 for (i = pre; i < llen; i++)
92 printf_P(PSTR("%c"), isprint(addr[i]) ? addr[i] : '.');
93 putchar('\n');
94
95 pre = 0;
96 addr += 16;
97 len -= llen;
98 }
99 }
100 #endif
101 #if 0
102 void dump_heap(void)
103 {
104 extern unsigned int __brkval;
105
106 dump_ram((uint8_t *) __malloc_heap_start,
107 __brkval - (unsigned int) __malloc_heap_start,
108 "=== Heap:");
109 }
110 #endif
111
112 #if 0
113 /* TODO: combine with dump_ram() */
114 void dump_eep(const uint8_t *addr, unsigned int len,
115 uint8_t (*readfkt)(const uint8_t *))
116 {
117 uint_fast8_t i;
118 uint8_t buf[16];
119
120 printf_P(PSTR("eeprom dump:"));
121 while (len) {
122 printf_P(PSTR("\n 0x%.4x:"), (unsigned int) addr);
123 for (i = 0; i<16; i++)
124 buf[i] = readfkt(addr + i);
125 for (i = 0; i<16; i++)
126 printf_P(PSTR(" %.2x"), buf[i]);
127 printf_P(PSTR(" "));
128 for (i = 0; i<16; i++)
129 printf_P(PSTR("%c"), isprint(buf[i]) ? buf[i] : '.');
130
131 addr += 16;
132 len -= len > 16 ? 16 : len;
133 }
134 putchar('\n');
135 }
136 #endif
137
138
139 /*
140 * EEPROM Display
141 * md addr {len}
142 */
143 command_ret_t do_dump_mem(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
144 {
145 // static const uint8_t *addr;
146 // static uint16_t length = 128;
147 uint8_t (*readhow)(const uint8_t *);
148
149 (void) cmdtp; (void) flag;
150
151 if (argc < 2)
152 return CMD_RET_USAGE;
153
154 const uint8_t *addr;
155 uint16_t length = 128;
156
157 if (strchr(argv[0],'r') != NULL)
158 readhow = ram_read_byte;
159 else if (strchr(argv[0],'e') != NULL)
160 readhow = eeprom_read_byte;
161 else
162 return CMD_RET_USAGE;
163
164 /* Address is specified since argc > 1 */
165 addr = (const uint8_t *) (size_t) strtoul(argv[1], NULL, 16);
166
167 /* If another parameter, it is the length to display. */
168 if (argc > 2)
169 length = (uint16_t) strtoul(argv[2], NULL, 16);
170
171 /* Print the lines. */
172 dump_mem(addr, length, readhow, NULL);
173
174 return CMD_RET_SUCCESS;
175 }
176
177 command_ret_t do_eep_cp(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
178 {
179 uint16_t src, dest, count;
180 int_fast8_t step;
181
182 (void) cmdtp;
183 (void) flag;
184
185 if (argc != 4)
186 return CMD_RET_USAGE;
187
188 src = (size_t) strtoul(argv[1], NULL, 16);
189 dest = (size_t) strtoul(argv[2], NULL, 16);
190 count = (size_t) strtoul(argv[3], NULL, 16);
191
192 if (src > E2END) {
193 debug("src > EEPROM size: 0x%04x\n", src);
194 return CMD_RET_FAILURE;
195 }
196 if (dest > E2END) {
197 debug("dest > EEPROM size: 0x%04x\n", dest);
198 return CMD_RET_FAILURE;
199 }
200 if (count > E2END+1) {
201 debug("count > EEPROM size: 0x%04x\n", count);
202 return CMD_RET_FAILURE;
203 }
204 if (count == 0) {
205 debug("Zero length?\n");
206 return CMD_RET_FAILURE;
207 }
208
209 if (dest > src) {
210 src += count - 1;
211 dest += count - 1;
212 step = -1;
213 } else
214 step = 1;
215
216 while (count-- > 0) {
217 uint8_t data;
218 data = eeprom_read_byte((uint8_t *) src);
219 eeprom_write_byte((uint8_t *) dest, data);
220 src += step;
221 dest += step;
222
223 }
224 return CMD_RET_SUCCESS;
225 }
226
227 /*------------------------------------------------------------------------------*/
228
229
230 #if 0
231
232 struct __freelist {
233 size_t sz;
234 struct __freelist *nx;
235 };
236
237 extern char *__brkval; /* first location not yet allocated */
238 extern struct __freelist *__flp; /* freelist pointer (head of freelist) */
239
240 #define STACK_POINTER() ((char *)AVR_STACK_POINTER_REG)
241
242 void
243 printfreelist(const char * title)
244 {
245 struct __freelist *fp1;
246 int i;
247 unsigned int freesum = 0;
248
249 if (!__flp) {
250 printf("%s no free list\n", title ? title : "");
251 } else {
252 printf("Free list: %s\n", title ? title : "");
253 for (i = 0, fp1 = __flp; fp1; i++, fp1 = fp1->nx) {
254 printf(" entry %d @ %04x: size %4u, next ",
255 i, (size_t)fp1, fp1->sz);
256 if (fp1->nx)
257 printf("%04x\n", (size_t)fp1->nx);
258 else
259 printf("NULL\n");
260 freesum += fp1->sz;
261 }
262 }
263
264 freesum += (size_t) STACK_POINTER() - __malloc_margin - (size_t) __brkval;
265
266 printf("SP: %04x, __brkval: %04x, Total free: %04u\n",
267 (size_t) STACK_POINTER(), (size_t) __brkval, freesum);
268 }
269
270 #endif
271
272 #endif /* DEBUG */
273