]>
Commit | Line | Data |
---|---|---|
35edb766 L |
1 | /* |
2 | * (C) Copyright 2014 Leo C. <erbl259-lmu@yahoo.de> | |
3 | * | |
8ed66016 | 4 | * SPDX-License-Identifier: GPL-2.0 |
35edb766 L |
5 | */ |
6 | ||
8ed66016 | 7 | #include "debug.h" |
92b46605 L |
8 | #include "common.h" |
9 | #include <stdlib.h> | |
f338df2a | 10 | #include <string.h> |
92b46605 L |
11 | #include <ctype.h> |
12 | #include <avr/eeprom.h> | |
13 | ||
14 | #include "command.h" | |
ad9bc17c | 15 | #include "cli_readline.h" |
e39cd2a2 | 16 | #include "print-utils.h" |
35edb766 | 17 | |
92b46605 L |
18 | /* |
19 | * Debugging | |
20 | */ | |
e39cd2a2 | 21 | |
92b46605 L |
22 | #ifdef DEBUG |
23 | ||
92b46605 | 24 | |
f338df2a | 25 | #if 0 |
92b46605 L |
26 | void dump_heap(void) |
27 | { | |
28 | extern unsigned int __brkval; | |
29 | ||
e39cd2a2 | 30 | dump_ram(__malloc_heap_start, |
92b46605 L |
31 | __brkval - (unsigned int) __malloc_heap_start, |
32 | "=== Heap:"); | |
33 | } | |
f338df2a | 34 | #endif |
92b46605 | 35 | |
92b46605 L |
36 | |
37 | /* | |
61b0cfe9 | 38 | * Memory Display |
92b46605 L |
39 | * md addr {len} |
40 | */ | |
d0581f88 | 41 | command_ret_t do_dump_mem(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[]) |
92b46605 | 42 | { |
d9d8de47 | 43 | int (*readwhat)(uint8_t *buf, uint32_t addr, uint8_t count); |
507d25e2 | 44 | |
f338df2a | 45 | (void) cmdtp; (void) flag; |
92b46605 L |
46 | |
47 | if (argc < 2) | |
48 | return CMD_RET_USAGE; | |
49 | ||
e39cd2a2 L |
50 | uint32_t addr; |
51 | uint32_t length = 128; | |
507d25e2 | 52 | |
c748023e L |
53 | switch (argv[0][3]) { |
54 | case 'r': | |
ad9bc17c | 55 | readwhat = ram_read_buf; |
c748023e L |
56 | break; |
57 | case 'e': | |
ad9bc17c | 58 | readwhat = eeprom_read_buf; |
c748023e L |
59 | break; |
60 | case 'f': | |
ad9bc17c | 61 | readwhat = flash_read_buf; |
c748023e L |
62 | break; |
63 | default: | |
f338df2a | 64 | return CMD_RET_USAGE; |
c748023e | 65 | } |
92b46605 | 66 | |
f338df2a | 67 | /* Address is specified since argc > 1 */ |
e39cd2a2 | 68 | addr = strtoul(argv[1], NULL, 16); |
f338df2a L |
69 | |
70 | /* If another parameter, it is the length to display. */ | |
71 | if (argc > 2) | |
72 | length = (uint16_t) strtoul(argv[2], NULL, 16); | |
92b46605 L |
73 | |
74 | /* Print the lines. */ | |
ad9bc17c | 75 | dump_mem(addr, addr, length, readwhat, NULL); |
92b46605 | 76 | |
d0581f88 | 77 | return CMD_RET_SUCCESS; |
92b46605 L |
78 | } |
79 | ||
d0581f88 | 80 | command_ret_t do_eep_cp(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[]) |
92b46605 L |
81 | { |
82 | uint16_t src, dest, count; | |
83 | int_fast8_t step; | |
84 | ||
85 | (void) cmdtp; | |
86 | (void) flag; | |
87 | ||
88 | if (argc != 4) | |
89 | return CMD_RET_USAGE; | |
90 | ||
91 | src = (size_t) strtoul(argv[1], NULL, 16); | |
92 | dest = (size_t) strtoul(argv[2], NULL, 16); | |
93 | count = (size_t) strtoul(argv[3], NULL, 16); | |
94 | ||
95 | if (src > E2END) { | |
96 | debug("src > EEPROM size: 0x%04x\n", src); | |
d0581f88 | 97 | return CMD_RET_FAILURE; |
92b46605 L |
98 | } |
99 | if (dest > E2END) { | |
100 | debug("dest > EEPROM size: 0x%04x\n", dest); | |
d0581f88 | 101 | return CMD_RET_FAILURE; |
92b46605 L |
102 | } |
103 | if (count > E2END+1) { | |
104 | debug("count > EEPROM size: 0x%04x\n", count); | |
d0581f88 | 105 | return CMD_RET_FAILURE; |
92b46605 L |
106 | } |
107 | if (count == 0) { | |
69988dc1 | 108 | debug("Zero length?\n"); |
d0581f88 | 109 | return CMD_RET_FAILURE; |
92b46605 L |
110 | } |
111 | ||
112 | if (dest > src) { | |
113 | src += count - 1; | |
114 | dest += count - 1; | |
115 | step = -1; | |
116 | } else | |
117 | step = 1; | |
118 | ||
119 | while (count-- > 0) { | |
120 | uint8_t data; | |
121 | data = eeprom_read_byte((uint8_t *) src); | |
122 | eeprom_write_byte((uint8_t *) dest, data); | |
123 | src += step; | |
124 | dest += step; | |
125 | ||
126 | } | |
d0581f88 | 127 | return CMD_RET_SUCCESS; |
92b46605 | 128 | } |
69988dc1 | 129 | |
4565be9a | 130 | |
ad9bc17c L |
131 | /* Modify memory. |
132 | * | |
133 | * Syntax: | |
134 | * !mm {addr} | |
135 | * !nm {addr} | |
136 | */ | |
137 | ||
138 | static uint8_t *mm_last_addr; | |
139 | ||
140 | static command_ret_t | |
141 | mod_mem_avr(cmd_tbl_t *cmdtp, int incrflag, int flag, int argc, char * const argv[]) | |
142 | { | |
143 | uint8_t *addr; | |
144 | uint8_t data; | |
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) strtoul(argv[1], NULL, 16); | |
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 | data = *addr; | |
171 | printf_P(PSTR("%04x: %02x"), addr, data); | |
172 | ||
8ed66016 | 173 | nbytes = cli_readline(PSTR(" ? "), 0); |
ad9bc17c L |
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 = strtoul(console_buffer, &endp, 16); | |
185 | nbytes = endp - console_buffer; | |
186 | if (nbytes) { | |
187 | *addr = data; | |
188 | if (incrflag) | |
189 | addr++; | |
190 | } | |
191 | } | |
192 | } while (nbytes); | |
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 | ||
4565be9a L |
208 | /*------------------------------------------------------------------------------*/ |
209 | ||
507d25e2 | 210 | #if 1 |
dea9a315 | 211 | |
69988dc1 L |
212 | struct __freelist { |
213 | size_t sz; | |
214 | struct __freelist *nx; | |
215 | }; | |
216 | ||
217 | extern char *__brkval; /* first location not yet allocated */ | |
218 | extern struct __freelist *__flp; /* freelist pointer (head of freelist) */ | |
219 | ||
220 | #define STACK_POINTER() ((char *)AVR_STACK_POINTER_REG) | |
221 | ||
222 | void | |
223 | printfreelist(const char * title) | |
224 | { | |
225 | struct __freelist *fp1; | |
226 | int i; | |
227 | unsigned int freesum = 0; | |
228 | ||
507d25e2 L |
229 | /* TODO: printf_P */ |
230 | ||
69988dc1 L |
231 | if (!__flp) { |
232 | printf("%s no free list\n", title ? title : ""); | |
233 | } else { | |
234 | printf("Free list: %s\n", title ? title : ""); | |
235 | for (i = 0, fp1 = __flp; fp1; i++, fp1 = fp1->nx) { | |
236 | printf(" entry %d @ %04x: size %4u, next ", | |
237 | i, (size_t)fp1, fp1->sz); | |
238 | if (fp1->nx) | |
239 | printf("%04x\n", (size_t)fp1->nx); | |
240 | else | |
241 | printf("NULL\n"); | |
242 | freesum += fp1->sz; | |
243 | } | |
244 | } | |
507d25e2 | 245 | |
69988dc1 L |
246 | freesum += (size_t) STACK_POINTER() - __malloc_margin - (size_t) __brkval; |
247 | ||
248 | printf("SP: %04x, __brkval: %04x, Total free: %04u\n", | |
249 | (size_t) STACK_POINTER(), (size_t) __brkval, freesum); | |
250 | } | |
251 | ||
dea9a315 | 252 | #endif |
69988dc1 | 253 | |
92b46605 | 254 | #endif /* DEBUG */ |