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


The following commit(s) were added to refs/heads/master by this push:
     new 717b26ac92f !drivers/rpmsg/rpmsg_port: add Go-Back-N retransmission
717b26ac92f is described below

commit 717b26ac92f0f4cb5afde1ac4b51595648284300
Author: mazhuang <[email protected]>
AuthorDate: Sun Mar 8 19:58:36 2026 +0800

    !drivers/rpmsg/rpmsg_port: add Go-Back-N retransmission
    
    A frame lost or corrupted on a port link was gone: the port had no way
    to ask for it again. Add a Go-Back-N ARQ, CONFIG_RPMSG_PORT_RETX
    (default y):
    
    - the frame header carries seqno, ackno and min_seq;
    - unacknowledged DATA frames stay on a per-port retransmission queue,
      with a retry count per node;
    - two duplicate ACKs trigger a fast retransmit, up to retx_max_retry
      attempts;
    - the SPI master and slave drivers fill in and check the new fields, and
      the option selects the SPI CRC so that a corrupted frame is detected.
    
    BREAKING CHANGE: with the default RPMSG_PORT_RETX=y the port frame
    header grows by 8 bytes on every rpmsg_port transport (SPI and UART), so
    a peer built without this change cannot talk to one built with it. Quick
    fix: set CONFIG_RPMSG_PORT_RETX=n on both sides to keep the old frame
    format, or update both sides together.
    
    Signed-off-by: mazhuang <[email protected]>
    Signed-off-by: Bowen Wang <[email protected]>
---
 drivers/rpmsg/Kconfig                |  10 ++
 drivers/rpmsg/rpmsg_port.c           | 130 +++++++++++++++++++++-
 drivers/rpmsg/rpmsg_port.h           | 209 +++++++++++++++++++++++++++++++++++
 drivers/rpmsg/rpmsg_port_spi.c       |  51 ++++++++-
 drivers/rpmsg/rpmsg_port_spi_slave.c |  47 +++++++-
 include/nuttx/rpmsg/rpmsg_port.h     |   4 +
 6 files changed, 440 insertions(+), 11 deletions(-)

diff --git a/drivers/rpmsg/Kconfig b/drivers/rpmsg/Kconfig
index b0a6a91ecfd..340ccfbcb2e 100644
--- a/drivers/rpmsg/Kconfig
+++ b/drivers/rpmsg/Kconfig
@@ -184,6 +184,16 @@ config RPMSG_PORT
        ---help---
                Rpmsg port transport layer used for cross chip communication.
 
+config RPMSG_PORT_RETX
+       bool "Rpmsg Port Retransmission Support"
+       default y
+       depends on RPMSG_PORT
+       select RPMSG_PORT_SPI_CRC if RPMSG_PORT_SPI || RPMSG_PORT_SPI_SLAVE
+       ---help---
+               Enable retransmission mechanism for rpmsg port transport.
+               When enabled, unacknowledged DATA frames will be retransmitted
+               via fast-retransmit triggered by duplicate ACKs.
+
 config RPMSG_PORT_SPI
        bool "Rpmsg SPI Port Driver Support"
        default n
diff --git a/drivers/rpmsg/rpmsg_port.c b/drivers/rpmsg/rpmsg_port.c
index d8ede0e089e..75ceecddff9 100644
--- a/drivers/rpmsg/rpmsg_port.c
+++ b/drivers/rpmsg/rpmsg_port.c
@@ -42,9 +42,6 @@
 
 #define RPMSG_PORT_TIMEOUT_MS       15000
 
-#define RPMSG_PORT_BUF_TO_NODE(q,b) ((q)->node + ((FAR void *)(b) - (q)->buf) 
/ (q)->len)
-#define RPMSG_PORT_NODE_TO_BUF(q,n) ((q)->buf + (((n) - (q)->node)) * (q)->len)
-
 #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)
@@ -553,6 +550,13 @@ int rpmsg_port_initialize(FAR struct rpmsg_port_s *port,
   port->ops = ops;
   strlcpy(port->rpmsg.cpuname, cfg->remotecpu, RPMSG_NAME_SIZE);
 
+#ifdef CONFIG_RPMSG_PORT_RETX
+  list_initialize(&port->retx.queue);
+  spin_lock_init(&port->retx.lock);
+  port->retx.last_ackno = UINT16_MAX;
+  port->retx.retx_max_retry = cfg->retx_max_retry;
+#endif
+
   rdev = &port->rdev;
   memset(rdev, 0, sizeof(*rdev));
   metal_mutex_init(&rdev->lock);
@@ -735,6 +739,126 @@ void rpmsg_port_drop_packets(FAR struct rpmsg_port_s 
*rport, uint8_t type)
     }
 }
 
