variegated-board-cfg

Crates.iovariegated-board-cfg
lib.rsvariegated-board-cfg
version0.2.1
created_at2024-10-08 20:37:25.275184+00
updated_at2025-03-28 16:39:09.580237+00
descriptionStore pin and other HAL configurations for your embedded project in a TOML file.
homepage
repositoryhttps://github.com/variegated-coffee/variegated-board-cfg
max_upload_size
id1401662
size22,612
Magnus Nordlander (magnusnordlander)

documentation

README

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>;
});
Commit count: 7

cargo fmt