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
Dependents: SatGPS AntiTheftGPS FLIGHT_CONTROL_AND_COMMUNICATIONS_SYSTEM GPS-Lora ... more
Diff: GPS.cpp
- Revision:
- 3:28a1b60b0f37
- Parent:
- 2:8aa059e7d8b1
- Child:
- 5:7f130f85d5a4
--- a/GPS.cpp Fri Apr 15 12:23:52 2011 +0000 +++ b/GPS.cpp Sat Apr 16 09:37:33 2011 +0000 @@ -24,6 +24,10 @@ GPS::GPS(PinName tx, PinName rx, const char *name) : Serial(tx, rx, name) { + _nmeaOnUart0 = false; + + _lastByte = 0; + switch(_uidx) { case 1: _base = LPC_UART1; break; case 2: _base = LPC_UART2; break; @@ -84,15 +88,15 @@ GPS_Geodetic * GPS::geodetic(GPS_Geodetic *q) { - GPS_Geodetic a, b; + GPS_Geodetic a; if (q == NULL) q = new GPS_Geodetic; do { memcpy(&a, &thePlace, sizeof(GPS_Geodetic)); - memcpy(&b, &thePlace, sizeof(GPS_Geodetic)); + memcpy(q, &thePlace, sizeof(GPS_Geodetic)); } - while (memcmp(&a, &b, sizeof(GPS_Geodetic)) != 0); + while (memcmp(&a, q, sizeof(GPS_Geodetic)) != 0); return q; } @@ -100,15 +104,15 @@ GPS_VTG * GPS::vtg(GPS_VTG *q) { - GPS_VTG a, b; + GPS_VTG a; if (q == NULL) q = new GPS_VTG; do { memcpy(&a, &theVTG, sizeof(GPS_VTG)); - memcpy(&b, &theVTG, sizeof(GPS_VTG)); + memcpy(q, &theVTG, sizeof(GPS_VTG)); } - while (memcmp(&a, &b, sizeof(GPS_VTG)) != 0); + while (memcmp(&a, q, sizeof(GPS_VTG)) != 0); return q; } @@ -156,15 +160,34 @@ if (_base) { iir = (uint32_t)*((char *)_base + GPS_IIR); while((int)(*((char *)_base + GPS_LSR) & 0x1)) { - c = (char)(*((char *)_base + GPS_RBR) & 0xFF); - // debugging LPC_UART0->RBR = c; - buffer[active_buffer][rx_buffer_in++] = c; - rx_buffer_in &= (GPS_BUFFER_LEN - 1); + c = (char)(*((char *)_base + GPS_RBR) & 0xFF); + + // strtok workaround. + // Found that ,, together (which some NMEA sentences + // contain for a null/empty field) confuses strtok() + // function. Solution:- Push a "zero" into the string + // for missing/empty fields. + if (c == ',' && _lastByte == ',') { + buffer[active_buffer][rx_buffer_in] = '0'; + if (++rx_buffer_in >= GPS_BUFFER_LEN) rx_buffer_in = 0; + } + + // Debugging/dumping data. + if (_nmeaOnUart0) LPC_UART0->RBR = c; + + // Put the byte into the string. + buffer[active_buffer][rx_buffer_in] = c; + if (++rx_buffer_in >= GPS_BUFFER_LEN) rx_buffer_in = 0; + + // Save for next time an irq occurs. See strtok() above. + _lastByte = c; + + // If end of NMEA sentence flag for processing. if (c == '\n') { active_buffer = active_buffer == 0 ? 1 : 0; process_required = true; rx_buffer_in = 0; - } + } } } }