示例#1
0
void print_digit_hex(uint8_t value)
{
    if (value > 9) {
        CDC_Device_SendByte(&CDC_interface, value - 10 + 'A');
    } else {
        CDC_Device_SendByte(&CDC_interface, value + '0');
    }
}
示例#2
0
void print_hex(uint16_t value)
{
    CDC_Device_SendByte(&CDC_interface, '0');
    CDC_Device_SendByte(&CDC_interface, 'x');
    print_digit_hex((value & 0xF000) >> 12);
    print_digit_hex((value & 0x0F00) >>  8);
    print_digit_hex((value & 0x00F0) >>  4);
    print_digit_hex((value & 0x000F) >>  0);
}
示例#3
0
void printPacket(MRF_packet_t *rx_packet)
{
    // Print a label for the packet type
    switch (rx_packet->type) {
        case PACKET_TYPE_SERIAL:
            sendStringP(typeSerialString);
            break;
        case PACKET_TYPE_SERIAL_ECC:
            sendStringP(typeSerialECCString);
            break;
        case PACKET_TYPE_PACKET:
            sendStringP(typePacketString);
            break;
        case PACKET_TYPE_PACKET_ECC:
            sendStringP(typePacketECCString);
            break;
        default:
            sendStringP(typeUnknownString);
            break;
    }
    
    // Print the packet length
    sendStringP(packetLengthString);
    print_dec(rx_packet->payloadSize);
    CDC_Device_SendByte(&CDC_interface, ':');
    CDC_Device_SendByte(&CDC_interface, ' ');
    
    // Print the packet contents
    for (int i = 0; i < rx_packet->payloadSize; i++) {
        if (i % 10 == 0) {
            CDC_Device_SendByte(&CDC_interface, '\n');
            CDC_Device_SendByte(&CDC_interface, '\r');
        }
        
        uint8_t byte = rx_packet->payload[i];
        CDC_Device_SendByte(&CDC_interface, byte);
//        if (byte & 0xF0 > 0x90) {
//            CDC_Device_SendByte(&CDC_interface, ((byte & 0xF0) >> 4) + 'A');
//        } else {
//            CDC_Device_SendByte(&CDC_interface, ((byte & 0xF0) >> 4) + '0');
//        }
//        
//        if (byte & 0x0F > 0x09) {
//            CDC_Device_SendByte(&CDC_interface, (byte & 0x0F) + 'A');
//        } else {
//            CDC_Device_SendByte(&CDC_interface, (byte & 0x0F) + '0');
//        }
    }
    
    CDC_Device_Flush(&CDC_interface);
}
/** Main program entry point. This routine contains the overall program flow, including initial
 *  setup of all components and the main program loop.
 */
