Xmega Application Note


uart_usb_lib.c

Go to the documentation of this file.
00001 /* This file has been prepared for Doxygen automatic documentation generation.*/
00047 #include "config.h"
00048 #include "usb_drv.h"
00049 #include "usb_descriptors.h"
00050 #include "uart_usb_lib.h"
00051 
00052 /* Local counters for buffer. */
00053 uint8_t tx_counter;
00054 uint8_t rx_counter;
00055 
00058 void uart_usb_init(void)
00059 {
00060         tx_counter = 0;
00061         rx_counter = 0;
00062 }
00063 
00068 uint8_t uart_usb_test_hit(void)
00069 {
00070         if (!rx_counter)
00071         {
00072                 Usb_select_endpoint(RX_EP);
00073                 if (Is_usb_receive_out())
00074                 {
00075                         
00076 #ifdef __ICCAVR__
00077 #pragma diag_suppress=Pa082 /* Suppress warning for undefined order of volatile access. */
00078 #endif
00079                         rx_counter = Usb_byte_counter();
00080 #ifdef __ICCAVR__
00081 #pragma diag_default=Pa082 /* Back to default.*/
00082 #endif
00083 
00084                         if (!rx_counter)
00085                         {
00086                                 Usb_ack_receive_out();
00087                         }
00088                 }
00089         }
00090         return (rx_counter!=0);
00091 }
00092 
00093 
00101 uint8_t uart_usb_getchar(void)
00102 {
00103         register uint8_t data_rx;
00104         
00105         Usb_select_endpoint(RX_EP);
00106         if (!rx_counter){
00107                 while (!uart_usb_test_hit());
00108         }
00109         
00110         data_rx = Usb_read_byte();
00111         rx_counter--;
00112         
00113         if (!rx_counter){
00114                 Usb_ack_receive_out();
00115         }
00116         
00117         return data_rx;
00118 }
00119 
00125 uint8_t uart_usb_tx_ready(void)
00126 {
00127         if (!Is_usb_write_enabled())
00128         {
00129                 return false;
00130         }
00131         return true;
00132 }
00133 
00142 int uart_usb_putchar(int data_to_send)
00143 {
00144         Usb_select_endpoint(TX_EP);
00145         while(!uart_usb_tx_ready()); /* Wait Endpoint ready */
00146         Usb_write_byte(data_to_send);
00147         tx_counter++;
00148         if(!Is_usb_write_enabled()) /* If Endpoint full -> flush */
00149         {
00150                 uart_usb_flush();
00151         }
00152         return data_to_send;
00153 }
00154 
00155 
00160 void uart_usb_flush (void)
00161 {
00162         Usb_select_endpoint(TX_EP);
00163         Usb_send_in();
00164         tx_counter = 0;
00165         
00166 }
@DOC_TITLE@
Generated on Mon Jan 18 09:26:08 2010 for AVR1907 Xplain USB Gateway by doxygen 1.5.5