00001 00002 00003 00004 00005 00006 00007 00008 00009 00010 00011 00012 00013 00014 00015 00016 00017 00018 00019 00020 00021 00022 00023 00024 00025 00026 00027 00028 00029 00030 00031 00032 //_____ I N C L U D E S ___________________________________________________ 00033 00034 #include "config.h" 00035 #include "conf_usb.h" 00036 #include "usb_task.h" 00037 #include "lib_mcu\usb\usb_drv.h" 00038 #include "usb_descriptors.h" 00039 #include "lib_mcu\power\power_drv.h" 00040 #include "lib_mcu\wdt\wdt_drv.h" 00041 #include "lib_mcu\pll\pll_drv.h" 00042 00043 #if ((USB_HOST_FEATURE == ENABLED)) 00044 #include "modules\usb\host_chap9\usb_host_task.h" 00045 #endif 00046 00047 #if ((USB_DEVICE_FEATURE == ENABLED)) 00048 #include "modules\usb\device_chap9\usb_device_task.h" 00049 #endif 00050 00051 #ifndef USE_USB_PADS_REGULATOR 00052 #error "USE_USB_PADS_REGULATOR" should be defined as ENABLE or DISABLE in conf_usb.h file 00053 #endif 00054 00055 00056 //_____ M A C R O S ________________________________________________________ 00057 00058 //_____ D E F I N I T I O N S ______________________________________________ 00059 00070 volatile U16 g_usb_event=0; 00071 00072 00073 #if (USB_DEVICE_FEATURE == ENABLED) 00080 extern bit usb_connected; 00081 00088 extern U8 usb_configuration_nb; 00089 #endif 00090 00091 00092 #if (USB_HOST_FEATURE == ENABLED) 00099 volatile U8 private_sof_counter=0; 00100 #endif 00101 00102 #if ((USB_DEVICE_FEATURE == ENABLED)&& (USB_HOST_FEATURE == ENABLED)) 00108 U8 g_usb_mode=USB_MODE_UNDEFINED; 00109 U8 g_old_usb_mode; 00110 #endif 00111 00112 00113 00114 00115 //_____ D E C L A R A T I O N S ____________________________________________ 00116 00117 00128 void usb_task_init(void) 00129 { 00130 #if (USB_HOST_FEATURE == ENABLED) 00131 U8 delay; 00132 #endif 00133 00134 #if (USE_USB_PADS_REGULATOR==ENABLE) // Otherwise assume USB PADs regulator is not used 00135 Usb_enable_regulator(); 00136 #endif 00137 00138 // ---- DUAL ROLE DEVICE USB MODE --------------------------------------------- 00139 #if ((USB_DEVICE_FEATURE == ENABLED)&& (USB_HOST_FEATURE == ENABLED)) 00140 Usb_enable_uid_pin(); 00141 delay=PORTA; 00142 g_usb_mode=USB_MODE_UNDEFINED; 00143 if(Is_usb_id_device()) 00144 { 00145 g_usb_mode=USB_MODE_DEVICE; 00146 usb_device_task_init(); 00147 } 00148 else 00149 { 00150 g_usb_mode=USB_MODE_HOST; 00151 Usb_ack_id_transition(); // REQUIRED !!! Startup with ID=0, Ack ID pin transistion (default hwd start up is device mode) 00152 Usb_enable_id_interrupt(); 00153 Enable_interrupt(); 00154 usb_host_task_init(); 00155 } 00156 g_old_usb_mode=g_usb_mode; // Store current usb mode, for mode change detection 00157 // ----------------------------------------------------------------------------- 00158 00159 // ---- DEVICE ONLY USB MODE --------------------------------------------------- 00160 #elif ((USB_DEVICE_FEATURE == ENABLED)&& (USB_HOST_FEATURE == DISABLE)) 00161 Usb_force_device_mode(); 00162 usb_device_task_init(); 00163 // ----------------------------------------------------------------------------- 00164 00165 // ---- REDUCED HOST ONLY USB MODE --------------------------------------------- 00166 #elif ((USB_DEVICE_FEATURE == DISABLE)&& (USB_HOST_FEATURE == ENABLED)) 00167 Usb_force_host_mode(); 00168 usb_host_task_init(); 00169 #elif ((USB_DEVICE_FEATURE == DISABLE)&& (USB_HOST_FEATURE == DISABLE)) 00170 #error at least one of USB_DEVICE_FEATURE or USB_HOST_FEATURE should be enabled 00171 #endif 00172 // ----------------------------------------------------------------------------- 00173 00174 00175 } 00176 00187 void usb_task(void) 00188 { 00189 // ---- DUAL ROLE DEVICE USB MODE --------------------------------------------- 00190 #if ((USB_DEVICE_FEATURE == ENABLED)&& (USB_HOST_FEATURE == ENABLED)) 00191 if(Is_usb_id_device()) 00192 { g_usb_mode=USB_MODE_DEVICE;} 00193 else 00194 { g_usb_mode=USB_MODE_HOST;} 00195 // TODO !!! ID pin hot state change 00196 // Preliminary management: HARDWARE RESET !!! 00197 #if ( ID_PIN_CHANGE_GENERATE_RESET == ENABLE) 00198 // Hot ID transition generates wdt reset 00199 if((g_old_usb_mode!=g_usb_mode)) 00200 {Wdt_change_16ms(); while(1);} 00201 #endif 00202 // Depending on current usb mode, launch the correct usb task (device or host) 00203 switch(g_usb_mode) 00204 { 00205 case USB_MODE_DEVICE: 00206 usb_device_task(); 00207 break; 00208 case USB_MODE_HOST: 00209 usb_host_task(); 00210 break; 00211 case USB_MODE_UNDEFINED: // No break ! 00212 default: 00213 break; 00214 } 00215 // ----------------------------------------------------------------------------- 00216 00217 // ---- DEVICE ONLY USB MODE --------------------------------------------------- 00218 #elif ((USB_DEVICE_FEATURE == ENABLED)&& (USB_HOST_FEATURE == DISABLE)) 00219 usb_device_task(); 00220 // ----------------------------------------------------------------------------- 00221 00222 // ---- REDUCED HOST ONLY USB MODE --------------------------------------------- 00223 #elif ((USB_DEVICE_FEATURE == DISABLE)&& (USB_HOST_FEATURE == ENABLED)) 00224 usb_host_task(); 00225 // ----------------------------------------------------------------------------- 00226 00228 #elif ((USB_DEVICE_FEATURE == DISABLE)&& (USB_HOST_FEATURE == DISABLE)) 00229 #error at least one of USB_DEVICE_FEATURE or USB_HOST_FEATURE should be enabled 00230 #error otherwise the usb task has nothiing to do ... 00231 #endif 00232 // ----------------------------------------------------------------------------- 00233 00234 } 00235 00236 00265 #ifdef AVRGCC 00266 ISR(SIG_USB_GEN) 00267 #else 00268 #pragma vector = USB_GENERAL_vect 00269 __interrupt void usb_general_interrupt() 00270 #endif 00271 { 00272 // ---------- DEVICE events management ----------------------------------- 00273 #if (USB_DEVICE_FEATURE == ENABLED) 00274 //- VBUS state detection 00275 if (Is_usb_vbus_transition() && Is_usb_vbus_interrupt_enabled()) 00276 { 00277 Usb_ack_vbus_transition(); 00278 if (Is_usb_vbus_high()) 00279 { 00280 usb_connected = TRUE; 00281 Usb_vbus_on_action(); 00282 Usb_send_event(EVT_USB_POWERED); 00283 Usb_enable_reset_interrupt(); 00284 usb_start_device(); 00285 Usb_attach(); 00286 } 00287 else 00288 { 00289 Usb_vbus_off_action(); 00290 usb_connected = FALSE; 00291 Usb_send_event(EVT_USB_UNPOWERED); 00292 } 00293 } 00294 // - Device start of frame received 00295 if (Is_usb_sof() && Is_sof_interrupt_enabled()) 00296 { 00297 Usb_ack_sof(); 00298 Usb_sof_action(); 00299 } 00300 // - Device Suspend event (no more USB activity detected) 00301 if (Is_usb_suspend() && Is_suspend_interrupt_enabled()) 00302 { 00303 Usb_ack_suspend(); 00304 Usb_enable_wake_up_interrupt(); 00305 Usb_ack_wake_up(); // clear wake up to detect next event 00306 Usb_freeze_clock(); 00307 Usb_send_event(EVT_USB_SUSPEND); 00308 Usb_suspend_action(); 00309 } 00310 // - Wake up event (USB activity detected): Used to resume 00311 if (Is_usb_wake_up() && Is_swake_up_interrupt_enabled()) 00312 { 00313 Usb_unfreeze_clock(); 00314 Usb_ack_wake_up(); 00315 Usb_disable_wake_up_interrupt(); 00316 Usb_wake_up_action(); 00317 Usb_send_event(EVT_USB_WAKE_UP); 00318 } 00319 // - Resume state bus detection 00320 if (Is_usb_resume() && Is_resume_interrupt_enabled()) 00321 { 00322 Usb_disable_wake_up_interrupt(); 00323 Usb_ack_resume(); 00324 Usb_disable_resume_interrupt(); 00325 Usb_resume_action(); 00326 Usb_send_event(EVT_USB_RESUME); 00327 } 00328 // - USB bus reset detection 00329 if (Is_usb_reset()&& Is_reset_interrupt_enabled()) 00330 { 00331 Usb_ack_reset(); 00332 usb_init_device(); 00333 Usb_reset_action(); 00334 Usb_send_event(EVT_USB_RESET); 00335 } 00336 #endif// End DEVICE FEATURE MODE 00337 00338 // ---------- HOST events management ----------------------------------- 00339 #if (USB_HOST_FEATURE == ENABLED) 00340 // - ID pin change detection 00341 if(Is_usb_id_transition()&&Is_usb_id_interrupt_enabled()) 00342 { 00343 Usb_ack_id_transition(); 00344 if(Is_usb_id_device()) // Going to device mode 00345 { 00346 Usb_send_event(EVT_USB_DEVICE_FUNCTION); 00347 } 00348 else // Going to host mode 00349 { 00350 Usb_send_event(EVT_USB_HOST_FUNCTION); 00351 } 00352 #if ( ID_PIN_CHANGE_GENERATE_RESET == ENABLE) 00353 // Hot ID transition generates wdt reset 00354 Wdt_change_16ms(); while(1); 00355 #endif 00356 Usb_id_transition_action(); 00357 } 00358 // - The device has been disconnected 00359 if(Is_device_disconnection() && Is_host_device_disconnection_interrupt_enabled()) 00360 { 00361 Host_ack_device_disconnection(); 00362 device_state=DEVICE_UNATTACHED; 00363 Usb_send_event(EVT_HOST_DISCONNECTION); 00364 Host_device_disconnection_action(); 00365 } 00366 // - Device connection 00367 if(Is_device_connection() && Is_host_device_connection_interrupt_enabled()) 00368 { 00369 Host_ack_device_connection(); 00370 host_disable_all_pipe(); 00371 Host_device_connection_action(); 00372 } 00373 // - Host Start of frame has been sent 00374 if (Is_host_sof() && Is_host_sof_interrupt_enabled()) 00375 { 00376 Host_ack_sof(); 00377 Usb_send_event(EVT_HOST_SOF); 00378 private_sof_counter++; 00379 Host_sof_action(); 00380 } 00381 // - Host Wake-up has been received 00382 if (Is_host_hwup() && Is_host_hwup_interrupt_enabled()) 00383 { 00384 Host_disable_hwup_interrupt(); // Wake up interrupt should be disable host is now wake up ! 00385 // CAUTION HWUP can be cleared only when USB clock is active! 00386 Pll_start_auto(); // First Restart the PLL for USB operation 00387 Wait_pll_ready(); // Get sure pll is lock 00388 Usb_unfreeze_clock(); // Enable clock on USB interface 00389 Host_ack_hwup(); // Clear HWUP interrupt flag 00390 Usb_send_event(EVT_HOST_HWUP); // Send software event 00391 Host_hwup_action(); // Map custom action 00392 } 00393 #endif // End HOST FEATURE MODE 00394 } 00395 00396 00397 extern void suspend_action(void) 00398 { 00399 Enable_interrupt(); 00400 Enter_power_down_mode(); 00401 } 00402 00403 extern void host_suspend_action(void) 00404 { 00405 //Enter_power_down_mode(); 00406 }