int main(void)
{
	SetupHardware();

	RingBuffer_InitBuffer(&USBtoUSART_Buffer, USBtoUSART_Buffer_Data, sizeof(USBtoUSART_Buffer_Data));
	RingBuffer_InitBuffer(&USARTtoUSB_Buffer, USARTtoUSB_Buffer_Data, sizeof(USARTtoUSB_Buffer_Data));

	LEDs_SetAllLEDs(LEDMASK_USB_NOTREADY);
	GlobalInterruptEnable();

	for (;;)
	{
		/* Only try to read in bytes from the CDC interface if the transmit buffer is not full */
		if (!(RingBuffer_IsFull(&USBtoUSART_Buffer)))
		{
			int16_t ReceivedByte = CDC_Device_ReceiveByte(&VirtualSerial_CDC_Interface);

			/* Read bytes from the USB OUT endpoint into the USART transmit buffer */
			if (!(ReceivedByte < 0))
			  RingBuffer_Insert(&USBtoUSART_Buffer, ReceivedByte);
		}

		/* Check if the UART receive buffer flush timer has expired or the buffer is nearly full */
		uint16_t BufferCount = RingBuffer_GetCount(&USARTtoUSB_Buffer);
		if (BufferCount)
		{
			Endpoint_SelectEndpoint(VirtualSerial_CDC_Interface.Config.DataINEndpoint.Address);

			/* Check if a packet is already enqueued to the host - if so, we shouldn't try to send more data
			 * until it completes as there is a chance nothing is listening and a lengthy timeout could occur */
			if (Endpoint_IsINReady())
			{
				/* Never send more than one bank size less one byte to the host at a time, so that we don't block
				 * while a Zero Length Packet (ZLP) to terminate the transfer is sent if the host isn't listening */
				uint8_t BytesToSend = MIN(BufferCount, (CDC_TXRX_EPSIZE - 1));

				/* Read bytes from the USART receive buffer into the USB IN endpoint */
				while (BytesToSend--)
				{
					/* Try to send the next byte of data to the host, abort if there is an error without dequeuing */
					if (CDC_Device_SendByte(&VirtualSerial_CDC_Interface,
											RingBuffer_Peek(&USARTtoUSB_Buffer)) != ENDPOINT_READYWAIT_NoError)
					{
						break;
					}

					/* Dequeue the already sent byte from the buffer now we have confirmed that no transmission error occurred */
					RingBuffer_Remove(&USARTtoUSB_Buffer);
				}
			}
		}

		/* Load the next byte from the USART transmit buffer into the USART */
		if (!(RingBuffer_IsEmpty(&USBtoUSART_Buffer)))
		  Serial_SendByte(RingBuffer_Remove(&USBtoUSART_Buffer));

		CDC_Device_USBTask(&VirtualSerial_CDC_Interface);
		USB_USBTask();
	}
}
示例#5
0
int
main(void) {
  uint8_t i = 1;
  uint8_t count = 3;

  setup();

  blink(3);
  _delay_ms(100);

  while (1) {
    // Print greeting at most count times.
    if (i <= count) {
      if (USB_DeviceState == DEVICE_STATE_Configured && ok_to_send) {
        blink(i);
        CDC_Device_SendString(&VirtualSerial_CDC_Interface, "Hello World! ");
        CDC_Device_SendByte(&VirtualSerial_CDC_Interface, '0' + i);
        CDC_Device_SendString(&VirtualSerial_CDC_Interface, "\r\n");
        CDC_Device_Flush(&VirtualSerial_CDC_Interface);

        i++;
      }
    }

    if (USB_DeviceState == DEVICE_STATE_Configured) {
      /* Must throw away unused bytes from the host, or it will lock up
         while waiting for the device */
      CDC_Device_ReceiveByte(&VirtualSerial_CDC_Interface);
    }
    CDC_Device_USBTask(&VirtualSerial_CDC_Interface);
    USB_USBTask();
  }
}
void HandleSerial(void)
{
	// Only try to read in bytes from the CDC interface if the transmit buffer is not full 
	if (!(RingBuffer_IsFull(&FromHost_Buffer)))
	{
		int16_t ReceivedByte = CDC_Device_ReceiveByte(&VirtualSerial_CDC_Interface);
		// Read bytes from the USB OUT endpoint into the USART transmit buffer 
		if (!(ReceivedByte < 0)){
		  	RingBuffer_Insert(&FromHost_Buffer, ReceivedByte);
			CDC_Device_SendByte(&VirtualSerial_CDC_Interface, ReceivedByte);
		}
	}

	while (RingBuffer_GetCount(&FromHost_Buffer) > 0)
	{
		int16_t c = RingBuffer_Remove(&FromHost_Buffer);

		if(c == '\n' || c == '\r'){
			if(cmd_cnt > 0 && (cmd[cmd_cnt-1] == '\n' || cmd[cmd_cnt-1] == '\r'))
				cmd_cnt--;
			cmd[cmd_cnt] = 0;			
			execute_command();
			cmd_cnt = 0;			
		}
		else{
			cmd[cmd_cnt++] = c;			
		}
	}
}
示例#7
0
uint16_t print_digit(uint16_t value, uint16_t place)
{
    uint16_t remainder;
    uint16_t quotient = stupid_divide(value, place, &remainder);
    CDC_Device_SendByte(&CDC_interface, '0' + quotient);
    return remainder;
}
void CDC_Send(uint8_t * Buffer){
	uint8_t i=0;

	while (Buffer[i]!=0){
		CDC_Device_SendByte(&VirtualSerial_CDC_Interface, Buffer[i]);
		i++;
	}
}
示例#9
0
/** Main program entry point. This routine contains the overall program flow, including initial
 *  setup of all components and the main program loop.
 */
