forked from robokoding/STK500
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSTK500.java
234 lines (201 loc) · 8.03 KB
/
STK500.java
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
227
228
229
230
231
232
233
234
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.FileInputStream;
import java.io.InputStreamReader;
import java.io.FileNotFoundException;
public class STK500 {
private OutputStream outStream = null;
private InputStream inputStream = null;
private static void openStreams() {
try {
/* TODO: implement opening bluetooth */
} catch (Exception e) {
e.printStackTrace();
}
}
private static void closeStreams() {
try {
/* TODO: implement closing bluetooth */
outStream.close();
inputStream.close();
} catch (Exception e) {
e.printStackTrace();
}
}
private static byte[] readProgram() {
FileInputStream fis = null;
File file = new File("/sdcard/sumorobot/main.hex");
// every line, except last one, has has 45 bytes (including \r\n)
int programLines = (int) Math.ceil(file.length() / 45.0);
// every line has 32 bytes of program data (excluding checksums, addresses, etc.)
int unusedBytes = 45 - 32;
// calculate program length according to program lines and unused bytes
int programLength = (int) file.length() - (programLines * unusedBytes);
// the actualy program data is half the size, as the hex file represents hex data in individual chars
programLength /= 2;
// create a byte array with the program length
byte[] program = new byte[programLength];
try {
// open the file stream
log.logcat("opening hex file", "d");
fis = new FileInputStream(file);
log.logcat("Total program size (in bytes) : " + programLength, "d");
log.logcat("Total file size to read (in bytes) : " + fis.available(), "d");
int content;
int lineIndex = 0;
int lineNumber = 1;
int programIndex = 0;
char[] line = new char[45];
// read the file byte by byte
while ((content = fis.read()) != -1) {
// append byte to the line
line[lineIndex++] = (char) content;
// when the line is complete
if (content == 10) {
// take only the actual program data form the line
for (int index = 9; index < lineIndex - 4; index += 2) {
// convert hexadecimals represented as chars into bytes
program[programIndex++] = Integer.decode("0x" + line[index] + line[index+1]).byteValue();
}
// start a new line
lineIndex = 0;
}
}
} catch (IOException e) {
log.logcat("reading hex failed: " + e.getMessage(), "d");
} finally {
try {
if (fis != null)
fis.close();
} catch (IOException e) {
e.printStackTrace();
}
}
return program;
}
public static void main (String[] args) {
byte[] program = readProgram();
System.out.println("program length: " + program.length);
System.out.println("opening streams");
openStreams();
System.out.println("syncing");
for (int i = 0; i < 5; i++) {
outStream.write(0x30);
outStream.write(0x20);
try {Thread.sleep(50);} catch (InterruptedException e) {e.printStackTrace();}
}
System.out.println("waiting for response");
int insync = inputStream.read();
int ok = inputStream.read();
if (insync == 0x14 && ok == 0x10) {
System.out.println("insync");
}
System.out.println("reading major version");
outStream.write(0x41);
outStream.write(0x81);
outStream.write(0x20);
try {Thread.sleep(50);} catch (InterruptedException e) {e.printStackTrace();}
System.out.println("waiting for response");
insync = inputStream.read();
int major = inputStream.read();
ok = inputStream.read();
if (insync == 0x14 && ok == 0x10) {
System.out.println("insync", "d");
}
System.out.println("reading minor version");
outStream.write(0x41);
outStream.write(0x82);
outStream.write(0x20);
try {Thread.sleep(50);} catch (InterruptedException e) {e.printStackTrace();}
System.out.println("waiting for response");
insync = inputStream.read();
int minor = inputStream.read();
ok = inputStream.read();
if (insync == 0x14 && ok == 0x10) {
System.out.println("insync");
}
System.out.println("version: " + major + "." + minor);
System.out.println("entering programming mode");
outStream.write(0x50);
outStream.write(0x20);
try {Thread.sleep(50);} catch (InterruptedException e) {e.printStackTrace();}
System.out.println("waiting for response");
insync = inputStream.read();
ok = inputStream.read();
if (insync == 0x14 && ok == 0x10) {
System.out.println("insync");
}
System.out.println("getting device signature");
outStream.write(0x75);
outStream.write(0x20);
try {Thread.sleep(50);} catch (InterruptedException e) {e.printStackTrace();}
System.out.println("waiting for response");
insync = inputStream.read();
byte [] signature = new byte[3];
inputStream.read(signature, 0, 3);
ok = inputStream.read();
if (insync == 0x14 && ok == 0x10) {
System.out.println("insync");
}
System.out.println("signature: " + signature[0] + "." + signature[1] + "." + signature[2]);
int size = 0;
int address = 0;
int programIndex = 0;
while (true) {
int laddress = address % 256;
int haddress = address / 256;
address += 64;
System.out.println("loading page address");
outStream.write(0x55);
outStream.write(laddress);
outStream.write(haddress);
outStream.write(0x20);
try {Thread.sleep(50);} catch (InterruptedException e) {e.printStackTrace();}
System.out.println("waiting for response");
insync = inputStream.read();
ok = inputStream.read();
if (insync == 0x14 && ok == 0x10) {
System.out.println("insync");
}
if (program.length - programIndex < 128) {
size = program.length - programIndex;
} else {
size = 128;
}
System.out.println("programming page size: " + size + " haddress: " + haddress + " laddress: " + laddress);
outStream.write(0x64);
outStream.write(0x00);
outStream.write(size);
outStream.write(0x46);
for (int i = 0; i < size; i++) {
outStream.write(program[programIndex++]);
}
outStream.write(0x20);
try {Thread.sleep(50);} catch (InterruptedException e) {e.printStackTrace();}
System.out.println("receiving sync ack");
insync = inputStream.read();
ok = inputStream.read();
if (insync == 0x14 && ok == 0x10) {
System.out.println("insync");
}
if (size != 0x80) {
break;
}
}
System.out.println("program index: " + programIndex);
System.out.println("leaving programming mode");
outStream.write(0x51);
outStream.write(0x20);
try {Thread.sleep(50);} catch (InterruptedException e) {e.printStackTrace();}
System.out.println("receiving sync ack");
insync = inputStream.read();
ok = inputStream.read();
if (insync == 0x14 && ok == 0x10) {
System.out.println("insync");
}
System.out.println("closing streams");
closeStreams();
}
}