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