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

Subversion Repositories or1k

[/] [or1k/] [trunk/] [or1ksim/] [testbench/] [acv_gpio.c] - Blame information for rev 1778

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

Line No. Rev Author Line
1 478 erez
/* GPIO test */
2
 
3 483 erez
#include "spr_defs.h"
4 478 erez
#include "support.h"
5 499 erez
#include "int.h"
6 478 erez
 
7
/* Relative Register Addresses */
8 502 erez
#define RGPIO_IN        0x00
9
#define RGPIO_OUT       0x04
10
#define RGPIO_OE        0x08
11
#define RGPIO_INTE      0x0C
12
#define RGPIO_PTRIG     0x10
13
#define RGPIO_AUX       0x14
14
#define RGPIO_CTRL      0x18
15
#define RGPIO_INTS      0x1C
16 478 erez
 
17
/* Fields inside RGPIO_CTRL */
18
#define RGPIO_CTRL_ECLK      0x00000001
19
#define RGPIO_CTRL_NEC       0x00000002
20
#define RGPIO_CTRL_INTE      0x00000004
21 502 erez
#define RGPIO_CTRL_INTS      0x00000008
22 478 erez
 
23
#define GPIO_BASE 0xB0000000LU
24
 
25 483 erez
#define GPIO_INT_LINE 23 /* To which interrupt is GPIO connected */
26
 
27 478 erez
typedef volatile unsigned long *GPIO_REG;
28
 
29
GPIO_REG rgpio_in = (unsigned long *)(GPIO_BASE + RGPIO_IN),
30
  rgpio_out = (unsigned long *)(GPIO_BASE + RGPIO_OUT),
31
  rgpio_oe = (unsigned long *)(GPIO_BASE + RGPIO_OE),
32
  rgpio_inte = (unsigned long *)(GPIO_BASE + RGPIO_INTE),
33
  rgpio_ptrig = (unsigned long *)(GPIO_BASE + RGPIO_PTRIG),
34
  rgpio_aux = (unsigned long *)(GPIO_BASE + RGPIO_AUX),
35 502 erez
  rgpio_ctrl = (unsigned long *)(GPIO_BASE + RGPIO_CTRL),
36
  rgpio_ints = (unsigned long *)(GPIO_BASE + RGPIO_INTS);
37 478 erez
 
38 483 erez
/* fails if x is false */
39
#define ASSERT(x) ((x)?1: fail (__FILE__, __LINE__))
40
 
41
static void fail (char *file, int line)
42
{
43 1024 simons
  printf( "Test failed in %s:%i\n", file, line );
44 483 erez
  report( 0xeeeeeeee );
45
  exit( 1 );
46
}
47
 
48
 
49
static void wait_input( unsigned long value )
50
{
51
  unsigned long first = *rgpio_in;
52
  if ( first == value )
53
    return;
54
  while ( 1 ) {
55
    unsigned long curr = *rgpio_in;
56
    if ( curr == value )
57
      return;
58
    if ( curr != first ) {
59 1024 simons
      printf( "While waiting for 0x%08lX, input changed from 0x%08lX to 0x%08lX\n", value, first, curr );
60 483 erez
      ASSERT( 0 );
61
    }
62
  }
63
}
64
 
65
static volatile unsigned int_count = 0;
66
 
67 499 erez
 
68
static void gpio_interrupt( void *arg )
69 483 erez
{
70
  ++ int_count;
71
}
72
 
73
static void init_interrupts( void )
74
{
75 499 erez
  int_init();
76 600 simons
  int_add( GPIO_INT_LINE, gpio_interrupt, 0);
77 483 erez
 
78
  /* Enable interrupts */
79 600 simons
  mtspr( SPR_SR, mfspr(SPR_SR) | SPR_SR_IEE );
80 483 erez
}
81
 
82
 
83
 
84
static void test_registers( void )
85
{
86 1024 simons
  printf( "Testing initial values of all registers\n" );
87 483 erez
  ASSERT( *rgpio_oe == 0 );
88
  ASSERT( *rgpio_inte == 0 );
89
  ASSERT( *rgpio_ptrig == 0 );
90
  ASSERT( *rgpio_ctrl == 0 );
91 502 erez
  ASSERT( *rgpio_ints == 0 );
92 483 erez
 
93 1024 simons
  printf( "Verifying that RGPIO_IN is read-only\n" );
94 483 erez
  {
95
    unsigned long value = *rgpio_in;
96
    unsigned i;
97
    *rgpio_in = ~value;
98
    ASSERT( *rgpio_in == value );
99
 
100
    for ( i = 0; i < 32; ++ i ) {
101
      *rgpio_in = 1LU << i;
102
      ASSERT( *rgpio_in == value );
103
    }
104
  }
105
}
106
 
