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

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

commit f94160095ea2411317b3b37f177acbca4544ecd1
Author: Shoukui Zhang <[email protected]>
AuthorDate: Thu Dec 21 10:43:26 2023 +0800

    Adapt i2c slave callback interface for rp2040 and s32k11x
    
    Signed-off-by: Shoukui Zhang <[email protected]>
---
 arch/arm/src/rp2040/rp2040_i2c_slave.c                    | 15 ++++++++++++---
 arch/arm/src/s32k1xx/s32k1xx_lpi2c_slave.c                | 12 +++++++++++-
 boards/arm/s32k1xx/rddrone-bms772/src/s32k1xx_smbus_sbd.c | 15 ++++++++++++---
 3 files changed, 35 insertions(+), 7 deletions(-)

diff --git a/arch/arm/src/rp2040/rp2040_i2c_slave.c 
b/arch/arm/src/rp2040/rp2040_i2c_slave.c
index af9e057dc5..c2b1d9f066 100644
--- a/arch/arm/src/rp2040/rp2040_i2c_slave.c
+++ b/arch/arm/src/rp2040/rp2040_i2c_slave.c
@@ -152,6 +152,7 @@ static int i2c_interrupt(int irq, void *context, void *arg)
   rp2040_i2c_slave_t *priv = (rp2040_i2c_slave_t *)arg;
   uint32_t  data_cmd;
   uint32_t  state;
+  int length;
 
   state = getreg32(RP2040_I2C_IC_INTR_STAT(priv->controller));
 
@@ -159,7 +160,8 @@ static int i2c_interrupt(int irq, void *context, void *arg)
 
   if (state & RP2040_I2C_IC_INTR_STAT_R_RD_REQ)
     {
-      if (priv->tx_buf_ptr < priv->tx_buf_end)
+      length = priv->tx_buf_end - priv->tx_buf_ptr;
+      if (length > 0)
         {
           while (priv->tx_buf_ptr < priv->tx_buf_end
                  &&   getreg32(RP2040_I2C_IC_TXFLR(priv->controller))
@@ -168,6 +170,11 @@ static int i2c_interrupt(int irq, void *context, void *arg)
               putreg32(*priv->tx_buf_ptr++,
                        RP2040_I2C_IC_DATA_CMD(priv->controller));
             }
+
+          if (priv->callback != NULL)
+            {
+              priv->callback(priv, I2CS_TX_COMPLETE, length);
+            }
         }
       else
         {
@@ -203,7 +210,8 @@ static int i2c_interrupt(int irq, void *context, void *arg)
     {
       if (priv->callback != NULL && priv->rx_buf_ptr > priv->rx_buffer)
         {
-          priv->callback(priv, priv->rx_buf_ptr - priv->rx_buffer);
+          priv->callback(priv, I2CS_RX_COMPLETE,
+                         priv->rx_buf_ptr - priv->rx_buffer);
           priv->rx_buf_ptr = priv->rx_buffer;
         }
 
@@ -216,7 +224,8 @@ static int i2c_interrupt(int irq, void *context, void *arg)
     {
       if (priv->callback != NULL && priv->rx_buf_ptr > priv->rx_buffer)
         {
-          priv->callback(priv, priv->rx_buf_ptr - priv->rx_buffer);
+          priv->callback(priv, I2CS_RX_COMPLETE,
+                         priv->rx_buf_ptr - priv->rx_buffer);
           priv->rx_buf_ptr = priv->rx_buffer;
         }
 
diff --git a/arch/arm/src/s32k1xx/s32k1xx_lpi2c_slave.c 
b/arch/arm/src/s32k1xx/s32k1xx_lpi2c_slave.c
index 32792bbb0f..533f97c585 100644
--- a/arch/arm/src/s32k1xx/s32k1xx_lpi2c_slave.c
+++ b/arch/arm/src/s32k1xx/s32k1xx_lpi2c_slave.c
@@ -427,9 +427,19 @@ static int s32k1xx_lpi2c_slave_isr_process(
 
       if ((priv->read_bufindex > 0) && (priv->callback != NULL))
         {
-          priv->callback(priv->callback_arg, priv->read_bufindex);
+          priv->callback(priv->callback_arg, I2CS_RX_COMPLETE,
+                         priv->read_bufindex);
           priv->read_bufindex = 0;
         }
+
+      /* Execute the registered callback function if data was send */
+
+      if ((priv->write_bufindex > 0) && (priv->callback != NULL))
+        {
+          priv->callback(priv->callback_arg, I2CS_TX_COMPLETE,
+                         priv->write_bufindex);
+          priv->write_bufindex = 0;
+        }
     }
 
   /* Slave Bit Error (abort current transfer) */
diff --git a/boards/arm/s32k1xx/rddrone-bms772/src/s32k1xx_smbus_sbd.c 
b/boards/arm/s32k1xx/rddrone-bms772/src/s32k1xx_smbus_sbd.c
index c870a77e24..0d48fb45a0 100644
--- a/boards/arm/s32k1xx/rddrone-bms772/src/s32k1xx_smbus_sbd.c
+++ b/boards/arm/s32k1xx/rddrone-bms772/src/s32k1xx_smbus_sbd.c
@@ -384,10 +384,12 @@ static ssize_t smbus_sbd_write(struct file *filep, const 
char *buffer,
  *
  ****************************************************************************/
 
-static int smbus_sbd_callback(void *arg, size_t rx_len)
+static int smbus_sbd_callback(void *arg, i2c_slave_complete_t state,
+                              size_t rx_len)
 {
   struct smbus_sbd_dev_s *dev;
   int buffer_length;
+  int ret = OK;
   int i;
 
   /* Retrieve the pointer to the SMBus SBD slave device struct */
@@ -726,8 +728,15 @@ static int smbus_sbd_callback(void *arg, size_t rx_len)
    * request has been received.
    */
 
-  return I2CS_WRITE(dev->i2c_slave_dev, (const uint8_t *)dev->write_buffer,
-                    buffer_length);
+  ret = I2CS_WRITE(dev->i2c_slave_dev, (const uint8_t *)dev->write_buffer,
+                   buffer_length);
+  if (ret >= 0)
+    {
+      dev->i2c_slave_dev->callback(dev->i2c_slave_dev->callback_arg,
+                                   I2CS_TX_COMPLETE, buffer_length);
+    }
+
+  return ret;
 }
 
 /****************************************************************************

Reply via email to