usb_standard_request.c File Reference

#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"

Include dependency graph for usb_standard_request.c:

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) /.
U8 device_status = DEVICE_STATUS


Detailed Description

Process USB device enumeration requests.

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.

Author:
Atmel Corporation: http://www.atmel.com
Support and FAQ: http://support.atmel.no/

Definition in file usb_standard_request.c.


Define Documentation

#define Usb_write_PGM_byte ( byte   )     (Usb_write_byte(*byte))

Definition at line 95 of file usb_standard_request.c.

Referenced by usb_get_descriptor().


Function Documentation

U8 bin_to_ascii ( U8  b  )  [static]

This function is used to convert a 4 bit number into an ascii character 5 => '5' 10 => 'A'.

Parameters:
binary value to convert
Returns:
converted character

Definition at line 639 of file usb_standard_request.c.

Referenced by usb_get_descriptor().

00640 {
00641 return ( (b <= 0x09) ? (b+'0') : (b+'A'-10) );
00642 }

Here is the caller graph for this function:

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, 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       }
00311               
00312       nb_byte=0;
00313       while(data_to_transfer != 0)        
00314       {
00315          if(nb_byte++==EP_CONTROL_LENGTH) 
00316             break;         
00317          
00318 #if (USE_DEVICE_SN_UNIQUE==ENABLE)
00319 
00320          if(f_get_serial_string && (data_to_transfer < (initial_data_to_transfer-1)))    //if we are sending the signature characters (third byte and more...)
00321          {                                                                              //(The first two bytes are the length and the descriptor)
00322             
00323             switch (byte_to_send)
00324             {
00325                case 0:
00326                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
00327                break;
00328               
00329                case 1:
00330                Usb_write_byte(0);                                                   //then, sends a null character (Usb_unicode)                    
00331                break;
00332     
00333                case 2:
00334                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 
00335                break;
00336     
00337                case 3:
00338                Usb_write_byte(0);                                                   //then, sends a null character (Usb_unicode)  
00339                sn_index++;                                                         //increments the signature address pointer.
00340                break;
00341             }
00342             byte_to_send = (byte_to_send+1)%4;     
00343          }
00344          else
00345          {
00346             Usb_write_PGM_byte(pbuffer++);                                                 //Write a flash byte to USB        
00347          }        
00348 #else          
00349          Usb_write_PGM_byte(pbuffer++);          
00350 #endif
00351          data_to_transfer --;                                                             //decrements the number of bytes to transmit.
00352       }
00353       
00354       if (Is_usb_nak_out_sent())
00355          break;
00356       else
00357          Usb_send_control_in();
00358    }
00359    
00360 #if (USE_DEVICE_SN_UNIQUE==ENABLE)      
00361    f_get_serial_string=FALSE;                                                   //end of signature transmission    
00362 #endif   
00363 
00364    if((zlp == TRUE) && (!Is_usb_nak_out_sent()))
00365    {
00366       while(!Is_usb_read_control_enabled());
00367       Usb_send_control_in();
00368    }
00369 
00370    while (!(Is_usb_nak_out_sent()));
00371    Usb_ack_nak_out();
00372    Usb_ack_control_out();
00373    return TRUE;
00374 }

Here is the call graph for this function:

Here is the caller graph for this function:

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, 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());                // waits for status phase done
00212                                              // before using the new address
00213    Usb_enable_address();
00214 }

Here is the caller graph for this function:

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 }

Here is the call graph for this function:

Here is the caller graph for this function:

void usb_get_configuration ( void   )  [static]

This function manages the GET CONFIGURATION request.

The current configuration number is returned.

Definition at line 380 of file usb_standard_request.c.

References Is_usb_receive_out, Usb_ack_in_ready, Usb_ack_receive_out, Usb_ack_receive_setup, usb_configuration_nb, and Usb_write_byte.

Referenced by usb_process_request().

00381 {
00382    Usb_ack_receive_setup();
00383 
00384    Usb_write_byte(usb_configuration_nb);
00385    Usb_ack_in_ready();
00386 
00387    while( !Is_usb_receive_out() );
00388    Usb_ack_receive_out();
00389 }

Here is the caller graph for this function:

Bool usb_get_status ( U8  bmRequestType  )  [static]

This function manages the GET STATUS request.

The device, interface or endpoint status is returned.

< dummy read

< dummy read

Definition at line 395 of file usb_standard_request.c.

