Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

CAN message timestamp #80

Open
spnda opened this issue Dec 13, 2024 · 3 comments
Open

CAN message timestamp #80

spnda opened this issue Dec 13, 2024 · 3 comments

Comments

@spnda
Copy link

spnda commented Dec 13, 2024

In short I am trying to get a timestamp of a single CAN message through millis or micros using the FIFO mailbox and interrupts. This timestamp is supposed to be as precise as possible, which is why I would vastly prefer it to be queried within the interrupt or from the CAN controller(?). Using something like the following function makes millis return garbage:

void can_receive(const CAN_message_t& msg) {
    const auto timestamp = millis();
    can_buf.emplace_back(timestamp, msg);
}
Received CAN msg: ..., with timestamp: 23527, with data: ...
Received CAN msg: ..., with timestamp: 33502, with data: ...
Received CAN msg: ..., with timestamp: 43665, with data: ...
Received CAN msg: ..., with timestamp: 53472, with data: ...
Received CAN msg: ..., with timestamp: 63373, with data: ...
Received CAN msg: ..., with timestamp: 7928, with data: ...
Received CAN msg: ..., with timestamp: 17933, with data: ...
Received CAN msg: ..., with timestamp: 27911, with data: ...
Received CAN msg: ..., with timestamp: 37904, with data: ...

When printing them later outside of the interrupt the timestamp can be seen to be garbage. Am I not allowed to query the timestamp using the aforementioned functions within an interrupt? I've seen other libraries do it, so maybe it's because of the complexity of the interrupt handler. Does FlexCAN offer some alternative? CAN_message_t's timestamp is only 16-bit and I read somewhere else that it's only indicative of the tx/rx time for each message, making it useless for me. Or is there some way to make use of it here?

@tonton81
Copy link
Owner

never use millis() in an interrupt, it itself uses an interrupt which is currently not the active one so ends up being paused or read wrong

@spnda
Copy link
Author

spnda commented Feb 12, 2025

@tonton81 do you then have any idea how I could instead track the time at which a message is received? The only other idea I have is have a function that is run every N microseconds through TeensyThreads that will then give each message the time it reads then, but this isn't really that reliable.

@tonton81
Copy link
Owner

tonton81 commented Feb 12, 2025

what i did was set a volatile global variable whenever the isr was entered and flag it in the isr, then if the isr returns to loop the flag when checked would check millis() from the loop, then unset the flag

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants