1+ import time
12from typing import Any , List , Optional , Tuple
23
34import 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
0 commit comments