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
GPS.cpp@5:7f130f85d5a4, 2011-04-20 (annotated)
- Committer:
- AjK
- Date:
- Wed Apr 20 09:15:07 2011 +0000
- Revision:
- 5:7f130f85d5a4
- Parent:
- 3:28a1b60b0f37
- Child:
- 6:64771e31464e
1.15 See ChangeLog.c
Who changed what in which revision?
User | Revision | Line number | New 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 | 0:db98027c0bbb | 31 | switch(_uidx) { |
AjK | 0:db98027c0bbb | 32 | case 1: _base = LPC_UART1; break; |
AjK | 0:db98027c0bbb | 33 | case 2: _base = LPC_UART2; break; |
AjK | 0:db98027c0bbb | 34 | case 3: _base = LPC_UART3; break; |
AjK | 0:db98027c0bbb | 35 | default : _base = NULL; break; |
AjK | 0:db98027c0bbb | 36 | } |
AjK | 0:db98027c0bbb | 37 | |
AjK | 0:db98027c0bbb | 38 | _pps = NULL; |
AjK | 0:db98027c0bbb | 39 | _ppsInUse = false; |
AjK | 0:db98027c0bbb | 40 | |
AjK | 0:db98027c0bbb | 41 | if (_base != NULL) attach(this, &GPS::rx_irq); |
AjK | 0:db98027c0bbb | 42 | |
AjK | 0:db98027c0bbb | 43 | _second100 = new Ticker; |
AjK | 0:db98027c0bbb | 44 | _second100->attach_us(this, &GPS::ticktock, GPS_TICKTOCK); |
AjK | 0:db98027c0bbb | 45 | } |
AjK | 0:db98027c0bbb | 46 | |
AjK | 0:db98027c0bbb | 47 | void |
AjK | 0:db98027c0bbb | 48 | GPS::ppsAttach(PinName irq, ppsEdgeType type) |
AjK | 0:db98027c0bbb | 49 | { |
AjK | 0:db98027c0bbb | 50 | if (_pps != NULL) delete(_pps); |
AjK | 0:db98027c0bbb | 51 | _pps = new InterruptIn(irq); |
AjK | 0:db98027c0bbb | 52 | if (type == ppsRise) _pps->rise(this, &GPS::pps_irq); |
AjK | 0:db98027c0bbb | 53 | else _pps->fall(this, &GPS::pps_irq); |
AjK | 0:db98027c0bbb | 54 | _ppsInUse = true; |
AjK | 0:db98027c0bbb | 55 | } |
AjK | 0:db98027c0bbb | 56 | |
AjK | 0:db98027c0bbb | 57 | void |
AjK | 0:db98027c0bbb | 58 | GPS::ppsUnattach(void) |
AjK | 0:db98027c0bbb | 59 | { |
AjK | 0:db98027c0bbb | 60 | if (_pps != NULL) delete(_pps); |
AjK | 0:db98027c0bbb | 61 | _ppsInUse = false; |
AjK | 0:db98027c0bbb | 62 | } |
AjK | 0:db98027c0bbb | 63 | |
AjK | 0:db98027c0bbb | 64 | double |
AjK | 0:db98027c0bbb | 65 | GPS::latitude(void) |
AjK | 0:db98027c0bbb | 66 | { |
AjK | 0:db98027c0bbb | 67 | double a, b; |
AjK | 0:db98027c0bbb | 68 | do { a = thePlace.lat; b = thePlace.lat; } while (a != b); |
AjK | 0:db98027c0bbb | 69 | return a; |
AjK | 0:db98027c0bbb | 70 | } |
AjK | 0:db98027c0bbb | 71 | |
AjK | 0:db98027c0bbb | 72 | double |
AjK | 0:db98027c0bbb | 73 | GPS::longitude(void) |
AjK | 0:db98027c0bbb | 74 | { |
AjK | 0:db98027c0bbb | 75 | double a, b; |
AjK | 0:db98027c0bbb | 76 | do { a = thePlace.lon; b = thePlace.lon; } while (a != b); |
AjK | 0:db98027c0bbb | 77 | return a; |
AjK | 0:db98027c0bbb | 78 | } |
AjK | 0:db98027c0bbb | 79 | |
AjK | 0:db98027c0bbb | 80 | double |
AjK | 0:db98027c0bbb | 81 | GPS::altitude(void) |
AjK | 0:db98027c0bbb | 82 | { |
AjK | 0:db98027c0bbb | 83 | double a, b; |
AjK | 0:db98027c0bbb | 84 | do { a = thePlace.alt; b = thePlace.alt; } while (a != b); |
AjK | 0:db98027c0bbb | 85 | return a; |
AjK | 0:db98027c0bbb | 86 | } |
AjK | 0:db98027c0bbb | 87 | |
AjK | 0:db98027c0bbb | 88 | GPS_Geodetic * |
AjK | 0:db98027c0bbb | 89 | GPS::geodetic(GPS_Geodetic *q) |
AjK | 0:db98027c0bbb | 90 | { |
AjK | 3:28a1b60b0f37 | 91 | GPS_Geodetic a; |
AjK | 0:db98027c0bbb | 92 | |
AjK | 0:db98027c0bbb | 93 | if (q == NULL) q = new GPS_Geodetic; |
AjK | 0:db98027c0bbb | 94 | |
AjK | 0:db98027c0bbb | 95 | do { |
AjK | 0:db98027c0bbb | 96 | memcpy(&a, &thePlace, sizeof(GPS_Geodetic)); |
AjK | 3:28a1b60b0f37 | 97 | memcpy(q, &thePlace, sizeof(GPS_Geodetic)); |
AjK | 0:db98027c0bbb | 98 | } |
AjK | 3:28a1b60b0f37 | 99 | while (memcmp(&a, q, sizeof(GPS_Geodetic)) != 0); |
AjK | 0:db98027c0bbb | 100 | |
AjK | 0:db98027c0bbb | 101 | return q; |
AjK | 0:db98027c0bbb | 102 | } |
AjK | 0:db98027c0bbb | 103 | |
AjK | 2:8aa059e7d8b1 | 104 | GPS_VTG * |
AjK | 2:8aa059e7d8b1 | 105 | GPS::vtg(GPS_VTG *q) |
AjK | 2:8aa059e7d8b1 | 106 | { |
AjK | 3:28a1b60b0f37 | 107 | GPS_VTG a; |
AjK | 2:8aa059e7d8b1 | 108 | |
AjK | 2:8aa059e7d8b1 | 109 | if (q == NULL) q = new GPS_VTG; |
AjK | 2:8aa059e7d8b1 | 110 | |
AjK | 2:8aa059e7d8b1 | 111 | do { |
AjK | 2:8aa059e7d8b1 | 112 | memcpy(&a, &theVTG, sizeof(GPS_VTG)); |
AjK | 3:28a1b60b0f37 | 113 | memcpy(q, &theVTG, sizeof(GPS_VTG)); |
AjK | 2:8aa059e7d8b1 | 114 | } |
AjK | 3:28a1b60b0f37 | 115 | while (memcmp(&a, q, sizeof(GPS_VTG)) != 0); |
AjK | 2:8aa059e7d8b1 | 116 | |
AjK | 2:8aa059e7d8b1 | 117 | return q; |
AjK | 2:8aa059e7d8b1 | 118 | } |
AjK | 2:8aa059e7d8b1 | 119 | |
AjK | 0:db98027c0bbb | 120 | void |
AjK | 0:db98027c0bbb | 121 | GPS::ticktock(void) |
AjK | 0:db98027c0bbb | 122 | { |
AjK | 0:db98027c0bbb | 123 | // Increment the time structure by 1/100th of a second. |
AjK | 0:db98027c0bbb | 124 | ++theTime; |
AjK | 0:db98027c0bbb | 125 | |
AjK | 0:db98027c0bbb | 126 | // Test the serial queue. |
AjK | 0:db98027c0bbb | 127 | if (process_required) { |
AjK | 0:db98027c0bbb | 128 | char *s = buffer[active_buffer == 0 ? 1 : 0]; |
AjK | 0:db98027c0bbb | 129 | if (!strncmp(s, "$GPRMC", 6)) { |
AjK | 0:db98027c0bbb | 130 | theTime.nmea_rmc(s); |
AjK | 0:db98027c0bbb | 131 | cb_rmc.call(); |
AjK | 0:db98027c0bbb | 132 | if (!_ppsInUse) theTime.fractionalReset(); |
AjK | 0:db98027c0bbb | 133 | } |
AjK | 0:db98027c0bbb | 134 | else if (!strncmp(s, "$GPGGA", 6)) { |
AjK | 0:db98027c0bbb | 135 | thePlace.nmea_gga(s); |
AjK | 0:db98027c0bbb | 136 | cb_gga.call(); |
AjK | 0:db98027c0bbb | 137 | } |
AjK | 2:8aa059e7d8b1 | 138 | else if (!strncmp(s, "$GPVTG", 6)) { |
AjK | 2:8aa059e7d8b1 | 139 | theVTG.nmea_vtg(s); |
AjK | 2:8aa059e7d8b1 | 140 | cb_vtg.call(); |
AjK | 2:8aa059e7d8b1 | 141 | } |
AjK | 0:db98027c0bbb | 142 | process_required = false; |
AjK | 0:db98027c0bbb | 143 | } |
AjK | 5:7f130f85d5a4 | 144 | |
AjK | 5:7f130f85d5a4 | 145 | // If we have a valid GPS time then, once per minute, set the RTC. |
AjK | 5:7f130f85d5a4 | 146 | if (theTime.status == 'A' && theTime.second == 0 && theTime.tenths == 0 && theTime.hundreths == 0) { |
AjK | 5:7f130f85d5a4 | 147 | // set_time() is defined in rtc_time.h |
AjK | 5:7f130f85d5a4 | 148 | // http://mbed.org/projects/libraries/svn/mbed/trunk/rtc_time.h |
AjK | 5:7f130f85d5a4 | 149 | set_time(theTime.to_C_tm()); |
AjK | 5:7f130f85d5a4 | 150 | } |
AjK | 5:7f130f85d5a4 | 151 | |
AjK | 0:db98027c0bbb | 152 | } |
AjK | 0:db98027c0bbb | 153 | |
AjK | 0:db98027c0bbb | 154 | void |
AjK | 0:db98027c0bbb | 155 | GPS::pps_irq(void) |
AjK | 0:db98027c0bbb | 156 | { |
AjK | 0:db98027c0bbb | 157 | theTime.fractionalReset(); |
AjK | 0:db98027c0bbb | 158 | theTime++; // Increment the time/date by one second. |
AjK | 0:db98027c0bbb | 159 | cb_pps.call(); |
AjK | 0:db98027c0bbb | 160 | } |
AjK | 2:8aa059e7d8b1 | 161 | |
AjK | 0:db98027c0bbb | 162 | void |
AjK | 0:db98027c0bbb | 163 | GPS::rx_irq(void) |
AjK | 0:db98027c0bbb | 164 | { |
AjK | 0:db98027c0bbb | 165 | uint32_t iir __attribute__((unused)); |
AjK | 0:db98027c0bbb | 166 | char c; |
AjK | 0:db98027c0bbb | 167 | |
AjK | 0:db98027c0bbb | 168 | if (_base) { |
AjK | 0:db98027c0bbb | 169 | iir = (uint32_t)*((char *)_base + GPS_IIR); |
AjK | 0:db98027c0bbb | 170 | while((int)(*((char *)_base + GPS_LSR) & 0x1)) { |
AjK | 3:28a1b60b0f37 | 171 | c = (char)(*((char *)_base + GPS_RBR) & 0xFF); |
AjK | 3:28a1b60b0f37 | 172 | |
AjK | 3:28a1b60b0f37 | 173 | // strtok workaround. |
AjK | 3:28a1b60b0f37 | 174 | // Found that ,, together (which some NMEA sentences |
AjK | 3:28a1b60b0f37 | 175 | // contain for a null/empty field) confuses strtok() |
AjK | 3:28a1b60b0f37 | 176 | // function. Solution:- Push a "zero" into the string |
AjK | 3:28a1b60b0f37 | 177 | // for missing/empty fields. |
AjK | 3:28a1b60b0f37 | 178 | if (c == ',' && _lastByte == ',') { |
AjK | 3:28a1b60b0f37 | 179 | buffer[active_buffer][rx_buffer_in] = '0'; |
AjK | 3:28a1b60b0f37 | 180 | if (++rx_buffer_in >= GPS_BUFFER_LEN) rx_buffer_in = 0; |
AjK | 3:28a1b60b0f37 | 181 | } |
AjK | 3:28a1b60b0f37 | 182 | |
AjK | 3:28a1b60b0f37 | 183 | // Debugging/dumping data. |
AjK | 3:28a1b60b0f37 | 184 | if (_nmeaOnUart0) LPC_UART0->RBR = c; |
AjK | 3:28a1b60b0f37 | 185 | |
AjK | 3:28a1b60b0f37 | 186 | // Put the byte into the string. |
AjK | 3:28a1b60b0f37 | 187 | buffer[active_buffer][rx_buffer_in] = c; |
AjK | 3:28a1b60b0f37 | 188 | if (++rx_buffer_in >= GPS_BUFFER_LEN) rx_buffer_in = 0; |
AjK | 3:28a1b60b0f37 | 189 | |
AjK | 3:28a1b60b0f37 | 190 | // Save for next time an irq occurs. See strtok() above. |
AjK | 3:28a1b60b0f37 | 191 | _lastByte = c; |
AjK | 3:28a1b60b0f37 | 192 | |
AjK | 3:28a1b60b0f37 | 193 | // If end of NMEA sentence flag for processing. |
AjK | 0:db98027c0bbb | 194 | if (c == '\n') { |
AjK | 0:db98027c0bbb | 195 | active_buffer = active_buffer == 0 ? 1 : 0; |
AjK | 0:db98027c0bbb | 196 | process_required = true; |
AjK | 0:db98027c0bbb | 197 | rx_buffer_in = 0; |
AjK | 3:28a1b60b0f37 | 198 | } |
AjK | 0:db98027c0bbb | 199 | } |
AjK | 0:db98027c0bbb | 200 | } |
AjK | 0:db98027c0bbb | 201 | } |