Skip to content

Commit b1e06a6

Browse files
committed
10KB/s programming
1 parent 4c8593f commit b1e06a6

File tree

2 files changed

+109
-19
lines changed

2 files changed

+109
-19
lines changed

prog/prog.ino

Lines changed: 60 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -172,13 +172,35 @@ unsigned char c2_erase_device (void) {
172172
retval = c2_read_data();
173173
}
174174

175+
unsigned char c2_write_sfr (unsigned char addr, unsigned char val) {
176+
unsigned char retval;
177+
c2_write_addr(0xB4);
178+
c2_write_data(0x0A);
179+
c2_poll_bit_low(INBUSY);
180+
c2_poll_bit_high(OUTREADY);
181+
retval = c2_read_data();
182+
c2_write_data(addr);
183+
c2_poll_bit_low(INBUSY);
184+
c2_write_data(0x1);
185+
c2_poll_bit_low(INBUSY);
186+
c2_write_data(val);
187+
c2_poll_bit_low(INBUSY);
188+
}
189+
175190
unsigned char c2_init_PI (void) {
176191
c2_rst();
177192
c2_write_addr(0x02);
178193
c2_write_data(0x02);
179194
c2_write_data(0x04);
180195
c2_write_data(0x01);
181-
delay(20);
196+
197+
// set up SFRs
198+
delay(25);
199+
c2_write_sfr(0xff, 0x80);
200+
delay(1);
201+
c2_write_sfr(0xef, 0x02);
202+
delay(1);
203+
c2_write_sfr(0xA9, 0x00);
182204
return 0;
183205
}
184206

@@ -224,9 +246,9 @@ void setup() {
224246

225247
unsigned int i;
226248
unsigned char retval;
227-
unsigned char rx_message[250],rx_message_ptr;
249+
unsigned char rx_message[300],rx_message_ptr;
228250
unsigned char rx,main_state,bytes_to_receive,rx_state;
229-
unsigned char flash_buffer[250];
251+
unsigned char flash_buffer[300];
230252
unsigned long addr;
231253

232254
unsigned char rx_state_machine (unsigned char state, unsigned char rx_char) {
@@ -249,8 +271,13 @@ unsigned char rx_state_machine (unsigned char state, unsigned char rx_char) {
249271
return state;
250272
}
251273

252-
void loop() {
274+
#define swap(x) ((((x)>>8) & 0xff) | (((x)<<8) & 0xff00))
253275

276+
void loop() {
277+
unsigned char crc;
278+
unsigned char newcrc;
279+
unsigned char c;
280+
unsigned long coff;
254281
if (Serial.available()) {
255282
rx = Serial.read();
256283
rx_state = rx_state_machine(rx_state, rx);
@@ -272,11 +299,37 @@ void loop() {
272299
rx_state = 0;
273300
break;
274301
case 0x03:
275-
addr = (((unsigned long)(rx_message[3]))<<16) + (((unsigned long)(rx_message[4]))<<8) + (((unsigned long)(rx_message[5]))<<0);
302+
addr = (((unsigned long)(rx_message[4]))<<8) + (((unsigned long)(rx_message[5]))<<0);
303+
crc = rx_message[6];
304+
newcrc = rx_message[5] + rx_message[4];
276305
for (i=0;i<rx_message[2];i++) {
277-
flash_buffer[i] = rx_message[i+6];
306+
flash_buffer[i] = rx_message[i+7];
278307
}
279-
c2_write_flash_block(addr,flash_buffer,rx_message[2]);
308+
309+
310+
for(i=0; i < rx_message[2]; i++)
311+
{
312+
newcrc += flash_buffer[i];
313+
}
314+
315+
if (crc != newcrc)
316+
{
317+
Serial.write(0x43);
318+
break;
319+
}
320+
321+
322+
c = rx_message[2];
323+
coff = 0;
324+
c2_write_flash_block(addr, flash_buffer,c);
325+
// while (c)
326+
327+
// {
328+
// c2_write_flash_block(addr + coff, flash_buffer + coff,min(c, 4));
329+
// coff += 4;
330+
// c -= min(c, 4);
331+
//}
332+
//delay(1);
280333
Serial.write(0x83);
281334
rx_state = 0;
282335
break;

prog_server.py

Lines changed: 49 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -12,20 +12,30 @@ class PI():
1212
def __init__(self, com):
1313
self.ser = serial.Serial(com, 1000000, timeout = 1)
1414

15+
def conf(self,):
16+
17+
# init Programming Interface (PI)
18+
while True:
19+
try:
20+
self.ser.write('\x01\x00')
21+
x =struct.unpack('B', self.ser.read(1))[0]
22+
print 'x:',hex(x)
23+
assert(0x81 == x)
24+
break
25+
except:
26+
while self.ser.read(1) != '': pass
27+
28+
print 'PI initiated'
29+
30+
1531
def prog(self, firmware):
1632

1733
print 'Connected'
1834

1935
#f = open(firmware,'r').readlines()
2036
f = firmware.splitlines()
2137

22-
# init Programming Interface (PI)
23-
self.ser.write('\x01\x00')
24-
x =struct.unpack('B', self.ser.read(1))[0]
25-
print 'x:',hex(x)
26-
assert(0x81 == x)
27-
28-
print 'PI initiated'
38+
self.conf()
2939

3040
# erase device
3141
self.ser.write('\x04\x00')
@@ -51,11 +61,28 @@ def prog(self, firmware):
5161
buf += data
5262
buf_size += size
5363

54-
if buf_size > 175:
55-
#print hex(addrh), hex(addrl), buf
56-
self.ser.write([0x3, buf_size + 4, buf_size, 0, addrh, addrl])
57-
self.ser.write(buf.decode('hex'))
58-
assert(0x83 == struct.unpack('B', self.ser.read(1))[0])
64+
if buf_size > 256 - 0x20 or i == f[-2]:
65+
attempts = 0
66+
while True:
67+
try:
68+
print hex(addrh), hex(addrl), buf
69+
crc = addrh + addrl
70+
crc += sum([struct.unpack('B', x)[0] for x in buf.decode('hex')])
71+
assert(len(buf.decode('hex')) == buf_size)
72+
self.ser.write([0x3, buf_size + 4 + 1, buf_size, 0, addrh, addrl, crc & 0xff])
73+
self.ser.write(buf.decode('hex'))
74+
ret = struct.unpack('B', self.ser.read(1))[0]
75+
if ret == 0x83:
76+
pass
77+
else:
78+
print 'error flash write returned ', hex(ret)
79+
raise RuntimeError('bad crc')
80+
break
81+
except Exception as e:
82+
attempts += 1
83+
self.conf()
84+
print e
85+
print 'attempts:',attempts
5986
total += buf_size
6087
buf_size = 0
6188
buf = ''
@@ -65,6 +92,16 @@ def prog(self, firmware):
6592
self.ser.write('\x02\x00')
6693
assert(0x82 == struct.unpack('B', self.ser.read(1))[0])
6794

95+
# reset device
96+
self.ser.write('\x02\x00')
97+
assert(0x82 == struct.unpack('B', self.ser.read(1))[0])
98+
99+
# reset device
100+
self.ser.write('\x02\x00')
101+
assert(0x82 == struct.unpack('B', self.ser.read(1))[0])
102+
103+
104+
68105
print 'Device reset'
69106

70107
print 'Once'

0 commit comments

Comments
 (0)