-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathusbprogrammer.cpp
189 lines (136 loc) · 3.87 KB
/
usbprogrammer.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
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
#include "usbprogrammer.h"
#define convertEndianess(A) ((((uint16_t)(A) & 0xff00) >> 8) | \
(((uint16_t)(A) & 0x00ff) << 8))
#define arr unsigned char[]
#define __packed __attribute__((__packed__))
struct RWPacket{
uint16_t cmd;
uint8_t padding;
uint16_t addr;
uint16_t size;
uint16_t data[505];
} __packed;
UsbProgrammer *UsbProgrammer::programmer = NULL;
UsbProgrammer::UsbProgrammer() :
progInit(false)
{
IsInitialized();
}
bool UsbProgrammer::ReadBlock(uint16_t addr, int size, uint16_t buffer[])
{
if(!IsInitialized()) return false;
RWPacket packet;
packet.cmd = 0x100;
packet.padding = 0;
int readed = 0;
unsigned short address = addr;
do {
int psize = 0x1F8;
if(psize+readed > size)
psize = size - readed;
packet.addr = convertEndianess(address);
packet.size = convertEndianess(psize);
if(!usb.WriteData((unsigned char*)&packet,7)) return false;
uint16_t tmp[3+psize];
if(!usb.ReadData((unsigned char*)tmp,6+psize*2)) return false;
for(int i=readed; i<readed+psize; i++)
buffer[i] = convertEndianess(tmp[3+i-readed]);
address += psize;
readed += psize;
} while (readed < size);
return true;
}
bool UsbProgrammer::Read(uint16_t addr, uint16_t *data)
{
if(!IsInitialized()) return false;
RWPacket packet;
packet.cmd = 0x100;
packet.padding = 0;
packet.size = 0x0100;
packet.addr = convertEndianess(addr);
if(!usb.WriteData((unsigned char*)&packet,7)) return false;
uint16_t tmp[4];
if(!usb.ReadData((unsigned char*)tmp,8)) return false;
*data = convertEndianess(tmp[3]);
return true;
}
bool UsbProgrammer::WriteBlock(uint16_t addr, int size, uint16_t buffer[])
{
if(!IsInitialized()) return false;
RWPacket packet;
packet.cmd = 0x200;
packet.padding = 0;
int writed = 0;
unsigned short address = addr;
do {
int psize = 0x1F8;
if(psize+writed > size)
psize = size - writed;
packet.addr = convertEndianess(address);
packet.size = convertEndianess(psize);
for(int i=writed; i<writed+psize; i++)
packet.data[i-writed] = convertEndianess(buffer[i]);
if(!usb.WriteData((unsigned char*)&packet,7+psize*2)) return false;
address += psize;
writed += psize;
} while (writed < size);
return true;
}
bool UsbProgrammer::Write(uint16_t addr, uint16_t data)
{
if(!IsInitialized()) return false;
RWPacket packet = {
0x200,
0,
convertEndianess(addr),
0x0100,
{convertEndianess(data)}
};
if(!usb.WriteData((unsigned char*)&packet,9)) return false;
return true;
}
bool UsbProgrammer::SetTransferSpeed(uint16_t speed)
{
if(!usb.IsInitialized()) return false;
uint8_t low = speed;
uint8_t high = speed >> 8;
return usb.WriteData((arr){0,3,0,high,low},5);
}
bool UsbProgrammer::IsXAPStopped(void)
{
if(!IsInitialized()) return false;
uint16_t data[2];
usb.WriteData((arr){0,4,0},3);
usb.ReadData((unsigned char*)data,4);
return data[1];
}
bool UsbProgrammer::IsInitialized(void)
{
if(usb.IsInitialized()) {
if(!progInit) {
SetMode(true);
SetTransferSpeed(0x189);
ClearCmdBits();
progInit = true;
}
return true;
} else
return false;
}
bool UsbProgrammer::SetMode(bool spi)
{
if(!usb.IsInitialized()) return false;
uint8_t data = spi ? 0 : 0xff;
return usb.WriteData((arr){0,9,0,0,data},5);
}
bool UsbProgrammer::ClearCmdBits()
{
if(!usb.IsInitialized()) return false;
return usb.WriteData((arr){0,15,0,0,0,0,0},7);
}
UsbProgrammer* UsbProgrammer::getProgrammer()
{
if(programmer == NULL)
programmer = new UsbProgrammer();
return programmer;
}