Add some documentation.
[bertos.git] / bertos / net / afsk.c
index 6fea471bd69f971b10a9ad63ddfa63d1906b86b3..c30cb22502457899fad5cb5c17cd80bda783ac94 100644 (file)
@@ -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)
@@ -254,7 +254,9 @@ void afsk_adc_isr(Afsk *af, int8_t curr_sample)
                 * Determine bit value by reading the last 3 sampled bits.
                 * If the number of ones is two or greater, the bit value is a 1,
                 * otherwise is a 0.
+                * This algorithm presumes that there are 8 samples per bit.
                 */
+               STATIC_ASSERT(SAMPLEPERBIT == 8);
                uint8_t bits = af->sampled_bits & 0x07;
                if (bits == 0x07 // 111, 3 bits set to 1
                 || bits == 0x06 // 110, 2 bits
@@ -267,12 +269,11 @@ 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);
        }
 
 
        AFSK_STROBE_OFF();
-       AFSK_ADC_IRQ_END();
 }
 
 static void afsk_txStart(Afsk *af)
@@ -284,7 +285,7 @@ static void afsk_txStart(Afsk *af)
                af->stuff_cnt = 0;
                af->sending = true;
                af->preamble_len = DIV_ROUND(CONFIG_AFSK_PREAMBLE_LEN * BITRATE, 8000);
-               AFSK_DAC_IRQ_START();
+               AFSK_DAC_IRQ_START(af->dac_ch);
        }
        ATOMIC(af->trailer_len  = DIV_ROUND(CONFIG_AFSK_TRAILER_LEN  * BITRATE, 8000));
 }
@@ -303,9 +304,8 @@ void afsk_dac_isr(Afsk *af)
                        /* We have just finished transimitting a char, get a new one. */
                        if (fifo_isempty(&af->tx_fifo) && af->trailer_len == 0)
                        {
-                               AFSK_DAC_IRQ_STOP();
+                               AFSK_DAC_IRQ_STOP(af->dac_ch);
                                af->sending = false;
-                               AFSK_DAC_IRQ_END();
                                return;
                        }
                        else
@@ -343,9 +343,8 @@ void afsk_dac_isr(Afsk *af)
                                {
                                        if (fifo_isempty(&af->tx_fifo))
                                        {
-                                               AFSK_DAC_IRQ_STOP();
+                                               AFSK_DAC_IRQ_STOP(af->dac_ch);
                                                af->sending = false;
-                                               AFSK_DAC_IRQ_END();
                                                return;
                                        }
                                        else
@@ -403,9 +402,8 @@ void afsk_dac_isr(Afsk *af)
        af->phase_acc += af->phase_inc;
        af->phase_acc %= SIN_LEN;
 
-       AFSK_SET_DAC(sin_sample(af->phase_acc));
+       AFSK_DAC_SET(af->dac_ch, sin_sample(af->phase_acc));
        af->sample_count--;
-       AFSK_DAC_IRQ_END();
 }
 
 
@@ -465,12 +463,14 @@ static int afsk_flush(KFile *fd)
 }
 
 
-void afsk_init(Afsk *af)
+void afsk_init(Afsk *af, int adc_ch, int dac_ch)
 {
        #if CONFIG_AFSK_RXTIMEOUT != -1
        MOD_CHECK(timer);
        #endif
        memset(af, 0, sizeof(*af));
+       af->adc_ch = adc_ch;
+       af->dac_ch = dac_ch;
 
        fifo_init(&af->delay_fifo, (uint8_t *)af->delay_buf, sizeof(af->delay_buf));
        fifo_init(&af->rx_fifo, af->rx_buf, sizeof(af->rx_buf));
@@ -481,7 +481,8 @@ void afsk_init(Afsk *af)
 
        fifo_init(&af->tx_fifo, af->tx_buf, sizeof(af->tx_buf));
 
-       AFSK_ADC_INIT();
+       AFSK_ADC_INIT(adc_ch, af);
+       AFSK_DAC_INIT(dac_ch, af);
        AFSK_STROBE_INIT();
        kprintf("MARK_INC %d, SPACE_INC %d\n", MARK_INC, SPACE_INC);