first shot at re-adding libusb-0.12 support
[debian/gnuradio] / usrp / host / lib / usrp_prims_common.cc
1 /* -*- c++ -*- */
2 /*
3  * Copyright 2003,2004,2006,2009 Free Software Foundation, Inc.
4  * 
5  * This file is part of GNU Radio
6  * 
7  * GNU Radio is free software; you can redistribute it and/or modify
8  * it under the terms of the GNU General Public License as published by
9  * the Free Software Foundation; either version 3, or (at your option)
10  * any later version.
11  * 
12  * GNU Radio is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  * GNU General Public License for more details.
16  * 
17  * You should have received a copy of the GNU General Public License
18  * along with GNU Radio; see the file COPYING.  If not, write to
19  * the Free Software Foundation, Inc., 51 Franklin Street,
20  * Boston, MA 02110-1301, USA.
21  */
22
23 #ifdef HAVE_CONFIG_H
24 #include "config.h"
25 #endif
26
27 #include "usrp/usrp_prims.h"
28 #include "usrp_commands.h"
29 #include "usrp_ids.h"
30 #include "usrp_i2c_addr.h"
31 #include "fpga_regs_common.h"
32 #include "fpga_regs_standard.h"
33 #include <errno.h>
34 #include <stdio.h>
35 #include <unistd.h>
36 #include <stdlib.h>
37 #include <string.h>
38 #include <ctype.h>
39 #include <time.h>               // FIXME should check with autoconf (nanosleep)
40 #include <algorithm>
41 #include <ad9862.h>
42 #include <assert.h>
43
44 #if 1
45 #include <usb.h>
46 #else
47 #include <libusb-1.0/libusb.h>
48 #endif
49
50 extern "C" {
51 #include "md5.h"
52 };
53
54 #define VERBOSE 0
55
56 using namespace ad9862;
57
58 static const int FIRMWARE_HASH_SLOT     = 0;
59 static const int FPGA_HASH_SLOT         = 1;
60
61 static const int hash_slot_addr[2] = {
62   USRP_HASH_SLOT_0_ADDR,
63   USRP_HASH_SLOT_1_ADDR
64 };
65
66 static const char *default_firmware_filename = "std.ihx";
67 static const char *default_fpga_filename     = "std_2rxhb_2tx.rbf";
68
69 /*
70  * Forward Declarations 
71  */
72
73 bool _usrp_configured_p (libusb_device *q);
74 libusb_device_handle *usrp_open_interface(libusb_device *dev,
75                                           int interface,
76                                           int altinterface);
77 bool write_internal_ram (libusb_device_handle *udh, unsigned char *buf,
78                          int start_addr, size_t len);
79 int write_cmd (libusb_device_handle *udh, int request, int value,
80                int index, unsigned char *bytes, int len);
81
82 #include "std_paths.h"
83 #include <stdio.h>
84
85 char *
86 find_file (const char *filename, int hw_rev)
87 {
88   const char **sp = std_paths;
89   static char path[1000];
90   char *s;
91
92   s = getenv("USRP_PATH");
93   if (s) {
94     snprintf (path, sizeof (path), "%s/rev%d/%s", s, hw_rev, filename);
95     if (access (path, R_OK) == 0)
96       return path;
97   }
98
99   while (*sp){
100     snprintf (path, sizeof (path), "%s/rev%d/%s", *sp, hw_rev, filename);
101     if (access (path, R_OK) == 0)
102       return path;
103     sp++;
104   }
105   return 0;
106 }
107
108 const char *
109 get_proto_filename(const std::string user_filename, const char *env_var, const char *def)
110 {
111   if (user_filename.length() != 0)
112     return user_filename.c_str();
113
114   char *s = getenv(env_var);
115   if (s && *s)
116     return s;
117
118   return def;
119 }
120
121
122 void power_down_9862s (libusb_device_handle *udh);
123
124 // ----------------------------------------------------------------
125
126 bool
127 usrp_usrp0_p (libusb_device *q)
128 {
129   return usrp_usrp_p (q) && usrp_hw_rev (q) == 0;
130 }
131
132 bool
133 usrp_usrp1_p (libusb_device *q)
134 {
135   return usrp_usrp_p (q) && usrp_hw_rev (q) == 1;
136 }
137
138 bool
139 usrp_usrp2_p (libusb_device *q)
140 {
141   return usrp_usrp_p (q) && usrp_hw_rev (q) == 2;
142 }
143
144
145 bool
146 usrp_unconfigured_usrp_p (libusb_device *q)
147 {
148   return usrp_usrp_p (q) && !_usrp_configured_p (q);
149 }
150
151 bool
152 usrp_configured_usrp_p (libusb_device *q)
153 {
154   return usrp_usrp_p (q) && _usrp_configured_p (q);
155 }
156
157 libusb_device_handle *
158 usrp_open_cmd_interface (libusb_device *dev)
159 {
160   return usrp_open_interface (dev, USRP_CMD_INTERFACE, USRP_CMD_ALTINTERFACE);
161 }
162
163 libusb_device_handle *
164 usrp_open_rx_interface (libusb_device *dev)
165 {
166   return usrp_open_interface (dev, USRP_RX_INTERFACE, USRP_RX_ALTINTERFACE);
167 }
168
169 libusb_device_handle *
170 usrp_open_tx_interface (libusb_device *dev)
171 {
172   return usrp_open_interface (dev, USRP_TX_INTERFACE, USRP_TX_ALTINTERFACE);
173 }
174
175
176 // ----------------------------------------------------------------
177 // whack the CPUCS register using the upload RAM vendor extension
178
179 static bool
180 reset_cpu (libusb_device_handle *udh, bool reset_p)
181 {
182   unsigned char v;
183
184   if (reset_p)
185     v = 1;              // hold processor in reset
186   else
187     v = 0;              // release reset
188
189   return write_internal_ram (udh, &v, 0xE600, 1);
190 }
191
192 // ----------------------------------------------------------------
193 // Load intel format file into cypress FX2 (8051)
194
195 static bool
196 _usrp_load_firmware (libusb_device_handle *udh, const char *filename,
197                      unsigned char hash[USRP_HASH_SIZE])
198 {
199   FILE  *f = fopen (filename, "ra");
200   if (f == 0){
201     perror (filename);
202     return false;
203   }
204
205   if (!reset_cpu (udh, true))   // hold CPU in reset while loading firmware
206     goto fail;
207
208   
209   char s[1024];
210   int length;
211   int addr;
212   int type;
213   unsigned char data[256];
214   unsigned char checksum, a;
215   unsigned int b;
216   int i;
217
218   while (!feof(f)){
219     fgets(s, sizeof (s), f); /* we should not use more than 263 bytes normally */
220     if(s[0]!=':'){
221       fprintf(stderr,"%s: invalid line: \"%s\"\n", filename, s);
222       goto fail;
223     }
224     sscanf(s+1, "%02x", &length);
225     sscanf(s+3, "%04x", &addr);
226     sscanf(s+7, "%02x", &type);
227
228     if(type==0){
229
230       a=length+(addr &0xff)+(addr>>8)+type;
231       for(i=0;i<length;i++){
232         sscanf (s+9+i*2,"%02x", &b);
233         data[i]=b;
234         a=a+data[i];
235       }
236
237       sscanf (s+9+length*2,"%02x", &b);
238       checksum=b;
239       if (((a+checksum)&0xff)!=0x00){
240         fprintf (stderr, "  ** Checksum failed: got 0x%02x versus 0x%02x\n", (-a)&0xff, checksum);
241         goto fail;
242       }
243       if (!write_internal_ram (udh, data, addr, length))
244         goto fail;
245     }
246     else if (type == 0x01){      // EOF
247       break;
248     }
249     else if (type == 0x02){
250       fprintf(stderr, "Extended address: whatever I do with it?\n");
251       fprintf (stderr, "%s: invalid line: \"%s\"\n", filename, s);
252       goto fail;
253     }
254   }
255
256   // we jam the hash value into the FX2 memory before letting
257   // the cpu out of reset.  When it comes out of reset it
258   // may renumerate which will invalidate udh.
259
260   if (!usrp_set_hash (udh, FIRMWARE_HASH_SLOT, hash))
261     fprintf (stderr, "usrp: failed to write firmware hash slot\n");
262
263   if (!reset_cpu (udh, false))          // take CPU out of reset
264     goto fail;
265
266   fclose (f);
267   return true;
268
269  fail:
270   fclose (f);
271   return false;
272 }
273
274 // ----------------------------------------------------------------
275 // load fpga
276
277 static bool
278 _usrp_load_fpga (libusb_device_handle *udh, const char *filename,
279                  unsigned char hash[USRP_HASH_SIZE])
280 {
281   bool ok = true;
282
283   FILE  *fp = fopen (filename, "rb");
284   if (fp == 0){
285     perror (filename);
286     return false;
287   }
288
289   unsigned char buf[MAX_EP0_PKTSIZE];   // 64 is max size of EP0 packet on FX2
290   int n;
291
292   usrp_set_led (udh, 1, 1);             // led 1 on
293
294
295   // reset FPGA (and on rev1 both AD9862's, thus killing clock)
296   usrp_set_fpga_reset (udh, 1);         // hold fpga in reset
297
298   if (write_cmd (udh, VRQ_FPGA_LOAD, 0, FL_BEGIN, 0, 0) != 0)
299     goto fail;
300   
301   while ((n = fread (buf, 1, sizeof (buf), fp)) > 0){
302     if (write_cmd (udh, VRQ_FPGA_LOAD, 0, FL_XFER, buf, n) != n)
303       goto fail;
304   }
305
306   if (write_cmd (udh, VRQ_FPGA_LOAD, 0, FL_END, 0, 0) != 0)
307     goto fail;
308   
309   fclose (fp);
310
311   if (!usrp_set_hash (udh, FPGA_HASH_SLOT, hash))
312     fprintf (stderr, "usrp: failed to write fpga hash slot\n");
313
314   // On the rev1 USRP, the {tx,rx}_{enable,reset} bits are
315   // controlled over the serial bus, and hence aren't observed until
316   // we've got a good fpga bitstream loaded.
317
318   usrp_set_fpga_reset (udh, 0);         // fpga out of master reset
319
320   // now these commands will work
321   
322   ok &= usrp_set_fpga_tx_enable (udh, 0);
323   ok &= usrp_set_fpga_rx_enable (udh, 0);
324
325   ok &= usrp_set_fpga_tx_reset (udh, 1);        // reset tx and rx paths
326   ok &= usrp_set_fpga_rx_reset (udh, 1);
327   ok &= usrp_set_fpga_tx_reset (udh, 0);        // reset tx and rx paths
328   ok &= usrp_set_fpga_rx_reset (udh, 0);
329
330   if (!ok)
331     fprintf (stderr, "usrp: failed to reset tx and/or rx path\n");
332
333   // Manually reset all regs except master control to zero.
334   // FIXME may want to remove this when we rework FPGA reset strategy.
335   // In the mean while, this gets us reproducible behavior.
336   for (int i = 0; i < FR_USER_0; i++){
337     if (i == FR_MASTER_CTRL)
338       continue;
339     usrp_write_fpga_reg(udh, i, 0);
340   }
341
342   power_down_9862s (udh);               // on the rev1, power these down!
343   usrp_set_led (udh, 1, 0);             // led 1 off
344
345   return true;
346
347  fail:
348   power_down_9862s (udh);               // on the rev1, power these down!
349   fclose (fp);
350   return false;
351 }
352
353 // ----------------------------------------------------------------
354
355 bool 
356 usrp_set_led (libusb_device_handle *udh, int which, bool on)
357 {
358   int r = write_cmd (udh, VRQ_SET_LED, on, which, 0, 0);
359
360   return r == 0;
361 }
362
363
364 bool
365 usrp_set_switch (libusb_device_handle *udh, int cmd_byte, bool on)
366 {
367   return write_cmd (udh, cmd_byte, on, 0, 0, 0) == 0;
368 }
369
370 bool
371 usrp1_fpga_write (libusb_device_handle *udh,
372                   int regno, int value)
373 {
374   // on the rev1 usrp, we use the generic spi_write interface
375
376   unsigned char buf[4];
377
378   buf[0] = (value >> 24) & 0xff;        // MSB first
379   buf[1] = (value >> 16) & 0xff;
380   buf[2] = (value >>  8) & 0xff;
381   buf[3] = (value >>  0) & 0xff;
382   
383   return usrp_spi_write (udh, 0x00 | (regno & 0x7f),
384                          SPI_ENABLE_FPGA,
385                          SPI_FMT_MSB | SPI_FMT_HDR_1,
386                          buf, sizeof (buf));
387 }
388
389 bool
390 usrp1_fpga_read (libusb_device_handle *udh,
391                  int regno, int *value)
392 {
393   *value = 0;
394   unsigned char buf[4];
395
396   bool ok = usrp_spi_read (udh, 0x80 | (regno & 0x7f),
397                            SPI_ENABLE_FPGA,
398                            SPI_FMT_MSB | SPI_FMT_HDR_1,
399                            buf, sizeof (buf));
400
401   if (ok)
402     *value = (buf[0] << 24) | (buf[1] << 16) | (buf[2] << 8) | buf[3];
403
404   return ok;
405 }
406
407
408 bool 
409 usrp_set_fpga_reset (libusb_device_handle *udh, bool on)
410 {
411   return usrp_set_switch (udh, VRQ_FPGA_SET_RESET, on);
412 }
413
414 bool 
415 usrp_set_fpga_tx_enable (libusb_device_handle *udh, bool on)
416 {
417   return usrp_set_switch (udh, VRQ_FPGA_SET_TX_ENABLE, on);
418 }
419
420 bool 
421 usrp_set_fpga_rx_enable (libusb_device_handle *udh, bool on)
422 {
423   return usrp_set_switch (udh, VRQ_FPGA_SET_RX_ENABLE, on);
424 }
425
426 bool 
427 usrp_set_fpga_tx_reset (libusb_device_handle *udh, bool on)
428 {
429   return usrp_set_switch (udh, VRQ_FPGA_SET_TX_RESET, on);
430 }
431
432 bool 
433 usrp_set_fpga_rx_reset (libusb_device_handle *udh, bool on)
434 {
435   return usrp_set_switch (udh, VRQ_FPGA_SET_RX_RESET, on);
436 }
437
438
439 // ----------------------------------------------------------------
440 // conditional load stuff
441
442 static bool
443 compute_hash (const char *filename, unsigned char hash[USRP_HASH_SIZE])
444 {
445   assert (USRP_HASH_SIZE == 16);
446   memset (hash, 0, USRP_HASH_SIZE);
447
448   FILE *fp = fopen (filename, "rb");
449   if (fp == 0){
450     perror (filename);
451     return false;
452   }
453   int r = md5_stream (fp, hash);
454   fclose (fp);
455   
456   return r == 0;
457 }
458
459 static usrp_load_status_t
460 usrp_conditionally_load_something (libusb_device_handle *udh,
461                                    const char *filename,
462                                    bool force,
463                                    int slot,
464                                    bool loader (libusb_device_handle *,
465                                                 const char *,
466                                                 unsigned char [USRP_HASH_SIZE]))
467 {
468   unsigned char file_hash[USRP_HASH_SIZE];
469   unsigned char usrp_hash[USRP_HASH_SIZE];
470   
471   if (access (filename, R_OK) != 0){
472     perror (filename);
473     return ULS_ERROR;
474   }
475
476   if (!compute_hash (filename, file_hash))
477     return ULS_ERROR;
478
479   if (!force
480       && usrp_get_hash (udh, slot, usrp_hash)
481       && memcmp (file_hash, usrp_hash, USRP_HASH_SIZE) == 0)
482     return ULS_ALREADY_LOADED;
483
484   bool r = loader (udh, filename, file_hash);
485
486   if (!r)
487     return ULS_ERROR;
488
489   return ULS_OK;
490 }
491
492 usrp_load_status_t
493 usrp_load_firmware (libusb_device_handle *udh,
494                     const char *filename,
495                     bool force)
496 {
497   return usrp_conditionally_load_something (udh, filename, force,
498                                             FIRMWARE_HASH_SLOT,
499                                             _usrp_load_firmware);
500 }
501
502 usrp_load_status_t
503 usrp_load_fpga (libusb_device_handle *udh,
504                 const char *filename,
505                 bool force)
506 {
507   return usrp_conditionally_load_something (udh, filename, force,
508                                             FPGA_HASH_SLOT,
509                                             _usrp_load_fpga);
510 }
511
512 bool
513 our_nanosleep (const struct timespec *delay)
514 {
515   struct timespec       new_delay = *delay;
516   struct timespec       remainder;
517
518   while (1){
519     int r = nanosleep (&new_delay, &remainder);
520     if (r == 0)
521       return true;
522     if (errno == EINTR)
523       new_delay = remainder;
524     else {
525       perror ("nanosleep");
526       return false;
527     }
528   }
529 }
530
531 static bool
532 mdelay (int millisecs)
533 {
534   struct timespec       ts;
535   ts.tv_sec = millisecs / 1000;
536   ts.tv_nsec = (millisecs - (1000 * ts.tv_sec)) * 1000000;
537   return our_nanosleep (&ts);
538 }
539
540 void
541 load_status_msg (usrp_load_status_t s, const char *type, const char *filename)
542 {
543   char *e = getenv("USRP_VERBOSE");
544   bool verbose = e != 0;
545   
546   switch (s){
547   case ULS_ERROR:
548     fprintf (stderr, "usrp: failed to load %s %s.\n", type, filename);
549     break;
550     
551   case ULS_ALREADY_LOADED:
552     if (verbose)
553       fprintf (stderr, "usrp: %s %s already loaded.\n", type, filename);
554     break;
555
556   case ULS_OK:
557     if (verbose)
558       fprintf (stderr, "usrp: %s %s loaded successfully.\n", type, filename);
559     break;
560   }
561 }
562
563 bool
564 _usrp_get_status (libusb_device_handle *udh, int which, bool *trouble)
565 {
566   unsigned char status;
567   *trouble = true;
568   
569   if (write_cmd (udh, VRQ_GET_STATUS, 0, which,
570                  &status, sizeof (status)) != sizeof (status))
571     return false;
572
573   *trouble = status;
574   return true;
575 }
576
577 bool
578 usrp_check_rx_overrun (libusb_device_handle *udh, bool *overrun_p)
579 {
580   return _usrp_get_status (udh, GS_RX_OVERRUN, overrun_p);
581 }
582
583 bool
584 usrp_check_tx_underrun (libusb_device_handle *udh, bool *underrun_p)
585 {
586   return _usrp_get_status (udh, GS_TX_UNDERRUN, underrun_p);
587 }
588
589
590 bool
591 usrp_i2c_write (libusb_device_handle *udh, int i2c_addr,
592                 const void *buf, int len)
593 {
594   if (len < 1 || len > MAX_EP0_PKTSIZE)
595     return false;
596
597   return write_cmd (udh, VRQ_I2C_WRITE, i2c_addr, 0,
598                     (unsigned char *) buf, len) == len;
599 }
600
601
602 bool
603 usrp_i2c_read (libusb_device_handle *udh, int i2c_addr,
604                void *buf, int len)
605 {
606   if (len < 1 || len > MAX_EP0_PKTSIZE)
607     return false;
608
609   return write_cmd (udh, VRQ_I2C_READ, i2c_addr, 0,
610                     (unsigned char *) buf, len) == len;
611 }
612
613 bool
614 usrp_spi_write (libusb_device_handle *udh,
615                 int optional_header, int enables, int format,
616                 const void *buf, int len)
617 {
618   if (len < 0 || len > MAX_EP0_PKTSIZE)
619     return false;
620
621   return write_cmd (udh, VRQ_SPI_WRITE,
622                     optional_header,
623                     ((enables & 0xff) << 8) | (format & 0xff),
624                     (unsigned char *) buf, len) == len;
625 }
626
627
628 bool
629 usrp_spi_read (libusb_device_handle *udh,
630                int optional_header, int enables, int format,
631                void *buf, int len)
632 {
633   if (len < 0 || len > MAX_EP0_PKTSIZE)
634     return false;
635
636   return write_cmd (udh, VRQ_SPI_READ,
637                     optional_header,
638                     ((enables & 0xff) << 8) | (format & 0xff),
639                     (unsigned char *) buf, len) == len;
640 }
641
642 bool
643 usrp_9862_write (libusb_device_handle *udh, int which_codec,
644                  int regno, int value)
645 {
646   if (0)
647     fprintf (stderr, "usrp_9862_write which = %d, reg = %2d, val = %3d (0x%02x)\n",
648              which_codec, regno, value, value);
649
650   unsigned char buf[1];
651
652   buf[0] = value;
653   
654   return usrp_spi_write (udh, 0x00 | (regno & 0x3f),
655                          which_codec == 0 ? SPI_ENABLE_CODEC_A : SPI_ENABLE_CODEC_B,
656                          SPI_FMT_MSB | SPI_FMT_HDR_1,
657                          buf, 1);
658 }
659
660 bool
661 usrp_9862_read (libusb_device_handle *udh, int which_codec,
662                 int regno, unsigned char *value)
663 {
664   return usrp_spi_read (udh, 0x80 | (regno & 0x3f),
665                         which_codec == 0 ? SPI_ENABLE_CODEC_A : SPI_ENABLE_CODEC_B,
666                         SPI_FMT_MSB | SPI_FMT_HDR_1,
667                         value, 1);
668 }
669
670 bool
671 usrp_9862_write_many (libusb_device_handle *udh,
672                       int which_codec,
673                       const unsigned char *buf,
674                       int len)
675 {
676   if (len & 0x1)
677     return false;               // must be even
678
679   bool result = true;
680
681   while (len > 0){
682     result &= usrp_9862_write (udh, which_codec, buf[0], buf[1]);
683     len -= 2;
684     buf += 2;
685   }
686
687   return result;
688 }
689
690
691 bool
692 usrp_9862_write_many_all (libusb_device_handle *udh,
693                            const unsigned char *buf, int len)
694 {
695   // FIXME handle 2/2 and 4/4 versions
696
697   bool result;
698   result  = usrp_9862_write_many (udh, 0, buf, len);
699   result &= usrp_9862_write_many (udh, 1, buf, len);
700   return result;
701 }
702
703
704 static const int EEPROM_PAGESIZE = 16;
705
706 bool
707 usrp_eeprom_write (libusb_device_handle *udh, int i2c_addr,
708                    int eeprom_offset, const void *buf, int len)
709 {
710   unsigned char cmd[2];
711   const unsigned char *p = (unsigned char *) buf;
712   
713   // The simplest thing that could possibly work:
714   //   all writes are single byte writes.
715   //
716   // We could speed this up using the page write feature,
717   // but we write so infrequently, why bother...
718
719   while (len-- > 0){
720     cmd[0] = eeprom_offset++;
721     cmd[1] = *p++;
722     bool r = usrp_i2c_write (udh, i2c_addr, cmd, sizeof (cmd));
723     mdelay (10);                // delay 10ms worst case write time
724     if (!r)
725       return false;
726   }
727   
728   return true;
729 }
730
731 bool
732 usrp_eeprom_read (libusb_device_handle *udh, int i2c_addr,
733                   int eeprom_offset, void *buf, int len)
734 {
735   unsigned char *p = (unsigned char *) buf;
736
737   // We setup a random read by first doing a "zero byte write".
738   // Writes carry an address.  Reads use an implicit address.
739
740   unsigned char cmd[1];
741   cmd[0] = eeprom_offset;
742   if (!usrp_i2c_write (udh, i2c_addr, cmd, sizeof (cmd)))
743     return false;
744
745   while (len > 0){
746     int n = std::min (len, MAX_EP0_PKTSIZE);
747     if (!usrp_i2c_read (udh, i2c_addr, p, n))
748       return false;
749     len -= n;
750     p += n;
751   }
752   return true;
753 }
754  
755 // ----------------------------------------------------------------
756
757 static bool
758 slot_to_codec (int slot, int *which_codec)
759 {
760   *which_codec = 0;
761   
762   switch (slot){
763   case SLOT_TX_A:
764   case SLOT_RX_A:
765     *which_codec = 0;
766     break;
767
768   case SLOT_TX_B:
769   case SLOT_RX_B:
770     *which_codec = 1;
771     break;
772
773   default:
774     fprintf (stderr, "usrp_prims:slot_to_codec: invalid slot = %d\n", slot);
775     return false;
776   }
777   return true;
778 }
779
780 static bool
781 tx_slot_p (int slot)
782 {
783   switch (slot){
784   case SLOT_TX_A:
785   case SLOT_TX_B:
786     return true;
787
788   default:
789     return false;
790   }
791 }
792
793 bool
794 usrp_write_aux_dac (libusb_device_handle *udh, int slot,
795                     int which_dac, int value)
796 {
797   int which_codec;
798   
799   if (!slot_to_codec (slot, &which_codec))
800     return false;
801
802   if (!(0 <= which_dac && which_dac < 4)){
803     fprintf (stderr, "usrp_write_aux_dac: invalid dac = %d\n", which_dac);
804     return false;
805   }
806
807   value &= 0x0fff;      // mask to 12-bits
808   
809   if (which_dac == 3){
810     // dac 3 is really 12-bits.  Use value as is.
811     bool r = true;
812     r &= usrp_9862_write (udh, which_codec, 43, (value >> 4));       // most sig
813     r &= usrp_9862_write (udh, which_codec, 42, (value & 0xf) << 4); // least sig
814     return r;
815   }
816   else {
817     // dac 0, 1, and 2 are really 8 bits.  
818     value = value >> 4;         // shift value appropriately
819     return usrp_9862_write (udh, which_codec, 36 + which_dac, value);
820   }
821 }
822
823
824 bool
825 usrp_read_aux_adc (libusb_device_handle *udh, int slot,
826                    int which_adc, int *value)
827 {
828   *value = 0;
829   int   which_codec;
830
831   if (!slot_to_codec (slot, &which_codec))
832     return false;
833
834   if (!(0 <= which_codec && which_codec < 2)){
835     fprintf (stderr, "usrp_read_aux_adc: invalid adc = %d\n", which_adc);
836     return false;
837   }
838
839   unsigned char aux_adc_control =
840     AUX_ADC_CTRL_REFSEL_A               // on chip reference
841     | AUX_ADC_CTRL_REFSEL_B;            // on chip reference
842
843   int   rd_reg = 26;    // base address of two regs to read for result
844   
845   // program the ADC mux bits
846   if (tx_slot_p (slot))
847     aux_adc_control |= AUX_ADC_CTRL_SELECT_A2 | AUX_ADC_CTRL_SELECT_B2;
848   else {
849     rd_reg += 2;
850     aux_adc_control |= AUX_ADC_CTRL_SELECT_A1 | AUX_ADC_CTRL_SELECT_B1;
851   }
852   
853   // I'm not sure if we can set the mux and issue a start conversion
854   // in the same cycle, so let's do them one at a time.
855
856   usrp_9862_write (udh, which_codec, 34, aux_adc_control);
857
858   if (which_adc == 0)
859     aux_adc_control |= AUX_ADC_CTRL_START_A;
860   else {
861     rd_reg += 4;
862     aux_adc_control |= AUX_ADC_CTRL_START_B;
863   }
864
865   // start the conversion
866   usrp_9862_write (udh, which_codec, 34, aux_adc_control);
867
868   // read the 10-bit result back
869   unsigned char v_lo = 0;
870   unsigned char v_hi = 0;
871   bool r = usrp_9862_read (udh, which_codec, rd_reg, &v_lo);
872   r &= usrp_9862_read (udh, which_codec, rd_reg + 1, &v_hi);
873
874   if (r)
875     *value = ((v_hi << 2) | ((v_lo >> 6) & 0x3)) << 2;  // format as 12-bit
876   
877   return r;
878 }
879
880 // ----------------------------------------------------------------
881
882 static int slot_to_i2c_addr (int slot)
883 {
884   switch (slot){
885   case SLOT_TX_A:       return I2C_ADDR_TX_A;
886   case SLOT_RX_A:       return I2C_ADDR_RX_A;
887   case SLOT_TX_B:       return I2C_ADDR_TX_B;
888   case SLOT_RX_B:       return I2C_ADDR_RX_B;
889   default:              return -1;
890   }
891 }
892
893 static void
894 set_chksum (unsigned char *buf)
895 {
896   int sum = 0;
897   unsigned int i;
898   for (i = 0; i < DB_EEPROM_CLEN - 1; i++)
899     sum += buf[i];
900   buf[i] = -sum;
901 }
902
903 static usrp_dbeeprom_status_t
904 read_dboard_eeprom (libusb_device_handle *udh,
905                     int slot_id, unsigned char *buf)
906 {
907   int i2c_addr = slot_to_i2c_addr (slot_id);
908   if (i2c_addr == -1)
909     return UDBE_BAD_SLOT;
910
911   if (!usrp_eeprom_read (udh, i2c_addr, 0, buf, DB_EEPROM_CLEN))
912     return UDBE_NO_EEPROM;
913
914   if (buf[DB_EEPROM_MAGIC] != DB_EEPROM_MAGIC_VALUE)
915     return UDBE_INVALID_EEPROM;
916
917   int sum = 0;
918   for (unsigned int i = 0; i < DB_EEPROM_CLEN; i++)
919     sum += buf[i];
920
921   if ((sum & 0xff) != 0)
922     return UDBE_INVALID_EEPROM;
923
924   return UDBE_OK;
925 }
926
927 usrp_dbeeprom_status_t
928 usrp_read_dboard_eeprom (libusb_device_handle *udh,
929                          int slot_id, usrp_dboard_eeprom *eeprom)
930 {
931   unsigned char buf[DB_EEPROM_CLEN];
932
933   memset (eeprom, 0, sizeof (*eeprom));
934
935   usrp_dbeeprom_status_t s = read_dboard_eeprom (udh, slot_id, buf);
936   if (s != UDBE_OK)
937     return s;
938
939   eeprom->id = (buf[DB_EEPROM_ID_MSB] << 8) | buf[DB_EEPROM_ID_LSB];
940   eeprom->oe = (buf[DB_EEPROM_OE_MSB] << 8) | buf[DB_EEPROM_OE_LSB];
941   eeprom->offset[0] = (buf[DB_EEPROM_OFFSET_0_MSB] << 8) | buf[DB_EEPROM_OFFSET_0_LSB];
942   eeprom->offset[1] = (buf[DB_EEPROM_OFFSET_1_MSB] << 8) | buf[DB_EEPROM_OFFSET_1_LSB];
943
944   return UDBE_OK;
945 }
946
947 bool
948 usrp_write_dboard_offsets (libusb_device_handle *udh, int slot_id,
949                            short offset0, short offset1)
950 {
951   unsigned char buf[DB_EEPROM_CLEN];
952
953   usrp_dbeeprom_status_t s = read_dboard_eeprom (udh, slot_id, buf);
954   if (s != UDBE_OK)
955     return false;
956
957   buf[DB_EEPROM_OFFSET_0_LSB] = (offset0 >> 0) & 0xff;
958   buf[DB_EEPROM_OFFSET_0_MSB] = (offset0 >> 8) & 0xff;
959   buf[DB_EEPROM_OFFSET_1_LSB] = (offset1 >> 0) & 0xff;
960   buf[DB_EEPROM_OFFSET_1_MSB] = (offset1 >> 8) & 0xff;
961   set_chksum (buf);
962
963   return usrp_eeprom_write (udh, slot_to_i2c_addr (slot_id),
964                             0, buf, sizeof (buf));
965 }