These are the examples provided for [[/users/frank26080115/libraries/LPC1700CMSIS_Lib/]] Note, the entire "program" is not compilable!
usbcore.c
00001 /*---------------------------------------------------------------------------- 00002 * U S B - K e r n e l 00003 *---------------------------------------------------------------------------- 00004 * Name: USBCORE.C 00005 * Purpose: USB Core Module 00006 * Version: V1.10 00007 *---------------------------------------------------------------------------- 00008 * This software is supplied "AS IS" without any warranties, express, 00009 * implied or statutory, including but not limited to the implied 00010 * warranties of fitness for purpose, satisfactory quality and 00011 * noninfringement. Keil extends you a royalty-free right to reproduce 00012 * and distribute executable files created using this software for use 00013 * on NXP Semiconductors LPC family microcontroller devices only. Nothing 00014 * else gives you the right to use this software. 00015 * 00016 * Copyright (c) 2005-2009 Keil Software. 00017 *---------------------------------------------------------------------------*/ 00018 00019 00020 #include "lpc_types.h" 00021 00022 #include "usb.h" 00023 #include "usbcfg.h" 00024 #include "usbhw.h" 00025 #include "usbcore.h" 00026 #include "usbdesc.h" 00027 #include "usbuser.h" 00028 00029 #if (USB_CLASS) 00030 00031 #if (USB_AUDIO) 00032 #include "audio.h" 00033 #include "adcuser.h" 00034 #endif 00035 00036 #if (USB_HID) 00037 #include "hid.h" 00038 #include "hiduser.h" 00039 #endif 00040 00041 #if (USB_MSC) 00042 #include "msc.h" 00043 #include "mscuser.h" 00044 extern MSC_CSW CSW; 00045 #endif 00046 00047 #if (USB_CDC) 00048 #include "cdc.h" 00049 #include "cdcuser.h" 00050 #endif 00051 00052 #endif 00053 00054 #if (USB_VENDOR) 00055 #include "vendor.h" 00056 #endif 00057 00058 #ifndef __IAR_SYSTEMS_ICC__ 00059 #pragma diag_suppress 111,1441 00060 #endif 00061 00062 uint16_t USB_DeviceStatus; 00063 uint8_t USB_DeviceAddress; 00064 uint8_t USB_Configuration; 00065 uint32_t USB_EndPointMask; 00066 uint32_t USB_EndPointHalt; 00067 uint8_t USB_NumInterfaces; 00068 uint8_t USB_AltSetting[USB_IF_NUM]; 00069 00070 uint8_t EP0Buf[USB_MAX_PACKET0]; 00071 00072 00073 USB_EP_DATA EP0Data; 00074 00075 USB_SETUP_PACKET SetupPacket; 00076 00077 00078 /* 00079 * Reset USB Core 00080 * Parameters: None 00081 * Return Value: None 00082 */ 00083 00084 void USB_ResetCore (void) { 00085 00086 USB_DeviceStatus = USB_POWER; 00087 USB_DeviceAddress = 0; 00088 USB_Configuration = 0; 00089 USB_EndPointMask = 0x00010001; 00090 USB_EndPointHalt = 0x00000000; 00091 } 00092 00093 00094 /* 00095 * USB Request - Setup Stage 00096 * Parameters: None (global SetupPacket) 00097 * Return Value: None 00098 */ 00099 00100 void USB_SetupStage (void) { 00101 USB_ReadEP(0x00, (uint8_t *)&SetupPacket); 00102 } 00103 00104 00105 /* 00106 * USB Request - Data In Stage 00107 * Parameters: None (global EP0Data) 00108 * Return Value: None 00109 */ 00110 00111 void USB_DataInStage (void) { 00112 uint32_t cnt; 00113 00114 if (EP0Data.Count > USB_MAX_PACKET0) { 00115 cnt = USB_MAX_PACKET0; 00116 } else { 00117 cnt = EP0Data.Count; 00118 } 00119 cnt = USB_WriteEP(0x80, EP0Data.pData, cnt); 00120 EP0Data.pData += cnt; 00121 EP0Data.Count -= cnt; 00122 } 00123 00124 00125 /* 00126 * USB Request - Data Out Stage 00127 * Parameters: None (global EP0Data) 00128 * Return Value: None 00129 */ 00130 00131 void USB_DataOutStage (void) { 00132 uint32_t cnt; 00133 00134 cnt = USB_ReadEP(0x00, EP0Data.pData); 00135 EP0Data.pData += cnt; 00136 EP0Data.Count -= cnt; 00137 } 00138 00139 00140 /* 00141 * USB Request - Status In Stage 00142 * Parameters: None 00143 * Return Value: None 00144 */ 00145 00146 void USB_StatusInStage (void) { 00147 USB_WriteEP(0x80, NULL, 0); 00148 } 00149 00150 00151 /* 00152 * USB Request - Status Out Stage 00153 * Parameters: None 00154 * Return Value: None 00155 */ 00156 00157 void USB_StatusOutStage (void) { 00158 USB_ReadEP(0x00, EP0Buf); 00159 } 00160 00161 00162 /* 00163 * Get Descriptor USB Request 00164 * Parameters: None (global SetupPacket) 00165 * Return Value: TRUE - Success, FALSE - Error 00166 */ 00167 00168 #ifdef __IAR_SYSTEMS_ICC__ 00169 inline uint32_t USB_GetDescriptor (void) { 00170 #else 00171 __inline uint32_t USB_GetDescriptor (void) { 00172 #endif 00173 uint8_t *pD; 00174 uint32_t len, n; 00175 00176 switch (SetupPacket.bmRequestType.BM.Recipient) 00177 { 00178 case REQUEST_TO_DEVICE: 00179 switch (SetupPacket.wValue.WB.H) 00180 { 00181 case USB_DEVICE_DESCRIPTOR_TYPE: 00182 EP0Data.pData = (uint8_t *)USB_DeviceDescriptor; 00183 len = USB_DEVICE_DESC_SIZE; 00184 break; 00185 case USB_CONFIGURATION_DESCRIPTOR_TYPE: 00186 pD = (uint8_t *)USB_ConfigDescriptor; 00187 for (n = 0; n != SetupPacket.wValue.WB.L; n++) 00188 { 00189 if (((USB_CONFIGURATION_DESCRIPTOR *)pD)->bLength != 0) 00190 { 00191 pD += ((USB_CONFIGURATION_DESCRIPTOR *)pD)->wTotalLength; 00192 } 00193 } 00194 if (((USB_CONFIGURATION_DESCRIPTOR *)pD)->bLength == 0) 00195 { 00196 return (FALSE); 00197 } 00198 EP0Data.pData = pD; 00199 len = ((USB_CONFIGURATION_DESCRIPTOR *)pD)->wTotalLength; 00200 break; 00201 case USB_STRING_DESCRIPTOR_TYPE: 00202 EP0Data.pData = (uint8_t *)USB_StringDescriptor + SetupPacket.wValue.WB.L; 00203 len = ((USB_STRING_DESCRIPTOR *)EP0Data.pData)->bLength; 00204 break; 00205 default: 00206 return (FALSE); 00207 } 00208 break; 00209 case REQUEST_TO_INTERFACE: 00210 switch (SetupPacket.wValue.WB.H) 00211 { 00212 #if USB_HID 00213 case HID_HID_DESCRIPTOR_TYPE: 00214 if (SetupPacket.wIndex.WB.L != USB_HID_IF_NUM) 00215 { 00216 return (FALSE); /* Only Single HID Interface is supported */ 00217 } 00218 EP0Data.pData = (uint8_t *)USB_ConfigDescriptor + HID_DESC_OFFSET; 00219 len = HID_DESC_SIZE; 00220 break; 00221 case HID_REPORT_DESCRIPTOR_TYPE: 00222 if (SetupPacket.wIndex.WB.L != USB_HID_IF_NUM) 00223 { 00224 return (FALSE); /* Only Single HID Interface is supported */ 00225 } 00226 EP0Data.pData = (uint8_t *)HID_ReportDescriptor; 00227 len = HID_ReportDescSize; 00228 break; 00229 case HID_PHYSICAL_DESCRIPTOR_TYPE: 00230 return (FALSE); /* HID Physical Descriptor is not supported */ 00231 #endif 00232 default: 00233 return (FALSE); 00234 } 00235 break; 00236 default: 00237 return (FALSE); 00238 } 00239 00240 if (EP0Data.Count > len) 00241 { 00242 EP0Data.Count = len; 00243 } 00244 USB_DataInStage(); 00245 00246 return (TRUE); 00247 } 00248 00249 00250 /* 00251 * Set Configuration USB Request 00252 * Parameters: None (global SetupPacket) 00253 * Return Value: TRUE - Success, FALSE - Error 00254 */ 00255 00256 #ifdef __IAR_SYSTEMS_ICC__ 00257 inline uint32_t USB_SetConfiguration (void) { 00258 #else 00259 __inline uint32_t USB_SetConfiguration (void) { 00260 #endif 00261 USB_COMMON_DESCRIPTOR *pD; 00262 uint32_t alt, n; 00263 uint32_t tmp; 00264 00265 if (SetupPacket.wValue.WB.L) 00266 { 00267 pD = (USB_COMMON_DESCRIPTOR *)USB_ConfigDescriptor; 00268 while (pD->bLength) 00269 { 00270 switch (pD->bDescriptorType) 00271 { 00272 case USB_CONFIGURATION_DESCRIPTOR_TYPE: 00273 if (((USB_CONFIGURATION_DESCRIPTOR *)pD)->bConfigurationValue == SetupPacket.wValue.WB.L) 00274 { 00275 USB_Configuration = SetupPacket.wValue.WB.L; 00276 USB_Configure(TRUE); 00277 /* if (((USB_CONFIGURATION_DESCRIPTOR *)pD)->bmAttributes & USB_CONFIG_SELF_POWERED) 00278 { 00279 USB_DeviceStatus |= USB_GETSTATUS_SELF_POWERED; 00280 } 00281 else 00282 { 00283 USB_DeviceStatus &= ~USB_GETSTATUS_SELF_POWERED; 00284 } */ 00285 } 00286 else 00287 { 00288 // (uint8_t *)pD += ((USB_CONFIGURATION_DESCRIPTOR *)pD)->wTotalLength; 00289 tmp = (uint32_t)pD; 00290 tmp += ((USB_CONFIGURATION_DESCRIPTOR *)pD)->wTotalLength; 00291 pD = (USB_COMMON_DESCRIPTOR *)tmp; 00292 continue; 00293 } 00294 break; 00295 case USB_INTERFACE_DESCRIPTOR_TYPE: 00296 alt = ((USB_INTERFACE_DESCRIPTOR *)pD)->bAlternateSetting; 00297 break; 00298 case USB_ENDPOINT_DESCRIPTOR_TYPE: 00299 if (alt == 0) 00300 { 00301 n = ((USB_ENDPOINT_DESCRIPTOR *)pD)->bEndpointAddress & 0x8F; 00302 USB_ConfigEP((USB_ENDPOINT_DESCRIPTOR *)pD); 00303 USB_EnableEP(n); 00304 USB_ResetEP(n); 00305 } 00306 break; 00307 } 00308 // (uint8_t *)pD += pD->bLength; 00309 tmp = (uint32_t)pD; 00310 tmp += pD->bLength; 00311 pD = (USB_COMMON_DESCRIPTOR *)tmp; 00312 } 00313 } 00314 else 00315 { 00316 USB_Configuration = 0; 00317 USB_Configure(FALSE); 00318 } 00319 00320 if (USB_Configuration == SetupPacket.wValue.WB.L) 00321 { 00322 return (TRUE); 00323 } 00324 else 00325 { 00326 return (FALSE); 00327 } 00328 } 00329 00330 /* 00331 * USB Endpoint 0 Event Callback 00332 * Parameter: event 00333 */ 00334 00335 void USB_EndPoint0 (uint32_t event) 00336 { 00337 switch (event) 00338 { 00339 case USB_EVT_SETUP: 00340 USB_SetupStage(); 00341 EP0Data.Count = SetupPacket.wLength; 00342 switch (SetupPacket.bmRequestType.BM.Type) 00343 { 00344 case REQUEST_STANDARD: 00345 switch (SetupPacket.bRequest) 00346 { 00347 case USB_REQUEST_SET_ADDRESS: 00348 switch (SetupPacket.bmRequestType.BM.Recipient) 00349 { 00350 case REQUEST_TO_DEVICE: 00351 USB_DeviceAddress = SetupPacket.wValue.WB.L;//0x80 | SetupPacket.wValue.WB.L; 00352 USB_StatusInStage(); 00353 USB_SetAddress(USB_DeviceAddress); 00354 break; 00355 default: 00356 break; 00357 } 00358 break; 00359 00360 case USB_REQUEST_GET_DESCRIPTOR: 00361 USB_GetDescriptor(); 00362 break; 00363 00364 /* case USB_REQUEST_GET_CONFIGURATION: 00365 switch (SetupPacket.bmRequestType.BM.Recipient) 00366 { 00367 case REQUEST_TO_DEVICE: 00368 EP0Data.pData = &USB_Configuration; 00369 USB_DataInStage(); 00370 break; 00371 default: 00372 break; 00373 } 00374 break; */ 00375 00376 case USB_REQUEST_SET_CONFIGURATION: 00377 switch (SetupPacket.bmRequestType.BM.Recipient) 00378 { 00379 case REQUEST_TO_DEVICE: 00380 if (!USB_SetConfiguration()) 00381 { 00382 USB_SetStallEP(0x80); 00383 EP0Data.Count = 0; 00384 break; 00385 } 00386 USB_StatusInStage(); 00387 #if USB_CONFIGURE_EVENT 00388 USB_Configure_Event(); 00389 #endif 00390 break; 00391 default: 00392 break; 00393 } 00394 break; 00395 00396 default: 00397 break; 00398 00399 } 00400 break; 00401 00402 case REQUEST_CLASS: 00403 #if USB_CLASS 00404 switch (SetupPacket.bmRequestType.BM.Recipient) 00405 { 00406 case REQUEST_TO_INTERFACE: 00407 #if USB_HID 00408 if (SetupPacket.wIndex.WB.L == USB_HID_IF_NUM) 00409 { 00410 switch (SetupPacket.bRequest) 00411 { 00412 case HID_REQUEST_GET_REPORT: 00413 if (HID_GetReport()) 00414 { 00415 EP0Data.pData = EP0Buf; 00416 USB_DataInStage(); 00417 } 00418 break; 00419 case HID_REQUEST_SET_REPORT: 00420 EP0Data.pData = EP0Buf; 00421 break; 00422 case HID_REQUEST_GET_IDLE: 00423 if (HID_GetIdle()) 00424 { 00425 EP0Data.pData = EP0Buf; 00426 USB_DataInStage(); 00427 } 00428 break; 00429 case HID_REQUEST_SET_IDLE: 00430 if (HID_SetIdle()) 00431 { 00432 USB_StatusInStage(); 00433 } 00434 break; 00435 case HID_REQUEST_GET_PROTOCOL: 00436 if (HID_GetProtocol()) 00437 { 00438 EP0Data.pData = EP0Buf; 00439 USB_DataInStage(); 00440 } 00441 break; 00442 case HID_REQUEST_SET_PROTOCOL: 00443 if (HID_SetProtocol()) 00444 { 00445 USB_StatusInStage(); 00446 } 00447 break; 00448 default: 00449 break; 00450 } 00451 } 00452 #endif /* USB_HID */ 00453 default: 00454 break; 00455 } 00456 break; 00457 #else 00458 #endif /* USB_CLASS */ 00459 00460 case REQUEST_VENDOR: 00461 break; 00462 00463 default: 00464 USB_SetStallEP(0x80); 00465 EP0Data.Count = 0; 00466 break; 00467 } 00468 break; 00469 00470 case USB_EVT_OUT: 00471 if (SetupPacket.bmRequestType.BM.Dir == 0) 00472 { 00473 if (EP0Data.Count) 00474 { 00475 USB_DataOutStage(); 00476 if (EP0Data.Count == 0) 00477 { 00478 switch (SetupPacket.bmRequestType.BM.Type) 00479 { 00480 case REQUEST_STANDARD: 00481 USB_SetStallEP(0x80); 00482 //EP0Data.Count = 0; 00483 break; 00484 #if (USB_CLASS) 00485 case REQUEST_CLASS: 00486 switch (SetupPacket.bmRequestType.BM.Recipient) 00487 { 00488 case REQUEST_TO_INTERFACE: 00489 #if USB_HID 00490 if (SetupPacket.wIndex.WB.L == USB_HID_IF_NUM) 00491 { 00492 if (!HID_SetReport()) 00493 { 00494 USB_SetStallEP(0x80); 00495 //EP0Data.Count = 0; 00496 //break; 00497 } 00498 break; 00499 //HID_SetReport(); 00500 } 00501 #endif 00502 USB_SetStallEP(0x80); 00503 //EP0Data.Count = 0; 00504 break; 00505 case REQUEST_TO_ENDPOINT: 00506 USB_SetStallEP(0x80); 00507 //EP0Data.Count = 0; 00508 break; 00509 default: 00510 USB_SetStallEP(0x80); 00511 //EP0Data.Count = 0; 00512 break; 00513 } 00514 break; 00515 #endif 00516 default: 00517 USB_SetStallEP(0x80); 00518 //EP0Data.Count = 0; 00519 break; 00520 } 00521 USB_StatusInStage(); 00522 } 00523 } 00524 } 00525 else 00526 { 00527 ;//USB_StatusOutStage(); 00528 } 00529 00530 00531 break; 00532 00533 case USB_EVT_IN: 00534 if (SetupPacket.bmRequestType.BM.Dir == 1) 00535 { 00536 if(EP0Data.Count > 0) // ysq add 00537 USB_DataInStage(); 00538 } 00539 //else 00540 //{ 00541 // if (USB_DeviceAddress & 0x80) 00542 // { 00543 // USB_DeviceAddress &= 0x7F; 00544 // USB_SetAddress(USB_DeviceAddress); 00545 // } 00546 //} 00547 break; 00548 00549 case USB_EVT_IN_STALL: 00550 USB_ClrStallEP(0x80); 00551 break; 00552 00553 case USB_EVT_OUT_STALL: 00554 USB_ClrStallEP(0x00); 00555 break; 00556 } 00557 }
Generated on Tue Jul 12 2022 17:28:10 by 1.7.2