File: jtdev.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 (359 lines) | stat: -rw-r--r-- 8,501 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
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
/* MSPDebug - debugging tool for MSP430 MCUs
 * Copyright (C) 2009-2012 Daniel Beer
 * Copyright (C) 2012 Peter Bägel
 *
 * ppdev/ppi abstraction inspired by uisp src/DARPA.C
 *   originally written by Sergey Larin;
 *   corrected by Denis Chertykov, Uros Platise and Marek Michalkiewicz.
 *
 * 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 <stdint.h>
#include "jtdev.h"
#include "output.h"

#if defined(__linux__) || \
    ( defined(__FreeBSD__) || defined(__DragonFly__) )
/*===== includes =============================================================*/

#include <fcntl.h>
#include <unistd.h>

#if defined(__linux__)

#include <linux/ppdev.h>

#define par_claim(fd)			ioctl(fd, PPCLAIM, NULL)
#define par_write_data(fd, ptr)		ioctl(fd, PPWDATA, ptr)
#define par_write_control(fd, ptr)	ioctl(fd, PPWCONTROL, ptr)
#define par_release(fd)			ioctl(fd, PPRELEASE, NULL)
#define par_read_status(fd, ptr)	ioctl(fd, PPRSTATUS, ptr)

#elif defined(__FreeBSD__) || defined(__DragonFly__)

#if defined(__FreeBSD__)
#include <dev/ppbus/ppi.h>
#include <dev/ppbus/ppbconf.h>
#else /* __DragonFly__ */
#include <dev/misc/ppi/ppi.h>
#include <bus/ppbus/ppbconf.h>
#endif

#define par_claim(fd)			(0)
#define par_write_data(fd, ptr)		ioctl(fd, PPISDATA, ptr)
#define par_write_control(fd, ptr)	ioctl(fd, PPISCTRL, ptr)
#define par_release(fd)			(0)
#define par_read_status(fd, ptr)	ioctl(fd, PPIGSTATUS, ptr)

#endif /* __linux__ || (__FreeBSD__ || __DragonFly__) */

#include <sys/ioctl.h>

#include "util.h"

/*===== private symbols ======================================================*/

/*--- port data (out) ---*/
#define DATA0 ((unsigned char)0x01)
#define DATA1 ((unsigned char)0x02)
#define DATA2 ((unsigned char)0x04)
#define DATA3 ((unsigned char)0x08)
#define DATA4 ((unsigned char)0x10)
#define DATA5 ((unsigned char)0x20)
#define DATA6 ((unsigned char)0x40)
#define DATA7 ((unsigned char)0x80)

/*--- port status (in) ---*/
#define ERR  ((unsigned char)0x08)
#define SEL  ((unsigned char)0x10)
#define PE   ((unsigned char)0x20)
#define ACK  ((unsigned char)0x40)
#define BUSY ((unsigned char)0x80)

/*--- port control (out) ---*/
#ifndef STROBE
#define STROBE   ((unsigned char)0x01) /* inverted by PC-hardware */
#endif
#ifndef AUTOFEED
#define AUTOFEED ((unsigned char)0x02)
#endif
#ifndef INIT
#define INIT     ((unsigned char)0x04)
#endif
#ifndef SELECTIN
#define SELECTIN ((unsigned char)0x08)
#endif
#define IRQEN    ((unsigned char)0x10)

/*--- JTAG signal mapping ---*/
#define TEST      INIT
#define TDO       PE
#define TDI       DATA0
#define TMS       DATA1
#define TCK       DATA2
#define XOUT      DATA3
#define POWER    (DATA4 | DATA7)
#define RESET     STROBE
#define ENABLE   (SELECTIN | AUTOFEED)
#define LED_GREEN DATA5
#define LED_RED   DATA6

#define TCLK	  TDI

/*===== public functions =====================================================*/

static void do_ppwdata(struct jtdev *p)
{
	if (par_write_data(p->port, &p->data_register) < 0) {
		pr_error("jtdev: par_write_data");
		p->failed = 1;
	}
}

static void do_ppwcontrol(struct jtdev *p)
{
	if (par_write_control(p->port, &p->control_register) < 0) {
		pr_error("jtdev: par_write_control");
		p->failed = 1;
	}
}

static int jtpif_open(struct jtdev *p, const char *device)
{
	p->port = open(device, O_RDWR);
	if (p->port < 0) {
		printc_err("jtdev: can't open %s: %s\n",
			   device, last_error());
		return -1;
	}

	if (par_claim(p->port) < 0) {
		printc_err("jtdev: par_claim: %s: %s\n",
			   device, last_error());
		close(p->port);
		return -1;
	}

	p->data_register = 0;
	p->control_register = 0;
	p->failed = 0;

	do_ppwdata(p);
	do_ppwcontrol(p);

	return 0;
}

static void jtpif_close(struct jtdev *p)
{
	if (par_release(p->port) < 0)
		pr_error("warning: jtdev: failed to release port");

	close(p->port);
}

static void jtpif_power_on(struct jtdev *p)
{
	/* power supply on */
	p->data_register |= POWER;
	do_ppwdata(p);
}

