reflow-firmware2.0/src/main.rs

143 lines
3.5 KiB
Rust
Raw Normal View History

2018-08-19 13:46:08 +02:00
#![no_std]
#![feature(asm, used, const_fn, naked_functions, alloc, box_syntax)]
#![no_main]
#![feature(extern_prelude)]
#[macro_use]
extern crate hcl;
#[macro_use]
extern crate alloc;
use core::mem;
use hcl::platform::gpio;
use hcl::platform::irq;
use hcl::platform::rcc;
use hcl::platform::scs;
use hcl::platform::timer;
use hcl::platform::usart;
use hcl::platform::dma;
use hcl::dma::*;
2018-09-01 01:06:15 +02:00
mod systick;
2018-08-19 14:05:43 +02:00
mod printer;
2018-08-28 21:17:26 +02:00
mod max6675;
2018-08-19 14:05:43 +02:00
use printer::UsartPrinter;
2018-08-19 13:46:08 +02:00
2018-08-28 21:17:26 +02:00
2018-08-19 13:46:08 +02:00
fn configure_clocks(rcc: &mut rcc::RCC) {
rcc.clock_control.set_hse_on(true);
2018-08-28 21:17:26 +02:00
while !rcc.clock_control.hse_ready() {}
2018-08-19 13:46:08 +02:00
2018-08-28 21:17:26 +02:00
rcc.clock_config
.configure(|c| {
2018-08-29 00:43:29 +02:00
c.set_pll_multiplier(10)
.set_pll_source(rcc::PllSource::HsiDiv2)
});
2018-08-19 13:46:08 +02:00
rcc.clock_control.set_pll_on(true);
2018-08-28 21:17:26 +02:00
while !rcc.clock_control.pll_ready() {}
2018-08-19 13:46:08 +02:00
2018-08-28 21:17:26 +02:00
rcc.clock_config
.switch_clock_source(rcc::SystemClockSource::Pll);
2018-08-19 13:46:08 +02:00
}
2018-09-01 01:06:15 +02:00
hcl_ivt!{
systick => systick::isr;
}
2018-08-19 13:46:08 +02:00
fn configure_peripherals(rcc: &mut hcl::platform::rcc::RCC,
2018-09-01 01:06:15 +02:00
gpio_a: &mut gpio::GPIO,
gpio_c: &mut gpio::GPIO,
2018-08-28 21:17:26 +02:00
usart: &mut usart::USART) {
rcc.apb2_enable
2018-09-01 01:06:15 +02:00
.configure(|a| a.set_gpio_a(true).set_gpio_c(true).set_spi1(true));
2018-08-28 21:17:26 +02:00
rcc.apb1_enable.configure(|a| a.set_usart2(true));
2018-09-01 01:06:15 +02:00
gpio_a.configure(|g| {
g.set_mode(2, gpio::PinMode::Output50MHz)
2018-08-19 13:46:08 +02:00
.set_output_config(2, gpio::OutputConfig::AfPushPull)
.set_mode(3, gpio::PinMode::Output50MHz)
.set_output_config(3, gpio::OutputConfig::AfPushPull)
2018-08-20 00:30:54 +02:00
// SCK1
2018-08-19 13:46:08 +02:00
.set_mode(5, gpio::PinMode::Output50MHz)
2018-08-20 00:30:54 +02:00
.set_output_config(5, gpio::OutputConfig::AfPushPull)
// MOSI1
.set_mode(7, gpio::PinMode::Output50MHz)
.set_output_config(7, gpio::OutputConfig::AfPushPull)
// MISO1
.set_mode(6, gpio::PinMode::Input)
2018-08-29 00:43:29 +02:00
.set_input_config(6, gpio::InputConfig::PullUpDown)
2018-09-01 01:06:15 +02:00
// ???
2018-08-20 00:30:54 +02:00
.set_mode(4, gpio::PinMode::Output50MHz)
2018-08-28 21:17:26 +02:00
.set_output_config(4, gpio::OutputConfig::PushPull)
2018-09-01 01:06:15 +02:00
// 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)
2018-08-28 21:17:26 +02:00
});
2018-09-01 01:06:15 +02:00
2018-08-28 21:17:26 +02:00
usart.configure(|u| {
2018-08-29 00:43:29 +02:00
u.set_enabled(true)
.set_tx_enabled(true)
.set_baudgen((21, 11))
}); // 115.2 kbaud
2018-08-19 13:46:08 +02:00
}
// 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);
2018-09-01 01:06:15 +02:00
systick::configure(&mut scs.systick);
configure_peripherals(&mut p.rcc, &mut p.gpio_a, &mut p.gpio_c, &mut p.usart2);
2018-08-19 13:46:08 +02:00
2018-08-19 14:05:43 +02:00
let mut printer = UsartPrinter::init(p.usart2);
2018-08-19 13:46:08 +02:00
2018-08-28 21:17:26 +02:00
let spi = &mut p.spi1;
let gpio = &mut p.gpio_a;
2018-08-20 00:30:54 +02:00
2018-08-28 21:17:26 +02:00
loop {
2018-08-19 13:46:08 +02:00
2018-09-01 01:06:15 +02:00
let res = max6675::read(spi, gpio, 9);
2018-08-20 00:30:54 +02:00
2018-08-29 00:43:29 +02:00
let msg = match res {
Ok(temp) => format!("> {}\r\n", temp).into_bytes(),
Err(err) => format!("Error > {}\r\n", err).into_bytes(),
};
2018-08-20 00:30:54 +02:00
2018-08-29 00:43:29 +02:00
printer.print(msg);
2018-09-01 01:06:15 +02:00
let old = p.gpio_c.output();
p.gpio_c.set_output(old ^ (1<<13));
systick::delay_ms(1000);
2018-08-20 00:30:54 +02:00
}
2018-08-19 13:46:08 +02:00
}
entry_point!(main);
fn main(scs: scs::Instance, p: hcl::platform::Instance) {
2018-08-28 21:17:26 +02:00
declare_thread_stack!(stack, 3072);
2018-08-19 13:46:08 +02:00
unsafe {
hcl::set_process_stack(&stack);
hcl::use_process_stack(true);
}
run(scs, p);
}