A GPS serial interrupt service routine that has an on the fly nmea parser. Works with a STM32F411RE and a Adafruit GPS logger.

Dependents:   Bicycl_Computer_NUCLEO-F411RE Bicycl_Computer_NUCLEO-L476RG

Fork of GPS by Simon Ford

main.cpp

#include "mbed.h"
#include "GPSISR.h"

#define PIN_RX_GPS      PA_12 //GPS Shield RX pin
#define PIN_TX_GPS      PA_11 //GPS Shield TX pin
Serial pc(USBTX, USBRX);

// Set up serial interrupe service handler for gps characters.
GPS MyGPS(PIN_TX_GPS,PIN_RX_GPS, 9600);
int main()
{
    while (1) {
	if (MyGPS.dataready()) {
					MyGPS.read();
					pc.printf("NMEA has valid data");
					pc.printf("Sats : %d \n", MyGPS.buffer.satellites);
					pc.printf("%d-%d-%d\n", MyGPS.buffer.month, MyGPS.buffer.day, MyGPS.buffer.year);
					pc.printf("%d:%d:%d\n", MyGPS.buffer.hours, MyGPS.buffer.minutes, MyGPS.buffer.seconds);
	}
	else {
                pc.printf("NMEA has no valid data");
	}   
   }  
} 

nmea.h

Committer:
trevieze
Date:
2017-03-01
Revision:
5:c5f700c1e1af
Parent:
1:4b5ffae743c0

File content as of revision 5:c5f700c1e1af:

/*
    File:       nmea.h
    Version:    0.1.0
    Date:       Feb. 23, 2013
	License:	GPL v2
    
	NMEA GPS content parser
    
    ****************************************************************************
    Copyright (C) 2013 Radu Motisan  <radu.motisan@gmail.com>
	
	http://www.pocketmagic.net

    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 2 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, write to the Free Software
    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
    ****************************************************************************
 */
//#include "../timeout.h"



class NMEA {
	
	private:
		bool			m_bFlagRead,					// flag used by the parser, when a valid sentence has begun
						m_bFlagDataReady;				// valid GPS fix and data available, user can call reader functions
		char			tmp_words[20][15],				//	hold parsed words for one given NMEA sentence
						tmp_szChecksum[15];				//	hold the received checksum for one given NMEA sentence
		
		// will be set to true for characters between $ and * only
		bool			m_bFlagComputedCks ;			// used to compute checksum and indicate valid checksum interval (between $ and * in a given sentence)
		int				m_nChecksum ;					// numeric checksum, computed for a given sentence
		bool			m_bFlagReceivedCks ;			// after getting  * we start cuttings the received checksum
		int				index_received_checksum ;		// used to parse received checksum
		
		// word cutting variables
		int				m_nWordIdx ,					// the current word in a sentence
						m_nPrevIdx,						// last character index where we did a cut
						m_nNowIdx ;						// current character index
		
		// globals to store parser results
		float			res_fLongitude;					// GPRMC and GPGGA
		char			res_clon;						// E or W
		float			res_fLatitude;					// GPRMC and GPGGA
		char			res_clat;						// N or S
		unsigned char	res_nUTCHour, res_nUTCMin, res_nUTCSec,		// GPRMC and GPGGA 
						res_nUTCDay, res_nUTCMonth, res_nUTCYear;	// GPRMC
		int				res_nSatellitesUsed;			// GPGGA
		float			res_fAltitude;					// GPGGA
		float			res_fSpeed;						// GPRMC
		float			res_fBearing;					// GPRMC
		
		// the parser, currently handling GPRMC and GPGGA, but easy to add any new sentences
		void			parsedata();
		// aux functions
		int				digit2dec(char hexdigit);
		float			string2float(char* s);
		int				mstrcmp(const char *s1, const char *s2);
		float 			trunc(float v);
		
	public:
		// constructor, initing a few variables
		NMEA() {
			m_bFlagRead = false; //are we in a sentence?
			m_bFlagDataReady = false; //is data available?
		}
		
		/*
		 * The serial data is assembled on the fly, without using any redundant buffers. 
		 * When a sentence is complete (one that starts with $, ending in EOL), all processing is done on 
		 * this temporary buffer that we've built: checksum computation, extracting sentence "words" (the CSV values), 
		 * and so on.
		 * When a new sentence is fully assembled using the fusedata function, the code calls parsedata. 
		 * This function in turn, splits the sentences and interprets the data. Here is part of the parser function, 
		 * handling both the $GPRMC NMEA sentence:
		 */
		int				fusedata(char c);
		

		// READER functions: retrieving results, call isdataready() first
		bool			isdataready();
		int				getHour();
		int				getMinute();
		int				getSecond();
		int				getDay();
		int				getMonth();
		int				getYear();
		float			getLatitude();
		char			getlatc();
		float			getLongitude();
		char			getlonc();
		int				getSatellites();
		float			getAltitude();
		float			getSpeed();
		float			getBearing();
	};