601: [part 1/n] Change macrotables to build.rs codegen r=lulf a=Dirbaio

This PR replaces the "macrotables" (the macros like `stm32_data::peripherals!`) with a `const METADATA`.

Macrotables had some problems:

- Hard to debug
- Somewhat footgunny (typo the "pattern" and then nothing matches and the macro now expands to nothing, silently!)
- Limited power
  - Can't count, so we had to add a [special macrotable for that](f50f3f0a73/embassy-stm32/src/dma/bdma.rs (L26)).
  - Can't remove duplicates, so we had to fallback to [Rust code in build.rs](f50f3f0a73/embassy-stm32/build.rs (L105-L145))
  - Can't include the results as a listto another macro, so again [build.rs](https://github.com/embassy-rs/embassy/blob/master/embassy-stm32/build.rs#L100-L101).

They work fine for the 95% of cases, but for the remaining 5% we need Rust code in build.rs. So we might as well do everything with Rust code, so everything is consistent.

The new approach generates a `const METADATA: Metadata = Metadata { ... }` with [these structs](https://github.com/embassy-rs/embassy/blob/unmacrotablize/stm32-metapac-gen/src/assets/metadata.rs) in `stm32-metapac`. `build.rs` can then read that and generate whatever code.


Co-authored-by: Dario Nieuwenhuis <dirbaio@dirbaio.net>
This commit is contained in:
bors[bot]
2022-02-09 15:27:35 +00:00
committed by GitHub
12 changed files with 359 additions and 235 deletions

View File

@ -9,7 +9,6 @@ use embassy::waitqueue::AtomicWaker;
use crate::dma::Request;
use crate::pac;
use crate::pac::bdma::vals;
use crate::rcc::sealed::RccPeripheral;
use super::{Word, WordSize};
@ -77,11 +76,7 @@ pub(crate) unsafe fn init() {
crate::interrupt::$irq::steal().enable();
};
}
pac::peripherals! {
(bdma, $peri:ident) => {
crate::peripherals::$peri::enable();
};
}
crate::generated::init_bdma();
}
pac::dma_channels! {

View File

@ -7,7 +7,6 @@ use embassy::waitqueue::AtomicWaker;
use crate::interrupt;
use crate::pac;
use crate::pac::dma::{regs, vals};
use crate::rcc::sealed::RccPeripheral;
use super::{Request, Word, WordSize};
@ -74,11 +73,7 @@ pub(crate) unsafe fn init() {
interrupt::$irq::steal().enable();
};
}
pac::peripherals! {
(dma, $peri:ident) => {
crate::peripherals::$peri::enable();
};
}
crate::generated::init_dma();
}
pac::dma_channels! {

View File

@ -49,11 +49,5 @@ pac::dma_channels! {
/// safety: must be called only once
pub(crate) unsafe fn init() {
crate::pac::peripheral_rcc! {
($name:ident, dmamux, DMAMUX, $clock:ident, ($reg:ident, $field:ident, $set_field:ident), $rst:tt) => {
crate::pac::RCC.$reg().modify(|reg| {
reg.$set_field(true);
});
};
}
crate::generated::init_dmamux();
}

View File

@ -608,13 +608,7 @@ crate::pac::pins!(
);
pub(crate) unsafe fn init() {
crate::pac::peripheral_rcc! {
($name:ident, gpio, GPIO, $clock:ident, ($reg:ident, $field:ident, $set_field:ident), $rst:tt) => {
crate::pac::RCC.$reg().modify(|reg| {
reg.$set_field(true);
});
};
}
crate::generated::init_gpio();
}
mod eh02 {

View File

@ -3,11 +3,4 @@ pub use critical_section::CriticalSection;
pub use embassy::interrupt::{take, Interrupt};
pub use embassy_hal_common::interrupt::Priority4 as Priority;
use crate::pac::Interrupt as InterruptEnum;
use embassy::interrupt::declare;
crate::pac::interrupts!(
($name:ident) => {
declare!($name);
};
);
pub use crate::generated::interrupt::*;

View File

@ -65,8 +65,6 @@ mod generated {
#![allow(unused_imports)]
#![allow(non_snake_case)]
use crate::interrupt;
include!(concat!(env!("OUT_DIR"), "/generated.rs"));
}
pub use embassy_macros::interrupt;

View File

@ -1,6 +1,5 @@
#![macro_use]
use crate::peripherals;
use crate::time::Hertz;
use core::mem::MaybeUninit;
@ -104,66 +103,3 @@ pub(crate) mod sealed {
}
pub trait RccPeripheral: sealed::RccPeripheral + 'static {}
crate::pac::peripheral_rcc!(
($inst:ident, gpio, GPIO, $clk:ident, $en:tt, $rst:tt) => {};
($inst:ident, $module:ident, $block:ident, $clk:ident, ($en_reg:ident, $en_field:ident, $en_set_field:ident), ($rst_reg:ident, $rst_field:ident, $rst_set_field:ident)) => {
impl sealed::RccPeripheral for peripherals::$inst {
fn frequency() -> crate::time::Hertz {
critical_section::with(|_| {
unsafe { get_freqs().$clk }
})
}
fn enable() {
critical_section::with(|_| {
unsafe {
crate::pac::RCC.$en_reg().modify(|w| w.$en_set_field(true));
}
})
}
fn disable() {
critical_section::with(|_| {
unsafe {
crate::pac::RCC.$en_reg().modify(|w| w.$en_set_field(false));
}
})
}
fn reset() {
critical_section::with(|_| {
unsafe {
crate::pac::RCC.$rst_reg().modify(|w| w.$rst_set_field(true));
crate::pac::RCC.$rst_reg().modify(|w| w.$rst_set_field(false));
}
})
}
}
impl RccPeripheral for peripherals::$inst {}
};
($inst:ident, $module:ident, $block:ident, $clk:ident, ($en_reg:ident, $en_field:ident, $en_set_field:ident), _) => {
impl sealed::RccPeripheral for peripherals::$inst {
fn frequency() -> crate::time::Hertz {
critical_section::with(|_| {
unsafe { get_freqs().$clk }
})
}
fn enable() {
critical_section::with(|_| {
unsafe {
crate::pac::RCC.$en_reg().modify(|w| w.$en_set_field(true));
}
})
}
fn disable() {
critical_section::with(|_| {
unsafe {
crate::pac::RCC.$en_reg().modify(|w| w.$en_set_field(false));
}
})
}
fn reset() {}
}
impl RccPeripheral for peripherals::$inst {}
};
);