#include "config.h"
#include "conf_usb.h"
#include "lib_mcu/usb/usb_drv.h"
#include "lib_mcu/pll/pll_drv.h"
#include "usb_descriptors.h"
#include "modules/usb/device_chap9/usb_standard_request.h"
#include "usb_specific_request.h"
#include "lib_mcu/flash/flash_drv.h"
Go to the source code of this file.
Defines | |
#define | Usb_write_PGM_byte(byte) (Usb_write_byte(*byte)) |
Functions | |
static U8 | bin_to_ascii (U8 b) |
This function is used to convert a 4 bit number into an ascii character 5 => '5' 10 => 'A'. | |
static Bool | usb_get_descriptor (void) |
This function manages the GET DESCRIPTOR request. | |
static void | usb_set_address (void) |
This function manages the SET ADDRESS request. | |
static Bool | usb_set_configuration (void) |
This function manages the SET CONFIGURATION request. | |
static void | usb_get_configuration (void) |
This function manages the GET CONFIGURATION request. | |
static Bool | usb_get_status (U8 bmRequestType) |
This function manages the GET STATUS request. | |
static Bool | usb_set_feature (U8 bmRequestType) |
This function manages the SET FEATURE request. | |
static Bool | usb_clear_feature (U8 bmRequestType) |
This function manages the SET FEATURE request. | |
static Bool | usb_get_interface (void) |
This function manages the SETUP_GET_INTERFACE request. | |
static Bool | usb_set_interface (void) |
This function manages the SETUP_SET_INTERFACE request. | |
void | usb_process_request (void) |
void | usb_generate_remote_wakeup (void) |
This function manages the remote wake up generation. | |
Variables | |
U8 | f_get_serial_string = FALSE |
U8 code * | pbuffer |
U8 | endpoint_status [MAX_EP_NB] |
U8 | data_to_transfer |
U8 | usb_configuration_nb |
Public : (U8) usb_configuration_nb Store the number of the USB configuration used by the USB device when its value is different from zero, it means the device mode is enumerated Used with USB_DEVICE_FEATURE == ENABLED only /. | |
U8 | remote_wakeup_feature = DISABLE |
Public : (U8) remote_wakeup_feature Store a host request for remote wake up (set feature received) /. | |
static U8 | device_status = DEVICE_STATUS |
This file contains the USB endpoint 0 management routines corresponding to the standard enumeration process (refer to chapter 9 of the USB specification. This file calls routines of the usb_specific_request.c file for non-standard request management. The enumeration parameters (descriptor tables) are contained in the usb_descriptors.c file.
Definition in file usb_standard_request.c.
#define Usb_write_PGM_byte | ( | byte | ) | (Usb_write_byte(*byte)) |
This function is used to convert a 4 bit number into an ascii character 5 => '5' 10 => 'A'.
binary | value to convert |
Definition at line 669 of file usb_standard_request.c.
Referenced by usb_get_descriptor().
Bool usb_get_descriptor | ( | void | ) | [static] |
This function manages the GET DESCRIPTOR request.
The device descriptor, the configuration descriptor and the device qualifier are supported. All other descriptors must be supported by the usb_user_get_descriptor function. Only 1 configuration is supported.
< sizeof (usb_user_device_descriptor);
< sizeof (usb_user_configuration_descriptor);
< don't care of wIndex field
< read wLength
< clear the receive setup flag
< send only requested number of data
< Send data until necessary
< Check endpoint 0 size
Definition at line 246 of file usb_standard_request.c.
References bin_to_ascii(), data_to_transfer, DESCRIPTOR_CONFIGURATION, DESCRIPTOR_DEVICE, EP_CONTROL_LENGTH, f_get_serial_string, FALSE, Flash_read_sn, Is_usb_nak_out_sent, Is_usb_read_control_enabled, Is_usb_vbus_low, LSB, MSB, pbuffer, TRUE, Usb_ack_control_out, Usb_ack_nak_out, Usb_ack_receive_setup, Usb_get_conf_desc_length, Usb_get_conf_desc_pointer, Usb_get_dev_desc_length, Usb_get_dev_desc_pointer, Usb_read_byte, Usb_send_control_in, usb_user_get_descriptor(), Usb_write_byte, and Usb_write_PGM_byte.
Referenced by usb_process_request().
00247 { 00248 Bool zlp; 00249 U16 wLength; 00250 U8 descriptor_type ; 00251 U8 string_type; 00252 U8 dummy; 00253 U8 nb_byte; 00254 U8 byte_to_send; 00255 #if (USE_DEVICE_SN_UNIQUE==ENABLE) 00256 U16 sn_index=0; 00257 U8 initial_data_to_transfer; 00258 #endif 00259 00260 zlp = FALSE; /* no zero length packet */ 00261 string_type = Usb_read_byte(); /* read LSB of wValue */ 00262 descriptor_type = Usb_read_byte(); /* read MSB of wValue */ 00263 00264 switch (descriptor_type) 00265 { 00266 case DESCRIPTOR_DEVICE: 00267 data_to_transfer = Usb_get_dev_desc_length(); 00268 pbuffer = Usb_get_dev_desc_pointer(); 00269 break; 00270 00271 case DESCRIPTOR_CONFIGURATION: 00272 data_to_transfer = Usb_get_conf_desc_length(); 00273 pbuffer = Usb_get_conf_desc_pointer(); 00274 break; 00275 00276 default: 00277 if( !usb_user_get_descriptor(descriptor_type, string_type)) 00278 return FALSE; // Unknow descriptor then stall request 00279 break; 00280 } 00281 00282 dummy = Usb_read_byte(); 00283 dummy = Usb_read_byte(); 00284 LSB(wLength) = Usb_read_byte(); 00285 MSB(wLength) = Usb_read_byte(); 00286 Usb_ack_receive_setup() ; 00287 00288 if (wLength > data_to_transfer) 00289 { 00290 if ((data_to_transfer % EP_CONTROL_LENGTH) == 0) { zlp = TRUE; } 00291 else { zlp = FALSE; } 00292 } 00293 else 00294 { 00295 data_to_transfer = (U8)wLength; 00296 } 00297 00298 Usb_ack_nak_out(); 00299 00300 byte_to_send=0; 00301 #if (USE_DEVICE_SN_UNIQUE==ENABLE) 00302 initial_data_to_transfer = data_to_transfer; 00303 #endif 00304 while((data_to_transfer != 0) && (!Is_usb_nak_out_sent())) 00305 { 00306 while(!Is_usb_read_control_enabled()) 00307 { 00308 if (Is_usb_nak_out_sent()) 00309 break; // don't clear the flag now, it will be cleared after 00310 if (Is_usb_vbus_low()) 00311 break; 00312 } 00313 00314 nb_byte=0; 00315 while(data_to_transfer != 0) 00316 { 00317 if(nb_byte++==EP_CONTROL_LENGTH) 00318 break; 00319 00320 #if (USE_DEVICE_SN_UNIQUE==ENABLE) 00321 00322 if(f_get_serial_string && (data_to_transfer < (initial_data_to_transfer-1))) //if we are sending the signature characters (third byte and more...) 00323 { //(The first two bytes are the length and the descriptor) 00324 00325 switch (byte_to_send) 00326 { 00327 case 0: 00328 Usb_write_byte(bin_to_ascii((Flash_read_sn(sn_index)>>4) & 0x0F)); //sends the fist part (MSB) of the signature hex number, converted in ascii 00329 break; 00330 00331 case 1: 00332 Usb_write_byte(0); //then, sends a null character (Usb_unicode) 00333 break; 00334 00335 case 2: 00336 Usb_write_byte(bin_to_ascii(Flash_read_sn(sn_index) & 0x0F)); //sends the second part (LSB) of the signature hex number, converted in ascii 00337 break; 00338 00339 case 3: 00340 Usb_write_byte(0); //then, sends a null character (Usb_unicode) 00341 sn_index++; //increments the signature address pointer. 00342 break; 00343 } 00344 byte_to_send = (byte_to_send+1)%4; 00345 } 00346 else 00347 { 00348 Usb_write_PGM_byte(pbuffer++); //Write a flash byte to USB 00349 } 00350 #else 00351 Usb_write_PGM_byte(pbuffer++); 00352 #endif 00353 data_to_transfer --; //decrements the number of bytes to transmit. 00354 } 00355 00356 if (Is_usb_nak_out_sent()) 00357 break; 00358 if (Is_usb_vbus_low()) 00359 break; 00360 Usb_send_control_in(); 00361 } 00362 00363 #if (USE_DEVICE_SN_UNIQUE==ENABLE) 00364 f_get_serial_string=FALSE; //end of signature transmission 00365 #endif 00366 00367 if((zlp == TRUE) && (!Is_usb_nak_out_sent())) 00368 { 00369 while(!Is_usb_read_control_enabled()) {if(Is_usb_vbus_low())break;} 00370 Usb_send_control_in(); 00371 } 00372 00373 while (!(Is_usb_nak_out_sent())) {if(Is_usb_vbus_low())break;} 00374 Usb_ack_nak_out(); 00375 Usb_ack_control_out(); 00376 return TRUE; 00377 }
void usb_set_address | ( | void | ) | [static] |
This function manages the SET ADDRESS request.
When complete, the device will filter the requests using the new address.
Definition at line 203 of file usb_standard_request.c.
References Is_usb_in_ready, Is_usb_vbus_low, Usb_ack_receive_setup, Usb_configure_address, Usb_enable_address, Usb_read_byte, and Usb_send_control_in.
Referenced by usb_process_request().
00204 { 00205 U8 addr = Usb_read_byte(); 00206 Usb_configure_address(addr); 00207 00208 Usb_ack_receive_setup(); 00209 00210 Usb_send_control_in(); // send a ZLP for STATUS phase 00211 while(!Is_usb_in_ready()) {if(Is_usb_vbus_low())break;} // waits for status phase done 00212 // before using the new address 00213 Usb_enable_address(); 00214 }
Bool usb_set_configuration | ( | void | ) | [static] |
This function manages the SET CONFIGURATION request.
If the selected configuration is valid, this function call the usb_user_endpoint_init() function that will configure the endpoints following the configuration number.
Definition at line 222 of file usb_standard_request.c.
References FALSE, NB_CONFIGURATION, TRUE, Usb_ack_receive_setup, usb_configuration_nb, Usb_read_byte, Usb_send_control_in, Usb_set_configuration_action, and usb_user_endpoint_init().
Referenced by usb_process_request().
00223 { 00224 U8 configuration_number; 00225 00226 // Get/Check new configuration 00227 configuration_number = Usb_read_byte(); 00228 if (configuration_number > NB_CONFIGURATION) 00229 return FALSE; // Bad configuration number then stall request 00230 Usb_ack_receive_setup(); 00231 usb_configuration_nb = configuration_number; 00232 00233 Usb_send_control_in(); // send a ZLP for STATUS phase 00234 usb_user_endpoint_init(usb_configuration_nb); // endpoint configuration 00235 Usb_set_configuration_action(); 00236 return TRUE; 00237 }
void usb_get_configuration | ( | void | ) | [static] |
This function manages the GET CONFIGURATION request.
The current configuration number is returned.
Definition at line 383 of file usb_standard_request.c.
References Is_usb_receive_out, Is_usb_vbus_low, Usb_ack_in_ready, Usb_ack_receive_out, Usb_ack_receive_setup, usb_configuration_nb, and Usb_write_byte.
Referenced by usb_process_request().
00384 { 00385 Usb_ack_receive_setup(); 00386 00387 Usb_write_byte(usb_configuration_nb); 00388 Usb_ack_in_ready(); 00389 00390 while( !Is_usb_receive_out() ){if(Is_usb_vbus_low())break;} 00391 Usb_ack_receive_out(); 00392 }
This function manages the GET STATUS request.
The device, interface or endpoint status is returned.
< dummy read
< dummy read
Definition at line 398 of file usb_standard_request.c.
References device_status, endpoint_status, FALSE, Is_usb_receive_out, Is_usb_vbus_low, MSK_EP_DIR, TRUE, Usb_ack_receive_out, Usb_ack_receive_setup, Usb_read_byte, Usb_send_control_in, USB_SETUP_GET_STAND_DEVICE, USB_SETUP_GET_STAND_ENDPOINT, USB_SETUP_GET_STAND_INTERFACE, and Usb_write_byte.
Referenced by usb_process_request().
00399 { 00400 U8 wIndex; 00401 U8 dummy; 00402 00403 dummy = Usb_read_byte(); 00404 dummy = Usb_read_byte(); 00405 wIndex = Usb_read_byte(); 00406 00407 switch(bmRequestType) 00408 { 00409 case USB_SETUP_GET_STAND_DEVICE: 00410 Usb_ack_receive_setup(); 00411 Usb_write_byte(device_status); 00412 break; 00413 00414 case USB_SETUP_GET_STAND_INTERFACE: 00415 Usb_ack_receive_setup(); 00416 Usb_write_byte(0); // Reserved - always 0 00417 break; 00418 00419 case USB_SETUP_GET_STAND_ENDPOINT: 00420 Usb_ack_receive_setup(); 00421 wIndex = wIndex & MSK_EP_DIR; 00422 Usb_write_byte( endpoint_status[wIndex] ); 00423 break; 00424 00425 default: 00426 return FALSE; 00427 } 00428 Usb_write_byte(0); 00429 00430 Usb_send_control_in(); 00431 while( !Is_usb_receive_out() ){if(Is_usb_vbus_low())break;} 00432 Usb_ack_receive_out(); 00433 return TRUE; 00434 }
This function manages the SET FEATURE request.
The USB test modes are supported by this function.
< dummy read
Definition at line 440 of file usb_standard_request.c.
References BDEV_HNP_NB_RETRY, device_status, DISABLED, ENABLED, endpoint_status, EP_CONTROL, FALSE, FEATURE_DEVICE_REMOTE_WAKEUP, FEATURE_ENDPOINT_HALT, HNP_SUPPORT, Is_usb_endpoint_enabled, MSK_EP_DIR, OTG_A_ALT_HNP_SUPPORT, OTG_A_HNP_SUPPORT, OTG_B_HNP_ENABLE, otg_features_supported, remote_wakeup_feature, TRUE, Usb_ack_receive_setup, USB_DEVICE_STATUS_REMOTEWAKEUP, Usb_enable_stall_handshake, Usb_read_byte, USB_REMOTE_WAKEUP, USB_REMOTE_WAKEUP_FEATURE, Usb_select_endpoint, Usb_send_control_in, USB_SETUP_SET_STAND_DEVICE, USB_SETUP_SET_STAND_ENDPOINT, and USB_SETUP_SET_STAND_INTERFACE.
Referenced by usb_process_request().
00441 { 00442 U8 wValue; 00443 U8 wIndex; 00444 U8 dummy; 00445 00446 switch (bmRequestType) 00447 { 00448 case USB_SETUP_SET_STAND_DEVICE: 00449 wValue = Usb_read_byte(); 00450 switch (wValue) 00451 { 00452 case USB_REMOTE_WAKEUP: 00453 if ((wValue != FEATURE_DEVICE_REMOTE_WAKEUP) 00454 || (USB_REMOTE_WAKEUP_FEATURE != ENABLED)) 00455 return FALSE; // Invalid request 00456 device_status |= USB_DEVICE_STATUS_REMOTEWAKEUP; 00457 remote_wakeup_feature = ENABLED; 00458 Usb_ack_receive_setup(); 00459 Usb_send_control_in(); 00460 break; 00461 00462 #if (USB_OTG_FEATURE == ENABLED) 00463 case OTG_B_HNP_ENABLE: 00464 if (((OTG_BMATTRIBUTES&HNP_SUPPORT) == 0) || (USB_OTG_FEATURE == DISABLED)) // see usb_descriptors.h 00465 return FALSE; // Invalid request 00466 otg_features_supported |= OTG_B_HNP_ENABLE; 00467 otg_device_nb_hnp_retry = BDEV_HNP_NB_RETRY; 00468 Usb_ack_receive_setup(); 00469 Usb_send_control_in(); 00470 break; 00471 00472 case OTG_A_HNP_SUPPORT: 00473 if (((OTG_BMATTRIBUTES&HNP_SUPPORT) == 0) || (USB_OTG_FEATURE == DISABLED)) 00474 return FALSE; // Invalid request 00475 otg_features_supported |= OTG_A_HNP_SUPPORT; 00476 Usb_ack_receive_setup(); 00477 Usb_send_control_in(); 00478 break; 00479 00480 case OTG_A_ALT_HNP_SUPPORT: 00481 if (((OTG_BMATTRIBUTES&HNP_SUPPORT) == 0) || (USB_OTG_FEATURE == DISABLED)) 00482 return FALSE; // Invalid request 00483 otg_features_supported |= OTG_A_ALT_HNP_SUPPORT; 00484 Usb_ack_receive_setup(); 00485 Usb_send_control_in(); 00486 break; 00487 #endif 00488 00489 default: 00490 return FALSE; // Unknow request 00491 break; 00492 } 00493 break; 00494 00495 case USB_SETUP_SET_STAND_INTERFACE: 00496 return FALSE; // Unknow request 00497 break; 00498 00499 case USB_SETUP_SET_STAND_ENDPOINT: 00500 wValue = Usb_read_byte(); 00501 dummy = Usb_read_byte(); 00502 if (wValue != FEATURE_ENDPOINT_HALT) 00503 return FALSE; // Unknow request 00504 wIndex = (Usb_read_byte() & MSK_EP_DIR); 00505 if (wIndex == EP_CONTROL) 00506 { 00507 Usb_enable_stall_handshake(); 00508 Usb_ack_receive_setup(); 00509 } 00510 Usb_select_endpoint(wIndex); 00511 if( !Is_usb_endpoint_enabled()) 00512 { 00513 Usb_select_endpoint(EP_CONTROL); 00514 return FALSE; // Invalid request 00515 } 00516 Usb_enable_stall_handshake(); 00517 Usb_select_endpoint(EP_CONTROL); 00518 endpoint_status[wIndex] = 0x01; 00519 Usb_ack_receive_setup(); 00520 Usb_send_control_in(); 00521 break; 00522 00523 default: 00524 return FALSE; // Unknow request 00525 break; 00526 } 00527 return TRUE; 00528 }
This function manages the SET FEATURE request.
Definition at line 533 of file usb_standard_request.c.
References device_status, DISABLED, ENABLED, endpoint_status, EP_CONTROL, FALSE, FEATURE_DEVICE_REMOTE_WAKEUP, FEATURE_ENDPOINT_HALT, Is_usb_endpoint_enabled, MSK_EP_DIR, remote_wakeup_feature, TRUE, Usb_ack_receive_setup, USB_DEVICE_STATUS_REMOTEWAKEUP, Usb_disable_stall_handshake, Usb_read_byte, USB_REMOTE_WAKEUP_FEATURE, Usb_reset_data_toggle, Usb_reset_endpoint, Usb_select_endpoint, Usb_send_control_in, USB_SETUP_SET_STAND_DEVICE, USB_SETUP_SET_STAND_ENDPOINT, and USB_SETUP_SET_STAND_INTERFACE.
Referenced by usb_process_request().
00534 { 00535 U8 wValue; 00536 U8 wIndex; 00537 U8 dummy; 00538 00539 switch (bmRequestType) 00540 { 00541 case USB_SETUP_SET_STAND_DEVICE: 00542 wValue = Usb_read_byte(); 00543 if ((wValue != FEATURE_DEVICE_REMOTE_WAKEUP) || (USB_REMOTE_WAKEUP_FEATURE != ENABLED)) 00544 return FALSE; // Invalid request 00545 device_status &= ~USB_DEVICE_STATUS_REMOTEWAKEUP; 00546 remote_wakeup_feature = DISABLED; 00547 Usb_ack_receive_setup(); 00548 Usb_send_control_in(); 00549 break; 00550 00551 case USB_SETUP_SET_STAND_INTERFACE: 00552 return FALSE; // Unknow request 00553 break; 00554 00555 case USB_SETUP_SET_STAND_ENDPOINT: 00556 wValue = Usb_read_byte(); 00557 dummy = Usb_read_byte(); 00558 if (wValue != FEATURE_ENDPOINT_HALT) 00559 return FALSE; // Unknow request 00560 wIndex = (Usb_read_byte() & MSK_EP_DIR); 00561 Usb_select_endpoint(wIndex); 00562 if( !Is_usb_endpoint_enabled()) 00563 { 00564 Usb_select_endpoint(EP_CONTROL); 00565 return FALSE; // Invalid request 00566 } 00567 if(wIndex != EP_CONTROL) 00568 { 00569 Usb_disable_stall_handshake(); 00570 Usb_reset_endpoint(wIndex); 00571 Usb_reset_data_toggle(); 00572 } 00573 Usb_select_endpoint(EP_CONTROL); 00574 endpoint_status[wIndex] = 0x00; 00575 Usb_ack_receive_setup(); 00576 Usb_send_control_in(); 00577 break; 00578 00579 default: 00580 return FALSE; // Unknow request 00581 break; 00582 } 00583 return TRUE; 00584 }
Bool usb_get_interface | ( | void | ) | [static] |
This function manages the SETUP_GET_INTERFACE request.
Definition at line 589 of file usb_standard_request.c.
References FALSE, Is_usb_receive_out, Is_usb_vbus_low, LSB, MSB, TRUE, Usb_ack_receive_out, Usb_ack_receive_setup, Usb_read_byte, Usb_send_control_in, usb_user_interface_get(), and Usb_write_byte.
Referenced by usb_process_request().
00590 { 00591 U16 wInterface; 00592 U8 wValue_msb; 00593 U8 wValue_lsb; 00594 00595 // Read wValue 00596 wValue_lsb = Usb_read_byte(); 00597 wValue_msb = Usb_read_byte(); 00598 // wValue = Alternate Setting 00599 // wIndex = Interface 00600 LSB(wInterface)=Usb_read_byte(); 00601 MSB(wInterface)=Usb_read_byte(); 00602 if( (0!=wValue_msb) || (0!=wValue_msb) ) 00603 return FALSE; 00604 Usb_ack_receive_setup(); 00605 00606 Usb_write_byte( usb_user_interface_get(wInterface) ); 00607 Usb_send_control_in(); 00608 00609 while( !Is_usb_receive_out() ){if(Is_usb_vbus_low())break;} 00610 Usb_ack_receive_out(); 00611 return TRUE; 00612 }
Bool usb_set_interface | ( | void | ) | [static] |
This function manages the SETUP_SET_INTERFACE request.
Definition at line 617 of file usb_standard_request.c.
References EP_CONTROL, FALSE, Is_usb_in_ready, Is_usb_vbus_low, LSB, MSB, TRUE, Usb_ack_receive_setup, Usb_read_byte, Usb_select_endpoint, Usb_send_control_in, and usb_user_interface_reset().
Referenced by usb_process_request().
00618 { 00619 U16 wInterface; 00620 U8 wValue_msb; 00621 U8 wValue_lsb; 00622 00623 // Read wValue 00624 wValue_lsb = Usb_read_byte(); 00625 wValue_msb = Usb_read_byte(); 00626 // wValue = Alternate Setting 00627 // wIndex = Interface 00628 LSB(wInterface)=Usb_read_byte(); 00629 MSB(wInterface)=Usb_read_byte(); 00630 if( 0!=wValue_msb ) 00631 return FALSE; 00632 Usb_ack_receive_setup(); 00633 00634 usb_user_interface_reset(wInterface, wValue_lsb); 00635 Usb_select_endpoint(EP_CONTROL); 00636 00637 Usb_send_control_in(); 00638 while(!Is_usb_in_ready()) {if(Is_usb_vbus_low())break;} 00639 return TRUE; 00640 }
Definition at line 94 of file usb_standard_request.c.
U8 endpoint_status[MAX_EP_NB] |
Definition at line 98 of file usb_standard_request.c.
Definition at line 99 of file usb_standard_request.c.
U8 device_status = DEVICE_STATUS [static] |
Definition at line 102 of file usb_standard_request.c.
Referenced by usb_clear_feature(), usb_get_status(), and usb_set_feature().