stm32ral/stm32g0/stm32g061/tim2.rs
1#![allow(non_snake_case, non_upper_case_globals)]
2#![allow(non_camel_case_types)]
3//! General-purpose-timers
4
5#[cfg(not(feature = "nosync"))]
6pub use crate::stm32g0::peripherals::tim2_v3::Instance;
7pub use crate::stm32g0::peripherals::tim2_v3::{RegisterBlock, ResetValues};
8pub use crate::stm32g0::peripherals::tim2_v3::{
9 AF1, ARR, CCER, CCMR1, CCMR2, CCR1, CCR2, CCR3, CCR4, CNT, CR1, CR2, DCR, DIER, DMAR, EGR, OR1,
10 PSC, SMCR, SR, TISEL,
11};
12
13/// Access functions for the TIM2 peripheral instance
14pub mod TIM2 {
15 use super::ResetValues;
16
17 #[cfg(not(feature = "nosync"))]
18 use super::Instance;
19
20 #[cfg(not(feature = "nosync"))]
21 const INSTANCE: Instance = Instance {
22 addr: 0x40000000,
23 _marker: ::core::marker::PhantomData,
24 };
25
26 /// Reset values for each field in TIM2
27 pub const reset: ResetValues = ResetValues {
28 CR1: 0x00000000,
29 CR2: 0x00000000,
30 SMCR: 0x00000000,
31 DIER: 0x00000000,
32 SR: 0x00000000,
33 EGR: 0x00000000,
34 CCMR1: 0x00000000,
35 CCMR2: 0x00000000,
36 CCER: 0x00000000,
37 CNT: 0x00000000,
38 PSC: 0x00000000,
39 ARR: 0xFFFFFFFF,
40 CCR1: 0x00000000,
41 CCR2: 0x00000000,
42 CCR3: 0x00000000,
43 CCR4: 0x00000000,
44 DCR: 0x00000000,
45 DMAR: 0x00000000,
46 OR1: 0x00000000,
47 AF1: 0x00000000,
48 TISEL: 0x00000000,
49 };
50
51 #[cfg(not(feature = "nosync"))]
52 #[allow(renamed_and_removed_lints)]
53 #[allow(private_no_mangle_statics)]
54 #[no_mangle]
55 static mut TIM2_TAKEN: bool = false;
56
57 /// Safe access to TIM2
58 ///
59 /// This function returns `Some(Instance)` if this instance is not
60 /// currently taken, and `None` if it is. This ensures that if you
61 /// do get `Some(Instance)`, you are ensured unique access to
62 /// the peripheral and there cannot be data races (unless other
63 /// code uses `unsafe`, of course). You can then pass the
64 /// `Instance` around to other functions as required. When you're
65 /// done with it, you can call `release(instance)` to return it.
66 ///
67 /// `Instance` itself dereferences to a `RegisterBlock`, which
68 /// provides access to the peripheral's registers.
69 #[cfg(not(feature = "nosync"))]
70 #[inline]
71 pub fn take() -> Option<Instance> {
72 external_cortex_m::interrupt::free(|_| unsafe {
73 if TIM2_TAKEN {
74 None
75 } else {
76 TIM2_TAKEN = true;
77 Some(INSTANCE)
78 }
79 })
80 }
81
82 /// Release exclusive access to TIM2
83 ///
84 /// This function allows you to return an `Instance` so that it
85 /// is available to `take()` again. This function will panic if
86 /// you return a different `Instance` or if this instance is not
87 /// already taken.
88 #[cfg(not(feature = "nosync"))]
89 #[inline]
90 pub fn release(inst: Instance) {
91 external_cortex_m::interrupt::free(|_| unsafe {
92 if TIM2_TAKEN && inst.addr == INSTANCE.addr {
93 TIM2_TAKEN = false;
94 } else {
95 panic!("Released a peripheral which was not taken");
96 }
97 });
98 }
99
100 /// Unsafely steal TIM2
101 ///
102 /// This function is similar to take() but forcibly takes the
103 /// Instance, marking it as taken irregardless of its previous
104 /// state.
105 #[cfg(not(feature = "nosync"))]
106 #[inline]
107 pub unsafe fn steal() -> Instance {
108 TIM2_TAKEN = true;
109 INSTANCE
110 }
111}
112
113/// Raw pointer to TIM2
114///
115/// Dereferencing this is unsafe because you are not ensured unique
116/// access to the peripheral, so you may encounter data races with
117/// other users of this peripheral. It is up to you to ensure you
118/// will not cause data races.
119///
120/// This constant is provided for ease of use in unsafe code: you can
121/// simply call for example `write_reg!(gpio, GPIOA, ODR, 1);`.
122pub const TIM2: *const RegisterBlock = 0x40000000 as *const _;