usb_drv.c File Reference

#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

U8 usb_config_ep (U8 config0, U8 config1)
 usb_configure_endpoint.
U8 usb_select_enpoint_interrupt (void)
 usb_select_endpoint_interrupt.
U8 usb_send_packet (U8 ep_num, U8 *tbuf, U8 data_length)
 usb_send_packet.
U8 usb_read_packet (U8 ep_num, U8 *rbuf, U8 data_length)
 usb_read_packet.
void usb_halt_endpoint (U8 ep_num)
 usb_halt_endpoint.
U8 usb_init_device (void)
 usb_init_device.
U8 host_config_pipe (U8 config0, U8 config1)
 --------------------------------------------------------- ------------------ HOST --------------------------------- ---------------------------------------------------------
U8 host_determine_pipe_size (U16 size)
 host_determine_pipe_size.
void host_disable_all_pipe (void)
 host_disable_all_pipe.
U8 usb_get_nb_pipe_interrupt (void)


Detailed Description

This file contains the USB driver routines.

Author:
Atmel Corporation: http://www.atmel.com
Support and FAQ: http://support.atmel.no/

Definition in file usb_drv.c.


Function Documentation

U8 usb_config_ep ( U8  config0,
U8  config1 
)

usb_configure_endpoint.

This function configures an endpoint with the selected type.

Parameters:
config0 
config1 
Returns:
Is_endpoint_configured.

Definition at line 70 of file usb_drv.c.

00071 {
00072     Usb_enable_endpoint();
00073     UECFG0X = config0;
00074     UECFG1X = (UECFG1X & (1<<ALLOC)) | config1;
00075     Usb_allocate_memory();
00076     return (Is_endpoint_configured());
00077 }

U8 usb_select_enpoint_interrupt ( void   ) 

usb_select_endpoint_interrupt.

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.

Parameters:
none 
Returns:
endpoint number.

Definition at line 90 of file usb_drv.c.

00091 {
00092 U8 interrupt_flags;
00093 U8 ep_num;
00094 
00095    ep_num = 0;
00096    interrupt_flags = Usb_interrupt_flags();
00097 
00098    while(ep_num < MAX_EP_NB)
00099    {
00100       if (interrupt_flags & 1)
00101       {
00102          return (ep_num);
00103       }
00104       else
00105       {
00106          ep_num++;
00107          interrupt_flags = interrupt_flags >> 1;
00108       }
00109    }
00110    return 0;
00111 }

U8 usb_send_packet ( U8  ep_num,
U8 tbuf,
U8  data_length 
)

usb_send_packet.

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

Parameters:
ep_num number of the addressed endpoint
*tbuf address of the first data to send
data_length number of bytes to send
Returns:
address of the next U8 to send.
Example: usb_send_packet(3,&first_data,0x20); // send packet on the endpoint #3 while(!(Usb_tx_complete)); // wait packet ACK'ed by the Host Usb_clear_tx_complete(); // acknowledge the transmit

Note: tbuf is incremented of 'data_length'.

Definition at line 133 of file usb_drv.c.

00134 {
00135 U8 remaining_length;
00136 
00137    remaining_length = data_length;
00138    Usb_select_endpoint(ep_num);
00139    while(Is_usb_write_enabled() && (0 != remaining_length))
00140    {
00141       Usb_write_byte(*tbuf);
00142       remaining_length--;
00143       tbuf++;
00144    }
00145    return remaining_length;
00146 }

U8 usb_read_packet ( U8  ep_num,
U8 rbuf,
U8  data_length 
)

usb_read_packet.

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

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
Returns:
address of the next U8 to send.
Example: while(!(Usb_rx_complete)); // wait new packet received usb_read_packet(4,&first_data,usb_get_nb_byte); // read packet from ep 4 Usb_clear_rx(); // acknowledge the transmit

Note: rbuf is incremented of 'data_length'.

Definition at line 168 of file usb_drv.c.

00169 {
00170 U8 remaining_length;
00171 
00172    remaining_length = data_length;
00173    Usb_select_endpoint(ep_num);
00174 
00175    while(Is_usb_read_enabled() && (0 != remaining_length))
00176    {
00177       *rbuf = Usb_read_byte();
00178       remaining_length--;
00179       rbuf++;
00180    }
00181    return remaining_length;
00182 }

void usb_halt_endpoint ( U8  ep_num  ) 

