Skip to content
Snippets Groups Projects
Commit 046d614b authored by Jorge Aparicio's avatar Jorge Aparicio
Browse files

update examples

parent 2ce1b573
No related branches found
No related tags found
No related merge requests found
Showing with 161 additions and 558 deletions
[target.thumbv7m-none-eabi]
runner = 'arm-none-eabi-gdb'
rustflags = [
"-C", "link-arg=-Tlink.x",
"-C", "linker=arm-none-eabi-ld",
"-Z", "linker-flavor=ld",
]
[build]
target = "thumbv7m-none-eabi"
target remote :3333
monitor arm semihosting enable
load
step
**/*.rs.bk
*.org
.gdb_history
Cargo.lock
target/
......@@ -2,6 +2,8 @@ language: rust
matrix:
include:
- env: TARGET=x86_64-unknown-linux-gnu
rust: nightly
- env: TARGET=thumbv7m-none-eabi
rust: nightly
addons:
......
[dependencies.core]
stage = 0
[dependencies.compiler_builtins]
features = ["mem"]
stage = 1
\ No newline at end of file
set -euxo pipefail
main() {
local src=$(pwd)
local td=$(mktemp -d)
local version=0.1.8
local url=https://github.com/japaric/cortex-m-quickstart/archive/v$version.tar.gz
if [ $TARGET = x86_64-unknown-linux-gnu ]; then
cargo build --target $TARGET
return
fi
pushd $td
curl -L $url | tar --strip-components 1 -xz
rm -rf build.rs examples memory.x src
ln -s $src/examples .
cat >>Cargo.toml <<EOF
[dependencies.blue-pill]
path = "$src"
[dependencies.embedded-hal]
git = "https://github.com/japaric/embedded-hal"
rev = "5295697669f5b48a900aa325b8ebb4d4e8d4b236"
[dependencies.cortex-m-rtfm]
version = "0.1.1"
[dependencies.futures]
default-features = false
version = "0.1.14"
[dependencies.nb]
git = "https://github.com/japaric/nb"
EOF
for path in $(ls examples/*); do
local ex=$(basename $path)
ex=${ex%.*}
case $ex in
*-await)
continue
;;
esac
xargo check --example $ex --target $TARGET
done
popd
rm -rf $td
xargo build --target $TARGET
xargo test --target $TARGET --examples
}
main
//! Blinky using `await!`
#![allow(unreachable_code)] // for the `await!` macro
#![deny(unsafe_code)]
#![deny(warnings)]
#![feature(const_fn)]
#![feature(used)]
#![no_std]
extern crate blue_pill;
extern crate embedded_hal as hal;
// version = "0.2.3"
extern crate cortex_m_rt;
// version = "0.1.0"
#[macro_use]
extern crate cortex_m_rtfm as rtfm;
extern crate futures;
#[macro_use]
extern crate nb;
use blue_pill::led::{self, Green};
use blue_pill::time::Hertz;
use blue_pill::{Timer, stm32f103xx};
use futures::future::{self, Loop};
use futures::{Async, Future};
use hal::prelude::*;
use rtfm::{P0, T0, TMax};
// CONFIGURATION
const FREQUENCY: Hertz = Hertz(1);
// RESOURCES
peripherals!(stm32f103xx, {
GPIOC: Peripheral {
ceiling: C0,
},
RCC: Peripheral {
ceiling: C0,
},
TIM3: Peripheral {
ceiling: C0,
},
});
// INITIALIZATION PHASE
fn init(ref prio: P0, thr: &TMax) {
let gpioc = &GPIOC.access(prio, thr);
let rcc = &RCC.access(prio, thr);
let tim3 = TIM3.access(prio, thr);
let timer = Timer(&*tim3);
led::init(gpioc, rcc);
timer.init(FREQUENCY.invert(), rcc);
}
// IDLE LOOP
fn idle(ref prio: P0, ref thr: T0) -> ! {
let tim3 = TIM3.access(prio, thr);
let timer = Timer(&*tim3);
// Tasks
let mut blinky = (|| {
let mut state = false;
loop {
await!(timer.wait()).unwrap(); // NOTE E = !
state != state;
if state {
Green.on();
} else {
Green.off();
}
}
})();
// Event loop
timer.resume();
loop {
blinky.resume();
}
}
// TASKS
tasks!(stm32f103xx, {});
......@@ -17,7 +17,7 @@ use blue_pill::Timer;
use blue_pill::led::{self, Green};
use blue_pill::prelude::*;
use blue_pill::time::Hertz;
use rtfm::app;
use rtfm::{app, Threshold};
const FREQUENCY: Hertz = Hertz(1);
......@@ -37,7 +37,7 @@ fn init(p: init::Peripherals) {
timer.init(FREQUENCY.invert(), p.RCC);
}
fn idle(r: idle::Resources) -> ! {
fn idle(_t: &mut Threshold, r: idle::Resources) -> ! {
let timer = Timer(&*r.TIM3);
timer.resume();
......
//! Blinky using futures
#![allow(unreachable_code)] // for the `try_nb!` macro
#![deny(unsafe_code)]
#![deny(warnings)]
#![feature(const_fn)]
#![feature(used)]
#![no_std]
extern crate blue_pill;
extern crate embedded_hal as hal;
// version = "0.2.3"
extern crate cortex_m_rt;
// version = "0.1.0"
#[macro_use]
extern crate cortex_m_rtfm as rtfm;
extern crate futures;
#[macro_use]
extern crate nb;
use blue_pill::led::{self, Green};
use blue_pill::time::Hertz;
use blue_pill::{Timer, stm32f103xx};
use futures::future::{self, Loop};
use futures::{Async, Future};
use hal::prelude::*;
use rtfm::{P0, T0, TMax};
// CONFIGURATION
const FREQUENCY: Hertz = Hertz(1);
// RESOURCES
peripherals!(stm32f103xx, {
GPIOC: Peripheral {
ceiling: C0,
},
RCC: Peripheral {
ceiling: C0,
},
TIM3: Peripheral {
ceiling: C0,
},
});
// INITIALIZATION PHASE
fn init(ref prio: P0, thr: &TMax) {
let gpioc = &GPIOC.access(prio, thr);
let rcc = &RCC.access(prio, thr);
let tim3 = TIM3.access(prio, thr);
let timer = Timer(&*tim3);
led::init(gpioc, rcc);
timer.init(FREQUENCY.invert(), rcc);
}
// IDLE LOOP
fn idle(ref prio: P0, ref thr: T0) -> ! {
let tim3 = TIM3.access(prio, thr);
let timer = Timer(&*tim3);
// Tasks
let mut blinky = future::loop_fn::<_, (), _, _>(true, |state| {
future::poll_fn(move || Ok(Async::Ready(try_nb!(timer.wait()))))
.map(move |_| {
if state {
Green.on();
} else {
Green.off();
}
Loop::Continue(!state)
})
});
// Event loop
timer.resume();
loop {
blinky.poll().unwrap(); // NOTE(unwrap) E = !
}
}
// TASKS
tasks!(stm32f103xx, {});
//! Blinks the user LED
#![deny(unsafe_code)]
#![deny(warnings)]
#![feature(const_fn)]
#![feature(proc_macro)]
#![no_std]
extern crate blue_pill;
extern crate cortex_m;
#[macro_use(task)]
extern crate cortex_m_rtfm as rtfm;
use blue_pill::led::{self, Green};
......@@ -18,15 +15,19 @@ use rtfm::{app, Threshold};
app! {
device: blue_pill::stm32f103xx,
resources: {
static ON: bool = false;
},
tasks: {
SYS_TICK: {
priority: 1,
path: toggle,
resources: [ON],
},
},
}
// INITIALIZATION PHASE
fn init(p: init::Peripherals) {
fn init(p: init::Peripherals, _r: init::Resources) {
led::init(p.GPIOC, p.RCC);
p.SYST.set_clock_source(SystClkSource::Core);
......@@ -35,7 +36,6 @@ fn init(p: init::Peripherals) {
p.SYST.enable_counter();
}
// IDLE LOOP
fn idle() -> ! {
// Sleep
loop {
......@@ -44,14 +44,10 @@ fn idle() -> ! {
}
// TASKS
task!(SYS_TICK, blink, Locals {
static STATE: bool = false;
});
fn blink(_t: &mut Threshold, l: &mut Locals, _r: SYS_TICK::Resources) {
*l.STATE = !*l.STATE;
fn toggle(_t: &mut Threshold, r: SYS_TICK::Resources) {
**r.ON = !**r.ON;
if *l.STATE {
if **r.ON {
Green.on();
} else {
Green.off();
......
//! Input capture using TIM1
#![deny(unsafe_code)]
#![deny(warnings)]
#![feature(proc_macro)]
......@@ -14,7 +13,7 @@ extern crate nb;
use blue_pill::prelude::*;
use blue_pill::time::Milliseconds;
use blue_pill::{Capture, Channel};
use rtfm::app;
use rtfm::{app, Threshold};
// CONFIGURATION
const RESOLUTION: Milliseconds = Milliseconds(1);
......@@ -33,7 +32,7 @@ fn init(p: init::Peripherals) {
capture.init(RESOLUTION, p.AFIO, p.GPIOA, p.RCC);
}
fn idle(r: idle::Resources) -> ! {
fn idle(_t: &mut Threshold, r: idle::Resources) -> ! {
const CHANNELS: [Channel; 4] =
[Channel::_1, Channel::_2, Channel::_3, Channel::_4];
......
......@@ -14,9 +14,8 @@ extern crate nb;
use blue_pill::time::Milliseconds;
use blue_pill::{Capture, Channel};
use blue_pill::prelude::*;
use rtfm::app;
use rtfm::{app, Threshold};
// CONFIGURATION
const RESOLUTION: Milliseconds = Milliseconds(1);
app! {
......@@ -33,7 +32,7 @@ fn init(p: init::Peripherals) {
capture.init(RESOLUTION, p.AFIO, p.GPIOA, p.RCC);
}
fn idle(r: idle::Resources) -> ! {
fn idle(_t: &mut Threshold, r: idle::Resources) -> ! {
const CHANNELS: [Channel; 4] =
[Channel::_1, Channel::_2, Channel::_3, Channel::_4];
......
......@@ -14,9 +14,8 @@ extern crate nb;
use blue_pill::prelude::*;
use blue_pill::time::Milliseconds;
use blue_pill::{Capture, Channel};
use rtfm::app;
use rtfm::{app, Threshold};
// CONFIGURATION
const RESOLUTION: Milliseconds = Milliseconds(1);
app! {
......@@ -33,7 +32,7 @@ fn init(p: init::Peripherals) {
capture.init(RESOLUTION, p.AFIO, p.GPIOA, p.RCC);
}
fn idle(r: idle::Resources) -> ! {
fn idle(_t: &mut Threshold, r: idle::Resources) -> ! {
const CHANNELS: [Channel; 2] = [Channel::_1, Channel::_2];
let capture = Capture(&*r.TIM3);
......
//! Input capture using TIM4
#![deny(unsafe_code)]
#![deny(warnings)]
#![feature(proc_macro)]
......@@ -14,9 +13,8 @@ extern crate nb;
use blue_pill::time::Milliseconds;
use blue_pill::{Capture, Channel};
use blue_pill::prelude::*;
use rtfm::app;
use rtfm::{app, Threshold};
// CONFIGURATION
const RESOLUTION: Milliseconds = Milliseconds(1);
app! {
......@@ -33,7 +31,7 @@ fn init(p: init::Peripherals) {
capture.init(RESOLUTION, p.AFIO, p.GPIOB, p.RCC);
}
fn idle(r: idle::Resources) -> ! {
fn idle(_t: &mut Threshold, r: idle::Resources) -> ! {
const CHANNELS: [Channel; 4] =
[Channel::_1, Channel::_2, Channel::_3, Channel::_4];
......
#![feature(proc_macro)]
#![no_std]
extern crate blue_pill;
extern crate cortex_m;
extern crate cortex_m_rtfm as rtfm;
use blue_pill::stm32f103xx::Interrupt;
use rtfm::{app, Resource, Threshold};
app! {
device: blue_pill::stm32f103xx,
resources: {
static R1: bool = false;
},
tasks: {
EXTI0: {
path: exti0,
priority: 1,
resources: [R1],
},
EXTI1: {
path: exti1,
priority: 2,
resources: [R1],
},
EXTI2: {
path: exti2,
priority: 3,
},
},
}
fn init(_p: init::Peripherals, _r: init::Resources) {}
fn idle() -> ! {
loop {
rtfm::wfi();
}
}
fn exti0(t: &mut Threshold, r: EXTI0::Resources) {
// Threshold == 1
rtfm::set_pending(Interrupt::EXTI1); // ~> exti1
r.R1.claim(t, |_r1, _t| {
// Threshold = 2
rtfm::set_pending(Interrupt::EXTI1);
rtfm::set_pending(Interrupt::EXTI2); // ~> exti2
}); // Threshold = 1
// ~> exti1
rtfm::atomic(t, |t| {
// Threshold = MAX
let _r1 = r.R1.borrow(t);
rtfm::set_pending(Interrupt::EXTI1);
rtfm::set_pending(Interrupt::EXTI2);
}); // Threshold = 1
// ~> exti2, exti1
}
fn exti1(_t: &mut Threshold, _r: EXTI1::Resources) {
// .. modify R1 ..
}
fn exti2() {}
//! Two concurrent tasks using `await!`
#![allow(unreachable_code)] // for the `await!` macro
#![deny(unsafe_code)]
#![deny(warnings)]
#![feature(const_fn)]
#![feature(used)]
#![no_std]
extern crate blue_pill;
extern crate embedded_hal as hal;
// version = "0.2.3"
extern crate cortex_m_rt;
// version = "0.1.0"
#[macro_use]
extern crate cortex_m_rtfm as rtfm;
extern crate futures;
#[macro_use]
extern crate nb;
use blue_pill::led::{self, Green};
use blue_pill::time::Hertz;
use blue_pill::{Serial, Timer, stm32f103xx};
use futures::future::{self, Loop};
use futures::{Async, Future};
use hal::prelude::*;
use rtfm::{P0, T0, TMax};
// CONFIGURATION
const BAUD_RATE: Hertz = Hertz(115_200);
const FREQUENCY: Hertz = Hertz(1);
// RESOURCES
peripherals!(stm32f103xx, {
AFIO: Peripheral {
ceiling: C0,
},
GPIOA: Peripheral {
ceiling: C0,
},
GPIOC: Peripheral {
ceiling: C0,
},
RCC: Peripheral {
ceiling: C0,
},
TIM3: Peripheral {
ceiling: C0,
},
USART1: Peripheral {
ceiling: C0,
},
});
// INITIALIZATION PHASE
fn init(ref prio: P0, thr: &TMax) {
let afio = &AFIO.access(prio, thr);
let gpioa = &GPIOA.access(prio, thr);
let gpioc = &GPIOC.access(prio, thr);
let rcc = &RCC.access(prio, thr);
let tim3 = TIM3.access(prio, thr);
let usart1 = USART1.access(prio, thr);
let timer = Timer(&*tim3);
let serial = Serial(&*usart1);
led::init(gpioc, rcc);
serial.init(BAUD_RATE.invert(), afio, None, gpioa, rcc);
timer.init(FREQUENCY.invert(), rcc);
}
// IDLE LOOP
#[inline(never)]
fn idle(ref prio: P0, ref thr: T0) -> ! {
let tim3 = TIM3.access(prio, thr);
let usart1 = USART1.access(prio, thr);
let timer = Timer(&*tim3);
let serial = Serial(&*usart1);
// Tasks
let mut blinky = (|| {
let mut state = false;
loop {
await!(timer.wait()).unwrap(); // NOTE(unwrap) E = !
state != state;
if state {
Green.on();
} else {
Green.off();
}
}
})();
let mut loopback = (|| loop {
let byte = await!(serial.read()).unwrap();
await!(serial.write()).unwrap();
})();
// Resume the timer count
timer.resume();
// Event loop
loop {
blinky.resume();
loopback.resume();
}
}
// TASKS
tasks!(stm32f103xx, {});
//! Two concurrent tasks using futures
#![allow(unreachable_code)] // for the `try_nb!` macro
#![deny(unsafe_code)]
#![deny(warnings)]
#![feature(const_fn)]
#![feature(used)]
#![no_std]
extern crate blue_pill;
extern crate embedded_hal as hal;
// version = "0.2.3"
extern crate cortex_m_rt;
// version = "0.1.0"
#[macro_use]
extern crate cortex_m_rtfm as rtfm;
extern crate futures;
#[macro_use]
extern crate nb;
use blue_pill::led::{self, Green};
use blue_pill::time::Hertz;
use blue_pill::{Serial, Timer, stm32f103xx};
use futures::future::{self, Loop};
use futures::{Async, Future};
use hal::prelude::*;
use rtfm::{P0, T0, TMax};
// CONFIGURATION
const BAUD_RATE: Hertz = Hertz(115_200);
const FREQUENCY: Hertz = Hertz(1);
// RESOURCES
peripherals!(stm32f103xx, {
AFIO: Peripheral {
ceiling: C0,
},
GPIOA: Peripheral {
ceiling: C0,
},
GPIOC: Peripheral {
ceiling: C0,
},
RCC: Peripheral {
ceiling: C0,
},
TIM3: Peripheral {
ceiling: C0,
},
USART1: Peripheral {
ceiling: C0,
},
});
// INITIALIZATION PHASE
fn init(ref prio: P0, thr: &TMax) {
let afio = &AFIO.access(prio, thr);
let gpioa = &GPIOA.access(prio, thr);
let gpioc = &GPIOC.access(prio, thr);
let rcc = &RCC.access(prio, thr);
let tim3 = TIM3.access(prio, thr);
let usart1 = USART1.access(prio, thr);
let timer = Timer(&*tim3);
let serial = Serial(&*usart1);
led::init(gpioc, rcc);
serial.init(BAUD_RATE.invert(), afio, None, gpioa, rcc);
timer.init(FREQUENCY.invert(), rcc);
}
// IDLE LOOP
#[inline(never)]
fn idle(ref prio: P0, ref thr: T0) -> ! {
let tim3 = TIM3.access(prio, thr);
let usart1 = USART1.access(prio, thr);
let timer = Timer(&*tim3);
let serial = Serial(&*usart1);
// Tasks
let mut blinky = future::loop_fn::<_, (), _, _>(true, |state| {
future::poll_fn(move || Ok(Async::Ready(try_nb!(timer.wait()))))
.map(move |_| {
if state {
Green.on();
} else {
Green.off();
}
Loop::Continue(!state)
})
});
let mut loopback = future::loop_fn::<_, (), _, _>((), |_| {
future::poll_fn(move || Ok(Async::Ready(try_nb!(serial.read()))))
.and_then(|byte| {
future::poll_fn(
move || Ok(Async::Ready(try_nb!(serial.write(byte)))),
)
})
.map(|_| Loop::Continue(()))
});
// Event loop
timer.resume();
loop {
loopback.poll().unwrap(); // NOTE(unwrap) E = !
blinky.poll().unwrap();
}
}
// TASKS
tasks!(stm32f103xx, {});
//! Serial loopback
//! Running two tasks concurrently
#![deny(unsafe_code)]
#![deny(warnings)]
#![feature(const_fn)]
#![feature(proc_macro)]
#![no_std]
extern crate blue_pill;
#[macro_use]
extern crate cortex_m;
extern crate cortex_m_rtfm as rtfm;
use blue_pill::Serial;
use blue_pill::led::{self, Green};
use blue_pill::prelude::*;
use blue_pill::serial::Event;
use blue_pill::time::Hertz;
use blue_pill::{Serial, Timer, stm32f103xx};
use cortex_m::peripheral::SystClkSource;
use rtfm::{app, Threshold};
const BAUD_RATE: Hertz = Hertz(115_200);
app! {
device: stm32f103xx,
device: blue_pill::stm32f103xx,
resources: {
static ON: bool = false;
},
tasks: {
TIM2: {
priority: 1,
enabled: true,
resources: [TIM2],
SYS_TICK: {
path: toggle,
resources: [ON],
},
USART1: {
priority: 1,
enabled: true,
path: loopback,
resources: [USART1],
},
},
}
// CONFIGURATION
pub const BAUD_RATE: Hertz = Hertz(115_200);
pub const FREQUENCY: Hertz = Hertz(1);
// INITIALIZATION PHASE
fn init(p: init::Peripherals) {
fn init(p: init::Peripherals, _r: init::Resources) {
let serial = Serial(p.USART1);
let timer = Timer(p.TIM2);
led::init(p.GPIOC, p.RCC);
serial.init(BAUD_RATE.invert(), p.AFIO, None, p.GPIOA, p.RCC);
serial.listen(Event::Rxne);
timer.init(FREQUENCY.invert(), p.RCC);
timer.resume();
p.SYST.set_clock_source(SystClkSource::Core);
p.SYST.set_reload(8_000_000); // 1s
p.SYST.enable_interrupt();
p.SYST.enable_counter();
}
// IDLE LOOP
fn idle() -> ! {
// Sleep
loop {
rtfm::wfi();
}
}
// TASKS
task!(TIM2, blinky, Local {
static STATE: bool = false;
});
fn blinky(_t: &mut Threshold, l: &mut Local, r: TIM2::Resources) {
let timer = Timer(&**r.TIM2);
fn loopback(_t: &mut Threshold, r: USART1::Resources) {
let serial = Serial(&**r.USART1);
timer.wait().unwrap();
let byte = serial.read().unwrap();
serial.write(byte).unwrap();
}
*l.STATE = !*l.STATE;
fn toggle(_t: &mut Threshold, r: SYS_TICK::Resources) {
**r.ON = !**r.ON;
if *l.STATE {
if **r.ON {
Green.on();
} else {
Green.off();
}
}
task!(USART1, loopback);
fn loopback(_t: &mut Threshold, r: USART1::Resources) {
let serial = Serial(&**r.USART1);
let byte = serial.read().unwrap();
serial.write(byte).unwrap();
}
//! CPU usage monitor
#![deny(unsafe_code)]
#![deny(warnings)]
#![feature(const_fn)]
#![feature(proc_macro)]
#![no_std]
extern crate blue_pill;
#[macro_use(iprint, iprintln)]
extern crate cortex_m;
#[macro_use]
extern crate cortex_m_rtfm as rtfm;
use blue_pill::Timer;
use blue_pill::stm32f103xx;
use blue_pill::time::Hertz;
use blue_pill::prelude::*;
use rtfm::{app, Resource, Threshold};
app! {
device: stm32f103xx,
device: blue_pill::stm32f103xx,
resources: {
static SLEEP_TIME: u32 = 0;
......@@ -33,17 +27,14 @@ app! {
tasks: {
TIM2: {
priority: 1,
enabled: true,
path: periodic,
resources: [ITM, SLEEP_TIME, TIM2],
},
},
}
// CONFIGURATION
const FREQUENCY: Hertz = Hertz(1);
// INITIALIZATION PHASE
fn init(p: init::Peripherals, _r: init::Resources) {
let timer = Timer(p.TIM2);
......@@ -53,13 +44,12 @@ fn init(p: init::Peripherals, _r: init::Resources) {
timer.resume();
}
// IDLE LOOP
fn idle(_t: &mut Threshold, mut r: idle::Resources) -> ! {
fn idle(t: &mut Threshold, mut r: idle::Resources) -> ! {
loop {
// For the span of this critical section the processor will not service
// interrupts (tasks)
rtfm::atomic(|cs| {
let sleep_time = r.SLEEP_TIME.borrow_mut(cs);
rtfm::atomic(t, |t| {
let sleep_time = r.SLEEP_TIME.borrow_mut(t);
// Sleep
let before = r.DWT.cyccnt.read();
......@@ -76,8 +66,6 @@ fn idle(_t: &mut Threshold, mut r: idle::Resources) -> ! {
}
}
task!(TIM2, periodic);
fn periodic(_t: &mut Threshold, r: TIM2::Resources) {
let timer = Timer(&**r.TIM2);
......
//! Sets PB12 high
#![deny(unsafe_code)]
#![deny(warnings)]
#![feature(proc_macro)]
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment