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