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