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

Subversion Repositories gecko4

[/] [gecko4/] [trunk/] [GECKO4com/] [fx2_firmware/] [c/] [gecko4com_main.c] - Blame information for rev 2

Details | Compare with Previous | View Log

Line No. Rev Author Line
1 2 ktt1
/******************************************************************************/
2
/*            _   _            __   ____                                      */
3
/*           / / | |          / _| |  __|                                     */
4
/*           | |_| |  _   _  / /   | |_                                       */
5
/*           |  _  | | | | | | |   |  _|                                      */
6
/*           | | | | | |_| | \ \_  | |__                                      */
7
/*           |_| |_| \_____|  \__| |____| microLab                            */
8
/*                                                                            */
9
/*           Bern University of Applied Sciences (BFH)                        */
10
/*           Quellgasse 21                                                    */
11
/*           Room HG 4.33                                                     */
12
/*           2501 Biel/Bienne                                                 */
13
/*           Switzerland                                                      */
14
/*                                                                            */
15
/*           http://www.microlab.ch                                           */
16
/******************************************************************************/
17
/* GECKO4COM
18
 
19
 
20
   This program is free software: you can redistribute it and/or modify
21
   it under the terms of the GNU General Public License as published by
22
   the Free Software Foundation, either version 3 of the License, or
23
   (at your option) any later version.
24
 
25
   This program is distributed in the hope that it will be useful,
26
   but WITHOUT ANY WARRANTY; without even the implied warranty of
27
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
28
   GNU General Public License for more details.
29
   You should have received a copy of the GNU General Public License
30
   along with this program.  If not, see <http://www.gnu.org/licenses/>.
31
 
32
   Parts of this code are based on the USRP2 firmware (GNU Radio Project),
33
   version 3.0.2, Copyright 2003 Free Software Foundation, Inc
34
  ********************************************************************
35
*/
36
 
37
#include "fx2regs.h"
38
#include "isr.h"
39
#include "fx2utils.h"
40
#include "usb_common.h"
41
#include "usb_requests.h"
42
#include "syncdelay.h"
43
 
44
sbit at 0x80+0 PA0;
45
sbit at 0x80+1 PA1;
46
sbit at 0x80+2 PA2;
47
sbit at 0x80+3 PA3;
48
sbit at 0x80+4 PA4;
49
sbit at 0x80+5 PA5;
50
sbit at 0x80+6 PA6;
51
sbit at 0x80+7 PA7;
52
 
53
#define USB_TMC_STATUS_SUCCESS 1
54
#define USB_TMC_INDICATOR_PULSE_MASK 1<<2
55
#define USB_TMC_INITIATE_ABORT_BULK_OUT 1
56
#define USB_TMC_CHECK_ABORT_BULK_OUT_STATUS 2
57
#define USB_TMC_INITIATE_ABORT_BULK_IN 3
58
#define USB_TMC_CHECK_ABORT_BULK_IN_STATUS 4
59
#define USB_TMC_INITIATE_CLEAR 5
60
#define USB_TMC_CHECK_CLEAR_STATUS 6
61
#define USB_TMC_GET_CAPABILITIES 7
62
#define USB_TMC_INDICATOR_PULSE 64
63
#define USB_TMC_READ_STATUS_BYTE 128
64
 
65
#define IFCONFIG_INTERNAL_CLOCK 1<<7
66
#define IFCONFIG_INTERNAL_48MHZ_Clock 1<<6
67
#define IFCONFIG_DRIVE_IFCLK_PIN 1<<5
68
#define IFCONFIG_INVERT_IFCLK 1<<4
69
#define IFCONFIG_ASYNC_MODE 1<<3
70
#define IFCONFIG_USE_GSTATE 1<<2
71
#define IFCONFIG_USE_GPIF 2
72
#define IFCONFIG_USE_FIFOS 3
73
 
