The microcontroller kit (STM32F4 discovery kit) which I am using has CAN and USB support. I want to program my microcontroller so that each time when a CAN message is received, it is transmitted via USB to the host PC. I also need to see the time at which each CAN message was received. In the host PC I can see this information.

How do I find the time information about the CAN message received, that is, at what time was each CAN message received?

I already have a method which I will explain here. I need your suggestion regarding my idea or you may suggest a new method.

My method: Run a timer inside the microcontroller. Each time a CAN message is received, the value present in timer is stored and the timer is reset. The stored timer value along with the CAN message information is transfered via USB to the host machine.

Here each timer increment might be equivalent to 1 millisecond or 1 microsecond depending on the timer configuration. The microcontroller will be always sending the time difference between two CAN messages.

I am also planning to use another counter which would increment for each timer overflow. Sometimes a CAN message might only arrive after two seconds (or even some other time). In that case the timer can overflow. Each time when a timer has finished, the counter will be incremented. This happens until next CAN message arrives. This counter value will be also transmitted to the host PC.

How is my above method? Is there any better idea?


2 Answers 2


I never worked with STM32F4 but like most of other controllers with CAN, it has timestamp register. I checked in this reference manual, this one has a register

Check out CAN_RDTxR. It has timestamp field. Description says,

This field contains the 16-bit timer value captured at the SOF detection.

You might want to use this in-built feature instead of using your own timers etc.

(P.S. I would strongly recomment you to use FIFO, if you are not using them!)

  • 1
    \$\begingroup\$ @JohnU XKCD has this: xkcd.com/293 :) \$\endgroup\$
    – Swanand
    Aug 22, 2013 at 9:37
  • \$\begingroup\$ @Swanand: Yes. I saw this in the manual. But it was confusing for me since this same timing register was also present for transmit i.e there is a register CAN_TDTxr. As per the spec. it is for TTCAN (and I don't know about it much). Thats why I didn't mention about this register here. Also could you give some hints about FIFO ? Also why a FIFO is required ? \$\endgroup\$
    – robomon
    Aug 22, 2013 at 13:41
  • \$\begingroup\$ FIFO/Message boxes/ CAN Mailboxes are nothing but structure to store CAN messages... They are available in bunch... So that you can simultaneously Rx or Tx multiple message. Each mailbox will have all info (ID, Data, Timestamp, Error count etc) related to that message. So, Using Mailboxes gives you ability to have "One-stop" for all info about a message. Some mailboxes allows you to set filters for certain IDs. This allows you to get specific message instead of scanning bus continuously. \$\endgroup\$
    – Swanand
    Aug 26, 2013 at 7:38

What if the second counter which you will be using for detecting an overflow of timer1 also overflows?? How will you capture that overflow??

A simpler solution would be to use a RTC module(external or inbuilt) to keep track of time. Whenever you receive a message via CAN, read the time from the RTC module and output it via USB. This will save you a lot of software related complications as well.

If you further dont want to even use a RTC module, just output the data whenever you receive it via CAN over USB. If the other end of the USB is connected to a computer, get the time from the computer itself(capture it using Microsoft Visual Studio maybe?).


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.