-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
commit fcf0930 Author: Fabio Battaglia <tabaglio@posteo.net> Date: Tue Sep 3 07:28:34 2024 +0200 Better check for CXFER response after a transfer commit d602734 Author: Fabio Battaglia <tabaglio@posteo.net> Date: Mon Sep 2 20:56:34 2024 +0200 replace some magic numbers with named fields commit 837ca6b Author: Fabio Battaglia <tabaglio@posteo.net> Date: Mon Sep 2 16:33:58 2024 +0200 Correctly purge remaining data in serial port, and use correct endianneess for received checksum commit cd3f467 Author: Fabio Battaglia <tabaglio@posteo.net> Date: Mon Sep 2 15:47:59 2024 +0200 pad the cxfer commands, and transfer blocks of 1024k by default commit 23f327e Author: Fabio Battaglia <tabaglio@posteo.net> Date: Mon Sep 2 13:26:26 2024 +0200 Add the correct command code to cxfer commands commit a096845 Author: Fabio Battaglia <tabaglio@posteo.net> Date: Mon Sep 2 13:02:03 2024 +0200 update board command interface for cxfer_read commit 460edaf Author: Fabio Battaglia <tabaglio@posteo.net> Date: Mon Sep 2 11:39:35 2024 +0200 bump version and changelog commit bfc56ed Author: Fabio Battaglia <tabaglio@posteo.net> Date: Mon Sep 2 11:36:49 2024 +0200 wire up the call to the cxfer read command commit 0c67be2 Author: Fabio Battaglia <tabaglio@posteo.net> Date: Mon Sep 2 10:55:09 2024 +0200 Implement code to read data with a cxfer commit e8966be Author: Fabio Battaglia <tabaglio@posteo.net> Date: Sun Sep 1 19:56:29 2024 +0200 send command to start the transfer commit 3e1c9f3 Author: Fabio Battaglia <tabaglio@posteo.net> Date: Sun Sep 1 19:51:36 2024 +0200 Make checksum calculator available to everyone, add option to ignore response, test the grouper commit d3385a8 Author: Fabio Battaglia <tabaglio@posteo.net> Date: Sun Sep 1 18:54:34 2024 +0200 send address and shift map commit 6350ded Author: Fabio Battaglia <tabaglio@posteo.net> Date: Sun Sep 1 18:39:14 2024 +0200 Add new utils file with utility code commit 418d70d Author: Fabio Battaglia <tabaglio@posteo.net> Date: Sun Sep 1 17:56:31 2024 +0200 cxfer: send clear command commit 9cdbac2 Author: Fabio Battaglia <tabaglio@posteo.net> Date: Sun Sep 1 17:51:48 2024 +0200 begin writing cxfer code commit 3b40abd Author: Fabio Battaglia <tabaglio@posteo.net> Date: Sun Sep 1 16:34:53 2024 +0200 removed useless code from hardware_board_commands, define empty cxfer_read in m3_board_commands commit 5406509 Author: Fabio Battaglia <tabaglio@posteo.net> Date: Sun Sep 1 16:27:55 2024 +0200 bump version, begin defining the cxfer read command
- Loading branch information
Showing
10 changed files
with
244 additions
and
85 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,96 @@ | ||
"""Code to support the CXFER transfer modes""" | ||
|
||
from enum import Enum | ||
import logging | ||
import struct | ||
from typing import Callable, final | ||
|
||
import serial | ||
|
||
from dupicolib.board_utilities import BoardUtilities | ||
|
||
_LOGGER = logging.getLogger(__name__) | ||
|
||
@final | ||
class CXFERTransfer: | ||
_XMIT_BLOCK_SIZE: int = 1024 | ||
_XFER_RESPONSE_SIZE: int = 4 | ||
_XFER_CHECKSUM_SIZE: int = 2 | ||
|
||
class CXFERSubCommand(Enum): | ||
SET_ADDR_MAP_0 = 0x00 | ||
SET_ADDR_MAP_1 = 0x01 | ||
SET_ADDR_MAP_2 = 0x02 | ||
SET_ADDR_MAP_3 = 0x03 | ||
SET_DATA_MAP_0 = 0x10 | ||
SET_DATA_MAP_1 = 0x11 | ||
SET_DATA_MAP_2 = 0x12 | ||
SET_DATA_MAP_3 = 0x13 | ||
SET_HI_OUT_MASK = 0xE0 | ||
SET_DATA_MASK = 0xE1 | ||
SET_ADDR_WIDTH = 0xE2 | ||
SET_DATA_WIDTH = 0xE3 | ||
CLEAR = 0xF0 | ||
EXECUTE_READ = 0xFF | ||
|
||
class CXFERResponse(Enum): | ||
XFER_PKT_START = 0xDEADBEEF | ||
XFER_PKT_FAIL = 0xBAADF00D | ||
XFER_DONE = 0xC00FFFEE | ||
|
||
@classmethod | ||
def read(cls, command_code: int, ser: serial.Serial, update_callback: Callable[[int], None] | None = None) -> bytes | None: | ||
file_data: bytearray = bytearray() | ||
data_block: bytearray | ||
resp: int | ||
|
||
# Start the transfer! | ||
BoardUtilities.send_binary_command(ser, bytes([command_code, cls.CXFERSubCommand.EXECUTE_READ.value, *([0] * 16)]), 0) | ||
|
||
while True: | ||
data: bytes = ser.read(cls._XFER_RESPONSE_SIZE) | ||
|
||
if (data_len := len(data)) != cls._XFER_RESPONSE_SIZE: | ||
raise IOError(f'Received {data_len} data for starting block!') | ||
|
||
resp, = struct.unpack('>I', data) | ||
|
||
if resp == cls.CXFERResponse.XFER_PKT_START.value: | ||
_LOGGER.debug(f'Received a XFER_PKT_START packet, current file size {len(file_data)}') | ||
elif resp == cls.CXFERResponse.XFER_DONE.value: | ||
_LOGGER.info(f'Received a XFER_DONE packet, current file size {len(file_data)}') | ||
break | ||
else: | ||
raise IOError(f'Received {resp:0{4}X} while expecting a start block.') | ||
|
||
data_block = bytearray() | ||
rem_data: int = cls._XMIT_BLOCK_SIZE | ||
|
||
while rem_data > 0: | ||
data = ser.read(16) | ||
|
||
if (data_len := len(data)) <= 0: | ||
raise IOError('Timed out while waiting to read data...') | ||
|
||
rem_data = rem_data - data_len | ||
data_block.extend(data) | ||
|
||
calc_checksum: int = BoardUtilities.cxfer_checksum_calculator(bytes(data_block)) | ||
data = ser.read(cls._XFER_CHECKSUM_SIZE) | ||
if (data_len := len(data)) != cls._XFER_CHECKSUM_SIZE: | ||
raise IOError(f'Received {data_len} data for checksum!') | ||
resp, = struct.unpack('<H', data) | ||
|
||
if resp != calc_checksum: | ||
raise IOError(f'Calculated checksum is {calc_checksum:0{4}X}, received is {resp:0{4}X}') | ||
|
||
# Append the block data to the file buffer | ||
file_data.extend(data_block) | ||
|
||
# Once verified, send the checksum back | ||
ser.write(data) | ||
|
||
if update_callback: | ||
update_callback(len(file_data)) | ||
|
||
return bytes(file_data) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
"""Miscellaneous utilities used by the library""" | ||
|
||
from itertools import zip_longest | ||
from collections.abc import Iterable | ||
from typing import Iterator, Tuple, TypeVar | ||
|
||
T = TypeVar('T') | ||
|
||
|
||
# From https://stackoverflow.com/questions/434287/how-to-iterate-over-a-list-in-chunks | ||
# See https://docs.python.org/3/library/itertools.html#itertools.zip_longest | ||
def iter_grouper(iterable: Iterable[T], group_size: int, fillvalue: T | None = None) -> Iterator[Tuple[T]]: | ||
# We are feeding multiple copies of the same iterator to zip_longest, so it will consume from the same | ||
# source when zipping values, resulting in grouping the values | ||
args = [iter(iterable)] * group_size | ||
return zip_longest(*args, fillvalue=fillvalue) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.