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_host_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 *******************************************************************************/
42 #include "r_typedefs.h"
44 #include "devdrv_usb_host_api.h"
45 #include "usb0_host.h"
46 #include "MBRZA1H.h" /* INTC Driver Header */
47 #include "usb0_host_dmacdrv.h"
48 #include "ohci_wrapp_RZ_A1_local.h"
51 /*******************************************************************************
53 *******************************************************************************/
56 /*******************************************************************************
58 *******************************************************************************/
59 #define DUMMY_ACCESS OSTM0CNT
61 /* #define CACHE_WRITEBACK */
64 /*******************************************************************************
65 Imported global variables and functions (from other files)
66 *******************************************************************************/
67 extern int32_t io_cwb(unsigned long start
, unsigned long end
);
70 /*******************************************************************************
71 Exported global variables and functions (to be accessed by other files)
72 *******************************************************************************/
73 static void usb0_host_enable_dmac0(uint32_t src
, uint32_t dst
, uint32_t count
,
74 uint32_t size
, uint32_t dir
, uint32_t fifo
, uint16_t dfacc
);
75 static void usb0_host_enable_dmac1(uint32_t src
, uint32_t dst
, uint32_t count
,
76 uint32_t size
, uint32_t dir
, uint32_t fifo
, uint16_t dfacc
);
77 static void Userdef_USB_usb0_host_delay_10us_2(void);
80 /*******************************************************************************
81 Private global variables and functions
82 *******************************************************************************/
85 /*******************************************************************************
86 * Function Name: Userdef_USB_usb0_host_d0fifo_dmaintid
87 * Description : get D0FIFO DMA Interrupt ID
89 * Return Value : D0FIFO DMA Interrupt ID
90 *******************************************************************************/
91 uint16_t Userdef_USB_usb0_host_d0fifo_dmaintid (void)
93 #if(1) /* ohci_wrapp */
100 /*******************************************************************************
101 * Function Name: Userdef_USB_usb0_host_d1fifo_dmaintid
102 * Description : get D1FIFO DMA Interrupt ID
104 * Return Value : D1FIFO DMA Interrupt ID
105 *******************************************************************************/
106 uint16_t Userdef_USB_usb0_host_d1fifo_dmaintid (void)
108 #if(1) /* ohci_wrapp */
115 /*******************************************************************************
116 * Function Name: Userdef_USB_usb0_host_attach
117 * Description : Wait for the software of 1ms.
118 * : Alter this function according to the user's system.
120 * Return Value : none
121 *******************************************************************************/
122 void Userdef_USB_usb0_host_attach (void)
125 // printf("channel 0 attach device\n");
127 ohciwrapp_loc_Connect(1);
130 /*******************************************************************************
131 * Function Name: Userdef_USB_usb0_host_detach
132 * Description : Wait for the software of 1ms.
133 * : Alter this function according to the user's system.
135 * Return Value : none
136 *******************************************************************************/
137 void Userdef_USB_usb0_host_detach (void)
140 // printf("channel 0 detach device\n");
142 ohciwrapp_loc_Connect(0);
145 /*******************************************************************************
146 * Function Name: Userdef_USB_usb0_host_delay_1ms
147 * Description : Wait for the software of 1ms.
148 * : Alter this function according to the user's system.
150 * Return Value : none
151 *******************************************************************************/
152 void Userdef_USB_usb0_host_delay_1ms (void)
157 /*******************************************************************************
158 * Function Name: Userdef_USB_usb0_host_delay_xms
159 * Description : Wait for the software in the period of time specified by the
161 * : Alter this function according to the user's system.
162 * Arguments : uint32_t msec ; Wait Time (msec)
163 * Return Value : none
164 *******************************************************************************/
165 void Userdef_USB_usb0_host_delay_xms (uint32_t msec
)
170 /*******************************************************************************
171 * Function Name: Userdef_USB_usb0_host_delay_10us
172 * Description : Waits for software for the period specified by the argument.
173 * : Alter this function according to the user's system.
174 * Arguments : uint32_t usec ; Wait Time(x 10usec)
175 * Return Value : none
176 *******************************************************************************/
177 void Userdef_USB_usb0_host_delay_10us (uint32_t usec
)
181 /* Wait 10us (Please change for your MCU) */
182 for (i
= 0; i
< usec
; ++i
)
184 Userdef_USB_usb0_host_delay_10us_2();
188 /*******************************************************************************
189 * Function Name: Userdef_USB_usb0_host_delay_10us_2
190 * Description : Waits for software for the period specified by the argument.
191 * : Alter this function according to the user's system.
193 * Return Value : none
194 *******************************************************************************/
195 static void Userdef_USB_usb0_host_delay_10us_2 (void)
198 volatile unsigned long tmp
;
200 /* Wait 1us (Please change for your MCU) */
201 for (i
= 0; i
< 14; ++i
)
207 /*******************************************************************************
208 * Function Name: Userdef_USB_usb0_host_delay_500ns
209 * Description : Wait for software for 500ns.
210 * : Alter this function according to the user's system.
212 * Return Value : none
213 *******************************************************************************/
214 void Userdef_USB_usb0_host_delay_500ns (void)
217 volatile unsigned long tmp
;
219 /* Wait 500ns (Please change for your MCU) */
220 /* Wait 500ns I clock 266MHz */
224 /*******************************************************************************
225 * Function Name: Userdef_USB_usb0_host_start_dma
226 * Description : Enables DMA transfer on the information specified by the argument.
227 * : Set DMAC register by this function to enable DMA transfer.
228 * : After executing this function, USB module is set to start DMA
229 * : transfer. DMA transfer should not wait for DMA transfer complete.
230 * Arguments : USB_HOST_DMA_t *dma : DMA parameter
232 * : uint32_t fifo; FIFO for using
233 * : uint32_t buffer; Start address of transfer source/destination
234 * : uint32_t bytes; Transfer size(Byte)
235 * : uint32_t dir; Transfer direction(0:Buffer->FIFO, 1:FIFO->Buffer)
236 * : uint32_t size; DMA transfer size
237 * : } USB_HOST_DMA_t;
238 * : uint16_t dfacc ; 0 : cycle steal mode
239 * : 1 : 16byte continuous mode
240 * : 2 : 32byte continuous mode
241 * Return Value : none
242 *******************************************************************************/
243 void Userdef_USB_usb0_host_start_dma (USB_HOST_DMA_t
* dma
, uint16_t dfacc
)
250 #ifdef CACHE_WRITEBACK
254 trncount
= dma
->bytes
;
257 if (dir
== USB_HOST_FIFO2BUF
)
259 /* DxFIFO determination */
261 #ifndef __USB_HOST_DF_ACC_ENABLE__
262 if (dma
->fifo
== USB_HOST_D0FIFO_DMA
)
264 src
= (uint32_t)(&USB200
.D0FIFO
.UINT32
);
268 src
= (uint32_t)(&USB200
.D1FIFO
.UINT32
);
274 src
+= 3; /* byte access */
278 src
+= 2; /* short access */
293 if (dma
->fifo
== USB_HOST_D0FIFO_DMA
)
295 src
= (uint32_t)(&USB200
.D0FIFOB0
);
299 src
= (uint32_t)(&USB200
.D1FIFOB0
);
305 if (dma
->fifo
== USB_HOST_D0FIFO_DMA
)
307 src
= (uint32_t)(&USB200
.D0FIFOB0
);
311 src
= (uint32_t)(&USB200
.D1FIFOB0
);
317 if (dma
->fifo
== USB_HOST_D0FIFO_DMA
)
319 src
= (uint32_t)(&USB200
.D0FIFO
.UINT32
);
323 src
= (uint32_t)(&USB200
.D1FIFO
.UINT32
);
330 dfacc
= 0; /* force normal access */
332 if (dma
->fifo
== USB_HOST_D0FIFO_DMA
)
334 src
= (uint32_t)(&USB200
.D0FIFO
.UINT32
);
338 src
= (uint32_t)(&USB200
.D1FIFO
.UINT32
);
340 src
+= 2; /* short access */
345 dfacc
= 0; /* force normal access */
346 if (dma
->fifo
== USB_HOST_D0FIFO_DMA
)
348 src
= (uint32_t)(&USB200
.D0FIFO
.UINT32
);
352 src
= (uint32_t)(&USB200
.D1FIFO
.UINT32
);
354 src
+= 3; /* byte access */
360 /* DxFIFO determination */
362 #ifndef __USB_HOST_DF_ACC_ENABLE__
363 if (dma
->fifo
== USB_HOST_D0FIFO_DMA
)
365 dst
= (uint32_t)(&USB200
.D0FIFO
.UINT32
);
369 dst
= (uint32_t)(&USB200
.D1FIFO
.UINT32
);
375 dst
+= 3; /* byte access */
379 dst
+= 2; /* short access */
393 if (dma
->fifo
== USB_HOST_D0FIFO_DMA
)
395 dst
= (uint32_t)(&USB200
.D0FIFOB0
);
399 dst
= (uint32_t)(&USB200
.D1FIFOB0
);
405 if (dma
->fifo
== USB_HOST_D0FIFO_DMA
)
407 dst
= (uint32_t)(&USB200
.D0FIFOB0
);
411 dst
= (uint32_t)(&USB200
.D1FIFOB0
);
417 if (dma
->fifo
== USB_HOST_D0FIFO_DMA
)
419 dst
= (uint32_t)(&USB200
.D0FIFO
.UINT32
);
423 dst
= (uint32_t)(&USB200
.D1FIFO
.UINT32
);
430 dfacc
= 0; /* force normal access */
431 if (dma
->fifo
== USB_HOST_D0FIFO_DMA
)
433 dst
= (uint32_t)(&USB200
.D0FIFO
.UINT32
);
437 dst
= (uint32_t)(&USB200
.D1FIFO
.UINT32
);
439 dst
+= 2; /* short access */
444 dfacc
= 0; /* force normal access */
445 if (dma
->fifo
== USB_HOST_D0FIFO_DMA
)
447 dst
= (uint32_t)(&USB200
.D0FIFO
.UINT32
);
451 dst
= (uint32_t)(&USB200
.D1FIFO
.UINT32
);
453 dst
+= 3; /* byte access */
458 #ifdef CACHE_WRITEBACK
459 ptr
= (uint32_t)dma
->buffer
;
460 if ((ptr
& 0x20000000ul
) == 0)
462 io_cwb((uint32_t)ptr
,(uint32_t)(ptr
)+trncount
);
466 if (dma
->fifo
== USB_HOST_D0FIFO_DMA
)
468 usb0_host_enable_dmac0(src
, dst
, trncount
, size
, dir
, dma
->fifo
, dfacc
);
472 usb0_host_enable_dmac1(src
, dst
, trncount
, size
, dir
, dma
->fifo
, dfacc
);
476 /*******************************************************************************
477 * Function Name: usb0_host_enable_dmac0
478 * Description : Enables DMA transfer on the information specified by the argument.
479 * Arguments : uint32_t src : src address
480 * : uint32_t dst : dst address
481 * : uint32_t count : transfer byte
482 * : uint32_t size : transfer size
483 * : uint32_t dir : direction
484 * : uint32_t fifo : FIFO(D0FIFO or D1FIFO)
485 * : uint16_t dfacc : 0 : normal access
486 * : : 1 : 16byte access
487 * : : 2 : 32byte access
488 * Return Value : none
489 *******************************************************************************/
490 static void usb0_host_enable_dmac0 (uint32_t src
, uint32_t dst
, uint32_t count
,
491 uint32_t size
, uint32_t dir
, uint32_t fifo
, uint16_t dfacc
)
493 dmac_transinfo_t trans_info
;
494 uint32_t request_factor
= 0;
497 /* ==== Variable setting for DMAC initialization ==== */
498 trans_info
.src_addr
= (uint32_t)src
; /* Start address of transfer source */
499 trans_info
.dst_addr
= (uint32_t)dst
; /* Start address of transfer destination */
500 trans_info
.count
= (uint32_t)count
; /* Total byte count to be transferred */
501 #ifndef __USB_HOST_DF_ACC_ENABLE__
504 trans_info
.src_size
= DMAC_TRANS_SIZE_8
; /* Transfer source transfer size */
505 trans_info
.dst_size
= DMAC_TRANS_SIZE_8
; /* Transfer destination transfer size */
509 trans_info
.src_size
= DMAC_TRANS_SIZE_16
; /* Transfer source transfer size */
510 trans_info
.dst_size
= DMAC_TRANS_SIZE_16
; /* Transfer destination transfer size */
514 trans_info
.src_size
= DMAC_TRANS_SIZE_32
; /* Transfer source transfer size */
515 trans_info
.dst_size
= DMAC_TRANS_SIZE_32
; /* Transfer destination transfer size */
519 // printf("size error!!\n");
525 trans_info
.src_size
= DMAC_TRANS_SIZE_256
; /* Transfer source transfer size */
526 trans_info
.dst_size
= DMAC_TRANS_SIZE_256
; /* Transfer destination transfer size */
531 trans_info
.src_size
= DMAC_TRANS_SIZE_128
; /* Transfer source transfer size */
532 trans_info
.dst_size
= DMAC_TRANS_SIZE_128
; /* Transfer destination transfer size */
539 trans_info
.src_size
= DMAC_TRANS_SIZE_8
; /* Transfer source transfer size */
540 trans_info
.dst_size
= DMAC_TRANS_SIZE_8
; /* Transfer destination transfer size */
544 trans_info
.src_size
= DMAC_TRANS_SIZE_16
; /* Transfer source transfer size */
545 trans_info
.dst_size
= DMAC_TRANS_SIZE_16
; /* Transfer destination transfer size */
549 trans_info
.src_size
= DMAC_TRANS_SIZE_32
; /* Transfer source transfer size */
550 trans_info
.dst_size
= DMAC_TRANS_SIZE_32
; /* Transfer destination transfer size */
554 // printf("size error!!\n");
559 if (dir
== USB_HOST_FIFO2BUF
)
561 request_factor
= DMAC_REQ_USB0_DMA0_RX
; /* USB_0 channel 0 receive FIFO full */
562 trans_info
.saddr_dir
= DMAC_TRANS_ADR_NO_INC
; /* Count direction of transfer source address */
563 trans_info
.daddr_dir
= DMAC_TRANS_ADR_INC
; /* Count direction of transfer destination address */
565 else if (dir
== USB_HOST_BUF2FIFO
)
567 request_factor
= DMAC_REQ_USB0_DMA0_TX
; /* USB_0 channel 0 receive FIFO empty */
568 trans_info
.saddr_dir
= DMAC_TRANS_ADR_INC
; /* Count direction of transfer source address */
569 trans_info
.daddr_dir
= DMAC_TRANS_ADR_NO_INC
; /* Count direction of transfer destination address */
576 /* ==== DMAC initialization ==== */
577 usb0_host_DMAC1_PeriReqInit((const dmac_transinfo_t
*)&trans_info
,
581 0); /* Don't care DMAC_REQ_REQD is setting in usb0_host_DMAC1_PeriReqInit() */
583 /* ==== DMAC startup ==== */
584 ret
= usb0_host_DMAC1_Open(DMAC_REQ_MODE_PERI
);
588 // printf("DMAC1 Open error!!\n");
594 /*******************************************************************************
595 * Function Name: usb0_host_enable_dmac1
596 * Description : Enables DMA transfer on the information specified by the argument.
597 * Arguments : uint32_t src : src address
598 * : uint32_t dst : dst address
599 * : uint32_t count : transfer byte
600 * : uint32_t size : transfer size
601 * : uint32_t dir : direction
602 * : uint32_t fifo : FIFO(D0FIFO or D1FIFO)
603 * : uint16_t dfacc : 0 : normal access
604 * : : 1 : 16byte access
605 * : : 2 : 32byte access
606 * Return Value : none
607 *******************************************************************************/
608 static void usb0_host_enable_dmac1 (uint32_t src
, uint32_t dst
, uint32_t count
,
609 uint32_t size
, uint32_t dir
, uint32_t fifo
, uint16_t dfacc
)
611 dmac_transinfo_t trans_info
;
612 uint32_t request_factor
= 0;
615 /* ==== Variable setting for DMAC initialization ==== */
616 trans_info
.src_addr
= (uint32_t)src
; /* Start address of transfer source */
617 trans_info
.dst_addr
= (uint32_t)dst
; /* Start address of transfer destination */
618 trans_info
.count
= (uint32_t)count
; /* Total byte count to be transferred */
619 #ifndef __USB_HOST_DF_ACC_ENABLE__
622 trans_info
.src_size
= DMAC_TRANS_SIZE_8
; /* Transfer source transfer size */
623 trans_info
.dst_size
= DMAC_TRANS_SIZE_8
; /* Transfer destination transfer size */
627 trans_info
.src_size
= DMAC_TRANS_SIZE_16
; /* Transfer source transfer size */
628 trans_info
.dst_size
= DMAC_TRANS_SIZE_16
; /* Transfer destination transfer size */
632 trans_info
.src_size
= DMAC_TRANS_SIZE_32
; /* Transfer source transfer size */
633 trans_info
.dst_size
= DMAC_TRANS_SIZE_32
; /* Transfer destination transfer size */
637 // printf("size error!!\n");
643 trans_info
.src_size
= DMAC_TRANS_SIZE_256
; /* Transfer source transfer size */
644 trans_info
.dst_size
= DMAC_TRANS_SIZE_256
; /* Transfer destination transfer size */
649 trans_info
.src_size
= DMAC_TRANS_SIZE_128
; /* Transfer source transfer size */
650 trans_info
.dst_size
= DMAC_TRANS_SIZE_128
; /* Transfer destination transfer size */
657 trans_info
.src_size
= DMAC_TRANS_SIZE_8
; /* Transfer source transfer size */
658 trans_info
.dst_size
= DMAC_TRANS_SIZE_8
; /* Transfer destination transfer size */
662 trans_info
.src_size
= DMAC_TRANS_SIZE_16
; /* Transfer source transfer size */
663 trans_info
.dst_size
= DMAC_TRANS_SIZE_16
; /* Transfer destination transfer size */
667 trans_info
.src_size
= DMAC_TRANS_SIZE_32
; /* Transfer source transfer size */
668 trans_info
.dst_size
= DMAC_TRANS_SIZE_32
; /* Transfer destination transfer size */
672 // printf("size error!!\n");
677 if (dir
== USB_HOST_FIFO2BUF
)
679 request_factor
=DMAC_REQ_USB0_DMA1_RX
; /* USB_0 channel 0 receive FIFO full */
680 trans_info
.saddr_dir
= DMAC_TRANS_ADR_NO_INC
; /* Count direction of transfer source address */
681 trans_info
.daddr_dir
= DMAC_TRANS_ADR_INC
; /* Count direction of transfer destination address */
683 else if (dir
== USB_HOST_BUF2FIFO
)
685 request_factor
=DMAC_REQ_USB0_DMA1_TX
; /* USB_0 channel 0 receive FIFO empty */
686 trans_info
.saddr_dir
= DMAC_TRANS_ADR_INC
; /* Count direction of transfer source address */
687 trans_info
.daddr_dir
= DMAC_TRANS_ADR_NO_INC
; /* Count direction of transfer destination address */
694 /* ==== DMAC initialization ==== */
695 usb0_host_DMAC2_PeriReqInit((const dmac_transinfo_t
*)&trans_info
,
699 0); /* Don't care DMAC_REQ_REQD is setting in usb0_host_DMAC2_PeriReqInit() */
701 /* ==== DMAC startup ==== */
702 ret
= usb0_host_DMAC2_Open(DMAC_REQ_MODE_PERI
);
706 // printf("DMAC2 Open error!!\n");
712 /*******************************************************************************
713 * Function Name: Userdef_USB_usb0_host_stop_dma0
714 * Description : Disables DMA transfer.
716 * Return Value : uint32_t return Transfer Counter register(DMATCRn) value
717 * : regarding to the bus width.
718 * Notice : This function should be executed to DMAC executed at the time
719 * : of specification of D0_FIF0_DMA in dma->fifo.
720 *******************************************************************************/
721 uint32_t Userdef_USB_usb0_host_stop_dma0 (void)
725 /* ==== DMAC release ==== */
726 usb0_host_DMAC1_Close(&remain
);
731 /*******************************************************************************
732 * Function Name: Userdef_USB_usb0_host_stop_dma1
733 * Description : Disables DMA transfer.
734 * : This function should be executed to DMAC executed at the time
735 * : of specification of D1_FIF0_DMA in dma->fifo.
737 * Return Value : uint32_t return Transfer Counter register(DMATCRn) value
738 * : regarding to the bus width.
739 *******************************************************************************/
740 uint32_t Userdef_USB_usb0_host_stop_dma1 (void)
744 /* ==== DMAC release ==== */
745 usb0_host_DMAC2_Close(&remain
);
750 /*******************************************************************************
751 * Function Name: Userdef_USB_usb0_host_notice
752 * Description : Notice of USER
753 * Arguments : const char *format
754 * Return Value : none
755 *******************************************************************************/
756 void Userdef_USB_usb0_host_notice (const char * format
)
763 /*******************************************************************************
764 * Function Name: Userdef_USB_usb0_host_user_rdy
765 * Description : This function notify a user and wait for trigger
766 * Arguments : const char *format
768 * Return Value : none
769 *******************************************************************************/
770 void Userdef_USB_usb0_host_user_rdy (const char * format
, uint16_t data
)
772 // printf(format, data);