stm32: replace peripheral_rcc! macrotable with build.rs

This commit is contained in:
Dario Nieuwenhuis 2022-02-09 00:58:17 +01:00
parent d1a9680422
commit 8160af6af9
7 changed files with 89 additions and 120 deletions

View File

@ -145,6 +145,91 @@ fn main() {
)* )*
}); });
// ========
// Generate RccPeripheral impls
for p in METADATA.peripherals {
if !singletons.contains(&p.name.to_string()) {
continue;
}
if let Some(rcc) = &p.rcc {
let en = rcc.enable.as_ref().unwrap();
let rst = match &rcc.reset {
Some(rst) => {
let rst_reg = format_ident!("{}", rst.register.to_ascii_lowercase());
let set_rst_field = format_ident!("set_{}", rst.field.to_ascii_lowercase());
quote! {
critical_section::with(|_| unsafe {
crate::pac::RCC.#rst_reg().modify(|w| w.#set_rst_field(true));
crate::pac::RCC.#rst_reg().modify(|w| w.#set_rst_field(false));
});
}
}
None => TokenStream::new(),
};
let pname = format_ident!("{}", p.name);
let clk = format_ident!("{}", rcc.clock.to_ascii_lowercase());
let en_reg = format_ident!("{}", en.register.to_ascii_lowercase());
let set_en_field = format_ident!("set_{}", en.field.to_ascii_lowercase());
g.extend(quote! {
impl crate::rcc::sealed::RccPeripheral for peripherals::#pname {
fn frequency() -> crate::time::Hertz {
critical_section::with(|_| unsafe {
crate::rcc::get_freqs().#clk
})
}
fn enable() {
critical_section::with(|_| unsafe {
crate::pac::RCC.#en_reg().modify(|w| w.#set_en_field(true))
})
}
fn disable() {
critical_section::with(|_| unsafe {
crate::pac::RCC.#en_reg().modify(|w| w.#set_en_field(false));
})
}
fn reset() {
#rst
}
}
impl crate::rcc::RccPeripheral for peripherals::#pname {}
});
}
}
// ========
// Generate fns to enable GPIO, DMA in RCC
for kind in ["dma", "bdma", "dmamux", "gpio"] {
let mut gg = TokenStream::new();
for p in METADATA.peripherals {
if p.registers.is_some() && p.registers.as_ref().unwrap().kind == kind {
if let Some(rcc) = &p.rcc {
let en = rcc.enable.as_ref().unwrap();
let en_reg = format_ident!("{}", en.register.to_ascii_lowercase());
let set_en_field = format_ident!("set_{}", en.field.to_ascii_lowercase());
gg.extend(quote! {
crate::pac::RCC.#en_reg().modify(|w| w.#set_en_field(true));
})
}
}
}
let fname = format_ident!("init_{}", kind);
g.extend(quote! {
pub unsafe fn #fname(){
#gg
}
})
}
// ======== // ========
// Write generated.rs // Write generated.rs

View File

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

View File

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

View File

@ -49,11 +49,5 @@ pac::dma_channels! {
/// safety: must be called only once /// safety: must be called only once
pub(crate) unsafe fn init() { pub(crate) unsafe fn init() {
crate::pac::peripheral_rcc! { crate::generated::init_dmamux();
($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);
});
};
}
} }

View File

@ -608,13 +608,7 @@ crate::pac::pins!(
); );
pub(crate) unsafe fn init() { pub(crate) unsafe fn init() {
crate::pac::peripheral_rcc! { crate::generated::init_gpio();
($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);
});
};
}
} }
mod eh02 { mod eh02 {

View File

@ -1,6 +1,5 @@
#![macro_use] #![macro_use]
use crate::peripherals;
use crate::time::Hertz; use crate::time::Hertz;
use core::mem::MaybeUninit; use core::mem::MaybeUninit;
@ -104,66 +103,3 @@ pub(crate) mod sealed {
} }
pub trait RccPeripheral: sealed::RccPeripheral + 'static {} 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 {}
};
);

View File

@ -126,7 +126,6 @@ pub fn gen_chip(
let mut interrupt_table: Vec<Vec<String>> = Vec::new(); let mut interrupt_table: Vec<Vec<String>> = Vec::new();
let mut peripherals_table: Vec<Vec<String>> = Vec::new(); let mut peripherals_table: Vec<Vec<String>> = Vec::new();
let mut peripheral_pins_table: Vec<Vec<String>> = Vec::new(); let mut peripheral_pins_table: Vec<Vec<String>> = Vec::new();
let mut peripheral_rcc_table: Vec<Vec<String>> = Vec::new();
let mut dma_channels_table: Vec<Vec<String>> = Vec::new(); let mut dma_channels_table: Vec<Vec<String>> = Vec::new();
let mut peripheral_dma_channels_table: Vec<Vec<String>> = Vec::new(); let mut peripheral_dma_channels_table: Vec<Vec<String>> = Vec::new();
let mut peripheral_counts: BTreeMap<String, u8> = BTreeMap::new(); let mut peripheral_counts: BTreeMap<String, u8> = BTreeMap::new();
@ -264,34 +263,6 @@ pub fn gen_chip(
} }
_ => {} _ => {}
} }
if let Some(rcc) = &p.rcc {
let mut clock = rcc.clock.to_ascii_lowercase();
if p.name.starts_with("TIM") {
clock = format!("{}_tim", clock)
}
let mut row = Vec::new();
row.push(p.name.clone());
row.push(bi.kind.clone());
row.push(bi.block.clone());
row.push(clock);
for reg in [&rcc.enable, &rcc.reset] {
if let Some(reg) = reg {
row.push(format!(
"({}, {}, set_{})",
reg.register.to_ascii_lowercase(),
reg.field.to_ascii_lowercase(),
reg.field.to_ascii_lowercase()
));
} else {
row.push("_".to_string())
}
}
peripheral_rcc_table.push(row);
}
} }
dev.peripherals.push(ir_peri); dev.peripherals.push(ir_peri);
@ -422,7 +393,6 @@ pub fn gen_chip(
"peripheral_dma_channels", "peripheral_dma_channels",
&peripheral_dma_channels_table, &peripheral_dma_channels_table,
); );
make_table(&mut data, "peripheral_rcc", &peripheral_rcc_table);
make_table(&mut data, "dma_channels", &dma_channels_table); make_table(&mut data, "dma_channels", &dma_channels_table);
make_table(&mut data, "dbgmcu", &dbgmcu_table); make_table(&mut data, "dbgmcu", &dbgmcu_table);
make_peripheral_counts(&mut data, &peripheral_counts); make_peripheral_counts(&mut data, &peripheral_counts);