Skip to content
Snippets Groups Projects
Commit 39f01003 authored by Per Lindgren's avatar Per Lindgren
Browse files

bare6 v1

parent e954f30e
No related branches found
No related tags found
No related merge requests found
...@@ -5,39 +5,49 @@ ...@@ -5,39 +5,49 @@
#![feature(use_nested_groups)] #![feature(use_nested_groups)]
#![no_std] #![no_std]
extern crate cortex_m; extern crate stm32f40x;
extern crate cortex_m_rt;
#[macro_use] #[macro_use]
extern crate cortex_m_debug; extern crate cortex_m_debug;
use cortex_m::{asm::bkpt, peripheral::DWT}; use stm32f40x::{DWT, GPIOA, RCC};
fn main() {
ipln!("init");
let dwt = unsafe { &mut *DWT.get() }; // get the reference to DWD in memory
let rcc = unsafe { &mut *RCC.get() }; // get the reference to RCC in memory
let gpioa = unsafe { &mut *GPIOA.get() }; // get the reference to GPIOA in memory
dwt.enable_cycle_counter();
idle(dwt, rcc, gpioa);
}
// uses the DWT.CYCNT // uses the DWT.CYCNT
// doc: ARM trm_100166_0001_00_en.pdf, chapter 9.2 // doc: ARM trm_100166_0001_00_en.pdf, chapter 9.2
// we use the `cortex-m` abstraction // we use the `cortex-m` abstraction
fn wait_cycles(nr_cycles: u32) { fn wait_cycles(dwt: &mut DWT, nr_cycles: u32) {
unsafe { let t = dwt.cyccnt.read().wrapping_add(nr_cycles);
let t = (*DWT.get()).cyccnt.read().wrapping_add(nr_cycles); while (dwt.cyccnt.read().wrapping_sub(t) as i32) < 0 {}
while ((*DWT.get()).cyccnt.read().wrapping_sub(t) as i32) < 0 {}
}
} }
fn main() { // user application
unsafe { (*DWT.get()).enable_cycle_counter() }; fn idle(dwt: &mut DWT, rcc: &mut RCC, gpioa: &mut GPIOA) {
let mut i = 0; ipln!("idle");
loop { // power on GPIOA, RM0368 6.3.11
ipln!("tick {}", i); rcc.ahb1enr.modify(|_, w| w.gpioaen().set_bit());
wait_cycles(64_000_000);
i += 1; // this will eventually cause a panic, when the counter wraps.
}
}
// As we are not using interrupts, we just register a dummy catch all handler // configure PA5 as output, RM0368 8.4.1
#[link_section = ".vector_table.interrupts"] gpioa.moder.modify(|_, w| w.moder5().bits(1));
#[used]
static INTERRUPTS: [extern "C" fn(); 240] = [default_handler; 240];
extern "C" fn default_handler() { loop {
cortex_m::asm::bkpt(); ipln!("led on");
// set PA5 high, RM0368 8.4.7
gpioa.bsrr.write(|w| w.bs5().set_bit());
wait_cycles(dwt, 10_000);
ipln!("idle off");
// set PA5 low, RM0368 8.4.7
gpioa.bsrr.write(|w| w.br5().set_bit());
wait_cycles(dwt, 10_000);
}
} }
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment