wpilibsuite / wpilibsuite/SystemcoreTesting
CAN Bus hardware timestamps don't come from the hardware
Nobody has claimed this yet.
- Dominant language
- Java
- Stars
- 186
- Forks
- 24
- Avg merge
- 8h 27m
- Merged PRs (30d)
- 9
Description
The mcp251xfd driver has been patched to have the "hardware" timestamps be based on the monotonic clock, except it wipes away the hardware timestamp completely and replaces it with the current monotonic time at the time the packet is received by the driver:
diff --git a/drivers/net/can/spi/mcp251xfd/mcp251xfd.h b/drivers/net/can/spi/mcp251xfd/mcp251xfd.h
index dcbbd2b2fae8273844bd1843209ecd5a2ed3a43c..5ee4672e48136f7de6bf06be0a14fe5d6468a1d3 100644
--- a/drivers/net/can/spi/mcp251xfd/mcp251xfd.h
+++ b/drivers/net/can/spi/mcp251xfd/mcp251xfd.h
@@ -825,7 +825,7 @@ static inline void mcp251xfd_skb_set_timestamp(struct sk_buff *skb, u64 ns)
{
struct skb_shared_hwtstamps *hwtstamps = skb_hwtstamps(skb);
- hwtstamps->hwtstamp = ns_to_ktime(ns);
+ hwtstamps->hwtstamp = ktime_get_raw();
}
static inline
This means that there is nondeterministic latency between the actual reception time and the reported timestamp. Instead, the driver should be patched to keep the timestamp from the mcp2518fd hardware but switch the time base to the monotonic clock (and the previous patch should be reverted):
diff --git a/drivers/net/can/spi/mcp251xfd/mcp251xfd-timestamp.c b/drivers/net/can/spi/mcp251xfd/mcp251xfd-timestamp.c
index 202ca0d24d03b9f5bebba23a9f8d0e6f6422c728..3b6b47cb09a4e09f1b25a8a339d7a837a527806a 100644
--- a/drivers/net/can/spi/mcp251xfd/mcp251xfd-timestamp.c
+++ b/drivers/net/can/spi/mcp251xfd/mcp251xfd-timestamp.c
@@ -53,7 +53,7 @@ void mcp251xfd_timestamp_init(struct mcp251xfd_priv *priv)
void mcp251xfd_timestamp_start(struct mcp251xfd_priv *priv)
{
- timecounter_init(&priv->tc, &priv->cc, ktime_get_real_ns());
+ timecounter_init(&priv->tc, &priv->cc, ktime_get_ns());
schedule_delayed_work(&priv->timestamp,
MCP251XFD_TIMESTAMP_WORK_DELAY_SEC * HZ);
}
This patch also uses CLOCK_MONOTONIC, not CLOCK_MONOTONIC_RAW as the current patch does, since we're using CLOCK_MONOTONIC everywhere else.
Contributor guide
No contributing guide indexed for this repository
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start with drivers/net/can/spi/mcp251xfd/mcp251xfd-timestamp.c and mcp251xfd.h, reading the timestamp initialization and skb timestamp helper. Trace how the mcp2518fd hardware timestamp reaches the socket buffer and compare the clock bases used there. Done means the hardware timestamp is preserved, the time base is monotonic, and the previous receive-time replacement is removed.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- c
- Domain
- embedded-iot, operating-systems
- Issue type
- Bug
- Difficulty
- 3/5
- Estimated time
- 1-2 days
- Activity status
- Stale
- Clarity
- Clearly specified
- Newbie friendliness
- 45/100