Skip to content

mod: flash FW from arduino_alvik module #17

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Mar 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
52 changes: 46 additions & 6 deletions arduino_alvik/arduino_alvik.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@


class ArduinoAlvik:

_update_thread_running = False
_update_thread_id = None
_touch_events_thread_running = False
Expand All @@ -32,9 +31,9 @@ def __init__(self):
self.right_wheel = _ArduinoAlvikWheel(self._packeter, ord('R'))
self._led_state = list((None,))
self.left_led = self.DL1 = _ArduinoAlvikRgbLed(self._packeter, 'left', self._led_state,
rgb_mask=[0b00000100, 0b00001000, 0b00010000])
rgb_mask=[0b00000100, 0b00001000, 0b00010000])
self.right_led = self.DL2 = _ArduinoAlvikRgbLed(self._packeter, 'right', self._led_state,
rgb_mask=[0b00100000, 0b01000000, 0b10000000])
rgb_mask=[0b00100000, 0b01000000, 0b10000000])
self._battery_perc = None
self._touch_byte = None
self._behaviour = None
Expand Down Expand Up @@ -234,7 +233,7 @@ def _stop_update_thread(cls):
def _wait_for_target(self, idle_time):
start = ticks_ms()
while True:
if ticks_diff(ticks_ms(), start) >= idle_time*1000 and self.is_target_reached():
if ticks_diff(ticks_ms(), start) >= idle_time * 1000 and self.is_target_reached():
break
else:
# print(self._last_ack)
Expand Down Expand Up @@ -280,7 +279,7 @@ def rotate(self, angle: float, unit: str = 'deg', blocking: bool = True):
uart.write(self._packeter.msg[0:self._packeter.msg_size])
self._waiting_ack = ord('R')
if blocking:
self._wait_for_target(idle_time=(angle/MOTOR_CONTROL_DEG_S))
self._wait_for_target(idle_time=(angle / MOTOR_CONTROL_DEG_S))

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

def stop(self):
"""
Expand Down Expand Up @@ -1337,3 +1336,44 @@ def register_callback(self, event_name: str, callback: callable, args: tuple = N
if event_name not in self.__class__.available_events:
return
super().register_callback(event_name, callback, args)


# UPDATE FIRMWARE METHOD #

def update_firmware(file_path: str):
"""

:param file_path: path of your FW bin
:return:
"""

from sys import exit
from stm32_flash import (
CHECK_STM32,
STM32_endCommunication,
STM32_startCommunication,
STM32_NACK,
STM32_eraseMEM,
STM32_writeMEM, )

if CHECK_STM32.value() is not 1:
print("Turn on your Alvik to continue...")
while CHECK_STM32.value() is not 1:
sleep_ms(500)

ans = STM32_startCommunication()
if ans == STM32_NACK:
print("Cannot establish connection with STM32")
exit(-1)

print('\nSTM32 FOUND')

print('\nERASING MEM')
STM32_eraseMEM(0xFFFF)

print("\nWRITING MEM")
STM32_writeMEM(file_path)
print("\nDONE")
print("\nLower Boot0 and reset STM32")

STM32_endCommunication()
24 changes: 0 additions & 24 deletions arduino_alvik/firmware_updater.py

This file was deleted.

5 changes: 5 additions & 0 deletions examples/flash_firmware.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# from machine import reset
from arduino_alvik import update_firmware

update_firmware('./firmware.bin')
# reset()
1 change: 1 addition & 0 deletions install.bat
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ python -m mpremote %port_string% fs cp arduino_alvik/constants.py :lib/arduino_a
python -m mpremote %port_string% fs cp arduino_alvik/conversions.py :lib/arduino_alvik/conversions.py
python -m mpremote %port_string% fs cp arduino_alvik/pinout_definitions.py :lib/arduino_alvik/pinout_definitions.py
python -m mpremote %port_string% fs cp arduino_alvik/robot_definitions.py :lib/arduino_alvik/robot_definitions.py
python -m mpremote %port_string% fs cp arduino_alvik/stm32_flash.py :lib/arduino_alvik/stm32_flash.py
python -m mpremote %port_string% fs cp arduino_alvik/uart.py :lib/arduino_alvik/uart.py

echo Installing dependencies
Expand Down
1 change: 1 addition & 0 deletions install.sh
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ $python_command -m mpremote $connect_string fs cp arduino_alvik/constants.py :li
$python_command -m mpremote $connect_string fs cp arduino_alvik/conversions.py :lib/arduino_alvik/conversions.py
$python_command -m mpremote $connect_string fs cp arduino_alvik/pinout_definitions.py :lib/arduino_alvik/pinout_definitions.py
$python_command -m mpremote $connect_string fs cp arduino_alvik/robot_definitions.py :lib/arduino_alvik/robot_definitions.py
$python_command -m mpremote $connect_string fs cp arduino_alvik/stm32_flash.py :lib/arduino_alvik/stm32_flash.py
$python_command -m mpremote $connect_string fs cp arduino_alvik/uart.py :lib/arduino_alvik/uart.py

echo "Installing dependencies"
Expand Down
3 changes: 1 addition & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,10 @@
["arduino_alvik/pinout_definitions.py", "github:arduino/arduino-alvik-mpy/arduino_alvik/pinout_definitions.py"],
["arduino_alvik/robot_definitions.py", "github:arduino/arduino-alvik-mpy/arduino_alvik/robot_definitions.py"],
["arduino_alvik/uart.py", "github:arduino/arduino-alvik-mpy/arduino_alvik/uart.py"],
["arduino_alvik/firmware_updater.py", "github:arduino/arduino-alvik-mpy/arduino_alvik/firmware_updater.py"],
["arduino_alvik/stm32_flash.py", "github:arduino/arduino-alvik-mpy/arduino_alvik/stm32_flash.py"]
],
"deps": [
["github:arduino/ucPack-mpy", "0.1.5"]
],
"version": "0.3.0"
"version": "0.4.0"
}
7 changes: 1 addition & 6 deletions utilities/flash_firmware.bat
Original file line number Diff line number Diff line change
Expand Up @@ -24,19 +24,14 @@ if "%1"=="" (
set "filename=%1"
)

echo Installing flash firmware utilities...

python -m mpremote %port_string% fs cp ../arduino_alvik/firmware_updater.py :firmware_updater.py
python -m mpremote %port_string% fs cp ../arduino_alvik/stm32_flash.py :stm32_flash.py

echo Uploading %filename%

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

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

if /i "%userInput%"=="y" (
python -m mpremote %port_string% run ../arduino_alvik/firmware_updater.py
python -m mpremote %port_string% run ../examples/flash_firmware.py
) else (
echo The firmware will not be written to the device.
)
Expand Down
7 changes: 1 addition & 6 deletions utilities/flash_firmware.sh
Original file line number Diff line number Diff line change
Expand Up @@ -51,11 +51,6 @@ fi
# Uncomment the following line on windows machines
# python_command="python"

echo "Installing flash firmware utilities..."

$python_command -m mpremote $connect_string fs cp ../arduino_alvik/firmware_updater.py :firmware_updater.py
$python_command -m mpremote $connect_string fs cp ../arduino_alvik/stm32_flash.py :stm32_flash.py

echo "Uploading $filename..."

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

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