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");
	}   
   }  
} 
Committer:
trevieze
Date:
Sat Feb 18 01:37:25 2017 +0000
Revision:
4:7240a18102a6
Parent:
3:e64b8be9ce92
Added missing seconds in structure

Who changed what in which revision?

UserRevisionLine numberNew contents of line
trevieze 1:4b5ffae743c0 1 /* mbed EM-406 GPS Module Library
trevieze 1:4b5ffae743c0 2 * Copyright (c) 2008-2010, sford
trevieze 1:4b5ffae743c0 3 *
trevieze 1:4b5ffae743c0 4 * Permission is hereby granted, free of charge, to any person obtaining a copy
trevieze 1:4b5ffae743c0 5 * of this software and associated documentation files (the "Software"), to deal
trevieze 1:4b5ffae743c0 6 * in the Software without restriction, including without limitation the rights
trevieze 1:4b5ffae743c0 7 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
trevieze 1:4b5ffae743c0 8 * copies of the Software, and to permit persons to whom the Software is
trevieze 1:4b5ffae743c0 9 * furnished to do so, subject to the following conditions:
trevieze 1:4b5ffae743c0 10 *
trevieze 1:4b5ffae743c0 11 * The above copyright notice and this permission notice shall be included in
trevieze 1:4b5ffae743c0 12 * all copies or substantial portions of the Software.
trevieze 1:4b5ffae743c0 13 *
trevieze 1:4b5ffae743c0 14 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
trevieze 1:4b5ffae743c0 15 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
trevieze 1:4b5ffae743c0 16 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
trevieze 1:4b5ffae743c0 17 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
trevieze 1:4b5ffae743c0 18 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
trevieze 1:4b5ffae743c0 19 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
trevieze 1:4b5ffae743c0 20 * THE SOFTWARE.
trevieze 1:4b5ffae743c0 21 */
trevieze 3:e64b8be9ce92 22
trevieze 3:e64b8be9ce92 23 #include <string>
trevieze 2:dbc6c0789611 24 #include "nmea.h"
trevieze 1:4b5ffae743c0 25 #include "GPSISR.h"
trevieze 1:4b5ffae743c0 26
trevieze 2:dbc6c0789611 27 NMEA nmea; //parse GPS Sentence.
trevieze 2:dbc6c0789611 28
trevieze 1:4b5ffae743c0 29 GPS::GPS(PinName tx, PinName rx, int Baud) : _gps(tx, rx) {
trevieze 1:4b5ffae743c0 30 _gps.baud(Baud);
trevieze 1:4b5ffae743c0 31 _gps.attach(this,&GPS::Rx_interrupt, Serial::RxIrq);
trevieze 2:dbc6c0789611 32
trevieze 1:4b5ffae743c0 33 printf("Interrupt Attached");
trevieze 1:4b5ffae743c0 34 }
trevieze 1:4b5ffae743c0 35
trevieze 1:4b5ffae743c0 36 void GPS::getline() {
trevieze 1:4b5ffae743c0 37 while(_gps.getc() != '$'); // wait for the start of a line
trevieze 1:4b5ffae743c0 38 for(int i=0; i<256; i++) {
trevieze 1:4b5ffae743c0 39 msg[i] = _gps.getc();
trevieze 1:4b5ffae743c0 40 if(msg[i] == '\r') {
trevieze 1:4b5ffae743c0 41 msg[i] = 0;
trevieze 1:4b5ffae743c0 42 return;
trevieze 1:4b5ffae743c0 43 }
trevieze 1:4b5ffae743c0 44 }
trevieze 1:4b5ffae743c0 45 error("Overflowed message limit");
trevieze 1:4b5ffae743c0 46 return;
trevieze 1:4b5ffae743c0 47 }
trevieze 1:4b5ffae743c0 48
trevieze 1:4b5ffae743c0 49 // Interupt Routine to read in data from serial port
trevieze 1:4b5ffae743c0 50 void GPS::Rx_interrupt(void) {
trevieze 1:4b5ffae743c0 51 interrupt = 1;
trevieze 1:4b5ffae743c0 52 rx_in = _gps.getc();
trevieze 2:dbc6c0789611 53 nmea.fusedata(rx_in);
trevieze 1:4b5ffae743c0 54 interrupt = 0;
trevieze 1:4b5ffae743c0 55 return;
trevieze 1:4b5ffae743c0 56 }
trevieze 3:e64b8be9ce92 57 bool GPS::dataready(void){
trevieze 2:dbc6c0789611 58 return nmea.isdataready();
trevieze 2:dbc6c0789611 59
trevieze 2:dbc6c0789611 60 }
trevieze 3:e64b8be9ce92 61
trevieze 3:e64b8be9ce92 62 void GPS::read(void) {
trevieze 3:e64b8be9ce92 63
trevieze 4:7240a18102a6 64 buffer.hours = nmea.getHour();
trevieze 3:e64b8be9ce92 65 buffer.minutes = nmea.getMinute();
trevieze 4:7240a18102a6 66 buffer.seconds = nmea.getSecond();
trevieze 3:e64b8be9ce92 67 buffer.day = nmea.getDay();
trevieze 3:e64b8be9ce92 68 buffer.month = nmea.getMonth();
trevieze 3:e64b8be9ce92 69 buffer.year = nmea.getYear();
trevieze 3:e64b8be9ce92 70 buffer.latitude = 0.0;
trevieze 3:e64b8be9ce92 71 buffer.longitude = 0.0;
trevieze 3:e64b8be9ce92 72
trevieze 3:e64b8be9ce92 73 if (nmea.isdataready()) {
trevieze 3:e64b8be9ce92 74 buffer.latitude = nmea.getLatitude();
trevieze 3:e64b8be9ce92 75 buffer.longitude = nmea.getLongitude();
trevieze 3:e64b8be9ce92 76 buffer.speed = nmea.getSpeed();
trevieze 3:e64b8be9ce92 77 buffer.altitude = nmea.getAltitude();
trevieze 3:e64b8be9ce92 78 buffer.bearing = nmea.getBearing();
trevieze 3:e64b8be9ce92 79 buffer.satellites = nmea.getSatellites();
trevieze 3:e64b8be9ce92 80 }
trevieze 3:e64b8be9ce92 81 }