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_dma.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 "usb0_host.h"
41 /* #include "usb0_host_dmacdrv.h" */
44 /*******************************************************************************
46 *******************************************************************************/
49 /*******************************************************************************
51 *******************************************************************************/
54 /*******************************************************************************
55 Imported global variables and functions (from other files)
56 *******************************************************************************/
59 /*******************************************************************************
60 Exported global variables and functions (to be accessed by other files)
61 *******************************************************************************/
64 /*******************************************************************************
65 Private global variables and functions
66 *******************************************************************************/
67 static void usb0_host_dmaint(uint16_t fifo
);
68 static void usb0_host_dmaint_buf2fifo(uint16_t pipe
);
69 static void usb0_host_dmaint_fifo2buf(uint16_t pipe
);
72 /*******************************************************************************
73 * Function Name: usb0_host_dma_stop_d0
74 * Description : D0FIFO DMA stop
75 * Arguments : uint16_t pipe : pipe number
76 * : uint32_t remain : transfer byte
78 *******************************************************************************/
79 void usb0_host_dma_stop_d0 (uint16_t pipe
, uint32_t remain
)
86 dfacc
= RZA_IO_RegRead_16(&USB200
.D0FBCFG
,
87 USB_DnFBCFG_DFACC_SHIFT
,
99 if (g_usb0_host_DmaInfo
[USB_HOST_D0FIFO
].size
== 2)
103 else if (g_usb0_host_DmaInfo
[USB_HOST_D0FIFO
].size
== 1)
113 if (RZA_IO_RegRead_16(&g_usb0_host_pipecfg
[pipe
], USB_PIPECFG_BFRE_SHIFT
, USB_PIPECFG_BFRE
) == 1)
115 if (g_usb0_host_pipe_status
[pipe
] != USB_HOST_PIPE_DONE
)
117 buffer
= USB200
.D0FIFOCTR
;
118 dtln
= (buffer
& USB_HOST_BITDTLN
);
120 if ((dtln
% sds_b
) != 0)
122 remain
+= (sds_b
- (dtln
% sds_b
));
124 g_usb0_host_PipeDataSize
[pipe
] = (g_usb0_host_data_count
[pipe
] - remain
);
125 g_usb0_host_data_count
[pipe
] = remain
;
129 RZA_IO_RegWrite_16(&USB200
.D0FIFOSEL
,
131 USB_DnFIFOSEL_DREQE_SHIFT
,
132 USB_DnFIFOSEL_DREQE
);
135 /*******************************************************************************
136 * Function Name: usb0_host_dma_stop_d1
137 * Description : D1FIFO DMA stop
138 * Arguments : uint16_t pipe : pipe number
139 * : uint32_t remain : transfer byte
140 * Return Value : none
141 *******************************************************************************/
142 void usb0_host_dma_stop_d1 (uint16_t pipe
, uint32_t remain
)
149 dfacc
= RZA_IO_RegRead_16(&USB200
.D1FBCFG
,
150 USB_DnFBCFG_DFACC_SHIFT
,
162 if (g_usb0_host_DmaInfo
[USB_HOST_D1FIFO
].size
== 2)
166 else if (g_usb0_host_DmaInfo
[USB_HOST_D1FIFO
].size
== 1)
176 if (RZA_IO_RegRead_16(&g_usb0_host_pipecfg
[pipe
], USB_PIPECFG_BFRE_SHIFT
, USB_PIPECFG_BFRE
) == 1)
178 if (g_usb0_host_pipe_status
[pipe
] != USB_HOST_PIPE_DONE
)
180 buffer
= USB200
.D1FIFOCTR
;
181 dtln
= (buffer
& USB_HOST_BITDTLN
);
183 if ((dtln
% sds_b
) != 0)
185 remain
+= (sds_b
- (dtln
% sds_b
));
187 g_usb0_host_PipeDataSize
[pipe
] = (g_usb0_host_data_count
[pipe
] - remain
);
188 g_usb0_host_data_count
[pipe
] = remain
;
192 RZA_IO_RegWrite_16(&USB200
.D1FIFOSEL
,
194 USB_DnFIFOSEL_DREQE_SHIFT
,
195 USB_DnFIFOSEL_DREQE
);
198 /*******************************************************************************
199 * Function Name: usb0_host_dma_interrupt_d0fifo
200 * Description : This function is DMA interrupt handler entry.
201 * : Execute usb1_host_dmaint() after disabling DMA interrupt in this function.
202 * : Disable DMA interrupt to DMAC executed when USB_HOST_D0FIFO_DMA is
203 * : specified by dma->fifo.
204 * : Register this function as DMA complete interrupt.
205 * Arguments : uint32_t int_sense ; Interrupts detection mode
206 * : ; INTC_LEVEL_SENSITIVE : Level sense
207 * : ; INTC_EDGE_TRIGGER : Edge trigger
208 * Return Value : none
209 *******************************************************************************/
210 void usb0_host_dma_interrupt_d0fifo (uint32_t int_sense
)
212 usb0_host_dmaint(USB_HOST_D0FIFO
);
213 g_usb0_host_DmaStatus
[USB_HOST_D0FIFO
] = USB_HOST_DMA_READY
;
216 /*******************************************************************************
217 * Function Name: usb0_host_dma_interrupt_d1fifo
218 * Description : This function is DMA interrupt handler entry.
219 * : Execute usb0_host_dmaint() after disabling DMA interrupt in this function.
220 * : Disable DMA interrupt to DMAC executed when USB_HOST_D1FIFO_DMA is
221 * : specified by dma->fifo.
222 * : Register this function as DMA complete interrupt.
223 * Arguments : uint32_t int_sense ; Interrupts detection mode
224 * : ; INTC_LEVEL_SENSITIVE : Level sense
225 * : ; INTC_EDGE_TRIGGER : Edge trigger
226 * Return Value : none
227 *******************************************************************************/
228 void usb0_host_dma_interrupt_d1fifo (uint32_t int_sense
)
230 usb0_host_dmaint(USB_HOST_D1FIFO
);
231 g_usb0_host_DmaStatus
[USB_HOST_D1FIFO
] = USB_HOST_DMA_READY
;
234 /*******************************************************************************
235 * Function Name: usb0_host_dmaint
236 * Description : This function is DMA transfer end interrupt
237 * Arguments : uint16_t fifo ; fifo number
238 * : ; USB_HOST_D0FIFO
239 * : ; USB_HOST_D1FIFO
240 * Return Value : none
241 *******************************************************************************/
242 static void usb0_host_dmaint (uint16_t fifo
)
246 pipe
= g_usb0_host_DmaPipe
[fifo
];
248 if (g_usb0_host_DmaInfo
[fifo
].dir
== USB_HOST_BUF2FIFO
)
250 usb0_host_dmaint_buf2fifo(pipe
);
254 usb0_host_dmaint_fifo2buf(pipe
);
258 /*******************************************************************************
259 * Function Name: usb0_host_dmaint_fifo2buf
260 * Description : Executes read completion from FIFO by DMAC.
261 * Arguments : uint16_t pipe : pipe number
262 * Return Value : none
263 *******************************************************************************/
264 static void usb0_host_dmaint_fifo2buf (uint16_t pipe
)
269 if (g_usb0_host_pipe_status
[pipe
] != USB_HOST_PIPE_DONE
)
271 useport
= (uint16_t)(g_usb0_host_PipeTbl
[pipe
] & USB_HOST_FIFO_USE
);
273 if (useport
== USB_HOST_D0FIFO_DMA
)
275 remain
= Userdef_USB_usb0_host_stop_dma0();
276 usb0_host_dma_stop_d0(pipe
, remain
);
278 if (RZA_IO_RegRead_16(&g_usb0_host_pipecfg
[pipe
], USB_PIPECFG_BFRE_SHIFT
, USB_PIPECFG_BFRE
) == 0)
280 if (g_usb0_host_DmaStatus
[USB_HOST_D0FIFO
] == USB_HOST_DMA_BUSYEND
)
282 USB200
.D0FIFOCTR
= USB_HOST_BITBCLR
;
283 g_usb0_host_pipe_status
[pipe
] = USB_HOST_PIPE_DONE
;
287 usb0_host_enable_brdy_int(pipe
);
293 remain
= Userdef_USB_usb0_host_stop_dma1();
294 usb0_host_dma_stop_d1(pipe
, remain
);
296 if (RZA_IO_RegRead_16(&g_usb0_host_pipecfg
[pipe
], USB_PIPECFG_BFRE_SHIFT
, USB_PIPECFG_BFRE
) == 0)
298 if (g_usb0_host_DmaStatus
[USB_HOST_D1FIFO
] == USB_HOST_DMA_BUSYEND
)
300 USB200
.D1FIFOCTR
= USB_HOST_BITBCLR
;
301 g_usb0_host_pipe_status
[pipe
] = USB_HOST_PIPE_DONE
;
305 usb0_host_enable_brdy_int(pipe
);
312 /*******************************************************************************
313 * Function Name: usb0_host_dmaint_buf2fifo
314 * Description : Executes write completion in FIFO by DMAC.
315 * Arguments : uint16_t pipe : pipe number
316 * Return Value : none
317 *******************************************************************************/
318 static void usb0_host_dmaint_buf2fifo (uint16_t pipe
)
323 useport
= (uint16_t)(g_usb0_host_PipeTbl
[pipe
] & USB_HOST_FIFO_USE
);
325 if (useport
== USB_HOST_D0FIFO_DMA
)
327 remain
= Userdef_USB_usb0_host_stop_dma0();
328 usb0_host_dma_stop_d0(pipe
, remain
);
330 if (g_usb0_host_DmaBval
[USB_HOST_D0FIFO
] != 0)
332 RZA_IO_RegWrite_16(&USB200
.D0FIFOCTR
,
334 USB_DnFIFOCTR_BVAL_SHIFT
,
340 remain
= Userdef_USB_usb0_host_stop_dma1();
341 usb0_host_dma_stop_d1(pipe
, remain
);
343 if (g_usb0_host_DmaBval
[USB_HOST_D1FIFO
] != 0)
345 RZA_IO_RegWrite_16(&USB200
.D1FIFOCTR
,
347 USB_DnFIFOCTR_BVAL_SHIFT
,
352 usb0_host_enable_bemp_int(pipe
);