]> cloudbase.mooo.com Git - z180-stamp.git/blob - avr/debug.c
Set __malloc_margin, Print free heap command
[z180-stamp.git] / avr / debug.c
1 /*
2 * (C) Copyright 2014,2016 Leo C. <erbl259-lmu@yahoo.de>
3 *
4 * SPDX-License-Identifier: GPL-2.0
5 */
6
7 #include "debug.h"
8 #include "common.h"
9 #include <stdlib.h> /* __malloc_margin */
10 #include <string.h>
11 #include <ctype.h>
12 #include <avr/eeprom.h>
13
14 #include "command.h"
15 #include "cli_readline.h"
16 #include "eval_arg.h"
17 #include "print-utils.h"
18
19 /*
20 * Debugging
21 */
22
23 #ifdef DEBUG
24
25
26 #if 0
27 void dump_heap(void)
28 {
29 extern unsigned int __brkval;
30
31 dump_ram(__malloc_heap_start,
32 __brkval - (unsigned int) __malloc_heap_start,
33 "=== Heap:");
34 }
35 #endif
36
37
38 /*
39 * Memory Display
40 * md addr {len}
41 */
42 command_ret_t do_dump_mem(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
43 {
44 int (*readwhat)(uint8_t *buf, uint32_t addr, uint8_t count);
45
46 (void) cmdtp; (void) flag;
47
48 if (argc < 2)
49 return CMD_RET_USAGE;
50
51 uint32_t addr;
52 uint32_t length = 128;
53
54 switch (argv[0][3]) {
55 case 'r':
56 readwhat = ram_read_buf;
57 break;
58 case 'e':
59 readwhat = eeprom_read_buf;
60 break;
61 case 'f':
62 readwhat = flash_read_buf;
63 break;
64 default:
65 return CMD_RET_USAGE;
66 }
67
68 /* Address is specified since argc > 1 */
69 addr = eval_arg(argv[1], NULL);
70
71 /* If another parameter, it is the length to display. */
72 if (argc > 2)
73 length = (uint16_t) eval_arg(argv[2], NULL);
74
75 /* Print the lines. */
76 dump_mem(addr, addr, length, readwhat, NULL);
77
78 return CMD_RET_SUCCESS;
79 }
80
81 command_ret_t do_eep_cp(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
82 {
83 uint16_t src, dest, count;
84 int_fast8_t step;
85
86 (void) cmdtp;
87 (void) flag;
88
89 if (argc != 4)
90 return CMD_RET_USAGE;
91
92 src = (size_t) eval_arg(argv[1], NULL);
93 dest = (size_t) eval_arg(argv[2], NULL);
94 count = (size_t) eval_arg(argv[3], NULL);
95
96 if (src > E2END) {
97 debug("src > EEPROM size: 0x%04x\n", src);
98 return CMD_RET_FAILURE;
99 }
100 if (dest > E2END) {
101 debug("dest > EEPROM size: 0x%04x\n", dest);
102 return CMD_RET_FAILURE;
103 }
104 if (count > E2END+1) {
105 debug("count > EEPROM size: 0x%04x\n", count);
106 return CMD_RET_FAILURE;
107 }
108 if (count == 0) {
109 debug("Zero length?\n");
110 return CMD_RET_FAILURE;
111 }
112
113 if (dest > src) {
114 src += count - 1;
115 dest += count - 1;
116 step = -1;
117 } else
118 step = 1;
119
120 while (count-- > 0) {
121 uint8_t data;
122 data = eeprom_read_byte((uint8_t *) src);
123 eeprom_write_byte((uint8_t *) dest, data);
124 src += step;
125 dest += step;
126
127 }
128 return CMD_RET_SUCCESS;
129 }
130
131
132 /* Modify memory.
133 *
134 * Syntax:
135 * !mm {addr}
136 * !nm {addr}
137 */
138
139 static uint8_t *mm_last_addr;
140
141 static command_ret_t
142 mod_mem_avr(cmd_tbl_t *cmdtp, int incrflag, int flag, int argc, char * const argv[])
143 {
144 uint8_t *addr;
145 int nbytes;
146
147 (void) cmdtp;
148
149 if (argc != 2)
150 return CMD_RET_USAGE;
151
152 /* We use the last specified parameters, unless new ones are
153 * entered.
154 */
155 addr = mm_last_addr;
156
157 if ((flag & CMD_FLAG_REPEAT) == 0) {
158 /* New command specified.
159 */
160
161 /* Address is specified since argc > 1
162 */
163 addr = (uint8_t *) (size_t) eval_arg(argv[1], NULL);
164 }
165
166 /* Print the address, followed by value. Then accept input for
167 * the next value. A non-converted value exits.
168 */
169 do {
170 uint8_t data = *addr;
171 printf_P(PSTR("%04x: %02x"), addr, data);
172
173 nbytes = cli_readline(PSTR(" ? "), 0);
174 if (nbytes == 0 || (nbytes == 1 && console_buffer[0] == '-')) {
175 /* <CR> pressed as only input, don't modify current
176 * location and move to next. "-" pressed will go back.
177 */
178 if (incrflag)
179 addr += nbytes ? -1 : 1;
180 nbytes = 1;
181
182 } else {
183 char *endp;
184 data = eval_arg(console_buffer, &endp);
185 nbytes = endp - console_buffer;
186 if (nbytes) {
187 *addr = data;
188 if (incrflag)
189 addr++;
190 }
191 }
192 } while (nbytes > 0);
193
194 mm_last_addr = addr;
195 return CMD_RET_SUCCESS;
196 }
197
198
199 command_ret_t do_mem_mm_avr(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
200 {
201 return mod_mem_avr (cmdtp, 1, flag, argc, argv);
202 }
203 command_ret_t do_mem_nm_avr(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
204 {
205 return mod_mem_avr (cmdtp, 0, flag, argc, argv);
206 }
207
208 /*------------------------------------------------------------------------------*/
209
210 struct __freelist {
211 size_t sz;
212 struct __freelist *nx;
213 };
214
215 extern char *__brkval; /* first location not yet allocated */
216 extern struct __freelist *__flp; /* freelist pointer (head of freelist) */
217
218 #define STACK_POINTER() ((char *)AVR_STACK_POINTER_REG)
219
220 void
221 printfreelist(const char * title)
222 {
223 struct __freelist *fp1;
224 int i;
225 unsigned int freesum = 0;
226
227 /* TODO: printf_P */
228
229 if (!__flp) {
230 printf("%s no free list\n", title ? title : "");
231 } else {
232 printf("Free list: %s\n", title ? title : "");
233 for (i = 0, fp1 = __flp; fp1; i++, fp1 = fp1->nx) {
234 printf(" entry %d @ %04x: size %4u, next ",
235 i, (size_t)fp1, fp1->sz);
236 if (fp1->nx)
237 printf("%04x\n", (size_t)fp1->nx);
238 else
239 printf("NULL\n");
240 freesum += fp1->sz;
241 }
242 }
243
244 freesum += (size_t) STACK_POINTER() - __malloc_margin - (size_t) __brkval;
245
246 printf("SP: %04x, __brkval: %04x, Total free: %04u\n",
247 (size_t) STACK_POINTER(), (size_t) __brkval, freesum);
248 }
249
250 command_ret_t do_pr_free_avr(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
251 {
252 printfreelist(NULL);
253
254 return CMD_RET_SUCCESS;
255 }
256
257 #endif /* DEBUG */