3 * Copyright 2004,2006 Free Software Foundation, Inc.
5 * This file is part of GNU Radio
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 2, or (at your option)
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.
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.
24 #include "usrp_rev2_regs.h"
27 setup_enables (unsigned char enables)
29 // Software eanbles are active high.
30 // Hardware enables are active low.
32 // Uhh, the CODECs are active low, but the FPGA is active high...
33 enables ^= SPI_ENABLE_FPGA;
35 // KLUDGE: This code is fragile, but reasonably fast...
36 // low three bits of enables go into port A
37 USRP_PA = USRP_PA | (0x7 << 3); // disable FPGA, CODEC_A, CODEC_B
38 USRP_PA ^= (enables & 0x7) << 3; // enable specified devs
40 // high four bits of enables go into port E
41 USRP_PE = USRP_PE | (0xf << 4); // disable TX_A, RX_A, TX_B, RX_B
42 USRP_PE ^= (enables & 0xf0); // enable specified devs
45 #define disable_all() setup_enables (0)
50 disable_all (); /* disable all devs */
51 bitS_OUT = 0; /* idle state has CLK = 0 */
56 count_bits8 (unsigned char v)
58 static unsigned char count4[16] = {
76 return count4[v & 0xf] + count4[(v >> 4) & 0xf];
82 count_bits8 (unsigned char v)
84 unsigned char count = 0;
85 if (v & (1 << 0)) count++;
86 if (v & (1 << 1)) count++;
87 if (v & (1 << 2)) count++;
88 if (v & (1 << 3)) count++;
89 if (v & (1 << 4)) count++;
90 if (v & (1 << 5)) count++;
91 if (v & (1 << 6)) count++;
92 if (v & (1 << 7)) count++;
98 write_byte_msb (unsigned char v);
101 write_bytes_msb (const xdata unsigned char *buf, unsigned char len);
104 read_bytes_msb (xdata unsigned char *buf, unsigned char len);
107 // returns non-zero if successful, else 0
109 spi_read (unsigned char header_hi, unsigned char header_lo,
110 unsigned char enables, unsigned char format,
111 xdata unsigned char *buf, unsigned char len)
113 if (count_bits8 (enables) > 1)
114 return 0; // error, too many enables set
116 setup_enables (enables);
118 if (format & SPI_FMT_LSB){ // order: LSB
120 return 0; // error, not implemented
122 switch (format & SPI_FMR_HDR_MASK){
126 write_byte_lsb (header_lo);
129 write_byte_lsb (header_lo);
130 write_byte_lsb (header_hi);
136 read_bytes_lsb (buf, len);
142 switch (format & SPI_FMT_HDR_MASK){
146 write_byte_msb (header_lo);
149 write_byte_msb (header_hi);
150 write_byte_msb (header_lo);
156 read_bytes_msb (buf, len);
164 // returns non-zero if successful, else 0
166 spi_write (unsigned char header_hi, unsigned char header_lo,
167 unsigned char enables, unsigned char format,
168 const xdata unsigned char *buf, unsigned char len)
170 setup_enables (enables);
172 if (format & SPI_FMT_LSB){ // order: LSB
174 return 0; // error, not implemented
176 switch (format & SPI_FMR_HDR_MASK){
180 write_byte_lsb (header_lo);
183 write_byte_lsb (header_lo);
184 write_byte_lsb (header_hi);
190 write_bytes_lsb (buf, len);
196 switch (format & SPI_FMT_HDR_MASK){
200 write_byte_msb (header_lo);
203 write_byte_msb (header_hi);
204 write_byte_msb (header_lo);
210 write_bytes_msb (buf, len);
217 // ----------------------------------------------------------------
220 write_byte_msb (unsigned char v)
222 v = (v << 1) | (v >> 7); // rotate left (MSB into bottom bit)
227 v = (v << 1) | (v >> 7); // rotate left (MSB into bottom bit)
232 v = (v << 1) | (v >> 7); // rotate left (MSB into bottom bit)
237 v = (v << 1) | (v >> 7); // rotate left (MSB into bottom bit)
242 v = (v << 1) | (v >> 7); // rotate left (MSB into bottom bit)
247 v = (v << 1) | (v >> 7); // rotate left (MSB into bottom bit)
252 v = (v << 1) | (v >> 7); // rotate left (MSB into bottom bit)
257 v = (v << 1) | (v >> 7); // rotate left (MSB into bottom bit)
264 write_bytes_msb (const xdata unsigned char *buf, unsigned char len)
267 write_byte_msb (*buf++);
273 * This is incorrectly compiled by SDCC 2.4.0
323 read_byte_msb (void) _naked
375 read_bytes_msb (xdata unsigned char *buf, unsigned char len)
378 *buf++ = read_byte_msb ();