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 e3144ccbc0ea8f1c9249d3cf789af471df0c0e84
Author: Darryl Ring <[email protected]>
AuthorDate: Tue Oct 27 10:09:53 2026 -0700

    nuttx/can: Add CANIOC_GET_ERRORS ioctl
    
    Add a new CAN ioctl to return error state and counters from the CAN
    controller.
    
    Assisted-by: Claude:claude-sonnet-5-5
    Signed-off-by: Darryl Ring <[email protected]>
---
 Documentation/os/drivers/character/can.rst |  1 +
 include/nuttx/can/can.h                    | 40 +++++++++++++++++++++++++++++-
 2 files changed, 40 insertions(+), 1 deletion(-)

diff --git a/Documentation/os/drivers/character/can.rst 
b/Documentation/os/drivers/character/can.rst
index 30e097c07b7..2e7f2dd0fe2 100644
--- a/Documentation/os/drivers/character/can.rst
+++ b/Documentation/os/drivers/character/can.rst
@@ -68,6 +68,7 @@ The upper half driver supports the following ``ioctl`` 
commands:
   will always provide only a single message.
 - **CANIOC_GET_MSGALIGN**: Get messages alignment. See CANIOC_SET_MSGALIGN for
   explanation.
+- **CANIOC_GET_ERRORS**: Get the CAN controller error state and counters.
 
 The upper half driver supports the **strict TX priority ordering**:
 
diff --git a/include/nuttx/can/can.h b/include/nuttx/can/can.h
index 58bd0e20c20..1dc6ff1f311 100644
--- a/include/nuttx/can/can.h
+++ b/include/nuttx/can/can.h
@@ -330,6 +330,16 @@
  *                   is returned with the errno variable set to indicate the
  *                   nature of the error.
  *   Dependencies:   None
+ *
+ * CANIOC_GET_ERRORS
+ *   Description:    Get the CAN controller error state and counters.
+ *
+ *   Argument:       A pointer to a write-able instance of struct
+ *                   canioc_errors_s in which the values will be returned.
+ *   returned Value: Zero (OK) is returned on success.  Otherwise -1 (ERROR)
+ *                   is returned with the errno variable set to indicate the
+ *                   nature of the error.
+ *   Dependencies:   None
  */
 
 #define CANIOC_RTR                _CANIOC(1)
@@ -353,9 +363,10 @@
 #define CANIOC_GET_TRANSVSTATE    _CANIOC(19)
 #define CANIOC_SET_MSGALIGN       _CANIOC(20)
 #define CANIOC_GET_MSGALIGN       _CANIOC(21)
+#define CANIOC_GET_ERRORS         _CANIOC(22)
 
 #define CAN_FIRST                 0x0001         /* First common command */
-#define CAN_NCMDS                 21             /* 21 common commands   */
+#define CAN_NCMDS                 22             /* 22 common commands   */
 
 /* User defined ioctl commands are also supported. These will be forwarded
  * by the upper-half CAN driver to the lower-half CAN driver via the
@@ -909,6 +920,33 @@ struct canioc_bittiming_s
   uint8_t               bt_sjw;          /* Synchronization Jump Width in time 
quanta */
 };
 
+/* CANIOC_GET_ERRORS:
+ *
+ * The state is error-active unless the controller reports otherwise;
+ * WARNING is error-active with either error counter at or above 96.
+ * PASSIVE and BUSOFF follow the controller's own state.
+ */
+
+#define CANIOC_ERRSTATE_ACTIVE    0
+#define CANIOC_ERRSTATE_WARNING   1
+#define CANIOC_ERRSTATE_PASSIVE   2
+#define CANIOC_ERRSTATE_BUSOFF    3
+
+struct canioc_errors_s
+{
+  uint8_t               state;           /* CANIOC_ERRSTATE_* */
+  uint8_t               txerr;           /* Transmit error counter (TEC) */
+  uint8_t               rxerr;           /* Receive error counter (REC) */
+  uint32_t              errors;          /* Bus errors observed since the
+                                          * device was opened, monotonic */
+  uint32_t              arb_errors;      /* Errors observed during the
+                                          * arbitration phase */
+  uint32_t              data_errors;     /* Errors observed during the
+                                          * data phase */
+  uint32_t              rx_overruns;     /* RX frames the controller
+                                          * overwrote unread */
+};
+
 /* CANIOC_GET_CONNMODES/CANIOC_SET_CONNMODES:
  *
  * A CAN device may support loopback and silent mode. Both modes may not be

Reply via email to