示例#1
0
文件: kerosin.c 项目: C3MA/r0ket
int puts(const char * str)
{
	// There must be at least 1ms between USB frames (of up to 64 bytes)
	// This buffers all data and writes it out from the buffer one frame
	// and one millisecond at a time
#ifdef CFG_PRINTF_USBCDC
    if (USB_Configuration) 
    {
		while(*str)
			cdcBufferWrite(*str++);
		// Check if we can flush the buffer now or if we need to wait
		unsigned int currentTick = systickGetTicks();
		if (currentTick != lastTick)
		{
			uint8_t frame[64];
			uint32_t bytesRead = 0;
			while (cdcBufferDataPending())
			{
				// Read up to 64 bytes as long as possible
				bytesRead = cdcBufferReadLen(frame, 64);
				USB_WriteEP (CDC_DEP_IN, frame, bytesRead);
				systickDelay(1);
			}
			lastTick = currentTick;
		}
    }
#else
    // Handle output character by character in __putchar
    while(*str) __putchar(*str++);
#endif
	
	return 0;
}
int main(void)
{
  // Configure cpu and mandatory peripherals
  systemInit();

  // Check if projectconfig.h is properly configured for this example
  #if !defined CFG_CHIBI
    #error "CFG_CHIBI must be enabled in projectconfig.h for this example"
  #endif
  #if CFG_CHIBI_PROMISCUOUS == 0
    #error "CFG_CHIBI_PROMISCUOUS must set to 1 in projectconfig.h for this example"
  #endif
  #if defined CFG_INTERFACE
    #error "CFG_INTERFACE must be disabled in projectconfig.h for this example"
  #endif

  #if defined CFG_CHIBI && CFG_CHIBI_PROMISCUOUS != 0
    // Get a reference to the Chibi peripheral control block
    chb_pcb_t *pcb = chb_get_pcb();
    
    // Wait for incoming frames and transmit the raw data over uart
    while(1)
    {
      // Check for incoming messages 
      while (pcb->data_rcv) 
      { 
        // get the length of the data
        rx_data.len = chb_read(&rx_data);
        // make sure the length is nonzero
        if (rx_data.len)
        {
          // Enable LED to indicate message reception 
          gpioSetValue (CFG_LED_PORT, CFG_LED_PIN, CFG_LED_ON); 

          
          // Send raw data the to PC for processing using wsbridge
          uint8_t i;
          for (i=0; i<rx_data.len; i++)
          {
            #ifdef CFG_PRINTF_UART
              uartSendByte(rx_data.data[i]);
            #endif
            #ifdef CFG_PRINTF_USBCDC
               // ToDo: This really needs to be refactored!
              if (USB_Configuration) 
              {
                cdcBufferWrite(rx_data.data[i]);
                // Check if we can flush the buffer now or if we need to wait
                unsigned int currentTick = systickGetTicks();
                if (currentTick != lastTick)
                {
                  uint8_t frame[64];
                  uint32_t bytesRead = 0;
                  while (cdcBufferDataPending())
                  {
                    // Read 64 byte chunks until end of data
                    bytesRead = cdcBufferReadLen(frame, 64);
                    // debug_printf("%d,", bytesRead);
                    USB_WriteEP (CDC_DEP_IN, frame, bytesRead);
                    systickDelay(1);
                  }
                  lastTick = currentTick;
                }
              }  
            #endif
          }

          // Disable LED
          gpioSetValue (CFG_LED_PORT, CFG_LED_PIN, CFG_LED_OFF); 
        }
      }
    }
  #endif

  return 0;
}