1 /*******************************************************************************
3 * This software is supplied by Renesas Electronics Corporation and is only
4 * intended for use with Renesas products. No other uses are authorized. This
5 * software is owned by Renesas Electronics Corporation and is protected under
6 * all applicable laws, including copyright laws.
7 * THIS SOFTWARE IS PROVIDED "AS IS" AND RENESAS MAKES NO WARRANTIES REGARDING
8 * THIS SOFTWARE, WHETHER EXPRESS, IMPLIED OR STATUTORY, INCLUDING BUT NOT
9 * LIMITED TO WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE
10 * AND NON-INFRINGEMENT. ALL SUCH WARRANTIES ARE EXPRESSLY DISCLAIMED.
11 * TO THE MAXIMUM EXTENT PERMITTED NOT PROHIBITED BY LAW, NEITHER RENESAS
12 * ELECTRONICS CORPORATION NOR ANY OF ITS AFFILIATED COMPANIES SHALL BE LIABLE
13 * FOR ANY DIRECT, INDIRECT, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES FOR
14 * ANY REASON RELATED TO THIS SOFTWARE, EVEN IF RENESAS OR ITS AFFILIATES HAVE
15 * BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES.
16 * Renesas reserves the right, without notice, to make changes to this software
17 * and to discontinue the availability of this software. By using this software,
18 * you agree to the additional terms and conditions found by accessing the
20 * http://www.renesas.com/disclaimer
21 * Copyright (C) 2012 - 2014 Renesas Electronics Corporation. All rights reserved.
22 *******************************************************************************/
23 /*******************************************************************************
24 * File Name : usb1_function_dataio.c
26 * $Date:: 2014-07-09 16:29:19 +0900#$
31 * Description : RZ/A1H R7S72100 USB Sample Program
34 *******************************************************************************/
37 /*******************************************************************************
38 Includes <System Includes> , "Project Includes"
39 *******************************************************************************/
40 #include "usb1_function.h"
43 /*******************************************************************************
45 *******************************************************************************/
48 /*******************************************************************************
50 *******************************************************************************/
53 /*******************************************************************************
54 Imported global variables and functions (from other files)
55 *******************************************************************************/
58 /*******************************************************************************
59 Exported global variables and functions (to be accessed by other files)
60 *******************************************************************************/
63 /*******************************************************************************
64 Private global variables and functions
65 *******************************************************************************/
66 static uint16_t g_usb1_function_mbw
[(USB_FUNCTION_MAX_PIPE_NO
+ 1)];
68 static void usb1_function_start_receive_trns_c(uint16_t pipe
, uint32_t size
, uint8_t *data
);
69 static void usb1_function_start_receive_trns_d0(uint16_t pipe
, uint32_t size
, uint8_t *data
);
70 static void usb1_function_start_receive_trns_d1(uint16_t pipe
, uint32_t size
, uint8_t *data
);
71 static void usb1_function_start_receive_dma_d0(uint16_t pipe
, uint32_t size
, uint8_t *data
);
72 static void usb1_function_start_receive_dma_d1(uint16_t pipe
, uint32_t size
, uint8_t *data
);
73 static uint16_t usb1_function_read_dma_d0(uint16_t pipe
);
74 static uint16_t usb1_function_read_dma_d1(uint16_t pipe
);
75 static uint16_t usb1_function_write_dma_d0(uint16_t pipe
);
76 static uint16_t usb1_function_write_dma_d1(uint16_t pipe
);
78 static void usb1_function_read_c_fifo(uint16_t pipe
, uint16_t count
);
79 static void usb1_function_write_c_fifo(uint16_t Pipe
, uint16_t count
);
80 static void usb1_function_read_d0_fifo(uint16_t pipe
, uint16_t count
);
81 static void usb1_function_write_d0_fifo(uint16_t pipe
, uint16_t count
);
82 static void usb1_function_read_d1_fifo(uint16_t pipe
, uint16_t count
);
83 static void usb1_function_write_d1_fifo(uint16_t pipe
, uint16_t count
);
85 static void usb1_function_clear_transaction_counter(uint16_t pipe
);
86 static void usb1_function_set_transaction_counter(uint16_t pipe
, uint32_t count
);
88 static uint32_t usb1_function_com_get_dmasize(uint32_t trncount
, uint32_t dtptr
);
90 static uint16_t usb1_function_set_dfacc_d0(uint16_t mbw
, uint32_t count
);
91 static uint16_t usb1_function_set_dfacc_d1(uint16_t mbw
, uint32_t count
);
94 /*******************************************************************************
95 * Function Name: usb1_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 usb1_function_start_send_transfer (uint16_t pipe
, uint32_t size
, uint8_t * data
)
112 g_usb1_function_data_count
[pipe
] = size
;
113 g_usb1_function_data_pointer
[pipe
] = (uint8_t *)data
;
114 g_usb1_function_pipe_status
[pipe
] = DEVDRV_USBF_PIPE_WAIT
;
116 usb1_function_clear_bemp_sts(pipe
);
117 usb1_function_clear_brdy_sts(pipe
);
118 usb1_function_clear_nrdy_sts(pipe
);
120 mbw
= usb1_function_get_mbw(size
, (uint32_t)data
);
122 usefifo
= (uint16_t)(g_usb1_function_PipeTbl
[pipe
] & USB_FUNCTION_FIFO_USE
);
126 case USB_FUNCTION_D0FIFO_USE
:
127 case USB_FUNCTION_D0FIFO_DMA
:
128 usefifo
= USB_FUNCTION_D0USE
;
131 case USB_FUNCTION_D1FIFO_USE
:
132 case USB_FUNCTION_D1FIFO_DMA
:
133 usefifo
= USB_FUNCTION_D1USE
;
137 usefifo
= USB_FUNCTION_CUSE
;
141 usb1_function_set_curpipe(USB_FUNCTION_PIPE0
, usefifo
, DEVDRV_USBF_NO
, mbw
);
143 usb1_function_clear_transaction_counter(pipe
);
145 usb1_function_aclrm(pipe
);
147 status
= usb1_function_write_buffer(pipe
);
149 if (status
!= DEVDRV_USBF_FIFOERROR
)
151 usb1_function_set_pid_buf(pipe
);
157 /*******************************************************************************
158 * Function Name: usb1_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 usb1_function_write_buffer (uint16_t pipe
)
173 g_usb1_function_PipeIgnore
[pipe
] = 0;
174 usefifo
= (uint16_t)(g_usb1_function_PipeTbl
[pipe
] & USB_FUNCTION_FIFO_USE
);
178 case USB_FUNCTION_D0FIFO_USE
:
179 status
= usb1_function_write_buffer_d0(pipe
);
182 case USB_FUNCTION_D1FIFO_USE
:
183 status
= usb1_function_write_buffer_d1(pipe
);
186 case USB_FUNCTION_D0FIFO_DMA
:
187 status
= usb1_function_write_dma_d0(pipe
);
190 case USB_FUNCTION_D1FIFO_DMA
:
191 status
= usb1_function_write_dma_d1(pipe
);
195 status
= usb1_function_write_buffer_c(pipe
);
201 case DEVDRV_USBF_WRITING
: /* Continue of data write */
202 usb1_function_enable_nrdy_int(pipe
); /* Error (NORES or STALL) */
203 usb1_function_enable_brdy_int(pipe
); /* Enable Ready Interrupt */
206 case DEVDRV_USBF_WRITEEND
: /* End of data write */
207 case DEVDRV_USBF_WRITESHRT
: /* End of data write */
208 usb1_function_disable_brdy_int(pipe
); /* Disable Ready Interrupt */
209 usb1_function_clear_nrdy_sts(pipe
);
210 usb1_function_enable_nrdy_int(pipe
); /* Error (NORES or STALL) */
211 /* for last transfer */
212 usb1_function_enable_bemp_int(pipe
); /* Enable Empty Interrupt */
215 case DEVDRV_USBF_WRITEDMA
: /* DMA write */
216 usb1_function_clear_nrdy_sts(pipe
);
217 usb1_function_enable_nrdy_int(pipe
); /* Error (NORES or STALL) */
220 case DEVDRV_USBF_FIFOERROR
: /* FIFO access status */
222 usb1_function_disable_brdy_int(pipe
); /* Disable Ready Interrupt */
223 usb1_function_disable_bemp_int(pipe
); /* Disable Empty Interrupt */
224 g_usb1_function_pipe_status
[pipe
] = DEVDRV_USBF_FIFOERROR
;
228 return status
; /* End or Err or Continue */
231 /*******************************************************************************
232 * Function Name: usb1_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 usb1_function_write_buffer_c (uint16_t pipe
)
251 if (g_usb1_function_CtrZeroLengthFlag
== 1)
253 g_usb1_function_CtrZeroLengthFlag
= 0; /* Zero Length Packet Flag CLR */
254 return DEVDRV_USBF_WRITEEND
;
257 mbw
= usb1_function_get_mbw(g_usb1_function_data_count
[pipe
], (uint32_t)g_usb1_function_data_pointer
[pipe
]);
258 if (pipe
== USB_FUNCTION_PIPE0
)
260 buffer
= usb1_function_change_fifo_port(pipe
, USB_FUNCTION_CUSE
, USB_FUNCTION_CFIFO_WRITE
, mbw
);
264 buffer
= usb1_function_change_fifo_port(pipe
, USB_FUNCTION_CUSE
, DEVDRV_USBF_NO
, mbw
);
267 if (buffer
== DEVDRV_USBF_FIFOERROR
) /* FIFO access status */
269 return DEVDRV_USBF_FIFOERROR
;
272 size
= usb1_function_get_buf_size(pipe
); /* Data buffer size */
273 mxps
= usb1_function_get_mxps(pipe
); /* Max Packet Size */
275 if (g_usb1_function_data_count
[pipe
] <= (uint32_t)size
)
277 status
= DEVDRV_USBF_WRITEEND
; /* write continues */
278 count
= g_usb1_function_data_count
[pipe
];
282 status
= DEVDRV_USBF_WRITESHRT
; /* Null Packet is end of write */
285 if ((count
% mxps
) != 0)
287 status
= DEVDRV_USBF_WRITESHRT
; /* Short Packet is end of write */
292 status
= DEVDRV_USBF_WRITING
; /* write continues */
293 count
= (uint32_t)size
;
296 usb1_function_write_c_fifo(pipe
, (uint16_t)count
);
298 if (g_usb1_function_data_count
[pipe
] < (uint32_t)size
)
300 g_usb1_function_data_count
[pipe
] = 0;
302 if (RZA_IO_RegRead_16(&USB201
.CFIFOCTR
, USB_CFIFOCTR_BVAL_SHIFT
, USB_CFIFOCTR_BVAL
) == 0)
304 USB201
.CFIFOCTR
= USB_FUNCTION_BITBVAL
; /* Short Packet */
305 g_usb1_function_CtrZeroLengthFlag
= 1; /* Zero Length Packet Flag */
310 g_usb1_function_data_count
[pipe
] -= count
;
313 return status
; /* End or Err or Continue */
316 /*******************************************************************************
317 * Function Name: usb1_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 usb1_function_write_buffer_d0 (uint16_t pipe
)
336 mbw
= usb1_function_get_mbw(g_usb1_function_data_count
[pipe
], (uint32_t)g_usb1_function_data_pointer
[pipe
]);
337 buffer
= usb1_function_change_fifo_port(pipe
, USB_FUNCTION_D0USE
, DEVDRV_USBF_NO
, mbw
);
338 if (buffer
== DEVDRV_USBF_FIFOERROR
) /* FIFO access status */
340 return DEVDRV_USBF_FIFOERROR
;
343 size
= usb1_function_get_buf_size(pipe
); /* Data buffer size */
344 mxps
= usb1_function_get_mxps(pipe
); /* Max Packet Size */
346 if (g_usb1_function_data_count
[pipe
] <= (uint32_t)size
)
348 status
= DEVDRV_USBF_WRITEEND
; /* write continues */
349 count
= g_usb1_function_data_count
[pipe
];
353 status
= DEVDRV_USBF_WRITESHRT
; /* Null Packet is end of write */
356 if ((count
% mxps
) != 0)
358 status
= DEVDRV_USBF_WRITESHRT
; /* Short Packet is end of write */
363 status
= DEVDRV_USBF_WRITING
; /* write continues */
364 count
= (uint32_t)size
;
367 usb1_function_write_d0_fifo(pipe
, (uint16_t)count
);
369 if (g_usb1_function_data_count
[pipe
] < (uint32_t)size
)
371 g_usb1_function_data_count
[pipe
] = 0;
372 if (RZA_IO_RegRead_16(&USB201
.D0FIFOCTR
, USB_DnFIFOCTR_BVAL_SHIFT
, USB_DnFIFOCTR_BVAL
) == 0)
374 USB201
.D0FIFOCTR
= USB_FUNCTION_BITBVAL
; /* Short Packet */
379 g_usb1_function_data_count
[pipe
] -= count
;
382 return status
; /* End or Err or Continue */
385 /*******************************************************************************
386 * Function Name: usb1_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 usb1_function_write_buffer_d1 (uint16_t pipe
)
405 mbw
= usb1_function_get_mbw(g_usb1_function_data_count
[pipe
], (uint32_t)g_usb1_function_data_pointer
[pipe
]);
406 buffer
= usb1_function_change_fifo_port(pipe
, USB_FUNCTION_D1USE
, DEVDRV_USBF_NO
, mbw
);
408 if (buffer
== DEVDRV_USBF_FIFOERROR
) /* FIFO access status */
410 return DEVDRV_USBF_FIFOERROR
;
413 size
= usb1_function_get_buf_size(pipe
); /* Data buffer size */
414 mxps
= usb1_function_get_mxps(pipe
); /* Max Packet Size */
416 if (g_usb1_function_data_count
[pipe
] <= (uint32_t)size
)
418 status
= DEVDRV_USBF_WRITEEND
; /* write continues */
419 count
= g_usb1_function_data_count
[pipe
];
423 status
= DEVDRV_USBF_WRITESHRT
; /* Null Packet is end of write */
426 if ((count
% mxps
) != 0)
428 status
= DEVDRV_USBF_WRITESHRT
; /* Short Packet is end of write */
433 status
= DEVDRV_USBF_WRITING
; /* write continues */
434 count
= (uint32_t)size
;
437 usb1_function_write_d1_fifo(pipe
, (uint16_t)count
);
439 if (g_usb1_function_data_count
[pipe
] < (uint32_t)size
)
441 g_usb1_function_data_count
[pipe
] = 0;
443 if (RZA_IO_RegRead_16(&USB201
.D1FIFOCTR
, USB_DnFIFOCTR_BVAL_SHIFT
, USB_DnFIFOCTR_BVAL
) == 0)
445 USB201
.D1FIFOCTR
= USB_FUNCTION_BITBVAL
; /* Short Packet */
450 g_usb1_function_data_count
[pipe
] -= count
;
453 return status
; /* End or Err or Continue */
456 /*******************************************************************************
457 * Function Name: usb1_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_usb1_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 usb1_function_write_dma_d0 (uint16_t pipe
)
477 mbw
= usb1_function_get_mbw(g_usb1_function_data_count
[pipe
], (uint32_t)g_usb1_function_data_pointer
[pipe
]);
478 buffer
= usb1_function_change_fifo_port(pipe
, USB_FUNCTION_D0DMA
, DEVDRV_USBF_NO
, mbw
);
480 if (buffer
== DEVDRV_USBF_FIFOERROR
) /* FIFO access status */
482 return DEVDRV_USBF_FIFOERROR
;
485 size
= usb1_function_get_buf_size(pipe
); /* Data buffer size */
486 count
= g_usb1_function_data_count
[pipe
];
490 g_usb1_function_DmaPipe
[USB_FUNCTION_D0FIFO
] = pipe
;
492 if ((count
% size
) != 0)
494 g_usb1_function_DmaBval
[USB_FUNCTION_D0FIFO
] = 1;
498 g_usb1_function_DmaBval
[USB_FUNCTION_D0FIFO
] = 0;
501 dfacc
= usb1_function_set_dfacc_d0(mbw
, count
);
503 if (mbw
== USB_FUNCTION_BITMBW_32
)
505 g_usb1_function_DmaInfo
[USB_FUNCTION_D0FIFO
].size
= 2; /* 32bit transfer */
507 else if (mbw
== USB_FUNCTION_BITMBW_16
)
509 g_usb1_function_DmaInfo
[USB_FUNCTION_D0FIFO
].size
= 1; /* 16bit transfer */
513 g_usb1_function_DmaInfo
[USB_FUNCTION_D0FIFO
].size
= 0; /* 8bit transfer */
516 g_usb1_function_DmaInfo
[USB_FUNCTION_D0FIFO
].fifo
= USB_FUNCTION_D0FIFO_DMA
;
517 g_usb1_function_DmaInfo
[USB_FUNCTION_D0FIFO
].dir
= USB_FUNCTION_BUF2FIFO
;
518 g_usb1_function_DmaInfo
[USB_FUNCTION_D0FIFO
].buffer
= (uint32_t)g_usb1_function_data_pointer
[pipe
];
519 g_usb1_function_DmaInfo
[USB_FUNCTION_D0FIFO
].bytes
= count
;
521 Userdef_USB_usb1_function_start_dma(&g_usb1_function_DmaInfo
[USB_FUNCTION_D0FIFO
], dfacc
);
523 usb1_function_set_curpipe2(pipe
, USB_FUNCTION_D0DMA
, DEVDRV_USBF_NO
, mbw
, dfacc
);
525 RZA_IO_RegWrite_16(&USB201
.D0FIFOSEL
, 1, USB_DnFIFOSEL_DREQE_SHIFT
, USB_DnFIFOSEL_DREQE
);
527 g_usb1_function_data_count
[pipe
] = 0;
528 g_usb1_function_data_pointer
[pipe
] += count
;
529 status
= DEVDRV_USBF_WRITEDMA
; /* DMA write */
533 if (RZA_IO_RegRead_16(&USB201
.D0FIFOCTR
, USB_DnFIFOCTR_BVAL_SHIFT
, USB_DnFIFOCTR_BVAL
) == 0)
535 RZA_IO_RegWrite_16(&USB201
.D0FIFOCTR
, 1, USB_DnFIFOCTR_BVAL_SHIFT
, USB_DnFIFOCTR_BVAL
); /* Short Packet */
537 status
= DEVDRV_USBF_WRITESHRT
; /* Short Packet is end of write */
540 return status
; /* End or Err or Continue */
543 /*******************************************************************************
544 * Function Name: usb1_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_usb1_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 usb1_function_write_dma_d1 (uint16_t pipe
)
564 mbw
= usb1_function_get_mbw(g_usb1_function_data_count
[pipe
], (uint32_t)g_usb1_function_data_pointer
[pipe
]);
565 buffer
= usb1_function_change_fifo_port(pipe
, USB_FUNCTION_D1DMA
, DEVDRV_USBF_NO
, mbw
);
567 if (buffer
== DEVDRV_USBF_FIFOERROR
) /* FIFO access status */
569 return DEVDRV_USBF_FIFOERROR
;
572 size
= usb1_function_get_buf_size(pipe
); /* Data buffer size */
573 count
= g_usb1_function_data_count
[pipe
];
577 g_usb1_function_DmaPipe
[USB_FUNCTION_D1FIFO
] = pipe
;
578 if ((count
% size
) != 0)
580 g_usb1_function_DmaBval
[USB_FUNCTION_D1FIFO
] = 1;
584 g_usb1_function_DmaBval
[USB_FUNCTION_D1FIFO
] = 0;
587 dfacc
= usb1_function_set_dfacc_d1(mbw
, count
);
589 if (mbw
== USB_FUNCTION_BITMBW_32
)
591 g_usb1_function_DmaInfo
[USB_FUNCTION_D1FIFO
].size
= 2; /* 32bit transfer */
593 else if (mbw
== USB_FUNCTION_BITMBW_16
)
595 g_usb1_function_DmaInfo
[USB_FUNCTION_D1FIFO
].size
= 1; /* 16bit transfer */
599 g_usb1_function_DmaInfo
[USB_FUNCTION_D1FIFO
].size
= 0; /* 8bit transfer */
602 g_usb1_function_DmaInfo
[USB_FUNCTION_D1FIFO
].fifo
= USB_FUNCTION_D1FIFO_DMA
;
603 g_usb1_function_DmaInfo
[USB_FUNCTION_D1FIFO
].dir
= USB_FUNCTION_BUF2FIFO
;
604 g_usb1_function_DmaInfo
[USB_FUNCTION_D1FIFO
].buffer
= (uint32_t)g_usb1_function_data_pointer
[pipe
];
605 g_usb1_function_DmaInfo
[USB_FUNCTION_D1FIFO
].bytes
= count
;
607 Userdef_USB_usb1_function_start_dma(&g_usb1_function_DmaInfo
[USB_FUNCTION_D1FIFO
], dfacc
);
609 usb1_function_set_curpipe2(pipe
, USB_FUNCTION_D1DMA
, DEVDRV_USBF_NO
, mbw
, dfacc
);
611 RZA_IO_RegWrite_16(&USB201
.D1FIFOSEL
, 1, USB_DnFIFOSEL_DREQE_SHIFT
, USB_DnFIFOSEL_DREQE
);
613 g_usb1_function_data_count
[pipe
] = 0;
614 g_usb1_function_data_pointer
[pipe
] += count
;
616 status
= DEVDRV_USBF_WRITEDMA
; /* DMA write */
620 if (RZA_IO_RegRead_16(&USB201
.D1FIFOCTR
, USB_DnFIFOCTR_BVAL_SHIFT
, USB_DnFIFOCTR_BVAL
) == 0)
622 RZA_IO_RegWrite_16(&USB201
.D1FIFOCTR
, 1, USB_DnFIFOCTR_BVAL_SHIFT
, USB_DnFIFOCTR_BVAL
); /* Short Packet */
624 status
= DEVDRV_USBF_WRITESHRT
; /* Short Packet is end of write */
627 return status
; /* End or Err or Continue */
630 /*******************************************************************************
631 * Function Name: usb1_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 usb1_function_start_receive_transfer (uint16_t pipe
, uint32_t size
, uint8_t * data
)
643 usb1_function_clear_bemp_sts(pipe
);
644 usb1_function_clear_brdy_sts(pipe
);
645 usb1_function_clear_nrdy_sts(pipe
);
647 usefifo
= (uint16_t)(g_usb1_function_PipeTbl
[pipe
] & USB_FUNCTION_FIFO_USE
);
651 case USB_FUNCTION_D0FIFO_USE
:
652 usb1_function_start_receive_trns_d0(pipe
, size
, data
);
655 case USB_FUNCTION_D1FIFO_USE
:
656 usb1_function_start_receive_trns_d1(pipe
, size
, data
);
659 case USB_FUNCTION_D0FIFO_DMA
:
660 usb1_function_start_receive_dma_d0(pipe
, size
, data
);
663 case USB_FUNCTION_D1FIFO_DMA
:
664 usb1_function_start_receive_dma_d1(pipe
, size
, data
);
668 usb1_function_start_receive_trns_c(pipe
, size
, data
);
673 /*******************************************************************************
674 * Function Name: usb1_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 usb1_function_start_receive_trns_c (uint16_t pipe
, uint32_t size
, uint8_t * data
)
689 usb1_function_set_pid_nak(pipe
);
690 g_usb1_function_data_count
[pipe
] = size
;
691 g_usb1_function_data_pointer
[pipe
] = (uint8_t *)data
;
692 g_usb1_function_PipeIgnore
[pipe
] = 0;
694 g_usb1_function_PipeDataSize
[pipe
] = size
;
695 g_usb1_function_pipe_status
[pipe
] = DEVDRV_USBF_PIPE_WAIT
;
697 mbw
= usb1_function_get_mbw(size
, (uint32_t)data
);
698 usb1_function_set_curpipe(USB_FUNCTION_PIPE0
, USB_FUNCTION_CUSE
, USB_FUNCTION_CFIFO_READ
, mbw
);
699 USB201
.CFIFOCTR
= USB_FUNCTION_BITBCLR
;
701 usb1_function_set_transaction_counter(pipe
, size
);
703 usb1_function_aclrm(pipe
);
705 usb1_function_enable_nrdy_int(pipe
);
706 usb1_function_enable_brdy_int(pipe
);
708 usb1_function_set_pid_buf(pipe
);
711 /*******************************************************************************
712 * Function Name: usb1_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
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 usb1_function_start_receive_trns_d0 (uint16_t pipe
, uint32_t size
, uint8_t * data
)
728 usb1_function_set_pid_nak(pipe
);
729 g_usb1_function_data_count
[pipe
] = size
;
730 g_usb1_function_data_pointer
[pipe
] = (uint8_t *)data
;
731 g_usb1_function_PipeIgnore
[pipe
] = 0;
733 g_usb1_function_PipeDataSize
[pipe
] = size
;
734 g_usb1_function_pipe_status
[pipe
] = DEVDRV_USBF_PIPE_WAIT
;
736 mbw
= usb1_function_get_mbw(size
, (uint32_t)data
);
737 usb1_function_set_curpipe(USB_FUNCTION_PIPE0
, USB_FUNCTION_D0USE
, DEVDRV_USBF_NO
, mbw
);
739 usb1_function_set_transaction_counter(pipe
, size
);
741 usb1_function_aclrm(pipe
);
743 usb1_function_enable_nrdy_int(pipe
);
744 usb1_function_enable_brdy_int(pipe
);
746 usb1_function_set_pid_buf(pipe
);
749 /*******************************************************************************
750 * Function Name: usb1_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 usb1_function_start_receive_trns_d1 (uint16_t pipe
, uint32_t size
, uint8_t * data
)
765 usb1_function_set_pid_nak(pipe
);
766 g_usb1_function_data_count
[pipe
] = size
;
767 g_usb1_function_data_pointer
[pipe
] = (uint8_t *)data
;
768 g_usb1_function_PipeIgnore
[pipe
] = 0;
770 g_usb1_function_PipeDataSize
[pipe
] = size
;
771 g_usb1_function_pipe_status
[pipe
] = DEVDRV_USBF_PIPE_WAIT
;
773 mbw
= usb1_function_get_mbw(size
, (uint32_t)data
);
774 usb1_function_set_curpipe(USB_FUNCTION_PIPE0
, USB_FUNCTION_D1USE
, DEVDRV_USBF_NO
, mbw
);
776 usb1_function_set_transaction_counter(pipe
, size
);
778 usb1_function_aclrm(pipe
);
780 usb1_function_enable_nrdy_int(pipe
);
781 usb1_function_enable_brdy_int(pipe
);
783 usb1_function_set_pid_buf(pipe
);
786 /*******************************************************************************
787 * Function Name: usb1_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 usb1_function_start_receive_dma_d0 (uint16_t pipe
, uint32_t size
, uint8_t * data
)
803 usb1_function_set_pid_nak(pipe
);
804 g_usb1_function_data_count
[pipe
] = size
;
805 g_usb1_function_data_pointer
[pipe
] = (uint8_t *)data
;
806 g_usb1_function_PipeIgnore
[pipe
] = 0;
808 g_usb1_function_PipeDataSize
[pipe
] = 0;
809 g_usb1_function_pipe_status
[pipe
] = DEVDRV_USBF_PIPE_WAIT
;
811 mbw
= usb1_function_get_mbw(size
, (uint32_t)data
);
812 usb1_function_set_curpipe(USB_FUNCTION_PIPE0
, USB_FUNCTION_D0USE
, DEVDRV_USBF_NO
, mbw
);
814 usb1_function_set_transaction_counter(pipe
, size
);
816 usb1_function_aclrm(pipe
);
818 if (RZA_IO_RegRead_16(&g_usb1_function_pipecfg
[pipe
], USB_PIPECFG_BFRE_SHIFT
, USB_PIPECFG_BFRE
) == 1)
820 usb1_function_read_dma(pipe
);
822 usb1_function_enable_nrdy_int(pipe
);
823 usb1_function_enable_brdy_int(pipe
);
827 usb1_function_enable_nrdy_int(pipe
);
828 usb1_function_enable_brdy_int(pipe
);
831 usb1_function_set_pid_buf(pipe
);
834 /*******************************************************************************
835 * Function Name: usb1_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 usb1_function_start_receive_dma_d1 (uint16_t pipe
, uint32_t size
, uint8_t * data
)
851 usb1_function_set_pid_nak(pipe
);
852 g_usb1_function_data_count
[pipe
] = size
;
853 g_usb1_function_data_pointer
[pipe
] = (uint8_t *)data
;
854 g_usb1_function_PipeIgnore
[pipe
] = 0;
856 g_usb1_function_PipeDataSize
[pipe
] = 0;
857 g_usb1_function_pipe_status
[pipe
] = DEVDRV_USBF_PIPE_WAIT
;
859 mbw
= usb1_function_get_mbw(size
, (uint32_t)data
);
860 usb1_function_set_curpipe(USB_FUNCTION_PIPE0
, USB_FUNCTION_D1USE
, DEVDRV_USBF_NO
, mbw
);
862 usb1_function_set_transaction_counter(pipe
, size
);
864 usb1_function_aclrm(pipe
);
866 if (RZA_IO_RegRead_16(&g_usb1_function_pipecfg
[pipe
], USB_PIPECFG_BFRE_SHIFT
, USB_PIPECFG_BFRE
) == 1)
868 usb1_function_read_dma(pipe
);
870 usb1_function_enable_nrdy_int(pipe
);
871 usb1_function_enable_brdy_int(pipe
);
875 usb1_function_enable_nrdy_int(pipe
);
876 usb1_function_enable_brdy_int(pipe
);
879 usb1_function_set_pid_buf(pipe
);
882 /*******************************************************************************
883 * Function Name: usb1_function_read_buffer
884 * Description : Reads data from the buffer allocated in the pipe specified
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 usb1_function_read_buffer (uint16_t pipe
)
898 g_usb1_function_PipeIgnore
[pipe
] = 0;
900 if ((g_usb1_function_PipeTbl
[pipe
] & USB_FUNCTION_FIFO_USE
) == USB_FUNCTION_D0FIFO_USE
)
902 status
= usb1_function_read_buffer_d0(pipe
);
904 else if ((g_usb1_function_PipeTbl
[pipe
] & USB_FUNCTION_FIFO_USE
) == USB_FUNCTION_D1FIFO_USE
)
906 status
= usb1_function_read_buffer_d1(pipe
);
910 status
= usb1_function_read_buffer_c(pipe
);
915 case USB_FUNCTION_READING
: /* Continue of data read */
918 case USB_FUNCTION_READEND
: /* End of data read */
919 case USB_FUNCTION_READSHRT
: /* End of data read */
920 usb1_function_disable_brdy_int(pipe
);
921 g_usb1_function_PipeDataSize
[pipe
] -= g_usb1_function_data_count
[pipe
];
922 g_usb1_function_pipe_status
[pipe
] = DEVDRV_USBF_PIPE_DONE
;
925 case USB_FUNCTION_READOVER
: /* buffer over */
926 if ((g_usb1_function_PipeTbl
[pipe
] & USB_FUNCTION_FIFO_USE
) == USB_FUNCTION_D0FIFO_USE
)
928 USB201
.D0FIFOCTR
= USB_FUNCTION_BITBCLR
; /* Clear BCLR */
930 else if ((g_usb1_function_PipeTbl
[pipe
] & USB_FUNCTION_FIFO_USE
) == USB_FUNCTION_D1FIFO_USE
)
932 USB201
.D1FIFOCTR
= USB_FUNCTION_BITBCLR
; /* Clear BCLR */
936 USB201
.CFIFOCTR
= USB_FUNCTION_BITBCLR
; /* Clear BCLR */
938 usb1_function_disable_brdy_int(pipe
); /* Disable Ready Interrupt */
939 g_usb1_function_PipeDataSize
[pipe
] -= g_usb1_function_data_count
[pipe
];
940 g_usb1_function_pipe_status
[pipe
] = DEVDRV_USBF_FIFOERROR
;
943 case DEVDRV_USBF_FIFOERROR
: /* FIFO access status */
945 usb1_function_disable_brdy_int(pipe
); /* Disable Ready Interrupt */
946 g_usb1_function_pipe_status
[pipe
] = DEVDRV_USBF_FIFOERROR
;
950 return status
; /* End or Err or Continue */
953 /*******************************************************************************
954 * Function Name: usb1_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 usb1_function_read_buffer_c (uint16_t pipe
)
973 mbw
= usb1_function_get_mbw(g_usb1_function_data_count
[pipe
], (uint32_t)g_usb1_function_data_pointer
[pipe
]);
974 buffer
= usb1_function_change_fifo_port(pipe
, USB_FUNCTION_CUSE
, DEVDRV_USBF_NO
, mbw
);
976 if (buffer
== DEVDRV_USBF_FIFOERROR
) /* FIFO access status */
978 return DEVDRV_USBF_FIFOERROR
;
981 dtln
= (uint32_t)(buffer
& USB_FUNCTION_BITDTLN
);
982 mxps
= usb1_function_get_mxps(pipe
); /* Max Packet Size */
984 if (g_usb1_function_data_count
[pipe
] < dtln
) /* Buffer Over ? */
986 status
= USB_FUNCTION_READOVER
;
987 usb1_function_set_pid_nak(pipe
); /* Set NAK */
988 count
= g_usb1_function_data_count
[pipe
];
990 else if (g_usb1_function_data_count
[pipe
] == dtln
) /* just Receive Size */
992 status
= USB_FUNCTION_READEND
;
993 usb1_function_set_pid_nak(pipe
); /* Set NAK */
998 status
= USB_FUNCTION_READSHRT
; /* Null Packet receive */
1001 if ((count
% mxps
) != 0)
1003 status
= USB_FUNCTION_READSHRT
; /* Short Packet receive */
1006 else /* continue Receive data */
1008 status
= USB_FUNCTION_READING
;
1013 status
= USB_FUNCTION_READSHRT
; /* Null Packet receive */
1014 usb1_function_set_pid_nak(pipe
); /* Set NAK */
1017 if ((count
% mxps
) != 0)
1019 status
= USB_FUNCTION_READSHRT
; /* Short Packet receive */
1020 usb1_function_set_pid_nak(pipe
); /* Set NAK */
1024 if (count
== 0) /* 0 length packet */
1026 USB201
.CFIFOCTR
= USB_FUNCTION_BITBCLR
; /* Clear BCLR */
1030 usb1_function_read_c_fifo(pipe
, (uint16_t)count
);
1033 g_usb1_function_data_count
[pipe
] -= count
;
1035 return status
; /* End or Err or Continue */
1038 /*******************************************************************************
1039 * Function Name: usb1_function_read_buffer_d0
1040 * Description : Reads data from the buffer allocated in the pipe specified in
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 usb1_function_read_buffer_d0 (uint16_t pipe
)
1058 uint16_t pipebuf_size
;
1060 mbw
= usb1_function_get_mbw(g_usb1_function_data_count
[pipe
], (uint32_t)g_usb1_function_data_pointer
[pipe
]);
1061 buffer
= usb1_function_change_fifo_port(pipe
, USB_FUNCTION_D0USE
, DEVDRV_USBF_NO
, mbw
);
1063 if (buffer
== DEVDRV_USBF_FIFOERROR
) /* FIFO access status */
1065 return DEVDRV_USBF_FIFOERROR
;
1068 dtln
= (uint32_t)(buffer
& USB_FUNCTION_BITDTLN
);
1069 mxps
= usb1_function_get_mxps(pipe
); /* Max Packet Size */
1071 if (g_usb1_function_data_count
[pipe
] < dtln
) /* Buffer Over ? */
1073 status
= USB_FUNCTION_READOVER
;
1074 usb1_function_set_pid_nak(pipe
); /* Set NAK */
1075 count
= g_usb1_function_data_count
[pipe
];
1077 else if (g_usb1_function_data_count
[pipe
] == dtln
) /* just Receive Size */
1079 status
= USB_FUNCTION_READEND
;
1080 usb1_function_set_pid_nak(pipe
); /* Set NAK */
1085 status
= USB_FUNCTION_READSHRT
; /* Null Packet receive */
1088 if ((count
% mxps
) != 0)
1090 status
= USB_FUNCTION_READSHRT
; /* Short Packet receive */
1093 else /* continue Receive data */
1095 status
= USB_FUNCTION_READING
;
1100 status
= USB_FUNCTION_READSHRT
; /* Null Packet receive */
1101 usb1_function_set_pid_nak(pipe
); /* Set NAK */
1104 if ((count
% mxps
) != 0)
1106 status
= USB_FUNCTION_READSHRT
; /* Short Packet receive */
1107 usb1_function_set_pid_nak(pipe
); /* Set NAK */
1111 pipebuf_size
= usb1_function_get_buf_size(pipe
); /* Data buffer size */
1113 if (count
!= pipebuf_size
)
1115 status
= USB_FUNCTION_READSHRT
; /* Short Packet receive */
1116 usb1_function_set_pid_nak(pipe
); /* Set NAK */
1121 if (count
== 0) /* 0 length packet */
1123 USB201
.D0FIFOCTR
= USB_FUNCTION_BITBCLR
; /* Clear BCLR */
1127 usb1_function_read_d0_fifo(pipe
, (uint16_t)count
);
1130 g_usb1_function_data_count
[pipe
] -= count
;
1132 return status
; /* End or Err or Continue */
1135 /*******************************************************************************
1136 * Function Name: usb1_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 usb1_function_read_buffer_d1 (uint16_t pipe
)
1155 uint16_t pipebuf_size
;
1157 mbw
= usb1_function_get_mbw(g_usb1_function_data_count
[pipe
], (uint32_t)g_usb1_function_data_pointer
[pipe
]);
1158 buffer
= usb1_function_change_fifo_port(pipe
, USB_FUNCTION_D1USE
, DEVDRV_USBF_NO
, mbw
);
1160 if (buffer
== DEVDRV_USBF_FIFOERROR
) /* FIFO access status */
1162 return DEVDRV_USBF_FIFOERROR
;
1165 dtln
= (uint32_t)(buffer
& USB_FUNCTION_BITDTLN
);
1166 mxps
= usb1_function_get_mxps(pipe
); /* Max Packet Size */
1168 if (g_usb1_function_data_count
[pipe
] < dtln
) /* Buffer Over ? */
1170 status
= USB_FUNCTION_READOVER
;
1171 usb1_function_set_pid_nak(pipe
); /* Set NAK */
1172 count
= g_usb1_function_data_count
[pipe
];
1174 else if (g_usb1_function_data_count
[pipe
] == dtln
) /* just Receive Size */
1176 status
= USB_FUNCTION_READEND
;
1177 usb1_function_set_pid_nak(pipe
); /* Set NAK */
1181 status
= USB_FUNCTION_READSHRT
; /* Null Packet receive */
1184 if ((count
% mxps
) != 0)
1186 status
= USB_FUNCTION_READSHRT
; /* Short Packet receive */
1189 else /* continue Receive data */
1191 status
= USB_FUNCTION_READING
;
1195 status
= USB_FUNCTION_READSHRT
; /* Null Packet receive */
1196 usb1_function_set_pid_nak(pipe
); /* Set NAK */
1199 if ((count
% mxps
) != 0)
1201 status
= USB_FUNCTION_READSHRT
; /* Short Packet receive */
1202 usb1_function_set_pid_nak(pipe
); /* Set NAK */
1206 pipebuf_size
= usb1_function_get_buf_size(pipe
); /* Data buffer size */
1208 if (count
!= pipebuf_size
)
1210 status
= USB_FUNCTION_READSHRT
; /* Short Packet receive */
1211 usb1_function_set_pid_nak(pipe
); /* Set NAK */
1216 if (count
== 0) /* 0 length packet */
1218 USB201
.D1FIFOCTR
= USB_FUNCTION_BITBCLR
; /* Clear BCLR */
1222 usb1_function_read_d1_fifo(pipe
, (uint16_t)count
);
1225 g_usb1_function_data_count
[pipe
] -= count
;
1227 return status
; /* End or Err or Continue */
1230 /*******************************************************************************
1231 * Function Name: usb1_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 usb1_function_read_dma (uint16_t pipe
)
1246 g_usb1_function_PipeIgnore
[pipe
] = 0;
1247 if ((g_usb1_function_PipeTbl
[pipe
] & USB_FUNCTION_FIFO_USE
) == USB_FUNCTION_D0FIFO_DMA
)
1249 status
= usb1_function_read_dma_d0(pipe
);
1253 status
= usb1_function_read_dma_d1(pipe
);
1258 case USB_FUNCTION_READING
: /* Continue of data read */
1261 case USB_FUNCTION_READZERO
: /* End of data read */
1262 usb1_function_disable_brdy_int(pipe
);
1263 g_usb1_function_pipe_status
[pipe
] = DEVDRV_USBF_PIPE_DONE
;
1266 case USB_FUNCTION_READEND
: /* End of data read */
1267 case USB_FUNCTION_READSHRT
: /* End of data read */
1268 usb1_function_disable_brdy_int(pipe
);
1270 if (RZA_IO_RegRead_16(&g_usb1_function_pipecfg
[pipe
], USB_PIPECFG_BFRE_SHIFT
, USB_PIPECFG_BFRE
) == 1)
1272 g_usb1_function_PipeDataSize
[pipe
] -= g_usb1_function_data_count
[pipe
];
1276 case USB_FUNCTION_READOVER
: /* buffer over */
1277 usb1_function_disable_brdy_int(pipe
); /* Disable Ready Interrupt */
1279 if (RZA_IO_RegRead_16(&g_usb1_function_pipecfg
[pipe
], USB_PIPECFG_BFRE_SHIFT
, USB_PIPECFG_BFRE
) == 1)
1281 g_usb1_function_PipeDataSize
[pipe
] -= g_usb1_function_data_count
[pipe
];
1283 g_usb1_function_pipe_status
[pipe
] = DEVDRV_USBF_FIFOERROR
;
1286 case DEVDRV_USBF_FIFOERROR
: /* FIFO access status */
1288 usb1_function_disable_brdy_int(pipe
); /* Disable Ready Interrupt */
1289 g_usb1_function_pipe_status
[pipe
] = DEVDRV_USBF_FIFOERROR
;
1293 return status
; /* End or Err or Continue */
1296 /*******************************************************************************
1297 * Function Name: usb1_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 usb1_function_read_dma_d0 (uint16_t pipe
)
1318 uint16_t pipebuf_size
;
1320 g_usb1_function_DmaStatus
[USB_FUNCTION_D0FIFO
] = USB_FUNCTION_DMA_READY
;
1322 mbw
= usb1_function_get_mbw(g_usb1_function_data_count
[pipe
], (uint32_t)g_usb1_function_data_pointer
[pipe
]);
1324 if (RZA_IO_RegRead_16(&g_usb1_function_pipecfg
[pipe
], USB_PIPECFG_BFRE_SHIFT
, USB_PIPECFG_BFRE
) == 1)
1326 count
= g_usb1_function_data_count
[pipe
];
1327 status
= USB_FUNCTION_READING
;
1331 buffer
= usb1_function_change_fifo_port(pipe
, USB_FUNCTION_D0DMA
, DEVDRV_USBF_NO
, mbw
);
1333 if (buffer
== DEVDRV_USBF_FIFOERROR
) /* FIFO access status */
1335 return DEVDRV_USBF_FIFOERROR
;
1338 dtln
= (uint32_t)(buffer
& USB_FUNCTION_BITDTLN
);
1339 mxps
= usb1_function_get_mxps(pipe
); /* Max Packet Size */
1341 if (g_usb1_function_data_count
[pipe
] < dtln
) /* Buffer Over ? */
1343 status
= USB_FUNCTION_READOVER
;
1344 count
= g_usb1_function_data_count
[pipe
];
1346 else if (g_usb1_function_data_count
[pipe
] == dtln
) /* just Receive Size */
1348 status
= USB_FUNCTION_READEND
;
1352 status
= USB_FUNCTION_READSHRT
; /* Null Packet receive */
1355 if ((count
% mxps
) != 0)
1357 status
= USB_FUNCTION_READSHRT
; /* Short Packet receive */
1360 else /* continue Receive data */
1362 status
= USB_FUNCTION_READING
;
1367 status
= USB_FUNCTION_READSHRT
; /* Null Packet receive */
1370 if ((count
% mxps
) != 0)
1372 status
= USB_FUNCTION_READSHRT
; /* Short Packet receive */
1376 pipebuf_size
= usb1_function_get_buf_size(pipe
); /* Data buffer size */
1377 if (count
!= pipebuf_size
)
1379 status
= USB_FUNCTION_READSHRT
; /* Short Packet receive */
1385 if (count
== 0) /* 0 length packet */
1387 if (RZA_IO_RegRead_16(&g_usb1_function_pipecfg
[pipe
], USB_PIPECFG_BFRE_SHIFT
, USB_PIPECFG_BFRE
) == 0)
1389 USB201
.D0FIFOCTR
= USB_FUNCTION_BITBCLR
; /* Clear BCLR */
1390 status
= USB_FUNCTION_READZERO
; /* Null Packet receive */
1394 usb1_function_set_curpipe(pipe
, USB_FUNCTION_D0DMA
, DEVDRV_USBF_NO
, mbw
);
1395 /* transaction counter No set */
1396 /* FRDY = 1, DTLN = 0 -> BRDY */
1401 dfacc
= usb1_function_set_dfacc_d0(mbw
, count
);
1403 if (mbw
== USB_FUNCTION_BITMBW_32
)
1405 g_usb1_function_DmaInfo
[USB_FUNCTION_D0FIFO
].size
= 2; /* 32bit transfer */
1407 else if (mbw
== USB_FUNCTION_BITMBW_16
)
1409 g_usb1_function_DmaInfo
[USB_FUNCTION_D0FIFO
].size
= 1; /* 16bit transfer */
1413 g_usb1_function_DmaInfo
[USB_FUNCTION_D0FIFO
].size
= 0; /* 8bit transfer */
1416 g_usb1_function_DmaPipe
[USB_FUNCTION_D0FIFO
] = pipe
; /* not use in read operation */
1417 g_usb1_function_DmaBval
[USB_FUNCTION_D0FIFO
] = 0; /* not use in read operation */
1419 g_usb1_function_DmaInfo
[USB_FUNCTION_D0FIFO
].fifo
= USB_FUNCTION_D0FIFO_DMA
;
1420 g_usb1_function_DmaInfo
[USB_FUNCTION_D0FIFO
].dir
= USB_FUNCTION_FIFO2BUF
;
1421 g_usb1_function_DmaInfo
[USB_FUNCTION_D0FIFO
].buffer
= (uint32_t)g_usb1_function_data_pointer
[pipe
];
1422 g_usb1_function_DmaInfo
[USB_FUNCTION_D0FIFO
].bytes
= count
;
1424 if (status
== USB_FUNCTION_READING
)
1426 g_usb1_function_DmaStatus
[USB_FUNCTION_D0FIFO
] = USB_FUNCTION_DMA_BUSY
;
1430 g_usb1_function_DmaStatus
[USB_FUNCTION_D0FIFO
] = USB_FUNCTION_DMA_BUSYEND
;
1433 Userdef_USB_usb1_function_start_dma(&g_usb1_function_DmaInfo
[USB_FUNCTION_D0FIFO
], dfacc
);
1435 usb1_function_set_curpipe2(pipe
, USB_FUNCTION_D0DMA
, DEVDRV_USBF_NO
, mbw
, dfacc
);
1437 RZA_IO_RegWrite_16(&USB201
.D0FIFOSEL
,
1439 USB_DnFIFOSEL_DREQE_SHIFT
,
1440 USB_DnFIFOSEL_DREQE
);
1443 if (RZA_IO_RegRead_16(&g_usb1_function_pipecfg
[pipe
], USB_PIPECFG_BFRE_SHIFT
, USB_PIPECFG_BFRE
) == 0)
1445 g_usb1_function_data_count
[pipe
] -= count
;
1446 g_usb1_function_data_pointer
[pipe
] += count
;
1447 g_usb1_function_PipeDataSize
[pipe
] += count
;
1450 return status
; /* End or Err or Continue */
1453 /*******************************************************************************
1454 * Function Name: usb1_function_read_dma_d1
1455 * Description : Reads data from the buffer allocated in the pipe specified in
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 usb1_function_read_dma_d1 (uint16_t pipe
)
1475 uint16_t pipebuf_size
;
1477 g_usb1_function_DmaStatus
[USB_FUNCTION_D1FIFO
] = USB_FUNCTION_DMA_READY
;
1479 mbw
= usb1_function_get_mbw(g_usb1_function_data_count
[pipe
], (uint32_t)g_usb1_function_data_pointer
[pipe
]);
1481 if (RZA_IO_RegRead_16(&g_usb1_function_pipecfg
[pipe
], USB_PIPECFG_BFRE_SHIFT
, USB_PIPECFG_BFRE
) == 1)
1483 count
= g_usb1_function_data_count
[pipe
];
1484 status
= USB_FUNCTION_READING
;
1488 buffer
= usb1_function_change_fifo_port(pipe
, USB_FUNCTION_D1DMA
, DEVDRV_USBF_NO
, mbw
);
1489 if (buffer
== DEVDRV_USBF_FIFOERROR
) /* FIFO access status */
1491 return DEVDRV_USBF_FIFOERROR
;
1494 dtln
= (uint32_t)(buffer
& USB_FUNCTION_BITDTLN
);
1495 mxps
= usb1_function_get_mxps(pipe
); /* Max Packet Size */
1497 if (g_usb1_function_data_count
[pipe
] < dtln
) /* Buffer Over ? */
1499 status
= USB_FUNCTION_READOVER
;
1500 count
= g_usb1_function_data_count
[pipe
];
1502 else if (g_usb1_function_data_count
[pipe
] == dtln
) /* just Receive Size */
1504 status
= USB_FUNCTION_READEND
;
1508 status
= USB_FUNCTION_READSHRT
; /* Null Packet receive */
1511 if ((count
% mxps
) != 0)
1513 status
= USB_FUNCTION_READSHRT
; /* Short Packet receive */
1516 else /* continue Receive data */
1518 status
= USB_FUNCTION_READING
;
1522 status
= USB_FUNCTION_READSHRT
; /* Null Packet receive */
1525 if ((count
% mxps
) != 0)
1527 status
= USB_FUNCTION_READSHRT
; /* Short Packet receive */
1531 pipebuf_size
= usb1_function_get_buf_size(pipe
); /* Data buffer size */
1532 if (count
!= pipebuf_size
)
1534 status
= USB_FUNCTION_READSHRT
; /* Short Packet receive */
1540 if (count
== 0) /* 0 length packet */
1542 if (RZA_IO_RegRead_16(&g_usb1_function_pipecfg
[pipe
], USB_PIPECFG_BFRE_SHIFT
, USB_PIPECFG_BFRE
) == 0)
1544 USB201
.D1FIFOCTR
= USB_FUNCTION_BITBCLR
; /* Clear BCLR */
1545 status
= USB_FUNCTION_READZERO
; /* Null Packet receive */
1549 usb1_function_set_curpipe(pipe
, USB_FUNCTION_D1DMA
, DEVDRV_USBF_NO
, mbw
);
1550 /* transaction counter No set */
1551 /* FRDY = 1, DTLN = 0 -> BRDY */
1556 dfacc
= usb1_function_set_dfacc_d1(mbw
, count
);
1558 if (mbw
== USB_FUNCTION_BITMBW_32
)
1560 g_usb1_function_DmaInfo
[USB_FUNCTION_D1FIFO
].size
= 2; /* 32bit transfer */
1562 else if (mbw
== USB_FUNCTION_BITMBW_16
)
1564 g_usb1_function_DmaInfo
[USB_FUNCTION_D1FIFO
].size
= 1; /* 16bit transfer */
1568 g_usb1_function_DmaInfo
[USB_FUNCTION_D1FIFO
].size
= 0; /* 8bit transfer */
1571 g_usb1_function_DmaPipe
[USB_FUNCTION_D1FIFO
] = pipe
; /* not use in read operation */
1572 g_usb1_function_DmaBval
[USB_FUNCTION_D1FIFO
] = 0; /* not use in read operation */
1574 g_usb1_function_DmaInfo
[USB_FUNCTION_D1FIFO
].fifo
= USB_FUNCTION_D1FIFO_DMA
;
1575 g_usb1_function_DmaInfo
[USB_FUNCTION_D1FIFO
].dir
= USB_FUNCTION_FIFO2BUF
;
1576 g_usb1_function_DmaInfo
[USB_FUNCTION_D1FIFO
].buffer
= (uint32_t)g_usb1_function_data_pointer
[pipe
];
1577 g_usb1_function_DmaInfo
[USB_FUNCTION_D1FIFO
].bytes
= count
;
1579 if (status
== USB_FUNCTION_READING
)
1581 g_usb1_function_DmaStatus
[USB_FUNCTION_D1FIFO
] = USB_FUNCTION_DMA_BUSY
;
1585 g_usb1_function_DmaStatus
[USB_FUNCTION_D1FIFO
] = USB_FUNCTION_DMA_BUSYEND
;
1588 Userdef_USB_usb1_function_start_dma(&g_usb1_function_DmaInfo
[USB_FUNCTION_D1FIFO
], dfacc
);
1590 usb1_function_set_curpipe2(pipe
, USB_FUNCTION_D1DMA
, DEVDRV_USBF_NO
, mbw
, dfacc
);
1592 RZA_IO_RegWrite_16(&USB201
.D1FIFOSEL
,
1594 USB_DnFIFOSEL_DREQE_SHIFT
,
1595 USB_DnFIFOSEL_DREQE
);
1598 if (RZA_IO_RegRead_16(&g_usb1_function_pipecfg
[pipe
], USB_PIPECFG_BFRE_SHIFT
, USB_PIPECFG_BFRE
) == 0)
1600 g_usb1_function_data_count
[pipe
] -= count
;
1601 g_usb1_function_data_pointer
[pipe
] += count
;
1602 g_usb1_function_PipeDataSize
[pipe
] += count
;
1605 return status
; /* End or Err or Continue */
1608 /*******************************************************************************
1609 * Function Name: usb1_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 usb1_function_change_fifo_port (uint16_t pipe
, uint16_t fifosel
, uint16_t isel
, uint16_t mbw
)
1624 volatile uint32_t loop2
;
1626 usb1_function_set_curpipe(pipe
, fifosel
, isel
, mbw
);
1628 for (loop
= 0; loop
< 4; loop
++)
1632 case USB_FUNCTION_CUSE
:
1633 buffer
= USB201
.CFIFOCTR
;
1636 case USB_FUNCTION_D0USE
:
1637 case USB_FUNCTION_D0DMA
:
1638 buffer
= USB201
.D0FIFOCTR
;
1641 case USB_FUNCTION_D1USE
:
1642 case USB_FUNCTION_D1DMA
:
1643 buffer
= USB201
.D1FIFOCTR
;
1651 if ((buffer
& USB_FUNCTION_BITFRDY
) == USB_FUNCTION_BITFRDY
)
1663 return DEVDRV_USBF_FIFOERROR
;
1666 /*******************************************************************************
1667 * Function Name: usb1_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 usb1_function_set_curpipe (uint16_t pipe
, uint16_t fifosel
, uint16_t isel
, uint16_t mbw
)
1680 volatile uint32_t loop2
;
1682 g_usb1_function_mbw
[pipe
] = mbw
;
1686 case USB_FUNCTION_CUSE
:
1687 buffer
= USB201
.CFIFOSEL
;
1688 buffer
&= (uint16_t)~(USB_FUNCTION_BITISEL
| USB_FUNCTION_BITCURPIPE
);
1689 buffer
|= (uint16_t)(~isel
& USB_FUNCTION_BITISEL
);
1690 USB201
.CFIFOSEL
= buffer
;
1692 for (loop
= 0; loop
< 4; loop
++)
1694 if ((USB201
.CFIFOSEL
& (USB_FUNCTION_BITISEL
| USB_FUNCTION_BITCURPIPE
)) ==
1695 (buffer
& (USB_FUNCTION_BITISEL
| USB_FUNCTION_BITCURPIPE
)))
1707 buffer
&= (uint16_t)~(USB_FUNCTION_BITISEL
| USB_FUNCTION_BITCURPIPE
| USB_FUNCTION_BITMBW
);
1708 buffer
|= (uint16_t)(isel
| pipe
| mbw
);
1709 USB201
.CFIFOSEL
= buffer
;
1711 for (loop
= 0; loop
< 4; loop
++)
1713 if ((USB201
.CFIFOSEL
& (USB_FUNCTION_BITISEL
| USB_FUNCTION_BITCURPIPE
)) ==
1714 (buffer
& (USB_FUNCTION_BITISEL
| USB_FUNCTION_BITCURPIPE
)))
1728 case USB_FUNCTION_D0DMA
:
1729 case USB_FUNCTION_D0USE
:
1730 buffer
= USB201
.D0FIFOSEL
;
1731 buffer
&= (uint16_t)~(USB_FUNCTION_BITCURPIPE
);
1732 USB201
.D0FIFOSEL
= buffer
;
1734 for (loop
= 0; loop
< 4; loop
++)
1736 if ((USB201
.D0FIFOSEL
& USB_FUNCTION_BITCURPIPE
) ==
1737 (buffer
& USB_FUNCTION_BITCURPIPE
))
1749 buffer
&= (uint16_t)~(USB_FUNCTION_BITCURPIPE
| USB_FUNCTION_BITMBW
);
1750 buffer
|= (uint16_t)(pipe
| mbw
);
1751 USB201
.D0FIFOSEL
= buffer
;
1753 for (loop
= 0; loop
< 4; loop
++)
1755 if ((USB201
.D0FIFOSEL
& USB_FUNCTION_BITCURPIPE
) ==
1756 (buffer
& USB_FUNCTION_BITCURPIPE
))
1769 case USB_FUNCTION_D1DMA
:
1770 case USB_FUNCTION_D1USE
:
1771 buffer
= USB201
.D1FIFOSEL
;
1772 buffer
&= (uint16_t)~(USB_FUNCTION_BITCURPIPE
);
1773 USB201
.D1FIFOSEL
= buffer
;
1775 for (loop
= 0; loop
< 4; loop
++)
1777 if ((USB201
.D1FIFOSEL
& USB_FUNCTION_BITCURPIPE
) ==
1778 (buffer
& USB_FUNCTION_BITCURPIPE
))
1790 buffer
&= (uint16_t)~(USB_FUNCTION_BITCURPIPE
| USB_FUNCTION_BITMBW
);
1791 buffer
|= (uint16_t)(pipe
| mbw
);
1792 USB201
.D1FIFOSEL
= buffer
;
1794 for (loop
= 0; loop
< 4; loop
++)
1796 if ((USB201
.D1FIFOSEL
& USB_FUNCTION_BITCURPIPE
) ==
1797 (buffer
& USB_FUNCTION_BITCURPIPE
))
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. */
1826 /*******************************************************************************
1827 * Function Name: usb1_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 usb1_function_set_curpipe2 (uint16_t pipe
, uint16_t fifosel
, uint16_t isel
, uint16_t mbw
, uint16_t dfacc
)
1841 #ifdef __USB_FUNCTION_DF_ACC_ENABLE__
1844 volatile uint32_t loop2
;
1846 g_usb1_function_mbw
[pipe
] = mbw
;
1850 case USB_FUNCTION_CUSE
:
1851 buffer
= USB201
.CFIFOSEL
;
1852 buffer
&= (uint16_t)~(USB_FUNCTION_BITISEL
| USB_FUNCTION_BITCURPIPE
);
1853 buffer
|= (uint16_t)(~isel
& USB_FUNCTION_BITISEL
);
1854 USB201
.CFIFOSEL
= buffer
;
1856 for (loop
= 0; loop
< 4; loop
++)
1858 if ((USB201
.CFIFOSEL
& (USB_FUNCTION_BITISEL
| USB_FUNCTION_BITCURPIPE
)) ==
1859 (buffer
& (USB_FUNCTION_BITISEL
| USB_FUNCTION_BITCURPIPE
)))
1870 buffer
&= (uint16_t)~(USB_FUNCTION_BITISEL
| USB_FUNCTION_BITCURPIPE
| USB_FUNCTION_BITMBW
);
1871 buffer
|= (uint16_t)(isel
| pipe
| mbw
);
1872 USB201
.CFIFOSEL
= buffer
;
1874 for (loop
= 0; loop
< 4; loop
++)
1876 if ((USB201
.CFIFOSEL
& (USB_FUNCTION_BITISEL
| USB_FUNCTION_BITCURPIPE
)) ==
1877 (buffer
& (USB_FUNCTION_BITISEL
| USB_FUNCTION_BITCURPIPE
)))
1891 case USB_FUNCTION_D0DMA
:
1892 case USB_FUNCTION_D0USE
:
1893 buffer
= USB201
.D0FIFOSEL
;
1894 #ifdef __USB_FUNCTION_DF_ACC_ENABLE__
1895 buffer
&= (uint16_t)~(USB_FUNCTION_BITCURPIPE
| USB_FUNCTION_BITMBW
);
1899 buffer
|= (uint16_t)(USB_FUNCTION_BITMBW_32
);
1902 buffer
&= (uint16_t)~(USB_FUNCTION_BITCURPIPE
);
1904 USB201
.D0FIFOSEL
= buffer
;
1906 for (loop
= 0; loop
< 4; loop
++)
1908 if ((USB201
.D0FIFOSEL
& USB_FUNCTION_BITCURPIPE
) == (buffer
& USB_FUNCTION_BITCURPIPE
))
1921 #ifdef __USB_FUNCTION_DF_ACC_ENABLE__
1924 dummy
= USB201
.D0FIFO
.UINT32
;
1927 buffer
&= (uint16_t)~(USB_FUNCTION_BITCURPIPE
| USB_FUNCTION_BITMBW
);
1928 buffer
|= (uint16_t)(pipe
| mbw
);
1929 USB201
.D0FIFOSEL
= buffer
;
1931 for (loop
= 0; loop
< 4; loop
++)
1933 if ((USB201
.D0FIFOSEL
& USB_FUNCTION_BITCURPIPE
) == (buffer
& USB_FUNCTION_BITCURPIPE
))
1947 case USB_FUNCTION_D1DMA
:
1948 case USB_FUNCTION_D1USE
:
1949 buffer
= USB201
.D1FIFOSEL
;
1950 #ifdef __USB_FUNCTION_DF_ACC_ENABLE__
1951 buffer
&= (uint16_t)~(USB_FUNCTION_BITCURPIPE
| USB_FUNCTION_BITMBW
);
1955 buffer
|= (uint16_t)(USB_FUNCTION_BITMBW_32
);
1958 buffer
&= (uint16_t)~(USB_FUNCTION_BITCURPIPE
);
1960 USB201
.D1FIFOSEL
= buffer
;
1962 for (loop
= 0; loop
< 4; loop
++)
1964 if ((USB201
.D1FIFOSEL
& USB_FUNCTION_BITCURPIPE
) == (buffer
& USB_FUNCTION_BITCURPIPE
))
1976 #ifdef __USB_FUNCTION_DF_ACC_ENABLE__
1979 dummy
= USB201
.D1FIFO
.UINT32
;
1980 loop
= dummy
; // avoid warning.
1983 buffer
&= (uint16_t)~(USB_FUNCTION_BITCURPIPE
| USB_FUNCTION_BITMBW
);
1984 buffer
|= (uint16_t)(pipe
| mbw
);
1985 USB201
.D1FIFOSEL
= buffer
;
1987 for (loop
= 0; loop
< 4; loop
++)
1989 if ((USB201
.D1FIFOSEL
& USB_FUNCTION_BITCURPIPE
) == (buffer
& USB_FUNCTION_BITCURPIPE
))
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. */
2018 /*******************************************************************************
2019 * Function Name: usb1_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_usb1_function_mbw[].
2027 * Arguments : uint16_t pipe ; Pipe Number
2028 * : uint16_t count ; Data Size(Byte)
2029 * Return Value : none
2030 *******************************************************************************/
2031 static void usb1_function_write_c_fifo (uint16_t pipe
, uint16_t count
)
2035 if (g_usb1_function_mbw
[pipe
] == USB_FUNCTION_BITMBW_8
)
2037 for (even
= count
; even
; --even
)
2039 USB201
.CFIFO
.UINT8
[HH
] = *g_usb1_function_data_pointer
[pipe
];
2040 g_usb1_function_data_pointer
[pipe
] += 1;
2043 else if (g_usb1_function_mbw
[pipe
] == USB_FUNCTION_BITMBW_16
)
2045 for (even
= (uint16_t)(count
/ 2); even
; --even
)
2047 USB201
.CFIFO
.UINT16
[H
] = *((uint16_t *)g_usb1_function_data_pointer
[pipe
]);
2048 g_usb1_function_data_pointer
[pipe
] += 2;
2053 for (even
= (uint16_t)(count
/ 4); even
; --even
)
2055 USB201
.CFIFO
.UINT32
= *((uint32_t *)g_usb1_function_data_pointer
[pipe
]);
2056 g_usb1_function_data_pointer
[pipe
] += 4;
2061 /*******************************************************************************
2062 * Function Name: usb1_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_usb1_function_mbw[].
2070 * Arguments : uint16_t pipe ; Pipe Number
2071 * : uint16_t count ; Data Size(Byte)
2072 * Return Value : none
2073 *******************************************************************************/
2074 static void usb1_function_read_c_fifo (uint16_t pipe
, uint16_t count
)
2078 if (g_usb1_function_mbw
[pipe
] == USB_FUNCTION_BITMBW_8
)
2080 for (even
= count
; even
; --even
)
2082 *g_usb1_function_data_pointer
[pipe
] = USB201
.CFIFO
.UINT8
[HH
];
2083 g_usb1_function_data_pointer
[pipe
] += 1;
2086 else if (g_usb1_function_mbw
[pipe
] == USB_FUNCTION_BITMBW_16
)
2088 for (even
= (uint16_t)((count
+ 1) / 2); even
; --even
)
2090 *((uint16_t *)g_usb1_function_data_pointer
[pipe
]) = USB201
.CFIFO
.UINT16
[H
];
2091 g_usb1_function_data_pointer
[pipe
] += 2;
2096 for (even
= (uint16_t)((count
+ 3) / 4); even
; --even
)
2098 *((uint32_t *)g_usb1_function_data_pointer
[pipe
]) = USB201
.CFIFO
.UINT32
;
2099 g_usb1_function_data_pointer
[pipe
] += 4;
2104 /*******************************************************************************
2105 * Function Name: usb1_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_usb1_function_mbw[].
2113 * Arguments : uint16_t pipe ; Pipe Number
2114 * : uint16_t count ; Data Size(Byte)
2115 * Return Value : none
2116 *******************************************************************************/
2117 static void usb1_function_write_d0_fifo (uint16_t pipe
, uint16_t count
)
2121 if (g_usb1_function_mbw
[pipe
] == USB_FUNCTION_BITMBW_8
)
2123 for (even
= count
; even
; --even
)
2125 USB201
.D0FIFO
.UINT8
[HH
] = *g_usb1_function_data_pointer
[pipe
];
2126 g_usb1_function_data_pointer
[pipe
] += 1;
2129 else if (g_usb1_function_mbw
[pipe
] == USB_FUNCTION_BITMBW_16
)
2131 for (even
= (uint16_t)(count
/ 2); even
; --even
)
2133 USB201
.D0FIFO
.UINT16
[H
] = *((uint16_t *)g_usb1_function_data_pointer
[pipe
]);
2134 g_usb1_function_data_pointer
[pipe
] += 2;
2139 for (even
= (uint16_t)(count
/ 4); even
; --even
)
2141 USB201
.D0FIFO
.UINT32
= *((uint32_t *)g_usb1_function_data_pointer
[pipe
]);
2142 g_usb1_function_data_pointer
[pipe
] += 4;
2147 /*******************************************************************************
2148 * Function Name: usb1_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_usb1_function_mbw[].
2156 * Arguments : uint16_t pipe ; Pipe Number
2157 * : uint16_t count ; Data Size(Byte)
2158 * Return Value : none
2159 *******************************************************************************/
2160 static void usb1_function_read_d0_fifo (uint16_t pipe
, uint16_t count
)
2164 if (g_usb1_function_mbw
[pipe
] == USB_FUNCTION_BITMBW_8
)
2166 for (even
= count
; even
; --even
)
2168 *g_usb1_function_data_pointer
[pipe
] = USB201
.D0FIFO
.UINT8
[HH
];
2169 g_usb1_function_data_pointer
[pipe
] += 1;
2172 else if (g_usb1_function_mbw
[pipe
] == USB_FUNCTION_BITMBW_16
)
2174 for (even
= (uint16_t)((count
+ 1) / 2); even
; --even
)
2176 *((uint16_t *)g_usb1_function_data_pointer
[pipe
]) = USB201
.D0FIFO
.UINT16
[H
];
2177 g_usb1_function_data_pointer
[pipe
] += 2;
2182 for (even
= (uint16_t)((count
+ 3) / 4); even
; --even
)
2184 *((uint32_t *)g_usb1_function_data_pointer
[pipe
]) = USB201
.D0FIFO
.UINT32
;
2185 g_usb1_function_data_pointer
[pipe
] += 4;
2190 /*******************************************************************************
2191 * Function Name: usb1_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_usb1_function_mbw[].
2199 * Arguments : uint16_t pipe ; Pipe Number
2200 * : uint16_t count ; Data Size(Byte)
2201 * Return Value : none
2202 *******************************************************************************/
2203 static void usb1_function_write_d1_fifo (uint16_t pipe
, uint16_t count
)
2207 if (g_usb1_function_mbw
[pipe
] == USB_FUNCTION_BITMBW_8
)
2209 for (even
= count
; even
; --even
)
2211 USB201
.D1FIFO
.UINT8
[HH
] = *g_usb1_function_data_pointer
[pipe
];
2212 g_usb1_function_data_pointer
[pipe
] += 1;
2215 else if (g_usb1_function_mbw
[pipe
] == USB_FUNCTION_BITMBW_16
)
2217 for (even
= (uint16_t)(count
/ 2); even
; --even
)
2219 USB201
.D1FIFO
.UINT16
[H
] = *((uint16_t *)g_usb1_function_data_pointer
[pipe
]);
2220 g_usb1_function_data_pointer
[pipe
] += 2;
2225 for (even
= (uint16_t)(count
/ 4); even
; --even
)
2227 USB201
.D1FIFO
.UINT32
= *((uint32_t *)g_usb1_function_data_pointer
[pipe
]);
2228 g_usb1_function_data_pointer
[pipe
] += 4;
2233 /*******************************************************************************
2234 * Function Name: usb1_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_usb1_function_mbw[].
2242 * Arguments : uint16_t pipe ; Pipe Number
2243 * : uint16_t count ; Data Size(Byte)
2244 * Return Value : none
2245 *******************************************************************************/
2246 static void usb1_function_read_d1_fifo (uint16_t pipe
, uint16_t count
)
2250 if (g_usb1_function_mbw
[pipe
] == USB_FUNCTION_BITMBW_8
)
2252 for (even
= count
; even
; --even
)
2254 *g_usb1_function_data_pointer
[pipe
] = USB201
.D1FIFO
.UINT8
[HH
];
2255 g_usb1_function_data_pointer
[pipe
] += 1;
2258 else if (g_usb1_function_mbw
[pipe
] == USB_FUNCTION_BITMBW_16
)
2260 for (even
= (uint16_t)((count
+ 1) / 2); even
; --even
)
2262 *((uint16_t *)g_usb1_function_data_pointer
[pipe
]) = USB201
.D1FIFO
.UINT16
[H
];
2263 g_usb1_function_data_pointer
[pipe
] += 2;
2268 for (even
= (uint16_t)((count
+ 3) / 4); even
; --even
)
2270 *((uint32_t *)g_usb1_function_data_pointer
[pipe
]) = USB201
.D1FIFO
.UINT32
;
2271 g_usb1_function_data_pointer
[pipe
] += 4;
2276 /*******************************************************************************
2277 * Function Name: usb1_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
2285 *******************************************************************************/
2286 static uint32_t usb1_function_com_get_dmasize (uint32_t trncount
, uint32_t dtptr
)
2290 if (((trncount
& 0x0001) != 0) || ((dtptr
& 0x00000001) != 0))
2292 /* When transfer byte count is odd */
2293 /* or transfer data area is 8-bit alignment */
2294 size
= 0; /* 8bit */
2296 else if (((trncount
& 0x0003) != 0) || ((dtptr
& 0x00000003) != 0))
2298 /* When the transfer byte count is multiples of 2 */
2299 /* or the transfer data area is 16-bit alignment */
2300 size
= 1; /* 16bit */
2304 /* When the transfer byte count is multiples of 4 */
2305 /* or the transfer data area is 32-bit alignment */
2306 size
= 2; /* 32bit */
2312 /*******************************************************************************
2313 * Function Name: usb1_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 usb1_function_get_mbw (uint32_t trncount
, uint32_t dtptr
)
2326 size
= usb1_function_com_get_dmasize(trncount
, dtptr
);
2331 mbw
= USB_FUNCTION_BITMBW_8
;
2336 mbw
= USB_FUNCTION_BITMBW_16
;
2341 mbw
= USB_FUNCTION_BITMBW_32
;
2347 /*******************************************************************************
2348 * Function Name: usb1_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 usb1_function_set_transaction_counter (uint16_t pipe
, uint32_t bsize
)
2365 mxps
= usb1_function_get_mxps(pipe
); /* Max Packet Size */
2367 if ((bsize
% mxps
) == 0)
2369 cnt
= (uint16_t)(bsize
/ mxps
);
2373 cnt
= (uint16_t)((bsize
/ mxps
) + 1);
2378 case USB_FUNCTION_PIPE1
:
2379 RZA_IO_RegWrite_16(&USB201
.PIPE1TRE
,
2381 USB_PIPEnTRE_TRCLR_SHIFT
,
2382 USB_PIPEnTRE_TRCLR
);
2383 USB201
.PIPE1TRN
= cnt
;
2384 RZA_IO_RegWrite_16(&USB201
.PIPE1TRE
,
2386 USB_PIPEnTRE_TRENB_SHIFT
,
2387 USB_PIPEnTRE_TRENB
);
2390 case USB_FUNCTION_PIPE2
:
2391 RZA_IO_RegWrite_16(&USB201
.PIPE2TRE
,
2393 USB_PIPEnTRE_TRCLR_SHIFT
,
2394 USB_PIPEnTRE_TRCLR
);
2395 USB201
.PIPE2TRN
= cnt
;
2396 RZA_IO_RegWrite_16(&USB201
.PIPE2TRE
,
2398 USB_PIPEnTRE_TRENB_SHIFT
,
2399 USB_PIPEnTRE_TRENB
);
2402 case USB_FUNCTION_PIPE3
:
2403 RZA_IO_RegWrite_16(&USB201
.PIPE3TRE
,
2405 USB_PIPEnTRE_TRCLR_SHIFT
,
2406 USB_PIPEnTRE_TRCLR
);
2407 USB201
.PIPE3TRN
= cnt
;
2408 RZA_IO_RegWrite_16(&USB201
.PIPE3TRE
,
2410 USB_PIPEnTRE_TRENB_SHIFT
,
2411 USB_PIPEnTRE_TRENB
);
2414 case USB_FUNCTION_PIPE4
:
2415 RZA_IO_RegWrite_16(&USB201
.PIPE4TRE
,
2417 USB_PIPEnTRE_TRCLR_SHIFT
,
2418 USB_PIPEnTRE_TRCLR
);
2419 USB201
.PIPE4TRN
= cnt
;
2420 RZA_IO_RegWrite_16(&USB201
.PIPE4TRE
,
2422 USB_PIPEnTRE_TRENB_SHIFT
,
2423 USB_PIPEnTRE_TRENB
);
2426 case USB_FUNCTION_PIPE5
:
2427 RZA_IO_RegWrite_16(&USB201
.PIPE5TRE
,
2429 USB_PIPEnTRE_TRCLR_SHIFT
,
2430 USB_PIPEnTRE_TRCLR
);
2431 USB201
.PIPE5TRN
= cnt
;
2432 RZA_IO_RegWrite_16(&USB201
.PIPE5TRE
,
2434 USB_PIPEnTRE_TRENB_SHIFT
,
2435 USB_PIPEnTRE_TRENB
);
2438 case USB_FUNCTION_PIPE9
:
2439 RZA_IO_RegWrite_16(&USB201
.PIPE9TRE
,
2441 USB_PIPEnTRE_TRCLR_SHIFT
,
2442 USB_PIPEnTRE_TRCLR
);
2443 USB201
.PIPE9TRN
= cnt
;
2444 RZA_IO_RegWrite_16(&USB201
.PIPE9TRE
,
2446 USB_PIPEnTRE_TRENB_SHIFT
,
2447 USB_PIPEnTRE_TRENB
);
2450 case USB_FUNCTION_PIPEA
:
2451 RZA_IO_RegWrite_16(&USB201
.PIPEATRE
,
2453 USB_PIPEnTRE_TRCLR_SHIFT
,
2454 USB_PIPEnTRE_TRCLR
);
2455 USB201
.PIPEATRN
= cnt
;
2456 RZA_IO_RegWrite_16(&USB201
.PIPEATRE
,
2458 USB_PIPEnTRE_TRENB_SHIFT
,
2459 USB_PIPEnTRE_TRENB
);
2462 case USB_FUNCTION_PIPEB
:
2463 RZA_IO_RegWrite_16(&USB201
.PIPEBTRE
,
2465 USB_PIPEnTRE_TRCLR_SHIFT
,
2466 USB_PIPEnTRE_TRCLR
);
2467 USB201
.PIPEBTRN
= cnt
;
2468 RZA_IO_RegWrite_16(&USB201
.PIPEBTRE
,
2470 USB_PIPEnTRE_TRENB_SHIFT
,
2471 USB_PIPEnTRE_TRENB
);
2474 case USB_FUNCTION_PIPEC
:
2475 RZA_IO_RegWrite_16(&USB201
.PIPECTRE
,
2477 USB_PIPEnTRE_TRCLR_SHIFT
,
2478 USB_PIPEnTRE_TRCLR
);
2479 USB201
.PIPECTRN
= cnt
;
2480 RZA_IO_RegWrite_16(&USB201
.PIPECTRE
,
2482 USB_PIPEnTRE_TRENB_SHIFT
,
2483 USB_PIPEnTRE_TRENB
);
2486 case USB_FUNCTION_PIPED
:
2487 RZA_IO_RegWrite_16(&USB201
.PIPEDTRE
,
2489 USB_PIPEnTRE_TRCLR_SHIFT
,
2490 USB_PIPEnTRE_TRCLR
);
2491 USB201
.PIPEDTRN
= cnt
;
2492 RZA_IO_RegWrite_16(&USB201
.PIPEDTRE
,
2494 USB_PIPEnTRE_TRENB_SHIFT
,
2495 USB_PIPEnTRE_TRENB
);
2498 case USB_FUNCTION_PIPEE
:
2499 RZA_IO_RegWrite_16(&USB201
.PIPEETRE
,
2501 USB_PIPEnTRE_TRCLR_SHIFT
,
2502 USB_PIPEnTRE_TRCLR
);
2503 USB201
.PIPEETRN
= cnt
;
2504 RZA_IO_RegWrite_16(&USB201
.PIPEETRE
,
2506 USB_PIPEnTRE_TRENB_SHIFT
,
2507 USB_PIPEnTRE_TRENB
);
2510 case USB_FUNCTION_PIPEF
:
2511 RZA_IO_RegWrite_16(&USB201
.PIPEFTRE
,
2513 USB_PIPEnTRE_TRCLR_SHIFT
,
2514 USB_PIPEnTRE_TRCLR
);
2515 USB201
.PIPEFTRN
= cnt
;
2516 RZA_IO_RegWrite_16(&USB201
.PIPEFTRE
,
2518 USB_PIPEnTRE_TRENB_SHIFT
,
2519 USB_PIPEnTRE_TRENB
);
2527 /*******************************************************************************
2528 * Function Name: usb1_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 usb1_function_clear_transaction_counter (uint16_t pipe
)
2538 case USB_FUNCTION_PIPE1
:
2539 RZA_IO_RegWrite_16(&USB201
.PIPE1TRE
,
2541 USB_PIPEnTRE_TRENB_SHIFT
,
2542 USB_PIPEnTRE_TRENB
);
2543 RZA_IO_RegWrite_16(&USB201
.PIPE1TRE
,
2545 USB_PIPEnTRE_TRCLR_SHIFT
,
2546 USB_PIPEnTRE_TRCLR
);
2549 case USB_FUNCTION_PIPE2
:
2550 RZA_IO_RegWrite_16(&USB201
.PIPE2TRE
,
2552 USB_PIPEnTRE_TRENB_SHIFT
,
2553 USB_PIPEnTRE_TRENB
);
2554 RZA_IO_RegWrite_16(&USB201
.PIPE2TRE
,
2556 USB_PIPEnTRE_TRCLR_SHIFT
,
2557 USB_PIPEnTRE_TRCLR
);
2560 case USB_FUNCTION_PIPE3
:
2561 RZA_IO_RegWrite_16(&USB201
.PIPE3TRE
,
2563 USB_PIPEnTRE_TRENB_SHIFT
,
2564 USB_PIPEnTRE_TRENB
);
2565 RZA_IO_RegWrite_16(&USB201
.PIPE3TRE
,
2567 USB_PIPEnTRE_TRCLR_SHIFT
,
2568 USB_PIPEnTRE_TRCLR
);
2571 case USB_FUNCTION_PIPE4
:
2572 RZA_IO_RegWrite_16(&USB201
.PIPE4TRE
,
2574 USB_PIPEnTRE_TRENB_SHIFT
,
2575 USB_PIPEnTRE_TRENB
);
2576 RZA_IO_RegWrite_16(&USB201
.PIPE4TRE
,
2578 USB_PIPEnTRE_TRCLR_SHIFT
,
2579 USB_PIPEnTRE_TRCLR
);
2582 case USB_FUNCTION_PIPE5
:
2583 RZA_IO_RegWrite_16(&USB201
.PIPE5TRE
,
2585 USB_PIPEnTRE_TRENB_SHIFT
,
2586 USB_PIPEnTRE_TRENB
);
2587 RZA_IO_RegWrite_16(&USB201
.PIPE5TRE
,
2589 USB_PIPEnTRE_TRCLR_SHIFT
,
2590 USB_PIPEnTRE_TRCLR
);
2593 case USB_FUNCTION_PIPE9
:
2594 RZA_IO_RegWrite_16(&USB201
.PIPE9TRE
,
2596 USB_PIPEnTRE_TRENB_SHIFT
,
2597 USB_PIPEnTRE_TRENB
);
2598 RZA_IO_RegWrite_16(&USB201
.PIPE9TRE
,
2600 USB_PIPEnTRE_TRCLR_SHIFT
,
2601 USB_PIPEnTRE_TRCLR
);
2604 case USB_FUNCTION_PIPEA
:
2605 RZA_IO_RegWrite_16(&USB201
.PIPEATRE
,
2607 USB_PIPEnTRE_TRENB_SHIFT
,
2608 USB_PIPEnTRE_TRENB
);
2609 RZA_IO_RegWrite_16(&USB201
.PIPEATRE
,
2611 USB_PIPEnTRE_TRCLR_SHIFT
,
2612 USB_PIPEnTRE_TRCLR
);
2615 case USB_FUNCTION_PIPEB
:
2616 RZA_IO_RegWrite_16(&USB201
.PIPEBTRE
,
2618 USB_PIPEnTRE_TRENB_SHIFT
,
2619 USB_PIPEnTRE_TRENB
);
2620 RZA_IO_RegWrite_16(&USB201
.PIPEBTRE
,
2622 USB_PIPEnTRE_TRCLR_SHIFT
,
2623 USB_PIPEnTRE_TRCLR
);
2626 case USB_FUNCTION_PIPEC
:
2627 RZA_IO_RegWrite_16(&USB201
.PIPECTRE
,
2629 USB_PIPEnTRE_TRENB_SHIFT
,
2630 USB_PIPEnTRE_TRENB
);
2631 RZA_IO_RegWrite_16(&USB201
.PIPECTRE
,
2633 USB_PIPEnTRE_TRCLR_SHIFT
,
2634 USB_PIPEnTRE_TRCLR
);
2637 case USB_FUNCTION_PIPED
:
2638 RZA_IO_RegWrite_16(&USB201
.PIPEDTRE
,
2640 USB_PIPEnTRE_TRENB_SHIFT
,
2641 USB_PIPEnTRE_TRENB
);
2642 RZA_IO_RegWrite_16(&USB201
.PIPEDTRE
,
2644 USB_PIPEnTRE_TRCLR_SHIFT
,
2645 USB_PIPEnTRE_TRCLR
);
2648 case USB_FUNCTION_PIPEE
:
2649 RZA_IO_RegWrite_16(&USB201
.PIPEETRE
,
2651 USB_PIPEnTRE_TRENB_SHIFT
,
2652 USB_PIPEnTRE_TRENB
);
2653 RZA_IO_RegWrite_16(&USB201
.PIPEETRE
,
2655 USB_PIPEnTRE_TRCLR_SHIFT
,
2656 USB_PIPEnTRE_TRCLR
);
2659 case USB_FUNCTION_PIPEF
:
2660 RZA_IO_RegWrite_16(&USB201
.PIPEFTRE
,
2662 USB_PIPEnTRE_TRENB_SHIFT
,
2663 USB_PIPEnTRE_TRENB
);
2664 RZA_IO_RegWrite_16(&USB201
.PIPEFTRE
,
2666 USB_PIPEnTRE_TRCLR_SHIFT
,
2667 USB_PIPEnTRE_TRCLR
);
2675 /*******************************************************************************
2676 * Function Name: usb1_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
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
2684 * Arguments : uint16_t pipe ; Pipe Number
2685 * Return Value : none
2686 *******************************************************************************/
2687 void usb1_function_stop_transfer (uint16_t pipe
)
2693 usb1_function_set_pid_nak(pipe
);
2695 usefifo
= (uint16_t)(g_usb1_function_PipeTbl
[pipe
] & USB_FUNCTION_FIFO_USE
);
2698 case USB_FUNCTION_D0FIFO_USE
:
2699 usb1_function_clear_transaction_counter(pipe
);
2700 USB201
.D0FIFOCTR
= USB_FUNCTION_BITBCLR
; /* Buffer Clear */
2701 fifo
= USB_FUNCTION_D0USE
;
2704 case USB_FUNCTION_D1FIFO_USE
:
2705 usb1_function_clear_transaction_counter(pipe
);
2706 USB201
.D1FIFOCTR
= USB_FUNCTION_BITBCLR
; /* Buffer Clear */
2707 fifo
= USB_FUNCTION_D1USE
;
2710 case USB_FUNCTION_D0FIFO_DMA
:
2711 remain
= Userdef_USB_usb1_function_stop_dma0();
2712 usb1_function_dma_stop_d0(pipe
, remain
);
2713 usb1_function_clear_transaction_counter(pipe
);
2714 USB201
.D0FIFOCTR
= USB_FUNCTION_BITBCLR
; /* Buffer Clear */
2715 fifo
= USB_FUNCTION_D0DMA
;
2718 case USB_FUNCTION_D1FIFO_DMA
:
2719 remain
= Userdef_USB_usb1_function_stop_dma1();
2720 usb1_function_dma_stop_d1(pipe
, remain
);
2721 usb1_function_clear_transaction_counter(pipe
);
2722 USB201
.D1FIFOCTR
= USB_FUNCTION_BITBCLR
; /* Buffer Clear */
2723 fifo
= USB_FUNCTION_D1DMA
;
2727 usb1_function_clear_transaction_counter(pipe
);
2728 USB201
.CFIFOCTR
= USB_FUNCTION_BITBCLR
; /* Buffer Clear */
2729 fifo
= USB_FUNCTION_CUSE
;
2733 usb1_function_set_curpipe(USB_FUNCTION_PIPE0
, fifo
, DEVDRV_USBF_NO
, USB_FUNCTION_BITMBW_16
);
2735 /* Interrupt of pipe set is disabled */
2736 usb1_function_disable_brdy_int(pipe
);
2737 usb1_function_disable_nrdy_int(pipe
);
2738 usb1_function_disable_bemp_int(pipe
);
2740 usb1_function_aclrm(pipe
);
2741 usb1_function_set_csclr(pipe
);
2743 if ( g_usb1_function_pipe_status
[pipe
] == DEVDRV_USBF_PIPE_WAIT
)
2745 g_usb1_function_pipe_status
[pipe
] = DEVDRV_USBF_PIPE_NORES
;
2749 /*******************************************************************************
2750 * Function Name: usb1_function_set_dfacc_d0
2751 * Description : Sets the DFACC setting value in D0FIFO using the transfer size.
2752 * Arguments : uint16_t mbw ; MBW
2753 * : uint16_t count ; data count
2754 * Return Value : DFACC Access mode
2755 *******************************************************************************/
2756 static uint16_t usb1_function_set_dfacc_d0 (uint16_t mbw
, uint32_t count
)
2760 #ifndef __USB_FUNCTION_DF_ACC_ENABLE__
2761 RZA_IO_RegWrite_16(&USB201
.D0FBCFG
,
2763 USB_DnFBCFG_DFACC_SHIFT
,
2765 RZA_IO_RegWrite_16(&USB201
.D0FBCFG
,
2767 USB_DnFBCFG_TENDE_SHIFT
,
2771 if (mbw
== USB_FUNCTION_BITMBW_32
)
2773 if ((count
% 32) == 0)
2775 /* 32byte transfer */
2776 RZA_IO_RegWrite_16(&USB201
.D0FBCFG
,
2778 USB_DnFBCFG_DFACC_SHIFT
,
2780 RZA_IO_RegWrite_16(&USB201
.D0FBCFG
,
2782 USB_DnFBCFG_TENDE_SHIFT
,
2786 else if ((count
% 16) == 0)
2788 /* 16byte transfer */
2789 RZA_IO_RegWrite_16(&USB201
.D0FBCFG
,
2791 USB_DnFBCFG_DFACC_SHIFT
,
2793 RZA_IO_RegWrite_16(&USB201
.D0FBCFG
,
2795 USB_DnFBCFG_TENDE_SHIFT
,
2801 RZA_IO_RegWrite_16(&USB201
.D0FBCFG
,
2803 USB_DnFBCFG_DFACC_SHIFT
,
2805 RZA_IO_RegWrite_16(&USB201
.D0FBCFG
,
2807 USB_DnFBCFG_TENDE_SHIFT
,
2812 else if (mbw
== USB_FUNCTION_BITMBW_16
)
2814 RZA_IO_RegWrite_16(&USB201
.D0FBCFG
,
2816 USB_DnFBCFG_DFACC_SHIFT
,
2818 RZA_IO_RegWrite_16(&USB201
.D0FBCFG
,
2820 USB_DnFBCFG_TENDE_SHIFT
,
2826 RZA_IO_RegWrite_16(&USB201
.D0FBCFG
,
2828 USB_DnFBCFG_DFACC_SHIFT
,
2830 RZA_IO_RegWrite_16(&USB201
.D0FBCFG
,
2832 USB_DnFBCFG_TENDE_SHIFT
,
2840 /*******************************************************************************
2841 * Function Name: usb1_function_set_dfacc_d1
2842 * Description : Set the DFACC setting value in D1FIFO using the transfer size.
2843 * Arguments : uint16_t mbw ; MBW
2844 * : uint16_t count ; data count
2845 * Return Value : DFACC Access mode
2846 *******************************************************************************/
2847 static uint16_t usb1_function_set_dfacc_d1 (uint16_t mbw
, uint32_t count
)
2851 #ifndef __USB_FUNCTION_DF_ACC_ENABLE__
2852 RZA_IO_RegWrite_16(&USB201
.D1FBCFG
,
2854 USB_DnFBCFG_DFACC_SHIFT
,
2856 RZA_IO_RegWrite_16(&USB201
.D1FBCFG
,
2858 USB_DnFBCFG_TENDE_SHIFT
,
2862 if (mbw
== USB_FUNCTION_BITMBW_32
)
2864 if ((count
% 32) == 0)
2866 /* 32byte transfer */
2867 RZA_IO_RegWrite_16(&USB201
.D1FBCFG
,
2869 USB_DnFBCFG_DFACC_SHIFT
,
2871 RZA_IO_RegWrite_16(&USB201
.D1FBCFG
,
2873 USB_DnFBCFG_TENDE_SHIFT
,
2877 else if ((count
% 16) == 0)
2879 /* 16byte transfer */
2880 RZA_IO_RegWrite_16(&USB201
.D1FBCFG
,
2882 USB_DnFBCFG_DFACC_SHIFT
,
2884 RZA_IO_RegWrite_16(&USB201
.D1FBCFG
,
2886 USB_DnFBCFG_TENDE_SHIFT
,
2892 RZA_IO_RegWrite_16(&USB201
.D1FBCFG
,
2894 USB_DnFBCFG_DFACC_SHIFT
,
2896 RZA_IO_RegWrite_16(&USB201
.D1FBCFG
,
2898 USB_DnFBCFG_TENDE_SHIFT
,
2903 else if (mbw
== USB_FUNCTION_BITMBW_16
)
2905 RZA_IO_RegWrite_16(&USB201
.D1FBCFG
,
2907 USB_DnFBCFG_DFACC_SHIFT
,
2909 RZA_IO_RegWrite_16(&USB201
.D1FBCFG
,
2911 USB_DnFBCFG_TENDE_SHIFT
,
2917 RZA_IO_RegWrite_16(&USB201
.D1FBCFG
,
2919 USB_DnFBCFG_DFACC_SHIFT
,
2921 RZA_IO_RegWrite_16(&USB201
.D1FBCFG
,
2923 USB_DnFBCFG_TENDE_SHIFT
,