int USBLink::receive(uint8_t *data, uint32_t len, uint16_t timeoutMs) { uint32_t time, start, timeout = timeoutMs * CLKFREQ_MS; if (!USB_handleState()) { g_bufUsed = 0; g_recvComplete = 0; return -1; } if (timeout==0) // this is special case... { if (len>GBUF_SIZE || g_bufUsed!=0&&g_bufUsed!=len) return LINK_RESULT_ERROR; if (g_bufUsed==0) { g_bufUsed = len; // register receive buffer USB_Recv(g_buf, g_bufUsed); } else if (g_recvComplete) // if it has come in, then copy { memcpy((void *)data, (void *)g_buf, len); g_bufUsed = 0; return len; } return 0; } else { if (g_bufUsed!=0) // if we have a receive pending, reset USB_RecvReset(); USB_Recv(data, len); } // wait while(1) { start = g_recvTimerStart; // avoid race condition with usb interrupt routine-- sample here time = LPC_TIMER1->TC; // time is guaranteed to be more recent than start if ((uint32_t)(time-start) > timeout || g_recvComplete) break; } if (g_recvComplete) return len; else { USB_RecvReset(); return LINK_RESULT_ERROR_RECV_TIMEOUT; } }
void UsbMidiModule::_poll() { midi_event_t midiEvent; int numReceived; while (numReceived = USB_Recv(getOutEndpointId(), &midiEvent, sizeof(midiEvent))) { // MIDI USB messages are 4 bytes in size. if (numReceived != 4) return; // They get decoded to up to 3 MIDI Serial bytes. u8 data[3]; unsigned count = UsbToMidi::process(midiEvent, data); if (m_midiInFifo.hasSpaceFor(count)) { for (unsigned i=0; i<count; ++i) { m_midiInFifo.push(data[i]); } } } }
// Recv 1 byte if ready int USB_Recv(u8 ep) { u8 c; if (USB_Recv(ep,&c,1) != 1) return -1; return c; }
int USB_Recv(uint8_t ep) { uint8_t c; if (USB_Recv(ep, &c, 1) == 1) return (c); return (-1); }
int Serial_::read(void) { if (peek_buffer >= 0) { int c = peek_buffer; peek_buffer = -1; return c; } return USB_Recv(CDC_RX); }
void CDC::accept() { while (m_ibuf->room()) { int c = USB_Recv(CDC_RX); if (c == IOStream::EOF) break; m_ibuf->putchar(c); } }
int Serial_::peek(void) { if (peek_buffer < 0) { if(USBDevice.configured()) { peek_buffer = USB_Recv(CDC_RX); } } return peek_buffer; }
int Serial_::read(void) { int c; if (_serialPeek != -1) { c = _serialPeek; _serialPeek = -1; } else { c = USB_Recv(CDC_RX); } return c; }
void Serial_::accept(void) { ring_buffer *buffer = &cdc_rx_buffer; int c = USB_Recv(CDC_RX); int i = (unsigned int)(buffer->head+1) % SERIAL_BUFFER_SIZE; // if we should be storing the received character into the location // just before the tail (meaning that the head would advance to the // current location of the tail), we're about to overflow the buffer // and so we don't write the character or advance the head. if (i != buffer->tail) { buffer->buffer[buffer->head] = c; buffer->head = i; } }
int Serial_::read(void) { if (peek_buffer >= 0) { int c = peek_buffer; peek_buffer = -1; return c; } if(USBDevice.configured()) { return USB_Recv(CDC_RX); } return -1; }
void MIDIUSB_::accept(void) { midi_buffer *buffer = &(this->_rx_buffer); int i = (unsigned int)(buffer->head+1) % MIDI_BUFFER_SIZE; // if we should be storing the received character into the location // just before the tail (meaning that the head would advance to the // current location of the tail), we're about to overflow the buffer // and so we don't write the character or advance the head. // while we have room to store a byte while (i != buffer->tail) { int c = USB_Recv(MIDI_RX); if (c == -1) break; // no more data buffer->buffer[buffer->head] = c; buffer->head = i; i = (unsigned int)(buffer->head+1) % MIDI_BUFFER_SIZE; } }
int Serial_::peek(void) { if (peek_buffer < 0) peek_buffer = USB_Recv(CDC_RX); return peek_buffer; }