1
+ import time
1
2
from typing import Any , List , Optional , Tuple
2
3
3
4
import can
@@ -122,9 +123,23 @@ def __init__(
122
123
# Common
123
124
124
125
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
+ )
126
131
self .tickFrequency = timerCapabilities .tickFrequency # clock ticks per second
127
132
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
+
128
143
self .channel_info = channel
129
144
130
145
def _recv_internal (
@@ -163,7 +178,8 @@ def _recv_internal(
163
178
if m .type == OCI_CANFDRX_MESSAGE .value :
164
179
msg = can .Message (
165
180
timestamp = float (m .data .canFDRxMessage .timeStamp )
166
- / self .tickFrequency ,
181
+ / self .tickFrequency
182
+ + self .timeOffset ,
167
183
arbitration_id = m .data .canFDRxMessage .frameID ,
168
184
is_extended_id = bool (
169
185
m .data .canFDRxMessage .flags & OCI_CAN_MSG_FLAG_EXTENDED
@@ -187,7 +203,8 @@ def _recv_internal(
187
203
)
188
204
elif m .type == OCI_CAN_RX_MESSAGE .value :
189
205
msg = can .Message (
190
- timestamp = float (m .data .rxMessage .timeStamp ) / self .tickFrequency ,
206
+ timestamp = float (m .data .rxMessage .timeStamp ) / self .tickFrequency
207
+ + self .timeOffset ,
191
208
arbitration_id = m .data .rxMessage .frameID ,
192
209
is_extended_id = bool (
193
210
m .data .rxMessage .flags & OCI_CAN_MSG_FLAG_EXTENDED
0 commit comments