]> cloudbase.mooo.com Git - z180-stamp.git/blob - avr/debug.c
Add copyright notice
[z180-stamp.git] / avr / debug.c
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 "print-utils.h"
15 #include "debug.h"
16
17 /*
18 * Debugging
19 */
20
21 #ifdef DEBUG
22
23
24 #if 0
25 void dump_heap(void)
26 {
27 extern unsigned int __brkval;
28
29 dump_ram(__malloc_heap_start,
30 __brkval - (unsigned int) __malloc_heap_start,
31 "=== Heap:");
32 }
33 #endif
34
35
36 /*
37 * Memory Display
38 * md addr {len}
39 */
40 command_ret_t do_dump_mem(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
41 {
42 void (*readhow)(uint8_t *buf, uint32_t addr, uint8_t count);
43
44 (void) cmdtp; (void) flag;
45
46 if (argc < 2)
47 return CMD_RET_USAGE;
48
49 uint32_t addr;
50 uint32_t length = 128;
51
52 if (strchr(argv[0],'r') != NULL)
53 readhow = ram_read_buf;
54 else if (strchr(argv[0],'e') != NULL)
55 readhow = eeprom_read_buf;
56 else
57 return CMD_RET_USAGE;
58
59 /* Address is specified since argc > 1 */
60 addr = strtoul(argv[1], NULL, 16);
61
62 /* If another parameter, it is the length to display. */
63 if (argc > 2)
64 length = (uint16_t) strtoul(argv[2], NULL, 16);
65
66 /* Print the lines. */
67 dump_mem(addr, addr, length, readhow, NULL);
68
69 return CMD_RET_SUCCESS;
70 }
71
72 command_ret_t do_eep_cp(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
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);
89 return CMD_RET_FAILURE;
90 }
91 if (dest > E2END) {
92 debug("dest > EEPROM size: 0x%04x\n", dest);
93 return CMD_RET_FAILURE;
94 }
95 if (count > E2END+1) {
96 debug("count > EEPROM size: 0x%04x\n", count);
97 return CMD_RET_FAILURE;
98 }
99 if (count == 0) {
100 debug("Zero length?\n");
101 return CMD_RET_FAILURE;
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 }
119 return CMD_RET_SUCCESS;
120 }
121
122
123 /*------------------------------------------------------------------------------*/
124
125 command_ret_t do_testarg(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
126 {
127 my_puts_P(cmdtp->name);
128 printf_P(PSTR("\n%s\n"), argv[0]);
129
130 return CMD_RET_SUCCESS;
131 }
132
133 /*------------------------------------------------------------------------------*/
134
135
136 #if 1
137
138 struct __freelist {
139 size_t sz;
140 struct __freelist *nx;
141 };
142
143 extern char *__brkval; /* first location not yet allocated */
144 extern struct __freelist *__flp; /* freelist pointer (head of freelist) */
145
146 #define STACK_POINTER() ((char *)AVR_STACK_POINTER_REG)
147
148 void
149 printfreelist(const char * title)
150 {
151 struct __freelist *fp1;
152 int i;
153 unsigned int freesum = 0;
154
155 /* TODO: printf_P */
156
157 if (!__flp) {
158 printf("%s no free list\n", title ? title : "");
159 } else {
160 printf("Free list: %s\n", title ? title : "");
161 for (i = 0, fp1 = __flp; fp1; i++, fp1 = fp1->nx) {
162 printf(" entry %d @ %04x: size %4u, next ",
163 i, (size_t)fp1, fp1->sz);
164 if (fp1->nx)
165 printf("%04x\n", (size_t)fp1->nx);
166 else
167 printf("NULL\n");
168 freesum += fp1->sz;
169 }
170 }
171
172 freesum += (size_t) STACK_POINTER() - __malloc_margin - (size_t) __brkval;
173
174 printf("SP: %04x, __brkval: %04x, Total free: %04u\n",
175 (size_t) STACK_POINTER(), (size_t) __brkval, freesum);
176 }
177
178 #endif
179
180 #endif /* DEBUG */