Skip to content
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

Add support for 11 bit IDs, PROGRAM, PROGRAM6 messages #1

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
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
6 changes: 3 additions & 3 deletions pyccp/listeners/message_sorter.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,11 @@ class MessageSorter(can.Listener):
"""A can.Listener which sorts incoming CCP messages by type."""

def __init__(
self, dto_id: int, cro_id: int,
self, dto_id: int, cro_id: int, is_extended_id: bool = True,
):
self.dto_id = dto_id
self.cro_id = cro_id

self.is_extended_id = is_extended_id
self._crm_queue = queue.Queue()
self._evm_queue = queue.Queue()
self._daq_queue = queue.Queue()
Expand Down Expand Up @@ -73,7 +73,7 @@ def on_message_received(self, msg: can.Message):
# CROs are logged by master

def get_command_return_message(
self, timeout: float = 0.5
self, timeout: float = 1.5
) -> messages.CommandReturnMessage:
"""Return the first CRM in the queue.

Expand Down
61 changes: 49 additions & 12 deletions pyccp/master.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,19 +33,22 @@ class Master:
"""A CAN Calibration Protocol (CCP) master node."""

def __init__(
self, transport: can.Bus, cro_id: int, dto_id: int,
self, transport: can.Bus, cro_id: int, dto_id: int, is_extended_id: bool
):
self.slaveConnections = {}
self.cro_id = cro_id
self.dto_id = dto_id
self.is_extended_id = is_extended_id
self._transport = transport
# Receive both DTOs and CROs, the latter in case local echo is enabled.

self._transport.set_filters(
[
{"can_id": dto_id, "can_mask": 0x1FFFFFFF, "extended": True},
{"can_id": cro_id, "can_mask": 0x1FFFFFFF, "extended": True},
{"can_id": dto_id, "can_mask": 0x1FFFFFFF, "extended": self.is_extended_id},
{"can_id": cro_id, "can_mask": 0x1FFFFFFF, "extended": self.is_extended_id},
]
)

self._queue = MessageSorter(dto_id, cro_id)
self._notifier = can.Notifier(self._transport, [self._queue])
self.ctr = 0
Expand All @@ -54,6 +57,7 @@ def _send(self, command_code: CommandCodes, **kwargs):
cro = CommandReceiveObject(
arbitration_id=self.cro_id,
command_code=command_code,
is_extended_id=self.is_extended_id,
ctr=self.ctr,
**kwargs
)
Expand All @@ -63,7 +67,7 @@ def _send(self, command_code: CommandCodes, **kwargs):
"Sent CRO CTR{}: %s %s".format(self.ctr), command_code.name, kwargs_str
)

def _receive(self) -> bytearray:
def _receive(self,timeout=1.5) -> bytearray:
"""Check that the response is what we expect it to be.

Raises
Expand All @@ -79,11 +83,11 @@ def _receive(self) -> bytearray:
Five data bytes.

"""

try:
crm = self._queue.get_command_return_message()
crm = self._queue.get_command_return_message(timeout=timeout)
except Empty:
raise CCPError("No reply from slave")

if crm.ctr == self.ctr:
self.ctr = (self.ctr + 1) % 0x100
else:
Expand Down Expand Up @@ -396,14 +400,47 @@ def get_s_status(self):
def build_chksum(self):
raise NotImplementedError # pragma: no cover

def clear_memory(self):
raise NotImplementedError # pragma: no cover
def clear_memory(self,size: int):
self._send(CommandCodes.CLEAR_MEMORY,size=size)
self._receive(timeout=10.0)

def program(self):
raise NotImplementedError # pragma: no cover
def program(self, size: int, data: int) -> tuple:
"""Program data from master to slave.

def program6(self):
raise NotImplementedError # pragma: no cover
Parameters
----------
size : int
Number of bytes to be transferred (0 - 5).
data : int
Data to be written to MTA0 in the slave.

Returns
-------
tuple
mta0_ext: Current MTA0 extension,
mta0_addr: Current MTA0 address
"""
self._send(CommandCodes.PROGRAM, size=size, data=data)
data = self._receive()
return data[0], data[1:]

def program6(self, data: int) -> tuple:
"""Program 6 bytes of data from master to slave.

Parameters
----------
data : int
Data to be written to MTA0 in the slave.

Returns
-------
tuple
mta0_ext: Current MTA0 extension,
mta0_addr: Current MTA0 address
"""
self._send(CommandCodes.PROGRAM_6, size=6, data=data)
data = self._receive()
return data[0], data[1:]

def move(self):
raise NotImplementedError # pragma: no cover
Expand Down
7 changes: 5 additions & 2 deletions pyccp/messages/command_receive.py
Original file line number Diff line number Diff line change
Expand Up @@ -70,8 +70,11 @@ class CommandCodes(enum.IntEnum):
# CommandCodes.GET_S_STATUS: getSStatus,
# CommandCodes.BUILD_CHKSUM: buildChksum,
# CommandCodes.CLEAR_MEMORY: clearMemory,
CommandCodes.CLEAR_MEMORY: COMMANDS_DB.get_message_by_name("clear_memory"),
# CommandCodes.PROGRAM: program,
CommandCodes.PROGRAM: COMMANDS_DB.get_message_by_name("program"),
# CommandCodes.PROGRAM_6: program6,
CommandCodes.PROGRAM_6: COMMANDS_DB.get_message_by_name("program6"),
# CommandCodes.MOVE: move,
# CommandCodes.TEST: test,
# CommandCodes.GET_ACTIVE_CAL_PAGE: getActiveCalPage,
Expand All @@ -87,6 +90,7 @@ def __init__(
arbitration_id: int = 0,
command_code: CommandCodes = None,
ctr: int = 0,
is_extended_id: bool = True,
**kwargs: int,
):
"""Create a CommandReceiveObject.
Expand All @@ -107,11 +111,10 @@ def __init__(
self.data = bytearray(MAX_DLC)
self.command_code = command_code
self.ctr = ctr

if command_code is not None:
self.data = self.encode(**kwargs)

super().__init__(arbitration_id=arbitration_id, data=self.data)
super().__init__(arbitration_id=arbitration_id, data=self.data,is_extended_id=is_extended_id)

def encode(self, **kwargs: int) -> bytes:
"""Encode keyword arguments to bytes.
Expand Down
Loading