Allows for a GPS module to be connected to a serial port and exposes an easy to use API to get the GPS data. New feature, added Mbed/LPC17xx RTC synchronisation

Fork of MODGPS by Andy K

Committer:
AjK
Date:
Thu Apr 21 14:06:17 2011 +0000
Revision:
6:64771e31464e
Parent:
5:7f130f85d5a4
1.16 See ChangeLog.c

Who changed what in which revision?

UserRevisionLine numberNew contents of line
AjK 0:db98027c0bbb 1 /*
AjK 0:db98027c0bbb 2 Copyright (c) 2010 Andy Kirkham
AjK 0:db98027c0bbb 3
AjK 0:db98027c0bbb 4 Permission is hereby granted, free of charge, to any person obtaining a copy
AjK 0:db98027c0bbb 5 of this software and associated documentation files (the "Software"), to deal
AjK 0:db98027c0bbb 6 in the Software without restriction, including without limitation the rights
AjK 0:db98027c0bbb 7 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
AjK 0:db98027c0bbb 8 copies of the Software, and to permit persons to whom the Software is
AjK 0:db98027c0bbb 9 furnished to do so, subject to the following conditions:
AjK 0:db98027c0bbb 10
AjK 0:db98027c0bbb 11 The above copyright notice and this permission notice shall be included in
AjK 0:db98027c0bbb 12 all copies or substantial portions of the Software.
AjK 0:db98027c0bbb 13
AjK 0:db98027c0bbb 14 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
AjK 0:db98027c0bbb 15 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
AjK 0:db98027c0bbb 16 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AjK 0:db98027c0bbb 17 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
AjK 0:db98027c0bbb 18 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
AjK 0:db98027c0bbb 19 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
AjK 0:db98027c0bbb 20 THE SOFTWARE.
AjK 0:db98027c0bbb 21 */
AjK 0:db98027c0bbb 22
AjK 0:db98027c0bbb 23 #include "GPS.h"
AjK 0:db98027c0bbb 24
AjK 0:db98027c0bbb 25 GPS::GPS(PinName tx, PinName rx, const char *name) : Serial(tx, rx, name)
AjK 0:db98027c0bbb 26 {
AjK 3:28a1b60b0f37 27 _nmeaOnUart0 = false;
AjK 3:28a1b60b0f37 28
AjK 3:28a1b60b0f37 29 _lastByte = 0;
AjK 3:28a1b60b0f37 30
AjK 6:64771e31464e 31 _gga = (char *)NULL;
AjK 6:64771e31464e 32
AjK 6:64771e31464e 33 _rmc = (char *)NULL;
AjK 6:64771e31464e 34
AjK 6:64771e31464e 35 _vtg = (char *)NULL;
AjK 6:64771e31464e 36
AjK 0:db98027c0bbb 37 switch(_uidx) {
AjK 0:db98027c0bbb 38 case 1: _base = LPC_UART1; break;
AjK 0:db98027c0bbb 39 case 2: _base = LPC_UART2; break;
AjK 0:db98027c0bbb 40 case 3: _base = LPC_UART3; break;
AjK 0:db98027c0bbb 41 default : _base = NULL; break;
AjK 0:db98027c0bbb 42 }
AjK 0:db98027c0bbb 43
AjK 0:db98027c0bbb 44 _pps = NULL;
AjK 0:db98027c0bbb 45 _ppsInUse = false;
AjK 0:db98027c0bbb 46
AjK 0:db98027c0bbb 47 if (_base != NULL) attach(this, &GPS::rx_irq);
AjK 0:db98027c0bbb 48
AjK 0:db98027c0bbb 49 _second100 = new Ticker;
AjK 0:db98027c0bbb 50 _second100->attach_us(this, &GPS::ticktock, GPS_TICKTOCK);
AjK 0:db98027c0bbb 51 }
AjK 0:db98027c0bbb 52
AjK 0:db98027c0bbb 53 void
AjK 0:db98027c0bbb 54 GPS::ppsAttach(PinName irq, ppsEdgeType type)
AjK 0:db98027c0bbb 55 {
AjK 0:db98027c0bbb 56 if (_pps != NULL) delete(_pps);
AjK 0:db98027c0bbb 57 _pps = new InterruptIn(irq);
AjK 0:db98027c0bbb 58 if (type == ppsRise) _pps->rise(this, &GPS::pps_irq);
AjK 0:db98027c0bbb 59 else _pps->fall(this, &GPS::pps_irq);
AjK 0:db98027c0bbb 60 _ppsInUse = true;
AjK 0:db98027c0bbb 61 }
AjK 0:db98027c0bbb 62
AjK 0:db98027c0bbb 63 void
AjK 0:db98027c0bbb 64 GPS::ppsUnattach(void)
AjK 0:db98027c0bbb 65 {
AjK 0:db98027c0bbb 66 if (_pps != NULL) delete(_pps);
AjK 0:db98027c0bbb 67 _ppsInUse = false;
AjK 0:db98027c0bbb 68 }
AjK 0:db98027c0bbb 69
AjK 0:db98027c0bbb 70 double
AjK 0:db98027c0bbb 71 GPS::latitude(void)
AjK 0:db98027c0bbb 72 {
AjK 0:db98027c0bbb 73 double a, b;
AjK 0:db98027c0bbb 74 do { a = thePlace.lat; b = thePlace.lat; } while (a != b);
AjK 0:db98027c0bbb 75 return a;
AjK 0:db98027c0bbb 76 }
AjK 0:db98027c0bbb 77
AjK 0:db98027c0bbb 78 double
AjK 0:db98027c0bbb 79 GPS::longitude(void)
AjK 0:db98027c0bbb 80 {
AjK 0:db98027c0bbb 81 double a, b;
AjK 0:db98027c0bbb 82 do { a = thePlace.lon; b = thePlace.lon; } while (a != b);
AjK 0:db98027c0bbb 83 return a;
AjK 0:db98027c0bbb 84 }
AjK 0:db98027c0bbb 85
AjK 0:db98027c0bbb 86 double
AjK 0:db98027c0bbb 87 GPS::altitude(void)
AjK 0:db98027c0bbb 88 {
AjK 0:db98027c0bbb 89 double a, b;
AjK 0:db98027c0bbb 90 do { a = thePlace.alt; b = thePlace.alt; } while (a != b);
AjK 0:db98027c0bbb 91 return a;
AjK 0:db98027c0bbb 92 }
AjK 0:db98027c0bbb 93
AjK 0:db98027c0bbb 94 GPS_Geodetic *
AjK 0:db98027c0bbb 95 GPS::geodetic(GPS_Geodetic *q)
AjK 0:db98027c0bbb 96 {
AjK 3:28a1b60b0f37 97 GPS_Geodetic a;
AjK 0:db98027c0bbb 98
AjK 0:db98027c0bbb 99 if (q == NULL) q = new GPS_Geodetic;
AjK 0:db98027c0bbb 100
AjK 0:db98027c0bbb 101 do {
AjK 0:db98027c0bbb 102 memcpy(&a, &thePlace, sizeof(GPS_Geodetic));
AjK 3:28a1b60b0f37 103 memcpy(q, &thePlace, sizeof(GPS_Geodetic));
AjK 0:db98027c0bbb 104 }
AjK 3:28a1b60b0f37 105 while (memcmp(&a, q, sizeof(GPS_Geodetic)) != 0);
AjK 0:db98027c0bbb 106
AjK 0:db98027c0bbb 107 return q;
AjK 0:db98027c0bbb 108 }
AjK 0:db98027c0bbb 109
AjK 2:8aa059e7d8b1 110 GPS_VTG *
AjK 2:8aa059e7d8b1 111 GPS::vtg(GPS_VTG *q)
AjK 2:8aa059e7d8b1 112 {
AjK 3:28a1b60b0f37 113 GPS_VTG a;
AjK 2:8aa059e7d8b1 114
AjK 2:8aa059e7d8b1 115 if (q == NULL) q = new GPS_VTG;
AjK 2:8aa059e7d8b1 116
AjK 2:8aa059e7d8b1 117 do {
AjK 2:8aa059e7d8b1 118 memcpy(&a, &theVTG, sizeof(GPS_VTG));
AjK 3:28a1b60b0f37 119 memcpy(q, &theVTG, sizeof(GPS_VTG));
AjK 2:8aa059e7d8b1 120 }
AjK 3:28a1b60b0f37 121 while (memcmp(&a, q, sizeof(GPS_VTG)) != 0);
AjK 2:8aa059e7d8b1 122
AjK 2:8aa059e7d8b1 123 return q;
AjK 2:8aa059e7d8b1 124 }
AjK 2:8aa059e7d8b1 125
AjK 0:db98027c0bbb 126 void
AjK 0:db98027c0bbb 127 GPS::ticktock(void)
AjK 0:db98027c0bbb 128 {
AjK 6:64771e31464e 129 int i;
AjK 6:64771e31464e 130
AjK 0:db98027c0bbb 131 // Increment the time structure by 1/100th of a second.
AjK 0:db98027c0bbb 132 ++theTime;
AjK 0:db98027c0bbb 133
AjK 0:db98027c0bbb 134 // Test the serial queue.
AjK 0:db98027c0bbb 135 if (process_required) {
AjK 0:db98027c0bbb 136 char *s = buffer[active_buffer == 0 ? 1 : 0];
AjK 0:db98027c0bbb 137 if (!strncmp(s, "$GPRMC", 6)) {
AjK 6:64771e31464e 138 if (_rmc) {
AjK 6:64771e31464e 139 for(i = 0; s[i] != '\n'; i++) {
AjK 6:64771e31464e 140 _rmc[i] = s[i];
AjK 6:64771e31464e 141 }
AjK 6:64771e31464e 142 _rmc[i++] = '\n'; _rmc[i] = '\0';
AjK 6:64771e31464e 143 }
AjK 0:db98027c0bbb 144 theTime.nmea_rmc(s);
AjK 0:db98027c0bbb 145 cb_rmc.call();
AjK 0:db98027c0bbb 146 if (!_ppsInUse) theTime.fractionalReset();
AjK 0:db98027c0bbb 147 }
AjK 0:db98027c0bbb 148 else if (!strncmp(s, "$GPGGA", 6)) {
AjK 6:64771e31464e 149 if (_gga) {
AjK 6:64771e31464e 150 for(int i = 0; s[i] != '\n'; i++) {
AjK 6:64771e31464e 151 _gga[i] = s[i];
AjK 6:64771e31464e 152 }
AjK 6:64771e31464e 153 _gga[i++] = '\n'; _gga[i] = '\0';
AjK 6:64771e31464e 154 }
AjK 0:db98027c0bbb 155 thePlace.nmea_gga(s);
AjK 0:db98027c0bbb 156 cb_gga.call();
AjK 0:db98027c0bbb 157 }
AjK 2:8aa059e7d8b1 158 else if (!strncmp(s, "$GPVTG", 6)) {
AjK 6:64771e31464e 159 if (_vtg) {
AjK 6:64771e31464e 160 for(int i = 0; s[i] != '\n'; i++) {
AjK 6:64771e31464e 161 _vtg[i] = s[i];
AjK 6:64771e31464e 162 }
AjK 6:64771e31464e 163 _vtg[i++] = '\n'; _vtg[i] = '\0';
AjK 6:64771e31464e 164 }
AjK 2:8aa059e7d8b1 165 theVTG.nmea_vtg(s);
AjK 2:8aa059e7d8b1 166 cb_vtg.call();
AjK 2:8aa059e7d8b1 167 }
AjK 6:64771e31464e 168 else {
AjK 6:64771e31464e 169 if (_ukn) {
AjK 6:64771e31464e 170 for(int i = 0; s[i] != '\n'; i++) {
AjK 6:64771e31464e 171 _ukn[i] = s[i];
AjK 6:64771e31464e 172 }
AjK 6:64771e31464e 173 _ukn[i++] = '\n'; _ukn[i] = '\0';
AjK 6:64771e31464e 174 cb_ukn.call();
AjK 6:64771e31464e 175 }
AjK 6:64771e31464e 176 }
AjK 0:db98027c0bbb 177 process_required = false;
AjK 0:db98027c0bbb 178 }
AjK 5:7f130f85d5a4 179
AjK 5:7f130f85d5a4 180 // If we have a valid GPS time then, once per minute, set the RTC.
AjK 5:7f130f85d5a4 181 if (theTime.status == 'A' && theTime.second == 0 && theTime.tenths == 0 && theTime.hundreths == 0) {
AjK 5:7f130f85d5a4 182 // set_time() is defined in rtc_time.h
AjK 5:7f130f85d5a4 183 // http://mbed.org/projects/libraries/svn/mbed/trunk/rtc_time.h
AjK 5:7f130f85d5a4 184 set_time(theTime.to_C_tm());
AjK 5:7f130f85d5a4 185 }
AjK 5:7f130f85d5a4 186
AjK 0:db98027c0bbb 187 }
AjK 0:db98027c0bbb 188
AjK 0:db98027c0bbb 189 void
AjK 0:db98027c0bbb 190 GPS::pps_irq(void)
AjK 0:db98027c0bbb 191 {
AjK 0:db98027c0bbb 192 theTime.fractionalReset();
AjK 0:db98027c0bbb 193 theTime++; // Increment the time/date by one second.
AjK 0:db98027c0bbb 194 cb_pps.call();
AjK 0:db98027c0bbb 195 }
AjK 2:8aa059e7d8b1 196
AjK 0:db98027c0bbb 197 void
AjK 0:db98027c0bbb 198 GPS::rx_irq(void)
AjK 0:db98027c0bbb 199 {
AjK 0:db98027c0bbb 200 uint32_t iir __attribute__((unused));
AjK 0:db98027c0bbb 201 char c;
AjK 0:db98027c0bbb 202
AjK 0:db98027c0bbb 203 if (_base) {
AjK 0:db98027c0bbb 204 iir = (uint32_t)*((char *)_base + GPS_IIR);
AjK 0:db98027c0bbb 205 while((int)(*((char *)_base + GPS_LSR) & 0x1)) {
AjK 3:28a1b60b0f37 206 c = (char)(*((char *)_base + GPS_RBR) & 0xFF);
AjK 3:28a1b60b0f37 207
AjK 3:28a1b60b0f37 208 // strtok workaround.
AjK 3:28a1b60b0f37 209 // Found that ,, together (which some NMEA sentences
AjK 3:28a1b60b0f37 210 // contain for a null/empty field) confuses strtok()
AjK 3:28a1b60b0f37 211 // function. Solution:- Push a "zero" into the string
AjK 3:28a1b60b0f37 212 // for missing/empty fields.
AjK 3:28a1b60b0f37 213 if (c == ',' && _lastByte == ',') {
AjK 3:28a1b60b0f37 214 buffer[active_buffer][rx_buffer_in] = '0';
AjK 3:28a1b60b0f37 215 if (++rx_buffer_in >= GPS_BUFFER_LEN) rx_buffer_in = 0;
AjK 3:28a1b60b0f37 216 }
AjK 3:28a1b60b0f37 217
AjK 3:28a1b60b0f37 218 // Debugging/dumping data.
AjK 3:28a1b60b0f37 219 if (_nmeaOnUart0) LPC_UART0->RBR = c;
AjK 3:28a1b60b0f37 220
AjK 3:28a1b60b0f37 221 // Put the byte into the string.
AjK 3:28a1b60b0f37 222 buffer[active_buffer][rx_buffer_in] = c;
AjK 3:28a1b60b0f37 223 if (++rx_buffer_in >= GPS_BUFFER_LEN) rx_buffer_in = 0;
AjK 3:28a1b60b0f37 224
AjK 3:28a1b60b0f37 225 // Save for next time an irq occurs. See strtok() above.
AjK 3:28a1b60b0f37 226 _lastByte = c;
AjK 3:28a1b60b0f37 227
AjK 3:28a1b60b0f37 228 // If end of NMEA sentence flag for processing.
AjK 0:db98027c0bbb 229 if (c == '\n') {
AjK 0:db98027c0bbb 230 active_buffer = active_buffer == 0 ? 1 : 0;
AjK 0:db98027c0bbb 231 process_required = true;
AjK 0:db98027c0bbb 232 rx_buffer_in = 0;
AjK 3:28a1b60b0f37 233 }
AjK 0:db98027c0bbb 234 }
AjK 0:db98027c0bbb 235 }
AjK 0:db98027c0bbb 236 }