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

Subversion Repositories minsoc

[/] [minsoc/] [trunk/] [utils/] [contributions/] [gpio/] [sw/] [gpio.c] - Blame information for rev 83

Go to most recent revision | Details | Compare with Previous | View Log

Line No. Rev Author Line
1 40 rfajardo
#include "../support/support.h"
2
#include "../support/board.h"
3
 
4
#include "../support/spr_defs.h"
5
 
6
#include "../drivers/uart.h"
7
 
8
#include "gpio.h"
9
 
10
void gpio_init(gpio_t *gpio, long instance_num, unsigned long base_addr)
11
{
12
    int i = MIN_GPIO_BIT;
13
 
14
    if ( gpio != NULL ) {
15
            gpio->instance_num = instance_num;
16
            gpio->base_addr = (unsigned char*)base_addr;
17
            for ( ;i<=MAX_GPIO_BIT;i++)
18
                    gpio->vectors[i].vec = NULL;
19
            return;
20
    } else {
21
            // Print the error msgs here
22
            //
23
            uart_print_str("gpio inst in NULL.\n");
24
            return;
25
    }
26
}
27
 
28
void gpio_config_bit(gpio_t *gpio, unsigned long bit, iotype_t io)
29
{
30
    if ( gpio != NULL ) {
31
            if ( io == IO_INPUT ) {
32
                gpio->io_config |= (1 << bit);
33
                *(unsigned long*)(gpio->base_addr + OE_REG_OFFSET) &= (~(1 << bit));
34
            } else {
35
                gpio->io_config &= (~(1 << bit));
36
                *(unsigned long*)(gpio->base_addr + OE_REG_OFFSET) |= (1 << bit);
37
            }
38
            return;
39
    } else {
40
            // Print the error msgs here
41
            //
42
            uart_print_str("gpio inst in NULL.\n");
43
            return;
44
    }
45
}
46
 
47
void gpio_set_bit(gpio_t *gpio, unsigned long bit, unsigned long val)
48
{
49
    if ( gpio != NULL ) {
50
            if ( val != 0 )
51
                *(unsigned long*)(gpio->base_addr + OUT_REG_OFFSET) |= (1 << bit);
52
            else
53
                *(unsigned long*)(gpio->base_addr + OUT_REG_OFFSET) &= (~(1 << bit));
54
            return;
55
    } else {
56
            // Print the error msgs here
57
            //
58
            uart_print_str("gpio inst in NULL.\n");
59
            return;
60
    }
61
}
62
 
63
void gpio_get_bit(gpio_t *gpio, unsigned long bit, unsigned long *val)
64
{
65
    unsigned long temp;
66
 
67
    if ( gpio != NULL ) {
68
            temp = *(unsigned long*)(gpio->base_addr + IN_REG_OFFSET);
69
            *val = (temp & (1 << bit))? 1 : 0;
70
            return;
71
    } else {
72
            // Print the error msgs here
73
            //
74
            uart_print_str("gpio inst in NULL.\n");
75
            return;
76
    }
77
}
78
 
79
 
80
void gpio_add_interrupt(gpio_t *gpio, unsigned int bit, edge_t edge,void (*func)() )
81
{
82
    if ( gpio != NULL ) {
83
        if ( ( gpio->io_config &(1 << bit)) != 0 ) {  // Port bit is configured as IO_INPUT
84
                //
85
                // Disable the interrupts
86
                //
87
                *(unsigned long*)(gpio->base_addr + CTRL_REG_OFFSET) &= (~0x01);
88
 
89
                // Enable the interrupt bit
90
                //
91
                *(unsigned long*)(gpio->base_addr + INTE_REG_OFFSET) |= (1 << bit);
92
 
93
                // Enable the edge type
94
                //
95
                if ( edge == POS_EDGE )
96
                    *(unsigned long*)(gpio->base_addr + PTRIG_REG_OFFSET) |= (1 << bit);
97
                else
98
                    *(unsigned long*)(gpio->base_addr + PTRIG_REG_OFFSET) &= (~(1 << bit));
99
 
100
                // Set the function vector
101
                //
102
                gpio->vectors[bit].vec = func;
103
 
104
                int_add( 6, gpio_interrupt, gpio );
105
 
106
                // Re-enable the global control bit
107
                //
108
                    *(unsigned long*)(gpio->base_addr + CTRL_REG_OFFSET) |= 0x01;
109
        } else {
110
                // Port is configured as IO_OUTPUT
111
                uart_print_str("gpio pin is not an input pin.\n");
112
                return;
113
        }
114
 
115
    } else {
116
            // Print the error msgs here
117
            //
118
            uart_print_str("gpio inst in NULL.\n");
119
            return;
120
    }
121
 
122
}
123
 
