Xmega Application Note


usb_drv.c File Reference


Detailed Description

This file contains the USB driver routines.

Application note:
AVR1907: Xplain Evaluation Board
Documentation
For comprehensive code documentation, supported compilers, compiler settings and supported devices see readme.html
Author:
Atmel Corporation: http://www.atmel.com
Support email: avr@atmel.com
Revision
3122
Date
2010-01-13 13:26:22 +0100 (on, 13 jan 2010)

Copyright (c) 2010, Atmel Corporation All rights reserved.

Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:

1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.

2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.

3. The name of ATMEL may not be used to endorse or promote products derived from this software without specific prior written permission.

THIS SOFTWARE IS PROVIDED BY ATMEL "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE EXPRESSLY AND SPECIFICALLY DISCLAIMED. IN NO EVENT SHALL ATMEL BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

Definition in file usb_drv.c.

#include "config.h"
#include "conf_usb.h"
#include "usb_drv.h"

Include dependency graph for usb_drv.c:

Go to the source code of this file.

Functions

uint8_t usb_config_ep (uint8_t config0, uint8_t config1)
 This function configures an endpoint with the selected type.
void usb_halt_endpoint (uint8_t ep_num)
 This function sends a STALL handshake for the next Host request. A STALL handshake will be send for each next request untill a SETUP or a Clear Halt Feature occurs for this endpoint.
uint8_t usb_init_device (void)
 This function initializes the USB device controller and configures the Default Control Endpoint.
uint8_t usb_read_packet (uint8_t ep_num, uint8_t *rbuf, uint8_t data_length)
 This function moves the data stored in the selected endpoint fifo to the address specified by *rbuf.
uint8_t usb_select_enpoint_interrupt (void)
 This function select the endpoint where an event occurs and returns the number of this endpoint. If no event occurs on the endpoints, this function returns 0.
uint8_t usb_send_packet (uint8_t ep_num, uint8_t *tbuf, uint8_t data_length)
 This function moves the data pointed by tbuf to the selected endpoint fifo and sends it through the USB.


Function Documentation

uint8_t usb_config_ep ( uint8_t  config0,
uint8_t  config1 
)

This function configures an endpoint with the selected type.

Parameters:
config0 
config1 
Return values:
Is_endpoint_configured. 

Definition at line 59 of file usb_drv.c.

References Is_endpoint_configured, Usb_allocate_memory, and Usb_enable_endpoint.

00060 {
00061         Usb_enable_endpoint();
00062         UECFG0X = config0;
00063         UECFG1X = (UECFG1X & (1<<ALLOC)) | config1;
00064         Usb_allocate_memory();
00065         return (Is_endpoint_configured());
00066 }

void usb_halt_endpoint ( uint8_t  ep_num  ) 

This function sends a STALL handshake for the next Host request. A STALL handshake will be send for each next request untill a SETUP or a Clear Halt Feature occurs for this endpoint.

Parameters:
ep_num number of the addressed endpoint

Definition at line 153 of file usb_drv.c.

References Usb_enable_stall_handshake, and Usb_select_endpoint.

00154 {
00155         Usb_select_endpoint(ep_num);
00156         Usb_enable_stall_handshake();
00157 }

uint8_t usb_init_device ( void   ) 

This function initializes the USB device controller and configures the Default Control Endpoint.

Return values:
status 

Definition at line 165 of file usb_drv.c.

References DIRECTION_OUT, EP_CONTROL, Is_usb_endpoint_enabled, Is_usb_id_device, NYET_DISABLED, ONE_BANK, SIZE_64, TYPE_CONTROL, usb_configure_endpoint, Usb_select_device, and Usb_select_endpoint.

Referenced by ISR(), and usb_start_device().

00166 {
00167         Usb_select_device();
00168         if(Is_usb_id_device())
00169         {
00170                 Usb_select_endpoint(EP_CONTROL);
00171                 if(!Is_usb_endpoint_enabled())
00172                 {
00173                         return usb_configure_endpoint(EP_CONTROL,    \
00174                                                       TYPE_CONTROL,  \
00175                                                       DIRECTION_OUT, \
00176                                                       SIZE_64,       \
00177                                                       ONE_BANK,      \
00178                                                       NYET_DISABLED);
00179                 }
00180         }
00181         return false;
00182 }

uint8_t usb_read_packet ( uint8_t  ep_num,
uint8_t *  rbuf,
uint8_t  data_length 
)

This function moves the data stored in the selected endpoint fifo to the address specified by *rbuf.

Note:
rbuf is incremented of 'data_length'.
Parameters:
ep_num number of the addressed endpoint
rbuf aaddress of the first data to write with the USB data
data_length number of bytes to read
Return values:
address of the next uint8_t to send.

Definition at line 131 of file usb_drv.c.

References Is_usb_read_enabled, Usb_read_byte, and Usb_select_endpoint.

00132 {
00133         uint8_t remaining_length;
00134         
00135         remaining_length = data_length;
00136         Usb_select_endpoint(ep_num);
00137         
00138         while(Is_usb_read_enabled() && (0 != remaining_length)){
00139                 *rbuf = Usb_read_byte();
00140                 remaining_length--;
00141                 rbuf++;
00142         }
00143         return remaining_length;
00144 }

uint8_t usb_select_enpoint_interrupt ( void   ) 

This function select the endpoint where an event occurs and returns the number of this endpoint. If no event occurs on the endpoints, this function returns 0.

Return values:
endpoint number.

Definition at line 75 of file usb_drv.c.

References Usb_interrupt_flags.

00076 {
00077         uint8_t interrupt_flags;
00078         uint8_t ep_num;
00079         
00080         ep_num = 0;
00081         interrupt_flags = Usb_interrupt_flags();
00082         
00083         while(ep_num < 9){
00084                 if (interrupt_flags & 1){
00085                         return (ep_num);
00086                 }else{
00087                         ep_num++;
00088                         interrupt_flags = interrupt_flags >> 1;
00089                 }
00090         }
00091         return 0;
00092 }

uint8_t usb_send_packet ( uint8_t  ep_num,
uint8_t *  tbuf,
uint8_t  data_length 
)

This function moves the data pointed by tbuf to the selected endpoint fifo and sends it through the USB.

Note:
tbuf is incremented of 'data_length'.
Parameters:
ep_num number of the addressed endpoint
*tbuf address of the first data to send
data_length number of bytes to send
Return values:
address of the next byte to send.

Definition at line 106 of file usb_drv.c.

References Is_usb_write_enabled, Usb_select_endpoint, and Usb_write_byte.

00107 {
00108         uint8_t remaining_length;
00109         
00110         remaining_length = data_length;
00111         Usb_select_endpoint(ep_num);
00112         while(Is_usb_write_enabled() && (0 != remaining_length)){
00113                 Usb_write_byte(*tbuf);
00114                 remaining_length--;
00115                 tbuf++;
00116         }
00117         return remaining_length;
00118 }

@DOC_TITLE@
Generated on Mon Jan 18 09:26:12 2010 for AVR1907 Xplain USB Gateway by doxygen 1.5.5