stm32: move macrotables to embassy-stm32 build.rs

This commit is contained in:
Dario Nieuwenhuis 2022-02-26 01:40:43 +01:00
parent e6299549a0
commit dd828a7a92
21 changed files with 212 additions and 96 deletions

View File

@ -2,6 +2,7 @@ use proc_macro2::TokenStream;
use quote::{format_ident, quote};
use std::collections::{HashMap, HashSet};
use std::env;
use std::fmt::Write as _;
use std::fs;
use std::path::PathBuf;
use stm32_metapac::metadata::METADATA;
@ -530,9 +531,127 @@ fn main() {
}
// ========
// Write generated.rs
// Write foreach_foo! macrotables
let mut interrupts_table: Vec<Vec<String>> = Vec::new();
let mut peripherals_table: Vec<Vec<String>> = Vec::new();
let mut pins_table: Vec<Vec<String>> = Vec::new();
let mut dma_channels_table: Vec<Vec<String>> = Vec::new();
let gpio_base = METADATA
.peripherals
.iter()
.find(|p| p.name == "GPIOA")
.unwrap()
.address as u32;
let gpio_stride = 0x400;
for p in METADATA.peripherals {
if let Some(regs) = &p.registers {
if regs.kind == "gpio" {
let port_letter = p.name.chars().skip(4).next().unwrap();
assert_eq!(0, (p.address as u32 - gpio_base) % gpio_stride);
let port_num = (p.address as u32 - gpio_base) / gpio_stride;
for pin_num in 0u32..16 {
let pin_name = format!("P{}{}", port_letter, pin_num);
pins_table.push(vec![
pin_name,
p.name.to_string(),
port_num.to_string(),
pin_num.to_string(),
format!("EXTI{}", pin_num),
]);
}
}
for irq in p.interrupts {
let mut row = Vec::new();
row.push(p.name.to_string());
row.push(regs.kind.to_string());
row.push(regs.block.to_string());
row.push(irq.signal.to_string());
row.push(irq.interrupt.to_ascii_uppercase());
interrupts_table.push(row)
}
let mut row = Vec::new();
row.push(regs.kind.to_string());
row.push(p.name.to_string());
peripherals_table.push(row);
}
}
let mut dma_channel_count: usize = 0;
let mut bdma_channel_count: usize = 0;
for ch in METADATA.dma_channels {
let mut row = Vec::new();
let dma_peri = METADATA
.peripherals
.iter()
.find(|p| p.name == ch.dma)
.unwrap();
let bi = dma_peri.registers.as_ref().unwrap();
let num;
match bi.kind {
"dma" => {
num = dma_channel_count;
dma_channel_count += 1;
}
"bdma" => {
num = bdma_channel_count;
bdma_channel_count += 1;
}
_ => panic!("bad dma channel kind {}", bi.kind),
}
row.push(ch.name.to_string());
row.push(ch.dma.to_string());
row.push(bi.kind.to_string());
row.push(ch.channel.to_string());
row.push(num.to_string());
if let Some(dmamux) = &ch.dmamux {
let dmamux_channel = ch.dmamux_channel.unwrap();
row.push(format!(
"{{dmamux: {}, dmamux_channel: {}}}",
dmamux, dmamux_channel
));
} else {
row.push("{}".to_string());
}
dma_channels_table.push(row);
}
g.extend(quote! {
pub(crate) const DMA_CHANNEL_COUNT: usize = #dma_channel_count;
pub(crate) const BDMA_CHANNEL_COUNT: usize = #bdma_channel_count;
});
for irq in METADATA.interrupts {
let name = irq.name.to_ascii_uppercase();
interrupts_table.push(vec![name.clone()]);
if name.contains("EXTI") {
interrupts_table.push(vec!["EXTI".to_string(), name.clone()]);
}
}
let mut m = String::new();
make_table(&mut m, "foreach_interrupt", &interrupts_table);
make_table(&mut m, "foreach_peripheral", &peripherals_table);
make_table(&mut m, "foreach_pin", &pins_table);
make_table(&mut m, "foreach_dma_channel", &dma_channels_table);
let out_dir = &PathBuf::from(env::var_os("OUT_DIR").unwrap());
let out_file = out_dir.join("macros.rs").to_string_lossy().to_string();
fs::write(out_file, m).unwrap();
// ========
// Write generated.rs
let out_file = out_dir.join("generated.rs").to_string_lossy().to_string();
fs::write(out_file, g.to_string()).unwrap();
@ -644,3 +763,30 @@ impl<T: Iterator> IteratorExt for T {
}
}
}
fn make_table(out: &mut String, name: &str, data: &Vec<Vec<String>>) {
write!(
out,
"#[macro_export]
macro_rules! {} {{
($($pat:tt => $code:tt;)*) => {{
macro_rules! __{}_inner {{
$(($pat) => $code;)*
($_:tt) => {{}}
}}
",
name, name
)
.unwrap();
for row in data {
write!(out, " __{}_inner!(({}));\n", name, row.join(",")).unwrap();
}
write!(
out,
" }};
}}"
)
.unwrap();
}

View File

@ -36,7 +36,7 @@ pub trait Instance: sealed::Instance + crate::rcc::RccPeripheral + 'static {}
pub trait Common: sealed::Common + 'static {}
pub trait AdcPin<T: Instance>: sealed::AdcPin<T> {}
crate::pac::peripherals!(
foreach_peripheral!(
(adc, $inst:ident) => {
impl crate::adc::sealed::Instance for peripherals::$inst {
fn regs() -> &'static crate::pac::adc::Adc {
@ -44,7 +44,7 @@ crate::pac::peripherals!(
}
#[cfg(not(adc_f1))]
fn common_regs() -> &'static crate::pac::adccommon::AdcCommon {
crate::pac::peripherals!{
foreach_peripheral!{
(adccommon, $common_inst:ident) => {
return &crate::pac::$common_inst
};
@ -57,7 +57,7 @@ crate::pac::peripherals!(
);
#[cfg(not(adc_f1))]
crate::pac::peripherals!(
foreach_peripheral!(
(adccommon, $inst:ident) => {
impl sealed::Common for peripherals::$inst {
fn regs() -> &'static crate::pac::adccommon::AdcCommon {

View File

@ -70,7 +70,7 @@ pub(crate) mod sealed {
pub trait Instance: sealed::Instance + RccPeripheral {}
crate::pac::peripherals!(
foreach_peripheral!(
(can, $inst:ident) => {
impl sealed::Instance for peripherals::$inst {
fn regs() -> &'static crate::pac::can::Can {
@ -86,7 +86,7 @@ crate::pac::peripherals!(
};
);
crate::pac::peripherals!(
foreach_peripheral!(
(can, CAN) => {
unsafe impl bxcan::FilterOwner for peripherals::CAN {
const NUM_FILTER_BANKS: u8 = 14;

View File

@ -16,7 +16,7 @@ pub trait Instance: sealed::Instance + 'static {}
pub trait DacPin<T: Instance, const C: u8>: crate::gpio::Pin + 'static {}
crate::pac::peripherals!(
foreach_peripheral!(
(dac, $inst:ident) => {
impl crate::dac::sealed::Instance for peripherals::$inst {
fn regs() -> &'static crate::pac::dac::Dac {

View File

@ -474,7 +474,7 @@ macro_rules! impl_peripheral {
};
}
crate::pac::interrupts! {
foreach_interrupt! {
($inst:ident, dcmi, $block:ident, GLOBAL, $irq:ident) => {
impl_peripheral!($inst, $irq);
};

View File

@ -7,6 +7,7 @@ use embassy::interrupt::{Interrupt, InterruptExt};
use embassy::waitqueue::AtomicWaker;
use crate::dma::Request;
use crate::generated::BDMA_CHANNEL_COUNT;
use crate::pac;
use crate::pac::bdma::vals;
@ -22,62 +23,44 @@ impl From<WordSize> for vals::Size {
}
}
const CH_COUNT: usize = pac::peripheral_count!(bdma) * 8;
struct State {
ch_wakers: [AtomicWaker; CH_COUNT],
ch_wakers: [AtomicWaker; BDMA_CHANNEL_COUNT],
}
impl State {
const fn new() -> Self {
const AW: AtomicWaker = AtomicWaker::new();
Self {
ch_wakers: [AW; CH_COUNT],
ch_wakers: [AW; BDMA_CHANNEL_COUNT],
}
}
}
static STATE: State = State::new();
macro_rules! dma_num {
(DMA1) => {
0
};
(DMA2) => {
1
};
(BDMA) => {
0
};
(BDMA1) => {
0
};
(BDMA2) => {
1
};
}
pub(crate) unsafe fn on_irq() {
pac::peripherals! {
foreach_peripheral! {
(bdma, BDMA1) => {
// BDMA1 in H7 doesn't use DMAMUX, which breaks
};
(bdma, $dma:ident) => {
let isr = pac::$dma.isr().read();
let dman = dma_num!($dma);
for chn in 0..pac::dma_channels_count!($dma) {
let cr = pac::$dma.ch(chn).cr();
if isr.tcif(chn) && cr.read().tcie() {
let isr = pac::$dma.isr().read();
foreach_dma_channel! {
($channel_peri:ident, $dma, bdma, $channel_num:expr, $index:expr, $dmamux:tt) => {
let cr = pac::$dma.ch($channel_num).cr();
if isr.tcif($channel_num) && cr.read().tcie() {
cr.write(|_| ()); // Disable channel interrupts with the default value.
let n = dma_num!($dma) * 8 + chn;
STATE.ch_wakers[n].wake();
STATE.ch_wakers[$index].wake();
}
}
};
}
};
}
}
/// safety: must be called only once
pub(crate) unsafe fn init() {
pac::interrupts! {
foreach_interrupt! {
($peri:ident, bdma, $block:ident, $signal_name:ident, $irq:ident) => {
crate::interrupt::$irq::steal().enable();
};
@ -85,20 +68,20 @@ pub(crate) unsafe fn init() {
crate::generated::init_bdma();
}
pac::dma_channels! {
($channel_peri:ident, BDMA1, bdma, $channel_num:expr, $dmamux:tt) => {
foreach_dma_channel! {
($channel_peri:ident, BDMA1, bdma, $channel_num:expr, $index:expr, $dmamux:tt) => {
// BDMA1 in H7 doesn't use DMAMUX, which breaks
};
($channel_peri:ident, $dma_peri:ident, bdma, $channel_num:expr, $dmamux:tt) => {
($channel_peri:ident, $dma_peri:ident, bdma, $channel_num:expr, $index:expr, $dmamux:tt) => {
impl crate::dma::sealed::Channel for crate::peripherals::$channel_peri {
unsafe fn start_write<W: Word>(&mut self, request: Request, buf: *const[W], reg_addr: *mut W) {
unsafe fn start_write<W: Word>(&mut self, _request: Request, buf: *const[W], reg_addr: *mut W) {
let (ptr, len) = super::slice_ptr_parts(buf);
low_level_api::start_transfer(
pac::$dma_peri,
$channel_num,
#[cfg(any(bdma_v2, dmamux))]
request,
_request,
vals::Dir::FROMMEMORY,
reg_addr as *const u32,
ptr as *mut u32,
@ -113,13 +96,13 @@ pac::dma_channels! {
}
unsafe fn start_write_repeated<W: Word>(&mut self, request: Request, repeated: W, count: usize, reg_addr: *mut W) {
unsafe fn start_write_repeated<W: Word>(&mut self, _request: Request, repeated: W, count: usize, reg_addr: *mut W) {
let buf = [repeated];
low_level_api::start_transfer(
pac::$dma_peri,
$channel_num,
#[cfg(any(bdma_v2, dmamux))]
request,
_request,
vals::Dir::FROMMEMORY,
reg_addr as *const u32,
buf.as_ptr() as *mut u32,
@ -133,13 +116,13 @@ pac::dma_channels! {
)
}
unsafe fn start_read<W: Word>(&mut self, request: Request, reg_addr: *const W, buf: *mut [W]) {
unsafe fn start_read<W: Word>(&mut self, _request: Request, reg_addr: *const W, buf: *mut [W]) {
let (ptr, len) = super::slice_ptr_parts_mut(buf);
low_level_api::start_transfer(
pac::$dma_peri,
$channel_num,
#[cfg(any(bdma_v2, dmamux))]
request,
_request,
vals::Dir::FROMPERIPHERAL,
reg_addr as *const u32,
ptr as *mut u32,
@ -165,7 +148,7 @@ pac::dma_channels! {
}
fn set_waker(&mut self, waker: &Waker) {
unsafe {low_level_api::set_waker(dma_num!($dma_peri) * 8 + $channel_num, waker )}
unsafe { low_level_api::set_waker($index, waker) }
}
}

View File

@ -4,14 +4,13 @@ use core::task::Waker;
use embassy::interrupt::{Interrupt, InterruptExt};
use embassy::waitqueue::AtomicWaker;
use crate::generated::DMA_CHANNEL_COUNT;
use crate::interrupt;
use crate::pac;
use crate::pac::dma::{regs, vals};
use super::{Request, Word, WordSize};
const CH_COUNT: usize = pac::peripheral_count!(DMA) * 8;
impl From<WordSize> for vals::Size {
fn from(raw: WordSize) -> Self {
match raw {
@ -23,44 +22,31 @@ impl From<WordSize> for vals::Size {
}
struct State {
ch_wakers: [AtomicWaker; CH_COUNT],
ch_wakers: [AtomicWaker; DMA_CHANNEL_COUNT],
}
impl State {
const fn new() -> Self {
const AW: AtomicWaker = AtomicWaker::new();
Self {
ch_wakers: [AW; CH_COUNT],
ch_wakers: [AW; DMA_CHANNEL_COUNT],
}
}
}
static STATE: State = State::new();
macro_rules! dma_num {
(DMA1) => {
0
};
(DMA2) => {
1
};
}
pub(crate) unsafe fn on_irq() {
pac::peripherals! {
foreach_peripheral! {
(dma, $dma:ident) => {
for isrn in 0..2 {
let isr = pac::$dma.isr(isrn).read();
for chn in 0..4 {
let cr = pac::$dma.st(isrn * 4 + chn).cr();
if isr.tcif(chn) && cr.read().tcie() {
foreach_dma_channel! {
($channel_peri:ident, $dma, dma, $channel_num:expr, $index:expr, $dmamux:tt) => {
let cr = pac::$dma.st($channel_num).cr();
if pac::$dma.isr($channel_num/4).read().tcif($channel_num%4) && cr.read().tcie() {
cr.write(|_| ()); // Disable channel interrupts with the default value.
let n = dma_num!($dma) * 8 + isrn * 4 + chn;
STATE.ch_wakers[n].wake();
STATE.ch_wakers[$index].wake();
}
}
};
}
};
}
@ -68,7 +54,7 @@ pub(crate) unsafe fn on_irq() {
/// safety: must be called only once
pub(crate) unsafe fn init() {
pac::interrupts! {
foreach_interrupt! {
($peri:ident, dma, $block:ident, $signal_name:ident, $irq:ident) => {
interrupt::$irq::steal().enable();
};
@ -76,8 +62,8 @@ pub(crate) unsafe fn init() {
crate::generated::init_dma();
}
pac::dma_channels! {
($channel_peri:ident, $dma_peri:ident, dma, $channel_num:expr, $dmamux:tt) => {
foreach_dma_channel! {
($channel_peri:ident, $dma_peri:ident, dma, $channel_num:expr, $index:expr, $dmamux:tt) => {
impl crate::dma::sealed::Channel for crate::peripherals::$channel_peri {
unsafe fn start_write<W: Word>(&mut self, request: Request, buf: *const [W], reg_addr: *mut W) {
let (ptr, len) = super::slice_ptr_parts(buf);
@ -149,7 +135,7 @@ pac::dma_channels! {
}
fn set_waker(&mut self, waker: &Waker) {
unsafe {low_level_api::set_waker(dma_num!($dma_peri) * 8 + $channel_num, waker )}
unsafe {low_level_api::set_waker($index, waker )}
}
}

View File

@ -35,8 +35,8 @@ pub trait MuxChannel: sealed::MuxChannel + super::Channel {
type Mux;
}
pac::dma_channels! {
($channel_peri:ident, $dma_peri:ident, $version:ident, $channel_num:expr, {dmamux: $dmamux:ident, dmamux_channel: $dmamux_channel:expr}) => {
foreach_dma_channel! {
($channel_peri:ident, $dma_peri:ident, $version:ident, $channel_num:expr, $index:expr, {dmamux: $dmamux:ident, dmamux_channel: $dmamux_channel:expr}) => {
impl sealed::MuxChannel for peripherals::$channel_peri {
const DMAMUX_CH_NUM: u8 = $dmamux_channel;
const DMAMUX_REGS: pac::dmamux::Dmamux = pac::$dmamux;

View File

@ -278,7 +278,7 @@ impl<'a> Future for ExtiInputFuture<'a> {
macro_rules! foreach_exti_irq {
($action:ident) => {
crate::pac::interrupts!(
foreach_interrupt!(
(EXTI0) => { $action!(EXTI0); };
(EXTI1) => { $action!(EXTI1); };
(EXTI2) => { $action!(EXTI2); };

View File

@ -127,7 +127,7 @@ impl<'d, T: Instance> Fmc<'d, T> {
));
}
crate::pac::peripherals!(
foreach_peripheral!(
(fmc, $inst:ident) => {
impl crate::fmc::sealed::Instance for crate::peripherals::$inst {
fn regs() -> stm32_metapac::fmc::Fmc {

View File

@ -558,7 +558,7 @@ impl sealed::Pin for AnyPin {
// ====================
crate::pac::pins!(
foreach_pin!(
($pin_name:ident, $port_name:ident, $port_num:expr, $pin_num:expr, $exti_ch:ident) => {
impl Pin for peripherals::$pin_name {
#[cfg(feature = "exti")]

View File

@ -37,7 +37,7 @@ pin_trait!(SdaPin, Instance);
dma_trait!(RxDma, Instance);
dma_trait!(TxDma, Instance);
crate::pac::interrupts!(
foreach_interrupt!(
($inst:ident, i2c, $block:ident, EV, $irq:ident) => {
impl sealed::Instance for peripherals::$inst {
fn regs() -> crate::pac::i2c::I2c {

View File

@ -11,6 +11,7 @@ pub(crate) use stm32_metapac as pac;
// This must go FIRST so that all the other modules see its macros.
pub mod fmt;
include!(concat!(env!("OUT_DIR"), "/macros.rs"));
// Utilities
pub mod interrupt;
@ -62,7 +63,7 @@ pub mod usb_otg;
pub mod subghz;
// This must go last, so that it sees all the impl_foo! macros defined earlier.
mod generated {
pub(crate) mod generated {
#![allow(dead_code)]
#![allow(unused_imports)]

View File

@ -127,7 +127,7 @@ macro_rules! impl_compare_capable_16bit {
};
}
crate::pac::interrupts! {
foreach_interrupt! {
($inst:ident, timer, TIM_GP16, UP, $irq:ident) => {
impl crate::pwm::sealed::CaptureCompare16bitInstance for crate::peripherals::$inst {
unsafe fn set_output_compare_mode(

View File

@ -136,7 +136,7 @@ pub(crate) mod sealed {
pub trait Instance: sealed::Instance + crate::rcc::RccPeripheral {}
crate::pac::peripherals!(
foreach_peripheral!(
(rng, $inst:ident) => {
impl Instance for peripherals::$inst {}
@ -165,7 +165,7 @@ macro_rules! irq {
};
}
crate::pac::interrupts!(
foreach_interrupt!(
(RNG) => {
irq!(RNG);
};

View File

@ -1271,7 +1271,7 @@ where
}
}
crate::pac::peripherals!(
foreach_peripheral!(
(sdmmc, $inst:ident) => {
impl sealed::Instance for peripherals::$inst {
type Interrupt = crate::interrupt::$inst;

View File

@ -874,7 +874,7 @@ pin_trait!(MisoPin, Instance);
dma_trait!(RxDma, Instance);
dma_trait!(TxDma, Instance);
crate::pac::peripherals!(
foreach_peripheral!(
(spi, $inst:ident) => {
impl sealed::Instance for peripherals::$inst {
fn regs() -> &'static crate::pac::spi::Spi {

View File

@ -29,7 +29,7 @@ type T = peripherals::TIM4;
#[cfg(time_driver_tim5)]
type T = peripherals::TIM5;
crate::pac::interrupts! {
foreach_interrupt! {
(TIM2, timer, $block:ident, UP, $irq:ident) => {
#[cfg(time_driver_tim2)]
#[interrupt]

View File

@ -155,7 +155,7 @@ macro_rules! impl_32bit_timer {
};
}
crate::pac::interrupts! {
foreach_interrupt! {
($inst:ident, timer, TIM_BASIC, UP, $irq:ident) => {
impl_basic_16bit_timer!($inst, $irq);

View File

@ -601,7 +601,7 @@ pin_trait!(CkPin, Instance);
dma_trait!(TxDma, Instance);
dma_trait!(RxDma, Instance);
crate::pac::interrupts!(
foreach_interrupt!(
($inst:ident, usart, $block:ident, $signal_name:ident, $irq:ident) => {
impl sealed::Instance for peripherals::$inst {
fn regs(&self) -> crate::pac::usart::Usart {

View File

@ -135,7 +135,7 @@ pin_trait!(UlpiD5Pin, Instance);
pin_trait!(UlpiD6Pin, Instance);
pin_trait!(UlpiD7Pin, Instance);
crate::pac::peripherals!(
foreach_peripheral!(
(otgfs, $inst:ident) => {
impl sealed::Instance for peripherals::$inst {
const REGISTERS: *const () = crate::pac::$inst.0 as *const ();
@ -223,7 +223,7 @@ crate::pac::peripherals!(
};
);
crate::pac::interrupts!(
foreach_interrupt!(
($inst:ident, otgfs, $block:ident, GLOBAL, $irq:ident) => {
unsafe impl USBInterrupt for crate::interrupt::$irq {}
};