+#ifdef CONFIG_RPMSG_PORT_RETX
+
+/****************************************************************************
+ * Name: rpmsg_port_retx_process_ack
+ ****************************************************************************/
+
+void rpmsg_port_retx_process_ack(FAR struct rpmsg_port_s *port,
+                                  uint16_t ackno, uint16_t min_seq)
+{
+  FAR struct rpmsg_port_node_s *node;
+  FAR struct rpmsg_port_node_s *tmp;
+  irqstate_t flags;
+
+  flags = spin_lock_irqsave(&port->retx.lock);
+
+  /* Traverse and delete acknowledged or retry-exhausted nodes */
+
+  list_for_every_entry_safe(&port->retx.queue, node, tmp,
+                            struct rpmsg_port_node_s, node)
+    {
+      FAR struct rpmsg_port_header_s *hdr =
+        RPMSG_PORT_NODE_TO_BUF(&port->txq, node);
+      if (RPMSG_PORT_SEQ_LT(hdr->seqno, ackno) ||
+          node->retry_count >= port->retx.retx_max_retry)
+        {
+          if (node->retry_count >= port->retx.retx_max_retry)
+            {
+              rpmsgerr("Retransmission failed for seqno %u after %u retries "
+                       "ackno: %u\n",
+                       hdr->seqno, node->retry_count, ackno);
+            }
+
+          rpmsg_port_retx_del(port, node);
+        }
+    }
+
+  /* Duplicate ACK detection for fast retransmit */
+
+  if (!list_is_empty(&port->retx.queue) &&
+      (ackno == port->retx.last_ackno ||
+       atomic_read(&port->retx.ackdup_count) >= 2))
+    {
+      atomic_add(&port->retx.ackdup_count, 1);
+    }
+  else
+    {
+      atomic_set(&port->retx.ackdup_count, 0);
+    }
+
+  rpmsginfo("retx ack: ackno %u, last_ackno %u, ackdup %" PRId32 "\n",
+            ackno, port->retx.last_ackno,
+            atomic_read(&port->retx.ackdup_count));
+  port->retx.last_ackno = ackno;
+
+  /* Advance rx_next based on peer's min_seq to skip abandoned frames */
+
+  if (RPMSG_PORT_SEQ_LT(port->retx.rx_next, min_seq))
+    {
+      rpmsgerr("Peer abandoned seqno [%u, %u), advancing rx_next\n",
+               port->retx.rx_next, min_seq);
+      port->retx.rx_next = min_seq;
+    }
+
+  spin_unlock_irqrestore(&port->retx.lock, flags);
+}
+
+/****************************************************************************
+ * Name: rpmsg_port_retx_rx_valid
+ ****************************************************************************/
+
+bool rpmsg_port_retx_rx_valid(FAR struct rpmsg_port_s *port,
+                              FAR struct rpmsg_port_header_s *hdr)
+{
+  bool valid = false;
+  irqstate_t flags;
+
+  flags = spin_lock_irqsave(&port->retx.lock);
+  if (hdr->seqno == port->retx.rx_next)
+    {
+      port->retx.rx_next++;
+      valid = true;
+    }
+  else
+    {
+      rpmsgerr("rx_validate FAIL got seq:%u expect:%u cmd:%u\n",
+               hdr->seqno, port->retx.rx_next, hdr->cmd);
+    }
+
+  spin_unlock_irqrestore(&port->retx.lock, flags);
+  return valid;
+}
+
+/****************************************************************************
+ * Name: rpmsg_port_retx_reset
+ ****************************************************************************/
+
+void rpmsg_port_retx_reset(FAR struct rpmsg_port_s *port)
+{
+  FAR struct rpmsg_port_node_s *node;
+  FAR struct rpmsg_port_node_s *tmp;
+  irqstate_t flags;
+
+  flags = spin_lock_irqsave(&port->retx.lock);
+
+  list_for_every_entry_safe(&port->retx.queue, node, tmp,
+                            struct rpmsg_port_node_s, node)
+    {
+      rpmsg_port_retx_del(port, node);
+    }
+
+  atomic_set(&port->retx.ackdup_count, 0);
+  port->retx.tx_next = 0;
+  port->retx.rx_next = 0;
+  port->retx.last_ackno = UINT16_MAX;
+
+  spin_unlock_irqrestore(&port->retx.lock, flags);
+}
+
+#endif /* CONFIG_RPMSG_PORT_RETX */
+
 /****************************************************************************
  * Name: rpmsg_port_register
  ****************************************************************************/
diff --git a/drivers/rpmsg/rpmsg_port.h b/drivers/rpmsg/rpmsg_port.h
index 316507c50cd..c5bf2d457fe 100644
--- a/drivers/rpmsg/rpmsg_port.h
+++ b/drivers/rpmsg/rpmsg_port.h
@@ -29,6 +29,7 @@
 
 #include <stdbool.h>
 
+#include <nuttx/atomic.h>
 #include <nuttx/list.h>
 #include <nuttx/spinlock.h>
 #include <nuttx/semaphore.h>
@@ -46,6 +47,12 @@
 #define RPMSG_PORT_DROP_RXQ     0x02
 #define RPMSG_PORT_DROP_ALL     0x03
 
+#define RPMSG_PORT_BUF_TO_NODE(q, b) \
+  ((q)->node + ((FAR void *)(b) - (q)->buf) / (q)->len)
+#define RPMSG_PORT_NODE_TO_BUF(q, n) \
+  ((q)->buf + (((n) - (q)->node)) * (q)->len)
+#define RPMSG_PORT_SEQ_LT(seq, ack)  ((int16_t)((seq) - (ack)) < 0)
+
 /****************************************************************************
  * Public Types
  ****************************************************************************/
@@ -58,6 +65,12 @@ 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
+  uint16_t seqno;
+  uint16_t ackno;
+  uint16_t min_seq;
+  uint16_t reserved;
+#endif
   uint8_t  buf[0];                /* Payload buffer */
 } end_packed_struct;
 
@@ -72,6 +85,9 @@ struct rpmsg_port_list_s
 struct rpmsg_port_node_s
 {
   struct list_node node;          /* List node for free/ready/retx queue */
+#ifdef CONFIG_RPMSG_PORT_RETX
+  uint16_t         retry_count;   /* Retry counter for retransmission */
+#endif
 };
 
 struct rpmsg_port_s;
@@ -137,6 +153,19 @@ struct rpmsg_port_ops_s
   CODE void (*dump)(FAR struct rpmsg_port_s *port, bool verbose);
 };
 
+#ifdef CONFIG_RPMSG_PORT_RETX
+struct rpmsg_port_retx_s
+{
+  struct list_node                  queue;
+  spinlock_t                        lock;
+  atomic_t                          ackdup_count;
+  uint16_t                          tx_next;
+  uint16_t                          rx_next;
+  uint16_t                          last_ackno;
+  uint16_t                          retx_max_retry;
+};
+#endif
+
 struct rpmsg_port_s
 {
   struct rpmsg_s                    rpmsg;
@@ -147,6 +176,12 @@ struct rpmsg_port_s
   /* Ops need implemented by drivers under port layer */
 
   const FAR struct rpmsg_port_ops_s *ops;
+
+#ifdef CONFIG_RPMSG_PORT_RETX
+  /* Retransmission mechanism for DATA frames */
+
+  struct rpmsg_port_retx_s          retx;
+#endif
 };
 
 #ifndef __ASSEMBLY__
