-
Notifications
You must be signed in to change notification settings - Fork 23
/
prog.ino
374 lines (333 loc) · 8.88 KB
/
prog.ino
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
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
/**
* Based on https://gist.github.com/racerxdl/c9a592808acdd9cd178e6e97c83f8baf
* which was based on: https://github.com/jaromir-sukuba/efm8prog/
Use his SW to program EFM8 using arduino.
**/
// GPIO is manipulated through PORT mappings for better speed.
// Flashes EFM8 at about 10kB/s
// Baud rate: 1000000
// Digital pin 2 on Mega
#define C2D_PORT PORTE
#define C2D_PIN 4
// Digital pin 3 on Mega
#define C2CK_PORT PORTE
#define C2CK_PIN 5
#define LED LED_BUILTIN
#define INBUSY 0x02
#define OUTREADY 0x01
static void c2_send_bits (unsigned char data, unsigned char len);
static unsigned char c2_read_bits (unsigned char len);
void c2_rst (void);
void c2_write_addr (unsigned char addr);
static unsigned char c2_read_addr (void);
static unsigned char c2_read_data (void);
static void c2_write_data (unsigned char addr);
unsigned char c2_init_PI (void);
unsigned char c2_read_flash_block (unsigned int addr, unsigned char * data, unsigned char len);
static unsigned char c2_poll_bit_low (unsigned char mask);
static unsigned char c2_poll_bit_high (unsigned char mask);
unsigned char c2_write_flash_block (unsigned int addr, unsigned char * data, unsigned char len);
unsigned char c2_erase_device (void);
void c2_rst() {
C2CK_PORT &= ~(1<<C2CK_PIN);
delayMicroseconds(50);
C2CK_PORT |= (1<<C2CK_PIN);
delayMicroseconds(50);
}
#define c2_pulse_clk()\
C2CK_PORT &= ~(1<<C2CK_PIN); \
C2CK_PORT |= (1<<C2CK_PIN);
static unsigned char c2_read_bits (unsigned char len) {
unsigned char i, data, mask;
mask = 0x01 << (len-1);
data = 0;
//pinMode(C2D, INPUT);
DDRE &= ~(1<<C2D_PIN);
PINE &= (1<<C2D_PIN);
for (i=0;i<len;i++) {
c2_pulse_clk();
data = data >> 1;
if (PINE & (1<<C2D_PIN)) {
data = data | mask;
}
}
DDRE |= (1<<C2D_PIN);
//pinMode(C2D, OUTPUT);
return data;
}
static void c2_send_bits (unsigned char data, unsigned char len) {
unsigned char i;
//pinMode(C2D, OUTPUT);
DDRE |= (1<<C2D_PIN);
for (i=0;i<len;i++) {
if (data&0x01) {
C2D_PORT |= (1<<C2D_PIN);
} else {
C2D_PORT &= ~(1<<C2D_PIN);
}
c2_pulse_clk();
data = data >> 1;
}
}
static void c2_write_data (unsigned char data) {
unsigned char retval;
c2_send_bits(0x0, 1);
c2_send_bits(0x1, 2);
c2_send_bits(0x0, 2);
c2_send_bits(data, 8);
retval = 0;
while (retval == 0) {
retval = c2_read_bits(1);
}
c2_send_bits(0x0, 1);
}
static unsigned char c2_poll_bit_high (unsigned char mask) {
unsigned char retval;
retval = c2_read_addr();
while ((retval&mask)==0) retval = c2_read_addr();
}
static unsigned char c2_poll_bit_low (unsigned char mask) {
unsigned char retval;
retval = c2_read_addr();
while (retval&mask) retval = c2_read_addr();
}
unsigned char c2_read_flash_block (unsigned int addr, unsigned char * data, unsigned char len) {
unsigned char retval,i;
c2_write_addr(0xB4);
c2_write_data(0x06);
c2_poll_bit_low(INBUSY);
c2_poll_bit_high(OUTREADY);
retval = c2_read_data();
c2_write_data(addr>>8);
c2_poll_bit_low(INBUSY);
c2_write_data(addr&0xFF);
c2_poll_bit_low(INBUSY);
c2_write_data(len);
c2_poll_bit_low(INBUSY);
c2_poll_bit_high(OUTREADY);
retval = c2_read_data();
for (i=0;i<len;i++) {
c2_poll_bit_high(OUTREADY);
retval = c2_read_data();
data[i] = retval;
}
return i;
}
unsigned char c2_write_flash_block (unsigned int addr, unsigned char * data, unsigned char len) {
unsigned char retval,i;
c2_write_addr(0xB4);
c2_write_data(0x07);
c2_poll_bit_low(INBUSY);
c2_poll_bit_high(OUTREADY);
retval = c2_read_data();
c2_write_data(addr>>8);
c2_poll_bit_low(INBUSY);
c2_write_data(addr&0xFF);
c2_poll_bit_low(INBUSY);
c2_write_data(len);
c2_poll_bit_low(INBUSY);
c2_poll_bit_high(OUTREADY);
retval = c2_read_data();
for (i=0;i<len;i++) {
c2_write_data(data[i] );
c2_poll_bit_low(INBUSY);
}
c2_poll_bit_high(OUTREADY);
}
unsigned char c2_erase_device (void) {
unsigned char retval;
c2_write_addr(0xB4);
c2_write_data(0x03);
c2_poll_bit_low(INBUSY);
c2_poll_bit_high(OUTREADY);
retval = c2_read_data();
c2_write_data(0xDE);
c2_poll_bit_low(INBUSY);
c2_write_data(0xAD);
c2_poll_bit_low(INBUSY);
c2_write_data(0xA5);
c2_poll_bit_low(INBUSY);
c2_poll_bit_high(OUTREADY);
retval = c2_read_data();
}
unsigned char c2_write_sfr (unsigned char addr, unsigned char val) {
unsigned char retval;
c2_write_addr(0xB4);
c2_write_data(0x0A);
c2_poll_bit_low(INBUSY);
c2_poll_bit_high(OUTREADY);
retval = c2_read_data();
c2_write_data(addr);
c2_poll_bit_low(INBUSY);
c2_write_data(0x1);
c2_poll_bit_low(INBUSY);
c2_write_data(val);
c2_poll_bit_low(INBUSY);
}
unsigned char c2_init_PI (void) {
c2_rst();
c2_write_addr(0x02);
c2_write_data(0x02);
c2_write_data(0x04);
c2_write_data(0x01);
return 0;
}
unsigned char c2_init_PI_sfr (void) {
c2_rst();
c2_write_addr(0x02);
c2_write_data(0x02);
c2_write_data(0x04);
c2_write_data(0x01);
// set up SFRs
delay(25);
c2_write_sfr(0xff, 0x80);
delay(1);
c2_write_sfr(0xef, 0x02);
delay(1);
c2_write_sfr(0xA9, 0x00);
return 0;
}
static unsigned char c2_read_data() {
unsigned char retval;
c2_send_bits(0x0, 1);
c2_send_bits(0x0, 2);
c2_send_bits(0x0, 2);
retval = 0;
while (retval == 0) {
retval = c2_read_bits(1);
}
retval = c2_read_bits(8);
c2_send_bits(0x0, 1);
return retval;
}
static unsigned char c2_read_addr() {
unsigned char retval;
c2_send_bits(0x0, 1);
c2_send_bits(0x2, 2);
retval = c2_read_bits(8);
c2_send_bits(0x0, 1);
return retval;
}
void c2_write_addr(unsigned char addr) {
c2_send_bits(0x0,1);
c2_send_bits(0x3,2);
c2_send_bits(addr,8);
c2_send_bits(0x0,1);
}
void setup() {
Serial.begin(1000000);
DDRE |= (1<<C2D_PIN);
DDRE |= (1<<C2CK_PIN);
C2CK_PORT |= (1<<C2CK_PIN);
digitalWrite(LED, LOW);
}
unsigned int i;
unsigned char retval;
unsigned char rx_message[300],rx_message_ptr;
unsigned char rx,main_state,bytes_to_receive,rx_state;
unsigned char flash_buffer[300];
unsigned long addr;
unsigned char rx_state_machine (unsigned char state, unsigned char rx_char) {
if (state==0) {
rx_message_ptr = 0;
rx_message[rx_message_ptr++] = rx_char;
return 1;
}
if (state==1) {
bytes_to_receive = rx_char;
rx_message[rx_message_ptr++] = rx_char;
if (bytes_to_receive==0) return 3;
return 2;
}
if (state==2) {
rx_message[rx_message_ptr++] = rx_char;
bytes_to_receive--;
if (bytes_to_receive==0) return 3;
}
return state;
}
#define swap(x) ((((x)>>8) & 0xff) | (((x)<<8) & 0xff00))
void loop() {
unsigned char crc;
unsigned char newcrc;
unsigned char c;
unsigned long coff;
if (Serial.available()) {
rx = Serial.read();
rx_state = rx_state_machine(rx_state, rx);
if (rx_state == 3) {
switch (rx_message[0]) {
case 0x0:
Serial.write(0x80);
break;
case 0x01:
c2_init_PI();
Serial.write(0x81);
digitalWrite(LED, HIGH);
rx_state = 0;
break;
case 0x02:
c2_rst();
Serial.write(0x82);
digitalWrite(LED, LOW);
rx_state = 0;
break;
case 0x03:
addr = (((unsigned long)(rx_message[4]))<<8) + (((unsigned long)(rx_message[5]))<<0);
crc = rx_message[6];
newcrc = rx_message[5] + rx_message[4];
for (i=0;i<rx_message[2];i++) {
flash_buffer[i] = rx_message[i+7];
}
for(i=0; i < rx_message[2]; i++)
{
newcrc += flash_buffer[i];
}
if (crc != newcrc)
{
Serial.write(0x43);
break;
}
c = rx_message[2];
coff = 0;
c2_write_flash_block(addr, flash_buffer,c);
// while (c)
// {
// c2_write_flash_block(addr + coff, flash_buffer + coff,min(c, 4));
// coff += 4;
// c -= min(c, 4);
//}
//delay(1);
Serial.write(0x83);
rx_state = 0;
break;
case 0x04:
c2_erase_device();
Serial.write(0x84);
rx_state = 0;
break;
case 0x05:
Serial.write(0x85);
addr = (((unsigned long)(rx_message[3]))<<16) + (((unsigned long)(rx_message[4]))<<8) + (((unsigned long)(rx_message[5]))<<0);
c2_read_flash_block(addr,flash_buffer,rx_message[2]);
for (i=0;i<rx_message[2];i++) {
Serial.write(flash_buffer[i]);
}
rx_state = 0;
break;
case 0x06:
c2_write_addr(rx_message[3]);
c2_write_data(rx_message[4]);
Serial.write(0x86);
rx_state = 0;
break;
case 0x07:
c2_write_addr(rx_message[3]);
Serial.write(c2_read_data());
Serial.write(0x87);
rx_state = 0;
break;
}
}
}
}