00001 /*This file is prepared for Doxygen automatic documentation generation.*/ 00018 /* Copyright (c) 2009 Atmel Corporation. All rights reserved. 00019 * 00020 * Redistribution and use in source and binary forms, with or without 00021 * modification, are permitted provided that the following conditions are met: 00022 * 00023 * 1. Redistributions of source code must retain the above copyright notice, this 00024 * list of conditions and the following disclaimer. 00025 * 00026 * 2. Redistributions in binary form must reproduce the above copyright notice, 00027 * this list of conditions and the following disclaimer in the documentation 00028 * and/or other materials provided with the distribution. 00029 * 00030 * 3. The name of Atmel may not be used to endorse or promote products derived 00031 * from this software without specific prior written permission. 00032 * 00033 * 4. This software may only be redistributed and used in connection with an Atmel 00034 * AVR product. 00035 * 00036 * THIS SOFTWARE IS PROVIDED BY ATMEL "AS IS" AND ANY EXPRESS OR IMPLIED 00037 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF 00038 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT ARE 00039 * EXPRESSLY AND SPECIFICALLY DISCLAIMED. IN NO EVENT SHALL ATMEL BE LIABLE FOR 00040 * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 00041 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 00042 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND 00043 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 00044 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 00045 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE 00046 * 00047 */ 00048 00049 //_____ I N C L U D E S ___________________________________________________ 00050 #include "compiler.h" 00051 #include "conf_usb.h" 00052 #include "usb_drv.h" 00053 #include "uart_usb_lib.h" 00054 00055 //_____ M A C R O S ________________________________________________________ 00056 00057 00058 //_____ D E F I N I T I O N S ______________________________________________ 00059 00060 //_____ D E C L A R A T I O N S ____________________________________________ 00061 Bool b_tx_new; 00062 Bool b_rx_new; 00063 00064 void uart_usb_init(void) 00065 { 00066 b_tx_new = TRUE; 00067 b_rx_new = TRUE; 00068 } 00069 00070 // Functions that manage characters input through USB 00071 // 00072 00073 Bool uart_usb_test_hit(void) 00074 { 00075 if( Is_usb_out_received(RX_EP) ) 00076 { 00077 if( 0!=Usb_byte_count(RX_EP) ) 00078 { 00079 if( b_rx_new ) 00080 { 00081 Usb_reset_endpoint_fifo_access(RX_EP); 00082 b_rx_new = FALSE; 00083 } 00084 return TRUE; 00085 } 00086 Usb_ack_out_received_free(RX_EP); 00087 b_rx_new = TRUE; 00088 } 00089 return FALSE; 00090 } 00091 00092 00093 char uart_usb_getchar(void) 00094 { 00095 register char data_rx; 00096 00097 while( !uart_usb_test_hit() ); 00098 data_rx=Usb_read_endpoint_data(RX_EP, 8); 00099 if( 0==Usb_byte_count(RX_EP) ) { 00100 Usb_ack_out_received_free(RX_EP); 00101 b_rx_new = TRUE; 00102 } 00103 return data_rx; 00104 } 00105 00106 00107 // Functions that manage characters output through USB 00108 // 00109 Bool uart_usb_tx_ready(void) 00110 { 00111 if( Is_usb_write_enabled(TX_EP) ) 00112 { 00113 if( b_tx_new ) 00114 { 00115 Usb_reset_endpoint_fifo_access(TX_EP); 00116 b_tx_new = FALSE; 00117 } 00118 return TRUE; 00119 } 00120 return FALSE; 00121 } 00122 00123 00124 int uart_usb_putchar(int data_to_send) 00125 { 00126 while( !uart_usb_tx_ready() ); // Wait Endpoint ready 00127 Usb_write_endpoint_data(TX_EP, 8, data_to_send); 00128 if( !Is_usb_write_enabled(TX_EP) ) // If Endpoint full -> flush 00129 { 00130 Usb_ack_in_ready_send(TX_EP); 00131 b_tx_new = TRUE; 00132 } 00133 return data_to_send; 00134 } 00135 00136 00137 void uart_usb_flush (void) 00138 { 00139 if( 0 != Usb_nb_busy_bank(TX_EP) ) 00140 return; // Don't flush because a previous flush is on-going and all bank don't be busy 00141 Usb_ack_in_ready_send(TX_EP); 00142 b_tx_new = TRUE; 00143 }