From be180c1c521694193f011bba4169cebe41c8dee2 Mon Sep 17 00:00:00 2001 From: Bob McWhirter Date: Wed, 2 Jun 2021 15:03:24 -0400 Subject: [PATCH 01/12] Create the new peripheral_pins! macro table. --- stm32-data | 2 +- stm32-metapac/build.rs | 43 +++++++++++++++++++++++++++++++++++------- 2 files changed, 37 insertions(+), 8 deletions(-) diff --git a/stm32-data b/stm32-data index dfc67fe25..33dfa6748 160000 --- a/stm32-data +++ b/stm32-data @@ -1 +1 @@ -Subproject commit dfc67fe255e1e70101e9f1e3fb8b4fd8bb37362f +Subproject commit 33dfa674865b1b5f0bfb86f3217055a6a057a6fb diff --git a/stm32-metapac/build.rs b/stm32-metapac/build.rs index 49ca76201..1d0696ba9 100644 --- a/stm32-metapac/build.rs +++ b/stm32-metapac/build.rs @@ -40,11 +40,22 @@ pub struct Peripheral { pub block: Option, #[serde(default)] pub clock: Option, + #[serde(default)] + pub pins: Option>, +} + +#[derive(Debug, Eq, PartialEq, Clone, Deserialize)] +pub struct Pin { + pub pin: String, + pub signal: String, + pub af: Option, } struct BlockInfo { - module: String, // usart_v1/USART -> usart - version: String, // usart_v1/USART -> v1 + module: String, + // usart_v1/USART -> usart + version: String, + // usart_v1/USART -> v1 block: String, // usart_v1/USART -> USART } @@ -79,7 +90,7 @@ macro_rules! {} {{ ", name, name ) - .unwrap(); + .unwrap(); for row in data { write!(out, " __{}_inner!(({}));\n", name, row.join(",")).unwrap(); @@ -90,7 +101,7 @@ macro_rules! {} {{ " }}; }}" ) - .unwrap(); + .unwrap(); } fn main() { @@ -123,6 +134,7 @@ fn main() { let mut cfgs: HashSet = HashSet::new(); let mut pin_table: Vec> = Vec::new(); let mut interrupt_table: Vec> = Vec::new(); + let mut peripheral_pins_table: Vec> = Vec::new(); let dma_base = chip .peripherals @@ -149,11 +161,27 @@ fn main() { if let Some(block) = &p.block { let bi = BlockInfo::parse(block); + if let Some(pins) = &p.pins { + for pin in pins { + let mut row = Vec::new(); + row.push(name.clone()); + row.push(bi.module.clone()); + row.push(bi.block.clone()); + row.push(pin.pin.clone()); + row.push(pin.signal.clone()); + if let Some(ref af) = pin.af { + row.push(af.clone()); + } + peripheral_pins_table.push(row); + } + } + + cfgs.insert(bi.module.clone()); cfgs.insert(format!("{}_{}", bi.module, bi.version)); if let Some(old_version) = - peripheral_versions.insert(bi.module.clone(), bi.version.clone()) + peripheral_versions.insert(bi.module.clone(), bi.version.clone()) { if old_version != bi.version { panic!( @@ -227,6 +255,7 @@ fn main() { make_table(&mut extra, "pins", &pin_table); make_table(&mut extra, "interrupts", &interrupt_table); make_table(&mut extra, "peripheral_versions", &peripheral_version_table); + make_table(&mut extra, "peripheral_pins", &peripheral_pins_table); for (module, version) in peripheral_versions { println!("loading {} {}", module, version); @@ -248,7 +277,7 @@ fn main() { transform::NameKind::Enum => format!("{}::vals::{}", prefix, s), _ => s.to_string(), }) - .unwrap(); + .unwrap(); ir.merge(peri); } @@ -277,7 +306,7 @@ fn main() { "PROVIDE({} = DefaultHandler);\n", name.to_ascii_uppercase() ) - .unwrap(); + .unwrap(); } File::create(out.join("device.x")) From d4d914ea502fcd1a50a4d1f509617c0783f91740 Mon Sep 17 00:00:00 2001 From: Bob McWhirter Date: Thu, 3 Jun 2021 10:13:11 -0400 Subject: [PATCH 02/12] Remove the Option around the pins Vec. --- stm32-metapac/build.rs | 31 +++++++++++++++---------------- 1 file changed, 15 insertions(+), 16 deletions(-) diff --git a/stm32-metapac/build.rs b/stm32-metapac/build.rs index 1d0696ba9..046bc11f3 100644 --- a/stm32-metapac/build.rs +++ b/stm32-metapac/build.rs @@ -41,7 +41,7 @@ pub struct Peripheral { #[serde(default)] pub clock: Option, #[serde(default)] - pub pins: Option>, + pub pins: Vec, } #[derive(Debug, Eq, PartialEq, Clone, Deserialize)] @@ -52,11 +52,12 @@ pub struct Pin { } struct BlockInfo { + /// usart_v1/USART -> usart module: String, - // usart_v1/USART -> usart + /// usart_v1/USART -> v1 version: String, - // usart_v1/USART -> v1 - block: String, // usart_v1/USART -> USART + /// usart_v1/USART -> USART + block: String, } impl BlockInfo { @@ -161,19 +162,17 @@ fn main() { if let Some(block) = &p.block { let bi = BlockInfo::parse(block); - if let Some(pins) = &p.pins { - for pin in pins { - let mut row = Vec::new(); - row.push(name.clone()); - row.push(bi.module.clone()); - row.push(bi.block.clone()); - row.push(pin.pin.clone()); - row.push(pin.signal.clone()); - if let Some(ref af) = pin.af { - row.push(af.clone()); - } - peripheral_pins_table.push(row); + for pin in &p.pins { + let mut row = Vec::new(); + row.push(name.clone()); + row.push(bi.module.clone()); + row.push(bi.block.clone()); + row.push(pin.pin.clone()); + row.push(pin.signal.clone()); + if let Some(ref af) = pin.af { + row.push(af.clone()); } + peripheral_pins_table.push(row); } From 6958091b500a1104a3d9d3a2737b9fe756f25702 Mon Sep 17 00:00:00 2001 From: Bob McWhirter Date: Thu, 3 Jun 2021 11:09:29 -0400 Subject: [PATCH 03/12] Move DAC, I2C, SPI and RNG to macro-tables. --- embassy-stm32/gen.py | 66 ++++++++++++++++++------------------ embassy-stm32/src/dac/mod.rs | 28 +++++++++------ embassy-stm32/src/i2c/mod.rs | 28 ++++++++++----- embassy-stm32/src/rng.rs | 55 ++++++++++++++++++++++++++++++ embassy-stm32/src/spi/mod.rs | 41 ++++++++++++++++------ stm32-metapac/build.rs | 6 ++++ 6 files changed, 162 insertions(+), 62 deletions(-) diff --git a/embassy-stm32/gen.py b/embassy-stm32/gen.py index bfb791945..e8645ccf9 100644 --- a/embassy-stm32/gen.py +++ b/embassy-stm32/gen.py @@ -63,32 +63,32 @@ with open(output_file, 'w') as f: if (func := funcs.get(f'{name}_CK')) != None: f.write(f'impl_usart_pin!({name}, CkPin, {pin}, {func});') - if block_mod == 'rng': - for irq in chip['interrupts']: - if re.search('RNG', irq): - f.write(f'impl_rng!({name}, {irq});') + # if block_mod == 'rng': + # for irq in chip['interrupts']: + # if re.search('RNG', irq): + # f.write(f'impl_rng!({name}, {irq});') - if block_mod == 'spi': - if 'clock' in peri: - clock = peri['clock'] - f.write(f'impl_spi!({name}, {clock});') - for pin, funcs in af.items(): - if pin in pins: - if (func := funcs.get(f'{name}_SCK')) != None: - f.write(f'impl_spi_pin!({name}, SckPin, {pin}, {func});') - if (func := funcs.get(f'{name}_MOSI')) != None: - f.write(f'impl_spi_pin!({name}, MosiPin, {pin}, {func});') - if (func := funcs.get(f'{name}_MISO')) != None: - f.write(f'impl_spi_pin!({name}, MisoPin, {pin}, {func});') + # if block_mod == 'spi': + # if 'clock' in peri: + # clock = peri['clock'] + # f.write(f'impl_spi!({name}, {clock});') + # for pin, funcs in af.items(): + # if pin in pins: + # if (func := funcs.get(f'{name}_SCK')) != None: + # f.write(f'impl_spi_pin!({name}, SckPin, {pin}, {func});') + # if (func := funcs.get(f'{name}_MOSI')) != None: + # f.write(f'impl_spi_pin!({name}, MosiPin, {pin}, {func});') + # if (func := funcs.get(f'{name}_MISO')) != None: + # f.write(f'impl_spi_pin!({name}, MisoPin, {pin}, {func});') - if block_mod == 'i2c': - f.write(f'impl_i2c!({name});') - for pin, funcs in af.items(): - if pin in pins: - if func := funcs.get(f'{name}_SCL'): - f.write(f'impl_i2c_pin!({name}, SclPin, {pin}, {func});') - if func := funcs.get(f'{name}_SDA'): - f.write(f'impl_i2c_pin!({name}, SdaPin, {pin}, {func});') + # if block_mod == 'i2c': + # f.write(f'impl_i2c!({name});') + # for pin, funcs in af.items(): + # if pin in pins: + # if func := funcs.get(f'{name}_SCL'): + # f.write(f'impl_i2c_pin!({name}, SclPin, {pin}, {func});') + # if func := funcs.get(f'{name}_SDA'): + # f.write(f'impl_i2c_pin!({name}, SdaPin, {pin}, {func});') if block_mod == 'gpio': custom_singletons = True @@ -145,15 +145,15 @@ with open(output_file, 'w') as f: if re.match('EXTI', irq): exti_interrupts.append(irq) - if block_mod == 'dac': - f.write(f'impl_dac!({name});') - if 'dac_out1' in peri: - pin = peri['dac_out1'] - f.write(f'impl_dac_pin!({name}, 1, {pin});') - if 'dac_out2' in peri: - pin = peri['dac_out2'] - f.write(f'impl_dac_pin!({name}, 2, {pin});') - + # if block_mod == 'dac': + # f.write(f'impl_dac!({name});') + # if 'dac_out1' in peri: + # pin = peri['dac_out1'] + # f.write(f'impl_dac_pin!({name}, 1, {pin});') + # if 'dac_out2' in peri: + # pin = peri['dac_out2'] + # f.write(f'impl_dac_pin!({name}, 2, {pin});') + # if not custom_singletons: singletons.append(name) diff --git a/embassy-stm32/src/dac/mod.rs b/embassy-stm32/src/dac/mod.rs index e1a603d40..46c485b35 100644 --- a/embassy-stm32/src/dac/mod.rs +++ b/embassy-stm32/src/dac/mod.rs @@ -3,6 +3,7 @@ #[cfg_attr(dac_v2, path = "v2.rs")] mod _version; use crate::gpio::NoPin; +use crate::peripherals; pub use _version::*; pub(crate) mod sealed { @@ -23,8 +24,8 @@ pub trait DacPin: sealed::DacPin + 'static {} impl DacPin for NoPin {} impl sealed::DacPin for NoPin {} -macro_rules! impl_dac { - ($inst:ident) => { +crate::pac::peripherals!( + (dac, $inst:ident) => { impl crate::dac::sealed::Instance for peripherals::$inst { fn regs() -> &'static crate::pac::dac::Dac { &crate::pac::$inst @@ -33,16 +34,21 @@ macro_rules! impl_dac { impl crate::dac::Instance for peripherals::$inst {} }; -} +); -macro_rules! impl_dac_pin { - ($inst:ident, $channel:expr, $pin:ident ) => { - impl crate::dac::DacPin for peripherals::$pin {} +crate::pac::peripheral_pins!( + ($inst:ident, dac, DAC, $pin:ident, OUT1) => { + impl DacPin for peripherals::$pin {} - impl crate::dac::sealed::DacPin for peripherals::$pin { - //fn af_num(&self) -> u8 { - //$af - //} + impl sealed::DacPin for peripherals::$pin { + } + + }; + + ($inst:ident, dac, DAC, $pin:ident, OUT2) => { + impl DacPin for peripherals::$pin {} + + impl sealed::DacPin for peripherals::$pin { } }; -} +); diff --git a/embassy-stm32/src/i2c/mod.rs b/embassy-stm32/src/i2c/mod.rs index 7b0dd8160..f77e68600 100644 --- a/embassy-stm32/src/i2c/mod.rs +++ b/embassy-stm32/src/i2c/mod.rs @@ -3,6 +3,7 @@ #[cfg_attr(i2c_v1, path = "v1.rs")] #[cfg_attr(i2c_v2, path = "v2.rs")] mod _version; +use crate::peripherals; pub use _version::*; pub enum Error { @@ -37,8 +38,8 @@ pub trait SclPin: sealed::SclPin + 'static {} pub trait SdaPin: sealed::SdaPin + 'static {} -macro_rules! impl_i2c { - ($inst:ident) => { +crate::pac::peripherals!( + (i2c, $inst:ident) => { impl crate::i2c::sealed::Instance for peripherals::$inst { fn regs() -> &'static crate::pac::i2c::I2c { &crate::pac::$inst @@ -46,17 +47,28 @@ macro_rules! impl_i2c { } impl crate::i2c::Instance for peripherals::$inst {} + }; -} +); -macro_rules! impl_i2c_pin { - ($inst:ident, $pin_func:ident, $pin:ident, $af:expr) => { - impl crate::i2c::$pin_func for peripherals::$pin {} +crate::pac::peripheral_pins!( + ($inst:ident, i2c, I2C, $pin:ident, SDA, $af:expr) => { + impl SdaPin for peripherals::$pin {} - impl crate::i2c::sealed::$pin_func for peripherals::$pin { + impl sealed::SdaPin for peripherals::$pin { fn af_num(&self) -> u8 { $af } } }; -} + + ($inst:ident, i2c, I2C, $pin:ident, SCL, $af:expr) => { + impl SclPin for peripherals::$pin {} + + impl sealed::SclPin for peripherals::$pin { + fn af_num(&self) -> u8 { + $af + } + } + }; +); diff --git a/embassy-stm32/src/rng.rs b/embassy-stm32/src/rng.rs index 694ad4a1c..4dd26c671 100644 --- a/embassy-stm32/src/rng.rs +++ b/embassy-stm32/src/rng.rs @@ -9,6 +9,7 @@ use futures::future::poll_fn; use rand_core::{CryptoRng, RngCore}; use crate::pac; +use crate::peripherals; pub(crate) static RNG_WAKER: AtomicWaker = AtomicWaker::new(); @@ -134,6 +135,58 @@ pub(crate) mod sealed { pub trait Instance: sealed::Instance {} +crate::pac::peripherals!( + (rng, $inst:ident) => { + impl Instance for peripherals::$inst {} + + impl sealed::Instance for peripherals::$inst { + fn regs() -> crate::pac::rng::Rng { + crate::pac::RNG + } + } + }; +); + +macro_rules! irq { + ($irq:ident) => { + mod rng_irq { + use crate::interrupt; + + #[interrupt] + unsafe fn $irq() { + let bits = $crate::pac::RNG.sr().read(); + if bits.drdy() || bits.seis() || bits.ceis() { + $crate::pac::RNG.cr().write(|reg| reg.set_ie(false)); + $crate::rng::RNG_WAKER.wake(); + } + } + } + }; +} + +crate::pac::interrupts!( + (RNG) => { + irq!(RNG); + }; + + (RNG_LPUART1) => { + irq!(RNG_LPUART1); + }; + + (AES_RNG_LPUART1) => { + irq!(AES_RNG_LPUART1); + }; + + (AES_RNG) => { + irq!(AES_RNG); + }; + + (HASH_RNG) => { + irq!(HASH_RNG); + }; +); + +/* macro_rules! impl_rng { ($inst:ident, $irq:ident) => { impl crate::rng::sealed::Instance for peripherals::RNG { @@ -158,3 +211,5 @@ macro_rules! impl_rng { } }; } + + */ diff --git a/embassy-stm32/src/spi/mod.rs b/embassy-stm32/src/spi/mod.rs index d8bd66d9c..a888622de 100644 --- a/embassy-stm32/src/spi/mod.rs +++ b/embassy-stm32/src/spi/mod.rs @@ -4,6 +4,7 @@ #[cfg_attr(spi_v2, path = "v2.rs")] #[cfg_attr(spi_v3, path = "v3.rs")] mod _version; +use crate::peripherals; pub use _version::*; use crate::gpio::Pin; @@ -71,26 +72,46 @@ pub trait MosiPin: sealed::MosiPin + 'static {} pub trait MisoPin: sealed::MisoPin + 'static {} -macro_rules! impl_spi { - ($inst:ident, $clk:ident) => { - impl crate::spi::sealed::Instance for peripherals::$inst { +crate::pac::peripherals!( + (spi, $inst:ident) => { + impl sealed::Instance for peripherals::$inst { fn regs() -> &'static crate::pac::spi::Spi { &crate::pac::$inst } } - impl crate::spi::Instance for peripherals::$inst {} + impl Instance for peripherals::$inst {} }; -} +); -macro_rules! impl_spi_pin { - ($inst:ident, $pin_func:ident, $pin:ident, $af:expr) => { - impl crate::spi::$pin_func for peripherals::$pin {} +crate::pac::peripheral_pins!( + ($inst:ident, spi, SPI, $pin:ident, SCK, $af:expr) => { + impl SckPin for peripherals::$pin {} - impl crate::spi::sealed::$pin_func for peripherals::$pin { + impl sealed::SckPin for peripherals::$pin { fn af_num(&self) -> u8 { $af } } }; -} + + ($inst:ident, spi, SPI, $pin:ident, MOSI, $af:expr) => { + impl MosiPin for peripherals::$pin {} + + impl sealed::MosiPin for peripherals::$pin { + fn af_num(&self) -> u8 { + $af + } + } + }; + + ($inst:ident, spi, SPI, $pin:ident, MISO, $af:expr) => { + impl MisoPin for peripherals::$pin {} + + impl sealed::MisoPin for peripherals::$pin { + fn af_num(&self) -> u8 { + $af + } + } + }; +); diff --git a/stm32-metapac/build.rs b/stm32-metapac/build.rs index 046bc11f3..d33693f3c 100644 --- a/stm32-metapac/build.rs +++ b/stm32-metapac/build.rs @@ -135,6 +135,7 @@ fn main() { let mut cfgs: HashSet = HashSet::new(); let mut pin_table: Vec> = Vec::new(); let mut interrupt_table: Vec> = Vec::new(); + let mut peripherals_table: Vec> = Vec::new(); let mut peripheral_pins_table: Vec> = Vec::new(); let dma_base = chip @@ -178,6 +179,10 @@ fn main() { cfgs.insert(bi.module.clone()); cfgs.insert(format!("{}_{}", bi.module, bi.version)); + let mut peripheral_row = Vec::new(); + peripheral_row.push( bi.module.clone() ); + peripheral_row.push( name.clone() ); + peripherals_table.push( peripheral_row ); if let Some(old_version) = peripheral_versions.insert(bi.module.clone(), bi.version.clone()) @@ -253,6 +258,7 @@ fn main() { make_table(&mut extra, "pins", &pin_table); make_table(&mut extra, "interrupts", &interrupt_table); + make_table(&mut extra, "peripherals", &peripherals_table); make_table(&mut extra, "peripheral_versions", &peripheral_version_table); make_table(&mut extra, "peripheral_pins", &peripheral_pins_table); From 00892c7362c7c1d21542828320512f69d5f18ddc Mon Sep 17 00:00:00 2001 From: Bob McWhirter Date: Thu, 3 Jun 2021 11:27:17 -0400 Subject: [PATCH 04/12] Migrate USART to macro tables. --- embassy-stm32/gen.py | 28 ++++++++++----------- embassy-stm32/src/usart/mod.rs | 45 ++++++++++++++++++++++++++-------- 2 files changed, 49 insertions(+), 24 deletions(-) diff --git a/embassy-stm32/gen.py b/embassy-stm32/gen.py index e8645ccf9..55612cb3f 100644 --- a/embassy-stm32/gen.py +++ b/embassy-stm32/gen.py @@ -48,20 +48,20 @@ with open(output_file, 'w') as f: custom_singletons = False - if block_mod == 'usart': - f.write(f'impl_usart!({name});') - for pin, funcs in af.items(): - if pin in pins: - if (func := funcs.get(f'{name}_RX')) != None: - f.write(f'impl_usart_pin!({name}, RxPin, {pin}, {func});') - if (func := funcs.get(f'{name}_TX')) != None: - f.write(f'impl_usart_pin!({name}, TxPin, {pin}, {func});') - if (func := funcs.get(f'{name}_CTS')) != None: - f.write(f'impl_usart_pin!({name}, CtsPin, {pin}, {func});') - if (func := funcs.get(f'{name}_RTS')) != None: - f.write(f'impl_usart_pin!({name}, RtsPin, {pin}, {func});') - if (func := funcs.get(f'{name}_CK')) != None: - f.write(f'impl_usart_pin!({name}, CkPin, {pin}, {func});') + # if block_mod == 'usart': + # f.write(f'impl_usart!({name});') + # for pin, funcs in af.items(): + # if pin in pins: + # if (func := funcs.get(f'{name}_RX')) != None: + # f.write(f'impl_usart_pin!({name}, RxPin, {pin}, {func});') + # if (func := funcs.get(f'{name}_TX')) != None: + # f.write(f'impl_usart_pin!({name}, TxPin, {pin}, {func});') + # if (func := funcs.get(f'{name}_CTS')) != None: + # f.write(f'impl_usart_pin!({name}, CtsPin, {pin}, {func});') + # if (func := funcs.get(f'{name}_RTS')) != None: + # f.write(f'impl_usart_pin!({name}, RtsPin, {pin}, {func});') + # if (func := funcs.get(f'{name}_CK')) != None: + # f.write(f'impl_usart_pin!({name}, CkPin, {pin}, {func});') # if block_mod == 'rng': # for irq in chip['interrupts']: diff --git a/embassy-stm32/src/usart/mod.rs b/embassy-stm32/src/usart/mod.rs index 77b217c3a..adf48c326 100644 --- a/embassy-stm32/src/usart/mod.rs +++ b/embassy-stm32/src/usart/mod.rs @@ -3,6 +3,7 @@ #[cfg_attr(usart_v1, path = "v1.rs")] #[cfg_attr(usart_v2, path = "v2.rs")] mod _version; +use crate::peripherals; pub use _version::*; use crate::gpio::Pin; @@ -51,24 +52,48 @@ pub trait CtsPin: sealed::CtsPin {} pub trait RtsPin: sealed::RtsPin {} pub trait CkPin: sealed::CkPin {} -macro_rules! impl_usart { - ($inst:ident) => { - impl crate::usart::sealed::Instance for peripherals::$inst { +crate::pac::peripherals!( + (usart, $inst:ident) => { + impl sealed::Instance for peripherals::$inst { fn regs(&self) -> crate::pac::usart::Usart { crate::pac::$inst } } - impl crate::usart::Instance for peripherals::$inst {} - }; -} -macro_rules! impl_usart_pin { - ($inst:ident, $func:ident, $pin:ident, $af:expr) => { - impl crate::usart::sealed::$func for peripherals::$pin { + impl Instance for peripherals::$inst {} + }; +); + +macro_rules! impl_pin { + ($inst:ident, $pin:ident, $signal:ident, $af:expr) => { + impl sealed::$signal for peripherals::$pin { fn af_num(&self) -> u8 { $af } } - impl crate::usart::$func for peripherals::$pin {} + + impl $signal for peripherals::$pin {} }; } + +crate::pac::peripheral_pins!( + ($inst:ident, usart, USART, $pin:ident, TX, $af:expr) => { + impl_pin!($inst, $pin, TxPin, $af); + }; + + ($inst:ident, usart, USART, $pin:ident, RX, $af:expr) => { + impl_pin!($inst, $pin, RxPin, $af); + }; + + ($inst:ident, usart, USART, $pin:ident, CTS, $af:expr) => { + impl_pin!($inst, $pin, CtsPin, $af); + }; + + ($inst:ident, usart, USART, $pin:ident, RTS, $af:expr) => { + impl_pin!($inst, $pin, RtsPin, $af); + }; + + ($inst:ident, usart, USART, $pin:ident, CK, $af:expr) => { + impl_pin!($inst, $pin, CkPin, $af); + }; +); From 3dd497c1e63bdf03c064ca58ef15cda51eb17146 Mon Sep 17 00:00:00 2001 From: Bob McWhirter Date: Thu, 3 Jun 2021 11:29:29 -0400 Subject: [PATCH 05/12] Refactor some I2c signal pin macro. --- embassy-stm32/src/i2c/mod.rs | 22 +++++++++++----------- 1 file changed, 11 insertions(+), 11 deletions(-) diff --git a/embassy-stm32/src/i2c/mod.rs b/embassy-stm32/src/i2c/mod.rs index f77e68600..abda196a7 100644 --- a/embassy-stm32/src/i2c/mod.rs +++ b/embassy-stm32/src/i2c/mod.rs @@ -51,24 +51,24 @@ crate::pac::peripherals!( }; ); -crate::pac::peripheral_pins!( - ($inst:ident, i2c, I2C, $pin:ident, SDA, $af:expr) => { - impl SdaPin for peripherals::$pin {} +macro_rules! impl_pin { + ($inst:ident, $pin:ident, $signal:ident, $af:expr) => { + impl $signal for peripherals::$pin {} - impl sealed::SdaPin for peripherals::$pin { + impl sealed::$signal for peripherals::$pin { fn af_num(&self) -> u8 { $af } } }; +} + +crate::pac::peripheral_pins!( + ($inst:ident, i2c, I2C, $pin:ident, SDA, $af:expr) => { + impl_pin!($inst, $pin, SdaPin, $af); + }; ($inst:ident, i2c, I2C, $pin:ident, SCL, $af:expr) => { - impl SclPin for peripherals::$pin {} - - impl sealed::SclPin for peripherals::$pin { - fn af_num(&self) -> u8 { - $af - } - } + impl_pin!($inst, $pin, SclPin, $af); }; ); From c00a85f9a9e1506c23650b27d8db5a1c5b046aa5 Mon Sep 17 00:00:00 2001 From: Bob McWhirter Date: Thu, 3 Jun 2021 11:31:03 -0400 Subject: [PATCH 06/12] Refactor SPI signal pin macro. --- embassy-stm32/src/spi/mod.rs | 30 ++++++++++++------------------ 1 file changed, 12 insertions(+), 18 deletions(-) diff --git a/embassy-stm32/src/spi/mod.rs b/embassy-stm32/src/spi/mod.rs index a888622de..730169ec0 100644 --- a/embassy-stm32/src/spi/mod.rs +++ b/embassy-stm32/src/spi/mod.rs @@ -84,34 +84,28 @@ crate::pac::peripherals!( }; ); -crate::pac::peripheral_pins!( - ($inst:ident, spi, SPI, $pin:ident, SCK, $af:expr) => { - impl SckPin for peripherals::$pin {} +macro_rules! impl_pin { + ($inst:ident, $pin:ident, $signal:ident, $af:expr) => { + impl $signal for peripherals::$pin {} - impl sealed::SckPin for peripherals::$pin { + impl sealed::$signal for peripherals::$pin { fn af_num(&self) -> u8 { $af } } }; +} + +crate::pac::peripheral_pins!( + ($inst:ident, spi, SPI, $pin:ident, SCK, $af:expr) => { + impl_pin!($inst, $pin, SckPin, $af); + }; ($inst:ident, spi, SPI, $pin:ident, MOSI, $af:expr) => { - impl MosiPin for peripherals::$pin {} - - impl sealed::MosiPin for peripherals::$pin { - fn af_num(&self) -> u8 { - $af - } - } + impl_pin!($inst, $pin, MosiPin, $af); }; ($inst:ident, spi, SPI, $pin:ident, MISO, $af:expr) => { - impl MisoPin for peripherals::$pin {} - - impl sealed::MisoPin for peripherals::$pin { - fn af_num(&self) -> u8 { - $af - } - } + impl_pin!($inst, $pin, MisoPin, $af); }; ); From 75dc0fd542b3d9744cc2a0a4b661cb2951a9c2bd Mon Sep 17 00:00:00 2001 From: Bob McWhirter Date: Thu, 3 Jun 2021 13:23:21 -0400 Subject: [PATCH 07/12] Migrate TIM[2-5] to macro tables. --- embassy-stm32/gen.py | 6 +++--- embassy-stm32/src/clock.rs | 14 +++++++++++--- 2 files changed, 14 insertions(+), 6 deletions(-) diff --git a/embassy-stm32/gen.py b/embassy-stm32/gen.py index 55612cb3f..3cf93a2ca 100644 --- a/embassy-stm32/gen.py +++ b/embassy-stm32/gen.py @@ -136,9 +136,9 @@ with open(output_file, 'w') as f: if (func := funcs.get(f'{name}_D7')) != None: f.write(f'impl_sdmmc_pin!({name}, D7Pin, {pin}, {func});') - if block_name == 'TimGp16': - if re.match('TIM[2345]$', name): - f.write(f'impl_timer!({name});') + # if block_name == 'TimGp16': + # if re.match('TIM[2345]$', name): + # f.write(f'impl_timer!({name});') if block_mod == 'exti': for irq in chip['interrupts']: diff --git a/embassy-stm32/src/clock.rs b/embassy-stm32/src/clock.rs index aa83c5b45..694ca666d 100644 --- a/embassy-stm32/src/clock.rs +++ b/embassy-stm32/src/clock.rs @@ -10,6 +10,7 @@ use embassy::time::{Clock as EmbassyClock, TICKS_PER_SECOND}; use crate::interrupt::{CriticalSection, Interrupt, Mutex}; use crate::pac::timer::TimGp16; +use crate::peripherals; use crate::time::Hertz; // Clock timekeeping works with something we call "periods", which are time intervals @@ -362,15 +363,22 @@ pub trait Instance: sealed::Instance + Sized + 'static {} macro_rules! impl_timer { ($inst:ident) => { - impl crate::clock::sealed::Instance for peripherals::$inst { + impl sealed::Instance for peripherals::$inst { type Interrupt = crate::interrupt::$inst; fn inner() -> crate::clock::TimerInner { - const INNER: crate::clock::TimerInner = crate::clock::TimerInner(crate::pac::$inst); + const INNER: TimerInner = TimerInner(crate::pac::$inst); INNER } } - impl crate::clock::Instance for peripherals::$inst {} + impl Instance for peripherals::$inst {} }; } + +crate::pac::peripherals!( + (timer, TIM2) => { impl_timer!(TIM2); }; + (timer, TIM3) => { impl_timer!(TIM3); }; + (timer, TIM4) => { impl_timer!(TIM4); }; + (timer, TIM5) => { impl_timer!(TIM5); }; +); From fe47f781be10264ce1d3b533c1bdbf6508f6b361 Mon Sep 17 00:00:00 2001 From: Bob McWhirter Date: Thu, 3 Jun 2021 13:35:27 -0400 Subject: [PATCH 08/12] Migrate exti_irq stuff to macro tables. --- embassy-stm32/gen.py | 10 +++++----- embassy-stm32/src/exti.rs | 22 ++++++++++++++++++++++ embassy-stm32/src/lib.rs | 2 +- stm32-metapac/build.rs | 24 +++++++++++++++--------- 4 files changed, 43 insertions(+), 15 deletions(-) diff --git a/embassy-stm32/gen.py b/embassy-stm32/gen.py index 3cf93a2ca..7c128c1aa 100644 --- a/embassy-stm32/gen.py +++ b/embassy-stm32/gen.py @@ -140,10 +140,10 @@ with open(output_file, 'w') as f: # if re.match('TIM[2345]$', name): # f.write(f'impl_timer!({name});') - if block_mod == 'exti': - for irq in chip['interrupts']: - if re.match('EXTI', irq): - exti_interrupts.append(irq) + # if block_mod == 'exti': + # for irq in chip['interrupts']: + # if re.match('EXTI', irq): + # exti_interrupts.append(irq) # if block_mod == 'dac': # f.write(f'impl_dac!({name});') @@ -160,4 +160,4 @@ with open(output_file, 'w') as f: f.write(f"embassy_extras::peripherals!({','.join(singletons)});") # ========= exti interrupts - f.write(f"impl_exti_irq!({','.join(exti_interrupts)});") + # f.write(f"impl_exti_irq!({','.join(exti_interrupts)});") diff --git a/embassy-stm32/src/exti.rs b/embassy-stm32/src/exti.rs index 61c28aa7e..a8875fb5d 100644 --- a/embassy-stm32/src/exti.rs +++ b/embassy-stm32/src/exti.rs @@ -214,6 +214,7 @@ impl_exti!(EXTI15, 15); pub(crate) unsafe fn init() {} +/* macro_rules! impl_exti_irq { ($($e:ident),+) => { /// safety: must be called only once @@ -234,3 +235,24 @@ macro_rules! impl_exti_irq { )+ }; } + */ + +/// safety: must be called only once +pub(crate) unsafe fn init_exti() { + use embassy::interrupt::Interrupt; + use embassy::interrupt::InterruptExt; + + crate::pac::exti_interrupts!( + ($e:ident) => { + crate::interrupt::$e::steal().enable(); + }; + ); +} + +crate::pac::exti_interrupts!( + ($e:ident) => { + unsafe fn $e() { + on_irq() + } + }; +); diff --git a/embassy-stm32/src/lib.rs b/embassy-stm32/src/lib.rs index c51f93eb5..54d46c5b9 100644 --- a/embassy-stm32/src/lib.rs +++ b/embassy-stm32/src/lib.rs @@ -83,7 +83,7 @@ pub fn init(config: Config) -> Peripherals { #[cfg(dma)] dma::init(); - generated::init_exti(); + exti::init_exti(); rcc::init(config.rcc); } diff --git a/stm32-metapac/build.rs b/stm32-metapac/build.rs index d33693f3c..b19906ea8 100644 --- a/stm32-metapac/build.rs +++ b/stm32-metapac/build.rs @@ -91,7 +91,7 @@ macro_rules! {} {{ ", name, name ) - .unwrap(); + .unwrap(); for row in data { write!(out, " __{}_inner!(({}));\n", name, row.join(",")).unwrap(); @@ -102,7 +102,7 @@ macro_rules! {} {{ " }}; }}" ) - .unwrap(); + .unwrap(); } fn main() { @@ -176,16 +176,15 @@ fn main() { peripheral_pins_table.push(row); } - cfgs.insert(bi.module.clone()); cfgs.insert(format!("{}_{}", bi.module, bi.version)); let mut peripheral_row = Vec::new(); - peripheral_row.push( bi.module.clone() ); - peripheral_row.push( name.clone() ); - peripherals_table.push( peripheral_row ); + peripheral_row.push(bi.module.clone()); + peripheral_row.push(name.clone()); + peripherals_table.push(peripheral_row); if let Some(old_version) = - peripheral_versions.insert(bi.module.clone(), bi.version.clone()) + peripheral_versions.insert(bi.module.clone(), bi.version.clone()) { if old_version != bi.version { panic!( @@ -256,8 +255,15 @@ fn main() { .map(|(kind, version)| vec![kind.clone(), version.clone()]) .collect(); + let exti_interrupt_table = &interrupt_table + .iter() + .filter(|row| row[0].contains("EXTI")) + .map(|row| row.clone()) + .collect(); + make_table(&mut extra, "pins", &pin_table); make_table(&mut extra, "interrupts", &interrupt_table); + make_table(&mut extra, "exti_interrupts", &exti_interrupt_table); make_table(&mut extra, "peripherals", &peripherals_table); make_table(&mut extra, "peripheral_versions", &peripheral_version_table); make_table(&mut extra, "peripheral_pins", &peripheral_pins_table); @@ -282,7 +288,7 @@ fn main() { transform::NameKind::Enum => format!("{}::vals::{}", prefix, s), _ => s.to_string(), }) - .unwrap(); + .unwrap(); ir.merge(peri); } @@ -311,7 +317,7 @@ fn main() { "PROVIDE({} = DefaultHandler);\n", name.to_ascii_uppercase() ) - .unwrap(); + .unwrap(); } File::create(out.join("device.x")) From 2c722ec0ee8fc53e18f1b6df0b602df1aecfd59b Mon Sep 17 00:00:00 2001 From: Bob McWhirter Date: Thu, 3 Jun 2021 13:50:48 -0400 Subject: [PATCH 09/12] Migrate sdmmc to macro tables. --- embassy-stm32/gen.py | 88 ---------------------------------- embassy-stm32/src/sdmmc/mod.rs | 2 +- embassy-stm32/src/sdmmc/v2.rs | 59 ++++++++++++++++++----- 3 files changed, 49 insertions(+), 100 deletions(-) diff --git a/embassy-stm32/gen.py b/embassy-stm32/gen.py index 7c128c1aa..ddd4a3b88 100644 --- a/embassy-stm32/gen.py +++ b/embassy-stm32/gen.py @@ -48,48 +48,6 @@ with open(output_file, 'w') as f: custom_singletons = False - # if block_mod == 'usart': - # f.write(f'impl_usart!({name});') - # for pin, funcs in af.items(): - # if pin in pins: - # if (func := funcs.get(f'{name}_RX')) != None: - # f.write(f'impl_usart_pin!({name}, RxPin, {pin}, {func});') - # if (func := funcs.get(f'{name}_TX')) != None: - # f.write(f'impl_usart_pin!({name}, TxPin, {pin}, {func});') - # if (func := funcs.get(f'{name}_CTS')) != None: - # f.write(f'impl_usart_pin!({name}, CtsPin, {pin}, {func});') - # if (func := funcs.get(f'{name}_RTS')) != None: - # f.write(f'impl_usart_pin!({name}, RtsPin, {pin}, {func});') - # if (func := funcs.get(f'{name}_CK')) != None: - # f.write(f'impl_usart_pin!({name}, CkPin, {pin}, {func});') - - # if block_mod == 'rng': - # for irq in chip['interrupts']: - # if re.search('RNG', irq): - # f.write(f'impl_rng!({name}, {irq});') - - # if block_mod == 'spi': - # if 'clock' in peri: - # clock = peri['clock'] - # f.write(f'impl_spi!({name}, {clock});') - # for pin, funcs in af.items(): - # if pin in pins: - # if (func := funcs.get(f'{name}_SCK')) != None: - # f.write(f'impl_spi_pin!({name}, SckPin, {pin}, {func});') - # if (func := funcs.get(f'{name}_MOSI')) != None: - # f.write(f'impl_spi_pin!({name}, MosiPin, {pin}, {func});') - # if (func := funcs.get(f'{name}_MISO')) != None: - # f.write(f'impl_spi_pin!({name}, MisoPin, {pin}, {func});') - - # if block_mod == 'i2c': - # f.write(f'impl_i2c!({name});') - # for pin, funcs in af.items(): - # if pin in pins: - # if func := funcs.get(f'{name}_SCL'): - # f.write(f'impl_i2c_pin!({name}, SclPin, {pin}, {func});') - # if func := funcs.get(f'{name}_SDA'): - # f.write(f'impl_i2c_pin!({name}, SdaPin, {pin}, {func});') - if block_mod == 'gpio': custom_singletons = True port = name[4:] @@ -111,53 +69,7 @@ with open(output_file, 'w') as f: f.write(f'impl_dma_channel!({channel}, {dma_num}, {ch_num});') - if peri['block'] == 'sdmmc_v2/SDMMC': - f.write(f'impl_sdmmc!({name});') - for pin, funcs in af.items(): - if pin in pins: - if (func := funcs.get(f'{name}_CK')) != None: - f.write(f'impl_sdmmc_pin!({name}, CkPin, {pin}, {func});') - if (func := funcs.get(f'{name}_CMD')) != None: - f.write(f'impl_sdmmc_pin!({name}, CmdPin, {pin}, {func});') - if (func := funcs.get(f'{name}_D0')) != None: - f.write(f'impl_sdmmc_pin!({name}, D0Pin, {pin}, {func});') - if (func := funcs.get(f'{name}_D1')) != None: - f.write(f'impl_sdmmc_pin!({name}, D1Pin, {pin}, {func});') - if (func := funcs.get(f'{name}_D2')) != None: - f.write(f'impl_sdmmc_pin!({name}, D2Pin, {pin}, {func});') - if (func := funcs.get(f'{name}_D3')) != None: - f.write(f'impl_sdmmc_pin!({name}, D3Pin, {pin}, {func});') - if (func := funcs.get(f'{name}_D4')) != None: - f.write(f'impl_sdmmc_pin!({name}, D4Pin, {pin}, {func});') - if (func := funcs.get(f'{name}_D5')) != None: - f.write(f'impl_sdmmc_pin!({name}, D5Pin, {pin}, {func});') - if (func := funcs.get(f'{name}_D6')) != None: - f.write(f'impl_sdmmc_pin!({name}, D6Pin, {pin}, {func});') - if (func := funcs.get(f'{name}_D7')) != None: - f.write(f'impl_sdmmc_pin!({name}, D7Pin, {pin}, {func});') - - # if block_name == 'TimGp16': - # if re.match('TIM[2345]$', name): - # f.write(f'impl_timer!({name});') - - # if block_mod == 'exti': - # for irq in chip['interrupts']: - # if re.match('EXTI', irq): - # exti_interrupts.append(irq) - - # if block_mod == 'dac': - # f.write(f'impl_dac!({name});') - # if 'dac_out1' in peri: - # pin = peri['dac_out1'] - # f.write(f'impl_dac_pin!({name}, 1, {pin});') - # if 'dac_out2' in peri: - # pin = peri['dac_out2'] - # f.write(f'impl_dac_pin!({name}, 2, {pin});') - # if not custom_singletons: singletons.append(name) f.write(f"embassy_extras::peripherals!({','.join(singletons)});") - - # ========= exti interrupts - # f.write(f"impl_exti_irq!({','.join(exti_interrupts)});") diff --git a/embassy-stm32/src/sdmmc/mod.rs b/embassy-stm32/src/sdmmc/mod.rs index 087cb4c40..9244c22a6 100644 --- a/embassy-stm32/src/sdmmc/mod.rs +++ b/embassy-stm32/src/sdmmc/mod.rs @@ -1,6 +1,6 @@ #![macro_use] -#[cfg_attr(sdmmc_v1, path = "v1.rs")] +//#[cfg_attr(sdmmc_v1, path = "v1.rs")] #[cfg_attr(sdmmc_v2, path = "v2.rs")] mod _version; diff --git a/embassy-stm32/src/sdmmc/v2.rs b/embassy-stm32/src/sdmmc/v2.rs index e432ca9a3..6d21091a9 100644 --- a/embassy-stm32/src/sdmmc/v2.rs +++ b/embassy-stm32/src/sdmmc/v2.rs @@ -15,6 +15,7 @@ use crate::interrupt::Interrupt; use crate::pac; use crate::pac::gpio::Gpio; use crate::pac::sdmmc::Sdmmc as RegBlock; +use crate::peripherals; use crate::time::Hertz; /// The signalling scheme used on the SDMMC bus @@ -1469,12 +1470,12 @@ where } } -macro_rules! impl_sdmmc { - ($inst:ident) => { - impl crate::sdmmc::sealed::Instance for peripherals::$inst { +crate::pac::peripherals!( + (sdmmc, $inst:ident) => { + impl sealed::Instance for peripherals::$inst { type Interrupt = crate::interrupt::$inst; - fn inner() -> crate::sdmmc::SdmmcInner { + fn inner() -> SdmmcInner { const INNER: crate::sdmmc::SdmmcInner = crate::sdmmc::SdmmcInner(crate::pac::$inst); INNER } @@ -1485,20 +1486,56 @@ macro_rules! impl_sdmmc { } } - impl crate::sdmmc::Instance for peripherals::$inst {} + impl Instance for peripherals::$inst {} }; -} +); -macro_rules! impl_sdmmc_pin { - ($inst:ident, $func:ident, $pin:ident, $num:expr) => { - impl crate::sdmmc::sealed::$func for peripherals::$pin { - const AF_NUM: u8 = $num; +macro_rules! impl_pin { + ($inst:ident, $pin:ident, $signal:ident, $af:expr) => { + impl crate::sdmmc::sealed::$signal for peripherals::$pin { + const AF_NUM: u8 = $af; } - impl crate::sdmmc::$func for peripherals::$pin {} + impl crate::sdmmc::$signal for peripherals::$pin {} }; } +crate::pac::peripheral_pins!( + ($inst:ident, sdmmc, SDMMC, $pin:ident, CK, $af:expr) => { + impl_pin!($inst, $pin, CkPin, $af); + }; + ($inst:ident, sdmmc, SDMMC, $pin:ident, CMD, $af:expr) => { + impl_pin!($inst, $pin, CmdPin, $af); + }; + ($inst:ident, sdmmc, SDMMC, $pin:ident, D0, $af:expr) => { + impl_pin!($inst, $pin, D0Pin, $af); + }; + ($inst:ident, sdmmc, SDMMC, $pin:ident, D1, $af:expr) => { + impl_pin!($inst, $pin, D1Pin, $af); + }; + ($inst:ident, sdmmc, SDMMC, $pin:ident, D2, $af:expr) => { + impl_pin!($inst, $pin, D2Pin, $af); + }; + ($inst:ident, sdmmc, SDMMC, $pin:ident, D3, $af:expr) => { + impl_pin!($inst, $pin, D3Pin, $af); + }; + ($inst:ident, sdmmc, SDMMC, $pin:ident, D4, $af:expr) => { + impl_pin!($inst, $pin, D4Pin, $af); + }; + ($inst:ident, sdmmc, SDMMC, $pin:ident, D5, $af:expr) => { + impl_pin!($inst, $pin, D5Pin, $af); + }; + ($inst:ident, sdmmc, SDMMC, $pin:ident, D6, $af:expr) => { + impl_pin!($inst, $pin, D6Pin, $af); + }; + ($inst:ident, sdmmc, SDMMC, $pin:ident, D6, $af:expr) => { + impl_pin!($inst, $pin, D7Pin, $af); + }; + ($inst:ident, sdmmc, SDMMC, $pin:ident, D8, $af:expr) => { + impl_pin!($inst, $pin, D8Pin, $af); + }; +); + #[cfg(feature = "sdmmc-rs")] mod sdmmc_rs { use super::*; From d75bf143eb715db0855dff0bc10650de1b6d5ab9 Mon Sep 17 00:00:00 2001 From: Bob McWhirter Date: Thu, 3 Jun 2021 14:18:58 -0400 Subject: [PATCH 10/12] Remove the exti_interrupts table. --- embassy-stm32/src/exti.rs | 55 ++++++++++++++++++++++++++++++++++----- stm32-metapac/build.rs | 7 ----- 2 files changed, 48 insertions(+), 14 deletions(-) diff --git a/embassy-stm32/src/exti.rs b/embassy-stm32/src/exti.rs index a8875fb5d..0ee93bede 100644 --- a/embassy-stm32/src/exti.rs +++ b/embassy-stm32/src/exti.rs @@ -237,22 +237,63 @@ macro_rules! impl_exti_irq { } */ +macro_rules! foreach_exti_irq { + ($action:ident) => { + crate::pac::interrupts!( + (EXTI0) => { $action!(EXTI0); }; + (EXTI1) => { $action!(EXTI1); }; + (EXTI2) => { $action!(EXTI2); }; + (EXTI3) => { $action!(EXTI3); }; + (EXTI4) => { $action!(EXTI4); }; + (EXTI5) => { $action!(EXTI5); }; + (EXTI6) => { $action!(EXTI6); }; + (EXTI7) => { $action!(EXTI7); }; + (EXTI8) => { $action!(EXTI8); }; + (EXTI9) => { $action!(EXTI9); }; + (EXTI10) => { $action!(EXTI10); }; + (EXTI11) => { $action!(EXTI11); }; + (EXTI12) => { $action!(EXTI12); }; + (EXTI13) => { $action!(EXTI13); }; + (EXTI14) => { $action!(EXTI14); }; + (EXTI15) => { $action!(EXTI15); }; + + // plus the weird ones + (EXTI0_1) => { $action!( EXTI0_1 ); }; + (EXTI15_10) => { $action!(EXTI15_10); }; + (EXTI15_4) => { $action!(EXTI15_4); }; + (EXTI1_0) => { $action!(EXTI1_0); }; + (EXTI2_3) => { $action!(EXTI2_3); }; + (EXTI2_TSC) => { $action!(EXTI2_TSC); }; + (EXTI3_2) => { $action!(EXTI3_2); }; + (EXTI4_15) => { $action!(EXTI4_15); }; + (EXTI9_5) => { $action!(EXTI9_5); }; + ); + }; +} + +macro_rules! enable_irq { + ($e:ident) => { + crate::interrupt::$e::steal().enable(); + }; +} + /// safety: must be called only once pub(crate) unsafe fn init_exti() { use embassy::interrupt::Interrupt; use embassy::interrupt::InterruptExt; - crate::pac::exti_interrupts!( - ($e:ident) => { - crate::interrupt::$e::steal().enable(); - }; - ); + foreach_exti_irq!(enable_irq); } -crate::pac::exti_interrupts!( +use crate::interrupt; + +macro_rules! impl_irq { ($e:ident) => { + #[interrupt] unsafe fn $e() { on_irq() } }; -); +} + +foreach_exti_irq!(impl_irq); diff --git a/stm32-metapac/build.rs b/stm32-metapac/build.rs index b19906ea8..ef95f3313 100644 --- a/stm32-metapac/build.rs +++ b/stm32-metapac/build.rs @@ -255,15 +255,8 @@ fn main() { .map(|(kind, version)| vec![kind.clone(), version.clone()]) .collect(); - let exti_interrupt_table = &interrupt_table - .iter() - .filter(|row| row[0].contains("EXTI")) - .map(|row| row.clone()) - .collect(); - make_table(&mut extra, "pins", &pin_table); make_table(&mut extra, "interrupts", &interrupt_table); - make_table(&mut extra, "exti_interrupts", &exti_interrupt_table); make_table(&mut extra, "peripherals", &peripherals_table); make_table(&mut extra, "peripheral_versions", &peripheral_version_table); make_table(&mut extra, "peripheral_pins", &peripheral_pins_table); From 240616aa7238941afb0c663e3adfab3950257538 Mon Sep 17 00:00:00 2001 From: Bob McWhirter Date: Thu, 3 Jun 2021 14:25:17 -0400 Subject: [PATCH 11/12] General clean-up and removal of dead code. --- embassy-stm32/src/exti.rs | 27 +-------------------------- embassy-stm32/src/i2c/mod.rs | 4 ++-- embassy-stm32/src/lib.rs | 2 +- embassy-stm32/src/rng.rs | 28 ---------------------------- embassy-stm32/src/sdmmc/v2.rs | 6 +++--- 5 files changed, 7 insertions(+), 60 deletions(-) diff --git a/embassy-stm32/src/exti.rs b/embassy-stm32/src/exti.rs index 0ee93bede..90d4afd56 100644 --- a/embassy-stm32/src/exti.rs +++ b/embassy-stm32/src/exti.rs @@ -212,31 +212,6 @@ impl_exti!(EXTI13, 13); impl_exti!(EXTI14, 14); impl_exti!(EXTI15, 15); -pub(crate) unsafe fn init() {} - -/* -macro_rules! impl_exti_irq { - ($($e:ident),+) => { - /// safety: must be called only once - pub(crate) unsafe fn init_exti() { - use embassy::interrupt::Interrupt; - use embassy::interrupt::InterruptExt; - - $( - crate::interrupt::$e::steal().enable(); - )+ - } - - $( - #[crate::interrupt] - unsafe fn $e() { - crate::exti::on_irq() - } - )+ - }; -} - */ - macro_rules! foreach_exti_irq { ($action:ident) => { crate::pac::interrupts!( @@ -278,7 +253,7 @@ macro_rules! enable_irq { } /// safety: must be called only once -pub(crate) unsafe fn init_exti() { +pub(crate) unsafe fn init() { use embassy::interrupt::Interrupt; use embassy::interrupt::InterruptExt; diff --git a/embassy-stm32/src/i2c/mod.rs b/embassy-stm32/src/i2c/mod.rs index abda196a7..0f9414c53 100644 --- a/embassy-stm32/src/i2c/mod.rs +++ b/embassy-stm32/src/i2c/mod.rs @@ -40,13 +40,13 @@ pub trait SdaPin: sealed::SdaPin + 'static {} crate::pac::peripherals!( (i2c, $inst:ident) => { - impl crate::i2c::sealed::Instance for peripherals::$inst { + impl sealed::Instance for peripherals::$inst { fn regs() -> &'static crate::pac::i2c::I2c { &crate::pac::$inst } } - impl crate::i2c::Instance for peripherals::$inst {} + impl Instance for peripherals::$inst {} }; ); diff --git a/embassy-stm32/src/lib.rs b/embassy-stm32/src/lib.rs index 54d46c5b9..c205bc49d 100644 --- a/embassy-stm32/src/lib.rs +++ b/embassy-stm32/src/lib.rs @@ -83,7 +83,7 @@ pub fn init(config: Config) -> Peripherals { #[cfg(dma)] dma::init(); - exti::init_exti(); + exti::init(); rcc::init(config.rcc); } diff --git a/embassy-stm32/src/rng.rs b/embassy-stm32/src/rng.rs index 4dd26c671..704f1a97b 100644 --- a/embassy-stm32/src/rng.rs +++ b/embassy-stm32/src/rng.rs @@ -185,31 +185,3 @@ crate::pac::interrupts!( irq!(HASH_RNG); }; ); - -/* -macro_rules! impl_rng { - ($inst:ident, $irq:ident) => { - impl crate::rng::sealed::Instance for peripherals::RNG { - fn regs() -> crate::pac::rng::Rng { - crate::pac::RNG - } - } - - impl crate::rng::Instance for peripherals::RNG {} - - mod rng_irq { - use crate::interrupt; - - #[interrupt] - unsafe fn $irq() { - let bits = $crate::pac::RNG.sr().read(); - if bits.drdy() || bits.seis() || bits.ceis() { - $crate::pac::RNG.cr().write(|reg| reg.set_ie(false)); - $crate::rng::RNG_WAKER.wake(); - } - } - } - }; -} - - */ diff --git a/embassy-stm32/src/sdmmc/v2.rs b/embassy-stm32/src/sdmmc/v2.rs index 6d21091a9..2c7f8ac00 100644 --- a/embassy-stm32/src/sdmmc/v2.rs +++ b/embassy-stm32/src/sdmmc/v2.rs @@ -1476,7 +1476,7 @@ crate::pac::peripherals!( type Interrupt = crate::interrupt::$inst; fn inner() -> SdmmcInner { - const INNER: crate::sdmmc::SdmmcInner = crate::sdmmc::SdmmcInner(crate::pac::$inst); + const INNER: SdmmcInner = SdmmcInner(crate::pac::$inst); INNER } @@ -1492,11 +1492,11 @@ crate::pac::peripherals!( macro_rules! impl_pin { ($inst:ident, $pin:ident, $signal:ident, $af:expr) => { - impl crate::sdmmc::sealed::$signal for peripherals::$pin { + impl sealed::$signal for peripherals::$pin { const AF_NUM: u8 = $af; } - impl crate::sdmmc::$signal for peripherals::$pin {} + impl $signal for peripherals::$pin {} }; } From b4dca64e206dfc01fde98b6b4cc2b0a05245bc0d Mon Sep 17 00:00:00 2001 From: Bob McWhirter Date: Thu, 3 Jun 2021 14:53:48 -0400 Subject: [PATCH 12/12] Move most of DMA out of gen.py. --- embassy-stm32/gen.py | 5 ----- embassy-stm32/src/dma/mod.rs | 29 +++++++++++++++++++++++++++-- 2 files changed, 27 insertions(+), 7 deletions(-) diff --git a/embassy-stm32/gen.py b/embassy-stm32/gen.py index ddd4a3b88..2736cd69d 100644 --- a/embassy-stm32/gen.py +++ b/embassy-stm32/gen.py @@ -60,15 +60,10 @@ with open(output_file, 'w') as f: if block_mod == 'dma': custom_singletons = True - num_dmas += 1 - dma_num = int(name[3:])-1 # substract 1 because we want DMA1=0, DMA2=1 - for ch_num in range(8): channel = f'{name}_CH{ch_num}' singletons.append(channel) - f.write(f'impl_dma_channel!({channel}, {dma_num}, {ch_num});') - if not custom_singletons: singletons.append(name) diff --git a/embassy-stm32/src/dma/mod.rs b/embassy-stm32/src/dma/mod.rs index aef31ce74..773cdc8b8 100644 --- a/embassy-stm32/src/dma/mod.rs +++ b/embassy-stm32/src/dma/mod.rs @@ -8,6 +8,7 @@ mod _version; pub use _version::*; use crate::pac; +use crate::peripherals; pub(crate) mod sealed { use super::*; @@ -31,8 +32,8 @@ pub trait Channel: sealed::Channel + Sized {} macro_rules! impl_dma_channel { ($type:ident, $dma_num:expr, $ch_num:expr) => { - impl crate::dma::Channel for peripherals::$type {} - impl crate::dma::sealed::Channel for peripherals::$type { + impl Channel for peripherals::$type {} + impl sealed::Channel for peripherals::$type { #[inline] fn num(&self) -> u8 { $dma_num * 8 + $ch_num @@ -40,3 +41,27 @@ macro_rules! impl_dma_channel { } }; } + +crate::pac::peripherals!( + (dma,DMA1) => { + impl_dma_channel!(DMA1_CH0, 0, 0); + impl_dma_channel!(DMA1_CH1, 0, 1); + impl_dma_channel!(DMA1_CH2, 0, 2); + impl_dma_channel!(DMA1_CH3, 0, 3); + impl_dma_channel!(DMA1_CH4, 0, 4); + impl_dma_channel!(DMA1_CH5, 0, 5); + impl_dma_channel!(DMA1_CH6, 0, 6); + impl_dma_channel!(DMA1_CH7, 0, 7); + }; + + (dma,DMA2) => { + impl_dma_channel!(DMA2_CH0, 1, 0); + impl_dma_channel!(DMA2_CH1, 1, 1); + impl_dma_channel!(DMA2_CH2, 1, 2); + impl_dma_channel!(DMA2_CH3, 1, 3); + impl_dma_channel!(DMA2_CH4, 1, 4); + impl_dma_channel!(DMA2_CH5, 1, 5); + impl_dma_channel!(DMA2_CH6, 1, 6); + impl_dma_channel!(DMA2_CH7, 1, 7); + }; +);