diff --git a/hardware/AFSK.c b/hardware/AFSK.c index f82a81f..925cddb 100755 --- a/hardware/AFSK.c +++ b/hardware/AFSK.c @@ -61,6 +61,8 @@ void AFSK_init(Afsk *afsk) { AFSK_modem = afsk; // Set phase increment afsk->phaseInc = MARK_INC; + afsk->silentSamples = 0; + // Initialise FIFO buffers fifo_init(&afsk->delayFifo, (uint8_t *)afsk->delayBuf, sizeof(afsk->delayBuf)); fifo_init(&afsk->rxFifo, afsk->rxBuf, sizeof(afsk->rxBuf)); @@ -263,8 +265,12 @@ static bool hdlcParse(Hdlc *hdlc, bool bit, FIFOBuffer *fifo) { // If we have not yet seen a HDLC_FLAG indicating that // a transmission is actually taking place, don't bother // with anything. - if (!hdlc->receiving) + if (!hdlc->receiving) { + hdlc->dcd = false; + hdlc->dcd_count = 0; + return ret; + } // First check if what we are seeing is a stuffed bit. // Since the different HDLC control characters like @@ -312,6 +318,8 @@ static bool hdlcParse(Hdlc *hdlc, bool bit, FIFOBuffer *fifo) { } else { // If it is, abort and return false hdlc->receiving = false; + hdlc->dcd = false; + hdlc->dcd_count = 0; LED_RX_OFF(); ret = false; } @@ -324,6 +332,8 @@ static bool hdlcParse(Hdlc *hdlc, bool bit, FIFOBuffer *fifo) { } else { // If it is, well, you know by now! hdlc->receiving = false; + hdlc->dcd = false; + hdlc->dcd_count = 0; LED_RX_OFF(); ret = false; } @@ -338,7 +348,6 @@ static bool hdlcParse(Hdlc *hdlc, bool bit, FIFOBuffer *fifo) { hdlc->currentByte >>= 1; } - //digitalWrite(13, LOW); return ret; } @@ -444,6 +453,9 @@ void AFSK_adc_isr(Afsk *afsk, int8_t currentSample) { } else { afsk->currentPhase -= PHASE_INC; } + afsk->silentSamples = 0; + } else { + afsk->silentSamples++; } // We increment our phase counter @@ -513,6 +525,12 @@ void AFSK_adc_isr(Afsk *afsk, int8_t currentSample) { } } + if (afsk->silentSamples > DCD_TIMEOUT_SAMPLES) { + afsk->silentSamples = 0; + afsk->hdlc.dcd = false; + LED_RX_OFF(); + } + } diff --git a/hardware/AFSK.h b/hardware/AFSK.h index 8a0169f..512cb54 100755 --- a/hardware/AFSK.h +++ b/hardware/AFSK.h @@ -53,6 +53,7 @@ inline static uint8_t sinSample(uint16_t i) { #define PHASE_INC 1 // Nudge by an eigth of a sample each adjustment #define DCD_MIN_COUNT 6 +#define DCD_TIMEOUT_SAMPLES 96 #if BITRATE == 960 #define FILTER_CUTOFF 600 @@ -107,6 +108,8 @@ typedef struct Afsk uint16_t phaseAcc; // Phase accumulator uint16_t phaseInc; // Phase increment per sample + uint8_t silentSamples; // How many samples were completely silent + FIFOBuffer txFifo; // FIFO for transmit data uint8_t txBuf[CONFIG_AFSK_TX_BUFLEN]; // Actual data storage for said FIFO diff --git a/main.c b/main.c index 9ff480b..8582f47 100755 --- a/main.c +++ b/main.c @@ -41,7 +41,7 @@ void init(void) { sei(); AFSK_init(&modem); - ax25_init(&AX25, &modem.fd, ax25_callback); + ax25_init(&AX25, &modem, &modem.fd, ax25_callback); serial_init(&serial); stdout = &serial.uart0; diff --git a/protocol/AX25.c b/protocol/AX25.c index f291ecb..5e4b0bb 100755 --- a/protocol/AX25.c +++ b/protocol/AX25.c @@ -12,11 +12,13 @@ #define DECODE_CALL(buf, addr) for (unsigned i = 0; i < sizeof((addr)); i++) { char c = (*(buf)++ >> 1); (addr)[i] = (c == ' ') ? '\x0' : c; } #define AX25_SET_REPEATED(msg, idx, val) do { if (val) { (msg)->rpt_flags |= _BV(idx); } else { (msg)->rpt_flags &= ~_BV(idx) ; } } while(0) -void ax25_init(AX25Ctx *ctx, FILE *channel, ax25_callback_t hook) { +void ax25_init(AX25Ctx *ctx, Afsk *modem, FILE *channel, ax25_callback_t hook) { memset(ctx, 0, sizeof(*ctx)); ctx->ch = channel; + ctx->modem = modem; ctx->hook = hook; ctx->crc_in = ctx->crc_out = CRC_CCIT_INIT_VAL; + ctx->ready_for_data = true; } static void ax25_decode(AX25Ctx *ctx) { @@ -93,6 +95,7 @@ void ax25_poll(AX25Ctx *ctx) { } ctx->escape = false; } + } static void ax25_putchar(AX25Ctx *ctx, uint8_t c) @@ -103,6 +106,7 @@ static void ax25_putchar(AX25Ctx *ctx, uint8_t c) } void ax25_sendRaw(AX25Ctx *ctx, void *_buf, size_t len) { + ctx->ready_for_data = false; ctx->crc_out = CRC_CCIT_INIT_VAL; fputc(HDLC_FLAG, ctx->ch); const uint8_t *buf = (const uint8_t *)_buf; @@ -115,6 +119,7 @@ void ax25_sendRaw(AX25Ctx *ctx, void *_buf, size_t len) { fputc(HDLC_FLAG, ctx->ch); + ctx->ready_for_data = true; } #if SERIAL_PROTOCOL == PROTOCOL_SIMPLE_SERIAL diff --git a/protocol/AX25.h b/protocol/AX25.h index 938abb9..82ac210 100755 --- a/protocol/AX25.h +++ b/protocol/AX25.h @@ -4,6 +4,7 @@ #include #include #include "device.h" +#include "hardware/AFSK.h" #define AX25_MIN_FRAME_LEN 18 #ifndef CUSTOM_FRAME_SIZE @@ -30,6 +31,7 @@ struct AX25Msg; typedef struct AX25Ctx { uint8_t buf[AX25_MAX_FRAME_LEN]; + Afsk *modem; FILE *ch; size_t frame_len; uint16_t crc_in; @@ -37,6 +39,7 @@ typedef struct AX25Ctx { ax25_callback_t hook; bool sync; bool escape; + bool ready_for_data; } AX25Ctx; #if SERIAL_PROTOCOL == PROTOCOL_SIMPLE_SERIAL @@ -68,6 +71,6 @@ typedef struct AX25Ctx { void ax25_poll(AX25Ctx *ctx); void ax25_sendRaw(AX25Ctx *ctx, void *_buf, size_t len); -void ax25_init(AX25Ctx *ctx, FILE *channel, ax25_callback_t hook); +void ax25_init(AX25Ctx *ctx, Afsk *modem, FILE *channel, ax25_callback_t hook); #endif \ No newline at end of file diff --git a/protocol/KISS.c b/protocol/KISS.c index ad175c5..d188a71 100755 --- a/protocol/KISS.c +++ b/protocol/KISS.c @@ -80,8 +80,7 @@ void kiss_csma(AX25Ctx *ctx, uint8_t *buf, size_t len) { } if (FLOWCONTROL) { - while (channel->sending_data) { /* Wait */ } - + while (!ctx->ready_for_data) { /* Wait */ } fputc(FEND, &serial->uart0); fputc(CMD_READY, &serial->uart0); fputc(0x01, &serial->uart0);