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

Subversion Repositories minsoc

[/] [minsoc/] [branches/] [rc-1.0/] [utils/] [contributions/] [gpio/] [sw/] [old/] [gpio.c] - Blame information for rev 141

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
#include "../support/uart.h"
4
 
5
#include "../support/spr_defs.h"
6
 
7
#include "gpio.h"
8
 
9
 
10
void uart_print_str(char *);
11
void uart_print_long(unsigned long);
12
 
13
// Dummy or32 except vectors
14
void buserr_except(){}
15
void dpf_except(){}
16
void ipf_except(){}
17
void lpint_except(){}
18
void align_except(){}
19
void illegal_except(){}
20
/*void hpint_except(){
21
 
22
}*/
23
void dtlbmiss_except(){}
24
void itlbmiss_except(){}
25
void range_except(){}
26
void syscall_except(){}
27
void res1_except(){}
28
void trap_except(){}
29
void res2_except(){}
30
 
31
 
32
void uart_interrupt()
33
{
34
    char lala;
35
    unsigned char interrupt_id;
36
    interrupt_id = REG8(UART_BASE + UART_IIR);
37
    if ( interrupt_id & UART_IIR_RDI )
38
    {
39
        lala = uart_getc();
40
        uart_putc(lala+1);
41
    }
42
}
43
 
44
 
45
void uart_print_str(char *p)
46
{
47
        while(*p != 0) {
48
                uart_putc(*p);
49
                p++;
50
        }
51
}
52
 
53
void uart_print_long(unsigned long ul)
54
{
55
  int i;
56
  char c;
57
 
58
 
59
  uart_print_str("0x");
60
  for(i=0; i<8; i++) {
61
 
62
  c = (char) (ul>>((7-i)*4)) & 0xf;
63
  if(c >= 0x0 && c<=0x9)
64
    c += '0';
65
  else
66
    c += 'a' - 10;
67
  uart_putc(c);
68
  }
69
 
70
}
71
 
72
void uart_print_short(unsigned long ul)
73
{
74
  int i;
75
  char c;
76
  char flag=0;
77
 
78
 
79
  uart_print_str("0x");
80
  for(i=0; i<8; i++) {
81
 
82
  c = (char) (ul>>((7-i)*4)) & 0xf;
83
  if(c >= 0x0 && c<=0x9)
84
    c += '0';
85
  else
86
    c += 'a' - 10;
87
  if ((c != '0') || (i==7))
88
    flag=1;
89
  if(flag)
90
    uart_putc(c);
91
  }
92
 
93
}
94
 
95
/*
96
 *
97
 *
98
 *
99
 *
100
 *
101
 *
102
 *
103
 *
104
 *
105
 */
106
 
107
void gpio_init(gpio_t *gpio, long instance_num, unsigned long base_addr)
108
{
109
    int i = MIN_GPIO_BIT;
110
 
111
    if ( gpio != NULL ) {
112
            gpio->instance_num = instance_num;
113
            gpio->base_addr = (unsigned char*)base_addr;
114
            for ( ;i<=MAX_GPIO_BIT;i++)
115
                    gpio->vectors[i].vec = NULL;
116
            return;
117
    } else {
118
            // Print the error msgs here
119
            //
120
            uart_print_str("gpio inst in NULL.\n");
121
            return;
122
    }
123
}
124
 
125
void gpio_config_bit(gpio_t *gpio, unsigned long bit, iotype_t io)
126
{
127
    if ( gpio != NULL ) {
128
            if ( io == IO_INPUT ) {
129
                gpio->io_config |= (1 << bit);
130
                *(unsigned long*)(gpio->base_addr + OE_REG_OFFSET) &= (~(1 << bit));
131
            } else {
132
                gpio->io_config &= (~(1 << bit));
133
                *(unsigned long*)(gpio->base_addr + OE_REG_OFFSET) |= (1 << bit);
134
            }
135
            return;
136
    } else {
137
            // Print the error msgs here
138
            //
139
            uart_print_str("gpio inst in NULL.\n");
140
            return;
141
    }
142
}
143
 
