Skip to content

Commit

Permalink
Example of fetching GPS data with NMEA via UART
Browse files Browse the repository at this point in the history
  • Loading branch information
sivakov512 committed Nov 5, 2023
1 parent 85f0d22 commit 5b12c42
Show file tree
Hide file tree
Showing 2 changed files with 123 additions and 0 deletions.
1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ defmt = "0.3"
defmt-rtt = "0.4"
embedded-hal = { version = "0.2", features = ["unproven"] }
fugit = "0.3"
nmea = { version = "0.6", default_features = false, features = ["all-sentences"] }
panic-probe = { version = "0.3", features = ["print-defmt"] }
# Board support for RP Pico
rp-pico = "0.8"
Expand Down
122 changes: 122 additions & 0 deletions examples/gps_nmea.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,122 @@
#![no_main]
#![no_std]

use rp_pico as bsp;

use bsp::entry;
use defmt_rtt as _;
use fugit::RateExtU32;
use panic_probe as _;

use bsp::hal::{
self,
clocks::{init_clocks_and_plls, Clock},
gpio::bank0::{Gpio0, Gpio1, Gpio4, Gpio5},
pac,
sio::Sio,
uart::{DataBits, StopBits, UartConfig, UartPeripheral},
watchdog::Watchdog,
};
use core::fmt::Write;
use defmt::info;
use embedded_hal::serial::Read;

type GPSUartPins = (
hal::gpio::Pin<Gpio4, hal::gpio::FunctionUart, hal::gpio::PullNone>,
hal::gpio::Pin<Gpio5, hal::gpio::FunctionUart, hal::gpio::PullNone>,
);

type DebugUartPins = (
hal::gpio::Pin<Gpio0, hal::gpio::FunctionUart, hal::gpio::PullNone>,
hal::gpio::Pin<Gpio1, hal::gpio::FunctionUart, hal::gpio::PullNone>,
);

const NMEA_LENGTH: usize = 164;
const START_BYTE: u8 = 36;

#[entry]
fn main() -> ! {
info!("Running gps_nmea program");

let mut pac = pac::Peripherals::take().unwrap();
let mut watchdog = Watchdog::new(pac.WATCHDOG);
let sio = Sio::new(pac.SIO);

let clocks = init_clocks_and_plls(
bsp::XOSC_CRYSTAL_FREQ,
pac.XOSC,
pac.CLOCKS,
pac.PLL_SYS,
pac.PLL_USB,
&mut pac.RESETS,
&mut watchdog,
)
.ok()
.unwrap();

let pins = bsp::Pins::new(
pac.IO_BANK0,
pac.PADS_BANK0,
sio.gpio_bank0,
&mut pac.RESETS,
);

let mut gps_uart = UartPeripheral::new(
pac.UART1,
(pins.gpio4.reconfigure(), pins.gpio5.reconfigure()) as GPSUartPins,
&mut pac.RESETS,
)
.enable(
UartConfig::new(9_600.Hz(), DataBits::Eight, None, StopBits::One),
clocks.peripheral_clock.freq(),
)
.unwrap();

let mut debug_uart = UartPeripheral::new(
pac.UART0,
(pins.gpio0.reconfigure(), pins.gpio1.reconfigure()) as DebugUartPins,
&mut pac.RESETS,
)
.enable(
UartConfig::new(9_600.Hz(), DataBits::Eight, None, StopBits::One),
clocks.peripheral_clock.freq(),
)
.unwrap();

let mut buffer: [u8; NMEA_LENGTH] = [0; NMEA_LENGTH];
let mut pointer = 0;

let mut parser = nmea::Nmea::default();

loop {
while let Ok(byte) = gps_uart.read() {
if pointer == 0 && byte != START_BYTE {
continue;
}

if pointer >= NMEA_LENGTH {
pointer = 0;
continue;
}

if byte == START_BYTE {
if pointer != 0 {
if let Ok(ascii) = core::str::from_utf8(&buffer[..pointer]) {
write!(debug_uart, "{}", ascii).unwrap();

if let Ok(_) = parser.parse(ascii) {
write!(debug_uart, "{:?}", parser).unwrap();
}

write!(debug_uart, "\n").unwrap();
}
}

pointer = 0
}

buffer[pointer] = byte;
pointer += 1;
}
}
}

0 comments on commit 5b12c42

Please sign in to comment.