Merge pull request #1606 from JcBernack/rng-update

STM32: RNG update
This commit is contained in:
Dario Nieuwenhuis 2023-07-30 23:44:11 +00:00 committed by GitHub
commit 6c6bd11c1a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
16 changed files with 192 additions and 111 deletions

View File

@ -57,7 +57,7 @@ sdio-host = "0.5.0"
embedded-sdmmc = { git = "https://github.com/embassy-rs/embedded-sdmmc-rs", rev = "a4f293d3a6f72158385f79c98634cb8a14d0d2fc", optional = true } embedded-sdmmc = { git = "https://github.com/embassy-rs/embedded-sdmmc-rs", rev = "a4f293d3a6f72158385f79c98634cb8a14d0d2fc", optional = true }
critical-section = "1.1" critical-section = "1.1"
atomic-polyfill = "1.0.1" atomic-polyfill = "1.0.1"
stm32-metapac = "13" stm32-metapac = { git = "https://github.com/embassy-rs/stm32-data-generated", tag = "stm32-data-1f8ab493e029fc601edebc6bac105a63cc9858fe" }
vcell = "0.1.3" vcell = "0.1.3"
bxcan = "0.7.0" bxcan = "0.7.0"
nb = "1.0.0" nb = "1.0.0"
@ -75,7 +75,7 @@ critical-section = { version = "1.1", features = ["std"] }
[build-dependencies] [build-dependencies]
proc-macro2 = "1.0.36" proc-macro2 = "1.0.36"
quote = "1.0.15" quote = "1.0.15"
stm32-metapac = { version = "13", default-features = false, features = ["metadata"]} stm32-metapac = { git = "https://github.com/embassy-rs/stm32-data-generated", tag = "stm32-data-1f8ab493e029fc601edebc6bac105a63cc9858fe", default-features = false, features = ["metadata"]}
[features] [features]
default = ["rt"] default = ["rt"]

View File

