Imported Upstream version 3.2.2
[debian/gnuradio] / usrp2 / firmware / lib / hal_io.h
1 /* -*- c++ -*- */
2 /*
3  * Copyright 2007 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 #ifndef INCLUDED_HAL_IO_H
20 #define INCLUDED_HAL_IO_H
21
22 #include "memory_map.h"
23
24 void hal_io_init(void);
25 void hal_finish();
26
27
28 /*
29  * ------------------------------------------------------------------------
30  * The GPIO pins are organized into two banks of 16-bits.
31  * Bank 0 goes to the Tx daughterboard, Bank 1 goes to the Rx daughterboard.
32  *
33  * Each pin may be configured as an input or an output from the FPGA.
34  * For output pins, there are four signals which may be routed to the
35  * pin.  The four signals are the value written by s/w, the output of
36  * the ATR controller, or two different sources of debug info from the
37  * FPGA fabric.
38  * ------------------------------------------------------------------------
39  */
40
41 #define GPIO_TX_BANK    0       // pins that connect to the Tx daughterboard
42 #define GPIO_RX_BANK    1       // pins that connect to the Rx daughterboard
43
44
45 /*!
46  * \brief Set the data direction for GPIO pins
47  *
48  * If the bit is set, it's an output from the FPGA.
49  * \param value is a 16-bit bitmask of values
50  * \param mask is a 16-bit bitmask of which bits to effect.
51  */ 
52 void hal_gpio_set_ddr(int bank, int value, int mask);
53
54 /*!
55  * \brief Select the source of the signal for an output pin.
56  *
57  * \param code is is one of 's', 'a', '0', '1'
58  *   where 's' selects software output, 'a' selects ATR output, '0' selects
59  *   debug 0, '1' selects debug 1.
60  */
61 void hal_gpio_set_sel(int bank, int bitno, char code);
62
63 /*!
64  * \brief Select the source of the signal for the output pins.
65  *
66  * \param codes is is a string of 16 characters composed of '.', 's',
67  * 'a', '0', or '1' where '.' means "don't change", 's' selects
68  * software output, 'a' selects ATR output, '0' selects debug 0, '1'
69  * selects debug 1.
70  */
71 void hal_gpio_set_sels(int bank, char *codes);
72
73
74 /*!
75  * \brief write \p value to gpio pins specified by \p mask.
76  */
77 void hal_gpio_write(int bank, int value, int mask);
78
79 /*!
80  * \brief read GPIO bits
81  */
82 int  hal_gpio_read(int bank);
83
84
85 /*
86  * ------------------------------------------------------------------------
87  *                         control the leds
88  *
89  * Low 4-bits are the general purpose leds on the board
90  * The next bit is the led on the ethernet connector
91  * ------------------------------------------------------------------------
92  */
93
94 void hal_set_leds(int value, int mask);
95 void hal_set_led_src(int value, int mask);
96 void hal_toggle_leds(int mask);
97
98 /*
99  * ------------------------------------------------------------------------
100  *                         simple timeouts
101  * ------------------------------------------------------------------------
102  */
103
104
105
106 static inline void
107 hal_set_timeout(int delta_ticks)
108 {
109   int t = timer_regs->time + delta_ticks;
110   if (t == 0)                   // kills timer
111     t = 1;
112   timer_regs->time = t;
113 }
114
115 /*
116  * ------------------------------------------------------------------------
117  *                      interrupt enable/disable
118  * ------------------------------------------------------------------------
119  */
120
121 /*!
122  * \brief Disable interrupts and return previous interrupt enable state.
123  * [Microblaze specific]
124  */
125 static inline int
126 hal_disable_ints(void)
127 {
128   int result, t0;
129
130   asm volatile("mfs   %0, rmsr       \n\
131                 andni %1, %0, 0x2    \n\
132                 mts   rmsr, %1"
133                : "=r" (result), "=r" (t0));
134   return result;
135 }
136
137 /*!
138  * \brief Enable interrupts and return previous interrupt enable state.
139  * [Microblaze specific]
140  */
141 static inline int
142 hal_enable_ints(void)
143 {
144   int result, t0;
145
146   asm volatile("mfs  %0, rmsr         \n\
147                 ori  %1, %0, 0x2      \n\
148                 mts  rmsr, %1"
149                : "=r" (result), "=r" (t0));
150   return result;
151 }
152
153 /*!
154  * \brief Set interrupt enable state to \p prev_state.
155  * [Microblaze specific]
156  */
157 static inline void
158 hal_restore_ints(int prev_state)
159 {
160   int t0, t1;
161   asm volatile("andi  %0, %2, 0x2       \n\
162                 mfs   %1, rmsr          \n\
163                 andni %1, %1, 0x2       \n\
164                 or    %1, %1, %0        \n\
165                 mts   rmsr, %1"
166                : "=r" (t0), "=r"(t1) : "r" (prev_state));
167 }
168
169 #endif /* INCLUDED_HAL_IO_H */