Main Page | Alphabetical List | Data Structures | File List | Data Fields | Globals | Related Pages

usb_task.c

Go to the documentation of this file.
00001 00002 00003 00004 00005 00006 00007 00008 00009 00010 00011 00012 00013 00014 00015 00016 00017 00018 00019 00020 00021 00022 00023 //_____ I N C L U D E S ___________________________________________________ 00024 00025 #include "config.h" 00026 #include "conf\conf_usb.h" 00027 #include "usb_task.h" 00028 #include "lib_mcu\usb\usb_drv.h" 00029 #include "modules\usb\usb_enum.h" 00030 #include "intrins.h" 00031 00032 00033 //_____ M A C R O S ________________________________________________________ 00034 00035 //_____ D E F I N I T I O N S ______________________________________________ 00036 00042 volatile bit usb_connected; 00043 U8 xdata sof_counter; 00044 volatile U8 data toggle_next_time; 00045 00046 extern _MEM_TYPE_SLOW_ U8 usb_configuration_nb; 00047 00048 volatile bit usb_continuous_mode; 00049 U8 send_free = 0; 00050 00051 S_hid_set_control SetControlBytes; 00052 00053 //_____ D E C L A R A T I O N S ____________________________________________ 00068 void usb_task_init(void) 00069 { 00070 Set_page_usb(); 00071 Usb_enable(); // enable the USB controller 00072 usb_start_device(); 00073 sof_counter = 0; 00074 usb_connected = TRUE; 00075 toggle_next_time = 0; 00076 usb_continuous_mode = FALSE; 00077 } 00078 00079 00080 00095 void usb_start_device (void) 00096 { 00097 U16 c; 00098 usb_generate_clock(); 00099 Set_page_usb(); 00100 Usb_unfreeze_clock(); 00101 Usb_set_speed(); // for speed analysis 00102 Usb_ack_suspend(); 00103 Usb_detach(); 00104 for (c=0; c<1000; c++); 00105 Usb_attach(); 00106 Usb_enable_suspend_interrupt(); 00107 Usb_enable_resume_interrupt(); 00108 Usb_enable_reset_interrupt(); 00109 Usb_enable_wake_up_interrupt(); 00110 Usb_enable_sof_interrupt(); 00111 Enable_usb_interrupt(); 00112 Enable_interrupt(); 00113 usb_init_device(); // configure the USB controller EP0 00114 00115 Usb_attach(); 00116 } 00117 00133 void usb_task(void) 00134 { 00135 if (!usb_connected) /* power down mode when USB not connected */ 00136 { 00137 Pll_stop(); /* disable PLL before going in power down mode */ 00138 Enable_interrupt(); 00139 00140 // Shutdown board ressources 00141 Led_all_off(); 00142 Disable_ale(); 00143 if (!usb_connected) 00144 { 00145 Usb_suspend_action(); // see in conf_usb.h 00146 _nop_(); 00147 } 00148 } 00149 else //USB Connected 00150 { 00151 // USB MANAGEMENT 00152 00153 Set_page_usb (); 00154 00155 Usb_select_endpoint(EP_CONTROL); 00156 if (Is_usb_receive_setup()) 00157 { 00158 usb_process_request(); 00159 if(SetControlBytes.type==0) { usb_continuous_mode = FALSE; } 00160 if(SetControlBytes.type==1) { usb_continuous_mode = TRUE ; } 00161 00162 } 00166 00167 if(usb_continuous_mode == TRUE) 00168 { 00169 Usb_select_endpoint(EP_HID_IN); 00170 if(Is_usb_in_ready()) 00171 { 00172 send_free = 1; 00173 Usb_ack_in_ready(); 00174 } 00175 } 00176 00180 } 00181 } 00182 00183 00199 void usb_interrupt(void) interrupt IRQ_USB 00200 { 00201 if (Is_usb_sof()) 00202 { 00203 Usb_ack_sof(); 00204 sof_counter++; 00205 if (usb_continuous_mode == FALSE) 00206 { 00207 if (sof_counter == 0x40) 00208 { 00209 Switch_ale(); 00210 sof_counter = 0; 00211 } 00212 } 00213 else 00214 { 00215 if (sof_counter == 0xFF) 00216 { 00217 Switch_ale(); 00218 } 00219 } 00220 return; 00221 } 00222 00223 if (Is_usb_suspend()) 00224 { 00225 Usb_ack_suspend(); 00226 Usb_freeze_clock(); 00227 usb_connected = FALSE; 00228 } 00229 00230 if (Is_usb_wake_up()) 00231 { 00232 if (!usb_connected) /* reconfigure PLL when exiting power down mode */ 00233 { 00234 Pll_enable(); 00235 } 00236 Usb_unfreeze_clock(); 00237 Enable_ale(); 00238 Usb_ack_suspend(); 00239 Usb_ack_wake_up(); 00240 usb_connected = TRUE; 00241 } 00242 00243 if (Is_usb_reset()) 00244 { 00245 Usb_ack_reset(); 00246 usb_enum_var_init(); 00247 Usb_enable_suspend_interrupt(); 00248 Usb_enable_wake_up_interrupt(); 00249 Usb_enable_sof_interrupt(); 00250 usb_connected = TRUE; 00251 } 00252 } 00253 00254 00255

Generated on Mon Apr 10 17:23:30 2006 for Atmel by doxygen 1.3.7