int main(void)
{
	SetupHardware();

	GlobalInterruptEnable();

	uint8_t sending = 0;

	for (;;) {

		while (1) {
                	int16_t ReceivedByte = CDC_Device_ReceiveByte(&VirtualSerial_CDC_Interface);
			if (ReceivedByte < 0) 
				break;

			if (!configured) continue;

			if (!sending) {
				PORTC.OUTSET = PIN1_bm;
				sending = 1;
			}

			PORTD.OUTTGL = PIN5_bm;
                	while(!USART_IsTXDataRegisterEmpty(&USART));
                	USART_PutChar(&USART, ReceivedByte & 0xff);
		}

		if (sending) {
			USART_ClearTXComplete(&USART);
               		while(!USART_IsTXComplete(&USART));
			PORTC.OUTCLR = PIN1_bm;
			sending = 0;	
		}

                Endpoint_SelectEndpoint(VirtualSerial_CDC_Interface.Config.DataINEndpoint.Address);

                /* Check if a packet is already enqueued to the host - if so, we shouldn't try to send more data
                 * until it completes as there is a chance nothing is listening and a lengthy timeout could occur */

		if (configured && Endpoint_IsINReady()) {
			uint8_t maxbytes = CDC_TXRX_EPSIZE;
                	while (USART_RXBufferData_Available(&USART_data) && maxbytes-->0) {
                        	uint8_t b = USART_RXBuffer_GetByte(&USART_data);
				CDC_Device_SendByte(&VirtualSerial_CDC_Interface, b);
				PORTD.OUTTGL = PIN5_bm;
                	}
                }

		CDC_Device_USBTask(&VirtualSerial_CDC_Interface);
		USB_USBTask();

		if (loop++) continue;
		if (!configured) continue;

		PORTD.OUTTGL = PIN5_bm;
	}
}
示例#10
0
static int CDC_Device_putchar(char c,
                              FILE* Stream)
{
	if(c == 0x0A)
	{
		CDC_Device_putchar(0x0D, Stream);
	}
	return CDC_Device_SendByte((USB_ClassInfo_CDC_Device_t*)fdev_get_udata(Stream), c) ? _FDEV_ERR : 0;
}
示例#11
0
/** Main program entry point. This routine contains the overall program flow, including initial
 *  setup of all components and the main program loop.
 */
