embedded_c_sdk_bind_hal/
lib.rs

1#![no_std]
2
3#[macro_use]
4mod common;
5mod ll_api;
6mod tick_freq_hz;
7
8pub mod adc;
9pub mod dma;
10pub mod flash;
11pub mod gpio;
12pub mod i2c;
13pub mod print;
14pub mod pwm;
15pub mod spi;
16pub mod tick;
17pub mod usart;
18
19pub use common::format;
20pub use ll_api::ll_cmd::*;
21
22#[cfg(feature = "print-log-csdk")]
23pub use embedded_c_sdk_bind_print_macros::{print, println};
24
25#[macro_export]
26macro_rules! ll_invoke {
27    ( $( $x:expr ),* ) => {
28        {
29            unsafe { $crate::ll_invoke( $( $x as $crate::InvokeParam, )* ) }
30        }
31    };
32}
33
34#[macro_export]
35macro_rules! sys_tick_handler {
36    () => {
37        <$crate::tick::Tick as $crate::tick::HalTickHandler>::on_sys_tick_interrupt();
38    };
39}
40
41/// Macro for setting up a periodic interval to execute a function.
42///
43/// # Syntax
44/// ```rust
45/// setInterval!(function, period_ms);
46/// setInterval!(function, period_ms, param1, param2, ...);
47/// ```
48///
49/// # Examples
50/// ```rust
51/// setInterval!(my_function, 1000);
52/// setInterval!(my_function_with_params, 1000, arg1, arg2);
53/// ```
54///
55/// # Arguments
56/// * `$f` - The function to be executed.
57/// * `$period_ms` - The period in milliseconds after which the function should be called.
58/// * `$( $param:expr ),*` - Optional parameters to pass to the function.
59///
60/// # Behavior
61/// * Initializes a static `Tick` variable.
62/// * Checks if the elapsed time since the last execution is greater than or equal to the specified period.
63/// * If so, resets the `Tick` and calls the function with or without parameters.
64#[macro_export]
65macro_rules! setInterval {
66    ($f:expr, $period_ms:expr) => {
67        {
68            static mut TICK: Tick = Tick::with_value(0);
69
70            if unsafe { TICK.elapsed_time().to_millis() } >= $period_ms {
71                unsafe { TICK = Tick::now(); }
72                $f();
73            }
74        }
75    };
76
77    ($f:expr, $period_ms:expr, $( $param:expr ),*) => {
78        {
79            static mut TICK: Tick = Tick::with_value(0);
80
81            if unsafe { TICK.elapsed_time().to_millis() } >= $period_ms {
82                unsafe { TICK = Tick::now(); }
83                $f($( $param, )*);
84            }
85        }
86    };
87}
88
89macro_rules! peripherals_struct {
90    ($($(#[$cfg:meta])? $name:ident),*$(,)?) => {
91        /// Struct containing all the peripheral.
92        ///
93        /// To obtain the peripherals, you must initialize the HAL, by calling [`crate::init`].
94        #[allow(non_snake_case)]
95        pub struct Peripherals {
96            $(
97                #[doc = concat!(stringify!($name), " peripheral")]
98                $(#[$cfg])?
99                pub $name: $crate::gpio::Peri<'static, peripherals::$name>,
100            )*
101        }
102
103        impl Peripherals {
104            ///Returns all the peripherals
105            #[inline]
106            pub(crate) fn take() -> Self {
107                // safety: OK because we're inside a CS.
108                unsafe {
109                    Self::steal()
110                }
111            }
112        }
113
114        impl Peripherals {
115            #[inline]
116            pub unsafe fn steal() -> Self {
117                Self {
118                    $(
119                        $(#[$cfg])?
120                        $name: peripherals::$name::steal(),
121                    )*
122                }
123            }
124        }
125    };
126}
127
128/// Initialize the C-SDK HAL.
129///
130/// This returns the peripheral singletons that can be used for creating drivers.
131///
132/// This should only be called once at startup, otherwise it panics.
133pub fn init() -> Peripherals {
134    ll_invoke!(INVOKE_ID_LL_DRV_INIT);
135
136    Peripherals::take()
137}
138
139#[rustfmt::skip]
140embassy_hal_internal::peripherals_definition!(
141    PA0, PA1, PA2, PA3, PA4, PA5, PA6, PA7, PA8, PA9, PA10, PA11, PA12, PA13, PA14, PA15, 
142    PB0, PB1, PB2, PB3, PB4, PB5, PB6, PB7, PB8, PB9, PB10, PB11, PB12, PB13, PB14, PB15, 
143    PC0, PC1, PC2, PC3, PC4, PC5, PC6, PC7, PC8, PC9, PC10, PC11, PC12, PC13, PC14, PC15, 
144    PD0, PD1, PD2, PD3, PD4, PD5, PD6, PD7, PD8, PD9, PD10, PD11, PD12, PD13, PD14, PD15, 
145    PE0, PE1, PE2, PE3, PE4, PE5, PE6, PE7, PE8, PE9, PE10, PE11, PE12, PE13, PE14, PE15, 
146    PF0, PF1, PF2, PF3, PF4, PF5, PF6, PF7, PF8, PF9, PF10, PF11, PF12, PF13, PF14, PF15, 
147    PG0, PG1, PG2, PG3, PG4, PG5, PG6, PG7, PG8, PG9, PG10, PG11, PG12, PG13, PG14, PG15, 
148    PH0, PH1, PH2, PH3, PH4, PH5, PH6, PH7, PH8, PH9, PH10, PH11, PH12, PH13, PH14, PH15, 
149);
150
151#[rustfmt::skip]
152peripherals_struct!(
153    PA0, PA1, PA2, PA3, PA4, PA5, PA6, PA7, PA8, PA9, PA10, PA11, PA12, PA13, PA14, PA15, 
154    PB0, PB1, PB2, PB3, PB4, PB5, PB6, PB7, PB8, PB9, PB10, PB11, PB12, PB13, PB14, PB15, 
155    PC0, PC1, PC2, PC3, PC4, PC5, PC6, PC7, PC8, PC9, PC10, PC11, PC12, PC13, PC14, PC15, 
156    PD0, PD1, PD2, PD3, PD4, PD5, PD6, PD7, PD8, PD9, PD10, PD11, PD12, PD13, PD14, PD15, 
157    PE0, PE1, PE2, PE3, PE4, PE5, PE6, PE7, PE8, PE9, PE10, PE11, PE12, PE13, PE14, PE15, 
158    PF0, PF1, PF2, PF3, PF4, PF5, PF6, PF7, PF8, PF9, PF10, PF11, PF12, PF13, PF14, PF15, 
159    PG0, PG1, PG2, PG3, PG4, PG5, PG6, PG7, PG8, PG9, PG10, PG11, PG12, PG13, PG14, PG15, 
160    PH0, PH1, PH2, PH3, PH4, PH5, PH6, PH7, PH8, PH9, PH10, PH11, PH12, PH13, PH14, PH15, 
161);