Skip to content

Commit

Permalink
[SC64][SW] SW programing bug fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
Polprzewodnikowy committed Jan 24, 2023
1 parent 6def5b0 commit 3fc6229
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 5 deletions.
10 changes: 9 additions & 1 deletion sw/pc/primer.py
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,12 @@ def __load_chunk(self, f: io.BufferedReader) -> tuple[int, bytes]:
return (id, data)

def load(self, path: str, require_all: bool=False) -> None:
self.__update_info = None
self.__mcu_data = None
self.__fpga_data = None
self.__bootloader_data = None
self.__primer_data = None

try:
with open(path, 'rb') as f:
if (f.read(len(self.__UPDATE_TOKEN)) != self.__UPDATE_TOKEN):
Expand Down Expand Up @@ -228,7 +234,7 @@ def __write_memory(self, address: int, data: bytes) -> None:
length = len(data)
if (length == 0 or length > self.__MEMORY_RW_MAX_SIZE):
raise ValueError('Wrong data size for write memory command')
if (length % 4):
if (((address % 4) != 0) or ((length % 4) != 0)):
raise ValueError('Write memory command requires 4 byte alignment')
self.__cmd_send(b'\x31')
self.__data_write(address.to_bytes(4, byteorder='big'))
Expand Down Expand Up @@ -354,6 +360,8 @@ def load_flash_and_run(self, data: bytes, description: str) -> None:
length = len(data)
if (length > (self.__FLASH_PAGE_SIZE * self.__FLASH_NUM_PAGES)):
raise LCMXO2PrimerException('FPGA data size too big')
if ((length % self.__FLASH_PAGE_SIZE) != 0):
raise LCMXO2PrimerException('FPGA data size not aligned to page size')

self.__cmd_execute(self.__CMD_ENABLE_FLASH)

Expand Down
14 changes: 10 additions & 4 deletions sw/pc/sc64.py
Original file line number Diff line number Diff line change
Expand Up @@ -460,7 +460,13 @@ def download_save(self) -> bytes:
def upload_bootloader(self, data: bytes) -> None:
if (len(data) > self.__Length.BOOTLOADER):
raise ValueError('Bootloader size too big')
self.__program_flash(self.__Address.BOOTLOADER, data)
padded_data = data + (b'\xFF' * (self.__Length.BOOTLOADER - len(data)))
if (self.__read_memory(self.__Address.BOOTLOADER, self.__Length.BOOTLOADER) != padded_data):
self.__erase_flash_region(self.__Address.BOOTLOADER, self.__Length.BOOTLOADER)
self.__write_memory(self.__Address.BOOTLOADER, data)
self.__flash_wait_busy()
if (self.__read_memory(self.__Address.BOOTLOADER, self.__Length.BOOTLOADER) != padded_data):
raise ConnectionException('Bootloader program failure')

def set_rtc(self, t: datetime) -> None:
to_bcd = lambda v: ((int((v / 10) % 10) << 4) | int(int(v) % 10))
Expand Down Expand Up @@ -739,7 +745,7 @@ def __call__(self, parser, namespace, values, option_string):
parser = argparse.ArgumentParser(description='SC64 control software')
parser.add_argument('--backup-firmware', metavar='file', help='backup SC64 firmware and write it to specified file')
parser.add_argument('--update-firmware', metavar='file', help='update SC64 firmware from specified file')
parser.add_argument('--bootloader', metavar='file', help='update SC64 bootloader (not recommended, use --update-firmware instead)')
parser.add_argument('--update-bootloader', metavar='file', help='update SC64 bootloader (not recommended, use --update-firmware instead)')
parser.add_argument('--reset-state', action='store_true', help='reset SC64 internal state')
parser.add_argument('--print-state', action='store_true', help='print SC64 internal state')
parser.add_argument('--boot', type=SC64.BootMode, action=EnumAction, help='set boot mode')
Expand Down Expand Up @@ -779,8 +785,8 @@ def __call__(self, parser, namespace, values, option_string):
sc64.update_firmware(f.read(), status_callback)
print('done')

if (args.bootloader):
with open(args.bootloader, 'rb') as f:
if (args.update_bootloader):
with open(args.update_bootloader, 'rb') as f:
print('Uploading Bootloader... ', end='', flush=True)
sc64.upload_bootloader(f.read())
print('done')
Expand Down

0 comments on commit 3fc6229

Please sign in to comment.