diff --git a/embassy-stm32/src/dbgmcu/mod.rs b/embassy-stm32/src/dbgmcu/mod.rs new file mode 100644 index 000000000..8dc4cc53f --- /dev/null +++ b/embassy-stm32/src/dbgmcu/mod.rs @@ -0,0 +1,13 @@ +pub struct Dbgmcu {} + +impl Dbgmcu { + pub unsafe fn enable_all() { + crate::pac::DBGMCU.cr().modify(|cr| { + crate::pac::dbgmcu! { + (cr, $fn_name:ident) => { + cr.$fn_name(true); + }; + } + }); + } +} diff --git a/embassy-stm32/src/lib.rs b/embassy-stm32/src/lib.rs index c10310e2d..07f8b9f3b 100644 --- a/embassy-stm32/src/lib.rs +++ b/embassy-stm32/src/lib.rs @@ -31,6 +31,8 @@ pub mod adc; pub mod clock; #[cfg(dac)] pub mod dac; +#[cfg(dbgmcu)] +pub mod dbgmcu; #[cfg(all(eth, feature = "net"))] pub mod eth; #[cfg(exti)] diff --git a/examples/stm32f4/src/bin/spi.rs b/examples/stm32f4/src/bin/spi.rs index 7cf391394..f8c9997ed 100644 --- a/examples/stm32f4/src/bin/spi.rs +++ b/examples/stm32f4/src/bin/spi.rs @@ -18,17 +18,14 @@ use embassy_stm32::pac; use embassy_stm32::spi::{Config, Spi}; use embassy_stm32::time::Hertz; use embedded_hal::blocking::spi::Transfer; +use embassy_stm32::dbgmcu::Dbgmcu; #[entry] fn main() -> ! { info!("Hello World, dude!"); unsafe { - pac::DBGMCU.cr().modify(|w| { - w.set_dbg_sleep(true); - w.set_dbg_standby(true); - w.set_dbg_stop(true); - }); + Dbgmcu::enable_all(); pac::RCC.ahb1enr().modify(|w| { w.set_gpioaen(true); diff --git a/examples/stm32h7/src/bin/usart_dma.rs b/examples/stm32h7/src/bin/usart_dma.rs index 0073d5c66..097466cea 100644 --- a/examples/stm32h7/src/bin/usart_dma.rs +++ b/examples/stm32h7/src/bin/usart_dma.rs @@ -14,6 +14,7 @@ use embassy::time::Clock; use embassy::util::Forever; use embassy_stm32::dma::NoDma; use embassy_stm32::usart::{Config, Uart}; +use embassy_stm32::dbgmcu::Dbgmcu; use example_common::*; use embassy_traits::uart::Write as _Write; @@ -72,13 +73,9 @@ fn main() -> ! { let pp = unsafe { pac::Peripherals::steal() }; - pp.DBGMCU.cr.modify(|_, w| { - w.dbgsleep_d1().set_bit(); - w.dbgstby_d1().set_bit(); - w.dbgstop_d1().set_bit(); - w.d1dbgcken().set_bit(); - w - }); + unsafe { + Dbgmcu::enable_all(); + } pp.RCC.ahb4enr.modify(|_, w| { w.gpioaen().set_bit(); diff --git a/stm32-metapac-gen/src/lib.rs b/stm32-metapac-gen/src/lib.rs index fee991111..bfc2384cd 100644 --- a/stm32-metapac-gen/src/lib.rs +++ b/stm32-metapac-gen/src/lib.rs @@ -10,6 +10,7 @@ use std::path::Path; use std::path::PathBuf; use chiptool::{generate, ir, transform}; +use chiptool::util::ToSanitizedSnakeCase; #[derive(Debug, Eq, PartialEq, Clone, Deserialize)] pub struct Chip { @@ -245,6 +246,21 @@ pub fn gen(options: Options) { peripherals: Vec::new(), }; + // Load DBGMCU register for chip + let mut dbgmcu: Option = core.peripherals.iter().find_map(|(name, p)| { + if name == "DBGMCU" { + p.block.as_ref().map(|block| { + let bi = BlockInfo::parse(block); + let dbgmcu_reg_path = data_dir + .join("registers") + .join(&format!("{}_{}.yaml", bi.module, bi.version)); + serde_yaml::from_reader(File::open(dbgmcu_reg_path).unwrap()).unwrap() + }) + } else { + None + } + }); + // Load RCC register for chip let rcc = core.peripherals.iter().find_map(|(name, p)| { if name == "RCC" { @@ -270,12 +286,24 @@ pub fn gen(options: Options) { let mut peripheral_dma_channels_table: Vec> = Vec::new(); let mut peripheral_counts: HashMap = HashMap::new(); let mut dma_channel_counts: HashMap = HashMap::new(); + let mut dbgmcu_table: Vec> = Vec::new(); let gpio_base = core.peripherals.get(&"GPIOA".to_string()).unwrap().address; let gpio_stride = 0x400; let number_suffix_re = Regex::new("^(.*?)[0-9]*$").unwrap(); + if let Some(ref mut reg) = dbgmcu { + if let Some(ref cr) = reg.fieldsets.get("CR") { + for field in cr.fields.iter().filter(|e| e.name.contains("DBG")) { + let mut fn_name = String::new(); + fn_name.push_str("set_"); + fn_name.push_str( &field.name.to_sanitized_snake_case() ); + dbgmcu_table.push( vec!( "cr".into(), fn_name )); + } + } + } + let mut has_bdma = false; let mut has_dma = false; @@ -559,6 +587,7 @@ pub fn gen(options: Options) { ); make_table(&mut extra, "peripheral_rcc", &peripheral_rcc_table); make_table(&mut extra, "dma_channels", &dma_channels_table); + make_table(&mut extra, "dbgmcu", &dbgmcu_table); make_peripheral_counts(&mut extra, &peripheral_counts); make_dma_channel_counts(&mut extra, &dma_channel_counts);