Skip to content

Commit 72fc37a

Browse files
committed
adds bootloader support
1 parent b0126a9 commit 72fc37a

17 files changed

Lines changed: 2115 additions & 39 deletions

src/bootloader/Bootloader.hex

Lines changed: 433 additions & 0 deletions
Large diffs are not rendered by default.

src/bootloader/erase_chip.bat

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
ST-LINK_CLI.exe -c SWD SWCLK=9 -ME

src/bootloader/flashbootloader.bat

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
ST-LINK_CLI.exe -c SWD SWCLK=9 -P Bootloader.hex -RST

src/bootloader/reboot_chip.bat

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
ST-LINK_CLI.exe -RST

src/platformio.ini

Lines changed: 30 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -113,14 +113,10 @@ build_flags =
113113
;board_build.f_cpu = 80000000L
114114
src_filter = ${env:ExpLRS_RX_V3.src_filter}
115115

116-
[env:R9M_RX_STM32F301]
116+
[env:R9M_RX_STM32F301_STLINK]
117117
platform = ststm32
118118
framework = arduino
119119
board = NUCLEO_F103RB
120-
;board = PILL_F103XX
121-
;board = genericSTM32F103RB
122-
;board_build.variant = PILL_F103XX
123-
;board_build.core = stm32
124120
build_unflags = -Os ;disable default optimistation level
125121
build_flags =
126122
-D TARGET_R9M_RX
@@ -130,11 +126,38 @@ build_flags =
130126
-O3
131127
-DVECT_TAB_OFFSET=0x08008000U
132128
-Wl,-TSTM32Linker.ld ;customer linker with memory offset for bootloader
133-
134129
src_filter = ${common_env_data.src_filter} -<ESP32*.*> -<ESP8266*.*> -<WS281B*.*> -<tx_*.cpp>
135130
upload_protocol = custom
136131
upload_command = $PROJECT_PACKAGES_DIR/tool-stm32duino/stlink/ST-LINK_CLI.exe -c SWD SWCLK=9 -P $SOURCE 0x08008000 -RST
137132

133+
[env:R9M_RX_STM32F301_BetaflightPassthrough]
134+
platform = ${env:R9M_RX_STM32F301_STLINK.platform}
135+
framework = ${env:R9M_RX_STM32F301_STLINK.framework}
136+
board = ${env:R9M_RX_STM32F301_STLINK.board}
137+
build_unflags = ${env:R9M_RX_STM32F301_STLINK.build_unflags}
138+
build_flags = ${env:R9M_RX_STM32F301_STLINK.build_flags}
139+
src_filter = ${env:R9M_RX_STM32F301_STLINK.src_filter}
140+
upload_protocol = custom
141+
upload_command =
142+
python python/BFinitPassthrough.py
143+
python python/UARTupload.py $SOURCE
144+
145+
146+
147+
148+
149+
150+
151+
152+
153+
154+
155+
156+
157+
158+
159+
160+
138161
;build.vect=VECT_TAB_ADDR=0x08008000
139162
;build.vect=VECT_TAB_ADDR
140163
; change MCU frequency
@@ -152,4 +175,4 @@ upload_command = $PROJECT_PACKAGES_DIR/tool-stm32duino/stlink/ST-LINK_CLI.exe -c
152175

153176

154177
[common_env_data]
155-
src_filter = +<*> -<.git/> -<svn/> -<example/> -<examples/> -<test/> -<tests/>
178+
src_filter = +<*> -<.git/> -<svn/> -<example/> -<examples/> -<test/> -<tests/> -<*.py>

