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

usb_enum.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 "lib_mcu\usb\usb_drv.h" 00028 #include "usb_enum.h" 00029 #include "usb_user_configuration.h" 00030 #include "usb_user_enum.h" 00031 00032 //_____ M A C R O S ________________________________________________________ 00033 00034 00035 //_____ D E F I N I T I O N ________________________________________________ 00036 00037 //_____ P R I V A T E D E C L A R A T I O N ______________________________ 00038 void usb_get_descriptor( void); 00039 static void usb_read_request( void); 00040 static void usb_set_address( void); 00041 static void usb_set_configuration(void); 00042 static void usb_clear_feature( void); 00043 static void usb_set_feature( void); 00044 static void usb_get_status( void); 00045 static void usb_get_configuration(void); 00046 00047 //_____ D E C L A R A T I O N ______________________________________________ 00048 00049 #ifndef NO_SUPPORT_USB_PING_PONG 00050 bdata bit rx_bank; 00051 #endif 00052 static bit zlp; 00053 static _MEM_TYPE_SLOW_ U8 endpoint_status[7]; 00054 _MEM_TYPE_SLOW_ U8 *pbuffer; 00055 _MEM_TYPE_SLOW_ U8 data_to_transfer; 00056 00057 00058 static U8 bmRequestType; 00059 00060 _MEM_TYPE_SLOW_ U8 usb_configuration_nb; 00061 extern bit usb_connected; 00062 extern code S_usb_device_descriptor usb_user_device_descriptor; 00063 extern code S_usb_user_configuration_descriptor usb_user_configuration_descriptor; 00064 00065 00078 void usb_enum_var_init (void) 00079 { 00080 U8 ep_num ; 00081 00082 for( ep_num=0 ; ep_num<NB_ENDPOINTS ; ep_num++ ) 00083 { 00084 endpoint_status[ep_num] = 0; 00085 } 00086 usb_connected = FALSE; 00087 usb_configuration_nb = 0 ; 00088 #ifndef NO_SUPPORT_USB_PING_PONG 00089 rx_bank = 0; 00090 #endif 00091 00092 } 00093 00094 00115 void usb_process_request(void) 00116 { 00117 U8 bmRequest; 00118 00119 Usb_select_endpoint(EP_CONTROL); 00120 00121 bmRequestType = Usb_read_byte(); 00122 bmRequest = Usb_read_byte(); 00123 00124 switch (bmRequest) 00125 { 00126 case GET_DESCRIPTOR: 00127 if (0x80 == bmRequestType) { usb_get_descriptor(); } 00128 else { usb_user_read_request(bmRequestType, bmRequest); } 00129 break; 00130 00131 case GET_CONFIGURATION: 00132 if (0x80 == bmRequestType) { usb_get_configuration(); } 00133 else { usb_user_read_request(bmRequestType, bmRequest); } 00134 break; 00135 00136 case SET_ADDRESS: 00137 if (0x00 == bmRequestType) { usb_set_address(); } 00138 else { usb_user_read_request(bmRequestType, bmRequest); } 00139 break; 00140 00141 case SET_CONFIGURATION: 00142 if (0x00 == bmRequestType) { usb_set_configuration(); } 00143 else { usb_user_read_request(bmRequestType, bmRequest); } 00144 break; 00145 00146 case CLEAR_FEATURE: 00147 if (0x02 >= bmRequestType) { usb_clear_feature(); } 00148 else { usb_user_read_request(bmRequestType, bmRequest); } 00149 break; 00150 00151 case SET_FEATURE: 00152 if (0x02 >= bmRequestType) { usb_set_feature(); } 00153 else { usb_user_read_request(bmRequestType, bmRequest); } 00154 break; 00155 00156 case GET_STATUS: 00157 if ((0x7F < bmRequestType) & (0x82 >= bmRequestType)) 00158 { usb_get_status(); } 00159 else { usb_user_read_request(bmRequestType, bmRequest); } 00160 break; 00161 00162 case GET_INTERFACE: 00163 case SET_DESCRIPTOR: 00164 case SET_INTERFACE: 00165 case SYNCH_FRAME: 00166 default: 00167 if(usb_user_read_request(bmRequestType, bmRequest) == FALSE) 00168 { 00169 Usb_enable_stall_handshake(); 00170 Usb_ack_receive_setup(); 00171 return; 00172 } 00173 break; 00174 } 00175 } 00176 00177 00189 void usb_set_address(void) 00190 { 00191 U8 addr; 00192 00193 addr = Usb_read_byte(); 00194 Usb_ack_receive_setup(); 00195 00196 Usb_send_control_in(); 00197 while(!Is_usb_in_ready()); 00198 00199 Usb_ack_in_ready(); // for Tiger compatibility 00200 Usb_configure_address(addr); 00201 Usb_enable_address(); 00202 } 00203 00204 00218 void usb_set_configuration( void ) 00219 { 00220 U8 configuration_number; 00221 00222 configuration_number = Usb_read_byte(); 00223 00224 if (configuration_number <= NB_CONFIGURATIONS) 00225 { 00226 Usb_ack_receive_setup(); 00227 usb_configuration_nb = configuration_number; 00228 } 00229 else 00230 { 00233 Usb_enable_stall_handshake(); 00234 Usb_ack_receive_setup(); 00235 return; 00236 } 00237 00238 Usb_send_control_in(); 00239 while(!Is_usb_in_ready()); 00240 Usb_ack_in_ready(); 00241 00242 usb_user_endpoint_init(usb_configuration_nb); 00243 Usb_set_configuration_action(); 00244 } 00245 00246 00261 void usb_get_descriptor(void) 00262 { 00263 U16 wLength ; 00264 U8 descriptor_type ; 00265 U8 string_type ; 00266 U8 c; // New 00267 00268 zlp = FALSE; /* no zero length packet */ 00269 string_type = Usb_read_byte(); /* read LSB of wValue */ 00270 descriptor_type = Usb_read_byte(); /* read MSB of wValue */ 00271 00272 switch (descriptor_type) 00273 { 00274 case DEVICE_DESCRIPTOR: 00275 data_to_transfer = Usb_get_dev_desc_length(); 00276 pbuffer = Usb_get_dev_desc_pointer(); 00277 break; 00278 case CONFIGURATION_DESCRIPTOR: 00279 data_to_transfer = Usb_get_conf_desc_length(); 00280 pbuffer = Usb_get_conf_desc_pointer(); 00281 break; 00282 default: 00283 if( usb_user_get_descriptor(descriptor_type, string_type)==FALSE ) 00284 { 00285 Usb_enable_stall_handshake(); 00286 Usb_ack_receive_setup(); 00287 return; 00288 } 00289 break; 00290 } 00291 00292 ACC = Usb_read_byte(); 00293 ACC = Usb_read_byte(); 00294 ((U8*)&wLength)[1] = Usb_read_byte(); 00295 ((U8*)&wLength)[0] = Usb_read_byte(); 00296 Usb_ack_receive_setup() ; 00297 Usb_set_DIR(); /* set out on EP0 */ 00298 00299 if (wLength > data_to_transfer) 00300 { 00301 if ((data_to_transfer % EP_CONTROL_LENGTH) == 0) { zlp = TRUE; } 00302 else { zlp = FALSE;} 00303 } 00304 else 00305 { 00306 data_to_transfer = (U8)wLength; 00307 } 00308 00309 while((data_to_transfer != 0) && (!Is_usb_receive_out())) 00310 { 00311 if (data_to_transfer > EP_CONTROL_LENGTH) { c = EP_CONTROL_LENGTH; } 00312 else { c = data_to_transfer; } 00313 data_to_transfer -= c; 00314 while ( c!= 0 ) 00315 { 00316 Usb_write_byte(*pbuffer); 00317 c--; 00318 pbuffer ++; 00319 } 00320 Usb_send_control_in(); 00321 while((!Is_usb_read_control_enabled()) && (!Is_usb_receive_out())); 00322 Usb_ack_in_ready(); 00323 /* while(!Is_usb_read_control_enabled()); // snd3 like, not compatible with c5131 00324 while(data_to_transfer != 0) 00325 { 00326 Usb_write_byte(*pbuffer); 00327 pbuffer ++; 00328 data_to_transfer --; 00329 } 00330 Usb_send_control_in(); 00331 */ 00332 } 00333 00334 // Usb_send_control_in(); 00335 00336 if(Is_usb_receive_out()) 00337 { 00338 Usb_ack_receive_out(); 00339 return; 00340 } 00341 if(zlp == TRUE) 00342 { 00343 Usb_send_control_in(); 00344 while((!Is_usb_read_control_enabled()) && (!Is_usb_receive_out())); 00345 Usb_ack_in_ready(); 00346 } 00347 00348 while(!Is_usb_receive_out()); 00349 Usb_ack_receive_out(); 00350 } 00351 00352 00364 void usb_get_configuration(void) 00365 { 00366 Usb_ack_receive_setup(); 00367 Usb_set_DIR(); /* set out on EP0 */ 00368 00369 Usb_write_byte(usb_configuration_nb); 00370 // Usb_ack_in_ready(); 00371 Usb_send_control_in(); 00372 while(!Is_usb_in_ready());// while(!Is_usb_read_control_enabled()); 00373 Usb_ack_in_ready(); 00374 while( !Is_usb_receive_out() ); 00375 Usb_ack_receive_out(); 00376 } 00377 00389 void usb_get_status(void) 00390 { 00391 U8 wIndex; 00392 00393 ACC = Usb_read_byte(); 00394 ACC = Usb_read_byte(); 00395 wIndex = Usb_read_byte(); 00396 00397 switch(bmRequestType) 00398 { 00399 case REQUEST_DEVICE_STATUS: Usb_ack_receive_setup(); 00400 Usb_set_DIR(); 00401 Usb_write_byte(DEVICE_STATUS); 00402 break; 00403 00404 case REQUEST_INTERFACE_STATUS: Usb_ack_receive_setup(); 00405 Usb_set_DIR(); 00406 Usb_write_byte(INTERFACE_STATUS); 00407 break; 00408 00409 case REQUEST_ENDPOINT_STATUS: Usb_ack_receive_setup(); 00410 Usb_set_DIR(); 00411 wIndex = wIndex & MSK_EP_DIR; 00412 Usb_write_byte(endpoint_status[wIndex]); 00413 break; 00414 default: 00415 Usb_enable_stall_handshake(); 00416 Usb_ack_receive_setup(); 00417 return; 00418 } 00419 00420 Usb_write_byte(0x00); 00421 Usb_send_control_in(); 00422 while(!Is_usb_in_ready()); 00423 Usb_ack_in_ready(); 00424 while( !Is_usb_receive_out() ); 00425 Usb_ack_receive_out(); 00426 } 00427 00428 00440 void usb_set_feature(void) 00441 { 00442 U8 wValue; 00443 U8 wIndex; 00444 00445 if (bmRequestType == INTERFACE_TYPE) 00446 { 00449 Usb_enable_stall_handshake(); 00450 Usb_ack_receive_setup(); 00451 return; 00452 } 00453 else if (bmRequestType == ENDPOINT_TYPE) 00454 { 00455 wValue = Usb_read_byte(); 00456 ACC = Usb_read_byte(); 00457 00458 if (wValue == FEATURE_ENDPOINT_HALT) 00459 { 00460 wIndex = (Usb_read_byte() & MSK_EP_DIR); 00461 00462 if (wIndex == EP_CONTROL) 00463 { 00464 Usb_enable_stall_handshake(); 00465 Usb_ack_receive_setup(); 00466 return; 00467 } 00468 00469 Usb_select_endpoint(wIndex); 00470 if(Is_usb_endpoint_enabled()) 00471 { 00472 Usb_enable_stall_handshake(); 00473 Usb_select_endpoint(EP_CONTROL); 00474 endpoint_status[wIndex] = 0x01; 00475 Usb_ack_receive_setup(); 00476 Usb_send_control_in(); 00477 } 00478 else 00479 { 00480 Usb_enable_stall_handshake(); 00481 Usb_ack_receive_setup(); 00482 return; 00483 } 00484 } 00485 else 00486 { 00487 Usb_enable_stall_handshake(); 00488 Usb_ack_receive_setup(); 00489 return; 00490 } 00491 } 00492 } 00493 00494 00505 void usb_clear_feature(void) 00506 { 00507 U8 wValue; 00508 U8 wIndex; 00509 00510 if (bmRequestType == ZERO_TYPE) 00511 { 00514 Usb_enable_stall_handshake(); 00515 Usb_ack_receive_setup(); 00516 return; 00517 } 00518 else if (bmRequestType == INTERFACE_TYPE) 00519 { 00522 Usb_enable_stall_handshake(); 00523 Usb_ack_receive_setup(); 00524 return; 00525 } 00526 else if (bmRequestType == ENDPOINT_TYPE) 00527 { 00528 wValue = Usb_read_byte(); 00529 ACC = Usb_read_byte(); 00530 00531 if (wValue == FEATURE_ENDPOINT_HALT) 00532 { 00533 wIndex = (Usb_read_byte() & MSK_EP_DIR); 00534 00535 Usb_select_endpoint(wIndex); 00536 if(Is_usb_endpoint_enabled()) 00537 { 00538 if(wIndex != EP_CONTROL) 00539 { 00540 Usb_disable_stall_handshake(); 00541 Usb_reset_endpoint(wIndex); 00542 Usb_reset_data_toggle(); 00543 } 00544 Usb_select_endpoint(EP_CONTROL); 00545 endpoint_status[wIndex] = 0x00; 00546 Usb_ack_receive_setup(); 00547 Usb_send_control_in(); 00548 } 00549 else 00550 { 00551 Usb_enable_stall_handshake(); 00552 Usb_ack_receive_setup(); 00553 return; 00554 } 00555 } 00556 else 00557 { 00558 Usb_enable_stall_handshake(); 00559 Usb_ack_receive_setup(); 00560 return; 00561 } 00562 } 00563 }

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