wspr-beacon/src/application/setup.rs

91 lines
2.3 KiB
Rust

use stm32f1xx_hal::{
delay::Delay,
i2c,
pac::Interrupt,
prelude::*,
serial::{Config, Serial},
stm32,
timer::{Event, Timer},
};
use nmea0183::Parser;
use crate::application::App;
use crate::time;
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);
defmt::info!("Clock Setup done");
// Acquire the GPIOC peripheral
let mut gpiob = dp.GPIOB.split(&mut rcc.apb2);
let mut gpioc = dp.GPIOC.split(&mut rcc.apb2);
let mut afio = dp.AFIO.constrain(&mut rcc.apb2);
let board_led = gpioc.pc13.into_push_pull_output(&mut gpioc.crh);
let delay = Delay::new(cp.SYST, clocks);
// USART3
// Configure pb10 as a push_pull output, this will be the tx pin
let tx = gpiob.pb10.into_alternate_push_pull(&mut gpiob.crh);
// Take ownership over pb11
let rx = gpiob.pb11;
// Set up the usart device. Taks ownership over the USART register and tx/rx pins. The rest of
// the registers are used to enable and configure the device.
let serial = Serial::usart3(
dp.USART3,
(tx, rx),
&mut afio.mapr,
Config::default().baudrate(9600.bps()),
clocks,
&mut rcc.apb1,
);
let gps_parser = Parser::new();
let scl = gpiob.pb6.into_alternate_open_drain(&mut gpiob.crl);
let sda = gpiob.pb7.into_alternate_open_drain(&mut gpiob.crl);
let mut i2c = i2c::BlockingI2c::i2c1(
dp.I2C1,
(scl, sda),
&mut afio.mapr,
i2c::Mode::Standard {
frequency: 400_000.hz(),
},
clocks,
&mut rcc.apb1,
5,
1,
5,
5,
);
time::setup(dp.TIM2, &clocks, &mut rcc.apb1);
App {
delay,
board_led,
serial,
i2c,
gps_parser,
locator: arrayvec::ArrayString::new(),
}
}