int main(void)
{
	SetupHardware();

	RingBuffer_InitBuffer(&USBtoUSART_Buffer);
	RingBuffer_InitBuffer(&USARTtoUSB_Buffer);

	sei();

	for (;;)
	{
		/* Only try to read in bytes from the CDC interface if the transmit buffer is not full */
		if (!(RingBuffer_IsFull(&USBtoUSART_Buffer)))
		{
			int16_t ReceivedByte = CDC_Device_ReceiveByte(&VirtualSerial_CDC_Interface);

			/* Read bytes from the USB OUT endpoint into the USART transmit buffer */
			if (!(ReceivedByte < 0))
			  RingBuffer_Insert(&USBtoUSART_Buffer, ReceivedByte);
		}

		/* Check if the UART receive buffer flush timer has expired or the buffer is nearly full */
		RingBuff_Count_t BufferCount = RingBuffer_GetCount(&USARTtoUSB_Buffer);
		if ((TIFR0 & (1 << TOV0)) || (BufferCount > BUFFER_NEARLY_FULL))
		{
			TIFR0 |= (1 << TOV0);

			if (USARTtoUSB_Buffer.Count) {
				LEDs_TurnOnLEDs(LEDMASK_TX);
				PulseMSRemaining.TxLEDPulse = TX_RX_LED_PULSE_MS;
			}

			/* Read bytes from the USART receive buffer into the USB IN endpoint */
			while (BufferCount--)
			  CDC_Device_SendByte(&VirtualSerial_CDC_Interface, RingBuffer_Remove(&USARTtoUSB_Buffer));

			/* Turn off TX LED(s) once the TX pulse period has elapsed */
			if (PulseMSRemaining.TxLEDPulse && !(--PulseMSRemaining.TxLEDPulse))
			  LEDs_TurnOffLEDs(LEDMASK_TX);

			/* Turn off RX LED(s) once the RX pulse period has elapsed */
			if (PulseMSRemaining.RxLEDPulse && !(--PulseMSRemaining.RxLEDPulse))
			  LEDs_TurnOffLEDs(LEDMASK_RX);
		}

		/* Load the next byte from the USART transmit buffer into the USART */
		if (!(RingBuffer_IsEmpty(&USBtoUSART_Buffer))) {
		  Serial_TxByte(RingBuffer_Remove(&USBtoUSART_Buffer));

		  	LEDs_TurnOnLEDs(LEDMASK_RX);
			PulseMSRemaining.RxLEDPulse = TX_RX_LED_PULSE_MS;
		}

		CDC_Device_USBTask(&VirtualSerial_CDC_Interface);
		USB_USBTask();
	}
}
示例#12
0
int main(void)
{
	SetupHardware();

	RingBuffer_InitBuffer(&USBtoUSART_Buffer, USBtoUSART_Buffer_Data, sizeof(USBtoUSART_Buffer_Data));
	RingBuffer_InitBuffer(&USARTtoUSB_Buffer, USARTtoUSB_Buffer_Data, sizeof(USARTtoUSB_Buffer_Data));

	LEDs_SetAllLEDs(0);

	sei();

	for (;;)
	{
		if ((PINF & 0x01) == 0x01)
			LEDs_SetAllLEDs(2);
		else
			LEDs_SetAllLEDs(0);
		/* Only try to read in bytes from the CDC interface if the (outbound) transmit buffer is not full */
		if (!(RingBuffer_IsFull(&USBtoUSART_Buffer)))
		{
			int16_t ReceivedByte = CDC_Device_ReceiveByte(&VirtualSerial_CDC_Interface);

			/* Read bytes from the USB OUT endpoint into the USART transmit buffer */
			if (!(ReceivedByte < 0))
			  RingBuffer_Insert(&USBtoUSART_Buffer, ReceivedByte);
		}

		/* Check if the UART receive buffer flush timer has expired or the buffer is nearly full */
		uint16_t BufferCount = RingBuffer_GetCount(&USARTtoUSB_Buffer);
		if ((TIFR0 & (1 << TOV0)) || (BufferCount > (uint8_t)(sizeof(USARTtoUSB_Buffer_Data) * .75)))
		{
			/* Clear flush timer expiry flag */
			TIFR0 |= (1 << TOV0);

			/* Read bytes from the USART receive buffer into the USB IN endpoint */
			while (BufferCount--)
			{
				/* Try to send the next byte of data to the host, abort if there is an error without dequeuing */
				if (CDC_Device_SendByte(&VirtualSerial_CDC_Interface,
				                        RingBuffer_Peek(&USARTtoUSB_Buffer)) != ENDPOINT_READYWAIT_NoError)
				{
					break;
				}

				/* Dequeue the already sent byte from the buffer now we have confirmed that no transmission error occurred */
				RingBuffer_Remove(&USARTtoUSB_Buffer);
			}
		}

		/* Load the next byte from the USART transmit buffer into the USART */
		if (!(RingBuffer_IsEmpty(&USBtoUSART_Buffer)))
		  Serial_SendByte(RingBuffer_Remove(&USBtoUSART_Buffer));

		CDC_Device_USBTask(&VirtualSerial_CDC_Interface);
		USB_USBTask();
	}
}
示例#13
0
/******************************************************************************
* Nombre de la funcion	: int8_t serial_write(uint8_t data)
*    returns int8_t     : Si se envio un byte exitosamente, retorna 0. Si hubo
*                         un error, retorna -1
*    data               : dato a enviar
* Creada por		: Alejandro Weinstein
* Fecha Creacion	: 2013-04-08
*
* Descripcion : Envia un byte por el puerto serial
******************************************************************************/
int8_t serial_write(uint8_t data)
{
  uint8_t err_code;
  err_code = CDC_Device_SendByte(&VirtualSerial_CDC_Interface, data);
  if ( err_code == ENDPOINT_READYWAIT_NoError) {
    return 0;
  } else {
    return 1;
  }
}
uint16_t VncServerSendResponse(uint8_t * buffer, uint16_t length)
{
    int i;
    for(i=0; i<length; i++)
    {
        if (CDC_Device_SendByte(&VirtualSerial_CDC_Interface,
                buffer[i]) != ENDPOINT_READYWAIT_NoError)
        {
            break;
        }
    }

    return i;
}
示例#15
0
文件: ISP.c 项目: NicoHood/Hoodloader
void sendCDCbyte(uint8_t b){
	//TODO improve this
	// try to send until sucess
	while (CDC_Device_SendByte(&VirtualSerial_CDC_Interface, b) != ENDPOINT_READYWAIT_NoError){
		CDC_Device_USBTask(&VirtualSerial_CDC_Interface);
		USB_USBTask();
		while (1){
			// TODO remove this freezing loop!
			LEDs_TurnOnLEDs(LEDS_ERR);
			_delay_ms(100);
			LEDs_TurnOnLEDs(LEDS_ERR);
			_delay_ms(100);
		}
	}
}
示例#16
0
/* This function sends a byte to the USB host. */
status_t USBVC001_SendByte(const uint8_t DataByte)
{
  status_t Status = (uint32_t)DAVEApp_SUCCESS;

  /* Send a byte to the host. */
  if(CDC_Device_SendByte(&USBVC001_CDCInterface, DataByte)
      != ENDPOINT_RWSTREAM_NoError)
  {
    Status = USBVC001_USBCDC001_ERROR;
  }
  if(CDC_Device_Flush(&USBVC001_CDCInterface) != ENDPOINT_READYWAIT_NoError)
  {
    Status = USBVC001_USBCDC001_ERROR;
  }

  return Status;
}
示例#17
0
/* Main thread */
void usb_thread (void)
{
    for (;;)
    {
        bcond_wait(&usb_active);
        t_yield();
        int16_t c;
        while ((c = CDC_Device_ReceiveByte(&VirtualSerial_CDC_Interface)) > 0)
            stream_put(&cdc_instr, c);

        t_yield();
        while ( ! stream_empty(&cdc_outstr))
            CDC_Device_SendByte(&VirtualSerial_CDC_Interface, stream_get(&cdc_outstr));

        CDC_Device_USBTask(&VirtualSerial_CDC_Interface);
        USB_USBTask();
    }
}
示例#18
0
void Jennic_In_Task()
{
  size_t i;

  Endpoint_SelectEndpoint(VirtualSerial_CDC1_Interface.Config.DataINEndpoint.Address);
  if ( !Endpoint_IsINReady() )
    return;

  /* Read bytes from the USART receive buffer and send with CDC */
  /* pass through in programming mode */
  if (ringbuf_elements(&USARTtoUSB_Buffer) && jennic_in_programming_mode)
  {
    uint8_t byte = ringbuf_get(&USARTtoUSB_Buffer);

      if (CDC_Device_SendByte(&VirtualSerial_CDC1_Interface, byte) != ENDPOINT_READYWAIT_NoError)
        return;
  }
}
示例#19
0
文件: USBtoSerial.c 项目: ldarlok/LCS
void sendUSARTtoUSB ()
{
	uint16_t BufferCount = RingBuffer_GetCount(&USARTtoUSB_Buffer);
	if (BufferCount)
	{
		Endpoint_SelectEndpoint(VirtualSerial_CDC_Interface.Config.DataINEndpoint.Address);
		if (Endpoint_IsINReady())
		{
			uint8_t BytesToSend = MIN(BufferCount, (CDC_TXRX_EPSIZE - 1));
			while (BytesToSend--)
			{
				if (CDC_Device_SendByte(&VirtualSerial_CDC_Interface, RingBuffer_Peek(&USARTtoUSB_Buffer)) != ENDPOINT_READYWAIT_NoError)
				{
					break;
				}
				RingBuffer_Remove(&USARTtoUSB_Buffer);
			}
		}
	}
}
示例#20
0
void UARTBridge_Task(void)
{
	/* Must be in the configured state for the USART Bridge code to process data */
	if (USB_DeviceState != DEVICE_STATE_Configured)
	  return;

	/* Only try to read in bytes from the CDC interface if the transmit buffer is not full */
	if (!(RingBuffer_IsFull(&USBtoUART_Buffer)))
	{
		int16_t ReceivedByte = CDC_Device_ReceiveByte(&VirtualSerial_CDC_Interface);

		/* Read bytes from the USB OUT endpoint into the UART transmit buffer */
		if (!(ReceivedByte < 0))
		  RingBuffer_Insert(&USBtoUART_Buffer, ReceivedByte);
	}

	/* Check if the UART receive buffer flush timer has expired or buffer is nearly full */
	uint16_t BufferCount = RingBuffer_GetCount(&UARTtoUSB_Buffer);
	if ((TIFR0 & (1 << TOV0)) || (BufferCount > 200))
	{
		/* Clear flush timer expiry flag */
		TIFR0 |= (1 << TOV0);

		/* Read bytes from the USART receive buffer into the USB IN endpoint */
		while (BufferCount--)
		{
			/* Try to send the next byte of data to the host, abort if there is an error without dequeuing */
			if (CDC_Device_SendByte(&VirtualSerial_CDC_Interface,
									RingBuffer_Peek(&UARTtoUSB_Buffer)) != ENDPOINT_READYWAIT_NoError)
			{
				break;
			}

			/* Dequeue the already sent byte from the buffer now we have confirmed that no transmission error occurred */
			RingBuffer_Remove(&UARTtoUSB_Buffer);
		}
	}

	CDC_Device_USBTask(&VirtualSerial_CDC_Interface);
}
示例#21
0
/** Main program entry point. This routine contains the overall program flow, including initial
 *  setup of all components and the main program loop.
 */
