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

Subversion Repositories usb_fpga_2_14

[/] [usb_fpga_2_14/] [trunk/] [fx2/] [ztex-isr.h] - Blame information for rev 2

Details | Compare with Previous | View Log

Line No. Rev Author Line
1 2 ZTEX
/*%
2
   ZTEX Firmware Kit for EZ-USB FX2 Microcontrollers
3
   Copyright (C) 2009-2017 ZTEX GmbH.
4
   http://www.ztex.de
5
 
6
   This Source Code Form is subject to the terms of the Mozilla Public
7
   License, v. 2.0. If a copy of the MPL was not distributed with this file,
8
   You can obtain one at http://mozilla.org/MPL/2.0/.
9
 
10
   Alternatively, the contents of this file may be used under the terms
11
   of the GNU General Public License Version 3, as described below:
12
 
13
   This program is free software; you can redistribute it and/or modify
14
   it under the terms of the GNU General Public License version 3 as
15
   published by the Free Software Foundation.
16
 
17
   This program is distributed in the hope that it will be useful, but
18
   WITHOUT ANY WARRANTY; without even the implied warranty of
19
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
20
   General Public License for more details.
21
 
22
   You should have received a copy of the GNU General Public License
23
   along with this program; if not, see http://www.gnu.org/licenses/.
24
%*/
25
 
26
/*
27
   Interrupt routines
28
*/
29
 
30
#ifndef[ZTEX_ISR_H]
31
#define[ZTEX_ISR_H]
32
 
33
__xdata BYTE ep0_prev_setup_request = 0xff;
34
__xdata BYTE ep0_vendor_cmd_setup = 0;
35
 
36
__xdata WORD ISOFRAME_COUNTER[4] = {0, 0, 0, 0};    // counters for iso frames automatically reset by sync frame request
37
 
38
/* *********************************************************************
39
   ***** toggleData ****************************************************
40
   ********************************************************************* */
41
static void resetToggleData () {
42
#define[RESET_TOGGLE_DATA_EP(][);][
43
#ifeq[EP$0_DIR][OUT]
44
    TOGCTL = $0;                         // EP$0 out
45
    TOGCTL = $0 | bmBIT5;
46
#endif    
47
#ifeq[EP$0_DIR][IN]
48
    TOGCTL = bmBIT4 | $0;                        // EP$0 in
49
    TOGCTL = bmBIT4 | $0 | bmBIT5;
50
#endif]
51
 
52
    TOGCTL = 0;                          // EP0 out
53
    TOGCTL = 0 | bmBIT5;
54
    TOGCTL = 0x10;                      // EP0 in
55
    TOGCTL = 0x10 | bmBIT5;
56
#ifeq[EP1OUT_DIR][OUT]
57
    TOGCTL = 1;                         // EP1 out
58
    TOGCTL = 1 | bmBIT5;
59
#endif    
60
#ifeq[EP1IN_DIR][IN]
61
    TOGCTL = 0x11;                      // EP1 in
62
    TOGCTL = 0x11 | bmBIT5;
63
#endif    
64
    RESET_TOGGLE_DATA_EP(2);
65
    RESET_TOGGLE_DATA_EP(4);
66
    RESET_TOGGLE_DATA_EP(6);
67
    RESET_TOGGLE_DATA_EP(8);
68
}
69
 
70
/* *********************************************************************
71
   ***** getStringDescriptor *******************************************
72
   ********************************************************************* */
73
#define[SEND_STRING_DESCRIPTOR(][);][sendStringDescriptor(LSB($0), MSB($0), sizeof($0) );]
74
 
75
static void sendStringDescriptor (BYTE loAddr, BYTE hiAddr, BYTE size)
76
{
77
    BYTE b,i;
78
    if ( size > 31) size = 31;
79
    if (SETUPDAT[7] == 0 && SETUPDAT[6]<size ) size = SETUPDAT[6];
80
    AUTOPTRSETUP = 7;
81
    AUTOPTRL1 = loAddr;
82
    AUTOPTRH1 = hiAddr;
83
    AUTOPTRL2 = (BYTE)(((unsigned short)(&EP0BUF))+1);
84
    AUTOPTRH2 = (BYTE)((((unsigned short)(&EP0BUF))+1) >> 8);
85
    XAUTODAT2 = 3;
86
    b=XAUTODAT1;
87
    for (i=0; (b!=0) && (i<size); i++) {
88
        XAUTODAT2 = b;
89
        XAUTODAT2 = 0;
90
        b = XAUTODAT1;
91
    }
92
    i = (i+1) << 1;
93
    EP0BUF[0] = i;
94
    EP0BUF[1] = 3;
95
    EP0BCH = 0;
96
    EP0BCL = i;
97
}
98
 
99
/* *********************************************************************
100
   ***** ep0_payload_update ********************************************
101
   ********************************************************************* */
102
static void ep0_payload_update() {
103
    ep0_payload_transfer = ( ep0_payload_remaining > 64 ) ? 64 : ep0_payload_remaining;
104
    ep0_payload_remaining -= ep0_payload_transfer;
105
}
106
 
107
 
108
/* *********************************************************************
109
   ***** ep0_vendor_cmd_su **********************************************
110
   ********************************************************************* */
111
static void ep0_vendor_cmd_su() {
112
    switch ( ep0_prev_setup_request ) {
113
        EP0_VENDOR_COMMANDS_SU;
114
        default:
115
            EP0CS |= 0x01;                      // set stall, unknown request
116
    }
117
}
118
 
119
/* *********************************************************************
120
   ***** SUDAV_ISR *****************************************************
121
   ********************************************************************* */
