nuttx/can.h: support timestamp for can frame

and update "can.rst" file for add struct timeval ch_ts info.

Signed-off-by: zhaohaiyang1 <zhaohaiyang1@xiaomi.com>
This commit is contained in:
zhaohaiyang1 2024-08-30 14:34:45 +08:00 committed by GUIDINGLI
parent d01bbaecc1
commit 9985b0551e
4 changed files with 41 additions and 0 deletions

View file

@ -27,6 +27,10 @@ Files supporting CAN can be found in the following locations:
directory for the specific processor ``<architecture>`` and for
the specific ``<chip>`` 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``.

View file

@ -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

View file

@ -45,6 +45,10 @@
#include <nuttx/can/can.h>
#include <nuttx/can/mcp2515.h>
#ifdef CONFIG_CAN_TIMESTAMP
#include <time.h>
#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));

View file

@ -44,6 +44,10 @@
# include <nuttx/wqueue.h>
#endif
#ifdef CONFIG_CAN_TIMESTAMP
#include <sys/time.h>
#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