]> git.donarmstrong.com Git - qmk_firmware.git/blob - tool/mbed/mbed-sdk/libraries/USBDevice/USBDevice/TARGET_RENESAS/TARGET_RZ_A1H/usb0/src/common/usb0_function_dma.c
Squashed 'tmk_core/' changes from 7967731..b9e0ea0
[qmk_firmware.git] / tool / mbed / mbed-sdk / libraries / USBDevice / USBDevice / TARGET_RENESAS / TARGET_RZ_A1H / usb0 / src / common / usb0_function_dma.c
1 /*******************************************************************************
2 * DISCLAIMER
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
19 * following link:
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
25 * $Rev: 1116 $
26 * $Date:: 2014-07-09 16:29:19 +0900#$
27 * Device(s)    : RZ/A1H
28 * Tool-Chain   :
29 * OS           : None
30 * H/W Platform :
31 * Description  : RZ/A1H R7S72100 USB Sample Program
32 * Operation    :
33 * Limitations  :
34 *******************************************************************************/
35
36
37 /*******************************************************************************
38 Includes   <System Includes> , "Project Includes"
39 *******************************************************************************/
40 #include "usb0_function.h"
41
42
43 /*******************************************************************************
44 Typedef definitions
45 *******************************************************************************/
46
47
48 /*******************************************************************************
49 Macro definitions
50 *******************************************************************************/
51
52
53 /*******************************************************************************
54 Imported global variables and functions (from other files)
55 *******************************************************************************/
56
57
58 /*******************************************************************************
59 Exported global variables and functions (to be accessed by other files)
60 *******************************************************************************/
61
62
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);
69
70
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
76 * Return Value : none
77 *******************************************************************************/
78 void usb0_function_dma_stop_d0 (uint16_t pipe, uint32_t remain)
79 {
80     uint16_t dtln;
81     uint16_t dfacc;
82     uint16_t buffer;
83     uint16_t sds_b = 1;
84
85     dfacc = RZA_IO_RegRead_16(&USB200.D0FBCFG, USB_DnFBCFG_DFACC_SHIFT, USB_DnFBCFG_DFACC);
86
87     if (dfacc == 2)
88     {
89         sds_b = 32;
90     }
91     else if (dfacc == 1)
92     {
93         sds_b = 16;
94     }
95     else
96     {
97         if (g_usb0_function_DmaInfo[USB_FUNCTION_D0FIFO].size == 2)
98         {
99             sds_b = 4;
100         }
101         else if (g_usb0_function_DmaInfo[USB_FUNCTION_D0FIFO].size == 1)
102         {
103             sds_b = 2;
104         }
105         else
106         {
107             sds_b = 1;
108         }
109     }
110
111     if (RZA_IO_RegRead_16(&g_usb0_function_pipecfg[pipe], USB_PIPECFG_BFRE_SHIFT, USB_PIPECFG_BFRE) == 1)
112     {
113         if (g_usb0_function_pipe_status[pipe] != DEVDRV_USBF_PIPE_DONE)
114         {
115             buffer = USB200.D0FIFOCTR;
116             dtln   = (buffer & USB_FUNCTION_BITDTLN);
117
118             if ((dtln % sds_b) != 0)
119             {
120                 remain += (sds_b - (dtln % sds_b));
121             }
122             g_usb0_function_PipeDataSize[pipe] = (g_usb0_function_data_count[pipe] - remain);
123             g_usb0_function_data_count[pipe]   = remain;
124         }
125     }
126
127     RZA_IO_RegWrite_16(&USB200.D0FIFOSEL, 0, USB_DnFIFOSEL_DREQE_SHIFT, USB_DnFIFOSEL_DREQE);
128 }
129
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)
138 {
139     uint16_t dtln;
140     uint16_t dfacc;
141     uint16_t buffer;
142     uint16_t sds_b = 1;
143
144     dfacc = RZA_IO_RegRead_16(&USB200.D1FBCFG, USB_DnFBCFG_DFACC_SHIFT, USB_DnFBCFG_DFACC);
145
146     if (dfacc == 2)
147     {
148         sds_b = 32;
149     }
150     else if (dfacc == 1)
151     {
152         sds_b = 16;
153     }
154     else
155     {
156         if (g_usb0_function_DmaInfo[USB_FUNCTION_D1FIFO].size == 2)
157         {
158             sds_b = 4;
159         }
160         else if (g_usb0_function_DmaInfo[USB_FUNCTION_D1FIFO].size == 1)
161         {
162             sds_b = 2;
163         }
164         else
165         {
166             sds_b = 1;
167         }
168     }
169
170     if (RZA_IO_RegRead_16(&g_usb0_function_pipecfg[pipe], USB_PIPECFG_BFRE_SHIFT, USB_PIPECFG_BFRE) == 1)
171     {
172         if (g_usb0_function_pipe_status[pipe] != DEVDRV_USBF_PIPE_DONE)
173         {
174             buffer = USB200.D1FIFOCTR;
175             dtln   = (buffer & USB_FUNCTION_BITDTLN);
176
177             if ((dtln % sds_b) != 0)
178             {
179                 remain += (sds_b - (dtln % sds_b));
180             }
181             g_usb0_function_PipeDataSize[pipe] = (g_usb0_function_data_count[pipe] - remain);
182             g_usb0_function_data_count[pipe]   = remain;
183         }
184     }
185
186     RZA_IO_RegWrite_16(&USB200.D1FIFOSEL, 0, USB_DnFIFOSEL_DREQE_SHIFT, USB_DnFIFOSEL_DREQE);
187 }
188
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)
202 {
203     usb0_function_dmaint(USB_FUNCTION_D0FIFO);
204     g_usb0_function_DmaStatus[USB_FUNCTION_D0FIFO] = USB_FUNCTION_DMA_READY;
205 }
206
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)
220 {
221     usb0_function_dmaint(USB_FUNCTION_D1FIFO);
222     g_usb0_function_DmaStatus[USB_FUNCTION_D1FIFO] = USB_FUNCTION_DMA_READY;
223 }
224
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)
234 {
235     uint16_t pipe;
236
237     pipe = g_usb0_function_DmaPipe[fifo];
238
239     if (g_usb0_function_DmaInfo[fifo].dir == USB_FUNCTION_BUF2FIFO)
240     {
241         usb0_function_dmaint_buf2fifo(pipe);
242     }
243     else
244     {
245         usb0_function_dmaint_fifo2buf(pipe);
246     }
247 }
248
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)
256 {
257     uint32_t remain;
258     uint16_t useport;
259
260     if (g_usb0_function_pipe_status[pipe] != DEVDRV_USBF_PIPE_DONE)
261     {
262         useport = (uint16_t)(g_usb0_function_PipeTbl[pipe] & USB_FUNCTION_FIFO_USE);
263
264         if (useport == USB_FUNCTION_D0FIFO_DMA)
265         {
266             remain = Userdef_USB_usb0_function_stop_dma0();
267             usb0_function_dma_stop_d0(pipe, remain);
268
269             if (RZA_IO_RegRead_16(&g_usb0_function_pipecfg[pipe], USB_PIPECFG_BFRE_SHIFT, USB_PIPECFG_BFRE) == 0)
270             {
271                 if (g_usb0_function_DmaStatus[USB_FUNCTION_D0FIFO] == USB_FUNCTION_DMA_BUSYEND)
272                 {
273                     USB200.D0FIFOCTR = USB_FUNCTION_BITBCLR;
274                     g_usb0_function_pipe_status[pipe] = DEVDRV_USBF_PIPE_DONE;
275                 }
276                 else
277                 {
278                     usb0_function_enable_brdy_int(pipe);
279                 }
280             }
281         }
282         else
283         {
284             remain = Userdef_USB_usb0_function_stop_dma1();
285             usb0_function_dma_stop_d1(pipe, remain);
286
287             if (RZA_IO_RegRead_16(&g_usb0_function_pipecfg[pipe], USB_PIPECFG_BFRE_SHIFT, USB_PIPECFG_BFRE) == 0)
288             {
289                 if (g_usb0_function_DmaStatus[USB_FUNCTION_D1FIFO] == USB_FUNCTION_DMA_BUSYEND)
290                 {
291                     USB200.D1FIFOCTR = USB_FUNCTION_BITBCLR;
292                     g_usb0_function_pipe_status[pipe] = DEVDRV_USBF_PIPE_DONE;
293                 }
294                 else
295                 {
296                     usb0_function_enable_brdy_int(pipe);
297                 }
298             }
299         }
300     }
301 }
302
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)
310 {
311     uint32_t remain;
312     uint16_t useport;
313
314     useport = (uint16_t)(g_usb0_function_PipeTbl[pipe] & USB_FUNCTION_FIFO_USE);
315
316     if (useport == USB_FUNCTION_D0FIFO_DMA)
317     {
318         remain = Userdef_USB_usb0_function_stop_dma0();
319         usb0_function_dma_stop_d0(pipe, remain);
320
321         if (g_usb0_function_DmaBval[USB_FUNCTION_D0FIFO] != 0)
322         {
323             RZA_IO_RegWrite_16(&USB200.D0FIFOCTR,
324                                 1,
325                                 USB_DnFIFOCTR_BVAL_SHIFT,
326                                 USB_DnFIFOCTR_BVAL);
327         }
328     }
329     else
330     {
331         remain = Userdef_USB_usb0_function_stop_dma1();
332         usb0_function_dma_stop_d1(pipe, remain);
333
334         if (g_usb0_function_DmaBval[USB_FUNCTION_D1FIFO] != 0)
335         {
336             RZA_IO_RegWrite_16(&USB200.D1FIFOCTR,
337                                 1,
338                                 USB_DnFIFOCTR_BVAL_SHIFT,
339                                 USB_DnFIFOCTR_BVAL);
340         }
341     }
342
343     usb0_function_enable_bemp_int(pipe);
344 }
345
346 /* End of File */