OpenCores
URL https://opencores.org/ocsvn/xenie/xenie/trunk

Subversion Repositories xenie

[/] [xenie/] [trunk/] [examples/] [Eth_example/] [mb_fw/] [xenie_eth_test_womtd/] [src/] [gpio.c] - Blame information for rev 4

Details | Compare with Previous | View Log

Line No. Rev Author Line
1 4 DFC
/***************************************************************************
2
 *
3
 * (C) Copyright 2017 DFC Design, s.r.o., Brno, Czech Republic
4
 * Author: Marek Kvas (m.kvas@dspfpga.com)
5
 *
6
 ***************************************************************************
7
 *
8
 * This file is part of Xenia Ethernet Example project.
9
 *
10
 * Xenia Ethernet Example project is free software: you can
11
 * redistribute it and/or modify it under the terms of
12
 * the GNU Lesser General Public License as published by the Free
13
 * Software Foundation, either version 3 of the License, or
14
 * (at your option) any later version.
15
 *
16
 * Xenia Ethernet Example project is distributed in the hope that
17
 * it will be useful, but WITHOUT ANY WARRANTY; without even
18
 * the implied warranty of MERCHANTABILITY or FITNESS FOR A
19
 * PARTICULAR PURPOSE.  See the GNU Lesser General Public License
20
 * for more details.
21
 *
22
 * You should have received a copy of the GNU Lesser General Public
23
 * License along with Xenia Ethernet Example project.  If not,
24
 * see <http://www.gnu.org/licenses/>.
25
 *
26
 ***************************************************************************
27
 */
28
 
29
#include <stdio.h>
30
#include "xparameters.h"
31
#include "xgpio_l.h"
32
#include "gpio.h"
33
 
34
/*
35
 * List of available GPIO cores/banks
36
 */
37
static struct gpio_inst gpio_list[] =
38
                {{XPAR_AXI_GPIO_0_BASEADDR, {0,0}},
39
                 {XPAR_AXI_GPIO_1_VERSION_BASEADDR, {0,0}},
40
                 {XPAR_AXI_GPIO_2_NETINFO_BASEADDR, {0,0}},
41
                };
42
 
43
/*
44
 * return number of GPIO banks available.
45
 */
46
int gpio_banks_available()
47
{
48
        return (sizeof(gpio_list)/ sizeof(struct gpio_inst)) * 2;
49
}
50
 
51
/*
52
 * Set bits in direction register.
53
 * Bits that are 1 in val will be 1
54
 * in register too.
55
 */
56
void gpio_set_dir(int bank, u32 val)
57
{
58
        u32 data;
59
        int core_idx = bank/2;
60
        int port_idx = bank%2;
61
        u32 off = (port_idx == 0)?XGPIO_TRI_OFFSET:XGPIO_TRI2_OFFSET;
62
        u32 baseAddr = gpio_list[core_idx].baseAddr;
63
 
64
        data = XGpio_ReadReg(baseAddr, off);
65
        data |= val;
66
        XGpio_WriteReg(baseAddr, off, data);
67
}
68
 
69
/*
70
 * Clear bits in direction register.
71
 * Bits that are 1 in val will be 0
72
 * in register.
73
 */
74
void gpio_clear_dir(int bank, u32 val)
75
{
76
        u32 data;
77
        int core_idx = bank/2;
78
        int port_idx = bank%2;
79
        u32 off = (port_idx == 0)?XGPIO_TRI_OFFSET:XGPIO_TRI2_OFFSET;
80
        u32 baseAddr = gpio_list[core_idx].baseAddr;
81
 
82
        data = XGpio_ReadReg(baseAddr, off);
83
        data &= (~val);
84
        XGpio_WriteReg(baseAddr, off, data);
85
}
86
 
87
/*
88
 * Get current state of inputs
89
 * for specified bank.
90
 */
91
u32 gpio_get(int bank)
92
{
93
        int core_idx = bank/2;
94
        int port_idx = bank%2;
95
        u32 off = (port_idx == 0)?XGPIO_DATA_OFFSET:XGPIO_DATA2_OFFSET;
96
        u32 baseAddr = gpio_list[core_idx].baseAddr;
97
        return XGpio_ReadReg(baseAddr, off);
98
}
99
 
100
/*
101
 * Get current state of direction
102
 * register for specified bank.
103
 */
