-
Notifications
You must be signed in to change notification settings - Fork 132
/
wspr.c
483 lines (388 loc) · 13 KB
/
wspr.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
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
/* Raspberry Pi bareback LF/MF WSPR transmitter
Works at frequencies up to about 1MHz - above that the tuning resolution isn't good enough
for the 1.46Hz tuning steps WSPR requires.
The output is a square wave so a low pass filter is REQUIRED
Based on WSPR code from F8CHK and PiFM code from http://www.icrobotics.co.uk/wiki/index.php/Turning_the_Raspberry_Pi_Into_an_FM_Transmitter
Brought together by Dan MD1CLV
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <ctype.h>
#include <dirent.h>
#include <math.h>
#include <fcntl.h>
#include <assert.h>
#include <sys/mman.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include "wspr.h" // wspr definitions and functions
/* RF code: */
#define BCM2708_PERI_BASE 0x20000000
#define GPIO_BASE (BCM2708_PERI_BASE + 0x200000) /* GPIO controller */
#define PAGE_SIZE (4*1024)
#define BLOCK_SIZE (4*1024)
int mem_fd;
char *gpio_mem, *gpio_map;
char *spi0_mem, *spi0_map;
// I/O access
volatile unsigned *gpio;
volatile unsigned *allof7e;
// GPIO setup macros. Always use INP_GPIO(x) before using OUT_GPIO(x) or SET_GPIO_ALT(x,y)
#define INP_GPIO(g) *(gpio+((g)/10)) &= ~(7<<(((g)%10)*3))
#define OUT_GPIO(g) *(gpio+((g)/10)) |= (1<<(((g)%10)*3))
#define SET_GPIO_ALT(g,a) *(gpio+(((g)/10))) |= (((a)<=3?(a)+4:(a)==4?3:2)<<(((g)%10)*3))
#define GPIO_SET *(gpio+7) // sets bits which are 1 ignores bits which are 0
#define GPIO_CLR *(gpio+10) // clears bits which are 1 ignores bits which are 0
#define GPIO_GET *(gpio+13) // sets bits which are 1 ignores bits which are 0
#define ACCESS(base) *(volatile int*)((int)allof7e+base-0x7e000000)
#define SETBIT(base, bit) ACCESS(base) |= 1<<bit
#define CLRBIT(base, bit) ACCESS(base) &= ~(1<<bit)
#define CM_GP0CTL (0x7e101070)
#define GPFSEL0 (0x7E200000)
#define CM_GP0DIV (0x7e101074)
struct GPCTL {
char SRC : 4;
char ENAB : 1;
char KILL : 1;
char : 1;
char BUSY : 1;
char FLIP : 1;
char MASH : 2;
unsigned int : 13;
char PASSWD : 8;
};
void txon()
{
allof7e = (unsigned *)mmap(
NULL,
0x01000000, //len
PROT_READ|PROT_WRITE,
MAP_SHARED,
mem_fd,
0x20000000 //base
);
if ((int)allof7e==-1) exit(-1);
SETBIT(GPFSEL0 , 14);
CLRBIT(GPFSEL0 , 13);
CLRBIT(GPFSEL0 , 12);
struct GPCTL setupword = {6/*SRC*/, 1, 0, 0, 0, 1,0x5a};
ACCESS(CM_GP0CTL) = *((int*)&setupword);
}
void txoff()
{
struct GPCTL setupword = {6/*SRC*/, 0, 0, 0, 0, 1,0x5a};
ACCESS(CM_GP0CTL) = *((int*)&setupword);
}
void setfreq(long freq)
{
ACCESS(CM_GP0DIV) = (0x5a << 24) + freq;
}
//
// Set up a memory regions to access GPIO
//
void setup_io()
{
/* open /dev/mem */
if ((mem_fd = open("/dev/mem", O_RDWR|O_SYNC) ) < 0) {
printf("can't open /dev/mem \n");
exit (-1);
}
/* mmap GPIO */
// Allocate MAP block
if ((gpio_mem = malloc(BLOCK_SIZE + (PAGE_SIZE-1))) == NULL) {
printf("allocation error \n");
exit (-1);
}
// Make sure pointer is on 4K boundary
if ((unsigned long)gpio_mem % PAGE_SIZE)
gpio_mem += PAGE_SIZE - ((unsigned long)gpio_mem % PAGE_SIZE);
// Now map it
gpio_map = (unsigned char *)mmap(
gpio_mem,
BLOCK_SIZE,
PROT_READ|PROT_WRITE,
MAP_SHARED|MAP_FIXED,
mem_fd,
GPIO_BASE
);
if ((long)gpio_map < 0) {
printf("mmap error %d\n", (int)gpio_map);
exit (-1);
}
// Always use volatile pointer!
gpio = (volatile unsigned *)gpio_map;
}
void setup_gpios()
{
int g;
// Switch GPIO 7..11 to output mode
/************************************************************************\
* You are about to change the GPIO settings of your computer. *
* Mess this up and it will stop working! *
* It might be a good idea to 'sync' before running this program *
* so at least you still have your code changes written to the SD-card! *
\************************************************************************/
// Set GPIO pins 7-11 to output
for (g=7; g<=11; g++) {
INP_GPIO(g); // must use INP_GPIO before we can use OUT_GPIO
//OUT_GPIO(g);
}
}
/*
WSPR encoding module:
Thanks to K1JT, G4JNT and PE1NZZ for publishing
helping infos.
Encoding process is in 5 steps:
* bits packing of user message in 50 bits
* store the 50 bits dans 11 octets (88 bits and only 81 useful)
* convolutionnal encoding with two pariy generators (-> 162 bits)
* interleaving of the 162 bits with bit-reverse technique
* synchronisation with a psudo-random vector to obtain the
162 symbols defining one frequency of 4.
F8CHK 29/03/2011 */
void
Code_msg (char usr_message[], unsigned long int *N, unsigned long int *M)
{
unsigned long int n, m;
unsigned int i, j, power, callsign_length;
char callsign[7] = "", // callsign string
locator[5] = "", // locator string
power_str[3] = ""; // power string
strcpy (callsign, " "); // filling with spaces
i = 0;
while (usr_message[i] != ' ')
{
callsign[i] = islower(usr_message[i])?toupper(usr_message[i]):usr_message[i]; // extract callsign
i++;
}
callsign_length = i;
i++;
j = 0;
while (usr_message[i] != ' ')
locator[j++] = islower(usr_message[i])?toupper(usr_message[i++]):usr_message[i++]; // extract locator
locator[j] = 0;
i++;
j = 0;
while (usr_message[i] != 0)
power_str[j++] = usr_message[i++]; // extract power
power_str[j] = 0;
power = atoi (power_str); // power needs to be an integer
printf("Call: %s / Locator: %s / Power: %ddBm\n", callsign, locator, power);
// Place a space in first position if third character is not a digit
if (!isdigit (callsign[2]))
{
for (i = callsign_length; i > 0; i--)
callsign[i] = callsign[i - 1];
callsign[0] = ' ';
}
// callsign encoding:
// numbers have a value between 0 and 9
// and letters a value between 10 and 35
// spaces a value of 36
n = (callsign[0] >= '0'
&& callsign[0] <= '9' ? callsign[0] - '0' : callsign[0] ==
' ' ? 36 : callsign[0] - 'A' + 10);
n = n * 36 + (callsign[1] >= '0'
&& callsign[1] <= '9' ? callsign[1] - '0' : callsign[1] ==
' ' ? 36 : callsign[1] - 'A' + 10);
n = n * 10 + (callsign[2] - '0'); // only number (0-9)
n = 27 * n + (callsign[3] == ' ' ? 26 : callsign[3] - 'A'); // only space or letter
n = 27 * n + (callsign[4] == ' ' ? 26 : callsign[4] - 'A');
n = 27 * n + (callsign[5] == ' ' ? 26 : callsign[5] - 'A');
// Locator encoding
m =
(179 - 10 * (locator[0] - 65) - (locator[2] - 48)) * 180 +
10 * (locator[1] - 65) + locator[3] - 48;
// Power encoding
m = m * 128 + power + 64;
*N = n;
*M = m;
}
void
Pack_msg (unsigned long int N, unsigned long int M, unsigned char c[])
{
// Bit packing
// Store in 11 characters because we need 81 bits for FEC correction
c[0] = N >> 20; // Callsign
c[1] = N >> 12;
c[2] = N >> 4;
c[3] = N;
c[3] = c[3] << 4;
c[3] = c[3] | (M >> 18); // locator and power
c[4] = M >> 10;
c[5] = M >> 2;
c[6] = M & 0x03;
c[6] = c[6] << 6;
c[7] = 0; // always at 0
c[8] = 0;
c[9] = 0;
c[10] = 0;
}
void
Generate_parity (unsigned char c[], unsigned char symbols[])
{
unsigned long int Reg0 = 0, // 32 bits shift register
Reg1 = 0, result0, result1;
int count1, // to count the number
count2, // of bits at one
bit_result = 0, i, j, k, l;
l = 0;
for (j = 0; j < 11; j++) // each byte
{
for (i = 7; i >= 0; i--)
{
Reg0 = (Reg0 << 1);
Reg0 = Reg0 | (c[j] >> i); // each bit
Reg1 = Reg0;
result0 = Reg0 & POLYNOM_1; // first polynom
count1 = 0;
for (k = 0; k < 32; k++) // how many bit at one?
{
bit_result = result0 >> k;
if ((bit_result & 0x01) == 1)
count1++;
}
if (count1 % 2 == 1) // if number of one is odd
symbols[l] = 1; // parity = 1
l++;
result1 = Reg1 & POLYNOM_2; // second polynom
count2 = 0;
for (k = 0; k < 32; k++) // how many bit at one?
{
bit_result = result1 >> k;
if ((bit_result & 0x01) == 1)
count2++;
}
if (count2 % 2 == 1) // if number of one is odd
symbols[l] = 1; // parity = 1
l++;
} // end of each bit (32) loop
} // end of each byte (11) loop
}
void
Interleave (unsigned char symbols[], unsigned char symbols_interleaved[])
{
int i, j, k, l, P;
P = 0;
while (P < 162)
{
for (k = 0; k <= 255; k++) // bits reverse, ex: 0010 1110 --> 0111 0100
{
i = k;
j = 0;
for (l = 7; l >= 0; l--) // hard work is done here...
{
j = j | (i & 0x01) << l;
i = i >> 1;
}
if (j < 162)
symbols_interleaved[j] = symbols[P++]; // range in interleaved table
}
} // end of while, interleaved table is full
}
void
Synchronise (unsigned char symbols_interleaved[],
unsigned char symbols_wspr[])
{
unsigned int sync_word [162]={
1,1,0,0,0,0,0,0,1,0,0,0,1,1,1,0,0,0,1,0,0,1,0,1,1,1,1,0,0,0,0,0,0,0,1,0,0,1,0,1,0,0,
0,0,0,0,1,0,1,1,0,0,1,1,0,1,0,0,0,1,1,0,1,0,0,0,0,1,1,0,1,0,1,0,1,0,1,0,0,1,0,0,1,0,
1,1,0,0,0,1,1,0,1,0,1,0,0,0,1,0,0,0,0,0,1,0,0,1,0,0,1,1,1,0,1,1,0,0,1,1,0,1,0,0,0,1,
1,1,0,0,0,0,0,1,0,1,0,0,1,1,0,0,0,0,0,0,0,1,1,0,1,0,1,1,0,0,0,1,1,0,0,0
};
int i;
for (i = 0; i < 162; i++)
symbols_wspr[i] = sync_word[i] + 2 * symbols_interleaved[i];
}
void
code_wspr (char* wspr_message, unsigned char* wspr_symbols)
{
unsigned char symbols_parity[162] = "", // contains 2*81 parity bits
symbols_interleaved[162] = "", // contains parity bits after interleaving
c_packed[11]; // for bit packing
unsigned long N, // for callsign
M; // for locator and power
Code_msg (wspr_message, &N, &M);
Pack_msg (N, M, c_packed);
Generate_parity (c_packed, symbols_parity);
Interleave (symbols_parity, symbols_interleaved);
Synchronise (symbols_interleaved, wspr_symbols);
}
void calculate_tuning_info(tuning_data* tuning_info)
{
double divisor;
unsigned long decimal_part;
unsigned long fractional_part;
double actual_divisor;
divisor = (double)500000000/tuning_info->requested;
decimal_part = (unsigned long) divisor;
fractional_part = (divisor - decimal_part) * (1 << 12);
tuning_info->tuning_word = decimal_part * (1 << 12) + fractional_part;
actual_divisor = (double)tuning_info->tuning_word / (float)(1 << 12);
tuning_info->actual = (double)500000000 / actual_divisor;
}
void sym_to_tuning_words(double base_freq, unsigned char* wspr_symbols, unsigned long* tuning_words)
{
int i;
double symbol_freq;
tuning_data tuning_info[4];
for (i = 0; i < 4; i++)
{
symbol_freq = base_freq + (i-2) * WSPR_OFFSET;
tuning_info[i].requested = symbol_freq;
calculate_tuning_info(&tuning_info[i]);
printf("Symbol %d: Target freq=%fHz, Actual freq=%fHz, Error=%fHz, Tuning Word=%lx\n", i, symbol_freq, tuning_info[i].actual, symbol_freq-tuning_info[i].actual, tuning_info[i].tuning_word);
}
for (i = 0; i < 162; i++)
{
tuning_words[i] = tuning_info[wspr_symbols[i]].tuning_word;
}
}
int main(int argc, char *argv[])
{
char wspr_message[20]; // user beacon message to encode
unsigned char wspr_symbols[162] = {};
unsigned long tuning_words[162];
int i;
double centre_freq;
if(argc != 5){
printf("Usage: wspr-pi <callsign> <locator> <power in dBm> <frequency in Hz>\n");
printf("\te.g.: wspr-pi MD1CLV IO74 30 137500\n");
return 1;
}
// argv[1]=callsign, argv[2]=locator, argv[3]=power(dBm)
sprintf(wspr_message, "%s %s %s", argv[1], argv[2], argv[3]);
printf("Sending |%s|\n", wspr_message);
code_wspr(wspr_message, wspr_symbols);
for (i = 0; i < 162; i++)
printf("%d, ", wspr_symbols[i]);
printf("\n");
centre_freq = atof(argv[4]);
sym_to_tuning_words(centre_freq, wspr_symbols, tuning_words);
/* Now we have the list of tuning words, let's transmit them
Note that this version doesn't check whether we are in a correct timeslot
*/
setup_io();
setup_gpios();
printf("Transmitting... ");
txon();
for (i = 0; i < 162; i++) {
setfreq(tuning_words[i]);
usleep(8192*1000/12);
}
txoff();
printf("Done!\n");
return 0;
}