usb_drv.c File Reference

This file contains the USB driver routines. More...

#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)
U8 usb_select_enpoint_interrupt (void)
U8 usb_send_packet (U8 ep_num, U8 *tbuf, U8 data_length)
U8 usb_read_packet (U8 ep_num, U8 *rbuf, U8 data_length)
void usb_halt_endpoint (U8 ep_num)
U8 usb_init_device (void)
U8 host_config_pipe (U8 config0, U8 config1)
U8 host_determine_pipe_size (U16 size)
void host_disable_all_pipe (void)
U8 usb_get_nb_pipe_interrupt (void)
 Returns the pipe number that generates a USB communication interrupt.


Detailed Description

This file contains the USB driver routines.

,v

Copyright (c) 2006 Atmel.

Use of this program is subject to Atmel's End User License Agreement. Please read file license.txt for copyright notice.

This file contains the USB driver routines.

Version:
1.11 at90usb128-demo-hidgen-std-2_0_0
Id
usb_drv.c,v 1.11 2006/07/27 07:02:03 rletendu Exp
Todo:
Bug:

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 43 of file usb_drv.c.

References ALLOC, Is_endpoint_configured, Usb_allocate_memory, and Usb_enable_endpoint.

00044 {
00045     Usb_enable_endpoint();
00046     UECFG0X = config0;
00047     UECFG1X = (UECFG1X & (1<<ALLOC)) | config1;
00048     Usb_allocate_memory();
00049     return (Is_endpoint_configured());
00050 }

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 63 of file usb_drv.c.

References ep_num, MAX_EP_NB, and Usb_interrupt_flags.

00064 {
00065 U8 interrupt_flags;
00066 U8 ep_num;
00067 
00068    ep_num = 0;
00069    interrupt_flags = Usb_interrupt_flags();
00070 
00071    while(ep_num < MAX_EP_NB)
00072    {
00073       if (interrupt_flags & 1)
00074       {
00075          return (ep_num);
00076       }
00077       else
00078       {
00079          ep_num++;
00080          interrupt_flags = interrupt_flags >> 1;
00081       }
00082    }
00083    return 0;
00084 }

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 106 of file usb_drv.c.

References Is_usb_write_enabled, Usb_select_endpoint, and Usb_write_byte.

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

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 141 of file usb_drv.c.

References Is_usb_read_enabled, Usb_read_byte, and Usb_select_endpoint.

00142 {
00143 U8 remaining_length;
00144 
00145    remaining_length = data_length;
00146    Usb_select_endpoint(ep_num);
00147 
00148    while(Is_usb_read_enabled() && (0 != remaining_length))
00149    {
00150       *rbuf = Usb_read_byte();
00151       remaining_length--;
00152       rbuf++;
00153    }
00154    return remaining_length;
00155 }

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 167 of file usb_drv.c.

References Usb_enable_stall_handshake, and Usb_select_endpoint.

00168 {
00169    Usb_select_endpoint(ep_num);
00170    Usb_enable_stall_handshake();
00171 }

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 183 of file usb_drv.c.

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

Referenced by usb_general_interrupt(), and usb_start_device().

00184 {
00185    Usb_select_device();
00186    if(Is_usb_id_device())
00187    {
00188       Usb_select_endpoint(EP_CONTROL);
00189       if(!Is_usb_endpoint_enabled())
00190       {
00191 #if (USB_LOW_SPEED_DEVICE==DISABLE)
00192          return usb_configure_endpoint(EP_CONTROL,    \
00193                                 TYPE_CONTROL,  \
00194                                 DIRECTION_OUT, \
00195                                 SIZE_64,       \
00196                                 ONE_BANK,      \
00197                                 NYET_DISABLED);
00198 #else
00199          return usb_configure_endpoint(EP_CONTROL,    \
00200                                 TYPE_CONTROL,  \
00201                                 DIRECTION_OUT, \
00202                                 SIZE_8,       \
00203                                 ONE_BANK,      \
00204                                 NYET_DISABLED);
00205 #endif
00206       }
00207    }
00208    return FALSE;
00209 }

U8 host_config_pipe ( U8  config0,
U8  config1 
)

usb_configure_pipe.

This function configures a pipe with the selected type.

Parameters:
config0 
config1 
Returns:
Is_endpoint_configured.

Definition at line 228 of file usb_drv.c.

References Host_allocate_memory, Host_enable_pipe, and Is_pipe_configured.

00229 {
00230     Host_enable_pipe();
00231     UPCFG0X = config0;
00232     UPCFG1X = config1;
00233     Host_allocate_memory();
00234     return (Is_pipe_configured());
00235 }

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 244 of file usb_drv.c.

References SIZE_1024, SIZE_128, SIZE_16, SIZE_256, SIZE_32, SIZE_512, SIZE_64, and SIZE_8.

00245 {
00246         if(size <= 8  ) {return (SIZE_8   );}
00247    else if(size <= 16 ) {return (SIZE_16  );}
00248    else if(size <= 32 ) {return (SIZE_32  );}
00249    else if(size <= 64 ) {return (SIZE_64  );}
00250    else if(size <= 128) {return (SIZE_128 );}
00251    else if(size <= 256) {return (SIZE_256 );}
00252    else if(size <= 512) {return (SIZE_512 );}
00253    else                 {return (SIZE_1024);}
00254 
00255 }

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 264 of file usb_drv.c.

References Host_disable_pipe, Host_reset_pipe, Host_select_pipe, and Host_unallocate_memory.

Referenced by usb_general_interrupt().

00265 {
00266 U8 i;
00267    for (i=0;i<7;i++)
00268    {
00269       Host_reset_pipe(i);
00270       Host_select_pipe(i);
00271       Host_unallocate_memory();
00272       Host_disable_pipe();
00273    }
00274 }

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 285 of file usb_drv.c.

References Host_get_pipe_interrupt, and MAX_EP_NB.

00286 {
00287 U8 interrupt_flags;
00288 U8 i;
00289 
00290    interrupt_flags = Host_get_pipe_interrupt();
00291    for(i=0;i< MAX_EP_NB;i++)
00292    {
00293       if (interrupt_flags & (1<<i))
00294       {
00295          return (i);
00296       }
00297    }
00298    // This return should never occurs ....
00299    return MAX_EP_NB+1;
00300 }


Generated on Fri Jan 26 17:33:05 2007 for Atmel by  doxygen 1.5.1-p1