usb_halt_endpoint.

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
Returns:
none

Definition at line 194 of file usb_drv.c.

00195 {
00196    Usb_select_endpoint(ep_num);
00197    Usb_enable_stall_handshake();
00198 }

U8 usb_init_device ( void   ) 

usb_init_device.

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

Parameters:
none 
Returns:
status

Definition at line 210 of file usb_drv.c.

Referenced by usb_general_interrupt(), and usb_start_device().

00211 {
00212    Usb_select_device();
00213    if(Is_usb_id_device())
00214    {
00215       Usb_select_endpoint(EP_CONTROL);
00216       if(!Is_usb_endpoint_enabled())
00217       {
00218 #if (USB_LOW_SPEED_DEVICE==DISABLE)
00219          return usb_configure_endpoint(EP_CONTROL,    \
00220                                 TYPE_CONTROL,  \
00221                                 DIRECTION_OUT, \
00222                                 SIZE_64,       \
00223                                 ONE_BANK,      \
00224                                 NYET_DISABLED);
00225 #else
00226          return usb_configure_endpoint(EP_CONTROL,    \
00227                                 TYPE_CONTROL,  \
00228                                 DIRECTION_OUT, \
00229                                 SIZE_8,       \
00230                                 ONE_BANK,      \
00231                                 NYET_DISABLED);
00232 #endif
00233       }
00234    }
00235    return FALSE;
00236 }

Here is the caller graph for this function:

U8 host_config_pipe ( U8  config0,
U8  config1 
)

--------------------------------------------------------- ------------------ HOST --------------------------------- ---------------------------------------------------------

usb_configure_pipe.

This function configures a pipe with the selected type.

Parameters:
config0 
config1 
Returns:
Is_endpoint_configured.

Definition at line 255 of file usb_drv.c.

00256 {
00257     Host_enable_pipe();
00258     UPCFG0X = config0;
00259     UPCFG1X = config1;
00260     Host_allocate_memory();
00261     return (Is_pipe_configured());
00262 }

U8 host_determine_pipe_size ( U16  size  ) 

host_determine_pipe_size.

This function returns the size configuration register value according to the endpint size detected inthe device enumeration process.

Returns:
pipe size register value.

Definition at line 271 of file usb_drv.c.

Referenced by host_auto_configure_endpoint().

00272 {
00273         if(size <= 8  ) {return (SIZE_8   );}
00274    else if(size <= 16 ) {return (SIZE_16  );}
00275    else if(size <= 32 ) {return (SIZE_32  );}
00276    else if(size <= 64 ) {return (SIZE_64  );}
00277    else if(size <= 128) {return (SIZE_128 );}
00278    else if(size <= 256) {return (SIZE_256 );}
00279    else if(size <= 512) {return (SIZE_512 );}
00280    else                 {return (SIZE_1024);}
00281 
00282 }

Here is the caller graph for this function:

void host_disable_all_pipe ( void   ) 

host_disable_all_pipe.

This function disable all pipes for the host controller Usefull to execute upon device disconnection.

Returns:
none.

Definition at line 291 of file usb_drv.c.

Referenced by usb_general_interrupt().

00292 {
00293 U8 i;
00294    for (i=0;i<7;i++)
00295    {
00296       Host_reset_pipe(i);
00297       Host_select_pipe(i);
00298       Host_unallocate_memory();
00299       Host_disable_pipe();
00300    }
00301 }

Here is the caller graph for this function:

U8 usb_get_nb_pipe_interrupt ( void   ) 

Returns the pipe number that generates a USB communication interrupt This function sould be called only when an interrupt has been detected. Otherwize the return value is incorect

Parameters:
none 
Returns:
pipe_number

Definition at line 312 of file usb_drv.c.

Referenced by usb_pipe_interrupt().

00313 {
00314 U8 interrupt_flags;
00315 U8 i;
00316 
00317    interrupt_flags = Host_get_pipe_interrupt();
00318    for(i=0;i< MAX_EP_NB;i++)
00319    {
00320       if (interrupt_flags & (1<<i))
00321       {
00322          return (i);
00323       }
00324    }
00325    // This return should never occurs ....
00326    return MAX_EP_NB+1;
00327 }

Here is the caller graph for this function:


Generated on Wed Sep 23 09:17:16 2009 for ATMEL by  doxygen 1.5.3