Skip to main content

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