Ejemplo n.º 1
0
//	Blocking Send of data to an endpoint
int USB_Send(u8 ep, const void* d, int len)
{
	if (!_usbConfiguration)
		return -1;

	int r = len;
	const u8* data = (const u8*)d;
	u8 zero = ep & TRANSFER_ZERO;
	u8 timeout = 250;		// 250ms timeout on send? TODO
	while (len)
	{
		u8 n = USB_SendSpace(ep);
		if (n == 0)
		{
			if (!(--timeout))
				return -1;
			delay(1);
			continue;
		}

		if (n > len)
			n = len;
		len -= n;
		{
			LockEP lock(ep);
			if (ep & TRANSFER_ZERO)
			{
				while (n--)
					Send8(0);
			}
			else if (ep & TRANSFER_PGM)
			{
				while (n--)
					Send8(pgm_read_byte(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;
}
Ejemplo n.º 2
0
Archivo: Core.cpp Proyecto: Dzenik/Cosa
int 
USB_Send(uint8_t ep, const void* d, int len)
{
  if (!_usbConfiguration) return (-1);

  int res = len;
  const uint8_t* data = (const uint8_t*)d;
  uint8_t timeout = 250;
  while (len) {
    uint8_t n = USB_SendSpace(ep);
    if (n == 0) {
      if (!(--timeout)) return (-1);
      delay(1);
      continue;
    }
    if (n > len) n = len;
    {
      LockEP lock(ep);
      if (!ReadWriteAllowed()) continue;
      len -= n;
      if (ep & TRANSFER_ZERO) {
	while (n--) Send8(0);
      }
      else if (ep & TRANSFER_PGM) {
	while (n--) Send8(pgm_read_byte(data++));
      }
      else {
	while (n--) Send8(*data++);
      }
      if (!ReadWriteAllowed() || ((len == 0) && (ep & TRANSFER_RELEASE)))
	ReleaseTX();
    }
  }
  TX_LED_ON;
  TxLEDPulse = TX_RX_LED_PULSE_MS;
  return (res);
}
Ejemplo n.º 3
0
int Serial_::availableForWrite(void)
{
	return USB_SendSpace(CDC_TX);
}
Ejemplo n.º 4
0
//	Blocking Send of data to an endpoint
int USB_Send(u8 ep, const void* d, int len)
{
	if (!_usbConfiguration)
		return -1;

	if (_usbSuspendState & (1<<SUSPI)) {
		//send a remote wakeup
		UDCON |= (1 << RMWKUP);
	}

	int r = len;
	const u8* data = (const u8*)d;
	u8 timeout = 250;		// 250ms timeout on send? TODO
	bool sendZlp = false;

	while (len || sendZlp)
	{
		u8 n = USB_SendSpace(ep);
		if (n == 0)
		{
			if (!(--timeout))
				return -1;
			delay(1);
			continue;
		}

		if (n > len) {
			n = len;
		}

		{
			LockEP lock(ep);
			// Frame may have been released by the SOF interrupt handler
			if (!ReadWriteAllowed())
				continue;

			len -= n;
			if (ep & TRANSFER_ZERO)
			{
				while (n--)
					Send8(0);
			}
			else if (ep & TRANSFER_PGM)
			{
				while (n--)
					Send8(pgm_read_byte(data++));
			}
			else
			{
				while (n--)
					Send8(*data++);
			}

			if (sendZlp) {
				ReleaseTX();
				sendZlp = false;
			} else if (!ReadWriteAllowed()) { // ...release if buffer is full...
				ReleaseTX();
				if (len == 0) sendZlp = true;
			} else if ((len == 0) && (ep & TRANSFER_RELEASE)) { // ...or if forced with TRANSFER_RELEASE
				// XXX: TRANSFER_RELEASE is never used can be removed?
				ReleaseTX();
			}
		}
	}
	TXLED1;					// light the TX LED
	TxLEDPulse = TX_RX_LED_PULSE_MS;
	return r;
}