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