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_usbint.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 #if(1) /* ohci_wrapp */
42 #include "ohci_wrapp_RZ_A1_local.h"
46 /*******************************************************************************
48 *******************************************************************************/
51 /*******************************************************************************
53 *******************************************************************************/
56 /*******************************************************************************
57 Imported global variables and functions (from other files)
58 *******************************************************************************/
61 /*******************************************************************************
62 Exported global variables and functions (to be accessed by other files)
63 *******************************************************************************/
64 static void usb0_host_interrupt1(void);
65 static void usb0_host_BRDYInterrupt(uint16_t Status, uint16_t Int_enbl);
66 static void usb0_host_NRDYInterrupt(uint16_t Status, uint16_t Int_enbl);
67 static void usb0_host_BEMPInterrupt(uint16_t Status, uint16_t Int_enbl);
70 /*******************************************************************************
71 Private global variables and functions
72 *******************************************************************************/
75 /*******************************************************************************
76 * Function Name: usb0_host_interrupt
77 * Description : Executes USB interrupt.
78 * : Register this function in the USB interrupt handler.
79 * : Set CFIF0 in the pipe set before the interrupt after executing
81 * Arguments : uint32_t int_sense ; Interrupts detection mode
82 * : ; INTC_LEVEL_SENSITIVE : Level sense
83 * : ; INTC_EDGE_TRIGGER : Edge trigger
85 *******************************************************************************/
86 void usb0_host_interrupt (uint32_t int_sense)
92 savepipe1 = USB200.CFIFOSEL;
93 savepipe2 = USB200.PIPESEL;
94 usb0_host_interrupt1();
96 /* Control transmission changes ISEL within interruption processing. */
97 /* For this reason, write return of ISEL cannot be performed. */
98 buffer = USB200.CFIFOSEL;
99 buffer &= (uint16_t)~(USB_HOST_BITCURPIPE);
100 buffer |= (uint16_t)(savepipe1 & USB_HOST_BITCURPIPE);
101 USB200.CFIFOSEL = buffer;
102 USB200.PIPESEL = savepipe2;
105 /*******************************************************************************
106 * Function Name: usb0_host_interrupt1
107 * Description : Execue the USB interrupt.
109 * Return Value : none
110 *******************************************************************************/
111 void usb0_host_interrupt1 (void)
123 volatile uint16_t dumy_sts;
125 intsts0 = USB200.INTSTS0;
126 intsts1 = USB200.INTSTS1;
127 intenb0 = USB200.INTENB0;
128 intenb1 = USB200.INTENB1;
130 if ((intsts1 & USB_HOST_BITBCHG) && (intenb1 & USB_HOST_BITBCHGE))
132 USB200.INTSTS1 = (uint16_t)~USB_HOST_BITBCHG;
133 RZA_IO_RegWrite_16(&USB200.INTENB1,
135 USB_INTENB1_BCHGE_SHIFT,
137 g_usb0_host_bchg_flag = USB_HOST_YES;
139 else if ((intsts1 & USB_HOST_BITSACK) && (intenb1 & USB_HOST_BITSACKE))
141 USB200.INTSTS1 = (uint16_t)~USB_HOST_BITSACK;
142 #if(1) /* ohci_wrapp */
143 ohciwrapp_loc_TransEnd(USB_HOST_PIPE0, TD_CC_NOERROR);
145 g_usb0_host_CmdStage &= (~USB_HOST_CMD_FIELD);
146 g_usb0_host_CmdStage |= USB_HOST_CMD_DONE;
149 else if ((intsts1 & USB_HOST_BITSIGN) && (intenb1 & USB_HOST_BITSIGNE))
151 USB200.INTSTS1 = (uint16_t)~USB_HOST_BITSIGN;
152 #if(1) /* ohci_wrapp */
153 g_usb0_host_pipe_status[USB_HOST_PIPE0] = USB_HOST_PIPE_NORES; /* exit NORES */
154 ohciwrapp_loc_TransEnd(USB_HOST_PIPE0, TD_CC_STALL);
156 g_usb0_host_CmdStage &= (~USB_HOST_CMD_FIELD);
157 g_usb0_host_CmdStage |= USB_HOST_CMD_NORES;
160 else if (((intsts1 & USB_HOST_BITDTCH) == USB_HOST_BITDTCH)
161 && ((intenb1 & USB_HOST_BITDTCHE) == USB_HOST_BITDTCHE))
163 USB200.INTSTS1 = (uint16_t)~USB_HOST_BITDTCH;
164 RZA_IO_RegWrite_16(&USB200.INTENB1,
166 USB_INTENB1_DTCHE_SHIFT,
168 g_usb0_host_detach_flag = USB_HOST_YES;
170 Userdef_USB_usb0_host_detach();
172 usb0_host_UsbDetach2();
174 else if (((intsts1 & USB_HOST_BITATTCH) == USB_HOST_BITATTCH)
175 && ((intenb1 & USB_HOST_BITATTCHE) == USB_HOST_BITATTCHE))
177 USB200.INTSTS1 = (uint16_t)~USB_HOST_BITATTCH;
178 RZA_IO_RegWrite_16(&USB200.INTENB1,
180 USB_INTENB1_ATTCHE_SHIFT,
182 g_usb0_host_attach_flag = USB_HOST_YES;
184 Userdef_USB_usb0_host_attach();
186 usb0_host_UsbAttach();
188 else if ((intsts0 & intenb0 & (USB_HOST_BITBEMP | USB_HOST_BITNRDY | USB_HOST_BITBRDY)))
190 brdysts = USB200.BRDYSTS;
191 nrdysts = USB200.NRDYSTS;
192 bempsts = USB200.BEMPSTS;
193 brdyenb = USB200.BRDYENB;
194 nrdyenb = USB200.NRDYENB;
195 bempenb = USB200.BEMPENB;
197 if ((intsts0 & USB_HOST_BITBRDY) && (intenb0 & USB_HOST_BITBRDYE) && (brdysts & brdyenb))
199 usb0_host_BRDYInterrupt(brdysts, brdyenb);
201 else if ((intsts0 & USB_HOST_BITBEMP) && (intenb0 & USB_HOST_BITBEMPE) && (bempsts & bempenb))
203 usb0_host_BEMPInterrupt(bempsts, bempenb);
205 else if ((intsts0 & USB_HOST_BITNRDY) && (intenb0 & USB_HOST_BITNRDYE) && (nrdysts & nrdyenb))
207 usb0_host_NRDYInterrupt(nrdysts, nrdyenb);
219 /* Three dummy read for clearing interrupt requests */
220 dumy_sts = USB200.INTSTS0;
221 dumy_sts = USB200.INTSTS1;
225 /*******************************************************************************
226 * Function Name: usb0_host_BRDYInterrupt
227 * Description : Executes USB BRDY interrupt.
228 * Arguments : uint16_t Status ; BRDYSTS Register Value
229 * : uint16_t Int_enbl ; BRDYENB Register Value
230 * Return Value : none
231 *******************************************************************************/
232 void usb0_host_BRDYInterrupt (uint16_t Status, uint16_t Int_enbl)
235 volatile uint16_t dumy_sts;
237 if ((Status & g_usb0_host_bit_set[USB_HOST_PIPE0]) && (Int_enbl & g_usb0_host_bit_set[USB_HOST_PIPE0]))
239 USB200.BRDYSTS = (uint16_t)~g_usb0_host_bit_set[USB_HOST_PIPE0];
241 #if(1) /* ohci_wrapp */
242 switch ((g_usb0_host_CmdStage & (USB_HOST_STAGE_FIELD | USB_HOST_CMD_FIELD)))
244 case (USB_HOST_STAGE_STATUS | USB_HOST_CMD_DOING):
245 buffer = usb0_host_read_buffer_c(USB_HOST_PIPE0);
246 usb0_host_disable_brdy_int(USB_HOST_PIPE0);
247 g_usb0_host_CmdStage &= (~USB_HOST_CMD_FIELD);
248 g_usb0_host_CmdStage |= USB_HOST_CMD_DONE;
249 ohciwrapp_loc_TransEnd(USB_HOST_PIPE0, TD_CC_NOERROR);
252 case (USB_HOST_STAGE_DATA | USB_HOST_CMD_DOING):
253 buffer = usb0_host_read_buffer_c(USB_HOST_PIPE0);
256 case USB_HOST_READING: /* Continue of data read */
259 case USB_HOST_READEND: /* End of data read */
260 case USB_HOST_READSHRT: /* End of data read */
261 usb0_host_disable_brdy_int(USB_HOST_PIPE0);
262 g_usb0_host_CmdStage &= (~USB_HOST_CMD_FIELD);
263 g_usb0_host_CmdStage |= USB_HOST_CMD_DONE;
264 ohciwrapp_loc_TransEnd(USB_HOST_PIPE0, TD_CC_NOERROR);
267 case USB_HOST_READOVER: /* buffer over */
268 USB200.CFIFOCTR = USB_HOST_BITBCLR;
269 usb0_host_disable_brdy_int(USB_HOST_PIPE0);
270 g_usb0_host_CmdStage &= (~USB_HOST_CMD_FIELD);
271 g_usb0_host_CmdStage |= USB_HOST_CMD_DONE;
272 ohciwrapp_loc_TransEnd(USB_HOST_PIPE0, TD_CC_NOERROR);
275 case USB_HOST_FIFOERROR: /* FIFO access error */
285 switch ((g_usb0_host_CmdStage & (USB_HOST_MODE_FIELD | USB_HOST_STAGE_FIELD | USB_HOST_CMD_FIELD)))
287 case (USB_HOST_MODE_WRITE | USB_HOST_STAGE_STATUS | USB_HOST_CMD_DOING):
288 case (USB_HOST_MODE_NO_DATA | USB_HOST_STAGE_STATUS | USB_HOST_CMD_DOING):
289 buffer = usb0_host_read_buffer_c(USB_HOST_PIPE0);
290 usb0_host_disable_brdy_int(USB_HOST_PIPE0);
291 g_usb0_host_CmdStage &= (~USB_HOST_CMD_FIELD);
292 g_usb0_host_CmdStage |= USB_HOST_CMD_DONE;
295 case (USB_HOST_MODE_READ | USB_HOST_STAGE_DATA | USB_HOST_CMD_DOING):
296 buffer = usb0_host_read_buffer_c(USB_HOST_PIPE0);
300 case USB_HOST_READING: /* Continue of data read */
303 case USB_HOST_READEND: /* End of data read */
304 case USB_HOST_READSHRT: /* End of data read */
305 usb0_host_disable_brdy_int(USB_HOST_PIPE0);
306 g_usb0_host_CmdStage &= (~USB_HOST_CMD_FIELD);
307 g_usb0_host_CmdStage |= USB_HOST_CMD_DONE;
310 case USB_HOST_READOVER: /* buffer over */
311 USB200.CFIFOCTR = USB_HOST_BITBCLR;
312 usb0_host_disable_brdy_int(USB_HOST_PIPE0);
313 g_usb0_host_CmdStage &= (~USB_HOST_CMD_FIELD);
314 g_usb0_host_CmdStage |= USB_HOST_CMD_DONE;
317 case USB_HOST_FIFOERROR: /* FIFO access error */
330 usb0_host_brdy_int(Status, Int_enbl);
333 /* Three dummy reads for clearing interrupt requests */
334 dumy_sts = USB200.BRDYSTS;
337 /*******************************************************************************
338 * Function Name: usb0_host_NRDYInterrupt
339 * Description : Executes USB NRDY interrupt.
340 * Arguments : uint16_t Status ; NRDYSTS Register Value
341 * : uint16_t Int_enbl ; NRDYENB Register Value
342 * Return Value : none
343 *******************************************************************************/
344 void usb0_host_NRDYInterrupt (uint16_t Status, uint16_t Int_enbl)
347 volatile uint16_t dumy_sts;
349 if ((Status & g_usb0_host_bit_set[USB_HOST_PIPE0]) && (Int_enbl & g_usb0_host_bit_set[USB_HOST_PIPE0]))
351 USB200.NRDYSTS = (uint16_t)~g_usb0_host_bit_set[USB_HOST_PIPE0];
352 pid = usb0_host_get_pid(USB_HOST_PIPE0);
354 if ((pid == USB_HOST_PID_STALL) || (pid == USB_HOST_PID_STALL2))
356 g_usb0_host_CmdStage &= (~USB_HOST_CMD_FIELD);
357 g_usb0_host_CmdStage |= USB_HOST_CMD_STALL;
358 #if(1) /* ohci_wrapp */
359 g_usb0_host_pipe_status[USB_HOST_PIPE0] = USB_HOST_PIPE_STALL;
360 ohciwrapp_loc_TransEnd(USB_HOST_PIPE0, TD_CC_STALL);
363 else if (pid == USB_HOST_PID_NAK)
365 g_usb0_host_CmdStage &= (~USB_HOST_CMD_FIELD);
366 g_usb0_host_CmdStage |= USB_HOST_CMD_NORES;
367 #if(1) /* ohci_wrapp */
368 g_usb0_host_pipe_status[USB_HOST_PIPE0] = USB_HOST_PIPE_NORES;
369 ohciwrapp_loc_TransEnd(USB_HOST_PIPE0, TD_CC_STALL);
379 usb0_host_nrdy_int(Status, Int_enbl);
382 /* Three dummy reads for clearing interrupt requests */
383 dumy_sts = USB200.NRDYSTS;
386 /*******************************************************************************
387 * Function Name: usb0_host_BEMPInterrupt
388 * Description : Executes USB BEMP interrupt.
389 * Arguments : uint16_t Status ; BEMPSTS Register Value
390 * : uint16_t Int_enbl ; BEMPENB Register Value
391 * Return Value : none
392 *******************************************************************************/
393 void usb0_host_BEMPInterrupt (uint16_t Status, uint16_t Int_enbl)
397 volatile uint16_t dumy_sts;
399 if ((Status & g_usb0_host_bit_set[USB_HOST_PIPE0]) && (Int_enbl & g_usb0_host_bit_set[USB_HOST_PIPE0]))
401 USB200.BEMPSTS = (uint16_t)~g_usb0_host_bit_set[USB_HOST_PIPE0];
402 pid = usb0_host_get_pid(USB_HOST_PIPE0);
404 if ((pid == USB_HOST_PID_STALL) || (pid == USB_HOST_PID_STALL2))
406 g_usb0_host_CmdStage &= (~USB_HOST_CMD_FIELD);
407 g_usb0_host_CmdStage |= USB_HOST_CMD_STALL;
408 #if(1) /* ohci_wrapp */
409 g_usb0_host_pipe_status[USB_HOST_PIPE0] = USB_HOST_PIPE_STALL; /* exit STALL */
410 ohciwrapp_loc_TransEnd(USB_HOST_PIPE0, TD_CC_STALL);
415 #if(1) /* ohci_wrapp */
416 switch ((g_usb0_host_CmdStage & (USB_HOST_STAGE_FIELD | USB_HOST_CMD_FIELD)))
418 case (USB_HOST_STAGE_STATUS | USB_HOST_CMD_DOING):
419 g_usb0_host_CmdStage &= (~USB_HOST_CMD_FIELD);
420 g_usb0_host_CmdStage |= USB_HOST_CMD_DONE;
421 ohciwrapp_loc_TransEnd(USB_HOST_PIPE0, TD_CC_NOERROR);
424 case (USB_HOST_STAGE_DATA | USB_HOST_CMD_DOING):
425 buffer = usb0_host_write_buffer(USB_HOST_PIPE0);
428 case USB_HOST_WRITING: /* Continue of data write */
429 case USB_HOST_WRITEEND: /* End of data write (zero-length) */
432 case USB_HOST_WRITESHRT: /* End of data write */
433 g_usb0_host_CmdStage &= (~USB_HOST_STAGE_FIELD);
434 g_usb0_host_CmdStage |= USB_HOST_STAGE_STATUS;
435 ohciwrapp_loc_TransEnd(USB_HOST_PIPE0, TD_CC_NOERROR);
438 case USB_HOST_FIFOERROR: /* FIFO access error */
449 switch ((g_usb0_host_CmdStage & (USB_HOST_MODE_FIELD | USB_HOST_STAGE_FIELD | USB_HOST_CMD_FIELD)))
451 case (USB_HOST_MODE_READ | USB_HOST_STAGE_STATUS | USB_HOST_CMD_DOING):
452 g_usb0_host_CmdStage &= (~USB_HOST_CMD_FIELD);
453 g_usb0_host_CmdStage |= USB_HOST_CMD_DONE;
456 case (USB_HOST_MODE_WRITE | USB_HOST_STAGE_DATA | USB_HOST_CMD_DOING):
457 buffer = usb0_host_write_buffer(USB_HOST_PIPE0);
460 case USB_HOST_WRITING: /* Continue of data write */
461 case USB_HOST_WRITEEND: /* End of data write (zero-length) */
464 case USB_HOST_WRITESHRT: /* End of data write */
465 g_usb0_host_CmdStage &= (~USB_HOST_STAGE_FIELD);
466 g_usb0_host_CmdStage |= USB_HOST_STAGE_STATUS;
469 case USB_HOST_FIFOERROR: /* FIFO access error */
475 case (USB_HOST_MODE_WRITE | USB_HOST_STAGE_STATUS | USB_HOST_CMD_DOING):
476 g_usb0_host_CmdStage &= (~USB_HOST_CMD_FIELD);
477 g_usb0_host_CmdStage |= USB_HOST_CMD_IDLE;
489 usb0_host_bemp_int(Status, Int_enbl);
492 /* Three dummy reads for clearing interrupt requests */
493 dumy_sts = USB200.BEMPSTS;