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@0:db98027c0bbb, 2010-11-15 (annotated)
- Committer:
- AjK
- Date:
- Mon Nov 15 20:11:16 2010 +0000
- Revision:
- 0:db98027c0bbb
- Child:
- 2:8aa059e7d8b1
1.8
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 | 0:db98027c0bbb | 27 | switch(_uidx) { |
AjK | 0:db98027c0bbb | 28 | case 1: _base = LPC_UART1; break; |
AjK | 0:db98027c0bbb | 29 | case 2: _base = LPC_UART2; break; |
AjK | 0:db98027c0bbb | 30 | case 3: _base = LPC_UART3; break; |
AjK | 0:db98027c0bbb | 31 | default : _base = NULL; break; |
AjK | 0:db98027c0bbb | 32 | } |
AjK | 0:db98027c0bbb | 33 | |
AjK | 0:db98027c0bbb | 34 | _pps = NULL; |
AjK | 0:db98027c0bbb | 35 | _ppsInUse = false; |
AjK | 0:db98027c0bbb | 36 | |
AjK | 0:db98027c0bbb | 37 | if (_base != NULL) attach(this, &GPS::rx_irq); |
AjK | 0:db98027c0bbb | 38 | |
AjK | 0:db98027c0bbb | 39 | _second100 = new Ticker; |
AjK | 0:db98027c0bbb | 40 | _second100->attach_us(this, &GPS::ticktock, GPS_TICKTOCK); |
AjK | 0:db98027c0bbb | 41 | } |
AjK | 0:db98027c0bbb | 42 | |
AjK | 0:db98027c0bbb | 43 | void |
AjK | 0:db98027c0bbb | 44 | GPS::ppsAttach(PinName irq, ppsEdgeType type) |
AjK | 0:db98027c0bbb | 45 | { |
AjK | 0:db98027c0bbb | 46 | if (_pps != NULL) delete(_pps); |
AjK | 0:db98027c0bbb | 47 | _pps = new InterruptIn(irq); |
AjK | 0:db98027c0bbb | 48 | if (type == ppsRise) _pps->rise(this, &GPS::pps_irq); |
AjK | 0:db98027c0bbb | 49 | else _pps->fall(this, &GPS::pps_irq); |
AjK | 0:db98027c0bbb | 50 | _ppsInUse = true; |
AjK | 0:db98027c0bbb | 51 | } |
AjK | 0:db98027c0bbb | 52 | |
AjK | 0:db98027c0bbb | 53 | void |
AjK | 0:db98027c0bbb | 54 | GPS::ppsUnattach(void) |
AjK | 0:db98027c0bbb | 55 | { |
AjK | 0:db98027c0bbb | 56 | if (_pps != NULL) delete(_pps); |
AjK | 0:db98027c0bbb | 57 | _ppsInUse = false; |
AjK | 0:db98027c0bbb | 58 | } |
AjK | 0:db98027c0bbb | 59 | |
AjK | 0:db98027c0bbb | 60 | double |
AjK | 0:db98027c0bbb | 61 | GPS::latitude(void) |
AjK | 0:db98027c0bbb | 62 | { |
AjK | 0:db98027c0bbb | 63 | double a, b; |
AjK | 0:db98027c0bbb | 64 | do { a = thePlace.lat; b = thePlace.lat; } while (a != b); |
AjK | 0:db98027c0bbb | 65 | return a; |
AjK | 0:db98027c0bbb | 66 | } |
AjK | 0:db98027c0bbb | 67 | |
AjK | 0:db98027c0bbb | 68 | double |
AjK | 0:db98027c0bbb | 69 | GPS::longitude(void) |
AjK | 0:db98027c0bbb | 70 | { |
AjK | 0:db98027c0bbb | 71 | double a, b; |
AjK | 0:db98027c0bbb | 72 | do { a = thePlace.lon; b = thePlace.lon; } while (a != b); |
AjK | 0:db98027c0bbb | 73 | return a; |
AjK | 0:db98027c0bbb | 74 | } |
AjK | 0:db98027c0bbb | 75 | |
AjK | 0:db98027c0bbb | 76 | double |
AjK | 0:db98027c0bbb | 77 | GPS::altitude(void) |
AjK | 0:db98027c0bbb | 78 | { |
AjK | 0:db98027c0bbb | 79 | double a, b; |
AjK | 0:db98027c0bbb | 80 | do { a = thePlace.alt; b = thePlace.alt; } while (a != b); |
AjK | 0:db98027c0bbb | 81 | return a; |
AjK | 0:db98027c0bbb | 82 | } |
AjK | 0:db98027c0bbb | 83 | |
AjK | 0:db98027c0bbb | 84 | GPS_Geodetic * |
AjK | 0:db98027c0bbb | 85 | GPS::geodetic(GPS_Geodetic *q) |
AjK | 0:db98027c0bbb | 86 | { |
AjK | 0:db98027c0bbb | 87 | GPS_Geodetic a, b; |
AjK | 0:db98027c0bbb | 88 | |
AjK | 0:db98027c0bbb | 89 | if (q == NULL) q = new GPS_Geodetic; |
AjK | 0:db98027c0bbb | 90 | |
AjK | 0:db98027c0bbb | 91 | do { |
AjK | 0:db98027c0bbb | 92 | memcpy(&a, &thePlace, sizeof(GPS_Geodetic)); |
AjK | 0:db98027c0bbb | 93 | memcpy(&b, &thePlace, sizeof(GPS_Geodetic)); |
AjK | 0:db98027c0bbb | 94 | } |
AjK | 0:db98027c0bbb | 95 | while (memcmp(&a, &b, sizeof(GPS_Geodetic)) != 0); |
AjK | 0:db98027c0bbb | 96 | |
AjK | 0:db98027c0bbb | 97 | return q; |
AjK | 0:db98027c0bbb | 98 | } |
AjK | 0:db98027c0bbb | 99 | |
AjK | 0:db98027c0bbb | 100 | void |
AjK | 0:db98027c0bbb | 101 | GPS::ticktock(void) |
AjK | 0:db98027c0bbb | 102 | { |
AjK | 0:db98027c0bbb | 103 | // Increment the time structure by 1/100th of a second. |
AjK | 0:db98027c0bbb | 104 | ++theTime; |
AjK | 0:db98027c0bbb | 105 | |
AjK | 0:db98027c0bbb | 106 | // Test the serial queue. |
AjK | 0:db98027c0bbb | 107 | if (process_required) { |
AjK | 0:db98027c0bbb | 108 | char *s = buffer[active_buffer == 0 ? 1 : 0]; |
AjK | 0:db98027c0bbb | 109 | if (!strncmp(s, "$GPRMC", 6)) { |
AjK | 0:db98027c0bbb | 110 | theTime.nmea_rmc(s); |
AjK | 0:db98027c0bbb | 111 | cb_rmc.call(); |
AjK | 0:db98027c0bbb | 112 | if (!_ppsInUse) theTime.fractionalReset(); |
AjK | 0:db98027c0bbb | 113 | } |
AjK | 0:db98027c0bbb | 114 | else if (!strncmp(s, "$GPGGA", 6)) { |
AjK | 0:db98027c0bbb | 115 | thePlace.nmea_gga(s); |
AjK | 0:db98027c0bbb | 116 | cb_gga.call(); |
AjK | 0:db98027c0bbb | 117 | } |
AjK | 0:db98027c0bbb | 118 | process_required = false; |
AjK | 0:db98027c0bbb | 119 | } |
AjK | 0:db98027c0bbb | 120 | } |
AjK | 0:db98027c0bbb | 121 | |
AjK | 0:db98027c0bbb | 122 | void |
AjK | 0:db98027c0bbb | 123 | GPS::pps_irq(void) |
AjK | 0:db98027c0bbb | 124 | { |
AjK | 0:db98027c0bbb | 125 | theTime.fractionalReset(); |
AjK | 0:db98027c0bbb | 126 | theTime++; // Increment the time/date by one second. |
AjK | 0:db98027c0bbb | 127 | cb_pps.call(); |
AjK | 0:db98027c0bbb | 128 | } |
AjK | 0:db98027c0bbb | 129 | |
AjK | 0:db98027c0bbb | 130 | void |
AjK | 0:db98027c0bbb | 131 | GPS::rx_irq(void) |
AjK | 0:db98027c0bbb | 132 | { |
AjK | 0:db98027c0bbb | 133 | uint32_t iir __attribute__((unused)); |
AjK | 0:db98027c0bbb | 134 | char c; |
AjK | 0:db98027c0bbb | 135 | |
AjK | 0:db98027c0bbb | 136 | if (_base) { |
AjK | 0:db98027c0bbb | 137 | iir = (uint32_t)*((char *)_base + GPS_IIR); |
AjK | 0:db98027c0bbb | 138 | while((int)(*((char *)_base + GPS_LSR) & 0x1)) { |
AjK | 0:db98027c0bbb | 139 | c = (char)(*((char *)_base + GPS_RBR) & 0xFF); |
AjK | 0:db98027c0bbb | 140 | buffer[active_buffer][rx_buffer_in++] = c; |
AjK | 0:db98027c0bbb | 141 | rx_buffer_in &= (GPS_BUFFER_LEN - 1); |
AjK | 0:db98027c0bbb | 142 | if (c == '\n') { |
AjK | 0:db98027c0bbb | 143 | active_buffer = active_buffer == 0 ? 1 : 0; |
AjK | 0:db98027c0bbb | 144 | process_required = true; |
AjK | 0:db98027c0bbb | 145 | rx_buffer_in = 0; |
AjK | 0:db98027c0bbb | 146 | } |
AjK | 0:db98027c0bbb | 147 | } |
AjK | 0:db98027c0bbb | 148 | } |
AjK | 0:db98027c0bbb | 149 | } |