int main(void)
{
	SetupHardware();
	
	LEDs_SetAllLEDs(LEDMASK_USB_NOTREADY);

	for (;;)
	{
		CheckJoystickMovement();

		/* Discard all received data on the first CDC interface */
		while (CDC_Device_BytesReceived(&VirtualSerial1_CDC_Interface))
		  CDC_Device_ReceiveByte(&VirtualSerial1_CDC_Interface);

		/* Echo all received data on the second CDC interface */
		while (CDC_Device_BytesReceived(&VirtualSerial2_CDC_Interface))
		  CDC_Device_SendByte(&VirtualSerial2_CDC_Interface, CDC_Device_ReceiveByte(&VirtualSerial2_CDC_Interface));
		  
		CDC_Device_USBTask(&VirtualSerial1_CDC_Interface);
		CDC_Device_USBTask(&VirtualSerial2_CDC_Interface);
		USB_USBTask();
	}
}
示例#22
0
void UARTBridge_Task(void)
{
	/* Must be in the configured state for the USART Bridge code to process data */
	if (USB_DeviceState != DEVICE_STATE_Configured)
	  return;

	/* Read bytes from the USB OUT endpoint into the UART transmit buffer */
	int16_t ReceivedByte = CDC_Device_ReceiveByte(&VirtualSerial_CDC_Interface);
	if (!(ReceivedByte < 0) && !(RingBuffer_IsFull(&USBtoUART_Buffer)))
	  RingBuffer_Insert(&USBtoUART_Buffer, ReceivedByte);
	
	/* Check if the UART receive buffer flush timer has expired or buffer is nearly full */
	RingBuff_Count_t BufferCount = RingBuffer_GetCount(&UARTtoUSB_Buffer);
	if ((TIFR0 & (1 << TOV0)) || (BufferCount > 200))
	{
		TIFR0 |= (1 << TOV0);

		/* Read bytes from the UART receive buffer into the USB IN endpoint */
		while (BufferCount--)
		  CDC_Device_SendByte(&VirtualSerial_CDC_Interface, RingBuffer_Remove(&UARTtoUSB_Buffer));
	}

	CDC_Device_USBTask(&VirtualSerial_CDC_Interface);
}
示例#23
0
/** Main program entry point. This routine contains the overall program flow, including initial
 *  setup of all components and the main program loop.
 */
