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 00045 //_____ I N C L U D E S ___________________________________________________ 00046 00047 #include "config.h" 00048 #include "conf_usb.h" 00049 #include "hid_task.h" 00050 #include "lib_mcu/usb/usb_drv.h" 00051 #include "usb_descriptors.h" 00052 #include "modules/usb/device_chap9/usb_standard_request.h" 00053 #include "usb_specific_request.h" 00054 #include "lib_mcu/util/start_boot.h" 00055 #include "lib_mcu/adc/adc_drv.h" 00056 00057 00058 //_____ M A C R O S ________________________________________________________ 00059 00060 00061 //_____ D E F I N I T I O N S ______________________________________________ 00062 00063 00064 //_____ D E C L A R A T I O N S ____________________________________________ 00065 00066 extern volatile U16 g_u16_cpt2_sof; 00067 extern U8 jump_bootloader; 00068 #if( HID_GENERIC_DEMO_FULL == ENABLE ) 00069 U8 g_temperature =0; 00070 U8 g_potentiometer =0; 00071 #else 00072 U8 g_last_joy =0; 00073 #endif 00074 00075 void hid_report_out (void); 00076 void hid_report_in (void); 00077 00080 void hid_task_init(void) 00081 { 00082 #if( HID_GENERIC_DEMO_FULL == ENABLE ) 00083 init_adc(); 00084 #endif 00085 Leds_init(); 00086 Joy_init(); 00087 } 00088 00092 void hid_task(void) 00093 { 00094 if(!Is_device_enumerated()) // Check USB HID is enumerated 00095 return; 00096 00097 hid_report_out(); 00098 hid_report_in(); 00099 } 00100 00101 00104 void hid_report_out(void) 00105 { 00106 Usb_select_endpoint(EP_HID_OUT); 00107 if(Is_usb_receive_out()) 00108 { 00109 #if( HID_GENERIC_DEMO_FULL == ENABLE ) 00110 //* Read report from HID Generic Demo Full 00111 U8 report_byte_4; 00112 U8 report_byte_5; 00113 Leds_set_val( Usb_read_byte()); // RepportOUT[0] is LEDS value 00114 GPIOR1 = Usb_read_byte(); // not used 00115 GPIOR1 = Usb_read_byte(); // not used 00116 report_byte_4 = Usb_read_byte(); 00117 report_byte_5 = Usb_read_byte(); 00118 // Check if we received DFU mode command from host 00119 if( (report_byte_4==0x55) && (report_byte_5==0xAA) ) 00120 jump_bootloader = TRUE; 00121 #else 00122 //* Read report from HID Generic Demo 00123 U8 led_state; 00124 U8 led_number; 00125 led_state = Usb_read_byte()&0x0F; // RepportOUT[0] is LEDS value 00126 led_number = Usb_read_byte()&0x0F; 00127 switch (led_number) 00128 { 00129 case 1: 00130 if(led_state) 00131 { Led0_on(); } 00132 else {Led0_off();} 00133 break; 00134 case 2: 00135 if(led_state) 00136 { Led1_on(); } 00137 else {Led1_off();} 00138 break; 00139 case 3: 00140 if(led_state) 00141 { Led2_on(); } 00142 else {Led2_off();} 00143 break; 00144 case 4: 00145 if(led_state) 00146 { Led3_on(); } 00147 else {Led3_off();} 00148 break; 00149 } 00150 #endif 00151 Usb_ack_receive_out(); 00152 } 00153 00154 //** Check if we received DFU mode command from host 00155 if(jump_bootloader) 00156 { 00157 U32 volatile tempo; 00158 Leds_off(); 00159 Usb_detach(); // Detach actual generic HID application 00160 for(tempo=0;tempo<70000;tempo++); // Wait some time before 00161 start_boot(); // Jumping to booltoader 00162 } 00163 } 00164 00165 00168 void hid_report_in(void) 00169 { 00170 U8 joy =0; 00171 00172 Usb_select_endpoint(EP_HID_IN); 00173 if(!Is_usb_write_enabled()) 00174 return; // Not ready to send report 00175 00176 #if( HID_GENERIC_DEMO_FULL == ENABLE ) 00177 #if (TARGET_BOARD!=USBKEY) 00178 if (g_u16_cpt2_sof%100==0) 00179 { 00180 g_potentiometer=Get_adc_pot_val()>>2; 00181 } 00182 #endif 00183 if (g_u16_cpt2_sof>=600) 00184 { 00185 g_temperature=(S8)Read_temperature(); 00186 g_temperature=(S8)Read_temperature(); 00187 g_u16_cpt2_sof=0; 00188 } 00189 // Build the Joytick report 00190 if(Is_joy_up()) // Check for UP event 00191 { 00192 joy+=0x01; 00193 } 00194 if(Is_joy_down()) // Check for DOWN event 00195 { 00196 joy+=0x02; 00197 } 00198 if(Is_joy_right()) // Check for RIGHT event 00199 { 00200 joy+=0x04; 00201 } 00202 if(Is_joy_left()) // Check for LEFT event 00203 { 00204 joy+=0x08; 00205 } 00206 #else 00207 // Build the Joytick report 00208 if(Is_joy_up()|| Is_joy_down() || Is_joy_right() || Is_joy_left() ) 00209 { 00210 joy=0x01; 00211 } 00212 if(joy==g_last_joy) 00213 return; // Same report then no send report 00214 g_last_joy=joy; 00215 #endif 00216 00217 // Send report 00218 #if( HID_GENERIC_DEMO_FULL == ENABLE ) 00219 Usb_write_byte(Leds_get_val()); // Leds 00220 Usb_write_byte(joy); // Joystick 00221 Usb_write_byte(g_potentiometer); // Potentiometer 00222 Usb_write_byte(g_temperature); // temperature 00223 #else 00224 Usb_write_byte(g_last_joy); // Joystick 00225 Usb_write_byte(GPIOR1); // Dummy (not used) 00226 Usb_write_byte(GPIOR1); // Dummy (not used) 00227 Usb_write_byte(GPIOR1); // Dummy (not used) 00228 #endif 00229 Usb_write_byte(GPIOR1); // Dummy (not used) 00230 Usb_write_byte(GPIOR1); // Dummy (not used) 00231 Usb_write_byte(GPIOR1); // Dummy (not used) 00232 Usb_write_byte(GPIOR1); // Dummy (not used) 00233 Usb_ack_in_ready(); // Send data over the USB 00234 } 00235
1.5.3