-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathusbsoftrock.c
executable file
·490 lines (399 loc) · 14.7 KB
/
usbsoftrock.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
484
485
486
487
488
489
/***
* SoftRock USB I2C host control program
* Copyright (C) 2009 Andrew Nilsson ([email protected])
*
* 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, write to the Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Based on powerSwitch.c by Christian Starkjohann,
* and usbtemp.c by Mathias Dalheimer
* of Objective Development Software GmbH (2005)
* (see http://www.obdev.at/avrusb)
*/
/*
General Description:
This program queries and controls the AVR Si570 hardware.
It must be linked with libusb, a library for accessing the USB bus from
Linux, FreeBSD, Mac OS X and other Unix operating systems. Libusb can be
obtained from http://libusb.sourceforge.net/.
*/
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <math.h>
#include <getopt.h>
#ifdef HAVE_LIBNCURSES
#include <ncurses.h>
#endif
/* Needed to compile on older FC3 systems */
#if defined __linux
#include <sys/types.h>
#include <linux/limits.h>
#endif
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <usb.h> /* this is libusb, see http://libusb.sourceforge.net/ */
#include "operations.h"
#include "config_ops.h"
#include "interactive.h"
#define USBDEV_SHARED_VENDOR 0x16C0 /* VOTI */
#define USBDEV_SHARED_PRODUCT 0x05DC /* Obdev's free shared PID */
/* Use obdev's generic shared VID/PID pair and follow the rules outlined
* in firmware/usbdrv/USBID-License.txt.
*/
// Number of attempts to complete an USB operation before giving up.
#define USB_MAX_RETRIES 3
double multiplier = 4;
#define VENDOR_NAME "www.obdev.at"
#define PRODUCT_NAME "DG8SAQ-I2C"
extern char serialNumberString[256];
#define MAX_COMMAND_ARGS 3
int verbose = 0;
int i2cAddress = SI570_I2C_ADDR;
double fXtall = SI570_NOMINAL_XTALL_FREQ;
double startupFreq = SI570_DEFAULT_STARTUP_FREQ;
int setByValue = 0;
int major;
int minor;
static void usage(char *name)
{
fprintf(stderr, "usbsoftrock %s\n", VERSION);
fprintf(stderr, "usage: %s [OPTION] COMMAND\n\n", name);
fprintf(stderr, "OPTION is one or more of\n");
fprintf(stderr, " -a Advanced firmware present\n");
fprintf(stderr, " i.e. let the firmware calculate registers\n");
fprintf(stderr, " -d Enter a mode that listens for commands via UDP.\n");
// fprintf(stderr, " -h <freq MHz> Enable subharmonic (/3) sampling from frequency (DEFAULT off)\n");
fprintf(stderr, " -i <address> I2C address in DECIMAL (DEFAULT = 85 (0x55))\n");
fprintf(stderr, " -m <multiplier> Multiplication factor for frequency (DEFAULT = 4)\n");
fprintf(stderr, " -p <port num> Port to listen for UDP datagrams (DEFAULT = 19004)\n");
fprintf(stderr, " -s <startup frequency MHz> Factory programmed startup frequency (DEFAULT = 56.32)\n");
fprintf(stderr, " -v Verbose output (fairly useful)\n");
fprintf(stderr, " -vv Even more verbose output (debugging)\n");
fprintf(stderr, " -x <calibrated xtall freq MHz> Corrected XTALL frequency of Si570 device calculated\n");
fprintf(stderr, " through the use of the calibrate command immediately\n");
fprintf(stderr, " after startup.\n");
fprintf(stderr, "COMMAND is one of\n");
fprintf(stderr, " calibrate (may require -s option)\n");
fprintf(stderr, " getfreq\n");
fprintf(stderr, " getregisters\n");
#ifdef HAVE_LIBNCURSES
fprintf(stderr, " interactive\n");
#endif
fprintf(stderr, " ptt {on|off}\n");
fprintf(stderr, " set bpf {on|off} (PE0FKO+TF3LJ+Mobo)\n");
fprintf(stderr, " set bpf_addr <band> <filter> (PE0FKO >= 15.12+Mobo\n");
fprintf(stderr, " set bpf_point <crossover> <f in MHz> (PE0FKO+TF3LJ+Mobo)\n");
fprintf(stderr, " set lpf {on|off} (TF3LJ+Mobo only)\n");
fprintf(stderr, " set lpf_addr <band> <filter> \" \n");
fprintf(stderr, " set lpf_point <crossover> <f in MHz> \" \n");
fprintf(stderr, " set freq <frequency in MHz>\n");
fprintf(stderr, " set si570_addr <i2c address in decimal>\n");
fprintf(stderr, " set si570_multiplier [band] <decimal factor> (PE0FKO>=15.12+Mobo)\n");
fprintf(stderr, " set startup <frequency in MHz> (PE0FKO+TF3LJ+Mobo)\n");
fprintf(stderr, " set xtall <frequency in MHz> (PE0FKO+TF3LJ+Mobo)\n");
fprintf(stderr, " status\n\n");
fprintf(stderr, " where TF3LJ = Lofturs AtMega168 derivative\n");
fprintf(stderr, " Mobo = Mobo 4.3 Project AT90USB162 Firmware\n");
}
int do_command(usb_dev_handle *handle, char **argv, int argc, char *result) {
sprintf(result, "ok");
if ((strcmp(argv[0], "get") == 0) && (argc >= 1)) {
if(strcmp(argv[1], "freq") == 0){
double freq;
if (setByValue)
freq = readFrequencyByValue(handle);
else
freq = getFrequency(handle);
if (freq != 0.00) {
if (verbose >= 2)
printf("Frequency : %f (x %.2f)\n", freq / multiplier, multiplier);
sprintf(result, "ok %f", freq / multiplier);
} else {
sprintf(result, "error");
return -1;
}
} else if (strcmp(argv[1], "si570_multiplier") == 0) {
double sub, mul;
// Just works for non per-band multipliers
if (readMultiplyLO(handle, 0, &mul, &sub) == 0)
sprintf(result, "ok %f", mul);
else
sprintf(result, "error");
} else if (strcmp(argv[1], "local_multiplier")) {
sprintf(result, "ok %f", multiplier);
} else {
sprintf(result, "error");
}
} else if ((strcmp(argv[0], "set") == 0) && (argc >= 2)) {
if ((strcmp(argv[1], "ptt") == 0) && (argc >= 1)) {
setPTT(handle, (strncmp(argv[2],"on",2) == 0) ? 1 : 0);
} else if ((strcmp(argv[1], "bpf") == 0)) {
setBPF(handle, (strncmp(argv[2],"on",2) == 0) ? 1 : 0);
} else if (strcmp(argv[1], "freq") == 0) {
if (setByValue)
setFreqByValue(handle, atof(argv[2]));
else
setFrequency(handle, atof(argv[2]));
} else if (strcmp(argv[1], "local_multiplier")) {
multiplier = atof(argv[2]);
} else {
sprintf(result, "error");
return -1;
}
} else {
sprintf(result, "error");
return -1;
}
return 0;
}
/**
* Main routine. Parse commandline args and trigger actions.
*/
int main(int argc, char **argv) {
usb_dev_handle *handle = NULL;
char * usbSerialID = NULL;
int c;
int port = 19004;
int daemon = 0;
// Read options
while ( (c = getopt(argc, argv, "adi:m:p:s:u:vx:")) != -1) {
switch (c) {
case 'i':
i2cAddress = atoi(optarg);
break;
case 'a':
setByValue = 1;
break;
case 'd':
daemon = 1;
break;
case 'm':
multiplier = atof(optarg);
break;
case 'p':
port = atoi(optarg);
break;
case 's':
startupFreq = atof(optarg);
break;
case 'x':
fXtall = atof(optarg);
break;
case 'u':
usbSerialID = optarg;
break;
case 'v':
verbose++;
break;
default: /* '?' */
usage(argv[0]);
exit(EXIT_FAILURE);
}
}
if (verbose) {
printf("I2C Address = %X\n", i2cAddress);
printf("fXtall = %f\n", fXtall);
printf("multiplier = %f\n", multiplier);
printf("startupFreq = %f\n", startupFreq);
}
if((argc <= optind) && (daemon == 0)){
usage(argv[0]);
exit(1);
}
usb_init();
char attempt=0, error=0;
do {
attempt++;
error=usbOpenDevice(&handle, USBDEV_SHARED_VENDOR, VENDOR_NAME, USBDEV_SHARED_PRODUCT, PRODUCT_NAME, usbSerialID);
if(error != 0){
fprintf(stderr, "Could not open USB device \"%s\" with vid=0x%x pid=0x%x, retrying\n", PRODUCT_NAME, USBDEV_SHARED_VENDOR, USBDEV_SHARED_PRODUCT);
sleep(2*attempt);
}
} while (error && attempt < USB_MAX_RETRIES);
if (error) {
fprintf(stderr, "Permanent problem opening usb device. Giving up.\n");
exit(1);
}
unsigned short version = readVersion(handle);
major = (version & 0xFF00) >> 8;
minor = (version & 0xFF);
/* Relocate lower later */
if (daemon) {
printf("Starting daemon...\n");
int socket_desc;
socket_desc=socket(AF_INET,SOCK_DGRAM,IPPROTO_UDP);
if (socket_desc==-1)
perror("Create socket");
struct sockaddr_in address;
/* type of socket created in socket() */
address.sin_family = AF_INET;
address.sin_addr.s_addr = INADDR_ANY;
address.sin_port = htons(port);
/* bind the socket to the port specified above */
int retval;
if (bind(socket_desc,(struct sockaddr *)&address,sizeof(address)) != 0) {
fprintf(stderr, "Error binding to port %d\n", port);
exit(0);
}
while (1) {
ssize_t bytes;
char buffer[1024];
struct sockaddr_in clnt;
socklen_t clnt_len;
clnt_len = sizeof(clnt);
bytes = recvfrom (socket_desc, buffer, sizeof(buffer) - 1, 0, (struct sockaddr *)&clnt, &clnt_len);
if (bytes > 0) {
buffer[bytes] = 0;
if (verbose >= 2)
printf("Returned %d bytes from %s: %s\n", bytes, inet_ntoa(clnt.sin_addr), buffer);
if (strncmp(buffer, "quit", 4) == 0) {
if (verbose)
printf("Quit command received\n");
exit(0);
}
char *saveptr;
char *token;
char **args = malloc(MAX_COMMAND_ARGS * sizeof(char *));
int argn = 0;
for (int i=0; i < MAX_COMMAND_ARGS;i++)
args[i] = NULL;
token = strtok_r(buffer, " ", &saveptr);
while (token != NULL) {
args[argn] = strcpy(malloc(strlen(token) + 1), token);
argn++;
token = strtok_r(NULL, " ", &saveptr);
}
// Execute command here
char result[100];
do_command(handle, args, argn, result);
// Cleanup
for (int i=0; i < MAX_COMMAND_ARGS;i++) {
if (args[i] != NULL)
free(args[i]);
}
int retlen = strlen(result);
if (sendto(socket_desc, result, retlen,0, (struct sockaddr *) &clnt, clnt_len) != retlen) {
perror("Failed to send ack");
exit(1);
}
} else {
fprintf(stderr, "recvfrom returned %d\n", bytes);
}
}
close(socket_desc);
exit(0);
}
/* Device has been opened - perform the requested operation */
if (strcmp(argv[optind], "getregisters") == 0) {
getRegisters(handle);
} else if(strcmp(argv[optind], "getfreq") == 0){
double freq;
if (setByValue)
freq = readFrequencyByValue(handle);
else
freq = getFrequency(handle);
if (freq != 0.00)
printf("Frequency : %f (x %.2f)\n", freq / multiplier, multiplier);
#ifdef HAVE_LIBNCURSES
} else if (strcmp(argv[optind], "interactive") == 0) {
run_interactive(handle);
#endif
} else if ((strcmp(argv[optind], "ptt") == 0) && (argc >= optind + 1)) {
setPTT(handle, (strcmp(argv[optind+1],"on") == 0) ? 1 : 0);
} else if (strcmp(argv[optind], "calibrate") == 0) {
calibrate(handle);
} else if ((strcmp(argv[optind], "set") == 0) && (argc >= optind + 2)) {
if ((strcmp(argv[optind+1], "bpf_addr") == 0) && (argc >= optind + 3)) {
// set bpf_addr index value
setBPFAddress(handle, atoi(argv[optind+2]), atoi(argv[optind+3]));
} else if ((strcmp(argv[optind+1], "bpf_point") == 0) && (argc >= optind + 3)) {
// set bpf_point index (int) value (float)
setBPFCrossOver(handle, atoi(argv[optind+2]), atof(argv[optind+3]));
} else if ((strcmp(argv[optind+1], "bpf") == 0) && (argc >= optind + 2)) {
setBPF(handle, (strcmp(argv[optind+2],"on") == 0) ? 1 : 0);
} else if ((strcmp(argv[optind+1], "lpf") == 0) && (argc >= optind + 2)) {
setLPF(handle, (strcmp(argv[optind+2],"on") == 0) ? 1 : 0);
} else if ((strcmp(argv[optind+1], "lpf_addr") == 0) && (argc >= optind + 3)) {
// set bpf_addr index value
setBPFAddress(handle, atoi(argv[optind+2]), atoi(argv[optind+3]));
displayLPFs(handle);
} else if ((strcmp(argv[optind+1], "lpf_point") == 0) && (argc >= optind + 3)) {
// set lpf_point index (int) value (float)
setLPFCrossOver(handle, atoi(argv[optind+2]), atof(argv[optind+3]));
} else if (strcmp(argv[optind+1], "freq") == 0) {
if (setByValue)
setFreqByValue(handle, atof(argv[optind+2]));
else
setFrequency(handle, atof(argv[optind+2]));
} else if ((strcmp(argv[optind+1], "si570_addr") == 0) && (argc >= optind + 2)) {
setSi570Address(handle, atoi(argv[optind+2]));
} else if (strcmp(argv[optind+1], "si570_multiplier") == 0) {
int index = 0;
int valueIndex = optind+2;
// If there are 2 args after the variable name, one is index
if (argc >= optind + 3) {
index = atoi(argv[optind+2]);
valueIndex++;
}
double sub, mul;
readMultiplyLO(handle, index, &mul, &sub);
mul = atof(argv[valueIndex]);
setMultiplyLo(handle, index, mul, sub);
if (verbose)
printf("Set multiply [%d] to : %f\n", index, mul);
} else if (strcmp(argv[optind+1], "xtall") == 0) {
setXtallFrequency(handle, atof(argv[optind+2]));
} else if (strcmp(argv[optind+1], "startup") == 0) {
setStartupFrequency(handle, atof(argv[optind+2]));
} else {
usage(argv[0]);
exit(1);
}
} else if (strcmp(argv[optind], "status") == 0) {
printf("USB SerialID: %s\n", serialNumberString);
if (major >= 15) {
readFrequencyByValue(handle);
readStartupFreq(handle);
readXtallFreq(handle);
readSmoothTunePPM(handle);
if (major >= 16 || minor >= 12) {
readSi570Address(handle);
}
displayBands(handle);
displayLPFs(handle);
/*
if (major >= 16 || ((major >= 15) && (minor >= 12))) {
displayBands(handle);
} else if (minor >= 10) {
double sub, mul;
readMultiplyLO(handle, 0, &mul, &sub);
printf("LO Subtract : %f\n", sub);
printf("Multiply : %f\n", mul);
}
//displayBPFFilters(handle);
//displayLPFFilters(handle);*/
}
} else {
usage(argv[0]);
exit(1);
}
usb_close(handle);
return 0;
}