Site Information

 Loading... Please wait...

Error Reporting in SocketCAN with Specific Reference to the MCP2515 CAN Controller

Posted by Wilfried Voss on

Error Reporting in SocketCANSocketCAN is a set of open-source CAN drivers and a network stack, included in the Linux kernel, which allows CAN devices to be accessed via standard socket APIs. It provides a flexible and extensible framework for CAN communication, diagnostics, and application development.

One powerful feature of SocketCAN is error reporting via special CAN frames, which is essential for debugging, maintaining communication reliability, and monitoring CAN bus health.

This report focuses on:

  • General error reporting in SocketCAN.

  • Specific capabilities and limitations of the MCP2515 CAN controller in this context.

  • A technical comparison with more advanced controllers.


2. Error Reporting in SocketCAN

SocketCAN, the native CAN interface for Linux, supports a powerful feature known as error reporting. Unlike traditional CAN implementations that primarily focus on frame transmission and reception, SocketCAN extends its capabilities by allowing software-level visibility into CAN controller states and bus health.

Error reporting in SocketCAN is implemented through special software-generated error frames—known as CAN_ERR frames. These frames are not transmitted on the physical CAN bus; instead, they are delivered directly to user-space applications through the same socket interface used for standard CAN communication.

Each error frame carries a can_id with the CAN_ERR_FLAG set and a payload (data[]) containing diagnostic information. This design enables real-time monitoring of network reliability, controller behavior, and bus anomalies.


Types of Errors Reported

SocketCAN supports a wide range of error types, including:

  • Bus-Off Detection (CAN_ERR_BUSOFF): Indicates the controller has disconnected due to excessive errors.

  • Error Passive/Warning State (CAN_ERR_CRTL): Alerts the application when the controller's error counters exceed predefined thresholds.

  • Arbitration Lost (CAN_ERR_LOSTARB): Signals that the node lost arbitration during transmission.

  • Protocol Violations (CAN_ERR_PROT): Covers CAN-layer issues like bit errors, CRC mismatches, or form violations.

  • No Acknowledge Received (CAN_ERR_ACK): Indicates that a transmitted frame was not acknowledged by any other node.

  • Controller Restarted (CAN_ERR_RESTARTED): Notifies that the controller was restarted after a bus-off state.


How It Works

To enable error reporting, applications set an error mask on a raw CAN socket using the CAN_RAW_ERR_FILTER option. This allows selective reception of only those error frames relevant to the application.

Example in C:

int err_mask = CAN_ERR_BUSOFF | CAN_ERR_CRTL | CAN_ERR_PROT; setsockopt(sock, SOL_CAN_RAW, CAN_RAW_ERR_FILTER, &err_mask, sizeof(err_mask));

Once set, any matching error event triggers a CAN_ERR frame that is received through the normal recv() system call.


Advantages of SocketCAN Error Reporting

1. Real-Time Diagnostics

Applications receive error notifications as soon as they occur, allowing immediate action—such as logging, fallback behavior, or notifying users.

2. Granular Visibility

Unlike traditional black-box CAN stacks, SocketCAN exposes detailed controller states, enabling developers to understand:

  • Whether errors are due to transmission issues, protocol violations, or physical layer problems

  • If a node has become error-passive or bus-off

  • Which error counters are rising and under what conditions

3. Non-Intrusive Monitoring

Because CAN_ERR frames are software-generated, they do not impact bus traffic or require special hardware. They allow passive health monitoring without interfering with communication.

4. Integration with Linux Tools

Linux utilities such as candump -e (from the can-utils package) can display error frames in human-readable form, making SocketCAN ideal for development, debugging, and production diagnostics.

5. Flexible and Extensible

With its standardized interface, SocketCAN allows developers to:

  • Subscribe to only the error types they care about

  • Integrate seamlessly into C/C++, Python, Rust, or other environments

  • Build robust error-handling logic based on live bus conditions

SocketCAN error reporting transforms Linux-based CAN communication into a transparent, diagnosable, and intelligent system. By providing detailed insights into bus and controller conditions, it helps engineers detect faults, optimize reliability, and develop more resilient applications.

Whether you're debugging an embedded system, validating a networked automotive ECU, or logging faults in an industrial automation setup, SocketCAN error frames are an indispensable tool in the developer's toolkit.

2.1 Error Frame Basics

In SocketCAN, error frames are represented as software-generated diagnostic frames, not actual error frames on the CAN bus (since the standard CAN specification does not allow transmission of error frames by user applications).

These are internally generated by the CAN driver and kernel stack and are distinguished by the CAN_ERR_FLAG (0x20000000) in the CAN identifier field.

2.2 Error Categories and Data Fields

Each error frame can include multiple error conditions, packed in both the can_id and the data[] payload.

Error Flag Meaning
CAN_ERR_TX_TIMEOUT Timeout waiting for TX complete
CAN_ERR_LOSTARB Arbitration lost during message sending
CAN_ERR_CRTL Controller state issues: warning, passive, or bus-off
CAN_ERR_PROT Protocol violations: form, CRC, stuff, bit errors
CAN_ERR_TRX Physical transceiver failure (e.g., dominant/recessive stuck states)
CAN_ERR_ACK No acknowledge received
CAN_ERR_BUSOFF Controller entered bus-off state
CAN_ERR_RESTARTED Controller restarted after bus-off recovery

2.3 data[] Field Interpretation

The data[] field carries additional context based on the specific error type, e.g.:

  • data[1] might include control state (tx/rx error counter values)

  • data[2] contains protocol violation types (e.g., bit error, stuff error)

SocketCAN documentation outlines the structure, and tools like candump -e from can-utils interpret this data.


