File: fet3.c

package info (click to toggle)
mspdebug 0.25-1
  • links: PTS, VCS
  • area: main
  • in suites: sid, trixie
  • size: 4,700 kB
  • sloc: ansic: 67,821; makefile: 201; xml: 19
file content (314 lines) | stat: -rw-r--r-- 6,427 bytes parent folder | download
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
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
/* MSPDebug - debugging tool for MSP430 MCUs
 * Copyright (C) 2009-2012 Daniel Beer
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
 */

#include <stdlib.h>
#include <string.h>
#include "util/ctrlc.h"
#include "dis.h"
#include "output.h"
#include "comport.h"
#include "cdc_acm.h"
#include "fet3.h"
#include "chipinfo.h"
#include "v3hil.h"

struct fet3 {
	struct device		base;
	struct v3hil		hil;
};

static int fet3_readmem(device_t dev_base, address_t addr,
			uint8_t *mem, address_t len)
{
	struct fet3 *fet = (struct fet3 *)dev_base;

	if (addr & 1) {
		uint8_t word[2];

		if (v3hil_read(&fet->hil, addr - 1, word, 2) < 0)
			return -1;

		mem[0] = word[1];
		addr++;
		len--;
	}

	while (len >= 2) {
		int r = 128;

		if (r > len)
			r = len;

		r = v3hil_read(&fet->hil, addr, mem, r & ~1);
		if (r < 0)
			return -1;

		addr += r;
		mem += r;
		len -= r;
	}

	if (len) {
		uint8_t word[2];

		if (v3hil_read(&fet->hil, addr, word, 2) < 0)
			return -1;

		mem[0] = word[0];
		addr++;
		len--;
	}

	return 0;
}

static int fet3_writemem(device_t dev_base, address_t addr,
			    const uint8_t *mem, address_t len)
{
	struct fet3 *fet = (struct fet3 *)dev_base;

	if (addr & 1) {
		uint8_t word[2];

		if (v3hil_read(&fet->hil, addr - 1, word, 2) < 0)
			return -1;

		word[1] = mem[0];
		if (v3hil_write(&fet->hil, addr - 1, word, 2) < 0)
			return -1;

		addr++;
		len--;
	}

	while (len >= 2) {
		int r = 128;

		if (r > len)
			r = len;

		r = v3hil_write(&fet->hil, addr, mem, r & ~1);
		if (r < 0)
			return -1;

		addr += r;
		mem += r;
		len -= r;
	}

	if (len) {
		uint8_t word[2];

		if (v3hil_read(&fet->hil, addr, word, 2) < 0)
			return -1;

		word[0] = mem[0];
		if (v3hil_write(&fet->hil, addr, word, 2) < 0)
			return -1;

		addr++;
		len--;
	}

	return 0;
}

static int fet3_setregs(device_t dev_base, const address_t *regs)
{
	struct fet3 *fet = (struct fet3 *)dev_base;

	memcpy(fet->hil.regs, regs, sizeof(fet->hil.regs));
	return -1;
}

static int fet3_getregs(device_t dev_base, address_t *regs)
{
	struct fet3 *fet = (struct fet3 *)dev_base;

	memcpy(regs, fet->hil.regs, sizeof(fet->hil.regs));
	return 0;
}

static int fet3_ctl(device_t dev_base, device_ctl_t type)
{
	struct fet3 *fet = (struct fet3 *)dev_base;

	switch (type) {
	case DEVICE_CTL_RESET:
		if (v3hil_sync(&fet->hil) < 0)
			return -1;
		return v3hil_update_regs(&fet->hil);

	case DEVICE_CTL_RUN:
		if (v3hil_flush_regs(&fet->hil) < 0)
			return -1;
		return v3hil_context_restore(&fet->hil, 0);

	case DEVICE_CTL_HALT:
		if (v3hil_context_save(&fet->hil) < 0)
			return -1;
		return v3hil_update_regs(&fet->hil);

	case DEVICE_CTL_STEP:
		if (v3hil_flush_regs(&fet->hil) < 0)
			return -1;
		if (v3hil_single_step(&fet->hil) < 0)
			return -1;
		return v3hil_update_regs(&fet->hil);

	default:
		printc_err("fet3: unsupported operation\n");
		return -1;
	}

	return 0;
}

