stm32f30x_memory_map/
lib.rs

1//! Memory map for STM32F30X microcontrollers
2
3#![deny(missing_docs)]
4#![deny(warnings)]
5#![no_std]
6
7extern crate volatile_register;
8
9#[allow(missing_docs)]
10pub mod btim;
11#[allow(missing_docs)]
12pub mod dbgmcu;
13#[allow(missing_docs)]
14pub mod gpio;
15#[allow(missing_docs)]
16pub mod gptim;
17#[allow(missing_docs)]
18pub mod i2c;
19#[allow(missing_docs)]
20pub mod rcc;
21#[allow(missing_docs)]
22pub mod spi;
23#[allow(missing_docs)]
24pub mod usart;
25
26use btim::BTim;
27use dbgmcu::Dbgmcu;
28use gpio::Gpio;
29use gptim::GpTim;
30use i2c::I2c;
31use rcc::Rcc;
32use spi::Spi;
33use usart::Usart;
34
35const GPIOA: usize = 0x48000000;
36const GPIOB: usize = 0x48000400;
37const GPIOC: usize = 0x48000800;
38const GPIOD: usize = 0x48000c00;
39const GPIOE: usize = 0x48001000;
40const GPIOF: usize = 0x48001400;
41const GPIOG: usize = 0x48001800;
42const GPIOH: usize = 0x48001c00;
43// const TSC: usize = 0x40024000;
44// const CRC: usize = 0x40023000;
45// const Flash: usize = 0x40022000;
46const RCC: usize = 0x40021000;
47// const DMA1: usize = 0x40020000;
48// const DMA2: usize = 0x40020400;
49const TIM2: usize = 0x40000000;
50const TIM3: usize = 0x40000400;
51const TIM4: usize = 0x40000800;
52// const TIM15: usize = 0x40014000;
53// const TIM16: usize = 0x40014400;
54// const TIM17: usize = 0x40014800;
55const USART1: usize = 0x40013800;
56const USART2: usize = 0x40004400;
57const USART3: usize = 0x40004800;
58// const UART4: usize = 0x40004c00;
59// const UART5: usize = 0x40005000;
60const SPI1: usize = 0x40013000;
61// const SPI2: usize = 0x40003800;
62// const SPI3: usize = 0x40003c00;
63// const I2S2ext: usize = 0x40003400;
64// const I2S3ext: usize = 0x40004000;
65// const EXTI: usize = 0x40010400;
66// const COMP: usize = 0x4001001c;
67// const PWR: usize = 0x40007000;
68// const CAN: usize = 0x40006400;
69// const USB_FS: usize = 0x40005c00;
70const I2C1: usize = 0x40005400;
71// const I2C2: usize = 0x40005800;
72// const IWDG: usize = 0x40003000;
73// const WWDG: usize = 0x40002c00;
74// const RTC: usize = 0x40002800;
75const TIM6: usize = 0x40001000;
76const TIM7: usize = 0x40001400;
77// const DAC: usize = 0x40007400;
78// const NVIC: usize = 0xe000e000;
79// const FPU: usize = 0xe000ed88;
80const DBGMCU: usize = 0xe0042000;
81// const TIM1: usize = 0x40012c00;
82// const TIM8: usize = 0x40013400;
83// const ADC1: usize = 0x50000000;
84// const ADC2: usize = 0x50000100;
85// const ADC3: usize = 0x50000400;
86// const ADC4: usize = 0x50000500;
87// const ADC1_2: usize = 0x50000300;
88// const ADC3_4: usize = 0x50000700;
89// const SYSCFG: usize = 0x40010000;
90// const OPAMP: usize = 0x40010038;
91
92/// DBGMCU register block (&'static)
93pub fn dbgmcu() -> &'static Dbgmcu {
94    unsafe { deref(DBGMCU) }
95}
96
97/// DBGMCU register block (&'static mut)
98pub unsafe fn dbgmcu_mut() -> &'static mut Dbgmcu {
99    deref_mut(DBGMCU)
100}
101
102/// GPIOA register block (&'static)
103pub fn gpioa() -> &'static Gpio {
104    unsafe { deref(GPIOA) }
105}
106
107/// GPIOA register block (&'static mut)
108pub unsafe fn gpioa_mut() -> &'static mut Gpio {
109    deref_mut(GPIOA)
110}
111
112/// GPIOB register block (&'static)
113pub fn gpiob() -> &'static Gpio {
114    unsafe { deref(GPIOB) }
115}
116
117/// GPIOB register block (&'static mut)
118pub unsafe fn gpiob_mut() -> &'static mut Gpio {
119    deref_mut(GPIOB)
120}
121
122/// GPIOC register block (&'static)
123pub fn gpioc() -> &'static Gpio {
124    unsafe { deref(GPIOC) }
125}
126
127/// GPIOC register block (&'static mut)
128pub unsafe fn gpioc_mut() -> &'static mut Gpio {
129    deref_mut(GPIOC)
130}
131
132/// GPIOD register block (&'static)
133pub fn gpiod() -> &'static Gpio {
134    unsafe { deref(GPIOD) }
135}
136
137/// GPIOD register block (&'static mut)
138pub unsafe fn gpiod_mut() -> &'static mut Gpio {
139    deref_mut(GPIOD)
140}
141
142/// GPIOE register block (&'static)
143pub fn gpioe() -> &'static Gpio {
144    unsafe { deref(GPIOE) }
145}
146
147/// GPIOE register block (&'static mut)
148pub unsafe fn gpioe_mut() -> &'static mut Gpio {
149    deref_mut(GPIOE)
150}
151
152/// GPIOF register block (&'static)
153pub fn gpiof() -> &'static Gpio {
154    unsafe { deref(GPIOF) }
155}
156
157/// GPIOF register block (&'static mut)
158pub unsafe fn gpiof_mut() -> &'static mut Gpio {
159    deref_mut(GPIOF)
160}
161
162/// GPIOG register block (&'static)
163pub fn gpiog() -> &'static Gpio {
164    unsafe { deref(GPIOG) }
165}
166
167/// GPIOG register block (&'static mut)
168pub unsafe fn gpiog_mut() -> &'static mut Gpio {
169    deref_mut(GPIOG)
170}
171
172/// GPIOH register block (&'static)
173pub fn gpioh() -> &'static Gpio {
174    unsafe { deref(GPIOH) }
175}
176
177/// GPIOH register block (&'static mut)
178pub unsafe fn gpioh_mut() -> &'static mut Gpio {
179    deref_mut(GPIOH)
180}
181
182/// I2C1 register block (&'static)
183pub fn i2c1() -> &'static I2c {
184    unsafe { deref(I2C1) }
185}
186
187/// I2C1 register block (&'static mut)
188pub unsafe fn i2c1_mut() -> &'static mut I2c {
189    deref_mut(I2C1)
190}
191
192/// RCC register block (&'static)
193pub fn rcc() -> &'static Rcc {
194    unsafe { deref(RCC) }
195}
196
197/// RCC register block (&'static mut)
198pub unsafe fn rcc_mut() -> &'static mut Rcc {
199    deref_mut(RCC)
200}
201
202/// SPI1 register block (&'static)
203pub fn spi1() -> &'static Spi {
204    unsafe { deref(SPI1) }
205}
206
207/// SPI1 register block (&'static mut)
208pub unsafe fn spi1_mut() -> &'static mut Spi {
209    deref_mut(SPI1)
210}
211
212/// TIM2 register block (&'static)
213pub fn tim2() -> &'static GpTim {
214    unsafe { deref(TIM2) }
215}
216
217/// TIM2 register block (&'static mut)
218pub unsafe fn tim2_mut() -> &'static mut GpTim {
219    deref_mut(TIM2)
220}
221
222/// TIM3 register block (&'static)
223pub fn tim3() -> &'static GpTim {
224    unsafe { deref(TIM3) }
225}
226
227/// TIM3 register block (&'static mut)
228pub unsafe fn tim3_mut() -> &'static mut GpTim {
229    deref_mut(TIM3)
230}
231
232/// TIM4 register block (&'static)
233pub fn tim4() -> &'static GpTim {
234    unsafe { deref(TIM4) }
235}
236
237/// TIM4 register block (&'static mut)
238pub unsafe fn tim4_mut() -> &'static mut GpTim {
239    deref_mut(TIM4)
240}
241
242/// TIM6 register block (&'static)
243pub fn tim6() -> &'static BTim {
244    unsafe { deref(TIM6) }
245}
246
247/// TIM6 register block (&'static mut)
248pub unsafe fn tim6_mut() -> &'static mut BTim {
249    deref_mut(TIM6)
250}
251
252/// TIM7 register block (&'static)
253pub fn tim7() -> &'static BTim {
254    unsafe { deref(TIM7) }
255}
256
257/// TIM7 register block (&'static mut)
258pub unsafe fn tim7_mut() -> &'static mut BTim {
259    deref_mut(TIM7)
260}
261
262/// USART1 register block (&'static)
263pub fn usart1() -> &'static Usart {
264    unsafe { deref(USART1) }
265}
266
267/// USART1 register block (&'static mut)
268pub unsafe fn usart1_mut() -> &'static mut Usart {
269    deref_mut(USART1)
270}
271
272/// USART2 register block (&'static)
273pub fn usart2() -> &'static Usart {
274    unsafe { deref(USART2) }
275}
276
277/// USART2 register block (&'static mut)
278pub unsafe fn usart2_mut() -> &'static mut Usart {
279    deref_mut(USART2)
280}
281
282/// USART3 register block (&'static)
283pub fn usart3() -> &'static Usart {
284    unsafe { deref(USART3) }
285}
286
287/// USART3 register block (&'static mut)
288pub unsafe fn usart3_mut() -> &'static mut Usart {
289    deref_mut(USART3)
290}
291
292unsafe fn deref<T>(address: usize) -> &'static T {
293    &*(address as *const T)
294}
295
296unsafe fn deref_mut<T>(address: usize) -> &'static mut T {
297    &mut *(address as *mut T)
298}
299
300// Here we extend the peripheral API -- AKA ~~svd2rust is~~ SVD files are great
301// but not perfect
302use core::ptr;
303
304impl spi::Dr {
305    /// Reads a byte (`u8`) from this register
306    pub fn read_u8(&self) -> u8 {
307        unsafe { ptr::read_volatile(self as *const _ as *const u8) }
308    }
309
310    /// Writes a byte (`u8`) to this register
311    pub fn write_u8(&mut self, value: u8) {
312        unsafe { ptr::write_volatile(self as *mut _ as *mut u8, value) }
313    }
314}