Merged r10712:10765 from jcorgan/gpio into trunk. Adds out-of-band and streaming...
[debian/gnuradio] / usrp2 / firmware / lib / u2_init.c
1 /*
2  * Copyright 2007 Free Software Foundation, Inc.
3  *
4  * This program is free software: you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation, either version 3 of the License, or
7  * (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
16  */
17
18 #include "u2_init.h"
19 #include "memory_map.h"
20 #include "spi.h"
21 #include "pic.h"
22 #include "hal_io.h"
23 #include "lsadc.h"
24 #include "lsdac.h"
25 #include "buffer_pool.h"
26 #include "hal_uart.h"
27 #include "i2c.h"
28 #include "bool.h"
29 #include "mdelay.h"
30 #include "ad9777.h"
31 #include "clocks.h"
32 #include "db.h"
33 #include "usrp2_i2c_addr.h"
34
35 //#include "nonstdio.h"
36
37 unsigned char u2_hw_rev_major;
38 unsigned char u2_hw_rev_minor;
39
40 static inline void
41 get_hw_rev(void)
42 {
43   bool ok = eeprom_read(I2C_ADDR_MBOARD, MBOARD_REV_LSB, &u2_hw_rev_minor, 1);
44   ok &= eeprom_read(I2C_ADDR_MBOARD, MBOARD_REV_MSB, &u2_hw_rev_major, 1);
45 }
46
47 /*
48  * We ought to arrange for this to be called before main, but for now,
49  * we require that the user's main call u2_init as the first thing...
50  */
51 bool
52 u2_init(void)
53 {
54   // Set GPIOs to inputs, disable GPIO streaming
55   hal_gpio_set_ddr(GPIO_TX_BANK, 0x0000, 0xffff);
56   hal_gpio_set_ddr(GPIO_RX_BANK, 0x0000, 0xffff);
57
58   hal_gpio_write(GPIO_TX_BANK, 0x0000, 0xffff); // init s/w output value to zero
59   hal_gpio_write(GPIO_RX_BANK, 0x0000, 0xffff);
60
61   dsp_rx_regs->gpio_stream_enable = 0; // I, Q LSBs come from DSP
62
63   hal_io_init();
64
65   // init spi, so that we can switch over to the high-speed clock
66   spi_init();
67
68   // set up the default clocks
69   clocks_init();
70
71   // clocks_enable_test_clk(true,1);
72
73   // Enable ADCs
74   output_regs->adc_ctrl = ADC_CTRL_ON;
75
76   // Set up AD9777 DAC
77   ad9777_write_reg(0, R0_1R);
78   ad9777_write_reg(1, R1_INTERP_4X | R1_REAL_MIX);
79   ad9777_write_reg(2, 0);
80   ad9777_write_reg(3, R3_PLL_DIV_1);
81   ad9777_write_reg(4, R4_PLL_ON | R4_CP_AUTO);
82   ad9777_write_reg(5, R5_I_FINE_GAIN(0));
83   ad9777_write_reg(6, R6_I_COARSE_GAIN(0xf));
84   ad9777_write_reg(7, 0);       // I dac offset
85   ad9777_write_reg(8, 0);
86   ad9777_write_reg(9,  R9_Q_FINE_GAIN(0));
87   ad9777_write_reg(10, R10_Q_COARSE_GAIN(0xf));
88   ad9777_write_reg(11, 0);      // Q dac offset
89   ad9777_write_reg(12, 0);
90   
91   // Initial values for tx and rx mux registers
92   dsp_tx_regs->tx_mux = 0x10;
93   dsp_rx_regs->rx_mux = 0x44444444;
94
95   // Set up serdes
96   output_regs->serdes_ctrl = (SERDES_ENABLE | SERDES_RXEN);
97
98   pic_init();   // progammable interrupt controller
99   bp_init();    // buffer pool
100   i2c_init();
101   lsadc_init();     // low-speed ADCs
102   lsdac_init();     // low-speed DACs
103   db_init();        // daughterboard init
104   
105   get_hw_rev();
106
107   hal_enable_ints();
108
109   // flash all leds to let us know board is alive
110   hal_set_leds(0x0, 0x1f);
111   mdelay(100);
112   hal_set_leds(0x1f, 0x1f);
113   mdelay(100);
114   hal_set_leds(0x1, 0x1f);  // Leave the first one on
115
116 #if 0
117   // test register readback
118   int rr, vv;
119   vv = ad9777_read_reg(0);
120   printf("ad9777 reg[0] = 0x%x\n", vv);
121   
122   for (rr = 0x04; rr <= 0x0d; rr++){
123     vv = ad9510_read_reg(rr);
124     printf("ad9510 reg[0x%x] = 0x%x\n", rr, vv);
125   }
126 #endif
127   
128   return true;
129 }