00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
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
00029
00030
00031
00032
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
00043
00044
00045
00058 Bool usb_user_read_request(U8 type, U8 request)
00059 {
00060 U8 descriptor_type ;
00061 U8 string_type ;
00062
00063 string_type = Usb_read_byte();
00064 descriptor_type = Usb_read_byte();
00065 switch(request)
00066 {
00067 case GET_DESCRIPTOR:
00068
00069 switch (descriptor_type)
00070 {
00071 case REPORT:
00072 hid_get_report();
00073 return TRUE;
00074 break;
00075
00076 case HID:
00077 hid_get_hid_descriptor();
00078 return TRUE;
00079 break;
00080 default:
00081 return FALSE;
00082 break;
00083 }
00084 break;
00085 case SET_CONFIGURATION:
00086 switch (descriptor_type)
00087 {
00088 case SET_REPORT:
00089 hid_set_report();
00090 return TRUE;
00091 break;
00092
00093 default:
00094 return FALSE;
00095 break;
00096 }
00097 break;
00098 case GET_INTERFACE:
00099
00100 usb_hid_get_interface();
00101 return TRUE;
00102 break;
00103
00104 default:
00105 return FALSE;
00106 break;
00107
00108 }
00109 return FALSE;
00110 }
00111
00112
00113
00122 void usb_user_endpoint_init(U8 conf_nb)
00123 {
00124 usb_configure_endpoint(EP_HID_IN, \
00125 TYPE_INTERRUPT, \
00126 DIRECTION_IN, \
00127 SIZE_8, \
00128 ONE_BANK, \
00129 NYET_ENABLED);
00130
00131 usb_configure_endpoint(EP_HID_OUT, \
00132 TYPE_INTERRUPT, \
00133 DIRECTION_OUT, \
00134 SIZE_8, \
00135 ONE_BANK, \
00136 NYET_ENABLED);
00137
00138 }
00139
00140
00147 Bool usb_user_get_descriptor(U8 type, U8 string)
00148 {
00149 switch(type)
00150 {
00151 case STRING_DESCRIPTOR:
00152 switch (string)
00153 {
00154 case LANG_ID:
00155 data_to_transfer = sizeof (usb_user_language_id);
00156 pbuffer = &(usb_user_language_id.bLength);
00157 return TRUE;
00158 break;
00159 case MAN_INDEX:
00160 data_to_transfer = sizeof (usb_user_manufacturer_string_descriptor);
00161 pbuffer = &(usb_user_manufacturer_string_descriptor.bLength);
00162 return TRUE;
00163 break;
00164 case PROD_INDEX:
00165 data_to_transfer = sizeof (usb_user_product_string_descriptor);
00166 pbuffer = &(usb_user_product_string_descriptor.bLength);
00167 return TRUE;
00168 break;
00169 case SN_INDEX:
00170 data_to_transfer = sizeof (usb_user_serial_number);
00171 pbuffer = &(usb_user_serial_number.bLength);
00172 return TRUE;
00173 break;
00174 default:
00175 return FALSE;
00176 }
00177 default:
00178 return FALSE;
00179 }
00180
00181 return FALSE;
00182 }
00183
00184
00193 void hid_get_report(void)
00194 {
00195
00196 U16 wLength;
00197 U8 nb_byte;
00198 bit zlp;
00199
00200
00201
00202 LSB(wInterface)=Usb_read_byte();
00203 MSB(wInterface)=Usb_read_byte();
00204
00205 data_to_transfer = sizeof(usb_hid_report_descriptor);
00206 pbuffer = &(usb_hid_report_descriptor.report[0]);
00207
00208 LSB(wLength) = Usb_read_byte();
00209 MSB(wLength) = Usb_read_byte();
00210 Usb_ack_receive_setup() ;
00211
00212 if (wLength > data_to_transfer)
00213 {
00214 if ((data_to_transfer % EP_CONTROL_LENGTH) == 0) { zlp = TRUE; }
00215 else { zlp = FALSE; }
00216 }
00217 else
00218 {
00219 data_to_transfer = (U8)wLength;
00220 }
00221
00222 while((data_to_transfer != 0) && (!Is_usb_receive_out()))
00223 {
00224 while(!Is_usb_read_control_enabled());
00225
00226 nb_byte=0;
00227 while(data_to_transfer != 0)
00228 {
00229 if(nb_byte++==EP_CONTROL_LENGTH)
00230 {
00231 break;
00232 }
00233 #ifndef AVRGCC
00234 Usb_write_byte(*pbuffer++);
00235 #else // AVRGCC does not support point to PGM space
00236 #warning with avrgcc assumes devices descriptors are stored in the lower 64Kbytes of on-chip flash memory
00237 Usb_write_byte(pgm_read_byte_near((unsigned int)pbuffer++));
00238 #endif
00239 data_to_transfer --;
00240 }
00241 Usb_send_control_in();
00242 }
00243
00244 Usb_send_control_in();
00245
00246 if(Is_usb_receive_out()) { Usb_ack_receive_out(); return; }
00247 if(zlp == TRUE) { Usb_send_control_in(); }
00248
00249 while(!Is_usb_receive_out());
00250 Usb_ack_receive_out();
00251 }
00252
00253
00254
00263 void hid_set_report (void)
00264 {
00265 Usb_ack_receive_setup();
00266 Usb_send_control_in();
00267
00268 while(!Is_usb_receive_out());
00269 Usb_ack_receive_out();
00270 Usb_send_control_in();
00271
00272 }
00273
00274
00283 void usb_hid_set_idle (void)
00284 {
00285 U8 dummy;
00286 dummy = Usb_read_byte();
00287 dummy = Usb_read_byte();
00288 LSB(wInterface)=Usb_read_byte();
00289 MSB(wInterface)=Usb_read_byte();
00290
00291 Usb_ack_receive_setup();
00292
00293 Usb_send_control_in();
00294 while(!Is_usb_in_ready());
00295 }
00296
00297
00306 void usb_hid_get_interface (void)
00307 {
00308 U8 dummy;
00309 dummy = Usb_read_byte();
00310 dummy = Usb_read_byte();
00311 LSB(wInterface)=Usb_read_byte();
00312 MSB(wInterface)=Usb_read_byte();
00313
00314 Usb_ack_receive_setup();
00315
00316 Usb_send_control_in();
00317 while(!Is_usb_in_ready());
00318 }
00319
00328 void hid_get_hid_descriptor(void)
00329 {
00330
00331 U16 wLength;
00332 U8 nb_byte;
00333 bit zlp;
00334
00335
00336
00337 LSB(wInterface)=Usb_read_byte();
00338 MSB(wInterface)=Usb_read_byte();
00339
00340 data_to_transfer = sizeof(usb_conf_desc.hid);
00341 pbuffer = &(usb_conf_desc.hid.bLength);
00342
00343 LSB(wLength) = Usb_read_byte();
00344 MSB(wLength) = Usb_read_byte();
00345 Usb_ack_receive_setup() ;
00346
00347 if (wLength > data_to_transfer)
00348 {
00349 if ((data_to_transfer % EP_CONTROL_LENGTH) == 0) { zlp = TRUE; }
00350 else { zlp = FALSE; }
00351 }
00352 else
00353 {
00354 data_to_transfer = (U8)wLength;
00355 }
00356
00357 while((data_to_transfer != 0) && (!Is_usb_receive_out()))
00358 {
00359 while(!Is_usb_read_control_enabled());
00360
00361 nb_byte=0;
00362 while(data_to_transfer != 0)
00363 {
00364 if(nb_byte++==EP_CONTROL_LENGTH)
00365 {
00366 break;
00367 }
00368 #ifndef AVRGCC
00369 Usb_write_byte(*pbuffer++);
00370
00371
00372
00373
00374
00375
00376
00377
00378
00379
00380 #else // AVRGCC does not support point to PGM space
00381 #warning with avrgcc assumes devices descriptors are stored in the lower 64Kbytes of on-chip flash memory
00382 Usb_write_byte(pgm_read_byte_near((unsigned int)pbuffer++));
00383 #endif
00384 data_to_transfer --;
00385 }
00386 Usb_send_control_in();
00387 }
00388
00389 Usb_send_control_in();
00390
00391 if(Is_usb_receive_out()) { Usb_ack_receive_out(); return; }
00392 if(zlp == TRUE) { Usb_send_control_in(); }
00393
00394 while(!Is_usb_receive_out());
00395 Usb_ack_receive_out();
00396 }