|
| 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 | +} |
0 commit comments