]> cloudbase.mooo.com Git - z180-stamp.git/blob - avr/debug.c
27b5329d5defdf39f9ffc8dc5e56d79f28fc3e25
[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 #if 0
18 void dump_heap(void)
19 {
20 extern unsigned int __brkval;
21
22 dump_ram(__malloc_heap_start,
23 __brkval - (unsigned int) __malloc_heap_start,
24 "=== Heap:");
25 }
26 #endif
27
28
29 /*
30 * Memory Display
31 * md addr {len}
32 */
33 command_ret_t do_dump_mem(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
34 {
35 void (*readhow)(uint8_t *buf, uint32_t addr, uint8_t count);
36
37 (void) cmdtp; (void) flag;
38
39 if (argc < 2)
40 return CMD_RET_USAGE;
41
42 uint32_t addr;
43 uint32_t length = 128;
44
45 if (strchr(argv[0],'r') != NULL)
46 readhow = ram_read_buf;
47 else if (strchr(argv[0],'e') != NULL)
48 readhow = eeprom_read_buf;
49 else
50 return CMD_RET_USAGE;
51
52 /* Address is specified since argc > 1 */
53 addr = strtoul(argv[1], NULL, 16);
54
55 /* If another parameter, it is the length to display. */
56 if (argc > 2)
57 length = (uint16_t) strtoul(argv[2], NULL, 16);
58
59 /* Print the lines. */
60 dump_mem(addr, addr, length, readhow, NULL);
61
62 return CMD_RET_SUCCESS;
63 }
64
65 command_ret_t do_eep_cp(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
66 {
67 uint16_t src, dest, count;
68 int_fast8_t step;
69
70 (void) cmdtp;
71 (void) flag;
72
73 if (argc != 4)
74 return CMD_RET_USAGE;
75
76 src = (size_t) strtoul(argv[1], NULL, 16);
77 dest = (size_t) strtoul(argv[2], NULL, 16);
78 count = (size_t) strtoul(argv[3], NULL, 16);
79
80 if (src > E2END) {
81 debug("src > EEPROM size: 0x%04x\n", src);
82 return CMD_RET_FAILURE;
83 }
84 if (dest > E2END) {
85 debug("dest > EEPROM size: 0x%04x\n", dest);
86 return CMD_RET_FAILURE;
87 }
88 if (count > E2END+1) {
89 debug("count > EEPROM size: 0x%04x\n", count);
90 return CMD_RET_FAILURE;
91 }
92 if (count == 0) {
93 debug("Zero length?\n");
94 return CMD_RET_FAILURE;
95 }
96
97 if (dest > src) {
98 src += count - 1;
99 dest += count - 1;
100 step = -1;
101 } else
102 step = 1;
103
104 while (count-- > 0) {
105 uint8_t data;
106 data = eeprom_read_byte((uint8_t *) src);
107 eeprom_write_byte((uint8_t *) dest, data);
108 src += step;
109 dest += step;
110
111 }
112 return CMD_RET_SUCCESS;
113 }
114
115 /*------------------------------------------------------------------------------*/
116
117
118 #if 1
119
120 struct __freelist {
121 size_t sz;
122 struct __freelist *nx;
123 };
124
125 extern char *__brkval; /* first location not yet allocated */
126 extern struct __freelist *__flp; /* freelist pointer (head of freelist) */
127
128 #define STACK_POINTER() ((char *)AVR_STACK_POINTER_REG)
129
130 void
131 printfreelist(const char * title)
132 {
133 struct __freelist *fp1;
134 int i;
135 unsigned int freesum = 0;
136
137 /* TODO: printf_P */
138
139 if (!__flp) {
140 printf("%s no free list\n", title ? title : "");
141 } else {
142 printf("Free list: %s\n", title ? title : "");
143 for (i = 0, fp1 = __flp; fp1; i++, fp1 = fp1->nx) {
144 printf(" entry %d @ %04x: size %4u, next ",
145 i, (size_t)fp1, fp1->sz);
146 if (fp1->nx)
147 printf("%04x\n", (size_t)fp1->nx);
148 else
149 printf("NULL\n");
150 freesum += fp1->sz;
151 }
152 }
153
154 freesum += (size_t) STACK_POINTER() - __malloc_margin - (size_t) __brkval;
155
156 printf("SP: %04x, __brkval: %04x, Total free: %04u\n",
157 (size_t) STACK_POINTER(), (size_t) __brkval, freesum);
158 }
159
160 #endif
161
162 #endif /* DEBUG */