forked from husarion/rosbot-docker
-
Notifications
You must be signed in to change notification settings - Fork 0
/
flash-firmware.py
executable file
·123 lines (87 loc) · 3.1 KB
/
flash-firmware.py
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
#!/usr/bin/python3
import sh
import time
import sys
import argparse
from periphery import GPIO
class FirmwareFlasher:
def __init__(self, sys_arch, binary_file):
self.binary_file = binary_file
self.sys_arch = sys_arch
self.max_approach_no = 5
print(f"System architecture: {self.sys_arch}")
if self.sys_arch.stdout == b'armv7l\n':
# Setups ThinkerBoard pins
print("Device: ThinkerBoard\n")
self.port = "/dev/ttyS1"
boot0_pin_no = 164
reset_pin_no = 184
elif self.sys_arch.stdout == b'x86_64\n':
# Setups UpBoard pins
print("Device: UpBoard\n")
self.port = "/dev/ttyS4"
boot0_pin_no = 17
reset_pin_no = 18
elif self.sys_arch.stdout == b'aarch64\n':
# Setups RPi pins
print("Device: RPi\n")
self.port = "/dev/ttyAMA0"
boot0_pin_no = 17
reset_pin_no = 18
else:
print("Unknown device...")
self.boot0_pin = GPIO(boot0_pin_no, "out")
self.reset_pin = GPIO(reset_pin_no, "out")
def enter_bootloader_mode(self):
self.boot0_pin.write(True)
self.reset_pin.write(True)
time.sleep(0.2)
self.reset_pin.write(False)
time.sleep(0.2)
def exit_bootloader_mode(self):
self.boot0_pin.write(False)
self.reset_pin.write(True)
time.sleep(0.2)
self.reset_pin.write(False)
time.sleep(0.2)
def flash_firmware(self):
self.enter_bootloader_mode()
# Flashing the firmware
succes_no = 0
for i in range(self.max_approach_no):
try:
if succes_no == 0:
# Disable the flash write-protection
sh.stm32flash(self.port, "-u", _out=sys.stdout)
time.sleep(0.2)
succes_no += 1
if succes_no == 1:
# Disable the flash read-protection
sh.stm32flash(self.port, "-k", _out=sys.stdout)
time.sleep(0.2)
succes_no += 1
if succes_no == 2:
# Flashing the firmware
sh.stm32flash(self.port, "-v", w=self.binary_file, b="115200", _out=sys.stdout)
time.sleep(0.2)
break
except:
pass
else:
print('ERROR! Something goes wrong. Try again.')
self.exit_bootloader_mode()
def main():
parser = argparse.ArgumentParser(
description='Flashing the firmware on STM32 microcontroller in ROSbot')
parser.add_argument(
"file",
nargs='?',
default="/root/firmware_diff.bin",
help="Path to a firmware file. Default = /root/firmware_diff.bin")
binary_file = parser.parse_args().file
sys_arch = sh.uname('-m')
flasher = FirmwareFlasher(sys_arch, binary_file)
flasher.flash_firmware()
print("Done.")
if __name__ == "__main__":
main()