usb_specific_request.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 //_____ I N C L U D E S ____________________________________________________
00020 
00021 #include "config.h"
00022 #include "conf_usb.h"
00023 #include "lib_mcu\usb\usb_drv.h"
00024 #include "usb_descriptors.h"
00025 #include "modules\usb\device_chap9\usb_standard_request.h"
00026 #include "usb_specific_request.h"
00027 
00028 //_____ M A C R O S ________________________________________________________
00029 
00030 //_____ D E F I N I T I O N ________________________________________________
00031 
00032 //_____ P R I V A T E   D E C L A R A T I O N ______________________________
00033 
00034 #ifdef AVRGCC
00035 extern PGM_VOID_P pbuffer;
00036 #else
00037 extern U8   code *pbuffer;
00038 #endif
00039 extern U8   data_to_transfer;
00040 extern code S_usb_hid_report_descriptor usb_hid_report_descriptor;
00041 extern U16  wInterface;
00042 U8 jump_bootloader=0;
00043 
00044 
00045 //_____ D E C L A R A T I O N ______________________________________________
00046 
00059 Bool usb_user_read_request(U8 type, U8 request)
00060 {
00061 U8  descriptor_type ;
00062 U8  string_type     ;
00063 
00064    string_type     = Usb_read_byte();
00065    descriptor_type = Usb_read_byte();
00066    switch(request)
00067    {
00068       case GET_DESCRIPTOR:
00069 
00070          switch (descriptor_type)
00071          {
00072             case REPORT:
00073                hid_get_report();
00074                return TRUE;
00075                break;
00076 
00077             case HID:
00078                hid_get_hid_descriptor();
00079                return TRUE;
00080                break;
00081             default:
00082                return FALSE;
00083                break;
00084          }
00085          break;
00086       case SET_CONFIGURATION:
00087          switch (descriptor_type)
00088          {
00089             case SET_REPORT:
00090                hid_set_report();
00091                return TRUE;
00092                break;
00093 
00094             case HID:
00095                hid_set_report();
00096                return TRUE;
00097                break;
00098 
00099             case SET_HID_REPORT:
00100                usb_hid_user_set_hid_report();
00101                return TRUE;
00102                break;
00103 
00104             default:
00105                return FALSE;
00106                break;
00107          }
00108          break;
00109    case GET_INTERFACE:
00110 //      usb_hid_set_idle();
00111       usb_hid_get_interface();
00112       return TRUE;
00113       break;
00114 
00115       default:
00116          return FALSE;
00117          break;
00118 
00119    }
00120    return FALSE;
00121 }
00122 
00123 
00124 
00133 void usb_user_endpoint_init(U8 conf_nb)
00134 {
00135   usb_configure_endpoint(EP_HID_IN,      \
00136                          TYPE_INTERRUPT,     \
00137                          DIRECTION_IN,  \
00138                          SIZE_8,       \
00139                          ONE_BANK,     \
00140                          NYET_ENABLED);
00141 
00142   usb_configure_endpoint(EP_HID_OUT,      \
00143                          TYPE_INTERRUPT,     \
00144                          DIRECTION_OUT,  \
00145                          SIZE_8,       \
00146                          ONE_BANK,     \
00147                          NYET_ENABLED);
00148 
00149 }
00150 
00151 
00158 Bool usb_user_get_descriptor(U8 type, U8 string)
00159 {
00160    switch(type)
00161    {
00162       case STRING_DESCRIPTOR:
00163          switch (string)
00164          {
00165             case LANG_ID:
00166                data_to_transfer = sizeof (usb_user_language_id);
00167                pbuffer = &(usb_user_language_id.bLength);
00168                return TRUE;
00169                break;
00170             case MAN_INDEX:
00171                data_to_transfer = sizeof (usb_user_manufacturer_string_descriptor);
00172                pbuffer = &(usb_user_manufacturer_string_descriptor.bLength);
00173                return TRUE;
00174                break;
00175             case PROD_INDEX:
00176                data_to_transfer = sizeof (usb_user_product_string_descriptor);
00177                pbuffer = &(usb_user_product_string_descriptor.bLength);
00178                return TRUE;
00179                break;
00180             case SN_INDEX:
00181                data_to_transfer = sizeof (usb_user_serial_number);
00182                pbuffer = &(usb_user_serial_number.bLength);
00183                return TRUE;
00184                break;
00185             default:
00186                return FALSE;
00187          }
00188       default:
00189          return FALSE;
00190    }
00191 
00192    return FALSE;
00193 }
00194 
00195 
00204 void hid_get_report(void)
00205 {
00206 
00207 U16 wLength;
00208 U8  nb_byte;
00209 bit zlp;
00210 
00211 
00212 
00213    LSB(wInterface)=Usb_read_byte();
00214    MSB(wInterface)=Usb_read_byte();
00215 
00216    data_to_transfer = sizeof(usb_hid_report_descriptor);
00217    pbuffer = &(usb_hid_report_descriptor.report[0]);
00218 
00219    LSB(wLength) = Usb_read_byte();      
00220    MSB(wLength) = Usb_read_byte();
00221    Usb_ack_receive_setup() ;                  
00222 
00223    if (wLength > data_to_transfer)
00224    {
00225       if ((data_to_transfer % EP_CONTROL_LENGTH) == 0) { zlp = TRUE; }
00226       else { zlp = FALSE; }                   
00227    }
00228    else
00229    {
00230       data_to_transfer = (U8)wLength;         
00231    }
00232 
00233    while((data_to_transfer != 0) && (!Is_usb_receive_out()))
00234    {
00235       while(!Is_usb_read_control_enabled());
00236 
00237       nb_byte=0;
00238       while(data_to_transfer != 0)        
00239       {
00240          if(nb_byte++==EP_CONTROL_LENGTH) 
00241          {
00242             break;
00243          }
00244 #ifndef AVRGCC
00245          Usb_write_byte(*pbuffer++);
00246 #else    // AVRGCC does not support point to PGM space
00247 #warning with avrgcc assumes devices descriptors are stored in the lower 64Kbytes of on-chip flash memory
00248          Usb_write_byte(pgm_read_byte_near((unsigned int)pbuffer++));
00249 #endif
00250          data_to_transfer --;
00251       }
00252       Usb_send_control_in();
00253    }
00254 
00255    Usb_send_control_in();
00256 
00257    if(Is_usb_receive_out()) { Usb_ack_receive_out(); return; } 
00258    if(zlp == TRUE)        { Usb_send_control_in(); }
00259 
00260    while(!Is_usb_receive_out());
00261    Usb_ack_receive_out();
00262 }
00263 
00264 
00265 
00274 void hid_set_report (void)
00275 {
00276    Usb_ack_receive_setup();
00277    Usb_send_control_in();
00278 
00279    while(!Is_usb_receive_out());
00280    Usb_ack_receive_out();
00281    Usb_send_control_in();
00282 
00283 }
00284 
00285 
00294 void usb_hid_set_idle (void)
00295 {
00296   U8 dummy;
00297   dummy = Usb_read_byte();
00298   dummy = Usb_read_byte();
00299   LSB(wInterface)=Usb_read_byte();
00300   MSB(wInterface)=Usb_read_byte();
00301 
00302   Usb_ack_receive_setup();
00303 
00304   Usb_send_control_in();                       /* send a ZLP for STATUS phase */
00305   while(!Is_usb_in_ready());
00306 }
00307 
00308 
00317 void usb_hid_get_interface (void)
00318 {
00319   U8 dummy;
00320   dummy = Usb_read_byte();
00321   dummy = Usb_read_byte();
00322   LSB(wInterface)=Usb_read_byte();
00323   MSB(wInterface)=Usb_read_byte();
00324 
00325   Usb_ack_receive_setup();
00326 
00327   Usb_send_control_in();                       /* send a ZLP for STATUS phase */
00328   while(!Is_usb_in_ready());
00329 }
00330 
00331 void usb_hid_user_set_hid_report(void)
00332 {
00333 
00334    Usb_ack_receive_setup();
00335    Usb_send_control_in();
00336 
00337    while(!Is_usb_receive_out());
00338 
00339    if(Usb_read_byte()==0x55)
00340       if(Usb_read_byte()==0xAA)
00341          if(Usb_read_byte()==0x55)
00342             if(Usb_read_byte()==0xAA)
00343             {
00344                jump_bootloader=1;
00345             }
00346    Usb_ack_receive_out();
00347    Usb_send_control_in();
00348    while(!Is_usb_in_ready());
00349 }
00358 void hid_get_hid_descriptor(void)
00359 {
00360 
00361 U16 wLength;
00362 U8  nb_byte;
00363 bit zlp;
00364 
00365 
00366 
00367    LSB(wInterface)=Usb_read_byte();
00368    MSB(wInterface)=Usb_read_byte();
00369 
00370    data_to_transfer = sizeof(usb_conf_desc.hid);
00371    pbuffer = &(usb_conf_desc.hid.bLength);
00372 
00373    LSB(wLength) = Usb_read_byte();      
00374    MSB(wLength) = Usb_read_byte();
00375    Usb_ack_receive_setup() ;                  
00376 
00377    if (wLength > data_to_transfer)
00378    {
00379       if ((data_to_transfer % EP_CONTROL_LENGTH) == 0) { zlp = TRUE; }
00380       else { zlp = FALSE; }                   
00381    }
00382    else
00383    {
00384       data_to_transfer = (U8)wLength;         
00385    }
00386 
00387    while((data_to_transfer != 0) && (!Is_usb_receive_out()))
00388    {
00389       while(!Is_usb_read_control_enabled());
00390 
00391       nb_byte=0;
00392       while(data_to_transfer != 0)        
00393       {
00394          if(nb_byte++==EP_CONTROL_LENGTH) 
00395          {
00396             break;
00397          }
00398 #ifndef AVRGCC
00399          Usb_write_byte(*pbuffer++);
00400 
00401 
00402 
00403 
00404 
00405 
00406 
00407 
00408 
00409 
00410 #else    // AVRGCC does not support point to PGM space
00411 #warning with avrgcc assumes devices descriptors are stored in the lower 64Kbytes of on-chip flash memory
00412          Usb_write_byte(pgm_read_byte_near((unsigned int)pbuffer++));
00413 #endif
00414          data_to_transfer --;
00415       }
00416       Usb_send_control_in();
00417    }
00418 
00419    Usb_send_control_in();
00420 
00421    if(Is_usb_receive_out()) { Usb_ack_receive_out(); return; } 
00422    if(zlp == TRUE)        { Usb_send_control_in(); }
00423 
00424    while(!Is_usb_receive_out());
00425    Usb_ack_receive_out();
00426 }

Generated on Fri Jan 26 17:33:01 2007 for Atmel by  doxygen 1.5.1-p1