summaryrefslogtreecommitdiff
path: root/tmk_core/tool/mbed/mbed-sdk/libraries/USBHost/USBHost/TARGET_RENESAS/TARGET_RZ_A1H/usb0/src/host/usb0_host_usbint.c
blob: f4e5a27c77bbe389521626ea8a28e05d5c45ffef (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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
/*******************************************************************************
* DISCLAIMER
* This software is supplied by Renesas Electronics Corporation and is only
* intended for use with Renesas products. No other uses are authorized. This
* software is owned by Renesas Electronics Corporation and is protected under
* all applicable laws, including copyright laws.
* THIS SOFTWARE IS PROVIDED "AS IS" AND RENESAS MAKES NO WARRANTIES REGARDING
* THIS SOFTWARE, WHETHER EXPRESS, IMPLIED OR STATUTORY, INCLUDING BUT NOT
* LIMITED TO WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE
* AND NON-INFRINGEMENT. ALL SUCH WARRANTIES ARE EXPRESSLY DISCLAIMED.
* TO THE MAXIMUM EXTENT PERMITTED NOT PROHIBITED BY LAW, NEITHER RENESAS
* ELECTRONICS CORPORATION NOR ANY OF ITS AFFILIATED COMPANIES SHALL BE LIABLE
* FOR ANY DIRECT, INDIRECT, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES FOR
* ANY REASON RELATED TO THIS SOFTWARE, EVEN IF RENESAS OR ITS AFFILIATES HAVE
* BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES.
* Renesas reserves the right, without notice, to make changes to this software
* and to discontinue the availability of this software. By using this software,
* you agree to the additional terms and conditions found by accessing the
* following link:
* http://www.renesas.com/disclaimer
* Copyright (C) 2012 - 2014 Renesas Electronics Corporation. All rights reserved.
*******************************************************************************/
/*******************************************************************************
* File Name    : usb0_host_usbint.c
* $Rev: 1116 $
* $Date:: 2014-07-09 16:29:19 +0900#$
* Device(s)    : RZ/A1H
* Tool-Chain   :
* OS           : None
* H/W Platform :
* Description  : RZ/A1H R7S72100 USB Sample Program
* Operation    :
* Limitations  :
*******************************************************************************/


/*******************************************************************************
Includes   <System Includes> , "Project Includes"
*******************************************************************************/
#include "usb0_host.h"
#if(1) /* ohci_wrapp */
#include "ohci_wrapp_RZ_A1_local.h"
#endif


/*******************************************************************************
Typedef definitions
*******************************************************************************/


/*******************************************************************************
Macro definitions
*******************************************************************************/


/*******************************************************************************
Imported global variables and functions (from other files)
*******************************************************************************/


/*******************************************************************************
Exported global variables and functions (to be accessed by other files)
*******************************************************************************/
static void usb0_host_interrupt1(void);
static void usb0_host_BRDYInterrupt(uint16_t Status, uint16_t Int_enbl);
static void usb0_host_NRDYInterrupt(uint16_t Status, uint16_t Int_enbl);
static void usb0_host_BEMPInterrupt(uint16_t Status, uint16_t Int_enbl);


/*******************************************************************************
Private global variables and functions
*******************************************************************************/


/*******************************************************************************
* Function Name: usb0_host_interrupt
* Description  : Executes USB interrupt.
*              : Register this function in the USB interrupt handler.
*              : Set CFIF0 in the pipe set before the interrupt after executing
*              : this function.
* Arguments    : uint32_t int_sense ; Interrupts detection mode
*              :                    ;  INTC_LEVEL_SENSITIVE : Level sense
*              :                    ;  INTC_EDGE_TRIGGER : Edge trigger
* Return Value : none
*******************************************************************************/
void usb0_host_interrupt (uint32_t int_sense)
{
    uint16_t savepipe1;
    uint16_t savepipe2;
    uint16_t buffer;

    savepipe1 = USB200.CFIFOSEL;
    savepipe2 = USB200.PIPESEL;
    usb0_host_interrupt1();

    /* Control transmission changes ISEL within interruption processing. */
    /* For this reason, write return of ISEL cannot be performed. */
    buffer = USB200.CFIFOSEL;
    buffer &= (uint16_t)~(USB_HOST_BITCURPIPE);
    buffer |= (uint16_t)(savepipe1 & USB_HOST_BITCURPIPE);
    USB200.CFIFOSEL = buffer;
    USB200.PIPESEL = savepipe2;
}

/*******************************************************************************
* Function Name: usb0_host_interrupt1
* Description  : Execue the USB interrupt.
* Arguments    : none
* Return Value : none
*******************************************************************************/
void usb0_host_interrupt1 (void)
{
    uint16_t intsts0;
    uint16_t intsts1;
    uint16_t intenb0;
    uint16_t intenb1;
    uint16_t brdysts;
    uint16_t nrdysts;
    uint16_t bempsts;
    uint16_t brdyenb;
    uint16_t nrdyenb;
    uint16_t bempenb;
    volatile uint16_t dumy_sts;

    intsts0 = USB200.INTSTS0;
    intsts1 = USB200.INTSTS1;
    intenb0 = USB200.INTENB0;
    intenb1 = USB200.INTENB1;

    if ((intsts1 & USB_HOST_BITBCHG) && (intenb1 & USB_HOST_BITBCHGE))
    {
            USB200.INTSTS1 = (uint16_t)~USB_HOST_BITBCHG;
            RZA_IO_RegWrite_16(&USB200.INTENB1,
                                0,
                                USB_INTENB1_BCHGE_SHIFT,
                                USB_INTENB1_BCHGE);
            g_usb0_host_bchg_flag = USB_HOST_YES;
    }
    else if ((intsts1 & USB_HOST_BITSACK) && (intenb1 & USB_HOST_BITSACKE))
    {
        USB200.INTSTS1 = (uint16_t)~USB_HOST_BITSACK;
#if(1) /* ohci_wrapp */
        ohciwrapp_loc_TransEnd(USB_HOST_PIPE0, TD_CC_NOERROR);
#else
        g_usb0_host_CmdStage &= (~USB_HOST_CMD_FIELD);
        g_usb0_host_CmdStage |= USB_HOST_CMD_DONE;
#endif
    }
    else if ((intsts1 & USB_HOST_BITSIGN) && (intenb1 & USB_HOST_BITSIGNE))
    {
        USB200.INTSTS1 = (uint16_t)~USB_HOST_BITSIGN;
#if(1) /* ohci_wrapp */
        g_usb0_host_pipe_status[USB_HOST_PIPE0] = USB_HOST_PIPE_NORES;  /* exit NORES */
        ohciwrapp_loc_TransEnd(USB_HOST_PIPE0, TD_CC_STALL);
#else
        g_usb0_host_CmdStage &= (~USB_HOST_CMD_FIELD);
        g_usb0_host_CmdStage |= USB_HOST_CMD_NORES;
#endif
    }
    else if (((intsts1 & USB_HOST_BITDTCH) == USB_HOST_BITDTCH)
          && ((intenb1 & USB_HOST_BITDTCHE) == USB_HOST_BITDTCHE))
    {
        USB200.INTSTS1 = (uint16_t)~USB_HOST_BITDTCH;
        RZA_IO_RegWrite_16(&USB200.INTENB1,
                            0,
                            USB_INTENB1_DTCHE_SHIFT,
                            USB_INTENB1_DTCHE);
        g_usb0_host_detach_flag = USB_HOST_YES;

        Userdef_USB_usb0_host_detach();

        usb0_host_UsbDetach2();
    }
    else if (((intsts1 & USB_HOST_BITATTCH) == USB_HOST_BITATTCH)
          && ((intenb1 & USB_HOST_BITATTCHE) == USB_HOST_BITATTCHE))
    {
        USB200.INTSTS1 = (uint16_t)~USB_HOST_BITATTCH;
        RZA_IO_RegWrite_16(&USB200.INTENB1,
                            0,
                            USB_INTENB1_ATTCHE_SHIFT,
                            USB_INTENB1_ATTCHE);
        g_usb0_host_attach_flag = USB_HOST_YES;

        Userdef_USB_usb0_host_attach();

        usb0_host_UsbAttach();
    }
    else if ((intsts0 & intenb0 & (USB_HOST_BITBEMP | USB_HOST_BITNRDY | USB_HOST_BITBRDY)))
    {
        brdysts = USB200.BRDYSTS;
        nrdysts = USB200.NRDYSTS;
        bempsts = USB200.BEMPSTS;
        brdyenb = USB200.BRDYENB;
        nrdyenb = USB200.NRDYENB;
        bempenb = USB200.BEMPENB;

        if ((intsts0 & USB_HOST_BITBRDY) && (intenb0 & USB_HOST_BITBRDYE) && (brdysts & brdyenb))
        {
            usb0_host_BRDYInterrupt(brdysts, brdyenb);
        }
        else if ((intsts0 & USB_HOST_BITBEMP) && (intenb0 & USB_HOST_BITBEMPE) && (bempsts & bempenb))
        {
            usb0_host_BEMPInterrupt(bempsts, bempenb);
        }
        else if ((intsts0 & USB_HOST_BITNRDY) && (intenb0 & USB_HOST_BITNRDYE) && (nrdysts & nrdyenb))
        {
            usb0_host_NRDYInterrupt(nrdysts, nrdyenb);
        }
        else
        {
            /* Do Nothing */
        }
    }
    else
    {
        /* Do Nothing */
    }

    /* Three dummy read for clearing interrupt requests */
    dumy_sts = USB200.INTSTS0;
    dumy_sts = USB200.INTSTS1;

}

/*******************************************************************************
* Function Name: usb0_host_BRDYInterrupt
* Description  : Executes USB BRDY interrupt.
* Arguments    : uint16_t Status   ; BRDYSTS Register Value
*              : uint16_t Int_enbl ; BRDYENB Register Value
* Return Value : none
*******************************************************************************/
void usb0_host_BRDYInterrupt (uint16_t Status, uint16_t Int_enbl)
{
    uint16_t          buffer;
    volatile uint16_t dumy_sts;

    if ((Status & g_usb0_host_bit_set[USB_HOST_PIPE0]) && (Int_enbl & g_usb0_host_bit_set[USB_HOST_PIPE0]))
    {
        USB200.BRDYSTS = (uint16_t)~g_usb0_host_bit_set[USB_HOST_PIPE0];

#if(1) /* ohci_wrapp */
        switch ((g_usb0_host_CmdStage & (USB_HOST_STAGE_FIELD | USB_HOST_CMD_FIELD)))
        {
            case (USB_HOST_STAGE_STATUS | USB_HOST_CMD_DOING):
                buffer  = usb0_host_read_buffer_c(USB_HOST_PIPE0);
                usb0_host_disable_brdy_int(USB_HOST_PIPE0);
                g_usb0_host_CmdStage &= (~USB_HOST_CMD_FIELD);
                g_usb0_host_CmdStage |= USB_HOST_CMD_DONE;
                ohciwrapp_loc_TransEnd(USB_HOST_PIPE0, TD_CC_NOERROR);
            break;

            case (USB_HOST_STAGE_DATA | USB_HOST_CMD_DOING):
                buffer  = usb0_host_read_buffer_c(USB_HOST_PIPE0);
                switch (buffer)
                {
                    case USB_HOST_READING:                  /* Continue of data read */
                    break;

                    case USB_HOST_READEND:                  /* End of data read */
                    case USB_HOST_READSHRT:                 /* End of data read */
                        usb0_host_disable_brdy_int(USB_HOST_PIPE0);
                        g_usb0_host_CmdStage &= (~USB_HOST_CMD_FIELD);
                        g_usb0_host_CmdStage |= USB_HOST_CMD_DONE;
                        ohciwrapp_loc_TransEnd(USB_HOST_PIPE0, TD_CC_NOERROR);
                    break;

                    case USB_HOST_READOVER:                 /* buffer over */
                        USB200.CFIFOCTR = USB_HOST_BITBCLR;
                        usb0_host_disable_brdy_int(USB_HOST_PIPE0);
                        g_usb0_host_CmdStage &= (~USB_HOST_CMD_FIELD);
                        g_usb0_host_CmdStage |= USB_HOST_CMD_DONE;
                        ohciwrapp_loc_TransEnd(USB_HOST_PIPE0, TD_CC_NOERROR);
                    break;

                    case USB_HOST_FIFOERROR:                    /* FIFO access error */
                    default:
                    break;
                }
            break;

            default:
            break;
        }
#else
        switch ((g_usb0_host_CmdStage & (USB_HOST_MODE_FIELD | USB_HOST_STAGE_FIELD | USB_HOST_CMD_FIELD)))
        {
            case (USB_HOST_MODE_WRITE   | USB_HOST_STAGE_STATUS | USB_HOST_CMD_DOING):
            case (USB_HOST_MODE_NO_DATA | USB_HOST_STAGE_STATUS | USB_HOST_CMD_DOING):
                buffer  = usb0_host_read_buffer_c(USB_HOST_PIPE0);
                usb0_host_disable_brdy_int(USB_HOST_PIPE0);
                g_usb0_host_CmdStage &= (~USB_HOST_CMD_FIELD);
                g_usb0_host_CmdStage |= USB_HOST_CMD_DONE;
            break;

            case (USB_HOST_MODE_READ   | USB_HOST_STAGE_DATA | USB_HOST_CMD_DOING):
                buffer  = usb0_host_read_buffer_c(USB_HOST_PIPE0);

                switch (buffer)
                {
                    case USB_HOST_READING:                  /* Continue of data read */
                    break;

                    case USB_HOST_READEND:                  /* End of data read */
                    case USB_HOST_READSHRT:                 /* End of data read */
                        usb0_host_disable_brdy_int(USB_HOST_PIPE0);
                        g_usb0_host_CmdStage &= (~USB_HOST_CMD_FIELD);
                        g_usb0_host_CmdStage |= USB_HOST_CMD_DONE;
                    break;

                    case USB_HOST_READOVER:                 /* buffer over */
                        USB200.CFIFOCTR = USB_HOST_BITBCLR;
                        usb0_host_disable_brdy_int(USB_HOST_PIPE0);
                        g_usb0_host_CmdStage &= (~USB_HOST_CMD_FIELD);
                        g_usb0_host_CmdStage |= USB_HOST_CMD_DONE;
                    break;

                    case USB_HOST_FIFOERROR:                    /* FIFO access error */
                    default:
                    break;
                }
            break;

            default:
            break;
        }
#endif
    }
    else
    {
        usb0_host_brdy_int(Status, Int_enbl);
    }

    /* Three dummy reads for clearing interrupt requests */
    dumy_sts = USB200.BRDYSTS;
}

/*******************************************************************************
* Function Name: usb0_host_NRDYInterrupt
* Description  : Executes USB NRDY interrupt.
* Arguments    : uint16_t Status        ; NRDYSTS Register Value
*              : uint16_t Int_enbl      ; NRDYENB Register Value
* Return Value : none
*******************************************************************************/
void usb0_host_NRDYInterrupt (uint16_t Status, uint16_t Int_enbl)
{
    uint16_t          pid;
    volatile uint16_t dumy_sts;

    if ((Status & g_usb0_host_bit_set[USB_HOST_PIPE0]) && (Int_enbl & g_usb0_host_bit_set[USB_HOST_PIPE0]))
    {
        USB200.NRDYSTS = (uint16_t)~g_usb0_host_bit_set[USB_HOST_PIPE0];
        pid = usb0_host_get_pid(USB_HOST_PIPE0);

        if ((pid == USB_HOST_PID_STALL) || (pid == USB_HOST_PID_STALL2))
        {
            g_usb0_host_CmdStage &= (~USB_HOST_CMD_FIELD);
            g_usb0_host_CmdStage |= USB_HOST_CMD_STALL;
#if(1) /* ohci_wrapp */
            g_usb0_host_pipe_status[USB_HOST_PIPE0] = USB_HOST_PIPE_STALL;
            ohciwrapp_loc_TransEnd(USB_HOST_PIPE0, TD_CC_STALL);
#endif
        }
        else if (pid  == USB_HOST_PID_NAK)
        {
            g_usb0_host_CmdStage &= (~USB_HOST_CMD_FIELD);
            g_usb0_host_CmdStage |= USB_HOST_CMD_NORES;
#if(1) /* ohci_wrapp */
            g_usb0_host_pipe_status[USB_HOST_PIPE0] = USB_HOST_PIPE_NORES;
            ohciwrapp_loc_TransEnd(USB_HOST_PIPE0, TD_CC_STALL);
#endif
        }
        else
        {
            /* Do Nothing */
        }
    }
    else
    {
        usb0_host_nrdy_int(Status, Int_enbl);
    }

    /* Three dummy reads for clearing interrupt requests */
    dumy_sts = USB200.NRDYSTS;
}

/*******************************************************************************
* Function Name: usb0_host_BEMPInterrupt
* Description  : Executes USB BEMP interrupt.
* Arguments    : uint16_t Status        ; BEMPSTS Register Value
*              : uint16_t Int_enbl      ; BEMPENB Register Value
* Return Value : none
*******************************************************************************/
void usb0_host_BEMPInterrupt (uint16_t Status, uint16_t Int_enbl)
{
    uint16_t          buffer;
    uint16_t          pid;
    volatile uint16_t dumy_sts;

    if ((Status & g_usb0_host_bit_set[USB_HOST_PIPE0]) && (Int_enbl & g_usb0_host_bit_set[USB_HOST_PIPE0]))
    {
        USB200.BEMPSTS = (uint16_t)~g_usb0_host_bit_set[USB_HOST_PIPE0];
        pid = usb0_host_get_pid(USB_HOST_PIPE0);

        if ((pid == USB_HOST_PID_STALL) || (pid == USB_HOST_PID_STALL2))
        {
            g_usb0_host_CmdStage &= (~USB_HOST_CMD_FIELD);
            g_usb0_host_CmdStage |= USB_HOST_CMD_STALL;
#if(1) /* ohci_wrapp */
            g_usb0_host_pipe_status[USB_HOST_PIPE0] = USB_HOST_PIPE_STALL;      /* exit STALL */
            ohciwrapp_loc_TransEnd(USB_HOST_PIPE0, TD_CC_STALL);
#endif
        }
        else
        {
#if(1) /* ohci_wrapp */
            switch ((g_usb0_host_CmdStage & (USB_HOST_STAGE_FIELD | USB_HOST_CMD_FIELD)))
            {
                case (USB_HOST_STAGE_STATUS | USB_HOST_CMD_DOING):
                    g_usb0_host_CmdStage &= (~USB_HOST_CMD_FIELD);
                    g_usb0_host_CmdStage |= USB_HOST_CMD_DONE;
                    ohciwrapp_loc_TransEnd(USB_HOST_PIPE0, TD_CC_NOERROR);
                break;

                case (USB_HOST_STAGE_DATA | USB_HOST_CMD_DOING):
                    buffer  = usb0_host_write_buffer(USB_HOST_PIPE0);
                    switch (buffer)
                    {
                        case USB_HOST_WRITING:                  /* Continue of data write */
                        case USB_HOST_WRITEEND:                 /* End of data write (zero-length) */
                        break;

                        case USB_HOST_WRITESHRT:                    /* End of data write */
                            g_usb0_host_CmdStage &= (~USB_HOST_STAGE_FIELD);
                            g_usb0_host_CmdStage |= USB_HOST_STAGE_STATUS;
                            ohciwrapp_loc_TransEnd(USB_HOST_PIPE0, TD_CC_NOERROR);
                        break;

                        case USB_HOST_FIFOERROR:                    /* FIFO access error */
                        default:
                        break;
                    }
                break;

                default:
                    /* do nothing */
                break;
            }
#else
            switch ((g_usb0_host_CmdStage & (USB_HOST_MODE_FIELD | USB_HOST_STAGE_FIELD | USB_HOST_CMD_FIELD)))
            {
                case (USB_HOST_MODE_READ | USB_HOST_STAGE_STATUS | USB_HOST_CMD_DOING):
                    g_usb0_host_CmdStage &= (~USB_HOST_CMD_FIELD);
                    g_usb0_host_CmdStage |= USB_HOST_CMD_DONE;
                break;

                case (USB_HOST_MODE_WRITE | USB_HOST_STAGE_DATA | USB_HOST_CMD_DOING):
                    buffer  = usb0_host_write_buffer(USB_HOST_PIPE0);
                    switch (buffer)
                    {
                        case USB_HOST_WRITING:                  /* Continue of data write */
                        case USB_HOST_WRITEEND:                 /* End of data write (zero-length) */
                        break;

                        case USB_HOST_WRITESHRT:                    /* End of data write */
                            g_usb0_host_CmdStage &= (~USB_HOST_STAGE_FIELD);
                            g_usb0_host_CmdStage |= USB_HOST_STAGE_STATUS;
                        break;

                        case USB_HOST_FIFOERROR:                    /* FIFO access error */
                        default:
                        break;
                    }
                break;

                case (USB_HOST_MODE_WRITE | USB_HOST_STAGE_STATUS | USB_HOST_CMD_DOING):
                    g_usb0_host_CmdStage &= (~USB_HOST_CMD_FIELD);
                    g_usb0_host_CmdStage |= USB_HOST_CMD_IDLE;
                break;

                default:
                    /* do nothing */
                break;
            }
#endif
        }
    }
    else
    {
        usb0_host_bemp_int(Status, Int_enbl);
    }

    /* Three dummy reads for clearing interrupt requests */
    dumy_sts = USB200.BEMPSTS;
}

/* End of File */