NetBurner 3.5.8
PDF Version
CAN Error Counter Monitor

Example Path: examples/PlatformSpecific/MODRT1171/CAN/CanErrorMonitor

Overview

Every CAN controller tracks its own health with two error counters defined by the CAN specification, and this example shows how to read and interpret them on the i.MX RT1171's FlexCAN:

  • TEC (Transmit Error Counter) counts up by 8 each time one of this node's transmissions fails – most commonly because no other node acknowledged the frame – and back down by 1 with each successful transmission.
  • REC (Receive Error Counter) counts up when receive errors are seen on the bus and back down with each good reception.

The counters drive the controller's fault-confinement state:

State Condition Behavior
ERROR ACTIVE TEC and REC < 128 Normal operation
ERROR PASSIVE TEC or REC >= 128 Node keeps working but signals errors passively, so a faulty node cannot disturb bus traffic
BUS OFF TEC > 255 Node has detached from the bus

A monitor task samples the counters and fault state once a second and prints a one-line summary whenever something changes:

CAN: TEC=128 REC= 0 ERR-PASSIVE TX-WARNING

Everything is read by polling two status registers (the ECR error counters and the ESR1 fault state) – no CAN interrupts are used – so the monitor code is easy to lift into your own application as a periodic health check.

FlexCAN recovers from bus-off automatically: after 128 occurrences of 11 consecutive recessive bits on the bus, the controller re-enters the error-active state and the counters restart at 0. The 'R' menu command additionally shows a full manual re-initialization of the controller.

The FlexCAN driver (fsl_flexcan, NXP SDK version 2.7.1) is built into the NetBurner MODRT1171 platform library; the example includes <fsl_flexcan.h> directly and bundles no driver source.

Hardware requirements

  • MODRT1171 module
  • Personal Computer
  • Serial terminal program connected to the MODRT1171 UART0 serial port
  • Optional: 3.3 V CAN transceiver (SN65HVD230 or similar) and a second CAN node at 500 kbit/s, wired as in the CanSimple example

Board settings

Same wiring as the CanSimple example: FlexCAN1 RX on module pin P1.26, TX on P1.27 (rev 1.5), to the transceiver's RXD/TXD. Running with no transceiver at all is a supported – and instructive – configuration, described below.

Note
This example is written for the MODRT1171 rev 1.5 module. At rev 1.6 (which makes the module a MOD5441X drop-in replacement), the FlexCAN1 signals move from the P1 header to the P2 header: RX moves P1.26 -> P2.41 and TX moves P1.27 -> P2.44. The FlexCAN1 peripheral instance and this example's driver code are unchanged; only the two pin-function lines in UserMain() change with the module revision. Designs that need more than one CAN bus have options beyond FlexCAN1: FlexCAN3 is available on the I2C pins for designs that do not use I2C (or that bit-bang I2C on spare GPIO), and on rev 1.5 FlexCAN2 also reaches module pins. For pin details see the MODRT1171 Software Developer Guide and the MODRT1171 Pinout Update document: https://www.netburner.com/download/MODRT1171_Rev1p5_to_Rev1p6_Pinout_Update/

Running the demo

Build and load the application, then open the serial terminal:

Monitor start -- CAN: TEC= 0 REC= 0 ERR-ACTIVE
Menu:
S - transmit a frame on ID 0x123
P - print the error counters and fault state now
R - re-initialize the controller
void print(const char *str)
Write out a zero-terminated, unbuffered string.

The instructive way to run this example is with NO transceiver attached – one 'S' press makes every state the controller can reach play out on its own. With nothing driving the RX pad (the example pulls it up so the line reads as an idle bus), each dominant bit the controller transmits reads back recessive – a bit error – so every transmit attempt raises TEC by 8. TEC races through ERR-PASSIVE (128) into BUS-OFF (above 255) within milliseconds; FlexCAN's automatic bus-off recovery then observes 128 x 11 recessive bit times on the idle line, returns the controller to ERR-ACTIVE with the counters cleared, the still-pending frame retries, and the cycle repeats several times per second. The once-a-second monitor samples this cycle at random points, so the output looks like:

Sent ID 0x123
CAN: TEC= 94 REC= 0 BUS-OFF
CAN: TEC= 50 REC= 0 BUS-OFF
CAN: TEC=240 REC= 0 ERR-PASSIVE TX-WARNING
CAN: TEC=117 REC= 0 BUS-OFF TX-WARNING

Press 'R' to cancel the retrying frame; the monitor then reports a quiet controller:

Re-initializing FlexCAN controller ***
CAN: TEC= 0 REC= 0 ERR-ACTIVE

With a transceiver attached but no second node, the failure mode changes: transmitted bits read back correctly through the transceiver and only the acknowledgement is missing (an ACK error). TEC climbs by 8 per attempt and parks at 128, ERR-PASSIVE – the CAN specification stops an error-passive transmitter that sees only missing acknowledgements from counting further, so BUS-OFF is not reached in this configuration.

On a working bus (transceiver plus a second node), each acknowledged transmission steps TEC back down, and the monitor reports the return to ERR-ACTIVE as the counters recover.

To verify FlexCAN operation on a single module with no external hardware, run the FlexcanLoopback example first; for interactive send/receive between two nodes, see the CanSimple example.