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