Imported Upstream version 3.2.2
[debian/gnuradio] / usrp2 / firmware / lib / lsdac.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 "lsdac.h"
20 #include "spi.h"
21 #include "memory_map.h"
22
23 // AD5624, AD5623
24
25 #define CMD(x)            ((x) << 19)
26 #define CMD_WR_INPUT_N       CMD(0)   // write input N
27 #define CMD_UP_DAC_N         CMD(1)   // update DAC N from input reg
28 #define CMD_WR_INPUT_N_LDAC  CMD(2)   // write input N, update all
29 #define CMD_WR_UP_DAC_N      CMD(3)   // write and update N
30 #define CMD_WR_PWR_CONFIG    CMD(4)   // write power up/down config reg
31 #define CMD_SW_RESET         CMD(5)   // force s/w reset
32 #define CMD_WR_LDAC_CFG      CMD(6)   // write LDAC config reg
33 #define CMD_WR_INT_REF_CFG   CMD(7)   // write internal ref cfg reg (AD5623R only)
34
35
36 //#define SPI_SS_DEBUG SPI_SS_TX_DB
37 #define SPI_SS_DEBUG 0
38
39 inline static void
40 _write_rx(uint32_t v)
41 {
42   spi_transact(SPI_TXONLY, SPI_SS_RX_DAC | SPI_SS_DEBUG, v, 24, SPIF_PUSH_RISE);
43 }
44
45 inline static void
46 _write_tx(uint32_t v)
47 {
48   spi_transact(SPI_TXONLY, SPI_SS_TX_DAC | SPI_SS_DEBUG, v, 24, SPIF_PUSH_RISE);
49 }
50
51 void 
52 lsdac_init(void)
53 {
54   _write_tx(CMD_SW_RESET | 0x1);                // power-on reset
55   _write_rx(CMD_SW_RESET | 0x1);                // power-on reset
56 }
57
58 void
59 lsdac_write_rx(int which_dac, int value)
60 {
61   _write_rx(CMD_WR_UP_DAC_N | ((which_dac & 0x7) << 16) | ((value << 4) & 0xffff));
62 }
63
64 void
65 lsdac_write_tx(int which_dac, int value)
66 {
67   _write_tx(CMD_WR_UP_DAC_N | ((which_dac & 0x7) << 16) | ((value << 4) & 0xffff));
68 }