-
Notifications
You must be signed in to change notification settings - Fork 1
/
ozyio.c
executable file
·537 lines (447 loc) · 12.8 KB
/
ozyio.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
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
/**
* @file ozyio.c
* @brief USB I/O with Ozy
* @author John Melton, G0ORX/N6LYT
* @version 0.1
* @date 2009-10-13
*/
/* Copyright (C)
* 2009 - John Melton, G0ORX/N6LYT
* 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., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*
*/
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h> // tolower
#include <libusb-1.0/libusb.h>
#include "ozyio.h"
#define OZY_PID (0x0007)
#define OZY_VID (0xfffe)
#define VRQ_SDR1K_CTL 0x0d
#define SDR1KCTRL_READ_VERSION 0x7
#define VRT_VENDOR_IN 0xC0
#define VENDOR_REQ_TYPE_IN 0xc0
#define VENDOR_REQ_TYPE_OUT 0x40
#define VRQ_I2C_WRITE (0x08)
#define VRT_VENDOR_OUT (0x40)
#define VENDOR_REQ_SET_LED 0x01
#define VENDOR_REQ_FPGA_LOAD 0x02
#define FL_BEGIN 0
#define FL_XFER 1
#define FL_END 2
#define OZY_IO_TIMEOUT 500
#define MAX_EPO_PACKET_SIZE 64
static int init=0;
extern unsigned char penny_fw, mercury_fw;
extern unsigned short penny_fp, penny_rp, penny_alc;
extern int adc_overflow;
void writepenny(unsigned char mode);
static libusb_device_handle* ozy_handle;
//static libusb_context* context;
int ozy_open(void) {
int rc;
if(init==0) {
rc=libusb_init(NULL);
if(rc<0) {
fprintf(stderr,"libusb_init failed: %d\n",rc);
return rc;
}
init=1;
}
ozy_handle=libusb_open_device_with_vid_pid(NULL, OZY_VID, OZY_PID);
if(ozy_handle==NULL) {
fprintf(stderr,"libusbio: cannot find ozy device\n");
return -1;
}
rc=libusb_detach_kernel_driver(ozy_handle,0);
if(rc<0) {
// fprintf(stderr,"libusb_detach_kernel_driver failed: %d\n",rc);
}
rc=libusb_claim_interface(ozy_handle,0);
if(rc<0) {
fprintf(stderr,"libusb_claim_interface failed: %d\n",rc);
return rc;
}
return 0;
}
int ozy_close() {
int rc;
rc=libusb_attach_kernel_driver(ozy_handle,0);
if(rc<0) {
// fprintf(stderr,"libusb_attach_kernel_driver failed: %d\n",rc);
}
libusb_close(ozy_handle);
return 0;
}
int ozy_get_firmware_string(unsigned char* buffer,int buffer_size) {
int rc;
rc=libusb_control_transfer(ozy_handle, VRT_VENDOR_IN, VRQ_SDR1K_CTL, SDR1KCTRL_READ_VERSION, 0, buffer, buffer_size, OZY_IO_TIMEOUT);
if(rc<0) {
fprintf(stderr,"ozy__get_firmware_string failed: %d\n",rc);
return rc;
}
buffer[rc]='\0';
return 0;
}
int ozy_write(int ep,unsigned char* buffer,int buffer_size) {
int rc;
int bytes;
rc = libusb_bulk_transfer(ozy_handle,(unsigned char)ep,buffer,buffer_size,&bytes,OZY_IO_TIMEOUT);
if(rc==0) {
rc=bytes;
}
return rc;
}
int ozy_read(int ep,unsigned char* buffer,int buffer_size) {
int rc;
int bytes;
rc = libusb_bulk_transfer(ozy_handle,(unsigned char)ep,buffer,buffer_size,&bytes,OZY_IO_TIMEOUT);
if(rc==0) {
rc=bytes;
}
return rc;
}
int ozy_write_ram(int fx2_start_addr, unsigned char *bufp, int count) {
int pkt_size = MAX_EPO_PACKET_SIZE;
int len = count;
int bytes_written = 0;
int addr;
int bytes_written_this_write;
int nsize;
for ( addr = fx2_start_addr; addr < fx2_start_addr + len; addr += pkt_size, bufp += pkt_size ) {
nsize = len + fx2_start_addr - addr;
if ( nsize > pkt_size ) nsize = pkt_size;
bytes_written_this_write = libusb_control_transfer(ozy_handle, 0x40, 0xa0, addr, 0, bufp, nsize, OZY_IO_TIMEOUT);
if ( bytes_written_this_write >= 0 ) {
bytes_written += bytes_written_this_write;
}
else {
return bytes_written_this_write;
}
}
return bytes_written;
}
int ozy_reset_cpu(int reset) {
unsigned char write_buf;
if ( reset ) write_buf = 1;
else write_buf = 0;
if ( ozy_write_ram(0xe600, &write_buf, 1) != 1 ) return 0;
else return 1;
}
static unsigned int hexitToUInt(char c) {
c = tolower(c);
if ( c >= '0' && c <= '9' ) {
return c - '0';
}
else if ( c >= 'a' && c <= 'f' ) {
return 10 + (c - 'a');
}
return 0;
}
static int ishexit(unsigned char c) {
c = tolower(c);
if ( c >= '0' && c <= '9' ) return 1;
if ( c >= 'a' && c <= 'f' ) return 1;
return 0;
}
static int hexitsToUInt(char *p, int count) {
unsigned int result = 0;
int i;
char c;
unsigned int this_hex;
for ( i = 0; i < count; i++ ) {
c = *p;
++p;
if ( !ishexit(c) ) {
return -1;
}
this_hex = hexitToUInt(c);
result *= 16;
result += this_hex;
}
return result;
}
int ozy_load_firmware(char *fnamep) {
FILE *ifile;
int linecount = 0;
int length;
int addr;
int type;
char readbuf[1030];
unsigned char wbuf[256];
unsigned char my_cksum;
unsigned char cksum;
int this_val;
int i;
fprintf(stderr,"loading ozy firmware: %s\n",fnamep);
ifile = fopen(fnamep, "r");
if ( ifile == NULL ) {
fprintf(stderr, "Could not open: \'%s\'\n", fnamep);
return 0;
}
while ( fgets(readbuf, sizeof(readbuf), ifile) != NULL ) {
++linecount;
if ( readbuf[0] != ':' ) {
fprintf(stderr, "ozy_upload_firmware: bad record\n");
return 0;
}
length = hexitsToUInt(readbuf+1, 2);
addr = hexitsToUInt(readbuf+3, 4);
type = hexitsToUInt(readbuf+7, 2);
if ( length < 0 || addr < 0 || type < 0 ) {
fprintf(stderr, "ozy_upload_firmware: bad length, addr or type\n");
return 0;
}
switch ( type ) {
case 0: /* record */
my_cksum = (unsigned char)(length + (addr & 0xff) + ((addr >> 8) + type));
for ( i = 0; i < length; i++ ) {
this_val = hexitsToUInt(readbuf+9+(i*2),2);
#if 0
printf("i: %d val: 0x%02x\n", i, this_val);
#endif
if ( this_val < 0 ) {
fprintf(stderr, "ozy_upload_firmware: bad record data\n");
return 0;
}
wbuf[i] = (unsigned char)this_val;
my_cksum += wbuf[i];
}
this_val = hexitsToUInt(readbuf+9+(length*2),2);
if ( this_val < 0 ) {
fprintf(stderr, "ozy_upload_firmware: bad checksum data\n");
return 0;
}
cksum = (unsigned char)this_val;
#if 0
printf("\n%s", readbuf);
printf("len: %d (0x%02x) addr: 0x%04x mychk: 0x%02x chk: 0x%02x",
length, length, addr, my_cksum, cksum);
#endif
if ( ((cksum + my_cksum) & 0xff) != 0 ) {
fprintf(stderr, "ozy_upload_firmware: bad checksum\n");
return 0;
}
if ( ozy_write_ram(addr, wbuf, length) < 1 ) {
fprintf(stderr, "ozy_upload_firmware: bad write\n");
return 0;
}
break;
case 1: /* EOF */
break;
default: /* invalid */
fprintf(stderr, "ozy_upload_firmware: invalid type\n");
return 0;
}
}
// fprintf(stderr, "ozy_upload_firmware: Processed %d lines.\n", linecount);
return linecount;
}
int ozy_set_led(int which, int on) {
int rc;
int val;
if ( on ) {
val = 1;
}
else {
val = 0;
}
rc = libusb_control_transfer(ozy_handle, VENDOR_REQ_TYPE_OUT, VENDOR_REQ_SET_LED,
val, which, NULL, 0, OZY_IO_TIMEOUT);
if ( rc < 0 ) {
return 0;
}
return 1;
}
int ozy_load_fpga(char *rbf_fnamep) {
FILE *rbffile;
unsigned char buf[MAX_EPO_PACKET_SIZE];
int bytes_read;
int total_bytes_xferd = 0;
int rc;
fprintf(stderr,"loading ozy fpga: %s\n",rbf_fnamep);
rbffile = fopen(rbf_fnamep, "rb");
if ( rbffile == NULL ) {
fprintf(stderr, "Failed to open: \'%s\'\n", rbf_fnamep);
return 0;
}
rc = libusb_control_transfer(ozy_handle, VENDOR_REQ_TYPE_OUT, VENDOR_REQ_FPGA_LOAD,
0, FL_BEGIN, NULL, 0, OZY_IO_TIMEOUT);
if ( rc < 0 ) {
fprintf(stderr, "ozy_load_fpga: failed @ FL_BEGIN rc=%d\n",rc);
fclose(rbffile);
return 0;
}
/*
* read the rbf and send it over the wire, 64 bytes at a time
*/
while ( (bytes_read = fread(buf, 1, sizeof(buf), rbffile)) > 0 ) {
rc = libusb_control_transfer(ozy_handle, VENDOR_REQ_TYPE_OUT, VENDOR_REQ_FPGA_LOAD,
0, FL_XFER, buf, bytes_read, OZY_IO_TIMEOUT);
total_bytes_xferd += bytes_read;
if ( rc < 0 ) {
fprintf(stderr, "ozy_load_fpga: failed @ FL_XFER\n");
fclose(rbffile);
return 0;
}
}
printf("%d bytes transferred.\n", total_bytes_xferd);
fclose(rbffile);
rc = libusb_control_transfer(ozy_handle, VENDOR_REQ_TYPE_OUT, VENDOR_REQ_FPGA_LOAD,
0, FL_END, NULL, 0, OZY_IO_TIMEOUT);
if ( rc < 0 ) {
fprintf(stderr, "ozy_load_fpga: failed @ FL_END\n");
return 0;
}
return 1;
}
int ozy_i2c_write(unsigned char* buffer,int buffer_size, unsigned char cmd) {
int rc;
rc=libusb_control_transfer(ozy_handle, VRT_VENDOR_OUT, VRQ_I2C_WRITE, cmd, 0, buffer, buffer_size, OZY_IO_TIMEOUT);
if(rc<0) {
fprintf(stderr,"ozy_i2c_write failed: %d\n",rc);
return rc;
}
return rc;
}
int ozy_i2c_read(unsigned char* buffer,int buffer_size, unsigned char cmd) {
int rc;
rc=libusb_control_transfer(ozy_handle, VRT_VENDOR_IN, VRQ_I2C_READ, cmd, 0, buffer, buffer_size, OZY_IO_TIMEOUT);
if(rc<0) {
fprintf(stderr,"ozy_i2c_read failed: %d\n",rc);
return rc;
}
return rc;
}
void ozy_i2c_readpwr(int addr) {
int rc = 0;
unsigned char buffer[8];
switch (addr) {
case I2C_PENNY_ALC:
rc = ozy_i2c_read(buffer,2,I2C_PENNY_ALC);
if(rc<0) {
perror("ozy_i2c_init4: failed");
exit(1);
}
penny_alc = (buffer[0] << 8) + buffer[1];
break;
case I2C_PENNY_FWD:
rc = ozy_i2c_read(buffer,2,I2C_PENNY_FWD);
if(rc<0) {
perror("ozy_i2c_init5: failed");
exit(1);
}
penny_fp = (buffer[0] << 8) + buffer[1];
break;
case I2C_PENNY_REV:
rc = ozy_i2c_read(buffer,2,I2C_PENNY_REV);
if(rc<0) {
perror("ozy_i2c_init6: failed");
exit(1);
}
penny_rp = (buffer[0] << 8) + buffer[1];
break;
case I2C_ADC_OFS:
// adc overload
rc = ozy_i2c_read(buffer,2,I2C_ADC_OFS); // adc1 overflow status
if(rc<0) {
perror("ozy_i2c_init6: failed");
exit(1);
}
if (buffer[0] == 0) { // its overloaded
adc_overflow = 2000; // counts down to give hold time to client
}
break;
default:
break;
}
}
void ozy_i2c_readvars() {
int rc = 0;
unsigned char buffer[8];
fprintf(stderr,"ozy_i2c_init: starting\n");
if (rc != 0) {
fprintf(stderr,"ozy_i2c_init: failed open %d\n",rc);
}
rc = ozy_i2c_read(buffer,2,I2C_MERC1_FW);
if(rc<0) {
perror("ozy_i2c_init2: failed");
exit(1);
}
mercury_fw = buffer[1];
fprintf(stderr,"mercury firmware=%d\n",(int)buffer[1]);
rc = ozy_i2c_read(buffer,2,I2C_PENNY_FW);
if(rc<0) {
perror("ozy_i2c_init3: failed");
exit(1);
}
penny_fw = buffer[1];
fprintf(stderr,"penny firmware=%d\n",(int)buffer[1]);
writepenny((unsigned char)1);
}
void writepenny(unsigned char mode)
{
unsigned char Penny_TLV320[2];
unsigned char *Penny_TLV320_data; // 16 byte
int x;
// This is used to set the MicGain and Line in when Ozy/Magister is used
// The I2C settings are as follows:
// For mic input and boost on/off
// 1E 00 - Reset chip
// 12 01 - set digital interface active
// 08 15 - D/A on, mic input, mic 20dB boost
// 08 14 - ditto but no mic boost
// 0C 00 - All chip power on
// 0E 02 - Slave, 16 bit, I2S
// 10 00 - 48k, Normal mode
// 0A 00 - turn D/A mute off
// 00 00 - set Line in gain to 0
// For line input
// 1E 00 - Reset chip
// 12 01 - set digital interface active
// 08 10 - D/A on, line input
// 0C 00 - All chip power on
// 0E 02 - Slave, 16 bit, I2S
// 10 00 - 48k, Normal mode
// 0A 00 - turn D/A mute off
// 00 00 - set Line in gain to 0
fprintf(stderr,"write Penny\n");
// update mic gain on Penny or PennyLane TLV320
// need to select the config data depending on the Mic Gain (20dB) selected
if (mode == 0x01)
Penny_TLV320_data = (unsigned char []) { 0x1e, 0x00, 0x12, 0x01, 0x08, 0x15, 0x0c, 0x00, 0x0e, 0x02, 0x10, 0x00, 0x0a, 0x00, 0x00, 0x00 }; // mic in 20db gain
else if (mode & 2) // line in
Penny_TLV320_data = (unsigned char []) { 0x1e, 0x00, 0x12, 0x01, 0x08, 0x10, 0x0c, 0x00, 0x0e, 0x02, 0x10, 0x00, 0x0a, 0x00, 0x00, 0x00 }; // line in
else
Penny_TLV320_data = (unsigned char []) { 0x1e, 0x00, 0x12, 0x01, 0x08, 0x14, 0x0c, 0x00, 0x0e, 0x02, 0x10, 0x00, 0x0a, 0x00, 0x00, 0x00 }; // mic in not 20db gain
// // set the I2C interface speed to 400kHZ
// if (!(OZY.Set_I2C_Speed(hdev, 1)))
// {
// fprintf(stderr,"Unable to set I2C speed to 400kHz", "System Error!", MessageBoxButtons.OK, MessageBoxIcon.Error);
// return;
// send the configuration data to the TLV320 on Penelope or PennyLane
for (x = 0; x < 16; x += 2)
{
Penny_TLV320[0] = Penny_TLV320_data[x]; Penny_TLV320[1] = Penny_TLV320_data[x + 1];
int ozy_write_i2c(int ep,unsigned char* buffer,int buffer_size);
if (!(ozy_i2c_write(Penny_TLV320,2, 0x1b)))
{
fprintf(stderr,"Unable to configure TLV320 on Penelope via I2C\n");
// break out of the configuration loop
break;
}
}
fprintf(stderr,"write Penny done..\n");
}