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_intrn.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 *******************************************************************************/
68 /*******************************************************************************
69 * Function Name: usb0_function_brdy_int
70 * Description : Executes BRDY interrupt(USB_FUNCTION_PIPE1-9).
71 * : According to the pipe that interrupt is generated in,
72 * : reads/writes buffer allocated in the pipe.
73 * : This function is executed in the BRDY interrupt handler.
74 * : This function clears BRDY interrupt status and BEMP interrupt
76 * Arguments : uint16_t Status ; BRDYSTS Register Value
77 * : uint16_t Int_enbl ; BRDYENB Register Value
79 *******************************************************************************/
81 void usb0_function_brdy_int (uint16_t status
, uint16_t int_enb
)
83 uint32_t int_sense
= 0;
87 for (pipe
= USB_FUNCTION_PIPE1
; pipe
<= USB_FUNCTION_MAX_PIPE_NO
; pipe
++)
89 pipebit
= g_usb0_function_bit_set
[pipe
];
91 if ((status
& pipebit
) && (int_enb
& pipebit
))
93 USB200
.BRDYSTS
= (uint16_t)~pipebit
;
94 USB200
.BEMPSTS
= (uint16_t)~pipebit
;
95 if ((g_usb0_function_PipeTbl
[pipe
] & USB_FUNCTION_FIFO_USE
) == USB_FUNCTION_D0FIFO_DMA
)
97 if (g_usb0_function_DmaStatus
[USB_FUNCTION_D0FIFO
] != USB_FUNCTION_DMA_READY
)
99 usb0_function_dma_interrupt_d0fifo(int_sense
);
102 if (RZA_IO_RegRead_16(&g_usb0_function_pipecfg
[pipe
], USB_PIPECFG_BFRE_SHIFT
, USB_PIPECFG_BFRE
) == 0)
104 usb0_function_read_dma(pipe
);
105 usb0_function_disable_brdy_int(pipe
);
109 USB200
.D0FIFOCTR
= USB_FUNCTION_BITBCLR
;
110 g_usb0_function_pipe_status
[pipe
] = DEVDRV_USBF_PIPE_DONE
;
113 else if ((g_usb0_function_PipeTbl
[pipe
] & USB_FUNCTION_FIFO_USE
) == USB_FUNCTION_D1FIFO_DMA
)
115 if (g_usb0_function_DmaStatus
[USB_FUNCTION_D1FIFO
] != USB_FUNCTION_DMA_READY
)
117 usb0_function_dma_interrupt_d1fifo(int_sense
);
120 if (RZA_IO_RegRead_16(&g_usb0_function_pipecfg
[pipe
], USB_PIPECFG_BFRE_SHIFT
, USB_PIPECFG_BFRE
) == 0)
122 usb0_function_read_dma(pipe
);
123 usb0_function_disable_brdy_int(pipe
);
127 USB200
.D1FIFOCTR
= USB_FUNCTION_BITBCLR
;
128 g_usb0_function_pipe_status
[pipe
] = DEVDRV_USBF_PIPE_DONE
;
133 if (RZA_IO_RegRead_16(&g_usb0_function_pipecfg
[pipe
], USB_PIPECFG_DIR_SHIFT
, USB_PIPECFG_DIR
) == 0)
135 usb0_function_read_buffer(pipe
);
139 usb0_function_write_buffer(pipe
);
147 /*******************************************************************************
148 * Function Name: usb0_function_nrdy_int
149 * Description : Executes NRDY interrupt(USB_FUNCTION_PIPE1-9).
150 * : Checks NRDY interrupt cause by PID. When the cause if STALL,
151 * : regards the pipe state as STALL and ends the processing.
152 * : Then the cause is not STALL, increments the error count to
153 * : communicate again. When the error count is 3, determines
154 * : the pipe state as DEVDRV_USBF_PIPE_NORES and ends the processing.
155 * : This function is executed in the NRDY interrupt handler.
156 * : This function clears NRDY interrupt status.
157 * Arguments : uint16_t status ; NRDYSTS Register Value
158 * : uint16_t int_enb ; NRDYENB Register Value
159 * Return Value : none
160 *******************************************************************************/
161 void usb0_function_nrdy_int (uint16_t status
, uint16_t int_enb
)
167 bitcheck
= (uint16_t)(status
& int_enb
);
169 USB200
.NRDYSTS
= (uint16_t)~status
;
171 for (pipe
= USB_FUNCTION_PIPE1
; pipe
<= USB_FUNCTION_MAX_PIPE_NO
; pipe
++)
173 if ((bitcheck
&g_usb0_function_bit_set
[pipe
]) == g_usb0_function_bit_set
[pipe
])
175 if (RZA_IO_RegRead_16(&USB200
.SYSCFG0
, USB_SYSCFG_DCFM_SHIFT
, USB_SYSCFG_DCFM
) == 1)
177 if (g_usb0_function_pipe_status
[pipe
] == DEVDRV_USBF_PIPE_WAIT
)
179 pid
= usb0_function_get_pid(pipe
);
180 if ((pid
== DEVDRV_USBF_PID_STALL
) || (pid
== DEVDRV_USBF_PID_STALL2
))
182 g_usb0_function_pipe_status
[pipe
] = DEVDRV_USBF_PIPE_STALL
;
186 g_usb0_function_PipeIgnore
[pipe
]++;
187 if (g_usb0_function_PipeIgnore
[pipe
] == 3)
189 g_usb0_function_pipe_status
[pipe
] = DEVDRV_USBF_PIPE_NORES
;
193 usb0_function_set_pid_buf(pipe
);
206 /*******************************************************************************
207 * Function Name: usb0_function_bemp_int
208 * Description : Executes BEMP interrupt(USB_FUNCTION_PIPE1-9).
209 * Arguments : uint16_t status ; BEMPSTS Register Value
210 * : uint16_t int_enb ; BEMPENB Register Value
211 * Return Value : none
212 *******************************************************************************/
213 void usb0_function_bemp_int (uint16_t status
, uint16_t int_enb
)
220 bitcheck
= (uint16_t)(status
& int_enb
);
222 USB200
.BEMPSTS
= (uint16_t)~status
;
224 for (pipe
= USB_FUNCTION_PIPE1
; pipe
<= USB_FUNCTION_MAX_PIPE_NO
; pipe
++)
226 if ((bitcheck
&g_usb0_function_bit_set
[pipe
]) == g_usb0_function_bit_set
[pipe
])
228 pid
= usb0_function_get_pid(pipe
);
230 if ((pid
== DEVDRV_USBF_PID_STALL
) || (pid
== DEVDRV_USBF_PID_STALL2
))
232 g_usb0_function_pipe_status
[pipe
] = DEVDRV_USBF_PIPE_STALL
;
236 inbuf
= usb0_function_get_inbuf(pipe
);
240 usb0_function_disable_bemp_int(pipe
);
241 usb0_function_set_pid_nak(pipe
);
242 g_usb0_function_pipe_status
[pipe
] = DEVDRV_USBF_PIPE_DONE
;