@ -1,15 +1,17 @@
#![macro_use] #![macro_use]
use core::future::poll_fn; use core::future::poll_fn;
use core::marker::PhantomData;
use core::task::Poll; use core::task::Poll;
use embassy_hal_internal::{into_ref, PeripheralRef}; use embassy_hal_internal::{into_ref, PeripheralRef};
use embassy_sync::waitqueue::AtomicWaker; use embassy_sync::waitqueue::AtomicWaker;
use rand_core::{CryptoRng, RngCore}; use rand_core::{CryptoRng, RngCore};
use crate::{pac, peripherals, Peripheral}; use crate::interrupt::typelevel::Interrupt;
use crate::{interrupt, pac, peripherals, Peripheral};
pub(crate) static RNG_WAKER: AtomicWaker = AtomicWaker::new(); static RNG_WAKER: AtomicWaker = AtomicWaker::new();
#[cfg_attr(feature = "defmt", derive(defmt::Format))] #[cfg_attr(feature = "defmt", derive(defmt::Format))]
pub enum Error { pub enum Error {
@ -17,68 +19,145 @@ pub enum Error {
ClockError, ClockError,
} }
pub struct InterruptHandler<T: Instance> {
_phantom: PhantomData<T>,
}
impl<T: Instance> interrupt::typelevel::Handler<T::Interrupt> for InterruptHandler<T> {
unsafe fn on_interrupt() {
let bits = T::regs().sr().read();
if bits.drdy() || bits.seis() || bits.ceis() {
T::regs().cr().modify(|reg| reg.set_ie(false));
RNG_WAKER.wake();
}
}
}
pub struct Rng<'d, T: Instance> { pub struct Rng<'d, T: Instance> {
_inner: PeripheralRef<'d, T>, _inner: PeripheralRef<'d, T>,
} }
impl<'d, T: Instance> Rng<'d, T> { impl<'d, T: Instance> Rng<'d, T> {
pub fn new(inner: impl Peripheral<P = T> + 'd) -> Self { pub fn new(
inner: impl Peripheral<P = T> + 'd,
_irq: impl interrupt::typelevel::Binding<T::Interrupt, InterruptHandler<T>> + 'd,
) -> Self {
T::enable(); T::enable();
T::reset(); T::reset();
into_ref!(inner); into_ref!(inner);
let mut random = Self { _inner: inner }; let mut random = Self { _inner: inner };
random.reset(); random.reset();
T::Interrupt::unpend();
unsafe { T::Interrupt::enable() };
random random
} }
#[cfg(rng_v1)]
pub fn reset(&mut self) { pub fn reset(&mut self) {
// rng_v2 locks up on seed error, needs reset T::regs().cr().write(|reg| {
#[cfg(rng_v2)] reg.set_rngen(false);
if T::regs().sr().read().seis() {
T::reset();
}
T::regs().cr().modify(|reg| {
reg.set_rngen(true);
reg.set_ie(true);
}); });
T::regs().sr().modify(|reg| { T::regs().sr().modify(|reg| {
reg.set_seis(false); reg.set_seis(false);
reg.set_ceis(false); reg.set_ceis(false);
}); });
T::regs().cr().modify(|reg| {
reg.set_rngen(true);
});
// Reference manual says to discard the first. // Reference manual says to discard the first.
let _ = self.next_u32(); let _ = self.next_u32();
} }
pub async fn async_fill_bytes(&mut self, dest: &mut [u8]) -> Result<(), Error> { #[cfg(not(rng_v1))]
pub fn reset(&mut self) {
T::regs().cr().write(|reg| {
reg.set_rngen(false);
reg.set_condrst(true);
// set RNG config "A" according to reference manual
// this has to be written within the same write access as setting the CONDRST bit
reg.set_nistc(pac::rng::vals::Nistc::DEFAULT);
reg.set_rng_config1(pac::rng::vals::RngConfig1::CONFIGA);
reg.set_rng_config2(pac::rng::vals::RngConfig2::CONFIGA_B);
reg.set_rng_config3(pac::rng::vals::RngConfig3::CONFIGA);
reg.set_clkdiv(pac::rng::vals::Clkdiv::NODIV);
});
// wait for CONDRST to be set
while !T::regs().cr().read().condrst() {}
// magic number must be written immediately before every read or write access to HTCR
T::regs().htcr().write(|w| w.set_htcfg(pac::rng::vals::Htcfg::MAGIC));
// write recommended value according to reference manual
// note: HTCR can only be written during conditioning
T::regs()
.htcr()
.write(|w| w.set_htcfg(pac::rng::vals::Htcfg::RECOMMENDED));
// finish conditioning
T::regs().cr().modify(|reg| { T::regs().cr().modify(|reg| {
reg.set_rngen(true); reg.set_rngen(true);
reg.set_condrst(false);
}); });
// wait for CONDRST to be reset
while T::regs().cr().read().condrst() {}
}
pub fn recover_seed_error(&mut self) -> () {
self.reset();
// reset should also clear the SEIS flag
if T::regs().sr().read().seis() {
warn!("recovering from seed error failed");
return;
}
// wait for SECS to be cleared by RNG
while T::regs().sr().read().secs() {}
}
pub async fn async_fill_bytes(&mut self, dest: &mut [u8]) -> Result<(), Error> {
for chunk in dest.chunks_mut(4) { for chunk in dest.chunks_mut(4) {
poll_fn(|cx| { let bits = T::regs().sr().read();
RNG_WAKER.register(cx.waker()); if bits.seis() {
T::regs().cr().modify(|reg| { // in case of noise-source or seed error we try to recover here
reg.set_ie(true); // but we must not use the data in DR and we return an error
}); // to leave retry-logic to the application
self.recover_seed_error();
let bits = T::regs().sr().read(); return Err(Error::SeedError);
} else if bits.ceis() {
if bits.drdy() { // clock error detected, DR could still be used but keep it safe,
Poll::Ready(Ok(())) // clear the error and abort
} else if bits.seis() { T::regs().sr().modify(|sr| sr.set_ceis(false));
self.reset(); return Err(Error::ClockError);
Poll::Ready(Err(Error::SeedError)) } else if bits.drdy() {
} else if bits.ceis() { // DR can be read up to four times until the output buffer is empty
self.reset(); // DRDY is cleared automatically when that happens
Poll::Ready(Err(Error::ClockError)) let random_word = T::regs().dr().read();
} else { // reference manual: always check if DR is zero
Poll::Pending if random_word == 0 {
return Err(Error::SeedError);
} }
}) // write bytes to chunk
.await?; for (dest, src) in chunk.iter_mut().zip(random_word.to_be_bytes().iter()) {
let random_bytes = T::regs().dr().read().to_be_bytes(); *dest = *src
for (dest, src) in chunk.iter_mut().zip(random_bytes.iter()) { }
*dest = *src } else {
// wait for interrupt
poll_fn(|cx| {
// quick check to avoid registration if already done.
let bits = T::regs().sr().read();
if bits.drdy() || bits.seis() || bits.ceis() {
return Poll::Ready(());
}
RNG_WAKER.register(cx.waker());
T::regs().cr().modify(|reg| reg.set_ie(true));
// Need to check condition **after** `register` to avoid a race
// condition that would result in lost notifications.
let bits = T::regs().sr().read();
if bits.drdy() || bits.seis() || bits.ceis() {
Poll::Ready(())
} else {
Poll::Pending
}
})
.await;
} }
} }
@ -129,57 +208,20 @@ pub(crate) mod sealed {
} }
} }
pub trait Instance: sealed::Instance + crate::rcc::RccPeripheral {} pub trait Instance: sealed::Instance + Peripheral<P = Self> + crate::rcc::RccPeripheral + 'static + Send {
type Interrupt: interrupt::typelevel::Interrupt;
}
foreach_peripheral!( foreach_interrupt!(
(rng, $inst:ident) => { ($inst:ident, rng, RNG, GLOBAL, $irq:ident) => {
impl Instance for peripherals::$inst {} impl Instance for peripherals::$inst {
type Interrupt = crate::interrupt::typelevel::$irq;
}
impl sealed::Instance for peripherals::$inst { impl sealed::Instance for peripherals::$inst {
fn regs() -> crate::pac::rng::Rng { fn regs() -> crate::pac::rng::Rng {
crate::pac::RNG crate::pac::$inst
} }
} }
}; };
); );
#[cfg(feature = "rt")]
macro_rules! irq {
($irq:ident) => {
mod rng_irq {
use crate::interrupt;
#[interrupt]
unsafe fn $irq() {
let bits = $crate::pac::RNG.sr().read();
if bits.drdy() || bits.seis() || bits.ceis() {
$crate::pac::RNG.cr().write(|reg| reg.set_ie(false));
$crate::rng::RNG_WAKER.wake();
}
}
}
};
}
#[cfg(feature = "rt")]
foreach_interrupt!(
(RNG) => {
irq!(RNG);
};
(RNG_LPUART1) => {
irq!(RNG_LPUART1);
};
(AES_RNG_LPUART1) => {
irq!(AES_RNG_LPUART1);
};
(AES_RNG) => {
irq!(AES_RNG);
};
(HASH_RNG) => {
irq!(HASH_RNG);
};
);

