stm32ral/stm32f1/stm32f102/spi.rs
1#![allow(non_snake_case, non_upper_case_globals)]
2#![allow(non_camel_case_types)]
3//! Serial peripheral interface
4
5#[cfg(not(feature = "nosync"))]
6pub use crate::stm32f1::peripherals::spi_v1::Instance;
7pub use crate::stm32f1::peripherals::spi_v1::{RegisterBlock, ResetValues};
8pub use crate::stm32f1::peripherals::spi_v1::{CR1, CR2, CRCPR, DR, RXCRCR, SR, TXCRCR};
9
10/// Access functions for the SPI2 peripheral instance
11pub mod SPI2 {
12 use super::ResetValues;
13
14 #[cfg(not(feature = "nosync"))]
15 use super::Instance;
16
17 #[cfg(not(feature = "nosync"))]
18 const INSTANCE: Instance = Instance {
19 addr: 0x40003800,
20 _marker: ::core::marker::PhantomData,
21 };
22
23 /// Reset values for each field in SPI2
24 pub const reset: ResetValues = ResetValues {
25 CR1: 0x00000000,
26 CR2: 0x00000000,
27 SR: 0x00000002,
28 DR: 0x00000000,
29 CRCPR: 0x00000007,
30 RXCRCR: 0x00000000,
31 TXCRCR: 0x00000000,
32 };
33
34 #[cfg(not(feature = "nosync"))]
35 #[allow(renamed_and_removed_lints)]
36 #[allow(private_no_mangle_statics)]
37 #[no_mangle]
38 static mut SPI2_TAKEN: bool = false;
39
40 /// Safe access to SPI2
41 ///
42 /// This function returns `Some(Instance)` if this instance is not
43 /// currently taken, and `None` if it is. This ensures that if you
44 /// do get `Some(Instance)`, you are ensured unique access to
45 /// the peripheral and there cannot be data races (unless other
46 /// code uses `unsafe`, of course). You can then pass the
47 /// `Instance` around to other functions as required. When you're
48 /// done with it, you can call `release(instance)` to return it.
49 ///
50 /// `Instance` itself dereferences to a `RegisterBlock`, which
51 /// provides access to the peripheral's registers.
52 #[cfg(not(feature = "nosync"))]
53 #[inline]
54 pub fn take() -> Option<Instance> {
55 external_cortex_m::interrupt::free(|_| unsafe {
56 if SPI2_TAKEN {
57 None
58 } else {
59 SPI2_TAKEN = true;
60 Some(INSTANCE)
61 }
62 })
63 }
64
65 /// Release exclusive access to SPI2
66 ///
67 /// This function allows you to return an `Instance` so that it
68 /// is available to `take()` again. This function will panic if
69 /// you return a different `Instance` or if this instance is not
70 /// already taken.
71 #[cfg(not(feature = "nosync"))]
72 #[inline]
73 pub fn release(inst: Instance) {
74 external_cortex_m::interrupt::free(|_| unsafe {
75 if SPI2_TAKEN && inst.addr == INSTANCE.addr {
76 SPI2_TAKEN = false;
77 } else {
78 panic!("Released a peripheral which was not taken");
79 }
80 });
81 }
82
83 /// Unsafely steal SPI2
84 ///
85 /// This function is similar to take() but forcibly takes the
86 /// Instance, marking it as taken irregardless of its previous
87 /// state.
88 #[cfg(not(feature = "nosync"))]
89 #[inline]
90 pub unsafe fn steal() -> Instance {
91 SPI2_TAKEN = true;
92 INSTANCE
93 }
94}
95
96/// Raw pointer to SPI2
97///
98/// Dereferencing this is unsafe because you are not ensured unique
99/// access to the peripheral, so you may encounter data races with
100/// other users of this peripheral. It is up to you to ensure you
101/// will not cause data races.
102///
103/// This constant is provided for ease of use in unsafe code: you can
104/// simply call for example `write_reg!(gpio, GPIOA, ODR, 1);`.
105pub const SPI2: *const RegisterBlock = 0x40003800 as *const _;
106
107/// Access functions for the SPI3 peripheral instance
108pub mod SPI3 {
109 use super::ResetValues;
110
111 #[cfg(not(feature = "nosync"))]
112 use super::Instance;
113
114 #[cfg(not(feature = "nosync"))]
115 const INSTANCE: Instance = Instance {
116 addr: 0x40003c00,
117 _marker: ::core::marker::PhantomData,
118 };
119
120 /// Reset values for each field in SPI3
121 pub const reset: ResetValues = ResetValues {
122 CR1: 0x00000000,
123 CR2: 0x00000000,
124 SR: 0x00000002,
125 DR: 0x00000000,
126 CRCPR: 0x00000007,
127 RXCRCR: 0x00000000,
128 TXCRCR: 0x00000000,
129 };
130
131 #[cfg(not(feature = "nosync"))]
132 #[allow(renamed_and_removed_lints)]
133 #[allow(private_no_mangle_statics)]
134 #[no_mangle]
135 static mut SPI3_TAKEN: bool = false;
136
137 /// Safe access to SPI3
138 ///
139 /// This function returns `Some(Instance)` if this instance is not
140 /// currently taken, and `None` if it is. This ensures that if you
141 /// do get `Some(Instance)`, you are ensured unique access to
142 /// the peripheral and there cannot be data races (unless other
143 /// code uses `unsafe`, of course). You can then pass the
144 /// `Instance` around to other functions as required. When you're
145 /// done with it, you can call `release(instance)` to return it.
146 ///
147 /// `Instance` itself dereferences to a `RegisterBlock`, which
148 /// provides access to the peripheral's registers.
149 #[cfg(not(feature = "nosync"))]
150 #[inline]
151 pub fn take() -> Option<Instance> {
152 external_cortex_m::interrupt::free(|_| unsafe {
153 if SPI3_TAKEN {
154 None
155 } else {
156 SPI3_TAKEN = true;
157 Some(INSTANCE)
158 }
159 })
160 }
161
162 /// Release exclusive access to SPI3
163 ///
164 /// This function allows you to return an `Instance` so that it
165 /// is available to `take()` again. This function will panic if
166 /// you return a different `Instance` or if this instance is not
167 /// already taken.
168 #[cfg(not(feature = "nosync"))]
169 #[inline]
170 pub fn release(inst: Instance) {
171 external_cortex_m::interrupt::free(|_| unsafe {
172 if SPI3_TAKEN && inst.addr == INSTANCE.addr {
173 SPI3_TAKEN = false;
174 } else {
175 panic!("Released a peripheral which was not taken");
176 }
177 });
178 }
179
180 /// Unsafely steal SPI3
181 ///
182 /// This function is similar to take() but forcibly takes the
183 /// Instance, marking it as taken irregardless of its previous
184 /// state.
185 #[cfg(not(feature = "nosync"))]
186 #[inline]
187 pub unsafe fn steal() -> Instance {
188 SPI3_TAKEN = true;
189 INSTANCE
190 }
191}
192
193/// Raw pointer to SPI3
194///
195/// Dereferencing this is unsafe because you are not ensured unique
196/// access to the peripheral, so you may encounter data races with
197/// other users of this peripheral. It is up to you to ensure you
198/// will not cause data races.
199///
200/// This constant is provided for ease of use in unsafe code: you can
201/// simply call for example `write_reg!(gpio, GPIOA, ODR, 1);`.
202pub const SPI3: *const RegisterBlock = 0x40003c00 as *const _;