comparison src/jtag.c @ 2:abf221bf3ce4

AVR JTAG server.
author Thinker K.F. Li <thinker@branda.to>
date Sat, 21 Feb 2009 23:06:50 +0800
parents
children 6b1594fb668f
comparison
equal deleted inserted replaced
1:f7c60e525801 2:abf221bf3ce4
1 #include <stdio.h>
2 #include <util/delay.h>
3 #include "jtag.h"
4 #include "avriotools.h"
5
6 /* It is supposed to work at 1Mbps */
7 #define CLK_DELAY() do { _delay_ms(0.0004); } while(0)
8
9 void jtag_init(void) {
10 pin_mode(&JTAG_PORT, JTAG_TCK, PM_OUTPUT);
11 pin_mode(&JTAG_PORT, JTAG_TMS, PM_OUTPUT);
12 pin_mode(&JTAG_PORT, JTAG_TDI, PM_OUTPUT);
13 pin_mode(&JTAG_PORT, JTAG_TDO, PM_INPUT);
14 pin_lo(JTAG_PORT, JTAG_TCK);
15 pin_lo(JTAG_PORT, JTAG_TMS);
16 pin_lo(JTAG_PORT, JTAG_TDI);
17 }
18
19 #define TCK_LO() pin_lo(JTAG_PORT, JTAG_TCK)
20 #define TCK_HI() pin_hi(JTAG_PORT, JTAG_TCK)
21 #define SEND_BIT(pin, bit) \
22 do { \
23 if(bit) \
24 pin_hi(JTAG_PORT, JTAG_TDI); \
25 else \
26 pin_lo(JTAG_PORT, JTAG_TDI); \
27 CLK_DELAY(); \
28 TCK_HI(); \
29 CLK_DELAY(); \
30 } while(0)
31 #define GET_TDO() (JTAG_PIN & _BV(JTAG_TDO))
32
33 void jtag_tms(char *buf, int nbits) {
34 int i;
35 int byteoff, bitoff;
36 int bit;
37
38 for(i = 0; i < nbits; i++) {
39 byteoff = i / 8;
40 bitoff = i % 8;
41 bit = buf[byteoff] & (1 << bitoff);
42 SEND_BIT(JTAG_TMS, bit);
43 TCK_LO();
44 }
45 }
46
47 void jtag_shift(char *buf, int nbits) {
48 int i;
49 int byteoff, bitoff;
50 int bit;
51
52 for(i = 0; i < nbits; i++) {
53 byteoff = i / 8;
54 bitoff = i % 8;
55 bit = buf[byteoff] & (1 << bitoff);
56 SEND_BIT(JTAG_TDI, bit);
57 TCK_LO();
58 }
59 }
60
61 void jtag_shift_inout(char *ibuf, char *obuf, int nbits) {
62 int i;
63 int byteoff, bitoff;
64 int bit, tdo, bitmask;
65
66 for(i = 0; i < nbits; i++) {
67 byteoff = i / 8;
68 bitoff = i % 8;
69 bitmask = 1 << bitoff;
70
71 bit = ibuf[byteoff] & bitmask;
72 SEND_BIT(JTAG_TDI, bit);
73 tdo = GET_TDO();
74 TCK_LO();
75 if(tdo)
76 obuf[byteoff] |= bitmask;
77 else
78 obuf[byteoff] &= ~(bitmask);
79 }
80 }