]> git.donarmstrong.com Git - qmk_firmware.git/blob - tool/mbed/mbed-sdk/libraries/USBDevice/USBDevice/TARGET_RENESAS/TARGET_RZ_A1H/usb0/src/function/usb0_function_sub.c
Squashed 'tmk_core/' changes from 7967731..b9e0ea0
[qmk_firmware.git] / tool / mbed / mbed-sdk / libraries / USBDevice / USBDevice / TARGET_RENESAS / TARGET_RZ_A1H / usb0 / src / function / usb0_function_sub.c
1 /*******************************************************************************
2 * DISCLAIMER
3 * This software is supplied by Renesas Electronics Corporation and is only
4 * intended for use with Renesas products. No other uses are authorized. This
5 * software is owned by Renesas Electronics Corporation and is protected under
6 * all applicable laws, including copyright laws.
7 * THIS SOFTWARE IS PROVIDED "AS IS" AND RENESAS MAKES NO WARRANTIES REGARDING
8 * THIS SOFTWARE, WHETHER EXPRESS, IMPLIED OR STATUTORY, INCLUDING BUT NOT
9 * LIMITED TO WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE
10 * AND NON-INFRINGEMENT. ALL SUCH WARRANTIES ARE EXPRESSLY DISCLAIMED.
11 * TO THE MAXIMUM EXTENT PERMITTED NOT PROHIBITED BY LAW, NEITHER RENESAS
12 * ELECTRONICS CORPORATION NOR ANY OF ITS AFFILIATED COMPANIES SHALL BE LIABLE
13 * FOR ANY DIRECT, INDIRECT, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES FOR
14 * ANY REASON RELATED TO THIS SOFTWARE, EVEN IF RENESAS OR ITS AFFILIATES HAVE
15 * BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES.
16 * Renesas reserves the right, without notice, to make changes to this software
17 * and to discontinue the availability of this software. By using this software,
18 * you agree to the additional terms and conditions found by accessing the
19 * following link:
20 * http://www.renesas.com/disclaimer
21 * Copyright (C) 2012 - 2014 Renesas Electronics Corporation. All rights reserved.
22 *******************************************************************************/
23 /*******************************************************************************
24 * File Name    : usb0_function_sub.c
25 * $Rev: 1116 $
26 * $Date:: 2014-07-09 16:29:19 +0900#$
27 * Device(s)    : RZ/A1H
28 * Tool-Chain   :
29 * OS           : None
30 * H/W Platform :
31 * Description  : RZ/A1H R7S72100 USB Sample Program
32 * Operation    :
33 * Limitations  :
34 *******************************************************************************/
35
36
37 /*******************************************************************************
38 Includes   <System Includes> , "Project Includes"
39 *******************************************************************************/
40 #include "usb0_function.h"
41
42
43 /*******************************************************************************
44 Typedef definitions
45 *******************************************************************************/
46
47
48 /*******************************************************************************
49 Macro definitions
50 *******************************************************************************/
51
52
53 /*******************************************************************************
54 Imported global variables and functions (from other files)
55 *******************************************************************************/
56 #if 0
57 extern const uint16_t *g_usb0_function_EndPntPtr[];
58 extern uint8_t         g_usb0_function_DeviceDescriptor[];
59 extern uint8_t        *g_usb0_function_ConfigurationPtr[];
60 #endif
61
62
63 /*******************************************************************************
64 Exported global variables and functions (to be accessed by other files)
65 *******************************************************************************/
66
67
68 /*******************************************************************************
69 Private global variables and functions
70 *******************************************************************************/
71
72
73 /*******************************************************************************
74 * Function Name: usb0_function_ResetDCP
75 * Description  : Initializes the default control pipe(DCP).
76 * Outline      : Reset default control pipe
77 * Arguments    : none
78 * Return Value : none
79 *******************************************************************************/
80 void usb0_function_ResetDCP (void)
81 {
82     USB200.DCPCFG  = 0;
83 #if 0
84     USB200.DCPMAXP = g_usb0_function_DeviceDescriptor[7];
85 #else
86     USB200.DCPMAXP = 64;
87 #endif
88
89     USB200.CFIFOSEL  = (uint16_t)(USB_FUNCTION_BITMBW_8 | USB_FUNCTION_BITBYTE_LITTLE);
90     USB200.D0FIFOSEL = (uint16_t)(USB_FUNCTION_BITMBW_8 | USB_FUNCTION_BITBYTE_LITTLE);
91     USB200.D1FIFOSEL = (uint16_t)(USB_FUNCTION_BITMBW_8 | USB_FUNCTION_BITBYTE_LITTLE);
92 }
93
94 /*******************************************************************************
95 * Function Name: usb0_function_ResetEP
96 * Description  : Initializes the end point.
97 * Arguments    : uint16_t num          ; Configuration Number
98 * Return Value : none
99 *******************************************************************************/
100 #if 0
101 void usb0_function_ResetEP (uint16_t num)
102 {
103     uint16_t   pipe;
104     uint16_t   ep;
105     uint16_t   index;
106     uint16_t   buf;
107     uint16_t * tbl;
108
109     tbl = (uint16_t *)(g_usb0_function_EndPntPtr[num - 1]);
110
111     for (ep = 1; ep <= USB_FUNCTION_MAX_EP_NO; ++ep)
112     {
113         if (g_usb0_function_EPTableIndex[ep] != USB_FUNCTION_EP_ERROR)
114         {
115             index = (uint16_t)(USB_FUNCTION_EPTABLE_LENGTH * g_usb0_function_EPTableIndex[ep]);
116             pipe  = (uint16_t)(tbl[index + 0] & USB_FUNCTION_BITCURPIPE);
117
118             g_usb0_function_PipeTbl[pipe] = (uint16_t)( ((tbl[index + 1] & USB_FUNCTION_DIRFIELD) << 3) |
119                                                          ep                                             |
120                                                          (tbl[index + 0] & USB_FUNCTION_FIFO_USE)          );
121
122             if ((tbl[index + 1] & USB_FUNCTION_DIRFIELD) == USB_FUNCTION_DIR_P_OUT)
123             {
124                 tbl[index + 1] |= USB_FUNCTION_SHTNAKON;
125 #ifdef  __USB_DMA_BFRE_ENABLE__
126                 /* this routine cannnot be perfomred if read operation is executed in buffer size */
127                 if (((tbl[index + 0] & USB_FUNCTION_FIFO_USE) == USB_FUNCTION_D0FIFO_DMA) ||
128                     ((tbl[index + 0] & USB_FUNCTION_FIFO_USE) == USB_FUNCTION_D1FIFO_DMA))
129                 {
130                     tbl[index + 1] |= USB_FUNCTION_BFREON;
131                 }
132 #endif
133             }
134
135             /* Interrupt Disable */
136             buf  = USB200.BRDYENB;
137             buf &= (uint16_t)~g_usb0_function_bit_set[pipe];
138             USB200.BRDYENB = buf;
139             buf  = USB200.NRDYENB;
140             buf &= (uint16_t)~g_usb0_function_bit_set[pipe];
141             USB200.NRDYENB = buf;
142             buf  = USB200.BEMPENB;
143             buf &= (uint16_t)~g_usb0_function_bit_set[pipe];
144             USB200.BEMPENB = buf;
145
146             usb0_function_set_pid_nak(pipe);
147
148             /* CurrentPIPE Clear */
149             if (RZA_IO_RegRead_16(&USB200.CFIFOSEL,
150                                     USB_CFIFOSEL_CURPIPE_SHIFT,
151                                     USB_CFIFOSEL_CURPIPE) == pipe)
152             {
153                 RZA_IO_RegWrite_16(&USB200.CFIFOSEL,
154                                     0,
155                                     USB_CFIFOSEL_CURPIPE_SHIFT,
156                                     USB_CFIFOSEL_CURPIPE);
157             }
158
159             if (RZA_IO_RegRead_16(&USB200.D0FIFOSEL,
160                                     USB_DnFIFOSEL_CURPIPE_SHIFT,
161                                     USB_DnFIFOSEL_CURPIPE) == pipe)
162             {
163                 RZA_IO_RegWrite_16(&USB200.D0FIFOSEL,
164                                     0,
165                                     USB_DnFIFOSEL_CURPIPE_SHIFT,
166                                     USB_DnFIFOSEL_CURPIPE);
167             }
168
169             if (RZA_IO_RegRead_16(&USB200.D1FIFOSEL,
170                                     USB_DnFIFOSEL_CURPIPE_SHIFT,
171                                     USB_DnFIFOSEL_CURPIPE) == pipe)
172             {
173                 RZA_IO_RegWrite_16(&USB200.D1FIFOSEL,
174                                     0,
175                                     USB_DnFIFOSEL_CURPIPE_SHIFT,
176                                     USB_DnFIFOSEL_CURPIPE);
177             }
178
179             /* PIPE Configuration */
180             USB200.PIPESEL  = pipe;
181             USB200.PIPECFG  = tbl[index + 1];
182             USB200.PIPEBUF  = tbl[index + 2];
183             USB200.PIPEMAXP = tbl[index + 3];
184             USB200.PIPEPERI = tbl[index + 4];
185
186             g_usb0_function_pipecfg[pipe]  = tbl[index + 1];
187             g_usb0_function_pipebuf[pipe]  = tbl[index + 2];
188             g_usb0_function_pipemaxp[pipe] = tbl[index + 3];
189             g_usb0_function_pipeperi[pipe] = tbl[index + 4];
190
191             /* Buffer Clear */
192             usb0_function_set_sqclr(pipe);
193             usb0_function_aclrm(pipe);
194
195             /* init Global */
196             g_usb0_function_pipe_status[pipe]  = DEVDRV_USBF_PIPE_IDLE;
197             g_usb0_function_PipeDataSize[pipe] = 0;
198         }
199     }
200 }
201 #endif
202
203 /*******************************************************************************
204 * Function Name: usb0_function_EpToPipe
205 * Description  : Returns the pipe which end point specified by the argument is
206 *              : allocated to.
207 * Arguments    : uint16_t ep       ; Direction + Endpoint Number
208 * Return Value : USB_FUNCTION_EP_ERROR         : Error
209 *              : Others           : Pipe Number
210 *******************************************************************************/
211 uint16_t usb0_function_EpToPipe (uint16_t ep)
212 {
213     uint16_t pipe;
214
215     for (pipe = 1; pipe <= USB_FUNCTION_MAX_PIPE_NO; pipe++)
216     {
217         if ((g_usb0_function_PipeTbl[pipe] & 0x00ff) == ep)
218         {
219             return pipe;
220         }
221     }
222
223     return USB_FUNCTION_EP_ERROR;
224 }
225
226 /*******************************************************************************
227 * Function Name: usb0_function_InitEPTable
228 * Description  : Sets the end point by the Alternate setting value of the
229 *              : configuration number and the interface number specified by the
230 *              : argument.
231 * Arguments    : uint16_t Con_Num       ; Configuration Number
232 *              : uint16_t Int_Num       ; Interface Number
233 *              : uint16_t Alt_Num       ; Alternate Setting
234 * Return Value : none
235 *******************************************************************************/
236 #if 0
237 void usb0_function_InitEPTable (uint16_t Con_Num, uint16_t Int_Num, uint16_t Alt_Num)
238 {
239     uint8_t  * ptr;
240     uint16_t   point_interface;
241     uint16_t   point_endpoint;
242     uint16_t   length;
243     uint16_t   start;
244     uint16_t   numbers;
245     uint16_t   endpoint;
246
247     ptr = (uint8_t *)g_usb0_function_ConfigurationPtr[Con_Num - 1];
248     point_interface = *ptr;
249     length = (uint16_t)((uint16_t)*(ptr + 3) << 8 | (uint16_t)*(ptr + 2));
250     ptr  += *ptr;
251     start   = 0;
252     numbers = 0;
253     point_endpoint = 0;
254
255     for (; point_interface < length;)
256     {
257         switch (*(ptr + 1))                                     /* Descriptor Type ? */
258         {
259             case USB_FUNCTION_DT_INTERFACE:                     /* Interface */
260                 if ((*(ptr + 2) == Int_Num) && (*(ptr + 3) == Alt_Num))
261                 {
262                     numbers = *(ptr + 4);
263                 }
264                 else
265                 {
266                     start  += *(ptr + 4);
267                 }
268                 point_interface += *ptr;
269                 ptr += *ptr;
270             break;
271
272             case USB_FUNCTION_DT_ENDPOINT:                      /* Endpoint */
273                 if (point_endpoint < numbers)
274                 {
275                     endpoint = (uint16_t)(*(ptr + 2) & 0x0f);
276                     g_usb0_function_EPTableIndex[endpoint] = (uint16_t)(start + point_endpoint);
277                     ++point_endpoint;
278                 }
279                 point_interface += *ptr;
280                 ptr += *ptr;
281             break;
282
283             case USB_FUNCTION_DT_DEVICE:                        /* Device */
284             case USB_FUNCTION_DT_CONFIGURATION:                 /* Configuration */
285             case USB_FUNCTION_DT_STRING:                        /* String */
286             default:                                            /* Class, Vendor, else */
287                 point_interface += *ptr;
288                 ptr += *ptr;
289             break;
290         }
291     }
292 }
293 #endif
294
295 /*******************************************************************************
296 * Function Name: usb0_function_GetConfigNum
297 * Description  : Returns the number of configuration referring to the number of
298 *              : configuration described in the device descriptor.
299 * Arguments    : none
300 * Return Value : Number of possible configurations (bNumConfigurations).
301 *******************************************************************************/
302 #if 0
303 uint16_t usb0_function_GetConfigNum (void)
304 {
305     return (uint16_t)g_usb0_function_DeviceDescriptor[17];
306 }
307 #endif
308
309 /*******************************************************************************
310 * Function Name: usb0_function_GetInterfaceNum
311 * Description  : Returns the number of interface referring to the number of
312 *              : interface described in the configuration descriptor.
313 * Arguments    : uint16_t num       ; Configuration Number
314 * Return Value : Number of this interface (bNumInterfaces).
315 *******************************************************************************/
316 #if 0
317 uint16_t usb0_function_GetInterfaceNum (uint16_t num)
318 {
319     return (uint16_t)(*(g_usb0_function_ConfigurationPtr[num - 1] + 4));
320 }
321 #endif
322
323 /*******************************************************************************
324 * Function Name: usb0_function_GetAltNum
325 * Description  : Returns the Alternate setting value of the configuration number
326 *              : and the interface number specified by the argument.
327 * Arguments    : uint16_t Con_Num       ; Configuration Number
328 *              : uint16_t Int_Num       ; Interface Number
329 * Return Value : Value used to select this alternate setting(bAlternateSetting).
330 *******************************************************************************/
331 #if 0
332 uint16_t usb0_function_GetAltNum (uint16_t Con_Num, uint16_t Int_Num)
333 {
334     uint8_t  * ptr;
335     uint16_t   point;
336     uint16_t   alt_num = 0;
337     uint16_t   length;
338
339     ptr = (uint8_t *)(g_usb0_function_ConfigurationPtr[Con_Num - 1]);
340     point = ptr[0];
341     ptr  += ptr[0];                                 /* InterfaceDescriptor[0] */
342     length  = (uint16_t)(*(g_usb0_function_ConfigurationPtr[Con_Num - 1] + 2));
343     length |= (uint16_t)((uint16_t)(*(g_usb0_function_ConfigurationPtr[Con_Num - 1] + 3)) << 8);
344
345     for (; point < length;)                                 /* Search Descriptor Table size */
346     {
347         switch (ptr[1])                                     /* Descriptor Type ? */
348         {
349             case USB_FUNCTION_DT_INTERFACE:                 /* Interface */
350                 if (Int_Num == ptr[2])
351                 {
352                     alt_num = (uint16_t)ptr[3];             /* Alternate Number count */
353                 }
354                 point += ptr[0];
355                 ptr += ptr[0];
356             break;
357
358             case USB_FUNCTION_DT_DEVICE:                    /* Device */
359             case USB_FUNCTION_DT_CONFIGURATION:             /* Configuration */
360             case USB_FUNCTION_DT_STRING:                    /* String */
361             case USB_FUNCTION_DT_ENDPOINT:                  /* Endpoint */
362             default:                                        /* Class, Vendor, else */
363                 point += ptr[0];
364                 ptr   += ptr[0];
365             break;
366         }
367     }
368     return alt_num;
369 }
370 #endif
371
372 /*******************************************************************************
373 * Function Name: usb0_function_CheckRemoteWakeup
374 * Description  : Returns the result of the remote wake up function is supported
375 *              : or not referring to the configuration descriptor.
376 * Arguments    : none
377 * Return Value : DEVDRV_USBF_ON     :   Support Remote Wakeup
378 *              : DEVDRV_USBF_OFF    :   not Support Remote Wakeup
379 *******************************************************************************/
380 #if 0
381 uint16_t usb0_function_CheckRemoteWakeup (void)
382 {
383     uint8_t atr;
384
385     if (g_usb0_function_ConfigNum == 0)
386     {
387         return DEVDRV_USBF_OFF;
388     }
389
390     atr = *(g_usb0_function_ConfigurationPtr[g_usb0_function_ConfigNum - 1] + 7);
391
392     if (atr & USB_FUNCTION_CF_RWUP)
393     {
394         return DEVDRV_USBF_ON;
395     }
396
397     return DEVDRV_USBF_OFF;
398 }
399 #endif
400
401 /*******************************************************************************
402 * Function Name: usb0_function_clear_alt
403 * Description  : Initializes the Alternate setting area.
404 * Arguments    : none
405 * Return Value : none
406 *******************************************************************************/
407 #if 0
408 void usb0_function_clear_alt (void)
409 {
410     int i;
411
412     for (i = 0; i < USB_FUNCTION_ALT_NO; ++i)
413     {
414         g_usb0_function_Alternate[i] = 0;                           /* Alternate */
415     }
416 }
417 #endif
418
419 /*******************************************************************************
420 * Function Name: usb0_function_clear_pipe_tbl
421 * Description  : Initializes pipe definition table.
422 * Arguments    : none
423 * Return Value : none
424 *******************************************************************************/
425 void usb0_function_clear_pipe_tbl (void)
426 {
427     int pipe;
428
429     for (pipe = 0; pipe < (USB_FUNCTION_MAX_PIPE_NO + 1); ++pipe)
430     {
431         g_usb0_function_PipeTbl[pipe] = 0;
432     }
433 }
434
435 /*******************************************************************************
436 * Function Name: usb0_function_clear_ep_table_index
437 * Description  : Initializes the end point table index.
438 * Arguments    : none
439 * Return Value : none
440 *******************************************************************************/
441 #if 0
442 void usb0_function_clear_ep_table_index (void)
443 {
444     int ep;
445
446     for (ep = 0; ep <= USB_FUNCTION_MAX_EP_NO; ++ep)
447     {
448         g_usb0_function_EPTableIndex[ep] = USB_FUNCTION_EP_ERROR;
449     }
450 }
451 #endif
452
453 /* End of File */