9 extern uint8 CODE usbConfigurationDescriptor[];
11 static void usbStandardDeviceRequestHandler();
13 #define CONTROL_TRANSFER_STATE_NONE 0
14 #define CONTROL_TRANSFER_STATE_WRITE 1
15 #define CONTROL_TRANSFER_STATE_READ 2
20 uint8 XDATA controlTransferState = CONTROL_TRANSFER_STATE_NONE;
61 static void basicUsbInit()
120 controlTransferState = CONTROL_TRANSFER_STATE_NONE;
143 controlTransferState = CONTROL_TRANSFER_STATE_NONE;
150 controlTransferState = CONTROL_TRANSFER_STATE_NONE;
158 if (controlTransferState == CONTROL_TRANSFER_STATE_WRITE)
161 uint8 bytesReceived = USBCNT0;
162 if (bytesReceived > controlTransferBytesLeft)
164 bytesReceived = controlTransferBytesLeft;
166 usbReadFifo(0, bytesReceived, controlTransferPointer);
167 controlTransferPointer += bytesReceived;
168 controlTransferBytesLeft -= bytesReceived;
170 if (controlTransferBytesLeft)
186 if (controlTransferState == CONTROL_TRANSFER_STATE_NONE)
189 USBCS0 = (1<<6) | (1<<3) | (1<<5);
194 USBCS0 = (1<<6) | (1<<3);
195 controlTransferState = CONTROL_TRANSFER_STATE_NONE;
199 else if (USBCNT0 == 8)
207 controlTransferState = CONTROL_TRANSFER_STATE_NONE;
214 usbStandardDeviceRequestHandler();
233 if (controlTransferState == CONTROL_TRANSFER_STATE_NONE)
236 USBCS0 = (1<<6) | (1<<5);
238 else if (controlTransferState == CONTROL_TRANSFER_STATE_WRITE)
240 if (controlTransferBytesLeft)
248 USBCS0 = (1<<6) | (1<<3) | (1<<1);
249 controlTransferState = CONTROL_TRANSFER_STATE_NONE;
262 if (!(usbcs0 & (1<<1)) && (controlTransferState == CONTROL_TRANSFER_STATE_READ))
270 usbcs0 = (1<<1)|(1<<3);
271 bytesToSend = controlTransferBytesLeft;
272 controlTransferState = CONTROL_TRANSFER_STATE_NONE;
286 controlTransferPointer += bytesToSend;
287 controlTransferBytesLeft -= bytesToSend;
297 static void usbStandardDeviceRequestHandler()
317 case USB_REQUEST_GET_DESCRIPTOR:
321 case USB_DESCRIPTOR_TYPE_DEVICE:
327 case USB_DESCRIPTOR_TYPE_CONFIGURATION:
337 controlTransferPointer = (
uint8 XDATA *)usbConfigurationDescriptor;
338 controlTransferBytesLeft = *(
uint16 *)&usbConfigurationDescriptor[2];
341 case USB_DESCRIPTOR_TYPE_STRING:
354 controlTransferBytesLeft = controlTransferPointer[0];
362 if (controlTransferState == CONTROL_TRANSFER_STATE_NONE)
371 controlTransferState = CONTROL_TRANSFER_STATE_READ;
374 case USB_REQUEST_SET_ADDRESS:
388 case USB_REQUEST_SET_CONFIGURATION:
425 case USB_REQUEST_GET_CONFIGURATION:
432 case USB_REQUEST_GET_INTERFACE:
456 case USB_REQUEST_GET_STATUS:
460 case USB_RECIPIENT_DEVICE:
468 case USB_RECIPIENT_INTERFACE:
488 case USB_RECIPIENT_ENDPOINT:
521 case USB_REQUEST_SET_FEATURE:
522 case USB_REQUEST_CLEAR_FEATURE:
528 case USB_REQUEST_SYNCH_FRAME:
558 uint8 savedPICTL = PICTL;
559 BIT savedP0IE = P0IE;
576 SLEEP = (SLEEP & ~3) | 1;
577 __asm nop __endasm; __asm nop __endasm; __asm nop __endasm;
603 while(!(SLEEP & (1<<6))){};
612 controlTransferState = CONTROL_TRANSFER_STATE_READ;
613 controlTransferBytesLeft = bytesCount;
614 controlTransferPointer = source;
619 controlTransferState = CONTROL_TRANSFER_STATE_WRITE;
620 controlTransferBytesLeft = bytesCount;
621 controlTransferPointer = source;
626 controlTransferState = CONTROL_TRANSFER_STATE_WRITE;
627 controlTransferBytesLeft = 0;
632 controlTransferState = CONTROL_TRANSFER_STATE_NONE;
637 USBINDEX = endpointNumber;
638 USBMAXI = (maxPacketSize + 7) >> 3;
644 USBINDEX = endpointNumber;
645 USBMAXO = (maxPacketSize + 7) >> 3;
struct USB_DESCRIPTOR_DEVICE USB_DESCRIPTOR_DEVICE
void usbCallbackInitEndpoints(void)
uint8 CODE usbStringDescriptorCount
void usbInitEndpointOut(uint8 endpointNumber, uint8 maxPacketSize)
volatile BIT usbActivityFlag
#define USB_EP0_PACKET_SIZE
enum USB_DEVICE_STATES XDATA usbDeviceState
void usbControlWrite(uint16 bytesCount, uint8 XDATA *source)
USB_DESCRIPTOR_DEVICE CODE usbDeviceDescriptor
USB_SETUP_PACKET XDATA usbSetupPacket
void usbWriteFifo(uint8 endpointNumber, uint8 count, const uint8 XDATA *buffer)
void usbInitEndpointIn(uint8 endpointNumber, uint8 maxPacketSize)
void usbReadFifo(uint8 endpointNumber, uint8 count, uint8 XDATA *buffer)
void usbCallbackSetupHandler(void)
void usbControlStall(void)
void usbControlAcknowledge(void)
void usbCallbackControlWriteHandler(void)
void usbControlRead(uint16 bytesCount, uint8 XDATA *source)
void usbCallbackClassDescriptorHandler(void)
volatile BIT usbSuspendMode
uint16 CODE *CODE usbStringDescriptors[]