00001 00002 00003 00004 00005 00006 00007 00008 00009 00010 00011 00012 00013 /*_____ I N C L U D E S ____________________________________________________*/ 00014 00015 #include "config.h" 00016 #include "lib_mcu\usb\usb_drv.h" 00017 #include "usb_descriptors.h" 00018 00019 #include "uart_usb_lib.h" 00020 00021 /*_____ M A C R O S ________________________________________________________*/ 00022 00023 /*_____ D E F I N I T I O N ________________________________________________*/ 00024 00025 Uchar tx_counter; 00026 Uchar rx_counter; 00027 00028 /*_____ D E C L A R A T I O N ______________________________________________*/ 00029 00033 void uart_usb_init(void) 00034 { 00035 tx_counter = 0; 00036 rx_counter = 0; 00037 } 00038 00044 bit uart_usb_test_hit(void) 00045 { 00046 if (!rx_counter) 00047 { 00048 Usb_select_endpoint(RX_EP); 00049 if (Is_usb_receive_out()) 00050 { 00051 rx_counter = Usb_byte_counter(); 00052 if (!rx_counter) 00053 { 00054 Usb_ack_receive_out(); 00055 } 00056 } 00057 } 00058 return (rx_counter!=0); 00059 } 00060 00069 char uart_usb_getchar(void) 00070 { 00071 register Uchar data_rx; 00072 00073 Usb_select_endpoint(RX_EP); 00074 if (!rx_counter) while (!uart_usb_test_hit()); 00075 data_rx=Usb_read_byte(); 00076 rx_counter--; 00077 if (!rx_counter) Usb_ack_receive_out(); 00078 return data_rx; 00079 } 00080 00081 00088 bit uart_usb_tx_ready(void) 00089 { 00090 if (!Is_usb_write_enabled()) 00091 { 00092 return FALSE; 00093 } 00094 return TRUE; 00095 } 00096 00106 int uart_usb_putchar(int data_to_send) 00107 { 00108 Usb_select_endpoint(TX_EP); 00109 while(!uart_usb_tx_ready()); // Wait Endpoint ready 00110 Usb_write_byte(data_to_send); 00111 tx_counter++; 00112 if(!Is_usb_write_enabled()) //If Endpoint full -> flush 00113 { 00114 uart_usb_flush(); 00115 } 00116 return data_to_send; 00117 } 00118 00119 00124 void uart_usb_flush (void) 00125 { 00126 Usb_select_endpoint(TX_EP); 00127 Usb_send_in(); 00128 tx_counter = 0; 00129 00130 } 00131 00132