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