Skip to content

Commit ade0197

Browse files
committed
We have UART IRQs and blinking!
Turns out TIM6 isn't available on an F031, only the F051 and F061. The PAC didn't know this though.
1 parent d40ce1f commit ade0197

2 files changed

Lines changed: 44 additions & 42 deletions

File tree

src/main.rs

Lines changed: 27 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33

44
use neotron_bmc as _;
55

6-
use neotron_bmc::monotonic::{Tim6Monotonic, U16Ext};
6+
use neotron_bmc::monotonic::{Instant, Tim3Monotonic, U16Ext};
77

88
use cortex_m_rt::exception;
99

@@ -24,7 +24,7 @@ static VERSION: &'static str = include_str!(concat!(env!("OUT_DIR"), "/version.t
2424

2525
const PERIOD_MS: u16 = 1000;
2626

27-
#[app(device = crate::pac, peripherals = true, monotonic = crate::Tim6Monotonic)]
27+
#[app(device = crate::pac, peripherals = true, monotonic = crate::Tim3Monotonic)]
2828
const APP: () = {
2929
struct Resources {
3030
uart_cts: PA11<Alternate<AF1>>,
@@ -49,8 +49,8 @@ const APP: () = {
4949
.sysclk(48.mhz())
5050
.freeze(&mut flash);
5151

52-
defmt::info!("Configuring TIM6 at 7.8125 kHz...");
53-
crate::Tim6Monotonic::initialize(dp.TIM6);
52+
defmt::info!("Configuring TIM3 at 7.8125 kHz...");
53+
crate::Tim3Monotonic::initialize(dp.TIM3);
5454

5555
defmt::info!("Creating pins...");
5656
let gpioa = dp.GPIOA.split(&mut rcc);
@@ -69,7 +69,10 @@ const APP: () = {
6969

7070
defmt::info!("Creating UART...");
7171

72-
let serial = serial::Serial::usart1(dp.USART1, (uart_tx, uart_rx), 115_200.bps(), &mut rcc);
72+
let mut serial =
73+
serial::Serial::usart1(dp.USART1, (uart_tx, uart_rx), 115_200.bps(), &mut rcc);
74+
75+
serial.listen(serial::Event::Rxne);
7376

7477
ctx.spawn.led_status_blink().unwrap();
7578

@@ -90,29 +93,25 @@ const APP: () = {
9093
fn idle(_: idle::Context) -> ! {
9194
defmt::info!("Idle is running...");
9295
loop {
93-
cortex_m::asm::nop();
94-
// defmt::info!("Idle is asleep...");
95-
// cortex_m::asm::wfi();
96-
// defmt::info!("Idle is awake...");
96+
cortex_m::asm::wfi();
97+
defmt::info!("It is now {}", crate::Instant::now().counts());
98+
}
99+
}
100+
101+
#[task(binds = USART1, resources=[serial])]
102+
fn usart1_interrupt(ctx: usart1_interrupt::Context) {
103+
// Reading the register clears the RX-Not-Empty-Interrupt flag.
104+
match ctx.resources.serial.read() {
105+
Ok(b) => {
106+
defmt::info!("<< UART {:x}", b);
107+
}
108+
Err(_) => {
109+
defmt::warn!("<< UART None?");
110+
}
97111
}
98112
}
99113

100-
// #[task(binds = USART1, resources=[serial])]
101-
// fn usart1_interrupt(ctx: usart1_interrupt::Context) {
102-
// defmt::info!("USART1 IRQ!");
103-
// // Reading the register clears the RX-Not-Empty-Interrupt flag.
104-
// match ctx.resources.serial.read()
105-
// {
106-
// Ok(b) => {
107-
// defmt::info!("Read byte {:x}", b);
108-
// }
109-
// Err(_) => {
110-
// defmt::warn!("No byte available?");
111-
// }
112-
// }
113-
// }
114-
115-
#[task(resources = [led_status], schedule = [led_status_blink])]
114+
#[task(schedule = [led_status_blink], resources = [led_status])]
116115
fn led_status_blink(ctx: led_status_blink::Context) {
117116
// Use the safe local `static mut` of RTIC
118117
static mut LED_STATE: bool = false;
@@ -126,9 +125,9 @@ const APP: () = {
126125
ctx.resources.led_status.set_high().unwrap();
127126
*LED_STATE = true;
128127
}
129-
ctx.schedule
130-
.led_status_blink(ctx.scheduled + PERIOD_MS.millis())
131-
.unwrap();
128+
let next = ctx.scheduled + PERIOD_MS.millis();
129+
defmt::info!("Next blink at {}", next.counts());
130+
ctx.schedule.led_status_blink(next).unwrap();
132131
}
133132

134133
// Let it use the USB interrupt as a generic software interrupt.

src/monotonic.rs

Lines changed: 17 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
//! Using STM32F0 TIM6 as monotonic 16 bit timer.
1+
//! Using STM32F0 TIM3 as monotonic 16 bit timer.
22
//!
33
//! ## Prescaler Calculations
44
//!
@@ -25,34 +25,37 @@ use stm32f0xx_hal::pac;
2525
/// Implementor of the `rtic::Monotonic` traits and used to consume the timer
2626
/// to not allow for erroneous configuration.
2727
///
28-
/// This uses TIM6 internally.
29-
pub struct Tim6Monotonic;
28+
/// This uses TIM3 internally.
29+
pub struct Tim3Monotonic;
3030

3131
const CORE_CLOCK: u32 = 48_000_000;
3232
const PRESCALER: u32 = 6144;
3333
const HZ: u32 = CORE_CLOCK / PRESCALER;
3434

35-
impl Tim6Monotonic {
35+
impl Tim3Monotonic {
3636
/// Initialize the timer instance.
37-
pub fn initialize(timer: pac::TIM6) {
38-
// Enable and reset TIM6 in RCC
37+
pub fn initialize(timer: pac::TIM3) {
38+
// Enable and reset TIM3 in RCC
3939
//
40-
// Correctness: Since we only modify TIM6 related registers in the RCC
41-
// register block, and since we own pac::TIM6, we should be safe.
40+
// Correctness: Since we only modify TIM3 related registers in the RCC
41+
// register block, and since we own pac::TIM3, we should be safe.
4242
unsafe {
4343
let rcc = &*pac::RCC::ptr();
4444

4545
// Enable timer
46-
rcc.apb1enr.modify(|_, w| w.tim6en().set_bit());
46+
rcc.apb1enr.modify(|_, w| w.tim3en().set_bit());
4747

4848
// Reset timer
49-
rcc.apb1rstr.modify(|_, w| w.tim6rst().set_bit());
50-
rcc.apb1rstr.modify(|_, w| w.tim6rst().clear_bit());
49+
rcc.apb1rstr.modify(|_, w| w.tim3rst().set_bit());
50+
rcc.apb1rstr.modify(|_, w| w.tim3rst().clear_bit());
5151
}
5252

5353
// Set up prescaler
5454
timer.psc.write(|w| w.psc().bits(PRESCALER as u16));
5555

56+
// Update prescaler
57+
timer.egr.write(|w| w.ug().update());
58+
5659
// Enable counter
5760
timer.cr1.modify(|_, w| w.cen().set_bit());
5861

@@ -61,7 +64,7 @@ impl Tim6Monotonic {
6164
}
6265
}
6366

64-
impl Monotonic for Tim6Monotonic {
67+
impl Monotonic for Tim3Monotonic {
6568
type Instant = Instant;
6669

6770
fn ratio() -> rtic::Fraction {
@@ -91,7 +94,7 @@ impl Monotonic for Tim6Monotonic {
9194
/// before tasks can start; this is also the case in multi-core applications. User code must
9295
/// *never* call this function.
9396
unsafe fn reset() {
94-
let tim = &*pac::TIM6::ptr();
97+
let tim = &*pac::TIM3::ptr();
9598

9699
// Pause
97100
tim.cr1.modify(|_, w| w.cen().clear_bit());
@@ -122,7 +125,7 @@ impl Instant {
122125
/// Returns an instant corresponding to "now".
123126
pub fn now() -> Self {
124127
let now = {
125-
let tim = unsafe { &*pac::TIM6::ptr() };
128+
let tim = unsafe { &*pac::TIM3::ptr() };
126129
tim.cnt.read().cnt().bits()
127130
};
128131

0 commit comments

Comments
 (0)