-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathmain.rs
201 lines (190 loc) · 5.91 KB
/
main.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
#![deny(warnings)]
#![no_std]
#![no_main]
#![feature(core_intrinsics)]
use core::fmt::Write;
use core::intrinsics;
use core::panic::PanicInfo;
use cortex_m_rt::{entry, exception, ExceptionFrame};
use hal::pac::interrupt;
use hal::prelude::*;
use hal::serial;
use hal::time::Bps;
use bmp280::{self, BMP280};
use lsm303c::Lsm303c;
use shared_bus::CortexMBusManager as SharedBus;
static mut L: Option<hal::serial::Tx<hal::pac::USART1>> = None;
static mut RX: Option<hal::serial::Rx<hal::pac::USART1>> = None;
static mut QUIET: bool = true;
const TURN_QUIET: u8 = 'q' as u8;
#[entry]
fn main() -> ! {
let device = hal::pac::Peripherals::take().unwrap();
let core = cortex_m::Peripherals::take().unwrap();
let mut rcc = device.RCC.constrain();
let mut flash = device.FLASH.constrain();
let clocks = rcc
.cfgr
.sysclk(64.mhz())
.pclk1(32.mhz())
.pclk2(32.mhz())
.freeze(&mut flash.acr);
let gpioa = device.GPIOA.split(&mut rcc.ahb);
let gpiob = device.GPIOB.split(&mut rcc.ahb);
let mut serial =
device
.USART1
.serial((gpioa.pa9, gpioa.pa10), Bps(115200), clocks);
let ser_int = serial.get_interrupt();
serial.listen(serial::Event::Rxne);
let (mut tx, rx) = serial.split();
// COBS frame
tx.write(0x00).unwrap();
unsafe {
L = Some(tx);
RX = Some(rx);
};
let l = unsafe { extract(&mut L) };
write!(l, "logger ok\r\n").unwrap();
// I2C
let i2c = device.I2C1.i2c((gpiob.pb6, gpiob.pb7), 400.khz(), clocks);
write!(l, "i2c ok\r\n").unwrap();
let bus = SharedBus::new(i2c);
write!(l, "i2c shared\r\n").unwrap();
// lsm
let mut lsm303 = Lsm303c::default(bus.acquire()).expect("lsm error");
write!(l, "lsm ok\r\n").unwrap();
// bmp
let mut bmp = BMP280::new(bus.acquire()).expect("bmp error");
write!(l, "bmp created\r\n").unwrap();
bmp.reset();
bmp.set_config(bmp280::Config {
t_sb: bmp280::Standby::ms250,
filter: bmp280::Filter::c8,
});
bmp.set_control(bmp280::Control {
osrs_t: bmp280::Oversampling::x1,
osrs_p: bmp280::Oversampling::x1,
mode: bmp280::PowerMode::Forced,
});
write!(l, "bmp ok\r\n").unwrap();
// done
unsafe { cortex_m::interrupt::enable() };
unsafe { cortex_m::peripheral::NVIC::unmask(ser_int) };
write!(l, "All ok; Press 'q' to toggle verbosity!\r\n").unwrap();
loop {
match lsm303.all() {
Ok(meas) => {
if unsafe { !QUIET } {
let pressure = bmp.pressure();
let temp = bmp.temp();
bmp.set_control(bmp280::Control {
osrs_t: bmp280::Oversampling::x1,
osrs_p: bmp280::Oversampling::x1,
mode: bmp280::PowerMode::Forced,
});
write!(
l,
"lsm: mag({},{},{}); a({},{},{}); t({}); bmp: ps({}), t({})\r\n",
meas.mag.x,
meas.mag.y,
meas.mag.z,
meas.accel.x,
meas.accel.y,
meas.accel.z,
meas.temp,
pressure,
temp
)
.unwrap();
}
}
Err(e) => {
write!(l, "Err meas: {:?}", e).unwrap();
}
}
}
}
unsafe fn extract<T>(opt: &'static mut Option<T>) -> &'static mut T {
match opt {
Some(ref mut x) => &mut *x,
None => panic!("extract"),
}
}
#[interrupt]
fn USART1_EXTI25() {
let rx = unsafe { extract(&mut RX) };
let l = unsafe { extract(&mut L) };
match rx.read() {
Ok(b) => {
if b == TURN_QUIET {
unsafe {
QUIET = !QUIET;
}
} else {
// echo byte as is
write!(l, "{}", b as char).unwrap();
}
}
Err(nb::Error::WouldBlock) => {}
Err(nb::Error::Other(e)) => match e {
serial::Error::Overrun => {
rx.clear_overrun_error();
}
serial::Error::Framing => {
rx.clear_framing_error();
}
serial::Error::Noise => {
rx.clear_noise_error();
}
_ => {
write!(l, "read error: {:?}", e).unwrap();
}
},
};
}
#[exception]
fn HardFault(ef: &ExceptionFrame) -> ! {
panic!("HardFault at {:#?}", ef);
}
#[exception]
fn DefaultHandler(irqn: i16) {
panic!("Unhandled exception (IRQn = {})", irqn);
}
#[panic_handler]
fn panic(panic_info: &PanicInfo) -> ! {
match unsafe { &mut L } {
Some(ref mut l) => {
let payload = panic_info.payload().downcast_ref::<&str>();
match (panic_info.location(), payload) {
(Some(location), Some(msg)) => {
write!(
l,
"\r\npanic in file '{}' at line {}: {:?}\r\n",
location.file(),
location.line(),
msg
)
.unwrap();
}
(Some(location), None) => {
write!(
l,
"panic in file '{}' at line {}",
location.file(),
location.line()
)
.unwrap();
}
(None, Some(msg)) => {
write!(l, "panic: {:?}", msg).unwrap();
}
(None, None) => {
write!(l, "panic occured, no info available").unwrap();
}
}
}
None => {}
}
unsafe { intrinsics::abort() }
}