122
static void SUDAV_ISR () __interrupt
123
{
124
    BYTE a;
125
    ep0_prev_setup_request = bRequest;
126
    SUDPTRCTL = 1;
127
 
128
    // standard USB requests
129
    switch ( bRequest ) {
130
        case 0x00:      // get status 
131
            switch(SETUPDAT[0]) {
132
                case 0x80:              // self powered and remote 
133
                    EP0BUF[0] = 0;        // not self-powered, no remote wakeup
134
                    EP0BUF[1] = 0;
135
                    EP0BCH = 0;
136
                    EP0BCL = 2;
137
                    break;
138
                case 0x81:              // interface (reserved)
139
                    EP0BUF[0] = 0;        // always return zeros
140
                    EP0BUF[1] = 0;
141
                    EP0BCH = 0;
142
                    EP0BCL = 2;
143
                    break;
144
                case 0x82:
145
                    switch ( SETUPDAT[4] ) {
146
                        case 0x00 :
147
                        case 0x80 :
148
                            EP0BUF[0] = EP0CS & bmBIT0;
149
                            break;
150
                        case 0x01 :
151
                            EP0BUF[0] = EP1OUTCS & bmBIT0;
152
                            break;
153
                        case 0x81 :
154
                            EP0BUF[0] = EP1INCS & bmBIT0;
155
                            break;
156
                        default:
157
                            EP0BUF[0] = EPXCS[ ((SETUPDAT[4] >> 1)-1) & 3 ] & bmBIT0;
158
                            break;
159
                        }
160
                    EP0BUF[1] = 0;
161
                    EP0BCH = 0;
162
                    EP0BCL = 2;
163
                    break;
164
            }
165
            break;
166
        case 0x01:      // disable feature, e.g. remote wake, stall bit
167
            if ( SETUPDAT[0] == 2 && SETUPDAT[2] == 0 ) {
168
                switch ( SETUPDAT[4] ) {
169
                    case 0x00 :
170
                    case 0x80 :
171
                        EP0CS &= ~bmBIT0;
172
                        break;
173
                    case 0x01 :
174
                        EP1OUTCS &= ~bmBIT0;
175
                        break;
176
                    case 0x81 :
177
                         EP1INCS &= ~bmBIT0;
178
                        break;
179
                    default:
180
                         EPXCS[ ((SETUPDAT[4] >> 1)-1) & 3 ] &= ~bmBIT0;
181
                        break;
182
                }
183
            }
184
            break;
185
        case 0x03:      // enable feature, e.g. remote wake, test mode, stall bit
186
            if ( SETUPDAT[0] == 2 && SETUPDAT[2] == 0 ) {
187
                switch ( SETUPDAT[4] ) {
188
                    case 0x00 :
189
                    case 0x80 :
190
                        EP0CS |= bmBIT0;
191
                        break;
192
                    case 0x01 :
193
                        EP1OUTCS |= bmBIT0;
194
                        break;
195
                    case 0x81 :
196
                         EP1INCS |= bmBIT0;
197
                        break;
198
                    default:
199
                         EPXCS[ ((SETUPDAT[4] >> 1)-1) & 3 ] |= ~bmBIT0;
200
                        break;
201
                }
202
                a = ( (SETUPDAT[4] & 0x80) >> 3 ) | (SETUPDAT[4] & 0x0f);
203
                TOGCTL = a;
204
                TOGCTL = a | bmBIT5;
205
            }
206
            break;
207
        case 0x06:                      // get descriptor
208
            switch(SETUPDAT[3]) {
209
                case 0x01:              // device
210
                    SUDPTRH = MSB(&DeviceDescriptor);
211
                    SUDPTRL = LSB(&DeviceDescriptor);
212
                    break;
213
                case 0x02:              // configuration
214
                    if (USBCS & bmBIT7) {
215
                        SUDPTRH = MSB(&HighSpeedConfigDescriptor);
216
                        SUDPTRL = LSB(&HighSpeedConfigDescriptor);
217
                    }
218
                    else {
219
                        SUDPTRH = MSB(&FullSpeedConfigDescriptor);
220
                        SUDPTRL = LSB(&FullSpeedConfigDescriptor);
221
                    }
222
                    break;
223
                case 0x03:              // strings
224
                    switch (SETUPDAT[2]) {
225
                        case 1:
226
                            SEND_STRING_DESCRIPTOR(manufacturerString);
227
                            break;
228
                        case 2:
229
                            SEND_STRING_DESCRIPTOR(productString);
230
                            break;
231
                        case 3:
232
                            SEND_STRING_DESCRIPTOR(SN_STRING);
233
                            break;
234
                        case 4:
235
                            SEND_STRING_DESCRIPTOR(configurationString);
236
                            break;
237
                        default:
238
                            SUDPTRH = MSB(&LangStringDescriptor);
239
                            SUDPTRL = LSB(&LangStringDescriptor);
240
                            break;
241
                        }
242
                    break;
243
                case 0x06:              // device qualifier
244
                    SUDPTRH = MSB(&DeviceQualifierDescriptor);
245
                    SUDPTRL = LSB(&DeviceQualifierDescriptor);
246
                    break;
247
                case 0x07:              // other speed configuration
248
                    if (USBCS & bmBIT7) {
249
                        SUDPTRH = MSB(&FullSpeedConfigDescriptor);
250
                        SUDPTRL = LSB(&FullSpeedConfigDescriptor);
251
                    }
252
                    else {
253
                        SUDPTRH = MSB(&HighSpeedConfigDescriptor);
254
                        SUDPTRL = LSB(&HighSpeedConfigDescriptor);
255
                    }
256
                    break;
257
                default:
258
                    EP0CS |= 0x01;      // set stall, unknown descriptor
259
            }
260
            break;
261
        case 0x07:                      // set descriptor
262
            break;
263
        case 0x08:                      // get configuration
264
            EP0BUF[0] = 0;                // only one configuration
265
            EP0BCH = 0;
266
            EP0BCL = 1;
267
            break;
268
        case 0x09:                      // set configuration
269
            resetToggleData();
270
            break;                      // do nothing since we have only one configuration
271
        case 0x0a:                      // get alternate setting for an interface
272
            EP0BUF[0] = 0;                // only one alternate setting
273
            EP0BCH = 0;
274
            EP0BCL = 1;
275
            break;
276
        case 0x0b:                      // set alternate setting for an interface
277
            resetToggleData();
278
            break;                      // do nothing since we have only on alternate setting
279
        case 0x0c:                      // sync frame
280
            if ( SETUPDAT[0] == 0x82 ) {
281
                ISOFRAME_COUNTER[ ((SETUPDAT[4] >> 1)-1) & 3 ] = 0;
282
                EP0BUF[0] = USBFRAMEL;   // use current frame as sync frame, i hope that works
283
                EP0BUF[1] = USBFRAMEH;
284
                EP0BCH = 0;
285
                EP0BCL = 2;
286
            }
287
            break;                      // do nothing since we have only on alternate setting
288
 
289
    }
290
 
291
    // vendor request and commands
292
    switch ( bmRequestType ) {
293
        case 0xc0:                                      // vendor request 
294
            ep0_payload_remaining = (SETUPDAT[7] << 8) | SETUPDAT[6];
295
            ep0_payload_update();
296
 
297
            switch ( bRequest ) {
298
                case 0x22:                              // get ZTEX descriptor
299
                    SUDPTRCTL = 0;
300
                    EP0BCH = 0;
301
                    EP0BCL = ZTEX_DESCRIPTOR_LEN;
302
                    SUDPTRH = MSB(ZTEX_DESCRIPTOR_OFFS);
303
                    SUDPTRL = LSB(ZTEX_DESCRIPTOR_OFFS);
304
                    break;
305
                EP0_VENDOR_REQUESTS_SU;
306
                default:
307
                    EP0CS |= 0x01;                      // set stall, unknown request
308
            }
309
            break;
310
        case 0x40:                                      // vendor command
311
            /* vendor commands may overlap if they are send without pause. To avoid
312
               synchronization problems the setup sequences are executed in EP0OUT_ISR, i.e.
313
               after the first packet of payload data received. */
314
            if ( SETUPDAT[7]!=0 || SETUPDAT[6]!=0 ) {
315
                ep0_vendor_cmd_setup = 1;
316
                EP0BCL = 0;
317
                EXIF &= ~bmBIT4;                        // clear main USB interrupt flag
318
                USBIRQ = bmBIT0;                        // clear SUADV IRQ
319
                return;                                 // don't clear HSNAK bit. This is done after the command has completed
320
            }
321
            ep0_vendor_cmd_su();                        // setup sequences of vendor command with no payload ara executed immediately
322
            EP0BCL = 0;
323
            break;
324
    }
325
 
326
    EXIF &= ~bmBIT4;                                    // clear main USB interrupt flag
327
    USBIRQ = bmBIT0;                                    // clear SUADV IRQ
328
    EP0CS |= 0x80;                                      // clear the HSNAK bit
329
}
330
 
