Skip to main content

canopen_rs/
lib.rs

1//! # canopen-rs
2//!
3//! A `no_std`-first [CANopen] (CiA 301) protocol stack in Rust.
4//!
5//! This core crate is transport-agnostic and allocation-free: it models the
6//! object dictionary, encodes/decodes SDO and PDO messages, and drives the
7//! NMT state machine. It is designed to run unchanged on a bare-metal MCU
8//! node and on a host (Linux/SocketCAN) via the companion `canopen-host`
9//! crate.
10//!
11//! CAN frames are represented through the [`embedded_can`] traits, so any
12//! controller or socket that implements them can carry CANopen traffic.
13//!
14//! # Example: an SDO read, transport-free
15//!
16//! A device exposes an [`ObjectDictionary`]; a [client](sdo::SdoClient) reads it
17//! from a [server](sdo::SdoServer) one frame at a time — no bus required, so the
18//! same logic runs on a host or an MCU.
19//!
20//! ```
21//! use canopen_rs::sdo::{SdoClient, SdoEvent, SdoServer};
22//! use canopen_rs::{Address, DataType, Entry, NodeId, ObjectDictionary, Value};
23//!
24//! let node = NodeId::new(0x10).unwrap();
25//!
26//! // The device's object dictionary, served by an SDO server.
27//! let mut od = ObjectDictionary::<8>::new();
28//! od.insert(Address::new(0x1000, 0), Entry::constant(Value::Unsigned32(0x1234))).unwrap();
29//! let mut server = SdoServer::new(node);
30//!
31//! // The client drives the transaction; expedited vs segmented is automatic.
32//! let mut client = SdoClient::new(node);
33//! let mut frame = client.read(Address::new(0x1000, 0), DataType::Unsigned32);
34//! let value = loop {
35//!     let response = server.handle(&mut od, &frame).expect("server replies");
36//!     match client.on_response(&response) {
37//!         SdoEvent::Send(next) => frame = next,
38//!         SdoEvent::Complete(v) => break v.unwrap(),
39//!         SdoEvent::Aborted(code) => panic!("aborted {code:#010x}"),
40//!     }
41//! };
42//! assert_eq!(value, Value::Unsigned32(0x1234));
43//! ```
44//!
45//! ## Status
46//!
47//! Early development. The API will change. See the roadmap in the workspace
48//! `README.md`.
49//!
50//! [CANopen]: https://www.can-cia.org/canopen/
51#![no_std]
52#![deny(unsafe_code)]
53#![warn(missing_debug_implementations)]
54// Enable once the public API stabilises:
55// #![warn(missing_docs)]
56
57pub mod datatypes;
58pub mod emcy;
59pub mod lss;
60pub mod nmt;
61pub mod node;
62pub mod object_dictionary;
63pub mod pdo;
64pub mod sdo;
65pub mod sync;
66pub mod time;
67pub mod transport;
68pub mod types;
69
70pub use datatypes::{DataType, Value};
71pub use emcy::{EmergencyMessage, ErrorRegister};
72pub use lss::{LssAddress, LssSlave, LssState};
73pub use nmt::{NmtCommand, NmtState, NmtStateMachine};
74pub use node::{Node, TxFrame};
75pub use object_dictionary::{AccessType, Address, Entry, ObjectDictionary};
76pub use pdo::{MappingEntry, PdoKind, PdoMapping, TransmissionType};
77pub use sdo::{SdoClient, SdoEvent, SdoServer, Segment, SegmentReader, SegmentWriter};
78pub use sync::SyncCounter;
79pub use time::TimeOfDay;
80pub use types::{Error, NodeId, Result};