7 releases
0.9.6 | Dec 13, 2023 |
---|---|
0.9.5 | Dec 13, 2023 |
0.9.3 | Nov 7, 2023 |
#35 in #bitfield
23 downloads per month
Used in embedded-registers
19KB
195 lines
Embedded Registers
WARNING: This crate is currently in experimental state, so anything may change at any time.
This crate provides a procedural macro for effortless definitions of registers in embedded device drivers. This is automatically generates functions to read/write the register over I2C and SPI, although it isn't limited to those buses. The resulting struct may trivially be extended to work with any other similar communication bus.
- Allows defintion of read-only, write-only and read-write registers
- Generates I2C and SPI read/write functions
- Registers are defined as bitfields via bondrewd.
- Only the accessed bitfield members are decoded, conserving memory and saving on CPU time.
- Supports both async and blocking operation modes
This crate was designed for the embedded-devices crate, which aims to provide modern async-capable and coherent definitions for many embedded devices.
Usage
Add this to your Cargo.toml
:
[dependencies]
# You will need this to define your own bitfield-capable enums
bondrewd = { version = "0.1.14", default_features = false, features = ["derive"] }
embedded-registers = "0.9.6"
For a simple register definition example, take a look at this DeviceId
register
from the MCP9808 temperature sensor:
#![feature(generic_arg_infer)]
use embedded_registers::register;
#[register(address = 0b111, read)]
#[bondrewd(read_from = "msb0", default_endianness = "be", enforce_bytes = 2)]
pub struct DeviceId {
device_id: u8,
revision: u8,
}
You may then read the register simply by calling DeviceId::read_i2c
or DeviceId::read_i2c_blocking
(or similarly write to it if you specified write
in the definition above):
let reg = DeviceId::read_i2c(&mut i2c, 0x24 /* i2c device address */).await?;
info!("{}", reg);
// Prints: DeviceId ([4, 0]) => DeviceIdBitfield { device_id: 4, revision: 0 }
For more information and more complex examples, please refer to the embededded-registers docs and the bondrewd docs.
License
Licensed under either of
- Apache License, Version 2.0, (LICENSE-APACHE or https://www.apache.org/licenses/LICENSE-2.0)
- MIT license (LICENSE-MIT or https://opensource.org/licenses/MIT)
at your option. Unless you explicitly state otherwise, any contribution intentionally submitted for inclusion in this crate by you, as defined in the Apache-2.0 license, shall be dual licensed as above, without any additional terms or conditions.
Dependencies
~0.8–1.4MB
~30K SLoC