src/python/BFinitPassthrough.py

Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
import serial
2+
from xmodem import XMODEM
3+
import time
4+
import sys
5+
6+
result = []
7+
8+
def serial_ports():
9+
""" Lists serial port names
10+
11+
:raises EnvironmentError:
12+
On unsupported or unknown platforms
13+
:returns:
14+
A list of the serial ports available on the system
15+
"""
16+
if sys.platform.startswith('win'):
17+
ports = ['COM%s' % (i + 1) for i in range(256)]
18+
elif sys.platform.startswith('linux') or sys.platform.startswith('cygwin'):
19+
# this excludes your current terminal "/dev/tty"
20+
ports = glob.glob('/dev/tty[A-Za-z]*')
21+
elif sys.platform.startswith('darwin'):
22+
ports = glob.glob('/dev/tty.*')
23+
else:
24+
raise EnvironmentError('Unsupported platform')
25+
26+
for port in ports:
27+
try:
28+
s = serial.Serial(port)
29+
s.close()
30+
result.append(port)
31+
except (OSError, serial.SerialException):
32+
pass
33+
return result
34+
35+
if __name__ == '__main__':
36+
print(serial_ports())
37+
38+
print("Going to try using "+ result[0])
39+
40+
s = serial.Serial(port=result[0], baudrate=115200, bytesize=8, parity='N', stopbits=1, timeout=2, xonxoff=0, rtscts=0)
41+
42+
s.write(chr(0x23).encode())
43+
time.sleep(0.2)
44+
s.write(("serial\n").encode())
45+
time.sleep(0.2)
46+
47+
lines = []
48+
serialInfo = []
49+
SerialInfoSplit = []
50+
51+
SerialRXindex = -1
52+
uartNumber = -1
53+
print()
54+
print()
55+
print("Attempting to detect Betaflight RX configuration...")
56+
57+
while s.in_waiting:
58+
reading = s.readline().decode('utf-8')
59+
if "serial" in reading:
60+
lines.append(reading)
61+
print(reading)
62+
63+
for line in lines:
64+
if line[0:6] == "serial":
65+
serialInfo.append(line)
66+
67+
for line in serialInfo:
68+
data = line.split()
69+
SerialInfoSplit.append(data)
70+
71+
for i in range(0,len(serialInfo)):
72+
data = SerialInfoSplit[i][2]
73+
if(data == "64"):
74+
uartNumber = data
75+
SerialRXindex = i
76+
77+
if uartNumber != -1:
78+
print("Detected Betaflight Serial RX config: " +str(serialInfo[SerialRXindex]))
79+
else:
80+
print("Failed to detect correct serial RX config, possibly already configured?")
81+
print("If next step fails please reboot FC")
82+
print()
83+
print()
84+
sys.exit()
85+
86+
print("Setting serial passthrough...")
87+
s.write(("serialpassthrough "+str(SerialInfoSplit[SerialRXindex][1])+" 420000\n").encode())
88+
time.sleep(0.2)
89+
90+
while s.in_waiting:
91+
reading = s.readline().decode('utf-8')
92+
lines.append(reading)
93+
94+
print()
95+
print()

src/python/UARTupload.py

Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
import serial
2+
from xmodem import XMODEM
3+
import time
4+
import sys
5+
6+
result = []
7+
8+
filename = ''
9+
10+
BootloaderInitSeq1 = bytes([0xEC,0x04,0x32,0x62,0x6c,0x0A])
11+
BootloaderInitSeq2 = bytes([0x62,0x62,0x62,0x62,0x62,0x62])
12+
13+
def StatusCallback(total_packets, success_count, error_count):
14+
if(total_packets % 10 == 0):
15+
print("Pkts: " + str(total_packets) + " err: " +str(error_count))
16+
17+
def getc(size, timeout=1):
18+
return s.read(size)
19+
20+
def putc(data, timeout=1):
21+
s.write(data)
22+
23+
def serial_ports():
24+
""" Lists serial port names
25+
26+
:raises EnvironmentError:
27+
On unsupported or unknown platforms
28+
:returns:
29+
A list of the serial ports available on the system
30+
"""
31+
if sys.platform.startswith('win'):
32+
ports = ['COM%s' % (i + 1) for i in range(256)]
33+
elif sys.platform.startswith('linux') or sys.platform.startswith('cygwin'):
34+
# this excludes your current terminal "/dev/tty"
35+
ports = glob.glob('/dev/tty[A-Za-z]*')
36+
elif sys.platform.startswith('darwin'):
37+
ports = glob.glob('/dev/tty.*')
38+
else:
39+
raise EnvironmentError('Unsupported platform')
40+
41+
for port in ports:
42+
try:
43+
s = serial.Serial(port)
44+
s.close()
45+
result.append(port)
46+
except (OSError, serial.SerialException):
47+
pass
48+
return result
49+
50+
if __name__ == '__main__':
51+
print(serial_ports())
52+
53+
print("Going to use "+ result[0])
54+
55+
s = serial.Serial(port=result[0], baudrate=420000, bytesize=8, parity='N', stopbits=1, timeout=None, xonxoff=0, rtscts=0)
56+
57+
s.write(BootloaderInitSeq1)
58+
time.sleep(1)
59+
s.write(BootloaderInitSeq2)
60+
61+
s.close()
62+
time.sleep(1)
63+
s.open()
64+
65+
try:
66+
filename = sys.argv[1]
67+
except:
68+
filename = 'firmware.bin'
69+
70+
71+
modem = XMODEM(getc, putc)
72+
stream = open(filename, 'rb')
73+
status = modem.send(stream, retry=8, callback=StatusCallback)
74+
75+
if(status):
76+
print("Success!!!!")
77+
else:
78+
print("FAILED")
79+
80+
s.close()
81+
stream.close()
14.4 KB
Binary file not shown.
18.7 KB
Binary file not shown.

src/python/firmware.bin

41.8 KB
Binary file not shown.

0 commit comments

Comments
 (0)