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

- File List
- Globals

# usb_drv.c

Go to the documentation of this file.

    00001 /*This file is prepared for Doxygen automatic documentation generation.*/
    00015
    00016 /* Copyright (c) 2009 Atmel Corporation. All rights reserved.
    00017  *
    00018  * Redistribution and use in source and binary forms, with or without
    00019  * modification, are permitted provided that the following conditions are met:
    00020  *
    00021  * 1. Redistributions of source code must retain the above copyright notice,
    00022  * this list of conditions and the following disclaimer.
    00023  *
    00024  * 2. Redistributions in binary form must reproduce the above copyright notice,
    00025  * this list of conditions and the following disclaimer in the documentation
    00026  * and/or other materials provided with the distribution.
    00027  *
    00028  * 3. The name of Atmel may not be used to endorse or promote products derived
    00029  * from this software without specific prior written permission.
    00030  *
    00031  * 4. This software may only be redistributed and used in connection with an Atmel
    00032  * AVR product.
    00033  *
    00034  * THIS SOFTWARE IS PROVIDED BY ATMEL "AS IS" AND ANY EXPRESS OR IMPLIED
    00035  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
    00036  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT ARE EXPRESSLY AND
    00037  * SPECIFICALLY DISCLAIMED. IN NO EVENT SHALL ATMEL BE LIABLE FOR ANY DIRECT,
    00038  * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
    00039  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
    00040  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
    00041  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
    00042  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
    00043  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
    00044  */
    00045
    00046 //_____ I N C L U D E S ____________________________________________________
    00047
    00048 #include "config.h"
    00049 #include "conf_usb.h"
    00050 #include "usb_drv.h"
    00051
    00052 //_____ M A C R O S ________________________________________________________
    00053
    00054 //_____ D E C L A R A T I O N ______________________________________________
    00055
    00056
    00057
    00068 U8 usb_config_ep(U8 config0, U8 config1)
    00069 {
    00070     Usb_enable_endpoint();
    00071     UECFG0X = config0;
    00072     UECFG1X = (UECFG1X & (1<<ALLOC)) | config1;
    00073     Usb_allocate_memory();
    00074     return (Is_endpoint_configured());
    00075 }
    00076
    00088 U8 usb_select_enpoint_interrupt(void)
    00089 {
    00090 U8 interrupt_flags;
    00091 U8 ep_num;
    00092
    00093    ep_num = 0;
    00094    interrupt_flags = Usb_interrupt_flags();
    00095
    00096    while(ep_num < MAX_EP_NB)
    00097    {
    00098       if (interrupt_flags & 1)
    00099       {
    00100          return (ep_num);
    00101       }
    00102       else
    00103       {
    00104          ep_num++;
    00105          interrupt_flags = interrupt_flags >> 1;
    00106       }
    00107    }
    00108    return 0;
    00109 }
    00110
    00131 U8 usb_send_packet(U8 ep_num, U8* tbuf, U8 data_length)
    00132 {
    00133 U8 remaining_length;
    00134
    00135    remaining_length = data_length;
    00136    Usb_select_endpoint(ep_num);
    00137    while(Is_usb_write_enabled() && (0 != remaining_length))
    00138    {
    00139       Usb_write_byte(*tbuf);
    00140       remaining_length--;
    00141       tbuf++;
    00142    }
    00143    return remaining_length;
    00144 }
    00145
    00166 U8 usb_read_packet(U8 ep_num, U8* rbuf, U8  data_length)
    00167 {
    00168 U8 remaining_length;
    00169
    00170    remaining_length = data_length;
    00171    Usb_select_endpoint(ep_num);
    00172
    00173    while(Is_usb_read_enabled() && (0 != remaining_length))
    00174    {
    00175       *rbuf = Usb_read_byte();
    00176       remaining_length--;
    00177       rbuf++;
    00178    }
    00179    return remaining_length;
    00180 }
    00181
    00192 void usb_halt_endpoint (U8 ep_num)
    00193 {
    00194    Usb_select_endpoint(ep_num);
    00195    Usb_enable_stall_handshake();
    00196 }
    00197
    00208 U8 usb_init_device (void)
    00209 {
    00210       Usb_select_endpoint(EP_CONTROL);
    00211       if(!Is_usb_endpoint_enabled())
    00212       {
    00213 #if (USB_LOW_SPEED_DEVICE==DISABLE)
    00214          return usb_configure_endpoint(EP_CONTROL,    \
    00215                                 TYPE_CONTROL,  \
    00216                                 DIRECTION_OUT, \
    00217                                 SIZE_32,       \
    00218                                 ONE_BANK,      \
    00219                                 NYET_DISABLED);
    00220 #else
    00221          return usb_configure_endpoint(EP_CONTROL,    \
    00222                                 TYPE_CONTROL,  \
    00223                                 DIRECTION_OUT, \
    00224                                 SIZE_8,       \
    00225                                 ONE_BANK,      \
    00226                                 NYET_DISABLED);
    00227
    00228 #endif
    00229       }
    00230    return FALSE;
    00231 }
    00232
    00233
    00234