331
/* *********************************************************************
332
   ***** SOF_ISR *******************************************************
333
   ********************************************************************* */
334
void SOF_ISR() __interrupt
335
{
336
        EXIF &= ~bmBIT4;
337
        USBIRQ = bmBIT1;
338
}
339
 
340
/* *********************************************************************
341
   ***** SUTOK_ISR *****************************************************
342
   ********************************************************************* */
343
void SUTOK_ISR() __interrupt
344
{
345
        EXIF &= ~bmBIT4;
346
        USBIRQ = bmBIT2;
347
}
348
 
349
/* *********************************************************************
350
   ***** SUSP_ISR ******************************************************
351
   ********************************************************************* */
352
void SUSP_ISR() __interrupt
353
{
354
        EXIF &= ~bmBIT4;
355
        USBIRQ = bmBIT3;
356
}
357
 
358
/* *********************************************************************
359
   ***** URES_ISR ******************************************************
360
   ********************************************************************* */
361
void URES_ISR() __interrupt
362
{
363
        EXIF &= ~bmBIT4;
364
        USBIRQ = bmBIT4;
365
}
366
 
367
/* *********************************************************************
368
   ***** HSGRANT_ISR ***************************************************
369
   ********************************************************************* */
370
void HSGRANT_ISR() __interrupt
371
{
372
        EXIF &= ~bmBIT4;
373
//        while ( USBIRQ & bmBIT5 )
374
            USBIRQ = bmBIT5;
375
}
376
 
