pub struct Node<const N: usize> { /* private fields */ }Expand description
A CANopen device node: an object dictionary served over SDO, with NMT state, heartbeat/boot-up production, and PDO exchange.
Implementations§
Source§impl<const N: usize> Node<N>
impl<const N: usize> Node<N>
Sourcepub fn new(node_id: NodeId, od: ObjectDictionary<N>) -> Self
pub fn new(node_id: NodeId, od: ObjectDictionary<N>) -> Self
Create a node with node_id serving od. It starts in
NmtState::Initialising; call Node::boot to go operational-ready.
Sourcepub fn enable_lss(&mut self, address: LssAddress)
pub fn enable_lss(&mut self, address: LssAddress)
Enable LSS with this node’s 128-bit identity (LssAddress, object
0x1018). The node then answers LSS master requests on 0x7E5, letting
a master (re)assign its node-id over the bus.
A node awaiting an LSS-assigned id should be left in
NmtState::Initialising (do not call Node::boot) so it serves only
LSS; after the id is assigned, call Node::apply_lss_node_id then boot.
Sourcepub fn enable_lss_unconfigured(&mut self, address: LssAddress)
pub fn enable_lss_unconfigured(&mut self, address: LssAddress)
Enable LSS on a node that comes up unconfigured — it has no node-id
yet, so it answers only LSS (including FastscanMaster discovery,
when the master doesn’t know its address) and serves no SDO, NMT, or PDO
traffic until a master assigns an id. Construct the node with any
placeholder id, call this, and leave it in NmtState::Initialising
(do not boot); once the id is assigned over LSS, call
Node::apply_lss_node_id on the node’s reset, then boot.
Sourcepub fn set_node_id(&mut self, node_id: NodeId)
pub fn set_node_id(&mut self, node_id: NodeId)
Change the node-id, rebuilding the SDO server for the new COB-IDs. Call on the reset that follows an LSS reconfiguration.
Sourcepub fn apply_lss_node_id(&mut self) -> Option<NodeId>
pub fn apply_lss_node_id(&mut self) -> Option<NodeId>
Adopt a node-id assigned over LSS: if the LSS slave holds a valid pending id, apply it (rebuilding the SDO server) and return it. Call this on the node’s reset after an LSS configuration.
Sourcepub fn lss(&self) -> Option<&LssSlave>
pub fn lss(&self) -> Option<&LssSlave>
The LSS slave, if LSS is enabled (e.g. to read its pending node-id).
Sourcepub fn add_rpdo(
&mut self,
cob_id: u16,
mapping: PdoMapping<MAX_PDO_MAPPING>,
) -> Result<()>
pub fn add_rpdo( &mut self, cob_id: u16, mapping: PdoMapping<MAX_PDO_MAPPING>, ) -> Result<()>
Configure a receive PDO: when a frame arrives on cob_id (while
operational), its bytes are unpacked into the mapped objects.
Returns Error::MappingFull once MAX_PDOS receive PDOs are set.
Sourcepub fn add_tpdo(
&mut self,
cob_id: u16,
mapping: PdoMapping<MAX_PDO_MAPPING>,
transmission: TransmissionType,
) -> Result<()>
pub fn add_tpdo( &mut self, cob_id: u16, mapping: PdoMapping<MAX_PDO_MAPPING>, transmission: TransmissionType, ) -> Result<()>
Configure a transmit PDO: Node::sync_tpdos packs and emits it on SYNC
(for synchronous types) and Node::tpdo emits it on demand.
Returns Error::MappingFull once MAX_PDOS transmit PDOs are set.
Sourcepub fn configure_pdos_from_od(&mut self)
pub fn configure_pdos_from_od(&mut self)
(Re)build the PDO configuration from the PDO parameter objects in the
object dictionary — communication parameters at 0x1400+ (RPDO) /
0x1800+ (TPDO) and mapping parameters at 0x1600+ / 0x1A00+ (CiA 301
§7.5.2). Call this after a master configures PDOs by writing those
objects over SDO, so the node picks up the new layout.
Any programmatic PDO configuration is replaced. PDOs whose communication COB-ID has the “invalid” bit set are skipped, as are entries that are absent or malformed.
Sourcepub fn take_written_object(&mut self) -> Option<Address>
pub fn take_written_object(&mut self) -> Option<Address>
Take the address of the object a master most recently wrote over SDO,
clearing it. Poll after Node::on_frame to react to configuration
writes — for example, re-read the PDO layout when a PDO parameter object
(0x1400–0x1BFF) changes:
node.on_frame(cob_id, &data);
if let Some(addr) = node.take_written_object() {
if (0x1400..=0x1BFF).contains(&addr.index) {
node.configure_pdos_from_od();
}
}Sourcepub fn od(&self) -> &ObjectDictionary<N>
pub fn od(&self) -> &ObjectDictionary<N>
Borrow the object dictionary (e.g. to publish process data).
Sourcepub fn od_mut(&mut self) -> &mut ObjectDictionary<N>
pub fn od_mut(&mut self) -> &mut ObjectDictionary<N>
Mutably borrow the object dictionary.
Sourcepub fn boot(&mut self) -> TxFrame
pub fn boot(&mut self) -> TxFrame
Finish initialisation: enter pre-operational and return the boot-up
frame to transmit (0x700 + node, data 0x00).
Sourcepub fn node_guard_response(&mut self) -> TxFrame
pub fn node_guard_response(&mut self) -> TxFrame
The node-guarding response frame (0x700 + node), reporting the current
state with an alternating toggle bit. Call this when the master polls the
node with an RTR frame on that COB-ID (the legacy error-control
alternative to Node::heartbeat).
Sourcepub fn heartbeat(&self) -> TxFrame
pub fn heartbeat(&self) -> TxFrame
The heartbeat frame for the current state. Transmit it on your heartbeat
timer (the producer heartbeat time lives in object 0x1017).
Sourcepub fn error_register(&self) -> ErrorRegister
pub fn error_register(&self) -> ErrorRegister
The current error register (object 0x1001): the bitfield of active
error classes.
Sourcepub fn error_history(&self) -> &[u32]
pub fn error_history(&self) -> &[u32]
The pre-defined error field (object 0x1003): recorded emergency codes,
most-recent-first (up to MAX_ERROR_HISTORY). The low 16 bits of each
entry are the emergency error code.
Sourcepub fn raise_emergency(
&mut self,
code: u16,
register: ErrorRegister,
info: [u8; 5],
) -> TxFrame
pub fn raise_emergency( &mut self, code: u16, register: ErrorRegister, info: [u8; 5], ) -> TxFrame
Raise an emergency: set register (and the generic bit) in the error
register, record code at the front of the pre-defined error field,
mirror both into the object dictionary (0x1001 / 0x1003) when those
objects exist, and return the EMCY frame to transmit on 0x080 + node.
info carries five manufacturer-specific bytes in the frame. The
returned frame must still be transmitted by the caller (sans-I/O).
Sourcepub fn clear_errors(&mut self) -> TxFrame
pub fn clear_errors(&mut self) -> TxFrame
Clear all errors: reset the error register and pre-defined error field,
mirror the cleared state into the object dictionary, and return an
error-reset EMCY frame (code 0x0000) to transmit.
Sourcepub fn enable_sync_producer(&mut self, counter_overflow: u8) -> Result<()>
pub fn enable_sync_producer(&mut self, counter_overflow: u8) -> Result<()>
Configure this node as the network SYNC producer, with the given
synchronous-counter-overflow value (object 0x1019): 0 produces
counter-less (empty) SYNC frames, and 2..=240 a counting SYNC.
Returns Error::InvalidSyncCounter for the reserved value 1 or any
value above 240.
Sourcepub fn produce_sync(&mut self) -> Option<TxFrame>
pub fn produce_sync(&mut self) -> Option<TxFrame>
Produce the next SYNC frame to broadcast on SYNC_COB_ID,
advancing the counter when one is configured. Returns None unless the
node is a SYNC producer (see enable_sync_producer).
Call this on your SYNC-period timer. SYNC is a broadcast network service, so it is emitted independently of this node’s own NMT state; gate the call on the network state yourself if you only want SYNC while operational.
Sourcepub fn on_frame(&mut self, cob_id: u16, data: &[u8]) -> Option<TxFrame>
pub fn on_frame(&mut self, cob_id: u16, data: &[u8]) -> Option<TxFrame>
Process an incoming CAN frame, returning a response to transmit, if any.
Handles NMT node-control (0x000), SDO requests (0x600 + node), LSS
master requests (0x7E5, when enabled), and received PDOs. SDO is served
only in pre-operational and operational states, and PDOs only in
operational, per CiA 301; LSS is served regardless of NMT state. Frames
for other COB-IDs are ignored.
While the node is LSS-unconfigured (see Node::enable_lss_unconfigured)
it serves only LSS — everything else is ignored until a node-id is
assigned — since its data COB-IDs are not yet meaningful.
Sourcepub fn sync_tpdos(&self) -> Vec<TxFrame, MAX_PDOS>
pub fn sync_tpdos(&self) -> Vec<TxFrame, MAX_PDOS>
The synchronous transmit PDOs to send in response to a SYNC.
Packs every configured TPDO with a synchronous transmission type from the current object dictionary. Empty unless the node is operational — PDOs are exchanged only in that state (CiA 301 §7.3.5).