144
void gpio_set_bit(gpio_t *gpio, unsigned long bit, unsigned long val)
145
{
146
    if ( gpio != NULL ) {
147
            if ( val != 0 )
148
                *(unsigned long*)(gpio->base_addr + OUT_REG_OFFSET) |= (1 << bit);
149
            else
150
                *(unsigned long*)(gpio->base_addr + OUT_REG_OFFSET) &= (~(1 << bit));
151
            return;
152
    } else {
153
            // Print the error msgs here
154
            //
155
            uart_print_str("gpio inst in NULL.\n");
156
            return;
157
    }
158
}
159
 
160
void gpio_get_bit(gpio_t *gpio, unsigned long bit, unsigned long *val)
161
{
162
    unsigned long temp;
163
 
164
    if ( gpio != NULL ) {
165
            temp = *(unsigned long*)(gpio->base_addr + IN_REG_OFFSET);
166
            *val = (temp & (1 << bit))? 1 : 0;
167
            return;
168
    } else {
169
            // Print the error msgs here
170
            //
171
            uart_print_str("gpio inst in NULL.\n");
172
            return;
173
    }
174
}
175
 
176
 
177
void gpio_add_interrupt(gpio_t *gpio, unsigned int bit, edge_t edge,void (*func)() )
178
{
179
    if ( gpio != NULL ) {
180
        if ( ( gpio->io_config &(1 << bit)) != 0 ) {  // Port bit is configured as IO_INPUT
181
                //
182
                // Disable the interrupts
183
                //
184
                *(unsigned long*)(gpio->base_addr + CTRL_REG_OFFSET) &= (~0x01);
185
 
186
                // Enable the interrupt bit
187
                //
188
                *(unsigned long*)(gpio->base_addr + INTE_REG_OFFSET) |= (1 << bit);
189
 
190
                // Enable the edge type
191
                //
192
                if ( edge == POS_EDGE )
193
                    *(unsigned long*)(gpio->base_addr + PTRIG_REG_OFFSET) |= (1 << bit);
194
                else
195
                    *(unsigned long*)(gpio->base_addr + PTRIG_REG_OFFSET) &= (~(1 << bit));
196
 
197
                // Set the function vector
198
                //
199
                gpio->vectors[bit].vec = func;
200
 
201
                int_add( 6, gpio_interrupt, gpio );
202
 
203
                // Re-enable the global control bit
204
                //
205
                    *(unsigned long*)(gpio->base_addr + CTRL_REG_OFFSET) |= 0x01;
206
        } else {
207
                // Port is configured as IO_OUTPUT
208
                uart_print_str("gpio pin is not an input pin.\n");
209
                return;
210
        }
211
 
212
    } else {
213
            // Print the error msgs here
214
            //
215
            uart_print_str("gpio inst in NULL.\n");
216
            return;
217
    }
218
 
219
}
220
 
221
void gpio_interrupt(gpio_t *gpio)
222
{
223
    int i;
224
    unsigned long int interrupt_status;
225
 
226
    if ( (*(unsigned long*)(gpio->base_addr + CTRL_REG_OFFSET)) & 0x02 )
227
    {
228
            // Interrupt is pending here
229
            //
230
            interrupt_status = *(unsigned long*)(gpio->base_addr + INTS_REG_OFFSET);
231
 
232
            // Prioritize from lower bits(0) to higher ones(31)
233
            //
234
 
235
            for ( i=MIN_GPIO_BIT; i<=MAX_GPIO_BIT; i++ ) {
236
                if ( (interrupt_status & (1<<i)) ) {
237
                    *(unsigned long*)(gpio->base_addr + INTS_REG_OFFSET) &= (~( 1 << i ));
238
                    (gpio->vectors[i].vec)();
239
                }
240
            }
241
 
242
            *(unsigned long*)(gpio->base_addr + CTRL_REG_OFFSET) &= (~0x02);
243
 
244
    }
245
}
246
 
247
void hello_east()
248
{
249
        uart_print_str("Hello from PUSH Button EAST.\n");
250
}
251
 
252
 
253
void hello_west()
254
{
255
        uart_print_str("Hello from PUSH Button WEST.\n");
256
}
257
 
258
 
259
void hello_south()
260
{
261
        uart_print_str("Hello from PUSH Button SOUTH.\n");
262
}
263
 
264
 
265
 
266
 
267
#define MAX_COUNT 10
268
 
