-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathutilities.cpp
170 lines (155 loc) · 4.08 KB
/
utilities.cpp
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
#ifndef WIN32
#include <sys/utsname.h>
#endif
#include <stdio.h>
#include <string.h>
#include <strings.h>
#include <memory>
#include "ioparport.h"
#include "iofx2.h"
#include "ioftdi.h"
#include "ioxpc.h"
#include "utilities.h"
extern char *optarg;
void detect_chain(Jtag *jtag, DeviceDB *db)
{
int num=jtag->getChain();
for(int i=0; i<num; i++)
{
DeviceID id = jtag->getDeviceID(i);
fprintf(stderr,"JTAG loc.: %3d IDCODE: 0x%08lx ", i, (unsigned long)id);
int length = db->idToIRLength(id);
if (length > 0)
{
jtag->setDeviceIRLength(i,length);
fprintf(stderr,"Desc: %30s Rev: %c IR length: %2d\n",
db->idToDescription(id),
(int)(id >> 28) | 'A', length);
}
else
{
fprintf(stderr,"not found in '%s'.\n", db->getFile().c_str());
}
}
}
int getIO( std::auto_ptr<IOBase> *io, struct cable_t * cable, char const *dev,
char const *serial, bool verbose, bool use_ftd2xx,
unsigned int freq)
{
int res = 1;
unsigned int use_freq;
if (!cable)
{
fprintf(stderr, "No cable selected. You must use -c option."
" See xc3sprog -h for more help\n");
return 1;
}
if ((freq == 0) || ((cable->freq != 0) && (freq > cable->freq)))
use_freq = cable->freq;
else
use_freq = freq;
if (cable->cabletype == CABLE_PP)
{
#ifdef __MACH__
fprintf(stderr, "Parallel port cables are not supported on OS X.\n");
return 1;
#else
io->reset(new IOParport());
io->get()->setVerbose(verbose);
res = io->get()->Init(cable, dev, use_freq);
#endif
}
else if(cable->cabletype == CABLE_FTDI)
{
io->reset(new IOFtdi(use_ftd2xx));
io->get()->setVerbose(verbose);
res = io->get()->Init(cable, serial, use_freq);
}
else if(cable->cabletype == CABLE_FX2)
{
io->reset(new IOFX2());
io->get()->setVerbose(verbose);
res = io->get()->Init(cable, serial, use_freq);
}
else if(cable->cabletype == CABLE_XPC)
{
io->reset(new IOXPC());
io->get()->setVerbose(verbose);
res = io->get()->Init(cable, serial, use_freq);
}
else
{
fprintf(stderr, "Unknown Cable \"%s\" \n", getCableName(cable->cabletype));
}
return res;
}
const char *getCableName(int type)
{
switch (type)
{
case CABLE_PP: return "pp"; break;
case CABLE_FTDI: return "ftdi"; break;
case CABLE_FX2: return "fx2"; break;
case CABLE_XPC: return "xpc"; break;
case CABLE_UNKNOWN: return "unknown"; break;
default:
return "Unknown";
}
}
void
get_os_name(char *buf, int buflen)
{
memset(buf, 0, buflen);
#ifdef WIN32
snprintf(buf, buflen - 1, "Windows");
#else
struct utsname uts;
int ret;
// Obtain information about current system.
memset(&uts, 0, sizeof(uts));
ret = uname(&uts);
if (ret < 0)
snprintf(buf, buflen - 1, "<UNKNOWN>");
snprintf(buf, buflen - 1, "%s", uts.sysname);
#endif
}
void xc3sprog_Usleep(unsigned int usec)
{
#if defined(__WIN32__)
/* Busy wait, as scheduler prolongs all waits for to least 10 ms
http://sourceforge.net/apps/trac/scummvm/browser/vendor/freesci/ \
glutton/src/win32/usleep.c?rev=38187
*/
LARGE_INTEGER lFrequency;
LARGE_INTEGER lEndTime;
LARGE_INTEGER lCurTime;
QueryPerformanceFrequency (&lFrequency);
if (lFrequency.QuadPart)
{
QueryPerformanceCounter (&lEndTime);
lEndTime.QuadPart += (LONGLONG) usec * lFrequency.QuadPart / 1000000;
do
{
QueryPerformanceCounter (&lCurTime);
if (usec > 11000)
Sleep(0);
} while (lCurTime.QuadPart < lEndTime.QuadPart);
}
#else
usleep(usec);
#endif
}
/* Split string on delimiting character. */
std::vector<std::string> splitString(const std::string& s, char delim)
{
std::vector<std::string> res;
for (std::string::size_type i = 0, n = s.size(); i < n; )
{
std::string::size_type k = s.find(delim, i);
if (k == std::string::npos)
k = n;
res.push_back(s.substr(i, k - i));
i = k + 1;
}
return res;
}