]>
Commit | Line | Data |
---|---|---|
35edb766 L |
1 | /* |
2 | * (C) Copyright 2014 Leo C. <erbl259-lmu@yahoo.de> | |
3 | * | |
4 | * SPDX-License-Identifier: GPL-2.0+ | |
5 | */ | |
6 | ||
92b46605 L |
7 | #include "common.h" |
8 | #include <stdlib.h> | |
f338df2a | 9 | #include <string.h> |
92b46605 L |
10 | #include <ctype.h> |
11 | #include <avr/eeprom.h> | |
12 | ||
13 | #include "command.h" | |
e39cd2a2 | 14 | #include "print-utils.h" |
92b46605 | 15 | #include "debug.h" |
35edb766 | 16 | |
92b46605 L |
17 | /* |
18 | * Debugging | |
19 | */ | |
e39cd2a2 | 20 | |
92b46605 L |
21 | #ifdef DEBUG |
22 | ||
92b46605 | 23 | |
f338df2a | 24 | #if 0 |
92b46605 L |
25 | void dump_heap(void) |
26 | { | |
27 | extern unsigned int __brkval; | |
28 | ||
e39cd2a2 | 29 | dump_ram(__malloc_heap_start, |
92b46605 L |
30 | __brkval - (unsigned int) __malloc_heap_start, |
31 | "=== Heap:"); | |
32 | } | |
f338df2a | 33 | #endif |
92b46605 | 34 | |
92b46605 L |
35 | |
36 | /* | |
61b0cfe9 | 37 | * Memory Display |
92b46605 L |
38 | * md addr {len} |
39 | */ | |
d0581f88 | 40 | command_ret_t do_dump_mem(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[]) |
92b46605 | 41 | { |
e39cd2a2 | 42 | void (*readhow)(uint8_t *buf, uint32_t addr, uint8_t count); |
507d25e2 | 43 | |
f338df2a | 44 | (void) cmdtp; (void) flag; |
92b46605 L |
45 | |
46 | if (argc < 2) | |
47 | return CMD_RET_USAGE; | |
48 | ||
e39cd2a2 L |
49 | uint32_t addr; |
50 | uint32_t length = 128; | |
507d25e2 | 51 | |
f338df2a | 52 | if (strchr(argv[0],'r') != NULL) |
e39cd2a2 | 53 | readhow = ram_read_buf; |
f338df2a | 54 | else if (strchr(argv[0],'e') != NULL) |
e39cd2a2 | 55 | readhow = eeprom_read_buf; |
f338df2a L |
56 | else |
57 | return CMD_RET_USAGE; | |
92b46605 | 58 | |
f338df2a | 59 | /* Address is specified since argc > 1 */ |
e39cd2a2 | 60 | addr = strtoul(argv[1], NULL, 16); |
f338df2a L |
61 | |
62 | /* If another parameter, it is the length to display. */ | |
63 | if (argc > 2) | |
64 | length = (uint16_t) strtoul(argv[2], NULL, 16); | |
92b46605 L |
65 | |
66 | /* Print the lines. */ | |
fc454c83 | 67 | dump_mem(addr, addr, length, readhow, NULL); |
92b46605 | 68 | |
d0581f88 | 69 | return CMD_RET_SUCCESS; |
92b46605 L |
70 | } |
71 | ||
d0581f88 | 72 | command_ret_t do_eep_cp(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[]) |
92b46605 L |
73 | { |
74 | uint16_t src, dest, count; | |
75 | int_fast8_t step; | |
76 | ||
77 | (void) cmdtp; | |
78 | (void) flag; | |
79 | ||
80 | if (argc != 4) | |
81 | return CMD_RET_USAGE; | |
82 | ||
83 | src = (size_t) strtoul(argv[1], NULL, 16); | |
84 | dest = (size_t) strtoul(argv[2], NULL, 16); | |
85 | count = (size_t) strtoul(argv[3], NULL, 16); | |
86 | ||
87 | if (src > E2END) { | |
88 | debug("src > EEPROM size: 0x%04x\n", src); | |
d0581f88 | 89 | return CMD_RET_FAILURE; |
92b46605 L |
90 | } |
91 | if (dest > E2END) { | |
92 | debug("dest > EEPROM size: 0x%04x\n", dest); | |
d0581f88 | 93 | return CMD_RET_FAILURE; |
92b46605 L |
94 | } |
95 | if (count > E2END+1) { | |
96 | debug("count > EEPROM size: 0x%04x\n", count); | |
d0581f88 | 97 | return CMD_RET_FAILURE; |
92b46605 L |
98 | } |
99 | if (count == 0) { | |
69988dc1 | 100 | debug("Zero length?\n"); |
d0581f88 | 101 | return CMD_RET_FAILURE; |
92b46605 L |
102 | } |
103 | ||
104 | if (dest > src) { | |
105 | src += count - 1; | |
106 | dest += count - 1; | |
107 | step = -1; | |
108 | } else | |
109 | step = 1; | |
110 | ||
111 | while (count-- > 0) { | |
112 | uint8_t data; | |
113 | data = eeprom_read_byte((uint8_t *) src); | |
114 | eeprom_write_byte((uint8_t *) dest, data); | |
115 | src += step; | |
116 | dest += step; | |
117 | ||
118 | } | |
d0581f88 | 119 | return CMD_RET_SUCCESS; |
92b46605 | 120 | } |
69988dc1 | 121 | |
4565be9a L |
122 | |
123 | /*------------------------------------------------------------------------------*/ | |
124 | ||
507d25e2 | 125 | #if 1 |
dea9a315 | 126 | |
69988dc1 L |
127 | struct __freelist { |
128 | size_t sz; | |
129 | struct __freelist *nx; | |
130 | }; | |
131 | ||
132 | extern char *__brkval; /* first location not yet allocated */ | |
133 | extern struct __freelist *__flp; /* freelist pointer (head of freelist) */ | |
134 | ||
135 | #define STACK_POINTER() ((char *)AVR_STACK_POINTER_REG) | |
136 | ||
137 | void | |
138 | printfreelist(const char * title) | |
139 | { | |
140 | struct __freelist *fp1; | |
141 | int i; | |
142 | unsigned int freesum = 0; | |
143 | ||
507d25e2 L |
144 | /* TODO: printf_P */ |
145 | ||
69988dc1 L |
146 | if (!__flp) { |
147 | printf("%s no free list\n", title ? title : ""); | |
148 | } else { | |
149 | printf("Free list: %s\n", title ? title : ""); | |
150 | for (i = 0, fp1 = __flp; fp1; i++, fp1 = fp1->nx) { | |
151 | printf(" entry %d @ %04x: size %4u, next ", | |
152 | i, (size_t)fp1, fp1->sz); | |
153 | if (fp1->nx) | |
154 | printf("%04x\n", (size_t)fp1->nx); | |
155 | else | |
156 | printf("NULL\n"); | |
157 | freesum += fp1->sz; | |
158 | } | |
159 | } | |
507d25e2 | 160 | |
69988dc1 L |
161 | freesum += (size_t) STACK_POINTER() - __malloc_margin - (size_t) __brkval; |
162 | ||
163 | printf("SP: %04x, __brkval: %04x, Total free: %04u\n", | |
164 | (size_t) STACK_POINTER(), (size_t) __brkval, freesum); | |
165 | } | |
166 | ||
dea9a315 | 167 | #endif |
69988dc1 | 168 | |
92b46605 | 169 | #endif /* DEBUG */ |