]> cloudbase.mooo.com Git - z180-stamp.git/blame - avr/debug.c
Merge tag 'fatfs-0.10b'
[z180-stamp.git] / avr / debug.c
CommitLineData
92b46605
L
1#include "common.h"
2#include <stdlib.h>
f338df2a 3#include <string.h>
92b46605
L
4#include <ctype.h>
5#include <avr/eeprom.h>
6
7#include "command.h"
8#include "debug.h"
9
10/*
11 * Debugging
12 */
13#ifdef DEBUG
14
f338df2a 15//uint8_t eeprom_read_byte (const uint8_t *__p)
92b46605
L
16
17static void print_blanks(uint_fast8_t count)
18{
19 while(count--)
20 putchar(' ');
21}
22
f338df2a
L
23static uint8_t ram_read_byte(const uint8_t *p)
24{
25 return *p;
26}
92b46605 27
f338df2a
L
28void dump_mem(const uint8_t *startaddr, int len,
29 uint8_t (*readfkt)(const uint8_t *), char *title)
30{
31 uint8_t buf[16];
32 uint8_t llen = 16;
33 uint8_t pre = (size_t) startaddr % 16;
34 const uint8_t *addr = (uint8_t *) ((size_t) startaddr & ~0x0f);
35 len += pre;
36 uint8_t i;
37
38 if (title && *title)
39 printf_P(PSTR("%s\n"),title);
40
41 while (len) {
42 if (len < 16)
43 llen = len;
44
45 for (i = pre; i < llen; i++)
46 buf[i] = readfkt(addr + i);
47
48 printf_P(PSTR("%04x:"), addr);
49 for (i = 0; i < llen; i++) {
50 if ((i % 8) == 0)
51 putchar(' ');
52 if (i < pre)
53 printf_P(PSTR(".. "));
54 else
55 printf_P(PSTR("%.2x "), buf[i]);
56 }
57 /* fill line with whitespace for nice ASCII print */
58 print_blanks(3 * (16u - i) + (16u-i)/8 + 1 + pre);
59 /* Print data in ASCII characters */
60 for (i = pre; i < llen; i++)
61 printf_P(PSTR("%c"), isprint(buf[i]) ? buf[i] : '.');
62 putchar('\n');
63
64 pre = 0;
65 addr += 16;
66 len -= llen;
67 }
68}
69
70#if 0
92b46605
L
71void dump_ram(const uint8_t *startaddr, int len, char *title)
72{
73 uint8_t llen = 16;
74 uint8_t pre = (size_t) startaddr % 16;
75 const uint8_t *addr = (uint8_t *) ((size_t) startaddr & ~0x0f);
76 len += pre;
77 uint8_t i;
78
79 if (title && *title)
80 printf_P(PSTR("%s\n"),title);
81
82 while (len) {
83 if (len < 16)
84 llen = len;
85
86 printf_P(PSTR(" %.4x:"), (size_t) addr);
87 print_blanks(3 * pre);
88 for (i = pre; i < llen; i++)
89 printf_P(PSTR(" %.2x"), addr[i]);
90 print_blanks(3 * (16 - i + 1) + pre);
91 for (i = pre; i < llen; i++)
92 printf_P(PSTR("%c"), isprint(addr[i]) ? addr[i] : '.');
93 putchar('\n');
94
95 pre = 0;
96 addr += 16;
97 len -= llen;
98 }
99}
f338df2a
L
100#endif
101#if 0
92b46605
L
102void dump_heap(void)
103{
104 extern unsigned int __brkval;
105
106 dump_ram((uint8_t *) __malloc_heap_start,
107 __brkval - (unsigned int) __malloc_heap_start,
108 "=== Heap:");
109}
f338df2a 110#endif
92b46605 111
f338df2a 112#if 0
92b46605 113/* TODO: combine with dump_ram() */
f338df2a
L
114void dump_eep(const uint8_t *addr, unsigned int len,
115 uint8_t (*readfkt)(const uint8_t *))
92b46605
L
116{
117 uint_fast8_t i;
118 uint8_t buf[16];
119
120 printf_P(PSTR("eeprom dump:"));
121 while (len) {
122 printf_P(PSTR("\n 0x%.4x:"), (unsigned int) addr);
123 for (i = 0; i<16; i++)
f338df2a 124 buf[i] = readfkt(addr + i);
92b46605
L
125 for (i = 0; i<16; i++)
126 printf_P(PSTR(" %.2x"), buf[i]);
127 printf_P(PSTR(" "));
128 for (i = 0; i<16; i++)
129 printf_P(PSTR("%c"), isprint(buf[i]) ? buf[i] : '.');
130
131 addr += 16;
132 len -= len > 16 ? 16 : len;
133 }
134 putchar('\n');
135}
f338df2a
L
136#endif
137
92b46605
L
138
139/*
140 * EEPROM Display
141 * md addr {len}
142 */
f338df2a 143int do_dump_mem(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
92b46605 144{
f338df2a
L
145// static const uint8_t *addr;
146// static uint16_t length = 128;
147 uint8_t (*readhow)(const uint8_t *);
148
149 (void) cmdtp; (void) flag;
92b46605
L
150
151 if (argc < 2)
152 return CMD_RET_USAGE;
153
f338df2a
L
154 const uint8_t *addr;
155 uint16_t length = 128;
156
157 if (strchr(argv[0],'r') != NULL)
158 readhow = ram_read_byte;
159 else if (strchr(argv[0],'e') != NULL)
160 readhow = eeprom_read_byte;
161 else
162 return CMD_RET_USAGE;
92b46605 163
f338df2a
L
164 /* Address is specified since argc > 1 */
165 addr = (const uint8_t *) (size_t) strtoul(argv[1], NULL, 16);
166
167 /* If another parameter, it is the length to display. */
168 if (argc > 2)
169 length = (uint16_t) strtoul(argv[2], NULL, 16);
92b46605
L
170
171 /* Print the lines. */
f338df2a 172 dump_mem(addr, length, readhow, NULL);
92b46605
L
173
174 return 0;
175}
176
177int do_eep_cp(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
178{
179 uint16_t src, dest, count;
180 int_fast8_t step;
181
182 (void) cmdtp;
183 (void) flag;
184
185 if (argc != 4)
186 return CMD_RET_USAGE;
187
188 src = (size_t) strtoul(argv[1], NULL, 16);
189 dest = (size_t) strtoul(argv[2], NULL, 16);
190 count = (size_t) strtoul(argv[3], NULL, 16);
191
192 if (src > E2END) {
193 debug("src > EEPROM size: 0x%04x\n", src);
194 return 1;
195 }
196 if (dest > E2END) {
197 debug("dest > EEPROM size: 0x%04x\n", dest);
198 return 1;
199 }
200 if (count > E2END+1) {
201 debug("count > EEPROM size: 0x%04x\n", count);
202 return 1;
203 }
204 if (count == 0) {
205 debug("Zero length ???\n");
206 return 1;
207 }
208
209 if (dest > src) {
210 src += count - 1;
211 dest += count - 1;
212 step = -1;
213 } else
214 step = 1;
215
216 while (count-- > 0) {
217 uint8_t data;
218 data = eeprom_read_byte((uint8_t *) src);
219 eeprom_write_byte((uint8_t *) dest, data);
220 src += step;
221 dest += step;
222
223 }
224 return 0;
225}
226#endif /* DEBUG */
227