107
 
108
static void test_simple_io( void )
109
{
110
  unsigned i;
111
  unsigned long oe;
112
 
113 1024 simons
  printf( "Testing simple I/O\n" );
114 483 erez
  for ( i = 1, oe = 1; i < 31; ++ i, oe = (oe << 1) | 1 ) {
115
    *rgpio_oe = oe;
116
 
117
    *rgpio_out = 0xFFFFFFFF;
118
    wait_input( 0xFFFFFFFF );
119
 
120
    *rgpio_out = 0x00000000;
121
    wait_input( 0x00000000 );
122
  }
123
}
124
 
125
 
126 502 erez
static void clear_interrupt_status( void )
127
{
128
  *rgpio_ctrl &= ~RGPIO_CTRL_INTS;
129
  *rgpio_ints = 0;
130
}
131
 
132
static void assert_good_interrupt( unsigned expected_count, unsigned long expected_mask )
133
{
134
  ASSERT( int_count == expected_count );
135
  ASSERT( (*rgpio_ctrl & RGPIO_CTRL_INTS) == RGPIO_CTRL_INTS );
136
  ASSERT( (*rgpio_in & ~*rgpio_oe) == expected_mask );
137
  ASSERT( (*rgpio_ints & ~*rgpio_oe) == expected_mask );
138
}
139
 
140 483 erez
static void test_interrupts( void )
141
{
142
  unsigned i;
143
 
144 1024 simons
  printf( "Testing interrupts\n" );
145 483 erez
 
146
  *rgpio_oe = 0x80000000;
147
  int_count = 0;
148
  *rgpio_inte = 0x7fffffff;
149
  *rgpio_ptrig = 0x7fffffff;
150
  *rgpio_ctrl = RGPIO_CTRL_INTE;
151
 
152
  *rgpio_out = 0x80000000;
153
  for ( i = 0; i < 31; ++ i ) {
154 502 erez
    /* Wait for interrupt */
155 483 erez
    while ( int_count <= i );
156 502 erez
    assert_good_interrupt( i + 1, 1LU << i );
157
    clear_interrupt_status();
158 483 erez
    *rgpio_out = (i % 2) ? 0x80000000 : 0;
159
  }
160
 
161
  /* Return things to normal */
162
  *rgpio_ctrl = 0;
163
}
164
 
165
static void test_external_clock( void )
166
{
167
  unsigned i;
168 1024 simons
  printf( "Testing external clock\n" );
169 483 erez
 
170
  *rgpio_oe = 0x80000000;
171
  *rgpio_inte = 0x7fffffff;
172
  *rgpio_ptrig = 0x7fffffff;
173
 
174
  /* Test positive edge */
175
  int_count = 0;
176
  *rgpio_ctrl = RGPIO_CTRL_INTE;
177
  *rgpio_out = 0x80000000;
178
  for ( i = 0; i < 31; ++ i ) {
179
    while ( int_count <= i );
180 502 erez
    assert_good_interrupt( i + 1, 1LU << i );
181
    clear_interrupt_status();
182 483 erez
    *rgpio_out = (i % 2) ? 0x80000000 : 0;
183
  }
184
 
185
  /* Test negative edge */
186
  int_count = 0;
187
  *rgpio_ctrl = RGPIO_CTRL_INTE | RGPIO_CTRL_NEC;
188
  *rgpio_out = 0x80000000;
189
  for ( i = 0; i < 31; ++ i ) {
190
    while ( int_count <= i );
191 502 erez
    assert_good_interrupt( i + 1, 1LU << i );
192
    clear_interrupt_status();
193 483 erez
    *rgpio_out = (i % 2) ? 0x80000000 : 0;
194
  }
195
 
196
  /* Return things to normal */
197
  *rgpio_ctrl = 0;
198
}
199
 
200
 
201
static void endshake( void )
202
{
203 1024 simons
  printf( "Finishing simulation\n" );
204 483 erez
  *rgpio_oe = 0xffff0000;
205
  *rgpio_out = 0x12340000;
206
  wait_input( 0x12345678 );
207
  *rgpio_oe = 0xffffffff;
208
  *rgpio_out = 0xDeadDead;
209
}
210
 
211
 
212 478 erez
int main()
213
{
214 1024 simons
  printf( "Starting GPIO test\n" );
215 478 erez
 
216 483 erez
  init_interrupts();
217
 
218
  test_registers();
219
  test_simple_io();
220
  test_interrupts();
221
  test_external_clock();
222
  endshake();
223 478 erez
 
224 1024 simons
  printf( "Ending GPIO test\n" );
225 478 erez
 
226
  report (0xdeaddead);
227
  return 0;
228
}
229
 
230
 

powered by: WebSVN 2.1.0

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