From e00ec3c717447fcfedf6fa23c8feaf01b1b654e8 Mon Sep 17 00:00:00 2001 From: batt Date: Sat, 10 Oct 2009 10:54:40 +0000 Subject: [PATCH] Factor out hdlc context, add some documentation. git-svn-id: https://src.develer.com/svnoss/bertos/trunk@3061 38d2e660-2303-0410-9eaa-f027e97ec537 --- bertos/net/afsk.c | 60 ++++++++++++++++++++++---------------------- bertos/net/afsk.h | 64 +++++++++++++++++++++++++++++++++++++++++------ 2 files changed, 87 insertions(+), 37 deletions(-) diff --git a/bertos/net/afsk.c b/bertos/net/afsk.c index a7273bdf..4164fccd 100644 --- a/bertos/net/afsk.c +++ b/bertos/net/afsk.c @@ -109,67 +109,67 @@ INLINE uint8_t sin_sample(uint16_t idx) #define EDGE_FOUND(bitline) BIT_DIFFER((bitline), (bitline) >> 1) -static void hdlc_parse(Afsk *af, bool bit) +static void hdlc_parse(Hdlc *hdlc, bool bit, FIFOBuffer *fifo) { - af->hdlc_demod_bits <<= 1; - af->hdlc_demod_bits |= bit ? 1 : 0; + hdlc->demod_bits <<= 1; + hdlc->demod_bits |= bit ? 1 : 0; /* HDLC Flag */ - if (af->hdlc_demod_bits == HDLC_FLAG) + if (hdlc->demod_bits == HDLC_FLAG) { - if (!fifo_isfull(&af->rx_fifo)) + if (!fifo_isfull(fifo)) { - fifo_push(&af->rx_fifo, HDLC_FLAG); - af->hdlc_rxstart = true; + fifo_push(fifo, HDLC_FLAG); + hdlc->rxstart = true; } else - af->hdlc_rxstart = false; + hdlc->rxstart = false; - af->hdlc_currchar = 0; - af->hdlc_bit_idx = 0; + hdlc->currchar = 0; + hdlc->bit_idx = 0; return; } /* Reset */ - if ((af->hdlc_demod_bits & HDLC_RESET) == HDLC_RESET) + if ((hdlc->demod_bits & HDLC_RESET) == HDLC_RESET) { - af->hdlc_rxstart = false; + hdlc->rxstart = false; return; } - if (!af->hdlc_rxstart) + if (!hdlc->rxstart) return; /* Stuffed bit */ - if ((af->hdlc_demod_bits & 0x3f) == 0x3e) + if ((hdlc->demod_bits & 0x3f) == 0x3e) return; - if (af->hdlc_demod_bits & 0x01) - af->hdlc_currchar |= 0x80; + if (hdlc->demod_bits & 0x01) + hdlc->currchar |= 0x80; - if (++af->hdlc_bit_idx >= 8) + if (++hdlc->bit_idx >= 8) { - if ((af->hdlc_currchar == HDLC_FLAG - || af->hdlc_currchar == HDLC_RESET - || af->hdlc_currchar == AX25_ESC)) + if ((hdlc->currchar == HDLC_FLAG + || hdlc->currchar == HDLC_RESET + || hdlc->currchar == AX25_ESC)) { - if (!fifo_isfull(&af->rx_fifo)) - fifo_push(&af->rx_fifo, AX25_ESC); + if (!fifo_isfull(fifo)) + fifo_push(fifo, AX25_ESC); else - af->hdlc_rxstart = false; + hdlc->rxstart = false; } - if (!fifo_isfull(&af->rx_fifo)) - fifo_push(&af->rx_fifo, af->hdlc_currchar); + if (!fifo_isfull(fifo)) + fifo_push(fifo, hdlc->currchar); else - af->hdlc_rxstart = false; + hdlc->rxstart = false; - af->hdlc_currchar = 0; - af->hdlc_bit_idx = 0; + hdlc->currchar = 0; + hdlc->bit_idx = 0; return; } - af->hdlc_currchar >>= 1; + hdlc->currchar >>= 1; } void afsk_adc_isr(Afsk *af, int8_t curr_sample) @@ -267,7 +267,7 @@ void afsk_adc_isr(Afsk *af, int8_t curr_sample) * NRZI coding: if 2 consecutive bits have the same value * a 1 is received, otherwise it's a 0. */ - hdlc_parse(af, !EDGE_FOUND(af->found_bits)); + hdlc_parse(&af->hdlc, !EDGE_FOUND(af->found_bits), &af->rx_fifo); } diff --git a/bertos/net/afsk.h b/bertos/net/afsk.h index 4ab20c50..902c5b94 100644 --- a/bertos/net/afsk.h +++ b/bertos/net/afsk.h @@ -58,12 +58,31 @@ #define SAMPLEPERBIT (SAMPLERATE / BITRATE) +/** + * HDLC (High-Level Data Link Control) context. + * Maybe to be moved in a separate HDLC module one day. + */ +typedef struct Hdlc +{ + uint8_t demod_bits; ///< Bitstream from the demodulator. + uint8_t bit_idx; ///< Current received bit. + uint8_t currchar; ///< Current received character. + bool rxstart; ///< True if an HDLC_FLAG char has been found in the bitstream. +} Hdlc; + +/** + * AFSK1200 modem context. + */ typedef struct Afsk { + /** Base "class" */ KFile fd; + /** ADC channel to be used by the demodulator */ int adc_ch; + + /** DAC channel to be used by the modulator */ int dac_ch; /** Current sample of bit for output data. */ @@ -98,29 +117,60 @@ typedef struct Afsk */ int8_t delay_buf[SAMPLEPERBIT / 2 + 1]; + /** FIFO for received data */ FIFOBuffer rx_fifo; + + /** FIFO rx buffer */ uint8_t rx_buf[CONFIG_AFSK_RX_BUFLEN]; + /** FIFO for transmitted data */ FIFOBuffer tx_fifo; + + /** FIFO tx buffer */ uint8_t tx_buf[CONFIG_AFSK_TX_BUFLEN]; + /** IIR filter X cells, used to filter sampled data by the demodulator */ int16_t iir_x[2]; + + /** IIR filter Y cells, used to filter sampled data by the demodulator */ int16_t iir_y[2]; + /** + * Bits sampled by the demodulator are here. + * Since ADC samplerate is higher than the bitrate, the bits here are + * SAMPLEPERBIT times the bitrate. + */ uint8_t sampled_bits; - uint8_t found_bits; + + /** + * Current phase, needed to know when the bitstream at ADC speed + * should be sampled. + */ int8_t curr_phase; - /* True while modem sends data */ - volatile bool sending; + /** Bits found by the demodulator at the correct bitrate speed. */ + uint8_t found_bits; + /** True while modem sends data */ + volatile bool sending; - bool hdlc_rxstart; - uint8_t hdlc_currchar; - uint8_t hdlc_bit_idx; - uint8_t hdlc_demod_bits; + /** Hdlc context */ + Hdlc hdlc; + /** + * Preamble length. + * When the AFSK modem wants to send data, before sending the actual data, + * shifts out preamble_len HDLC_FLAG characters. + * This helps to synchronize the demodulator filters on the receiver side. + */ uint16_t preamble_len; + + /** + * Preamble length. + * After sending the actual data, the AFSK shifts out + * trailer_len HDLC_FLAG characters. + * This helps to synchronize the demodulator filters on the receiver side. + */ uint16_t trailer_len; } Afsk; -- 2.25.1