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