00001 00002 00003 00004 00005 00006 00007 00008 00009 00010 00011 00012 00013 00014 00015 00016 00017 00018 00019 00020 00021 00022 //_____ I N C L U D E S ___________________________________________________ 00023 00024 #include "config.h" 00025 #include "conf_usb.h" 00026 #include "usb_device_task.h" 00027 #include "modules\usb\usb_task.h" 00028 #if (USB_OTG_FEATURE == ENABLED) 00029 #include "modules/usb/host_chap9/usb_host_task.h" 00030 #endif 00031 #include "lib_mcu\usb\usb_drv.h" 00032 #include "usb_descriptors.h" 00033 #include "modules\usb\device_chap9\usb_standard_request.h" 00034 #include "lib_mcu\pll\pll_drv.h" 00035 00036 #if ((TARGET_BOARD==SPIDER) && (USB_OTG_FEATURE==ENABLED)) 00037 #include "lib_board/lcd/lcd_drv.h" 00038 #endif 00039 00040 //_____ M A C R O S ________________________________________________________ 00041 00042 00043 00044 00045 //_____ D E F I N I T I O N S ______________________________________________ 00046 00052 bit usb_connected=0; 00053 00059 extern U8 usb_configuration_nb; 00060 00061 00062 #if (USB_OTG_FEATURE == ENABLED) 00065 U8 otg_b_device_state; 00066 00069 U8 otg_device_sessions; 00070 00073 U8 otg_b_device_hnp; 00074 00077 U16 otg_tb_srp_cpt; 00078 00082 U8 sof_seen_in_session; 00083 #endif 00084 00085 00086 //_____ D E C L A R A T I O N S ____________________________________________ 00087 00100 void usb_device_task_init(void) 00101 { 00102 Enable_interrupt(); 00103 Usb_disable(); 00104 Usb_enable(); 00105 Usb_select_device(); 00106 #if (USB_LOW_SPEED_DEVICE==ENABLE) 00107 Usb_low_speed_mode(); 00108 #endif 00109 Usb_enable_vbus_interrupt(); 00110 Enable_interrupt(); 00111 #if (USB_OTG_FEATURE == ENABLED) 00112 Usb_enable_id_interrupt(); 00113 Clear_otg_features_from_host(); 00114 otg_device_sessions = 0; 00115 #endif 00116 } 00117 00131 void usb_start_device (void) 00132 { 00133 Pll_start_auto(); 00134 Wait_pll_ready(); 00135 Usb_unfreeze_clock(); 00136 Usb_enable_suspend_interrupt(); 00137 Usb_enable_reset_interrupt(); 00138 usb_init_device(); // configure the USB controller EP0 00139 Usb_attach(); 00140 #if (USB_OTG_FEATURE == ENABLED) 00141 Usb_enable_id_interrupt(); 00142 #endif 00143 } 00144 00155 void usb_device_task(void) 00156 { 00157 00158 #if (USB_OTG_FEATURE == ENABLED) 00159 // Check if a reset has been received 00160 if(Is_usb_event(EVT_USB_RESET)) 00161 { 00162 Usb_ack_event(EVT_USB_RESET); 00163 Usb_reset_endpoint(0); 00164 usb_configuration_nb=0; 00165 otg_b_device_state = B_IDLE; 00166 Clear_otg_features_from_host(); 00167 } 00168 00169 // When OTG mode enabled, B-Device is managed thanks to its state machine 00170 switch (otg_b_device_state) 00171 { 00172 //------------------------------------------------------ 00173 // B_IDLE state 00174 // 00175 // - waits for Vbus to rise 00176 // - initiate SRP if asked by user 00177 // 00178 case B_IDLE: 00179 if (Is_usb_vbus_high()) 00180 { 00181 // Vbus rise 00182 usb_connected = TRUE; 00183 remote_wakeup_feature = DISABLED; 00184 usb_start_device(); 00185 Usb_vbus_on_action(); 00186 Usb_attach(); 00187 otg_b_device_state = B_PERIPHERAL; 00188 Ack_user_request_srp(); 00189 Clear_otg_features_from_host(); 00190 remote_wakeup_feature = DISABLED; 00191 End_session_with_srp(); 00192 if (Is_srp_sent_and_waiting_answer() && (sof_seen_in_session == TRUE)) 00193 { 00194 Ack_srp_sent_and_answer(); 00195 Otg_print_new_failure_message(OTGMSG_A_RESPONDED,OTG_TEMPO_2SEC); 00196 } 00197 Usb_enable_sof_interrupt(); 00198 00199 } 00200 else 00201 { 00202 if (Is_user_requested_srp() && Is_usb_id_device()) 00203 { 00204 // User has requested a SRP 00205 Ack_user_request_srp(); 00206 if (!Is_srp_sent_and_waiting_answer()) 00207 { 00208 Pll_start_auto(); // reinit device mode 00209 Wait_pll_ready(); 00210 Usb_disable(); 00211 Usb_enable_uid_pin(); 00212 Usb_enable(); 00213 Usb_unfreeze_clock(); 00214 Usb_select_device(); 00215 Usb_attach(); 00216 otg_b_device_state = B_SRP_INIT; 00217 Usb_device_initiate_srp(); // hardware waits for initial condition (SE0, Session End level) 00218 sof_seen_in_session = FALSE; 00219 } 00220 } 00221 if ((Is_srp_sent_and_waiting_answer()) && (Is_tb_srp_counter_overflow())) 00222 { 00223 // SRP failed because A-Device did not respond 00224 End_session_with_srp(); 00225 Ack_srp_sent_and_answer(); 00226 Otg_print_new_failure_message(OTGMSG_SRP_A_NO_RESP,OTG_TEMPO_3SEC); 00227 } 00228 } 00229 break; 00230 00231 00232 //------------------------------------------------------ 00233 // B_SRP_INIT 00234 // 00235 // - a SRP has been initiated 00236 // - B-Device waits it is finished to initialize variables 00237 // 00238 case B_SRP_INIT: 00239 if (!Is_usb_device_initiating_srp()) 00240 { 00241 otg_b_device_state = B_IDLE; // SRP initiated, return to Idle state (wait for Vbus to rise) 00242 Srp_sent_and_waiting_answer(); 00243 Init_tb_srp_counter(); 00244 Start_session_with_srp(); 00245 Otg_print_new_event_message(OTGMSG_SRP_STARTED,TB_SRP_FAIL_MIN); 00246 } 00247 break; 00248 00249 00250 //------------------------------------------------------ 00251 // B_PERIPHERAL : the main state of OTG Peripheral 00252 // 00253 // - all events are interrupt-handled 00254 // - but they are saved and this function can execute alternate actions 00255 // - also handle user requests (disconnect) 00256 // 00257 // ====================================================================================== 00258 case B_PERIPHERAL: 00259 if (Is_otg_event(EVT_OTG_DEVICE_CONNECTED)) 00260 { 00261 Otg_ack_event(EVT_OTG_DEVICE_CONNECTED); // set on a SetConfiguration descriptor reception 00262 Otg_print_new_event_message(OTGMSG_CONNECTED_TO_A,OTG_TEMPO_4SEC); 00263 } 00264 if (Is_usb_event(EVT_USB_SUSPEND)) // SUSPEND state 00265 { 00266 // Suspend and HNP operations are handled in the interrupt functions 00267 } 00268 if (Is_srp_sent_and_waiting_answer() && (sof_seen_in_session == TRUE)) 00269 { 00270 Ack_srp_sent_and_answer(); 00271 Otg_print_new_failure_message(OTGMSG_A_RESPONDED,OTG_TEMPO_2SEC); 00272 } 00273 if ((Is_srp_sent_and_waiting_answer()) && (Is_tb_srp_counter_overflow())) 00274 { 00275 // SRP failed because A-Device did not respond 00276 End_session_with_srp(); 00277 Ack_srp_sent_and_answer(); 00278 Otg_print_new_failure_message(OTGMSG_SRP_A_NO_RESP,OTG_TEMPO_3SEC); 00279 } 00280 00281 if (Is_usb_event(EVT_USB_RESUME) && !Is_usb_pending_remote_wake_up()) // RESUME signal detected 00282 { 00283 Usb_ack_event(EVT_USB_RESUME); 00284 Usb_ack_event(EVT_USB_SUSPEND); 00285 Usb_ack_remote_wake_up_start(); 00286 } 00287 if (Is_usb_event(EVT_USB_UNPOWERED)) 00288 { 00289 Usb_ack_event(EVT_USB_UNPOWERED); 00290 Clear_all_user_request(); 00291 otg_b_device_state = B_IDLE; 00292 } 00293 if(Is_usb_event(EVT_USB_RESET)) 00294 { 00295 Usb_ack_event(EVT_USB_RESET); 00296 Usb_reset_endpoint(0); 00297 usb_configuration_nb=0; 00298 Clear_otg_features_from_host(); 00299 } 00300 if (Is_otg_event(EVT_OTG_HNP_ERROR)) 00301 { 00302 Otg_ack_event(EVT_OTG_HNP_ERROR); 00303 Otg_print_new_failure_message(OTGMSG_DEVICE_NO_RESP,OTG_TEMPO_4SEC); 00304 PORTC &= ~0x10; 00305 } 00306 if (Is_user_requested_disc()) 00307 { 00308 Ack_user_request_disc(); 00309 if (Is_usb_id_device()) 00310 { 00311 Usb_detach(); 00312 Usb_freeze_clock(); 00313 while (Is_usb_vbus_high()); // wait for Vbus to be under Va_vbus_valid 00314 otg_b_device_state = B_IDLE; 00315 usb_configuration_nb = 0; 00316 usb_connected = FALSE; 00317 Clear_all_user_request(); 00318 } 00319 } 00320 break; 00321 00322 //------------------------------------------------------ 00323 // B_HOST 00324 // 00325 // - state entered after an HNP success 00326 // - handle user requests (disconnection, suspend, hnp) 00327 // - call the "host_task()" for Host level handlers 00328 // 00329 // ====================================================================================== 00330 case B_HOST: 00331 if (Is_otg_event(EVT_OTG_DEV_UNSUPPORTED)) 00332 { 00333 Otg_ack_event(EVT_OTG_DEV_UNSUPPORTED); 00334 Clear_all_user_request(); 00335 otg_b_device_state = B_IDLE; 00336 device_state = DEVICE_UNATTACHED; 00337 } 00338 if (Is_user_requested_disc() || Is_user_requested_suspend() || Is_user_requested_hnp()) 00339 { 00340 Ack_user_request_disc(); // suspend and hnp requests cleared in B_END_HNP_SUSPEND stage 00341 Host_disable_sof(); // go into suspend mode 00342 Usb_host_reject_hnp(); 00343 otg_b_device_state = B_END_HNP_SUSPEND; 00344 Usb_ack_suspend(); 00345 Usb_enable_suspend_interrupt(); 00346 } 00347 if (Is_usb_event(EVT_USB_UNPOWERED)) 00348 { 00349 Usb_ack_event(EVT_USB_UNPOWERED); 00350 Usb_freeze_clock(); 00351 otg_b_device_state = B_IDLE; 00352 device_state = DEVICE_UNATTACHED; 00353 } 00354 usb_host_task(); // call the host task 00355 break; 00356 00357 //------------------------------------------------------ 00358 // B_END_HNP_SUSPEND 00359 // 00360 // - device enters this state after being B_HOST, on a user request to stop bus activity (suspend, disconnect or hnp request) 00361 // - macro is reset to peripheral mode 00362 // 00363 // ====================================================================================== 00364 case B_END_HNP_SUSPEND: 00365 if (Is_usb_event(EVT_USB_SUSPEND)) 00366 { 00367 Usb_ack_event(EVT_USB_SUSPEND); 00368 Usb_device_stop_hnp(); 00369 Usb_select_device(); 00370 device_state = DEVICE_UNATTACHED; 00371 if (Is_user_requested_hnp() || Is_user_requested_suspend()) 00372 { 00373 otg_b_device_state = B_PERIPHERAL; 00374 Ack_user_request_suspend(); 00375 Ack_user_request_hnp(); 00376 } 00377 else 00378 { 00379 otg_b_device_state = B_IDLE; 00380 Usb_detach(); 00381 Usb_freeze_clock(); 00382 } 00383 } 00384 break; 00385 00386 00387 default: 00388 otg_b_device_state = B_IDLE; 00389 Clear_all_user_request(); 00390 device_state = DEVICE_UNATTACHED; 00391 break; 00392 } 00393 00394 00395 #else 00396 00397 // Non-OTG exclusives Device operations 00398 if (Is_usb_vbus_high()&& usb_connected==FALSE) 00399 { 00400 usb_connected = TRUE; 00401 remote_wakeup_feature = DISABLED; 00402 usb_start_device(); 00403 Usb_vbus_on_action(); 00404 Usb_attach(); 00405 } 00406 00407 if(Is_usb_event(EVT_USB_RESET)) 00408 { 00409 Usb_ack_event(EVT_USB_RESET); 00410 Usb_reset_endpoint(0); 00411 usb_configuration_nb=0; 00412 } 00413 #endif 00414 00415 00416 00417 // ======================================= 00418 // Common Standard Device Control Requests 00419 // ======================================= 00420 // - device enumeration process 00421 // - device control commands and features 00422 Usb_select_endpoint(EP_CONTROL); 00423 if (Is_usb_receive_setup()) 00424 { 00425 usb_process_request(); 00426 } 00427 }
1.5.1-p1