Imported Upstream version 3.2.2
[debian/gnuradio] / usrp2 / firmware / lib / lsadc.c
1 /* -*- c++ -*- */
2 /*
3  * Copyright 2008 Free Software Foundation, Inc.
4  *
5  * This program is free software: you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation, either version 3 of the License, or
8  * (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
17  */
18
19 #include "lsadc.h"
20 #include "spi.h"
21 #include "memory_map.h"
22
23
24 // AD9712 or AD7922   1 MS/s, 10-/12-bit ADCs
25
26 //#define SPI_SS_DEBUG SPI_SS_RX_DB
27 #define SPI_SS_DEBUG 0
28
29
30 void 
31 lsadc_init(void)
32 {
33   // nop
34 }
35
36 /* 
37  * The ADC's are pipelined.   That is, you have to tell them
38  * which of the two inputs you want one cycle ahead of time.
39  * We could optimize and keep track of which one we used last
40  * time, but for simplicity we'll always tell it which
41  * one we want.  This takes 2 16-bit xfers, one to set the
42  * input and one to read the one we want.
43  */
44
45 int
46 _lsadc_read(int which_adc, int slave_select)
47 {
48   uint32_t r;
49   int channel = which_adc & 0x1;
50
51   // Set CHN and STY equal to channel number.  We don't want "daisy chain mode"
52   uint16_t cmd = (channel << 13) | (channel << 12);
53
54   spi_transact(SPI_TXONLY, slave_select | SPI_SS_DEBUG,
55                cmd, 16, SPIF_PUSH_RISE | SPIF_LATCH_RISE);
56
57   r = spi_transact(SPI_TXRX, slave_select | SPI_SS_DEBUG,
58                    cmd, 16, SPIF_PUSH_RISE | SPIF_LATCH_RISE);
59
60   return r & 0x0fff;
61 }
62
63 int
64 lsadc_read_rx(int which_adc)
65 {
66   return _lsadc_read(which_adc, SPI_SS_RX_ADC);
67 }
68
69 int
70 lsadc_read_tx(int which_adc)
71 {
72   return _lsadc_read(which_adc, SPI_SS_TX_ADC);
73 }