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