From d4a48f861a0970733c059a54df594a7a4acec1f3 Mon Sep 17 00:00:00 2001 From: Martin Teichmann <martin.teichmann@xfel.eu> Date: Thu, 16 Feb 2023 16:44:50 +0100 Subject: [PATCH] set pdo size to its actual value we used to initialize the sync manager to what was in the eeprom, which is big enough normally, but not always. Now we read out the pdo assignment first, and set the sync manager size accordingly. --- ebpfcat/ebpfcat.py | 6 ++++-- ebpfcat/ethercat.py | 22 ++++++++++++++++------ ebpfcat/ethercat_test.py | 20 +++++++++++++++++--- 3 files changed, 37 insertions(+), 11 deletions(-) diff --git a/ebpfcat/ebpfcat.py b/ebpfcat/ebpfcat.py index f6dcc75..d40f0c2 100644 --- a/ebpfcat/ebpfcat.py +++ b/ebpfcat/ebpfcat.py @@ -223,8 +223,10 @@ class EBPFTerminal(Terminal): f"Incompatible Terminal: {self.vendorId}:{self.productCode}") await self.to_operational(2) self.pdos = {} - if self.has_mailbox(): - await self.parse_pdos() + outbits, inbits = await self.parse_pdos() + self.pdo_out_sz = int((outbits + 7) // 8) + self.pdo_in_sz = int((inbits + 7) // 8) + await self.write_pdo_sm() def allocate(self, packet, readonly): """allocate space in packet for the pdos of this terminal diff --git a/ebpfcat/ethercat.py b/ebpfcat/ethercat.py index e21e867..da46569 100644 --- a/ebpfcat/ethercat.py +++ b/ebpfcat/ethercat.py @@ -450,8 +450,16 @@ class Terminal: elif mode == 6: self.mbx_out_off = offset self.mbx_out_sz = size - s = await self.read(0x800, data=0x80) - print(absolute, " ".join(f"{c:02x} {'|' if i % 8 == 7 else ''}" for i, c in enumerate(s))) + else: + print("wrong mode") + + async def write_pdo_sm(self): + await self.write(0x816, "B", 0) + await self.write(0x812, "H", self.pdo_out_sz) + await self.write(0x816, "B", self.pdo_out_sz > 0) + await self.write(0x81E, "B", 0) + await self.write(0x81A, "H", self.pdo_in_sz) + await self.write(0x81E, "B", self.pdo_in_sz > 0) async def parse_pdos(self): async def parse_eeprom(s): @@ -492,16 +500,18 @@ class Terminal: (sm, bitpos // 8, {8: "B", 16: "H", 32: "I", 64: "Q"}[bits]) bitpos += bits + return bitpos self.pdos = {} if self.has_mailbox(): - await parse(parse_sdo(0x1c12, 2)) - await parse(parse_sdo(0x1c13, 3)) + return (await parse(parse_sdo(0x1c12, 2)), + await parse(parse_sdo(0x1c13, 3))) else: - if 50 in self.eeprom: + return ( await parse(parse_eeprom(self.eeprom[50])) - if 51 in self.eeprom: + if 50 in self.eeprom else 0, await parse(parse_eeprom(self.eeprom[51])) + if 51 in self.eeprom else 0) async def parse_pdo(self, index, sm): assignment = await self.sdo_read(index) diff --git a/ebpfcat/ethercat_test.py b/ebpfcat/ethercat_test.py index 60b6a76..a000ec6 100644 --- a/ebpfcat/ethercat_test.py +++ b/ebpfcat/ethercat_test.py @@ -42,6 +42,8 @@ class MockEtherCat: async def roundtrip(self, *args, data=None): if data is not None: args += data, + if not self.expected: + self.test.fail(f"missing {args}") self.test.assertEqual(args, self.expected.pop(0)) return self.results.pop(0) @@ -70,7 +72,7 @@ class MockTerminal(Terminal): self.test_sdo = data["sdo"] await self.apply_eeprom() - async def to_operational(self, state): + async def to_operational(self, state=8): self.operational = state async def sdo_read(self, index, subindex=None): @@ -119,8 +121,14 @@ class Tests(TestCase): (ECCmd.FPWR, 4, 0x800, 0x80), (ECCmd.FPWR, 4, 0x800, H('00108000260001018010800022000102' '00110000040000038011100020000104')), + (ECCmd.FPWR, 4, 2070, 'B', 0), # disable sync manager + (ECCmd.FPWR, 4, 2066, 'H', 0), # set sync manager size + (ECCmd.FPWR, 4, 2070, 'B', False), # disable 0-length sync manager + (ECCmd.FPWR, 4, 2078, 'B', 0), # disable other sync manager + (ECCmd.FPWR, 4, 2074, 'H', 16), # set sync manager size + (ECCmd.FPWR, 4, 2078, 'B', True), # enable sync manager ] - ec.results = [None, None] + ec.results = [None, None, None, None, None, None, None, None] await ti.initialize(-1, 4) ai = AnalogInput(ti.channel1.value) SyncGroup.packet_index = 0x66554433 @@ -169,8 +177,14 @@ class Tests(TestCase): (ECCmd.FPWR, 7, 0x800, 0x80), (ECCmd.FPWR, 7, 0x800, H('0010800026000101801080002200010' '200110800240001038011000000000004')), + (ECCmd.FPWR, 7, 2070, 'B', 0), # disable sync manager + (ECCmd.FPWR, 7, 2066, 'H', 8), # set sync manager size + (ECCmd.FPWR, 7, 2070, 'B', True), # enable sync manager + (ECCmd.FPWR, 7, 2078, 'B', 0), # disable other sync manager + (ECCmd.FPWR, 7, 2074, 'H', 0), # set sync manager size + (ECCmd.FPWR, 7, 2078, 'B', False), # disable 0-length sync manager ] - ec.results = [None, None] + ec.results = [None, None, None, None, None, None, None, None] await ti.initialize(-2, 7) ao = AnalogOutput(ti.ch1_value) SyncGroup.packet_index = 0x55443322 -- GitLab