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 "lib_mcu/util/start_boot.h" 00056 #include <stdio.h> 00057 00058 00059 //_____ M A C R O S ________________________________________________________ 00060 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 S_line_coding line_coding; 00075 S_line_status line_status; // for detection of serial state input lines 00076 S_serial_state serial_state; // for serial state output lines 00077 00078 volatile U8 rs2usb[10]; 00079 00080 00089 void cdc_task_init(void) 00090 { 00091 uart_init(); 00092 Uart_enable_it_rx(); 00093 Leds_init(); 00094 Joy_init(); 00095 Hwb_button_init(); 00096 Usb_enable_sof_interrupt(); 00097 #ifdef __GNUC__ 00098 fdevopen((int (*)(char, FILE*))(uart_usb_putchar),(int (*)(FILE*))uart_usb_getchar); //for printf redirection 00099 #endif 00100 } 00101 00102 00103 00111 void cdc_task(void) 00112 { 00113 00114 if(Is_device_enumerated() && line_status.DTR) //Enumeration processs OK and COM port openned ? 00115 { 00116 if (Uart_tx_ready()) //USART free ? 00117 { 00118 if (uart_usb_test_hit()) // Something received from the USB ? 00119 { 00120 while (rx_counter) 00121 { 00122 uart_putchar(uart_usb_getchar()); // loop back USB to USART 00123 Led3_toggle(); 00124 } 00125 } 00126 } 00127 00128 if ( cpt_sof>=REPEAT_KEY_PRESSED) //Debounce joystick events 00129 { 00130 if (Is_btn_middle()){ 00131 printf ("Select Pressed !\r\n"); 00132 } 00133 if (Is_joy_right()) { 00134 printf ("Right Pressed !\r\n"); 00135 serial_state.bDCD = TRUE; 00136 } 00137 else 00138 serial_state.bDCD = FALSE; 00139 00140 if (Is_joy_left()) { 00141 printf ("Left Pressed !\r\n"); 00142 serial_state.bDSR = TRUE; 00143 } 00144 else 00145 serial_state.bDSR = FALSE; 00146 00147 if (Is_joy_down()) 00148 printf ("Down Pressed !\r\n"); 00149 00150 if (Is_joy_up()) 00151 printf ("Up Pressed !\r\n"); 00152 00153 if(Is_btn_left()) 00154 printf("Hello from AT90USBXXX !\r\n"); 00155 00156 cdc_update_serial_state(); 00157 } 00158 00159 if(usb_request_break_generation==TRUE) 00160 { 00161 usb_request_break_generation=FALSE; 00162 Led2_toggle(); 00163 } 00164 } 00165 } 00166 00178 void sof_action() 00179 { 00180 cpt_sof++; 00181 } 00182 00183 00189 #ifdef __GNUC__ 00190 ISR(USART1_RX_vect) 00191 #else 00192 #pragma vector = USART1_RX_vect 00193 __interrupt void usart_receive_interrupt() 00194 #endif 00195 { 00196 U8 i=0; 00197 U8 save_ep; 00198 00199 if(Is_device_enumerated()) 00200 { 00201 save_ep=Usb_get_selected_endpoint(); 00202 Usb_select_endpoint(TX_EP); 00203 do 00204 { 00205 if(Uart_rx_ready()) 00206 { 00207 rs2usb[i]=Uart_get_byte(); 00208 i++; 00209 } 00210 }while(Is_usb_write_enabled()==FALSE ); 00211 uart_usb_send_buffer((U8*)&rs2usb,i); 00212 Usb_select_endpoint(save_ep); 00213 } 00214 }