예제 #1
0
void UsbSerial_begin( void )
{
  USBD_Disconnect(); // tell the host we're not attached
  CDCDSerialDriver_Initialize();
  USBD_Connect();
  usbSerial.readSemaphore = SemaphoreCreate();
  SemaphoreTake( usbSerial.readSemaphore, -1 );
  usbSerial.writeSemaphore = SemaphoreCreate();
  SemaphoreTake( usbSerial.writeSemaphore, -1 );
}
예제 #2
0
/**
  Read data from a USB host.
  This will read up to 64 bytes of data at a time, as this is the maximum USB transfer
  for the Make Controller internally.  If you want to read more than that, 
  keep calling read until you've got what you need.  
  
  If nothing is ready to be read, this will not return until new data arrives.
  @param buffer Where to store the incoming data.
  @param length How many bytes to read. 64 is the max that can be read at one time.
  @param timeout The number of milliseconds to wait if no data is available.  -1 means wait forever.
  @return The number of bytes successfully read.
  
  \b Example
  \code
  char mydata[128];
  // simplest is reading a short chunk
  int read = UsbSerial_read(mydata, 20);
  
  // or, we can wait until we've read more than the maximum of 64 bytes
  int got_so_far = 0;
  while(got_so_far < 128) // wait until we've read 128 bytes
  {
    int read = UsbSerial_read(mydata, (128 - got_so_far)); // read some new data
    got_so_far += read; // add to how much we've gotten so far
  }
  \endcode
*/
int UsbSerial_read( char *buffer, int length, int timeout )
{
  if( USBD_GetState() != USBD_STATE_CONFIGURED )
    return 0;
  int length_to_go = length;
  if( usbSerial.rxBufCount ) { // do we already have some lying around?
    int copylen = MIN(usbSerial.rxBufCount, length_to_go);
    memcpy( buffer, usbSerial.rxBuf, copylen );
    buffer += copylen;
    usbSerial.rxBufCount -= copylen;
    length_to_go -= copylen;
  }
  if(length_to_go) { // if we still would like to get more
    unsigned char result = USBD_Read(CDCDSerialDriverDescriptors_DATAOUT,
                                  usbSerial.rxBuf, USBSER_MAX_READ, UsbSerial_onUsbData, 0);
    if(result == USBD_STATUS_SUCCESS) {
      if( SemaphoreTake( usbSerial.readSemaphore, timeout ) ) {
        int copylen = MIN(usbSerial.justGot, length_to_go);
        memcpy( buffer, usbSerial.rxBuf, copylen );
        buffer += copylen;
        usbSerial.rxBufCount -= copylen;
        length_to_go -= copylen;
        usbSerial.justGot = 0;
      }
    }
  }
  return length - length_to_go;
}
예제 #3
0
파일: display.c 프로젝트: ytai/PixieGuitar
static void WaitTransferComplete() {
  _DMA3IE = 1;
  while (!_DMA3IF) {
    Error e = SemaphoreTake(event);
    (void) e;
  }
  _DMA3IF = 0;
}
예제 #4
0
/**
  Write data to a USB host.
  @param buffer The data to send.
  @param length How many bytes to send.
  @return The number of bytes successfully written.
  
  \b Example
  \code
  int written = UsbSerial_write( "hi hi", 5 );
  \endcode
*/
int UsbSerial_write( const char *buffer, int length, int timeout )
{
  int rv = 0;
  if( USBD_GetState() == USBD_STATE_CONFIGURED ) {
    if( USBD_Write(CDCDSerialDriverDescriptors_DATAIN, 
          buffer, length, UsbSerial_onUsbWritten, 0) == USBD_STATUS_SUCCESS ) {
      if( SemaphoreTake( usbSerial.writeSemaphore, timeout ) ) {
        rv = usbSerial.justWrote;
        usbSerial.justWrote = 0;
      }
    }
  }
  return rv;
}