From d25108f85d2afbe8889b4b3c7822bc16955ee0da Mon Sep 17 00:00:00 2001 From: Robin Mueller Date: Tue, 17 Sep 2024 12:11:54 +0200 Subject: [PATCH] more docs --- examples/embassy/src/main.rs | 1 - examples/simple/examples/dma.rs | 2 ++ examples/simple/examples/timer-ticks.rs | 22 ++++++++++++---------- examples/simple/examples/wdt.rs | 8 +++++--- flashloader/src/main.rs | 2 ++ va416xx-hal/src/irq_router.rs | 18 ++++++++++++++++++ va416xx-hal/src/lib.rs | 24 ++++++++++++++++++++++++ va416xx-hal/src/timer.rs | 2 +- 8 files changed, 64 insertions(+), 15 deletions(-) create mode 100644 va416xx-hal/src/irq_router.rs diff --git a/examples/embassy/src/main.rs b/examples/embassy/src/main.rs index 830daa5..b9ebbeb 100644 --- a/examples/embassy/src/main.rs +++ b/examples/embassy/src/main.rs @@ -1,6 +1,5 @@ #![no_std] #![no_main] -use cortex_m::asm; use embassy_executor::Spawner; use embassy_time::Timer; use embedded_hal::digital::StatefulOutputPin; diff --git a/examples/simple/examples/dma.rs b/examples/simple/examples/dma.rs index 62cb2fa..baf933e 100644 --- a/examples/simple/examples/dma.rs +++ b/examples/simple/examples/dma.rs @@ -11,6 +11,7 @@ use panic_rtt_target as _; use rtt_target::{rprintln, rtt_init_print}; use simple_examples::peb1; use va416xx_hal::dma::{Dma, DmaCfg, DmaChannel, DmaCtrlBlock}; +use va416xx_hal::irq_router::enable_and_init_irq_router; use va416xx_hal::pwm::CountdownTimer; use va416xx_hal::{ pac::{self, interrupt}, @@ -45,6 +46,7 @@ fn main() -> ! { .xtal_n_clk_with_src_freq(peb1::EXTCLK_FREQ) .freeze(&mut dp.sysconfig) .unwrap(); + enable_and_init_irq_router(&mut dp.sysconfig, &dp.irq_router); // Safety: The DMA control block has an alignment rule of 128 and we constructed it directly // statically. let dma = Dma::new(&mut dp.sysconfig, dp.dma, DmaCfg::default(), unsafe { diff --git a/examples/simple/examples/timer-ticks.rs b/examples/simple/examples/timer-ticks.rs index 6da3f8e..b35355b 100644 --- a/examples/simple/examples/timer-ticks.rs +++ b/examples/simple/examples/timer-ticks.rs @@ -10,7 +10,12 @@ use panic_rtt_target as _; use rtt_target::{rprintln, rtt_init_print}; use simple_examples::peb1; use va416xx_hal::{ - pac::{self, interrupt}, + clock::{ + assert_periph_reset, deassert_periph_reset, disable_peripheral_clock, + enable_peripheral_clock, PeripheralSelect, + }, + irq_router::enable_and_init_irq_router, + pac::{self, adc::rxfifoirqtrg::W, interrupt}, prelude::*, timer::{default_ms_irq_handler, set_up_ms_tick, CountdownTimer, MS_COUNTER}, }; @@ -36,24 +41,21 @@ fn main() -> ! { .xtal_n_clk_with_src_freq(peb1::EXTCLK_FREQ) .freeze(&mut dp.sysconfig) .unwrap(); + enable_and_init_irq_router(&mut dp.sysconfig, &dp.irq_router); let _ = set_up_ms_tick(&mut dp.sysconfig, dp.tim0, &clocks); let mut second_timer = CountdownTimer::new(&mut dp.sysconfig, dp.tim1, &clocks); - second_timer.start(1.Hz()); second_timer.listen(); - // let delay_provider = dp.sy + second_timer.start(1.Hz()); loop { let current_ms = critical_section::with(|cs| MS_COUNTER.borrow(cs).get()); - if current_ms - last_ms >= 1000 { - last_ms = current_ms; + if current_ms >= last_ms + 1000 { + // To prevent drift. + last_ms += 1000; rprintln!("MS counter: {}", current_ms); let second = critical_section::with(|cs| SEC_COUNTER.borrow(cs).get()); rprintln!("Second counter: {}", second); } - for _ in 0..30000 { - asm::nop(); - } - - //delay_provider.delay_ms(1000); + asm::delay(1000); } } diff --git a/examples/simple/examples/wdt.rs b/examples/simple/examples/wdt.rs index 75c6e43..cf7b0c7 100644 --- a/examples/simple/examples/wdt.rs +++ b/examples/simple/examples/wdt.rs @@ -3,11 +3,12 @@ #![no_std] use core::cell::Cell; -use cortex_m::interrupt::Mutex; use cortex_m_rt::entry; +use critical_section::Mutex; use panic_rtt_target as _; use rtt_target::{rprintln, rtt_init_print}; use simple_examples::peb1; +use va416xx_hal::irq_router::enable_and_init_irq_router; use va416xx_hal::pac::{self, interrupt}; use va416xx_hal::prelude::*; use va416xx_hal::wdt::Wdt; @@ -40,6 +41,7 @@ fn main() -> ! { .xtal_n_clk_with_src_freq(peb1::EXTCLK_FREQ) .freeze(&mut dp.sysconfig) .unwrap(); + enable_and_init_irq_router(&mut dp.sysconfig, &dp.irq_router); let mut delay_sysclk = cortex_m::delay::Delay::new(cp.SYST, clocks.apb0().raw()); let mut last_interrupt_counter = 0; @@ -49,7 +51,7 @@ fn main() -> ! { if TEST_MODE != TestMode::AllowReset { wdt_ctrl.feed(); } - let interrupt_counter = cortex_m::interrupt::free(|cs| WDT_INTRPT_COUNT.borrow(cs).get()); + let interrupt_counter = critical_section::with(|cs| WDT_INTRPT_COUNT.borrow(cs).get()); if interrupt_counter > last_interrupt_counter { rprintln!("interrupt counter has increased to {}", interrupt_counter); last_interrupt_counter = interrupt_counter; @@ -65,7 +67,7 @@ fn main() -> ! { #[interrupt] #[allow(non_snake_case)] fn WATCHDOG() { - cortex_m::interrupt::free(|cs| { + critical_section::with(|cs| { WDT_INTRPT_COUNT .borrow(cs) .set(WDT_INTRPT_COUNT.borrow(cs).get() + 1); diff --git a/flashloader/src/main.rs b/flashloader/src/main.rs index c9bd62f..98edfca 100644 --- a/flashloader/src/main.rs +++ b/flashloader/src/main.rs @@ -108,6 +108,7 @@ mod app { use spacepackets::ecss::{ tc::PusTcReader, tm::PusTmCreator, EcssEnumU8, PusPacket, WritablePusPacket, }; + use va416xx_hal::irq_router::enable_and_init_irq_router; use va416xx_hal::{ clock::ClkgenExt, edac, @@ -163,6 +164,7 @@ mod app { .xtal_n_clk_with_src_freq(Hertz::from_raw(EXTCLK_FREQ)) .freeze(&mut cx.device.sysconfig) .unwrap(); + enable_and_init_irq_router(&mut cx.device.sysconfig, &cx.device.irq_router); setup_edac(&mut cx.device.sysconfig); let gpiob = PinsG::new(&mut cx.device.sysconfig, cx.device.portg); diff --git a/va416xx-hal/src/irq_router.rs b/va416xx-hal/src/irq_router.rs new file mode 100644 index 0000000..4c7dbb5 --- /dev/null +++ b/va416xx-hal/src/irq_router.rs @@ -0,0 +1,18 @@ +use crate::{ + clock::{PeripheralSelect, SyscfgExt}, + pac, +}; + +pub fn enable_and_init_irq_router(sysconfig: &mut pac::Sysconfig, irq_router: &pac::IrqRouter) { + sysconfig.enable_peripheral_clock(PeripheralSelect::IrqRouter); + sysconfig.assert_periph_reset_for_two_cycles(PeripheralSelect::IrqRouter); + unsafe { + irq_router.dmasel0().write_with_zero(|w| w); + irq_router.dmasel1().write_with_zero(|w| w); + irq_router.dmasel2().write_with_zero(|w| w); + irq_router.dmasel3().write_with_zero(|w| w); + irq_router.adcsel().write_with_zero(|w| w); + irq_router.dacsel0().write_with_zero(|w| w); + irq_router.dacsel1().write_with_zero(|w| w); + } +} diff --git a/va416xx-hal/src/lib.rs b/va416xx-hal/src/lib.rs index 8132766..e174c75 100644 --- a/va416xx-hal/src/lib.rs +++ b/va416xx-hal/src/lib.rs @@ -1,3 +1,26 @@ +//! This is the **H**ardware **A**bstraction **L**ayer (HAL) for the VA416xx MCU family. +//! +//! It is an additional hardware abstraction on top of the [peripheral access API](https://egit.irs.uni-stuttgart.de/rust/va416xx-rs/src/branch/main/va416xx). + +//! It is the result of reading the datasheet for the device and encoding a type-safe layer over the +//! raw PAC. This crate also implements traits specified by the +//! [embedded-hal](https://github.com/rust-embedded/embedded-hal) project, making it compatible with +//! various drivers in the embedded rust ecosystem. + +//! You have to enable one of the following device features to use this crate depending on +//! which chip you are using: + +//! - `va41630` +//! - `va41629` +//! - `va41628` +//! - `va41620` +//! +//! When using this HAL and writing applications for the VA416xx family in general, it is strongly +//! recommended that you set up the clock properly, because the default internal HBO clock +//! is not very accurate. You can use the [crate::clock] module for this. If you are working +//! with interrupts, it is strongly recommended to set up the IRQ router with the +//! [crate::irq_router] module at the very least because that peripheral has confusing and/or +//! faulty register reset values which might leads to weird bugs and glitches. #![no_std] #![cfg_attr(docsrs, feature(doc_auto_cfg))] #[cfg(test)] @@ -22,6 +45,7 @@ pub mod dma; pub mod edac; pub mod gpio; pub mod i2c; +pub mod irq_router; pub mod nvm; pub mod pwm; pub mod spi; diff --git a/va416xx-hal/src/timer.rs b/va416xx-hal/src/timer.rs index c6710a4..e3d356d 100644 --- a/va416xx-hal/src/timer.rs +++ b/va416xx-hal/src/timer.rs @@ -590,7 +590,7 @@ impl CountdownTimer { self.rst_val = (self.clock.raw() / self.curr_freq.raw()) - 1; self.set_reload(self.rst_val); // Decrementing counter, to set the reset value. - self.set_count(0); + self.set_count(self.rst_val); } #[inline(always)]