summaryrefslogtreecommitdiffstats
path: root/firmware/fx2/lib/isr.c
blob: 05412daf59cc16d1302b803a72309eca190a890b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
/* -*- c++ -*- */
/*
 * Copyright 2003 Free Software Foundation, Inc.
 * 
 * This file is part of GNU Radio
 * 
 * GNU Radio is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 3, or (at your option)
 * any later version.
 * 
 * GNU Radio is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with GNU Radio; see the file COPYING.  If not, write to
 * the Free Software Foundation, Inc., 51 Franklin Street,
 * Boston, MA 02110-1301, USA.
 */

#include "isr.h"
#include "fx2regs.h"
#include "syncdelay.h"

extern xdata unsigned char _standard_interrupt_vector[];
extern xdata unsigned char _usb_autovector[];
extern xdata unsigned char _fifo_gpif_autovector[];

#define LJMP_OPCODE	0x02

/*
 * Hook standard interrupt vector.
 *
 * vector_number is from the SV_<foo> list.
 * addr is the address of the interrupt service routine.
 */
void 
hook_sv (unsigned char vector_number, unsigned short addr)
{
  bit	t;
  
  // sanity checks

  if (vector_number < SV_MIN || vector_number > SV_MAX)
    return;

  if ((vector_number & 0x0f) != 0x03 && (vector_number & 0x0f) != 0x0b)
    return;

  t = EA;
  EA = 0;
  _standard_interrupt_vector[vector_number] = LJMP_OPCODE;
  _standard_interrupt_vector[vector_number + 1] = addr >> 8;
  _standard_interrupt_vector[vector_number + 2] = addr & 0xff;
  EA = t;
}

/*
 * Hook usb interrupt vector.
 *
 * vector_number is from the UV_<foo> list.
 * addr is the address of the interrupt service routine.
 */
void 
hook_uv (unsigned char vector_number, unsigned short addr)
{
  bit	t;
  
  // sanity checks

  if (vector_number < UV_MIN || vector_number > UV_MAX)
    return;

  if ((vector_number & 0x3) != 0)
    return;

  t = EA;
  EA = 0;
  _usb_autovector[vector_number] = LJMP_OPCODE;
  _usb_autovector[vector_number + 1] = addr >> 8;
  _usb_autovector[vector_number + 2] = addr & 0xff;
  EA = t;
}

/*
 * Hook fifo/gpif interrupt vector.
 *
 * vector_number is from the FGV_<foo> list.
 * addr is the address of the interrupt service routine.
 */
void 
hook_fgv (unsigned char vector_number, unsigned short addr)
{
  bit	t;
  
  // sanity checks

  if (vector_number < FGV_MIN || vector_number > FGV_MAX)
    return;

  if ((vector_number & 0x3) != 0)
    return;

  t = EA;
  EA = 0;
  _fifo_gpif_autovector[vector_number] = LJMP_OPCODE;
  _fifo_gpif_autovector[vector_number + 1] = addr >> 8;
  _fifo_gpif_autovector[vector_number + 2] = addr & 0xff;
  EA = t;
}

/*
 * One time call to enable autovectoring for both USB and FIFO/GPIF.
 *
 * This disables all USB and FIFO/GPIF interrupts and clears
 * any pending interrupts too.  It leaves the master USB and FIFO/GPIF
 * interrupts enabled.
 */
void
setup_autovectors (void)
{
  // disable master usb and fifo/gpif interrupt enables
  EIUSB = 0;
  EIEX4 = 0;

  hook_sv (SV_INT_2, (unsigned short) _usb_autovector);
  hook_sv (SV_INT_4, (unsigned short) _fifo_gpif_autovector);

  // disable all fifo interrupt enables
  SYNCDELAY;
  EP2FIFOIE = 0;	SYNCDELAY;
  EP4FIFOIE = 0;	SYNCDELAY;
  EP6FIFOIE = 0;	SYNCDELAY;
  EP8FIFOIE = 0;	SYNCDELAY;

  // clear all pending fifo irqs	
  EP2FIFOIRQ = 0xff;	SYNCDELAY;
  EP4FIFOIRQ = 0xff;	SYNCDELAY;
  EP6FIFOIRQ = 0xff;	SYNCDELAY;
  EP8FIFOIRQ = 0xff;	SYNCDELAY;

  IBNIE  = 0;
  IBNIRQ = 0xff;
  NAKIE  = 0;
  NAKIRQ = 0xff;
  USBIE  = 0;
  USBIRQ = 0xff;
  EPIE   = 0;
  EPIRQ  = 0xff;
  SYNCDELAY;	GPIFIE = 0;		
  SYNCDELAY;	GPIFIRQ = 0xff;
  USBERRIE = 0;
  USBERRIRQ = 0xff;
  CLRERRCNT = 0;
  
  INTSETUP = bmAV2EN | bmAV4EN | bmINT4IN;

  // clear master irq's for usb and fifo/gpif
  EXIF &= ~bmEXIF_USBINT;
  EXIF &= ~bmEXIF_IE4;
  
  // enable master usb and fifo/gpif interrrupts
  EIUSB = 1;
  EIEX4 = 1;
}