Imported Upstream version 3.2.2
[debian/gnuradio] / usrp2 / firmware / apps / test_phy_comm.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 // check communication with ethernet PHY chip
19
20 #include "u2_init.h"
21 #include "memory_map.h"
22 #include "hal_io.h"
23 #include "ethernet.h"
24 #include "pic.h"
25 #include "nonstdio.h"
26
27
28 #define DELTA_T     12500000            // .125s (10ns per tick)
29 //#define DELTA_T      10000
30
31 // debugging output on tx pins
32 #define LS_MASK  0xE0000
33 #define LS_1000  0x80000
34 #define LS_100   0x40000
35 #define LS_10    0x20000
36
37
38
39 #define U2_ETHERTYPE 0xBEEF
40
41
42 static volatile int led_link_up_flag = 0;
43
44 /*
45  * Called when eth phy state changes (w/ interrupts disabled)
46  */
47 void
48 link_changed_callback(int speed)
49 {
50   int v = 0;
51   switch(speed){
52   case 10:
53     v = LS_10;
54     led_link_up_flag = 0x2;
55     break;
56     
57   case 100:
58     v = LS_100;
59     led_link_up_flag = 0x2;
60     break;
61     
62   case 1000:
63     v = LS_100;
64     led_link_up_flag = 0x2;
65     break;
66
67   default:
68     v = 0;
69     led_link_up_flag = 0;
70     break;
71   }
72
73   //hal_gpio_set_tx(v, LS_MASK);        /* set debug bits on d'board */
74
75   putstr("\neth link changed: speed = ");
76   puthex_nl(speed);
77 }
78
79 void
80 timer_handler(unsigned irq)
81 {
82   static int led_counter = 0;
83
84   hal_set_timeout(DELTA_T);     // schedule next timeout
85   output_regs->leds = (led_counter++ & 0x1) | led_link_up_flag;
86 }
87
88 int
89 main(void)
90 {
91   u2_init();
92
93   putstr("\n test_phy_comm\n");
94
95   pic_register_handler(IRQ_TIMER, timer_handler);
96   hal_set_timeout(DELTA_T);     // schedule timeout
97
98   // setup tx gpio bits for GPIOM_FPGA_1 -- fpga debug output
99   //hal_gpio_set_sels(GPIO_TX_BANK, "1111111111111111");
100   //hal_gpio_set_sels(GPIO_RX_BANK, "1111111111111111");
101
102   ethernet_register_link_changed_callback(link_changed_callback);
103
104   output_regs->phy_ctrl = 1;    /* reset the eth PHY */
105   output_regs->phy_ctrl = 0;
106
107   ethernet_init();
108
109   while(1)
110     ;
111
112   return 0;
113 }