Added selectable KISS/direct serial framing

This commit is contained in:
Mark Qvist 2015-05-21 10:24:17 +02:00
parent c7f6809406
commit 9fcfa4de63
8 changed files with 70 additions and 40 deletions

View file

@ -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

View file

@ -19,7 +19,6 @@
// Serial protocol settings
#define SERIAL_PROTOCOL PROTOCOL_KISS
//#define CUSTOM_FRAME_SIZE 330
// Serial settings
#define BAUD 9600

View file

@ -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

4
main.c
View file

@ -3,6 +3,7 @@
#include <avr/io.h>
#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);

View file

@ -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
}

View file

@ -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

View file

@ -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

View file

@ -6,4 +6,7 @@
#define m644p 0x03
#define REF_3V3 0x01
#define REF_5V 0x02
#define REF_5V 0x02
#define SERIAL_FRAMING_KISS 0x01
#define SERIAL_FRAMING_DIRECT 0x02