forked from XenoKrelian/RpiJtag
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathrpjtag_io.c
226 lines (196 loc) · 4.63 KB
/
rpjtag_io.c
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
#include "rpjtag_io.h"
#include <stdlib.h>
#include <sys/mman.h>
#include <stdio.h>
#include <fcntl.h>
#include <string.h>
#include <unistd.h>
//Credit for GPIO setup goes to http://elinux.org/RPi_Low-level_peripherals
//
// Set up a memory regions to access GPIO
//
int mem_fd;
void *gpio_map;
// I/O access
volatile unsigned *gpio;
void tick_clk()
{
GPIO_SET(JTAG_TCK);
//nop_sleep(WAIT);
GPIO_CLR(JTAG_TCK);
}
void close_io()
{
munmap(gpio_map, BLOCK_SIZE);
}
int setup_io()
{
/* open /dev/mem */
if ((mem_fd = open("/dev/mem", O_RDWR|O_SYNC) ) < 0)
{
printf("can't open /dev/mem \n");
return -1;
}
/* mmap GPIO */
gpio_map = mmap(
NULL, //Any adddress in our space will do
BLOCK_SIZE, //Map length
PROT_READ|PROT_WRITE,// Enable reading & writting to mapped memory
MAP_SHARED, //Shared with other processes
mem_fd, //File to map
GPIO_BASE);
close(mem_fd); //No need to keep mem_fd open after mmap
if (gpio_map == MAP_FAILED)
{
printf("mmap error %d\n", (int)gpio_map);
return -2;
}
// Always use volatile pointer!
gpio = (volatile unsigned *)gpio_map;
INP_GPIO(JTAG_TCK);
INP_GPIO(JTAG_TMS);
INP_GPIO(JTAG_TDI);
INP_GPIO(JTAG_TDO); //Receive output from Device to rpi
nop_sleep(WAIT);
OUT_GPIO(JTAG_TDI); //Send data from rpi to Device
OUT_GPIO(JTAG_TMS);
OUT_GPIO(JTAG_TCK);
nop_sleep(WAIT);
return 0;
}
int read_jtag_tdo()
{
return ( GPIO_READ(JTAG_TDO) ) ? 1 : 0;
}
int tdi=-1;
void send_cmd_no_tms(int iTDI)
{
if(iTDI == 0)
{
if (tdi != 0)
{
GPIO_CLR(JTAG_TDI);
tdi = 0;
}
}
else
{
if (tdi != 1)
{
GPIO_SET(JTAG_TDI);
tdi = 1;
}
}
//nop_sleep(WAIT);
GPIO_SET(JTAG_TCK);
//nop_sleep(WAIT);
GPIO_CLR(JTAG_TCK);
//nop_sleep(WAIT);
}
//void set_pin(int pin, int val)
//{
// if (val == 0)
// {
// GPIO_CLR(pin);
// }
// else
// {
// GPIO_SET(pin);
// }
//}
void send_cmd(int iTDI,int iTMS)
{
if(iTDI == 1)
{
GPIO_SET(JTAG_TDI);
tdi = 1;
}
else
{
GPIO_CLR(JTAG_TDI);
tdi = 0;
}
if(iTMS == 1)
{
GPIO_SET(JTAG_TMS);
}
else
GPIO_CLR(JTAG_TMS);
//nop_sleep(WAIT);
tick_clk();
//nop_sleep(WAIT);
}
void reset_clk()
{
GPIO_CLR(JTAG_TCK);
}
//Mainly used for command words (CFG_IN)
void send_cmdWord_msb_first(unsigned int cmd, int lastBit, int bitoffset) //Send data, example 0xFFFF,0,20 would send 20 1's, with not TMS
{
while(bitoffset--)
{
int x = ( cmd >> bitoffset) & 0x01;
send_cmd(x,(lastBit==1 && bitoffset==0));
}
}
//Mainly used for IR Register codes
void send_cmdWord_msb_last(unsigned int cmd, int lastBit, int bitoffset) //Send data, example 0xFFFF,0,20 would send 20 1's, with not TMS
{
int i;
for(i=0;i<bitoffset;i++)
{
int x = ( cmd >> i ) & 0x01;
send_cmd(x,(lastBit==1 && bitoffset==i+1));
}
}
void send_byte(unsigned char byte, int lastbyte) //Send single byte, example from fgetc
{
int x;
for(x=7;x>=0;x--)
{
send_cmd(byte>>x&0x01,( x==0) && (lastbyte==1));
}
}
void send_byte_no_tms(unsigned char byte)
{
//int x;
// for(x=7;x>=0;x--)
// {
// send_cmd_no_tms(byte>>x&0x01);
// }
send_cmd_no_tms(byte&0x80);
send_cmd_no_tms(byte&0x40);
send_cmd_no_tms(byte&0x20);
send_cmd_no_tms(byte&0x10);
send_cmd_no_tms(byte&0x08);
send_cmd_no_tms(byte&0x04);
send_cmd_no_tms(byte&0x02);
send_cmd_no_tms(byte&0x01);
}
//Does a NOP call in BCM2708, and is meant to be run @ 750 MHz
void nop_sleep(long x)
{
while (x--) {
asm("nop");
}
}
void jtag_read_data(char* data,int iSize)
{
if(iSize==0) return;
int bitOffset = 0;
int temp;
memset(data,0,(iSize+7)/8);
iSize--;
while(iSize--)
{
temp = read_jtag_tdo();
send_cmd(0,0);
data[bitOffset/8] |= (temp << (bitOffset & 7));
bitOffset++;
}
temp = read_jtag_tdo(); //Read last bit, while also going to EXIT
send_cmd(0,1);
data[bitOffset/8] |= (temp << (bitOffset & 7));
send_cmd(0,1); //Go to UPDATE STATE
send_cmd(0,1); //Go to SELECT DR-SCAN
}