-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathafsk_rx.c
More file actions
326 lines (261 loc) · 9.7 KB
/
Copy pathafsk_rx.c
File metadata and controls
326 lines (261 loc) · 9.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
/*
* $Id: afsk_rx.c,v 1.12 2009-05-15 22:47:08 la7eca Exp $
* AFSK receiver/demodulator
*/
#include <stdbool.h>
#include <stdint.h>
#include <avr/io.h>
#include <avr/interrupt.h>
#include <avr/sleep.h>
#include "kernel/kernel.h"
#include "afsk.h"
#include "defines.h"
#include "config.h"
#include "kernel/stream.h"
#include "transceiver.h"
#include "ui.h"
/* The middle point between the two tones. */
#define TXTONE_MID ((AFSK_TXTONE_MARK+AFSK_TXTONE_SPACE)/2)
/* Symbol sampling interval. Used with counter TCNT1 */
#define SYMBOL_SAMPLE_INTERVAL ((SCALED_F_CPU/8/AFSK_BAUD))
#define SYMBOL_SAMPLE_RESYNC ((SCALED_F_CPU/8/AFSK_BAUD))*0.5
/* These are used with counter TCNT0 to measure frequency of input signal.
* Note that these values corresponds to the time of a wave period.
*/
#define CENTER_FREQUENCY ((SCALED_F_CPU/64)/TXTONE_MID-2)
#define MARK_FREQUENCY ((SCALED_F_CPU/64)/AFSK_TXTONE_MARK-1)
#define SPACE_FREQUENCY ((SCALED_F_CPU/64)/AFSK_TXTONE_SPACE-1)
#define FREQUENCY_DEVIATION 50
int8_t hard_symbol; /* Most recent detected symbol. */
int8_t soft_symbol; /* Differs from the above by also having UNDECIDED as valid state */
bool valid_symbol; /* (DCD) Will always be true when using the simple detector */
extern bool transmit; /* True when transmitter(modulator) is active (see afsk_tx.c). */
bool decoder_running = false;
bool decoder_enabled = false;
double sqlevel;
stream_t afsk_rx_stream;
static void _afsk_stop_decoder (void);
static void _afsk_start_decoder (void);
static void _afsk_abort (void);
static void sample_bits(void);
static void add_bit(bool);
/************************************************************
* Init afsk decoder
************************************************************/
stream_t* afsk_init_decoder ()
{
#if defined TARGET_USBKEY
set_bit(DIDR1, AIN1D);
make_output(TPB1);
make_output(USBKEY_LED3);
clear_bit(ACSR, ACD);
#endif
STREAM_INIT(afsk_rx_stream, AFSK_DECODER_BUFFER_SIZE);
return &afsk_rx_stream;
}
/****************************************************************************
* Start the decoding of a signal.
****************************************************************************/
static void _afsk_start_decoder ()
{
decoder_running = true;
valid_symbol = false;
soft_symbol = hard_symbol = UNDECIDED;
/* Setup Timer0 for symbol detection and sampling clock synchronization */
clear_bit (TCCR0A, WGM00); /* \ */
clear_bit (TCCR0A, WGM01); /* } Enable normal mode */
clear_bit (TCCR0B, WGM02); /* / */
set_bit (TCCR0B, CS00); /* \ */
set_bit (TCCR0B, CS01); /* } Prescaler set to 64 */
clear_bit (TCCR0B, CS02); /* / */
/* Setup Timer1 for symbol sampling at 1200 baud*/
clear_bit (TCCR1A, WGM10); /* \ */
clear_bit (TCCR1A, WGM11); /* } Enable normal mode */
clear_bit (TCCR1B, WGM12); /* / */
// clear_bit (ASSR, AS2); /* I/O clock as source */
clear_bit (TCCR1B, CS10); /* \ */
set_bit (TCCR1B, CS11); /* } Prescaler set to 8 */
clear_bit (TCCR1B, CS12); /* /
*/
#if defined TARGET_USBKEY
set_port(USBKEY_LED3);
#else
/* Enable pin change interrupt on rx data from receiver */
set_bit (PCMSK0, PCINT2);
set_bit (PCICR, PCIE0);
#endif
pri_rgb_led_on(true,true,false);
}
/****************************************************************************
* Stop the decoding (typically used when a signal drops).
****************************************************************************/
static void _afsk_stop_decoder ()
{
decoder_running = false;
#if defined TARGET_USBKEY
clear_port(USBKEY_LED3);
#else
/* Disable pin change interrupt on input pin from receiver */
clear_bit (PCICR, PCIE0);
clear_bit (PCMSK0, PCINT2);
#endif
pri_rgb_led_off();
_afsk_abort (); // Just in case the decoder is disabled while the HDLC
// decoder is still in sync
}
/****************************************************************************
* Enable/disable decoder. I.e. start/stop listening to radio receiver
* to see if there is a signal. If this is enabled, the AFSK decoder will
* be activated when incoming signal is stronger than squelch level.
****************************************************************************/
void afsk_enable_decoder ()
{
make_input(TXDATA);
#if defined TARGET_USBKEY
/* Enable analog comparator input and interrupt */
set_bit(ACSR, ACBG);
set_bit(ACSR, ACIE);
// set_bit(ACSR, ACIS0); // On rising edge only
// set_bit(ACSR, ACIS1);
#endif
decoder_enabled = true;
GET_PARAM(TRX_SQUELCH, &sqlevel);
}
void afsk_disable_decoder ()
{
decoder_enabled = false;
_afsk_stop_decoder();
#if defined TARGET_USBKEY
/* Disable analog comparator input and interrupt */
clear_bit(ACSR, ACIE);
clear_bit(ACSR, ACBG);
#endif
}
static uint16_t signal_ints = 0;
#define SIG_THRESHOLD 2
/***************************************************************************
* To be called periodically to see if there is a signal on the channel *
***************************************************************************/
void afsk_check_channel ()
{
if (decoder_enabled && !transmit)
{
#if defined TARGET_USBKEY
if ((!decoder_running) && (signal_ints >= SIG_THRESHOLD))
_afsk_start_decoder();
else if (decoder_running && (signal_ints < SIG_THRESHOLD))
_afsk_stop_decoder();
#else
if ((!decoder_running) && (adf7021_read_rssi() > sqlevel))
_afsk_start_decoder();
else if (decoder_running && (adf7021_read_rssi() <= sqlevel))
_afsk_stop_decoder();
#endif
}
signal_ints = 0;
}
/******************************************************************************
* Interrupt routine to be called when the input signal from the *
* receiver changes level (this corresponds to a zero-crossing of the input *
* signal). Measures frequency of signal and determines *
* if there has been a toggle between mark and space. *
******************************************************************************/
uint16_t prev_zc_count;
#if defined TARGET_USBKEY
ISR(ANALOG_COMP_vect)
#else
ISR(PCINT0_vect)
#endif
{
signal_ints++;
if (!decoder_running)
return;
int8_t symbol;
uint8_t count = TCNT0; /* Counts on Timer0 since last level-change */
TCNT0 = 0;
set_sleep_mode(SLEEP_MODE_IDLE);
/* Use use count now + previous count: The sample is done every
* half-period, but we use the time of the whole period for analysis.
*/
uint16_t counts = (uint16_t) prev_zc_count + count;
prev_zc_count = count;
symbol = UNDECIDED;
if (counts > CENTER_FREQUENCY &&
counts < MARK_FREQUENCY + FREQUENCY_DEVIATION)
symbol = MARK;
else
if (counts > SPACE_FREQUENCY - FREQUENCY_DEVIATION &&
counts < CENTER_FREQUENCY )
symbol = SPACE;
/* If toggle between mark and space */
if (symbol != UNDECIDED && symbol != soft_symbol)
sample_bits();
if (symbol == UNDECIDED && soft_symbol == UNDECIDED) {
if (valid_symbol)
_afsk_abort();
valid_symbol = false;
}
else
if (symbol != UNDECIDED && soft_symbol != UNDECIDED)
valid_symbol = true;
soft_symbol = symbol;
}
/*******************************************************************************
* To be called when a toggle between mark and space is detected, to
* sample incoming bits.
*******************************************************************************/
static void sample_bits()
{
/* The number of 1-bits is determined by the timer count */
uint16_t counts = TCNT1;
TCNT1 = 0;
uint8_t ones = (uint8_t)
((counts - (uint16_t) SYMBOL_SAMPLE_RESYNC+5) / (uint16_t) (SYMBOL_SAMPLE_INTERVAL+10));
if (ones >= 8)
/* Bit stuffing/FLAG will ensure that there should not be more than
* six 1-bits in a valid frame */
_afsk_abort();
else {
for (uint8_t i = 0; i<ones; i++)
add_bit(true);
add_bit(false);
}
}
/********************************************************************************
* Send a single bit to the HDLC decoder
********************************************************************************/
static uint8_t bit_count = 0;
static void add_bit(bool bit)
{
static uint8_t octet;
octet = (octet >> 1) | (bit ? 0x80 : 0x00);
bit_count++;
if (bit_count == 8)
{
/* Always leave room for abort token */
if (afsk_rx_stream.length.cnt < afsk_rx_stream.size-1)
stream_put_nb (&afsk_rx_stream, octet);
else
/* This should never happened, but if if it does it can only be
* caused by the buffer being too short or a thread running too
* long. Having some kind of log to report such errors would be
* a good idea.
*/
_afsk_abort();
bit_count = 0;
}
}
/******************************************************************************
* Indicate that decoding fails (e.g. if signal is lost)
******************************************************************************/
static void _afsk_abort ()
{
/* A sequence of seven or more consecutive ones will always
* reset the HDLC decoder to flag sync state.
* If the buffer full, an abort token has already been
* written to the stream
*/
bit_count = 0;
if (afsk_rx_stream.length.cnt < afsk_rx_stream.size)
stream_put_nb (&afsk_rx_stream, 0xFF);
}