]>
Commit | Line | Data |
---|---|---|
1 | /* | |
2 | * (C) Copyright 2014 Leo C. <erbl259-lmu@yahoo.de> | |
3 | * | |
4 | * SPDX-License-Identifier: GPL-2.0+ | |
5 | */ | |
6 | ||
7 | #include "common.h" | |
8 | #include <stdlib.h> | |
9 | #include <string.h> | |
10 | #include <ctype.h> | |
11 | #include <avr/eeprom.h> | |
12 | ||
13 | #include "command.h" | |
14 | #include "cli_readline.h" | |
15 | #include "print-utils.h" | |
16 | #include "debug.h" | |
17 | ||
18 | /* | |
19 | * Debugging | |
20 | */ | |
21 | ||
22 | #ifdef DEBUG | |
23 | ||
24 | ||
25 | #if 0 | |
26 | void dump_heap(void) | |
27 | { | |
28 | extern unsigned int __brkval; | |
29 | ||
30 | dump_ram(__malloc_heap_start, | |
31 | __brkval - (unsigned int) __malloc_heap_start, | |
32 | "=== Heap:"); | |
33 | } | |
34 | #endif | |
35 | ||
36 | ||
37 | /* | |
38 | * Memory Display | |
39 | * md addr {len} | |
40 | */ | |
41 | command_ret_t do_dump_mem(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[]) | |
42 | { | |
43 | void (*readwhat)(uint8_t *buf, uint32_t addr, uint8_t count); | |
44 | ||
45 | (void) cmdtp; (void) flag; | |
46 | ||
47 | if (argc < 2) | |
48 | return CMD_RET_USAGE; | |
49 | ||
50 | uint32_t addr; | |
51 | uint32_t length = 128; | |
52 | ||
53 | switch (argv[0][3]) { | |
54 | case 'r': | |
55 | readwhat = ram_read_buf; | |
56 | break; | |
57 | case 'e': | |
58 | readwhat = eeprom_read_buf; | |
59 | break; | |
60 | case 'f': | |
61 | readwhat = flash_read_buf; | |
62 | break; | |
63 | default: | |
64 | return CMD_RET_USAGE; | |
65 | } | |
66 | ||
67 | /* Address is specified since argc > 1 */ | |
68 | addr = strtoul(argv[1], NULL, 16); | |
69 | ||
70 | /* If another parameter, it is the length to display. */ | |
71 | if (argc > 2) | |
72 | length = (uint16_t) strtoul(argv[2], NULL, 16); | |
73 | ||
74 | /* Print the lines. */ | |
75 | dump_mem(addr, addr, length, readwhat, NULL); | |
76 | ||
77 | return CMD_RET_SUCCESS; | |
78 | } | |
79 | ||
80 | command_ret_t do_eep_cp(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[]) | |
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); | |
97 | return CMD_RET_FAILURE; | |
98 | } | |
99 | if (dest > E2END) { | |
100 | debug("dest > EEPROM size: 0x%04x\n", dest); | |
101 | return CMD_RET_FAILURE; | |
102 | } | |
103 | if (count > E2END+1) { | |
104 | debug("count > EEPROM size: 0x%04x\n", count); | |
105 | return CMD_RET_FAILURE; | |
106 | } | |
107 | if (count == 0) { | |
108 | debug("Zero length?\n"); | |
109 | return CMD_RET_FAILURE; | |
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 | } | |
127 | return CMD_RET_SUCCESS; | |
128 | } | |
129 | ||
130 | ||
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 | ||
173 | nbytes = cli_readline(PSTR(" ? ")); | |
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 | ||
208 | /*------------------------------------------------------------------------------*/ | |
209 | ||
210 | #if 1 | |
211 | ||
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 | ||
229 | /* TODO: printf_P */ | |
230 | ||
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 | } | |
245 | ||
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 | ||
252 | #endif | |
253 | ||
254 | #endif /* DEBUG */ |