static void jtpif_power_off(struct jtdev *p)
{
	/* power supply off */
	p->data_register &= ~POWER;
	do_ppwdata(p);

	/* a high, inactive reset also powers the target */
	/* reset pin is inverted by PC hardware          */
	p->control_register |= RESET;
	do_ppwcontrol(p);
}

static void jtpif_connect(struct jtdev *p)
{
	p->control_register |= (TEST | ENABLE);
	do_ppwcontrol(p);
}

static void jtpif_release(struct jtdev *p)
{
	p->control_register &= ~(TEST | ENABLE);
	do_ppwcontrol(p);
}

static void jtpif_tck(struct jtdev *p, int out)
{
	if (out)
		p->data_register |= TCK;
	else
		p->data_register &= ~TCK;

	do_ppwdata(p);
}

static void jtpif_tms(struct jtdev *p, int out)
{
	if (out)
		p->data_register |= TMS;
	else
		p->data_register &= ~TMS;

	do_ppwdata(p);
}

static void jtpif_tdi(struct jtdev *p, int out)
{
	if (out)
		p->data_register |= TDI;
	else
		p->data_register &= ~TDI;

	do_ppwdata(p);
}

static void jtpif_rst(struct jtdev *p, int out)
{
	/* reset pin is inverted by PC hardware */
	if (out)
		p->control_register &= ~RESET;
	else
		p->control_register |= RESET;

	do_ppwcontrol(p);
}

static void jtpif_tst(struct jtdev *p, int out)
{
	if (out)
		p->control_register |= TEST;
	else
		p->control_register &= ~TEST;

	do_ppwcontrol(p);
}

static int jtpif_tdo_get(struct jtdev *p)
{
	uint8_t input;

	if (par_read_status(p->port, &input) < 0) {
		pr_error("jtdev: par_read_status:");
		p->failed = 1;
		return 0;
	}

	return (input & TDO) ? 1 : 0;
}

static void jtpif_tclk(struct jtdev *p, int out)
{
	if (out)
		p->data_register |= TCLK;
	else
		p->data_register &= ~TCLK;

	do_ppwdata(p);
}

static int jtpif_tclk_get(struct jtdev *p)
{
	return (p->data_register & TCLK) ? 1 : 0;
}

static void jtpif_tclk_strobe(struct jtdev *p, unsigned int count)
{
	int i;

	for (i = 0; i < count; i++) {
		jtpif_tclk(p, 1);
		jtpif_tclk(p, 0);

		if (p->failed)
			return;
	}
}

static void jtpif_led_green(struct jtdev *p, int out)
{
	if (out)
		p->data_register |= LED_GREEN;
	else
		p->data_register &= ~LED_GREEN;

	do_ppwdata(p);
}

static void jtpif_led_red(struct jtdev *p, int out)
{
	if (out)
		p->data_register |= LED_RED;
	else
		p->data_register &= ~LED_RED;

	do_ppwdata(p);
}
#else /* __linux__ */
static int jtpif_open(struct jtdev *p, const char *device)
{
	printc_err("jtdev: driver is not supported on this platform\n");
	p->failed = 1;
	return -1;
}

static void jtpif_close(struct jtdev *p) { }

static void jtpif_power_on(struct jtdev *p) { }
static void jtpif_power_off(struct jtdev *p) { }
static void jtpif_connect(struct jtdev *p) { }
static void jtpif_release(struct jtdev *p) { }

static void jtpif_tck(struct jtdev *p, int out) { }
static void jtpif_tms(struct jtdev *p, int out) { }
static void jtpif_tdi(struct jtdev *p, int out) { }
static void jtpif_rst(struct jtdev *p, int out) { }
static void jtpif_tst(struct jtdev *p, int out) { }
static int jtpif_tdo_get(struct jtdev *p) { return 0; }

static void jtpif_tclk(struct jtdev *p, int out) { }
static int jtpif_tclk_get(struct jtdev *p) { return 0; }
static void jtpif_tclk_strobe(struct jtdev *p, unsigned int count) { }

static void jtpif_led_green(struct jtdev *p, int out) { }
static void jtpif_led_red(struct jtdev *p, int out) { }
#endif

const struct jtdev_func jtdev_func_pif = {
  .jtdev_open        = jtpif_open,
  .jtdev_close       = jtpif_close,
  .jtdev_power_on    = jtpif_power_on,
  .jtdev_power_off   = jtpif_power_off,
  .jtdev_connect     = jtpif_connect,
  .jtdev_release     = jtpif_release,
  .jtdev_tck	     = jtpif_tck,
  .jtdev_tms	     = jtpif_tms,
  .jtdev_tdi	     = jtpif_tdi,
  .jtdev_rst	     = jtpif_rst,
  .jtdev_tst	     = jtpif_tst,
  .jtdev_tdo_get     = jtpif_tdo_get,
  .jtdev_tclk	     = jtpif_tclk,
  .jtdev_tclk_get    = jtpif_tclk_get,
  .jtdev_tclk_strobe = jtpif_tclk_strobe,
  .jtdev_led_green   = jtpif_led_green,
  .jtdev_led_red     = jtpif_led_red
};