Expand description
Tools for generating a zencan node
§Device Config File
A “device config” is a TOML file, which defines the behavior of a node. It has some general
configuration options, like how many RPDOs/TPDOs the device should support, and it creates the
set of application specific objects that will be accessible in the node’s object dictionary. It
is the input used by zencan-build to generate code for the node.
The file is read using DeviceConfig.
§Generating code using build.rs
The expected way to use this crate is in your project’s build.rs file. The
build_node_from_device_config() function can be used there to generate the code from a
device config file, and store it under a provided label. Then, in your code, the
include_modules! macro from the zencan-node create can be used to include the code wherever
you want.
§Example
In build.rs:
if let Err(e) =
zencan_build::build_node_from_device_config("EXAMPLE", "example_device_config.toml")
{
eprintln!("Error building node from example_device_config.toml: {}", e);
std::process::exit(1);
}Then, in main.rs:
mod zencan {
zencan_node::include_modules!(EXAMPLE);
}§The generated code
The generated code looks something like this:
pub static OBJECT1000: Object1000 = Object1000::default();
pub static OBJECT1001: Object1001 = Object1001::default();
pub static OBJECT1008: Object1008 = Object1008::default();
pub static NODE_STATE: NodeState<4usize, 4usize> = NodeState::new();
pub static NODE_MBOX: NodeMbox = NodeMbox::new(NODE_STATE.rpdos());
pub static OD_TABLE: [ODEntry; 31usize] = [
ODEntry {
index: 0x1000,
data: ObjectData::Storage(&OBJECT1000),
},
ODEntry {
index: 0x1001,
data: ObjectData::Storage(&OBJECT1001),
},
ODEntry {
index: 0x1008,
data: ObjectData::Storage(&OBJECT1008),
},
];For each object defined in the object dictionary, a type is created – e.g. Object1000 for
object 0x1000 – as well as an instance. All objects are put into a ’static table, called
OD_TABLE. Additionally, a NODE_STATE and a NODE_MBOX are created, and these must be provided
when instantiating node.
Modules§
- errors
- Error types for crate
Functions§
- build_
node_ from_ device_ config - Generate a node for inclusion via
include_modules!macro - compile_
device_ config - Compile a device config TOML file into rust code
- device_
config_ to_ string - Generate code for a node from a
DeviceConfigas a string - device_
config_ to_ tokens - Generate code for a node from a
DeviceConfigas a TokenStream