int main(void)
{
	SetupHardware();

	LEDs_SetAllLEDs(LEDMASK_USB_NOTREADY);
	GlobalInterruptEnable();

	for (;;)
	{
		CheckJoystickMovement();

		/* Discard all received data on the first CDC interface */
		CDC_Device_ReceiveByte(&VirtualSerial1_CDC_Interface);

		/* Echo all received data on the second CDC interface */
		int16_t ReceivedByte = CDC_Device_ReceiveByte(&VirtualSerial2_CDC_Interface);
		if (!(ReceivedByte < 0))
		  CDC_Device_SendByte(&VirtualSerial2_CDC_Interface, (uint8_t)ReceivedByte);

		CDC_Device_USBTask(&VirtualSerial1_CDC_Interface);
		CDC_Device_USBTask(&VirtualSerial2_CDC_Interface);
		USB_USBTask();
	}
}
示例#24
0
void sendByte(char c){
	CDC_Device_SendByte(&VirtualSerial_CDC_Interface, (uint8_t)c);
}
/** Main program entry point. This routine contains the overall program flow, including initial
 *  setup of all components and the main program loop.
 */
int main(void)
{
	SetupHardware();

	RingBuffer_InitBuffer(&USARTtoUSB_Buffer, USARTtoUSB_Buffer_Data, sizeof(USARTtoUSB_Buffer_Data));

	sei();

	for (;;)
	{
		/* Echo bytes from the host to the target via the hardware USART */
		if ((UCSR1A & (1 << UDRE1)) && CDC_Device_BytesReceived(&VirtualSerial_CDC_Interface))
		{
			UDR1 = CDC_Device_ReceiveByte(&VirtualSerial_CDC_Interface);

			LEDs_TurnOnLEDs(LEDMASK_TX);
			PulseMSRemaining.TxLEDPulse = TX_RX_LED_PULSE_MS;
		}

		/* Check if the millisecond timer has elapsed */
		if (TIFR0 & (1 << OCF0A))
		{
			/* Clear flush timer expiry flag */
			TIFR0 |= (1 << TOV0);

			/* Check if the reset pulse period has elapsed, if so tristate the target reset line */
			if (PulseMSRemaining.ResetPulse && !(--PulseMSRemaining.ResetPulse))
			{
				LEDs_TurnOffLEDs(LEDMASK_BUSY);
				AVR_RESET_LINE_DDR &= ~AVR_RESET_LINE_MASK;
			}

			/* Check if the LEDs should be ping-ponging (during enumeration) */
			if (PulseMSRemaining.PingPongLEDPulse && !(--PulseMSRemaining.PingPongLEDPulse))
			{
				LEDs_ToggleLEDs(LEDMASK_TX | LEDMASK_RX);
				PulseMSRemaining.PingPongLEDPulse = PING_PONG_LED_PULSE_MS;
			}

			/* Turn off TX LED(s) once the TX pulse period has elapsed */
			if (PulseMSRemaining.TxLEDPulse && !(--PulseMSRemaining.TxLEDPulse))
			  LEDs_TurnOffLEDs(LEDMASK_TX);

			/* Turn off RX LED(s) once the RX pulse period has elapsed */
			if (PulseMSRemaining.RxLEDPulse && !(--PulseMSRemaining.RxLEDPulse))
			  LEDs_TurnOffLEDs(LEDMASK_RX);

			/* Check if the receive buffer flush period has expired */
			uint16_t BufferCount = RingBuffer_GetCount(&USARTtoUSB_Buffer);
			if (!(--FlushPeriodRemaining) || (BufferCount > 200))
			{
				FlushPeriodRemaining = RECEIVE_BUFFER_FLUSH_MS;

				/* Start RX LED indicator pulse */
				if (BufferCount)
				{
					LEDs_TurnOnLEDs(LEDMASK_RX);
					PulseMSRemaining.RxLEDPulse = TX_RX_LED_PULSE_MS;
				}

				/* Echo bytes from the target to the host via the virtual serial port */
				while (BufferCount--)
				{
					/* Try to send the next byte of data to the host, abort if there is an error without dequeuing */
					if (CDC_Device_SendByte(&VirtualSerial_CDC_Interface,
											RingBuffer_Peek(&USARTtoUSB_Buffer)) != ENDPOINT_READYWAIT_NoError)
					{
						break;
					}
					
					/* Dequeue the already sent byte from the buffer now we have confirmed that no transmission error occurred */
					RingBuffer_Remove(&USARTtoUSB_Buffer);
				}
			}
		}

		CDC_Device_USBTask(&VirtualSerial_CDC_Interface);
		USB_USBTask();
	}
}
示例#26
0
/** Main program entry point. This routine contains the overall program flow, including initial
 *  setup of all components and the main program loop.
 */