377
/* *********************************************************************
378
   ***** EP0ACK_ISR ****************************************************
379
   ********************************************************************* */
380
void EP0ACK_ISR() __interrupt
381
{
382
        EXIF &= ~bmBIT4;        // clear USB interrupt flag
383
        USBIRQ = bmBIT6;        // clear EP0ACK IRQ
384
}
385
 
386
/* *********************************************************************
387
   ***** EP0IN_ISR *****************************************************
388
   ********************************************************************* */
389
static void EP0IN_ISR () __interrupt
390
{
391
    EUSB = 0;                    // block all USB interrupts
392
    ep0_payload_update();
393
    switch ( ep0_prev_setup_request ) {
394
        EP0_VENDOR_REQUESTS_DAT;
395
        default:
396
            EP0BCH = 0;
397
            EP0BCL = 0;
398
    }
399
    EXIF &= ~bmBIT4;            // clear USB interrupt flag
400
    EPIRQ = bmBIT0;             // clear EP0IN IRQ
401
    EUSB = 1;
402
}
403
 
404
/* *********************************************************************
405
   ***** EP0OUT_ISR ****************************************************
406
   ********************************************************************* */
407
static void EP0OUT_ISR () __interrupt
408
{
409
    EUSB = 0;                    // block all USB interrupts
410
    if ( ep0_vendor_cmd_setup ) {
411
        ep0_vendor_cmd_setup = 0;
412
        ep0_payload_remaining = (SETUPDAT[7] << 8) | SETUPDAT[6];
413
        ep0_vendor_cmd_su();
414
    }
415
 
416
    ep0_payload_update();
417
 
418
    switch ( ep0_prev_setup_request ) {
419
        EP0_VENDOR_COMMANDS_DAT;
420
    }
421
 
422
    EP0BCL = 0;
423
 
424
    EXIF &= ~bmBIT4;            // clear main USB interrupt flag
425
    EPIRQ = bmBIT1;             // clear EP0OUT IRQ
426
    if ( ep0_payload_remaining == 0 ) {
427
        EP0CS |= 0x80;          // clear the HSNAK bit
428
    }
429
    EUSB = 1;
430
}
431
 
432
 
433
/* *********************************************************************
434
   ***** EP1IN_ISR *****************************************************
435
   ********************************************************************* */
436
void EP1IN_ISR() __interrupt
437
{
438
    EXIF &= ~bmBIT4;
439
    EPIRQ = bmBIT2;
440
 
441
}
442
 
443
/* *********************************************************************
444
   ***** EP1OUT_ISR ****************************************************
445
   ********************************************************************* */
446
void EP1OUT_ISR() __interrupt
447
{
448
    EXIF &= ~bmBIT4;
449
    EPIRQ = bmBIT3;
450
}
451
 
452
/* *********************************************************************
453
   ***** EP2_ISR *******************************************************
454
   ********************************************************************* */
455
void EP2_ISR() __interrupt
456
{
457
    EXIF &= ~bmBIT4;
458
    EPIRQ = bmBIT4;
459
}
460
 
461
/* *********************************************************************
462
   ***** EP4_ISR *******************************************************
463
   ********************************************************************* */
464
void EP4_ISR() __interrupt
465
{
466
    EXIF &= ~bmBIT4;
467
    EPIRQ = bmBIT5;
468
}
469
 
470
/* *********************************************************************
471
   ***** EP6_ISR *******************************************************
472
   ********************************************************************* */
473
void EP6_ISR() __interrupt
474
{
475
    EXIF &= ~bmBIT4;
476
    EPIRQ = bmBIT6;
477
}
478
 
479
/* *********************************************************************
480
   ***** EP8_ISR *******************************************************
481
   ********************************************************************* */
482
void EP8_ISR() __interrupt
483
{
484
    EXIF &= ~bmBIT4;
485
    EPIRQ = bmBIT7;
486
}
487
 
488
#endif   /* ZTEX_ISR_H */

powered by: WebSVN 2.1.0

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