static device_status_t fet3_poll(device_t dev_base)
{
	/* We don't support breakpoints yet, so there's nothing to poll
	 * for.
	 */
	if(delay_ms(500) < 0)
		return DEVICE_STATUS_INTR;

	return DEVICE_STATUS_RUNNING;
}

static int fet3_erase(device_t dev_base, device_erase_type_t type,
		      address_t addr)
{
	struct fet3 *fet = (struct fet3 *)dev_base;

	if (type == DEVICE_ERASE_ALL) {
		printc_err("fet3: mass erase is not supported\n");
		return -1;
	}

	return v3hil_erase(&fet->hil,
		(type == DEVICE_ERASE_MAIN) ? ADDRESS_NONE : addr);
}

static int debug_init(struct fet3 *fet, const struct device_args *args)
{
	if (v3hil_comm_init(&fet->hil) < 0)
		return -1;

	printc_dbg("Set VCC: %d mV\n", args->vcc_mv);
	if (v3hil_set_vcc(&fet->hil, args->vcc_mv) < 0)
		return -1;

	printc_dbg("Starting interface...\n");
	if (v3hil_start_jtag(&fet->hil,
		    (args->flags & DEVICE_FLAG_JTAG) ?
		    V3HIL_JTAG_JTAG : V3HIL_JTAG_SPYBIWIRE) < 0)
		return -1;

	if (args->forced_chip_id) {
		fet->hil.chip = chipinfo_find_by_name(args->forced_chip_id);
		if (!fet->hil.chip) {
			printc_err("fet3: unknown chip: %s\n",
				args->forced_chip_id);
			goto fail_stop_jtag;
		}
	} else {
		if (v3hil_identify(&fet->hil) < 0)
			goto fail_stop_jtag;
	}

	fet->base.chip = fet->hil.chip;

	if (v3hil_configure(&fet->hil) < 0)
		goto fail_stop_jtag;
	if (v3hil_update_regs(&fet->hil) < 0)
		goto fail_stop_jtag;

	return 0;

fail_stop_jtag:
	v3hil_stop_jtag(&fet->hil);
	return -1;
}

static device_t fet3_open(const struct device_args *args)
{
	struct fet3 *fet;
	transport_t trans;

	if (args->flags & DEVICE_FLAG_TTY)
		trans = comport_open(args->path, 460800);
	else
		trans = cdc_acm_open(args->path, args->requested_serial,
				     460800, 0x2047, 0x0013);

	if (!trans) {
		printc_err("fet3: failed to open transport\n");
		return NULL;
	}

	fet = malloc(sizeof(*fet));
	if (!fet) {
		printc_err("fet3: malloc: %s\n", last_error());
		trans->ops->destroy(trans);
		return NULL;
	}

	memset(fet, 0, sizeof(*fet));
	fet->base.type = &device_ezfet;

	/* Breakpoints aren't supported yet */
	fet->base.max_breakpoints = 0;

	v3hil_init(&fet->hil, trans, 0);

	if (debug_init(fet, args) < 0) {
		trans->ops->destroy(trans);
		free(fet);
		return NULL;
	}

	return &fet->base;
}

static void fet3_destroy(device_t dev_base)
{
	struct fet3 *fet = (struct fet3 *)dev_base;
	transport_t tr = fet->hil.hal.trans;

	v3hil_flush_regs(&fet->hil);
	v3hil_context_restore(&fet->hil, 1);
	v3hil_stop_jtag(&fet->hil);

	tr->ops->destroy(tr);
	free(fet);
}

const struct device_class device_ezfet = {
	.name		= "ezfet",
	.help		= "Texas Instruments eZ-FET",
	.open		= fet3_open,
	.destroy	= fet3_destroy,
	.readmem	= fet3_readmem,
	.writemem	= fet3_writemem,
	.getregs	= fet3_getregs,
	.setregs	= fet3_setregs,
	.ctl		= fet3_ctl,
	.poll		= fet3_poll,
	.erase		= fet3_erase,
	.getconfigfuses = NULL
};