This repository has been archived by the owner on Jan 4, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathafsk_rx.c
317 lines (236 loc) · 7.43 KB
/
afsk_rx.c
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
/*
* AFSK Demodulation.
*
* Based on code from BertOS AFSK decoder.
* Originally by Develer S.r.l. (http://www.develer.com/), GPLv2 licensed.
*
*/
#include "hal.h"
#include "fifo.h"
#include <string.h>
#include "defines.h"
#include "afsk.h"
#include "ui/ui.h"
#include "fifo.h"
#include "adc_input.h"
#define ARM_MATH_CM4
#include "arm_math.h"
#define SAMPLERATE 9600 // The rate at which we are sampling
#define BITRATE 1200 // The actual bitrate at baseband. This is the baudrate.
#define SAMPLESPERBIT (SAMPLERATE / BITRATE) // How many DAC/ADC samples constitute one bit (8).
/* Important: Sample rate must be divisible by bitrate */
/* Phase sync constants */
#define PHASE_BITS 8 // How much to increment phase counter each sample
#define PHASE_INC 1 // Nudge by an eigth of a sample each adjustment
#define PHASE_MAX (SAMPLESPERBIT * PHASE_BITS) // Resolution of our phase counter = 64
#define PHASE_THRESHOLD (PHASE_MAX / 2) // Target transition point of our phase window
/* Detect transition */
#define BITS_DIFFER(bits1, bits2) (((bits1)^(bits2)) & 0x01)
#define TRANSITION_FOUND(bits) BITS_DIFFER((bits), (bits) >> 1)
/* Qeue of decoded bits. To be used by HDLC packet decoder */
static uint8_t _buf[AFSK_RX_QUEUE_SIZE];
static input_queue_t iq;
/*********************************************************
* This is our primary modem struct. It defines
* the values we need to demodulate data.
*********************************************************/
typedef struct AfskRx
{
int16_t iirX[2]; // Filter X cells
int16_t iirY[2]; // Filter Y cells
uint8_t sampled_bits; // Bits sampled by the demodulator (at ADC speed)
int8_t curr_phase; // Current phase of the demodulator
uint8_t found_bits; // Actual found bits at correct bitrate
bool cd; // Carrier detect
uint8_t cd_state;
} AfskRx;
static AfskRx afsk;
static void add_bit(bool bit);
int8_t delay_buf[100];
int8_t delay_buf1[100];
fifo_t fifo, fifo1;
/*******************************************
Modem Initialization
*******************************************/
input_queue_t* afsk_rx_init() {
/* Allocate memory for struct */
memset(&afsk, 0, sizeof(afsk));
fifo_init(&fifo, delay_buf, sizeof(delay_buf));
fifo_init(&fifo1, delay_buf1, sizeof(delay_buf1));
/* Fill sample FIFO with 0 */
for (int i = 0; i < SAMPLESPERBIT / 2; i++) {
fifo_push(&fifo, 0);
fifo_push(&fifo1, 0);
}
iqObjectInit(&iq, _buf, AFSK_RX_QUEUE_SIZE, NULL, NULL);
return &iq;
}
/*********************************************
* Turn receiving on and off
*********************************************/
void afsk_rx_enable()
{ adc_start_sampling(); }
void afsk_rx_disable()
{ adc_stop_sampling(); }
/*********************************************
* Handler for squelch on/off signal
*********************************************/
void trx_sq_handler(EXTDriver *extp, expchannel_t channel) {
(void) extp;
(void) channel;
}
#define ABS(x) ((x) < 0 ? -(x) : (x))
#define FIR_MAX_TAPS 16
/************************************************
* FIR filtering
************************************************/
typedef struct FIR
{
int8_t taps;
int8_t coef[FIR_MAX_TAPS];
int16_t mem[FIR_MAX_TAPS];
} FIR;
enum fir_filters
{
FIR_1200_BP=0,
FIR_2200_BP=1,
FIR_1200_LP=2
};
static FIR fir_table[] =
{
[FIR_1200_BP] = {
.taps = 11,
.coef = {
-12, -16, -15, 0, 20, 29, 20, 0, -15, -16, -12
},
.mem = {
0,
},
},
[FIR_2200_BP] = {
.taps = 11,
.coef = {
11, 15, -8, -26, 4, 30, 4, -26, -8, 15, 11
},
.mem = {
0,
},
},
[FIR_1200_LP] = {
.taps = 8,
.coef = {
-9, 3, 26, 47, 47, 26, 3, -9
},
.mem = {
0,
},
},
};
static int8_t fir_filter(int8_t s, enum fir_filters f)
{
int8_t Q = fir_table[f].taps - 1;
int8_t *B = fir_table[f].coef;
int16_t *Bmem = fir_table[f].mem;
int8_t i;
int16_t y;
Bmem[0] = s;
y = 0;
for (i = Q; i >= 0; i--)
{
y += Bmem[i] * B[i];
Bmem[i + 1] = Bmem[i];
}
return (int8_t) (y / 128);
}
/******************************************************************************
Automatic gain control.
*******************************************************************************/
#define DECAY 5
static uint8_t peak_mk = 250;
static uint8_t peak_sp = 250;
static int8_t agc (int8_t in, uint8_t *ppeak)
{
if (*ppeak > 100)
*ppeak -= DECAY;
if (in*2 > *ppeak)
*ppeak = in*2;
float factor = 250.0f / *ppeak;
return (int8_t) factor * (float) in;
}
/***************************************************************
This routine should be called 9600
times each second to analyze samples taken from
the physical medium.
****************************************************************/
void afsk_process_sample(int8_t curr_sample)
{
chSysLock();
afsk.iirY[0] = fir_filter(curr_sample, FIR_1200_BP);
afsk.iirY[1] = fir_filter(curr_sample, FIR_2200_BP);
afsk.iirY[0] = ABS(afsk.iirY[0]);
afsk.iirY[1] = ABS(afsk.iirY[1]);
afsk.iirY[0] = agc(afsk.iirY[0], &peak_mk);
afsk.iirY[1] = agc(afsk.iirY[1], &peak_sp);
afsk.sampled_bits <<= 1;
afsk.sampled_bits |= fir_filter(afsk.iirY[1] - afsk.iirY[0], FIR_1200_LP) > 0;
/*
* If there is a transition, adjust the phase of our sampler
* to stay in sync with the transmitter.
*/
if (TRANSITION_FOUND(afsk.sampled_bits)) {
if (afsk.curr_phase < PHASE_THRESHOLD) {
afsk.curr_phase += PHASE_INC;
} else {
afsk.curr_phase -= PHASE_INC;
}
}
afsk.curr_phase += PHASE_BITS;
/* Check if we have reached the end of
* our sampling window.
*/
if (afsk.curr_phase >= PHASE_MAX)
{
afsk.curr_phase %= PHASE_MAX;
/* Shift left to make room for the next bit */
afsk.found_bits <<= 1;
/*
* 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.
*/
uint8_t bits = afsk.sampled_bits & 0x07;
if ( bits == 0x07 // 111, 3 bits set to
|| bits == 0x06 // 110, 2 bits
|| bits == 0x05 // 101, 2 bits
|| bits == 0x03 // 011, 2 bits
)
afsk.found_bits |= 1;
/*
* Now we can pass the actual bit to the HDLC parser.
* We are using NRZI coding, so if 2 consecutive bits
* have the same value, we have a 1, otherwise a 0.
* We use the TRANSITION_FOUND function to determine this.
*/
add_bit( !TRANSITION_FOUND(afsk.found_bits) );
}
chSysUnlock();
}
/*********************************************************
* 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)
{
// chSysLock();
if (!iqIsFullI(&iq))
iqPutI(&iq, octet);
// chSysUnlock();
bit_count = 0;
}
}