uart_usb_lib.c

Go to the documentation of this file.
00001 /*This file has been prepared for Doxygen automatic documentation generation.*/
00013 
00014 /* Copyright (c) 2007, 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  * THIS SOFTWARE IS PROVIDED BY ATMEL ``AS IS'' AND ANY EXPRESS OR IMPLIED
00030  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
00031  * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE EXPRESSLY AND
00032  * SPECIFICALLY DISCLAIMED. IN NO EVENT SHALL ATMEL BE LIABLE FOR ANY DIRECT,
00033  * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
00034  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
00035  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
00036  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
00037  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
00038  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
00039  */
00040 
00041 /*_____ I N C L U D E S ____________________________________________________*/
00042 
00043 #include "config.h"
00044 #include "lib_mcu/usb/usb_drv.h"
00045 #include "usb_descriptors.h"
00046 
00047 #include "uart_usb_lib.h"
00048 
00049 /*_____ M A C R O S ________________________________________________________*/
00050 
00051 /*_____ D E F I N I T I O N ________________________________________________*/
00052 
00053 
00054 Uchar rx_counter;
00055 
00056 /*_____ D E C L A R A T I O N ______________________________________________*/
00057 
00061 void uart_usb_init(void)
00062 {
00063   rx_counter = 0;
00064 }
00065 
00071 bit uart_usb_test_hit(void)
00072 {
00073   if(!Is_device_enumerated())
00074      return FALSE;
00075 
00076   if (!rx_counter)
00077   {
00078     Usb_select_endpoint(RX_EP);
00079     if (Is_usb_receive_out())
00080     {
00081       rx_counter = Usb_byte_counter();
00082       if (!rx_counter)
00083       {
00084         Usb_ack_receive_out();
00085       }
00086     }
00087   }
00088   return (rx_counter!=0);
00089 }
00090 
00099 char uart_usb_getchar(void)
00100 {
00101   register Uchar data_rx;
00102 
00103   Usb_select_endpoint(RX_EP);
00104   if (!rx_counter) while (!uart_usb_test_hit());
00105   data_rx=Usb_read_byte();
00106   rx_counter--;
00107   if (!rx_counter) Usb_ack_receive_out();
00108   return data_rx;
00109 }
00110 
00111 
00118 bit uart_usb_tx_ready(void)
00119 {
00120   if(!Is_device_enumerated())
00121      return FALSE;
00122 
00123   if (!Is_usb_write_enabled())
00124   {
00125     return FALSE;
00126   }
00127   return TRUE;
00128 }
00129 
00139 int uart_usb_putchar(int data_to_send)
00140 {
00141    uart_usb_send_buffer((U8*)&data_to_send, 1);
00142    return data_to_send;
00143 }
00144 
00145 
00146 
00154 void uart_usb_send_buffer(U8 *buffer, U8 nb_data)
00155 {
00156    U8 zlp;
00157 
00158   if(!Is_device_enumerated())
00159      return;
00160    
00161    // Compute if zlp required
00162    if(nb_data%TX_EP_SIZE) 
00163    { zlp=FALSE;} 
00164    else { zlp=TRUE; }
00165    
00166    Usb_select_endpoint(TX_EP);
00167    while (nb_data)
00168    {
00169       while(Is_usb_write_enabled()==FALSE); // Wait Endpoint ready
00170       while(Is_usb_write_enabled() && nb_data)
00171       {
00172          Usb_write_byte(*buffer);
00173          buffer++;
00174          nb_data--;
00175       }
00176       Usb_ack_in_ready();
00177    }
00178    if(zlp)
00179    {
00180       while(Is_usb_write_enabled()==FALSE); // Wait Endpoint ready
00181       Usb_ack_in_ready();
00182    }
00183 }

Generated on Fri Oct 31 15:55:11 2008 for ATMEL by  doxygen 1.5.3