示例#1
0
/*
 *	_write
 *  Write to a file. 
 * 	file : file identifier (indicate in which channel/peripheral write data)
 * 	ptr : pointer to an array of char (provided by caller) on which take data to write
 *	len : amount of data to write
 */
int _write(int file, char *ptr, int len) 
{
char *loc_ptr=ptr;

	while (loc_ptr-ptr < len) 
	{
		USB_CDC_SendData((u8*)loc_ptr, 1);
		loc_ptr++;
	}	
	
	return len;
}
/*******************************************************************************
* Function Name  : ReceiverFunc
* Description    : Receiver interrupt handler.
* Input          : None
* Output         : None
* Return         : None
*******************************************************************************/
static void ReceiverFunc(void)
{
  uint16_t receive_data;

  /* Read data from UART */
  for (receive_data_length = 0; ((UART_GetFlagStatus(UART, UART_FLAG_RXFE) == RESET) && (receive_data_length < BUFFER_LENGTH)); )
  {
    receive_data = UART_ReceiveData(UART);

    if (UART_Flags(receive_data) == 0)
    {
      ReceiveBuffer[receive_data_length++] = UART_Data(receive_data);
    }
  }
  /* Initiate data portion sending via USB */
  USB_CDC_SendData(ReceiveBuffer, receive_data_length);
}
/* USB_CDC_HANDLE_DATA_SENT implementation - sending of pending data */
USB_Result USB_CDC_DataSent(void)
{
  USB_Result result = USB_SUCCESS;

  if (PendingDataLength)
  {
    result = USB_CDC_SendData(Buffer, PendingDataLength);
#ifdef USB_DEBUG_PROTO
    if (result == USB_SUCCESS)
    {
      SentByteCount += PendingDataLength;
    }
    else
    {
      SkippedByteCount += PendingDataLength;
    }
#endif /* USB_DEBUG_PROTO */
    PendingDataLength = 0;
    USB_CDC_ReceiveStart();
  }
  return USB_SUCCESS;
}
/* USB_CDC_HANDLE_DATA_RECEIVE implementation - data echoing */
USB_Result USB_CDC_RecieveData(uint8_t* Buffer, uint32_t Length)
{
  USB_Result result;

#ifdef USB_DEBUG_PROTO
  ReceivedByteCount += Length;
#endif /* USB_DEBUG_PROTO */

  /* Send back received data portion */
  result = USB_CDC_SendData(Buffer, Length);

#ifdef USB_DEBUG_PROTO
  if (result == USB_SUCCESS)
  {
    SentByteCount += Length;
  }
#ifndef USB_VCOM_SYNC
  else
  {
    SkippedByteCount += Length;
  }
#endif /* !USB_VCOM_SYNC */
#endif /* USB_DEBUG_PROTO */

#ifdef USB_VCOM_SYNC
  if (result != USB_SUCCESS)
  {
    /* If data cannot be sent now, it will await nearest possibility
     * (see USB_CDC_DataSent) */
    PendingDataLength = Length;
  }
  return result;
#else
  return USB_SUCCESS;
#endif /* USB_VCOM_SYNC */
}