3. Error Reporting Mechanism in SocketCAN

3.1 Enabling Error Reporting in Applications

To receive error frames:

  1. Create a raw CAN socket:

    int s = socket(PF_CAN, SOCK_RAW, CAN_RAW);
  2. Set error mask:

    int err_mask = CAN_ERR_BUSOFF | CAN_ERR_CRTL | CAN_ERR_PROT; setsockopt(s, SOL_CAN_RAW, CAN_RAW_ERR_FILTER, &err_mask, sizeof(err_mask));
  3. Bind to a CAN interface:

    struct sockaddr_can addr = { .can_family = AF_CAN, .can_ifindex = if_nametoindex("can0") }; bind(s, (struct sockaddr *)&addr, sizeof(addr));

The socket will now receive error frames when they are generated by the driver.


4. MCP2515 CAN Controller: Technical Overview

4.1 Chip Capabilities

  • SPI-based CAN 2.0B controller (standard/extended IDs)

  • 3 transmit buffers, 2 receive buffers

  • Error Warning Limit, Error Passive, and Bus-Off states

  • Transmit and receive error counters

  • Registers:

    • TEC/REC: Transmit/Receive error counters

    • EFLG: Error flag register indicating overflow, bus-off, etc.

    • CANINTF: Interrupt flags (e.g., TX, RX, error warning)

    • CANSTAT: Bus state (normal, listen-only, loopback)

4.2 Error-Related Conditions Detected

Error Condition Hardware Detection Notes
TX buffer full Triggered via status polling
Error warning (TEC/REC > 96) Flagged in EFLG.EWARN
Error passive (TEC/REC > 127) Flagged in EFLG.EPASS
Bus-off (TEC > 255) Flagged in EFLG.TXBO
RX overflow Flagged in EFLG.RX1OVFL / RX0OVFL
Arbitration lost ✅ (partially) Only for certain conditions; not exposed via driver
Protocol violations Not detailed internally
Bit/stuff/form errors No internal decoding of CAN protocol-level error

5. MCP2515 + SocketCAN via mcp251x Driver

5.1 SocketCAN Driver Overview

  • Driver: mcp251x.c

  • Interfaces MCP2515 over SPI

  • Converts hardware status into Linux CAN interface updates

  • Reports only limited error information

5.2 Reported Errors in Practice

When errors occur, the driver reads EFLG and generates error frames such as:

SocketCAN Error Frame Trigger from MCP2515
CAN_ERR_BUSOFF EFLG.TXBO
CAN_ERR_CRTL EFLG.EWARN / EPASS
CAN_ERR_RESTARTED After automatic restart (if enabled)
CAN_ERR_PROT ❌ Not supported
CAN_ERR_ACK ❌ Not supported

No CAN_ERR_LOSTARB, CAN_ERR_PROT, or CAN_ERR_TRX frames are provided because the MCP2515 does not supply the low-level diagnostics needed to trigger these reports.

5.3 Example Error Frame from MCP2515 in Linux

candump -e can0

Output:

can0 20000004 [8] 40 00 00 00 02 00 00 00
  • 0x20000004: CAN_ERR_FLAG | CAN_ERR_BUSOFF

  • Indicates the controller has gone bus-off but gives no cause or contributing factor.


6. Comparison with Advanced CAN Controllers

Feature MCP2515 STM32 bxCAN / FDCAN MCP2517FD TI TCAN4x
Error passive detection ✅ Yes ✅ Yes ✅ Yes ✅ Yes
Bus-off detection ✅ Yes ✅ Yes ✅ Yes ✅ Yes
Detailed protocol violations ❌ No ✅ Yes ✅ Yes ✅ Yes
Arbitration lost reporting ❌ No ✅ Yes ✅ Yes ✅ Yes
CAN FD support ❌ No ⚠️ Depends ✅ Yes ✅ Yes
Full error frame mapping ❌ Partial ✅ Full ✅ Full ✅ Full
SPI bottleneck ⚠️ Yes ❌ (native) ⚠️ Yes ❌ (native)

7. Known Issues and Debugging Constraints

7.1 Common Problems with MCP2515

  • Latency due to SPI communication

  • Missed error events during high traffic

  • Buffer overruns if SPI polling is not fast enough

  • No isolation of protocol-specific errors

7.2 Implications

For developers:

  • Diagnosing complex bus problems (like short pulses, noise, timing violations) is not feasible using MCP2515 + SocketCAN alone.

  • For real-time diagnostics, one must add external bus analyzers or logic probes.


8. Recommendations

Use MCP2515 + SocketCAN if:

  • Your project needs simple, low-cost CAN support

  • Error detection is limited to bus-off, passive, or warning states

  • You have external means for deeper diagnostics (e.g., oscilloscopes)

Avoid MCP2515 if:

  • You need to debug protocol-level errors

  • You require CAN FD support

  • You need detailed insight into arbitration loss, CRC faults, or physical layer issues

Recommended Alternatives:

  • MCP2517FD: Fully supports CAN FD + improved diagnostics

  • TI TCAN4550: SPI CAN FD controller with rich error reporting

  • STM32: On-chip CAN controller with tight integration

  • NXP i.MX RT or S32K series: High-performance CAN support with detailed SocketCAN drivers


9. Resources


10. Conclusion

While SocketCAN offers a powerful and extensible framework for CAN error reporting, the MCP2515's hardware limitations and the constraints of the mcp251x driver restrict the richness of error diagnostics. Only high-level errors like bus-off and passive state transitions are reported, with no support for in-depth protocol violations.

For mission-critical or diagnostic-heavy applications, developers should consider more capable CAN controllers with full driver support in SocketCAN to harness its complete error reporting features.