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_api.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"
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_api_function_init
71 * Description : Initializes the USB module in the USB function mode.
72 * Arguments : uint8_t int_level ; interruput level
73 * : uint16_t mode : Speed modes
74 * : : USB_FUCNTION_HIGH_SPEED: High-speed device
75 * : : USB_FUCNTION_FULL_SPEED: Full-speed device
76 * : uint16_t clockmode ; 48MHz ; USBFCLOCK_X1_48MHZ
77 * : ; 12MHz ; USBFCLOCK_EXTAL_12MHZ
79 *******************************************************************************/
81 void usb0_api_function_init (uint8_t int_level
, uint16_t mode
, uint16_t clockmode
)
83 volatile uint8_t dummy_buf
;
85 CPG
.STBCR7
&= 0xfd; /* The clock of USB0 modules is permitted */
86 dummy_buf
= CPG
.STBCR7
; /* (Dummy read) */
88 usb0_function_setting_interrupt(int_level
);
90 usb0_function_reset_module(clockmode
); /* reset USB module with setting tranciever */
93 usb0_function_init_status(); /* clear variables */
95 usb0_function_InitModule(mode
); /* select USB Function and Interrupt Enable */
96 /* Detect USB Device to attach or detach */
100 /*******************************************************************************
101 * Function Name: usb0_api_function_IsConfigured
102 * Description : Checks if the USB device is configured to return the result as
103 * : the return value.
105 * Return Value : DEVDRV_USBF_YES : Configured & Configured Suspend
106 * : DEVDRV_USBF_NO : not Configured
107 *******************************************************************************/
108 uint16_t usb0_api_function_IsConfigured (void)
112 dvst
= usb0_function_GetDeviceState();
114 if ((dvst
== USB_FUNCTION_DVST_CONFIGURED
) ||
115 (dvst
== USB_FUNCTION_DVST_CONFIGURED_SUSPEND
))
117 return DEVDRV_USBF_YES
;
120 return DEVDRV_USBF_NO
;
123 /*******************************************************************************
124 * Function Name: usb0_function_GetDeviceState
125 * Description : Returns the state of USB device.
127 * Return Value : Device States
128 *******************************************************************************/
129 uint16_t usb0_function_GetDeviceState (void)
134 dvsq
= USB200
.INTSTS0
;
135 switch(dvsq
& USB_FUNCTION_BITDVSQ
)
137 case USB_FUNCTION_DS_POWR
: /* Power state *//* power-on */
138 dvst
= USB_FUNCTION_DVST_POWERED
;
141 case USB_FUNCTION_DS_DFLT
: /* Default state *//* bus-reset */
142 dvst
= USB_FUNCTION_DVST_DEFAULT
;
145 case USB_FUNCTION_DS_ADDS
: /* Address state */
146 dvst
= USB_FUNCTION_DVST_ADDRESS
;
149 case USB_FUNCTION_DS_CNFG
: /* Configured state */
150 dvst
= USB_FUNCTION_DVST_CONFIGURED
;
153 case USB_FUNCTION_DS_SPD_CNFG
: /* Configured Suspend state */
154 dvst
= USB_FUNCTION_DVST_CONFIGURED_SUSPEND
;
157 case USB_FUNCTION_DS_SPD_POWR
: /* Power Suspend state */
158 case USB_FUNCTION_DS_SPD_DFLT
: /* Default Suspend state */
159 case USB_FUNCTION_DS_SPD_ADDR
: /* Address Suspend state */
160 dvst
= USB_FUNCTION_DVST_SUSPEND
;
164 dvst
= USB_FUNCTION_DVST_SUSPEND
;
171 /*******************************************************************************
172 * Function Name: usb0_api_function_start_receive_transfer
173 * Description : Starts USB data reception using the pipe specified in the argument.
174 * : The FIFO for using is set in the pipe definition table.
175 * Arguments : uint16_t pipe ; Pipe Number
176 * : uint32_t size ; Data Size
177 * : uint8_t *data ; Data data Address
178 * Return Value : none
179 *******************************************************************************/
180 void usb0_api_function_start_receive_transfer (uint16_t pipe
, uint32_t size
, uint8_t * data
)
182 usb0_function_start_receive_transfer(pipe
, size
, data
);
185 /*******************************************************************************
186 * Function Name: usb0_api_function_start_send_transfer
187 * Description : Starts the USB data communication using pipe specified by the argument.
188 * Arguments : uint16_t pipe ; Pipe Number
189 * : uint32_t size ; Data Size
190 * : uint8_t *data ; Data data Address
191 * Return Value : DEVDRV_USBF_WRITEEND ; Write end
192 * : DEVDRV_USBF_WRITESHRT ; short data
193 * : DEVDRV_USBF_WRITING ; Continue of data write
194 * : DEVDRV_USBF_WRITEDMA ; Write DMA
195 * : DEVDRV_USBF_FIFOERROR ; FIFO status
196 *******************************************************************************/
197 uint16_t usb0_api_function_start_send_transfer (uint16_t pipe
, uint32_t size
, uint8_t * data
)
201 status
= usb0_function_start_send_transfer(pipe
, size
, data
);
206 /*******************************************************************************
207 * Function Name: usb0_api_function_check_pipe_status
208 * Description : Starts USB data reception using the pipe specified in the argument.
209 * : The FIFO for using is set in the pipe definition table.
210 * Arguments : uint16_t pipe ; Pipe Number
211 * : uint32_t *size ; Data Size
212 * Return Value : Pipe Status
213 *******************************************************************************/
214 uint16_t usb0_api_function_check_pipe_status (uint16_t pipe
, uint32_t * size
)
216 if (g_usb0_function_pipe_status
[pipe
] == DEVDRV_USBF_PIPE_DONE
)
218 *size
= g_usb0_function_PipeDataSize
[pipe
];
219 g_usb0_function_pipe_status
[pipe
] = DEVDRV_USBF_PIPE_IDLE
;
221 return DEVDRV_USBF_PIPE_DONE
;
223 else if (g_usb0_function_pipe_status
[pipe
] == DEVDRV_USBF_PIPE_NORES
)
226 g_usb0_function_pipe_status
[pipe
] = DEVDRV_USBF_PIPE_IDLE
;
228 return DEVDRV_USBF_PIPE_NORES
;
230 else if (g_usb0_function_pipe_status
[pipe
] == DEVDRV_USBF_PIPE_STALL
)
233 g_usb0_function_pipe_status
[pipe
] = DEVDRV_USBF_PIPE_IDLE
;
235 return DEVDRV_USBF_PIPE_STALL
;
237 else if (g_usb0_function_pipe_status
[pipe
] == DEVDRV_USBF_FIFOERROR
)
240 g_usb0_function_pipe_status
[pipe
] = DEVDRV_USBF_PIPE_IDLE
;
242 return DEVDRV_USBF_FIFOERROR
;
249 return g_usb0_function_pipe_status
[pipe
];
252 /*******************************************************************************
253 * Function Name: usb0_api_function_clear_pipe_status
254 * Description : Starts USB data reception using the pipe specified in the argument.
255 * : The FIFO for using is set in the pipe definition table.
256 * Arguments : uint16_t pipe ; Pipe Number
257 * Return Value : Pipe Status
258 *******************************************************************************/
259 void usb0_api_function_clear_pipe_status (uint16_t pipe
)
261 g_usb0_function_pipe_status
[pipe
] = DEVDRV_USBF_PIPE_IDLE
;
262 g_usb0_function_PipeDataSize
[pipe
] = 0;
265 /*******************************************************************************
266 * Function Name: usb0_api_function_set_pid_buf
267 * Description : Enables communicaqtion in the pipe specified by the argument
269 * Arguments : uint16_t pipe ; pipe Number
270 * Return Value : none
271 *******************************************************************************/
272 void usb0_api_function_set_pid_buf (uint16_t pipe
)
274 usb0_function_set_pid_buf(pipe
);
277 /*******************************************************************************
278 * Function Name: usb0_api_function_set_pid_nak
279 * Description : Disables communication (NAK) in the pipe specified by the argument.
280 * : When the pipe status was enabling communication (BUF) before
281 * : executing before executing this function, waits in the software
282 * : until the pipe becomes ready after setting disabled.
283 * Arguments : uint16_t pipe ; pipe Number
284 * Return Value : none
285 *******************************************************************************/
286 void usb0_api_function_set_pid_nak (uint16_t pipe
)
288 usb0_function_set_pid_nak(pipe
);
291 /*******************************************************************************
292 * Function Name: usb0_api_function_set_pid_stall
293 * Description : Disables communication (STALL) in the pipe specified by the
295 * Arguments : uint16_t pipe ; pipe Number
296 * Return Value : none
297 *******************************************************************************/
298 void usb0_api_function_set_pid_stall (uint16_t pipe
)
300 usb0_function_set_pid_stall(pipe
);
303 /*******************************************************************************
304 * Function Name: usb0_api_function_clear_pid_stall
305 * Description : Disables communication (NAK) in the pipe specified by the argument.
306 * Arguments : uint16_t pipe ; pipe Number
307 * Return Value : none
308 *******************************************************************************/
309 void usb0_api_function_clear_pid_stall (uint16_t pipe
)
311 usb0_function_clear_pid_stall(pipe
);
314 /*******************************************************************************
315 * Function Name: usb0_api_function_get_pid
316 * Description : Returns the pipe state specified by the argument.
317 * Arguments : uint16_t pipe ; Pipe Number
319 *******************************************************************************/
320 uint16_t usb0_api_function_get_pid (uint16_t pipe
)
324 pid
= usb0_function_get_pid(pipe
);
329 /*******************************************************************************
330 * Function Name: usb0_api_function_check_stall
332 * Arguments : uint16_t pipe ; Pipe Number
334 *******************************************************************************/
335 int32_t usb0_api_function_check_stall (uint16_t pipe
)
339 pid
= usb0_function_get_pid(pipe
);
341 if ((pid
& DEVDRV_USBF_PID_STALL
) == DEVDRV_USBF_PID_STALL
)
343 return DEVDRV_USBF_STALL
;
346 return DEVDRV_SUCCESS
;
349 /*******************************************************************************
350 * Function Name: usb0_api_function_set_sqclr
351 * Description : Sets the sequence bit of the pipe specified by the argument to
353 * Arguments : uint16_t pipe ; Pipe Number
354 * Return Value : none
355 *******************************************************************************/
356 void usb0_api_function_set_sqclr (uint16_t pipe
)
358 usb0_function_set_sqclr(pipe
);
361 /*******************************************************************************
362 * Function Name: usb0_api_function_set_sqset
363 * Description : Sets the sequence bit of the pipe specified by the argument to
365 * Arguments : uint16_t pipe ; Pipe number
366 * Return Value : none
367 *******************************************************************************/
368 void usb0_api_function_set_sqset (uint16_t pipe
)
370 usb0_function_set_sqset(pipe
);
373 /*******************************************************************************
374 * Function Name: usb0_api_function_set_csclr
375 * Description : CSPLIT status clear setting of sprit transaction in specified
376 * : pipe is performed.
377 * : When SQSET bit or SQCLR bit, and SQSET bit or SQCLR bit
378 * : in DCPCTR register are continuously changed (when the sequence
379 * : toggle bit of data PID is continuously changed over two or more pipes),
380 * : the access cycle with 120 ns and more than 5 cycle bus clock is necessary.
381 * : Do not set both SQCLR bit and SQSET bit to 1 at the same time.
382 * : In addition, both bits should be operated after PID is set to NAK.
383 * : However, when it is set to the isochronous transfer as the transfer type
384 * : (TYPE=11), writing in SQSET bit is disabled.
385 * Arguments : uint16_t pipe ; Pipe number
386 * Return Value : none
387 *******************************************************************************/
388 void usb0_api_function_set_csclr (uint16_t pipe
)
390 usb0_function_set_csclr(pipe
);
393 /*******************************************************************************
394 * Function Name: usb0_api_function_set_curpipe
395 * Description : Allocates FIF0 specifed by the argument in the pipe assigned
397 * Arguments : uint16_t pipe ; Pipe Number
398 * : uint16_t fifosel ; Select FIFO
399 * : uint16_t isel ; FIFO Access Direction
400 * : uint16_t mbw ; FIFO Port Access Bit Width
401 * Return Value : none
402 *******************************************************************************/
403 void usb0_api_function_set_curpipe (uint16_t pipe
, uint16_t fifosel
, uint16_t isel
, uint16_t mbw
)
405 usb0_function_set_curpipe(pipe
, fifosel
, isel
, mbw
);
408 /*******************************************************************************
409 * Function Name: usb0_api_function_clear_brdy_sts
410 * Description : Clear BRDY interrupt status in the pipe spceified by the argument.
411 * Arguments : uint16_t pipe ; pipe Number
412 * Return Value : none
413 *******************************************************************************/
414 void usb0_api_function_clear_brdy_sts (uint16_t pipe
)
416 usb0_function_clear_brdy_sts(pipe
);
419 /*******************************************************************************
420 * Function Name: usb0_api_function_clear_bemp_sts
421 * Description : Clear BEMP interrupt status in the pipe spceified by the argument.
422 * Arguments : uint16_t pipe ; pipe Number
423 * Return Value : none
424 *******************************************************************************/
425 void usb0_api_function_clear_bemp_sts (uint16_t pipe
)
427 usb0_function_clear_bemp_sts(pipe
);
430 /*******************************************************************************
431 * Function Name: usb0_api_function_clear_nrdy_sts
432 * Description : Clear NRDY interrupt status in the pipe spceified by the argument.
433 * Arguments : uint16_t pipe ; pipe Number
434 * Return Value : none
435 *******************************************************************************/
436 void usb0_api_function_clear_nrdy_sts (uint16_t pipe
)
438 usb0_function_clear_nrdy_sts(pipe
);