summaryrefslogtreecommitdiff
path: root/avr/debug.c
blob: d4ae1f4aa7c09c68835f0a80d5597bc00c933449 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
/*
 * (C) Copyright 2014 Leo C. <erbl259-lmu@yahoo.de>
 *
 * SPDX-License-Identifier:	GPL-2.0+
 */

#include "common.h"
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include <avr/eeprom.h>

#include "command.h"
#include "print-utils.h"
#include "debug.h"

/*
 * Debugging
 */

#ifdef DEBUG


#if 0
void dump_heap(void)
{
	extern unsigned int __brkval;

	dump_ram(__malloc_heap_start,
		__brkval - (unsigned int) __malloc_heap_start,
		"=== Heap:");
}
#endif


/*
 * Memory Display
 *	md addr {len}
 */
command_ret_t do_dump_mem(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
{
	void (*readhow)(uint8_t *buf, uint32_t addr, uint8_t count);

	(void) cmdtp; (void) flag;

	if (argc < 2)
		return CMD_RET_USAGE;

	uint32_t addr;
	uint32_t length = 128;

	switch (argv[0][3]) {
	case 'r':
		readhow = ram_read_buf;
		break;
	case 'e':
		readhow = eeprom_read_buf;
		break;
	case 'f':
		readhow = flash_read_buf;
		break;
	default:
		return CMD_RET_USAGE;
	}

	/* Address is specified since argc > 1 */
	addr =  strtoul(argv[1], NULL, 16);

	/* If another parameter, it is the length to display. */
	if (argc > 2)
		length = (uint16_t) strtoul(argv[2], NULL, 16);

	/* Print the lines. */
	dump_mem(addr, addr, length, readhow, NULL);

	return CMD_RET_SUCCESS;
}

command_ret_t do_eep_cp(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
{
	uint16_t src, dest, count;
	int_fast8_t step;

	(void) cmdtp;
	(void) flag;

	if (argc != 4)
		return CMD_RET_USAGE;

	src = (size_t) strtoul(argv[1], NULL, 16);
	dest = (size_t) strtoul(argv[2], NULL, 16);
	count = (size_t) strtoul(argv[3], NULL, 16);

	if (src > E2END) {
		debug("src > EEPROM size: 0x%04x\n", src);
		return CMD_RET_FAILURE;
	}
	if (dest > E2END) {
		debug("dest > EEPROM size: 0x%04x\n", dest);
		return CMD_RET_FAILURE;
	}
	if (count > E2END+1) {
		debug("count > EEPROM size: 0x%04x\n", count);
		return CMD_RET_FAILURE;
	}
	if (count == 0) {
		debug("Zero length?\n");
		return CMD_RET_FAILURE;
	}

	if (dest > src) {
		src += count - 1;
		dest += count - 1;
		step = -1;
	} else
		step = 1;

	while (count-- > 0) {
		uint8_t data;
		data = eeprom_read_byte((uint8_t *) src);
		eeprom_write_byte((uint8_t *) dest, data);
		src += step;
		dest += step;

	}
	return CMD_RET_SUCCESS;
}


/*------------------------------------------------------------------------------*/

#if 1

struct __freelist {
	size_t sz;
	struct __freelist *nx;
};

extern char *__brkval;		/* first location not yet allocated */
extern struct __freelist *__flp; /* freelist pointer (head of freelist) */

#define STACK_POINTER() ((char *)AVR_STACK_POINTER_REG)

void
printfreelist(const char * title)
{
	struct __freelist *fp1;
	int i;
	unsigned int freesum = 0;

/* TODO: printf_P */

	if (!__flp) {
		printf("%s no free list\n", title ? title : "");
	} else {
		printf("Free list: %s\n", title ? title : "");
		for (i = 0, fp1 = __flp; fp1; i++, fp1 = fp1->nx) {
			printf("    entry %d @ %04x: size %4u, next ",
			       i, (size_t)fp1, fp1->sz);
			if (fp1->nx)
				printf("%04x\n", (size_t)fp1->nx);
			else
				printf("NULL\n");
			freesum += fp1->sz;
		}
	}

	freesum +=  (size_t) STACK_POINTER() - __malloc_margin - (size_t) __brkval;

	printf("SP: %04x, __brkval: %04x, Total free: %04u\n",
		(size_t) STACK_POINTER(), (size_t) __brkval, freesum);
}

#endif

#endif /* DEBUG */