]> git.donarmstrong.com Git - qmk_firmware.git/blob - tool/mbed/mbed-sdk/libraries/USBDevice/USBDevice/TARGET_RENESAS/TARGET_RZ_A1H/usb0/src/common/usb0_function_dataio.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 / common / usb0_function_dataio.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_dataio.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
57
58 /*******************************************************************************
59 Exported global variables and functions (to be accessed by other files)
60 *******************************************************************************/
61
62
63 /*******************************************************************************
64 Private global variables and functions
65 *******************************************************************************/
66 static uint16_t g_usb0_function_mbw[(USB_FUNCTION_MAX_PIPE_NO + 1)];
67
68 static void     usb0_function_start_receive_trns_c(uint16_t pipe, uint32_t size, uint8_t *data);
69 static void     usb0_function_start_receive_trns_d0(uint16_t pipe, uint32_t size, uint8_t *data);
70 static void     usb0_function_start_receive_trns_d1(uint16_t pipe, uint32_t size, uint8_t *data);
71 static void     usb0_function_start_receive_dma_d0(uint16_t pipe, uint32_t size, uint8_t *data);
72 static void     usb0_function_start_receive_dma_d1(uint16_t pipe, uint32_t size, uint8_t *data);
73 static uint16_t usb0_function_read_dma_d0(uint16_t pipe);
74 static uint16_t usb0_function_read_dma_d1(uint16_t pipe);
75 static uint16_t usb0_function_write_dma_d0(uint16_t pipe);
76 static uint16_t usb0_function_write_dma_d1(uint16_t pipe);
77
78 static void     usb0_function_read_c_fifo(uint16_t pipe, uint16_t count);
79 static void     usb0_function_write_c_fifo(uint16_t Pipe, uint16_t count);
80 static void     usb0_function_read_d0_fifo(uint16_t pipe, uint16_t count);
81 static void     usb0_function_write_d0_fifo(uint16_t pipe, uint16_t count);
82 static void     usb0_function_read_d1_fifo(uint16_t pipe, uint16_t count);
83 static void     usb0_function_write_d1_fifo(uint16_t pipe, uint16_t count);
84
85 static void     usb0_function_clear_transaction_counter(uint16_t pipe);
86 static void     usb0_function_set_transaction_counter(uint16_t pipe, uint32_t count);
87
88 static uint32_t usb0_function_com_get_dmasize(uint32_t trncount, uint32_t dtptr);
89
90 static uint16_t usb0_function_set_dfacc_d0(uint16_t mbw, uint32_t count);
91 static uint16_t usb0_function_set_dfacc_d1(uint16_t mbw, uint32_t count);
92
93
94 /*******************************************************************************
95 * Function Name: usb0_function_start_send_transfer
96 * Description  : Starts the USB data communication using pipe specified by the argument.
97 * Arguments    : uint16_t  pipe    ; Pipe Number
98 *              : uint32_t size     ; Data Size
99 *              : uint8_t  *data    ; Data Address
100 * Return Value : DEVDRV_USBF_WRITEEND           ; Write end
101 *              : DEVDRV_USBF_WRITESHRT          ; short data
102 *              : DEVDRV_USBF_WRITING            ; Continue of data write
103 *              : DEVDRV_USBF_WRITEDMA           ; Write DMA
104 *              : DEVDRV_USBF_FIFOERROR          ; FIFO status
105 *******************************************************************************/
106 uint16_t usb0_function_start_send_transfer (uint16_t pipe, uint32_t size, uint8_t * data)
107 {
108     uint16_t status;
109     uint16_t usefifo;
110     uint16_t mbw;
111
112     g_usb0_function_data_count[pipe]   = size;
113     g_usb0_function_data_pointer[pipe] = (uint8_t *)data;
114     g_usb0_function_pipe_status[pipe]  = DEVDRV_USBF_PIPE_WAIT;
115
116     usb0_function_clear_bemp_sts(pipe);
117     usb0_function_clear_brdy_sts(pipe);
118     usb0_function_clear_nrdy_sts(pipe);
119
120     mbw = usb0_function_get_mbw(size, (uint32_t)data);
121
122     usefifo = (uint16_t)(g_usb0_function_PipeTbl[pipe] & USB_FUNCTION_FIFO_USE);
123
124     switch (usefifo)
125     {
126         case USB_FUNCTION_D0FIFO_USE:
127         case USB_FUNCTION_D0FIFO_DMA:
128             usefifo = USB_FUNCTION_D0USE;
129         break;
130
131         case USB_FUNCTION_D1FIFO_USE:
132         case USB_FUNCTION_D1FIFO_DMA:
133             usefifo = USB_FUNCTION_D1USE;
134         break;
135
136         default:
137             usefifo = USB_FUNCTION_CUSE;
138         break;
139     };
140
141     usb0_function_set_curpipe(USB_FUNCTION_PIPE0, usefifo, DEVDRV_USBF_NO, mbw);
142
143     usb0_function_clear_transaction_counter(pipe);
144
145     usb0_function_aclrm(pipe);
146
147     status = usb0_function_write_buffer(pipe);
148
149     if (status != DEVDRV_USBF_FIFOERROR)
150     {
151         usb0_function_set_pid_buf(pipe);
152     }
153
154     return status;
155 }
156
157 /*******************************************************************************
158 * Function Name: usb0_function_write_buffer
159 * Description  : Writes data in the buffer allocated in the pipe specified by
160 *              : the argument. The FIFO for using is set in the pipe definition table.
161 * Arguments    : uint16_t pipe      ; Pipe Number
162 * Return Value : DEVDRV_USBF_WRITEEND           ; Write end
163 *              : DEVDRV_USBF_WRITESHRT          ; short data
164 *              : DEVDRV_USBF_WRITING            ; Continue of data write
165 *              : DEVDRV_USBF_WRITEDMA           ; Write DMA
166 *              : DEVDRV_USBF_FIFOERROR          ; FIFO status
167 *******************************************************************************/
168 uint16_t usb0_function_write_buffer (uint16_t pipe)
169 {
170     uint16_t status;
171     uint16_t usefifo;
172
173     g_usb0_function_PipeIgnore[pipe] = 0;
174     usefifo = (uint16_t)(g_usb0_function_PipeTbl[pipe] & USB_FUNCTION_FIFO_USE);
175
176     switch (usefifo)
177     {
178         case USB_FUNCTION_D0FIFO_USE:
179             status = usb0_function_write_buffer_d0(pipe);
180         break;
181
182         case USB_FUNCTION_D1FIFO_USE:
183             status = usb0_function_write_buffer_d1(pipe);
184         break;
185
186         case USB_FUNCTION_D0FIFO_DMA:
187             status = usb0_function_write_dma_d0(pipe);
188         break;
189
190         case USB_FUNCTION_D1FIFO_DMA:
191             status = usb0_function_write_dma_d1(pipe);
192         break;
193
194         default:
195             status = usb0_function_write_buffer_c(pipe);
196         break;
197     };
198
199     switch (status)
200     {
201         case DEVDRV_USBF_WRITING:                       /* Continue of data write */
202             usb0_function_enable_nrdy_int(pipe);        /* Error (NORES or STALL) */
203             usb0_function_enable_brdy_int(pipe);        /* Enable Ready Interrupt */
204         break;
205
206         case DEVDRV_USBF_WRITEEND:                      /* End of data write */
207         case DEVDRV_USBF_WRITESHRT:                     /* End of data write */
208             usb0_function_disable_brdy_int(pipe);       /* Disable Ready Interrupt */
209             usb0_function_clear_nrdy_sts(pipe);
210             usb0_function_enable_nrdy_int(pipe);        /* Error (NORES or STALL) */
211             /* for last transfer */
212             usb0_function_enable_bemp_int(pipe);        /* Enable Empty Interrupt */
213         break;
214
215         case DEVDRV_USBF_WRITEDMA:                      /* DMA write */
216             usb0_function_clear_nrdy_sts(pipe);
217             usb0_function_enable_nrdy_int(pipe);        /* Error (NORES or STALL) */
218         break;
219
220         case DEVDRV_USBF_FIFOERROR:                     /* FIFO access status */
221         default:
222             usb0_function_disable_brdy_int(pipe);       /* Disable Ready Interrupt */
223             usb0_function_disable_bemp_int(pipe);       /* Disable Empty Interrupt */
224             g_usb0_function_pipe_status[pipe] = DEVDRV_USBF_FIFOERROR;
225         break;
226     }
227
228     return status;                      /* End or Err or Continue */
229 }
230
231 /*******************************************************************************
232 * Function Name: usb0_function_write_buffer_c
233 * Description  : Writes data in the buffer allocated in the pipe specified in
234 *              : the argument. Writes data by CPU transfer using CFIFO.
235 * Arguments    : uint16_t pipe      ; Pipe Number
236 * Return Value : DEVDRV_USBF_WRITEEND           ; Write end
237 *              : DEVDRV_USBF_WRITESHRT          ; short data
238 *              : DEVDRV_USBF_WRITING            ; Continue of data write
239 *              : DEVDRV_USBF_WRITEDMA           ; Write DMA
240 *              : DEVDRV_USBF_FIFOERROR          ; FIFO status
241 *******************************************************************************/
242 uint16_t usb0_function_write_buffer_c (uint16_t pipe)
243 {
244     uint32_t count;
245     uint16_t size;
246     uint16_t buffer;
247     uint16_t mxps;
248     uint16_t status;
249     uint16_t mbw;
250
251     if (g_usb0_function_CtrZeroLengthFlag == 1)
252     {
253         g_usb0_function_CtrZeroLengthFlag = 0;                  /* Zero Length Packet Flag CLR */
254         return DEVDRV_USBF_WRITEEND;
255     }
256
257     mbw = usb0_function_get_mbw(g_usb0_function_data_count[pipe], (uint32_t)g_usb0_function_data_pointer[pipe]);
258     if (pipe == USB_FUNCTION_PIPE0)
259     {
260         buffer = usb0_function_change_fifo_port(pipe, USB_FUNCTION_CUSE, USB_FUNCTION_CFIFO_WRITE, mbw);
261     }
262     else
263     {
264         buffer = usb0_function_change_fifo_port(pipe, USB_FUNCTION_CUSE, DEVDRV_USBF_NO, mbw);
265     }
266
267     if (buffer == DEVDRV_USBF_FIFOERROR)                        /* FIFO access status */
268     {
269         return DEVDRV_USBF_FIFOERROR;
270     }
271
272     size = usb0_function_get_buf_size(pipe);                    /* Data buffer size */
273     mxps = usb0_function_get_mxps(pipe);                        /* Max Packet Size */
274
275     if (g_usb0_function_data_count[pipe] <= (uint32_t)size)
276     {
277         status = DEVDRV_USBF_WRITEEND;                          /* write continues */
278         count  = g_usb0_function_data_count[pipe];
279
280         if (count == 0)
281         {
282             status = DEVDRV_USBF_WRITESHRT;                     /* Null Packet is end of write */
283         }
284
285         if ((count % mxps) != 0)
286         {
287             status = DEVDRV_USBF_WRITESHRT;                     /* Short Packet is end of write */
288         }
289     }
290     else
291     {
292         status = DEVDRV_USBF_WRITING;                           /* write continues */
293         count  = (uint32_t)size;
294     }
295
296     usb0_function_write_c_fifo(pipe, (uint16_t)count);
297
298     if (g_usb0_function_data_count[pipe] < (uint32_t)size)
299     {
300         g_usb0_function_data_count[pipe] = 0;
301
302         if (RZA_IO_RegRead_16(&USB200.CFIFOCTR, USB_CFIFOCTR_BVAL_SHIFT, USB_CFIFOCTR_BVAL) == 0)
303         {
304             USB200.CFIFOCTR = USB_FUNCTION_BITBVAL;             /* Short Packet */
305             g_usb0_function_CtrZeroLengthFlag = 1;              /* Zero Length Packet Flag */
306         }
307     }
308     else
309     {
310         g_usb0_function_data_count[pipe] -= count;
311     }
312
313     return status;                                              /* End or Err or Continue */
314 }
315
316 /*******************************************************************************
317 * Function Name: usb0_function_write_buffer_d0
318 * Description  : Writes data in the buffer allocated in the pipe specified in the argument.
319 *              : Writes data by CPU transfer using D0FIFO.
320 * Arguments    : uint16_t pipe      ; Pipe Number
321 * Return Value : DEVDRV_USBF_WRITEEND           ; Write end
322 *              : DEVDRV_USBF_WRITESHRT          ; short data
323 *              : DEVDRV_USBF_WRITING            ; Continue of data write
324 *              : DEVDRV_USBF_WRITEDMA           ; Write DMA
325 *              : DEVDRV_USBF_FIFOERROR          ; FIFO status
326 *******************************************************************************/
327 uint16_t usb0_function_write_buffer_d0 (uint16_t pipe)
328 {
329     uint32_t count;
330     uint16_t size;
331     uint16_t buffer;
332     uint16_t mxps;
333     uint16_t status;
334     uint16_t mbw;
335
336     mbw    = usb0_function_get_mbw(g_usb0_function_data_count[pipe], (uint32_t)g_usb0_function_data_pointer[pipe]);
337     buffer = usb0_function_change_fifo_port(pipe, USB_FUNCTION_D0USE, DEVDRV_USBF_NO, mbw);
338     if (buffer == DEVDRV_USBF_FIFOERROR)                        /* FIFO access status */
339     {
340         return DEVDRV_USBF_FIFOERROR;
341     }
342
343     size = usb0_function_get_buf_size(pipe);                    /* Data buffer size */
344     mxps = usb0_function_get_mxps(pipe);                        /* Max Packet Size */
345
346     if (g_usb0_function_data_count[pipe] <= (uint32_t)size)
347     {
348         status = DEVDRV_USBF_WRITEEND;                          /* write continues */
349         count  = g_usb0_function_data_count[pipe];
350
351         if (count == 0)
352         {
353             status = DEVDRV_USBF_WRITESHRT;                     /* Null Packet is end of write */
354         }
355
356         if ((count % mxps) != 0)
357         {
358             status = DEVDRV_USBF_WRITESHRT;                     /* Short Packet is end of write */
359         }
360     }
361     else
362     {
363         status = DEVDRV_USBF_WRITING;                           /* write continues */
364         count  = (uint32_t)size;
365     }
366
367     usb0_function_write_d0_fifo(pipe, (uint16_t)count);
368
369     if (g_usb0_function_data_count[pipe] < (uint32_t)size)
370     {
371         g_usb0_function_data_count[pipe] = 0;
372         if (RZA_IO_RegRead_16(&USB200.D0FIFOCTR, USB_DnFIFOCTR_BVAL_SHIFT, USB_DnFIFOCTR_BVAL) == 0)
373         {
374             USB200.D0FIFOCTR = USB_FUNCTION_BITBVAL;            /* Short Packet */
375         }
376     }
377     else
378     {
379         g_usb0_function_data_count[pipe] -= count;
380     }
381
382     return status;                                              /* End or Err or Continue */
383 }
384
385 /*******************************************************************************
386 * Function Name: usb0_function_write_buffer_d1
387 * Description  : Writes data in the buffer allocated in the pipe specified in the argument.
388 *              : Writes data by CPU transfer using D1FIFO.
389 * Arguments    : uint16_t pipe      ; Pipe Number
390 * Return Value : DEVDRV_USBF_WRITEEND           ; Write end
391 *              : DEVDRV_USBF_WRITESHRT          ; short data
392 *              : DEVDRV_USBF_WRITING            ; Continue of data write
393 *              : DEVDRV_USBF_WRITEDMA           ; Write DMA
394 *              : DEVDRV_USBF_FIFOERROR          ; FIFO status
395 *******************************************************************************/
396 uint16_t usb0_function_write_buffer_d1 (uint16_t pipe)
397 {
398     uint32_t count;
399     uint16_t size;
400     uint16_t buffer;
401     uint16_t mxps;
402     uint16_t status;
403     uint16_t mbw;
404
405     mbw = usb0_function_get_mbw(g_usb0_function_data_count[pipe], (uint32_t)g_usb0_function_data_pointer[pipe]);
406     buffer = usb0_function_change_fifo_port(pipe, USB_FUNCTION_D1USE, DEVDRV_USBF_NO, mbw);
407
408     if (buffer == DEVDRV_USBF_FIFOERROR)                        /* FIFO access status */
409     {
410         return DEVDRV_USBF_FIFOERROR;
411     }
412
413     size = usb0_function_get_buf_size(pipe);                    /* Data buffer size */
414     mxps = usb0_function_get_mxps(pipe);                        /* Max Packet Size */
415
416     if (g_usb0_function_data_count[pipe] <= (uint32_t)size)
417     {
418         status = DEVDRV_USBF_WRITEEND;                          /* write continues */
419         count  = g_usb0_function_data_count[pipe];
420
421         if (count == 0)
422         {
423             status = DEVDRV_USBF_WRITESHRT;                     /* Null Packet is end of write */
424         }
425
426         if ((count % mxps) != 0)
427         {
428             status = DEVDRV_USBF_WRITESHRT;                     /* Short Packet is end of write */
429         }
430     }
431     else
432     {
433         status = DEVDRV_USBF_WRITING;                           /* write continues */
434         count  = (uint32_t)size;
435     }
436
437     usb0_function_write_d1_fifo(pipe, (uint16_t)count);
438
439     if (g_usb0_function_data_count[pipe] < (uint32_t)size)
440     {
441         g_usb0_function_data_count[pipe] = 0;
442
443         if (RZA_IO_RegRead_16(&USB200.D1FIFOCTR, USB_DnFIFOCTR_BVAL_SHIFT, USB_DnFIFOCTR_BVAL) == 0)
444         {
445             USB200.D1FIFOCTR = USB_FUNCTION_BITBVAL;            /* Short Packet */
446         }
447     }
448     else
449     {
450         g_usb0_function_data_count[pipe] -= count;
451     }
452
453     return status;                                              /* End or Err or Continue */
454 }
455
456 /*******************************************************************************
457 * Function Name: usb0_function_write_dma_d0
458 * Description  : Writes data in the buffer allocated in the pipe specified in the argument.
459 *              : Writes data by DMA transfer using D0FIFO.
460 *              : The DMA-ch for using is specified by Userdef_USB_usb0_function_start_dma().
461 * Arguments    : uint16_t pipe     ; Pipe Number
462 * Return Value : DEVDRV_USBF_WRITEEND           : Write end
463 *              : DEVDRV_USBF_WRITESHRT          : short data
464 *              : DEVDRV_USBF_WRITING            : Continue of data write
465 *              : DEVDRV_USBF_WRITEDMA           : Write DMA
466 *              : DEVDRV_USBF_FIFOERROR          : FIFO status
467 *******************************************************************************/
468 static uint16_t usb0_function_write_dma_d0 (uint16_t pipe)
469 {
470     uint32_t count;
471     uint16_t size;
472     uint16_t buffer;
473     uint16_t status;
474     uint16_t mbw;
475     uint16_t dfacc = 0;
476
477     mbw = usb0_function_get_mbw(g_usb0_function_data_count[pipe], (uint32_t)g_usb0_function_data_pointer[pipe]);
478     buffer = usb0_function_change_fifo_port(pipe, USB_FUNCTION_D0DMA, DEVDRV_USBF_NO, mbw);
479
480     if (buffer == DEVDRV_USBF_FIFOERROR)                            /* FIFO access status */
481     {
482         return DEVDRV_USBF_FIFOERROR;
483     }
484
485     size  = usb0_function_get_buf_size(pipe);                       /* Data buffer size */
486     count = g_usb0_function_data_count[pipe];
487
488     if (count != 0)
489     {
490         g_usb0_function_DmaPipe[USB_FUNCTION_D0FIFO] = pipe;
491
492         if ((count % size) != 0)
493         {
494             g_usb0_function_DmaBval[USB_FUNCTION_D0FIFO] = 1;
495         }
496         else
497         {
498             g_usb0_function_DmaBval[USB_FUNCTION_D0FIFO] = 0;
499         }
500
501         dfacc = usb0_function_set_dfacc_d0(mbw, count);
502
503         if (mbw == USB_FUNCTION_BITMBW_32)
504         {
505             g_usb0_function_DmaInfo[USB_FUNCTION_D0FIFO].size = 2;  /* 32bit transfer */
506         }
507         else if (mbw == USB_FUNCTION_BITMBW_16)
508         {
509             g_usb0_function_DmaInfo[USB_FUNCTION_D0FIFO].size = 1;  /* 16bit transfer */
510         }
511         else
512         {
513             g_usb0_function_DmaInfo[USB_FUNCTION_D0FIFO].size = 0;  /* 8bit transfer */
514         }
515
516         g_usb0_function_DmaInfo[USB_FUNCTION_D0FIFO].fifo   = USB_FUNCTION_D0FIFO_DMA;
517         g_usb0_function_DmaInfo[USB_FUNCTION_D0FIFO].dir    = USB_FUNCTION_BUF2FIFO;
518         g_usb0_function_DmaInfo[USB_FUNCTION_D0FIFO].buffer = (uint32_t)g_usb0_function_data_pointer[pipe];
519         g_usb0_function_DmaInfo[USB_FUNCTION_D0FIFO].bytes  = count;
520
521         Userdef_USB_usb0_function_start_dma(&g_usb0_function_DmaInfo[USB_FUNCTION_D0FIFO], dfacc);
522
523         usb0_function_set_curpipe2(pipe, USB_FUNCTION_D0DMA, DEVDRV_USBF_NO, mbw, dfacc);
524
525         RZA_IO_RegWrite_16(&USB200.D0FIFOSEL, 1, USB_DnFIFOSEL_DREQE_SHIFT, USB_DnFIFOSEL_DREQE);
526
527         g_usb0_function_data_count[pipe]    = 0;
528         g_usb0_function_data_pointer[pipe] += count;
529         status = DEVDRV_USBF_WRITEDMA;                              /* DMA write  */
530     }
531     else
532     {
533         if (RZA_IO_RegRead_16(&USB200.D0FIFOCTR, USB_DnFIFOCTR_BVAL_SHIFT, USB_DnFIFOCTR_BVAL) == 0)
534         {
535             RZA_IO_RegWrite_16(&USB200.D0FIFOCTR, 1, USB_DnFIFOCTR_BVAL_SHIFT, USB_DnFIFOCTR_BVAL); /* Short Packet */
536         }
537         status = DEVDRV_USBF_WRITESHRT;                             /* Short Packet is end of write */
538     }
539
540     return status;                                                  /* End or Err or Continue */
541 }
542
543 /*******************************************************************************
544 * Function Name: usb0_function_write_dma_d1
545 * Description  : Writes data in the buffer allocated in the pipe specified in the argument.
546 *              : Writes data by DMA transfer using D1FIFO.
547 *              : The DMA-ch for using is specified by Userdef_USB_usb0_function_start_dma().
548 * Arguments    : uint16_t pipe      ; Pipe Number
549 * Return Value : DEVDRV_USBF_WRITEEND           : Write end
550 *              : DEVDRV_USBF_WRITESHRT          : short data
551 *              : DEVDRV_USBF_WRITING            : Continue of data write
552 *              : DEVDRV_USBF_WRITEDMA           : Write DMA
553 *              : DEVDRV_USBF_FIFOERROR          : FIFO status
554 *******************************************************************************/
555 static uint16_t usb0_function_write_dma_d1 (uint16_t pipe)
556 {
557     uint32_t count;
558     uint16_t size;
559     uint16_t buffer;
560     uint16_t status;
561     uint16_t mbw;
562     uint16_t dfacc=0;
563
564     mbw = usb0_function_get_mbw(g_usb0_function_data_count[pipe], (uint32_t)g_usb0_function_data_pointer[pipe]);
565     buffer = usb0_function_change_fifo_port(pipe, USB_FUNCTION_D1DMA, DEVDRV_USBF_NO, mbw);
566
567     if (buffer == DEVDRV_USBF_FIFOERROR)                            /* FIFO access status */
568     {
569         return DEVDRV_USBF_FIFOERROR;
570     }
571
572     size  = usb0_function_get_buf_size(pipe);                       /* Data buffer size */
573     count = g_usb0_function_data_count[pipe];
574
575     if (count != 0)
576     {
577         g_usb0_function_DmaPipe[USB_FUNCTION_D1FIFO] = pipe;
578         if ((count % size) != 0)
579         {
580             g_usb0_function_DmaBval[USB_FUNCTION_D1FIFO] = 1;
581         }
582         else
583         {
584             g_usb0_function_DmaBval[USB_FUNCTION_D1FIFO] = 0;
585         }
586
587         dfacc = usb0_function_set_dfacc_d1(mbw, count);
588
589         if (mbw == USB_FUNCTION_BITMBW_32)
590         {
591             g_usb0_function_DmaInfo[USB_FUNCTION_D1FIFO].size = 2;  /* 32bit transfer */
592         }
593         else if (mbw == USB_FUNCTION_BITMBW_16)
594         {
595             g_usb0_function_DmaInfo[USB_FUNCTION_D1FIFO].size = 1;  /* 16bit transfer */
596         }
597         else
598         {
599             g_usb0_function_DmaInfo[USB_FUNCTION_D1FIFO].size = 0;  /* 8bit transfer */
600         }
601
602         g_usb0_function_DmaInfo[USB_FUNCTION_D1FIFO].fifo   = USB_FUNCTION_D1FIFO_DMA;
603         g_usb0_function_DmaInfo[USB_FUNCTION_D1FIFO].dir    = USB_FUNCTION_BUF2FIFO;
604         g_usb0_function_DmaInfo[USB_FUNCTION_D1FIFO].buffer = (uint32_t)g_usb0_function_data_pointer[pipe];
605         g_usb0_function_DmaInfo[USB_FUNCTION_D1FIFO].bytes  = count;
606
607         Userdef_USB_usb0_function_start_dma(&g_usb0_function_DmaInfo[USB_FUNCTION_D1FIFO], dfacc);
608
609         usb0_function_set_curpipe2(pipe, USB_FUNCTION_D1DMA, DEVDRV_USBF_NO, mbw, dfacc);
610
611         RZA_IO_RegWrite_16(&USB200.D1FIFOSEL, 1, USB_DnFIFOSEL_DREQE_SHIFT, USB_DnFIFOSEL_DREQE);
612
613         g_usb0_function_data_count[pipe]    = 0;
614         g_usb0_function_data_pointer[pipe] += count;
615
616         status = DEVDRV_USBF_WRITEDMA;                             /* DMA write  */
617     }
618     else
619     {
620         if (RZA_IO_RegRead_16(&USB200.D1FIFOCTR, USB_DnFIFOCTR_BVAL_SHIFT, USB_DnFIFOCTR_BVAL) == 0)
621         {
622             RZA_IO_RegWrite_16(&USB200.D1FIFOCTR, 1, USB_DnFIFOCTR_BVAL_SHIFT, USB_DnFIFOCTR_BVAL); /* Short Packet */
623         }
624         status = DEVDRV_USBF_WRITESHRT;                             /* Short Packet is end of write */
625     }
626
627     return status;                                                  /* End or Err or Continue */
628 }
629
630 /*******************************************************************************
631 * Function Name: usb0_function_start_receive_transfer
632 * Description  : Starts USB data reception using the pipe specified in the argument.
633 *              : The FIFO for using is set in the pipe definition table.
634 * Arguments    : uint16_t pipe      ; Pipe Number
635 *              : uint32_t size      ; Data Size
636 *              : uint8_t *data      ; Data Address
637 * Return Value : none
638 *******************************************************************************/
639 void usb0_function_start_receive_transfer (uint16_t pipe, uint32_t size, uint8_t * data)
640 {
641     uint16_t usefifo;
642
643     usb0_function_clear_bemp_sts(pipe);
644     usb0_function_clear_brdy_sts(pipe);
645     usb0_function_clear_nrdy_sts(pipe);
646
647     usefifo = (uint16_t)(g_usb0_function_PipeTbl[pipe] & USB_FUNCTION_FIFO_USE);
648
649     switch (usefifo)
650     {
651         case USB_FUNCTION_D0FIFO_USE:
652             usb0_function_start_receive_trns_d0(pipe, size, data);
653         break;
654
655         case USB_FUNCTION_D1FIFO_USE:
656             usb0_function_start_receive_trns_d1(pipe, size, data);
657         break;
658
659         case USB_FUNCTION_D0FIFO_DMA:
660             usb0_function_start_receive_dma_d0(pipe, size, data);
661         break;
662
663         case USB_FUNCTION_D1FIFO_DMA:
664             usb0_function_start_receive_dma_d1(pipe, size, data);
665         break;
666
667         default:
668             usb0_function_start_receive_trns_c(pipe, size, data);
669         break;
670     }
671 }
672
673 /*******************************************************************************
674 * Function Name: usb0_function_start_receive_trns_c
675 * Description  : Reads data from the buffer allocated in the pipe specified in the argument.
676 *              : Reads data by CPU transfer using CFIFO.
677 *              : When storing data in the buffer allocated in the pipe specified in the
678 *              : argument, BRDY interrupt is generated to read data
679 *              : in the interrupt.
680 * Arguments    : uint16_t pipe      ; Pipe Number
681 *              : uint32_t size      ; Data Size
682 *              : uint8_t *data      ; Data Address
683 * Return Value : none
684 *******************************************************************************/
685 static void usb0_function_start_receive_trns_c (uint16_t pipe, uint32_t size, uint8_t * data)
686 {
687     uint16_t mbw;
688
689     usb0_function_set_pid_nak(pipe);
690     g_usb0_function_data_count[pipe]   = size;
691     g_usb0_function_data_pointer[pipe] = (uint8_t *)data;
692     g_usb0_function_PipeIgnore[pipe]   = 0;
693
694     g_usb0_function_PipeDataSize[pipe] = size;
695     g_usb0_function_pipe_status[pipe]  = DEVDRV_USBF_PIPE_WAIT;
696
697     mbw = usb0_function_get_mbw(size, (uint32_t)data);
698     usb0_function_set_curpipe(USB_FUNCTION_PIPE0, USB_FUNCTION_CUSE, USB_FUNCTION_CFIFO_READ, mbw);
699     USB200.CFIFOCTR = USB_FUNCTION_BITBCLR;
700
701     usb0_function_set_transaction_counter(pipe, size);
702
703     usb0_function_aclrm(pipe);
704
705     usb0_function_enable_nrdy_int(pipe);
706     usb0_function_enable_brdy_int(pipe);
707
708     usb0_function_set_pid_buf(pipe);
709 }
710
711 /*******************************************************************************
712 * Function Name: usb0_function_start_receive_trns_d0
713 * Description  : Reads data from the buffer allocated in the pipe specified in the argument.
714 *              : Reads data by CPU transfer using D0FIFO.
715 *              : This function does not read data from the buffer.
716 *              : When storing data in the buffer allocated in the pipe specified
717 *              : in the argument, BRDY interrupt is generated to read data in the
718 *              : interrupt.
719 * Arguments    : uint16_t pipe      ; Pipe Number
720 *              : uint32_t size      ; Data Size
721 *              : uint8_t *data      ; Data Address
722 * Return Value : none
723 *******************************************************************************/
724 static void usb0_function_start_receive_trns_d0 (uint16_t pipe, uint32_t size, uint8_t * data)
725 {
726     uint16_t mbw;
727
728     usb0_function_set_pid_nak(pipe);
729     g_usb0_function_data_count[pipe]   = size;
730     g_usb0_function_data_pointer[pipe] = (uint8_t *)data;
731     g_usb0_function_PipeIgnore[pipe]   = 0;
732
733     g_usb0_function_PipeDataSize[pipe] = size;
734     g_usb0_function_pipe_status[pipe]  = DEVDRV_USBF_PIPE_WAIT;
735
736     mbw = usb0_function_get_mbw(size, (uint32_t)data);
737     usb0_function_set_curpipe(USB_FUNCTION_PIPE0, USB_FUNCTION_D0USE, DEVDRV_USBF_NO, mbw);
738
739     usb0_function_set_transaction_counter(pipe, size);
740
741     usb0_function_aclrm(pipe);
742
743     usb0_function_enable_nrdy_int(pipe);
744     usb0_function_enable_brdy_int(pipe);
745
746     usb0_function_set_pid_buf(pipe);
747 }
748
749 /*******************************************************************************
750 * Function Name: usb0_function_start_receive_trns_d1
751 * Description  : Reads data from the buffer allocated in the pipe specified in the argument.
752 *              : Reads data by CPU transfer using D1FIFO.
753 *              : This function does not read data from the buffer.
754 *              : When storing data in the buffer allocated in the pipe specified
755 *              : in the argument, BRDY interrupt is generated to read data.
756 * Arguments    : uint16_t pipe      ; Pipe Number
757 *              : uint32_t size      ; Data Size
758 *              : uint8_t *data      ; Data Address
759 * Return Value : none
760 *******************************************************************************/
761 static void usb0_function_start_receive_trns_d1 (uint16_t pipe, uint32_t size, uint8_t * data)
762 {
763     uint16_t mbw;
764
765     usb0_function_set_pid_nak(pipe);
766     g_usb0_function_data_count[pipe]   = size;
767     g_usb0_function_data_pointer[pipe] = (uint8_t *)data;
768     g_usb0_function_PipeIgnore[pipe]   = 0;
769
770     g_usb0_function_PipeDataSize[pipe] = size;
771     g_usb0_function_pipe_status[pipe]  = DEVDRV_USBF_PIPE_WAIT;
772
773     mbw = usb0_function_get_mbw(size, (uint32_t)data);
774     usb0_function_set_curpipe(USB_FUNCTION_PIPE0, USB_FUNCTION_D1USE, DEVDRV_USBF_NO, mbw);
775
776     usb0_function_set_transaction_counter(pipe, size);
777
778     usb0_function_aclrm(pipe);
779
780     usb0_function_enable_nrdy_int(pipe);
781     usb0_function_enable_brdy_int(pipe);
782
783     usb0_function_set_pid_buf(pipe);
784 }
785
786 /*******************************************************************************
787 * Function Name: usb0_function_start_receive_dma_d0
788 * Description  : Reads data from the buffer allocated in the pipe specified in the argument.
789 *              : Reads data by DMA transfer using D0FIFO.
790 *              : This function does not read data from the buffer.
791 *              : When storing data in the buffer allocated in the pipe specified
792 *              : in the argument, delivered read request to DMAC to read data from
793 *              : the buffer by DMAC.
794 * Arguments    : uint16_t pipe      ; Pipe Number
795 *              : uint32_t size      ; Data Size
796 *              : uint8_t *data      ; Data Address
797 * Return Value : none
798 *******************************************************************************/
799 static void usb0_function_start_receive_dma_d0 (uint16_t pipe, uint32_t size, uint8_t * data)
800 {
801     uint16_t mbw;
802
803     usb0_function_set_pid_nak(pipe);
804     g_usb0_function_data_count[pipe]   = size;
805     g_usb0_function_data_pointer[pipe] = (uint8_t *)data;
806     g_usb0_function_PipeIgnore[pipe]   = 0;
807
808     g_usb0_function_PipeDataSize[pipe] = 0;
809     g_usb0_function_pipe_status[pipe]  = DEVDRV_USBF_PIPE_WAIT;
810
811     mbw = usb0_function_get_mbw(size, (uint32_t)data);
812     usb0_function_set_curpipe(USB_FUNCTION_PIPE0, USB_FUNCTION_D0USE, DEVDRV_USBF_NO, mbw);
813
814     usb0_function_set_transaction_counter(pipe, size);
815
816     usb0_function_aclrm(pipe);
817
818     if (RZA_IO_RegRead_16(&g_usb0_function_pipecfg[pipe], USB_PIPECFG_BFRE_SHIFT, USB_PIPECFG_BFRE) == 1)
819     {
820         usb0_function_read_dma(pipe);
821
822         usb0_function_enable_nrdy_int(pipe);
823         usb0_function_enable_brdy_int(pipe);
824     }
825     else
826     {
827         usb0_function_enable_nrdy_int(pipe);
828         usb0_function_enable_brdy_int(pipe);
829     }
830
831     usb0_function_set_pid_buf(pipe);
832 }
833
834 /*******************************************************************************
835 * Function Name: usb0_function_start_receive_dma_d1
836 * Description  : Read data from the buffer allocated in the pipe specified in the argument.
837 *              : Reads data by DMA transfer using D0FIFO.
838 *              : This function does not read data from the buffer.
839 *              : When storing data in the buffer allocated in the pipe specified
840 *              : in the argument, delivered read request to DMAC to read data from
841 *              : the buffer by DMAC.
842 * Arguments    : uint16_t pipe      ; Pipe Number
843 *              : uint32_t size      ; Data Size
844 *              : uint8_t *data      ; Data Address
845 * Return Value : none
846 *******************************************************************************/
847 static void usb0_function_start_receive_dma_d1 (uint16_t pipe, uint32_t size, uint8_t * data)
848 {
849     uint16_t mbw;
850
851     usb0_function_set_pid_nak(pipe);
852     g_usb0_function_data_count[pipe]   = size;
853     g_usb0_function_data_pointer[pipe] = (uint8_t *)data;
854     g_usb0_function_PipeIgnore[pipe]   = 0;
855
856     g_usb0_function_PipeDataSize[pipe] = 0;
857     g_usb0_function_pipe_status[pipe]  = DEVDRV_USBF_PIPE_WAIT;
858
859     mbw = usb0_function_get_mbw(size, (uint32_t)data);
860     usb0_function_set_curpipe(USB_FUNCTION_PIPE0, USB_FUNCTION_D1USE, DEVDRV_USBF_NO, mbw);
861
862     usb0_function_set_transaction_counter(pipe, size);
863
864     usb0_function_aclrm(pipe);
865
866     if (RZA_IO_RegRead_16(&g_usb0_function_pipecfg[pipe], USB_PIPECFG_BFRE_SHIFT, USB_PIPECFG_BFRE) == 1)
867     {
868         usb0_function_read_dma(pipe);
869
870         usb0_function_enable_nrdy_int(pipe);
871         usb0_function_enable_brdy_int(pipe);
872     }
873     else
874     {
875         usb0_function_enable_nrdy_int(pipe);
876         usb0_function_enable_brdy_int(pipe);
877     }
878
879     usb0_function_set_pid_buf(pipe);
880 }
881
882 /*******************************************************************************
883 * Function Name: usb0_function_read_buffer
884 * Description  : Reads data from the buffer allocated in the pipe specified
885 *              : in the argument.
886 *              : Uses FIF0 set in the pipe definition table.
887 * Arguments    : uint16_t pipe     ; Pipe Number
888 * Return Value : USB_FUNCTION_READEND          ; Read end
889 *              : USB_FUNCTION_READSHRT         ; short data
890 *              : USB_FUNCTION_READING          ; Continue of data read
891 *              : USB_FUNCTION_READOVER         ; buffer over
892 *              : DEVDRV_USBF_FIFOERROR         ; FIFO status
893 *******************************************************************************/
894 uint16_t usb0_function_read_buffer (uint16_t pipe)
895 {
896     uint16_t status;
897
898     g_usb0_function_PipeIgnore[pipe] = 0;
899
900     if ((g_usb0_function_PipeTbl[pipe] & USB_FUNCTION_FIFO_USE) == USB_FUNCTION_D0FIFO_USE)
901     {
902         status = usb0_function_read_buffer_d0(pipe);
903     }
904     else if ((g_usb0_function_PipeTbl[pipe] & USB_FUNCTION_FIFO_USE) == USB_FUNCTION_D1FIFO_USE)
905     {
906         status = usb0_function_read_buffer_d1(pipe);
907     }
908     else
909     {
910         status = usb0_function_read_buffer_c(pipe);
911     }
912
913     switch (status)
914     {
915         case USB_FUNCTION_READING:                                      /* Continue of data read */
916         break;
917
918         case USB_FUNCTION_READEND:                                      /* End of data read */
919         case USB_FUNCTION_READSHRT:                                     /* End of data read */
920             usb0_function_disable_brdy_int(pipe);
921             g_usb0_function_PipeDataSize[pipe] -= g_usb0_function_data_count[pipe];
922             g_usb0_function_pipe_status[pipe]   = DEVDRV_USBF_PIPE_DONE;
923         break;
924
925         case USB_FUNCTION_READOVER:                                     /* buffer over */
926             if ((g_usb0_function_PipeTbl[pipe] & USB_FUNCTION_FIFO_USE) == USB_FUNCTION_D0FIFO_USE)
927             {
928                 USB200.D0FIFOCTR = USB_FUNCTION_BITBCLR;                /* Clear BCLR */
929             }
930             else if ((g_usb0_function_PipeTbl[pipe] & USB_FUNCTION_FIFO_USE) == USB_FUNCTION_D1FIFO_USE)
931             {
932                 USB200.D1FIFOCTR = USB_FUNCTION_BITBCLR;                /* Clear BCLR */
933             }
934             else
935             {
936                 USB200.CFIFOCTR = USB_FUNCTION_BITBCLR;                 /* Clear BCLR */
937             }
938             usb0_function_disable_brdy_int(pipe);                       /* Disable Ready Interrupt */
939             g_usb0_function_PipeDataSize[pipe] -= g_usb0_function_data_count[pipe];
940             g_usb0_function_pipe_status[pipe]   = DEVDRV_USBF_FIFOERROR;
941         break;
942
943         case DEVDRV_USBF_FIFOERROR:                                     /* FIFO access status */
944         default:
945             usb0_function_disable_brdy_int(pipe);                       /* Disable Ready Interrupt */
946             g_usb0_function_pipe_status[pipe] = DEVDRV_USBF_FIFOERROR;
947         break;
948     }
949
950     return status;                                                      /* End or Err or Continue */
951 }
952
953 /*******************************************************************************
954 * Function Name: usb0_function_read_buffer_c
955 * Description  : Reads data from the buffer allocated in the pipe specified in the argument.
956 *              : Reads data by CPU transfer using CFIFO.
957 * Arguments    : uint16_t pipe     ; Pipe Number
958 * Return Value : USB_FUNCTION_READEND          ; Read end
959 *              : USB_FUNCTION_READSHRT         ; short data
960 *              : USB_FUNCTION_READING          ; Continue of data read
961 *              : USB_FUNCTION_READOVER         ; buffer over
962 *              : DEVDRV_USBF_FIFOERROR         ; FIFO status
963 *******************************************************************************/
964 uint16_t usb0_function_read_buffer_c (uint16_t pipe)
965 {
966     uint32_t count;
967     uint32_t dtln;
968     uint16_t buffer;
969     uint16_t mxps;
970     uint16_t status;
971     uint16_t mbw;
972
973     mbw    = usb0_function_get_mbw(g_usb0_function_data_count[pipe], (uint32_t)g_usb0_function_data_pointer[pipe]);
974     buffer = usb0_function_change_fifo_port(pipe, USB_FUNCTION_CUSE, DEVDRV_USBF_NO, mbw);
975
976     if (buffer == DEVDRV_USBF_FIFOERROR)                        /* FIFO access status */
977     {
978         return DEVDRV_USBF_FIFOERROR;
979     }
980
981     dtln = (uint32_t)(buffer & USB_FUNCTION_BITDTLN);
982     mxps = usb0_function_get_mxps(pipe);                        /* Max Packet Size */
983
984     if (g_usb0_function_data_count[pipe] < dtln)                /* Buffer Over ? */
985     {
986         status = USB_FUNCTION_READOVER;
987         usb0_function_set_pid_nak(pipe);                        /* Set NAK */
988         count = g_usb0_function_data_count[pipe];
989     }
990     else if (g_usb0_function_data_count[pipe] == dtln)          /* just Receive Size */
991     {
992         status = USB_FUNCTION_READEND;
993         usb0_function_set_pid_nak(pipe);                        /* Set NAK */
994         count = dtln;
995
996         if (count == 0)
997         {
998             status = USB_FUNCTION_READSHRT;                     /* Null Packet receive */
999         }
1000
1001         if ((count % mxps) != 0)
1002         {
1003             status = USB_FUNCTION_READSHRT;                     /* Short Packet receive */
1004         }
1005     }
1006     else                                                        /* continue Receive data */
1007     {
1008         status = USB_FUNCTION_READING;
1009         count  = dtln;
1010
1011         if (count == 0)
1012         {
1013             status = USB_FUNCTION_READSHRT;                     /* Null Packet receive */
1014             usb0_function_set_pid_nak(pipe);                    /* Set NAK */
1015         }
1016
1017         if ((count % mxps) != 0)
1018         {
1019             status = USB_FUNCTION_READSHRT;                     /* Short Packet receive */
1020             usb0_function_set_pid_nak(pipe);                    /* Set NAK */
1021         }
1022     }
1023
1024     if (count == 0)                                             /* 0 length packet */
1025     {
1026         USB200.CFIFOCTR = USB_FUNCTION_BITBCLR;                 /* Clear BCLR */
1027     }
1028     else
1029     {
1030         usb0_function_read_c_fifo(pipe, (uint16_t)count);
1031     }
1032
1033     g_usb0_function_data_count[pipe] -= count;
1034
1035     return status;                                              /* End or Err or Continue */
1036 }
1037
1038 /*******************************************************************************
1039 * Function Name: usb0_function_read_buffer_d0
1040 * Description  : Reads data from the buffer allocated in the pipe specified in
1041 *              : the argument.
1042 *              : Reads data by CPU transfer using D0FIFO.
1043 * Arguments    : uint16_t pipe     ; Pipe Number
1044 * Return Value : USB_FUNCTION_READEND          ; Read end
1045 *              : USB_FUNCTION_READSHRT         ; short data
1046 *              : USB_FUNCTION_READING          ; Continue of data read
1047 *              : USB_FUNCTION_READOVER         ; buffer over
1048 *              : DEVDRV_USBF_FIFOERROR         ; FIFO status
1049 *******************************************************************************/
1050 uint16_t usb0_function_read_buffer_d0 (uint16_t pipe)
1051 {
1052     uint32_t count;
1053     uint32_t dtln;
1054     uint16_t buffer;
1055     uint16_t mxps;
1056     uint16_t status;
1057     uint16_t mbw;
1058     uint16_t pipebuf_size;
1059
1060     mbw    = usb0_function_get_mbw(g_usb0_function_data_count[pipe], (uint32_t)g_usb0_function_data_pointer[pipe]);
1061     buffer = usb0_function_change_fifo_port(pipe, USB_FUNCTION_D0USE, DEVDRV_USBF_NO, mbw);
1062
1063     if (buffer == DEVDRV_USBF_FIFOERROR)                        /* FIFO access status */
1064     {
1065         return DEVDRV_USBF_FIFOERROR;
1066     }
1067
1068     dtln = (uint32_t)(buffer & USB_FUNCTION_BITDTLN);
1069     mxps = usb0_function_get_mxps(pipe);                        /* Max Packet Size */
1070
1071     if (g_usb0_function_data_count[pipe] < dtln)                /* Buffer Over ? */
1072     {
1073         status = USB_FUNCTION_READOVER;
1074         usb0_function_set_pid_nak(pipe);                        /* Set NAK */
1075         count = g_usb0_function_data_count[pipe];
1076     }
1077     else if (g_usb0_function_data_count[pipe] == dtln)          /* just Receive Size */
1078     {
1079         status = USB_FUNCTION_READEND;
1080         usb0_function_set_pid_nak(pipe);                        /* Set NAK */
1081         count = dtln;
1082
1083         if (count == 0)
1084         {
1085             status = USB_FUNCTION_READSHRT;                     /* Null Packet receive */
1086         }
1087
1088         if ((count % mxps) != 0)
1089         {
1090             status = USB_FUNCTION_READSHRT;                     /* Short Packet receive */
1091         }
1092     }
1093     else                                                        /* continue Receive data */
1094     {
1095         status = USB_FUNCTION_READING;
1096         count  = dtln;
1097
1098         if (count == 0)
1099         {
1100             status = USB_FUNCTION_READSHRT;                     /* Null Packet receive */
1101             usb0_function_set_pid_nak(pipe);                    /* Set NAK */
1102         }
1103
1104         if ((count % mxps) != 0)
1105         {
1106             status = USB_FUNCTION_READSHRT;                     /* Short Packet receive */
1107             usb0_function_set_pid_nak(pipe);                    /* Set NAK */
1108         }
1109         else
1110         {
1111             pipebuf_size = usb0_function_get_buf_size(pipe);    /* Data buffer size */
1112
1113             if (count != pipebuf_size)
1114             {
1115                 status = USB_FUNCTION_READSHRT;                 /* Short Packet receive */
1116                 usb0_function_set_pid_nak(pipe);                /* Set NAK */
1117             }
1118         }
1119     }
1120
1121     if (count == 0)                                             /* 0 length packet */
1122     {
1123         USB200.D0FIFOCTR = USB_FUNCTION_BITBCLR;                /* Clear BCLR */
1124     }
1125     else
1126     {
1127         usb0_function_read_d0_fifo(pipe, (uint16_t)count);
1128     }
1129
1130     g_usb0_function_data_count[pipe] -= count;
1131
1132     return status;                                              /* End or Err or Continue */
1133 }
1134
1135 /*******************************************************************************
1136 * Function Name: usb0_function_read_buffer_d1
1137 * Description  : Reads data from the buffer allocated in the pipe specified
1138 *              : in the argument.
1139 *              : Reads data by CPU transfer using D1FIFO.
1140 * Arguments    : uint16_t pipe     ; Pipe Number
1141 * Return Value : USB_FUNCTION_READEND          ; Read end
1142 *              : USB_FUNCTION_READSHRT         ; short data
1143 *              : USB_FUNCTION_READING          ; Continue of data read
1144 *              : USB_FUNCTION_READOVER         ; buffer over
1145 *              : DEVDRV_USBF_FIFOERROR         ; FIFO status
1146 *******************************************************************************/
1147 uint16_t usb0_function_read_buffer_d1 (uint16_t pipe)
1148 {
1149     uint32_t count;
1150     uint32_t dtln;
1151     uint16_t buffer;
1152     uint16_t mxps;
1153     uint16_t status;
1154     uint16_t mbw;
1155     uint16_t pipebuf_size;
1156
1157     mbw    = usb0_function_get_mbw(g_usb0_function_data_count[pipe], (uint32_t)g_usb0_function_data_pointer[pipe]);
1158     buffer = usb0_function_change_fifo_port(pipe, USB_FUNCTION_D1USE, DEVDRV_USBF_NO, mbw);
1159
1160     if (buffer == DEVDRV_USBF_FIFOERROR)                        /* FIFO access status */
1161     {
1162         return DEVDRV_USBF_FIFOERROR;
1163     }
1164
1165     dtln = (uint32_t)(buffer & USB_FUNCTION_BITDTLN);
1166     mxps = usb0_function_get_mxps(pipe);                        /* Max Packet Size */
1167
1168     if (g_usb0_function_data_count[pipe] < dtln)                /* Buffer Over ? */
1169     {
1170         status = USB_FUNCTION_READOVER;
1171         usb0_function_set_pid_nak(pipe);                        /* Set NAK */
1172         count = g_usb0_function_data_count[pipe];
1173     }
1174     else if (g_usb0_function_data_count[pipe] == dtln)          /* just Receive Size */
1175     {
1176         status = USB_FUNCTION_READEND;
1177         usb0_function_set_pid_nak(pipe);                        /* Set NAK */
1178         count = dtln;
1179         if (count == 0)
1180         {
1181             status = USB_FUNCTION_READSHRT;                     /* Null Packet receive */
1182         }
1183
1184         if ((count % mxps) != 0)
1185         {
1186             status = USB_FUNCTION_READSHRT;                     /* Short Packet receive */
1187         }
1188     }
1189     else                                                        /* continue Receive data */
1190     {
1191         status = USB_FUNCTION_READING;
1192         count  = dtln;
1193         if (count == 0)
1194         {
1195             status = USB_FUNCTION_READSHRT;                     /* Null Packet receive */
1196             usb0_function_set_pid_nak(pipe);                    /* Set NAK */
1197         }
1198
1199         if ((count % mxps) != 0)
1200         {
1201             status = USB_FUNCTION_READSHRT;                     /* Short Packet receive */
1202             usb0_function_set_pid_nak(pipe);                    /* Set NAK */
1203         }
1204         else
1205         {
1206             pipebuf_size = usb0_function_get_buf_size(pipe);    /* Data buffer size */
1207
1208             if (count != pipebuf_size)
1209             {
1210                 status = USB_FUNCTION_READSHRT;                 /* Short Packet receive */
1211                 usb0_function_set_pid_nak(pipe);                /* Set NAK */
1212             }
1213         }
1214     }
1215
1216     if (count == 0)                                             /* 0 length packet */
1217     {
1218         USB200.D1FIFOCTR = USB_FUNCTION_BITBCLR;                /* Clear BCLR */
1219     }
1220     else
1221     {
1222         usb0_function_read_d1_fifo(pipe, (uint16_t)count);
1223     }
1224
1225     g_usb0_function_data_count[pipe] -= count;
1226
1227     return status;                                              /* End or Err or Continue */
1228 }
1229
1230 /*******************************************************************************
1231 * Function Name: usb0_function_read_dma
1232 * Description  : Reads data from the buffer allocated in the pipe specified
1233 *              : in the argument.
1234 *              : Reads data by DMA transfer using D0FIFO or D1FIFO.
1235 * Arguments    : uint16_t pipe     ; Pipe Number
1236 * Return Value : USB_FUNCTION_READEND          ; Read end
1237 *              : USB_FUNCTION_READSHRT         ; short data
1238 *              : USB_FUNCTION_READING          ; Continue of data read
1239 *              : USB_FUNCTION_READOVER         ; buffer over
1240 *              : DEVDRV_USBF_FIFOERROR         ; FIFO status
1241 *******************************************************************************/
1242 uint16_t usb0_function_read_dma (uint16_t pipe)
1243 {
1244     uint16_t status;
1245
1246     g_usb0_function_PipeIgnore[pipe] = 0;
1247     if ((g_usb0_function_PipeTbl[pipe] & USB_FUNCTION_FIFO_USE) == USB_FUNCTION_D0FIFO_DMA)
1248     {
1249         status = usb0_function_read_dma_d0(pipe);
1250     }
1251     else
1252     {
1253         status = usb0_function_read_dma_d1(pipe);
1254     }
1255
1256     switch (status)
1257     {
1258         case USB_FUNCTION_READING:                                      /* Continue of data read */
1259         break;
1260
1261         case USB_FUNCTION_READZERO:                                     /* End of data read */
1262             usb0_function_disable_brdy_int(pipe);
1263             g_usb0_function_pipe_status[pipe] = DEVDRV_USBF_PIPE_DONE;
1264         break;
1265
1266         case USB_FUNCTION_READEND:                                      /* End of data read */
1267         case USB_FUNCTION_READSHRT:                                     /* End of data read */
1268             usb0_function_disable_brdy_int(pipe);
1269
1270             if (RZA_IO_RegRead_16(&g_usb0_function_pipecfg[pipe], USB_PIPECFG_BFRE_SHIFT, USB_PIPECFG_BFRE) == 1)
1271             {
1272                 g_usb0_function_PipeDataSize[pipe] -= g_usb0_function_data_count[pipe];
1273             }
1274         break;
1275
1276         case USB_FUNCTION_READOVER:                                     /* buffer over */
1277             usb0_function_disable_brdy_int(pipe);                       /* Disable Ready Interrupt */
1278
1279             if (RZA_IO_RegRead_16(&g_usb0_function_pipecfg[pipe], USB_PIPECFG_BFRE_SHIFT, USB_PIPECFG_BFRE) == 1)
1280             {
1281                 g_usb0_function_PipeDataSize[pipe] -= g_usb0_function_data_count[pipe];
1282             }
1283             g_usb0_function_pipe_status[pipe] = DEVDRV_USBF_FIFOERROR;
1284         break;
1285
1286         case DEVDRV_USBF_FIFOERROR:                                     /* FIFO access status */
1287         default:
1288             usb0_function_disable_brdy_int(pipe);                       /* Disable Ready Interrupt */
1289             g_usb0_function_pipe_status[pipe] = DEVDRV_USBF_FIFOERROR;
1290         break;
1291     }
1292
1293     return status;                                                      /* End or Err or Continue */
1294 }
1295
1296 /*******************************************************************************
1297 * Function Name: usb0_function_read_dma_d0
1298 * Description  : Writes data in the buffer allocated in the pipe specified
1299 *              : in the argument.
1300 *              : Reads data by DMA transfer using D0FIFO.
1301 * Arguments    : uint16_t pipe     ; Pipe Number
1302 * Return Value : USB_FUNCTION_READEND          ; Read end
1303 *              : USB_FUNCTION_READSHRT         ; short data
1304 *              : USB_FUNCTION_READZERO         ; zero data
1305 *              : USB_FUNCTION_READING          ; Continue of data read
1306 *              : USB_FUNCTION_READOVER         ; buffer over
1307 *              : DEVDRV_USBF_FIFOERROR         ; FIFO status
1308 *******************************************************************************/
1309 static uint16_t usb0_function_read_dma_d0 (uint16_t pipe)
1310 {
1311     uint32_t count;
1312     uint32_t dtln;
1313     uint16_t buffer;
1314     uint16_t mxps;
1315     uint16_t status;
1316     uint16_t mbw;
1317     uint16_t dfacc = 0;
1318     uint16_t pipebuf_size;
1319
1320     g_usb0_function_DmaStatus[USB_FUNCTION_D0FIFO] = USB_FUNCTION_DMA_READY;
1321
1322     mbw = usb0_function_get_mbw(g_usb0_function_data_count[pipe], (uint32_t)g_usb0_function_data_pointer[pipe]);
1323
1324     if (RZA_IO_RegRead_16(&g_usb0_function_pipecfg[pipe], USB_PIPECFG_BFRE_SHIFT, USB_PIPECFG_BFRE) == 1)
1325     {
1326         count  = g_usb0_function_data_count[pipe];
1327         status = USB_FUNCTION_READING;
1328     }
1329     else
1330     {
1331         buffer = usb0_function_change_fifo_port(pipe, USB_FUNCTION_D0DMA, DEVDRV_USBF_NO, mbw);
1332
1333         if (buffer == DEVDRV_USBF_FIFOERROR)                        /* FIFO access status */
1334         {
1335             return DEVDRV_USBF_FIFOERROR;
1336         }
1337
1338         dtln = (uint32_t)(buffer & USB_FUNCTION_BITDTLN);
1339         mxps = usb0_function_get_mxps(pipe);                        /* Max Packet Size */
1340
1341         if (g_usb0_function_data_count[pipe] < dtln)                /* Buffer Over ? */
1342         {
1343             status = USB_FUNCTION_READOVER;
1344             count  = g_usb0_function_data_count[pipe];
1345         }
1346         else if (g_usb0_function_data_count[pipe] == dtln)          /* just Receive Size */
1347         {
1348             status = USB_FUNCTION_READEND;
1349             count  = dtln;
1350             if (count == 0)
1351             {
1352                 status = USB_FUNCTION_READSHRT;                     /* Null Packet receive */
1353             }
1354
1355             if ((count % mxps) != 0)
1356             {
1357                 status = USB_FUNCTION_READSHRT;                     /* Short Packet receive */
1358             }
1359         }
1360         else                                                        /* continue Receive data */
1361         {
1362             status = USB_FUNCTION_READING;
1363             count  = dtln;
1364
1365             if (count == 0)
1366             {
1367                 status = USB_FUNCTION_READSHRT;                     /* Null Packet receive */
1368             }
1369
1370             if ((count % mxps) != 0)
1371             {
1372                 status = USB_FUNCTION_READSHRT;                     /* Short Packet receive */
1373             }
1374             else
1375             {
1376                 pipebuf_size = usb0_function_get_buf_size(pipe);    /* Data buffer size */
1377                 if (count != pipebuf_size)
1378                 {
1379                     status = USB_FUNCTION_READSHRT;                 /* Short Packet receive */
1380                 }
1381             }
1382         }
1383     }
1384
1385     if (count == 0)                                                 /* 0 length packet */
1386     {
1387         if (RZA_IO_RegRead_16(&g_usb0_function_pipecfg[pipe], USB_PIPECFG_BFRE_SHIFT, USB_PIPECFG_BFRE) == 0)
1388         {
1389             USB200.D0FIFOCTR = USB_FUNCTION_BITBCLR;                /* Clear BCLR */
1390             status = USB_FUNCTION_READZERO;                         /* Null Packet receive */
1391         }
1392         else
1393         {
1394             usb0_function_set_curpipe(pipe, USB_FUNCTION_D0DMA, DEVDRV_USBF_NO, mbw);
1395                                                                     /* transaction counter No set */
1396                                                                     /* FRDY = 1, DTLN = 0 -> BRDY */
1397         }
1398     }
1399     else
1400     {
1401         dfacc = usb0_function_set_dfacc_d0(mbw, count);
1402
1403         if (mbw == USB_FUNCTION_BITMBW_32)
1404         {
1405             g_usb0_function_DmaInfo[USB_FUNCTION_D0FIFO].size = 2;  /* 32bit transfer */
1406         }
1407         else if (mbw == USB_FUNCTION_BITMBW_16)
1408         {
1409             g_usb0_function_DmaInfo[USB_FUNCTION_D0FIFO].size = 1;  /* 16bit transfer */
1410         }
1411         else
1412         {
1413             g_usb0_function_DmaInfo[USB_FUNCTION_D0FIFO].size = 0;  /* 8bit transfer */
1414         }
1415
1416         g_usb0_function_DmaPipe[USB_FUNCTION_D0FIFO] = pipe;        /* not use in read operation */
1417         g_usb0_function_DmaBval[USB_FUNCTION_D0FIFO] = 0;           /* not use in read operation */
1418
1419         g_usb0_function_DmaInfo[USB_FUNCTION_D0FIFO].fifo   = USB_FUNCTION_D0FIFO_DMA;
1420         g_usb0_function_DmaInfo[USB_FUNCTION_D0FIFO].dir    = USB_FUNCTION_FIFO2BUF;
1421         g_usb0_function_DmaInfo[USB_FUNCTION_D0FIFO].buffer = (uint32_t)g_usb0_function_data_pointer[pipe];
1422         g_usb0_function_DmaInfo[USB_FUNCTION_D0FIFO].bytes  = count;
1423
1424         if (status == USB_FUNCTION_READING)
1425         {
1426             g_usb0_function_DmaStatus[USB_FUNCTION_D0FIFO] = USB_FUNCTION_DMA_BUSY;
1427         }
1428         else
1429         {
1430             g_usb0_function_DmaStatus[USB_FUNCTION_D0FIFO] = USB_FUNCTION_DMA_BUSYEND;
1431         }
1432
1433         Userdef_USB_usb0_function_start_dma(&g_usb0_function_DmaInfo[USB_FUNCTION_D0FIFO], dfacc);
1434
1435         usb0_function_set_curpipe2(pipe, USB_FUNCTION_D0DMA, DEVDRV_USBF_NO, mbw, dfacc);
1436
1437         RZA_IO_RegWrite_16(&USB200.D0FIFOSEL,
1438                             1,
1439                             USB_DnFIFOSEL_DREQE_SHIFT,
1440                             USB_DnFIFOSEL_DREQE);
1441     }
1442
1443     if (RZA_IO_RegRead_16(&g_usb0_function_pipecfg[pipe], USB_PIPECFG_BFRE_SHIFT, USB_PIPECFG_BFRE) == 0)
1444     {
1445         g_usb0_function_data_count[pipe]   -= count;
1446         g_usb0_function_data_pointer[pipe] += count;
1447         g_usb0_function_PipeDataSize[pipe] += count;
1448     }
1449
1450     return status;                                                  /* End or Err or Continue */
1451 }
1452
1453 /*******************************************************************************
1454 * Function Name: usb0_function_read_dma_d1
1455 * Description  : Reads data from the buffer allocated in the pipe specified in
1456 *              : the argument.
1457 *              : Reads data by DMA transfer using D1FIFO.
1458 * Arguments    : uint16_t pipe     ; Pipe Number
1459 * Return Value : USB_FUNCTION_READEND          ; Read end
1460 *              : USB_FUNCTION_READSHRT         ; short data
1461 *              : USB_FUNCTION_READZERO         ; zero data
1462 *              : USB_FUNCTION_READING          ; Continue of data read
1463 *              : USB_FUNCTION_READOVER         ; buffer over
1464 *              : DEVDRV_USBF_FIFOERROR         ; FIFO status
1465 *******************************************************************************/
1466 static uint16_t usb0_function_read_dma_d1 (uint16_t pipe)
1467 {
1468     uint32_t count;
1469     uint32_t dtln;
1470     uint16_t buffer;
1471     uint16_t mxps;
1472     uint16_t status;
1473     uint16_t mbw;
1474     uint16_t dfacc=0;
1475     uint16_t pipebuf_size;
1476
1477     g_usb0_function_DmaStatus[USB_FUNCTION_D1FIFO] = USB_FUNCTION_DMA_READY;
1478
1479     mbw = usb0_function_get_mbw(g_usb0_function_data_count[pipe], (uint32_t)g_usb0_function_data_pointer[pipe]);
1480
1481     if (RZA_IO_RegRead_16(&g_usb0_function_pipecfg[pipe], USB_PIPECFG_BFRE_SHIFT, USB_PIPECFG_BFRE) == 1)
1482     {
1483         count  = g_usb0_function_data_count[pipe];
1484         status = USB_FUNCTION_READING;
1485     }
1486     else
1487     {
1488         buffer = usb0_function_change_fifo_port(pipe, USB_FUNCTION_D1DMA, DEVDRV_USBF_NO, mbw);
1489         if (buffer == DEVDRV_USBF_FIFOERROR)                        /* FIFO access status */
1490         {
1491             return DEVDRV_USBF_FIFOERROR;
1492         }
1493
1494         dtln = (uint32_t)(buffer & USB_FUNCTION_BITDTLN);
1495         mxps = usb0_function_get_mxps(pipe);                        /* Max Packet Size */
1496
1497         if (g_usb0_function_data_count[pipe] < dtln)                /* Buffer Over ? */
1498         {
1499             status = USB_FUNCTION_READOVER;
1500             count  = g_usb0_function_data_count[pipe];
1501         }
1502         else if (g_usb0_function_data_count[pipe] == dtln)          /* just Receive Size */
1503         {
1504             status = USB_FUNCTION_READEND;
1505             count  = dtln;
1506             if (count == 0)
1507             {
1508                 status = USB_FUNCTION_READSHRT;                     /* Null Packet receive */
1509             }
1510
1511             if ((count % mxps) != 0)
1512             {
1513                 status = USB_FUNCTION_READSHRT;                     /* Short Packet receive */
1514             }
1515         }
1516         else                                                        /* continue Receive data */
1517         {
1518             status = USB_FUNCTION_READING;
1519             count  = dtln;
1520             if (count == 0)
1521             {
1522                 status = USB_FUNCTION_READSHRT;                     /* Null Packet receive */
1523             }
1524
1525             if ((count % mxps) != 0)
1526             {
1527                 status = USB_FUNCTION_READSHRT;                     /* Short Packet receive */
1528             }
1529             else
1530             {
1531                 pipebuf_size = usb0_function_get_buf_size(pipe);    /* Data buffer size */
1532                 if (count != pipebuf_size)
1533                 {
1534                     status = USB_FUNCTION_READSHRT;                 /* Short Packet receive */
1535                 }
1536             }
1537         }
1538     }
1539
1540     if (count == 0)                                                 /* 0 length packet */
1541     {
1542         if (RZA_IO_RegRead_16(&g_usb0_function_pipecfg[pipe], USB_PIPECFG_BFRE_SHIFT, USB_PIPECFG_BFRE) == 0)
1543         {
1544             USB200.D1FIFOCTR = USB_FUNCTION_BITBCLR;                /* Clear BCLR */
1545             status = USB_FUNCTION_READZERO;                         /* Null Packet receive */
1546         }
1547         else
1548         {
1549             usb0_function_set_curpipe(pipe, USB_FUNCTION_D1DMA, DEVDRV_USBF_NO, mbw);
1550                                                                     /* transaction counter No set */
1551                                                                     /* FRDY = 1, DTLN = 0 -> BRDY */
1552         }
1553     }
1554     else
1555     {
1556         dfacc = usb0_function_set_dfacc_d1(mbw, count);
1557
1558         if (mbw == USB_FUNCTION_BITMBW_32)
1559         {
1560             g_usb0_function_DmaInfo[USB_FUNCTION_D1FIFO].size = 2;  /* 32bit transfer */
1561         }
1562         else if (mbw == USB_FUNCTION_BITMBW_16)
1563         {
1564             g_usb0_function_DmaInfo[USB_FUNCTION_D1FIFO].size = 1;  /* 16bit transfer */
1565         }
1566         else
1567         {
1568             g_usb0_function_DmaInfo[USB_FUNCTION_D1FIFO].size = 0;  /* 8bit transfer */
1569         }
1570
1571         g_usb0_function_DmaPipe[USB_FUNCTION_D1FIFO] = pipe;        /* not use in read operation */
1572         g_usb0_function_DmaBval[USB_FUNCTION_D1FIFO] = 0;           /* not use in read operation */
1573
1574         g_usb0_function_DmaInfo[USB_FUNCTION_D1FIFO].fifo   = USB_FUNCTION_D1FIFO_DMA;
1575         g_usb0_function_DmaInfo[USB_FUNCTION_D1FIFO].dir    = USB_FUNCTION_FIFO2BUF;
1576         g_usb0_function_DmaInfo[USB_FUNCTION_D1FIFO].buffer = (uint32_t)g_usb0_function_data_pointer[pipe];
1577         g_usb0_function_DmaInfo[USB_FUNCTION_D1FIFO].bytes  = count;
1578
1579         if (status == USB_FUNCTION_READING)
1580         {
1581             g_usb0_function_DmaStatus[USB_FUNCTION_D1FIFO] = USB_FUNCTION_DMA_BUSY;
1582         }
1583         else
1584         {
1585             g_usb0_function_DmaStatus[USB_FUNCTION_D1FIFO] = USB_FUNCTION_DMA_BUSYEND;
1586         }
1587
1588         Userdef_USB_usb0_function_start_dma(&g_usb0_function_DmaInfo[USB_FUNCTION_D1FIFO], dfacc);
1589
1590         usb0_function_set_curpipe2(pipe, USB_FUNCTION_D1DMA, DEVDRV_USBF_NO, mbw, dfacc);
1591
1592         RZA_IO_RegWrite_16(&USB200.D1FIFOSEL,
1593                             1,
1594                             USB_DnFIFOSEL_DREQE_SHIFT,
1595                             USB_DnFIFOSEL_DREQE);
1596     }
1597
1598     if (RZA_IO_RegRead_16(&g_usb0_function_pipecfg[pipe], USB_PIPECFG_BFRE_SHIFT, USB_PIPECFG_BFRE) == 0)
1599     {
1600         g_usb0_function_data_count[pipe]   -= count;
1601         g_usb0_function_data_pointer[pipe] += count;
1602         g_usb0_function_PipeDataSize[pipe] += count;
1603     }
1604
1605     return status;                                                  /* End or Err or Continue */
1606 }
1607
1608 /*******************************************************************************
1609 * Function Name: usb0_function_change_fifo_port
1610 * Description  : Allocates FIF0 specified by the argument in the pipe assigned
1611 *              : by the argument. After allocating FIF0, waits in the software
1612 *              : till the corresponding pipe becomes ready.
1613 * Arguments    : uint16_t pipe      ; Pipe Number
1614 *              : uint16_t fifosel   ; Select FIFO
1615 *              : uint16_t isel      ; FIFO Access Direction
1616 *              : uint16_t mbw       ; FIFO Port Access Bit Width
1617 * Return Value : DEVDRV_USBF_FIFOERROR         ; Error
1618 *              : Others            ; CFIFOCTR/D0FIFOCTR/D1FIFOCTR Register Value
1619 *******************************************************************************/
1620 uint16_t usb0_function_change_fifo_port (uint16_t pipe, uint16_t fifosel, uint16_t isel, uint16_t mbw)
1621 {
1622     uint16_t buffer;
1623     uint32_t loop;
1624     volatile uint32_t loop2;
1625
1626     usb0_function_set_curpipe(pipe, fifosel, isel, mbw);
1627
1628     for (loop = 0; loop < 4; loop++)
1629     {
1630         switch (fifosel)
1631         {
1632             case USB_FUNCTION_CUSE:
1633                 buffer = USB200.CFIFOCTR;
1634             break;
1635
1636             case USB_FUNCTION_D0USE:
1637             case USB_FUNCTION_D0DMA:
1638                 buffer = USB200.D0FIFOCTR;
1639             break;
1640
1641             case USB_FUNCTION_D1USE:
1642             case USB_FUNCTION_D1DMA:
1643                 buffer = USB200.D1FIFOCTR;
1644             break;
1645
1646             default:
1647                 buffer = 0;
1648             break;
1649         }
1650
1651         if ((buffer & USB_FUNCTION_BITFRDY) == USB_FUNCTION_BITFRDY)
1652         {
1653             return buffer;
1654         }
1655
1656         loop2 = 25;
1657         while (loop2-- > 0)
1658         {
1659             /* wait */
1660         }
1661     }
1662
1663     return DEVDRV_USBF_FIFOERROR;
1664 }
1665
1666 /*******************************************************************************
1667 * Function Name: usb0_function_set_curpipe
1668 * Description  : Allocates FIF0 specified by the argument in the pipe assigned
1669 *              : by the argument.
1670 * Arguments    : uint16_t pipe      ; Pipe Number
1671 *              : uint16_t fifosel   ; Select FIFO
1672 *              : uint16_t isel      ; FIFO Access Direction
1673 *              : uint16_t mbw       ; FIFO Port Access Bit Width
1674 * Return Value : none
1675 *******************************************************************************/
1676 void usb0_function_set_curpipe (uint16_t pipe, uint16_t fifosel, uint16_t isel, uint16_t mbw)
1677 {
1678     uint16_t buffer;
1679     uint32_t loop;
1680     volatile uint32_t loop2;
1681
1682     g_usb0_function_mbw[pipe] = mbw;
1683
1684     switch (fifosel)
1685     {
1686         case USB_FUNCTION_CUSE:
1687             buffer  = USB200.CFIFOSEL;
1688             buffer &= (uint16_t)~(USB_FUNCTION_BITISEL | USB_FUNCTION_BITCURPIPE);
1689             buffer |= (uint16_t)(~isel & USB_FUNCTION_BITISEL);
1690             USB200.CFIFOSEL = buffer;
1691
1692             for (loop = 0; loop < 4; loop++)
1693             {
1694                 if ((USB200.CFIFOSEL & (USB_FUNCTION_BITISEL | USB_FUNCTION_BITCURPIPE)) ==
1695                         (buffer & (USB_FUNCTION_BITISEL | USB_FUNCTION_BITCURPIPE)))
1696                 {
1697                     break;
1698                 }
1699
1700                 loop2 = 100;
1701
1702                 while (loop2-- > 0)
1703                 {
1704                     /* wait */
1705                 }
1706             }
1707             buffer &= (uint16_t)~(USB_FUNCTION_BITISEL | USB_FUNCTION_BITCURPIPE | USB_FUNCTION_BITMBW);
1708             buffer |= (uint16_t)(isel | pipe | mbw);
1709             USB200.CFIFOSEL = buffer;
1710
1711             for (loop = 0; loop < 4; loop++)
1712             {
1713                 if ((USB200.CFIFOSEL & (USB_FUNCTION_BITISEL | USB_FUNCTION_BITCURPIPE)) ==
1714                         (buffer & (USB_FUNCTION_BITISEL | USB_FUNCTION_BITCURPIPE)))
1715                 {
1716                     break;
1717                 }
1718
1719                 loop2 = 100;
1720
1721                 while (loop2-- > 0)
1722                 {
1723                     /* wait */
1724                 }
1725             }
1726         break;
1727
1728         case USB_FUNCTION_D0DMA:
1729         case USB_FUNCTION_D0USE:
1730             buffer  = USB200.D0FIFOSEL;
1731             buffer &= (uint16_t)~(USB_FUNCTION_BITCURPIPE);
1732             USB200.D0FIFOSEL = buffer;
1733
1734             for (loop = 0; loop < 4; loop++)
1735             {
1736                 if ((USB200.D0FIFOSEL & USB_FUNCTION_BITCURPIPE) ==
1737                         (buffer & USB_FUNCTION_BITCURPIPE))
1738                 {
1739                     break;
1740                 }
1741
1742                 loop2 = 100;
1743
1744                 while (loop2-- > 0)
1745                 {
1746                     /* wait */
1747                 }
1748             }
1749             buffer &= (uint16_t)~(USB_FUNCTION_BITCURPIPE | USB_FUNCTION_BITMBW);
1750             buffer |= (uint16_t)(pipe | mbw);
1751             USB200.D0FIFOSEL = buffer;
1752
1753             for (loop = 0; loop < 4; loop++)
1754             {
1755                 if ((USB200.D0FIFOSEL & USB_FUNCTION_BITCURPIPE) ==
1756                         (buffer & USB_FUNCTION_BITCURPIPE))
1757                 {
1758                     break;
1759                 }
1760
1761                 loop2 = 100;
1762                 while (loop2-- > 0)
1763                 {
1764                     /* wait */
1765                 }
1766             }
1767         break;
1768
1769         case USB_FUNCTION_D1DMA:
1770         case USB_FUNCTION_D1USE:
1771             buffer  = USB200.D1FIFOSEL;
1772             buffer &= (uint16_t)~(USB_FUNCTION_BITCURPIPE);
1773             USB200.D1FIFOSEL = buffer;
1774
1775             for (loop = 0; loop < 4; loop++)
1776             {
1777                 if ((USB200.D1FIFOSEL & USB_FUNCTION_BITCURPIPE) ==
1778                         (buffer & USB_FUNCTION_BITCURPIPE))
1779                 {
1780                     break;
1781                 }
1782
1783                 loop2 = 100;
1784
1785                 while (loop2-- > 0)
1786                 {
1787                     /* wait */
1788                 }
1789             }
1790             buffer &= (uint16_t)~(USB_FUNCTION_BITCURPIPE | USB_FUNCTION_BITMBW);
1791             buffer |= (uint16_t)(pipe | mbw);
1792             USB200.D1FIFOSEL = buffer;
1793
1794             for (loop = 0; loop < 4; loop++)
1795             {
1796                 if ((USB200.D1FIFOSEL & USB_FUNCTION_BITCURPIPE) ==
1797                         (buffer & USB_FUNCTION_BITCURPIPE))
1798                 {
1799                     break;
1800                 }
1801
1802                 loop2 = 100;
1803
1804                 while (loop2-- > 0)
1805                 {
1806                     /* wait */
1807                 }
1808             }
1809         break;
1810
1811         default:
1812         break;
1813     }
1814
1815     /* Cautions !!!
1816      * Depending on the external bus speed of CPU, you may need to wait for 450ns here.
1817      * For details, please look at the data sheet.   */
1818     loop2 = 100;
1819
1820     while (loop2-- > 0)
1821     {
1822         /* wait */
1823     }
1824 }
1825
1826 /*******************************************************************************
1827 * Function Name: usb0_function_set_curpipe2
1828 * Description  : Allocates FIF0 specified by the argument in the pipe assigned
1829 *              : by the argument.
1830 * Arguments    : uint16_t pipe      ; Pipe Number
1831 *              : uint16_t fifosel   ; Select FIFO
1832 *              : uint16_t isel      ; FIFO Access Direction
1833 *              : uint16_t mbw       ; FIFO Port Access Bit Width
1834 *              : uint16_t dfacc     ; DFACC Access mode
1835 * Return Value : none
1836 *******************************************************************************/
1837 void usb0_function_set_curpipe2 (uint16_t pipe, uint16_t fifosel, uint16_t isel, uint16_t mbw, uint16_t dfacc)
1838 {
1839     uint16_t buffer;
1840     uint32_t loop;
1841 #ifdef  __USB_FUNCTION_DF_ACC_ENABLE__
1842     uint32_t dummy;
1843 #endif
1844     volatile uint32_t loop2;
1845
1846     g_usb0_function_mbw[pipe] = mbw;
1847
1848     switch (fifosel)
1849     {
1850         case USB_FUNCTION_CUSE:
1851             buffer  = USB200.CFIFOSEL;
1852             buffer &= (uint16_t)~(USB_FUNCTION_BITISEL | USB_FUNCTION_BITCURPIPE);
1853             buffer |= (uint16_t)(~isel & USB_FUNCTION_BITISEL);
1854             USB200.CFIFOSEL = buffer;
1855
1856             for (loop = 0; loop < 4; loop++)
1857             {
1858                 if ((USB200.CFIFOSEL & (USB_FUNCTION_BITISEL | USB_FUNCTION_BITCURPIPE)) ==
1859                         (buffer & (USB_FUNCTION_BITISEL | USB_FUNCTION_BITCURPIPE)))
1860                 {
1861                     break;
1862                 }
1863
1864                 loop2 = 100;
1865                 while (loop2-- > 0)
1866                 {
1867                     /* wait */
1868                 }
1869             }
1870             buffer &= (uint16_t)~(USB_FUNCTION_BITISEL | USB_FUNCTION_BITCURPIPE | USB_FUNCTION_BITMBW);
1871             buffer |= (uint16_t)(isel | pipe | mbw);
1872             USB200.CFIFOSEL = buffer;
1873
1874             for (loop = 0; loop < 4; loop++)
1875             {
1876                 if ((USB200.CFIFOSEL & (USB_FUNCTION_BITISEL | USB_FUNCTION_BITCURPIPE)) ==
1877                         (buffer & (USB_FUNCTION_BITISEL | USB_FUNCTION_BITCURPIPE)))
1878                 {
1879                     break;
1880                 }
1881
1882                 loop2 = 100;
1883
1884                 while (loop2-- > 0)
1885                 {
1886                     /* wait */
1887                 }
1888             }
1889         break;
1890
1891         case USB_FUNCTION_D0DMA:
1892         case USB_FUNCTION_D0USE:
1893             buffer  = USB200.D0FIFOSEL;
1894 #ifdef  __USB_FUNCTION_DF_ACC_ENABLE__
1895             buffer &= (uint16_t)~(USB_FUNCTION_BITCURPIPE | USB_FUNCTION_BITMBW);
1896
1897             if (dfacc != 0)
1898             {
1899                 buffer |= (uint16_t)(USB_FUNCTION_BITMBW_32);
1900             }
1901 #else
1902             buffer &= (uint16_t)~(USB_FUNCTION_BITCURPIPE);
1903 #endif
1904             USB200.D0FIFOSEL = buffer;
1905
1906             for (loop = 0; loop < 4; loop++)
1907             {
1908                 if ((USB200.D0FIFOSEL & USB_FUNCTION_BITCURPIPE) == (buffer & USB_FUNCTION_BITCURPIPE))
1909                 {
1910                     break;
1911                 }
1912
1913                 loop2 = 100;
1914
1915                 while (loop2-- > 0)
1916                 {
1917                     /* wait */
1918                 }
1919             }
1920
1921 #ifdef  __USB_FUNCTION_DF_ACC_ENABLE__
1922             if (dfacc != 0)
1923             {
1924                 dummy = USB200.D0FIFO.UINT32;
1925             }
1926 #endif
1927             buffer &= (uint16_t)~(USB_FUNCTION_BITCURPIPE | USB_FUNCTION_BITMBW);
1928             buffer |= (uint16_t)(pipe | mbw);
1929             USB200.D0FIFOSEL = buffer;
1930
1931             for (loop = 0; loop < 4; loop++)
1932             {
1933                 if ((USB200.D0FIFOSEL & USB_FUNCTION_BITCURPIPE) == (buffer & USB_FUNCTION_BITCURPIPE))
1934                 {
1935                     break;
1936                 }
1937
1938                 loop2 = 100;
1939
1940                 while (loop2-- > 0)
1941                 {
1942                     /* wait */
1943                 }
1944             }
1945         break;
1946
1947         case USB_FUNCTION_D1DMA:
1948         case USB_FUNCTION_D1USE:
1949             buffer  = USB200.D1FIFOSEL;
1950 #ifdef  __USB_FUNCTION_DF_ACC_ENABLE__
1951             buffer &= (uint16_t)~(USB_FUNCTION_BITCURPIPE | USB_FUNCTION_BITMBW);
1952
1953             if (dfacc != 0)
1954             {
1955                 buffer |= (uint16_t)(USB_FUNCTION_BITMBW_32);
1956             }
1957 #else
1958             buffer &= (uint16_t)~(USB_FUNCTION_BITCURPIPE);
1959 #endif
1960             USB200.D1FIFOSEL = buffer;
1961
1962             for (loop = 0; loop < 4; loop++)
1963             {
1964                 if ((USB200.D1FIFOSEL & USB_FUNCTION_BITCURPIPE) == (buffer & USB_FUNCTION_BITCURPIPE))
1965                 {
1966                     break;
1967                 }
1968
1969                 loop2 = 100;
1970
1971                 while (loop2-- > 0)
1972                 {
1973                     /* wait */
1974                 }
1975             }
1976 #ifdef  __USB_FUNCTION_DF_ACC_ENABLE__
1977             if (dfacc != 0)
1978             {
1979                 dummy = USB200.D1FIFO.UINT32;
1980                 loop = dummy;                   // avoid warning.
1981             }
1982 #endif
1983             buffer &= (uint16_t)~(USB_FUNCTION_BITCURPIPE | USB_FUNCTION_BITMBW);
1984             buffer |= (uint16_t)(pipe | mbw);
1985             USB200.D1FIFOSEL = buffer;
1986
1987             for (loop = 0; loop < 4; loop++)
1988             {
1989                 if ((USB200.D1FIFOSEL & USB_FUNCTION_BITCURPIPE) == (buffer & USB_FUNCTION_BITCURPIPE))
1990                 {
1991                     break;
1992                 }
1993
1994                 loop2 = 100;
1995
1996                 while (loop2-- > 0)
1997                 {
1998                     /* wait */
1999                 }
2000             }
2001         break;
2002
2003         default:
2004         break;
2005     }
2006
2007     /* Cautions !!!
2008      * Depending on the external bus speed of CPU, you may need to wait for 450ns here.
2009      * For details, please look at the data sheet.   */
2010     loop2 = 100;
2011
2012     while (loop2-- > 0)
2013     {
2014         /* wait */
2015     }
2016 }
2017
2018 /*******************************************************************************
2019 * Function Name: usb0_function_write_c_fifo
2020 * Description  : Writes data in CFIFO.
2021 *              : Writes data by BYTE/WORD/LONG according to access size
2022 *              : to the pipe specified by the arguments.
2023 *              : Before executing this function, allocating CFIF0 in the specified pipe
2024 *              : should be completed.
2025 *              : Before executing this function, access size to the specified pipe
2026 *              : should be fixed and set in g_usb0_function_mbw[].
2027 * Arguments    : uint16_t pipe      ; Pipe Number
2028 *              : uint16_t count     ; Data Size(Byte)
2029 * Return Value : none
2030 *******************************************************************************/
2031 static void usb0_function_write_c_fifo (uint16_t pipe, uint16_t count)
2032 {
2033     uint16_t even;
2034
2035     if (g_usb0_function_mbw[pipe] == USB_FUNCTION_BITMBW_8)
2036     {
2037         for (even = count; even; --even)
2038         {
2039             USB200.CFIFO.UINT8[HH] = *g_usb0_function_data_pointer[pipe];
2040             g_usb0_function_data_pointer[pipe] += 1;
2041         }
2042     }
2043     else if (g_usb0_function_mbw[pipe] == USB_FUNCTION_BITMBW_16)
2044     {
2045         for (even = (uint16_t)(count / 2); even; --even)
2046         {
2047             USB200.CFIFO.UINT16[H] = *((uint16_t *)g_usb0_function_data_pointer[pipe]);
2048             g_usb0_function_data_pointer[pipe] += 2;
2049         }
2050     }
2051     else
2052     {
2053         for (even = (uint16_t)(count / 4); even; --even)
2054         {
2055             USB200.CFIFO.UINT32 = *((uint32_t *)g_usb0_function_data_pointer[pipe]);
2056             g_usb0_function_data_pointer[pipe] += 4;
2057         }
2058     }
2059 }
2060
2061 /*******************************************************************************
2062 * Function Name: usb0_function_read_c_fifo
2063 * Description  : Reads data from CFIFO.
2064 *              : Reads data by BYTE/WORD/LONG according to access size
2065 *              : to the pipe specified by the arguments.
2066 *              : Before executing this function, allocating CFIF0 in the specified pipe
2067 *              : should be completed.
2068 *              : Before executing this function, access size to the specified pipe
2069 *              : should be fixed and set in g_usb0_function_mbw[].
2070 * Arguments    : uint16_t pipe      ; Pipe Number
2071 *              : uint16_t count     ; Data Size(Byte)
2072 * Return Value : none
2073 *******************************************************************************/
2074 static void usb0_function_read_c_fifo (uint16_t pipe, uint16_t count)
2075 {
2076     uint16_t even;
2077
2078     if (g_usb0_function_mbw[pipe] == USB_FUNCTION_BITMBW_8)
2079     {
2080         for (even = count; even; --even)
2081         {
2082             *g_usb0_function_data_pointer[pipe] = USB200.CFIFO.UINT8[HH];
2083             g_usb0_function_data_pointer[pipe] += 1;
2084         }
2085     }
2086     else if (g_usb0_function_mbw[pipe] == USB_FUNCTION_BITMBW_16)
2087     {
2088         for (even = (uint16_t)((count + 1) / 2); even; --even)
2089         {
2090             *((uint16_t *)g_usb0_function_data_pointer[pipe]) = USB200.CFIFO.UINT16[H];
2091             g_usb0_function_data_pointer[pipe] += 2;
2092         }
2093     }
2094     else
2095     {
2096         for (even = (uint16_t)((count + 3) / 4); even; --even)
2097         {
2098             *((uint32_t *)g_usb0_function_data_pointer[pipe]) = USB200.CFIFO.UINT32;
2099             g_usb0_function_data_pointer[pipe] += 4;
2100         }
2101     }
2102 }
2103
2104 /*******************************************************************************
2105 * Function Name: usb0_function_write_d0_fifo
2106 * Description  : Writes data in D0FIFO.
2107 *              : Writes data by BYTE/WORD/LONG according to access size
2108 *              : to the pipe specified by the arguments.
2109 *              : Before executing this function, allocating CFIF0 in the specified pipe
2110 *              : should be completed.
2111 *              : Before executing this function, access size to the specified pipe
2112 *              : should be fixed and set in g_usb0_function_mbw[].
2113 * Arguments    : uint16_t pipe      ; Pipe Number
2114 *              : uint16_t count     ; Data Size(Byte)
2115 * Return Value : none
2116 *******************************************************************************/
2117 static void usb0_function_write_d0_fifo (uint16_t pipe, uint16_t count)
2118 {
2119     uint16_t even;
2120
2121     if (g_usb0_function_mbw[pipe] == USB_FUNCTION_BITMBW_8)
2122     {
2123         for (even = count; even; --even)
2124         {
2125             USB200.D0FIFO.UINT8[HH] = *g_usb0_function_data_pointer[pipe];
2126             g_usb0_function_data_pointer[pipe] += 1;
2127         }
2128     }
2129     else if (g_usb0_function_mbw[pipe] == USB_FUNCTION_BITMBW_16)
2130     {
2131         for (even = (uint16_t)(count / 2); even; --even)
2132         {
2133             USB200.D0FIFO.UINT16[H] = *((uint16_t *)g_usb0_function_data_pointer[pipe]);
2134             g_usb0_function_data_pointer[pipe] += 2;
2135         }
2136     }
2137     else
2138     {
2139         for (even = (uint16_t)(count / 4); even; --even)
2140         {
2141             USB200.D0FIFO.UINT32 = *((uint32_t *)g_usb0_function_data_pointer[pipe]);
2142             g_usb0_function_data_pointer[pipe] += 4;
2143         }
2144     }
2145 }
2146
2147 /*******************************************************************************
2148 * Function Name: usb0_function_read_d0_fifo
2149 * Description  : Reads data from D0FIFO.
2150 *              : Reads data by BYTE/WORD/LONG according to access size
2151 *              : to the pipe specified by the arguments.
2152 *              : Before executing this function, allocating DOFIF0 in the specified pipe
2153 *              : should be completed.
2154 *              : Before executing this function, access size to the specified pipe
2155 *              : should be fixed and set in g_usb0_function_mbw[].
2156 * Arguments    : uint16_t pipe      ; Pipe Number
2157 *              : uint16_t count     ; Data Size(Byte)
2158 * Return Value : none
2159 *******************************************************************************/
2160 static void usb0_function_read_d0_fifo (uint16_t pipe, uint16_t count)
2161 {
2162     uint16_t even;
2163
2164     if (g_usb0_function_mbw[pipe] == USB_FUNCTION_BITMBW_8)
2165     {
2166         for (even = count; even; --even)
2167         {
2168             *g_usb0_function_data_pointer[pipe] = USB200.D0FIFO.UINT8[HH];
2169             g_usb0_function_data_pointer[pipe] += 1;
2170         }
2171     }
2172     else if (g_usb0_function_mbw[pipe] == USB_FUNCTION_BITMBW_16)
2173     {
2174         for (even = (uint16_t)((count + 1) / 2); even; --even)
2175         {
2176             *((uint16_t *)g_usb0_function_data_pointer[pipe]) = USB200.D0FIFO.UINT16[H];
2177             g_usb0_function_data_pointer[pipe] += 2;
2178         }
2179     }
2180     else
2181     {
2182         for (even = (uint16_t)((count + 3) / 4); even; --even)
2183         {
2184             *((uint32_t *)g_usb0_function_data_pointer[pipe]) = USB200.D0FIFO.UINT32;
2185             g_usb0_function_data_pointer[pipe] += 4;
2186         }
2187     }
2188 }
2189
2190 /*******************************************************************************
2191 * Function Name: usb0_function_write_d1_fifo
2192 * Description  : Writes data in D1FIFO.
2193 *              : Writes data by BYTE/WORD/LONG according to access size
2194 *              : to the pipe specified by the arguments.
2195 *              : Before executing this function, allocating D1FIF0 in the specified pipe
2196 *              : should be completed.
2197 *              : Before executing this function, access size to the specified pipe
2198 *              : should be fixed and set in g_usb0_function_mbw[].
2199 * Arguments    : uint16_t pipe      ; Pipe Number
2200 *              : uint16_t count     ; Data Size(Byte)
2201 * Return Value : none
2202 *******************************************************************************/
2203 static void usb0_function_write_d1_fifo (uint16_t pipe, uint16_t count)
2204 {
2205     uint16_t even;
2206
2207     if (g_usb0_function_mbw[pipe] == USB_FUNCTION_BITMBW_8)
2208     {
2209         for (even = count; even; --even)
2210         {
2211             USB200.D1FIFO.UINT8[HH] = *g_usb0_function_data_pointer[pipe];
2212             g_usb0_function_data_pointer[pipe] += 1;
2213         }
2214     }
2215     else if (g_usb0_function_mbw[pipe] == USB_FUNCTION_BITMBW_16)
2216     {
2217         for (even = (uint16_t)(count / 2); even; --even)
2218         {
2219             USB200.D1FIFO.UINT16[H] = *((uint16_t *)g_usb0_function_data_pointer[pipe]);
2220             g_usb0_function_data_pointer[pipe] += 2;
2221         }
2222     }
2223     else
2224     {
2225         for (even = (uint16_t)(count / 4); even; --even)
2226         {
2227             USB200.D1FIFO.UINT32 = *((uint32_t *)g_usb0_function_data_pointer[pipe]);
2228             g_usb0_function_data_pointer[pipe] += 4;
2229         }
2230     }
2231 }
2232
2233 /*******************************************************************************
2234 * Function Name: usb0_function_read_d1_fifo
2235 * Description  : Reads data from D1FIFO.
2236 *              : Reads data by BYTE/WORD/LONG according to access size
2237 *              : to the pipe specified by the arguments.
2238 *              : Before executing this function, allocating D1FIF0 in the specified pipe
2239 *              : should be completed.
2240 *              : Before executing this function, access size to the specified pipe
2241 *              : should be fixed and set in g_usb0_function_mbw[].
2242 * Arguments    : uint16_t pipe      ; Pipe Number
2243 *              : uint16_t count     ; Data Size(Byte)
2244 * Return Value : none
2245 *******************************************************************************/
2246 static void usb0_function_read_d1_fifo (uint16_t pipe, uint16_t count)
2247 {
2248     uint16_t even;
2249
2250     if (g_usb0_function_mbw[pipe] == USB_FUNCTION_BITMBW_8)
2251     {
2252         for (even = count; even; --even)
2253         {
2254             *g_usb0_function_data_pointer[pipe] = USB200.D1FIFO.UINT8[HH];
2255             g_usb0_function_data_pointer[pipe] += 1;
2256         }
2257     }
2258     else if (g_usb0_function_mbw[pipe] == USB_FUNCTION_BITMBW_16)
2259     {
2260         for (even = (uint16_t)((count + 1) / 2); even; --even)
2261         {
2262             *((uint16_t *)g_usb0_function_data_pointer[pipe]) = USB200.D1FIFO.UINT16[H];
2263             g_usb0_function_data_pointer[pipe] += 2;
2264         }
2265     }
2266     else
2267     {
2268         for (even = (uint16_t)((count + 3) / 4); even; --even)
2269         {
2270             *((uint32_t *)g_usb0_function_data_pointer[pipe]) = USB200.D1FIFO.UINT32;
2271             g_usb0_function_data_pointer[pipe] += 4;
2272         }
2273     }
2274 }
2275
2276 /*******************************************************************************
2277 * Function Name: usb0_function_com_get_dmasize
2278 * Description  : Calculates access width of DMA transfer by the argument to
2279 *              : return as the Return Value.
2280 * Arguments    : uint32_t trncount   : transfer byte
2281 *              : uint32_t dtptr      : transfer data pointer
2282 * Return Value : DMA transfer size    : 0   8bit
2283 *              :                      : 1  16bit
2284 *              :                      : 2  32bit
2285 *******************************************************************************/
2286 static uint32_t usb0_function_com_get_dmasize (uint32_t trncount, uint32_t dtptr)
2287 {
2288     uint32_t size;
2289
2290     if (((trncount & 0x0001) != 0) || ((dtptr & 0x00000001) != 0))
2291     {
2292         /*  When transfer byte count is odd         */
2293         /* or transfer data area is 8-bit alignment */
2294         size = 0;           /* 8bit */
2295     }
2296     else if (((trncount & 0x0003) != 0) || ((dtptr & 0x00000003) != 0))
2297     {
2298         /* When the transfer byte count is multiples of 2 */
2299         /* or the transfer data area is 16-bit alignment */
2300         size = 1;           /* 16bit */
2301     }
2302     else
2303     {
2304         /* When the transfer byte count is multiples of 4 */
2305         /* or the transfer data area is 32-bit alignment */
2306         size = 2;           /* 32bit */
2307     }
2308
2309     return size;
2310 }
2311
2312 /*******************************************************************************
2313 * Function Name: usb0_function_get_mbw
2314 * Description  : Calculates access width of DMA to return the value set in MBW.
2315 * Arguments    : uint32_t trncount   : transfer byte
2316 *              : uint32_t dtptr      : transfer data pointer
2317 * Return Value : FIFO transfer size   : USB_FUNCTION_BITMBW_8    8bit
2318 *              :                      : USB_FUNCTION_BITMBW_16  16bit
2319 *              :                      : USB_FUNCTION_BITMBW_32  32bit
2320 *******************************************************************************/
2321 uint16_t usb0_function_get_mbw (uint32_t trncount, uint32_t dtptr)
2322 {
2323     uint32_t size;
2324     uint16_t mbw;
2325
2326     size = usb0_function_com_get_dmasize(trncount, dtptr);
2327
2328     if (size == 0)
2329     {
2330         /* 8bit */
2331         mbw = USB_FUNCTION_BITMBW_8;
2332     }
2333     else if (size == 1)
2334     {
2335         /* 16bit */
2336         mbw = USB_FUNCTION_BITMBW_16;
2337     }
2338     else
2339     {
2340         /* 32bit */
2341         mbw = USB_FUNCTION_BITMBW_32;
2342     }
2343
2344     return mbw;
2345 }
2346
2347 /*******************************************************************************
2348 * Function Name: usb0_function_set_transaction_counter
2349 * Description  : Sets transaction counter by the argument(PIPEnTRN).
2350 *              : Clears transaction before setting to enable transaction counter setting.
2351 * Arguments    : uint16_t pipe     ; Pipe number
2352 *              : uint32_t bsize    : Data transfer size
2353 * Return Value : none
2354 *******************************************************************************/
2355 static void usb0_function_set_transaction_counter (uint16_t pipe, uint32_t bsize)
2356 {
2357     uint16_t mxps;
2358     uint16_t cnt;
2359
2360     if (bsize  == 0)
2361     {
2362         return;
2363     }
2364
2365     mxps = usb0_function_get_mxps(pipe);            /* Max Packet Size */
2366
2367     if ((bsize % mxps) == 0)
2368     {
2369         cnt = (uint16_t)(bsize / mxps);
2370     }
2371     else
2372     {
2373         cnt = (uint16_t)((bsize / mxps) + 1);
2374     }
2375
2376     switch (pipe)
2377     {
2378         case USB_FUNCTION_PIPE1:
2379             RZA_IO_RegWrite_16(&USB200.PIPE1TRE,
2380                                 1,
2381                                 USB_PIPEnTRE_TRCLR_SHIFT,
2382                                 USB_PIPEnTRE_TRCLR);
2383             USB200.PIPE1TRN             = cnt;
2384             RZA_IO_RegWrite_16(&USB200.PIPE1TRE,
2385                                 1,
2386                                 USB_PIPEnTRE_TRENB_SHIFT,
2387                                 USB_PIPEnTRE_TRENB);
2388         break;
2389
2390         case USB_FUNCTION_PIPE2:
2391             RZA_IO_RegWrite_16(&USB200.PIPE2TRE,
2392                                 1,
2393                                 USB_PIPEnTRE_TRCLR_SHIFT,
2394                                 USB_PIPEnTRE_TRCLR);
2395             USB200.PIPE2TRN             = cnt;
2396             RZA_IO_RegWrite_16(&USB200.PIPE2TRE,
2397                                 1,
2398                                 USB_PIPEnTRE_TRENB_SHIFT,
2399                                 USB_PIPEnTRE_TRENB);
2400         break;
2401
2402         case USB_FUNCTION_PIPE3:
2403             RZA_IO_RegWrite_16(&USB200.PIPE3TRE,
2404                                 1,
2405                                 USB_PIPEnTRE_TRCLR_SHIFT,
2406                                 USB_PIPEnTRE_TRCLR);
2407             USB200.PIPE3TRN             = cnt;
2408             RZA_IO_RegWrite_16(&USB200.PIPE3TRE,
2409                                 1,
2410                                 USB_PIPEnTRE_TRENB_SHIFT,
2411                                 USB_PIPEnTRE_TRENB);
2412         break;
2413
2414         case USB_FUNCTION_PIPE4:
2415             RZA_IO_RegWrite_16(&USB200.PIPE4TRE,
2416                                 1,
2417                                 USB_PIPEnTRE_TRCLR_SHIFT,
2418                                 USB_PIPEnTRE_TRCLR);
2419             USB200.PIPE4TRN             = cnt;
2420             RZA_IO_RegWrite_16(&USB200.PIPE4TRE,
2421                                 1,
2422                                 USB_PIPEnTRE_TRENB_SHIFT,
2423                                 USB_PIPEnTRE_TRENB);
2424         break;
2425
2426         case USB_FUNCTION_PIPE5:
2427             RZA_IO_RegWrite_16(&USB200.PIPE5TRE,
2428                                 1,
2429                                 USB_PIPEnTRE_TRCLR_SHIFT,
2430                                 USB_PIPEnTRE_TRCLR);
2431             USB200.PIPE5TRN             = cnt;
2432             RZA_IO_RegWrite_16(&USB200.PIPE5TRE,
2433                                 1,
2434                                 USB_PIPEnTRE_TRENB_SHIFT,
2435                                 USB_PIPEnTRE_TRENB);
2436         break;
2437
2438         case USB_FUNCTION_PIPE9:
2439             RZA_IO_RegWrite_16(&USB200.PIPE9TRE,
2440                                 1,
2441                                 USB_PIPEnTRE_TRCLR_SHIFT,
2442                                 USB_PIPEnTRE_TRCLR);
2443             USB200.PIPE9TRN             = cnt;
2444             RZA_IO_RegWrite_16(&USB200.PIPE9TRE,
2445                                 1,
2446                                 USB_PIPEnTRE_TRENB_SHIFT,
2447                                 USB_PIPEnTRE_TRENB);
2448         break;
2449
2450         case USB_FUNCTION_PIPEA:
2451             RZA_IO_RegWrite_16(&USB200.PIPEATRE,
2452                                 1,
2453                                 USB_PIPEnTRE_TRCLR_SHIFT,
2454                                 USB_PIPEnTRE_TRCLR);
2455             USB200.PIPEATRN             = cnt;
2456             RZA_IO_RegWrite_16(&USB200.PIPEATRE,
2457                                 1,
2458                                 USB_PIPEnTRE_TRENB_SHIFT,
2459                                 USB_PIPEnTRE_TRENB);
2460         break;
2461
2462         case USB_FUNCTION_PIPEB:
2463             RZA_IO_RegWrite_16(&USB200.PIPEBTRE,
2464                                 1,
2465                                 USB_PIPEnTRE_TRCLR_SHIFT,
2466                                 USB_PIPEnTRE_TRCLR);
2467             USB200.PIPEBTRN             = cnt;
2468             RZA_IO_RegWrite_16(&USB200.PIPEBTRE,
2469                                 1,
2470                                 USB_PIPEnTRE_TRENB_SHIFT,
2471                                 USB_PIPEnTRE_TRENB);
2472         break;
2473
2474         case USB_FUNCTION_PIPEC:
2475             RZA_IO_RegWrite_16(&USB200.PIPECTRE,
2476                                 1,
2477                                 USB_PIPEnTRE_TRCLR_SHIFT,
2478                                 USB_PIPEnTRE_TRCLR);
2479             USB200.PIPECTRN             = cnt;
2480             RZA_IO_RegWrite_16(&USB200.PIPECTRE,
2481                                 1,
2482                                 USB_PIPEnTRE_TRENB_SHIFT,
2483                                 USB_PIPEnTRE_TRENB);
2484         break;
2485
2486         case USB_FUNCTION_PIPED:
2487             RZA_IO_RegWrite_16(&USB200.PIPEDTRE,
2488                                 1,
2489                                 USB_PIPEnTRE_TRCLR_SHIFT,
2490                                 USB_PIPEnTRE_TRCLR);
2491             USB200.PIPEDTRN             = cnt;
2492             RZA_IO_RegWrite_16(&USB200.PIPEDTRE,
2493                                 1,
2494                                 USB_PIPEnTRE_TRENB_SHIFT,
2495                                 USB_PIPEnTRE_TRENB);
2496         break;
2497
2498         case USB_FUNCTION_PIPEE:
2499             RZA_IO_RegWrite_16(&USB200.PIPEETRE,
2500                                 1,
2501                                 USB_PIPEnTRE_TRCLR_SHIFT,
2502                                 USB_PIPEnTRE_TRCLR);
2503             USB200.PIPEETRN             = cnt;
2504             RZA_IO_RegWrite_16(&USB200.PIPEETRE,
2505                                 1,
2506                                 USB_PIPEnTRE_TRENB_SHIFT,
2507                                 USB_PIPEnTRE_TRENB);
2508         break;
2509
2510         case USB_FUNCTION_PIPEF:
2511             RZA_IO_RegWrite_16(&USB200.PIPEFTRE,
2512                                 1,
2513                                 USB_PIPEnTRE_TRCLR_SHIFT,
2514                                 USB_PIPEnTRE_TRCLR);
2515             USB200.PIPEFTRN             = cnt;
2516             RZA_IO_RegWrite_16(&USB200.PIPEFTRE,
2517                                 1,
2518                                 USB_PIPEnTRE_TRENB_SHIFT,
2519                                 USB_PIPEnTRE_TRENB);
2520         break;
2521
2522         default:
2523         break;
2524     }
2525 }
2526
2527 /*******************************************************************************
2528 * Function Name: usb0_function_clear_transaction_counter
2529 * Description  : Clears the transaction counter by the argument.
2530 *              : After executing this function, the transaction counter is invalid.
2531 * Arguments    : uint16_t pipe     ; Pipe number
2532 * Return Value : none
2533 *******************************************************************************/
2534 void usb0_function_clear_transaction_counter (uint16_t pipe)
2535 {
2536     switch (pipe)
2537     {
2538         case USB_FUNCTION_PIPE1:
2539             RZA_IO_RegWrite_16(&USB200.PIPE1TRE,
2540                                 0,
2541                                 USB_PIPEnTRE_TRENB_SHIFT,
2542                                 USB_PIPEnTRE_TRENB);
2543             RZA_IO_RegWrite_16(&USB200.PIPE1TRE,
2544                                 1,
2545                                 USB_PIPEnTRE_TRCLR_SHIFT,
2546                                 USB_PIPEnTRE_TRCLR);
2547         break;
2548
2549         case USB_FUNCTION_PIPE2:
2550             RZA_IO_RegWrite_16(&USB200.PIPE2TRE,
2551                                 0,
2552                                 USB_PIPEnTRE_TRENB_SHIFT,
2553                                 USB_PIPEnTRE_TRENB);
2554             RZA_IO_RegWrite_16(&USB200.PIPE2TRE,
2555                                 1,
2556                                 USB_PIPEnTRE_TRCLR_SHIFT,
2557                                 USB_PIPEnTRE_TRCLR);
2558         break;
2559
2560         case USB_FUNCTION_PIPE3:
2561             RZA_IO_RegWrite_16(&USB200.PIPE3TRE,
2562                                 0,
2563                                 USB_PIPEnTRE_TRENB_SHIFT,
2564                                 USB_PIPEnTRE_TRENB);
2565             RZA_IO_RegWrite_16(&USB200.PIPE3TRE,
2566                                 1,
2567                                 USB_PIPEnTRE_TRCLR_SHIFT,
2568                                 USB_PIPEnTRE_TRCLR);
2569         break;
2570
2571         case USB_FUNCTION_PIPE4:
2572             RZA_IO_RegWrite_16(&USB200.PIPE4TRE,
2573                                 0,
2574                                 USB_PIPEnTRE_TRENB_SHIFT,
2575                                 USB_PIPEnTRE_TRENB);
2576             RZA_IO_RegWrite_16(&USB200.PIPE4TRE,
2577                                 1,
2578                                 USB_PIPEnTRE_TRCLR_SHIFT,
2579                                 USB_PIPEnTRE_TRCLR);
2580         break;
2581
2582         case USB_FUNCTION_PIPE5:
2583             RZA_IO_RegWrite_16(&USB200.PIPE5TRE,
2584                                 0,
2585                                 USB_PIPEnTRE_TRENB_SHIFT,
2586                                 USB_PIPEnTRE_TRENB);
2587             RZA_IO_RegWrite_16(&USB200.PIPE5TRE,
2588                                 1,
2589                                 USB_PIPEnTRE_TRCLR_SHIFT,
2590                                 USB_PIPEnTRE_TRCLR);
2591         break;
2592
2593         case USB_FUNCTION_PIPE9:
2594             RZA_IO_RegWrite_16(&USB200.PIPE9TRE,
2595                                 0,
2596                                 USB_PIPEnTRE_TRENB_SHIFT,
2597                                 USB_PIPEnTRE_TRENB);
2598             RZA_IO_RegWrite_16(&USB200.PIPE9TRE,
2599                                 1,
2600                                 USB_PIPEnTRE_TRCLR_SHIFT,
2601                                 USB_PIPEnTRE_TRCLR);
2602         break;
2603
2604         case USB_FUNCTION_PIPEA:
2605             RZA_IO_RegWrite_16(&USB200.PIPEATRE,
2606                                 0,
2607                                 USB_PIPEnTRE_TRENB_SHIFT,
2608                                 USB_PIPEnTRE_TRENB);
2609             RZA_IO_RegWrite_16(&USB200.PIPEATRE,
2610                                 1,
2611                                 USB_PIPEnTRE_TRCLR_SHIFT,
2612                                 USB_PIPEnTRE_TRCLR);
2613         break;
2614
2615         case USB_FUNCTION_PIPEB:
2616             RZA_IO_RegWrite_16(&USB200.PIPEBTRE,
2617                                 0,
2618                                 USB_PIPEnTRE_TRENB_SHIFT,
2619                                 USB_PIPEnTRE_TRENB);
2620             RZA_IO_RegWrite_16(&USB200.PIPEBTRE,
2621                                 1,
2622                                 USB_PIPEnTRE_TRCLR_SHIFT,
2623                                 USB_PIPEnTRE_TRCLR);
2624         break;
2625
2626         case USB_FUNCTION_PIPEC:
2627             RZA_IO_RegWrite_16(&USB200.PIPECTRE,
2628                                 0,
2629                                 USB_PIPEnTRE_TRENB_SHIFT,
2630                                 USB_PIPEnTRE_TRENB);
2631             RZA_IO_RegWrite_16(&USB200.PIPECTRE,
2632                                 1,
2633                                 USB_PIPEnTRE_TRCLR_SHIFT,
2634                                 USB_PIPEnTRE_TRCLR);
2635         break;
2636
2637         case USB_FUNCTION_PIPED:
2638             RZA_IO_RegWrite_16(&USB200.PIPEDTRE,
2639                                 0,
2640                                 USB_PIPEnTRE_TRENB_SHIFT,
2641                                 USB_PIPEnTRE_TRENB);
2642             RZA_IO_RegWrite_16(&USB200.PIPEDTRE,
2643                                 1,
2644                                 USB_PIPEnTRE_TRCLR_SHIFT,
2645                                 USB_PIPEnTRE_TRCLR);
2646         break;
2647
2648         case USB_FUNCTION_PIPEE:
2649             RZA_IO_RegWrite_16(&USB200.PIPEETRE,
2650                                 0,
2651                                 USB_PIPEnTRE_TRENB_SHIFT,
2652                                 USB_PIPEnTRE_TRENB);
2653             RZA_IO_RegWrite_16(&USB200.PIPEETRE,
2654                                 1,
2655                                 USB_PIPEnTRE_TRCLR_SHIFT,
2656                                 USB_PIPEnTRE_TRCLR);
2657         break;
2658
2659         case USB_FUNCTION_PIPEF:
2660             RZA_IO_RegWrite_16(&USB200.PIPEFTRE,
2661                                 0,
2662                                 USB_PIPEnTRE_TRENB_SHIFT,
2663                                 USB_PIPEnTRE_TRENB);
2664             RZA_IO_RegWrite_16(&USB200.PIPEFTRE,
2665                                 1,
2666                                 USB_PIPEnTRE_TRCLR_SHIFT,
2667                                 USB_PIPEnTRE_TRCLR);
2668         break;
2669
2670         default:
2671         break;
2672     }
2673 }
2674
2675 /*******************************************************************************
2676 * Function Name: usb0_function_stop_transfer
2677 * Description  : Stops the USB transfer in the pipe specified by the argument.
2678 *              : After stopping the USB transfer, clears the buffer allocated in
2679 *              : the pipe.
2680 *              : After executing this function, allocation in FIF0 becomes USB_FUNCTION_PIPE0;
2681 *              : invalid. After executing this function, BRDY/NRDY/BEMP interrupt
2682 *              : in the corresponding pipe becomes invalid. Sequence bit is also
2683 *              : cleared.
2684 * Arguments    : uint16_t  pipe     ; Pipe Number
2685 * Return Value : none
2686 *******************************************************************************/
2687 void usb0_function_stop_transfer (uint16_t pipe)
2688 {
2689     uint16_t usefifo;
2690     uint32_t remain;
2691     uint16_t fifo;
2692
2693     usb0_function_set_pid_nak(pipe);
2694
2695     usefifo = (uint16_t)(g_usb0_function_PipeTbl[pipe] & USB_FUNCTION_FIFO_USE);
2696     switch (usefifo)
2697     {
2698         case USB_FUNCTION_D0FIFO_USE:
2699             usb0_function_clear_transaction_counter(pipe);
2700             USB200.D0FIFOCTR = USB_FUNCTION_BITBCLR;        /* Buffer Clear */
2701             fifo = USB_FUNCTION_D0USE;
2702         break;
2703
2704         case USB_FUNCTION_D1FIFO_USE:
2705             usb0_function_clear_transaction_counter(pipe);
2706             USB200.D1FIFOCTR = USB_FUNCTION_BITBCLR;        /* Buffer Clear */
2707             fifo = USB_FUNCTION_D1USE;
2708         break;
2709
2710         case USB_FUNCTION_D0FIFO_DMA:
2711             remain = Userdef_USB_usb0_function_stop_dma0();
2712             usb0_function_dma_stop_d0(pipe, remain);
2713             usb0_function_clear_transaction_counter(pipe);
2714             USB200.D0FIFOCTR = USB_FUNCTION_BITBCLR;        /* Buffer Clear */
2715             fifo = USB_FUNCTION_D0DMA;
2716         break;
2717
2718         case USB_FUNCTION_D1FIFO_DMA:
2719             remain = Userdef_USB_usb0_function_stop_dma1();
2720             usb0_function_dma_stop_d1(pipe, remain);
2721             usb0_function_clear_transaction_counter(pipe);
2722             USB200.D1FIFOCTR = USB_FUNCTION_BITBCLR;        /* Buffer Clear */
2723             fifo = USB_FUNCTION_D1DMA;
2724         break;
2725
2726         default:
2727             usb0_function_clear_transaction_counter(pipe);
2728             USB200.CFIFOCTR =  USB_FUNCTION_BITBCLR;        /* Buffer Clear */
2729             fifo = USB_FUNCTION_CUSE;
2730         break;
2731     }
2732
2733     usb0_function_set_curpipe(USB_FUNCTION_PIPE0, fifo, DEVDRV_USBF_NO, USB_FUNCTION_BITMBW_16);
2734
2735     /* Interrupt of pipe set is disabled */
2736     usb0_function_disable_brdy_int(pipe);
2737     usb0_function_disable_nrdy_int(pipe);
2738     usb0_function_disable_bemp_int(pipe);
2739
2740     usb0_function_aclrm(pipe);
2741     usb0_function_set_csclr(pipe);
2742
2743     if ( g_usb0_function_pipe_status[pipe] == DEVDRV_USBF_PIPE_WAIT )
2744     {
2745         g_usb0_function_pipe_status[pipe] = DEVDRV_USBF_PIPE_NORES;
2746     }
2747
2748 }
2749
2750 /*******************************************************************************
2751 * Function Name: usb0_function_set_dfacc_d0
2752 * Description  : Sets the DFACC setting value in D0FIFO using the transfer size.
2753 * Arguments    : uint16_t mbw     ; MBW
2754 *              : uint16_t count   ; data count
2755 * Return Value : DFACC Access mode
2756 *******************************************************************************/
2757 static uint16_t usb0_function_set_dfacc_d0 (uint16_t mbw, uint32_t count)
2758 {
2759     uint16_t dfacc = 0;
2760
2761 #ifndef __USB_FUNCTION_DF_ACC_ENABLE__
2762     RZA_IO_RegWrite_16(&USB200.D0FBCFG,
2763                         0,
2764                         USB_DnFBCFG_DFACC_SHIFT,
2765                         USB_DnFBCFG_DFACC);
2766     RZA_IO_RegWrite_16(&USB200.D0FBCFG,
2767                         0,
2768                         USB_DnFBCFG_TENDE_SHIFT,
2769                         USB_DnFBCFG_TENDE);
2770     dfacc = 0;
2771 #else
2772     if (mbw == USB_FUNCTION_BITMBW_32)
2773     {
2774         if ((count % 32) == 0)
2775         {
2776             /* 32byte transfer */
2777             RZA_IO_RegWrite_16(&USB200.D0FBCFG,
2778                                 2,
2779                                 USB_DnFBCFG_DFACC_SHIFT,
2780                                 USB_DnFBCFG_DFACC);
2781             RZA_IO_RegWrite_16(&USB200.D0FBCFG,
2782                                 0,
2783                                 USB_DnFBCFG_TENDE_SHIFT,
2784                                 USB_DnFBCFG_TENDE);
2785             dfacc = 2;
2786         }
2787         else if ((count % 16) == 0)
2788         {
2789             /* 16byte transfer */
2790             RZA_IO_RegWrite_16(&USB200.D0FBCFG,
2791                                 1,
2792                                 USB_DnFBCFG_DFACC_SHIFT,
2793                                 USB_DnFBCFG_DFACC);
2794             RZA_IO_RegWrite_16(&USB200.D0FBCFG,
2795                                 0,
2796                                 USB_DnFBCFG_TENDE_SHIFT,
2797                                 USB_DnFBCFG_TENDE);
2798             dfacc = 1;
2799         }
2800         else
2801         {
2802             RZA_IO_RegWrite_16(&USB200.D0FBCFG,
2803                                 0,
2804                                 USB_DnFBCFG_DFACC_SHIFT,
2805                                 USB_DnFBCFG_DFACC);
2806             RZA_IO_RegWrite_16(&USB200.D0FBCFG,
2807                                 0,
2808                                 USB_DnFBCFG_TENDE_SHIFT,
2809                                 USB_DnFBCFG_TENDE);
2810             dfacc = 0;
2811         }
2812     }
2813     else if (mbw == USB_FUNCTION_BITMBW_16)
2814     {
2815         RZA_IO_RegWrite_16(&USB200.D0FBCFG,
2816                             0,
2817                             USB_DnFBCFG_DFACC_SHIFT,
2818                             USB_DnFBCFG_DFACC);
2819         RZA_IO_RegWrite_16(&USB200.D0FBCFG,
2820                             0,
2821                             USB_DnFBCFG_TENDE_SHIFT,
2822                             USB_DnFBCFG_TENDE);
2823         dfacc = 0;
2824     }
2825     else
2826     {
2827         RZA_IO_RegWrite_16(&USB200.D0FBCFG,
2828                             0,
2829                             USB_DnFBCFG_DFACC_SHIFT,
2830                             USB_DnFBCFG_DFACC);
2831         RZA_IO_RegWrite_16(&USB200.D0FBCFG,
2832                             0,
2833                             USB_DnFBCFG_TENDE_SHIFT,
2834                             USB_DnFBCFG_TENDE);
2835         dfacc = 0;
2836     }
2837 #endif
2838     return dfacc;
2839 }
2840
2841 /*******************************************************************************
2842 * Function Name: usb0_function_set_dfacc_d1
2843 * Description  : Set the DFACC setting value in D1FIFO using the transfer size.
2844 * Arguments    : uint16_t mbw     ; MBW
2845 *              : uint16_t count   ; data count
2846 * Return Value : DFACC Access mode
2847 *******************************************************************************/
2848 static uint16_t usb0_function_set_dfacc_d1 (uint16_t mbw, uint32_t count)
2849 {
2850     uint16_t dfacc = 0;
2851
2852 #ifndef __USB_FUNCTION_DF_ACC_ENABLE__
2853     RZA_IO_RegWrite_16(&USB200.D1FBCFG,
2854                         0,
2855                         USB_DnFBCFG_DFACC_SHIFT,
2856                         USB_DnFBCFG_DFACC);
2857     RZA_IO_RegWrite_16(&USB200.D1FBCFG,
2858                         0,
2859                         USB_DnFBCFG_TENDE_SHIFT,
2860                         USB_DnFBCFG_TENDE);
2861     dfacc = 0;
2862 #else
2863     if (mbw == USB_FUNCTION_BITMBW_32)
2864     {
2865         if ((count % 32) == 0)
2866         {
2867             /* 32byte transfer */
2868             RZA_IO_RegWrite_16(&USB200.D1FBCFG,
2869                                 2,
2870                                 USB_DnFBCFG_DFACC_SHIFT,
2871                                 USB_DnFBCFG_DFACC);
2872             RZA_IO_RegWrite_16(&USB200.D1FBCFG,
2873                                 0,
2874                                 USB_DnFBCFG_TENDE_SHIFT,
2875                                 USB_DnFBCFG_TENDE);
2876             dfacc = 2;
2877         }
2878         else if ((count % 16) == 0)
2879         {
2880             /* 16byte transfer */
2881             RZA_IO_RegWrite_16(&USB200.D1FBCFG,
2882                                 1,
2883                                 USB_DnFBCFG_DFACC_SHIFT,
2884                                 USB_DnFBCFG_DFACC);
2885             RZA_IO_RegWrite_16(&USB200.D1FBCFG,
2886                                 0,
2887                                 USB_DnFBCFG_TENDE_SHIFT,
2888                                 USB_DnFBCFG_TENDE);
2889             dfacc = 1;
2890         }
2891         else
2892         {
2893             RZA_IO_RegWrite_16(&USB200.D1FBCFG,
2894                                 0,
2895                                 USB_DnFBCFG_DFACC_SHIFT,
2896                                 USB_DnFBCFG_DFACC);
2897             RZA_IO_RegWrite_16(&USB200.D1FBCFG,
2898                                 0,
2899                                 USB_DnFBCFG_TENDE_SHIFT,
2900                                 USB_DnFBCFG_TENDE);
2901             dfacc = 0;
2902         }
2903     }
2904     else if (mbw == USB_FUNCTION_BITMBW_16)
2905     {
2906         RZA_IO_RegWrite_16(&USB200.D1FBCFG,
2907                             0,
2908                             USB_DnFBCFG_DFACC_SHIFT,
2909                             USB_DnFBCFG_DFACC);
2910         RZA_IO_RegWrite_16(&USB200.D1FBCFG,
2911                             0,
2912                             USB_DnFBCFG_TENDE_SHIFT,
2913                             USB_DnFBCFG_TENDE);
2914         dfacc = 0;
2915     }
2916     else
2917     {
2918         RZA_IO_RegWrite_16(&USB200.D1FBCFG,
2919                             0,
2920                             USB_DnFBCFG_DFACC_SHIFT,
2921                             USB_DnFBCFG_DFACC);
2922         RZA_IO_RegWrite_16(&USB200.D1FBCFG,
2923                             0,
2924                             USB_DnFBCFG_TENDE_SHIFT,
2925                             USB_DnFBCFG_TENDE);
2926         dfacc = 0;
2927     }
2928 #endif
2929
2930     return dfacc;
2931 }
2932
2933 /* End of File */