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

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

commit 8e906c2bf3bc429c40ecff8bb30277c3ab9663dc
Author: raiden00pl <[email protected]>
AuthorDate: Thu Nov 26 09:15:36 2026 +0100

    arch/arm/stm32: Fix the bxCAN error reports.
    
    ESR.LEC is a 3-bit code but was tested bit by bit, so an ACK error was
    reported as a stuff error and CRC errors as form errors.  The error
    interrupt wrote the TSR value back, which cleared pending RQCPx
    (write-1-to-clear) and lost their can_txdone().  The SocketCAN error
    frames had no CAN_ERR_FLAG, so they arrived as data frames.  Compare
    the masked LEC with each code, do not write TSR (ALSTx is cleared with
    RQCPx) and set CAN_ERR_FLAG.
    
    Assisted-by: Claude Code:claude-opus-5-5
    Signed-off-by: raiden00pl <[email protected]>
---
 arch/arm/src/common/stm32/stm32_can_bxcan.c      | 16 ++++++----------
 arch/arm/src/common/stm32/stm32_can_bxcan_sock.c | 18 +++++++-----------
 2 files changed, 13 insertions(+), 21 deletions(-)

diff --git a/arch/arm/src/common/stm32/stm32_can_bxcan.c 
b/arch/arm/src/common/stm32/stm32_can_bxcan.c
index 7c32484e295..15547417789 100644
--- a/arch/arm/src/common/stm32/stm32_can_bxcan.c
+++ b/arch/arm/src/common/stm32/stm32_can_bxcan.c
@@ -1861,41 +1861,41 @@ static int stm32can_sceinterrupt(int irq, void 
*context, void *arg)
 
       if (regval & CAN_ESR_LEC_MASK)
         {
-          if (regval & CAN_ESR_STUFFERROR)
+          if ((regval & CAN_ESR_LEC_MASK) == CAN_ESR_STUFFERROR)
             {
               /* Stuff Error */
 
               errbits |= CAN_ERROR_PROTOCOL;
               data[2] |= CAN_ERROR2_STUFF;
             }
-          else if (regval & CAN_ESR_FORMERROR)
+          else if ((regval & CAN_ESR_LEC_MASK) == CAN_ESR_FORMERROR)
             {
               /* Format Error */
 
               errbits |= CAN_ERROR_PROTOCOL;
               data[2] |= CAN_ERROR2_FORM;
             }
-          else if (regval & CAN_ESR_ACKERROR)
+          else if ((regval & CAN_ESR_LEC_MASK) == CAN_ESR_ACKERROR)
             {
               /* Acknowledge Error */
 
               errbits |= CAN_ERROR_NOACK;
             }
-          else if (regval & CAN_ESR_BRECERROR)
+          else if ((regval & CAN_ESR_LEC_MASK) == CAN_ESR_BRECERROR)
             {
               /* Bit recessive Error */
 
               errbits |= CAN_ERROR_PROTOCOL;
               data[2] |= CAN_ERROR2_BIT1;
             }
-          else if (regval & CAN_ESR_BDOMERROR)
+          else if ((regval & CAN_ESR_LEC_MASK) == CAN_ESR_BDOMERROR)
             {
               /* Bit dominant Error */
 
               errbits |= CAN_ERROR_PROTOCOL;
               data[2] |= CAN_ERROR2_BIT0;
             }
-          else if (regval & CAN_ESR_CRCERRPR)
+          else if ((regval & CAN_ESR_LEC_MASK) == CAN_ESR_CRCERRPR)
             {
               /* Receive CRC Error */
 
@@ -1916,10 +1916,6 @@ static int stm32can_sceinterrupt(int irq, void *context, 
void *arg)
           errbits |= CAN_ERROR_LOSTARB;
         }
 
-      /* Clear TSR register */
-
-      stm32can_putreg(priv, STM32_CAN_TSR_OFFSET, regval);
-
       /* Clear ERRI flag */
 
       stm32can_putreg(priv, STM32_CAN_MSR_OFFSET, CAN_MSR_ERRI);
diff --git a/arch/arm/src/common/stm32/stm32_can_bxcan_sock.c 
b/arch/arm/src/common/stm32/stm32_can_bxcan_sock.c
index 6b56d7ad78b..bd6507c24cf 100644
--- a/arch/arm/src/common/stm32/stm32_can_bxcan_sock.c
+++ b/arch/arm/src/common/stm32/stm32_can_bxcan_sock.c
@@ -1632,41 +1632,41 @@ static void stm32can_sceinterrupt_work(void *arg)
 
       if (regval & CAN_ESR_LEC_MASK)
         {
-          if (regval & CAN_ESR_STUFFERROR)
+          if ((regval & CAN_ESR_LEC_MASK) == CAN_ESR_STUFFERROR)
             {
               /* Stuff Error */
 
               errbits |= CAN_ERR_PROT;
               data[2] |= CAN_ERR_PROT_STUFF;
             }
-          else if (regval & CAN_ESR_FORMERROR)
+          else if ((regval & CAN_ESR_LEC_MASK) == CAN_ESR_FORMERROR)
             {
               /* Format Error */
 
               errbits |= CAN_ERR_PROT;
               data[2] |= CAN_ERR_PROT_FORM;
             }
-          else if (regval & CAN_ESR_ACKERROR)
+          else if ((regval & CAN_ESR_LEC_MASK) == CAN_ESR_ACKERROR)
             {
               /* Acknowledge Error */
 
               errbits |= CAN_ERR_ACK;
             }
-          else if (regval & CAN_ESR_BRECERROR)
+          else if ((regval & CAN_ESR_LEC_MASK) == CAN_ESR_BRECERROR)
             {
               /* Bit recessive Error */
 
               errbits |= CAN_ERR_PROT;
               data[2] |= CAN_ERR_PROT_BIT1;
             }
-          else if (regval & CAN_ESR_BDOMERROR)
+          else if ((regval & CAN_ESR_LEC_MASK) == CAN_ESR_BDOMERROR)
             {
               /* Bit dominant Error */
 
               errbits |= CAN_ERR_PROT;
               data[2] |= CAN_ERR_PROT_BIT0;
             }
-          else if (regval & CAN_ESR_CRCERRPR)
+          else if ((regval & CAN_ESR_LEC_MASK) == CAN_ESR_CRCERRPR)
             {
               /* Receive CRC Error */
 
@@ -1687,10 +1687,6 @@ static void stm32can_sceinterrupt_work(void *arg)
           errbits |= CAN_ERR_LOSTARB;
         }
 
-      /* Clear TSR register */
-
-      stm32can_putreg(priv, STM32_CAN_TSR_OFFSET, regval);
-
       /* Clear ERRI flag */
 
       stm32can_putreg(priv, STM32_CAN_MSR_OFFSET, CAN_MSR_ERRI);
@@ -1704,7 +1700,7 @@ static void stm32can_sceinterrupt_work(void *arg)
 
       /* Copy frame */
 
-      frame->can_id  = errbits;
+      frame->can_id  = CAN_ERR_FLAG | errbits;
       frame->can_dlc = CAN_ERR_DLC;
 
       memcpy(frame->data, data, CAN_ERR_DLC);

Reply via email to