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_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_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 void usb0_function_dmaint(uint16_t fifo
);
67 static void usb0_function_dmaint_buf2fifo(uint16_t pipe
);
68 static void usb0_function_dmaint_fifo2buf(uint16_t pipe
);
71 /*******************************************************************************
72 * Function Name: usb0_function_dma_stop_d0
73 * Description : D0FIFO DMA stop
74 * Arguments : uint16_t pipe : pipe number
75 * : uint32_t remain : transfer byte
77 *******************************************************************************/
78 void usb0_function_dma_stop_d0 (uint16_t pipe
, uint32_t remain
)
85 dfacc
= RZA_IO_RegRead_16(&USB200
.D0FBCFG
, USB_DnFBCFG_DFACC_SHIFT
, USB_DnFBCFG_DFACC
);
97 if (g_usb0_function_DmaInfo
[USB_FUNCTION_D0FIFO
].size
== 2)
101 else if (g_usb0_function_DmaInfo
[USB_FUNCTION_D0FIFO
].size
== 1)
111 if (RZA_IO_RegRead_16(&g_usb0_function_pipecfg
[pipe
], USB_PIPECFG_BFRE_SHIFT
, USB_PIPECFG_BFRE
) == 1)
113 if (g_usb0_function_pipe_status
[pipe
] != DEVDRV_USBF_PIPE_DONE
)
115 buffer
= USB200
.D0FIFOCTR
;
116 dtln
= (buffer
& USB_FUNCTION_BITDTLN
);
118 if ((dtln
% sds_b
) != 0)
120 remain
+= (sds_b
- (dtln
% sds_b
));
122 g_usb0_function_PipeDataSize
[pipe
] = (g_usb0_function_data_count
[pipe
] - remain
);
123 g_usb0_function_data_count
[pipe
] = remain
;
127 RZA_IO_RegWrite_16(&USB200
.D0FIFOSEL
, 0, USB_DnFIFOSEL_DREQE_SHIFT
, USB_DnFIFOSEL_DREQE
);
130 /*******************************************************************************
131 * Function Name: usb0_function_dma_stop_d1
132 * Description : D1FIFO DMA stop
133 * Arguments : uint16_t pipe : pipe number
134 * : uint32_t remain : transfer byte
135 * Return Value : none
136 *******************************************************************************/
137 void usb0_function_dma_stop_d1 (uint16_t pipe
, uint32_t remain
)
144 dfacc
= RZA_IO_RegRead_16(&USB200
.D1FBCFG
, USB_DnFBCFG_DFACC_SHIFT
, USB_DnFBCFG_DFACC
);
156 if (g_usb0_function_DmaInfo
[USB_FUNCTION_D1FIFO
].size
== 2)
160 else if (g_usb0_function_DmaInfo
[USB_FUNCTION_D1FIFO
].size
== 1)
170 if (RZA_IO_RegRead_16(&g_usb0_function_pipecfg
[pipe
], USB_PIPECFG_BFRE_SHIFT
, USB_PIPECFG_BFRE
) == 1)
172 if (g_usb0_function_pipe_status
[pipe
] != DEVDRV_USBF_PIPE_DONE
)
174 buffer
= USB200
.D1FIFOCTR
;
175 dtln
= (buffer
& USB_FUNCTION_BITDTLN
);
177 if ((dtln
% sds_b
) != 0)
179 remain
+= (sds_b
- (dtln
% sds_b
));
181 g_usb0_function_PipeDataSize
[pipe
] = (g_usb0_function_data_count
[pipe
] - remain
);
182 g_usb0_function_data_count
[pipe
] = remain
;
186 RZA_IO_RegWrite_16(&USB200
.D1FIFOSEL
, 0, USB_DnFIFOSEL_DREQE_SHIFT
, USB_DnFIFOSEL_DREQE
);
189 /*******************************************************************************
190 * Function Name: usb0_function_dma_interrupt_d0fifo
191 * Description : This function is DMA interrupt handler entry.
192 * : Execute usb0_function_dmaint() after disabling DMA interrupt in this function.
193 * : Disable DMA interrupt to DMAC executed when USB_FUNCTION_D0FIFO_DMA is
194 * : specified by dma->fifo.
195 * : Register this function as DMA complete interrupt.
196 * Arguments : uint32_t int_sense ; Interrupts detection mode
197 * : ; INTC_LEVEL_SENSITIVE : Level sense
198 * : ; INTC_EDGE_TRIGGER : Edge trigger
199 * Return Value : none
200 *******************************************************************************/
201 void usb0_function_dma_interrupt_d0fifo (uint32_t int_sense
)
203 usb0_function_dmaint(USB_FUNCTION_D0FIFO
);
204 g_usb0_function_DmaStatus
[USB_FUNCTION_D0FIFO
] = USB_FUNCTION_DMA_READY
;
207 /*******************************************************************************
208 * Function Name: usb0_function_dma_interrupt_d1fifo
209 * Description : This function is DMA interrupt handler entry.
210 * : Execute usb0_function_dmaint() after disabling DMA interrupt in this function.
211 * : Disable DMA interrupt to DMAC executed when USB_FUNCTION_D1FIFO_DMA is
212 * : specified by dma->fifo.
213 * : Register this function as DMA complete interrupt.
214 * Arguments : uint32_t int_sense ; Interrupts detection mode
215 * : ; INTC_LEVEL_SENSITIVE : Level sense
216 * : ; INTC_EDGE_TRIGGER : Edge trigger
217 * Return Value : none
218 *******************************************************************************/
219 void usb0_function_dma_interrupt_d1fifo (uint32_t int_sense
)
221 usb0_function_dmaint(USB_FUNCTION_D1FIFO
);
222 g_usb0_function_DmaStatus
[USB_FUNCTION_D1FIFO
] = USB_FUNCTION_DMA_READY
;
225 /*******************************************************************************
226 * Function Name: usb0_function_dmaint
227 * Description : This function is DMA transfer end interrupt
228 * Arguments : uint16_t fifo ; fifo number
229 * : ; USB_FUNCTION_D0FIFO
230 * : ; USB_FUNCTION_D1FIFO
231 * Return Value : none
232 *******************************************************************************/
233 static void usb0_function_dmaint (uint16_t fifo
)
237 pipe
= g_usb0_function_DmaPipe
[fifo
];
239 if (g_usb0_function_DmaInfo
[fifo
].dir
== USB_FUNCTION_BUF2FIFO
)
241 usb0_function_dmaint_buf2fifo(pipe
);
245 usb0_function_dmaint_fifo2buf(pipe
);
249 /*******************************************************************************
250 * Function Name: usb0_function_dmaint_fifo2buf
251 * Description : Executes read completion from FIFO by DMAC.
252 * Arguments : uint16_t pipe : pipe number
253 * Return Value : none
254 *******************************************************************************/
255 static void usb0_function_dmaint_fifo2buf (uint16_t pipe
)
260 if (g_usb0_function_pipe_status
[pipe
] != DEVDRV_USBF_PIPE_DONE
)
262 useport
= (uint16_t)(g_usb0_function_PipeTbl
[pipe
] & USB_FUNCTION_FIFO_USE
);
264 if (useport
== USB_FUNCTION_D0FIFO_DMA
)
266 remain
= Userdef_USB_usb0_function_stop_dma0();
267 usb0_function_dma_stop_d0(pipe
, remain
);
269 if (RZA_IO_RegRead_16(&g_usb0_function_pipecfg
[pipe
], USB_PIPECFG_BFRE_SHIFT
, USB_PIPECFG_BFRE
) == 0)
271 if (g_usb0_function_DmaStatus
[USB_FUNCTION_D0FIFO
] == USB_FUNCTION_DMA_BUSYEND
)
273 USB200
.D0FIFOCTR
= USB_FUNCTION_BITBCLR
;
274 g_usb0_function_pipe_status
[pipe
] = DEVDRV_USBF_PIPE_DONE
;
278 usb0_function_enable_brdy_int(pipe
);
284 remain
= Userdef_USB_usb0_function_stop_dma1();
285 usb0_function_dma_stop_d1(pipe
, remain
);
287 if (RZA_IO_RegRead_16(&g_usb0_function_pipecfg
[pipe
], USB_PIPECFG_BFRE_SHIFT
, USB_PIPECFG_BFRE
) == 0)
289 if (g_usb0_function_DmaStatus
[USB_FUNCTION_D1FIFO
] == USB_FUNCTION_DMA_BUSYEND
)
291 USB200
.D1FIFOCTR
= USB_FUNCTION_BITBCLR
;
292 g_usb0_function_pipe_status
[pipe
] = DEVDRV_USBF_PIPE_DONE
;
296 usb0_function_enable_brdy_int(pipe
);
303 /*******************************************************************************
304 * Function Name: usb0_function_dmaint_buf2fifo
305 * Description : Executes write completion in FIFO by DMAC.
306 * Arguments : uint16_t pipe : pipe number
307 * Return Value : none
308 *******************************************************************************/
309 static void usb0_function_dmaint_buf2fifo (uint16_t pipe
)
314 useport
= (uint16_t)(g_usb0_function_PipeTbl
[pipe
] & USB_FUNCTION_FIFO_USE
);
316 if (useport
== USB_FUNCTION_D0FIFO_DMA
)
318 remain
= Userdef_USB_usb0_function_stop_dma0();
319 usb0_function_dma_stop_d0(pipe
, remain
);
321 if (g_usb0_function_DmaBval
[USB_FUNCTION_D0FIFO
] != 0)
323 RZA_IO_RegWrite_16(&USB200
.D0FIFOCTR
,
325 USB_DnFIFOCTR_BVAL_SHIFT
,
331 remain
= Userdef_USB_usb0_function_stop_dma1();
332 usb0_function_dma_stop_d1(pipe
, remain
);
334 if (g_usb0_function_DmaBval
[USB_FUNCTION_D1FIFO
] != 0)
336 RZA_IO_RegWrite_16(&USB200
.D1FIFOCTR
,
338 USB_DnFIFOCTR_BVAL_SHIFT
,
343 usb0_function_enable_bemp_int(pipe
);