124
void gpio_interrupt(gpio_t *gpio)
125
{
126
    int i;
127
    unsigned long int interrupt_status;
128
 
129
    if ( (*(unsigned long*)(gpio->base_addr + CTRL_REG_OFFSET)) & 0x02 )
130
    {
131
            // Interrupt is pending here
132
            //
133
            interrupt_status = *(unsigned long*)(gpio->base_addr + INTS_REG_OFFSET);
134
 
135
            // Prioritize from lower bits(0) to higher ones(31)
136
            //
137
 
138
            for ( i=MIN_GPIO_BIT; i<=MAX_GPIO_BIT; i++ ) {
139
                if ( (interrupt_status & (1<<i)) ) {
140
                    *(unsigned long*)(gpio->base_addr + INTS_REG_OFFSET) &= (~( 1 << i ));
141
                    (gpio->vectors[i].vec)();
142
                }
143
            }
144
 
145
            *(unsigned long*)(gpio->base_addr + CTRL_REG_OFFSET) &= (~0x02);
146
 
147
    }
148
}
149
 
150
void hello_east()
151
{
152
        uart_print_str("Hello from PUSH Button EAST.\n");
153
}
154
 
155
 
156
void hello_west()
157
{
158
        uart_print_str("Hello from PUSH Button WEST.\n");
159
}
160
 
161
 
162
void hello_south()
163
{
164
        uart_print_str("Hello from PUSH Button SOUTH.\n");
165
}
166
 
167
 
168
 
169
 
170
#define MAX_COUNT 10
171
 
172
int main()
173
{
174
        gpio_t gpio_1;
175
        unsigned long t0, t1, t2, t3;
176
        unsigned long count = 0;
177
 
178
        tick_init();
179
        uart_init();
180
        int_init();
181
        int_add(2,&uart_interrupt);
182
 
183
        gpio_init( &gpio_1, 1, GPIO_BASE );
184
 
185
        gpio_config_bit( &gpio_1, LED_0, IO_OUTPUT);
186
        gpio_config_bit( &gpio_1, LED_1, IO_OUTPUT);
187
        gpio_config_bit( &gpio_1, LED_2, IO_OUTPUT);
188
        gpio_config_bit( &gpio_1, LED_3, IO_OUTPUT);
189
        gpio_config_bit( &gpio_1, LED_4, IO_OUTPUT);
190
        gpio_config_bit( &gpio_1, LED_5, IO_OUTPUT);
191
        gpio_config_bit( &gpio_1, LED_6, IO_OUTPUT);
192
        gpio_config_bit( &gpio_1, LED_7, IO_OUTPUT);
193
 
194
        while ( count++ < MAX_COUNT ) {
195
                gpio_set_bit( &gpio_1, LED_7, 0 );
196
                gpio_set_bit( &gpio_1, LED_0, 1 );
197
                udelay();
198
                gpio_set_bit( &gpio_1, LED_0, 0 );
199
                gpio_set_bit( &gpio_1, LED_1, 1 );
200
                udelay();
201
                gpio_set_bit( &gpio_1, LED_1, 0 );
202
                gpio_set_bit( &gpio_1, LED_2, 1 );
203
                udelay();
204
                gpio_set_bit( &gpio_1, LED_2, 0 );
205
                gpio_set_bit( &gpio_1, LED_3, 1 );
206
                udelay();
207
                gpio_set_bit( &gpio_1, LED_3, 0 );
208
                gpio_set_bit( &gpio_1, LED_4, 1 );
209
                udelay();
210
                gpio_set_bit( &gpio_1, LED_4, 0 );
211
                gpio_set_bit( &gpio_1, LED_5, 1 );
212
                udelay();
213
                gpio_set_bit( &gpio_1, LED_5, 0 );
214
                gpio_set_bit( &gpio_1, LED_6, 1 );
215
                udelay();
216
                gpio_set_bit( &gpio_1, LED_6, 0 );
217
                gpio_set_bit( &gpio_1, LED_7, 1 );
218
                udelay();
219
        }
220
 
221
        gpio_set_bit( &gpio_1, LED_7, 0 );
222
 
223
        report(0xdeaddead);
224
        or32_exit(0);
225
}

powered by: WebSVN 2.1.0

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