1 /*******************************************************************************
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
20 * http://www.renesas.com/disclaimer
21 * Copyright (C) 2012 - 2014 Renesas Electronics Corporation. All rights reserved.
22 *******************************************************************************/
23 /*******************************************************************************
24 * File Name : usb1_function_sub.c
26 * $Date:: 2014-07-09 16:29:19 +0900#$
31 * Description : RZ/A1H R7S72100 USB Sample Program
34 *******************************************************************************/
37 /*******************************************************************************
38 Includes <System Includes> , "Project Includes"
39 *******************************************************************************/
40 #include "usb1_function.h"
43 /*******************************************************************************
45 *******************************************************************************/
48 /*******************************************************************************
50 *******************************************************************************/
53 /*******************************************************************************
54 Imported global variables and functions (from other files)
55 *******************************************************************************/
57 extern const uint16_t *g_usb1_function_EndPntPtr[];
58 extern uint8_t g_usb1_function_DeviceDescriptor[];
59 extern uint8_t *g_usb1_function_ConfigurationPtr[];
63 /*******************************************************************************
64 Exported global variables and functions (to be accessed by other files)
65 *******************************************************************************/
68 /*******************************************************************************
69 Private global variables and functions
70 *******************************************************************************/
73 /*******************************************************************************
74 * Function Name: usb1_function_ResetDCP
75 * Description : Initializes the default control pipe(DCP).
76 * Outline : Reset default control pipe
79 *******************************************************************************/
80 void usb1_function_ResetDCP (void)
84 USB201.DCPMAXP = g_usb1_function_DeviceDescriptor[7];
89 USB201.CFIFOSEL = (uint16_t)(USB_FUNCTION_BITMBW_8 | USB_FUNCTION_BITBYTE_LITTLE);
90 USB201.D0FIFOSEL = (uint16_t)(USB_FUNCTION_BITMBW_8 | USB_FUNCTION_BITBYTE_LITTLE);
91 USB201.D1FIFOSEL = (uint16_t)(USB_FUNCTION_BITMBW_8 | USB_FUNCTION_BITBYTE_LITTLE);
94 /*******************************************************************************
95 * Function Name: usb1_function_ResetEP
96 * Description : Initializes the end point.
97 * Arguments : uint16_t num ; Configuration Number
99 *******************************************************************************/
101 void usb1_function_ResetEP (uint16_t num)
109 tbl = (uint16_t *)(g_usb1_function_EndPntPtr[num - 1]);
111 for (ep = 1; ep <= USB_FUNCTION_MAX_EP_NO; ++ep)
113 if (g_usb1_function_EPTableIndex[ep] != USB_FUNCTION_EP_ERROR)
115 index = (uint16_t)(USB_FUNCTION_EPTABLE_LENGTH * g_usb1_function_EPTableIndex[ep]);
116 pipe = (uint16_t)(tbl[index + 0] & USB_FUNCTION_BITCURPIPE);
118 g_usb1_function_PipeTbl[pipe] = (uint16_t)(((tbl[index + 1] & USB_FUNCTION_DIRFIELD) << 3) |
120 (tbl[index + 0] & USB_FUNCTION_FIFO_USE));
122 if ((tbl[index + 1] & USB_FUNCTION_DIRFIELD) == USB_FUNCTION_DIR_P_OUT)
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))
130 tbl[index + 1] |= USB_FUNCTION_BFREON;
135 /* Interrupt Disable */
136 buf = USB201.BRDYENB;
137 buf &= (uint16_t)~g_usb1_function_bit_set[pipe];
138 USB201.BRDYENB = buf;
139 buf = USB201.NRDYENB;
140 buf &= (uint16_t)~g_usb1_function_bit_set[pipe];
141 USB201.NRDYENB = buf;
142 buf = USB201.BEMPENB;
143 buf &= (uint16_t)~g_usb1_function_bit_set[pipe];
144 USB201.BEMPENB = buf;
146 usb1_function_set_pid_nak(pipe);
148 /* CurrentPIPE Clear */
149 if (RZA_IO_RegRead_16(&USB201.CFIFOSEL,
150 USB_CFIFOSEL_CURPIPE_SHIFT,
151 USB_CFIFOSEL_CURPIPE) == pipe)
153 RZA_IO_RegWrite_16(&USB201.CFIFOSEL,
155 USB_CFIFOSEL_CURPIPE_SHIFT,
156 USB_CFIFOSEL_CURPIPE);
159 if (RZA_IO_RegRead_16(&USB201.D0FIFOSEL,
160 USB_DnFIFOSEL_CURPIPE_SHIFT,
161 USB_DnFIFOSEL_CURPIPE) == pipe)
163 RZA_IO_RegWrite_16(&USB201.D0FIFOSEL,
165 USB_DnFIFOSEL_CURPIPE_SHIFT,
166 USB_DnFIFOSEL_CURPIPE);
169 if (RZA_IO_RegRead_16(&USB201.D1FIFOSEL,
170 USB_DnFIFOSEL_CURPIPE_SHIFT,
171 USB_DnFIFOSEL_CURPIPE) == pipe)
173 RZA_IO_RegWrite_16(&USB201.D1FIFOSEL,
175 USB_DnFIFOSEL_CURPIPE_SHIFT,
176 USB_DnFIFOSEL_CURPIPE);
179 /* PIPE Configuration */
180 USB201.PIPESEL = pipe;
181 USB201.PIPECFG = tbl[index + 1];
182 USB201.PIPEBUF = tbl[index + 2];
183 USB201.PIPEMAXP = tbl[index + 3];
184 USB201.PIPEPERI = tbl[index + 4];
186 g_usb1_function_pipecfg[pipe] = tbl[index + 1];
187 g_usb1_function_pipebuf[pipe] = tbl[index + 2];
188 g_usb1_function_pipemaxp[pipe] = tbl[index + 3];
189 g_usb1_function_pipeperi[pipe] = tbl[index + 4];
192 usb1_function_set_sqclr(pipe);
193 usb1_function_aclrm(pipe);
196 g_usb1_function_pipe_status[pipe] = DEVDRV_USBF_PIPE_IDLE;
197 g_usb1_function_PipeDataSize[pipe] = 0;
203 /*******************************************************************************
204 * Function Name: usb1_function_EpToPipe
205 * Description : Returns the pipe which end point specified by the argument is
207 * Arguments : uint16_t ep ; Direction + Endpoint Number
208 * Return Value : USB_FUNCTION_EP_ERROR : Error
209 * : Others : Pipe Number
210 *******************************************************************************/
211 uint16_t usb1_function_EpToPipe (uint16_t ep)
215 for (pipe = 1; pipe <= USB_FUNCTION_MAX_PIPE_NO; pipe++)
217 if ((g_usb1_function_PipeTbl[pipe] & 0x00ff) == ep)
223 return USB_FUNCTION_EP_ERROR;
226 /*******************************************************************************
227 * Function Name: usb1_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
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 *******************************************************************************/
237 void usb1_function_InitEPTable (uint16_t Con_Num, uint16_t Int_Num, uint16_t Alt_Num)
240 uint16_t point_interface;
241 uint16_t point_endpoint;
247 ptr = (uint8_t *)g_usb1_function_ConfigurationPtr[Con_Num - 1];
248 point_interface = *ptr;
249 length = (uint16_t)((uint16_t)*(ptr + 3) << 8 | (uint16_t)*(ptr + 2));
255 for (; point_interface < length;)
257 switch (*(ptr + 1)) /* Descriptor Type ? */
259 case USB_FUNCTION_DT_INTERFACE: /* Interface */
260 if ((*(ptr + 2) == Int_Num) && (*(ptr + 3) == Alt_Num))
262 numbers = *(ptr + 4);
268 point_interface += *ptr;
272 case USB_FUNCTION_DT_ENDPOINT: /* Endpoint */
273 if (point_endpoint < numbers)
275 endpoint = (uint16_t)(*(ptr + 2) & 0x0f);
276 g_usb1_function_EPTableIndex[endpoint] = (uint16_t)(start + point_endpoint);
279 point_interface += *ptr;
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;
295 /*******************************************************************************
296 * Function Name: usb1_function_GetConfigNum
297 * Description : Returns the number of configuration referring to the number of
298 * : configuration described in the device descriptor.
300 * Return Value : Number of possible configurations (bNumConfigurations).
301 *******************************************************************************/
303 uint16_t usb1_function_GetConfigNum (void)
305 return (uint16_t)g_usb1_function_DeviceDescriptor[17];
309 /*******************************************************************************
310 * Function Name: usb1_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 *******************************************************************************/
317 uint16_t usb1_function_GetInterfaceNum (uint16_t num)
319 return (uint16_t)(*(g_usb1_function_ConfigurationPtr[num - 1] + 4));
323 /*******************************************************************************
324 * Function Name: usb1_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 *******************************************************************************/
332 uint16_t usb1_function_GetAltNum (uint16_t Con_Num, uint16_t Int_Num)
336 uint16_t alt_num = 0;
339 ptr = (uint8_t *)(g_usb1_function_ConfigurationPtr[Con_Num - 1]);
341 ptr += ptr[0]; /* InterfaceDescriptor[0] */
342 length = (uint16_t)(*(g_usb1_function_ConfigurationPtr[Con_Num - 1] + 2));
343 length |= (uint16_t)((uint16_t)(*(g_usb1_function_ConfigurationPtr[Con_Num - 1] + 3)) << 8);
345 for (; point < length;) /* Search Descriptor Table size */
347 switch (ptr[1]) /* Descriptor Type ? */
349 case USB_FUNCTION_DT_INTERFACE: /* Interface */
350 if (Int_Num == ptr[2])
352 alt_num = (uint16_t)ptr[3]; /* Alternate Number count */
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 */
372 /*******************************************************************************
373 * Function Name: usb1_function_CheckRemoteWakeup
374 * Description : Returns the result of the remote wake up function is supported
375 * : or not referring to the configuration descriptor.
377 * Return Value : DEVDRV_USBF_ON : Support Remote Wakeup
378 * : DEVDRV_USBF_OFF : not Support Remote Wakeup
379 *******************************************************************************/
381 uint16_t usb1_function_CheckRemoteWakeup (void)
385 if (g_usb1_function_ConfigNum == 0)
387 return DEVDRV_USBF_OFF;
390 atr = *(g_usb1_function_ConfigurationPtr[g_usb1_function_ConfigNum - 1] + 7);
392 if (atr & USB_FUNCTION_CF_RWUP)
394 return DEVDRV_USBF_ON;
397 return DEVDRV_USBF_OFF;
401 /*******************************************************************************
402 * Function Name: usb1_function_clear_alt
403 * Description : Initializes the Alternate setting area.
405 * Return Value : none
406 *******************************************************************************/
408 void usb1_function_clear_alt (void)
412 for (i = 0; i < USB_FUNCTION_ALT_NO; ++i)
414 g_usb1_function_Alternate[i] = 0; /* Alternate */
419 /*******************************************************************************
420 * Function Name: usb1_function_clear_pipe_tbl
421 * Description : Initializes pipe definition table.
423 * Return Value : none
424 *******************************************************************************/
425 void usb1_function_clear_pipe_tbl (void)
429 for (pipe = 0; pipe < (USB_FUNCTION_MAX_PIPE_NO + 1); ++pipe)
431 g_usb1_function_PipeTbl[pipe] = 0;
435 /*******************************************************************************
436 * Function Name: usb1_function_clear_ep_table_index
437 * Description : Initializes the end point table index.
439 * Return Value : none
440 *******************************************************************************/
442 void usb1_function_clear_ep_table_index (void)
446 for (ep = 0; ep <= USB_FUNCTION_MAX_EP_NO; ++ep)
448 g_usb1_function_EPTableIndex[ep] = USB_FUNCTION_EP_ERROR;