stm32ral/stm32f4/instances/stk.rs
1#![allow(non_snake_case, non_upper_case_globals)]
2#![allow(non_camel_case_types)]
3//! SysTick timer
4//!
5//! Used by: stm32f401, stm32f405, stm32f407, stm32f410, stm32f411, stm32f412, stm32f413, stm32f427, stm32f429, stm32f446, stm32f469
6
7#[cfg(not(feature = "nosync"))]
8pub use crate::stm32f4::peripherals::stk::Instance;
9pub use crate::stm32f4::peripherals::stk::{RegisterBlock, ResetValues};
10pub use crate::stm32f4::peripherals::stk::{CALIB, CTRL, LOAD, VAL};
11
12/// Access functions for the STK peripheral instance
13pub mod STK {
14 use super::ResetValues;
15
16 #[cfg(not(feature = "nosync"))]
17 use super::Instance;
18
19 #[cfg(not(feature = "nosync"))]
20 const INSTANCE: Instance = Instance {
21 addr: 0xe000e010,
22 _marker: ::core::marker::PhantomData,
23 };
24
25 /// Reset values for each field in STK
26 pub const reset: ResetValues = ResetValues {
27 CTRL: 0x00000000,
28 LOAD: 0x00000000,
29 VAL: 0x00000000,
30 CALIB: 0x00000000,
31 };
32
33 #[cfg(not(feature = "nosync"))]
34 #[allow(renamed_and_removed_lints)]
35 #[allow(private_no_mangle_statics)]
36 #[no_mangle]
37 static mut STK_TAKEN: bool = false;
38
39 /// Safe access to STK
40 ///
41 /// This function returns `Some(Instance)` if this instance is not
42 /// currently taken, and `None` if it is. This ensures that if you
43 /// do get `Some(Instance)`, you are ensured unique access to
44 /// the peripheral and there cannot be data races (unless other
45 /// code uses `unsafe`, of course). You can then pass the
46 /// `Instance` around to other functions as required. When you're
47 /// done with it, you can call `release(instance)` to return it.
48 ///
49 /// `Instance` itself dereferences to a `RegisterBlock`, which
50 /// provides access to the peripheral's registers.
51 #[cfg(not(feature = "nosync"))]
52 #[inline]
53 pub fn take() -> Option<Instance> {
54 external_cortex_m::interrupt::free(|_| unsafe {
55 if STK_TAKEN {
56 None
57 } else {
58 STK_TAKEN = true;
59 Some(INSTANCE)
60 }
61 })
62 }
63
64 /// Release exclusive access to STK
65 ///
66 /// This function allows you to return an `Instance` so that it
67 /// is available to `take()` again. This function will panic if
68 /// you return a different `Instance` or if this instance is not
69 /// already taken.
70 #[cfg(not(feature = "nosync"))]
71 #[inline]
72 pub fn release(inst: Instance) {
73 external_cortex_m::interrupt::free(|_| unsafe {
74 if STK_TAKEN && inst.addr == INSTANCE.addr {
75 STK_TAKEN = false;
76 } else {
77 panic!("Released a peripheral which was not taken");
78 }
79 });
80 }
81
82 /// Unsafely steal STK
83 ///
84 /// This function is similar to take() but forcibly takes the
85 /// Instance, marking it as taken irregardless of its previous
86 /// state.
87 #[cfg(not(feature = "nosync"))]
88 #[inline]
89 pub unsafe fn steal() -> Instance {
90 STK_TAKEN = true;
91 INSTANCE
92 }
93}
94
95/// Raw pointer to STK
96///
97/// Dereferencing this is unsafe because you are not ensured unique
98/// access to the peripheral, so you may encounter data races with
99/// other users of this peripheral. It is up to you to ensure you
100/// will not cause data races.
101///
102/// This constant is provided for ease of use in unsafe code: you can
103/// simply call for example `write_reg!(gpio, GPIOA, ODR, 1);`.
104pub const STK: *const RegisterBlock = 0xe000e010 as *const _;