@@ -304,6 +339,180 @@ void rpmsg_port_update_timestamp(FAR struct 
rpmsg_port_queue_s *queue,
 
 void rpmsg_port_drop_packets(FAR struct rpmsg_port_s *rport, uint8_t type);
 
+#ifdef CONFIG_RPMSG_PORT_RETX
+
+/****************************************************************************
+ * Name: rpmsg_port_retx_del
+ *
+ * Description:
+ *   Remove a node from the retransmission queue and return the buffer
+ *   to txq. Caller must hold port->retx.lock.
+ *
+ ****************************************************************************/
+
+static inline_function
+void rpmsg_port_retx_del(FAR struct rpmsg_port_s *port,
+                         FAR struct rpmsg_port_node_s *node)
+{
+  list_delete(&node->node);
+  rpmsg_port_queue_return_buffer(&port->txq,
+                                 RPMSG_PORT_NODE_TO_BUF(&port->txq, node));
+}
+
+/****************************************************************************
+ * Name: rpmsg_port_retx_add
+ *
+ * Description:
+ *   Add a DATA frame to the retransmission queue.
+ *
+ ****************************************************************************/
+
+static inline_function
+void rpmsg_port_retx_add(FAR struct rpmsg_port_s *port,
+                         FAR struct rpmsg_port_header_s *txhdr)
+{
+  FAR struct rpmsg_port_node_s *node;
+  irqstate_t flags;
+
+  node = RPMSG_PORT_BUF_TO_NODE(&port->txq, txhdr);
+  node->retry_count = 0;
+
+  flags = spin_lock_irqsave(&port->retx.lock);
+  txhdr->seqno = port->retx.tx_next++;
+  list_add_tail(&port->retx.queue, &node->node);
+  spin_unlock_irqrestore(&port->retx.lock, flags);
+}
+
+/****************************************************************************
+ * Name: rpmsg_port_retx_process_ack
+ *
+ * Description:
+ *   Process an ACK number, removing all acknowledged frames from the
+ *   retransmission queue. Trigger retransmission on duplicate ACK.
+ *   Also advance rx_next based on peer's min_seq to skip abandoned frames.
+ *
+ ****************************************************************************/
+
+void rpmsg_port_retx_process_ack(FAR struct rpmsg_port_s *port,
+                                  uint16_t ackno, uint16_t min_seq);
+
+/****************************************************************************
+ * Name: rpmsg_port_retx_rx_valid
+ *
+ * Description:
+ *   Validate a received DATA frame's seqno and process its ackno.
+ *   Called from SPI complete_handler / slave_notify (interrupt context).
+ *
+ * Returned Value:
+ *   true  - frame is valid, caller should enqueue to rxq.ready
+ *   false - frame is invalid (duplicate/out-of-order), caller should drop
+ *
+ ****************************************************************************/
+
+bool rpmsg_port_retx_rx_valid(FAR struct rpmsg_port_s *port,
+                              FAR struct rpmsg_port_header_s *hdr);
+
+/****************************************************************************
+ * Name: rpmsg_port_retx_reset
+ *
+ * Description:
+ *   Clear the retransmission queue.
+ *
+ ****************************************************************************/
+
+void rpmsg_port_retx_reset(FAR struct rpmsg_port_s *port);
+
+/****************************************************************************
+ * Name: rpmsg_port_retx_get
+ *
+ * Description:
+ *   Get the head node of the retransmission queue.
+ *
+ * Returned Value:
+ *   Pointer to the head node, or NULL if the queue is empty.
+ *
+ ****************************************************************************/
+
+static inline_function FAR struct rpmsg_port_node_s *
+rpmsg_port_retx_get(FAR struct rpmsg_port_s *port)
+{
+  FAR struct rpmsg_port_node_s *node = NULL;
+  irqstate_t flags;
+
+  flags = spin_lock_irqsave(&port->retx.lock);
+  if (!list_is_empty(&port->retx.queue))
+    {
+      node = list_peek_head_type(&port->retx.queue,
+                                 struct rpmsg_port_node_s, node);
+    }
+
+  spin_unlock_irqrestore(&port->retx.lock, flags);
+  return node;
+}
+
+/****************************************************************************
+ * Name: rpmsg_port_retx_is_pending
+ *
+ * Description:
+ *   Check if retransmission is pending (dup-ack count reached threshold).
+ *
+ ****************************************************************************/
+
+static inline_function
+bool rpmsg_port_retx_is_pending(FAR struct rpmsg_port_s *port)
+{
+  return atomic_read(&port->retx.ackdup_count) >= 2;
+}
+
+/****************************************************************************
+ * Name: rpmsg_port_retx_tx_fill
+ *
+ * Description:
+ *   Fill retransmission fields (ackno, min_seq, reserved) in the TX frame
+ *   header before sending.
+ *
+ ****************************************************************************/
+
+static inline_function
+void rpmsg_port_retx_tx_fill(FAR struct rpmsg_port_s *port,
+                             FAR struct rpmsg_port_header_s *txhdr)
+{
+  irqstate_t flags;
+
+  flags = spin_lock_irqsave(&port->retx.lock);
+  txhdr->ackno = port->retx.rx_next;
+  txhdr->reserved = 0;
+
+  if (!list_is_empty(&port->retx.queue))
+    {
+      FAR struct rpmsg_port_node_s *head =
+        list_peek_head_type(&port->retx.queue,
+                            struct rpmsg_port_node_s, node);
+      FAR struct rpmsg_port_header_s *hdr =
+        RPMSG_PORT_NODE_TO_BUF(&port->txq, head);
+      txhdr->min_seq = hdr->seqno;
+    }
+  else
+    {
+      txhdr->min_seq = port->retx.tx_next;
+    }
+
+  spin_unlock_irqrestore(&port->retx.lock, flags);
+}
+
+#else /* !CONFIG_RPMSG_PORT_RETX */
+
+#  define rpmsg_port_retx_add(port, txhdr)
+#  define rpmsg_port_retx_del(port, node)
+#  define rpmsg_port_retx_get(port)                         NULL
+#  define rpmsg_port_retx_is_pending(port)                  false
+#  define rpmsg_port_retx_tx_fill(port, txhdr)
+#  define rpmsg_port_retx_process_ack(port, ackno, min_seq)
+#  define rpmsg_port_retx_rx_valid(port, hdr)               true
+#  define rpmsg_port_retx_reset(port)
+
+#endif /* CONFIG_RPMSG_PORT_RETX */
+
 /****************************************************************************
  * Name: rpmsg_port_initialize
  *
diff --git a/drivers/rpmsg/rpmsg_port_spi.c b/drivers/rpmsg/rpmsg_port_spi.c
index 62839deee15..36cf75d6a61 100644
--- a/drivers/rpmsg/rpmsg_port_spi.c
+++ b/drivers/rpmsg/rpmsg_port_spi.c
@@ -164,7 +164,8 @@ static void rpmsg_port_spi_pm_callback(wdparm_t arg)
   count = pm_wakelock_staycount(&rpspi->wakelock);
   if (count > 0 && atomic_read(&rpspi->transferring) == 0 &&
       rpmsg_port_queue_nused(&rpspi->port.txq) == 0 &&
-      rpmsg_port_queue_nused(&rpspi->port.rxq) == 0)
+      rpmsg_port_queue_nused(&rpspi->port.rxq) == 0 &&
+      !rpmsg_port_retx_is_pending(&rpspi->port))
     {
       pm_wakelock_relax(&rpspi->wakelock);
     }
@@ -265,6 +266,9 @@ static void rpmsg_port_spi_register_cb(FAR struct 
rpmsg_port_s *port,
 static void rpmsg_port_spi_exchange(FAR struct rpmsg_port_spi_s *rpspi)
 {
   FAR struct rpmsg_port_header_s *txhdr;
+#ifdef CONFIG_RPMSG_PORT_RETX
+  FAR struct rpmsg_port_node_s *node;
+#endif
   int pending;
 
   IOEXP_WRITEPIN(rpspi->ioe, rpspi->mreq, 0);
@@ -290,6 +294,14 @@ static void rpmsg_port_spi_exchange(FAR struct 
rpmsg_port_spi_s *rpspi)
     {
       txhdr->cmd = RPMSG_PORT_SPI_CMD_SHUTDOWN;
     }
+#ifdef CONFIG_RPMSG_PORT_RETX
+  else if (rpmsg_port_retx_is_pending(&rpspi->port))
+    {
+      node = rpmsg_port_retx_get(&rpspi->port);
+      node->retry_count++;
+      txhdr = RPMSG_PORT_NODE_TO_BUF(&rpspi->port.txq, node);
+    }
+#endif
   else if (rpspi->txavail > 0 &&
            rpmsg_port_queue_nused(&rpspi->port.txq) > 0)
     {
@@ -298,17 +310,26 @@ static void rpmsg_port_spi_exchange(FAR struct 
rpmsg_port_spi_s *rpspi)
 
       txhdr->cmd = RPMSG_PORT_SPI_CMD_DATA;
       rpspi->txhdr = txhdr;
+      rpmsg_port_retx_add(&rpspi->port, txhdr);
     }
   else
     {
       txhdr->cmd = RPMSG_PORT_SPI_CMD_AVAIL;
     }
 
+  rpmsg_port_retx_tx_fill(&rpspi->port, txhdr);
   txhdr->avail = rpmsg_port_queue_navail(&rpspi->port.rxq);
   txhdr->avail = txhdr->avail > 1 ? txhdr->avail - 1 : 0;
   txhdr->crc = rpmsg_port_spi_crc16(txhdr);
 
-  rpmsginfo("irq send cmd:%u avail:%u\n", txhdr->cmd, txhdr->avail);
+#ifdef CONFIG_RPMSG_PORT_RETX
+  rpmsginfo("%s cmd:%u avail:%u seq:%u ack:%u ackdup:%" PRId32 "\n",
+            rpmsg_port_retx_is_pending(&rpspi->port) ? "retx" : "send",
+            txhdr->cmd, txhdr->avail, txhdr->seqno, txhdr->ackno,
+            atomic_read(&rpspi->port.retx.ackdup_count));
+#else
+  rpmsginfo("send cmd:%u avail:%u\n", txhdr->cmd, txhdr->avail);
+#endif
 
   rpmsg_port_spi_pm_action(rpspi, true);
   rpmsg_port_update_timestamp(&rpspi->port.txq, txhdr, true);
@@ -336,7 +357,6 @@ static void rpmsg_port_spi_complete_handler(FAR void *arg)
   rpmsg_port_update_timestamp(&rpspi->port.rxq, rpspi->rxhdr, false);
   if (rpspi->txhdr != NULL)
     {
-      rpmsg_port_queue_return_buffer(&rpspi->port.txq, rpspi->txhdr);
       rpspi->txhdr = NULL;
     }
   else if (rpspi->cmdhdr->cmd == RPMSG_PORT_SPI_CMD_SHUTDOWN)
@@ -385,6 +405,7 @@ static void rpmsg_port_spi_complete_handler(FAR void *arg)
       if (rpspi->rxhdr->cmd == RPMSG_PORT_SPI_CMD_CONNECT)
         {
           rpspi->state = RPMSG_PORT_SPI_STATE_RECONNECTING;
+          rpmsg_port_retx_reset(&rpspi->port);
 
           /* Drop all the unprocessed rxq buffer and pre-send txq buffer
            * when a reconnect request to be received.
@@ -403,11 +424,22 @@ static void rpmsg_port_spi_complete_handler(FAR void *arg)
       if (rpspi->state == RPMSG_PORT_SPI_STATE_DISCONNECTING)
         {
           rpspi->state = RPMSG_PORT_SPI_STATE_CONNECTING;
+          rpmsg_port_retx_reset(&rpspi->port);
         }
 
       goto unlock;
     }
 
+  /* Process ACK only in CONNECTED state to avoid acting
+   * on stale ackno from CONNECT or SHUTDOWN frames.
+   */
+
+  if (rpspi->state == RPMSG_PORT_SPI_STATE_CONNECTED)
+    {
+      rpmsg_port_retx_process_ack(&rpspi->port, rpspi->rxhdr->ackno,
+                                  rpspi->rxhdr->min_seq);
+    }
+
   if (rpspi->rxhdr->cmd == RPMSG_PORT_SPI_CMD_SUSPEND)
     {
       rpmsg_modify_signals(&rpspi->port.rpmsg, 0, RPMSG_SIGNAL_RUNNING);
@@ -421,8 +453,16 @@ static void rpmsg_port_spi_complete_handler(FAR void *arg)
       if (rpspi->rxhdr->cmd == RPMSG_PORT_SPI_CMD_SHUTDOWN)
         {
           rpspi->state = RPMSG_PORT_SPI_STATE_DISCONNECTING;
+          rpmsg_port_retx_reset(&rpspi->port);
           rpmsg_port_drop_packets(&rpspi->port, RPMSG_PORT_DROP_ALL);
         }
+      else if (rpspi->rxhdr->cmd == RPMSG_PORT_SPI_CMD_DATA)
+        {
+          if (!rpmsg_port_retx_rx_valid(&rpspi->port, rpspi->rxhdr))
+            {
+              goto unlock;
+            }
+        }
 
       rpmsg_port_queue_add_buffer(&rpspi->port.rxq, rpspi->rxhdr);
       rpspi->rxhdr = rpmsg_port_queue_get_available_buffer(
@@ -437,8 +477,9 @@ out:
     {
       rpmsg_port_spi_exchange(rpspi);
     }
-  else if (rpspi->state == RPMSG_PORT_SPI_STATE_CONNECTED &&
-      rpspi->txavail > 0 && rpmsg_port_queue_nused(&rpspi->port.txq) > 0)
+  else if (rpmsg_port_retx_is_pending(&rpspi->port) ||
+      (rpspi->state == RPMSG_PORT_SPI_STATE_CONNECTED &&
+      rpspi->txavail > 0 && rpmsg_port_queue_nused(&rpspi->port.txq) > 0))
     {
       IOEXP_WRITEPIN(rpspi->ioe, rpspi->mreq, 1);
     }
diff --git a/drivers/rpmsg/rpmsg_port_spi_slave.c 
b/drivers/rpmsg/rpmsg_port_spi_slave.c
index 247c88d986f..0a2d2e7b827 100644
--- a/drivers/rpmsg/rpmsg_port_spi_slave.c
+++ b/drivers/rpmsg/rpmsg_port_spi_slave.c
@@ -192,7 +192,8 @@ static void rpmsg_port_spi_pm_callback(wdparm_t arg)
   count = pm_wakelock_staycount(&rpspi->wakelock);
   if (count > 0 && atomic_read(&rpspi->transferring) == 0 &&
       rpmsg_port_queue_nused(&rpspi->port.txq) == 0 &&
-      rpmsg_port_queue_nused(&rpspi->port.rxq) == 0)
+      rpmsg_port_queue_nused(&rpspi->port.rxq) == 0 &&
+      !rpmsg_port_retx_is_pending(&rpspi->port))
     {
       pm_wakelock_relax(&rpspi->wakelock);
     }
@@ -242,6 +243,9 @@ rpmsg_port_spi_pm_action(FAR struct rpmsg_port_spi_s 
*rpspi, bool stay)
 static void rpmsg_port_spi_exchange(FAR struct rpmsg_port_spi_s *rpspi)
 {
   FAR struct rpmsg_port_header_s *txhdr;
+#ifdef CONFIG_RPMSG_PORT_RETX
+  FAR struct rpmsg_port_node_s *node;
+#endif
 
   if (atomic_add(&rpspi->transferring, 1))
     {
@@ -259,6 +263,14 @@ static void rpmsg_port_spi_exchange(FAR struct 
rpmsg_port_spi_s *rpspi)
     {
       txhdr->cmd = RPMSG_PORT_SPI_CMD_SHUTDOWN;
     }
+#ifdef CONFIG_RPMSG_PORT_RETX
+  else if (rpmsg_port_retx_is_pending(&rpspi->port))
+    {
+      node = rpmsg_port_retx_get(&rpspi->port);
+      node->retry_count++;
+      txhdr = RPMSG_PORT_NODE_TO_BUF(&rpspi->port.txq, node);
+    }
+#endif
   else if (rpspi->txavail > 0 &&
            rpmsg_port_queue_nused(&rpspi->port.txq) > 0)
     {
@@ -267,17 +279,26 @@ static void rpmsg_port_spi_exchange(FAR struct 
rpmsg_port_spi_s *rpspi)
 
       txhdr->cmd = RPMSG_PORT_SPI_CMD_DATA;
       rpspi->txhdr = txhdr;
+      rpmsg_port_retx_add(&rpspi->port, txhdr);
     }
   else
     {
       txhdr->cmd = RPMSG_PORT_SPI_CMD_AVAIL;
     }
 
+  rpmsg_port_retx_tx_fill(&rpspi->port, txhdr);
   txhdr->avail = rpmsg_port_queue_navail(&rpspi->port.rxq);
   txhdr->avail = txhdr->avail > 1 ? txhdr->avail - 1 : 0;
   txhdr->crc = rpmsg_port_spi_crc16(txhdr);
 
+#ifdef CONFIG_RPMSG_PORT_RETX
+  rpmsginfo("%s cmd:%u avail:%u seq:%u ack:%u ackdup:%" PRId32 "\n",
+            rpmsg_port_retx_is_pending(&rpspi->port) ? "retx" : "send",
+            txhdr->cmd, txhdr->avail, txhdr->seqno, txhdr->ackno,
+            atomic_read(&rpspi->port.retx.ackdup_count));
+#else
   rpmsginfo("send cmd:%u avail:%u\n", txhdr->cmd, txhdr->avail);
+#endif
 
   rpmsg_port_spi_pm_action(rpspi, true);
   rpmsg_port_update_timestamp(&rpspi->port.txq, txhdr, true);
@@ -415,7 +436,6 @@ static void rpmsg_port_spi_slave_notify(FAR struct 
spi_slave_dev_s *dev,
   rpmsg_port_update_timestamp(&rpspi->port.rxq, rpspi->rxhdr, false);
   if (rpspi->txhdr != NULL)
     {
-      rpmsg_port_queue_return_buffer(&rpspi->port.txq, rpspi->txhdr);
       rpspi->txhdr = NULL;
     }
   else if (rpspi->cmdhdr->cmd == RPMSG_PORT_SPI_CMD_SHUTDOWN)
@@ -464,6 +484,7 @@ static void rpmsg_port_spi_slave_notify(FAR struct 
spi_slave_dev_s *dev,
       if (rpspi->rxhdr->cmd == RPMSG_PORT_SPI_CMD_CONNECT)
         {
           rpspi->state = RPMSG_PORT_SPI_STATE_RECONNECTING;
+          rpmsg_port_retx_reset(&rpspi->port);
 
           /* Drop all the unprocessed rxq buffer and pre-send txq buffer
            * when a reconnect request to be received.
@@ -482,11 +503,22 @@ static void rpmsg_port_spi_slave_notify(FAR struct 
spi_slave_dev_s *dev,
       if (rpspi->state == RPMSG_PORT_SPI_STATE_DISCONNECTING)
         {
           rpspi->state = RPMSG_PORT_SPI_STATE_CONNECTING;
+          rpmsg_port_retx_reset(&rpspi->port);
         }
 
       goto unlock;
     }
 
+  /* Process ACK only in CONNECTED state to avoid acting
+   * on stale ackno from CONNECT or SHUTDOWN frames.
+   */
+
+  if (rpspi->state == RPMSG_PORT_SPI_STATE_CONNECTED)
+    {
+      rpmsg_port_retx_process_ack(&rpspi->port, rpspi->rxhdr->ackno,
+                                   rpspi->rxhdr->min_seq);
+    }
+
   if (rpspi->rxhdr->cmd == RPMSG_PORT_SPI_CMD_SUSPEND)
     {
       rpmsg_modify_signals(&rpspi->port.rpmsg, 0, RPMSG_SIGNAL_RUNNING);
@@ -500,8 +532,16 @@ static void rpmsg_port_spi_slave_notify(FAR struct 
spi_slave_dev_s *dev,
       if (rpspi->rxhdr->cmd == RPMSG_PORT_SPI_CMD_SHUTDOWN)
         {
           rpspi->state = RPMSG_PORT_SPI_STATE_DISCONNECTING;
+          rpmsg_port_retx_reset(&rpspi->port);
           rpmsg_port_drop_packets(&rpspi->port, RPMSG_PORT_DROP_ALL);
         }
+      else if (rpspi->rxhdr->cmd == RPMSG_PORT_SPI_CMD_DATA)
+        {
+          if (!rpmsg_port_retx_rx_valid(&rpspi->port, rpspi->rxhdr))
+            {
+              goto unlock;
+            }
+        }
 
       rpmsg_port_queue_add_buffer(&rpspi->port.rxq, rpspi->rxhdr);
       rpspi->rxhdr = rpmsg_port_queue_get_available_buffer(
@@ -512,7 +552,8 @@ static void rpmsg_port_spi_slave_notify(FAR struct 
spi_slave_dev_s *dev,
 unlock:
   spin_unlock_irqrestore(&rpspi->lock, flags);
 out:
-  if (atomic_xchg(&rpspi->transferring, 0) > 1 ||
+  if (atomic_xchg(&rpspi->transferring, 0) ||
+      rpmsg_port_retx_is_pending(&rpspi->port) ||
       (rpspi->txavail > 0 && rpmsg_port_queue_nused(&rpspi->port.txq) > 0))
     {
       rpmsg_port_spi_exchange(rpspi);
diff --git a/include/nuttx/rpmsg/rpmsg_port.h b/include/nuttx/rpmsg/rpmsg_port.h
index 3188d841a0e..aab6c89781c 100644
--- a/include/nuttx/rpmsg/rpmsg_port.h
+++ b/include/nuttx/rpmsg/rpmsg_port.h
@@ -54,6 +54,10 @@ struct rpmsg_port_config_s
 
   FAR void       *txbuf;
   FAR void       *rxbuf;
+
+#ifdef CONFIG_RPMSG_PORT_RETX
+  uint16_t       retx_max_retry;     /* Maximum retransmission attempts */
+#endif
 };
 
 #if defined(CONFIG_RPMSG_PORT_SPI) || defined(CONFIG_RPMSG_PORT_SPI_SLAVE)

Reply via email to