Skip to content

Commit

Permalink
Merge pull request bitcraze#508 from jonasdn:master
Browse files Browse the repository at this point in the history
Use new flash_full API
  • Loading branch information
ataffanel authored Apr 30, 2021
2 parents a8be26e + b3263e3 commit 089fb3b
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 62 deletions.
30 changes: 9 additions & 21 deletions src/cfclient/ui/dialogs/bootloader.py
Original file line number Diff line number Diff line change
Expand Up @@ -367,8 +367,6 @@ def __init__(self, crazyflie: cflib.crazyflie.Crazyflie):
self._terminate_flashing = False

self._bl = Bootloader()
self._bl.progress_cb = self.statusChanged.emit
self._bl.terminate_flashing_cb = lambda: self._terminate_flashing

self._cf = crazyflie

Expand Down Expand Up @@ -400,32 +398,22 @@ def initiateColdBoot(self, linkURI):
except Exception as e:
self.failed_signal.emit("{}".format(e))

def rebootToBootloader(self):
self._bl.clink = self._cf.link_uri
self._cf.close_link()

try:
success = self._bl.start_bootloader(warm_boot=True)
if not success:
self.failed_signal.emit("Could not connect to bootloader")
else:
self.connectedSignal.emit()
except Exception as e:
self.failed_signal.emit("{}".format(e))

def programAction(self, filename, mcu_to_flash):

if self._boot_mode == self.WARM_BOOT:
self.rebootToBootloader()

targets = {}
if mcu_to_flash:
targets[mcu_to_flash] = ("fw",)
try:
self._terminate_flashing = False
self._bl.flash(str(filename), targets)
self._bl.clink = self._cf.link_uri
self._bl.flash_full(str(filename),
self._boot_mode is self.WARM_BOOT,
targets,
None,
self.statusChanged.emit,
lambda: self._terminate_flashing)
self.programmed.emit(True)
except Exception:
except Exception as e:
self.failed_signal.emit("{}".format(e))
self.programmed.emit(False)

def terminate_flashing(self):
Expand Down
62 changes: 21 additions & 41 deletions src/cfloader/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@

import cflib.crtp
from cflib.bootloader import Bootloader, Target
from cflib.bootloader.boottypes import BootVersion, TargetTypes
from cflib.bootloader.boottypes import BootVersion

from typing import Optional, List

Expand Down Expand Up @@ -108,7 +108,6 @@ def main():
elif sys.argv[0] == "reset":
action = "reset"
elif sys.argv[0] == "flash":
# print len(sys.argv)
if len(sys.argv) < 2:
print("The flash action require a file name.")
link.close()
Expand All @@ -128,62 +127,43 @@ def main():
link.close()
sys.exit(-1)

# Currently there's two different targets available
connected_targets = ()

try:
# Initialise the bootloader lib
bl = Bootloader(clink)

#########################################
# Get the connection with the bootloader
#########################################
# The connection is done by reseting to the bootloader (default)
if boot == "reset":
print("Reset to bootloader mode ..."),
warm_boot = (boot == "reset")
if warm_boot:
print("Reset to bootloader mode ...")
sys.stdout.flush()
if bl.start_bootloader(warm_boot=True):
print(" done!")
else:
print("Failed to warmboot")
bl.close()
sys.exit(-1)
else: # The connection is done by a cold boot ...
print("Restart the Crazyflie you want to bootload in the next"),
print(" 10 seconds ..."),

sys.stdout.flush()
if bl.start_bootloader(warm_boot=False):
print(" done!")
else:
print("Cannot connect the bootloader!")
bl.close()
sys.exit(-1)

print("Connected to bootloader on {} (version=0x{:X})".format(
BootVersion.to_ver_string(bl.protocol_version),
bl.protocol_version))

if bl.protocol_version == BootVersion.CF2_PROTO_VER:
connected_targets += (bl.get_target(TargetTypes.NRF51),)
connected_targets += (bl.get_target(TargetTypes.STM32),)

######################################
# Doing something (hopefully) useful
######################################

# Print information about the targets
for target in connected_targets:
print(target)
if action == "info":
None # Already done ...
elif action == "reset":
print
print("Reset in firmware mode ...")
bl.reset_to_firmware()
def print_info(version: int, connected_targets: [Target]):
print("Connected to bootloader on {} (version=0x{:X})".format(
BootVersion.to_ver_string(version),
version
)
)
for target in connected_targets:
print(target)

# flash_full called with no filename will not flash, just call
# our info callback
bl.flash_full(None, warm_boot, None, print_info)
elif action == "flash" and filename and targets:
bl.flash(filename, targets)
print("Reset in firmware mode ...")
try:
bl.flash_full(filename, warm_boot, targets)
except Exception as e:
print("Failed to flash: {}".format(e))
elif action == "reset":
bl.reset_to_firmware()
else:
None
Expand Down

0 comments on commit 089fb3b

Please sign in to comment.