Skip to content

Commit

Permalink
Fix GPS configuration
Browse files Browse the repository at this point in the history
  • Loading branch information
SuperTails committed Feb 24, 2024
1 parent b6370d6 commit 4096e5d
Show file tree
Hide file tree
Showing 3 changed files with 70 additions and 14 deletions.
1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ embedded-graphics = "0.8.1"
tinyvec = "1.6.0"
#nmea0183 = "0.4.0"
ublox = { version = "0.4.5", default-features = false }
nb = "1.1.0"

# cargo build/run
[profile.dev]
Expand Down
13 changes: 4 additions & 9 deletions src/bin/minimal.rs
Original file line number Diff line number Diff line change
Expand Up @@ -142,12 +142,11 @@ mod app {
let mut gps_uart = hal::serial::Serial::usart1(
cx.device.USART1,
(tx, rx),
Config::default().baudrate(115200.bps()),
Config::default().baudrate(9600.bps()),
clocks,
&mut rcc.apb2
);
gps_uart.listen(serial::Event::Rxne);
// rtic::pend(Interrupt::USART1);

// Spawn tasks
display_task::spawn().unwrap();
Expand Down Expand Up @@ -182,13 +181,9 @@ mod app {

#[task(binds = USART1, shared = [gps])]
fn on_uart(mut cx: on_uart::Context) {
info!("foo");
if let Ok(b) = cx.shared.gps.lock(|gps| gps.serial.read()) {
info!("hewwo {}", b);
}
// cx.shared.gps.lock(|gps| {
// gps.handle();
// });
cx.shared.gps.lock(|gps| {
gps.handle();
});

}

Expand Down
70 changes: 65 additions & 5 deletions src/gps.rs
Original file line number Diff line number Diff line change
@@ -1,21 +1,73 @@
use core::fmt::{self, Write};

use defmt::{error, info};
use stm32l4xx_hal::hal::serial;
use ublox::{PacketRef, Parser};
use ublox::{CfgMsgAllPortsBuilder, CfgPrtUartBuilder, NavPvt, PacketRef, Parser, UartMode, UartPortId};
use tinyvec::ArrayVec;

struct WriteableArrayVec(ArrayVec<[u8; 256]>);

impl Write for WriteableArrayVec {
fn write_str(&mut self, s: &str) -> fmt::Result {
for b in s.bytes() {
self.0.try_push(b);
}
Ok(())
}
}

impl WriteableArrayVec {
pub fn as_str(&self) -> Option<&str> {
core::str::from_utf8(self.0.as_slice()).ok()
}
}

pub struct Gps<SERIAL> {
pub serial: SERIAL,
pub parser: Parser<ArrayVec<[u8; 256]>>
}

impl<SERIAL> Gps<SERIAL>
where SERIAL: serial::Read<u8>,
SERIAL::Error: core::fmt::Debug
where SERIAL: serial::Read<u8> + serial::Write<u8>,
<SERIAL as serial::Read<u8>>::Error: core::fmt::Debug,
<SERIAL as serial::Write<u8>>::Error: core::fmt::Debug,
{
pub fn new(serial: SERIAL) -> Self {
Self {
let mut s = Self {
serial,
parser: Parser::new(ArrayVec::new())
};

s.configure();

s
}

fn configure(&mut self) {
let packet = CfgPrtUartBuilder {
portid: UartPortId::Uart1,
reserved0: 0,
tx_ready: 0,
mode: UartMode::new(
ublox::DataBits::Eight,
ublox::Parity::None,
ublox::StopBits::One,
),
baud_rate: 9600,
in_proto_mask: ublox::InProtoMask::UBLOX,
out_proto_mask: ublox::OutProtoMask::UBLOX,
flags: 0,
reserved5: 0,
}.into_packet_bytes();

for b in packet {
nb::block!(self.serial.write(b)).unwrap();
}

let packet = CfgMsgAllPortsBuilder::set_rate_for::<NavPvt>([1, 1, 1, 1, 1, 1]).into_packet_bytes();

for b in packet {
nb::block!(self.serial.write(b)).unwrap();
}
}

Expand All @@ -28,7 +80,15 @@ impl<SERIAL> Gps<SERIAL>
PacketRef::NavPvt(navpvt) => info!("lat: {}, lon: {}", navpvt.lat_degrees(), navpvt.lon_degrees()),
_ => info!("other packet")
},
Some(Err(_)) => error!("ubx error"),
Some(Err(err)) => {
let mut s = WriteableArrayVec(Default::default());
write!(&mut s, "{err:?}").unwrap();
if let Some(s) = s.as_str() {
error!("ubx error {}", s);
} else {
error!("ubx error");
}
}
None => break
}
}
Expand Down

0 comments on commit 4096e5d

Please sign in to comment.