From 9985b0551e59f8ed65b042daa83930f89beb5622 Mon Sep 17 00:00:00 2001 From: zhaohaiyang1 Date: Fri, 30 Aug 2024 14:34:45 +0800 Subject: [PATCH] nuttx/can.h: support timestamp for can frame and update "can.rst" file for add struct timeval ch_ts info. Signed-off-by: zhaohaiyang1 --- .../components/drivers/character/can.rst | 5 +++++ drivers/can/Kconfig | 8 ++++++++ drivers/can/mcp2515.c | 18 ++++++++++++++++++ include/nuttx/can/can.h | 10 ++++++++++ 4 files changed, 41 insertions(+) diff --git a/Documentation/components/drivers/character/can.rst b/Documentation/components/drivers/character/can.rst index 68d95686a10..86061c78b8e 100644 --- a/Documentation/components/drivers/character/can.rst +++ b/Documentation/components/drivers/character/can.rst @@ -27,6 +27,10 @@ Files supporting CAN can be found in the following locations: directory for the specific processor ```` and for the specific ```` CAN peripheral devices. +``struct timeval ch_ts``: This member variable that store in the +``can_hdr_s`` structure depends on ``CONFIG_CAN_TIMESTAMP`` and +is used to store the timestamp of the CAN message. + **Usage Note**: When reading from the CAN driver multiple messages may be returned, depending on (1) the size the returned can messages, and (2) the size of the buffer provided to receive CAN @@ -34,3 +38,4 @@ messages. *Never assume that a single message will be returned*... if you do this, *you will lose CAN data* under conditions where your read buffer can hold more than one small message. Below is an example about how you should think of the CAN read operation: +**Examples**: ``drivers/can/mcp2515.c``. diff --git a/drivers/can/Kconfig b/drivers/can/Kconfig index ff303d87139..3bc18943f74 100644 --- a/drivers/can/Kconfig +++ b/drivers/can/Kconfig @@ -33,6 +33,14 @@ config CAN_ERRORS bit will be set in the CAN message and the following message payload will include a more detailed description of certain errors. +config CAN_TIMESTAMP + bool "CAN timestamp reporting" + default n + ---help--- + Support CAN timestamp reporting. if this option is selected then + CAN timestamp reporting is enabled. in the CAN message the ch_ts + member variable will record the timestamp of each frame. + config CAN_FD bool "CAN FD" default n diff --git a/drivers/can/mcp2515.c b/drivers/can/mcp2515.c index af636d162d8..89d2c337666 100644 --- a/drivers/can/mcp2515.c +++ b/drivers/can/mcp2515.c @@ -45,6 +45,10 @@ #include #include +#ifdef CONFIG_CAN_TIMESTAMP +#include +#endif + #include "mcp2515.h" #if defined(CONFIG_CAN) && defined(CONFIG_CAN_MCP2515) @@ -2063,11 +2067,20 @@ static void mcp2515_error(FAR struct can_dev_s *dev, uint8_t status, static void mcp2515_receive(FAR struct can_dev_s *dev, uint8_t offset) { +#ifdef CONFIG_CAN_TIMESTAMP + clock_t clkval; + struct timespec ts; +#endif FAR struct mcp2515_can_s *priv; struct can_hdr_s hdr; int ret; uint8_t regval; +#ifdef CONFIG_CAN_TIMESTAMP + clkval = up_perf_gettime(); + up_perf_convert(clkval, &ts); +#endif + DEBUGASSERT(dev); priv = dev->cd_priv; DEBUGASSERT(priv); @@ -2153,6 +2166,11 @@ static void mcp2515_receive(FAR struct can_dev_s *dev, uint8_t offset) regval = RXREGVAL(MCP2515_RXB0DLC); hdr.ch_dlc = (regval & RXBDLC_DLC_MASK) >> RXBDLC_DLC_SHIFT; +#ifdef CONFIG_CAN_TIMESTAMP + hdr.ch_ts.tv_sec = ts.tv_sec; + hdr.ch_ts.tv_usec = ts.tv_nsec / 1000u; +#endif + /* Save the message data */ ret = can_receive(dev, &hdr, (FAR uint8_t *)&RXREGVAL(MCP2515_RXB0D0)); diff --git a/include/nuttx/can/can.h b/include/nuttx/can/can.h index 1ee02ae552e..841aca00018 100644 --- a/include/nuttx/can/can.h +++ b/include/nuttx/can/can.h @@ -44,6 +44,10 @@ # include #endif +#ifdef CONFIG_CAN_TIMESTAMP +#include +#endif + #ifdef CONFIG_CAN /**************************************************************************** @@ -592,6 +596,9 @@ begin_packed_struct struct can_hdr_s uint8_t ch_esi : 1; /* Error State Indicator */ #endif uint8_t ch_tcf : 1; /* Tx confirmation flag */ +#ifdef CONFIG_CAN_TIMESTAMP + struct timeval ch_ts; /* record the timestamp of each frame */ +#endif } end_packed_struct; #else @@ -609,6 +616,9 @@ begin_packed_struct struct can_hdr_s uint8_t ch_esi : 1; /* Error State Indicator */ #endif uint8_t ch_tcf : 1; /* Tx confirmation flag */ +#ifdef CONFIG_CAN_TIMESTAMP + struct timeval ch_ts; /* record the timestamp of each frame */ +#endif } end_packed_struct; #endif