This is an automated email from the ASF dual-hosted git repository.

GUIDINGLI pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/nuttx.git

commit 7944735f8d18ff85db2e0d9df2520f2d5cd4a9b1
Author: zhangbo56 <[email protected]>
AuthorDate: Wed Apr 22 20:35:08 2026 +0800

    drivers/rpmsg/rpmsg_port: keep the timestamps at the tail without RETX
    
    When CONFIG_RPMSG_PORT_RETX is not enabled, keep tx_nsec/rx_nsec at
    the tail of the buffer instead of in rpmsg_port_header_s. This saves
    16 bytes in the header, and restores the frame layout that a port
    without retransmission used before the timestamps moved into the
    header.
    
    Signed-off-by: zhangbo56 <[email protected]>
    Signed-off-by: Bowen Wang <[email protected]>
---
 drivers/rpmsg/rpmsg_port.c | 44 +++++++++++++++++++++++++++++++++++++++++---
 drivers/rpmsg/rpmsg_port.h |  2 +-
 2 files changed, 42 insertions(+), 4 deletions(-)

diff --git a/drivers/rpmsg/rpmsg_port.c b/drivers/rpmsg/rpmsg_port.c
index da06ca0618e..f05f3f48414 100644
--- a/drivers/rpmsg/rpmsg_port.c
+++ b/drivers/rpmsg/rpmsg_port.c
@@ -42,6 +42,14 @@
 
 #define RPMSG_PORT_TIMEOUT_MS       15000
 
+/* Without RETX the timestamps are kept at the tail of the buffer */
+
+#ifdef CONFIG_RPMSG_PORT_RETX
+#  define RPMSG_PORT_TAIL_SIZE      0
+#else
+#  define RPMSG_PORT_TAIL_SIZE      sizeof(struct rpmsg_timestamp_s)
+#endif
+
 #define rpmsg_port_from_rdev(d) container_of(d, struct rpmsg_port_s, rdev)
 #define rpmsg_port_hdr_from_buf(r) \
   container_of(r, struct rpmsg_port_header_s, buf)
@@ -264,7 +272,7 @@ rpmsg_port_get_tx_payload_buffer(FAR struct rpmsg_device 
*rdev,
     }
 
   *len = hdr->len - sizeof(struct rpmsg_port_header_s) -
-         sizeof(struct rpmsg_hdr);
+         sizeof(struct rpmsg_hdr) - RPMSG_PORT_TAIL_SIZE;
 
   return RPMSG_LOCATE_DATA(hdr->buf);
 }
@@ -384,7 +392,7 @@ static int rpmsg_port_get_tx_buffer_size(FAR struct 
rpmsg_device *rdev)
   FAR struct rpmsg_port_s *port = rpmsg_port_from_rdev(rdev);
 
   return port->txq.len - sizeof(struct rpmsg_port_header_s) -
-         sizeof(struct rpmsg_hdr);
+         sizeof(struct rpmsg_hdr) - RPMSG_PORT_TAIL_SIZE;
 }
 
 /****************************************************************************
@@ -396,7 +404,7 @@ static int rpmsg_port_get_rx_buffer_size(FAR struct 
rpmsg_device *rdev)
   FAR struct rpmsg_port_s *port = rpmsg_port_from_rdev(rdev);
 
   return port->rxq.len - sizeof(struct rpmsg_port_header_s) -
-         sizeof(struct rpmsg_hdr);
+         sizeof(struct rpmsg_hdr) - RPMSG_PORT_TAIL_SIZE;
 }
 
 /****************************************************************************
@@ -409,9 +417,23 @@ static int rpmsg_port_get_timestamp(FAR struct rpmsg_s 
*rpmsg,
 {
   FAR const struct rpmsg_hdr *rphdr = RPMSG_LOCATE_HDR(data);
   FAR const struct rpmsg_port_header_s *hdr = rpmsg_port_hdr_from_buf(rphdr);
+#ifndef CONFIG_RPMSG_PORT_RETX
+  FAR struct rpmsg_port_s *port = (FAR struct rpmsg_port_s *)rpmsg;
+  FAR const struct rpmsg_timestamp_s *rpts =
+    (FAR const struct rpmsg_timestamp_s *)((uint8_t *)hdr + port->rxq.len -
+                                           sizeof(struct rpmsg_timestamp_s));
 
+  if (hdr->len > port->rxq.len - sizeof(struct rpmsg_timestamp_s))
+    {
+      return -EINVAL;
+    }
+
+  ts->tx_nsec = rpts->tx_nsec;
+  ts->rx_nsec = rpts->rx_nsec;
+#else
   ts->tx_nsec = hdr->tx_nsec;
   ts->rx_nsec = hdr->rx_nsec;
+#endif
 
   return RPMSG_SUCCESS;
 }
@@ -691,6 +713,21 @@ void rpmsg_port_update_timestamp(FAR struct 
rpmsg_port_queue_s *queue,
 {
   struct timespec ts;
 
+#ifndef CONFIG_RPMSG_PORT_RETX
+  FAR struct rpmsg_timestamp_s *rpts =
+    (FAR struct rpmsg_timestamp_s *)((uint8_t *)hdr + queue->len -
+                                     sizeof(struct rpmsg_timestamp_s));
+
+  clock_gettime(CLOCK_MONOTONIC, &ts);
+  if (tx)
+    {
+      rpts->tx_nsec = 1000000000ull * ts.tv_sec + ts.tv_nsec;
+    }
+  else if (hdr->len <= queue->len - sizeof(struct rpmsg_timestamp_s))
+    {
+      rpts->rx_nsec = 1000000000ull * ts.tv_sec + ts.tv_nsec;
+    }
+#else
   clock_gettime(CLOCK_MONOTONIC, &ts);
   if (tx)
     {
@@ -700,6 +737,7 @@ void rpmsg_port_update_timestamp(FAR struct 
rpmsg_port_queue_s *queue,
     {
       hdr->rx_nsec = 1000000000ull * ts.tv_sec + ts.tv_nsec;
     }
+#endif
 }
 
 /****************************************************************************
diff --git a/drivers/rpmsg/rpmsg_port.h b/drivers/rpmsg/rpmsg_port.h
index a0406785217..b854f59d0da 100644
--- a/drivers/rpmsg/rpmsg_port.h
+++ b/drivers/rpmsg/rpmsg_port.h
@@ -65,9 +65,9 @@ begin_packed_struct struct rpmsg_port_header_s
   uint16_t cmd;                   /* Reserved for uart/spi port driver */
   uint16_t avail;                 /* Available rx buffer of peer side */
   uint16_t len;                   /* Data frame length */
+#ifdef CONFIG_RPMSG_PORT_RETX
   uint64_t tx_nsec;               /* TX timestamp in nanoseconds */
   uint64_t rx_nsec;               /* RX timestamp in nanoseconds */
-#ifdef CONFIG_RPMSG_PORT_RETX
   uint16_t seqno;
   uint16_t ackno;
   uint16_t min_seq;

Reply via email to