References device_status, endpoint_status, FALSE, Is_usb_receive_out, 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().

00396 {
00397    U8 wIndex;
00398    U8 dummy;
00399 
00400    dummy    = Usb_read_byte();                 
00401    dummy    = Usb_read_byte();                 
00402    wIndex   = Usb_read_byte();
00403 
00404    switch(bmRequestType)
00405    {
00406       case USB_SETUP_GET_STAND_DEVICE:
00407       Usb_ack_receive_setup();
00408       Usb_write_byte(device_status);
00409       break;
00410 
00411       case USB_SETUP_GET_STAND_INTERFACE:
00412       Usb_ack_receive_setup();
00413       Usb_write_byte(0);      // Reserved - always 0
00414       break;
00415 
00416       case USB_SETUP_GET_STAND_ENDPOINT:
00417       Usb_ack_receive_setup();
00418       wIndex = wIndex & MSK_EP_DIR;
00419       Usb_write_byte( endpoint_status[wIndex] );
00420       break;
00421     
00422       default:
00423       return FALSE;
00424    }
00425    Usb_write_byte(0);
00426 
00427    Usb_send_control_in();
00428    while( !Is_usb_receive_out() );
00429    Usb_ack_receive_out();
00430    return TRUE;
00431 }

Here is the caller graph for this function:

Bool usb_set_feature ( U8  bmRequestType  )  [static]

This function manages the SET FEATURE request.

The USB test modes are supported by this function.

< dummy read

Definition at line 437 of file usb_standard_request.c.

References device_status, 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_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().

00438 {
00439    U8 wValue;
00440    U8 wIndex;
00441    U8 dummy;
00442 
00443    switch (bmRequestType)
00444    {
00445       case USB_SETUP_SET_STAND_DEVICE:
00446       wValue = Usb_read_byte();
00447       switch (wValue)
00448       {
00449          case USB_REMOTE_WAKEUP:
00450          if ((wValue != FEATURE_DEVICE_REMOTE_WAKEUP)
00451          ||  (USB_REMOTE_WAKEUP_FEATURE != ENABLED))
00452             return FALSE;              // Invalid request
00453          device_status |= USB_DEVICE_STATUS_REMOTEWAKEUP;
00454          remote_wakeup_feature = ENABLED;
00455          Usb_ack_receive_setup();
00456          Usb_send_control_in();
00457          break;
00458             
00459          default:
00460          return FALSE;                 // Unknow request
00461          break;
00462       }
00463       break;
00464 
00465       case USB_SETUP_SET_STAND_INTERFACE:
00466       return FALSE;                    // Unknow request
00467       break;
00468 
00469       case USB_SETUP_SET_STAND_ENDPOINT:
00470       wValue   = Usb_read_byte();
00471       dummy    = Usb_read_byte();                
00472       if (wValue != FEATURE_ENDPOINT_HALT)
00473          return FALSE;                 // Unknow request
00474       wIndex = (Usb_read_byte() & MSK_EP_DIR);
00475       if (wIndex == EP_CONTROL)
00476       {
00477          Usb_enable_stall_handshake();
00478          Usb_ack_receive_setup();
00479       }
00480       Usb_select_endpoint(wIndex);
00481       if( !Is_usb_endpoint_enabled())
00482       {
00483          Usb_select_endpoint(EP_CONTROL);
00484          return FALSE;              // Invalid request
00485       }
00486       Usb_enable_stall_handshake();
00487       Usb_select_endpoint(EP_CONTROL);
00488       endpoint_status[wIndex] = 0x01;
00489       Usb_ack_receive_setup();
00490       Usb_send_control_in();
00491       break;
00492 
00493       default:
00494       return FALSE;                    // Unknow request
00495       break;
00496    }
00497    return TRUE;
00498 }

Here is the caller graph for this function:

Bool usb_clear_feature ( U8  bmRequestType  )  [static]

This function manages the SET FEATURE request.

Definition at line 503 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().

