]> cloudbase.mooo.com Git - z180-stamp.git/blob - avr/debug.c
env.c needs getopt-min.h
[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 uint8_t data;
146 int nbytes;
147
148 (void) cmdtp;
149
150 if (argc != 2)
151 return CMD_RET_USAGE;
152
153 /* We use the last specified parameters, unless new ones are
154 * entered.
155 */
156 addr = mm_last_addr;
157
158 if ((flag & CMD_FLAG_REPEAT) == 0) {
159 /* New command specified.
160 */
161
162 /* Address is specified since argc > 1
163 */
164 addr = (uint8_t *) (size_t) eval_arg(argv[1], NULL);
165 }
166
167 /* Print the address, followed by value. Then accept input for
168 * the next value. A non-converted value exits.
169 */
170 do {
171 data = *addr;
172 printf_P(PSTR("%04x: %02x"), addr, data);
173
174 nbytes = cli_readline(PSTR(" ? "), 0);
175 if (nbytes == 0 || (nbytes == 1 && console_buffer[0] == '-')) {
176 /* <CR> pressed as only input, don't modify current
177 * location and move to next. "-" pressed will go back.
178 */
179 if (incrflag)
180 addr += nbytes ? -1 : 1;
181 nbytes = 1;
182
183 } else {
184 char *endp;
185 data = eval_arg(console_buffer, &endp);
186 nbytes = endp - console_buffer;
187 if (nbytes) {
188 *addr = data;
189 if (incrflag)
190 addr++;
191 }
192 }
193 } while (nbytes > 0);
194
195 mm_last_addr = addr;
196 return CMD_RET_SUCCESS;
197 }
198
199
200 command_ret_t do_mem_mm_avr(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
201 {
202 return mod_mem_avr (cmdtp, 1, flag, argc, argv);
203 }
204 command_ret_t do_mem_nm_avr(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
205 {
206 return mod_mem_avr (cmdtp, 0, flag, argc, argv);
207 }
208
209 /*------------------------------------------------------------------------------*/
210
211 #if 1
212
213 struct __freelist {
214 size_t sz;
215 struct __freelist *nx;
216 };
217
218 extern char *__brkval; /* first location not yet allocated */
219 extern struct __freelist *__flp; /* freelist pointer (head of freelist) */
220
221 #define STACK_POINTER() ((char *)AVR_STACK_POINTER_REG)
222
223 void
224 printfreelist(const char * title)
225 {
226 struct __freelist *fp1;
227 int i;
228 unsigned int freesum = 0;
229
230 /* TODO: printf_P */
231
232 if (!__flp) {
233 printf("%s no free list\n", title ? title : "");
234 } else {
235 printf("Free list: %s\n", title ? title : "");
236 for (i = 0, fp1 = __flp; fp1; i++, fp1 = fp1->nx) {
237 printf(" entry %d @ %04x: size %4u, next ",
238 i, (size_t)fp1, fp1->sz);
239 if (fp1->nx)
240 printf("%04x\n", (size_t)fp1->nx);
241 else
242 printf("NULL\n");
243 freesum += fp1->sz;
244 }
245 }
246
247 freesum += (size_t) STACK_POINTER() - __malloc_margin - (size_t) __brkval;
248
249 printf("SP: %04x, __brkval: %04x, Total free: %04u\n",
250 (size_t) STACK_POINTER(), (size_t) __brkval, freesum);
251 }
252
253 #endif
254
255 #endif /* DEBUG */