Skip to content

Commit 8ccf676

Browse files
authored
Merge pull request #17 from arduino/new-firmware-updater
mod: flash FW from arduino_alvik module
2 parents e832a0d + a1d6d4c commit 8ccf676

File tree

8 files changed

+56
-44
lines changed

8 files changed

+56
-44
lines changed

arduino_alvik/arduino_alvik.py

+46-6
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@
1515

1616

1717
class ArduinoAlvik:
18-
1918
_update_thread_running = False
2019
_update_thread_id = None
2120
_touch_events_thread_running = False
@@ -32,9 +31,9 @@ def __init__(self):
3231
self.right_wheel = _ArduinoAlvikWheel(self._packeter, ord('R'))
3332
self._led_state = list((None,))
3433
self.left_led = self.DL1 = _ArduinoAlvikRgbLed(self._packeter, 'left', self._led_state,
35-
rgb_mask=[0b00000100, 0b00001000, 0b00010000])
34+
rgb_mask=[0b00000100, 0b00001000, 0b00010000])
3635
self.right_led = self.DL2 = _ArduinoAlvikRgbLed(self._packeter, 'right', self._led_state,
37-
rgb_mask=[0b00100000, 0b01000000, 0b10000000])
36+
rgb_mask=[0b00100000, 0b01000000, 0b10000000])
3837
self._battery_perc = None
3938
self._touch_byte = None
4039
self._behaviour = None
@@ -234,7 +233,7 @@ def _stop_update_thread(cls):
234233
def _wait_for_target(self, idle_time):
235234
start = ticks_ms()
236235
while True:
237-
if ticks_diff(ticks_ms(), start) >= idle_time*1000 and self.is_target_reached():
236+
if ticks_diff(ticks_ms(), start) >= idle_time * 1000 and self.is_target_reached():
238237
break
239238
else:
240239
# print(self._last_ack)
@@ -280,7 +279,7 @@ def rotate(self, angle: float, unit: str = 'deg', blocking: bool = True):
280279
uart.write(self._packeter.msg[0:self._packeter.msg_size])
281280
self._waiting_ack = ord('R')
282281
if blocking:
283-
self._wait_for_target(idle_time=(angle/MOTOR_CONTROL_DEG_S))
282+
self._wait_for_target(idle_time=(angle / MOTOR_CONTROL_DEG_S))
284283

