]>
Commit | Line | Data |
---|---|---|
35edb766 | 1 | /* |
2d914b45 | 2 | * (C) Copyright 2014,2016 Leo C. <erbl259-lmu@yahoo.de> |
35edb766 | 3 | * |
8ed66016 | 4 | * SPDX-License-Identifier: GPL-2.0 |
35edb766 L |
5 | */ |
6 | ||
8ed66016 | 7 | #include "debug.h" |
92b46605 | 8 | #include "common.h" |
92b46605 L |
9 | #include <avr/eeprom.h> |
10 | ||
11 | #include "command.h" | |
ad9bc17c | 12 | #include "cli_readline.h" |
2d914b45 | 13 | #include "eval_arg.h" |
e39cd2a2 | 14 | #include "print-utils.h" |
35edb766 | 15 | |
92b46605 L |
16 | /* |
17 | * Debugging | |
18 | */ | |
e39cd2a2 | 19 | |
92b46605 L |
20 | #ifdef DEBUG |
21 | ||
92b46605 | 22 | /* |
61b0cfe9 | 23 | * Memory Display |
92b46605 L |
24 | * md addr {len} |
25 | */ | |
b35fcb2f | 26 | command_ret_t do_dump_mem(cmd_tbl_t *cmdtp UNUSED, uint_fast8_t flag UNUSED, int argc, char * const argv[]) |
92b46605 | 27 | { |
b35fcb2f | 28 | ERRNUM (*readwhat)(uint8_t *buf, uint32_t addr, uint8_t count); |
92b46605 L |
29 | |
30 | if (argc < 2) | |
31 | return CMD_RET_USAGE; | |
32 | ||
e39cd2a2 L |
33 | uint32_t addr; |
34 | uint32_t length = 128; | |
507d25e2 | 35 | |
c748023e L |
36 | switch (argv[0][3]) { |
37 | case 'r': | |
ad9bc17c | 38 | readwhat = ram_read_buf; |
c748023e L |
39 | break; |
40 | case 'e': | |
ad9bc17c | 41 | readwhat = eeprom_read_buf; |
c748023e L |
42 | break; |
43 | case 'f': | |
ad9bc17c | 44 | readwhat = flash_read_buf; |
c748023e L |
45 | break; |
46 | default: | |
f338df2a | 47 | return CMD_RET_USAGE; |
c748023e | 48 | } |
92b46605 | 49 | |
f338df2a | 50 | /* Address is specified since argc > 1 */ |
2d914b45 | 51 | addr = eval_arg(argv[1], NULL); |
f338df2a L |
52 | |
53 | /* If another parameter, it is the length to display. */ | |
54 | if (argc > 2) | |
2d914b45 | 55 | length = (uint16_t) eval_arg(argv[2], NULL); |
92b46605 L |
56 | |
57 | /* Print the lines. */ | |
ad9bc17c | 58 | dump_mem(addr, addr, length, readwhat, NULL); |
92b46605 | 59 | |
d0581f88 | 60 | return CMD_RET_SUCCESS; |
92b46605 L |
61 | } |
62 | ||
fcf1d5b3 | 63 | command_ret_t do_eep_cp(cmd_tbl_t *cmdtp, uint_fast8_t flag, int argc, char * const argv[]) |
92b46605 L |
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 | ||
2d914b45 L |
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); | |
92b46605 L |
77 | |
78 | if (src > E2END) { | |
79 | debug("src > EEPROM size: 0x%04x\n", src); | |
d0581f88 | 80 | return CMD_RET_FAILURE; |
92b46605 L |
81 | } |
82 | if (dest > E2END) { | |
83 | debug("dest > EEPROM size: 0x%04x\n", dest); | |
d0581f88 | 84 | return CMD_RET_FAILURE; |
92b46605 L |
85 | } |
86 | if (count > E2END+1) { | |
87 | debug("count > EEPROM size: 0x%04x\n", count); | |
d0581f88 | 88 | return CMD_RET_FAILURE; |
92b46605 L |
89 | } |
90 | if (count == 0) { | |
69988dc1 | 91 | debug("Zero length?\n"); |
d0581f88 | 92 | return CMD_RET_FAILURE; |
92b46605 L |
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 | } | |
d0581f88 | 110 | return CMD_RET_SUCCESS; |
92b46605 | 111 | } |
69988dc1 | 112 | |
4565be9a | 113 | |
ad9bc17c L |
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 | |
fcf1d5b3 | 124 | mod_mem_avr(cmd_tbl_t *cmdtp, int incrflag, uint_fast8_t flag, int argc, char * const argv[]) |
ad9bc17c L |
125 | { |
126 | uint8_t *addr; | |
ad9bc17c L |
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 | */ | |
2d914b45 | 145 | addr = (uint8_t *) (size_t) eval_arg(argv[1], NULL); |
ad9bc17c L |
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 { | |
fb9b17a9 | 152 | uint8_t data = *addr; |
ad9bc17c L |
153 | printf_P(PSTR("%04x: %02x"), addr, data); |
154 | ||
8ed66016 | 155 | nbytes = cli_readline(PSTR(" ? "), 0); |
ad9bc17c L |
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; | |
2d914b45 | 166 | data = eval_arg(console_buffer, &endp); |
ad9bc17c L |
167 | nbytes = endp - console_buffer; |
168 | if (nbytes) { | |
169 | *addr = data; | |
170 | if (incrflag) | |
171 | addr++; | |
172 | } | |
173 | } | |
13e88ed5 | 174 | } while (nbytes > 0); |
ad9bc17c L |
175 | |
176 | mm_last_addr = addr; | |
177 | return CMD_RET_SUCCESS; | |
178 | } | |
179 | ||
180 | ||
fcf1d5b3 | 181 | command_ret_t do_mem_mm_avr(cmd_tbl_t *cmdtp, uint_fast8_t flag, int argc, char * const argv[]) |
ad9bc17c L |
182 | { |
183 | return mod_mem_avr (cmdtp, 1, flag, argc, argv); | |
184 | } | |
fcf1d5b3 | 185 | command_ret_t do_mem_nm_avr(cmd_tbl_t *cmdtp, uint_fast8_t flag, int argc, char * const argv[]) |
ad9bc17c L |
186 | { |
187 | return mod_mem_avr (cmdtp, 0, flag, argc, argv); | |
188 | } | |
189 | ||
4565be9a L |
190 | /*------------------------------------------------------------------------------*/ |
191 | ||
69988dc1 L |
192 | struct __freelist { |
193 | size_t sz; | |
194 | struct __freelist *nx; | |
195 | }; | |
196 | ||
b9aef06e L |
197 | extern char *__brkval; /* first location not yet allocated */ |
198 | extern struct __freelist *__flp; /* freelist pointer (head of freelist) */ | |
69988dc1 L |
199 | |
200 | #define STACK_POINTER() ((char *)AVR_STACK_POINTER_REG) | |
201 | ||
b9aef06e L |
202 | size_t get_freemem(void) |
203 | { | |
204 | return (size_t) STACK_POINTER() - __malloc_margin - (size_t) __brkval; | |
205 | } | |
206 | ||
69988dc1 L |
207 | void |
208 | printfreelist(const char * title) | |
209 | { | |
210 | struct __freelist *fp1; | |
211 | int i; | |
212 | unsigned int freesum = 0; | |
213 | ||
214 | if (!__flp) { | |
d649155c | 215 | printf_P(PSTR("%s no free list\n"), title ? title : ""); |
69988dc1 | 216 | } else { |
d649155c | 217 | printf_P(PSTR("Free list: %s\n"), title ? title : ""); |
69988dc1 | 218 | for (i = 0, fp1 = __flp; fp1; i++, fp1 = fp1->nx) { |
d649155c | 219 | printf_P(PSTR(" entry %d @ %04x: size %4u, next "), |
69988dc1 L |
220 | i, (size_t)fp1, fp1->sz); |
221 | if (fp1->nx) | |
d649155c | 222 | printf_P(PSTR("%04x\n"), (size_t)fp1->nx); |
69988dc1 | 223 | else |
d649155c | 224 | printf_P(PSTR("NULL\n")); |
69988dc1 L |
225 | freesum += fp1->sz; |
226 | } | |
227 | } | |
507d25e2 | 228 | |
b9aef06e | 229 | freesum += get_freemem(); |
69988dc1 | 230 | |
d649155c | 231 | printf_P(PSTR("SP: %04x, __brkval: %04x, Total free: %04u\n"), |
69988dc1 L |
232 | (size_t) STACK_POINTER(), (size_t) __brkval, freesum); |
233 | } | |
234 | ||
fcf1d5b3 | 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) |
fb9b17a9 L |
236 | { |
237 | printfreelist(NULL); | |
238 | ||
239 | return CMD_RET_SUCCESS; | |
240 | } | |
69988dc1 | 241 | |
b9aef06e L |
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 | ||
92b46605 | 257 | #endif /* DEBUG */ |