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 : usb0_function_userdef.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 *******************************************************************************/
41 #include "r_typedefs.h"
43 #include "devdrv_usb_function_api.h"
44 #include "usb0_function_dmacdrv.h" /* common DMAC driver for USB */
47 /*******************************************************************************
49 *******************************************************************************/
52 /*******************************************************************************
54 *******************************************************************************/
55 #define DUMMY_ACCESS OSTM0CNT
57 /* #define CACHE_WRITEBACK */
60 /*******************************************************************************
61 Imported global variables and functions (from other files)
62 *******************************************************************************/
63 extern int32_t io_cwb(unsigned long start
, unsigned long end
);
66 /*******************************************************************************
67 Exported global variables and functions (to be accessed by other files)
68 *******************************************************************************/
69 static void usb0_function_enable_dmac0(uint32_t src
, uint32_t dst
, uint32_t count
,
70 uint32_t size
, uint32_t dir
, uint32_t fifo
, uint16_t dfacc
);
71 static void usb0_function_enable_dmac1(uint32_t src
, uint32_t dst
, uint32_t count
,
72 uint32_t size
, uint32_t dir
, uint32_t fifo
, uint16_t dfacc
);
73 static void Userdef_USB_usb0_function_delay_10us_2(void);
76 /*******************************************************************************
77 Private global variables and functions
78 *******************************************************************************/
81 /*******************************************************************************
82 * Function Name: Userdef_USB_usb0_function_d0fifo_dmaintid
83 * Description : get D0FIFO DMA Interrupt ID
85 * Return Value : D0FIFO DMA Interrupt ID
86 *******************************************************************************/
87 IRQn_Type
Userdef_USB_usb0_function_d0fifo_dmaintid (void)
96 /*******************************************************************************
97 * Function Name: Userdef_USB_usb0_function_d1fifo_dmaintid
98 * Description : get D1FIFO DMA Interrupt ID
100 * Return Value : D1FIFO DMA Interrupt ID
101 *******************************************************************************/
102 IRQn_Type
Userdef_USB_usb0_function_d1fifo_dmaintid (void)
111 /*******************************************************************************
112 * Function Name: Userdef_USB_usb0_function_attach
113 * Description : Wait for the software of 1ms.
114 * : Alter this function according to the user's system.
116 * Return Value : none
117 *******************************************************************************/
118 void Userdef_USB_usb0_function_attach (void)
121 printf("channel 0 attach device\n");
125 /*******************************************************************************
126 * Function Name: Userdef_USB_usb0_function_detach
127 * Description : Wait for the software of 1ms.
128 * : Alter this function according to the user's system.
130 * Return Value : none
131 *******************************************************************************/
132 void Userdef_USB_usb0_function_detach (void)
135 printf("channel 0 detach device\n");
139 /*******************************************************************************
140 * Function Name: Userdef_USB_usb0_function_delay_1ms
141 * Description : Wait for the software of 1ms.
142 * : Alter this function according to the user's system.
144 * Return Value : none
145 *******************************************************************************/
146 void Userdef_USB_usb0_function_delay_1ms (void)
149 volatile unsigned long tmp
;
152 * Wait 1ms (Please change for your MCU).
154 for (i
= 0; i
< 1440; ++i
)
160 /*******************************************************************************
161 * Function Name: Userdef_USB_usb0_function_delay_xms
162 * Description : Wait for the software in the period of time specified by the
164 * : Alter this function according to the user's system.
165 * Arguments : uint32_t msec ; Wait Time (msec)
166 * Return Value : none
167 *******************************************************************************/
168 void Userdef_USB_usb0_function_delay_xms (uint32_t msec
)
170 volatile unsigned short i
;
172 for (i
= 0; i
< msec
; ++i
)
174 Userdef_USB_usb0_function_delay_1ms();
178 /*******************************************************************************
179 * Function Name: Userdef_USB_usb0_function_delay_10us
180 * Description : Waits for software for the period specified by the argument.
181 * : Alter this function according to the user's system.
182 * Arguments : uint32_t usec ; Wait Time(x 10usec)
183 * Return Value : none
184 *******************************************************************************/
185 void Userdef_USB_usb0_function_delay_10us (uint32_t usec
)
189 /* Wait 10us (Please change for your MCU) */
190 for (i
= 0; i
< usec
; ++i
)
192 Userdef_USB_usb0_function_delay_10us_2();
196 /*******************************************************************************
197 * Function Name: Userdef_USB_usb0_function_delay_10us_2
198 * Description : Waits for software for the period specified by the argument.
199 * : Alter this function according to the user's system.
201 * Return Value : none
202 *******************************************************************************/
203 static void Userdef_USB_usb0_function_delay_10us_2 (void)
206 volatile unsigned long tmp
;
208 /* Wait 1us (Please change for your MCU) */
209 for (i
= 0; i
< 14; ++i
)
215 /*******************************************************************************
216 * Function Name: Userdef_USB_usb0_function_delay_500ns
217 * Description : Wait for software for 500ns.
218 * : Alter this function according to the user's system.
220 * Return Value : none
221 *******************************************************************************/
222 void Userdef_USB_usb0_function_delay_500ns (void)
225 volatile unsigned long tmp
;
227 /* Wait 500ns (Please change for your MCU) */
228 /* Wait 500ns I clock 266MHz */
232 /*******************************************************************************
233 * Function Name: Userdef_USB_usb0_function_start_dma
234 * Description : Enables DMA transfer on the information specified by the argument.
235 * : Set DMAC register by this function to enable DMA transfer.
236 * : After executing this function, USB module is set to start DMA
237 * : transfer. DMA transfer should not wait for DMA transfer complete.
238 * Arguments : USB_FUNCTION_DMA_t *dma : DMA parameter
240 * : uint32_t fifo; FIFO for using
241 * : uint32_t buffer; Start address of transfer source/destination
242 * : uint32_t bytes; Transfer size(Byte)
243 * : uint32_t dir; Transfer direction(0:Buffer->FIFO, 1:FIFO->Buffer)
244 * : uint32_t size; DMA transfer size
245 * : } USB_FUNCTION_DMA_t;
246 * : uint16_t dfacc ; 0 : cycle steal mode
247 * : 1 : 16byte continuous mode
248 * : 2 : 32byte continuous mode
249 * Return Value : none
250 *******************************************************************************/
251 void Userdef_USB_usb0_function_start_dma (USB_FUNCTION_DMA_t
* dma
, uint16_t dfacc
)
258 #ifdef CACHE_WRITEBACK
262 trncount
= dma
->bytes
;
265 if (dir
== USB_FUNCTION_FIFO2BUF
)
267 /* DxFIFO determination */
269 #ifndef __USB_FUNCTION_DF_ACC_ENABLE__
270 if (dma
->fifo
== USB_FUNCTION_D0FIFO_DMA
)
272 src
= (uint32_t)(&USB200
.D0FIFO
.UINT32
);
276 src
= (uint32_t)(&USB200
.D1FIFO
.UINT32
);
282 src
+= 3; /* byte access */
286 src
+= 2; /* short access */
301 if (dma
->fifo
== USB_FUNCTION_D0FIFO_DMA
)
303 src
= (uint32_t)(&USB200
.D0FIFOB0
);
307 src
= (uint32_t)(&USB200
.D1FIFOB0
);
313 if (dma
->fifo
== USB_FUNCTION_D0FIFO_DMA
)
315 src
= (uint32_t)(&USB200
.D0FIFOB0
);
319 src
= (uint32_t)(&USB200
.D1FIFOB0
);
325 if (dma
->fifo
== USB_FUNCTION_D0FIFO_DMA
)
327 src
= (uint32_t)(&USB200
.D0FIFO
.UINT32
);
331 src
= (uint32_t)(&USB200
.D1FIFO
.UINT32
);
338 dfacc
= 0; /* force normal access */
340 if (dma
->fifo
== USB_FUNCTION_D0FIFO_DMA
)
342 src
= (uint32_t)(&USB200
.D0FIFO
.UINT32
);
346 src
= (uint32_t)(&USB200
.D1FIFO
.UINT32
);
348 src
+= 2; /* short access */
353 dfacc
= 0; /* force normal access */
355 if (dma
->fifo
== USB_FUNCTION_D0FIFO_DMA
)
357 src
= (uint32_t)(&USB200
.D0FIFO
.UINT32
);
361 src
= (uint32_t)(&USB200
.D1FIFO
.UINT32
);
363 src
+= 3; /* byte access */
369 /* DxFIFO determination */
371 #ifndef __USB_FUNCTION_DF_ACC_ENABLE__
372 if (dma
->fifo
== USB_FUNCTION_D0FIFO_DMA
)
374 dst
= (uint32_t)(&USB200
.D0FIFO
.UINT32
);
378 dst
= (uint32_t)(&USB200
.D1FIFO
.UINT32
);
384 dst
+= 3; /* byte access */
388 dst
+= 2; /* short access */
403 if (dma
->fifo
== USB_FUNCTION_D0FIFO_DMA
)
405 dst
= (uint32_t)(&USB200
.D0FIFOB0
);
409 dst
= (uint32_t)(&USB200
.D1FIFOB0
);
415 if (dma
->fifo
== USB_FUNCTION_D0FIFO_DMA
)
417 dst
= (uint32_t)(&USB200
.D0FIFOB0
);
421 dst
= (uint32_t)(&USB200
.D1FIFOB0
);
427 if (dma
->fifo
== USB_FUNCTION_D0FIFO_DMA
)
429 dst
= (uint32_t)(&USB200
.D0FIFO
.UINT32
);
433 dst
= (uint32_t)(&USB200
.D1FIFO
.UINT32
);
440 dfacc
= 0; /* force normal access */
441 if (dma
->fifo
== USB_FUNCTION_D0FIFO_DMA
)
443 dst
= (uint32_t)(&USB200
.D0FIFO
.UINT32
);
447 dst
= (uint32_t)(&USB200
.D1FIFO
.UINT32
);
449 dst
+= 2; /* short access */
454 dfacc
= 0; /* force normal access */
456 if (dma
->fifo
== USB_FUNCTION_D0FIFO_DMA
)
458 dst
= (uint32_t)(&USB200
.D0FIFO
.UINT32
);
462 dst
= (uint32_t)(&USB200
.D1FIFO
.UINT32
);
464 dst
+= 3; /* byte access */
469 #ifdef CACHE_WRITEBACK
470 ptr
= (uint32_t)dma
->buffer
;
472 if ((ptr
& 0x20000000ul
) == 0)
474 io_cwb((uint32_t)ptr
, (uint32_t)(ptr
) + trncount
);
478 if (dma
->fifo
== USB_FUNCTION_D0FIFO_DMA
)
480 usb0_function_enable_dmac0(src
, dst
, trncount
, size
, dir
, dma
->fifo
, dfacc
);
484 usb0_function_enable_dmac1(src
, dst
, trncount
, size
, dir
, dma
->fifo
, dfacc
);
488 /*******************************************************************************
489 * Function Name: usb0_function_enable_dmac0
490 * Description : Enables DMA transfer on the information specified by the argument.
491 * Arguments : uint32_t src : src address
492 * : uint32_t dst : dst address
493 * : uint32_t count : transfer byte
494 * : uint32_t size : transfer size
495 * : uint32_t dir : direction
496 * : uint32_t fifo : FIFO(D0FIFO or D1FIFO)
497 * : uint16_t dfacc : 0 : normal access
498 * : : 1 : 16byte access
499 * : : 2 : 32byte access
500 * Return Value : none
501 *******************************************************************************/
502 static void usb0_function_enable_dmac0 (uint32_t src
, uint32_t dst
, uint32_t count
,
503 uint32_t size
, uint32_t dir
, uint32_t fifo
, uint16_t dfacc
)
505 dmac_transinfo_t trans_info
;
506 uint32_t request_factor
= 0;
509 /* ==== Variable setting for DMAC initialization ==== */
510 trans_info
.src_addr
= (uint32_t)src
; /* Start address of transfer source */
511 trans_info
.dst_addr
= (uint32_t)dst
; /* Start address of transfer destination */
512 trans_info
.count
= (uint32_t)count
; /* Total byte count to be transferred */
513 #ifndef __USB_FUNCTION_DF_ACC_ENABLE__
516 trans_info
.src_size
= DMAC_TRANS_SIZE_8
; /* Transfer source transfer size */
517 trans_info
.dst_size
= DMAC_TRANS_SIZE_8
; /* Transfer destination transfer size */
521 trans_info
.src_size
= DMAC_TRANS_SIZE_16
; /* Transfer source transfer size */
522 trans_info
.dst_size
= DMAC_TRANS_SIZE_16
; /* Transfer destination transfer size */
526 trans_info
.src_size
= DMAC_TRANS_SIZE_32
; /* Transfer source transfer size */
527 trans_info
.dst_size
= DMAC_TRANS_SIZE_32
; /* Transfer destination transfer size */
531 printf("size error!!\n");
537 trans_info
.src_size
= DMAC_TRANS_SIZE_256
; /* Transfer source transfer size */
538 trans_info
.dst_size
= DMAC_TRANS_SIZE_256
; /* Transfer destination transfer size */
543 trans_info
.src_size
= DMAC_TRANS_SIZE_128
; /* Transfer source transfer size */
544 trans_info
.dst_size
= DMAC_TRANS_SIZE_128
; /* Transfer destination transfer size */
551 trans_info
.src_size
= DMAC_TRANS_SIZE_8
; /* Transfer source transfer size */
552 trans_info
.dst_size
= DMAC_TRANS_SIZE_8
; /* Transfer destination transfer size */
556 trans_info
.src_size
= DMAC_TRANS_SIZE_16
; /* Transfer source transfer size */
557 trans_info
.dst_size
= DMAC_TRANS_SIZE_16
; /* Transfer destination transfer size */
561 trans_info
.src_size
= DMAC_TRANS_SIZE_32
; /* Transfer source transfer size */
562 trans_info
.dst_size
= DMAC_TRANS_SIZE_32
; /* Transfer destination transfer size */
566 printf("size error!!\n");
571 if (dir
== USB_FUNCTION_FIFO2BUF
)
573 request_factor
=DMAC_REQ_USB0_DMA0_RX
; /* USB_0 channel 0 receive FIFO full */
574 trans_info
.saddr_dir
= DMAC_TRANS_ADR_NO_INC
; /* Count direction of transfer source address */
575 trans_info
.daddr_dir
= DMAC_TRANS_ADR_INC
; /* Count direction of transfer destination address */
577 else if (dir
== USB_FUNCTION_BUF2FIFO
)
579 request_factor
=DMAC_REQ_USB0_DMA0_TX
; /* USB_0 channel 0 receive FIFO empty */
580 trans_info
.saddr_dir
= DMAC_TRANS_ADR_INC
; /* Count direction of transfer source address */
581 trans_info
.daddr_dir
= DMAC_TRANS_ADR_NO_INC
; /* Count direction of transfer destination address */
588 /* ==== DMAC initialization ==== */
589 usb0_function_DMAC1_PeriReqInit((const dmac_transinfo_t
*)&trans_info
,
593 0); /* Don't care DMAC_REQ_REQD is setting in
594 usb0_function_DMAC1_PeriReqInit() */
596 /* ==== DMAC startup ==== */
597 ret
= usb0_function_DMAC1_Open(DMAC_REQ_MODE_PERI
);
600 printf("DMAC1 Open error!!\n");
606 /*******************************************************************************
607 * Function Name: usb0_function_enable_dmac1
608 * Description : Enables DMA transfer on the information specified by the argument.
609 * Arguments : uint32_t src : src address
610 * : uint32_t dst : dst address
611 * : uint32_t count : transfer byte
612 * : uint32_t size : transfer size
613 * : uint32_t dir : direction
614 * : uint32_t fifo : FIFO(D0FIFO or D1FIFO)
615 * : uint16_t dfacc : 0 : normal access
616 * : : 1 : 16byte access
617 * : : 2 : 32byte access
618 * Return Value : none
619 *******************************************************************************/
620 static void usb0_function_enable_dmac1 (uint32_t src
, uint32_t dst
, uint32_t count
,
621 uint32_t size
, uint32_t dir
, uint32_t fifo
, uint16_t dfacc
)
623 dmac_transinfo_t trans_info
;
624 uint32_t request_factor
= 0;
627 /* ==== Variable setting for DMAC initialization ==== */
628 trans_info
.src_addr
= (uint32_t)src
; /* Start address of transfer source */
629 trans_info
.dst_addr
= (uint32_t)dst
; /* Start address of transfer destination */
630 trans_info
.count
= (uint32_t)count
; /* Total byte count to be transferred */
631 #ifndef __USB_FUNCTION_DF_ACC_ENABLE__
634 trans_info
.src_size
= DMAC_TRANS_SIZE_8
; /* Transfer source transfer size */
635 trans_info
.dst_size
= DMAC_TRANS_SIZE_8
; /* Transfer destination transfer size */
639 trans_info
.src_size
= DMAC_TRANS_SIZE_16
; /* Transfer source transfer size */
640 trans_info
.dst_size
= DMAC_TRANS_SIZE_16
; /* Transfer destination transfer size */
644 trans_info
.src_size
= DMAC_TRANS_SIZE_32
; /* Transfer source transfer size */
645 trans_info
.dst_size
= DMAC_TRANS_SIZE_32
; /* Transfer destination transfer size */
649 printf("size error!!\n");
655 trans_info
.src_size
= DMAC_TRANS_SIZE_256
; /* Transfer source transfer size */
656 trans_info
.dst_size
= DMAC_TRANS_SIZE_256
; /* Transfer destination transfer size */
661 trans_info
.src_size
= DMAC_TRANS_SIZE_128
; /* Transfer source transfer size */
662 trans_info
.dst_size
= DMAC_TRANS_SIZE_128
; /* Transfer destination transfer size */
669 trans_info
.src_size
= DMAC_TRANS_SIZE_8
; /* Transfer source transfer size */
670 trans_info
.dst_size
= DMAC_TRANS_SIZE_8
; /* Transfer destination transfer size */
674 trans_info
.src_size
= DMAC_TRANS_SIZE_16
; /* Transfer source transfer size */
675 trans_info
.dst_size
= DMAC_TRANS_SIZE_16
; /* Transfer destination transfer size */
679 trans_info
.src_size
= DMAC_TRANS_SIZE_32
; /* Transfer source transfer size */
680 trans_info
.dst_size
= DMAC_TRANS_SIZE_32
; /* Transfer destination transfer size */
684 printf("size error!!\n");
689 if (dir
== USB_FUNCTION_FIFO2BUF
)
691 request_factor
=DMAC_REQ_USB0_DMA1_RX
; /* USB_0 channel 0 receive FIFO full */
692 trans_info
.saddr_dir
= DMAC_TRANS_ADR_NO_INC
; /* Count direction of transfer source address */
693 trans_info
.daddr_dir
= DMAC_TRANS_ADR_INC
; /* Count direction of transfer destination address */
695 else if (dir
== USB_FUNCTION_BUF2FIFO
)
697 request_factor
=DMAC_REQ_USB0_DMA1_TX
; /* USB_0 channel 0 receive FIFO empty */
698 trans_info
.saddr_dir
= DMAC_TRANS_ADR_INC
; /* Count direction of transfer source address */
699 trans_info
.daddr_dir
= DMAC_TRANS_ADR_NO_INC
; /* Count direction of transfer destination address */
706 /* ==== DMAC initialization ==== */
707 usb0_function_DMAC2_PeriReqInit((const dmac_transinfo_t
*)&trans_info
,
711 0); /* Don't care DMAC_REQ_REQD is setting in
712 usb0_function_DMAC1_PeriReqInit() */
714 /* ==== DMAC startup ==== */
715 ret
= usb0_function_DMAC2_Open(DMAC_REQ_MODE_PERI
);
718 printf("DMAC2 Open error!!\n");
724 /*******************************************************************************
725 * Function Name: Userdef_USB_usb0_function_stop_dma0
726 * Description : Disables DMA transfer.
727 * : This function should be executed to DMAC executed at the time
728 * : of specification of D0_FIF0_DMA in dma->fifo.
730 * Return Value : uint32_t return Transfer Counter register(DMATCRn) value
731 * : regarding to the bus width.
732 *******************************************************************************/
733 uint32_t Userdef_USB_usb0_function_stop_dma0 (void)
737 /* ==== DMAC release ==== */
738 usb0_function_DMAC1_Close(&remain
);
743 /*******************************************************************************
744 * Function Name: Userdef_USB_usb0_function_stop_dma1
745 * Description : Disables DMA transfer.
746 * : This function should be executed to DMAC executed at the time
747 * : of specification of D1_FIF0_DMA in dma->fifo.
749 * Return Value : uint32_t return Transfer Counter register(DMATCRn) value
750 * : regarding to the bus width.
751 *******************************************************************************/
752 uint32_t Userdef_USB_usb0_function_stop_dma1 (void)
756 /* ==== DMAC release ==== */
757 usb0_function_DMAC2_Close(&remain
);