summaryrefslogtreecommitdiff
path: root/tool/mbed/mbed-sdk/libraries/USBDevice/USBDevice/TARGET_RENESAS/TARGET_RZ_A1H/usb0/src/function/usb0_function_sub.c
blob: df7fbf5bbb790737b7b3ff3b341dc6c68ac33d45 (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
/*******************************************************************************
* 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_function_sub.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_function.h"


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


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


/*******************************************************************************
Imported global variables and functions (from other files)
*******************************************************************************/
#if 0
extern const uint16_t *g_usb0_function_EndPntPtr[];
extern uint8_t         g_usb0_function_DeviceDescriptor[];
extern uint8_t        *g_usb0_function_ConfigurationPtr[];
#endif


/*******************************************************************************
Exported global variables and functions (to be accessed by other files)
*******************************************************************************/


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


/*******************************************************************************
* Function Name: usb0_function_ResetDCP
* Description  : Initializes the default control pipe(DCP).
* Outline      : Reset default control pipe
* Arguments    : none
* Return Value : none
*******************************************************************************/
void usb0_function_ResetDCP (void)
{
    USB200.DCPCFG  = 0;
#if 0
    USB200.DCPMAXP = g_usb0_function_DeviceDescriptor[7];
#else
    USB200.DCPMAXP = 64;
#endif

    USB200.CFIFOSEL  = (uint16_t)(USB_FUNCTION_BITMBW_8 | USB_FUNCTION_BITBYTE_LITTLE);
    USB200.D0FIFOSEL = (uint16_t)(USB_FUNCTION_BITMBW_8 | USB_FUNCTION_BITBYTE_LITTLE);
    USB200.D1FIFOSEL = (uint16_t)(USB_FUNCTION_BITMBW_8 | USB_FUNCTION_BITBYTE_LITTLE);
}

/*******************************************************************************
* Function Name: usb0_function_ResetEP
* Description  : Initializes the end point.
* Arguments    : uint16_t num          ; Configuration Number
* Return Value : none
*******************************************************************************/
#if 0
void usb0_function_ResetEP (uint16_t num)
{
    uint16_t   pipe;
    uint16_t   ep;
    uint16_t   index;
    uint16_t   buf;
    uint16_t * tbl;

    tbl = (uint16_t *)(g_usb0_function_EndPntPtr[num - 1]);

    for (ep = 1; ep <= USB_FUNCTION_MAX_EP_NO; ++ep)
    {
        if (g_usb0_function_EPTableIndex[ep] != USB_FUNCTION_EP_ERROR)
        {
            index = (uint16_t)(USB_FUNCTION_EPTABLE_LENGTH * g_usb0_function_EPTableIndex[ep]);
            pipe  = (uint16_t)(tbl[index + 0] & USB_FUNCTION_BITCURPIPE);

            g_usb0_function_PipeTbl[pipe] = (uint16_t)( ((tbl[index + 1] & USB_FUNCTION_DIRFIELD) << 3) |
                                                         ep                                             |
                                                         (tbl[index + 0] & USB_FUNCTION_FIFO_USE)          );

            if ((tbl[index + 1] & USB_FUNCTION_DIRFIELD) == USB_FUNCTION_DIR_P_OUT)
            {
                tbl[index + 1] |= USB_FUNCTION_SHTNAKON;
#ifdef  __USB_DMA_BFRE_ENABLE__
                /* this routine cannnot be perfomred if read operation is executed in buffer size */
                if (((tbl[index + 0] & USB_FUNCTION_FIFO_USE) == USB_FUNCTION_D0FIFO_DMA) ||
                    ((tbl[index + 0] & USB_FUNCTION_FIFO_USE) == USB_FUNCTION_D1FIFO_DMA))
                {
                    tbl[index + 1] |= USB_FUNCTION_BFREON;
                }
#endif
            }

            /* Interrupt Disable */
            buf  = USB200.BRDYENB;
            buf &= (uint16_t)~g_usb0_function_bit_set[pipe];
            USB200.BRDYENB = buf;
            buf  = USB200.NRDYENB;
            buf &= (uint16_t)~g_usb0_function_bit_set[pipe];
            USB200.NRDYENB = buf;
            buf  = USB200.BEMPENB;
            buf &= (uint16_t)~g_usb0_function_bit_set[pipe];
            USB200.BEMPENB = buf;

            usb0_function_set_pid_nak(pipe);

            /* CurrentPIPE Clear */
            if (RZA_IO_RegRead_16(&USB200.CFIFOSEL,
                                    USB_CFIFOSEL_CURPIPE_SHIFT,
                                    USB_CFIFOSEL_CURPIPE) == pipe)
            {
                RZA_IO_RegWrite_16(&USB200.CFIFOSEL,
                                    0,
                                    USB_CFIFOSEL_CURPIPE_SHIFT,
                                    USB_CFIFOSEL_CURPIPE);
            }

            if (RZA_IO_RegRead_16(&USB200.D0FIFOSEL,
                                    USB_DnFIFOSEL_CURPIPE_SHIFT,
                                    USB_DnFIFOSEL_CURPIPE) == pipe)
            {
                RZA_IO_RegWrite_16(&USB200.D0FIFOSEL,
                                    0,
                                    USB_DnFIFOSEL_CURPIPE_SHIFT,
                                    USB_DnFIFOSEL_CURPIPE);
            }

            if (RZA_IO_RegRead_16(&USB200.D1FIFOSEL,
                                    USB_DnFIFOSEL_CURPIPE_SHIFT,
                                    USB_DnFIFOSEL_CURPIPE) == pipe)
            {
                RZA_IO_RegWrite_16(&USB200.D1FIFOSEL,
                                    0,
                                    USB_DnFIFOSEL_CURPIPE_SHIFT,
                                    USB_DnFIFOSEL_CURPIPE);
            }

            /* PIPE Configuration */
            USB200.PIPESEL  = pipe;
            USB200.PIPECFG  = tbl[index + 1];
            USB200.PIPEBUF  = tbl[index + 2];
            USB200.PIPEMAXP = tbl[index + 3];
            USB200.PIPEPERI = tbl[index + 4];

            g_usb0_function_pipecfg[pipe]  = tbl[index + 1];
            g_usb0_function_pipebuf[pipe]  = tbl[index + 2];
            g_usb0_function_pipemaxp[pipe] = tbl[index + 3];
            g_usb0_function_pipeperi[pipe] = tbl[index + 4];

            /* Buffer Clear */
            usb0_function_set_sqclr(pipe);
            usb0_function_aclrm(pipe);

            /* init Global */
            g_usb0_function_pipe_status[pipe]  = DEVDRV_USBF_PIPE_IDLE;
            g_usb0_function_PipeDataSize[pipe] = 0;
        }
    }
}
#endif

/*******************************************************************************
* Function Name: usb0_function_EpToPipe
* Description  : Returns the pipe which end point specified by the argument is
*              : allocated to.
* Arguments    : uint16_t ep       ; Direction + Endpoint Number
* Return Value : USB_FUNCTION_EP_ERROR         : Error
*              : Others           : Pipe Number
*******************************************************************************/
uint16_t usb0_function_EpToPipe (uint16_t ep)
{
    uint16_t pipe;

    for (pipe = 1; pipe <= USB_FUNCTION_MAX_PIPE_NO; pipe++)
    {
        if ((g_usb0_function_PipeTbl[pipe] & 0x00ff) == ep)
        {
            return pipe;
        }
    }

    return USB_FUNCTION_EP_ERROR;
}

/*******************************************************************************
* Function Name: usb0_function_InitEPTable
* Description  : Sets the end point by the Alternate setting value of the
*              : configuration number and the interface number specified by the
*              : argument.
* Arguments    : uint16_t Con_Num       ; Configuration Number
*              : uint16_t Int_Num       ; Interface Number
*              : uint16_t Alt_Num       ; Alternate Setting
* Return Value : none
*******************************************************************************/
#if 0
void usb0_function_InitEPTable (uint16_t Con_Num, uint16_t Int_Num, uint16_t Alt_Num)
{
    uint8_t  * ptr;
    uint16_t   point_interface;
    uint16_t   point_endpoint;
    uint16_t   length;
    uint16_t   start;
    uint16_t   numbers;
    uint16_t   endpoint;

    ptr = (uint8_t *)g_usb0_function_ConfigurationPtr[Con_Num - 1];
    point_interface = *ptr;
    length = (uint16_t)((uint16_t)*(ptr + 3) << 8 | (uint16_t)*(ptr + 2));
    ptr  += *ptr;
    start   = 0;
    numbers = 0;
    point_endpoint = 0;

    for (; point_interface < length;)
    {
        switch (*(ptr + 1))                                     /* Descriptor Type ? */
        {
            case USB_FUNCTION_DT_INTERFACE:                     /* Interface */
                if ((*(ptr + 2) == Int_Num) && (*(ptr + 3) == Alt_Num))
                {
                    numbers = *(ptr + 4);
                }
                else
                {
                    start  += *(ptr + 4);
                }
                point_interface += *ptr;
                ptr += *ptr;
            break;

            case USB_FUNCTION_DT_ENDPOINT:                      /* Endpoint */
                if (point_endpoint < numbers)
                {
                    endpoint = (uint16_t)(*(ptr + 2) & 0x0f);
                    g_usb0_function_EPTableIndex[endpoint] = (uint16_t)(start + point_endpoint);
                    ++point_endpoint;
                }
                point_interface += *ptr;
                ptr += *ptr;
            break;

            case USB_FUNCTION_DT_DEVICE:                        /* Device */
            case USB_FUNCTION_DT_CONFIGURATION:                 /* Configuration */
            case USB_FUNCTION_DT_STRING:                        /* String */
            default:                                            /* Class, Vendor, else */
                point_interface += *ptr;
                ptr += *ptr;
            break;
        }
    }
}
#endif

/*******************************************************************************
* Function Name: usb0_function_GetConfigNum
* Description  : Returns the number of configuration referring to the number of
*              : configuration described in the device descriptor.
* Arguments    : none
* Return Value : Number of possible configurations (bNumConfigurations).
*******************************************************************************/
#if 0
uint16_t usb0_function_GetConfigNum (void)
{
    return (uint16_t)g_usb0_function_DeviceDescriptor[17];
}
#endif

/*******************************************************************************
* Function Name: usb0_function_GetInterfaceNum
* Description  : Returns the number of interface referring to the number of
*              : interface described in the configuration descriptor.
* Arguments    : uint16_t num       ; Configuration Number
* Return Value : Number of this interface (bNumInterfaces).
*******************************************************************************/
#if 0
uint16_t usb0_function_GetInterfaceNum (uint16_t num)
{
    return (uint16_t)(*(g_usb0_function_ConfigurationPtr[num - 1] + 4));
}
#endif

/*******************************************************************************
* Function Name: usb0_function_GetAltNum
* Description  : Returns the Alternate setting value of the configuration number
*              : and the interface number specified by the argument.
* Arguments    : uint16_t Con_Num       ; Configuration Number
*              : uint16_t Int_Num       ; Interface Number
* Return Value : Value used to select this alternate setting(bAlternateSetting).
*******************************************************************************/
#if 0
uint16_t usb0_function_GetAltNum (uint16_t Con_Num, uint16_t Int_Num)
{
    uint8_t  * ptr;
    uint16_t   point;
    uint16_t   alt_num = 0;
    uint16_t   length;

    ptr = (uint8_t *)(g_usb0_function_ConfigurationPtr[Con_Num - 1]);
    point = ptr[0];
    ptr  += ptr[0];                                 /* InterfaceDescriptor[0] */
    length  = (uint16_t)(*(g_usb0_function_ConfigurationPtr[Con_Num - 1] + 2));
    length |= (uint16_t)((uint16_t)(*(g_usb0_function_ConfigurationPtr[Con_Num - 1] + 3)) << 8);

    for (; point < length;)                                 /* Search Descriptor Table size */
    {
        switch (ptr[1])                                     /* Descriptor Type ? */
        {
            case USB_FUNCTION_DT_INTERFACE:                 /* Interface */
                if (Int_Num == ptr[2])
                {
                    alt_num = (uint16_t)ptr[3];             /* Alternate Number count */
                }
                point += ptr[0];
                ptr += ptr[0];
            break;

            case USB_FUNCTION_DT_DEVICE:                    /* Device */
            case USB_FUNCTION_DT_CONFIGURATION:             /* Configuration */
            case USB_FUNCTION_DT_STRING:                    /* String */
            case USB_FUNCTION_DT_ENDPOINT:                  /* Endpoint */
            default:                                        /* Class, Vendor, else */
                point += ptr[0];
                ptr   += ptr[0];
            break;
        }
    }
    return alt_num;
}
#endif

/*******************************************************************************
* Function Name: usb0_function_CheckRemoteWakeup
* Description  : Returns the result of the remote wake up function is supported
*              : or not referring to the configuration descriptor.
* Arguments    : none
* Return Value : DEVDRV_USBF_ON     :   Support Remote Wakeup
*              : DEVDRV_USBF_OFF    :   not Support Remote Wakeup
*******************************************************************************/
#if 0
uint16_t usb0_function_CheckRemoteWakeup (void)
{
    uint8_t atr;

    if (g_usb0_function_ConfigNum == 0)
    {
        return DEVDRV_USBF_OFF;
    }

    atr = *(g_usb0_function_ConfigurationPtr[g_usb0_function_ConfigNum - 1] + 7);

    if (atr & USB_FUNCTION_CF_RWUP)
    {
        return DEVDRV_USBF_ON;
    }

    return DEVDRV_USBF_OFF;
}
#endif

/*******************************************************************************
* Function Name: usb0_function_clear_alt
* Description  : Initializes the Alternate setting area.
* Arguments    : none
* Return Value : none
*******************************************************************************/
#if 0
void usb0_function_clear_alt (void)
{
    int i;

    for (i = 0; i < USB_FUNCTION_ALT_NO; ++i)
    {
        g_usb0_function_Alternate[i] = 0;                           /* Alternate */
    }
}
#endif

/*******************************************************************************
* Function Name: usb0_function_clear_pipe_tbl
* Description  : Initializes pipe definition table.
* Arguments    : none
* Return Value : none
*******************************************************************************/
void usb0_function_clear_pipe_tbl (void)
{
    int pipe;

    for (pipe = 0; pipe < (USB_FUNCTION_MAX_PIPE_NO + 1); ++pipe)
    {
        g_usb0_function_PipeTbl[pipe] = 0;
    }
}

/*******************************************************************************
* Function Name: usb0_function_clear_ep_table_index
* Description  : Initializes the end point table index.
* Arguments    : none
* Return Value : none
*******************************************************************************/
#if 0
void usb0_function_clear_ep_table_index (void)
{
    int ep;

    for (ep = 0; ep <= USB_FUNCTION_MAX_EP_NO; ++ep)
    {
        g_usb0_function_EPTableIndex[ep] = USB_FUNCTION_EP_ERROR;
    }
}
#endif

/* End of File */