Imported Upstream version 3.0
[debian/gnuradio] / usrp / firmware / src / usrp2 / fpga_rev2.c
1 /* -*- c++ -*- */
2 /*
3  * Copyright 2004 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 2, 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 #include "fpga.h"
24 #include "fpga_regs_common.h"
25 #include "usrp_common.h"
26 #include "usrp_globals.h"
27 #include "spi.h"
28
29 unsigned char g_tx_reset = 0;
30 unsigned char g_rx_reset = 0;
31
32 void
33 fpga_write_reg (unsigned char regno, const xdata unsigned char *regval)
34 {
35   spi_write (0, 0x00 | (regno & 0x7f),
36              SPI_ENABLE_FPGA,
37              SPI_FMT_MSB | SPI_FMT_HDR_1,
38              regval, 4);
39 }
40
41
42 static xdata unsigned char regval[4] = {0, 0, 0, 0};
43
44 static void
45 write_fpga_master_ctrl (void)
46 {
47   unsigned char v = 0;
48   if (g_tx_enable)
49     v |= bmFR_MC_ENABLE_TX;
50   if (g_rx_enable)
51     v |= bmFR_MC_ENABLE_RX;
52   if (g_tx_reset)
53     v |= bmFR_MC_RESET_TX;
54   if (g_rx_reset)
55     v |= bmFR_MC_RESET_RX;
56   regval[3] = v;
57
58   fpga_write_reg (FR_MASTER_CTRL, regval);
59 }
60
61 // Resets both AD9862's and the FPGA serial bus interface.
62
63 void
64 fpga_set_reset (unsigned char on)
65 {
66   on &= 0x1;
67
68   if (on){
69     USRP_PC &= ~bmPC_nRESET;            // active low
70     g_tx_enable = 0;
71     g_rx_enable = 0;
72     g_tx_reset = 0;
73     g_rx_reset = 0;
74   }
75   else
76     USRP_PC |= bmPC_nRESET;
77 }
78
79 void
80 fpga_set_tx_enable (unsigned char on)
81 {
82   on &= 0x1;
83   g_tx_enable = on;
84
85   write_fpga_master_ctrl ();
86
87   if (on){
88     g_tx_underrun = 0;
89     fpga_clear_flags ();
90   }
91 }
92
93 void
94 fpga_set_rx_enable (unsigned char on)
95 {
96   on &= 0x1;
97   g_rx_enable = on;
98   
99   write_fpga_master_ctrl ();
100   if (on){
101     g_rx_overrun = 0;
102     fpga_clear_flags ();
103   }
104 }
105
106 void
107 fpga_set_tx_reset (unsigned char on)
108 {
109   on &= 0x1;
110   g_tx_reset = on;
111
112   write_fpga_master_ctrl ();
113 }
114
115 void
116 fpga_set_rx_reset (unsigned char on)
117 {
118   on &= 0x1;
119   g_rx_reset = on;
120   
121   write_fpga_master_ctrl ();
122 }