void InitControl(int end) { SetEP(0); _cmark = 0; _cend = end; }
void USB_Flush(uint8_t ep) { SetEP(ep); if (FifoByteCount()) ReleaseTX(); }
// Space in send EP uint8_t USBD_SendSpace(uint8_t ep) { SetEP(ep); if (!ReadWriteAllowed()) { return 0; } return 64 - FifoByteCount(); }
void Recv(u8 ep, u8* dst, u8 len) { SetEP(ep); while (len--) { while (!ReadWriteAllowed()) ; *dst++ = Recv8(); if (!ReadWriteAllowed()) // release empty buffer ReleaseRX(); } }
void USBSetupInterrupt() { SetEP(0); if (!ReceivedSetupInt()) return; Setup& setup = _setup; // global saves ~30 bytes Recv((u8*)&setup,8); ClearSetupInt(); if (setup.bmRequestType & DEVICETOHOST) WaitIN(); else ClearIN(); bool ok = true; u8 r = setup.bRequest; if (SET_ADDRESS == r) { WaitIN(); UDADDR = setup.wValueL | (1<<ADDEN); } else if (SET_CONFIGURATION == r) { _usbConfiguration = setup.wValueL; InitEndpoints(); } else if (GET_CONFIGURATION == r) { Send8(_usbConfiguration); } else if (GET_STATUS == r) { Send8(0); // All good as far as I know } else if (GET_DESCRIPTOR == r) { ok = SendDescriptor(); } else { ok = USBHook(); } if (ok) ClearIN(); else Stall(); }
void USBD_InitControl(int end) { SetEP(0); UDPHS->UDPHS_EPT[0].UDPHS_EPTCFG = _initEndpoints[0]; while( (signed int)UDPHS_EPTCFG_EPT_MAPD != (signed int)((UDPHS->UDPHS_EPT[0].UDPHS_EPTCFG) & (unsigned int)UDPHS_EPTCFG_EPT_MAPD) ) ; UDPHS->UDPHS_EPT[0].UDPHS_EPTCTLENB = UDPHS_EPTCTLENB_RX_BK_RDY | UDPHS_EPTCTLENB_RX_SETUP | UDPHS_EPTCTLENB_EPT_ENABL; _cmark = 0; _cend = end; }
// Blocking Send of data to an endpoint int USBD_Send(uint8_t ep, const void* d, int len) { if (!_usbConfiguration) return -1; int r = len; const uint8_t* data = (const uint8_t*)d; uint8_t zero = ep & TRANSFER_ZERO; uint8_t timeout = 250; // 250ms timeout on send? TODO while (len) { uint8_t n = USB_SendSpace(ep); if (n == 0) { if (!(--timeout)) return -1; delay(1); continue; } if (n > len) n = len; len -= n; { SetEP(ep); if (ep & TRANSFER_ZERO) { while (n--) Send8(0); } else if (ep & TRANSFER_PGM) { while (n--) Send8(*data++); } else { while (n--) Send8(*data++); } // if (!ReadWriteAllowed() || ((len == 0) && (ep & TRANSFER_RELEASE))) // Release full buffer // ReleaseTX(); } } TXLED1; // light the TX LED TxLEDPulse = TX_RX_LED_PULSE_MS; return r; }
// Transmit a packet to endpoint void Transfer(u8 ep, const u8* data, int len) { u8 zero = ep & TRANSFER_ZERO; SetEP(ep & 7); while (len--) { while (!ReadWriteAllowed()) ; // TODO Check for STALL etc u8 d = (ep & TRANSFER_PGM) ? pgm_read_byte(data) : data[0]; data++; if (zero) d = 0; Send8(d); if (!ReadWriteAllowed()) ReleaseTX(); } if (ep & TRANSFER_RELEASE) ReleaseTX(); }
LockEP(u8 ep) : _sreg(SREG) { cli(); SetEP(ep & 7); }
// Number of bytes, assumes a rx endpoint uint8_t USBD_Available(uint8_t ep) { SetEP(ep); return FifoByteCount(); }
LockEP(uint8_t ep) : _sreg(SREG) { cli(); SetEP(ep & 7); }
LockEP(u8 ep) : _sreg(SREG) { // POOBAH - see if there's a better way // cli(); SetEP(ep & 7); }
void USB_Flush(u8 ep) { SetEP(ep); if (WriteByteCount())//FifoByteCount()) ReleaseTX(); }
u8 HasData(u8 ep) { SetEP(ep); return ReadWriteAllowed(); // count in fifo }