104
u32 gpio_get_dir(int bank)
105
{
106
        int core_idx = bank/2;
107
        int port_idx = bank%2;
108
        u32 off = (port_idx == 0)?XGPIO_TRI_OFFSET:XGPIO_TRI2_OFFSET;
109
        u32 baseAddr = gpio_list[core_idx].baseAddr;
110
        return XGpio_ReadReg(baseAddr, off);
111
}
112
 
113
/*
114
 * Set bits in output  register.
115
 * Bits that are 1 in val will be 1
116
 * in register too.
117
 */
118
void gpio_set_out(int bank, u32 val)
119
{
120
        int core_idx = bank/2;
121
        int port_idx = bank%2;
122
        u32 off = (port_idx == 0)?XGPIO_DATA_OFFSET:XGPIO_DATA2_OFFSET;
123
        u32 baseAddr = gpio_list[core_idx].baseAddr;
124
        gpio_list[core_idx].gpio_odata[port_idx] |= val;
125
        XGpio_WriteReg(baseAddr, off, gpio_list[core_idx].gpio_odata[port_idx]);
126
}
127
 
128
/*
129
 * Clear bits in output  register.
130
 * Bits that are 1 in val will be 0
131
 * in register.
132
 */
133
void gpio_clear_out(int bank, u32 val)
134
{
135
        int core_idx = bank/2;
136
        int port_idx = bank%2;
137
        u32 off = (port_idx == 0)?XGPIO_DATA_OFFSET:XGPIO_DATA2_OFFSET;
138
        u32 baseAddr = gpio_list[core_idx].baseAddr;
139
        gpio_list[core_idx].gpio_odata[port_idx] &= (~val);
140
        XGpio_WriteReg(baseAddr, off, gpio_list[core_idx].gpio_odata[port_idx]);
141
}
142
 
143
/*
144
 * Set bit field in output register to
145
 * specified value val.
146
 */
147
void gpio_set_field(int bank, u32 mask, u32 shift, u32 val)
148
{
149
        int core_idx = bank/2;
150
        int port_idx = bank%2;
151
        u32 off = (port_idx == 0)?XGPIO_DATA_OFFSET:XGPIO_DATA2_OFFSET;
152
        u32 baseAddr = gpio_list[core_idx].baseAddr;
153
        gpio_list[core_idx].gpio_odata[port_idx] &= ~mask;
154
        gpio_list[core_idx].gpio_odata[port_idx] |= (val << shift) & mask;
155
        XGpio_WriteReg(baseAddr, off, gpio_list[core_idx].gpio_odata[port_idx]);
156
}
157
 
158
/*
159
 * Set bit field in direction register to
160
 * specified value val.
161
 */
162
void gpio_set_dir_field(int bank, u32 mask, u32 shift, u32 val)
163
{
164
        u32 data;
165
        int core_idx = bank/2;
166
        int port_idx = bank%2;
167
 
168
        u32 off = (port_idx == 0)?XGPIO_TRI_OFFSET:XGPIO_TRI2_OFFSET;
169
        u32 baseAddr = gpio_list[core_idx].baseAddr;
170
 
171
        data = XGpio_ReadReg(baseAddr, off);
172
        data &= ~mask;
173
        data |= (val << shift) & mask;
174
        XGpio_WriteReg(baseAddr, off, data);
175
}
176
 
177
/*
178
 * Get value of bit field from inputs.
179
 */
180
u32 gpio_get_field(int bank, u32 mask, u32 shift)
181
{
182
        u32 data = gpio_get(bank);
183
        data &= mask;
184
        data >>= shift;
185
        return data;
186
}
187
 
188
/*
189
 * Get value of bit field from direction register.
190
 */
191
u32 gpio_get_dir_field(int bank, u32 mask, u32 shift)
192
{
193
        u32 data = gpio_get_dir(bank);
194
        data &= mask;
195
        data >>= shift;
196
        return data;
197
}
198
 
199
/*
200
 * Initialize GPIOs to default state.
201
 * By default all pins are set as inputs
202
 * (all ones in direction register) and
203
 * output register is cleared.
204
 *
205
 * This is done for all known GPIO banks.
206
 */
207
void init_gpio_regs()
208
{
209
        int i;
210
        for (i = 0; i < gpio_banks_available();i++) {
211
                gpio_clear_out(i, 0xffffffff);
212
                gpio_set_dir(i, 0xffffffff);
213
        }
214
}
215
 

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.