Fixed compiler errors/warnings (declaration of _uidx, scope of index variables i)
Dependents: GPSDevice LogData_UM6-to-SDcard UM6withGPS mbed-cansat-test-GPS ... more
Fork of MODGPS by
GPS.cpp@3:28a1b60b0f37, 2011-04-16 (annotated)
- Committer:
- AjK
- Date:
- Sat Apr 16 09:37:33 2011 +0000
- Revision:
- 3:28a1b60b0f37
- Parent:
- 2:8aa059e7d8b1
- Child:
- 5:7f130f85d5a4
1.13 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 | 0:db98027c0bbb | 144 | } |
AjK | 0:db98027c0bbb | 145 | |
AjK | 0:db98027c0bbb | 146 | void |
AjK | 0:db98027c0bbb | 147 | GPS::pps_irq(void) |
AjK | 0:db98027c0bbb | 148 | { |
AjK | 0:db98027c0bbb | 149 | theTime.fractionalReset(); |
AjK | 0:db98027c0bbb | 150 | theTime++; // Increment the time/date by one second. |
AjK | 0:db98027c0bbb | 151 | cb_pps.call(); |
AjK | 0:db98027c0bbb | 152 | } |
AjK | 2:8aa059e7d8b1 | 153 | |
AjK | 0:db98027c0bbb | 154 | void |
AjK | 0:db98027c0bbb | 155 | GPS::rx_irq(void) |
AjK | 0:db98027c0bbb | 156 | { |
AjK | 0:db98027c0bbb | 157 | uint32_t iir __attribute__((unused)); |
AjK | 0:db98027c0bbb | 158 | char c; |
AjK | 0:db98027c0bbb | 159 | |
AjK | 0:db98027c0bbb | 160 | if (_base) { |
AjK | 0:db98027c0bbb | 161 | iir = (uint32_t)*((char *)_base + GPS_IIR); |
AjK | 0:db98027c0bbb | 162 | while((int)(*((char *)_base + GPS_LSR) & 0x1)) { |
AjK | 3:28a1b60b0f37 | 163 | c = (char)(*((char *)_base + GPS_RBR) & 0xFF); |
AjK | 3:28a1b60b0f37 | 164 | |
AjK | 3:28a1b60b0f37 | 165 | // strtok workaround. |
AjK | 3:28a1b60b0f37 | 166 | // Found that ,, together (which some NMEA sentences |
AjK | 3:28a1b60b0f37 | 167 | // contain for a null/empty field) confuses strtok() |
AjK | 3:28a1b60b0f37 | 168 | // function. Solution:- Push a "zero" into the string |
AjK | 3:28a1b60b0f37 | 169 | // for missing/empty fields. |
AjK | 3:28a1b60b0f37 | 170 | if (c == ',' && _lastByte == ',') { |
AjK | 3:28a1b60b0f37 | 171 | buffer[active_buffer][rx_buffer_in] = '0'; |
AjK | 3:28a1b60b0f37 | 172 | if (++rx_buffer_in >= GPS_BUFFER_LEN) rx_buffer_in = 0; |
AjK | 3:28a1b60b0f37 | 173 | } |
AjK | 3:28a1b60b0f37 | 174 | |
AjK | 3:28a1b60b0f37 | 175 | // Debugging/dumping data. |
AjK | 3:28a1b60b0f37 | 176 | if (_nmeaOnUart0) LPC_UART0->RBR = c; |
AjK | 3:28a1b60b0f37 | 177 | |
AjK | 3:28a1b60b0f37 | 178 | // Put the byte into the string. |
AjK | 3:28a1b60b0f37 | 179 | buffer[active_buffer][rx_buffer_in] = c; |
AjK | 3:28a1b60b0f37 | 180 | if (++rx_buffer_in >= GPS_BUFFER_LEN) rx_buffer_in = 0; |
AjK | 3:28a1b60b0f37 | 181 | |
AjK | 3:28a1b60b0f37 | 182 | // Save for next time an irq occurs. See strtok() above. |
AjK | 3:28a1b60b0f37 | 183 | _lastByte = c; |
AjK | 3:28a1b60b0f37 | 184 | |
AjK | 3:28a1b60b0f37 | 185 | // If end of NMEA sentence flag for processing. |
AjK | 0:db98027c0bbb | 186 | if (c == '\n') { |
AjK | 0:db98027c0bbb | 187 | active_buffer = active_buffer == 0 ? 1 : 0; |
AjK | 0:db98027c0bbb | 188 | process_required = true; |
AjK | 0:db98027c0bbb | 189 | rx_buffer_in = 0; |
AjK | 3:28a1b60b0f37 | 190 | } |
AjK | 0:db98027c0bbb | 191 | } |
AjK | 0:db98027c0bbb | 192 | } |
AjK | 0:db98027c0bbb | 193 | } |