View File

@ -11,13 +11,15 @@ use embassy_stm32::eth::{Ethernet, PacketQueue};
use embassy_stm32::peripherals::ETH; use embassy_stm32::peripherals::ETH;
use embassy_stm32::rng::Rng; use embassy_stm32::rng::Rng;
use embassy_stm32::time::mhz; use embassy_stm32::time::mhz;
use embassy_stm32::{bind_interrupts, eth, Config}; use embassy_stm32::{bind_interrupts, eth, peripherals, rng, Config};
use embassy_time::{Duration, Timer}; use embassy_time::{Duration, Timer};
use embedded_io::asynch::Write; use embedded_io::asynch::Write;
use static_cell::make_static; use static_cell::make_static;
use {defmt_rtt as _, panic_probe as _}; use {defmt_rtt as _, panic_probe as _};
bind_interrupts!(struct Irqs { bind_interrupts!(struct Irqs {
ETH => eth::InterruptHandler; ETH => eth::InterruptHandler;
HASH_RNG => rng::InterruptHandler<peripherals::RNG>;
}); });
type Device = Ethernet<'static, ETH, GenericSMI>; type Device = Ethernet<'static, ETH, GenericSMI>;
@ -36,7 +38,7 @@ async fn main(spawner: Spawner) -> ! {
info!("Hello World!"); info!("Hello World!");
// Generate random seed. // Generate random seed.
let mut rng = Rng::new(p.RNG); let mut rng = Rng::new(p.RNG, Irqs);
let mut seed = [0; 8]; let mut seed = [0; 8];
let _ = rng.async_fill_bytes(&mut seed).await; let _ = rng.async_fill_bytes(&mut seed).await;
let seed = u64::from_le_bytes(seed); let seed = u64::from_le_bytes(seed);

View File

@ -6,7 +6,7 @@ use defmt::*;
use embassy_executor::Spawner; use embassy_executor::Spawner;
use embassy_net::tcp::TcpSocket; use embassy_net::tcp::TcpSocket;
use embassy_net::{Stack, StackResources}; use embassy_net::{Stack, StackResources};
use embassy_stm32::rng::Rng; use embassy_stm32::rng::{self, Rng};
use embassy_stm32::time::mhz; use embassy_stm32::time::mhz;
use embassy_stm32::usb_otg::Driver; use embassy_stm32::usb_otg::Driver;
use embassy_stm32::{bind_interrupts, peripherals, usb_otg, Config}; use embassy_stm32::{bind_interrupts, peripherals, usb_otg, Config};
@ -38,6 +38,7 @@ async fn net_task(stack: &'static Stack<Device<'static, MTU>>) -> ! {
bind_interrupts!(struct Irqs { bind_interrupts!(struct Irqs {
OTG_FS => usb_otg::InterruptHandler<peripherals::USB_OTG_FS>; OTG_FS => usb_otg::InterruptHandler<peripherals::USB_OTG_FS>;
HASH_RNG => rng::InterruptHandler<peripherals::RNG>;
}); });
#[embassy_executor::main] #[embassy_executor::main]
@ -104,7 +105,7 @@ async fn main(spawner: Spawner) {
//}); //});
// Generate random seed // Generate random seed
let mut rng = Rng::new(p.RNG); let mut rng = Rng::new(p.RNG, Irqs);
let mut seed = [0; 8]; let mut seed = [0; 8];
unwrap!(rng.async_fill_bytes(&mut seed).await); unwrap!(rng.async_fill_bytes(&mut seed).await);
let seed = u64::from_le_bytes(seed); let seed = u64::from_le_bytes(seed);

View File

@ -11,14 +11,16 @@ use embassy_stm32::eth::{Ethernet, PacketQueue};
use embassy_stm32::peripherals::ETH; use embassy_stm32::peripherals::ETH;
use embassy_stm32::rng::Rng; use embassy_stm32::rng::Rng;
use embassy_stm32::time::mhz; use embassy_stm32::time::mhz;
use embassy_stm32::{bind_interrupts, eth, Config}; use embassy_stm32::{bind_interrupts, eth, peripherals, rng, Config};
use embassy_time::{Duration, Timer}; use embassy_time::{Duration, Timer};
use embedded_io::asynch::Write; use embedded_io::asynch::Write;
use rand_core::RngCore; use rand_core::RngCore;
use static_cell::make_static; use static_cell::make_static;
use {defmt_rtt as _, panic_probe as _}; use {defmt_rtt as _, panic_probe as _};
bind_interrupts!(struct Irqs { bind_interrupts!(struct Irqs {
ETH => eth::InterruptHandler; ETH => eth::InterruptHandler;
RNG => rng::InterruptHandler<peripherals::RNG>;
}); });
type Device = Ethernet<'static, ETH, GenericSMI>; type Device = Ethernet<'static, ETH, GenericSMI>;
@ -37,7 +39,7 @@ async fn main(spawner: Spawner) -> ! {
info!("Hello World!"); info!("Hello World!");
// Generate random seed. // Generate random seed.
let mut rng = Rng::new(p.RNG); let mut rng = Rng::new(p.RNG, Irqs);
let mut seed = [0; 8]; let mut seed = [0; 8];
rng.fill_bytes(&mut seed); rng.fill_bytes(&mut seed);
let seed = u64::from_le_bytes(seed); let seed = u64::from_le_bytes(seed);

View File

@ -12,14 +12,16 @@ use embassy_stm32::peripherals::ETH;
use embassy_stm32::rcc::{AHBPrescaler, APBPrescaler, Hse, HseMode, Pll, PllSource, Sysclk, VoltageScale}; use embassy_stm32::rcc::{AHBPrescaler, APBPrescaler, Hse, HseMode, Pll, PllSource, Sysclk, VoltageScale};
use embassy_stm32::rng::Rng; use embassy_stm32::rng::Rng;
use embassy_stm32::time::Hertz; use embassy_stm32::time::Hertz;
use embassy_stm32::{bind_interrupts, eth, Config}; use embassy_stm32::{bind_interrupts, eth, peripherals, rng, Config};
use embassy_time::{Duration, Timer}; use embassy_time::{Duration, Timer};
use embedded_io::asynch::Write; use embedded_io::asynch::Write;
use rand_core::RngCore; use rand_core::RngCore;
use static_cell::make_static; use static_cell::make_static;
use {defmt_rtt as _, panic_probe as _}; use {defmt_rtt as _, panic_probe as _};
bind_interrupts!(struct Irqs { bind_interrupts!(struct Irqs {
ETH => eth::InterruptHandler; ETH => eth::InterruptHandler;
RNG => rng::InterruptHandler<peripherals::RNG>;
}); });
type Device = Ethernet<'static, ETH, GenericSMI>; type Device = Ethernet<'static, ETH, GenericSMI>;
@ -56,7 +58,7 @@ async fn main(spawner: Spawner) -> ! {
info!("Hello World!"); info!("Hello World!");
// Generate random seed. // Generate random seed.
let mut rng = Rng::new(p.RNG); let mut rng = Rng::new(p.RNG, Irqs);
let mut seed = [0; 8]; let mut seed = [0; 8];
rng.fill_bytes(&mut seed); rng.fill_bytes(&mut seed);
let seed = u64::from_le_bytes(seed); let seed = u64::from_le_bytes(seed);

View File

@ -5,14 +5,19 @@
use defmt::*; use defmt::*;
use embassy_executor::Spawner; use embassy_executor::Spawner;
use embassy_stm32::rng::Rng; use embassy_stm32::rng::Rng;
use embassy_stm32::{bind_interrupts, peripherals, rng};
use {defmt_rtt as _, panic_probe as _}; use {defmt_rtt as _, panic_probe as _};
bind_interrupts!(struct Irqs {
RNG => rng::InterruptHandler<peripherals::RNG>;
});
#[embassy_executor::main] #[embassy_executor::main]
async fn main(_spawner: Spawner) { async fn main(_spawner: Spawner) {
let p = embassy_stm32::init(Default::default()); let p = embassy_stm32::init(Default::default());
info!("Hello World!"); info!("Hello World!");
let mut rng = Rng::new(p.RNG); let mut rng = Rng::new(p.RNG, Irqs);
let mut buf = [0u8; 16]; let mut buf = [0u8; 16];
unwrap!(rng.async_fill_bytes(&mut buf).await); unwrap!(rng.async_fill_bytes(&mut buf).await);

View File

@ -11,14 +11,16 @@ use embassy_stm32::eth::{Ethernet, PacketQueue};
use embassy_stm32::peripherals::ETH; use embassy_stm32::peripherals::ETH;
use embassy_stm32::rng::Rng; use embassy_stm32::rng::Rng;
use embassy_stm32::time::mhz; use embassy_stm32::time::mhz;
use embassy_stm32::{bind_interrupts, eth, Config}; use embassy_stm32::{bind_interrupts, eth, peripherals, rng, Config};
use embassy_time::{Duration, Timer}; use embassy_time::{Duration, Timer};
use embedded_io::asynch::Write; use embedded_io::asynch::Write;
use rand_core::RngCore; use rand_core::RngCore;
use static_cell::make_static; use static_cell::make_static;
use {defmt_rtt as _, panic_probe as _}; use {defmt_rtt as _, panic_probe as _};
bind_interrupts!(struct Irqs { bind_interrupts!(struct Irqs {
ETH => eth::InterruptHandler; ETH => eth::InterruptHandler;
RNG => rng::InterruptHandler<peripherals::RNG>;
}); });
type Device = Ethernet<'static, ETH, GenericSMI>; type Device = Ethernet<'static, ETH, GenericSMI>;
@ -38,7 +40,7 @@ async fn main(spawner: Spawner) -> ! {
info!("Hello World!"); info!("Hello World!");
// Generate random seed. // Generate random seed.
let mut rng = Rng::new(p.RNG); let mut rng = Rng::new(p.RNG, Irqs);
let mut seed = [0; 8]; let mut seed = [0; 8];
rng.fill_bytes(&mut seed); rng.fill_bytes(&mut seed);
let seed = u64::from_le_bytes(seed); let seed = u64::from_le_bytes(seed);

View File

@ -11,15 +11,17 @@ use embassy_stm32::eth::{Ethernet, PacketQueue};
use embassy_stm32::peripherals::ETH; use embassy_stm32::peripherals::ETH;
use embassy_stm32::rng::Rng; use embassy_stm32::rng::Rng;
use embassy_stm32::time::mhz; use embassy_stm32::time::mhz;
use embassy_stm32::{bind_interrupts, eth, Config}; use embassy_stm32::{bind_interrupts, eth, peripherals, rng, Config};
use embassy_time::{Duration, Timer}; use embassy_time::{Duration, Timer};
use embedded_io::asynch::Write; use embedded_io::asynch::Write;
use embedded_nal_async::{Ipv4Addr, SocketAddr, SocketAddrV4, TcpConnect}; use embedded_nal_async::{Ipv4Addr, SocketAddr, SocketAddrV4, TcpConnect};
use rand_core::RngCore; use rand_core::RngCore;
use static_cell::make_static; use static_cell::make_static;
use {defmt_rtt as _, panic_probe as _}; use {defmt_rtt as _, panic_probe as _};
bind_interrupts!(struct Irqs { bind_interrupts!(struct Irqs {
ETH => eth::InterruptHandler; ETH => eth::InterruptHandler;
RNG => rng::InterruptHandler<peripherals::RNG>;
}); });
type Device = Ethernet<'static, ETH, GenericSMI>; type Device = Ethernet<'static, ETH, GenericSMI>;
@ -39,7 +41,7 @@ async fn main(spawner: Spawner) -> ! {
info!("Hello World!"); info!("Hello World!");
// Generate random seed. // Generate random seed.
let mut rng = Rng::new(p.RNG); let mut rng = Rng::new(p.RNG, Irqs);
let mut seed = [0; 8]; let mut seed = [0; 8];
rng.fill_bytes(&mut seed); rng.fill_bytes(&mut seed);
let seed = u64::from_le_bytes(seed); let seed = u64::from_le_bytes(seed);

View File

@ -5,14 +5,19 @@
use defmt::*; use defmt::*;
use embassy_executor::Spawner; use embassy_executor::Spawner;
use embassy_stm32::rng::Rng; use embassy_stm32::rng::Rng;
use embassy_stm32::{bind_interrupts, peripherals, rng};
use {defmt_rtt as _, panic_probe as _}; use {defmt_rtt as _, panic_probe as _};
bind_interrupts!(struct Irqs {
RNG => rng::InterruptHandler<peripherals::RNG>;
});
#[embassy_executor::main] #[embassy_executor::main]
async fn main(_spawner: Spawner) { async fn main(_spawner: Spawner) {
let p = embassy_stm32::init(Default::default()); let p = embassy_stm32::init(Default::default());
info!("Hello World!"); info!("Hello World!");
let mut rng = Rng::new(p.RNG); let mut rng = Rng::new(p.RNG, Irqs);
let mut buf = [0u8; 16]; let mut buf = [0u8; 16];
unwrap!(rng.async_fill_bytes(&mut buf).await); unwrap!(rng.async_fill_bytes(&mut buf).await);

View File

@ -12,8 +12,8 @@ use embassy_lora::LoraTimer;
use embassy_stm32::exti::{Channel, ExtiInput}; use embassy_stm32::exti::{Channel, ExtiInput};
use embassy_stm32::gpio::{Input, Level, Output, Pin, Pull, Speed}; use embassy_stm32::gpio::{Input, Level, Output, Pin, Pull, Speed};
use embassy_stm32::rng::Rng; use embassy_stm32::rng::Rng;
use embassy_stm32::spi;
use embassy_stm32::time::khz; use embassy_stm32::time::khz;
use embassy_stm32::{bind_interrupts, peripherals, rng, spi};
use embassy_time::Delay; use embassy_time::Delay;
use lora_phy::mod_params::*; use lora_phy::mod_params::*;
use lora_phy::sx1276_7_8_9::SX1276_7_8_9; use lora_phy::sx1276_7_8_9::SX1276_7_8_9;
@ -23,6 +23,10 @@ use lorawan_device::async_device::lora_radio::LoRaRadio;
use lorawan_device::async_device::{region, Device, JoinMode}; use lorawan_device::async_device::{region, Device, JoinMode};
use {defmt_rtt as _, panic_probe as _}; use {defmt_rtt as _, panic_probe as _};
bind_interrupts!(struct Irqs {
RNG_LPUART1 => rng::InterruptHandler<peripherals::RNG>;
});
const LORAWAN_REGION: region::Region = region::Region::EU868; // warning: set this appropriately for the region const LORAWAN_REGION: region::Region = region::Region::EU868; // warning: set this appropriately for the region
#[embassy_executor::main] #[embassy_executor::main]
@ -60,7 +64,7 @@ async fn main(_spawner: Spawner) {
let radio = LoRaRadio::new(lora); let radio = LoRaRadio::new(lora);
let region: region::Configuration = region::Configuration::new(LORAWAN_REGION); let region: region::Configuration = region::Configuration::new(LORAWAN_REGION);
let mut device: Device<_, Crypto, _, _> = Device::new(region, radio, LoraTimer::new(), Rng::new(p.RNG)); let mut device: Device<_, Crypto, _, _> = Device::new(region, radio, LoraTimer::new(), Rng::new(p.RNG, Irqs));
defmt::info!("Joining LoRaWAN network"); defmt::info!("Joining LoRaWAN network");

View File

@ -6,9 +6,13 @@ use defmt::*;
use embassy_executor::Spawner; use embassy_executor::Spawner;
use embassy_stm32::rcc::{ClockSrc, PLLClkDiv, PLLMul, PLLSource, PLLSrcDiv}; use embassy_stm32::rcc::{ClockSrc, PLLClkDiv, PLLMul, PLLSource, PLLSrcDiv};
use embassy_stm32::rng::Rng; use embassy_stm32::rng::Rng;
use embassy_stm32::Config; use embassy_stm32::{bind_interrupts, peripherals, rng, Config};
use {defmt_rtt as _, panic_probe as _}; use {defmt_rtt as _, panic_probe as _};
bind_interrupts!(struct Irqs {
RNG => rng::InterruptHandler<peripherals::RNG>;
});
#[embassy_executor::main] #[embassy_executor::main]
async fn main(_spawner: Spawner) { async fn main(_spawner: Spawner) {
let mut config = Config::default(); let mut config = Config::default();
@ -24,7 +28,7 @@ async fn main(_spawner: Spawner) {
info!("Hello World!"); info!("Hello World!");
let mut rng = Rng::new(p.RNG); let mut rng = Rng::new(p.RNG, Irqs);
let mut buf = [0u8; 16]; let mut buf = [0u8; 16];
unwrap!(rng.async_fill_bytes(&mut buf).await); unwrap!(rng.async_fill_bytes(&mut buf).await);

View File

@ -6,9 +6,13 @@ use defmt::*;
use embassy_executor::Spawner; use embassy_executor::Spawner;
use embassy_stm32::rcc::{ClockSrc, PLLClkDiv, PLLMul, PLLSource, PLLSrcDiv}; use embassy_stm32::rcc::{ClockSrc, PLLClkDiv, PLLMul, PLLSource, PLLSrcDiv};
use embassy_stm32::rng::Rng; use embassy_stm32::rng::Rng;
use embassy_stm32::Config; use embassy_stm32::{bind_interrupts, peripherals, rng, Config};
use {defmt_rtt as _, panic_probe as _}; use {defmt_rtt as _, panic_probe as _};
bind_interrupts!(struct Irqs {
RNG => rng::InterruptHandler<peripherals::RNG>;
});
#[embassy_executor::main] #[embassy_executor::main]
async fn main(_spawner: Spawner) { async fn main(_spawner: Spawner) {
let mut config = Config::default(); let mut config = Config::default();
@ -23,7 +27,7 @@ async fn main(_spawner: Spawner) {
info!("Hello World!"); info!("Hello World!");
let mut rng = Rng::new(p.RNG); let mut rng = Rng::new(p.RNG, Irqs);
let mut buf = [0u8; 16]; let mut buf = [0u8; 16];
unwrap!(rng.async_fill_bytes(&mut buf).await); unwrap!(rng.async_fill_bytes(&mut buf).await);

View File

@ -9,7 +9,7 @@ use embassy_net::{Stack, StackResources};
use embassy_stm32::rcc::*; use embassy_stm32::rcc::*;
use embassy_stm32::rng::Rng; use embassy_stm32::rng::Rng;
use embassy_stm32::usb::Driver; use embassy_stm32::usb::Driver;
use embassy_stm32::{bind_interrupts, peripherals, usb, Config}; use embassy_stm32::{bind_interrupts, peripherals, rng, usb, Config};
use embassy_usb::class::cdc_ncm::embassy_net::{Device, Runner, State as NetState}; use embassy_usb::class::cdc_ncm::embassy_net::{Device, Runner, State as NetState};
use embassy_usb::class::cdc_ncm::{CdcNcmClass, State}; use embassy_usb::class::cdc_ncm::{CdcNcmClass, State};
use embassy_usb::{Builder, UsbDevice}; use embassy_usb::{Builder, UsbDevice};
@ -24,6 +24,7 @@ const MTU: usize = 1514;
bind_interrupts!(struct Irqs { bind_interrupts!(struct Irqs {
USB_FS => usb::InterruptHandler<peripherals::USB>; USB_FS => usb::InterruptHandler<peripherals::USB>;
RNG => rng::InterruptHandler<peripherals::RNG>;
}); });
#[embassy_executor::task] #[embassy_executor::task]
@ -99,7 +100,7 @@ async fn main(spawner: Spawner) {
//}); //});
// Generate random seed // Generate random seed
let mut rng = Rng::new(p.RNG); let mut rng = Rng::new(p.RNG, Irqs);
let seed = rng.next_u64(); let seed = rng.next_u64();
// Init network stack // Init network stack

View File

@ -10,9 +10,9 @@ use embassy_executor::Spawner;
use embassy_lora::iv::{InterruptHandler, Stm32wlInterfaceVariant}; use embassy_lora::iv::{InterruptHandler, Stm32wlInterfaceVariant};
use embassy_lora::LoraTimer; use embassy_lora::LoraTimer;
use embassy_stm32::gpio::{Level, Output, Pin, Speed}; use embassy_stm32::gpio::{Level, Output, Pin, Speed};
use embassy_stm32::rng::Rng; use embassy_stm32::rng::{self, Rng};
use embassy_stm32::spi::Spi; use embassy_stm32::spi::Spi;
use embassy_stm32::{bind_interrupts, pac}; use embassy_stm32::{bind_interrupts, pac, peripherals};
use embassy_time::Delay; use embassy_time::Delay;
use lora_phy::mod_params::*; use lora_phy::mod_params::*;
use lora_phy::sx1261_2::SX1261_2; use lora_phy::sx1261_2::SX1261_2;
@ -26,6 +26,7 @@ const LORAWAN_REGION: region::Region = region::Region::EU868; // warning: set th
bind_interrupts!(struct Irqs{ bind_interrupts!(struct Irqs{
SUBGHZ_RADIO => InterruptHandler; SUBGHZ_RADIO => InterruptHandler;
RNG => rng::InterruptHandler<peripherals::RNG>;
}); });
#[embassy_executor::main] #[embassy_executor::main]
@ -58,7 +59,7 @@ async fn main(_spawner: Spawner) {
}; };
let radio = LoRaRadio::new(lora); let radio = LoRaRadio::new(lora);
let region: region::Configuration = region::Configuration::new(LORAWAN_REGION); let region: region::Configuration = region::Configuration::new(LORAWAN_REGION);
let mut device: Device<_, Crypto, _, _> = Device::new(region, radio, LoraTimer::new(), Rng::new(p.RNG)); let mut device: Device<_, Crypto, _, _> = Device::new(region, radio, LoraTimer::new(), Rng::new(p.RNG, Irqs));
defmt::info!("Joining LoRaWAN network"); defmt::info!("Joining LoRaWAN network");

View File

@ -4,10 +4,14 @@
use defmt::*; use defmt::*;
use embassy_executor::Spawner; use embassy_executor::Spawner;
use embassy_stm32::pac; use embassy_stm32::rng::{self, Rng};
use embassy_stm32::rng::Rng; use embassy_stm32::{bind_interrupts, pac, peripherals};
use {defmt_rtt as _, panic_probe as _}; use {defmt_rtt as _, panic_probe as _};
bind_interrupts!(struct Irqs{
RNG => rng::InterruptHandler<peripherals::RNG>;
});
#[embassy_executor::main] #[embassy_executor::main]
async fn main(_spawner: Spawner) { async fn main(_spawner: Spawner) {
let mut config = embassy_stm32::Config::default(); let mut config = embassy_stm32::Config::default();
@ -21,7 +25,7 @@ async fn main(_spawner: Spawner) {
info!("Hello World!"); info!("Hello World!");
let mut rng = Rng::new(p.RNG); let mut rng = Rng::new(p.RNG, Irqs);
let mut buf = [0u8; 16]; let mut buf = [0u8; 16];
unwrap!(rng.async_fill_bytes(&mut buf).await); unwrap!(rng.async_fill_bytes(&mut buf).await);