uart_usb_lib.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 "lib_mcu/usb/usb_drv.h"
00048 #include "usb_descriptors.h"
00049 
00050 #include "uart_usb_lib.h"
00051 
00052 /*_____ M A C R O S ________________________________________________________*/
00053 
00054 /*_____ D E F I N I T I O N ________________________________________________*/
00055 
00056 
00057 Uchar rx_counter;
00058 
00059 /*_____ D E C L A R A T I O N ______________________________________________*/
00060 
00064 void uart_usb_init(void)
00065 {
00066   rx_counter = 0;
00067 }
00068 
00074 bit uart_usb_test_hit(void)
00075 {
00076   if(!Is_device_enumerated())
00077      return FALSE;
00078 
00079   if (!rx_counter)
00080   {
00081     Usb_select_endpoint(RX_EP);
00082     if (Is_usb_receive_out())
00083     {
00084       rx_counter = Usb_byte_counter();
00085       if (!rx_counter)
00086       {
00087         Usb_ack_receive_out();
00088       }
00089     }
00090   }
00091   return (rx_counter!=0);
00092 }
00093 
00102 char uart_usb_getchar(void)
00103 {
00104   register Uchar data_rx;
00105 
00106   Usb_select_endpoint(RX_EP);
00107   if (!rx_counter) while (!uart_usb_test_hit());
00108   data_rx=Usb_read_byte();
00109   rx_counter--;
00110   if (!rx_counter) Usb_ack_receive_out();
00111   return data_rx;
00112 }
00113 
00114 
00121 bit uart_usb_tx_ready(void)
00122 {
00123   if(!Is_device_enumerated())
00124      return FALSE;
00125 
00126   if (!Is_usb_write_enabled())
00127   {
00128     return FALSE;
00129   }
00130   return TRUE;
00131 }
00132 
00142 int uart_usb_putchar(int data_to_send)
00143 {
00144    uart_usb_send_buffer((U8*)&data_to_send, 1);
00145    return data_to_send;
00146 }
00147 
00148 
00149 
00157 void uart_usb_send_buffer(U8 *buffer, U8 nb_data)
00158 {
00159    U8 zlp;
00160 
00161   if(!Is_device_enumerated())
00162      return;
00163    
00164    // Compute if zlp required
00165    if(nb_data%TX_EP_SIZE) 
00166    { zlp=FALSE;} 
00167    else { zlp=TRUE; }
00168    
00169    Usb_select_endpoint(TX_EP);
00170    while (nb_data)
00171    {
00172       while(Is_usb_write_enabled()==FALSE); // Wait Endpoint ready
00173       while(Is_usb_write_enabled() && nb_data)
00174       {
00175          Usb_write_byte(*buffer);
00176          buffer++;
00177          nb_data--;
00178       }
00179       Usb_ack_in_ready();
00180    }
00181    if(zlp)
00182    {
00183       while(Is_usb_write_enabled()==FALSE); // Wait Endpoint ready
00184       Usb_ack_in_ready();
00185    }
00186 }

Generated on Mon Sep 14 13:51:27 2009 for ATMEL by  doxygen 1.5.3