|
3 | 3 |
|
4 | 4 | extern crate panic_halt;
|
5 | 5 | use cortex_m::peripheral::syst::SystClkSource;
|
6 |
| -use cortex_m_rt::{entry}; |
7 |
| -use cortex_m_semihosting::{hprintln}; |
| 6 | +use cortex_m_rt::entry; |
| 7 | +use cortex_m_semihosting::hprintln; |
8 | 8 |
|
9 | 9 | use f3::{
|
10 |
| - hal::{ |
11 |
| - i2c::I2c, |
12 |
| - prelude::*, |
13 |
| - stm32f30x |
14 |
| - }, |
| 10 | + hal::{i2c::I2c, prelude::*, stm32f30x}, |
15 | 11 | led::Leds,
|
16 |
| - Lsm303dlhc, |
| 12 | + Lsm303dlhc, |
17 | 13 | };
|
18 | 14 |
|
19 |
| -use cortexm_threads::{init, create_thread_with_config, sleep}; |
| 15 | +use cortexm_threads::{create_thread_with_config, init, sleep}; |
20 | 16 |
|
21 | 17 | static mut LEDS: Option<Leds> = None;
|
22 | 18 | static mut SENSOR: Option<Lsm303dlhc> = None;
|
23 | 19 |
|
24 | 20 | #[entry]
|
25 | 21 | fn main() -> ! {
|
26 | 22 | let cp = cortex_m::Peripherals::take().unwrap();
|
27 |
| - let dp = stm32f30x::Peripherals::take().unwrap(); |
28 |
| - |
29 |
| - let mut rcc = dp.RCC.constrain(); |
30 |
| - let leds = Leds::new(dp.GPIOE.split(&mut rcc.ahb)); |
31 |
| - unsafe { |
32 |
| - LEDS = Some(leds); |
33 |
| - } |
34 |
| - |
35 |
| - let mut gpiob = dp.GPIOB.split(&mut rcc.ahb); |
| 23 | + let dp = stm32f30x::Peripherals::take().unwrap(); |
| 24 | + |
| 25 | + let mut rcc = dp.RCC.constrain(); |
| 26 | + let leds = Leds::new(dp.GPIOE.split(&mut rcc.ahb)); |
| 27 | + unsafe { |
| 28 | + LEDS = Some(leds); |
| 29 | + } |
| 30 | + |
| 31 | + let mut gpiob = dp.GPIOB.split(&mut rcc.ahb); |
36 | 32 | let scl = gpiob.pb6.into_af4(&mut gpiob.moder, &mut gpiob.afrl);
|
37 | 33 | let sda = gpiob.pb7.into_af4(&mut gpiob.moder, &mut gpiob.afrl);
|
38 | 34 |
|
39 |
| - let mut flash = dp.FLASH.constrain(); |
40 |
| - let clocks = rcc.cfgr.freeze(&mut flash.acr); |
| 35 | + let mut flash = dp.FLASH.constrain(); |
| 36 | + let clocks = rcc.cfgr.freeze(&mut flash.acr); |
41 | 37 | let i2c = I2c::i2c1(dp.I2C1, (scl, sda), 400.khz(), clocks, &mut rcc.apb1);
|
42 |
| - unsafe { |
43 |
| - SENSOR = Some(Lsm303dlhc::new(i2c).unwrap()); |
44 |
| - } |
45 |
| - |
46 |
| - let mut syst = cp.SYST; |
| 38 | + unsafe { |
| 39 | + SENSOR = Some(Lsm303dlhc::new(i2c).unwrap()); |
| 40 | + } |
| 41 | + |
| 42 | + let mut syst = cp.SYST; |
47 | 43 | // configures the system timer to trigger a SysTick exception every second
|
48 | 44 | syst.set_clock_source(SystClkSource::Core);
|
49 | 45 | // tick every 12.5ms
|
50 | 46 | syst.set_reload(100_000);
|
51 | 47 | syst.enable_counter();
|
52 | 48 | syst.enable_interrupt();
|
53 | 49 |
|
54 |
| - let mut stack1 = [0xDEADBEEF; 1024]; |
| 50 | + let mut stack1 = [0xDEADBEEF; 1024]; |
55 | 51 | let mut stack2 = [0xDEADBEEF; 1024];
|
56 | 52 | let _ = create_thread_with_config(&mut stack1, user_task_1, 0xff, true);
|
57 | 53 | let _ = create_thread_with_config(&mut stack2, user_task_2, 0x00, false);
|
58 | 54 | init();
|
59 | 55 | }
|
60 | 56 |
|
61 | 57 | pub fn user_task_1() -> ! {
|
62 |
| - loop { |
63 |
| - if unsafe { LEDS.is_some() } { |
64 |
| - let leds = unsafe { LEDS.as_mut().unwrap() }; |
65 |
| - for curr in 0..8 { |
66 |
| - let next = (curr + 1) % 8; |
67 |
| - |
68 |
| - leds[next].on(); |
69 |
| - sleep(4); |
70 |
| - leds[curr].off(); |
71 |
| - sleep(4); |
72 |
| - } |
73 |
| - } |
74 |
| - } |
| 58 | + loop { |
| 59 | + if unsafe { LEDS.is_some() } { |
| 60 | + let leds = unsafe { LEDS.as_mut().unwrap() }; |
| 61 | + for curr in 0..8 { |
| 62 | + let next = (curr + 1) % 8; |
| 63 | + |
| 64 | + leds[next].on(); |
| 65 | + sleep(4); |
| 66 | + leds[curr].off(); |
| 67 | + sleep(4); |
| 68 | + } |
| 69 | + } |
| 70 | + } |
75 | 71 | }
|
76 | 72 |
|
77 | 73 | pub fn user_task_2() -> ! {
|
78 |
| - loop { |
79 |
| - if unsafe { SENSOR.is_some() } { |
80 |
| - let sensor = unsafe { SENSOR.as_mut().unwrap() }; |
81 |
| - let x = sensor.mag(); |
82 |
| - let _ = hprintln!("{:?}", x); |
83 |
| - sleep(50); |
84 |
| - } |
85 |
| - } |
| 74 | + loop { |
| 75 | + if unsafe { SENSOR.is_some() } { |
| 76 | + let sensor = unsafe { SENSOR.as_mut().unwrap() }; |
| 77 | + let x = sensor.mag(); |
| 78 | + let _ = hprintln!("{:?}", x); |
| 79 | + sleep(50); |
| 80 | + } |
| 81 | + } |
86 | 82 | }
|
0 commit comments