Skip to content

Commit 1db8018

Browse files
committed
added firmware flasher script
1 parent 2d941c4 commit 1db8018

File tree

2 files changed

+79
-0
lines changed

2 files changed

+79
-0
lines changed

user_program/flash_fw.py

Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
import os
2+
import sys
3+
import time
4+
import spidev
5+
import RPi.GPIO as GPIO
6+
7+
PBOARD_RESET_PIN = 25
8+
PBOARD_BOOT0_PIN = 12
9+
SLAVE_REQ_PIN = 16
10+
GPIO.setmode(GPIO.BCM)
11+
GPIO.setup(PBOARD_RESET_PIN, GPIO.IN)
12+
GPIO.setup(PBOARD_BOOT0_PIN, GPIO.IN)
13+
GPIO.setup(SLAVE_REQ_PIN, GPIO.IN, pull_up_down=GPIO.PUD_DOWN)
14+
15+
def enter_dfu():
16+
# RESET LOW: Enter reset
17+
GPIO.setup(PBOARD_RESET_PIN, GPIO.OUT)
18+
GPIO.output(PBOARD_RESET_PIN, GPIO.LOW)
19+
time.sleep(0.05)
20+
# BOOT0 HIGH: Boot into DFU mode
21+
GPIO.setup(PBOARD_BOOT0_PIN, GPIO.OUT)
22+
GPIO.output(PBOARD_BOOT0_PIN, GPIO.HIGH)
23+
time.sleep(0.05)
24+
# Release RESET, BOOT0 still HIGH, STM32 now in DFU mode
25+
GPIO.setup(PBOARD_RESET_PIN, GPIO.IN)
26+
time.sleep(1)
27+
28+
def exit_dfu():
29+
# Release BOOT0
30+
GPIO.setup(PBOARD_BOOT0_PIN, GPIO.IN)
31+
# Activate RESET
32+
GPIO.setup(PBOARD_RESET_PIN, GPIO.OUT)
33+
GPIO.output(PBOARD_RESET_PIN, GPIO.LOW)
34+
time.sleep(0.05)
35+
# Release RESET, BOOT0 is LOW, STM32 boots in normal mode
36+
GPIO.setup(PBOARD_RESET_PIN, GPIO.IN)
37+
time.sleep(0.2)
38+
39+
def flash_firmware(fw_path):
40+
print(f"----------------- Flashing: {fw_path.split('/')[-1]} -----------------")
41+
enter_dfu()
42+
exit_code = os.system(f'sudo stm32flash -w {fw_path} -a 0x3b /dev/i2c-1') >> 8
43+
exit_dfu()
44+
if exit_code != 0:
45+
for x in range(5):
46+
print("!!!!!!!!!!!!!!!!! TEST FLASH FAILED !!!!!!!!!!!!!!!!!")
47+
exit()
48+
49+
if(len(sys.argv) < 2):
50+
print(sys.argv[0] + ' hex_file')
51+
exit()
52+
53+
os.system("clear")
54+
55+
pcard_spi = spidev.SpiDev(0, 0)
56+
pcard_spi.max_speed_hz = 2000000
57+
58+
payload_fw_path = sys.argv[1]
59+
60+
flash_firmware(payload_fw_path)
61+
62+
SPI_MOSI_MAGIC = 0xde
63+
SPI_MOSI_MSG_TYPE_INFO_REQUEST = 1
64+
65+
nop_spi_msg_template = [SPI_MOSI_MAGIC] + [0]*31
66+
info_request_spi_msg_template = [SPI_MOSI_MAGIC, 0, SPI_MOSI_MSG_TYPE_INFO_REQUEST] + [0]*29
67+
68+
this_msg = list(info_request_spi_msg_template)
69+
pcard_spi.xfer(this_msg)
70+
time.sleep(0.1)
71+
response = pcard_spi.xfer(list(nop_spi_msg_template))
72+
time.sleep(0.1)
73+
print(response)
74+
75+
if response[0] != 205:
76+
for x in range(5):
77+
print("!!!!!!!!!!!!!!!!! WRONG RESPONSE !!!!!!!!!!!!!!!!!")
78+
else:
79+
print("----------------- TEST OK! -----------------")

0 commit comments

Comments
 (0)