-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
added more content to sharing-data-with-globals
- Loading branch information
1 parent
941bbd3
commit f0cae3e
Showing
2 changed files
with
135 additions
and
28 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
#![no_main] | ||
#![no_std] | ||
|
||
use core::cell::RefCell; | ||
|
||
use cortex_m::asm; | ||
use cortex_m_rt::entry; | ||
use critical_section::Mutex; | ||
use panic_rtt_target as _; | ||
use rtt_target::{rprintln, rtt_init_print}; | ||
|
||
use microbit::{ | ||
Board, | ||
hal::{ | ||
gpiote, | ||
pac::{self, interrupt}, | ||
}, | ||
}; | ||
|
||
static COUNTER: Mutex<RefCell<usize>> = Mutex::new(RefCell::new(0)); | ||
|
||
/// This "function" will be called when an interrupt is received. For now, just | ||
/// report and panic. | ||
#[interrupt] | ||
fn GPIOTE() { | ||
critical_section::with(|cs| { | ||
let mut count = COUNTER.borrow(cs).borrow_mut(); | ||
*count += 1; | ||
rprintln!("ouch {}", count); | ||
}); | ||
panic!(); | ||
} | ||
|
||
#[entry] | ||
fn main() -> ! { | ||
rtt_init_print!(); | ||
let board = Board::take().unwrap(); | ||
let button_a = board.buttons.button_a.into_floating_input(); | ||
|
||
// Set up the GPIOTE to generate an interrupt when Button A is pressed (GPIO | ||
// wire goes low). | ||
let gpiote = gpiote::Gpiote::new(board.GPIOTE); | ||
let channel = gpiote.channel0(); | ||
channel | ||
.input_pin(&button_a.degrade()) | ||
.hi_to_lo() | ||
.enable_interrupt(); | ||
channel.reset_events(); | ||
|
||
// Set up the NVIC to handle GPIO interrupts. | ||
unsafe { pac::NVIC::unmask(pac::Interrupt::GPIOTE) }; | ||
pac::NVIC::unpend(pac::Interrupt::GPIOTE); | ||
|
||
loop { | ||
// "wait for interrupt": CPU goes to sleep until an interrupt. | ||
asm::wfi(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters