forked from XenoKrelian/RpiJtag
-
Notifications
You must be signed in to change notification settings - Fork 1
/
rpjtag.c
140 lines (115 loc) · 3.09 KB
/
rpjtag.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
/*
* Raspberry Pi JTAG Programmer using GPIO connector
* Version 0.3 Copyright 2013 Rune 'Krelian' Joergensen
*
* 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 3 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, see <http://www.gnu.org/licenses/>.
*
*/
#include <stdlib.h>
#include <stdint.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include "rpjtag.h" //Defines
#include "rpjtag_io.h" //Basic IO functions
#include "rpjtag_stateMachine.h"
#include "rpjtag_bit_reader.h"
int CountDevices()
{
int tIRLen = 0;
SelectShiftIR();
for(x=0;x<MAX_CHAIN;x++) send_cmd(1,0); //Flush IR Registers, makes sure counting IR-Reg Size is currect
for(i=0;i<MAX_CHAIN;i++)
{
x = read_jtag_tdo();
send_cmd(0,0);
if(!x) break;
tIRLen++;
}
fprintf(stdout,"IR size from jtag: %d\n",tIRLen);
for(x=0;x<MAX_CHAIN;x++) send_cmd(1,0); //PUT all in BYPASS
send_cmd(1,1);
ExitShift(); // state returns to IDLE
SelectShiftDR();
for(x=0;x<MAX_CHAIN;x++) send_cmd(0,0); //Flush DR register, used during debug
for(x=0;x<tIRLen+1;x++) //put in 1's into DR, when TDO outputs a 1, end is reached
{
i = read_jtag_tdo();
send_cmd(1,0);
if(i) break;
}
if(x==tIRLen+1) x = 0; //if x reaches max an error occured
syncJTAGs();
return x;
}
//Raspberry PI side.
void help()
{
fprintf(stderr,
"Usage: rpjtag [options]\n"
" -h Print help\n"
" -i Specify bit file (default = 'top.bit')\n"
"\n");
exit(0);
}
int main(int argc, char *argv[])
{
char *ifile = "top.bit";
int opt;
nDevices = 0;
parms = 0;
IRTotalRegSize = 0;
jtag_state = 0x00;
fprintf(stderr, "Raspberry Pi JTAG Programmer, v0.3 (April 2013)\n\n");
while ((opt = getopt(argc, argv, "hi:")) != -1) {
switch (opt) {
case 'h':
help();
break;
case 'i':
ifile = optarg;
break;
default:
fprintf(stderr, "\n");
help();
exit(0);
break;
}
}
if(ifile == 0x00)
exit(0);
FILE* bitstream = load_bit_file(ifile);
// Set up gpi pointer for direct register access
fprintf(stdout, "setting up IO\n");
setup_io();
//rpjtag_stateMachine.c
fprintf(stdout, "sync-ing JTAG\n");
syncJTAGs(); //Puts all JTAG Devices in RESET MODE
fprintf(stdout, "Counting devices\n");
nDevices = CountDevices(); //Count Number of devices, also finds total IR size
fprintf(stdout,"Devices found: %d\n", nDevices);
if (nDevices != 1)
{
fprintf(stderr, "nDevices > 1, add some code so I know what to do!");
}
if(nDevices != 0)
{
//rpjtag_bit_reader.c
ProgramDevice(0,bitstream);
}
fclose(bitstream);
close_io();
printf("\nDone\n");
exit(0);
}