Clean up. Use log module. Add cfg file and move some setting to cfg.
[bertos.git] / bertos / net / afsk.c
1 /**
2  * \file
3  * <!--
4  * This file is part of BeRTOS.
5  *
6  * Bertos is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 2 of the License, or
9  * (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, write to the Free Software
18  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
19  *
20  * As a special exception, you may use this file as part of a free software
21  * library without restriction.  Specifically, if other files instantiate
22  * templates or use macros or inline functions from this file, or you compile
23  * this file and link it with other files to produce an executable, this
24  * file does not by itself cause the resulting executable to be covered by
25  * the GNU General Public License.  This exception does not however
26  * invalidate any other reasons why the executable file might be covered by
27  * the GNU General Public License.
28  *
29  * Copyright 2008 Develer S.r.l. (http://www.develer.com/)
30  *
31  * -->
32  *
33  * \brief AFSK1200 modem.
34  *
35  * \author Francesco Sacchi <asterix@develer.com>
36  */
37
38 #include "afsk.h"
39 #include <net/ax25.h>
40
41 #include "cfg/cfg_afsk.h"
42 #include "hw/hw_afsk.h"
43
44 #include <drv/timer.h>
45
46 #include <cfg/module.h>
47
48 #define LOG_LEVEL   AFSK_LOG_LEVEL
49 #define LOG_FORMAT  AFSK_LOG_FORMAT
50 #include <cfg/log.h>
51
52 #include <cpu/power.h>
53 #include <cpu/pgm.h>
54 #include <struct/fifobuf.h>
55
56 #include <string.h> /* memset */
57
58 #define PHASE_BIT    8
59 #define PHASE_INC    1
60
61 #define PHASE_MAX    (SAMPLEPERBIT * PHASE_BIT)
62 #define PHASE_THRES  (PHASE_MAX / 2) // - PHASE_BIT / 2)
63
64 // Modulator constants
65 #define MARK_FREQ  1200
66 #define MARK_INC   (uint16_t)(DIV_ROUND(SIN_LEN * (uint32_t)MARK_FREQ, CONFIG_AFSK_DAC_SAMPLERATE))
67
68 #define SPACE_FREQ 2200
69 #define SPACE_INC  (uint16_t)(DIV_ROUND(SIN_LEN * (uint32_t)SPACE_FREQ, CONFIG_AFSK_DAC_SAMPLERATE))
70
71 //Ensure sample rate is a multiple of bit rate
72 STATIC_ASSERT(!(CONFIG_AFSK_DAC_SAMPLERATE % BITRATE));
73
74 #define DAC_SAMPLEPERBIT (CONFIG_AFSK_DAC_SAMPLERATE / BITRATE)
75
76 /**
77  * Sine table for the first quarter of wave.
78  * The rest of the wave is computed from this first quarter.
79  * This table is used to generate the modulated data.
80  */
81 static const uint8_t PROGMEM sin_table[] =
82 {
83         128, 129, 131, 132, 134, 135, 137, 138, 140, 142, 143, 145, 146, 148, 149, 151,
84         152, 154, 155, 157, 158, 160, 162, 163, 165, 166, 167, 169, 170, 172, 173, 175,
85         176, 178, 179, 181, 182, 183, 185, 186, 188, 189, 190, 192, 193, 194, 196, 197,
86         198, 200, 201, 202, 203, 205, 206, 207, 208, 210, 211, 212, 213, 214, 215, 217,
87         218, 219, 220, 221, 222, 223, 224, 225, 226, 227, 228, 229, 230, 231, 232, 233,
88         234, 234, 235, 236, 237, 238, 238, 239, 240, 241, 241, 242, 243, 243, 244, 245,
89         245, 246, 246, 247, 248, 248, 249, 249, 250, 250, 250, 251, 251, 252, 252, 252,
90         253, 253, 253, 253, 254, 254, 254, 254, 254, 255, 255, 255, 255, 255, 255, 255,
91 };
92
93 #define SIN_LEN 512 ///< Full wave length
94
95 STATIC_ASSERT(sizeof(sin_table) == SIN_LEN / 4);
96
97
98 /**
99  * Given the index, this function computes the correct sine sample
100  * based only on the first quarter of wave.
101  */
102 INLINE uint8_t sin_sample(uint16_t idx)
103 {
104         ASSERT(idx < SIN_LEN);
105         uint16_t new_idx = idx % (SIN_LEN / 2);
106         new_idx = (new_idx >= (SIN_LEN / 4)) ? (SIN_LEN / 2 - new_idx - 1) : new_idx;
107
108         #if CPU_HARVARD
109                 uint8_t data = pgm_read_char(&sin_table[new_idx]);
110         #else
111                 uint8_t data = sin_table[new_idx];
112         #endif
113
114         return (idx >= (SIN_LEN / 2)) ? (255 - data) : data;
115 }
116
117
118 #define BIT_DIFFER(bitline1, bitline2) (((bitline1) ^ (bitline2)) & 0x01)
119 #define EDGE_FOUND(bitline)            BIT_DIFFER((bitline), (bitline) >> 1)
120
121 /**
122  * High-Level Data Link Control parsing function.
123  * Parse bitstream in order to find characters.
124  *
125  * \param hdlc HDLC context.
126  * \param bit  current bit to be parsed.
127  * \param fifo FIFO buffer used to push characters.
128  *
129  * \return true if all is ok, false if the fifo is full.
130  */
131 static bool hdlc_parse(Hdlc *hdlc, bool bit, FIFOBuffer *fifo)
132 {
133         bool ret = true;
134
135         hdlc->demod_bits <<= 1;
136         hdlc->demod_bits |= bit ? 1 : 0;
137
138         /* HDLC Flag */
139         if (hdlc->demod_bits == HDLC_FLAG)
140         {
141                 if (!fifo_isfull(fifo))
142                 {
143                         fifo_push(fifo, HDLC_FLAG);
144                         hdlc->rxstart = true;
145                 }
146                 else
147                 {
148                         ret = false;
149                         hdlc->rxstart = false;
150                 }
151
152                 hdlc->currchar = 0;
153                 hdlc->bit_idx = 0;
154                 return ret;
155         }
156
157         /* Reset */
158         if ((hdlc->demod_bits & HDLC_RESET) == HDLC_RESET)
159         {
160                 hdlc->rxstart = false;
161                 return ret;
162         }
163
164         if (!hdlc->rxstart)
165                 return ret;
166
167         /* Stuffed bit */
168         if ((hdlc->demod_bits & 0x3f) == 0x3e)
169                 return ret;
170
171         if (hdlc->demod_bits & 0x01)
172                 hdlc->currchar |= 0x80;
173
174         if (++hdlc->bit_idx >= 8)
175         {
176                 if ((hdlc->currchar == HDLC_FLAG
177                         || hdlc->currchar == HDLC_RESET
178                         || hdlc->currchar == AX25_ESC))
179                 {
180                         if (!fifo_isfull(fifo))
181                                 fifo_push(fifo, AX25_ESC);
182                         else
183                         {
184                                 hdlc->rxstart = false;
185                                 ret = false;
186                         }
187                 }
188
189                 if (!fifo_isfull(fifo))
190                         fifo_push(fifo, hdlc->currchar);
191                 else
192                 {
193                         hdlc->rxstart = false;
194                         ret = false;
195                 }
196
197                 hdlc->currchar = 0;
198                 hdlc->bit_idx = 0;
199         }
200         else
201                 hdlc->currchar >>= 1;
202
203         return ret;
204 }
205
206
207 /**
208  * ADC ISR callback.
209  * This function has to be called by the ADC ISR when a sample of the configured
210  * channel is available.
211  * \param af Afsk context to operate on.
212  * \param curr_sample current sample from the ADC.
213  */
214 void afsk_adc_isr(Afsk *af, int8_t curr_sample)
215 {
216         AFSK_STROBE_ON();
217
218         /*
219          * Frequency discriminator and LP IIR filter.
220          * This filter is designed to work
221          * at the given sample rate and bit rate.
222          */
223         STATIC_ASSERT(SAMPLERATE == 9600);
224         STATIC_ASSERT(BITRATE == 1200);
225
226         /*
227          * Frequency discrimination is achieved by simply multiplying
228          * the sample with a delayed sample of (samples per bit) / 2.
229          * Then the signal is lowpass filtered with a first order,
230          * 600 Hz filter. The filter implementation is selectable
231          * through the CONFIG_AFSK_FILTER config variable.
232          */
233
234         af->iir_x[0] = af->iir_x[1];
235
236         #if (CONFIG_AFSK_FILTER == AFSK_BUTTERWORTH)
237                 af->iir_x[1] = ((int8_t)fifo_pop(&af->delay_fifo) * curr_sample) >> 2;
238                 //af->iir_x[1] = ((int8_t)fifo_pop(&af->delay_fifo) * curr_sample) / 6.027339492;
239         #elif (CONFIG_AFSK_FILTER == AFSK_CHEBYSHEV)
240                 af->iir_x[1] = ((int8_t)fifo_pop(&af->delay_fifo) * curr_sample) >> 2;
241                 //af->iir_x[1] = ((int8_t)fifo_pop(&af->delay_fifo) * curr_sample) / 3.558147322;
242         #else
243                 #error Filter type not found!
244         #endif
245
246         af->iir_y[0] = af->iir_y[1];
247
248         #if CONFIG_AFSK_FILTER == AFSK_BUTTERWORTH
249                 /*
250                  * This strange sum + shift is an optimization for af->iir_y[0] * 0.668.
251                  * iir * 0.668 ~= (iir * 21) / 32 =
252                  * = (iir * 16) / 32 + (iir * 4) / 32 + iir / 32 =
253                  * = iir / 2 + iir / 8 + iir / 32 =
254                  * = iir >> 1 + iir >> 3 + iir >> 5
255                  */
256                 af->iir_y[1] = af->iir_x[0] + af->iir_x[1] + (af->iir_y[0] >> 1) + (af->iir_y[0] >> 3) + (af->iir_y[0] >> 5);
257                 //af->iir_y[1] = af->iir_x[0] + af->iir_x[1] + af->iir_y[0] * 0.6681786379;
258         #elif CONFIG_AFSK_FILTER == AFSK_CHEBYSHEV
259                 /*
260                  * This should be (af->iir_y[0] * 0.438) but
261                  * (af->iir_y[0] >> 1) is a faster approximation :-)
262                  */
263                 af->iir_y[1] = af->iir_x[0] + af->iir_x[1] + (af->iir_y[0] >> 1);
264                 //af->iir_y[1] = af->iir_x[0] + af->iir_x[1] + af->iir_y[0] * 0.4379097269;
265         #endif
266
267         /* Save this sampled bit in a delay line */
268         af->sampled_bits <<= 1;
269         af->sampled_bits |= (af->iir_y[1] > 0) ? 1 : 0;
270
271         /* Store current ADC sample in the af->delay_fifo */
272         fifo_push(&af->delay_fifo, curr_sample);
273
274         /* If there is an edge, adjust phase sampling */
275         if (EDGE_FOUND(af->sampled_bits))
276         {
277                 if (af->curr_phase < PHASE_THRES)
278                         af->curr_phase += PHASE_INC;
279                 else
280                         af->curr_phase -= PHASE_INC;
281         }
282         af->curr_phase += PHASE_BIT;
283
284         /* sample the bit */
285         if (af->curr_phase >= PHASE_MAX)
286         {
287                 af->curr_phase %= PHASE_MAX;
288
289                 /* Shift 1 position in the shift register of the found bits */
290                 af->found_bits <<= 1;
291
292                 /*
293                  * Determine bit value by reading the last 3 sampled bits.
294                  * If the number of ones is two or greater, the bit value is a 1,
295                  * otherwise is a 0.
296                  * This algorithm presumes that there are 8 samples per bit.
297                  */
298                 STATIC_ASSERT(SAMPLEPERBIT == 8);
299                 uint8_t bits = af->sampled_bits & 0x07;
300                 if (bits == 0x07 // 111, 3 bits set to 1
301                  || bits == 0x06 // 110, 2 bits
302                  || bits == 0x05 // 101, 2 bits
303                  || bits == 0x03 // 011, 2 bits
304                 )
305                         af->found_bits |= 1;
306
307                 /*
308                  * NRZI coding: if 2 consecutive bits have the same value
309                  * a 1 is received, otherwise it's a 0.
310                  */
311                 if (!hdlc_parse(&af->hdlc, !EDGE_FOUND(af->found_bits), &af->rx_fifo))
312                         af->status |= AFSK_RXFIFO_OVERRUN;
313         }
314
315
316         AFSK_STROBE_OFF();
317 }
318
319 static void afsk_txStart(Afsk *af)
320 {
321         if (!af->sending)
322         {
323                 af->phase_inc = MARK_INC;
324                 af->phase_acc = 0;
325                 af->stuff_cnt = 0;
326                 af->sending = true;
327                 af->preamble_len = DIV_ROUND(CONFIG_AFSK_PREAMBLE_LEN * BITRATE, 8000);
328                 AFSK_DAC_IRQ_START(af->dac_ch);
329         }
330         ATOMIC(af->trailer_len  = DIV_ROUND(CONFIG_AFSK_TRAILER_LEN  * BITRATE, 8000));
331 }
332
333 #define BIT_STUFF_LEN 5
334
335 #define SWITCH_TONE(inc)  (((inc) == MARK_INC) ? SPACE_INC : MARK_INC)
336
337 /**
338  * DAC ISR callback.
339  * This function has to be called by the DAC ISR when a sample of the configured
340  * channel has been converted out.
341  *
342  * \param af Afsk context to operate on.
343  *
344  * \return The next DAC output sample.
345  */
346 uint8_t afsk_dac_isr(Afsk *af)
347 {
348         AFSK_STROBE_ON();
349
350         /* Check if we are at a start of a sample cycle */
351         if (af->sample_count == 0)
352         {
353                 if (af->tx_bit == 0)
354                 {
355                         /* We have just finished transimitting a char, get a new one. */
356                         if (fifo_isempty(&af->tx_fifo) && af->trailer_len == 0)
357                         {
358                                 AFSK_DAC_IRQ_STOP(af->dac_ch);
359                                 af->sending = false;
360                                 AFSK_STROBE_OFF();
361                                 return 0;
362                         }
363                         else
364                         {
365                                 /*
366                                  * If we have just finished sending an unstuffed byte,
367                                  * reset bitstuff counter.
368                                  */
369                                 if (!af->bit_stuff)
370                                         af->stuff_cnt = 0;
371
372                                 af->bit_stuff = true;
373
374                                 /*
375                                  * Handle preamble and trailer
376                                  */
377                                 if (af->preamble_len == 0)
378                                 {
379                                         if (fifo_isempty(&af->tx_fifo))
380                                         {
381                                                 af->trailer_len--;
382                                                 af->curr_out = HDLC_FLAG;
383                                         }
384                                         else
385                                                 af->curr_out = fifo_pop(&af->tx_fifo);
386                                 }
387                                 else
388                                 {
389                                         af->preamble_len--;
390                                         af->curr_out = HDLC_FLAG;
391                                 }
392
393                                 /* Handle char escape */
394                                 if (af->curr_out == AX25_ESC)
395                                 {
396                                         if (fifo_isempty(&af->tx_fifo))
397                                         {
398                                                 AFSK_DAC_IRQ_STOP(af->dac_ch);
399                                                 af->sending = false;
400                                                 AFSK_STROBE_OFF();
401                                                 return 0;
402                                         }
403                                         else
404                                                 af->curr_out = fifo_pop(&af->tx_fifo);
405                                 }
406                                 else if (af->curr_out == HDLC_FLAG || af->curr_out == HDLC_RESET)
407                                         /* If these chars are not escaped disable bit stuffing */
408                                         af->bit_stuff = false;
409                         }
410                         /* Start with LSB mask */
411                         af->tx_bit = 0x01;
412                 }
413
414                 /* check for bit stuffing */
415                 if (af->bit_stuff && af->stuff_cnt >= BIT_STUFF_LEN)
416                 {
417                         /* If there are more than 5 ones in a row insert a 0 */
418                         af->stuff_cnt = 0;
419                         /* switch tone */
420                         af->phase_inc = SWITCH_TONE(af->phase_inc);
421                 }
422                 else
423                 {
424                         /*
425                          * NRZI: if we want to transmit a 1 the modulated frequency will stay
426                          * unchanged; with a 0, there will be a change in the tone.
427                          */
428                         if (af->curr_out & af->tx_bit)
429                         {
430                                 /*
431                                  * Transmit a 1:
432                                  * - Stay on the previous tone
433                                  * - Increase bit stuff counter
434                                  */
435                                 af->stuff_cnt++;
436                         }
437                         else
438                         {
439                                 /*
440                                  * Transmit a 0:
441                                  * - Reset bit stuff counter
442                                  * - Switch tone
443                                  */
444                                 af->stuff_cnt = 0;
445                                 af->phase_inc = SWITCH_TONE(af->phase_inc);
446                         }
447
448                         /* Go to the next bit */
449                         af->tx_bit <<= 1;
450                 }
451                 af->sample_count = DAC_SAMPLEPERBIT;
452         }
453
454         /* Get new sample and put it out on the DAC */
455         af->phase_acc += af->phase_inc;
456         af->phase_acc %= SIN_LEN;
457
458         af->sample_count--;
459         AFSK_STROBE_OFF();
460         return sin_sample(af->phase_acc);
461 }
462
463
464 static size_t afsk_read(KFile *fd, void *_buf, size_t size)
465 {
466         Afsk *af = AFSK_CAST(fd);
467         uint8_t *buf = (uint8_t *)_buf;
468
469         #if CONFIG_AFSK_RXTIMEOUT == 0
470         while (size-- && !fifo_isempty_locked(&af->rx_fifo))
471         #else
472         while (size--)
473         #endif
474         {
475                 #if CONFIG_AFSK_RXTIMEOUT != -1
476                 ticks_t start = timer_clock();
477                 #endif
478
479                 while (fifo_isempty_locked(&af->rx_fifo));
480                 {
481                         cpu_relax();
482                         #if CONFIG_AFSK_RXTIMEOUT != -1
483                         if (timer_clock() - start > ms_to_ticks(CONFIG_AFSK_RXTIMEOUT))
484                                 return buf - (uint8_t *)_buf;
485                         #endif
486                 }
487
488                 *buf++ = fifo_pop_locked(&af->rx_fifo);
489         }
490
491         return buf - (uint8_t *)_buf;
492 }
493
494 static size_t afsk_write(KFile *fd, const void *_buf, size_t size)
495 {
496         Afsk *af = AFSK_CAST(fd);
497         const uint8_t *buf = (const uint8_t *)_buf;
498
499         while (size--)
500         {
501                 while (fifo_isfull_locked(&af->tx_fifo))
502                         cpu_relax();
503
504                 fifo_push_locked(&af->tx_fifo, *buf++);
505                 afsk_txStart(af);
506         }
507
508         return buf - (const uint8_t *)_buf;
509 }
510
511 static int afsk_flush(KFile *fd)
512 {
513         Afsk *af = AFSK_CAST(fd);
514         while (af->sending)
515                 cpu_relax();
516         return 0;
517 }
518
519 static int afsk_error(KFile *fd)
520 {
521         Afsk *af = AFSK_CAST(fd);
522         int err;
523
524         ATOMIC(err = af->status);
525         return err;
526 }
527
528 static void afsk_clearerr(KFile *fd)
529 {
530         Afsk *af = AFSK_CAST(fd);
531         ATOMIC(af->status = 0);
532 }
533
534
535 /**
536  * Initialize an AFSK1200 modem.
537  * \param af Afsk context to operate on.
538  * \param adc_ch  ADC channel used by the demodulator.
539  * \param dac_ch  DAC channel used by the modulator.
540  */
541 void afsk_init(Afsk *af, int adc_ch, int dac_ch)
542 {
543         #if CONFIG_AFSK_RXTIMEOUT != -1
544         MOD_CHECK(timer);
545         #endif
546         memset(af, 0, sizeof(*af));
547         af->adc_ch = adc_ch;
548         af->dac_ch = dac_ch;
549
550         fifo_init(&af->delay_fifo, (uint8_t *)af->delay_buf, sizeof(af->delay_buf));
551         fifo_init(&af->rx_fifo, af->rx_buf, sizeof(af->rx_buf));
552
553         /* Fill sample FIFO with 0 */
554         for (int i = 0; i < SAMPLEPERBIT / 2; i++)
555                 fifo_push(&af->delay_fifo, 0);
556
557         fifo_init(&af->tx_fifo, af->tx_buf, sizeof(af->tx_buf));
558
559         AFSK_ADC_INIT(adc_ch, af);
560         AFSK_DAC_INIT(dac_ch, af);
561         AFSK_STROBE_INIT();
562         LOG_INFO("MARK_INC %d, SPACE_INC %d\n", MARK_INC, SPACE_INC);
563
564         DB(af->fd._type = KFT_AFSK);
565         af->fd.write = afsk_write;
566         af->fd.read = afsk_read;
567         af->fd.flush = afsk_flush;
568         af->fd.error = afsk_error;
569         af->fd.clearerr = afsk_clearerr;
570         af->phase_inc = MARK_INC;
571 }