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 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 00090 void cdc_task_init(void) 00091 { 00092 uart_init(); 00093 Uart_enable_it_rx(); 00094 Leds_init(); 00095 Joy_init(); 00096 Hwb_button_init(); 00097 Usb_enable_sof_interrupt(); 00098 #ifdef __GNUC__ 00099 fdevopen((int (*)(char, FILE*))(uart_usb_putchar),(int (*)(FILE*))uart_usb_getchar); //for printf redirection 00100 #endif 00101 } 00102 00103 00104 00112 void cdc_task(void) 00113 { 00114 00115 if(Is_device_enumerated() && line_status.DTR) //Enumeration processs OK and COM port openned ? 00116 { 00117 if (Uart_tx_ready()) //USART free ? 00118 { 00119 if (uart_usb_test_hit()) // Something received from the USB ? 00120 { 00121 while (rx_counter) 00122 { 00123 uart_putchar(uart_usb_getchar()); // loop back USB to USART 00124 Led1_toggle(); 00125 } 00126 } 00127 } 00128 00129 if ( cpt_sof>=REPEAT_KEY_PRESSED) //Debounce joystick events 00130 { 00131 if (Is_joy_select()) { 00132 printf ("Select Pressed !\r\n"); 00133 } 00134 if (Is_joy_right()) { 00135 printf ("Right Pressed !\r\n"); 00136 serial_state.bDCD = TRUE; 00137 } 00138 else 00139 serial_state.bDCD = FALSE; 00140 00141 if (Is_joy_left()) { 00142 printf ("Left Pressed !\r\n"); 00143 serial_state.bDSR = TRUE; 00144 } 00145 else 00146 serial_state.bDSR = FALSE; 00147 00148 if (Is_joy_down()) 00149 printf ("Down Pressed !\r\n"); 00150 00151 if (Is_joy_up()) 00152 printf ("Up Pressed !\r\n"); 00153 00154 if(Is_hwb()) 00155 printf("Hello from ATmega32U4 !\r\n"); 00156 00157 cdc_update_serial_state(); 00158 } 00159 00160 if(usb_request_break_generation==TRUE) 00161 { 00162 U32 volatile tempo; 00163 usb_request_break_generation=FALSE; 00164 Led2_toggle(); 00165 Usb_detach(); // Detach actual USB Device 00166 for(tempo=0;tempo<70000;tempo++); // Wait some time before 00167 start_boot(); // Jumping to booltoader 00168 } 00169 } 00170 } 00171 00183 void sof_action() 00184 { 00185 cpt_sof++; 00186 } 00187 00188 00194 #ifdef __GNUC__ 00195 ISR(USART1_RX_vect) 00196 #else 00197 #pragma vector = USART1_RX_vect 00198 __interrupt void usart_receive_interrupt() 00199 #endif 00200 { 00201 U8 i=0; 00202 U8 save_ep; 00203 00204 if(Is_device_enumerated()) 00205 { 00206 save_ep=Usb_get_selected_endpoint(); 00207 Usb_select_endpoint(TX_EP); 00208 do 00209 { 00210 if(Uart_rx_ready()) 00211 { 00212 rs2usb[i]=Uart_get_byte(); 00213 i++; 00214 } 00215 }while(Is_usb_write_enabled()==FALSE ); 00216 uart_usb_send_buffer((U8*)&rs2usb,i); 00217 Usb_select_endpoint(save_ep); 00218 } 00219 }