Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
1082: stm32: Add basic support for DMA priority settings r=lulf a=matoushybl

This adds very basic support for specifying priority for DMA interrupts. Unfortunately, the patch now doesn't allow for specifying different priorities for DMA1/DMA2, or BDMA1/BDMA2, which I didn't know how to support.

1083: stm32: Fix H7 unaligned erase r=lulf a=matoushybl

This PR simplifies erasing sectors on the H7, which was buggy.

Co-authored-by: Matous Hybl <hyblmatous@gmail.com>
  • Loading branch information
bors[bot] and matoushybl authored Dec 1, 2022
3 parents d8ea297 + 4cc0463 + 2a35a09 commit 9f85411
Show file tree
Hide file tree
Showing 5 changed files with 37 additions and 19 deletions.
7 changes: 5 additions & 2 deletions embassy-stm32/src/dma/bdma.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
use core::sync::atomic::{fence, Ordering};
use core::task::Waker;

use embassy_cortex_m::interrupt::Priority;
use embassy_sync::waitqueue::AtomicWaker;

use super::{TransferOptions, Word, WordSize};
Expand Down Expand Up @@ -38,10 +39,12 @@ impl State {
static STATE: State = State::new();

/// safety: must be called only once
pub(crate) unsafe fn init() {
pub(crate) unsafe fn init(irq_priority: Priority) {
foreach_interrupt! {
($peri:ident, bdma, $block:ident, $signal_name:ident, $irq:ident) => {
crate::interrupt::$irq::steal().enable();
let irq = crate::interrupt::$irq::steal();
irq.set_priority(irq_priority);
irq.enable();
};
}
crate::_generated::init_bdma();
Expand Down
7 changes: 5 additions & 2 deletions embassy-stm32/src/dma/dma.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use core::sync::atomic::{fence, Ordering};
use core::task::Waker;

use embassy_cortex_m::interrupt::Priority;
use embassy_sync::waitqueue::AtomicWaker;

use super::{Burst, FlowControl, Request, TransferOptions, Word, WordSize};
Expand Down Expand Up @@ -67,10 +68,12 @@ impl State {
static STATE: State = State::new();

/// safety: must be called only once
pub(crate) unsafe fn init() {
pub(crate) unsafe fn init(irq_priority: Priority) {
foreach_interrupt! {
($peri:ident, dma, $block:ident, $signal_name:ident, $irq:ident) => {
interrupt::$irq::steal().enable();
let irq = interrupt::$irq::steal();
irq.set_priority(irq_priority);
irq.enable();
};
}
crate::_generated::init_dma();
Expand Down
8 changes: 5 additions & 3 deletions embassy-stm32/src/dma/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ use core::mem;
use core::pin::Pin;
use core::task::{Context, Poll, Waker};

#[cfg(any(dma, bdma))]
use embassy_cortex_m::interrupt::Priority;
use embassy_hal_common::{impl_peripheral, into_ref};

#[cfg(dmamux)]
Expand Down Expand Up @@ -294,11 +296,11 @@ pub struct NoDma;
impl_peripheral!(NoDma);

// safety: must be called only once at startup
pub(crate) unsafe fn init() {
pub(crate) unsafe fn init(#[cfg(bdma)] bdma_priority: Priority, #[cfg(dma)] dma_priority: Priority) {
#[cfg(bdma)]
bdma::init();
bdma::init(bdma_priority);
#[cfg(dma)]
dma::init();
dma::init(dma_priority);
#[cfg(dmamux)]
dmamux::init();
#[cfg(gpdma)]
Expand Down
17 changes: 6 additions & 11 deletions embassy-stm32/src/flash/h7.rs
Original file line number Diff line number Diff line change
Expand Up @@ -79,24 +79,19 @@ pub(crate) unsafe fn blocking_erase(from: u32, to: u32) -> Result<(), Error> {
let from = from - super::FLASH_BASE as u32;
let to = to - super::FLASH_BASE as u32;

let bank_size = (super::FLASH_SIZE / 2) as u32;

let (bank, start, end) = if to <= bank_size {
let (start, end) = if to <= super::FLASH_SIZE as u32 {
let start_sector = from / super::ERASE_SIZE as u32;
let end_sector = to / super::ERASE_SIZE as u32;
(0, start_sector, end_sector)
} else if from >= SECOND_BANK_OFFSET as u32 && to <= (SECOND_BANK_OFFSET as u32 + bank_size) {
let start_sector = (from - SECOND_BANK_OFFSET as u32) / super::ERASE_SIZE as u32;
let end_sector = (to - SECOND_BANK_OFFSET as u32) / super::ERASE_SIZE as u32;
(1, start_sector, end_sector)
(start_sector, end_sector)
} else {
error!("Attempting to write outside of defined sectors");
error!("Attempting to write outside of defined sectors {:x} {:x}", from, to);
return Err(Error::Unaligned);
};

trace!("Erasing bank {}, sectors from {} to {}", bank, start, end);
trace!("Erasing sectors from {} to {}", start, end);
for sector in start..end {
let ret = erase_sector(pac::FLASH.bank(bank), sector as u8);
let bank = if sector >= 8 { 1 } else { 0 };
let ret = erase_sector(pac::FLASH.bank(bank), (sector % 8) as u8);
if ret.is_err() {
return ret;
}
Expand Down
17 changes: 16 additions & 1 deletion embassy-stm32/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,8 @@ pub(crate) mod _generated {
// Reexports
pub use _generated::{peripherals, Peripherals};
pub use embassy_cortex_m::executor;
#[cfg(any(dma, bdma))]
use embassy_cortex_m::interrupt::Priority;
pub use embassy_cortex_m::interrupt::_export::interrupt;
pub use embassy_hal_common::{into_ref, Peripheral, PeripheralRef};
#[cfg(feature = "unstable-pac")]
Expand All @@ -91,6 +93,10 @@ pub struct Config {
pub rcc: rcc::Config,
#[cfg(dbgmcu)]
pub enable_debug_during_sleep: bool,
#[cfg(bdma)]
pub bdma_interrupt_priority: Priority,
#[cfg(dma)]
pub dma_interrupt_priority: Priority,
}

impl Default for Config {
Expand All @@ -99,6 +105,10 @@ impl Default for Config {
rcc: Default::default(),
#[cfg(dbgmcu)]
enable_debug_during_sleep: true,
#[cfg(bdma)]
bdma_interrupt_priority: Priority::P0,
#[cfg(dma)]
dma_interrupt_priority: Priority::P0,
}
}
}
Expand Down Expand Up @@ -137,7 +147,12 @@ pub fn init(config: Config) -> Peripherals {
}

gpio::init();
dma::init();
dma::init(
#[cfg(bdma)]
config.bdma_interrupt_priority,
#[cfg(dma)]
config.dma_interrupt_priority,
);
#[cfg(feature = "exti")]
exti::init();

Expand Down

0 comments on commit 9f85411

Please sign in to comment.