285284
def move(self, distance: float, unit: str = 'cm', blocking: bool = True):
286285
"""
@@ -296,7 +295,7 @@ def move(self, distance: float, unit: str = 'cm', blocking: bool = True):
296295
uart.write(self._packeter.msg[0:self._packeter.msg_size])
297296
self._waiting_ack = ord('M')
298297
if blocking:
299-
self._wait_for_target(idle_time=(distance/MOTOR_CONTROL_MM_S))
298+
self._wait_for_target(idle_time=(distance / MOTOR_CONTROL_MM_S))
300299

301300
def stop(self):
302301
"""
@@ -1337,3 +1336,44 @@ def register_callback(self, event_name: str, callback: callable, args: tuple = N
13371336
if event_name not in self.__class__.available_events:
13381337
return
13391338
super().register_callback(event_name, callback, args)
1339+
1340+
1341+
# UPDATE FIRMWARE METHOD #
1342+
1343+
def update_firmware(file_path: str):
1344+
"""
1345+
1346+
:param file_path: path of your FW bin
1347+
:return:
1348+
"""
1349+
1350+
from sys import exit
1351+
from stm32_flash import (
1352+
CHECK_STM32,
1353+
STM32_endCommunication,
1354+
STM32_startCommunication,
1355+
STM32_NACK,
1356+
STM32_eraseMEM,
1357+
STM32_writeMEM, )
1358+
1359+
if CHECK_STM32.value() is not 1:
1360+
print("Turn on your Alvik to continue...")
1361+
while CHECK_STM32.value() is not 1:
1362+
sleep_ms(500)
1363+
1364+
ans = STM32_startCommunication()
1365+
if ans == STM32_NACK:
1366+
print("Cannot establish connection with STM32")
1367+
exit(-1)
1368+
1369+
print('\nSTM32 FOUND')
1370+
1371+
print('\nERASING MEM')
1372+
STM32_eraseMEM(0xFFFF)
1373+
1374+
print("\nWRITING MEM")
1375+
STM32_writeMEM(file_path)
1376+
print("\nDONE")
1377+
print("\nLower Boot0 and reset STM32")
1378+
1379+
STM32_endCommunication()

arduino_alvik/firmware_updater.py

-24
This file was deleted.

examples/flash_firmware.py

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
# from machine import reset
2+
from arduino_alvik import update_firmware
3+
4+
update_firmware('./firmware.bin')
5+
# reset()

install.bat

+1
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ python -m mpremote %port_string% fs cp arduino_alvik/constants.py :lib/arduino_a
2525
python -m mpremote %port_string% fs cp arduino_alvik/conversions.py :lib/arduino_alvik/conversions.py
2626
python -m mpremote %port_string% fs cp arduino_alvik/pinout_definitions.py :lib/arduino_alvik/pinout_definitions.py
2727
python -m mpremote %port_string% fs cp arduino_alvik/robot_definitions.py :lib/arduino_alvik/robot_definitions.py
28+
python -m mpremote %port_string% fs cp arduino_alvik/stm32_flash.py :lib/arduino_alvik/stm32_flash.py
2829
python -m mpremote %port_string% fs cp arduino_alvik/uart.py :lib/arduino_alvik/uart.py
2930

3031
echo Installing dependencies

install.sh

+1
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ $python_command -m mpremote $connect_string fs cp arduino_alvik/constants.py :li
5050
$python_command -m mpremote $connect_string fs cp arduino_alvik/conversions.py :lib/arduino_alvik/conversions.py
5151
$python_command -m mpremote $connect_string fs cp arduino_alvik/pinout_definitions.py :lib/arduino_alvik/pinout_definitions.py
5252
$python_command -m mpremote $connect_string fs cp arduino_alvik/robot_definitions.py :lib/arduino_alvik/robot_definitions.py
53+
$python_command -m mpremote $connect_string fs cp arduino_alvik/stm32_flash.py :lib/arduino_alvik/stm32_flash.py
5354
$python_command -m mpremote $connect_string fs cp arduino_alvik/uart.py :lib/arduino_alvik/uart.py
5455

5556
echo "Installing dependencies"

package.json

+1-2
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,10 @@
77
["arduino_alvik/pinout_definitions.py", "github:arduino/arduino-alvik-mpy/arduino_alvik/pinout_definitions.py"],
88
["arduino_alvik/robot_definitions.py", "github:arduino/arduino-alvik-mpy/arduino_alvik/robot_definitions.py"],
99
["arduino_alvik/uart.py", "github:arduino/arduino-alvik-mpy/arduino_alvik/uart.py"],
10-
["arduino_alvik/firmware_updater.py", "github:arduino/arduino-alvik-mpy/arduino_alvik/firmware_updater.py"],
1110
["arduino_alvik/stm32_flash.py", "github:arduino/arduino-alvik-mpy/arduino_alvik/stm32_flash.py"]
1211
],
1312
"deps": [
1413
["github:arduino/ucPack-mpy", "0.1.5"]
1514
],
16-
"version": "0.3.0"
15+
"version": "0.4.0"
1716
}

utilities/flash_firmware.bat

+1-6
Original file line numberDiff line numberDiff line change
@@ -24,19 +24,14 @@ if "%1"=="" (
2424
set "filename=%1"
2525
)
2626

27-
echo Installing flash firmware utilities...
28-
29-
python -m mpremote %port_string% fs cp ../arduino_alvik/firmware_updater.py :firmware_updater.py
30-
python -m mpremote %port_string% fs cp ../arduino_alvik/stm32_flash.py :stm32_flash.py
31-
3227
echo Uploading %filename%
3328

3429
python -m mpremote %port_string% fs cp %filename% :firmware.bin
3530

3631
set /p userInput=Do you want to flash the firmware right now? (y/N):
3732

3833
if /i "%userInput%"=="y" (
39-
python -m mpremote %port_string% run ../arduino_alvik/firmware_updater.py
34+
python -m mpremote %port_string% run ../examples/flash_firmware.py
4035
) else (
4136
echo The firmware will not be written to the device.
4237
)

utilities/flash_firmware.sh

+1-6
Original file line numberDiff line numberDiff line change
@@ -51,11 +51,6 @@ fi
5151
# Uncomment the following line on windows machines
5252
# python_command="python"
5353

54-
echo "Installing flash firmware utilities..."
55-
56-
$python_command -m mpremote $connect_string fs cp ../arduino_alvik/firmware_updater.py :firmware_updater.py
57-
$python_command -m mpremote $connect_string fs cp ../arduino_alvik/stm32_flash.py :stm32_flash.py
58-
5954
echo "Uploading $filename..."
6055

6156
$python_command -m mpremote $connect_string fs cp $filename :firmware.bin
@@ -64,7 +59,7 @@ echo "Do you want to flash the firmware right now? (y/N)"
6459
read do_flash
6560

6661
if [ "$do_flash" == "y" ] || [ "$do_flash" == "Y" ]; then
67-
$python_command -m mpremote $connect_string run ../arduino_alvik/firmware_updater.py
62+
$python_command -m mpremote $connect_string run ../examples/flash_firmware.py
6863
else
6964
echo "The firmware will not be written to the device."
7065
fi

0 commit comments

Comments
 (0)