Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add RNG demo and dummy async driver #38

Merged
merged 3 commits into from
Oct 2, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@ This crate is a working-in-progress and not ready for production use.
- [x] Device
- [ ] Host
- [x] XPI NOR flash driver using embedded-storage
- [x] RNG, in blocking mode
- [ ] power domain handling

### Related Crates
Expand Down
36 changes: 21 additions & 15 deletions examples/hpm5300evk/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,25 @@ version = "0.1.0"
edition = "2021"

[dependencies]
# hpm5301 is a subset of hpm5361, either is ok if you have the 5301 board.
# hpm5301 is a subset of hpm5361, either is ok if you have the HPM5301 board.
hpm-hal = { path = "../..", features = [
"rt",
"embassy",
"hpm5361",
"usb-pin-reuse-hpm5300",
] }

defmt = "0.3.8"
defmt-rtt = "0.4.1"
panic-halt = "0.2.0"
riscv-rt = "0.12.2"

riscv = { version = "0.11.1", features = ["critical-section-single-hart"] }

embedded-graphics = "0.8.1"
embedded-hal = "1.0.0"
embedded-hal-async = "1.0.0"
embedded-io = "0.6.1"
embedded-hal-bus = "0.2.0"

embassy-time = { version = "0.3.0", features = ["tick-hz-1_000_000"] }
embassy-executor = { version = "0.6.0", features = [
Expand All @@ -22,27 +31,24 @@ embassy-executor = { version = "0.6.0", features = [
"arch-riscv32",
"executor-thread",
] }
defmt = "0.3.8"
defmt-rtt = "0.4.1"
embedded-graphics = "0.8.1"
riscv = { version = "0.11.1", features = ["critical-section-single-hart"] }
embedded-hal = "1.0.0"
embedded-io = "0.6.1"
futures-util = { version = "0.3.30", default-features = false }
tinygif = "0.0.4"
heapless = "0.8.0"
micromath = "2.1.0"
embedded-hal-bus = "0.2.0"
assign-resources = "0.4.1"
mcan = "0.5.0"
embassy-embedded-hal = "0.2.0"
embassy-sync = "0.6.0"

embassy-usb = { version = "0.3.0", features = [
"defmt",
"max-handler-count-8",
"max-interface-count-8",
] }
usbd-hid = "0.8"
mcan = "0.5.0"

assign-resources = "0.4.1"
futures-util = { version = "0.3.30", default-features = false }
heapless = "0.8.0"
micromath = "2.1.0"
static_cell = "2"
rand_core = "0.6.4"


[profile.release]
strip = false # symbols are not flashed to the microcontroller, so don't strip them.
Expand Down
12 changes: 9 additions & 3 deletions examples/hpm5300evk/src/bin/adc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -58,11 +58,17 @@ async fn main(spawner: Spawner) -> ! {
loop {
Timer::after_millis(200).await;

// defmt::info!("tick {}", MCHTMR.mtime().read());
// let n = adc.blocking_read(&mut adc_ch7_pin, Default::default());
let mut sum = 0;
for _ in 0..1000 {
let n = adc.blocking_read(&mut adc_ch7_pin, Default::default());
sum += n as u32;
}
let n = sum / 1000;

let n = adc.blocking_read(&mut adc_ch7_pin, Default::default());
let voltage = (n as u32) * 3300 / 65536;

println!("ADC0_CH7: {}", n);
println!("ADC0_CH7: {} {}mV", n, voltage);
}
}

Expand Down
2 changes: 1 addition & 1 deletion examples/hpm5300evk/src/bin/blinky.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
use embedded_hal::delay::DelayNs;
use hal::gpio::{Level, Output, Speed};
use riscv::delay::McycleDelay;
use {defmt_rtt as _, hpm_hal as hal, riscv_rt as _};
use {defmt_rtt as _, hpm_hal as hal};

#[hal::entry]
fn main() -> ! {
Expand Down
2 changes: 1 addition & 1 deletion examples/hpm5300evk/src/bin/blocking_uart_tx.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use embedded_io::Write as _; // `writeln!` provider
use hpm_hal::gpio::{Level, Output, Speed};
use hpm_hal::uart::UartTx;
use riscv::delay::McycleDelay;
use {defmt_rtt as _, hpm_hal as hal, panic_halt as _, riscv_rt as _};
use {defmt_rtt as _, hpm_hal as hal, panic_halt as _};

const BANNER: &str = include_str!("../../../assets/BANNER");

Expand Down
4 changes: 2 additions & 2 deletions examples/hpm5300evk/src/bin/button.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@
use embedded_hal::delay::DelayNs;
use hpm_hal::gpio::{Input, Pull};
use riscv::delay::McycleDelay;
use {defmt_rtt as _, hpm_hal as hal, panic_halt as _, riscv_rt as _};
use {defmt_rtt as _, hpm_hal as hal, panic_halt as _};

#[riscv_rt::entry]
#[hal::entry]
fn main() -> ! {
let p = hal::init(Default::default());

Expand Down
2 changes: 1 addition & 1 deletion examples/hpm5300evk/src/bin/i2c_oled.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ use hal::gpio::{Level, Output, Speed};
use hal::i2c::I2c;
use hal::mode::Blocking;
use riscv::delay::McycleDelay;
use {defmt_rtt as _, hpm_hal as hal, riscv_rt as _};
use {defmt_rtt as _, hpm_hal as hal};

const BANNER: &str = include_str!("../../../assets/BANNER");

Expand Down
2 changes: 1 addition & 1 deletion examples/hpm5300evk/src/bin/pll_setting.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ use hal::pac;
use hal::uart::UartTx;
use hpm_hal::time::Hertz;
use riscv::delay::McycleDelay;
use {defmt_rtt as _, hpm_hal as hal, panic_halt as _, riscv_rt as _};
use {defmt_rtt as _, hpm_hal as hal, panic_halt as _};

const BANNER: &str = include_str!("../../../assets/BANNER");

Expand Down
2 changes: 1 addition & 1 deletion examples/hpm5300evk/src/bin/qspi.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ use hpm_hal::spi::{
};
use hpm_hal::time::Hertz;
use riscv::delay::McycleDelay;
use {defmt_rtt as _, hpm_hal as hal, riscv_rt as _};
use {defmt_rtt as _, hpm_hal as hal};

#[derive(Copy, Clone, Eq, PartialEq, Ord, PartialOrd, Hash, Debug)]
pub enum Orientation {
Expand Down
40 changes: 40 additions & 0 deletions examples/hpm5300evk/src/bin/rng.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
#![no_main]
#![no_std]
#![feature(type_alias_impl_trait)]
#![feature(impl_trait_in_assoc_type)]
#![feature(abi_riscv_interrupt)]

use embassy_time::Timer;
use rand_core::RngCore;
use {defmt_rtt as _, hpm_hal as hal};

#[embassy_executor::main(entry = "hpm_hal::entry")]
async fn main(_spawner: embassy_executor::Spawner) -> ! {
let p = hal::init(Default::default());

let mut rng = hal::rng::Rng::new(p.RNG).unwrap();
let mut buf = [0u8; 20];

defmt::println!("Async mode");

for _ in 0..5 {
rng.async_fill_bytes(&mut buf).await.unwrap();

defmt::println!("out: {:?}", buf);
}

Timer::after_millis(1000).await;

defmt::println!("Blocking mode(Notice about 0.3s delay when new seed is not ready");

loop {
rng.fill_bytes(&mut buf);

defmt::println!("out: {:?}", buf);
}
}

#[panic_handler]
fn panic(info: &core::panic::PanicInfo) -> ! {
defmt::panic!("{}", defmt::Debug2Format(info));
}
2 changes: 1 addition & 1 deletion examples/hpm5300evk/src/bin/spi.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ use hpm_hal::mode::Blocking;
use hpm_hal::spi::{Config, Spi};
use hpm_hal::time::Hertz;
use riscv::delay::McycleDelay;
use {defmt_rtt as _, hpm_hal as hal, panic_halt as _, riscv_rt as _};
use {defmt_rtt as _, hpm_hal as hal, panic_halt as _};

pub struct ST7789<
Spi: SpiDevice,
Expand Down
2 changes: 1 addition & 1 deletion examples/hpm5300evk/src/bin/tsns.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ use hal::gpio::{Level, Output, Speed};
use hal::pac;
use hpm_hal::time::Hertz;
use riscv::delay::McycleDelay;
use {defmt_rtt as _, hpm_hal as hal, riscv_rt as _};
use {defmt_rtt as _, hpm_hal as hal};

const BANNER: &str = include_str!("../../../assets/BANNER");

Expand Down
2 changes: 1 addition & 1 deletion examples/hpm5300evk/src/bin/usb.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ use embassy_usb::Builder;
use futures_util::future::join;
use hal::usb::{Instance, UsbDriver};
use hpm_hal::{bind_interrupts, peripherals};
use {defmt_rtt as _, hpm_hal as hal, riscv_rt as _};
use {defmt_rtt as _, hpm_hal as hal};

bind_interrupts!(struct Irqs {
USB0 => hal::usb::InterruptHandler<peripherals::USB0>;
Expand Down
2 changes: 1 addition & 1 deletion examples/hpm5300evk/src/bin/usb_hid.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ use hpm_hal::gpio::{Input, Pull};
use hpm_hal::{bind_interrupts, peripherals};
use static_cell::StaticCell;
use usbd_hid::descriptor::{KeyboardReport, SerializedDescriptor};
use {defmt_rtt as _, hpm_hal as hal, riscv_rt as _};
use {defmt_rtt as _, hpm_hal as hal};

bind_interrupts!(struct Irqs {
USB0 => hal::usb::InterruptHandler<peripherals::USB0>;
Expand Down
21 changes: 21 additions & 0 deletions src/rng.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
//! - FIFO underflow
//!

use embassy_futures::yield_now;
use embassy_hal_internal::{into_ref, Peripheral, PeripheralRef};
use rand_core::{CryptoRng, RngCore};

Expand Down Expand Up @@ -67,6 +68,26 @@ impl<'d, T: Instance> Rng<'d, T> {
Ok(())
}

#[inline]
async fn async_next_u32(&mut self) -> Result<u32, Error> {
while T::regs().sta().read().busy() {
yield_now().await;
}
Ok(T::regs().fo2b().read().0)
}

// NOTE: RNG interrupt is non-functional.
// See-also: https://github.com/hpmicro/hpm-hal/issues/37
pub async fn async_fill_bytes(&mut self, dest: &mut [u8]) -> Result<(), Error> {
for chunk in dest.chunks_mut(4) {
let rand = self.async_next_u32().await?;
for (slot, num) in chunk.iter_mut().zip(rand.to_ne_bytes().iter()) {
*slot = *num
}
}
Ok(())
}

/// Run self-test
pub fn run_selftest(&mut self) -> Result<(), Error> {
let r = T::regs();
Expand Down
Loading