Crate device_register
source · [−]Expand description
A zero cost toolkit to describe the register of your devies to ease driver development with no_std support.
no_stdsupport- Zero cost, no use of dyn
- No dsl, just a derive macro and impl a trait.
- Error passthrough
Usage
Simply derive using XXRegister, where XX is the premission.
The following permissions are supported
RORegister, read only permissionWORegister, write only permissionEORegister, edit only permission, when a register need to be read-modify-writeRERegister, Read and edit permissionRWRegister, Read, write and edit permission.
To define a register, simply derive using the desired permission.
Then use the register attribute to define it’s address, type for the address and the error type.
#[derive(RWRegister)]
#[register( addr = "42", ty = "u8", err = "DeviceError" )]
pub struct Register0(pub u16);The your driver only need to implement the RegisterInterface
Completed example
Here is a complete example. See the tests folder for more, or checkout the tmp117 driver for actual usage.
use std::collections::HashMap;
use device_register::*;
// The type of the address used by the driver
struct Address(pub u8);
// The type of the error
type DeviceError = ();
// We define the register with Read/Write permission
// Then we pass the address type, value and error type of the driveer
#[derive(Debug, Copy, PartialEq, Eq, Clone, RWRegister)]
#[register( addr = "Address(1)", ty = "Address", err = "DeviceError" )]
struct Register0(pub u16);
// Mock of the device driver
struct DeviceDriver {
// Simulate reading from the device
pub registers: HashMap<u8, u16>,
}
// We implement the required interface
impl<R> RegisterInterface<R, Address, DeviceError> for DeviceDriver
where
R: Register<Address = Address, Error = DeviceError> + Clone + From<u16>,
u16: From<R>,
{
fn read_register(&mut self) -> Result<R, DeviceError> {
let bytes = self.registers.get(&R::ADDRESS.0).unwrap();
Ok(bytes.clone().into())
}
fn write_register(&mut self, register: &R) -> Result<(), DeviceError> {
self.registers.insert(R::ADDRESS.0, register.clone().into());
Ok(())
}
}
let mut device = DeviceDriver{
registers: HashMap::new(),
};
// We can the Read/Write/Edit the registers that uses the Address and DeviceError types.
let write = Register0(42);
device.write(write).unwrap();
let read: Register0 = device.read().unwrap();
assert_eq!(read, write);
device.edit(|r: &mut Register0| {
r.0 = 43;
r
}).unwrap();
let read: Register0 = device.read().unwrap();
assert_eq!(read, Register0(43));
License
Licensed under either of
-
Apache License, Version 2.0 (LICENSE-APACHE or http://www.apache.org/licenses/LICENSE-2.0)
-
MIT license (LICENSE-MIT or http://opensource.org/licenses/MIT)
at your option.
Contribution
Unless you explicitly state otherwise, any contribution intentionally submitted for inclusion in the work by you, as defined in the Apache-2.0 license, shall be dual licensed as above, without any additional terms or conditions.
Traits
Trait to safely read-edit-write a register. Usefull when a register has reserved values for internal uses. Avoids writing garbage to the reserved bits.
Trait of a register that can only be edited. Some registers require a read-edit-write operation since some bits a reserved internally Editing a register allows to “safely” modify only a subset of values
Trait to safely read a register. Only a readable register can be read.
Trait of a read only register
Trait of a register containing an address
Traits that define how to read and write the registers. Note that those functions should mostly just be implemented and not used since they are not bound by Read/Write/Edit permission.
Trait a writable register, like a register but can be written to
Trait to safely write a register. Only a writable register can be written to.
Derive Macros
Create an edit only register
Create a read/edit register
Create a read only register
Create a read/write register
Create a read/write register