Update for RTIC 1.0.0, STM32F1xx HAL 0.8, Rust 2021
Not many changes here; the only actual code change was for the HAL update and it involved removing a couple of parameters that were no longer necessary.master
parent
519cd1d340
commit
9fdd9a4482
25
Cargo.toml
25
Cargo.toml
|
@ -4,28 +4,13 @@ version = "0.1.0"
|
||||||
authors = ["Levi Pearson <levipearson@gmail.com>"]
|
authors = ["Levi Pearson <levipearson@gmail.com>"]
|
||||||
description = "Base binary crate for STM32F103 Blue Pill boards"
|
description = "Base binary crate for STM32F103 Blue Pill boards"
|
||||||
categories = ["embedded", "no-std"]
|
categories = ["embedded", "no-std"]
|
||||||
edition = "2018"
|
edition = "2021"
|
||||||
|
|
||||||
[dependencies]
|
[dependencies]
|
||||||
cortex-m = "0.6.2"
|
cortex-m-rtic = "1.0.0"
|
||||||
cortex-m-rt = "0.6.12"
|
panic-rtt-target = { version = "0.1.2", features = ["cortex-m"] }
|
||||||
#cortex-m-semihosting = "0.3.5"
|
rtt-target = { version = "0.3.1", features = ["cortex-m"] }
|
||||||
# alternate panic impls, choose only one!
|
stm32f1xx-hal = { version = "0.8.0", features = ["rt", "stm32f103", "medium"] }
|
||||||
#panic-halt = "0.2.0"
|
|
||||||
#panic-semihosting = "0.5.3" # requires cortex-m-semihosting
|
|
||||||
#panic-itm = "0.4.1"
|
|
||||||
#panic-abort = "0.3.2"
|
|
||||||
#panic-ramdump = "0.1.1"
|
|
||||||
#panic-persist = "0.2.1"
|
|
||||||
panic-rtt-target = { version = "0.1.0", features = ["cortex-m"] }
|
|
||||||
embedded-hal = "0.2.3"
|
|
||||||
nb = "0.1.2"
|
|
||||||
rtt-target = { version = "0.2.0", features = ["cortex-m"] }
|
|
||||||
cortex-m-rtic = "0.5.0"
|
|
||||||
|
|
||||||
[dependencies.stm32f1xx-hal]
|
|
||||||
version = "0.6.1"
|
|
||||||
features = ["rt", "stm32f103", "medium"]
|
|
||||||
|
|
||||||
[[bin]]
|
[[bin]]
|
||||||
name = "blue_pill_rtic"
|
name = "blue_pill_rtic"
|
||||||
|
|
105
src/main.rs
105
src/main.rs
|
@ -7,32 +7,43 @@
|
||||||
|
|
||||||
#![deny(unsafe_code)]
|
#![deny(unsafe_code)]
|
||||||
#![no_std]
|
#![no_std]
|
||||||
#![cfg_attr(not(doc), no_main)]
|
#![no_main]
|
||||||
|
|
||||||
use rtt_target::{rprintln, rtt_init_print};
|
|
||||||
use panic_rtt_target as _;
|
use panic_rtt_target as _;
|
||||||
|
|
||||||
use stm32f1xx_hal::{
|
// RTIC requires that unused interrupts are declared in "dispatchers" when
|
||||||
|
// using software tasks; these free interrupts will be used to dispatch the
|
||||||
|
// software tasks.
|
||||||
|
//
|
||||||
|
// For a list, see:
|
||||||
|
// https://docs.rs/stm32f1xx-hal/0.6.1/stm32f1xx_hal/stm32/enum.Interrupt.html
|
||||||
|
#[rtic::app(device = stm32f1xx_hal::stm32, peripherals = true, dispatchers = [TAMPER])]
|
||||||
|
mod app {
|
||||||
|
|
||||||
|
use core::sync::atomic::{self, Ordering};
|
||||||
|
use rtt_target::{rprintln, rtt_init_print};
|
||||||
|
use stm32f1xx_hal::{
|
||||||
prelude::*,
|
prelude::*,
|
||||||
stm32,
|
stm32,
|
||||||
timer::{Timer, Event},
|
timer::{Event, Timer},
|
||||||
};
|
};
|
||||||
use core::sync::atomic::{self, Ordering};
|
|
||||||
|
|
||||||
use stm32f1xx_hal as hal;
|
use stm32f1xx_hal as hal;
|
||||||
|
|
||||||
#[rtic::app(device = stm32f1xx_hal::stm32, peripherals = true)]
|
// Defining this struct makes shared resources available to tasks;
|
||||||
const APP: () = {
|
// they will be initialized by the values returned from `init` and
|
||||||
// Defining this struct makes shared resources available to tasks; if
|
// will be wrapped in a `Mutex` and must be accessed via a closure
|
||||||
// they can't be statically initialized, they will be initialized by
|
// passed to its `lock` method.
|
||||||
// the values returned from `init`
|
// If you annotate a field with #[lock_free] you can opt-out of the
|
||||||
struct Resources {
|
// mutex but it may only be shared by tasks at the same priority.
|
||||||
// resources -- these are statically initialized via `init` attributes
|
#[shared]
|
||||||
#[init(0)]
|
struct Shared {}
|
||||||
beat: u8,
|
|
||||||
|
|
||||||
// late resources -- these must be initialized in the `init` task and
|
// This struct defines local resources (accessed by only one task);
|
||||||
// returned in `init::LateResources`
|
// they will be initialized by the values returned from `init` and
|
||||||
|
// can be accessed directly.
|
||||||
|
#[local]
|
||||||
|
struct Local {
|
||||||
led1: hal::gpio::gpioc::PC13<hal::gpio::Output<hal::gpio::PushPull>>,
|
led1: hal::gpio::gpioc::PC13<hal::gpio::Output<hal::gpio::PushPull>>,
|
||||||
tmr2: hal::timer::CountDownTimer<stm32::TIM2>,
|
tmr2: hal::timer::CountDownTimer<stm32::TIM2>,
|
||||||
tmr3: hal::timer::CountDownTimer<stm32::TIM3>,
|
tmr3: hal::timer::CountDownTimer<stm32::TIM3>,
|
||||||
|
@ -41,16 +52,14 @@ const APP: () = {
|
||||||
// This task does startup config; the peripherals are passed in thanks to
|
// This task does startup config; the peripherals are passed in thanks to
|
||||||
// `peripherals = true` in the app definition. They are the `device` and
|
// `peripherals = true` in the app definition. They are the `device` and
|
||||||
// `core` fields of `init::Context`.
|
// `core` fields of `init::Context`.
|
||||||
// Any dynamically-configured shared resources in `Resources` must be
|
|
||||||
// returned as part of `init::LateResources`.
|
|
||||||
#[init]
|
#[init]
|
||||||
fn init(cx: init::Context) -> init::LateResources {
|
fn init(cx: init::Context) -> (Shared, Local, init::Monotonics) {
|
||||||
rtt_init_print!();
|
rtt_init_print!();
|
||||||
rprintln!("init begin");
|
rprintln!("init begin");
|
||||||
|
|
||||||
// Set everything to 8MHz using the external clock
|
// Set everything to 8MHz using the external clock
|
||||||
let mut flash = cx.device.FLASH.constrain();
|
let mut flash = cx.device.FLASH.constrain();
|
||||||
let mut rcc = cx.device.RCC.constrain();
|
let rcc = cx.device.RCC.constrain();
|
||||||
let clocks = rcc
|
let clocks = rcc
|
||||||
.cfgr
|
.cfgr
|
||||||
.use_hse(8.mhz())
|
.use_hse(8.mhz())
|
||||||
|
@ -62,26 +71,20 @@ const APP: () = {
|
||||||
.freeze(&mut flash.acr);
|
.freeze(&mut flash.acr);
|
||||||
|
|
||||||
// LED is on pin C13, configure it for output
|
// LED is on pin C13, configure it for output
|
||||||
let mut gpioc = cx.device.GPIOC.split(&mut rcc.apb2);
|
let mut gpioc = cx.device.GPIOC.split();
|
||||||
let led1 = gpioc.pc13.into_push_pull_output(&mut gpioc.crh);
|
let led1 = gpioc.pc13.into_push_pull_output(&mut gpioc.crh);
|
||||||
|
|
||||||
// Use TIM2 for the beat counter task
|
// Use TIM2 for the beat counter task
|
||||||
let mut tmr2 = Timer::tim2(cx.device.TIM2, &clocks, &mut rcc.apb1)
|
let mut tmr2 = Timer::tim2(cx.device.TIM2, &clocks).start_count_down(1.hz());
|
||||||
.start_count_down(1.hz());
|
|
||||||
tmr2.listen(Event::Update);
|
tmr2.listen(Event::Update);
|
||||||
|
|
||||||
// Use TIM3 for the LED blinker task
|
// Use TIM3 for the LED blinker task
|
||||||
let mut tmr3 = Timer::tim3(cx.device.TIM3, &clocks, &mut rcc.apb1)
|
let mut tmr3 = Timer::tim3(cx.device.TIM3, &clocks).start_count_down(2.hz());
|
||||||
.start_count_down(2.hz());
|
|
||||||
tmr3.listen(Event::Update);
|
tmr3.listen(Event::Update);
|
||||||
|
|
||||||
rprintln!("init end");
|
rprintln!("init end");
|
||||||
|
|
||||||
init::LateResources {
|
(Shared {}, Local { led1, tmr2, tmr3 }, init::Monotonics())
|
||||||
led1,
|
|
||||||
tmr2,
|
|
||||||
tmr3,
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
#[idle]
|
#[idle]
|
||||||
|
@ -94,42 +97,32 @@ const APP: () = {
|
||||||
|
|
||||||
// Update the beat counter and periodically display the current count
|
// Update the beat counter and periodically display the current count
|
||||||
// on the RTT channel
|
// on the RTT channel
|
||||||
#[task(resources = [beat])]
|
// Since `beat` is a local, we can have it initialized.
|
||||||
|
#[task(local = [beat: u32 = 0])]
|
||||||
fn beat_update(cx: beat_update::Context) {
|
fn beat_update(cx: beat_update::Context) {
|
||||||
if *cx.resources.beat % 10 == 0 {
|
if *cx.local.beat % 10 == 0 {
|
||||||
rprintln!("TIM2 beat = {}", *cx.resources.beat);
|
rprintln!("TIM2 beat = {}", *cx.local.beat);
|
||||||
}
|
}
|
||||||
|
|
||||||
*cx.resources.beat += 1;
|
*cx.local.beat += 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Interrupt task for TIM2, the beat counter timer
|
// Interrupt task for TIM2, the beat counter timer
|
||||||
#[task(binds = TIM2, priority = 2, resources = [tmr2], spawn = [beat_update])]
|
#[task(binds = TIM2, priority = 2, local = [tmr2])]
|
||||||
fn tim2(cx: tim2::Context) {
|
fn tim2(cx: tim2::Context) {
|
||||||
// Delegate the state update to a software task
|
// Delegate the state update to a software task
|
||||||
cx.spawn.beat_update().unwrap();
|
beat_update::spawn().unwrap();
|
||||||
|
|
||||||
// Restart the timer and clear the interrupt flag
|
// Restart the timer and clear the interrupt flag
|
||||||
cx.resources.tmr2.start(1.hz());
|
cx.local.tmr2.start(1.hz());
|
||||||
cx.resources.tmr2.clear_update_interrupt_flag();
|
cx.local.tmr2.clear_update_interrupt_flag();
|
||||||
}
|
}
|
||||||
|
|
||||||
// Interrupt task for TIM3, the LED blink timer
|
// Interrupt task for TIM3, the LED blink timer
|
||||||
#[task(binds = TIM3, priority = 1, resources = [led1, tmr3])]
|
#[task(binds = TIM3, priority = 1, local = [led1, tmr3])]
|
||||||
fn tim3(cx: tim3::Context) {
|
fn tim3(cx: tim3::Context) {
|
||||||
cx.resources.led1.toggle().unwrap();
|
cx.local.led1.toggle();
|
||||||
cx.resources.tmr3.start(2.hz());
|
cx.local.tmr3.start(2.hz());
|
||||||
cx.resources.tmr3.clear_update_interrupt_flag();
|
cx.local.tmr3.clear_update_interrupt_flag();
|
||||||
}
|
}
|
||||||
|
}
|
||||||
// RTIC requires that unused interrupts are declared in an extern block when
|
|
||||||
// using software tasks; these free interrupts will be used to dispatch the
|
|
||||||
// software tasks.
|
|
||||||
//
|
|
||||||
// For a list, see:
|
|
||||||
// https://docs.rs/stm32f1xx-hal/0.6.1/stm32f1xx_hal/stm32/enum.Interrupt.html
|
|
||||||
extern "C" {
|
|
||||||
fn TAMPER();
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue