コード例 #1
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, 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();
	}
}
コード例 #2
0
ファイル: USB2AX_Serial.c プロジェクト: A-toft/usb2ax
/** 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(&ToUSB_Buffer, ToUSB_Buffer_Data, sizeof(ToUSB_Buffer_Data));

	LEDs_SetAllLEDs(LEDMASK_USB_NOTREADY);
	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);
		}
		
		/* Load the next byte from the USART transmit buffer into the USART */
		if (!(RingBuffer_IsEmpty(&USBtoUSART_Buffer)))
		  Serial_SendByte(RingBuffer_Remove(&USBtoUSART_Buffer));

		cdc_send_USB_data(&VirtualSerial_CDC_Interface);
		USB_USBTask();
	}
}
コード例 #3
0
ファイル: XPLAINBridge.c プロジェクト: 0xdec/tmk_keyboard
/** Event handler for the library USB Configuration Changed event. */
void EVENT_USB_Device_ConfigurationChanged(void)
{
	bool ConfigSuccess = true;

	/* Configure the device endpoints according to the selected mode */
	if (CurrentFirmwareMode == MODE_USART_BRIDGE)
	{
		ConfigSuccess &= CDC_Device_ConfigureEndpoints(&VirtualSerial_CDC_Interface);

		/* Configure the UART flush timer - run at Fcpu/1024 for maximum interval before overflow */
		TCCR0B = ((1 << CS02) | (1 << CS00));

		/* Initialize ring buffers used to hold serial data between USB and software UART interfaces */
		RingBuffer_InitBuffer(&USBtoUART_Buffer, USBtoUART_Buffer_Data, sizeof(USBtoUART_Buffer_Data));
		RingBuffer_InitBuffer(&UARTtoUSB_Buffer, UARTtoUSB_Buffer_Data, sizeof(UARTtoUSB_Buffer_Data));

		/* Start the software USART */
		SoftUART_Init();
	}
	else
	{
		ConfigSuccess &= Endpoint_ConfigureEndpoint(AVRISP_DATA_OUT_EPADDR, EP_TYPE_BULK, AVRISP_DATA_EPSIZE, 1);

		if ((AVRISP_DATA_IN_EPADDR & ENDPOINT_EPNUM_MASK) != (AVRISP_DATA_OUT_EPADDR & ENDPOINT_EPNUM_MASK))
		  ConfigSuccess &= Endpoint_ConfigureEndpoint(AVRISP_DATA_IN_EPADDR, EP_TYPE_BULK, AVRISP_DATA_EPSIZE, 1);

		/* Configure the V2 protocol packet handler */
		V2Protocol_Init();
	}

	LEDs_SetAllLEDs(ConfigSuccess ? LEDMASK_USB_READY : LEDMASK_USB_ERROR);
}
コード例 #4
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();
	}
}
コード例 #5
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();
	}
}
コード例 #6
0
void VncServerInit(void)
{
    USB_Init();

    /* Start the flush timer so that overflows occur rapidly to push received bytes to the USB interface */
    TCCR0B = (1 << CS02);

    RingBuffer_InitBuffer(&USBtoUSART_Buffer, USBtoUSART_Buffer_Data, sizeof(USBtoUSART_Buffer_Data));
    RingBuffer_InitBuffer(&USARTtoUSB_Buffer, USARTtoUSB_Buffer_Data, sizeof(USARTtoUSB_Buffer_Data));
}
コード例 #7
0
ファイル: application.c プロジェクト: aauer1/miniminer
//------------------------------------------------------------------------------
void appInit(Application *app)
{
    RingBuffer_InitBuffer(&app->USARTtoUSB_Buffer, app->USARTtoUSB_Buffer_Data, sizeof(app->USARTtoUSB_Buffer_Data));
    RingBuffer_InitBuffer(&app->buffer_, app->buffer_data_, sizeof(app->buffer_data_));

    app->state.state = STATE_RESET;

    asicInit();
    asicStop();
}
コード例 #8
0
/** Configures the board hardware and chip peripherals for the demo's functionality. */
void SetupHardware(void)
{

	/* Hardware Initialization */
	LEDs_Init();
	USB_Init(0);

	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);

}
コード例 #9
0
/** Main program entry point. This routine configures the hardware required by the application, then
 *  enters a loop to run the application tasks in sequence.
 */
int main(void)
{
    SetupHardware();

    RingBuffer_InitBuffer(&USARTtoUSB_Buffer);

    sei();

    for (;;) 
    {
		HID_Device_USBTask(&Surface_HID_Interface);
		USB_USBTask();
	
		/* Turn off the Tx LED when the tick count reaches zero */
		if (led1_ticks) 
		{
			led1_ticks--;
			if (led1_ticks == 0) 
			{
				LEDs_TurnOffLEDs(LEDS_LED1);
			}
		}
		
		/* Turn off the Rx LED when the tick count reaches zero */
		if (led2_ticks) 
		{
			led2_ticks--;
			if (led2_ticks == 0) 
			{
				LEDs_TurnOffLEDs(LEDS_LED2);
			}
		}
    }
}
コード例 #10
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)
{
	stdout=&mystdout;
	stdin=&mystdin;

	SetupHardware();

	//LEDs_SetAllLEDs(LEDMASK_USB_NOTREADY);
	GlobalInterruptEnable();

	//Eval Prototype Sharp Reset Pin
	//DDRB &= ~RESET_PIN; //Set Input
	//PORTB |= RESET_PIN; //Set High
	//DDRB |= RESET_PIN; //Set Output

	//Drude Sharp LCD Reset PIN
	PORTF |= RESETN_PIN; //Set High
	DDRF |= RESETN_PIN; //Set Output
	
	//Digitizer Interrupt
	//DDRB &= ~(_BV(7)); //PB7 input
	//PORTB &= ~(1<<PB7); //PB7 low
	PORTB |= (_BV(7)); //Set high (input pullup)
	
	//IP4787CZ32Y HDMI ESD interface chip (HDMI_ACT PIN) Active-High (Test-Point 12)
	PORTF |= (_BV(PF1)); //Set high (input pullup)
	
	//Toshiba Interrupt Pin
	PORTE |= (_BV(PE6)); //Set high (input pullup)
	//Toshiba Standby Pin
	PORTF |= (_BV(PF4)); //Set high (input pullup)
	//Toshiba Reset Pin - Active Low
	PORTC |= (_BV(PC7)); //Set high (input pullup)

	//LCD-CABC
	//PORTF |= (_BV(PF7)); //Set high (defaults to input pullup)
	//DDRF &= ~(_BV(PF7)); //Set output
	//PORTF |= (_BV(PF7)); //Set low

	//LED-PWM
	PORTD |= (_BV(PD6)); //Set high (defaults to input pullup)
	//PORTD &= ~(_BV(PD6)); //Set low
	//DDRD &= ~(_BV(PD6)); //Set output


	RingBuffer_InitBuffer(&FromHost_Buffer, FromHost_Buffer_Data, sizeof(FromHost_Buffer_Data));

	init_screen(0x1F); //magic number!
	
	mxt_list_types();

	for (;;)
	{
		HandleSerial();
		CDC_Device_USBTask(&VirtualSerial_CDC_Interface);
		HID_Device_USBTask(&Digitizer_HID_Interface);
		USB_USBTask();
		HandleDigitizer();
	}
}
コード例 #11
0
ファイル: DUAL_MODE.c プロジェクト: AlanChatham/hiduino
	/* Pull target /RESET line high */
	AVR_RESET_LINE_PORT |= AVR_RESET_LINE_MASK;
	AVR_RESET_LINE_DDR  |= AVR_RESET_LINE_MASK;
}


// Event handler for the library USB Connection event. */
void EVENT_USB_Device_Connect(void) {
	LEDs_SetAllLEDs(LEDMASK_USB_ENUMERATING);
}


// Event handler for the library USB Disconnection event. */
void EVENT_USB_Device_Disconnect(void) {
	LEDs_SetAllLEDs(LEDMASK_USB_NOTREADY);
}


// Event handler for the library USB Configuration Changed event. */
void EVENT_USB_Device_ConfigurationChanged(void) {

	bool ConfigSuccess = true;
	
	if (CURRENT_MODE == MODE_SERIAL) {
		RingBuffer_InitBuffer(&USBtoUSART_Buffer);
		RingBuffer_InitBuffer(&USARTtoUSB_Buffer);
		ConfigSuccess &= CDC_Device_ConfigureEndpoints(&VirtualSerial_Interface);
	}
	else {
		UCSR1B |= (1 << RXCIE1) | (1 << TXCIE1);
		ConfigSuccess &= MIDI_Device_ConfigureEndpoints(&MIDI_Interface);
	}
	
	LEDs_SetAllLEDs(ConfigSuccess ? LEDMASK_USB_READY : LEDMASK_USB_ERROR);
	
}
コード例 #12
0
ファイル: USBtoSerial.c プロジェクト: ldarlok/LCS
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();
	
	while(1)
	{
		if(RTR==1)
		{
			if (ReceivedByte==0xFF)
			{
				for (uint8_t i = 0; i < 4; i++)
				{
					if (ReceivedByte==0)
					{
						ReceivedByte=1;
					}
					buff[i] = ReceivedByte;
					RTR = 0;
					while(RTR!=1){}
				}
				uint8_t range = ((buff[1]*256+buff[2])/100);
				RingBuffer_Insert(&USARTtoUSB_Buffer,buff[1]);
				RingBuffer_Insert(&USARTtoUSB_Buffer,buff[2]);
				//RingBuffer_Insert(&USARTtoUSB_Buffer,range);
				sendUSARTtoUSB();
				_delay_ms(500);
			}
		}		
//  		if (!RingBuffer_IsFull(&USBtoUSART_Buffer))
// 		{
// 			int16_t ReceivedByte = CDC_Device_ReceiveByte(&VirtualSerial_CDC_Interface);
// 			if (!(ReceivedByte < 0))
// 			{
// 				sendUSBtoUSART();
// 			}
			CDC_Device_USBTask(&VirtualSerial_CDC_Interface);
			USB_USBTask();
//		}
	}
}
コード例 #13
0
ファイル: SerialToLCD.c プロジェクト: agprimatic/lufa
/** 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(&FromHost_Buffer, FromHost_Buffer_Data, sizeof(FromHost_Buffer_Data));

	GlobalInterruptEnable();

	for (;;)
	{
		/* 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);
		}

		while (RingBuffer_GetCount(&FromHost_Buffer) > 0)
		{
			static uint8_t EscapePending = 0;
			int16_t HD44780Byte = RingBuffer_Remove(&FromHost_Buffer);

			if (HD44780Byte == COMMAND_ESCAPE)
			{
				if (EscapePending)
				{
					HD44780_WriteData(HD44780Byte);
					EscapePending = 0;
				}
				else
				{
					/* Next received character is the command byte */
					EscapePending = 1;
				}
			}
			else
			{
				if (EscapePending)
				{
					HD44780_WriteCommand(HD44780Byte);
					EscapePending = 0;
				}
				else
				{
					HD44780_WriteData(HD44780Byte);
				}
			}
		}

		CDC_Device_USBTask(&VirtualSerial_CDC_Interface);
		USB_USBTask();
	}
}
コード例 #14
0
ファイル: USBtoSerial.c プロジェクト: Sieg777/ibm_se
/******************************************************************************
* Nombre de la funcion	: void serial_init(void)
*    returns            : void
* Creada por		: Alejandro Weinstein
* Fecha Creacion	: 2013-04-08
*
* Descripcion : Inicializa el purto serial virtual USB
******************************************************************************/
void serial_init(void)
{
  SetupHardware();

  RingBuffer_InitBuffer(&USBtoUSART_Buffer, USBtoUSART_Buffer_Data, 
			sizeof(USBtoUSART_Buffer_Data));
  
  LEDs_SetAllLEDs(LEDMASK_USB_NOTREADY);
  GlobalInterruptEnable();
}
コード例 #15
0
ファイル: Gamepad.c プロジェクト: eskerda/arduines
/** 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);
    
    sei();
    
    for (;;)
    {
        HID_Device_USBTask(&Joystick_HID_Interface);
        USB_USBTask();
    }
}
コード例 #16
0
ファイル: serial.c プロジェクト: aauer1/miniminer
//------------------------------------------------------------------------------
void serialInit(void)
{
    RingBuffer_InitBuffer(&buffer_, buffer_data_, sizeof(buffer_data_));
    frame_size_ = 0;
}
コード例 #17
0
int main(void)
{
    SetupHardware();

    RingBuffer_InitBuffer(&RF12toUSB_Buffer, RF12toUSB_Buffer_Data, sizeof(RF12toUSB_Buffer_Data));
    RingBuffer_InitBuffer(&USBtoRF12_Buffer, USBtoRF12_Buffer_Data, sizeof(USBtoRF12_Buffer_Data));
    RingBuffer_InitBuffer(&Transmit_Buffer,  Transmit_Buffer_Data,  sizeof(Transmit_Buffer_Data));

    sei();

    for (;;)
    {
        uint16_t RFM12B_status = RFM12B_SPI_Transfer(0);
        
        if (( RFM12B_status & RFM12B_STATUS_RSSI )
           && !RFM12B_Transmit_Active )
        {
            PORTD &= ~0x40;
        }
        else
        {
            PORTD |= 0x40;
        }
        
        /* Only try to read in bytes from the CDC interface if the transmit buffer is not full */
        if (  !RingBuffer_IsFull( &USBtoRF12_Buffer )
           && !RFM12B_Transmit_Active )
        {
            int16_t ReceivedByte = CDC_Device_ReceiveByte(&VirtualSerial_CDC_Interface);

            /* Read bytes from the USB OUT endpoint into the USART transmit buffer */
            if (!(ReceivedByte < 0)) 
            {  
               if ( ReceivedByte != END_OF_PACKET )
               {
                   RingBuffer_Insert( &USBtoRF12_Buffer, ReceivedByte );
               }
               else
               {
                   /* TODO : Need to implement LBT */
                   RFM12B_Start_Transmit();
               }
            }
        }

        /* Check to see if there's an RGIT or an FFIT bit set in the status
           register indicating that we need to send or receive a byte */
        if ( RFM12B_status & RFM12B_STATUS_RGIT )
        {
            if ( RFM12B_Transmit_Active )
            {
                RFM12B_Transmit();
            }
            else
            {
                RFM12B_Receive();
            }
        }
        
        /* Check the flush timer.  This is used to timeout incoming packets
           that don't arrive in time or to flush the data to the USB host.
           First, check to see if we are in the middle of receiving a packet */
        if ( RxLength )
        {
            /* A packet is being received. Check the flush timer
               and timeout the packet if it takes too long */
            if ( TIFR0 & _BV(TOV0))
            {
                /* Clear flush timer expiry flag */
                TIFR0 |= _BV(TOV0);

                /* Flush timer overflows every 4ms.  256 bytes at ~50k
                   should take approximately 42ms so we allow 12 counts
                   for overflow.  This should probably be calculated
                   based on the bit rate and max message length */
                if ( ++RxTimeout > 12 )
                {
                    RingBuffer_Insert( &RF12toUSB_Buffer, END_OF_PACKET );
                    RFM12B_SPI_Transfer( 0xCA81 );
                    RFM12B_SPI_Transfer( 0xCA83 );
                    RxLength = 0;
                }
            }
        }
        else
        {
            /* No packet is being received.  Check if the UART receive 
               buffer flush timer has expired or the buffer is nearly full */
            uint16_t BufferCount = RingBuffer_GetCount(&RF12toUSB_Buffer);
            if ((TIFR0 & (1 << TOV0)) || (BufferCount > (uint8_t)(sizeof(RF12toUSB_Buffer_Data) * .75)))
            {
                /* Clear flush timer expiry flag */
                TIFR0 |= _BV(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(&RF12toUSB_Buffer)) != ENDPOINT_READYWAIT_NoError)
                    {
                        break;
                    }
    
                    /* Dequeue the already sent byte from the buffer now we have confirmed that no transmission error occurred */
                    RingBuffer_Remove(&RF12toUSB_Buffer);
                }
            }
        }
        
        CDC_Device_USBTask(&VirtualSerial_CDC_Interface);
        USB_USBTask();
    }
}
コード例 #18
0
ファイル: Benito.c プロジェクト: softants/lufa-lib
/** 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(&Tx_Buffer);

	sei();

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

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

		/* Check if the millisecond timer has elapsed */
		if (TIFR0 & (1 << OCF0A))
		{
			/* 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 */
			RingBuff_Count_t BufferCount = RingBuffer_GetCount(&Tx_Buffer);
			if (!(--FlushPeriodRemaining) || (BufferCount > 200))
			{
				/* Echo bytes from the target to the host via the virtual serial port */
				if (BufferCount)
				{
					while (BufferCount--)
					  CDC_Device_SendByte(&VirtualSerial_CDC_Interface, RingBuffer_Remove(&Tx_Buffer));

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

				FlushPeriodRemaining = RECEIVE_BUFFER_FLUSH_MS;
			}

			/* Clear the millisecond timer CTC flag (cleared by writing logic one to the register) */
			TIFR0 |= (1 << OCF0A);
		}

		CDC_Device_USBTask(&VirtualSerial_CDC_Interface);
		USB_USBTask();
	}
}
コード例 #19
0
/*---------------------------------------------------------------------------*/
int transport_init()
{
  RingBuffer_InitBuffer(&tcpBuffer, tcpBufferData, sizeof(tcpBufferData));
}
コード例 #20
0
ファイル: Arduino-usbserial.c プロジェクト: m3741/due_eeprom
/** 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();
	}

}
コード例 #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();

	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();
	}
}
コード例 #22
0
ファイル: Arduino-usbmidi.c プロジェクト: Eloher/arduino-usb
/** 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)
{
    MIDI_EventPacket_t midiEvent;
    struct {
	uint8_t command;
	uint8_t channel;
	uint8_t data2;
	uint8_t data3;
    } midiMsg;
    int ind;

    int led1_ticks = 0;
    int led2_ticks = 0;

    SetupHardware();

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

    LEDs_SetAllLEDs(LEDMASK_USB_NOTREADY);
    sei();

    for (;;) {
	RingBuff_Count_t BufferCount = RingBuffer_GetCount(&USARTtoUSB_Buffer);

	/* See if we have a message yet */
	if (BufferCount >= 4) {
	    /* Read in the message from the serial buffer */
	    for (ind=0; ind<4; ind++) {
		((uint8_t *)&midiMsg)[ind] = RingBuffer_Remove(&USARTtoUSB_Buffer);
	    }

	    /* Build a midi event to send via USB */
	    midiEvent.CableNumber = 0;
	    midiEvent.Command = midiMsg.command >> 4;
	    midiEvent.Data1 = (midiMsg.command & 0xF0) | ((midiMsg.channel-1) & 0x0F);
	    midiEvent.Data2 = midiMsg.data2;
	    midiEvent.Data3 = midiMsg.data3;

	    MIDI_Device_SendEventPacket(&Keyboard_MIDI_Interface, &midiEvent);
	    MIDI_Device_Flush(&Keyboard_MIDI_Interface);

	    /* Turn on the TX led and starts its timer */
	    LEDs_TurnOnLEDs(LEDS_LED1);
	    led1_ticks = LED_ON_TICKS;
	}
	
	/* Turn off the Tx LED when the tick count reaches zero */
	if (led1_ticks) {
	    led1_ticks--;
	    if (led1_ticks == 0) {
		LEDs_TurnOffLEDs(LEDS_LED1);
	    }
	}

	if (MIDI_Device_ReceiveEventPacket(&Keyboard_MIDI_Interface, &midiEvent)) {
	    RingBuff_Count_t count = RingBuffer_GetCount(&USBtoUSART_Buffer);
	    /* Room to send a message? */
	    if ((BUFFER_SIZE - count) >= sizeof(midiMsg)) {
		midiMsg.command = midiEvent.Command << 4;
		midiMsg.channel = (midiEvent.Data1 & 0x0F) + 1;
		midiMsg.data2 = midiEvent.Data2;
		midiMsg.data3 = midiEvent.Data3;

		for (ind=0; ind<sizeof(midiMsg); ind++) {
		    RingBuffer_Insert(&USBtoUSART_Buffer, ((uint8_t *)&midiMsg)[ind]);
		}

		/* Turn on the RX led and start its timer */
		LEDs_TurnOnLEDs(LEDS_LED2);
		led2_ticks = LED_ON_TICKS;
	    } else {
		/* Turn on the RX led and leave it on to indicate the
		 * buffer is full and the sketch is not reading it 
		 * fast enough.
		 */
		LEDs_TurnOnLEDs(LEDS_LED2);
	    }

	    /* if there's no room in the serial buffer the message gets dropped */
	}

	/* Turn off the RX LED when the tick count reaches zero */
	if (led2_ticks) {
	    led2_ticks--;
	    if (led2_ticks == 0) {
		LEDs_TurnOffLEDs(LEDS_LED2);
	    }
	}

	/* any data to send to main processor? */
	if (!(RingBuffer_IsEmpty(&USBtoUSART_Buffer))) {
	    Serial_TxByte(RingBuffer_Remove(&USBtoUSART_Buffer));
	}

	MIDI_Device_USBTask(&Keyboard_MIDI_Interface);
	USB_USBTask();
    }
}