Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Demo use of PR#12 in nrf51-hal #11

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions examples/gpio_hal_blinky.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ extern crate cortex_m_rt;
extern crate microbit;
extern crate panic_halt;

use microbit::hal::delay::Delay;
use microbit::hal::delay::{Timer, Generic, TIMER0};
use microbit::hal::prelude::*;

use cortex_m_rt::entry;
Expand All @@ -14,7 +14,7 @@ use cortex_m_rt::entry;
fn main() -> ! {
if let Some(p) = microbit::Peripherals::take() {
let mut gpio = p.GPIO.split();
let mut delay = Delay::new(p.TIMER0);
let mut delay = Timer::<Generic, TIMER0>::new(p.TIMER0, 4).into_delay();
let mut led = gpio.pin13.into_push_pull_output();
let _ = gpio.pin4.into_push_pull_output();

Expand Down
5 changes: 3 additions & 2 deletions examples/led_blocking.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,18 +9,19 @@ use core::fmt::Write;

use cortex_m_rt::entry;

use microbit::hal::delay::Delay;
use microbit::hal::timer::{Timer, Generic};
use microbit::hal::prelude::*;
use microbit::hal::serial;
use microbit::hal::serial::BAUD115200;
use microbit::nrf51::TIMER0;

use microbit::led;

#[entry]
fn main() -> ! {
if let Some(p) = microbit::Peripherals::take() {
let mut gpio = p.GPIO.split();
let mut delay = Delay::new(p.TIMER0);
let mut delay = Timer::<Generic, TIMER0>::new(p.TIMER0, 4).into_delay();

// Display
let row1 = gpio.pin13.into_push_pull_output();
Expand Down
Binary file added examples/test_countdown.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
153 changes: 153 additions & 0 deletions examples/test_countdown.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,153 @@
#![no_main]
#![no_std]

extern crate cortex_m_rt;
extern crate microbit;
extern crate panic_halt;

use core::time::Duration;
use cortex_m_rt::entry;
use microbit::hal::prelude::*;
use microbit::hal::timer::{Timer, Generic};
use microbit::hal::hal::timer::CountDown;
use microbit::nrf51::{TIMER0, TIMER1, TIMER2, RTC0, RTC1};
use microbit::nb::block;

/*
@startuml

scale 500 as 60 pixels
skinparam monochrome reverse

robust "LED light" as LED
concise "Timer 2" as TIMER2
concise "Timer 1" as TIMER1
concise "Timer 0" as TIMER0
concise "RTC 1" as RTC1
concise "RTC 0" as RTC0

LED is Off

@0
TIMER2 is 500ms
TIMER1 is 1000ms
TIMER0 is 1500ms
RTC1 is 4000ms
RTC0 is 5000ms

@+500
LED is On
TIMER2 is " "
TIMER2 -> LED

@+500
LED is Off
TIMER2 is " "
TIMER1 is 1000ms
TIMER1 -> LED

@+500
LED is On
TIMER2 is " "
TIMER0 is 1500ms
TIMER0 -> LED

@+500
LED is Off
TIMER2 is 500ms
TIMER1 is " "
TIMER1 -> LED

@+500
LED is On
TIMER2 is " "
TIMER2 -> LED

@+500
LED is Off
TIMER2 is " "
TIMER1 is " "
TIMER0 is " "
TIMER0 -> LED

@4000
LED is On
RTC1 is " "
RTC1 -> LED

@5000
LED is Off
RTC0 is " "
RTC0 -> LED

@enduml
*/

#[entry]
fn main() -> ! {
if let Some(p) = microbit::Peripherals::take() {

// Start the LFCLK
p.CLOCK.tasks_lfclkstart.write(|w| unsafe { w.bits(1) });

let mut gpio = p.GPIO.split();

let mut pin = gpio.pin14.into_push_pull_output();
let _ = gpio.pin6.into_push_pull_output();

// 32bits @ 1MHz = ~72 minutes
let mut timer0 = Timer::<Generic, TIMER0>::new(p.TIMER0, 4).into_countdown();
// 16bits @ 31.25kHz = ~2 seconds
let mut timer1 = Timer::<Generic, TIMER1>::new(p.TIMER1, 9).into_countdown();
// 16bits @ 31.25kHz = ~2 seconds
let mut timer2 = Timer::<Generic, TIMER2>::new(p.TIMER2, 9).into_countdown();

// 24bits @ 32.768kHz = 512 seconds
let mut rtc0 = Timer::<Generic, RTC0>::new(p.RTC0, 0).into_countdown();
// 24bits @ 32.768kHz = 512 seconds
let mut rtc1 = Timer::<Generic, RTC1>::new(p.RTC1, 0).into_countdown();

CountDown::start(&mut rtc0, Duration::from_millis(5_000));
CountDown::start(&mut rtc1, Duration::from_millis(4_000));

CountDown::start(&mut timer0, Duration::from_millis(1_500));
CountDown::start(&mut timer1, Duration::from_millis(1_000));
CountDown::start(&mut timer2, Duration::from_millis(500));

// @+500
block!(timer2.wait());
pin.set_high();

// @+500
block!(timer1.wait());
pin.set_low();

// @+500
block!(timer0.wait());
pin.set_high();

// @+500
block!(timer1.wait());
pin.set_low();
CountDown::start(&mut timer2, Duration::from_millis(500));

// @+500
block!(timer2.wait());
pin.set_high();

// @+500
block!(timer0.wait());
pin.set_low();

// @4000
block!(rtc1.wait());
pin.set_high();

// @5000
block!(rtc0.wait());
pin.set_low();

}

panic!("FIN");
}
80 changes: 80 additions & 0 deletions examples/test_delay.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
#![no_main]
#![no_std]

extern crate cortex_m_rt;
extern crate microbit;
extern crate panic_halt;

use cortex_m_rt::entry;
use microbit::hal::prelude::*;
use microbit::hal::delay::{Timer, Generic};
use microbit::nrf51::{TIMER0, TIMER1, TIMER2, RTC0, RTC1};

#[entry]
fn main() -> ! {
if let Some(p) = microbit::Peripherals::take() {

// Start the LFCLK
p.CLOCK.tasks_lfclkstart.write(|w| unsafe { w.bits(1) });

let mut gpio = p.GPIO.split();

let mut pin = gpio.pin14.into_push_pull_output();
let _ = gpio.pin6.into_push_pull_output();

// 32bits @ 1MHz = ~72 minutes
let mut delay_timer0 = Timer::<Generic, TIMER0>::new(p.TIMER0, 4).into_delay();
// 16bits @ 31.25kHz = ~2 seconds
let mut delay_timer1 = Timer::<Generic, TIMER1>::new(p.TIMER1, 9).into_delay();
// 16bits @ 31.25kHz = ~2 seconds
let mut delay_timer2 = Timer::<Generic, TIMER2>::new(p.TIMER2, 9).into_delay();

// 24bits @ 32.768kHz = 512 seconds
let mut delay_rtc3 = Timer::<Generic, RTC0>::new(p.RTC0, 0).into_delay();
// 24bits @ 32.768kHz = 512 seconds
let mut delay_rtc4 = Timer::<Generic, RTC1>::new(p.RTC1, 0).into_delay();

const LONG: u16 = 800;
const SHORT: u16 = 400;

for _ in 0..2 {

for _ in 0..2 {
pin.set_high();
delay_timer0.delay_ms(LONG);
pin.set_low();
delay_timer0.delay_ms(SHORT);
}

for _ in 0..2 {
pin.set_high();
delay_timer1.delay_ms(LONG);
pin.set_low();
delay_timer1.delay_ms(SHORT);
}

for _ in 0..2 {
pin.set_high();
delay_timer2.delay_ms(LONG);
pin.set_low();
delay_timer2.delay_ms(SHORT);
}

for _ in 0..2 {
pin.set_high();
delay_rtc3.delay_ms(LONG);
pin.set_low();
delay_rtc3.delay_ms(SHORT);
}

for _ in 0..2 {
pin.set_high();
delay_rtc4.delay_ms(LONG);
pin.set_low();
delay_rtc4.delay_ms(SHORT);
}
}
}

panic!("FIN");
}
Binary file added examples/test_timer.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading