diff --git a/config.h b/config.h index f9ad5a9..8f3284e 100644 --- a/config.h +++ b/config.h @@ -4,6 +4,7 @@ // Choose whether to use KISS or direct // framing for serial data //#define SERIAL_FRAMING SERIAL_FRAMING_KISS -#define SERIAL_FRAMING SERIAL_FRAMING_DIRECT +#define SERIAL_FRAMING SERIAL_FRAMING_NMEA +#define NMEA_SKIP_SENTENCES 5 #endif \ No newline at end of file diff --git a/main.c b/main.c index 0836c88..41b08ae 100644 --- a/main.c +++ b/main.c @@ -49,7 +49,7 @@ int main (void) { char sbyte = uart0_getchar_nowait(); kiss_serialCallback(sbyte); } - #if SERIAL_FRAMING == SERIAL_FRAMING_DIRECT + #if (SERIAL_FRAMING == SERIAL_FRAMING_DIRECT) || (SERIAL_FRAMING == SERIAL_FRAMING_NMEA) kiss_checkTimeout(false); #endif } diff --git a/protocol/KISS.c b/protocol/KISS.c index 39c41d5..02abf20 100644 --- a/protocol/KISS.c +++ b/protocol/KISS.c @@ -20,6 +20,8 @@ unsigned long slotTime = 200; uint8_t p = 255; ticks_t timeout_ticks; +int skip_sentences = 0; + void kiss_init(LLPCtx *ctx, Afsk *afsk, Serial *ser) { llpCtx = ctx; serial = ser; @@ -27,7 +29,7 @@ void kiss_init(LLPCtx *ctx, Afsk *afsk, Serial *ser) { } void kiss_messageCallback(LLPCtx *ctx) { - if (SERIAL_FRAMING == SERIAL_FRAMING_DIRECT) { + if (SERIAL_FRAMING == SERIAL_FRAMING_DIRECT || SERIAL_FRAMING == SERIAL_FRAMING_NMEA) { for (unsigned i = 0; i < ctx->frame_len; i++) { uint8_t b = ctx->buf[i]; fputc(b, &serial->uart0); @@ -104,6 +106,48 @@ void kiss_serialCallback(uint8_t sbyte) { IN_FRAME = true; serialBuffer[frame_len++] = sbyte; if (frame_len >= LLP_MAX_DATA_SIZE) kiss_checkTimeout(true); + #elif SERIAL_FRAMING == SERIAL_FRAMING_NMEA + timeout_ticks = timer_clock(); + bool NMEA_ok = true; + if (!IN_FRAME) { + // This is the start of a frame, so + // we init filter data ($GPRMC) + strncpy((char*)serialBuffer, "$GPRMC", 6); + } + + serialBuffer[frame_len++] = sbyte; + + if (serialBuffer[0] != '$') NMEA_ok = false; + if (serialBuffer[1] != 'G') NMEA_ok = false; + if (serialBuffer[2] != 'P') NMEA_ok = false; + if (serialBuffer[3] != 'R') NMEA_ok = false; + if (serialBuffer[4] != 'M') NMEA_ok = false; + if (serialBuffer[5] != 'C') NMEA_ok = false; + + if (NMEA_ok) { + // Continue + IN_FRAME = true; + } else { + // Reset + IN_FRAME = false; + frame_len = 0; + } + + if (frame_len > 6 && serialBuffer[frame_len-3] == '*') { + if (skip_sentences >= NMEA_SKIP_SENTENCES) { + serialBuffer[frame_len++] = '\n'; + serialBuffer[frame_len++] = '\r'; + kiss_checkTimeout(true); + skip_sentences = 0; + } else { + skip_sentences++; + IN_FRAME = false; + frame_len = 0; + } + } + + if (frame_len >= LLP_MAX_DATA_SIZE) kiss_checkTimeout(true); + #else if (IN_FRAME && sbyte == FEND && command == CMD_DATA) { IN_FRAME = false; diff --git a/util/constants.h b/util/constants.h index 2e8b866..1039191 100644 --- a/util/constants.h +++ b/util/constants.h @@ -9,4 +9,5 @@ #define REF_5V 0x02 #define SERIAL_FRAMING_KISS 0x01 -#define SERIAL_FRAMING_DIRECT 0x02 \ No newline at end of file +#define SERIAL_FRAMING_DIRECT 0x02 +#define SERIAL_FRAMING_NMEA 0x03 \ No newline at end of file