]> cloudbase.mooo.com Git - z180-stamp.git/blob - avr/debug.c
One dump_mem() for all memory types (avr eeprom and ram, z180 ram)
[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 "print-utils.h"
9 #include "debug.h"
10 /*
11 * Debugging
12 */
13
14 #ifdef DEBUG
15
16
17 void eeprom_read_buf(uint8_t *buf, uint32_t addr, uint8_t count)
18 {
19 eeprom_read_block((void *) buf, (const void *) (size_t) addr, count);
20 }
21
22 void ram_read_buf(uint8_t *buf, uint32_t addr, uint8_t count)
23 {
24 while (count--)
25 *buf++ = *(uint8_t *) (size_t) addr++;
26 }
27
28
29 void dump_eep(uint32_t addr, unsigned int len, char *title)
30 {
31 dump_mem(addr, len, eeprom_read_buf, title);
32 }
33
34 void dump_ram(uint32_t addr, unsigned int len, char *title)
35 {
36 dump_mem(addr, len, ram_read_buf, title);
37 }
38
39
40 #if 0
41 void dump_heap(void)
42 {
43 extern unsigned int __brkval;
44
45 dump_ram(__malloc_heap_start,
46 __brkval - (unsigned int) __malloc_heap_start,
47 "=== Heap:");
48 }
49 #endif
50
51
52 /*
53 * Memory Display
54 * md addr {len}
55 */
56 command_ret_t do_dump_mem(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
57 {
58 void (*readhow)(uint8_t *buf, uint32_t addr, uint8_t count);
59
60 (void) cmdtp; (void) flag;
61
62 if (argc < 2)
63 return CMD_RET_USAGE;
64
65 uint32_t addr;
66 uint32_t length = 128;
67
68 if (strchr(argv[0],'r') != NULL)
69 readhow = ram_read_buf;
70 else if (strchr(argv[0],'e') != NULL)
71 readhow = eeprom_read_buf;
72 else
73 return CMD_RET_USAGE;
74
75 /* Address is specified since argc > 1 */
76 addr = strtoul(argv[1], NULL, 16);
77
78 /* If another parameter, it is the length to display. */
79 if (argc > 2)
80 length = (uint16_t) strtoul(argv[2], NULL, 16);
81
82 /* Print the lines. */
83 dump_mem(addr, length, readhow, NULL);
84
85 return CMD_RET_SUCCESS;
86 }
87
88 command_ret_t do_eep_cp(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
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);
105 return CMD_RET_FAILURE;
106 }
107 if (dest > E2END) {
108 debug("dest > EEPROM size: 0x%04x\n", dest);
109 return CMD_RET_FAILURE;
110 }
111 if (count > E2END+1) {
112 debug("count > EEPROM size: 0x%04x\n", count);
113 return CMD_RET_FAILURE;
114 }
115 if (count == 0) {
116 debug("Zero length?\n");
117 return CMD_RET_FAILURE;
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 }
135 return CMD_RET_SUCCESS;
136 }
137
138 /*------------------------------------------------------------------------------*/
139
140
141 #if 1
142
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
160 /* TODO: printf_P */
161
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 }
176
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
183 #endif
184
185 #endif /* DEBUG */