ATMEL: usb_drv.c Source File

- Main Page
- Modules
- Data Structures
- Files
- Related Pages

# usb_drv.c

Go to the documentation of this file.

    00001 /*This file is prepared for Doxygen automatic documentation generation.*/
    00013
    00014 /* Copyright (c) 2009 Atmel Corporation. All rights reserved.
    00015  *
    00016  * Redistribution and use in source and binary forms, with or without
    00017  * modification, are permitted provided that the following conditions are met:
    00018  *
    00019  * 1. Redistributions of source code must retain the above copyright notice,
    00020  * this list of conditions and the following disclaimer.
    00021  *
    00022  * 2. Redistributions in binary form must reproduce the above copyright notice,
    00023  * this list of conditions and the following disclaimer in the documentation
    00024  * and/or other materials provided with the distribution.
    00025  *
    00026  * 3. The name of Atmel may not be used to endorse or promote products derived
    00027  * from this software without specific prior written permission.
    00028  *
    00029  * 4. This software may only be redistributed and used in connection with an Atmel
    00030  * AVR product.
    00031  *
    00032  * THIS SOFTWARE IS PROVIDED BY ATMEL "AS IS" AND ANY EXPRESS OR IMPLIED
    00033  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
    00034  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT ARE EXPRESSLY AND
    00035  * SPECIFICALLY DISCLAIMED. IN NO EVENT SHALL ATMEL BE LIABLE FOR ANY DIRECT,
    00036  * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
    00037  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
    00038  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
    00039  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
    00040  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
    00041  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
    00042  */
    00043
    00044 //_____ I N C L U D E S ____________________________________________________
    00045
    00046 #include "config.h"
    00047 #include "conf_usb.h"
    00048 #include "usb_drv.h"
    00049
    00050 //_____ M A C R O S ________________________________________________________
    00051
    00052 //_____ D E C L A R A T I O N ______________________________________________
    00053
    00054 #if (USB_DEVICE_FEATURE==DISABLED && USB_HOST_FEATURE==DISABLED)
    00055    #error at least one of  USB_DEVICE_FEATURE or USB_HOST_FEATURE should be unabled
    00056 #endif
    00057
    00058 #if (USB_DEVICE_FEATURE == ENABLED)
    00059
    00070 U8 usb_config_ep(U8 config0, U8 config1)
    00071 {
    00072     Usb_enable_endpoint();
    00073     UECFG0X = config0;
    00074     UECFG1X = (UECFG1X & (1<<ALLOC)) | config1;
    00075     Usb_allocate_memory();
    00076     return (Is_endpoint_configured());
    00077 }
    00078
    00090 U8 usb_select_enpoint_interrupt(void)
    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 }
    00112
    00133 U8 usb_send_packet(U8 ep_num, U8* tbuf, U8 data_length)
    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 }
    00147
    00168 U8 usb_read_packet(U8 ep_num, U8* rbuf, U8  data_length)
    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 }
    00183
    00194 void usb_halt_endpoint (U8 ep_num)
    00195 {
    00196    Usb_select_endpoint(ep_num);
    00197    Usb_enable_stall_handshake();
    00198 }
    00199
    00210 U8 usb_init_device (void)
    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 }
    00237
    00238 #endif
    00239
    00243
    00244 #if (USB_HOST_FEATURE == ENABLED)
    00245
    00255 U8 host_config_pipe(U8 config0, U8 config1)
    00256 {
    00257     Host_enable_pipe();
    00258     UPCFG0X = config0;
    00259     UPCFG1X = config1;
    00260     Host_allocate_memory();
    00261     return (Is_pipe_configured());
    00262 }
    00263
    00271 U8 host_determine_pipe_size(U16 size)
    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 }
    00283
    00291 void host_disable_all_pipe(void)
    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 }
    00302
    00312 U8 usb_get_nb_pipe_interrupt(void)
    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 }
    00328
    00329
    00330 #endif   // USB_HOST_FEATURE == ENABLED
    00331
    00332