From f09d9820ed40371f552d3a910bc2d8170d290653 Mon Sep 17 00:00:00 2001 From: Nick Foster Date: Fri, 13 Aug 2010 11:34:07 -0700 Subject: first stab at a GPS driver in gps_ctrl.cpp. not the most expandable thing in the world but there's only so many GPS interfaces out there. --- host/lib/usrp/usrp2/gps_ctrl.cpp | 140 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 140 insertions(+) create mode 100644 host/lib/usrp/usrp2/gps_ctrl.cpp (limited to 'host/lib/usrp/usrp2/gps_ctrl.cpp') diff --git a/host/lib/usrp/usrp2/gps_ctrl.cpp b/host/lib/usrp/usrp2/gps_ctrl.cpp new file mode 100644 index 000000000..5c015be14 --- /dev/null +++ b/host/lib/usrp/usrp2/gps_ctrl.cpp @@ -0,0 +1,140 @@ +// +// Copyright 2010 Ettus Research LLC +// +// This program is free software: you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation, either version 3 of the License, or +// (at your option) any later version. +// +// This program is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. +// +// You should have received a copy of the GNU General Public License +// along with this program. If not, see . +// + +#include "gps_ctrl.hpp" +#include +#include +#include +#include + +using namespace uhd; +using namespace boost::gregorian; +using namespace boost::posix_time; + +/*! + * A usrp2 GPS control for Jackson Labs devices + */ + +//TODO: multiple baud rate support (requires mboard_impl changes for poking UART registers), NMEA support, better autodetection +class usrp2_gps_ctrl_impl : public usrp2_gps_ctrl{ +public: + usrp2_gps_ctrl_impl(usrp2_iface::sptr iface){ + _iface = iface; + //do init here + //so the Jackson Labs Firefly (and Fury) don't acknowledge successful commands -- only invalid ones. + //first we test to see if there's a Firefly/Fury connected by sending an invalid packet and listening for the response + + std::string reply; + + //TODO: try multiple baud rates (many GPS's are set up for 4800bps, you're fixed at 115200bps 8N1 right now) + //you have to poke registers in order to set baud rate, there's no dude/bro interface for it + _iface->write_uart(GPS_UART, "HAAAY GUYYYYS\n"); + try { + reply = _iface->read_uart(GPS_UART, 20); + } catch (std::runtime_error err) { + if(err.what() != std::string("usrp2 no control response")) throw; //sorry can't cope with that + else { //we don't actually have a GPS installed + gps_type = GPS_TYPE_NONE; + } + } + + if(reply == "Command Error") gps_type = GPS_TYPE_JACKSON_LABS; + else gps_type = GPS_TYPE_NONE; //we'll add NMEA support later + + switch(gps_type) { + case GPS_TYPE_JACKSON_LABS: + //issue some setup stuff so it quits spewing data out when not asked to + //none of these should issue replies so we don't bother looking for it + _iface->write_uart(GPS_UART, "SYST:COMM:SER:"); + _iface->write_uart(GPS_UART, "ECHO OFF\n"); //we split lines before 20 chars right now -- TODO: fix driver to split writes/reads for you + _iface->write_uart(GPS_UART, "SYST:COMM:SER:"); + _iface->write_uart(GPS_UART, "PRO OFF\n"); + _iface->write_uart(GPS_UART, "GPS:GPGGA 0\n"); + _iface->write_uart(GPS_UART, "GPS:GGAST 0\n"); + break; + + case GPS_TYPE_GENERIC_NMEA: + case GPS_TYPE_NONE: + default: + + break; + } + } + + ~usrp2_gps_ctrl_impl(void){ + + } + + ptime get_time(void) { + std::string reply; + ptime now; + switch(gps_type) { + case GPS_TYPE_JACKSON_LABS: + _iface->write_uart(GPS_UART, "PTIME:TIME\n"); + reply = _iface->read_uart(GPS_UART, 20); + now = ptime(get_date(), duration_from_string(reply)); + break; + case GPS_TYPE_GENERIC_NMEA: + case GPS_TYPE_NONE: + default: + throw std::runtime_error("get_time(): Unsupported GPS or no GPS detected\n"); + break; + } + return now; + } + + date get_date(void) { + std::string reply; + date today; + switch(gps_type) { + case GPS_TYPE_JACKSON_LABS: + _iface->write_uart(GPS_UART, "PTIME:DATE\n"); + reply = _iface->read_uart(GPS_UART, 20); + today = from_string(reply); + break; + case GPS_TYPE_GENERIC_NMEA: + case GPS_TYPE_NONE: + default: + throw std::runtime_error("get_date(): Unsupported GPS or no GPS detected\n"); + break; + } + return today; + } + + bool gps_detected(void) { + return (gps_type != GPS_TYPE_NONE); + } + +private: + usrp2_iface::sptr _iface; + + enum { + GPS_TYPE_JACKSON_LABS, + GPS_TYPE_GENERIC_NMEA, + GPS_TYPE_NONE + } gps_type; + + static const int GPS_UART = 2; //TODO: this should be plucked from fw_common.h or memory_map.h or somewhere in common with the firmware + +}; + +/*********************************************************************** + * Public make function for the GPS control + **********************************************************************/ +usrp2_gps_ctrl::sptr usrp2_gps_ctrl::make(usrp2_iface::sptr iface){ + return sptr(new usrp2_gps_ctrl_impl(iface)); +} -- cgit v1.2.3 From 5ab3f053529bb8d85a7f1ba9e1dd113c1f6eb813 Mon Sep 17 00:00:00 2001 From: Nick Foster Date: Fri, 13 Aug 2010 13:07:56 -0700 Subject: GPS interface works for Jackson Labs devices. --- host/lib/usrp/usrp2/gps_ctrl.cpp | 4 +++- host/lib/usrp/usrp2/usrp2_iface.cpp | 3 +-- 2 files changed, 4 insertions(+), 3 deletions(-) (limited to 'host/lib/usrp/usrp2/gps_ctrl.cpp') diff --git a/host/lib/usrp/usrp2/gps_ctrl.cpp b/host/lib/usrp/usrp2/gps_ctrl.cpp index 5c015be14..7152800aa 100644 --- a/host/lib/usrp/usrp2/gps_ctrl.cpp +++ b/host/lib/usrp/usrp2/gps_ctrl.cpp @@ -42,9 +42,10 @@ public: //TODO: try multiple baud rates (many GPS's are set up for 4800bps, you're fixed at 115200bps 8N1 right now) //you have to poke registers in order to set baud rate, there's no dude/bro interface for it - _iface->write_uart(GPS_UART, "HAAAY GUYYYYS\n"); + _iface->write_uart(GPS_UART, "HAAAY GUYYYYS\n"); try { reply = _iface->read_uart(GPS_UART, 20); + //std::cerr << "Got reply from GPS: " << reply.c_str() << " with length = " << reply.length() << std::endl; } catch (std::runtime_error err) { if(err.what() != std::string("usrp2 no control response")) throw; //sorry can't cope with that else { //we don't actually have a GPS installed @@ -57,6 +58,7 @@ public: switch(gps_type) { case GPS_TYPE_JACKSON_LABS: + std::cerr << "Found a Jackson Labs GPS" << std::endl; //issue some setup stuff so it quits spewing data out when not asked to //none of these should issue replies so we don't bother looking for it _iface->write_uart(GPS_UART, "SYST:COMM:SER:"); diff --git a/host/lib/usrp/usrp2/usrp2_iface.cpp b/host/lib/usrp/usrp2/usrp2_iface.cpp index 04b3b1e74..f4d354204 100644 --- a/host/lib/usrp/usrp2/usrp2_iface.cpp +++ b/host/lib/usrp/usrp2/usrp2_iface.cpp @@ -208,8 +208,7 @@ public: UHD_ASSERT_THROW(ntohl(in_data.id) == USRP2_CTRL_ID_I_HELLA_READ_THAT_UART_DUDE); //copy out the data - std::string result; - std::copy(in_data.data.uart_args.data, in_data.data.uart_args.data + num_bytes, result.begin()); + std::string result((const char *)in_data.data.uart_args.data, (size_t)in_data.data.uart_args.bytes); return result; } -- cgit v1.2.3 From 727d8711f94bf7fe634fc33659b77bc4b4884d9c Mon Sep 17 00:00:00 2001 From: Nick Foster Date: Fri, 13 Aug 2010 16:16:22 -0700 Subject: Support for NMEA reads. Uses NMEA parsing instead of Jackson Labs parsing. No multibaud support yet. read/write_uart() now do multiple-packet writes in multiples of 20 bytes (hardcoded). --- host/lib/usrp/usrp2/gps_ctrl.cpp | 65 ++++++++++++++++++++----------------- host/lib/usrp/usrp2/mboard_impl.cpp | 4 +++ host/lib/usrp/usrp2/usrp2_iface.cpp | 29 ++++++++++++----- host/lib/usrp/usrp2/usrp2_iface.hpp | 2 +- 4 files changed, 62 insertions(+), 38 deletions(-) (limited to 'host/lib/usrp/usrp2/gps_ctrl.cpp') diff --git a/host/lib/usrp/usrp2/gps_ctrl.cpp b/host/lib/usrp/usrp2/gps_ctrl.cpp index 7152800aa..20d670f81 100644 --- a/host/lib/usrp/usrp2/gps_ctrl.cpp +++ b/host/lib/usrp/usrp2/gps_ctrl.cpp @@ -20,10 +20,14 @@ #include #include #include +#include +#include +#include using namespace uhd; using namespace boost::gregorian; using namespace boost::posix_time; +using namespace boost::algorithm; /*! * A usrp2 GPS control for Jackson Labs devices @@ -42,9 +46,10 @@ public: //TODO: try multiple baud rates (many GPS's are set up for 4800bps, you're fixed at 115200bps 8N1 right now) //you have to poke registers in order to set baud rate, there's no dude/bro interface for it + _iface->read_uart(GPS_UART); //flush it out _iface->write_uart(GPS_UART, "HAAAY GUYYYYS\n"); try { - reply = _iface->read_uart(GPS_UART, 20); + reply = _iface->read_uart(GPS_UART); //std::cerr << "Got reply from GPS: " << reply.c_str() << " with length = " << reply.length() << std::endl; } catch (std::runtime_error err) { if(err.what() != std::string("usrp2 no control response")) throw; //sorry can't cope with that @@ -53,7 +58,7 @@ public: } } - if(reply == "Command Error") gps_type = GPS_TYPE_JACKSON_LABS; + if(trim_right_copy(reply) == "Command Error") gps_type = GPS_TYPE_JACKSON_LABS; else gps_type = GPS_TYPE_NONE; //we'll add NMEA support later switch(gps_type) { @@ -61,12 +66,18 @@ public: std::cerr << "Found a Jackson Labs GPS" << std::endl; //issue some setup stuff so it quits spewing data out when not asked to //none of these should issue replies so we don't bother looking for it - _iface->write_uart(GPS_UART, "SYST:COMM:SER:"); - _iface->write_uart(GPS_UART, "ECHO OFF\n"); //we split lines before 20 chars right now -- TODO: fix driver to split writes/reads for you - _iface->write_uart(GPS_UART, "SYST:COMM:SER:"); - _iface->write_uart(GPS_UART, "PRO OFF\n"); + //we have to sleep between commands because the JL device, despite not acking, takes considerable time to process each command. + boost::this_thread::sleep(boost::posix_time::milliseconds(200)); + _iface->write_uart(GPS_UART, "SYST:COMM:SER:ECHO OFF\n"); + boost::this_thread::sleep(boost::posix_time::milliseconds(200)); + _iface->write_uart(GPS_UART, "SYST:COMM:SER:PRO OFF\n"); + boost::this_thread::sleep(boost::posix_time::milliseconds(200)); _iface->write_uart(GPS_UART, "GPS:GPGGA 0\n"); + boost::this_thread::sleep(boost::posix_time::milliseconds(200)); _iface->write_uart(GPS_UART, "GPS:GGAST 0\n"); + boost::this_thread::sleep(boost::posix_time::milliseconds(200)); + _iface->write_uart(GPS_UART, "GPS:GPRMC 1\n"); + boost::this_thread::sleep(boost::posix_time::milliseconds(200)); break; case GPS_TYPE_GENERIC_NMEA: @@ -84,13 +95,27 @@ public: ptime get_time(void) { std::string reply; ptime now; + boost::tokenizer > tok(reply); + std::vector toked; switch(gps_type) { - case GPS_TYPE_JACKSON_LABS: - _iface->write_uart(GPS_UART, "PTIME:TIME\n"); - reply = _iface->read_uart(GPS_UART, 20); - now = ptime(get_date(), duration_from_string(reply)); - break; + case GPS_TYPE_JACKSON_LABS: //deprecated in favor of a single NMEA parser case GPS_TYPE_GENERIC_NMEA: + while(reply.length() == 0) reply = _iface->read_uart(GPS_UART); //loop until we hear something + tok.assign(reply); + toked.assign(tok.begin(), tok.end()); + + UHD_ASSERT_THROW(toked.size() == 11); //if it's not we got something weird in there + + now = ptime( date( + greg_year(boost::lexical_cast(toked[8].substr(4, 2)) + 2000), //just trust me on this one + greg_month(boost::lexical_cast(toked[8].substr(2, 2))), + greg_day(boost::lexical_cast(toked[8].substr(0, 2))) + ), + hours( boost::lexical_cast(toked[1].substr(0, 2))) + + minutes(boost::lexical_cast(toked[1].substr(2, 2))) + + seconds(boost::lexical_cast(toked[1].substr(4, 2))) + ); + break; case GPS_TYPE_NONE: default: throw std::runtime_error("get_time(): Unsupported GPS or no GPS detected\n"); @@ -99,24 +124,6 @@ public: return now; } - date get_date(void) { - std::string reply; - date today; - switch(gps_type) { - case GPS_TYPE_JACKSON_LABS: - _iface->write_uart(GPS_UART, "PTIME:DATE\n"); - reply = _iface->read_uart(GPS_UART, 20); - today = from_string(reply); - break; - case GPS_TYPE_GENERIC_NMEA: - case GPS_TYPE_NONE: - default: - throw std::runtime_error("get_date(): Unsupported GPS or no GPS detected\n"); - break; - } - return today; - } - bool gps_detected(void) { return (gps_type != GPS_TYPE_NONE); } diff --git a/host/lib/usrp/usrp2/mboard_impl.cpp b/host/lib/usrp/usrp2/mboard_impl.cpp index ed6398405..6c4234641 100644 --- a/host/lib/usrp/usrp2/mboard_impl.cpp +++ b/host/lib/usrp/usrp2/mboard_impl.cpp @@ -26,9 +26,11 @@ #include #include #include +#include using namespace uhd; using namespace uhd::usrp; +using namespace boost::posix_time; /*********************************************************************** * Structors @@ -57,6 +59,8 @@ usrp2_mboard_impl::usrp2_mboard_impl( _serdes_ctrl = usrp2_serdes_ctrl::make(_iface); _gps_ctrl = usrp2_gps_ctrl::make(_iface); + if(_gps_ctrl->gps_detected()) std::cout << "GPS time: " << _gps_ctrl->get_time() << std::endl; + //TODO move to dsp impl... //load the allowed decim/interp rates //_USRP2_RATES = range(4, 128+1, 1) + range(130, 256+1, 2) + range(260, 512+1, 4) diff --git a/host/lib/usrp/usrp2/usrp2_iface.cpp b/host/lib/usrp/usrp2/usrp2_iface.cpp index f4d354204..0771c4945 100644 --- a/host/lib/usrp/usrp2/usrp2_iface.cpp +++ b/host/lib/usrp/usrp2/usrp2_iface.cpp @@ -24,6 +24,7 @@ #include //used for htonl and ntohl #include #include +#include #include #include @@ -176,40 +177,52 @@ public: * UART **********************************************************************/ void write_uart(boost::uint8_t dev, const std::string &buf){ + //first tokenize the string into 20-byte substrings + boost::offset_separator f(20, 1, true, true); + boost::tokenizer tok(buf, f); + std::vector queue(tok.begin(), tok.end()); + + BOOST_FOREACH(std::string item, queue) { //setup the out data usrp2_ctrl_data_t out_data; out_data.id = htonl(USRP2_CTRL_ID_HEY_WRITE_THIS_UART_FOR_ME_BRO); out_data.data.uart_args.dev = dev; - out_data.data.uart_args.bytes = buf.size(); + out_data.data.uart_args.bytes = item.size(); //limitation of uart transaction size - UHD_ASSERT_THROW(buf.size() <= sizeof(out_data.data.uart_args.data)); + UHD_ASSERT_THROW(item.size() <= sizeof(out_data.data.uart_args.data)); //copy in the data - std::copy(buf.begin(), buf.end(), out_data.data.uart_args.data); + std::copy(item.begin(), item.end(), out_data.data.uart_args.data); //send and recv usrp2_ctrl_data_t in_data = this->ctrl_send_and_recv(out_data); UHD_ASSERT_THROW(ntohl(in_data.id) == USRP2_CTRL_ID_MAN_I_TOTALLY_WROTE_THAT_UART_DUDE); + } } - std::string read_uart(boost::uint8_t dev, size_t num_bytes){ + std::string read_uart(boost::uint8_t dev){ + int readlen = 20; + std::string result; + while(readlen == 20) { //while we keep receiving full packets //setup the out data usrp2_ctrl_data_t out_data; out_data.id = htonl(USRP2_CTRL_ID_SO_LIKE_CAN_YOU_READ_THIS_UART_BRO); out_data.data.uart_args.dev = dev; - out_data.data.uart_args.bytes = num_bytes; + out_data.data.uart_args.bytes = 20; //limitation of uart transaction size - UHD_ASSERT_THROW(num_bytes <= sizeof(out_data.data.uart_args.data)); + //UHD_ASSERT_THROW(num_bytes <= sizeof(out_data.data.uart_args.data)); //send and recv usrp2_ctrl_data_t in_data = this->ctrl_send_and_recv(out_data); UHD_ASSERT_THROW(ntohl(in_data.id) == USRP2_CTRL_ID_I_HELLA_READ_THAT_UART_DUDE); + readlen = in_data.data.uart_args.bytes; //copy out the data - std::string result((const char *)in_data.data.uart_args.data, (size_t)in_data.data.uart_args.bytes); - return result; + result += std::string((const char *)in_data.data.uart_args.data, (size_t)readlen); + } + return result; } /*********************************************************************** diff --git a/host/lib/usrp/usrp2/usrp2_iface.hpp b/host/lib/usrp/usrp2/usrp2_iface.hpp index cb247f074..55fbfa7e4 100644 --- a/host/lib/usrp/usrp2/usrp2_iface.hpp +++ b/host/lib/usrp/usrp2/usrp2_iface.hpp @@ -106,7 +106,7 @@ public: virtual void write_uart(boost::uint8_t dev, const std::string &buf) = 0; - virtual std::string read_uart(boost::uint8_t dev, size_t num_bytes) = 0; + virtual std::string read_uart(boost::uint8_t dev) = 0; /*! * Set the hardware revision number. Also selects the proper register set for the device. -- cgit v1.2.3 From 40faee2e6d87f7364a0c0c2cf310f1483c0331cf Mon Sep 17 00:00:00 2001 From: Nick Foster Date: Mon, 16 Aug 2010 16:09:52 -0700 Subject: Finished GPS driver, more or less. Should detect any 115kbaud GPS on the serial port, as long as it outputs GPRMC packets. Tweaked the serial driver for a stupid off-by-one mistake. --- firmware/microblaze/lib/hal_io.c | 17 +++--- firmware/microblaze/lib/hal_uart.c | 2 +- host/lib/usrp/usrp2/gps_ctrl.cpp | 116 +++++++++++++++++++++++++++---------- 3 files changed, 97 insertions(+), 38 deletions(-) (limited to 'host/lib/usrp/usrp2/gps_ctrl.cpp') diff --git a/firmware/microblaze/lib/hal_io.c b/firmware/microblaze/lib/hal_io.c index 261c8cc2a..be4c570c7 100644 --- a/firmware/microblaze/lib/hal_io.c +++ b/firmware/microblaze/lib/hal_io.c @@ -237,10 +237,10 @@ int puts(const char *s) char * fgets(hal_uart_name_t u, char * const s) { - char *x = s; - while((*x=(char)hal_uart_getc(u)) != '\n') x++; - *x = 0; - return s; + char *x = s; + while((*x=(char)hal_uart_getc(u)) != '\n') x++; + *x = 0; + return s; } int @@ -248,8 +248,8 @@ fngets(hal_uart_name_t u, char * const s, int len) { char *x = s; while(((*x=(char)hal_uart_getc(u)) != '\n') && ((x-s) < len)) x++; - *x = 0; - return (x-s); + *x = 0; + return (x-s); } int @@ -258,8 +258,9 @@ fngets_timeout(hal_uart_name_t u, char * const s, int len) char *x = s; while(((*x=(char)hal_uart_getc_timeout(u)) != '\n') && (*x != -1) && ((x-s) < len)) x++; - *x = 0; - return (x-s); + *x = 0; + //printf("Returning from fngets() with string %d of length %d\n", s[0], x-s); + return (x-s); } char * diff --git a/firmware/microblaze/lib/hal_uart.c b/firmware/microblaze/lib/hal_uart.c index 0ac6abd69..7836240fe 100644 --- a/firmware/microblaze/lib/hal_uart.c +++ b/firmware/microblaze/lib/hal_uart.c @@ -108,7 +108,7 @@ hal_uart_getc_timeout(hal_uart_name_t u) int timeout = 0; while (((uart_regs[u].rxlevel) == 0) && (timeout++ < HAL_UART_TIMEOUT_MS)) mdelay(1); - return (timeout == HAL_UART_TIMEOUT_MS) ? -1 : uart_regs[u].rxchar; //return -1 if nothing there, cause fngets to quit + return (timeout >= HAL_UART_TIMEOUT_MS) ? -1 : uart_regs[u].rxchar; //return -1 if nothing there, cause fngets to quit } int hal_uart_rx_flush(hal_uart_name_t u) diff --git a/host/lib/usrp/usrp2/gps_ctrl.cpp b/host/lib/usrp/usrp2/gps_ctrl.cpp index 20d670f81..2273b2cd9 100644 --- a/host/lib/usrp/usrp2/gps_ctrl.cpp +++ b/host/lib/usrp/usrp2/gps_ctrl.cpp @@ -33,74 +33,130 @@ using namespace boost::algorithm; * A usrp2 GPS control for Jackson Labs devices */ -//TODO: multiple baud rate support (requires mboard_impl changes for poking UART registers), NMEA support, better autodetection +//TODO: multiple baud rate support (requires mboard_impl changes for poking UART registers) class usrp2_gps_ctrl_impl : public usrp2_gps_ctrl{ public: usrp2_gps_ctrl_impl(usrp2_iface::sptr iface){ _iface = iface; - //do init here - //so the Jackson Labs Firefly (and Fury) don't acknowledge successful commands -- only invalid ones. - //first we test to see if there's a Firefly/Fury connected by sending an invalid packet and listening for the response std::string reply; + bool i_heard_some_nmea = false, i_heard_something_weird = false; - //TODO: try multiple baud rates (many GPS's are set up for 4800bps, you're fixed at 115200bps 8N1 right now) - //you have to poke registers in order to set baud rate, there's no dude/bro interface for it - _iface->read_uart(GPS_UART); //flush it out - _iface->write_uart(GPS_UART, "HAAAY GUYYYYS\n"); - try { - reply = _iface->read_uart(GPS_UART); - //std::cerr << "Got reply from GPS: " << reply.c_str() << " with length = " << reply.length() << std::endl; - } catch (std::runtime_error err) { - if(err.what() != std::string("usrp2 no control response")) throw; //sorry can't cope with that - else { //we don't actually have a GPS installed - gps_type = GPS_TYPE_NONE; + gps_type = GPS_TYPE_NONE; + +// set_uart_baud_rate(GPS_UART, 115200); + //first we look for a Jackson Labs Firefly (since that's what we sell with the USRP2+...) + + _iface->read_uart(GPS_UART); //get whatever junk is in the rx buffer right now, and throw it away + _iface->write_uart(GPS_UART, "HAAAY GUYYYYS\n"); //to elicit a response from the Firefly + + //then we loop until we either timeout, or until we get a response that indicates we're a JL device + int timeout = GPS_TIMEOUT_TRIES; + while(timeout--) { + reply = safe_gps_read(); + if(trim_right_copy(reply) == "Command Error") { + gps_type = GPS_TYPE_JACKSON_LABS; + break; } + else if(reply.substr(0, 3) == "$GP") i_heard_some_nmea = true; //but keep looking for that "Command Error" response + else if(reply.length() != 0) i_heard_something_weird = true; //probably wrong baud rate } - if(trim_right_copy(reply) == "Command Error") gps_type = GPS_TYPE_JACKSON_LABS; - else gps_type = GPS_TYPE_NONE; //we'll add NMEA support later + if((i_heard_some_nmea) && (gps_type != GPS_TYPE_JACKSON_LABS)) gps_type = GPS_TYPE_GENERIC_NMEA; + + //otherwise, we can try some other common baud rates looking to see if a GPS is connected (todo, later) + if((gps_type == GPS_TYPE_NONE) && i_heard_something_weird) { + std::cout << "Invalid reply, possible incorrect baud rate" << std::endl; + } + + bool found_gprmc = false; switch(gps_type) { case GPS_TYPE_JACKSON_LABS: - std::cerr << "Found a Jackson Labs GPS" << std::endl; - //issue some setup stuff so it quits spewing data out when not asked to - //none of these should issue replies so we don't bother looking for it + std::cout << "Found a Jackson Labs GPS" << std::endl; + //issue some setup stuff so it spits out the appropriate data + //none of these should issue replies so we don't bother looking for them //we have to sleep between commands because the JL device, despite not acking, takes considerable time to process each command. - boost::this_thread::sleep(boost::posix_time::milliseconds(200)); + boost::this_thread::sleep(boost::posix_time::milliseconds(FIREFLY_STUPID_DELAY_MS)); _iface->write_uart(GPS_UART, "SYST:COMM:SER:ECHO OFF\n"); - boost::this_thread::sleep(boost::posix_time::milliseconds(200)); + boost::this_thread::sleep(boost::posix_time::milliseconds(FIREFLY_STUPID_DELAY_MS)); _iface->write_uart(GPS_UART, "SYST:COMM:SER:PRO OFF\n"); - boost::this_thread::sleep(boost::posix_time::milliseconds(200)); + boost::this_thread::sleep(boost::posix_time::milliseconds(FIREFLY_STUPID_DELAY_MS)); _iface->write_uart(GPS_UART, "GPS:GPGGA 0\n"); - boost::this_thread::sleep(boost::posix_time::milliseconds(200)); + boost::this_thread::sleep(boost::posix_time::milliseconds(FIREFLY_STUPID_DELAY_MS)); _iface->write_uart(GPS_UART, "GPS:GGAST 0\n"); - boost::this_thread::sleep(boost::posix_time::milliseconds(200)); + boost::this_thread::sleep(boost::posix_time::milliseconds(FIREFLY_STUPID_DELAY_MS)); _iface->write_uart(GPS_UART, "GPS:GPRMC 1\n"); - boost::this_thread::sleep(boost::posix_time::milliseconds(200)); - break; + boost::this_thread::sleep(boost::posix_time::milliseconds(FIREFLY_STUPID_DELAY_MS)); + +// break; case GPS_TYPE_GENERIC_NMEA: + if(gps_type == GPS_TYPE_GENERIC_NMEA) std::cout << "Found a generic NMEA GPS device" << std::endl; + found_gprmc = false; + //here we loop around looking for a GPRMC packet. if we don't get one, we don't have a usable GPS. + timeout = GPS_TIMEOUT_TRIES; + while(timeout--) { + reply = safe_gps_read(); + if(reply.substr(0, 6) == "$GPRMC") { + found_gprmc = true; + break; + } + } + if(!found_gprmc) { + if(gps_type == GPS_TYPE_JACKSON_LABS) std::cout << "Firefly GPS not locked or warming up." << std::endl; + else std::cout << "GPS does not output GPRMC packets. Cannot retrieve time." << std::endl; + gps_type = GPS_TYPE_NONE; + } + break; + case GPS_TYPE_NONE: default: - break; + } + + } ~usrp2_gps_ctrl_impl(void){ } + std::string safe_gps_read() { + std::string reply; + try { + reply = _iface->read_uart(GPS_UART); + //std::cerr << "Got reply from GPS: " << reply.c_str() << " with length = " << reply.length() << std::endl; + } catch (std::runtime_error err) { + if(err.what() != std::string("usrp2 no control response")) throw; //sorry can't cope with that + else { //we don't actually have a GPS installed + reply = std::string(); + } + } + return reply; + } + ptime get_time(void) { std::string reply; ptime now; boost::tokenizer > tok(reply); std::vector toked; + int timeout = GPS_TIMEOUT_TRIES; + bool found_gprmc = false; switch(gps_type) { case GPS_TYPE_JACKSON_LABS: //deprecated in favor of a single NMEA parser case GPS_TYPE_GENERIC_NMEA: - while(reply.length() == 0) reply = _iface->read_uart(GPS_UART); //loop until we hear something + + while(timeout--) { + reply = safe_gps_read(); + if(reply.substr(0, 6) == "$GPRMC") { + found_gprmc = true; + break; + } + } + UHD_ASSERT_THROW(found_gprmc); + tok.assign(reply); toked.assign(tok.begin(), tok.end()); @@ -138,6 +194,8 @@ private: } gps_type; static const int GPS_UART = 2; //TODO: this should be plucked from fw_common.h or memory_map.h or somewhere in common with the firmware + static const int GPS_TIMEOUT_TRIES = 5; + static const int FIREFLY_STUPID_DELAY_MS = 200; }; -- cgit v1.2.3