Namespace CANProtocol
Public Static Attributes
| Type | Name |
|---|---|
| constexpr uint32_t | DUP_NODE_TIMEOUT_MS = 3000Duplicate NODE_ID clear time (#344): DUPLICATE_NODE_ID drops this long after the last foreign frame on our own HB_NODE_ID — 6× the 500 ms HB cadence, matchingPanelBridge 's own node timeout. A live duplicate re-raises within one HB period. |
| constexpr uint32_t | MASTER_TIMEOUT_MS = 1500Master-loss timeout: 3× the 500 ms HB_0 cadence, matchingPanelBridge 's own node timeout ratio (3000 ms / 500 ms). |
Public Functions
| Type | Name |
|---|---|
| bool | busOff () Return true if the CAN controller is in bus-off state. |
| void | drain () Process RX callbacks and batched-ControlPacket deadlines. Call once per loop(). |
| void | filterAcceptAll () Accept all incoming CAN frames. Use for PanelBridge . |
| void | filterAcceptId (uint32_t canId) Accept a specific CAN ID. Use for PanelGroup nodes. |
| void | flushBatched (uint32_t canId) Force a half-full ControlPacketPair batch to send immediately. |
| const NodeHealthPayload & | lastNodeHealth () The HEALTH_n payload serviceNodeStatus() last built for this node (#344). |
| HeartbeatPayload | makeHeartbeatPayload (uint8_t nodeId, uint16_t rxCount) Build the standard 8-byte heartbeat payload for the current node. |
| NodeHealthPayload | makeNodeHealthPayload (uint8_t nodeId, NodeKind kind, int8_t dieTempC, NodeFaultCode fault=NodeFaultCode::NONE) Build the 8-byte node-health payload for the current node. |
| ReadyPayload | makeReadyPayload (uint8_t flags=0) Build the 8-byte READY_n payload: this build's firmware release version (#295). |
| void | onReceive (CanRxCallback cb) Register a general-purpose RX frame handler. |
| void | onStatusChange (CanStatusCallback cb) Register a CAN bus status change callback. |
| void | onSyncReq (CanSyncReqCallback cb) Register a SYNC_REQ handler. |
| uint8_t | rec () Return the CAN Receive Error Counter (0-255) from ESR. |
| uint32_t | rxDropCount () Return the cumulative RX drop count since startup (#387): frames lost because the software RX ring was full when they arrived. Should stay 0; nonzero means loop() falls behind the bus. |
| uint32_t | rxOverrunCount () Return the cumulative RX FIFO overrun count since startup (#389): frames lost in hardware because all three FIFO slots were full when they arrived. RX is interrupt-driven, so this needs interrupts held off for three frame times; should stay 0. |
| void | send (uint32_t canId, const uint8_t * data, uint8_t len) Send a CAN frame. |
| void | sendBatched (uint32_t canId, const ControlPacket & pkt) Submit one ControlPacket to a CANProtocol-owned ControlPacketPair batch. |
| void | sendReady () Send this node's boot READY_n ( canIdReady(NODE_ID) ) carryingmakeReadyPayload() . |
| void | sendSyncReq (uint8_t nodeId) Ask one node to re-send its state: SYNC_REQ naming nodeId (#387).PanelBridge only. |
| void | serviceNodeStatus (NodeKind kind, uint16_t rxCount=0) Run the node-generic status duties. Call once per loop(), after drain() . |
| void | start () Start the CAN peripheral and apply registered filters. |
| void | startLoopback () Start in silent loopback mode — for bench testing only. |
| uint8_t | tec () Return the CAN Transmit Error Counter (0-255) from ESR. |
| uint32_t | txDropCount () Return the cumulative TX queue drop count since startup. |
Public Static Attributes Documentation
variable DUP_NODE_TIMEOUT_MS
Duplicate NODE_ID clear time (#344): DUPLICATE_NODE_ID drops this long after the last foreign frame on our own HB_NODE_ID — 6× the 500 ms HB cadence, matchingPanelBridge 's own node timeout. A live duplicate re-raises within one HB period.
constexpr uint32_t CANProtocol::DUP_NODE_TIMEOUT_MS;
variable MASTER_TIMEOUT_MS
Master-loss timeout: 3× the 500 ms HB_0 cadence, matchingPanelBridge 's own node timeout ratio (3000 ms / 500 ms).
constexpr uint32_t CANProtocol::MASTER_TIMEOUT_MS;
Public Functions Documentation
function busOff
Return true if the CAN controller is in bus-off state.
bool CANProtocol::busOff ()
function drain
Process RX callbacks and batched-ControlPacket deadlines. Call once per loop().
void CANProtocol::drain ()
Drains all frames received since the last call:
* SYNC_REQ: fires onSyncReq(), not forwarded to onReceive().
* TEST_SEQ: auto-replies with ECHO carrying the same payload, not forwarded.
* HB_0 on a non-bridge node: stamped for serviceNodeStatus()'s watchdog, then onReceive().
* The node's own HB_NODE_ID: stamped for serviceNodeStatus()'s duplicate NODE_ID check (#344), then onReceive().
* The node's own BOOT_CMD_n (not on the bridge): a CONNECT resets into the bootloader when one is installed (STM32Board::enterBootloader(), #365); never forwarded.
* All other frames: fires onReceive().
Also services sendBatched() deadlines: a half-full ControlPacketPair that has waited two drain() calls — and, on PanelBridge, at least 2 ms (#389) — is flushed with slot B set to the null sentinel.
function filterAcceptAll
Accept all incoming CAN frames. Use for PanelBridge .
void CANProtocol::filterAcceptAll ()
Configures a pass-all hardware mask filter. Mandatory IDs (CTRL_BCAST, TEST_SEQ, SYNC_REQ) are included but have no effect with pass-all active.
function filterAcceptId
Accept a specific CAN ID. Use for PanelGroup nodes.
void CANProtocol::filterAcceptId (
uint32_t canId
)
Adds one ID to the hardware filter list. Call multiple times for multiple IDs. CTRL_BCAST (0x7E0), TEST_SEQ (0x011), and SYNC_REQ (0x012) are always included automatically by start() — do not add them manually.
Parameters:
canId11-bit standard CAN ID to accept.
function flushBatched
Force a half-full ControlPacketPair batch to send immediately.
void CANProtocol::flushBatched (
uint32_t canId
)
If the named CAN ID has a pending slot A, sends it with slot B as the null sentinel. No-op if no packet is pending.
Parameters:
canIdCAN_ID_CTRL_BCAST, canIdEvt(NODE_ID), canIdEvtRel(NODE_ID), canIdEvtDir(NODE_ID), or canIdEvtAction(NODE_ID).
function lastNodeHealth
The HEALTH_n payload serviceNodeStatus() last built for this node (#344).
const NodeHealthPayload & CANProtocol::lastNodeHealth ()
PanelBridge reports it as its own node-0 _NODE_STATUS row. Before the first HEALTH_n (or with -DNODE_HEALTH_TELEM=0) it reads not-yet-seen: dieTempC DIE_TEMP_UNAVAILABLE, everything else 0 (nodeKind UNKNOWN) — the same rule the bridge applies to a node it has no HEALTH_n from.
function makeHeartbeatPayload
Build the standard 8-byte heartbeat payload for the current node.
HeartbeatPayload CANProtocol::makeHeartbeatPayload (
uint8_t nodeId,
uint16_t rxCount
)
Fills uptime, CAN health flags, and ESR-derived TEC/REC from CANProtocol state.
Parameters:
nodeIdNode ID (1-63 PanelGroup; GATEWAY_NODE_ID for PanelBridge).rxCountCaller-owned receive counter to embed in the payload.
Returns:
Fully populated HeartbeatPayload ready to send as HB_n.
function makeNodeHealthPayload
Build the 8-byte node-health payload for the current node.
NodeHealthPayload CANProtocol::makeNodeHealthPayload (
uint8_t nodeId,
NodeKind kind,
int8_t dieTempC,
NodeFaultCode fault=NodeFaultCode::NONE
)
The CAN/DCS packing boundary: takes the internal die temperature (via STM32Board::readDieTempC()) and the node's aggregated fault (via NodeStatus::aggregateFaults()) and packs them into a NodeHealthPayload. Sets the overheat flag (bit0) only when NODE_OVERHEAT_C is defined and dieTempC meets it; sets the DEGRADED flag (bit1) and faultId when fault is not NONE (the enum→byte cast happens here), and nodeKind from kind. faultMask + reserved bytes stay 0.
Parameters:
nodeIdNode ID (1-63 PanelGroup; GATEWAY_NODE_ID PanelBridge).kindThe node's board kind (#344) — from its firmware role, never from nodeId.dieTempCInternal die temp in whole °C (DIE_TEMP_UNAVAILABLE = unavailable).faultAggregated node fault (NodeFaultCode::NONE = healthy). Default NONE.
Returns:
Fully populated NodeHealthPayload ready to send as HEALTH_n.
function makeReadyPayload
Build the 8-byte READY_n payload: this build's firmware release version (#295).
ReadyPayload CANProtocol::makeReadyPayload (
uint8_t flags=0
)
Parameters:
flagsReadyFlag bits — 0 for the boot READY.
Returns:
ReadyPayload with fwMajor/fwMinor/fwPatch from FirmwareVersion.h, the bootloader version (STM32Board::bootloaderVersion()), flags, reserved bytes 0.
function onReceive
Register a general-purpose RX frame handler.
void CANProtocol::onReceive (
CanRxCallback cb
)
Fired by drain() for all frames except SYNC_REQ and TEST_SEQ. Only one handler per node — PanelGroup and PanelBridge each register their own.
Parameters:
cbCallback invoked with canId, data pointer, and payload length.
function onStatusChange
Register a CAN bus status change callback.
void CANProtocol::onStatusChange (
CanStatusCallback cb
)
Fires immediately with the current status upon registration (STARTING before start() is called), then on each subsequent status transition.
Parameters:
cbCallback to invoke with the new CanStatus.
function onSyncReq
Register a SYNC_REQ handler.
void CANProtocol::onSyncReq (
CanSyncReqCallback cb
)
Fired by drain() when a SYNC_REQ (0x012) for this node arrives — one naming its NODE_ID, or an untargeted one. PanelGroup registers this to re-send all its inputs. Once the node's transmit queue has emptied afterwards, drain() sends a READY flagged ReadyFlag::RESYNC: the end of the node's answer, which the bridge waits for before syncing the next node (#383, #387).
Parameters:
cbCallback to invoke on SYNC_REQ receipt.
function rec
Return the CAN Receive Error Counter (0-255) from ESR.
uint8_t CANProtocol::rec ()
function rxDropCount
Return the cumulative RX drop count since startup (#387): frames lost because the software RX ring was full when they arrived. Should stay 0; nonzero means loop() falls behind the bus.
uint32_t CANProtocol::rxDropCount ()
function rxOverrunCount
Return the cumulative RX FIFO overrun count since startup (#389): frames lost in hardware because all three FIFO slots were full when they arrived. RX is interrupt-driven, so this needs interrupts held off for three frame times; should stay 0.
uint32_t CANProtocol::rxOverrunCount ()
function send
Send a CAN frame.
void CANProtocol::send (
uint32_t canId,
const uint8_t * data,
uint8_t len
)
Every frame goes through the TX ring (16 entries; 64 on the bridge) and leaves in the order it was sent; the TX-complete interrupt keeps the three mailboxes loaded. A frame that loses arbitration is resent until it goes out; one that hits a bus error, up to 3 attempts in all (#389, D4/D7). At most one frame per CAN ID is in the mailboxes at a time, so a resend never lands after a newer frame of the same ID. A full ring, or a frame out of error attempts, is dropped and counted in txDropCount() — on the board; not reported over the bus.
Parameters:
canId11-bit standard CAN ID.dataPointer to payload bytes.lenPayload length in bytes (0-8).
function sendBatched
Submit one ControlPacket to a CANProtocol-owned ControlPacketPair batch.
void CANProtocol::sendBatched (
uint32_t canId,
const ControlPacket & pkt
)
Valid only for CAN_ID_CTRL_BCAST and the batched event frames — canIdEvt(n) (absolute), canIdEvtRel(n) (RotaryEncoder REL), canIdEvtDir(n) (RotaryEncoder DIR), and canIdEvtAction(n) (ActionButton) — each with its own pending slot. Pairs two consecutive packets into one 8-byte frame. If slot B does not arrive within two drain() calls (and, on PanelBridge, 2 ms — #389), slot A is sent with slot B set to the null sentinel (controlId == 0x0000).
A frame ID with no batch slot is silently discarded — adding a batched frame means registering it in _batches[] in begin(), not just defining its canId helper.
Parameters:
canIdCAN_ID_CTRL_BCAST, canIdEvt(NODE_ID), canIdEvtRel(NODE_ID), canIdEvtDir(NODE_ID), or canIdEvtAction(NODE_ID).pktControlPacket to batch.
function sendReady
Send this node's boot READY_n ( canIdReady(NODE_ID) ) carryingmakeReadyPayload() .
void CANProtocol::sendReady ()
Call once at the end of the node's setup(), after its boot EVT burst. PanelBridge marks the node alive, caches its version and queues the node for a sync. Bounded retry like every other special frame (02-can-protocol.md). From then on drain() ends every answer to a SYNC_REQ with a READY flagged ReadyFlag::RESYNC (#383, #387) — nothing for the caller to do.
function sendSyncReq
Ask one node to re-send its state: SYNC_REQ naming nodeId (#387).PanelBridge only.
void CANProtocol::sendSyncReq (
uint8_t nodeId
)
Parameters:
nodeId1-63.
function serviceNodeStatus
Run the node-generic status duties. Call once per loop(), after drain() .
void CANProtocol::serviceNodeStatus (
NodeKind kind,
uint16_t rxCount=0
)
Every node on the bus owes the same status traffic, whatever else it does (#340):
* HB_NODE_ID every 500 ms (makeHeartbeatPayload), carrying rxCount.
* HEALTH_NODE_ID every 1000 ms (makeNodeHealthPayload) with kind, the internal die temp and the node fault, plus the DiagSerial fault edge log (tag BRIDGE on NODE_ID 0, NODE otherwise). Compiled out with -DNODE_HEALTH_TELEM=0.
* Duplicate NODE_ID check (#344), on every node: a node never receives its own frames, so a frame on its own HB_NODE_ID (start() adds it to the filter) means another board has the same NODE_ID. The node fault is then NodeFaultCode::DUPLICATE_NODE_ID, ahead of every FaultSource (the NODE_ID is wrong, so nothing else this ID reports can be trusted); it clears DUP_NODE_TIMEOUT_MS after the last foreign frame. aggregateFaults() supplies the fault otherwise.
* Master-loss watchdog, on every node except PanelBridge (NODE_ID 0): once a master heartbeat (HB_0) has been seen, master loss is set after MASTER_TIMEOUT_MS of silence and cleared by the next HB_0.
* WARNING on a non-bridge node = master loss OR overheat (overheat only when a build defines NODE_OVERHEAT_C). The service tracks the two causes separately and writes the shared latch only when their OR changes, so a returning master never clears a live overheat, and overheat clears once the die cools. On PanelBridge an overheat is raise-only, because its latch also carries PanelBridge's dead-node WARNING.
The first call arms the HB/HEALTH timers — the first loop() after setup(), i.e. boot complete — so the first HB goes out ~500 ms and the first HEALTH ~1000 ms after it.
HB_0 arrival is recorded by drain(), which still passes the frame to onReceive(). start() adds HB_0 to the hardware filter on non-bridge nodes, so callers never register it by hand.
Parameters:
kindThis node's board kind (#344), sent in every HEALTH_n. Each node library passes its own (PanelGroup → PANEL_GROUP, PduNode → PDU, PanelBridge → BRIDGE), so a sketch never sets it. Required: a role can't forget it.rxCountCaller-owned accepted RX count for the heartbeat (PanelGroup: accepted CTRL_BCAST frames). Pass 0 when the node keeps no count.
Note:
Samples millis() itself, after drain() has stamped any HB_0, and compares elapsed time signed — so a millisecond ticking during drain() cannot read as a timeout (#225).
function start
Start the CAN peripheral and apply registered filters.
void CANProtocol::start ()
Must be called after STM32Board::begin() and after all filter and callback registrations. Always adds CTRL_BCAST, TEST_SEQ, and SYNC_REQ to the active filter — mandatory for all nodes, cannot be excluded — plus HB_0 on every node except PanelBridge (NODE_ID 0), for serviceNodeStatus()'s master-loss watchdog, and the node's own HB_NODE_ID, for its duplicate NODE_ID check (#344).
Resets the serviceNodeStatus() timers, which arm on its first call. Fires the onStatusChange callback with NORMAL on success.
function startLoopback
Start in silent loopback mode — for bench testing only.
void CANProtocol::startLoopback ()
Identical to start() but uses CAN_MODE_SILENT_LOOPBACK: frames transmitted by this node are received back internally without going on the physical bus. The duplicate NODE_ID check discounts the node's own looped-back heartbeats, so only an extra HB_NODE_ID a test sends itself reads as a duplicate.
Note:
Never call this in production firmware.
function tec
Return the CAN Transmit Error Counter (0-255) from ESR.
uint8_t CANProtocol::tec ()
function txDropCount
Return the cumulative TX queue drop count since startup.
uint32_t CANProtocol::txDropCount ()
The documentation for this class was generated from the following file Firmware/Libraries/CANProtocol/CANProtocol.cpp