Skip to content

Instantly share code, notes, and snippets.

@peter9477
Last active February 23, 2023 22:54
Show Gist options
  • Save peter9477/1c8e6496a99df173ed8aaee6aad9e9a9 to your computer and use it in GitHub Desktop.
Save peter9477/1c8e6496a99df173ed8aaee6aad9e9a9 to your computer and use it in GitHub Desktop.
Selected lines from my project, demonstrating how to split up Embassy Peripherals into board-specific chunks
//------------------------------
// board.rs
use embassy_nrf::{
interrupt,
peripherals::*,
Peripherals as EmbassyPeripherals,
};
pub type AccelTwim = TWISPI1;
pub type AccelIntPin = P1_00;
pub struct Accel {
pub irq: interrupt::SPIM1_SPIS1_TWIM1_TWIS1_SPI1_TWI1,
pub twi: AccelTwim,
pub sda: P0_22,
pub scl: P0_24,
pub int_pin: AccelIntPin, // active high (by default) interrupt signal
}
pub type AdcDevice = SAADC;
pub struct Adc {
pub irq: interrupt::SAADC,
pub battest: P0_06, // must be high to measure VBATT else keep low
pub vbatt: P0_04, // ADC chan for battery measurement
pub device: AdcDevice,
}
pub struct Battery {
pub chg: P0_12, // signals end of charge when on USB power
}
pub struct Flash {
pub irq: interrupt::QSPI,
pub qspi: QSPI,
pub csn: P1_01, // chip select (active low)
pub sck: P1_05, // clock, SCK0
pub io0: P1_06,
pub io1: P1_02,
pub io2: P1_04,
pub io3: P1_03,
}
pub struct Leds {
pub red: P0_29, // tri-colour red
pub green: P0_30, // tri-colour green
pub blue: P0_28, // tri-colour blue
}
pub struct TestPoints {
pub tp1: P0_15,
pub tp2: P0_17,
pub tp4: P0_08,
pub tp35: P0_02,
}
pub type DevUart = UARTE0;
pub struct Uart {
pub irq: interrupt::UARTE0_UART0,
pub uart: DevUart,
pub txd: P1_14, // UART transmit pin
pub rxd: P1_13, // UART receive pin
}
pub type UsbDevice = USBD;
pub struct Usb {
pub irq: interrupt::USBD,
pub usbd: UsbDevice,
}
// This collects them all together.
// Note that this is still a zero-sized type.
//
pub struct Peripherals {
pub accel: Accel,
pub adc: Adc,
pub battery: Battery,
pub flash: Flash,
pub leds: Leds,
pub testpoints: TestPoints,
pub uart: Uart,
pub usb: Usb,
}
impl Peripherals {
pub fn split(p: EmbassyPeripherals) -> Self {
#[allow(dead_code)]
{
// Check that this is zero-sized... compiler error if it's not.
const CHECK_SELF: [u8; 0] = [0; core::mem::size_of::<Peripherals>()];
}
Self {
accel: Accel {
irq: interrupt::take!(SPIM1_SPIS1_TWIM1_TWIS1_SPI1_TWI1),
twi: p.TWISPI1,
sda: p.P0_22,
scl: p.P0_24,
int_pin: p.P1_00,
},
adc: Adc {
device: p.SAADC,
irq: interrupt::take!(SAADC),
battest: p.P0_06,
vbatt: p.P0_04,
},
battery: Battery {
chg: p.P0_12,
},
flash: Flash {
irq: interrupt::take!(QSPI),
qspi: p.QSPI,
csn: p.P1_01,
sck: p.P1_05,
io0: p.P1_06,
io1: p.P1_02,
io2: p.P1_04,
io3: p.P1_03,
},
leds: Leds {
red: p.P0_29,
green: p.P0_30,
blue: p.P0_28,
},
testpoints: TestPoints {
tp1: p.P0_15,
tp2: p.P0_17,
tp4: p.P0_08,
tp35: p.P0_02,
},
uart: Uart {
irq: interrupt::take!(UARTE0_UART0),
uart: p.UARTE0,
txd: p.P1_14,
rxd: p.P1_13,
},
usb: Usb {
irq: interrupt::take!(USBD),
usbd: p.USBD,
},
}
}
}
//------------------------------
// main.rs
#[embassy_executor::main]
async fn main(spawner: Spawner) {
...
// initialize embassy
let mut ecfg: config::Config = Default::default();
ecfg.time_interrupt_priority = interrupt::Priority::P7;
ecfg.gpiote_interrupt_priority = interrupt::Priority::P7;
let p = embassy_nrf::init(ecfg);
let board = board::Peripherals::split(p);
flash::init(&spawner, board.flash);
let led_chan = leds::init(&spawner, board.leds);
battery::init(&spawner, board.battery);
let adc = adc::Adc::new(&spawner, board.adc);
let accel_chan = accel::init(&spawner, board.accel);
#[cfg(feature = "usb")]
unwrap!(spawner.spawn(usb::usb_task(sup, board.usb)));
... etc etc
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment