use stm32f1xx_hal::{ delay::Delay, pac::Interrupt, prelude::*, serial::{Config, Serial}, spi::{Mode, Phase, Polarity, Spi}, 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 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 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); let button1 = gpiob.pb0.into_floating_input(&mut gpiob.crl); let button2 = gpiob.pb1.into_floating_input(&mut gpiob.crl); // USART1 // Configure pa9 as a push_pull output, this will be the tx pin let tx = gpioa.pa9.into_alternate_push_pull(&mut gpioa.crh); // Take ownership over pa10 let rx = gpioa.pa10; // 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::usart1( dp.USART1, (tx, rx), &mut afio.mapr, Config::default().baudrate(9600.bps()), clocks, &mut rcc.apb2, ); let gps_parser = Parser::new(); // 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 spi = Spi::spi1( dp.SPI1, (sck, miso, mosi), &mut afio.mapr, Mode { polarity: Polarity::IdleLow, phase: Phase::CaptureOnFirstTransition, }, 1.khz(), clocks, &mut rcc.apb2, ); let disp_strobe = gpioa.pa0.into_push_pull_output(&mut gpioa.crl); time::setup(dp.TIM2, &clocks, &mut rcc.apb1); App { delay, board_led, serial, gps_parser, button1, button2, spi, disp_strobe, } }