Skip to content

Commit 82793c9

Browse files
New version for python 3.6
1 parent 34a8a73 commit 82793c9

File tree

3 files changed

+136
-8
lines changed

3 files changed

+136
-8
lines changed

README.md

Lines changed: 20 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,11 @@ C2 is a 2-pin protocol. Any arduino should work to implement the protocol via G
1414

1515
Program the firmware to the arduino and connect C2D, C2CK, and GND to your target device.
1616

17-
### Software
17+
# Software
1818

19-
You need to have Python installed. Then, install some required python modules.Use Python 2.7 and Pyserial
19+
You need to have Python installed. Then, install some required python modules.
20+
Use Python 2.7 and Pyserial for [flash27.py](https://github.com/christophe94700/efm8-arduino-programmer/blob/master/flash27.py)
21+
Use Python 3.6 and Pyserial for [flash36.py](https://github.com/christophe94700/efm8-arduino-programmer/blob/master/flash36.py)
2022

2123
```
2224
pip install -r requirements.txt
@@ -27,17 +29,27 @@ pip install -r requirements.txt
2729
Programming one target.
2830

2931
```
30-
py flash.py <serial-port> <firmware.hex>
32+
python flash.py <serial-port> <firmware.hex>
3133
```
3234

3335
Example for Linux:
34-
```flash.py /dev/ttyACM0 RF_Brige.hex```
36+
```flash27.py /dev/ttyACM0 RF_Brige.hex```
3537
or
36-
```sudo flash.py /dev/ttyACM0 RF_Brige.hex```
38+
```sudo flash27.py /dev/ttyACM0 RF_Brige.hex```
3739

38-
Example for Windows: ```py flash.py COM8 RF_Bridge.hex```
40+
Example for Windows: ```python flash27.py COM8 RF_Bridge.hex```
3941

40-
#Troubleshooting
42+
# Troubleshooting
4143

42-
- If you get python errors make sure you're not running python3
4344
- Some modules need sudo on some systems
45+
46+
# Changing the communication speed
47+
48+
49+
Edit the following lines:
50+
In the Python program modify the following line to switch to a speed of 115200baud / sec:
51+
52+
self.ser = serial.Serial (com, 115200, timeout = 1)
53+
In the program of your Arduino:
54+
55+
Serial.begin (115200);

flash.py renamed to flash27.py

File renamed without changes.

flash36.py

Lines changed: 116 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,116 @@
1+
#! python3.6
2+
import serial, sys, struct, time
3+
4+
5+
if len(sys.argv) != 3:
6+
print ("usage: %s <port> <firmware.hex>" % sys.argv[0])
7+
sys.exit(1)
8+
9+
class PI():
10+
def __init__(self, com):
11+
self.ser = serial.Serial(com, 1000000, timeout = 1)
12+
time.sleep(2)
13+
14+
def conf(self,):
15+
16+
# init Programming Interface (PI)
17+
while True:
18+
try:
19+
self.ser.write(b'\x01\x00')
20+
x=self.ser.read(1)
21+
assert x == b'\x81'
22+
break
23+
except:
24+
print ("error com")
25+
#while self.ser.read(1) != '': pass
26+
27+
print ("PI initiated")
28+
29+
30+
def prog(self, firmware):
31+
32+
print ("Connected")
33+
34+
#f = open(firmware,'r').readlines()
35+
f = firmware.splitlines()
36+
37+
self.conf()
38+
39+
# erase device
40+
self.ser.write(b'\x04\x00')
41+
assert self.ser.read(1)==b'\x84'
42+
43+
print ("Device erased")
44+
45+
# write hex file
46+
total = 0
47+
buf = ''
48+
buf_size = 0
49+
for i in f[1:-1]: # skip first and second lines
50+
assert(i[0] == ':')
51+
size = int(i[1:3],16)
52+
assert(size + 4 < 256)
53+
if buf_size == 0:
54+
addrh = int(i[3:5],16)
55+
addrl = int(i[5:7],16)
56+
assert(i[7:9] == '00')
57+
data = i[9:9 + size*2]
58+
assert(len(data) == size*2)
59+
buf += data
60+
buf_size += size
61+
62+
if buf_size > 256 - 0x20 or i == f[-2]:
63+
attempts = 0
64+
num=0
65+
while True:
66+
try:
67+
print (hex(addrh), hex(addrl), buf)
68+
crc = addrh + addrl
69+
for i in range(0,len(buf),2):
70+
val=buf[i]+buf[i+1]
71+
crc+=int(val,16)
72+
assert(len(buf)/2 == buf_size)
73+
self.ser.write([0x3, buf_size + 4 + 1, buf_size, 0, addrh, addrl, crc & 0xff])
74+
for i in range(0,len(buf),2):
75+
val=buf[i]+buf[i+1]
76+
string = bytes.fromhex(val)
77+
self.ser.write(string)
78+
ret =self.ser.read(1)
79+
if ret == b'\x83':
80+
pass
81+
else:
82+
print ("error flash write returned ", hex(ret))
83+
raise RuntimeError('bad crc')
84+
break
85+
except Exception as e:
86+
attempts += 1
87+
self.conf()
88+
print (e)
89+
print ("attempts:",attempts)
90+
total += buf_size
91+
buf_size = 0
92+
buf = ''
93+
print ("Wrote %d bytes" % total)
94+
95+
# reset device
96+
self.ser.write(b'\x02\x00')
97+
assert self.ser.read(1)==b'\x82'
98+
99+
# reset device
100+
self.ser.write(b'\x02\x00')
101+
assert self.ser.read(1)==b'\x82'
102+
103+
# reset device
104+
self.ser.write(b'\x02\x00')
105+
assert self.ser.read(1)==b'\x82'
106+
107+
108+
109+
print ("Device reset")
110+
111+
print ("Once")
112+
port=sys.argv[1]
113+
firmware=open(sys.argv[2], 'r').read()
114+
programmers = PI(port)
115+
116+
programmers.prog(firmware)

0 commit comments

Comments
 (0)