int main(void)
{
	SetupHardware();
	
	RingBuffer_InitBuffer(&USBtoUSART_Buffer);
	RingBuffer_InitBuffer(&USARTtoUSB_Buffer);

    setupEeprom();

	sei();


	for (;;)
	{

		// Only try to read in bytes from the CDC interface if the transmit buffer is not full
		if (!(RingBuffer_IsFull(&USBtoUSART_Buffer)))
		{
			int16_t ReceivedByte = CDC_Device_ReceiveByte(&VirtualSerial_CDC_Interface);

			// Read bytes from the USB OUT endpoint into the USART transmit buffer
			if (!(ReceivedByte < 0))
			  RingBuffer_Insert(&USBtoUSART_Buffer, ReceivedByte);
		}
		
		// Check if the UART receive buffer flush timer has expired or the buffer is nearly full
		RingBuff_Count_t BufferCount = RingBuffer_GetCount(&USARTtoUSB_Buffer);
		if ((TIFR0 & (1 << TOV0)) || (BufferCount > BUFFER_NEARLY_FULL))
		{
			TIFR0 |= (1 << TOV0);

			if (USARTtoUSB_Buffer.Count) {
				LEDs_TurnOnLEDs(LEDMASK_TX);
				PulseMSRemaining.TxLEDPulse = TX_RX_LED_PULSE_MS;
			}

			// Read bytes from the USART receive buffer into the USB IN endpoint
			while (BufferCount--)
			  CDC_Device_SendByte(&VirtualSerial_CDC_Interface, RingBuffer_Remove(&USARTtoUSB_Buffer));
			  
			// Turn off TX LED(s) once the TX pulse period has elapsed
			if (PulseMSRemaining.TxLEDPulse && !(--PulseMSRemaining.TxLEDPulse))
			  LEDs_TurnOffLEDs(LEDMASK_TX);

			// Turn off RX LED(s) once the RX pulse period has elapsed
			if (PulseMSRemaining.RxLEDPulse && !(--PulseMSRemaining.RxLEDPulse))
			  LEDs_TurnOffLEDs(LEDMASK_RX);

			if (ResetTimer > 0)
			{
				// SAM3X RESET/ERASE Sequence
				// --------------------------
				if (ResetTimer == 95) {
                    eepromInterruptDisable();
					setErasePin(true);
					setResetPin(false);
				}
				if (ResetTimer == 35) {
                    eepromInterruptDisable();
					setErasePin(false);
					setResetPin(false);
				}
				if (ResetTimer == 25) {
                    eepromInterruptDisable();
					setErasePin(false);
					setResetPin(true);
				}
				if (ResetTimer == 1) {
                    eepromInterruptDisable();
					setErasePin(false);
					setResetPin(false);
				}
				ResetTimer--;
			} else {
				setErasePin(false);
				setResetPin(false);
                eepromInterruptEnable();
			}
		}
		
		// Load the next byte from the USART transmit buffer into the USART
		if (!(RingBuffer_IsEmpty(&USBtoUSART_Buffer))) {
			Serial_TxByte(RingBuffer_Remove(&USBtoUSART_Buffer));
			LEDs_TurnOnLEDs(LEDMASK_RX);
			PulseMSRemaining.RxLEDPulse = TX_RX_LED_PULSE_MS;
		}

        // CDC_Device_USBTask(&VirtualSerial_CDC_Interface);
        // USB_USBTask();
        serviceEepromRequest();
		CDC_Device_USBTask(&VirtualSerial_CDC_Interface);
		USB_USBTask();
	}

}
void putchar_printf(char var, FILE *stream)
{
	CDC_Device_SendByte(&VirtualSerial_CDC_Interface, var);
}
示例#28
0
int8_t read_hex_value(uint8_t byte, uint16_t *result)
{
    // Keep accepting characters until a carriage return
    // or we've run out of room in the buffer.
    
    // If we're out of space, send a 'bell'
    if (textBufferIndex == 5) {
        CDC_Device_SendByte(&CDC_interface, 0x07);
        return 0;
    }
    
    // If we received a ctrl-c, exit
    if (byte == 0x03) {
        return -1;
    }
    
    // Capture whichever the first newline character
    // is first This indicates the end of the user input
    if (byte == '\n' || byte == '\r') {
        *result = 0;
        
        // Process each digit
        for (int i = 0; i < textBufferIndex; i++) {
            // Shift-over existing value
            *result = *result << 4;

            // Is this A-F?
            if (textBuffer[i] >= 'A' && textBuffer[i] <= 'F') {
                *result += textBuffer[i] - 'A' + 10;
            
            }
            
            // Is this 0-9?
            else if (textBuffer[i] >= '0' && textBuffer[i] <= '9') {
                *result += textBuffer[i] - '0';
            }
        }
        
        // Reset the index
        textBufferIndex = 0;
        
        return 1;
    }
    
    // Handle backspaces appropriately
    if (byte == 0x08) {
        // Make sure we don't backspace too far
        if (textBufferIndex == 0) {
            CDC_Device_SendByte(&CDC_interface, 0x07);
            return 0;
        }
        
        sendStringP(backSpaceString);
        textBufferIndex--;
        textBuffer[textBufferIndex] = '\0';
        return 0;
    }
    
    // Make every letter upper-case
    if (byte >= 'a' && byte <= 'z') {
        byte = byte - 32; // Crazy to-upper
    }
    
    // Filter out non-hex-digit input
    if ( (byte < '0') || (byte > '9' && byte < 'A') || (byte > 'F') ) {
        CDC_Device_SendByte(&CDC_interface, 0x07);
        return 0;
    }
    
    // Hopefully we've sanitized input enough
    CDC_Device_SendByte(&CDC_interface, byte);
    textBuffer[textBufferIndex] = byte;
    textBufferIndex++;
    return 0;
    

}
示例#29
0
void sendStringP(const uint8_t *string_in_progmem)
{
    while (pgm_read_byte(string_in_progmem) != '\0')
        CDC_Device_SendByte(&CDC_interface, pgm_read_byte(string_in_progmem++));
}
示例#30
0
void usb_serial_send(const char c){
    CDC_Device_SendByte(&VirtualSerial_CDC_Interface, c);
    CDC_Device_SendByte(&VirtualSerial_CDC_Interface, '\n');
}