- Main Page
- Modules
- Data Structures
- Files
- Related Pages

# usb_specific_request.c File Reference

`#include "config.h"`
`#include "conf_usb.h"`
`#include "lib_mcu/usb/usb_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_specific_request.c:

Go to the source code of this file.

## Functions
void | hid_get_report_descriptor (void)
void | usb_hid_set_report_ouput (void)
void | usb_hid_set_idle (U8 u8_report_id, U8 u8_duration)
void | usb_hid_get_idle (U8 u8_report_id)
void | hid_get_hid_descriptor (void)
void | usb_hid_set_report_feature (void)
Bool | usb_user_read_request (U8 type, U8 request)
| This function checks the specific request and if known then processes it
void | usb_user_endpoint_init (U8 conf_nb)
U8 | usb_user_interface_get (U16 wInterface)
void | usb_user_interface_reset (U16 wInterface, U8 alternate_setting)
Bool | usb_user_get_descriptor (U8 type, U8 string)
| This function fills the global descriptor.
## Variables
U8 code * | pbuffer
U8 | data_to_transfer
code S_usb_hid_report_descriptor | usb_hid_report_descriptor
U8 | jump_bootloader = 0
U8 | g_u8_report_rate = 0

## Detailed Description

user call-back functions This file contains the user call-back functions corresponding to the application: - Compiler: IAR EWAVR and GNU GCC for AVR

- Supported devices: AT90USB1287, AT90USB1286, AT90USB647, AT90USB646
  Author:
    Atmel Corporation: http://www.atmel.com
    Support and FAQ: http://support.atmel.no/

Definition in file usb_specific_request.c.

## Function Documentation

void hid_get_report_descriptor | ( | void |  | ) |

This function manages hit get repport request.

Definition at line 283 of file usb_specific_request.c.

References data_to_transfer, EP_CONTROL_LENGTH, FALSE, Is_usb_read_control_enabled, Is_usb_receive_out, LSB, MSB, pbuffer, S_usb_hid_report_descriptor::report, TRUE, Usb_ack_receive_out, Usb_ack_receive_setup, usb_hid_report_descriptor, Usb_read_byte, Usb_send_control_in, and Usb_write_byte.

Referenced by usb_user_read_request().

    00284 {
    00285    U16 wLength;
    00286    U8  nb_byte;
    00287    bit zlp = FALSE;
    00288    U16 wInterface;
    00289
    00290    LSB(wInterface)=Usb_read_byte();
    00291    MSB(wInterface)=Usb_read_byte();
    00292
    00293    data_to_transfer = sizeof(usb_hid_report_descriptor);
    00294    pbuffer = &(usb_hid_report_descriptor.report[0]);
    00295
    00296    LSB(wLength) = Usb_read_byte();
    00297    MSB(wLength) = Usb_read_byte();
    00298    Usb_ack_receive_setup();
    00299
    00300    if (wLength > data_to_transfer)
    00301    {
    00302       if ((data_to_transfer % EP_CONTROL_LENGTH) == 0) { zlp = TRUE; }
    00303       else { zlp = FALSE; }
    00304    }
    00305    else
    00306    {
    00307       data_to_transfer = (U8)wLength;           // send only requested number of data
    00308    }
    00309
    00310    while((data_to_transfer != 0) && (!Is_usb_receive_out()))
    00311    {
    00312       while(!Is_usb_read_control_enabled());
    00313
    00314       nb_byte=0;
    00315       while(data_to_transfer != 0)              // Send data until necessary
    00316       {
    00317          if(nb_byte++==EP_CONTROL_LENGTH)       // Check endpoint 0 size
    00318          {
    00319             break;
    00320          }
    00321 #ifndef __GNUC__
    00322          Usb_write_byte(*pbuffer++);
    00323 #else    // AVRGCC does not support point to PGM space
    00324 //warning with AVRGCC assumes devices descriptors are stored in the lower 64Kbytes of on-chip flash memory
    00325          Usb_write_byte(pgm_read_byte_near((unsigned int)pbuffer++));
    00326 #endif
    00327          data_to_transfer --;
    00328       }
    00329       Usb_send_control_in();
    00330    }
    00331
    00332    if(Is_usb_receive_out())
    00333    {
    00334       // abort from Host
    00335       Usb_ack_receive_out();
    00336       return;
    00337    }
    00338    if(zlp == TRUE)
    00339    {
    00340       while(!Is_usb_read_control_enabled());
    00341       Usb_send_control_in();
    00342    }
    00343
    00344    while(!Is_usb_receive_out());
    00345    Usb_ack_receive_out();
    00346 }

Here is the caller graph for this function:

void usb_hid_set_report_ouput | ( | void |  | ) |

This function manages hit set report request.

Definition at line 351 of file usb_specific_request.c.

References Is_usb_receive_out, Usb_ack_receive_out, Usb_ack_receive_setup, and Usb_send_control_in.

Referenced by usb_user_read_request().

    00352 {
    00353    Usb_ack_receive_setup();
    00354    Usb_send_control_in();
    00355
    00356    while(!Is_usb_receive_out());
    00357    Usb_ack_receive_out();
    00358    Usb_send_control_in();
    00359 }

Here is the caller graph for this function:

void usb_hid_set_idle | ( | U8 | u8_report_id,
|  | U8 | u8_duration |
| ) |  |  |

This function manages hid set idle request.

Parameters:

  | Duration | When the upper byte of wValue is 0 (zero), the duration is indefinite else from 0.004 to 1.020 seconds
  | Report | ID 0 the idle rate applies to all input reports, else only applies to the Report ID

Definition at line 367 of file usb_specific_request.c.

References g_u8_report_rate, Is_usb_in_ready, LSB, MSB, Usb_ack_receive_setup, Usb_read_byte, and Usb_send_control_in.

Referenced by usb_user_read_request().

    00368 {
    00369    U16 wInterface;
    00370
    00371    // Get interface number to put in idle mode
    00372    LSB(wInterface)=Usb_read_byte();
    00373    MSB(wInterface)=Usb_read_byte();
    00374    Usb_ack_receive_setup();
    00375
    00376    g_u8_report_rate = u8_duration;
    00377
    00378    Usb_send_control_in();
    00379    while(!Is_usb_in_ready());
    00380 }

Here is the caller graph for this function:

void usb_hid_get_idle | ( | U8 | u8_report_id | ) |

This function manages hid get idle request.

Parameters:

  | Report | ID 0 the idle rate applies to all input reports, else only applies to the Report ID

Definition at line 387 of file usb_specific_request.c.

References g_u8_report_rate, Is_usb_receive_out, LSB, MSB, Usb_ack_receive_out, Usb_ack_receive_setup, Usb_read_byte, Usb_send_control_in, and Usb_write_byte.

Referenced by usb_user_read_request().

    00388 {
    00389    U16 wLength;
    00390    U16 wInterface;
    00391
    00392    // Get interface number to put in idle mode
    00393    LSB(wInterface)= Usb_read_byte();
    00394    MSB(wInterface)= Usb_read_byte();
    00395    LSB(wLength)   = Usb_read_byte();
    00396    MSB(wLength)   = Usb_read_byte();
    00397    Usb_ack_receive_setup();
    00398
    00399    if( wLength != 0 )
    00400    {
    00401       Usb_write_byte(g_u8_report_rate);
    00402       Usb_send_control_in();
    00403    }
    00404
    00405    while(!Is_usb_receive_out());
    00406    Usb_ack_receive_out();
    00407 }

Here is the caller graph for this function:

void hid_get_hid_descriptor | ( | void |  | ) |

This function manages hid get hid descriptor request.

Definition at line 431 of file usb_specific_request.c.

References S_usb_hid_descriptor::bLength, data_to_transfer, EP_CONTROL_LENGTH, FALSE, S_usb_user_configuration_descriptor::hid, Is_usb_read_control_enabled, Is_usb_receive_out, LSB, MSB, pbuffer, TRUE, Usb_ack_receive_out, Usb_ack_receive_setup, usb_conf_desc, Usb_read_byte, Usb_send_control_in, and Usb_write_byte.

Referenced by usb_user_read_request().

    00432 {
    00433    U16 wLength;
    00434    U8  nb_byte;
    00435    bit zlp=FALSE;
    00436    U16 wInterface;
    00437
    00438    LSB(wInterface)=Usb_read_byte();
    00439    MSB(wInterface)=Usb_read_byte();
    00440
    00441    data_to_transfer = sizeof(usb_conf_desc.hid);
    00442    pbuffer = &(usb_conf_desc.hid.bLength);
    00443
    00444    LSB(wLength) = Usb_read_byte();
    00445    MSB(wLength) = Usb_read_byte();
    00446    Usb_ack_receive_setup();
    00447
    00448    if (wLength > data_to_transfer)
    00449    {
    00450       if ((data_to_transfer % EP_CONTROL_LENGTH) == 0) { zlp = TRUE; }
    00451       else { zlp = FALSE; }                     // no need of zero length packet
    00452    }
    00453    else
    00454    {
    00455       data_to_transfer = (U8)wLength;           // send only requested number of data
    00456    }
    00457
    00458    while((data_to_transfer != 0) && (!Is_usb_receive_out()))
    00459    {
    00460       while(!Is_usb_read_control_enabled());
    00461
    00462       nb_byte=0;
    00463       while(data_to_transfer != 0)              // Send data until necessary
    00464       {
    00465          if(nb_byte++==EP_CONTROL_LENGTH)       // Check endpoint 0 size
    00466          {
    00467             break;
    00468          }
    00469 #ifndef __GNUC__
    00470          Usb_write_byte(*pbuffer++);
    00471 #else    // AVRGCC does not support point to PGM space
    00472 //warning with AVRGCC assumes devices descriptors are stored in the lower 64Kbytes of on-chip flash memory
    00473          Usb_write_byte(pgm_read_byte_near((unsigned int)pbuffer++));
    00474 #endif
    00475          data_to_transfer --;
    00476       }
    00477       Usb_send_control_in();
    00478    }
    00479
    00480    if(Is_usb_receive_out())
    00481    {
    00482       // abort from Host
    00483       Usb_ack_receive_out();
    00484       return;
    00485    }
    00486    if(zlp == TRUE)
    00487    {
    00488       while(!Is_usb_read_control_enabled());
    00489       Usb_send_control_in();
    00490    }
    00491
    00492    while(!Is_usb_receive_out());
    00493    Usb_ack_receive_out();
    00494 }

Here is the caller graph for this function:

void usb_hid_set_report_feature | ( | void |  | ) |

Definition at line 409 of file usb_specific_request.c.

References Is_usb_in_ready, Is_usb_receive_out, jump_bootloader, Usb_ack_receive_out, Usb_ack_receive_setup, Usb_read_byte, and Usb_send_control_in.

Referenced by usb_user_read_request().

    00410 {
    00411
    00412    Usb_ack_receive_setup();
    00413    Usb_send_control_in();
    00414
    00415    while(!Is_usb_receive_out());
    00416
    00417    if(Usb_read_byte()==0x55)
    00418       if(Usb_read_byte()==0xAA)
    00419          if(Usb_read_byte()==0x55)
    00420             if(Usb_read_byte()==0xAA)
    00421             {
    00422                jump_bootloader=1;
    00423             }
    00424    Usb_ack_receive_out();
    00425    Usb_send_control_in();
    00426    while(!Is_usb_in_ready());
    00427 }

Here is the caller graph for this function:

Bool usb_user_read_request | ( | U8 | type,
|  | U8 | request |
| ) |  |  |

This function checks the specific request and if known then processes it

Parameters:

  | type | corresponding at bmRequestType (see USB specification)
  | request | corresponding at bRequest (see USB specification)

Returns:
  TRUE, when the request is processed

  FALSE, if the request is'nt know (STALL handshake is managed by the main standard request function).

Definition at line 88 of file usb_specific_request.c.

Referenced by usb_process_request().

    00089 {
    00090    U8    wValue_msb;
    00091    U8    wValue_lsb;
    00092
    00093    // Read wValue
    00094    wValue_lsb = Usb_read_byte();
    00095    wValue_msb = Usb_read_byte();
    00096
    00097    //** Specific request from Class HID
    00098    if( USB_SETUP_GET_STAND_INTERFACE == type )
    00099    {
    00100       switch( request )
    00101       {
    00102          case SETUP_GET_DESCRIPTOR:
    00103          switch( wValue_msb ) // Descriptor ID
    00104          {
    00105             case DESCRIPTOR_HID:
    00106             hid_get_hid_descriptor();
    00107             return TRUE;
    00108             break;
    00109
    00110             case DESCRIPTOR_REPORT:
    00111             hid_get_report_descriptor();
    00112             return TRUE;
    00113
    00114             case DESCRIPTOR_PHYSICAL:
    00115             // TODO
    00116             break;
    00117          }
    00118          break;
    00119       }
    00120    }
    00121    if( USB_SETUP_SET_CLASS_INTER == type )
    00122    {
    00123       switch( request )
    00124       {
    00125          case SETUP_HID_SET_REPORT:
    00126          // The MSB wValue field specifies the Report Type
    00127          // The LSB wValue field specifies the Report ID
    00128          switch (wValue_msb)
    00129          {
    00130             case REPORT_TYPE_INPUT:
    00131             // TODO
    00132             break;
    00133
    00134             case REPORT_TYPE_OUTPUT:
    00135             usb_hid_set_report_ouput();
    00136             return TRUE;
    00137             break;
    00138
    00139             case REPORT_TYPE_FEATURE:
    00140             usb_hid_set_report_feature();
    00141             return TRUE;
    00142             break;
    00143          }
    00144          break;
    00145
    00146          case SETUP_HID_SET_IDLE:
    00147          usb_hid_set_idle(wValue_lsb,wValue_msb);
    00148          return TRUE;
    00149
    00150          case SETUP_HID_SET_PROTOCOL:
    00151          // TODO
    00152          break;
    00153       }
    00154    }
    00155    if( USB_SETUP_GET_CLASS_INTER == type )
    00156    {
    00157       switch( request )
    00158       {
    00159          case SETUP_HID_GET_REPORT:
    00160          // TODO
    00161          break;
    00162          case SETUP_HID_GET_IDLE:
    00163          usb_hid_get_idle(wValue_lsb);
    00164          return TRUE;
    00165          case SETUP_HID_GET_PROTOCOL:
    00166          // TODO
    00167          break;
    00168       }
    00169    }
    00170    return FALSE;  // No supported request
    00171 }

Here is the caller graph for this function:

void usb_user_endpoint_init | ( | U8 | conf_nb | ) |

This function configures the endpoints

Parameters:

  | conf_nb | configuration number choosed by USB host

Definition at line 178 of file usb_specific_request.c.

Referenced by usb_set_configuration().

    00179 {
    00180    usb_configure_endpoint( EP_HID_IN,   \
    00181                            TYPE_INTERRUPT,\
    00182                            DIRECTION_IN,  \
    00183                            SIZE_8,        \
    00184                            ONE_BANK,      \
    00185                            NYET_ENABLED);
    00186
    00187    usb_configure_endpoint( EP_HID_OUT,    \
    00188                            TYPE_INTERRUPT,\
    00189                            DIRECTION_OUT, \
    00190                            SIZE_8,        \
    00191                            ONE_BANK,      \
    00192                            NYET_ENABLED);
    00193 }

Here is the caller graph for this function:

U8 usb_user_interface_get | ( | U16 | wInterface | ) |

This function returns the interface alternate setting

Parameters:

  | wInterface | Interface selected

Returns:
  alternate setting configurated

Definition at line 202 of file usb_specific_request.c.

Referenced by usb_get_interface().

    00203 {
    00204    return 0;  // Only one alternate setting possible for all interface
    00205 }

Here is the caller graph for this function:

void usb_user_interface_reset | ( | U16 | wInterface,
|  | U8 | alternate_setting |
| ) |  |  |

This function selects (and resets) the interface alternate setting

Parameters:

  | wInterface | Interface selected
  | alternate_setting | alternate setting selected

Definition at line 213 of file usb_specific_request.c.

Referenced by usb_set_interface().

    00214 {
    00215    // default setting selected = reset data toggle
    00216    if( INTERFACE_NB == wInterface )
    00217    {
    00218       // Interface HID
    00219       Usb_select_endpoint(EP_HID_IN);
    00220       Usb_disable_stall_handshake();
    00221       Usb_reset_endpoint(EP_HID_IN);
    00222       Usb_reset_data_toggle();
    00223       Usb_select_endpoint(EP_HID_OUT);
    00224       Usb_disable_stall_handshake();
    00225       Usb_reset_endpoint(EP_HID_OUT);
    00226       Usb_reset_data_toggle();
    00227    }
    00228 }

Here is the caller graph for this function:

Bool usb_user_get_descriptor | ( | U8 | type,
|  | U8 | string |
| ) |  |  |

This function fills the global descriptor.

Parameters:

  | type | corresponding at MSB of wValue (see USB specification)
  | string | corresponding at LSB of wValue (see USB specification)

Returns:
  FALSE, if the global descriptor no filled

Definition at line 238 of file usb_specific_request.c.

Referenced by usb_get_descriptor().

    00239 {
    00240    switch(type)
    00241    {
    00242       case DESCRIPTOR_STRING:
    00243       switch (string)
    00244       {
    00245          case LANG_ID:
    00246          data_to_transfer = sizeof (usb_user_language_id);
    00247          pbuffer = &(usb_user_language_id.bLength);
    00248          return TRUE;
    00249          break;
    00250
    00251          case MAN_INDEX:
    00252          data_to_transfer = sizeof (usb_user_manufacturer_string_descriptor);
    00253          pbuffer = &(usb_user_manufacturer_string_descriptor.bLength);
    00254          return TRUE;
    00255          break;
    00256
    00257          case PROD_INDEX:
    00258          data_to_transfer = sizeof (usb_user_product_string_descriptor);
    00259          pbuffer = &(usb_user_product_string_descriptor.bLength);
    00260          return TRUE;
    00261          break;
    00262
    00263 #if (USB_DEVICE_SN_USE==ENABLE)
    00264          case SN_INDEX:
    00265          data_to_transfer = sizeof (usb_user_serial_number);
    00266          pbuffer = &(usb_user_serial_number.bLength);
    00267 #if (USE_DEVICE_SN_UNIQUE==ENABLE)
    00268          f_get_serial_string=TRUE;
    00269          data_to_transfer += (SN_LENGTH*4);
    00270 #endif
    00271          return TRUE;
    00272          break;
    00273 #endif
    00274       }
    00275       break;
    00276    }
    00277    return FALSE;
    00278 }

Here is the caller graph for this function:

## Variable Documentation

U8 code* pbuffer

Definition at line 91 of file usb_standard_request.c.

Referenced by hid_get_hid_descriptor(), hid_get_report_descriptor(), usb_get_descriptor(), and usb_user_get_descriptor().

U8 data_to_transfer

Definition at line 96 of file usb_standard_request.c.

Referenced by hid_get_hid_descriptor(), hid_get_report_descriptor(), usb_get_descriptor(), and usb_user_get_descriptor().

code S_usb_hid_report_descriptor usb_hid_report_descriptor

Definition at line 179 of file usb_descriptors.c.

U8 jump_bootloader = 0

Definition at line 66 of file usb_specific_request.c.

U8 g_u8_report_rate = 0

Definition at line 68 of file usb_specific_request.c.

Referenced by usb_hid_get_idle(), and usb_hid_set_idle().