Skip to content

Commit 741881f

Browse files
committed
Speaker example
1 parent 272e40c commit 741881f

File tree

3 files changed

+127
-0
lines changed

3 files changed

+127
-0
lines changed

examples/v2-speaker/Cargo.toml

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
[package]
2+
name = "v2-speaker"
3+
version = "0.1.0"
4+
edition = "2018"
5+
6+
[dependencies]
7+
cortex-m = "0.6.1"
8+
embedded-hal = "0.2.4"
9+
cortex-m-rt = "0.6.12"
10+
panic-halt = "0.2.0"
11+
defmt-rtt = "0.2.0"
12+
defmt = "0.2.0"
13+
14+
[dependencies.microbit-v2]
15+
path = "../../microbit-v2"
16+
optional = true
17+
18+
[features]
19+
v2 = ["microbit-v2"]
20+
21+
default = [
22+
"defmt-default",
23+
]
24+
25+
# do NOT modify these features
26+
defmt-default = []
27+
defmt-trace = []
28+
defmt-debug = []
29+
defmt-info = []
30+
defmt-warn = []
31+
defmt-error = []

examples/v2-speaker/src/main.rs

Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
#![no_main]
2+
#![no_std]
3+
4+
use defmt_rtt as _;
5+
use panic_halt as _;
6+
7+
use core::cell::RefCell;
8+
use cortex_m::interrupt::Mutex;
9+
10+
use cortex_m_rt::entry;
11+
use microbit::{
12+
hal::{clocks::Clocks, gpio, prelude::OutputPin, pwm, time::Hertz},
13+
pac::{self, interrupt},
14+
};
15+
16+
static RTC: Mutex<RefCell<Option<pac::RTC0>>> = Mutex::new(RefCell::new(None));
17+
static SPEAKER: Mutex<RefCell<Option<pwm::Pwm<pac::PWM0>>>> = Mutex::new(RefCell::new(None));
18+
19+
#[entry]
20+
fn main() -> ! {
21+
if let Some(p) = microbit::Peripherals::take() {
22+
cortex_m::interrupt::free(move |cs| {
23+
let _clocks = Clocks::new(p.CLOCK)
24+
.enable_ext_hfosc()
25+
.set_lfclk_src_synth()
26+
.start_lfclk();
27+
28+
defmt::info!("Speaker");
29+
30+
p.RTC0.prescaler.write(|w| unsafe { w.bits(511) });
31+
p.RTC0.evtenset.write(|w| w.tick().set_bit());
32+
p.RTC0.intenset.write(|w| w.tick().set_bit());
33+
p.RTC0.tasks_start.write(|w| unsafe { w.bits(1) });
34+
35+
*RTC.borrow(cs).borrow_mut() = Some(p.RTC0);
36+
37+
let p0parts = gpio::p0::Parts::new(p.P0);
38+
39+
let mut speaker_pin = p0parts.p0_00.into_push_pull_output(gpio::Level::Low);
40+
let _ = speaker_pin.set_low();
41+
42+
let speaker = pwm::Pwm::new(p.PWM0);
43+
speaker
44+
.set_output_pin(pwm::Channel::C0, &speaker_pin.degrade())
45+
.set_prescaler(pwm::Prescaler::Div16)
46+
.set_period(Hertz(1000u32))
47+
.set_counter_mode(pwm::CounterMode::UpAndDown)
48+
.enable();
49+
speaker
50+
.set_seq_refresh(pwm::Seq::Seq0, 0)
51+
.set_seq_refresh(pwm::Seq::Seq1, 0)
52+
.set_seq_end_delay(pwm::Seq::Seq0, 0)
53+
.set_seq_end_delay(pwm::Seq::Seq1, 0);
54+
55+
let max_duty = speaker.max_duty();
56+
speaker.set_duty_on_common(max_duty / 2);
57+
58+
*SPEAKER.borrow(cs).borrow_mut() = Some(speaker);
59+
60+
unsafe {
61+
pac::NVIC::unmask(pac::Interrupt::RTC0);
62+
}
63+
pac::NVIC::unpend(pac::Interrupt::RTC0);
64+
});
65+
}
66+
67+
loop {
68+
continue;
69+
}
70+
}
71+
72+
// Define an exception, i.e. function to call when exception occurs. Here if our timer
73+
// trips, we'll print some random number
74+
#[interrupt]
75+
fn RTC0() {
76+
static mut FREQUENCY: u32 = 1;
77+
/* Enter critical section */
78+
cortex_m::interrupt::free(|cs| {
79+
if let Some(speaker) = SPEAKER.borrow(cs).borrow().as_ref() {
80+
speaker.set_period(Hertz(*FREQUENCY));
81+
defmt::info!("Speaker {}", *FREQUENCY);
82+
let max_duty = speaker.max_duty();
83+
speaker.set_duty_on_common(max_duty / 2);
84+
}
85+
if let Some(rtc) = RTC.borrow(cs).borrow().as_ref() {
86+
rtc.events_tick.write(|w| unsafe { w.bits(0) });
87+
}
88+
});
89+
*FREQUENCY += 1;
90+
if *FREQUENCY >= 1000 {
91+
*FREQUENCY = 1
92+
};
93+
}

microbit-common/src/v2/gpio.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -112,3 +112,6 @@ pub type SDA = p1::P1_00<Input<Floating>>;
112112
/* uart */
113113
pub type UART_TX = p1::P1_08<Output<PushPull>>;
114114
pub type UART_RX = p0::P0_06<Input<Floating>>;
115+
116+
/* speaker */
117+
pub type SPEAKER = p0::P0_00<Output<PushPull>>;

0 commit comments

Comments
 (0)