From 1d063adee08e11056f486e3c736e8aee6cd5f290 Mon Sep 17 00:00:00 2001 From: LongHairedHacker Date: Mon, 12 Apr 2021 00:06:18 +0200 Subject: [PATCH] Added GPS parsing test --- src/application/mod.rs | 39 ++++++++++++++++++++++++++++++--------- src/application/setup.rs | 33 +++++++++++++++++++++++++++++++-- 2 files changed, 61 insertions(+), 11 deletions(-) diff --git a/src/application/mod.rs b/src/application/mod.rs index 3bc78e3..04cb79c 100644 --- a/src/application/mod.rs +++ b/src/application/mod.rs @@ -1,8 +1,13 @@ use cortex_m::prelude::*; -use embedded_hal::digital::v2::OutputPin; +use embedded_hal::digital::v2::{OutputPin, ToggleableOutputPin}; +use nb::{self, block}; +use nmea0183::{ParseResult, Parser}; use stm32f1xx_hal::{ delay::Delay, - gpio::{gpioc, Output, PushPull}, + gpio::{gpiob, gpioc, Alternate, Floating, Input, Output, PushPull}, + prelude::*, + serial::Serial, + stm32, }; mod setup; @@ -13,20 +18,36 @@ pub use setup::setup; pub struct App { delay: Delay, board_led: gpioc::PC13>, + serial: Serial< + stm32::USART3, + ( + gpiob::PB10>, + gpiob::PB11>, + ), + >, } impl App { pub fn run(mut self) -> ! { defmt::info!("Application Startup!"); - loop { - defmt::info!("Blink!"); - self.board_led.set_high().unwrap(); - self.delay.delay_ms(500u32); + let mut parser = Parser::new(); - defmt::info!("Blonk!"); - self.board_led.set_low().unwrap(); - self.delay.delay_ms(500u32); + loop { + match self.serial.read() { + Ok(byte) => { + self.board_led.toggle().unwrap(); + if let Some(result) = parser.parse_from_byte(byte) { + match result { + Ok(ParseResult::GGA(Some(_))) => defmt::info!("Got GGA"), // Got GGA sentence + Ok(_) => {} // Some other sentences.. + Err(_) => {} // Got parse error + } + } + } + Err(nb::Error::WouldBlock) => {} + Err(nb::Error::Other(_)) => {} + } } //exit(); diff --git a/src/application/setup.rs b/src/application/setup.rs index bd12a64..0828789 100644 --- a/src/application/setup.rs +++ b/src/application/setup.rs @@ -1,4 +1,9 @@ -use stm32f1xx_hal::{delay::Delay, prelude::*, stm32}; +use stm32f1xx_hal::{ + delay::Delay, + prelude::*, + serial::{Config, Serial}, + stm32, +}; use crate::application::App; @@ -21,11 +26,35 @@ pub fn setup(cp: cortex_m::peripheral::Peripherals, dp: stm32::Peripherals) -> A 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); - App { delay, board_led } + // 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, + ); + + App { + delay, + board_led, + serial, + } }