Added a proper delay function
This commit is contained in:
parent
69ed2fc701
commit
1d906b34a6
2
hcl
2
hcl
|
@ -1 +1 @@
|
|||
Subproject commit 001d44d6b9693a9ad3fd6fc8bc44e8dc22ad7149
|
||||
Subproject commit 9dc33a9abadbde8e0c2ffc853a35feaf6990c143
|
45
src/main.rs
45
src/main.rs
|
@ -19,10 +19,10 @@ use hcl::platform::usart;
|
|||
use hcl::platform::dma;
|
||||
use hcl::dma::*;
|
||||
|
||||
mod systick;
|
||||
mod printer;
|
||||
mod max6675;
|
||||
|
||||
|
||||
use printer::UsartPrinter;
|
||||
|
||||
|
||||
|
@ -43,16 +43,25 @@ fn configure_clocks(rcc: &mut rcc::RCC) {
|
|||
.switch_clock_source(rcc::SystemClockSource::Pll);
|
||||
}
|
||||
|
||||
|
||||
|
||||
hcl_ivt!{
|
||||
systick => systick::isr;
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
fn configure_peripherals(rcc: &mut hcl::platform::rcc::RCC,
|
||||
gpio: &mut gpio::GPIO,
|
||||
gpio_a: &mut gpio::GPIO,
|
||||
gpio_c: &mut gpio::GPIO,
|
||||
usart: &mut usart::USART) {
|
||||
rcc.apb2_enable
|
||||
.configure(|a| a.set_gpio_a(true).set_spi1(true));
|
||||
.configure(|a| a.set_gpio_a(true).set_gpio_c(true).set_spi1(true));
|
||||
rcc.apb1_enable.configure(|a| a.set_usart2(true));
|
||||
|
||||
gpio.configure(|g| {
|
||||
g
|
||||
.set_mode(2, gpio::PinMode::Output50MHz)
|
||||
gpio_a.configure(|g| {
|
||||
g.set_mode(2, gpio::PinMode::Output50MHz)
|
||||
.set_output_config(2, gpio::OutputConfig::AfPushPull)
|
||||
.set_mode(3, gpio::PinMode::Output50MHz)
|
||||
.set_output_config(3, gpio::OutputConfig::AfPushPull)
|
||||
|
@ -65,11 +74,20 @@ fn configure_peripherals(rcc: &mut hcl::platform::rcc::RCC,
|
|||
// MISO1
|
||||
.set_mode(6, gpio::PinMode::Input)
|
||||
.set_input_config(6, gpio::InputConfig::PullUpDown)
|
||||
// NSS1
|
||||
// ???
|
||||
.set_mode(4, gpio::PinMode::Output50MHz)
|
||||
.set_output_config(4, gpio::OutputConfig::PushPull)
|
||||
// SS MAX6675
|
||||
.set_mode(9, gpio::PinMode::Output50MHz)
|
||||
.set_output_config(9, gpio::OutputConfig::PushPull)
|
||||
});
|
||||
|
||||
gpio_c.configure(|g| {
|
||||
g.set_mode(13, gpio::PinMode::Output50MHz)
|
||||
.set_output_config(13, gpio::OutputConfig::PushPull)
|
||||
});
|
||||
|
||||
|
||||
usart.configure(|u| {
|
||||
u.set_enabled(true)
|
||||
.set_tx_enabled(true)
|
||||
|
@ -78,12 +96,12 @@ fn configure_peripherals(rcc: &mut hcl::platform::rcc::RCC,
|
|||
}
|
||||
|
||||
|
||||
|
||||
// allowing inlining into main() breaks the stack, since main() must be naked to set up a process stack.
|
||||
#[inline(never)]
|
||||
fn run(mut scs: scs::Instance, mut p: hcl::platform::Instance) {
|
||||
configure_clocks(&mut p.rcc);
|
||||
configure_peripherals(&mut p.rcc, &mut p.gpio_a, &mut p.usart2);
|
||||
systick::configure(&mut scs.systick);
|
||||
configure_peripherals(&mut p.rcc, &mut p.gpio_a, &mut p.gpio_c, &mut p.usart2);
|
||||
|
||||
let mut printer = UsartPrinter::init(p.usart2);
|
||||
|
||||
|
@ -94,7 +112,7 @@ fn run(mut scs: scs::Instance, mut p: hcl::platform::Instance) {
|
|||
|
||||
loop {
|
||||
|
||||
let res = max6675::read(spi, gpio, 4);
|
||||
let res = max6675::read(spi, gpio, 9);
|
||||
|
||||
let msg = match res {
|
||||
Ok(temp) => format!("> {}\r\n", temp).into_bytes(),
|
||||
|
@ -103,9 +121,10 @@ fn run(mut scs: scs::Instance, mut p: hcl::platform::Instance) {
|
|||
|
||||
printer.print(msg);
|
||||
|
||||
for y in 0u32..0xFFFFFF {
|
||||
unsafe { asm!("nop" :::: "volatile") };
|
||||
}
|
||||
let old = p.gpio_c.output();
|
||||
p.gpio_c.set_output(old ^ (1<<13));
|
||||
|
||||
systick::delay_ms(1000);
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -0,0 +1,30 @@
|
|||
use hcl::platform::scs;
|
||||
|
||||
|
||||
static mut MILLIS : u32 = 0;
|
||||
|
||||
pub unsafe extern fn isr() {
|
||||
MILLIS = MILLIS.wrapping_add(1);
|
||||
}
|
||||
|
||||
pub fn configure(st: &mut scs::Systick) {
|
||||
st.configure(|s| s
|
||||
.set_internally_clocked(true)
|
||||
.set_interrupt_enabled(true)
|
||||
.set_reload_value(40_000)
|
||||
.set_enabled(true));
|
||||
}
|
||||
|
||||
|
||||
pub fn millis() -> u32 {
|
||||
unsafe {
|
||||
return MILLIS;
|
||||
}
|
||||
}
|
||||
|
||||
pub fn delay_ms(ms: u32) {
|
||||
let start = millis();
|
||||
while millis().wrapping_sub(start) < ms {
|
||||
unsafe { asm!("wfi" :::: "volatile"); }
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue