Skip to content

Commit

Permalink
flash: Patch City Hub on Windows
Browse files Browse the repository at this point in the history
Workaround for pybricks/support#256
  • Loading branch information
laurensvalk committed May 15, 2021
1 parent fa679a6 commit 241c534
Showing 1 changed file with 9 additions and 13 deletions.
22 changes: 9 additions & 13 deletions pybricksdev/flash.py
Original file line number Diff line number Diff line change
Expand Up @@ -234,13 +234,6 @@ class BootloaderConnection(BLERequestsConnection):
0x11, 'Erase', ['result'], '<B', write_with_response=False
)

# City hub bootloader always sends write response for most commands even
# when write without response is used which confuses Bluetooth stacks, so
# we always have to do write with response.
ERASE_FLASH_CITY_HUB = BootloaderRequest(
0x11, 'Erase', ['result'], '<B'
)

# Only the final flash message receives a reply.
PROGRAM_FLASH = BootloaderRequest(
0x22, 'Flash', [], '', request_reply=False, write_with_response=False
Expand Down Expand Up @@ -319,16 +312,19 @@ async def flash(self, firmware, metadata):
# Erase existing firmware
logger.debug("Erasing flash.")
try:
response = await self.bootloader_request(
self.ERASE_FLASH_CITY_HUB
if info.type_id == HubTypeId.CITY_HUB
else self.ERASE_FLASH,
timeout=5
)
response = await self.bootloader_request(self.ERASE_FLASH, timeout=5)
logger.debug(response)
except asyncio.TimeoutError:
logger.info("did not receive erase reply, continuing anyway")

# On City Hub with some Windows versions, the reply to the flash command is not
# received by this script until after the next message is sent. This confuses
# messages that come after. Here we send a bogus message without checking what
# comes back to flush that response.
if info.type_id == HubTypeId.CITY_HUB:
response = await self.write(bytearray((self.GET_CHECKSUM.command,)), True)
await asyncio.sleep(2)

# Get the bootloader ready to accept the firmware
logger.debug('Request begin update.')
response = await self.bootloader_request(
Expand Down

0 comments on commit 241c534

Please sign in to comment.