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 "conf_usb.h" 00048 #include "cdc_task.h" 00049 #include "lib_mcu/usb/usb_drv.h" 00050 #include "usb_descriptors.h" 00051 #include "modules/usb/device_chap9/usb_standard_request.h" 00052 #include "usb_specific_request.h" 00053 #include "lib_mcu/uart/uart_lib.h" 00054 #include "uart_usb_lib.h" 00055 #include <stdio.h> 00056 00057 00058 //_____ M A C R O S ________________________________________________________ 00059 00060 //#define CHECK_CDC_MAX_SPEED 00061 00062 00063 //_____ D E F I N I T I O N S ______________________________________________ 00064 00065 00066 00067 //_____ D E C L A R A T I O N S ____________________________________________ 00068 00069 00070 volatile U8 cpt_sof; 00071 extern U8 rx_counter; 00072 extern U8 tx_counter; 00073 extern volatile U8 usb_request_break_generation; 00074 00075 S_line_coding line_coding; 00076 S_line_status line_status; // for detection of serial state input lines 00077 S_serial_state serial_state; // for serial state output lines 00078 00079 volatile U8 rs2usb[10]; 00080 00081 #ifdef CHECK_CDC_MAX_SPEED 00082 Bool b_interrupt_init = FALSE; 00083 #endif 00084 00085 00094 void cdc_task_init(void) 00095 { 00096 uart_init(); 00097 Uart_enable_it_rx(); 00098 Leds_init(); 00099 Joy_init(); 00100 Hwb_button_init(); 00101 Usb_enable_sof_interrupt(); 00102 #ifdef __GNUC__ 00103 fdevopen((int (*)(char, FILE*))(uart_usb_putchar),(int (*)(FILE*))uart_usb_getchar); //for printf redirection 00104 #endif 00105 } 00106 00114 void cdc_task(void) 00115 { 00116 #ifdef CHECK_CDC_MAX_SPEED 00117 if( Is_device_enumerated() && line_status.DTR ) //Enumeration processs OK and COM port openned ? 00118 { 00119 if( !b_interrupt_init ) 00120 { 00121 b_interrupt_init = TRUE; 00122 Usb_select_endpoint(RX_EP); 00123 Usb_enable_receive_out_interrupt(); 00124 } 00125 if (Is_joy_down()) 00126 { 00127 Usb_select_endpoint(TX_EP); 00128 Usb_enable_in_ready_interrupt(); 00129 }else{ 00130 Usb_select_endpoint(TX_EP); 00131 Usb_disable_in_ready_interrupt(); 00132 } 00133 } 00134 #else 00135 if(Is_device_enumerated() && line_status.DTR) //Enumeration processs OK and COM port openned ? 00136 { 00137 if (Uart_tx_ready()) //USART free ? 00138 { 00139 if (uart_usb_test_hit()) // Something received from the USB ? 00140 { 00141 while (rx_counter) 00142 { 00143 uart_putchar(uart_usb_getchar()); // loop back USB to USART 00144 Led3_toggle(); 00145 } 00146 } 00147 } 00148 00149 if ( cpt_sof>=REPEAT_KEY_PRESSED) //Debounce joystick events 00150 { 00151 if (Is_btn_middle()){ 00152 printf ("Select Pressed !\r\n"); 00153 } 00154 if (Is_joy_right()) { 00155 printf ("Right Pressed !\r\n"); 00156 serial_state.bDCD = TRUE; 00157 } 00158 else 00159 serial_state.bDCD = FALSE; 00160 00161 if (Is_joy_left()) { 00162 printf ("Left Pressed !\r\n"); 00163 serial_state.bDSR = TRUE; 00164 } 00165 else 00166 serial_state.bDSR = FALSE; 00167 00168 if (Is_joy_down()) 00169 printf ("Down Pressed !\r\n"); 00170 00171 if (Is_joy_up()) 00172 printf ("Up Pressed !\r\n"); 00173 00174 if(Is_btn_left()) 00175 printf("Hello from AT90USBXXX !\r\n"); 00176 00177 cdc_update_serial_state(); 00178 } 00179 00180 if(usb_request_break_generation==TRUE) 00181 { 00182 usb_request_break_generation=FALSE; 00183 Led2_toggle(); 00184 } 00185 } 00186 #endif 00187 } 00188 00200 void sof_action() 00201 { 00202 cpt_sof++; 00203 } 00204 00205 00211 #ifdef __GNUC__ 00212 ISR(USART1_RX_vect) 00213 #else 00214 #pragma vector = USART1_RX_vect 00215 __interrupt void usart_receive_interrupt() 00216 #endif 00217 { 00218 U8 i=0; 00219 U8 save_ep; 00220 00221 if(Is_device_enumerated()) 00222 { 00223 save_ep=Usb_get_selected_endpoint(); 00224 Usb_select_endpoint(TX_EP); 00225 do 00226 { 00227 if(Uart_rx_ready()) 00228 { 00229 rs2usb[i]=Uart_get_byte(); 00230 i++; 00231 } 00232 }while(Is_usb_write_enabled()==FALSE ); 00233 uart_usb_send_buffer((U8*)&rs2usb,i); 00234 Usb_select_endpoint(save_ep); 00235 } 00236 } 00237 00238 00239 #ifdef CHECK_CDC_MAX_SPEED 00240 #ifdef __GNUC__ 00241 ISR(USB_Endpoint_Pipe_vect) 00242 #else 00243 #pragma vector = USB_Endpoint_Pipe_vect 00244 __interrupt void usb_endpoint_interrupt() 00245 #endif 00246 { 00247 U8 rx_counter, data_rx; 00248 U8 save_ep; 00249 00250 save_ep=Usb_get_selected_endpoint(); 00251 00252 // Read data received on the endpoint OUT 00253 Usb_select_endpoint(RX_EP); 00254 if (Is_usb_receive_out()) 00255 { 00256 for( U8 u8_i=Usb_byte_counter(); u8_i!=0; u8_i-- ) { 00257 data_rx=Usb_read_byte(); 00258 } 00259 Usb_ack_receive_out(); 00260 } 00261 00262 if (Is_joy_down()) 00263 { 00264 // Send data on the endpoint IN 00265 Usb_select_endpoint(TX_EP); 00266 if( Is_usb_write_enabled() ) 00267 { 00268 for( U8 u8_i=0; u8_i<64; u8_i++ ) { 00269 Usb_write_byte(0x30); 00270 } 00271 Usb_ack_in_ready(); 00272 } 00273 } 00274 00275 Usb_select_endpoint(save_ep); 00276 } 00277 #endif