reflow-firmware3.0/src/application/setup.rs

103 lines
2.7 KiB
Rust

use embedded_hal::digital::v2::OutputPin;
use rtt_target::rprintln;
use stm32f1xx_hal::{
delay::Delay,
prelude::*,
qei,
spi::{Mode, Phase, Polarity, Spi},
stm32,
timer::Timer,
};
use crate::application::styles::Styles;
use crate::application::App;
pub fn setup(cp: cortex_m::peripheral::Peripherals, dp: stm32::Peripherals) -> App {
// Take ownership over the raw flash and rcc devices and convert them into the corresponding
// HAL structs
let mut flash = dp.FLASH.constrain();
let mut rcc = dp.RCC.constrain();
// Freeze the configuration of all the clocks in the system and store the frozen frequencies in
// `clocks`
let clocks = rcc
.cfgr
.use_hse(8.mhz())
.sysclk(72.mhz())
.pclk1(36.mhz())
.freeze(&mut flash.acr);
rprintln!("Clock Setup done");
// Acquire the GPIOC peripheral
let mut gpioa = dp.GPIOA.split(&mut rcc.apb2);
let mut gpiob = dp.GPIOB.split(&mut rcc.apb2);
let mut gpioc = dp.GPIOC.split(&mut rcc.apb2);
let led = gpioc.pc13.into_push_pull_output(&mut gpioc.crh);
let mut heater = gpiob.pb6.into_push_pull_output(&mut gpiob.crl);
heater.set_low().unwrap();
let delay = Delay::new(cp.SYST, clocks);
let mut afio = dp.AFIO.constrain(&mut rcc.apb2);
let (_, pb3, pb4) = afio.mapr.disable_jtag(gpioa.pa15, gpiob.pb3, gpiob.pb4);
let qei = Timer::tim3(dp.TIM3, &clocks, &mut rcc.apb1).qei(
(pb4, gpiob.pb5),
&mut afio.mapr,
qei::QeiOptions::default(),
);
let button = pb3;
// SPI1
let sck = gpioa.pa5.into_alternate_push_pull(&mut gpioa.crl);
let miso = gpioa.pa6;
let mosi = gpioa.pa7.into_alternate_push_pull(&mut gpioa.crl);
let disp_cs = gpioa.pa0.into_push_pull_output(&mut gpioa.crl);
let max_cs = gpioa.pa9.into_push_pull_output(&mut gpioa.crh);
let rst = gpioa.pa1.into_push_pull_output(&mut gpioa.crl);
let dc = gpioa.pa4.into_push_pull_output(&mut gpioa.crl);
let mut disp_led = gpioa.pa8.into_push_pull_output(&mut gpioa.crh);
disp_led.set_high().unwrap();
let spi = Spi::spi1(
dp.SPI1,
(sck, miso, mosi),
&mut afio.mapr,
Mode {
polarity: Polarity::IdleLow,
phase: Phase::CaptureOnFirstTransition,
},
16.mhz(),
clocks,
&mut rcc.apb2,
);
App {
delay: delay,
board_led: led,
spi: Some(spi),
disp_cs: disp_cs,
disp_dc: Some(dc),
disp_rst: Some(rst),
max_cs: max_cs,
qei: qei,
button: button,
selected_profile: 0,
styles: Styles::new(),
temp_avg: 0.0,
heater: heater,
}
}