RX Frame CAN_SetTimestampHandler() example

Universal CAN Converter
Post Reply
User avatar
O.Hartkopp
Posts: 40
Joined: Fri 22. Nov 2013, 19:47

RX Frame CAN_SetTimestampHandler() example

Post by O.Hartkopp » Sun 5. Jun 2016, 20:24

Hello,

I would like to receive and evaluate CAN frames together with their timestamps.

In the provided example code from PCAN Router and PCAN Router pro the include can.h introduces a struct CANRxMsg_t which contains a 32 bit timestamp.

But no example uses this struct CANRxMsg_t and all examples disable the timstamp handler callback:

Code: Select all

CAN_SetTimestampHandler ( CAN_BUS1, NULL);
1. Do you have an example how to use a struct CANRxMsg_t with filled timestamp and enabled timstamp handler callback?

2. Is there any documentation that tell about the unit (ms / ns) which is counted in the 32 bit timstamp counter?

Thanks,
Oliver

User avatar
P.Steil
Hardware Development
Hardware Development
Posts: 32
Joined: Fri 14. Jan 2011, 10:27

Re: RX Frame CAN_SetTimestampHandler() example

Post by P.Steil » Mon 6. Jun 2016, 15:44

Hi Oliver,

you can use the example 3_TIMER as a basis.

- init the timer like in the example
- use CANRxMsg_t for the RX Queues
- create a copy of CAN_UserRead() for the CANRxMsg_t type
- add a handler for the timestamps like this:

Code: Select all

void  ISRGetTime ( CANRxMsg_t *msg)
{
   // read timer register
   msg->TimeStamp32 = SYSTIME_NOW;
}
Regards
Philipp Steil
Last edited by M.Gerber on Tue 7. Jun 2016, 17:03, edited 1 time in total.
Reason: Reformatted the code for better readability.
--------------------------------------------
PEAK-System HW development Team
support@peak-system.com
phone: +49-6151-8173-20
fax: +49-6151-8173-29
--------------------------------------------

User avatar
O.Hartkopp
Posts: 40
Joined: Fri 22. Nov 2013, 19:47

Re: RX Frame CAN_SetTimestampHandler() example

Post by O.Hartkopp » Tue 7. Jun 2016, 16:51

Hi Philipp,

following your suggestion
P.Steil wrote:you can use the example 3_TIMER as a basis.

- init the timer like in the example
- use CANRxMsg_t for the RX Queues
- create a copy of CAN_UserRead() for the CANRxMsg_t type
- add a handler for the timestamps like this:
void ISRGetTime ( CANRxMsg_t *msg)
{
// read timer register
msg->TimeStamp32 = SYSTIME_NOW;
}
I created a CAN_UserReadTS() function:

Code: Select all

// CAN_UserReadTS()
// read message with timestamp from CAN_BUSx
u32_t  CAN_UserReadTS ( CANHandle_t  hBus, CANRxMsg_t *pBuff)
{
	u32_t  ret;
	CANRxMsg_t  *pMsg;
	
	
	ret = 0;
	
	pMsg = CAN_RxQueueGetNext ( hBus);

	if ( pMsg != NULL)
	{
		pBuff->Id   = pMsg->Id;
		pBuff->Len  = pMsg->Len;
		pBuff->Type = pMsg->Type;
		
		pBuff->Data32[0] = pMsg->Data32[0];
		pBuff->Data32[1] = pMsg->Data32[1];
		
		pBuff->TimeStamp32 = pMsg->TimeStamp32;

		CAN_RxQueueReadNext ( hBus);
		ret = 1;
	}
	
	return ret;
}
And a timestamp handler in can_user.c

Code: Select all

// CAN_ISRGetTime()
// set timestamp in ISR for message
void  CAN_ISRGetTime ( CANRxMsg_t *rxmsg)
{
	// read timer register
	rxmsg->TimeStamp32 = SYSTIME_NOW;
}
and enabled

Code: Select all

// timestamp incomming frames
CAN_SetTimestampHandler ( CAN_BUS1, CAN_ISRGetTime);
And now

Code: Select all

CANRxMsg_t  RxMsg;

// process messages from CAN1
if ( CAN_UserReadTS ( CAN_BUS1, &RxMsg) != 0)
returns proper CAN frames with timestamps :)

Thanks for the hints!

Best regards,
Oliver

Post Reply