Skip to content

Commit 23046fa

Browse files
author
Felix Nieuwenhuizen
committed
etas interface: timestamps relative to epoch
1 parent b37a990 commit 23046fa

File tree

3 files changed

+32
-3
lines changed

3 files changed

+32
-3
lines changed

can/interfaces/etas/__init__.py

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import time
12
from typing import Any, List, Optional, Tuple
23

34
import can
@@ -122,9 +123,23 @@ def __init__(
122123
# Common
123124

124125
timerCapabilities = OCI_TimerCapabilities()
125-
OCI_GetTimerCapabilities(self.ctrl, ctypes.byref(timerCapabilities))
126+
ec = OCI_GetTimerCapabilities(self.ctrl, ctypes.byref(timerCapabilities))
127+
if ec != 0x0:
128+
raise can.exceptions.CanInitializationError(
129+
f"OCI_GetTimerCapabilities failed with error 0x{ec:X}"
130+
)
126131
self.tickFrequency = timerCapabilities.tickFrequency # clock ticks per second
127132

133+
# all timestamps are hardware timestamps relative to powerup
134+
# calculate an offset to make them relative to epoch
135+
now = OCI_Time()
136+
ec = OCI_GetTimerValue(self.ctrl, ctypes.byref(now))
137+
if ec != 0x0:
138+
raise can.exceptions.CanInitializationError(
139+
f"OCI_GetTimerValue failed with error 0x{ec:X}"
140+
)
141+
self.timeOffset = time.time() - (float(now.value) / self.tickFrequency)
142+
128143
self.channel_info = channel
129144

130145
def _recv_internal(
@@ -163,7 +178,8 @@ def _recv_internal(
163178
if m.type == OCI_CANFDRX_MESSAGE.value:
164179
msg = can.Message(
165180
timestamp=float(m.data.canFDRxMessage.timeStamp)
166-
/ self.tickFrequency,
181+
/ self.tickFrequency
182+
+ self.timeOffset,
167183
arbitration_id=m.data.canFDRxMessage.frameID,
168184
is_extended_id=bool(
169185
m.data.canFDRxMessage.flags & OCI_CAN_MSG_FLAG_EXTENDED
@@ -187,7 +203,8 @@ def _recv_internal(
187203
)
188204
elif m.type == OCI_CAN_RX_MESSAGE.value:
189205
msg = can.Message(
190-
timestamp=float(m.data.rxMessage.timeStamp) / self.tickFrequency,
206+
timestamp=float(m.data.rxMessage.timeStamp) / self.tickFrequency
207+
+ self.timeOffset,
191208
arbitration_id=m.data.rxMessage.frameID,
192209
is_extended_id=bool(
193210
m.data.rxMessage.flags & OCI_CAN_MSG_FLAG_EXTENDED

can/interfaces/etas/boa.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -231,6 +231,13 @@ class OCI_TimerEventMessage(ctypes.Structure):
231231
]
232232
OCI_GetTimerCapabilities.restype = OCI_ErrorCode
233233

234+
OCI_GetTimerValue = _oci.OCI_GetTimerValue
235+
OCI_GetTimerValue.argtypes = [
236+
OCI_ControllerHandle,
237+
ctypes.POINTER(OCI_Time),
238+
]
239+
OCI_GetTimerValue.restype = OCI_ErrorCode
240+
234241
# OCI CAN
235242

236243
# OCI CAN - CAN-FD

test/back2back_test.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -337,6 +337,11 @@ class BasicTestEtas(Back2BackTestCase):
337337
INTERFACE_2 = "etas"
338338
CHANNEL_2 = configs[2]["channel"]
339339

340+
def test_unique_message_instances(self):
341+
self.skipTest(
342+
"creating a second instance of a channel with differing self-reception settings is not supported"
343+
)
344+
340345

341346
@unittest.skipUnless(TEST_INTERFACE_SOCKETCAN, "skip testing of socketcan")
342347
class SocketCanBroadcastChannel(unittest.TestCase):

0 commit comments

Comments
 (0)