00504 {
00505    U8 wValue;
00506    U8 wIndex;
00507    U8 dummy;
00508 
00509    switch (bmRequestType)
00510    {
00511       case  USB_SETUP_SET_STAND_DEVICE:
00512       wValue = Usb_read_byte();
00513       if ((wValue != FEATURE_DEVICE_REMOTE_WAKEUP) || (USB_REMOTE_WAKEUP_FEATURE != ENABLED))
00514          return FALSE;              // Invalid request
00515       device_status &= ~USB_DEVICE_STATUS_REMOTEWAKEUP;
00516       remote_wakeup_feature = DISABLED;
00517       Usb_ack_receive_setup();
00518       Usb_send_control_in();
00519       break;
00520    
00521       case USB_SETUP_SET_STAND_INTERFACE:
00522       return FALSE;                    // Unknow request
00523       break;
00524       
00525       case USB_SETUP_SET_STAND_ENDPOINT:
00526       wValue = Usb_read_byte();
00527       dummy  = Usb_read_byte();
00528       if (wValue != FEATURE_ENDPOINT_HALT)
00529          return FALSE;                 // Unknow request
00530       wIndex = (Usb_read_byte() & MSK_EP_DIR);
00531       Usb_select_endpoint(wIndex);
00532       if( !Is_usb_endpoint_enabled())
00533       {
00534          Usb_select_endpoint(EP_CONTROL);
00535          return FALSE;              // Invalid request
00536       } 
00537       if(wIndex != EP_CONTROL)
00538       {
00539          Usb_disable_stall_handshake();
00540          Usb_reset_endpoint(wIndex);
00541          Usb_reset_data_toggle();
00542       }
00543       Usb_select_endpoint(EP_CONTROL);
00544       endpoint_status[wIndex] = 0x00;
00545       Usb_ack_receive_setup();
00546       Usb_send_control_in();
00547       break;
00548    
00549       default:
00550       return FALSE;                    // Unknow request
00551       break;
00552    }
00553    return TRUE;
00554 }

Here is the caller graph for this function:

Bool usb_get_interface ( void   )  [static]

This function manages the SETUP_GET_INTERFACE request.

Definition at line 559 of file usb_standard_request.c.

References FALSE, Is_usb_receive_out, 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().

00560 {
00561    U16   wInterface;
00562    U8    wValue_msb;
00563    U8    wValue_lsb;
00564 
00565    // Read wValue
00566    wValue_lsb = Usb_read_byte();
00567    wValue_msb = Usb_read_byte();
00568    // wValue = Alternate Setting
00569    // wIndex = Interface
00570    LSB(wInterface)=Usb_read_byte();
00571    MSB(wInterface)=Usb_read_byte();
00572    if( (0!=wValue_msb) || (0!=wValue_msb) )
00573       return FALSE;
00574    Usb_ack_receive_setup();
00575    
00576    Usb_write_byte( usb_user_interface_get(wInterface) );
00577    Usb_send_control_in();
00578 
00579    while( !Is_usb_receive_out() );
00580    Usb_ack_receive_out();
00581    return TRUE;
00582 }

Here is the call graph for this function:

Here is the caller graph for this function:

Bool usb_set_interface ( void   )  [static]

This function manages the SETUP_SET_INTERFACE request.

Definition at line 587 of file usb_standard_request.c.

References EP_CONTROL, FALSE, Is_usb_in_ready, 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().

00588 {
00589    U16   wInterface;
00590    U8    wValue_msb;
00591    U8    wValue_lsb;
00592 
00593    // Read wValue
00594    wValue_lsb = Usb_read_byte();
00595    wValue_msb = Usb_read_byte();
00596    // wValue = Alternate Setting
00597    // wIndex = Interface
00598    LSB(wInterface)=Usb_read_byte();
00599    MSB(wInterface)=Usb_read_byte();
00600    if( 0!=wValue_msb )
00601       return FALSE;
00602    Usb_ack_receive_setup();
00603    
00604    usb_user_interface_reset(wInterface, wValue_lsb);
00605    Usb_select_endpoint(EP_CONTROL);
00606             
00607    Usb_send_control_in();
00608    while(!Is_usb_in_ready());
00609    return TRUE;
00610 }

Here is the call graph for this function:

Here is the caller graph for this function:


Variable Documentation

U8 code* pbuffer

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.

Referenced by usb_clear_feature(), usb_get_status(), usb_process_request(), and usb_set_feature().

U8 data_to_transfer

Definition at line 99 of file usb_standard_request.c.

U8 device_status = DEVICE_STATUS

Definition at line 102 of file usb_standard_request.c.

Referenced by usb_clear_feature(), usb_get_status(), and usb_set_feature().


Generated on Fri Sep 11 14:25:29 2009 for ATMEL by  doxygen 1.5.3