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_controlrw.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"
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 *******************************************************************************/
69 /*******************************************************************************
70 * Function Name: usb0_host_CtrlTransStart
71 * Description : Executes USB control transfer.
72 * Arguments : uint16_t devadr ; device address
73 * : uint16_t Req ; bmRequestType & bRequest
74 * : uint16_t Val ; wValue
75 * : uint16_t Indx ; wIndex
76 * : uint16_t Len ; wLength
77 * : uint8_t *Buf ; Data buffer
78 * Return Value : DEVDRV_SUCCESS ; SUCCESS
79 * : DEVDRV_ERROR ; ERROR
80 *******************************************************************************/
81 int32_t usb0_host_CtrlTransStart (uint16_t devadr
, uint16_t Req
, uint16_t Val
,
82 uint16_t Indx
, uint16_t Len
, uint8_t * Buf
)
84 if (g_usb0_host_UsbDeviceSpeed
== USB_HOST_LOW_SPEED
)
86 RZA_IO_RegWrite_16(&USB200
.SOFCFG
,
88 USB_SOFCFG_TRNENSEL_SHIFT
,
93 RZA_IO_RegWrite_16(&USB200
.SOFCFG
,
95 USB_SOFCFG_TRNENSEL_SHIFT
,
99 USB200
.DCPMAXP
= (uint16_t)((uint16_t)(devadr
<< 12) + g_usb0_host_default_max_packet
[devadr
]);
101 if (g_usb0_host_pipe_status
[USB_HOST_PIPE0
] == USB_HOST_PIPE_IDLE
)
103 g_usb0_host_pipe_status
[USB_HOST_PIPE0
] = USB_HOST_PIPE_WAIT
;
104 g_usb0_host_PipeIgnore
[USB_HOST_PIPE0
] = 0; /* Ignore count clear */
105 g_usb0_host_CmdStage
= (USB_HOST_STAGE_SETUP
| USB_HOST_CMD_IDLE
);
109 g_usb0_host_CmdStage
|= USB_HOST_MODE_NO_DATA
; /* No-data Control */
113 if ((Req
& 0x0080) != 0)
115 g_usb0_host_CmdStage
|= USB_HOST_MODE_READ
; /* Control Read */
119 g_usb0_host_CmdStage
|= USB_HOST_MODE_WRITE
; /* Control Write */
123 g_usb0_host_SavReq
= Req
; /* save request */
124 g_usb0_host_SavVal
= Val
;
125 g_usb0_host_SavIndx
= Indx
;
126 g_usb0_host_SavLen
= Len
;
130 if ((g_usb0_host_SavReq
!= Req
) || (g_usb0_host_SavVal
!= Val
)
131 || (g_usb0_host_SavIndx
!= Indx
) || (g_usb0_host_SavLen
!= Len
))
137 switch ((g_usb0_host_CmdStage
& (USB_HOST_STAGE_FIELD
| USB_HOST_CMD_FIELD
)))
139 /* --------------- SETUP STAGE --------------- */
140 case (USB_HOST_STAGE_SETUP
| USB_HOST_CMD_IDLE
):
141 usb0_host_SetupStage(Req
, Val
, Indx
, Len
);
144 case (USB_HOST_STAGE_SETUP
| USB_HOST_CMD_DOING
):
148 case (USB_HOST_STAGE_SETUP
| USB_HOST_CMD_DONE
): /* goto next stage */
149 g_usb0_host_PipeIgnore
[USB_HOST_PIPE0
] = 0; /* Ignore count clear */
150 switch ((g_usb0_host_CmdStage
& (USB_HOST_MODE_FIELD
)))
152 case USB_HOST_MODE_WRITE
:
153 g_usb0_host_CmdStage
&= (~USB_HOST_STAGE_FIELD
);
154 g_usb0_host_CmdStage
|= USB_HOST_STAGE_DATA
;
157 case USB_HOST_MODE_READ
:
158 g_usb0_host_CmdStage
&= (~USB_HOST_STAGE_FIELD
);
159 g_usb0_host_CmdStage
|= USB_HOST_STAGE_DATA
;
162 case USB_HOST_MODE_NO_DATA
:
163 g_usb0_host_CmdStage
&= (~USB_HOST_STAGE_FIELD
);
164 g_usb0_host_CmdStage
|= USB_HOST_STAGE_STATUS
;
170 g_usb0_host_CmdStage
&= (~USB_HOST_CMD_FIELD
);
171 g_usb0_host_CmdStage
|= USB_HOST_CMD_IDLE
;
174 case (USB_HOST_STAGE_SETUP
| USB_HOST_CMD_NORES
):
175 if (g_usb0_host_PipeIgnore
[USB_HOST_PIPE0
] == 3)
177 g_usb0_host_pipe_status
[USB_HOST_PIPE0
] = USB_HOST_PIPE_NORES
; /* exit NORES */
181 g_usb0_host_PipeIgnore
[USB_HOST_PIPE0
]++; /* Ignore count */
182 g_usb0_host_CmdStage
&= (~USB_HOST_CMD_FIELD
);
183 g_usb0_host_CmdStage
|= USB_HOST_CMD_IDLE
;
187 /* --------------- DATA STAGE --------------- */
188 case (USB_HOST_STAGE_DATA
| USB_HOST_CMD_IDLE
):
189 switch ((g_usb0_host_CmdStage
& (USB_HOST_MODE_FIELD
)))
191 case USB_HOST_MODE_WRITE
:
192 usb0_host_CtrlWriteStart((uint32_t)Len
, Buf
);
195 case USB_HOST_MODE_READ
:
196 usb0_host_CtrlReadStart((uint32_t)Len
, Buf
);
204 case (USB_HOST_STAGE_DATA
| USB_HOST_CMD_DOING
):
208 case (USB_HOST_STAGE_DATA
| USB_HOST_CMD_DONE
): /* goto next stage */
209 g_usb0_host_PipeIgnore
[USB_HOST_PIPE0
] = 0; /* Ignore count clear */
210 g_usb0_host_CmdStage
&= (~USB_HOST_STAGE_FIELD
);
211 g_usb0_host_CmdStage
|= USB_HOST_STAGE_STATUS
;
212 g_usb0_host_CmdStage
&= (~USB_HOST_CMD_FIELD
);
213 g_usb0_host_CmdStage
|= USB_HOST_CMD_IDLE
;
216 case (USB_HOST_STAGE_DATA
| USB_HOST_CMD_NORES
):
217 if (g_usb0_host_PipeIgnore
[USB_HOST_PIPE0
] == 3)
219 g_usb0_host_pipe_status
[USB_HOST_PIPE0
] = USB_HOST_PIPE_NORES
; /* exit NORES */
223 g_usb0_host_PipeIgnore
[USB_HOST_PIPE0
]++; /* Ignore count */
224 g_usb0_host_CmdStage
&= (~USB_HOST_CMD_FIELD
);
225 g_usb0_host_CmdStage
|= USB_HOST_CMD_DOING
;
226 usb0_host_clear_pid_stall(USB_HOST_PIPE0
);
227 usb0_host_set_pid_buf(USB_HOST_PIPE0
);
231 case (USB_HOST_STAGE_DATA
| USB_HOST_CMD_STALL
):
232 g_usb0_host_pipe_status
[USB_HOST_PIPE0
] = USB_HOST_PIPE_STALL
; /* exit STALL */
235 /* --------------- STATUS STAGE --------------- */
236 case (USB_HOST_STAGE_STATUS
| USB_HOST_CMD_IDLE
):
237 usb0_host_StatusStage();
240 case (USB_HOST_STAGE_STATUS
| USB_HOST_CMD_DOING
):
244 case (USB_HOST_STAGE_STATUS
| USB_HOST_CMD_DONE
): /* end of Control transfer */
245 usb0_host_set_pid_nak(USB_HOST_PIPE0
);
246 g_usb0_host_pipe_status
[USB_HOST_PIPE0
] = USB_HOST_PIPE_DONE
; /* exit DONE */
249 case (USB_HOST_STAGE_STATUS
| USB_HOST_CMD_NORES
):
250 if (g_usb0_host_PipeIgnore
[USB_HOST_PIPE0
] == 3)
252 g_usb0_host_pipe_status
[USB_HOST_PIPE0
] = USB_HOST_PIPE_NORES
; /* exit NORES */
256 g_usb0_host_PipeIgnore
[USB_HOST_PIPE0
]++; /* Ignore count */
257 g_usb0_host_CmdStage
&= (~USB_HOST_CMD_FIELD
);
258 g_usb0_host_CmdStage
|= USB_HOST_CMD_DOING
;
259 usb0_host_clear_pid_stall(USB_HOST_PIPE0
);
260 usb0_host_set_pid_buf(USB_HOST_PIPE0
);
264 case (USB_HOST_STAGE_STATUS
| USB_HOST_CMD_STALL
):
265 g_usb0_host_pipe_status
[USB_HOST_PIPE0
] = USB_HOST_PIPE_STALL
; /* exit STALL */
272 if (g_usb0_host_pipe_status
[USB_HOST_PIPE0
] != USB_HOST_PIPE_WAIT
)
274 RZA_IO_RegWrite_16(&USB200
.SOFCFG
,
276 USB_SOFCFG_TRNENSEL_SHIFT
,
277 USB_SOFCFG_TRNENSEL
);
280 return DEVDRV_SUCCESS
;
283 /*******************************************************************************
284 * Function Name: usb0_host_SetupStage
285 * Description : Executes USB control transfer/set up stage.
286 * Arguments : uint16_t Req ; bmRequestType & bRequest
287 * : uint16_t Val ; wValue
288 * : uint16_t Indx ; wIndex
289 * : uint16_t Len ; wLength
290 * Return Value : none
291 *******************************************************************************/
292 void usb0_host_SetupStage (uint16_t Req
, uint16_t Val
, uint16_t Indx
, uint16_t Len
)
294 g_usb0_host_CmdStage
&= (~USB_HOST_CMD_FIELD
);
295 g_usb0_host_CmdStage
|= USB_HOST_CMD_DOING
;
297 USB200
.INTSTS1
= (uint16_t)~(USB_HOST_BITSACK
| USB_HOST_BITSIGN
); /* Status Clear */
300 USB200
.USBINDX
= Indx
;
301 USB200
.USBLENG
= Len
;
302 USB200
.DCPCTR
= USB_HOST_BITSUREQ
; /* PID=NAK & Send Setup */
305 /*******************************************************************************
306 * Function Name: usb0_host_StatusStage
307 * Description : Executes USB control transfer/status stage.
309 * Return Value : none
310 *******************************************************************************/
311 void usb0_host_StatusStage (void)
315 switch ((g_usb0_host_CmdStage
& (USB_HOST_MODE_FIELD
)))
317 case USB_HOST_MODE_READ
:
318 usb0_host_CtrlWriteStart((uint32_t)0, (uint8_t *)&Buf1
);
321 case USB_HOST_MODE_WRITE
:
322 usb0_host_CtrlReadStart((uint32_t)0, (uint8_t *)&Buf1
);
325 case USB_HOST_MODE_NO_DATA
:
326 usb0_host_CtrlReadStart((uint32_t)0, (uint8_t *)&Buf1
);
334 /*******************************************************************************
335 * Function Name: usb0_host_CtrlWriteStart
336 * Description : Executes USB control transfer/data stage(write).
337 * Arguments : uint32_t Bsize ; Data Size
338 * : uint8_t *Table ; Data Table Address
339 * Return Value : USB_HOST_WRITESHRT ; End of data write
340 * : USB_HOST_WRITEEND ; End of data write (not null)
341 * : USB_HOST_WRITING ; Continue of data write
342 * : USB_HOST_FIFOERROR ; FIFO access error
343 *******************************************************************************/
344 uint16_t usb0_host_CtrlWriteStart (uint32_t Bsize
, uint8_t * Table
)
349 g_usb0_host_CmdStage
&= (~USB_HOST_CMD_FIELD
);
350 g_usb0_host_CmdStage
|= USB_HOST_CMD_DOING
;
352 usb0_host_set_pid_nak(USB_HOST_PIPE0
); /* Set NAK */
353 g_usb0_host_data_count
[USB_HOST_PIPE0
] = Bsize
; /* Transfer size set */
354 g_usb0_host_data_pointer
[USB_HOST_PIPE0
] = Table
; /* Transfer address set */
356 USB200
.DCPCTR
= USB_HOST_BITSQSET
; /* SQSET=1, PID=NAK */
357 #if(1) /* ohci_wrapp */
358 Userdef_USB_usb0_host_delay_10us(3);
360 RZA_IO_RegWrite_16(&USB200
.DCPCFG
,
362 USB_DCPCFG_DIR_SHIFT
,
365 mbw
= usb0_host_get_mbw(g_usb0_host_data_count
[USB_HOST_PIPE0
], (uint32_t)g_usb0_host_data_pointer
[USB_HOST_PIPE0
]);
366 usb0_host_set_curpipe(USB_HOST_PIPE0
, USB_HOST_CUSE
, USB_HOST_BITISEL
, mbw
);
367 USB200
.CFIFOCTR
= USB_HOST_BITBCLR
; /* Buffer Clear */
369 usb0_host_clear_pid_stall(USB_HOST_PIPE0
);
370 EndFlag_K
= usb0_host_write_buffer_c(USB_HOST_PIPE0
);
371 /* Host Control sequence */
374 case USB_HOST_WRITESHRT
: /* End of data write */
375 g_usb0_host_CmdStage
&= (~USB_HOST_STAGE_FIELD
);
376 g_usb0_host_CmdStage
|= USB_HOST_STAGE_STATUS
;
377 usb0_host_enable_nrdy_int(USB_HOST_PIPE0
); /* Error (NORES or STALL) */
378 usb0_host_enable_bemp_int(USB_HOST_PIPE0
); /* Enable Empty Interrupt */
381 case USB_HOST_WRITEEND
: /* End of data write (not null) */
382 case USB_HOST_WRITING
: /* Continue of data write */
383 usb0_host_enable_nrdy_int(USB_HOST_PIPE0
); /* Error (NORES or STALL) */
384 usb0_host_enable_bemp_int(USB_HOST_PIPE0
); /* Enable Empty Interrupt */
387 case USB_HOST_FIFOERROR
: /* FIFO access error */
393 usb0_host_set_pid_buf(USB_HOST_PIPE0
); /* Set BUF */
394 return (EndFlag_K
); /* End or Err or Continue */
397 /*******************************************************************************
398 * Function Name: usb0_host_CtrlReadStart
399 * Description : Executes USB control transfer/data stage(read).
400 * Arguments : uint32_t Bsize ; Data Size
401 * : uint8_t *Table ; Data Table Address
402 * Return Value : none
403 *******************************************************************************/
404 void usb0_host_CtrlReadStart (uint32_t Bsize
, uint8_t * Table
)
408 g_usb0_host_CmdStage
&= (~USB_HOST_CMD_FIELD
);
409 g_usb0_host_CmdStage
|= USB_HOST_CMD_DOING
;
411 usb0_host_set_pid_nak(USB_HOST_PIPE0
); /* Set NAK */
412 g_usb0_host_data_count
[USB_HOST_PIPE0
] = Bsize
; /* Transfer size set */
413 g_usb0_host_data_pointer
[USB_HOST_PIPE0
] = Table
; /* Transfer address set */
415 USB200
.DCPCTR
= USB_HOST_BITSQSET
; /* SQSET=1, PID=NAK */
416 #if(1) /* ohci_wrapp */
417 Userdef_USB_usb0_host_delay_10us(3);
419 RZA_IO_RegWrite_16(&USB200
.DCPCFG
,
421 USB_DCPCFG_DIR_SHIFT
,
424 mbw
= usb0_host_get_mbw(g_usb0_host_data_count
[USB_HOST_PIPE0
], (uint32_t)g_usb0_host_data_pointer
[USB_HOST_PIPE0
]);
425 usb0_host_set_curpipe(USB_HOST_PIPE0
, USB_HOST_CUSE
, USB_HOST_NO
, mbw
);
426 USB200
.CFIFOCTR
= USB_HOST_BITBCLR
; /* Buffer Clear */
428 usb0_host_enable_nrdy_int(USB_HOST_PIPE0
); /* Error (NORES or STALL) */
429 usb0_host_enable_brdy_int(USB_HOST_PIPE0
); /* Ok */
430 usb0_host_clear_pid_stall(USB_HOST_PIPE0
);
431 usb0_host_set_pid_buf(USB_HOST_PIPE0
); /* Set BUF */