reflow-firmware2.0/src/main.rs

191 lines
5.4 KiB
Rust
Raw Permalink 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-09-02 16:27:09 +02:00
mod st7735;
2018-09-11 22:44:00 +02:00
mod logo;
mod freesans;
2018-08-28 21:17:26 +02:00
2018-08-19 14:05:43 +02:00
use printer::UsartPrinter;
2018-09-08 20:40:56 +02:00
use st7735::gfx::{PrimitveGFX, GFX};
2018-09-11 22:44:00 +02:00
use st7735::bitmaps::BitmapGFX;
2018-09-23 21:34:23 +02:00
use st7735::fonts::FontRenderer;
2018-09-11 22:44:00 +02:00
use freesans::FREE_SANS_FONT;
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
// SS MAX6675
.set_mode(9, gpio::PinMode::Output50MHz)
.set_output_config(9, gpio::OutputConfig::PushPull)
2018-09-04 01:10:27 +02:00
// ST7735 cs pin
.set_mode(0, gpio::PinMode::Output50MHz)
.set_output_config(0, gpio::OutputConfig::PushPull)
// ST7735 rst pin
.set_mode(1, gpio::PinMode::Output50MHz)
.set_output_config(1, gpio::OutputConfig::PushPull)
// ST7735 rs pin
.set_mode(4, gpio::PinMode::Output50MHz)
.set_output_config(4, gpio::OutputConfig::PushPull)
// ST7735 led pin
.set_mode(8, gpio::PinMode::Output50MHz)
.set_output_config(8, gpio::OutputConfig::PushPull)
2018-09-01 01:06:15 +02:00
});
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-09-04 01:10:27 +02:00
//let mut printer = UsartPrinter::init(p.usart2);
2018-08-19 13:46:08 +02:00
2018-09-02 16:27:09 +02:00
let mut spi = p.spi1;
let mut gpio = p.gpio_a;
let mut st7735 = st7735::St7735::new(st7735::DisplayType::RED_18_BLACKTAB, 0, 1, 4, 8);
let mut st7735io = st7735.start(spi, gpio);
2018-09-04 01:10:27 +02:00
loop {
st7735io.init();
2018-09-05 02:15:11 +02:00
st7735io.set_orientation(st7735::DisplayOrientation::Landscape);
st7735io.fill_rect(0, 0, 160, 128, st7735::COLOR_BLACK);
2018-09-11 22:44:00 +02:00
systick::delay_ms(100);
2018-09-05 02:15:11 +02:00
st7735io.fill_rect(4, 4, 76, 60, st7735::COLOR_RED);
2018-09-11 22:44:00 +02:00
systick::delay_ms(100);
2018-09-05 02:15:11 +02:00
st7735io.fill_rect(4, 64, 76, 60, st7735::COLOR_GREEN);
2018-09-11 22:44:00 +02:00
systick::delay_ms(100);
2018-09-05 02:15:11 +02:00
st7735io.fill_rect(80, 4, 76, 60, st7735::COLOR_BLUE);
2018-09-11 22:44:00 +02:00
systick::delay_ms(100);
2018-09-05 02:15:11 +02:00
st7735io.fill_rect(80, 64, 76, 60, st7735::COLOR_YELLOW);
2018-09-11 22:44:00 +02:00
systick::delay_ms(100);
2018-09-08 20:40:56 +02:00
2018-09-09 03:01:14 +02:00
st7735io.draw_circle(80, 64, 20, st7735::COLOR_MAGENTA);
2018-09-08 20:40:56 +02:00
st7735io.draw_line(4, 4, 80, 64, st7735::COLOR_GREEN);
2018-09-11 22:44:00 +02:00
2018-09-17 01:46:51 +02:00
systick::delay_ms(1000);
st7735io.draw_mono_bitmap(11, 4, &logo::LOGO_BW, st7735::COLOR_WHITE, st7735::COLOR_BLACK);
systick::delay_ms(1000);
st7735io.draw_color_bitmap(11, 4, &logo::LOGO_COLOR);
systick::delay_ms(1000);
st7735io.draw_text(20, 20, "Hello World", &FREE_SANS_FONT, 1, st7735::COLOR_RED);
2018-09-11 22:44:00 +02:00
systick::delay_ms(5000);
2018-09-04 01:10:27 +02:00
}
2018-09-02 16:27:09 +02:00
let (st7735, mut spi, mut gpio) = st7735io.done();
2018-08-20 00:30:54 +02:00
2018-09-04 01:10:27 +02:00
/*
2018-08-28 21:17:26 +02:00
loop {
2018-08-19 13:46:08 +02:00
2018-09-04 01:10:27 +02:00
2018-09-02 16:27:09 +02:00
let res = max6675::read(&mut spi, &mut 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-09-04 01:10:27 +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);
}