74
#define FIXED_EP2_PROG_FLAG 4
75
#define FIXED_EP4_PROG_FLAG 5
76
#define FIXED_EP6_PROG_FLAG 6
77
#define FIXED_EP8_PROG_FLAG 7
78
#define FIXED_EP2_EMPTY_FLAG 8
79
#define FIXED_EP4_EMPTY_FLAG 9
80
#define FIXED_EP6_EMPTY_FLAG 10
81
#define FIXED_EP8_EMPTY_FLAG 11
82
#define FIXED_EP2_FULL_FLAG 12
83
#define FIXED_EP4_FULL_FLAG 13
84
#define FIXED_EP6_FULL_FLAG 14
85
#define FIXED_EP8_FULL_FLAG 15
86
#define PORT_A_USE_FLAGD 1 << 7
87
#define PORT_A_USE_SLCS  1 << 6
88
#define PORT_A_USE_INT1  2
89
#define PORT_A_USE_INT0  1
90
#define PKTEND_ACTIVE_HIGH 1<<5
91
#define SLOE_ACTIVE_HIGH 1<<4
92
#define SLRD_ACTIVE_HIGH 1<<3
93
#define SLWR_ACTIVE_HIGH 1<<2
94
#define EMPTY_FLAG_ACTIVE_HIGH 2
95
#define FULL_FLAG_ACTIVE_HIGH 1
96
#define DISABLE_AUTO_ARMING 2
97
#define ENABLE_ENHANCED_PACKET_HANDLING 1
98
#define ACTIVE_ENDPOINT 1 << 7
99
#define IN_ENDPOINT 1<<6
100
#define ISOCHRONOUS_MODE 1 << 4
101
#define BULK_MODE 2 << 4
102
#define INTERRUPT_MODE 3 << 4
103
#define SIZE_1KB  1<<3
104
#define QUAD_BUFFERING 0
105
#define DOUBLE_BUFFERING 2
106
#define TRIPLE_BUFFERING 3
107
#define FIFO_IN_EARLY 1 << 6
108
#define FIFO_OUT_EARLY 1 << 5
109
#define FIFO_AUTO_OUT 1 << 4
110
#define FIFO_AUTO_IN 1 << 3
111
#define FIFO_ZEROLEN 1 << 2
112
#define FIFO_WORDWIDE 1
113
 
114
extern xdata char device_bus_attributes_hs[];
115
extern xdata char device_bus_attributes_fs[];
116
 
