linux-can.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Austin Schuh <austin@peloton-tech.com>
To: Oliver Hartkopp <socketcan@hartkopp.net>,
	linux-can <linux-can@vger.kernel.org>,
	Philipp Schrader <philipp@peloton-tech.com>
Subject: Re: CAN message timestamping
Date: Mon, 28 Mar 2016 20:42:48 -0700	[thread overview]
Message-ID: <CANGgnMbzfVcK+PgAeFMFHjCZMCSosHE2xfctOfkdFb=+ZHrQaw@mail.gmail.com> (raw)
In-Reply-To: <CANGgnMaOs03rJByb9KVFFG1p45ZuWZ4qt8-F7y6oqtdDveNs2A@mail.gmail.com>

On Mon, Mar 28, 2016 at 6:51 PM, Austin Schuh <austin@peloton-tech.com> wrote:
> Hi Oliver,
>
> I spent some time trying recvmsg, but it still only gives me
> timestamps with the real-time clock.  I do like the interface much
> better.  Thanks!
>
> I was able to get
>   setsockopt(socket_, SOL_SOCKET, SO_TIMESTAMPNS,
>       &enabled, sizeof(enabled))
> and
>   const int stamping_val = SOF_TIMESTAMPING_SOFTWARE |
>       SOF_TIMESTAMPING_SYS_HARDWARE |
>       SOF_TIMESTAMPING_RAW_HARDWARE;
>   setsockopt(socket_, SOL_SOCKET, SO_TIMESTAMPING, &stamping_val,
>       sizeof(stamping_val))
> to successfully timestamp values with recvmsg.
>
> I0328 18:25:09.927222 12739 can_data_source.cc:144] 0: 1459214709 927191637
> I0328 18:25:09.927227 12739 can_data_source.cc:146] 1: 0 0
> I0328 18:25:09.927233 12739 can_data_source.cc:148] 2: 0 0
> I0328 18:25:09.927240 12739 can_data_source.cc:169] Capture time is
> 1459214709927191637ns
>
> The time-stamp is definitely using the real-time clock.  The hardware
> timestamps are not filled in.  Maybe I could modify the sja1000-peak
> driver to abuse one of the hardware timestamps to return the timestamp
> I am looking for?  I'd really rather not have to run a custom
> kernel...
>
> Austin
>
> On Wed, Mar 23, 2016 at 12:00 AM Oliver Hartkopp <socketcan@hartkopp.net> wrote:
>>
>> Hi Austin,
>>
>> On 23.03.2016 06:12, Austin Schuh wrote:
>> > We recently tried doing CAN timestamping with the ioctl(s, SIOCGSTAMP,
>> > &tv) call.
>>
>> Did you ever try to use the recvmsg() call with enabled SO_TIMESTAMPING socket
>> option?
>>
>> See:
>>
>> https://www.kernel.org/doc/Documentation/networking/timestamping.txt
>> https://www.kernel.org/doc/Documentation/networking/timestamping/
>>
>> The candump tool also uses recvmsg() - but only with SO_TIMESTAMP so far which
>> does not provide the other RX timestamps (link HW TS).
>>
>> Regards,
>> Oliver
>>

Turns out timestamping in the driver is pretty easy.  The following
seems to be working for me.  (comments welcome!)  I don't think this
is something that should be up streamed, but I'm including it here in
case there is other interest.  I'm reading both clocks in the ISR to
reduce the amount of time difference between when they are both read.

$ git diff
diff --git a/drivers/net/can/sja1000/sja1000.c
b/drivers/net/can/sja1000/sja1000.c
index 76ef900..55d6583 100644
--- a/drivers/net/can/sja1000/sja1000.c
+++ b/drivers/net/can/sja1000/sja1000.c
@@ -370,6 +370,10 @@ static void sja1000_rx(struct net_device *dev)
        /* release receive buffer */
        sja1000_write_cmdreg(priv, CMD_RRB);

+       struct skb_shared_hwtstamps *shhwtstamps =
+           skb_hwtstamps(skb);
+       shhwtstamps->syststamp = ktime_get();
+       skb->tstamp = ktime_get_real();
        netif_rx(skb);

        stats->rx_packets++;

Austin

  reply	other threads:[~2016-03-29  3:43 UTC|newest]

Thread overview: 10+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2016-03-23  5:12 CAN message timestamping Austin Schuh
2016-03-23  7:00 ` Oliver Hartkopp
2016-03-29  1:51   ` Austin Schuh
2016-03-29  3:42     ` Austin Schuh [this message]
2016-03-29  4:28       ` Austin Schuh
2016-03-30 12:17         ` Oliver Hartkopp
2016-03-30 16:50           ` Austin Schuh
2016-04-04 19:45             ` Oliver Hartkopp
2016-04-04 20:32               ` Austin Schuh
2016-05-12  2:07                 ` Austin Schuh

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to='CANGgnMbzfVcK+PgAeFMFHjCZMCSosHE2xfctOfkdFb=+ZHrQaw@mail.gmail.com' \
    --to=austin@peloton-tech.com \
    --cc=linux-can@vger.kernel.org \
    --cc=philipp@peloton-tech.com \
    --cc=socketcan@hartkopp.net \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).