Skip to main content

ch32_hal/
macros.rs

1#![macro_use]
2#![allow(unused_macros)]
3
4macro_rules! dma_trait {
5    ($signal:ident, $instance:path$(, $mode:path)?) => {
6        #[doc = concat!(stringify!($signal), " DMA request trait")]
7        pub trait $signal<T: $instance $(, M: $mode)?>: crate::dma::Channel {
8            #[doc = concat!("Get the DMA request number needed to use this channel as", stringify!($signal))]
9            /// Note: in some chips, ST calls this the "channel", and calls channels "streams".
10            /// `embassy-stm32` always uses the "channel" and "request number" names.
11            fn request(&self) -> crate::dma::Request;
12        }
13    };
14}
15
16// Two pin-trait shapes, selected per chip. They never coexist in one build.
17//
18// - cfg(not(afio_h4)): PCFR remap. `trait Signal<T, const REMAP: u8 = 0>`.
19//   TX and RX share one const, same as the published API. `apply_remap!()`
20//   writes the group via `RemapPeripheral::set_remap` (patches cover split
21//   fields). Every pin entry is its own impl, so one pin can implement
22//   several signals and several groups.
23// - cfg(afio_h4): per-pin AF in `AFIO.GPIO_AFR`. The trait has no remap
24//   parameter; `af_num()` is the value `build.rs` read from `pin.af`.
25//
26// `if_remap!(impl TxPin<T, REMAP>)` keeps the const in the signature on
27// remap chips and drops it on H4.
28macro_rules! pin_trait {
29    ($signal:ident, $instance:path) => {
30        #[cfg(not(afio_h4))]
31        pub trait $signal<T: $instance, const REMAP: u8 = 0>: crate::gpio::Pin {}
32
33        #[cfg(afio_h4)]
34        pub trait $signal<T: $instance>: crate::gpio::Pin {
35            #[doc = concat!("AF number for `", stringify!($signal), "`.")]
36            fn af_num(&self) -> u8;
37        }
38    };
39}
40
41macro_rules! pin_trait_impl {
42    (crate::$mod:ident::$trait:ident, $instance:ident, $pin:ident, $n:expr) => {
43        #[cfg(not(afio_h4))]
44        impl crate::$mod::$trait<crate::peripherals::$instance, $n> for crate::peripherals::$pin {}
45
46        #[cfg(afio_h4)]
47        impl crate::$mod::$trait<crate::peripherals::$instance> for crate::peripherals::$pin {
48            fn af_num(&self) -> u8 {
49                $n
50            }
51        }
52    };
53}
54
55// MCO is a singleton peripheral (RCC), so its pin trait has no instance
56// parameter; it is still generated from the same ch32-data pin tables.
57macro_rules! mco_pin_impl {
58    ($pin:ident, $n:expr) => {
59        #[cfg(not(afio_h4))]
60        impl crate::rcc::McoPin for crate::peripherals::$pin {}
61
62        #[cfg(afio_h4)]
63        impl crate::rcc::McoPin for crate::peripherals::$pin {
64            fn af_num(&self) -> u8 {
65                $n
66            }
67        }
68    };
69}
70
71#[cfg(not(afio_h4))]
72macro_rules! if_remap {
73    ($($t:tt)*) => {
74        $($t)*
75    };
76}
77
78#[cfg(afio_h4)]
79macro_rules! if_remap {
80    (impl $trait:ident<$a:ty, REMAP>) => {
81        impl $trait<$a>
82    };
83    (impl $trait:ident<$a:ty, $b:ty, REMAP>) => {
84        impl $trait<$a, $b>
85    };
86}
87
88/// Write `AFIO.PCFR*` for the `REMAP` const in scope. No-op on CH32H4,
89/// where the mux is the per-pin AF number instead.
90macro_rules! apply_remap {
91    () => {
92        #[cfg(not(afio_h4))]
93        {
94            fn apply<P: crate::peripheral::RemapPeripheral, const R: u8>() {
95                P::set_remap(R);
96            }
97            apply::<T, REMAP>();
98        }
99    };
100}
101
102/// Configure a pin for AF use and consume it into `Option<Peri<'d, AnyPin>>`.
103///
104/// Mode/cnf is written on every family. On CH32H4 the AF number is also
105/// written to `AFIO.GPIO_AFR`. PCFR remap is `apply_remap!()`, not this macro,
106/// because the group is a property of the peripheral, not of one pin.
107macro_rules! new_pin {
108    ($name:ident, $af_type:expr) => {{
109        let pin = $name;
110        pin.set_as_af(
111            #[cfg(afio_h4)]
112            pin.af_num(),
113            $af_type,
114        );
115        Some(pin.into())
116    }};
117}
118
119/// Like `new_pin!` but leaves the typed pin in place.
120macro_rules! set_as_af {
121    ($pin:expr, $af_type:expr) => {{
122        $pin.set_as_af(
123            #[cfg(afio_h4)]
124            $pin.af_num(),
125            $af_type,
126        );
127    }};
128}
129
130#[allow(unused)]
131macro_rules! dma_trait_impl {
132    // DMA/GPDMA, without DMAMUX. The channel is hard-wired to the signal, so
133    // the request carries no number.
134    (crate::$mod:ident::$trait:ident$(<$mode:ident>)?, $instance:ident, {channel: $channel:ident}, $request:expr) => {
135        impl crate::$mod::$trait<crate::peripherals::$instance $(, crate::$mod::$mode)?> for crate::peripherals::$channel {
136            fn request(&self) -> crate::dma::Request {
137                $request
138            }
139        }
140    };
141    // DMAMUX (CH32H4). Every mux channel can carry the signal; the request
142    // number is a property of the signal, so it is the same for all 16
143    // channels. The `MuxChannel` bound is what limits the blanket impl to
144    // those channels — H4 has one mux, so there is no per-channel choice to
145    // make beyond that.
146    (crate::$mod:ident::$trait:ident$(<$mode:ident>)?, $instance:ident, {dmamux: $dmamux:ident}, $request:expr) => {
147        impl<C> crate::$mod::$trait<crate::peripherals::$instance $(, crate::$mod::$mode)?> for C
148        where
149            C: crate::dma::MuxChannel + crate::dma::Channel,
150        {
151            fn request(&self) -> crate::dma::Request {
152                $request
153            }
154        }
155    };
156}
157
158macro_rules! new_dma {
159    ($name:ident) => {{
160        let request = $name.request();
161        Some(crate::dma::ChannelAndRequest {
162            channel: $name.into(),
163            request,
164        })
165    }};
166}
167
168#[collapse_debuginfo(yes)]
169macro_rules! panic {
170    ($($x:tt)*) => {
171        {
172            #[cfg(not(feature = "defmt"))]
173            ::core::panic!($($x)*);
174            #[cfg(feature = "defmt")]
175            ::defmt::panic!($($x)*);
176        }
177    };
178}
179
180#[collapse_debuginfo(yes)]
181macro_rules! trace {
182    ($s:literal $(, $x:expr)* $(,)?) => {
183        {
184            #[cfg(feature = "defmt")]
185            ::defmt::trace!($s $(, $x)*);
186            #[cfg(not(feature = "defmt"))]
187            let _ = ($( & $x ),*);
188        }
189    };
190}
191
192#[collapse_debuginfo(yes)]
193macro_rules! debug {
194    ($s:literal $(, $x:expr)* $(,)?) => {
195        {
196            #[cfg(feature = "defmt")]
197            ::defmt::debug!($s $(, $x)*);
198            #[cfg(not(feature = "defmt"))]
199            let _ = ($( & $x ),*);
200        }
201    };
202}
203
204#[collapse_debuginfo(yes)]
205macro_rules! info {
206    ($s:literal $(, $x:expr)* $(,)?) => {
207        {
208            #[cfg(feature = "defmt")]
209            ::defmt::info!($s $(, $x)*);
210            #[cfg(not(feature = "defmt"))]
211            let _ = ($( & $x ),*);
212        }
213    };
214}
215
216#[collapse_debuginfo(yes)]
217macro_rules! warn {
218    ($s:literal $(, $x:expr)* $(,)?) => {
219        {
220            #[cfg(feature = "defmt")]
221            ::defmt::warn!($s $(, $x)*);
222            #[cfg(not(feature = "defmt"))]
223            let _ = ($( & $x ),*);
224        }
225    };
226}
227
228#[collapse_debuginfo(yes)]
229macro_rules! error {
230    ($s:literal $(, $x:expr)* $(,)?) => {
231        {
232            #[cfg(feature = "defmt")]
233            ::defmt::error!($s $(, $x)*);
234            #[cfg(not(feature = "defmt"))]
235            let _ = ($( & $x ),*);
236        }
237    };
238}
239
240#[collapse_debuginfo(yes)]
241macro_rules! unwrap {
242    ($e:expr) => {{
243        #[cfg(feature = "defmt")]
244        {
245            ::defmt::unwrap!($e)
246        }
247        #[cfg(not(feature = "defmt"))]
248        {
249            $e.unwrap()
250        }
251    }};
252}