diff --git a/config.h b/config.h index 86203cf..f9ad5a9 100644 --- a/config.h +++ b/config.h @@ -1,4 +1,9 @@ #ifndef CONFIG_H #define CONFIG_H +// Choose whether to use KISS or direct +// framing for serial data +//#define SERIAL_FRAMING SERIAL_FRAMING_KISS +#define SERIAL_FRAMING SERIAL_FRAMING_DIRECT + #endif \ No newline at end of file diff --git a/device.h b/device.h index d4358e7..4ee9726 100644 --- a/device.h +++ b/device.h @@ -19,7 +19,6 @@ // Serial protocol settings #define SERIAL_PROTOCOL PROTOCOL_KISS -//#define CUSTOM_FRAME_SIZE 330 // Serial settings #define BAUD 9600 diff --git a/hardware/AFSK.h b/hardware/AFSK.h index aecce47..add4a8b 100644 --- a/hardware/AFSK.h +++ b/hardware/AFSK.h @@ -42,8 +42,8 @@ inline static uint8_t sinSample(uint16_t i) { #define CONFIG_AFSK_RX_BUFLEN 64 #define CONFIG_AFSK_TX_BUFLEN 64 #define CONFIG_AFSK_RXTIMEOUT 0 -#define CONFIG_AFSK_PREAMBLE_LEN 150UL -#define CONFIG_AFSK_TRAILER_LEN 50UL +#define CONFIG_AFSK_PREAMBLE_LEN 50UL +#define CONFIG_AFSK_TRAILER_LEN 5UL #define BIT_STUFF_LEN 5 #define SAMPLERATE 9600 diff --git a/main.c b/main.c index 0a2f111..0836c88 100644 --- a/main.c +++ b/main.c @@ -3,6 +3,7 @@ #include #include "device.h" +#include "config.h" #include "util/FIFO.h" #include "util/time.h" #include "hardware/AFSK.h" @@ -48,6 +49,9 @@ int main (void) { char sbyte = uart0_getchar_nowait(); kiss_serialCallback(sbyte); } + #if SERIAL_FRAMING == SERIAL_FRAMING_DIRECT + kiss_checkTimeout(false); + #endif } return(0); diff --git a/protocol/KISS.c b/protocol/KISS.c index 955c0ed..047cc3a 100644 --- a/protocol/KISS.c +++ b/protocol/KISS.c @@ -18,6 +18,7 @@ unsigned long custom_tail = CONFIG_AFSK_TRAILER_LEN; unsigned long slotTime = 200; uint8_t p = 255; +ticks_t timeout_ticks; void kiss_init(LLPCtx *ctx, Afsk *afsk, Serial *ser) { llpCtx = ctx; @@ -26,7 +27,7 @@ void kiss_init(LLPCtx *ctx, Afsk *afsk, Serial *ser) { } void kiss_messageCallback(LLPCtx *ctx) { - if (false) { + if (SERIAL_FRAMING == SERIAL_FRAMING_DIRECT) { for (unsigned i = 0; i < ctx->frame_len; i++) { uint8_t b = ctx->buf[i]; fputc(b, &serial->uart0); @@ -88,41 +89,57 @@ void kiss_csma(LLPCtx *ctx, uint8_t *buf, size_t len) { } -void kiss_serialCallback(uint8_t sbyte) { - if (IN_FRAME && sbyte == FEND && command == CMD_DATA) { - IN_FRAME = false; +void kiss_checkTimeout(bool force) { + if (force || (IN_FRAME && timer_clock() - timeout_ticks > ms_to_ticks(TX_MAXWAIT))) { kiss_csma(llpCtx, serialBuffer, frame_len); - } else if (sbyte == FEND) { - IN_FRAME = true; - command = CMD_UNKNOWN; + IN_FRAME = false; frame_len = 0; - } else if (IN_FRAME && frame_len < LLP_MAX_FRAME_LENGTH) { - // Have a look at the command byte first - if (frame_len == 0 && command == CMD_UNKNOWN) { - // MicroModem supports only one HDLC port, so we - // strip off the port nibble of the command byte - sbyte = sbyte & 0x0F; - command = sbyte; - } else if (command == CMD_DATA) { - if (sbyte == FESC) { - ESCAPE = true; - } else { - if (ESCAPE) { - if (sbyte == TFEND) sbyte = FEND; - if (sbyte == TFESC) sbyte = FESC; - ESCAPE = false; - } - serialBuffer[frame_len++] = sbyte; - } - } else if (command == CMD_TXDELAY) { - custom_preamble = sbyte * 10UL; - } else if (command == CMD_TXTAIL) { - custom_tail = sbyte * 10; - } else if (command == CMD_SLOTTIME) { - slotTime = sbyte * 10; - } else if (command == CMD_P) { - p = sbyte; - } - } + +} + +void kiss_serialCallback(uint8_t sbyte) { + #if SERIAL_FRAMING == SERIAL_FRAMING_DIRECT + timeout_ticks = timer_clock(); + IN_FRAME = true; + serialBuffer[frame_len++] = sbyte; + if (frame_len >= LLP_MAX_FRAME_LENGTH) kiss_checkTimeout(true); + #else + if (IN_FRAME && sbyte == FEND && command == CMD_DATA) { + IN_FRAME = false; + kiss_csma(llpCtx, serialBuffer, frame_len); + } else if (sbyte == FEND) { + IN_FRAME = true; + command = CMD_UNKNOWN; + frame_len = 0; + } else if (IN_FRAME && frame_len < LLP_MAX_FRAME_LENGTH) { + // Have a look at the command byte first + if (frame_len == 0 && command == CMD_UNKNOWN) { + // MicroModem supports only one HDLC port, so we + // strip off the port nibble of the command byte + sbyte = sbyte & 0x0F; + command = sbyte; + } else if (command == CMD_DATA) { + if (sbyte == FESC) { + ESCAPE = true; + } else { + if (ESCAPE) { + if (sbyte == TFEND) sbyte = FEND; + if (sbyte == TFESC) sbyte = FESC; + ESCAPE = false; + } + serialBuffer[frame_len++] = sbyte; + } + } else if (command == CMD_TXDELAY) { + custom_preamble = sbyte * 10UL; + } else if (command == CMD_TXTAIL) { + custom_tail = sbyte * 10; + } else if (command == CMD_SLOTTIME) { + slotTime = sbyte * 10; + } else if (command == CMD_P) { + p = sbyte; + } + + } + #endif } \ No newline at end of file diff --git a/protocol/KISS.h b/protocol/KISS.h index ef484a4..45c0c6f 100644 --- a/protocol/KISS.h +++ b/protocol/KISS.h @@ -5,6 +5,7 @@ #include "../hardware/Serial.h" #include "../util/time.h" #include "LLP.h" +#include "config.h" #define FEND 0xC0 #define FESC 0xDB @@ -25,5 +26,6 @@ void kiss_init(LLPCtx *ctx, Afsk *afsk, Serial *ser); void kiss_csma(LLPCtx *ctx, uint8_t *buf, size_t len); void kiss_messageCallback(LLPCtx *ctx); void kiss_serialCallback(uint8_t sbyte); +void kiss_checkTimeout(bool force); #endif \ No newline at end of file diff --git a/protocol/LLP.h b/protocol/LLP.h index d192d44..a695a42 100644 --- a/protocol/LLP.h +++ b/protocol/LLP.h @@ -9,7 +9,7 @@ #define LLP_INTERLEAVE_SIZE 12 #define LLP_MIN_FRAME_LENGTH LLP_INTERLEAVE_SIZE -#define LLP_MAX_FRAME_LENGTH 50 * LLP_INTERLEAVE_SIZE +#define LLP_MAX_FRAME_LENGTH 65 * LLP_INTERLEAVE_SIZE #define LLP_HEADER_SIZE 10 #define LLP_CHECKSUM_SIZE 2 #define LLP_MAX_DATA_SIZE LLP_MAX_FRAME_LENGTH - LLP_HEADER_SIZE - LLP_CHECKSUM_SIZE // TODO: this is not right diff --git a/util/constants.h b/util/constants.h index 6a16a68..2e8b866 100644 --- a/util/constants.h +++ b/util/constants.h @@ -6,4 +6,7 @@ #define m644p 0x03 #define REF_3V3 0x01 -#define REF_5V 0x02 \ No newline at end of file +#define REF_5V 0x02 + +#define SERIAL_FRAMING_KISS 0x01 +#define SERIAL_FRAMING_DIRECT 0x02 \ No newline at end of file