Skip to content

Commit 51f4b4e

Browse files
authored
Merge pull request #16 from japaric/device
expand the device example
2 parents 3f66a58 + 8890c46 commit 51f4b4e

File tree

1 file changed

+45
-10
lines changed

1 file changed

+45
-10
lines changed

examples/device.rs

+45-10
Original file line numberDiff line numberDiff line change
@@ -24,28 +24,57 @@
2424
//!
2525
//! ---
2626
27+
#![deny(warnings)]
28+
#![feature(const_fn)]
2729
#![no_std]
2830

2931
extern crate cortex_m;
32+
extern crate cortex_m_semihosting;
3033
#[macro_use(exception, interrupt)]
3134
extern crate stm32f103xx;
3235

33-
use cortex_m::interrupt;
36+
use core::cell::RefCell;
37+
use core::fmt::Write;
38+
39+
use cortex_m::interrupt::{self, Mutex};
40+
use cortex_m::peripheral::SystClkSource;
41+
use cortex_m_semihosting::hio::{self, HStdout};
42+
use stm32f103xx::Interrupt;
43+
44+
static HSTDOUT: Mutex<RefCell<Option<HStdout>>> =
45+
Mutex::new(RefCell::new(None));
3446

3547
fn main() {
3648
interrupt::free(|cs| {
37-
let _gpioa = stm32f103xx::GPIOA.borrow(cs);
38-
// do something with GPIOA
49+
let hstdout = HSTDOUT.borrow(cs);
50+
if let Ok(fd) = hio::hstdout() {
51+
*hstdout.borrow_mut() = Some(fd);
52+
}
53+
54+
let nvic = stm32f103xx::NVIC.borrow(cs);
55+
nvic.enable(Interrupt::TIM2);
56+
57+
let syst = stm32f103xx::SYST.borrow(cs);
58+
syst.set_clock_source(SystClkSource::Core);
59+
syst.set_reload(8_000_000); // 1s
60+
syst.enable_counter();
61+
syst.enable_interrupt();
3962
});
4063
}
4164

42-
exception!(SYS_TICK, tick, locals: {
43-
ticks: u32 = 0;
44-
});
65+
exception!(SYS_TICK, tick);
66+
67+
fn tick() {
68+
interrupt::free(|cs| {
69+
let hstdout = HSTDOUT.borrow(cs);
70+
if let Some(hstdout) = hstdout.borrow_mut().as_mut() {
71+
writeln!(*hstdout, "Tick").ok();
72+
}
73+
74+
let nvic = stm32f103xx::NVIC.borrow(cs);
4575

46-
fn tick(l: &mut SYS_TICK::Locals) {
47-
l.ticks += 1;
48-
// ..
76+
nvic.set_pending(Interrupt::TIM2);
77+
});
4978
}
5079

5180
interrupt!(TIM2, tock, locals: {
@@ -54,5 +83,11 @@ interrupt!(TIM2, tock, locals: {
5483

5584
fn tock(l: &mut TIM2::Locals) {
5685
l.tocks += 1;
57-
// ..
86+
87+
interrupt::free(|cs| {
88+
let hstdout = HSTDOUT.borrow(cs);
89+
if let Some(hstdout) = hstdout.borrow_mut().as_mut() {
90+
writeln!(*hstdout, "Tock ({})", l.tocks).ok();
91+
}
92+
});
5893
}

0 commit comments

Comments
 (0)