]> cloudbase.mooo.com Git - z180-stamp.git/blob - avr/debug.c
Merge branch 'cli-quote'
[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 command_ret_t do_testarg(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
119 {
120 my_puts_P(cmdtp->name);
121 printf_P(PSTR("\n%s\n"), argv[0]);
122
123 return CMD_RET_SUCCESS;
124 }
125
126 /*------------------------------------------------------------------------------*/
127
128
129 #if 1
130
131 struct __freelist {
132 size_t sz;
133 struct __freelist *nx;
134 };
135
136 extern char *__brkval; /* first location not yet allocated */
137 extern struct __freelist *__flp; /* freelist pointer (head of freelist) */
138
139 #define STACK_POINTER() ((char *)AVR_STACK_POINTER_REG)
140
141 void
142 printfreelist(const char * title)
143 {
144 struct __freelist *fp1;
145 int i;
146 unsigned int freesum = 0;
147
148 /* TODO: printf_P */
149
150 if (!__flp) {
151 printf("%s no free list\n", title ? title : "");
152 } else {
153 printf("Free list: %s\n", title ? title : "");
154 for (i = 0, fp1 = __flp; fp1; i++, fp1 = fp1->nx) {
155 printf(" entry %d @ %04x: size %4u, next ",
156 i, (size_t)fp1, fp1->sz);
157 if (fp1->nx)
158 printf("%04x\n", (size_t)fp1->nx);
159 else
160 printf("NULL\n");
161 freesum += fp1->sz;
162 }
163 }
164
165 freesum += (size_t) STACK_POINTER() - __malloc_margin - (size_t) __brkval;
166
167 printf("SP: %04x, __brkval: %04x, Total free: %04u\n",
168 (size_t) STACK_POINTER(), (size_t) __brkval, freesum);
169 }
170
171 #endif
172
173 #endif /* DEBUG */