Skip to content

Commit

Permalink
async all the things
Browse files Browse the repository at this point in the history
  • Loading branch information
Jorge Aparicio committed Nov 23, 2016
1 parent 5a3fda8 commit a034ae2
Show file tree
Hide file tree
Showing 15 changed files with 1,359 additions and 376 deletions.
3 changes: 3 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,9 @@ stm32f30x-memory-map = "0.1.0"
features = ["memcpy"]
version = "0.0.20161008"

[dependencies.futuro]
git = "https://github.com/japaric/futuro"

[dependencies.r0]
optional = true
version = "0.1.0"
Expand Down
57 changes: 57 additions & 0 deletions examples/aio-concurrent.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
//! Echo server and blinking LED running concurrently
#![no_main]
#![no_std]

extern crate f3;
extern crate futuro;

use f3::led::LEDS;
use f3::serial::{Serial, Tx, Write};
use f3::timer::Timer;
use futuro::prelude::*;

// XXX can we get rid of this explicit state using Future adapters?
enum TxState {
Free(Tx),
Pending(Write),
}

#[no_mangle]
pub fn main() -> ! {
let Serial { tx, mut rx } = Serial::new().unwrap();

let mut timer = Timer::new().unwrap();
let mut on = true;
let mut blink = timer.periodic(100).map(|_| {
if on {
LEDS[0].on();
} else {
LEDS[0].off();
}

on = !on;
});

let mut tx_state = TxState::Free(tx);
let mut bytes = rx.bytes();
loop {
if let Async::Ready(byte) = bytes.poll() {
let tx = match tx_state {
TxState::Pending(write) => write.wait(),
TxState::Free(tx) => tx,
};
tx_state = TxState::Pending(tx.write(byte));
}

if let TxState::Pending(mut write) = tx_state {
if let Async::Ready(tx) = write.poll() {
tx_state = TxState::Free(tx);
} else {
tx_state = TxState::Pending(write);
}
}

blink.poll();
}
}
41 changes: 41 additions & 0 deletions examples/aio-echo.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
//! Echo server over Serial
#![no_main]
#![no_std]

extern crate f3;
extern crate futuro;

use futuro::prelude::*;
use f3::serial::{Serial, Tx, Write};

// XXX can we get rid of this explicit state using Future adapters?
enum TxState {
Free(Tx),
Pending(Write),
}

#[no_mangle]
pub fn main() -> ! {
let Serial { tx, mut rx } = Serial::new().unwrap();

let mut tx_state = TxState::Free(tx);
let mut bytes = rx.bytes();
loop {
if let Async::Ready(byte) = bytes.poll() {
let tx = match tx_state {
TxState::Pending(write) => write.wait(),
TxState::Free(tx) => tx,
};
tx_state = TxState::Pending(tx.write(byte));
}

if let TxState::Pending(mut write) = tx_state {
if let Async::Ready(tx) = write.poll() {
tx_state = TxState::Free(tx);
} else {
tx_state = TxState::Pending(write);
}
}
}
}
31 changes: 31 additions & 0 deletions examples/aio-i2c.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
#![no_main]
#![no_std]

#[macro_use]
extern crate f3;
extern crate futuro;

use futuro::prelude::*;
use f3::i2c::{Address, I2c};

#[no_mangle]
pub fn main() -> ! {
const MAGNETOMETER: u8 = 0b001_1110;
const MR_REG_M: u8 = 0x02;
const OUT_X_H_M: u8 = 0x3;

let i2c = I2c::new().unwrap();

// NOTE(write_all) configure the magnetometer to operate in continuous mode
// NOTE(write+read_exact) read the magnetic field
let buffer = i2c.write_all(Address::u7(MAGNETOMETER), &[MR_REG_M, 0b00])
.and_then(|i2c| i2c.write(None, OUT_X_H_M))
.and_then(|i2c| i2c.read_exact(None, [0u8; 6]))
.and_then(|(i2c, buffer)| i2c.stop().map(move |_| buffer))
.wait();

iprintln!("{:?}", buffer);

loop {
}
}
28 changes: 28 additions & 0 deletions examples/aio-periodic.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
#![no_main]
#![no_std]

extern crate f3;
extern crate futuro;

use f3::led::LEDS;
use f3::timer::Timer;
use futuro::prelude::*;

#[no_mangle]
pub fn main() -> ! {
let mut timer = Timer::new().unwrap();

let mut on = true;
let mut delay = timer.periodic(100).wait();
loop {
if on {
LEDS[0].on();
} else {
LEDS[0].off();
}

on = !on;

delay.next();
}
}
41 changes: 41 additions & 0 deletions examples/aio-sensors.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
#![no_main]
#![no_std]

#[macro_use]
extern crate f3;
extern crate futuro;

use f3::i2c::I2c;
use f3::l3gd20::L3gd20;
use f3::lsm303dlhc::Lsm303dlhc;
use f3::spi::Spi;
use f3::time::Instant;
use f3::timer::Timer;
use futuro::prelude::*;

#[no_mangle]
pub fn main() -> ! {
let mut lsm303dlhdc = I2c::new().map(|i2c| Lsm303dlhc::new(i2c)).unwrap();
let mut l3gd20 = Spi::new().map(|spi| L3gd20::new(spi)).unwrap();
let mut timer = Timer::new().unwrap();
let mut periodic = timer.periodic(1_000).wait();

loop {
let instant = Instant::now();
let (x, y, a, ar, mf) = lsm303dlhdc.acceleration()
.and_then(|(x, a)| {
x.magnetic_field().map(move |(x, mf)| (x, a, mf))
})
.join(l3gd20.angular_rate())
.map(|((x, a, mf), (y, ar))| (x, y, a, ar, mf))
.wait();
l3gd20 = y;
lsm303dlhdc = x;
let elapsed = instant.elapsed();

iprintln!("{} ticks", elapsed);
iprintln!("{:?}", (a.x(), ar.x(), mf.x()));

periodic.next();
}
}
36 changes: 36 additions & 0 deletions examples/aio-spi.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
#![no_main]
#![no_std]

#[macro_use]
extern crate f3;
extern crate futuro;

use f3::peripheral;
use f3::spi::Spi;
use futuro::prelude::*;

#[no_mangle]
pub fn main() -> ! {
// WHO_AM_I
const REGISTER_ADDRESS: u8 = 0x0f;
// Read the register
const READ: u8 = 1 << 7;

let spi = Spi::new().unwrap();
let rcc = unsafe { peripheral::rcc_mut() };
let gpioe = unsafe { peripheral::gpioe_mut() };

rcc.ahbenr.modify(|_, w| w.iopeen(true));
gpioe.moder.modify(|_, w| w.moder3(0b01));
gpioe.bsrr.write(|w| w.br3(true));

let buffer = spi.transfer_all([READ | REGISTER_ADDRESS, 0b00]).wait().1;

gpioe.bsrr.write(|w| w.bs3(true));

// Expected output: 0x0f - 0xd4
iprintln!("0x{:02x} - 0x{:02x}", REGISTER_ADDRESS, buffer[1]);

loop {
}
}
56 changes: 0 additions & 56 deletions src/delay.rs

This file was deleted.

Loading

0 comments on commit a034ae2

Please sign in to comment.