From 3d6a492f7966ff8dc6a017c33c309c1bd8f7825b Mon Sep 17 00:00:00 2001 From: Robin Mueller Date: Tue, 17 Sep 2024 00:11:44 +0200 Subject: [PATCH] some improvements --- examples/simple/Cargo.toml | 1 + examples/simple/examples/dma.rs | 24 ++++++++++++------------ examples/simple/examples/timer-ticks.rs | 8 ++++---- va416xx-hal/Cargo.toml | 1 + va416xx-hal/src/timer.rs | 6 +++--- 5 files changed, 21 insertions(+), 19 deletions(-) diff --git a/examples/simple/Cargo.toml b/examples/simple/Cargo.toml index d966d29..526e605 100644 --- a/examples/simple/Cargo.toml +++ b/examples/simple/Cargo.toml @@ -6,6 +6,7 @@ edition = "2021" [dependencies] cortex-m = { version = "0.7", features = ["critical-section-single-core"] } cortex-m-rt = "0.7" +critical-section = "1" panic-rtt-target = { version = "0.1.3" } rtt-target = { version = "0.5" } embedded-hal = "1" diff --git a/examples/simple/examples/dma.rs b/examples/simple/examples/dma.rs index 54b78f7..62cb2fa 100644 --- a/examples/simple/examples/dma.rs +++ b/examples/simple/examples/dma.rs @@ -4,8 +4,8 @@ use core::cell::Cell; -use cortex_m::interrupt::Mutex; use cortex_m_rt::entry; +use critical_section::Mutex; use embedded_hal::delay::DelayNs; use panic_rtt_target as _; use rtt_target::{rprintln, rtt_init_print}; @@ -88,10 +88,10 @@ fn transfer_example_8_bit( (0..64).for_each(|i| { src_buf[i] = i as u8; }); - cortex_m::interrupt::free(|cs| { + critical_section::with(|cs| { DMA_DONE_FLAG.borrow(cs).set(false); }); - cortex_m::interrupt::free(|cs| { + critical_section::with(|cs| { DMA_ACTIVE_FLAG.borrow(cs).set(false); }); // Safety: The source and destination buffer are valid for the duration of the DMA transfer. @@ -112,7 +112,7 @@ fn transfer_example_8_bit( // Use polling for completion status. loop { let mut dma_done = false; - cortex_m::interrupt::free(|cs| { + critical_section::with(|cs| { if DMA_ACTIVE_FLAG.borrow(cs).get() { rprintln!("DMA0 is active with 8 bit transfer"); DMA_ACTIVE_FLAG.borrow(cs).set(false); @@ -143,10 +143,10 @@ fn transfer_example_16_bit(dma0: &mut DmaChannel, delay_ms: &mut CountdownTimer< DMA_SRC_BUF[i] = (i as u32 * u16::MAX as u32 / (dest_buf_ref.len() as u32 - 1)) as u16; }); } - cortex_m::interrupt::free(|cs| { + critical_section::with(|cs| { DMA_DONE_FLAG.borrow(cs).set(false); }); - cortex_m::interrupt::free(|cs| { + critical_section::with(|cs| { DMA_ACTIVE_FLAG.borrow(cs).set(false); }); // Safety: The source and destination buffer are valid for the duration of the DMA transfer. @@ -170,7 +170,7 @@ fn transfer_example_16_bit(dma0: &mut DmaChannel, delay_ms: &mut CountdownTimer< // Use polling for completion status. loop { let mut dma_done = false; - cortex_m::interrupt::free(|cs| { + critical_section::with(|cs| { if DMA_ACTIVE_FLAG.borrow(cs).get() { rprintln!("DMA0 is active with 16-bit transfer"); DMA_ACTIVE_FLAG.borrow(cs).set(false); @@ -206,10 +206,10 @@ fn transfer_example_32_bit( (0..16).for_each(|i| { src_buf[i] = (i as u64 * u32::MAX as u64 / (src_buf.len() - 1) as u64) as u32; }); - cortex_m::interrupt::free(|cs| { + critical_section::with(|cs| { DMA_DONE_FLAG.borrow(cs).set(false); }); - cortex_m::interrupt::free(|cs| { + critical_section::with(|cs| { DMA_ACTIVE_FLAG.borrow(cs).set(false); }); // Safety: The source and destination buffer are valid for the duration of the DMA transfer. @@ -230,7 +230,7 @@ fn transfer_example_32_bit( // Use polling for completion status. loop { let mut dma_done = false; - cortex_m::interrupt::free(|cs| { + critical_section::with(|cs| { if DMA_ACTIVE_FLAG.borrow(cs).get() { rprintln!("DMA0 is active with 32-bit transfer"); DMA_ACTIVE_FLAG.borrow(cs).set(false); @@ -260,7 +260,7 @@ fn transfer_example_32_bit( #[allow(non_snake_case)] fn DMA_DONE0() { // Notify the main loop that the DMA transfer is finished. - cortex_m::interrupt::free(|cs| { + critical_section::with(|cs| { DMA_DONE_FLAG.borrow(cs).set(true); }); } @@ -269,7 +269,7 @@ fn DMA_DONE0() { #[allow(non_snake_case)] fn DMA_ACTIVE0() { // Notify the main loop that the DMA 0 is active now. - cortex_m::interrupt::free(|cs| { + critical_section::with(|cs| { DMA_ACTIVE_FLAG.borrow(cs).set(true); }); } diff --git a/examples/simple/examples/timer-ticks.rs b/examples/simple/examples/timer-ticks.rs index 4eb47e8..2f13b58 100644 --- a/examples/simple/examples/timer-ticks.rs +++ b/examples/simple/examples/timer-ticks.rs @@ -3,8 +3,8 @@ #![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; @@ -40,11 +40,11 @@ fn main() -> ! { second_timer.start(1.Hz()); second_timer.listen(); loop { - let current_ms = cortex_m::interrupt::free(|cs| MS_COUNTER.borrow(cs).get()); + let current_ms = critical_section::with(|cs| MS_COUNTER.borrow(cs).get()); if current_ms - last_ms >= 1000 { last_ms = current_ms; rprintln!("MS counter: {}", current_ms); - let second = cortex_m::interrupt::free(|cs| SEC_COUNTER.borrow(cs).get()); + let second = critical_section::with(|cs| SEC_COUNTER.borrow(cs).get()); rprintln!("Second counter: {}", second); } cortex_m::asm::delay(10000); @@ -60,7 +60,7 @@ fn TIM0() { #[interrupt] #[allow(non_snake_case)] fn TIM1() { - cortex_m::interrupt::free(|cs| { + critical_section::with(|cs| { let mut sec = SEC_COUNTER.borrow(cs).get(); sec += 1; SEC_COUNTER.borrow(cs).set(sec); diff --git a/va416xx-hal/Cargo.toml b/va416xx-hal/Cargo.toml index 038b44e..fac1f63 100644 --- a/va416xx-hal/Cargo.toml +++ b/va416xx-hal/Cargo.toml @@ -12,6 +12,7 @@ categories = ["embedded", "no-std", "hardware-support"] [dependencies] cortex-m = { version = "0.7", features = ["critical-section-single-core"] } +critical-section = "1" nb = "1" paste = "1" embedded-hal-nb = "1" diff --git a/va416xx-hal/src/timer.rs b/va416xx-hal/src/timer.rs index 7fd44a4..100d0a7 100644 --- a/va416xx-hal/src/timer.rs +++ b/va416xx-hal/src/timer.rs @@ -5,7 +5,7 @@ //! - [Timer MS and Second Tick Example](https://github.com/us-irs/va416xx-rs/blob/main/examples/simple/examples/timer-ticks.rs) use core::cell::Cell; -use cortex_m::interrupt::Mutex; +use critical_section::Mutex; use crate::clock::Clocks; use crate::gpio::{ @@ -787,7 +787,7 @@ pub fn set_up_ms_tick( /// This function can be called in a specified interrupt handler to increment /// the MS counter pub fn default_ms_irq_handler() { - cortex_m::interrupt::free(|cs| { + critical_section::with(|cs| { let mut ms = MS_COUNTER.borrow(cs).get(); ms += 1; MS_COUNTER.borrow(cs).set(ms); @@ -796,7 +796,7 @@ pub fn default_ms_irq_handler() { /// Get the current MS tick count pub fn get_ms_ticks() -> u32 { - cortex_m::interrupt::free(|cs| MS_COUNTER.borrow(cs).get()) + critical_section::with(|cs| MS_COUNTER.borrow(cs).get()) } pub struct DelayMs(CountdownTimer);