269
int main()
270
{
271
        gpio_t gpio_1;
272
        unsigned long t0, t1, t2, t3;
273
        unsigned long count = 0;
274
 
275
        uart_init();
276
        int_init();
277
        int_add(2,&uart_interrupt);
278
 
279
        gpio_init( &gpio_1, 1, GPIO_BASE );
280
 
281
        gpio_config_bit( &gpio_1, LED_0, IO_OUTPUT);
282
        gpio_config_bit( &gpio_1, LED_1, IO_OUTPUT);
283
        gpio_config_bit( &gpio_1, LED_2, IO_OUTPUT);
284
        gpio_config_bit( &gpio_1, LED_3, IO_OUTPUT);
285
        gpio_config_bit( &gpio_1, LED_4, IO_OUTPUT);
286
        gpio_config_bit( &gpio_1, LED_5, IO_OUTPUT);
287
        gpio_config_bit( &gpio_1, LED_6, IO_OUTPUT);
288
        gpio_config_bit( &gpio_1, LED_7, IO_OUTPUT);
289
 
290
        gpio_config_bit( &gpio_1, DIP_0, IO_INPUT);
291
        gpio_config_bit( &gpio_1, DIP_1, IO_INPUT);
292
        gpio_config_bit( &gpio_1, DIP_2, IO_INPUT);
293
        gpio_config_bit( &gpio_1, DIP_3, IO_INPUT);
294
 
295
        uart_print_str("Demo 1 : Check for running LED patterns on board ...\n");
296
 
297
        while ( count++ < MAX_COUNT ) {
298
                gpio_set_bit( &gpio_1, LED_7, 0 );
299
                gpio_set_bit( &gpio_1, LED_0, 1 );
300
                udelay( 100000 );
301
                gpio_set_bit( &gpio_1, LED_0, 0 );
302
                gpio_set_bit( &gpio_1, LED_1, 1 );
303
                udelay( 100000 );
304
                gpio_set_bit( &gpio_1, LED_1, 0 );
305
                gpio_set_bit( &gpio_1, LED_2, 1 );
306
                udelay( 100000 );
307
                gpio_set_bit( &gpio_1, LED_2, 0 );
308
                gpio_set_bit( &gpio_1, LED_3, 1 );
309
                udelay( 100000 );
310
                gpio_set_bit( &gpio_1, LED_3, 0 );
311
                gpio_set_bit( &gpio_1, LED_4, 1 );
312
                udelay( 100000 );
313
                gpio_set_bit( &gpio_1, LED_4, 0 );
314
                gpio_set_bit( &gpio_1, LED_5, 1 );
315
                udelay( 100000 );
316
                gpio_set_bit( &gpio_1, LED_5, 0 );
317
                gpio_set_bit( &gpio_1, LED_6, 1 );
318
                udelay( 100000 );
319
                gpio_set_bit( &gpio_1, LED_6, 0 );
320
                gpio_set_bit( &gpio_1, LED_7, 1 );
321
                udelay( 100000 );
322
        }
323
 
324
        gpio_set_bit( &gpio_1, LED_7, 0 );
325
 
326
        gpio_config_bit( &gpio_1, PUSH_EAST, IO_INPUT);
327
        gpio_add_interrupt( &gpio_1, PUSH_EAST, POS_EDGE, hello_east );
328
        gpio_config_bit( &gpio_1, PUSH_WEST, IO_INPUT);
329
        gpio_add_interrupt( &gpio_1, PUSH_WEST, POS_EDGE, hello_west );
330
        gpio_config_bit( &gpio_1, PUSH_SOUTH, IO_INPUT);
331
        gpio_add_interrupt( &gpio_1, PUSH_SOUTH, POS_EDGE, hello_south );
332
 
333
        uart_print_str("Demo 2 : Press the DIP switches and watch corresponding LED glow ...\n");
334
 
335
 
336
        while (1) {
337
                gpio_get_bit( &gpio_1, DIP_0, &t0 );
338
                gpio_get_bit( &gpio_1, DIP_1, &t1 );
339
                gpio_get_bit( &gpio_1, DIP_2, &t2 );
340
                gpio_get_bit( &gpio_1, DIP_3, &t3 );
341
                //
342
                gpio_set_bit( &gpio_1, LED_0, t0 );
343
                gpio_set_bit( &gpio_1, LED_1, t1 );
344
                gpio_set_bit( &gpio_1, LED_2, t2 );
345
                gpio_set_bit( &gpio_1, LED_3, t3 );
346
        }
347
 
348
 
349
        report(0xdeaddead);
350
        or32_exit(0);
351
}

powered by: WebSVN 2.1.0

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