117
unsigned char app_class_cmd(void) {
118
   switch (bRequestType) {
119
      case 0xA1 : switch (bRequest) {
120
                     case USB_TMC_GET_CAPABILITIES :
121
                        EP0BUF[0] = USB_TMC_STATUS_SUCCESS;
122
                        EP0BUF[1] = 0;
123
                        EP0BUF[2] = 0;
124
                        EP0BUF[3] = 1;
125
                        EP0BUF[4] = USB_TMC_INDICATOR_PULSE_MASK;
126
                        EP0BUF[5] = 0;
127
                        EP0BUF[6] = 0;
128
                        EP0BUF[7] = 0;
129
                        EP0BUF[8] = 0;
130
                        EP0BUF[9] = 0;
131
                        EP0BUF[10] = 0;
132
                        EP0BUF[11] = 0;
133
                        EP0BUF[12] = 0;
134
                        EP0BUF[13] = 1;
135
                        EP0BUF[14] = 0;
136
                        EP0BUF[15] = 0;
137
                        EP0BUF[16] = 0;
138
                        EP0BUF[17] = 0;
139
                        EP0BUF[18] = 0;
140
                        EP0BUF[19] = 0;
141
                        EP0BUF[20] = 0;
142
                        EP0BUF[21] = 0;
143
                        EP0BUF[22] = 0;
144
                        EP0BUF[23] = 0;
145
                        EP0BCH = 0;
146
                        EP0BCL = 0x18;
147
                        return 1;
148
                     case USB_TMC_INDICATOR_PULSE :
149
                        IOD = 0xEF; /* indicator pulse */
150
                        EP0BUF[0] = USB_TMC_STATUS_SUCCESS;
151
                        IOD = 0xFF;
152
                        EP0BCH = 0;
153
                        EP0BCL = 1;
154
                        return 1;
155
                     case USB_TMC_INITIATE_CLEAR :
156
                        /* reset the stuff in the FPGA */
157
                        PA1 = 1;
158
                        NOP;
159
                        PA1 = 0;
160
                        EP0BUF[0] = USB_TMC_STATUS_SUCCESS;
161
                        EP0BCH = 0;
162
                        EP0BCL = 1;
163
                        return 1;
164
                     case USB_TMC_CHECK_CLEAR_STATUS :
165
                        EP0BUF[0] = USB_TMC_STATUS_SUCCESS;
166
                        EP0BUF[1] = 0;
167
                        EP0BCH = 0;
168
                        EP0BCL = 2;
169
                        return 1;
170
                     case USB_TMC_READ_STATUS_BYTE :
171
                        IOD = 0xFF; /* select the USB488 status word */
172
                        EP0BUF[0] = USB_TMC_STATUS_SUCCESS;
173
                        EP0BUF[1] = wValueL;
174
                        EP0BUF[2] = (IOD&0xF)<<2;/*status byte*/
175
                        EP0BCH = 0;
176
                        EP0BCL = 3;
177
                        return 1;
178
                     default : break;
179
                 }
180
      /* TODO: finish the abort handling! */
181
      case 0xA2 : switch (bRequest) {
182
                     case USB_TMC_INITIATE_ABORT_BULK_OUT :
183
                        IOD = 0xCF; /* read btag lo nibble */
184
                        EP0BUF[0] = USB_TMC_STATUS_SUCCESS;
185
                        EP0BUF[1] = IOD&0xF;
186
                        IOD= 0xDF; /* read btag hi nibble */
187
                        EP0BCH = 0;
188
                        EP0BUF[1] |= (IOD&0xF)<<4;
189
                        EP0BCL = 2;
190
                        return 1;
191
                     case USB_TMC_CHECK_ABORT_BULK_OUT_STATUS :
192
                        EP0BUF[0] = USB_TMC_STATUS_SUCCESS;
193
                        EP0BUF[1] = 0;
194
                        EP0BUF[2] = 0;
195
                        EP0BUF[3] = 0;
196
                        EP0BUF[4] = 0;
197
                        EP0BUF[5] = 0;
198
                        EP0BUF[6] = 0;
199
                        EP0BUF[7] = 0;
200
                        EP0BCH = 0;
201
                        EP0BCL = 8;
202
                        return 1;
203
                     case USB_TMC_INITIATE_ABORT_BULK_IN :
204
                        IOD = 0xCF; /* read btag lo nibble */
205
                        EP0BUF[0] = USB_TMC_STATUS_SUCCESS;
206
                        EP0BUF[1] = IOD&0xF;
207
                        IOD= 0xDF; /* read btag hi nibble */
208
                        EP0BCH = 0;
209
                        EP0BUF[1] |= (IOD&0xF)<<4;
210
                        EP0BCL = 2;
211
                        return 1;
212
                     case USB_TMC_CHECK_ABORT_BULK_IN_STATUS :
213
                        EP0BUF[0] = USB_TMC_STATUS_SUCCESS;
214
                        EP0BUF[1] = 0;
215
                        EP0BUF[2] = 0;
216
                        EP0BUF[3] = 0;
217
                        EP0BUF[4] = 0;
218
                        EP0BUF[5] = 0;
219
                        EP0BUF[6] = 0;
220
                        EP0BUF[7] = 0;
221
                        EP0BCH = 0;
222
                        EP0BCL = 8;
223
                        return 1;
224
                     default : break;
225
                  }
226
      default : break;
227
   }
228
   return 0;
229
}
230
 
231
unsigned char app_vendor_cmd(void) {
232
   return 0;
233
}
234
 
