5 releases
new 0.2.1 | Mar 28, 2025 |
---|---|
0.2.0 | Mar 28, 2025 |
0.1.2 | Oct 8, 2024 |
0.1.1 | Oct 8, 2024 |
0.1.0 | Oct 8, 2024 |
#1855 in Embedded development
106 downloads per month
18KB
316 lines
Variegated Board Cfg
Substantial credits go to James Munns for the toml-cfg crate, Adam Greig for the assign-resources crate, and Adin Ackerman for the procedural overhaul PR for assign-resources.
The idea of this crate is to be able to store pin and peripheral configration in a config file, and then use that configuration to split the Peripherals struct into smaller, more purpose-built structs.
Usage
Config file path
This crate expects a board-cfg.toml
to be located in your project root. If you want to place it somewhere else, you can by setting the BOARD_CFG_PATH
environment variable.
Splitting peripherals
The main features here are the ability to split a peripherals struct, set type aliases, and enforce correct types from the configuration.
lib.rs
#[variegated_board_cfg::board_cfg("hid_bus")]
struct HidBus {
tx_pin: (),
rx_pin: impl embassy_rp::peripherals::Pin, // Forces a compile error if the type of rx_pin doesn't implement Pin
uart: (),
baud_rate: u32
}
board-cfg.toml
[hid_bus]
tx_pin = "embassy_rp::peripherals::PIN_0"
rx_pin = "embassy_rp::peripherals::PIN_1"
uart = "embassy_rp::peripherals::UART0"
baud_rate = 115200
Expansion
type HidBusTxPin = embassy_rp::peripherals::PIN_0;
type HidBusRxPin = embassy_rp::peripherals::PIN_1;
type HidBusUart = embassy_rp::peripherals::UART0;
struct HidBus {
tx_pin: HidBusTxPin,
rx_pin: HidBusRxPin,
uart: HidBusUart,
baud_rate: u32,
}
impl HidBus where HidBusRxPin: embassy_rp::peripherals::Pin {
}
macro_rules! hid_bus {
($P : ident) => {
HidBus {
tx_pin: $P.PIN_0,
rx_pin: $P.PIN_1,
uart: $P.UART0,
baud_rate: 115200
}
};
}
Binding interrupts
board-cfg.toml
[irq_aliases]
Nau7802Irq = "I2C0_IRQ"
DispIrq = "I2C1_IRQ"
lib.rs
variegated_board_cfg::aliased_bind_interrupts!(struct Irqs {
USBCTRL_IRQ => usb::InterruptHandler<USB>;
Nau7802Irq => i2c::InterruptHandler<Nau7802ConfigI2CInstance>;
DispIrq => i2c::InterruptHandler<Sh1107I2cDisplayConfigI2CInstance>;
});
Expansion
bind_interrupts!(struct Irqs {
USBCTRL_IRQ => usb::InterruptHandler<USB>;
I2C0_IRQ => i2c::InterruptHandler<Nau7802ConfigI2CInstance>;
I2C1_IRQ => i2c::InterruptHandler<Sh1107I2cDisplayConfigI2CInstance>;
});
Dependencies
~0.6–1.2MB
~27K SLoC