// ---------------------------------------------------------------------------- // If Isochronous mode is enabled and the max packet size is greater than 255, // break the FIFO writes up into multiple writes of 255 or less bytes. // ---------------------------------------------------------------------------- void USB_WriteFIFOIso(uint8_t fifoNum, uint16_t numBytes, uint8_t *dat) { uint8_t numBytesWrite; // USB_WriteFIFO() accepts a maximum of 255 bytes. If the number of bytes to // send is greated than 255, call USB_WriteFIFO() multiple times. while (numBytes > 0) { numBytesWrite = (numBytes > 255) ? 255 : numBytes; numBytes -= numBytesWrite; USB_WriteFIFO(fifoNum, numBytesWrite, dat, (numBytes == 0)); dat += numBytesWrite; } }
/***************************************************************************//** * @brief Handle Endpoint 2 IN transfer interrupt * @note This function takes no parameters, but it uses the EP2IN status * variables stored in @ref myUsbDevice.ep2in. ******************************************************************************/ void handleUsbIn2Int(void) { uint8_t xferred; bool callback; USB_SetIndex(2); if (USB_EpnInGetSentStall()) { USB_EpnInClearSentStall(); } else if (myUsbDevice.ep2in.state == D_EP_TRANSMITTING) { xferred = (myUsbDevice.ep2in.remaining > SLAB_USB_EP2IN_MAX_PACKET_SIZE) ? SLAB_USB_EP2IN_MAX_PACKET_SIZE : myUsbDevice.ep2in.remaining; myUsbDevice.ep2in.remaining -= xferred; myUsbDevice.ep2in.buf += xferred; callback = myUsbDevice.ep2in.misc.bits.callback; // Load more data if (myUsbDevice.ep2in.remaining > 0) { USB_WriteFIFO(2, (myUsbDevice.ep2in.remaining > SLAB_USB_EP2IN_MAX_PACKET_SIZE) ? SLAB_USB_EP2IN_MAX_PACKET_SIZE : myUsbDevice.ep2in.remaining, myUsbDevice.ep2in.buf, true); } else { myUsbDevice.ep2in.misc.bits.callback = false; myUsbDevice.ep2in.state = D_EP_IDLE; } if (callback == true) { USBD_XferCompleteCb(EP2IN, USB_STATUS_OK, xferred, myUsbDevice.ep2in.remaining); } } }
/***************************************************************************//** * @brief Handle Endpoint 3 IN transfer interrupt * @details Endpoint 3 IN is the only IN endpoint that supports isochronous * transfers. * @note This function takes no parameters, but it uses the EP3IN status * variables stored in @ref myUsbDevice.ep3in. ******************************************************************************/ void handleUsbIn3Int(void) { #if SLAB_USB_EP3IN_TRANSFER_TYPE == USB_EPTYPE_ISOC uint16_t xferred, nextIdx; #else uint8_t xferred; bool callback; #endif USB_SetIndex(3); if (USB_EpnInGetSentStall()) { USB_EpnInClearSentStall(); } else if (myUsbDevice.ep3in.state == D_EP_TRANSMITTING) { #if ((SLAB_USB_EP3IN_TRANSFER_TYPE == USB_EPTYPE_BULK) || (SLAB_USB_EP3IN_TRANSFER_TYPE == USB_EPTYPE_INTR)) xferred = (myUsbDevice.ep3in.remaining > SLAB_USB_EP3IN_MAX_PACKET_SIZE) ? SLAB_USB_EP3IN_MAX_PACKET_SIZE : myUsbDevice.ep3in.remaining; myUsbDevice.ep3in.remaining -= xferred; myUsbDevice.ep3in.buf += xferred; #endif #if ((SLAB_USB_EP3IN_TRANSFER_TYPE == USB_EPTYPE_BULK) || (SLAB_USB_EP3IN_TRANSFER_TYPE == USB_EPTYPE_INTR)) callback = myUsbDevice.ep3in.misc.bits.callback; #elif (SLAB_USB_EP3IN_TRANSFER_TYPE == USB_EPTYPE_ISOC) if (myUsbDevice.ep3in.misc.bits.callback == true) { // In Isochronous mode, the meaning of the USBD_XferCompleteCb parameters changes: // xferred is ignored // remaining is the current index into the circular buffer // the return value is the number of bytes to transmit in the next packet xferred = USBD_XferCompleteCb(EP3IN, USB_STATUS_OK, 0, myUsbDevice.ep3inIsoIdx); if (xferred == 0) { myUsbDevice.ep3in.misc.bits.inPacketPending = true; return; } } #endif // Load more data #if ((SLAB_USB_EP3IN_TRANSFER_TYPE == USB_EPTYPE_BULK) || (SLAB_USB_EP3IN_TRANSFER_TYPE == USB_EPTYPE_INTR)) if (myUsbDevice.ep3in.remaining > 0) { USB_WriteFIFO(3, (myUsbDevice.ep3in.remaining > SLAB_USB_EP3IN_MAX_PACKET_SIZE) ? SLAB_USB_EP3IN_MAX_PACKET_SIZE : myUsbDevice.ep3in.remaining, myUsbDevice.ep3in.buf, true); } else { myUsbDevice.ep3in.misc.bits.callback = false; myUsbDevice.ep3in.state = D_EP_IDLE; } if (callback == true) { USBD_XferCompleteCb(EP3IN, USB_STATUS_OK, xferred, myUsbDevice.ep3in.remaining); } #elif (SLAB_USB_EP3IN_TRANSFER_TYPE == USB_EPTYPE_ISOC) nextIdx = xferred + myUsbDevice.ep3inIsoIdx; myUsbDevice.ep3in.misc.bits.inPacketPending = false; // Check if the next index is past the end of the circular buffer. // If so, break the write up into two calls to USB_WriteFIFOIso() if (nextIdx > myUsbDevice.ep3in.remaining) { USB_WriteFIFOIso(3, myUsbDevice.ep3in.remaining - myUsbDevice.ep3inIsoIdx, &myUsbDevice.ep3in.buf[myUsbDevice.ep3inIsoIdx]); myUsbDevice.ep3inIsoIdx = nextIdx - myUsbDevice.ep3in.remaining; USB_WriteFIFOIso(3, myUsbDevice.ep3inIsoIdx, myUsbDevice.ep3in.buf); } else { USB_WriteFIFOIso(3, xferred, &myUsbDevice.ep3in.buf[myUsbDevice.ep3inIsoIdx]); myUsbDevice.ep3inIsoIdx = nextIdx; } #endif // ( ( SLAB_USB_EP3IN_TRANSFER_TYPE == USB_EPTYPE_BULK ) || ( SLAB_USB_EP3IN_TRANSFER_TYPE == USB_EPTYPE_INTR ) ) } }
/***************************************************************************//** * @brief Handles transmit data phase on Endpoint 0 ******************************************************************************/ static void handleUsbEp0Tx(void) { uint8_t count, count_snapshot, i; bool callback = myUsbDevice.ep0.misc.bits.callback; // The number of bytes to send in the next packet must be less than or equal // to the maximum EP0 packet size. count = (myUsbDevice.ep0.remaining >= USB_EP0_SIZE) ? USB_EP0_SIZE : myUsbDevice.ep0.remaining; // Save the packet size for future use. count_snapshot = count; // Strings can use the USB_STRING_DESCRIPTOR_UTF16LE_PACKED type to pack // UTF16LE data without the zero's between each character. // If the current string is of type USB_STRING_DESCRIPTOR_UTF16LE_PACKED, // unpacket it by inserting a zero between each character in the string. if (myUsbDevice.ep0String.encoding.type == USB_STRING_DESCRIPTOR_UTF16LE_PACKED) { // If ep0String.encoding.init is true, this is the beginning of the string. // The first two bytes of the string are the bLength and bDescriptorType // fields. These are no packed like the reset of the string, so write them // to the FIFO and set ep0String.encoding.init to false. if (myUsbDevice.ep0String.encoding.init == true) { USB_WriteFIFO(0, 2, myUsbDevice.ep0.buf, false); myUsbDevice.ep0.buf += 2; count -= 2; myUsbDevice.ep0String.encoding.init = false; } // Insert a 0x00 between each character of the string. for (i = 0; i < count / 2; i++) { USB_WriteFIFO(0, 1, myUsbDevice.ep0.buf, false); myUsbDevice.ep0.buf++; USB_WriteFIFO(0, 1, &txZero, false); } } // For any data other than USB_STRING_DESCRIPTOR_UTF16LE_PACKED, just send the // data normally. else { USB_WriteFIFO(0, count, myUsbDevice.ep0.buf, false); myUsbDevice.ep0.buf += count; } myUsbDevice.ep0.misc.bits.inPacketPending = false; myUsbDevice.ep0.remaining -= count_snapshot; // If the last packet of the transfer is exactly the maximum EP0 packet size, // we will have to send a ZLP (zero-length packet) after the last data packet // to signal to the host that the transfer is complete. // Check for the ZLP packet case here. if ((myUsbDevice.ep0.remaining == 0) && (count_snapshot != USB_EP0_SIZE)) { USB_Ep0SetLastInPacketReady(); myUsbDevice.ep0.state = D_EP_IDLE; myUsbDevice.ep0String.c = USB_STRING_DESCRIPTOR_UTF16LE; myUsbDevice.ep0.misc.c = 0; } else { // Do not call USB_Ep0SetLastInPacketReady() because we still need to send // the ZLP. USB_Ep0SetInPacketReady(); } // Make callback if requested if (callback == true) { USBD_XferCompleteCb(EP0, USB_STATUS_OK, count_snapshot, myUsbDevice.ep0.remaining); } }
int8_t USBD_Write(uint8_t epAddr, uint8_t *dat, uint16_t byteCount, bool callback) { bool usbIntsEnabled; USBD_Ep_TypeDef MEM_MODEL_SEG *ep; USB_SaveSfrPage(); // Verify the endpoint address is valid. switch (epAddr) { case EP0: #if SLAB_USB_EP1IN_USED case EP1IN: #endif #if SLAB_USB_EP2IN_USED case EP2IN: #endif #if SLAB_USB_EP3IN_USED case EP3IN: #endif break; #if SLAB_USB_EP1OUT_USED case EP1OUT: #endif #if SLAB_USB_EP2OUT_USED case EP2OUT: #endif #if SLAB_USB_EP3OUT_USED case EP3OUT: #endif default: SLAB_ASSERT(false); return USB_STATUS_ILLEGAL; } // If the device is not configured and it is not Endpoint 0, we cannot begin // a transfer. if ((epAddr != EP0) && (myUsbDevice.state != USBD_STATE_CONFIGURED)) { return USB_STATUS_DEVICE_UNCONFIGURED; } ep = GetEp(epAddr); // If the endpoint is not idle, we cannot start a new transfer. // Return the appropriate error code. if (ep->state != D_EP_IDLE) { if (ep->state == D_EP_STALL) { return USB_STATUS_EP_STALLED; } else { return USB_STATUS_EP_BUSY; } } DISABLE_USB_INTS; ep->buf = dat; ep->remaining = byteCount; ep->state = D_EP_TRANSMITTING; ep->misc.bits.callback = callback; switch (epAddr) { // For Endpoint 0, set the inPacketPending flag to true. The USB handler // will see this on the next SOF and begin the transfer. case (EP0): myUsbDevice.ep0.misc.bits.inPacketPending = true; break; // For data endpoints, we will call USB_WriteFIFO here to reduce latency // between the call to USBD_Write() and the first packet being sent. #if SLAB_USB_EP1IN_USED case (EP1IN): USB_WriteFIFO(1, (byteCount > SLAB_USB_EP1IN_MAX_PACKET_SIZE) ? SLAB_USB_EP1IN_MAX_PACKET_SIZE : byteCount, myUsbDevice.ep1in.buf, true); break; #endif // SLAB_USB_EP1IN_USED #if SLAB_USB_EP2IN_USED case (EP2IN): USB_WriteFIFO(2, (byteCount > SLAB_USB_EP2IN_MAX_PACKET_SIZE) ? SLAB_USB_EP2IN_MAX_PACKET_SIZE : byteCount, myUsbDevice.ep2in.buf, true); break; #endif // SLAB_USB_EP2IN_USED #if SLAB_USB_EP3IN_USED case (EP3IN): #if ((SLAB_USB_EP3IN_TRANSFER_TYPE == USB_EPTYPE_BULK) || (SLAB_USB_EP3IN_TRANSFER_TYPE == USB_EPTYPE_INTR)) USB_WriteFIFO(3, (byteCount > SLAB_USB_EP3IN_MAX_PACKET_SIZE) ? SLAB_USB_EP3IN_MAX_PACKET_SIZE : byteCount, myUsbDevice.ep3in.buf, true); #elif (SLAB_USB_EP3IN_TRANSFER_TYPE == USB_EPTYPE_ISOC) myUsbDevice.ep3in.misc.bits.inPacketPending = true; myUsbDevice.ep3inIsoIdx = 0; #endif break; #endif // SLAB_USB_EP3IN_USED } ENABLE_USB_INTS; USB_RestoreSfrPage(); return USB_STATUS_OK; }