235
void setup_endpoint_fifos() {
236
   /* IMPORTANT: Endpoint configurations:
237
                 EP6 => USBTMC IN endpoint (to PC) double buffered 512 bytes
238
                 EP8 => USBTMC OUT endpoint (from PC) double buffered 512 bytes
239
                 both endpoints are 8 bytes fifo
240
   */
241
   IFCONFIG = IFCONFIG_USE_FIFOS;
242
   SYNCDELAY;
243
   SYNCDELAY;
244
   PORTACFG = PORT_A_USE_FLAGD | PORT_A_USE_INT0;
245
   SYNCDELAY;
246
 
247
   PINFLAGSAB = (FIXED_EP6_FULL_FLAG << 4)|
248
                 FIXED_EP8_EMPTY_FLAG; /* Flag B is EP6 Full Flag; Flag A is EP8 Empty Flag */
249
   SYNCDELAY;
250
   PINFLAGSCD = (FIXED_EP4_FULL_FLAG << 4)|
251
                 FIXED_EP2_EMPTY_FLAG;
252
   SYNCDELAY;
253
   FIFOPINPOLAR = 0; /* All FIFO signals active low */
254
   SYNCDELAY;
255
   REVCTL = 0; /* Auto Arming and no enhanced packet handling */
256
   SYNCDELAY;
257
 
258
   EP2CFG=0;   /* Disabled endpoint */
259
   SYNCDELAY;
260
   EP4CFG=0;   /* Disabled endpoint */
261
   SYNCDELAY;
262
   EP6CFG=ACTIVE_ENDPOINT|IN_ENDPOINT|BULK_MODE|DOUBLE_BUFFERING;
263
   SYNCDELAY;
264
   EP8CFG=ACTIVE_ENDPOINT|BULK_MODE; /* fixed to double buffering ! */
265
   SYNCDELAY;
266
 
267
   EP2FIFOCFG = 0;
268
   SYNCDELAY;
269
   EP4FIFOCFG = 0;
270
   SYNCDELAY;
271
   EP6FIFOCFG = 0;
272
   SYNCDELAY;
273
   EP8FIFOCFG = 0;
274
   SYNCDELAY;     /* Set all FIFOs to passive byte wide */
275
 
276
 
277
   FIFORESET=0x80;
278
   SYNCDELAY;
279
   FIFORESET=0x02;
280
   SYNCDELAY;
281
   FIFORESET=0x04;
282
   SYNCDELAY;
283
   FIFORESET=0x06;
284
   SYNCDELAY;
285
   FIFORESET=0x08;
286
   SYNCDELAY;
287
   FIFORESET=0x00;
288
   SYNCDELAY;     /* Reset all FIFOs */
289
 
290
   EP8FIFOCFG=FIFO_AUTO_OUT|FIFO_OUT_EARLY; /* Autoout and OUTEARLY */
291
   SYNCDELAY;
292
   EP6FIFOCFG=FIFO_IN_EARLY;
293
   SYNCDELAY;
294
 
295
 
296
}
297
 
298
 
299
 
300
void main(void) {
301
   EA = 0; /* disable all interrupts */
302
 
303
   PA1 = 1; /* indicate to the FPGA that the FX2 is still setting up things */
304
   PA3 = 0; /* Indicate to the FPGA that the FX2 uses full speed */
305
   OEA = 1<<3|1<<1; /* PA1 and PA3 are set to output, the rest of the pins as input */
306
 
307
   setup_autovectors();
308
   usb_install_handlers();
309
   setup_endpoint_fifos();
310
   OED = 0xF0; /* The high 4 pins of the d-port are outputs the low 4 are inputs */
311
   IOD = 0xFF; /* select the USB488 status word */
312
 
313
   EA = 1; /* enable all interrupts */
314
 
315
   fx2_renumerate();
316
 
317
   /* now everything should be fine */
318
   PA1 = 0; /* indicate to the FPGA that the FX2 is ready */
319
 
320
   while (1) {
321
      if(usb_setup_packet_avail()) usb_handle_setup_packet();
322
   }
323
}

powered by: WebSVN 2.1.0

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