]> cloudbase.mooo.com Git - z180-stamp.git/blame - avr/debug.c
Add date rtc i2c
[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
92b46605
L
15static void print_blanks(uint_fast8_t count)
16{
17 while(count--)
18 putchar(' ');
19}
20
f338df2a
L
21static uint8_t ram_read_byte(const uint8_t *p)
22{
23 return *p;
24}
92b46605 25
f338df2a
L
26void dump_mem(const uint8_t *startaddr, int len,
27 uint8_t (*readfkt)(const uint8_t *), char *title)
28{
29 uint8_t buf[16];
61b0cfe9 30 char *indent = NULL;
f338df2a
L
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
61b0cfe9 37 if (title && *title) {
f338df2a 38 printf_P(PSTR("%s\n"),title);
61b0cfe9
L
39 indent = " ";
40 }
f338df2a
L
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
61b0cfe9 49 printf_P(PSTR("%s%04x:"),indent, addr);
f338df2a
L
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
61b0cfe9 71void dump_eep(const uint8_t *addr, unsigned int len, char *title)
92b46605 72{
61b0cfe9
L
73 dump_mem(addr, len, eeprom_read_byte, title);
74}
92b46605 75
61b0cfe9
L
76void dump_ram(const uint8_t *addr, unsigned int len, char *title)
77{
78 dump_mem(addr, len, ram_read_byte, title);
79}
92b46605 80
92b46605 81
f338df2a 82#if 0
92b46605
L
83void 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}
f338df2a 91#endif
92b46605 92
92b46605
L
93
94/*
61b0cfe9 95 * Memory Display
92b46605
L
96 * md addr {len}
97 */
d0581f88 98command_ret_t do_dump_mem(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
92b46605 99{
f338df2a
L
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;
92b46605
L
105
106 if (argc < 2)
107 return CMD_RET_USAGE;
108
f338df2a
L
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;
92b46605 118
f338df2a
L
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);
92b46605
L
125
126 /* Print the lines. */
f338df2a 127 dump_mem(addr, length, readhow, NULL);
92b46605 128
d0581f88 129 return CMD_RET_SUCCESS;
92b46605
L
130}
131
d0581f88 132command_ret_t do_eep_cp(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
92b46605
L
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);
d0581f88 149 return CMD_RET_FAILURE;
92b46605
L
150 }
151 if (dest > E2END) {
152 debug("dest > EEPROM size: 0x%04x\n", dest);
d0581f88 153 return CMD_RET_FAILURE;
92b46605
L
154 }
155 if (count > E2END+1) {
156 debug("count > EEPROM size: 0x%04x\n", count);
d0581f88 157 return CMD_RET_FAILURE;
92b46605
L
158 }
159 if (count == 0) {
69988dc1 160 debug("Zero length?\n");
d0581f88 161 return CMD_RET_FAILURE;
92b46605
L
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 }
d0581f88 179 return CMD_RET_SUCCESS;
92b46605 180}
69988dc1
L
181
182/*------------------------------------------------------------------------------*/
183
184
dea9a315
L
185#if 0
186
69988dc1
L
187struct __freelist {
188 size_t sz;
189 struct __freelist *nx;
190};
191
192extern char *__brkval; /* first location not yet allocated */
193extern struct __freelist *__flp; /* freelist pointer (head of freelist) */
194
195#define STACK_POINTER() ((char *)AVR_STACK_POINTER_REG)
196
197void
198printfreelist(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
dea9a315 225#endif
69988dc1 226
92b46605
L
227#endif /* DEBUG */
228