/** 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);
			}
		}
    }
}
Ejemplo n.º 2
0
void SetupHardware(void)
{

	// Disable watchdog if enabled by bootloader/fuses
	MCUSR &= ~(1 << WDRF);
	wdt_disable();

	Serial_Init(31250, false);

	// Start the flush timer so that overflows occur rapidly to
	// push received bytes to the USB interface
	TCCR0B = (1 << CS02);
			
	// Serial Interrupts
	UCSR1B = 0;
	UCSR1B = ((1 << RXCIE1) | (1 << TXEN1) | (1 << RXEN1));

	// https://github.com/ddiakopoulos/hiduino/issues/13
	/* Target /ERASE line is active HIGH: there is a mosfet that inverts logic */
	// These are defined in the makefile... 
	AVR_ERASE_LINE_PORT |= AVR_ERASE_LINE_MASK;
	AVR_ERASE_LINE_DDR |= AVR_ERASE_LINE_MASK; 

	// Disable clock division
	//clock_prescale_set(clock_div_1);
	CLKPR = (1 << CLKPCE);
    CLKPR = (0 << CLKPS3) | (0 << CLKPS2) | (0 << CLKPS1) | (0 << CLKPS0);

	LEDs_Init();
	USB_Init();

	LEDs_TurnOffLEDs(LEDS_LED1);
	LEDs_TurnOffLEDs(LEDS_LED2);
}
Ejemplo n.º 3
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();
	}
}
Ejemplo n.º 4
0
// From USB/Host to Arduino/Serial
void MIDI_To_Arduino(void)
{
	// Device must be connected and configured for the task to run
	if (USB_DeviceState != DEVICE_STATE_Configured) return;

	// Select the MIDI OUT stream
	Endpoint_SelectEndpoint(MIDI_STREAM_OUT_EPADDR);

	/* Check if a MIDI command has been received */
	if (Endpoint_IsOUTReceived())
	{
		MIDI_EventPacket_t MIDIEvent;

		/* Read the MIDI event packet from the endpoint */
		Endpoint_Read_Stream_LE(&MIDIEvent, sizeof(MIDIEvent), NULL);

		// Passthrough to Arduino
		Serial_SendByte(MIDIEvent.Data1);
		Serial_SendByte(MIDIEvent.Data2); 
		Serial_SendByte(MIDIEvent.Data3); 

		LEDs_TurnOffLEDs(LEDS_LED1);
		rx_ticks = TICK_COUNT;

		/* If the endpoint is now empty, clear the bank */
		if (!(Endpoint_BytesInEndpoint()))
		{
			/* Clear the endpoint ready for new packet */
			Endpoint_ClearOUT();
		}
	}

}
Ejemplo n.º 5
0
// From Arduino/Serial to USB/Host 
void MIDI_To_Host(void)
{
	// Device must be connected and configured for the task to run
	if (USB_DeviceState != DEVICE_STATE_Configured) return;

	// Select the MIDI IN stream
	Endpoint_SelectEndpoint(MIDI_STREAM_IN_EPADDR);

	if (Endpoint_IsINReady())
	{
		if (mPendingMessageValid == true)
		{
			mPendingMessageValid = false;

			// Write the MIDI event packet to the endpoint
			Endpoint_Write_Stream_LE(&mCompleteMessage, sizeof(mCompleteMessage), NULL);

			// Clear out complete message
			memset(&mCompleteMessage, 0, sizeof(mCompleteMessage)); 

			// Send the data in the endpoint to the host
			Endpoint_ClearIN();

			LEDs_TurnOffLEDs(LEDS_LED2);
			tx_ticks = TICK_COUNT; 
		}
	}

}
Ejemplo n.º 6
0
int
main(void)
{
	/* Disable watchdog */
	MCUSR &= ~(1 << WDRF);
	wdt_disable();

	/* Disable clock division */
	clock_prescale_set(clock_div_1);

	LEDs_Init();
	usb_init();

	sei();

	char c;
	for (;;) {
		c = getchar();

		switch (c) {
		case '1':
			printf("ok\r\n");
			LEDs_TurnOnLEDs(LEDS_LED1);
			break;

		case '0':
			printf("ok\r\n");
			LEDs_TurnOffLEDs(LEDS_LED1);
			break;

		default:
			printf("error\r\n");
		}
	}
}
Ejemplo n.º 7
0
int main(void)
{
    uint_reg_t Dummy;

    /* =============================
     *     Buttons Compile Check
     * ============================= */
    // cppcheck-suppress redundantAssignment
    Dummy = BUTTONS_BUTTON1;
    Buttons_Init();
    // cppcheck-suppress redundantAssignment
    Dummy = Buttons_GetStatus();
    Buttons_Disable();

    /* =============================
     *    Dataflash Compile Check
     * ============================= */
    // cppcheck-suppress redundantAssignment
    Dummy = DATAFLASH_TOTALCHIPS + DATAFLASH_NO_CHIP + DATAFLASH_CHIP1 + DATAFLASH_PAGE_SIZE + DATAFLASH_PAGES;
    Dataflash_Init();
    Dataflash_TransferByte(0);
    Dataflash_SendByte(0);
    // cppcheck-suppress redundantAssignment
    Dummy = Dataflash_ReceiveByte();
    // cppcheck-suppress redundantAssignment
    Dummy = Dataflash_GetSelectedChip();
    Dataflash_SelectChip(DATAFLASH_CHIP1);
    Dataflash_DeselectChip();
    Dataflash_SelectChipFromPage(0);
    Dataflash_ToggleSelectedChipCS();
    Dataflash_WaitWhileBusy();
    Dataflash_SendAddressBytes(0, 0);

    /* =============================
     *       LEDs Compile Check
     * ============================= */
    // cppcheck-suppress redundantAssignment
    Dummy = LEDS_LED1 + LEDS_LED2 + LEDS_LED3 + LEDS_LED4;
    LEDs_Init();
    LEDs_TurnOnLEDs(LEDS_ALL_LEDS);
    LEDs_TurnOffLEDs(LEDS_ALL_LEDS);
    LEDs_SetAllLEDs(LEDS_ALL_LEDS);
    LEDs_ChangeLEDs(LEDS_ALL_LEDS, LEDS_NO_LEDS);
    LEDs_ToggleLEDs(LEDS_ALL_LEDS);
    // cppcheck-suppress redundantAssignment
    Dummy = LEDs_GetLEDs();
    LEDs_Disable();

    /* =============================
     *     Joystick Compile Check
     * ============================= */
    // cppcheck-suppress redundantAssignment
    Dummy = JOY_LEFT + JOY_RIGHT + JOY_UP + JOY_DOWN + JOY_PRESS;
    Joystick_Init();
    // cppcheck-suppress redundantAssignment
    Dummy = Joystick_GetStatus();
    Joystick_Disable();

    (void)Dummy;
}
Ejemplo n.º 8
0
int main(void)
{
	uint_reg_t Dummy;

	/* =============================
	 *     Buttons Compile Check
	 * ============================= */
	Buttons_Init();
	// cppcheck-suppress redundantAssignment
	Dummy = Buttons_GetStatus();
	Buttons_Disable();

	/* =============================
	 *    Dataflash Compile Check
	 * ============================= */
	Dataflash_Init();
	Dataflash_TransferByte(0);
	Dataflash_SendByte(0);
	// cppcheck-suppress redundantAssignment
	Dummy = Dataflash_ReceiveByte();
	// cppcheck-suppress redundantAssignment
	Dummy = Dataflash_GetSelectedChip();
	Dataflash_SelectChip(0);
	Dataflash_DeselectChip();
	Dataflash_SelectChipFromPage(0);
	Dataflash_ToggleSelectedChipCS();
	Dataflash_WaitWhileBusy();
	Dataflash_SendAddressBytes(0, 0);

	/* =============================
	 *       LEDs Compile Check
	 * ============================= */
	LEDs_Init();
	LEDs_TurnOnLEDs(LEDS_ALL_LEDS);
	LEDs_TurnOffLEDs(LEDS_ALL_LEDS);
	LEDs_SetAllLEDs(LEDS_ALL_LEDS);
	LEDs_ChangeLEDs(LEDS_ALL_LEDS, LEDS_NO_LEDS);
	LEDs_ToggleLEDs(LEDS_ALL_LEDS);
	// cppcheck-suppress redundantAssignment
	Dummy = LEDs_GetLEDs();
	LEDs_Disable();

	/* =============================
	 *     Joystick Compile Check
	 * ============================= */
	Joystick_Init();
	// cppcheck-suppress redundantAssignment
	Dummy = Joystick_GetStatus();
	Joystick_Disable();

	(void)Dummy;
}
Ejemplo n.º 9
0
// write (length) bytes, (start) is a byte address
uint8_t write_eeprom_chunk(int start, int length) {
	// this writes byte-by-byte,
	// page writing may be faster (4 bytes at a time)
	fill(length);
	LEDs_TurnOffLEDs(LEDS_PMODE);
	for (int x = 0; x < length; x++) {
		//TODO check here if we can use intern address
		int addr = start + x;
		accessData(0xC0, addr, ram.RingBuffer_Data[x]);
		_delay_ms(45);
	}
	LEDs_TurnOnLEDs(LEDS_PMODE);
	return STK_OK;
}
Ejemplo n.º 10
0
void MIDI_IN(void) {

	MIDI_EventPacket_t ReceivedMIDIEvent;

	if (MIDI_Device_ReceiveEventPacket(&MIDI_Interface, &ReceivedMIDIEvent)) {
		LEDs_TurnOnLEDs(LEDMASK_RX);
		Serial_TxByte(ReceivedMIDIEvent.Data1);
		Serial_TxByte(ReceivedMIDIEvent.Data2);
		Serial_TxByte(ReceivedMIDIEvent.Data3); 	
	}
		
	else {
		LEDs_TurnOffLEDs(LEDMASK_RX);
	}

}
Ejemplo n.º 11
0
void WiFiEchoTest(void) {
	uint8_t c = 0;

	while (TRUE) {
		// Wait until FIFO empty and write char
		while (!UartTxFifoEmpty());
		UartPutchar(c);
		// Waitn until char is echoed and compare with sent
		while (!UartRxFifoData());
		if (c != UartGetchar()) {
			// Received character does not match!
			LEDs_TurnOffLEDs(LEDS_LED2);
			LEDs_ToggleLEDs(LEDS_LED1);
		}
		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();
	}
}
Ejemplo n.º 13
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)
{
    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();
    }
}
// Main
int main(void)
{
    setupHardware();

    LEDs_TurnOnLEDs(LEDMASK_USB_NOTREADY);
    sei();

    while(1)
    {
        // Perform the HID specific tasks
        hidTask();

        // Perform the USB library tasks
        USB_USBTask();

        // Show command processing activity on led 1 (Rx)
        if (activityIndicatorRx > 0)
        {
            // See if this is the first loop
            if (activityIndicatorRx == 1) LEDs_TurnOnLEDs(LEDS_LED1);
            activityIndicatorRx++;

            if (activityIndicatorRx > 5000) // Flash length is in loops (16 bit maximum)
            {
                // Turn the indicator off
                activityIndicatorRx = 0;
                LEDs_TurnOffLEDs(LEDS_LED1);
            }
        }

        // Show command processing activity on led 2 (Tx)
        if (activityIndicatorTx > 0)
        {
            // See if this is the first loop
            if (activityIndicatorTx == 1) LEDs_TurnOnLEDs(LEDS_LED2);
            activityIndicatorTx++;

            if (activityIndicatorTx > 5000) // Flash length is in loops (16 bit maximum)
            {
                // Turn the indicator off
                activityIndicatorTx = 0;
                LEDs_TurnOffLEDs(LEDS_LED2);
            }
        }

        // Show flashing for system/blink command on led 3 (L)
        if (activityIndicatorL > 0)
        {
            // See if this is the first loop
            if (activityIndicatorL == 1)
            {
                LEDs_TurnOnLEDs(LEDS_LED3);
                activityIndicatorLblink = 0;
            }

            // Increment the activity counters
            activityIndicatorL++;
            activityIndicatorLblink++;

            // Control the flashing of the LED
            if (activityIndicatorLblink == 5000) LEDs_TurnOffLEDs(LEDS_LED3);
            if (activityIndicatorLblink == 10000)
            {
                LEDs_TurnOnLEDs(LEDS_LED3);
                activityIndicatorLblink = 0;
            }

            if (activityIndicatorL > 200000) // Flash length is in loops (32 bit maximum)
            {
                // Turn the indicator off
                activityIndicatorL = 0;

                // Default state of the L LED is on
                LEDs_TurnOnLEDs(LEDS_LED3);
            }
        }
    }
}
Ejemplo n.º 15
0
void avrisp(int ReceivedByte){
	// is pmode active?
	if (ram.isp.pmode) LEDs_TurnOnLEDs(LEDS_PMODE);
	else LEDs_TurnOffLEDs(LEDS_PMODE);

	// is there an error?
	if (ram.isp.error) LEDs_TurnOnLEDs(LEDS_ERR);
	else LEDs_TurnOffLEDs(LEDS_ERR);

	// read in bytes from the CDC interface
	if (!(ReceivedByte < 0)){
		switch (ReceivedByte) {
		case STK_GET_SYNC:
			ram.isp.error = 0;
			replyOK();
			break;
		case STK_GET_SIGNON:
			if (getch() == CRC_EOP) {
				sendCDCbyte(STK_INSYNC);
				sendCDCbyte('A');
				sendCDCbyte('V');
				sendCDCbyte('R');
				sendCDCbyte(' ');
				sendCDCbyte('I');
				sendCDCbyte('S');
				sendCDCbyte('P');
				sendCDCbyte(STK_OK);
			}
			break;
		case STK_GET_PARM:
			get_parameters(getch());
			break;
		case STK_SET_PARM:
			set_parameters();
			replyOK();
			break;
		case STK_SET_PARM_EXT: // extended parameters - ignore for now
			fill(5);
			replyOK();
			break;

		case STK_PMODE_START:
			start_pmode();
			replyOK();
			break;
		case STK_SET_ADDR:
			ram.isp._addr = getch();
			ram.isp._addr += 256 * getch();
			replyOK();
			break;

		case STK_PROG_FLASH:
			//uint8_t low = getch();
			getch();
			//uint8_t high = getch();
			getch();
			replyOK();
			break;
		case STK_PROG_DATA:
			//uint8_t data = getch();
			getch();
			replyOK();
			break;

		case STK_PROG_PAGE:
			program_page();
			break;

		case STK_READ_PAGE:
			read_page();
			break;

		case STK_UNIVERSAL:
			universal();
			break;
		case STK_PMODE_END:
			ram.isp.error = 0;
			end_pmode();
			replyOK();
			break;

		case STK_READ_SIGN:
			read_signature();
			break;

			// expecting a command, not CRC_EOP
			// this is how we can get back in sync
		case CRC_EOP:
			ram.isp.error++;
			sendCDCbyte(STK_NOSYNC);
			break;

			// anything else we will return STK_UNKNOWN
		default:
			ram.isp.error++;
			if (CRC_EOP == getch())
				sendCDCbyte(STK_UNKNOWN);
			else
				sendCDCbyte(STK_NOSYNC);
		}
	}

}
Ejemplo n.º 16
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);

			/* Store received byte into the USART transmit buffer */
			if (!(ReceivedByte < 0))
			  RingBuffer_Insert(&USBtoUSART_Buffer, ReceivedByte);
		}

		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())
			{
			    LEDs_TurnOnLEDs(LEDMASK_TX);
			    PulseMSRemaining.TxLEDPulse = TX_RX_LED_PULSE_MS;
			
				/* 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 transmit buffer space is available */
		if (Serial_IsSendReady() && !(RingBuffer_IsEmpty(&USBtoUSART_Buffer)))
        {
		    Serial_SendByte(RingBuffer_Remove(&USBtoUSART_Buffer));
            
		  	LEDs_TurnOnLEDs(LEDMASK_RX);
		  	PulseMSRemaining.RxLEDPulse = TX_RX_LED_PULSE_MS;
        }            

		CDC_Device_USBTask(&VirtualSerial_CDC_Interface);
		USB_USBTask();
                
        /* 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);
	}
}
Ejemplo n.º 17
0
int main(void)
{
	unsigned char tmp;
	
	wdt_disable();		/* Disable watchdog if enabled by bootloader/fuses */
	 
//	Buttons_Init();
	LEDs_Init();
	clock_prescale_set(clock_div_1);

	// Timer 0 setup for simple count mode, used as timebase for IR-Signal capture
	TCCR0A = 0;		// simple count more 		
	TCCR0B = 5;		// system Clock 16Mhz/1024 = 64us per counter tick 
	TIMSK0 = 0x1;	// Enable overflow interrupt enable 
	TIFR0 = 0;		// clear Overflow interrupt flag before enabling interrupts
	EIFR =0 ;		// clear external interrupt register
	DDRD = 0;		// all inputs
	DDRC = 0;		// all inputs
	PORTD = 0x80;	// pull-up on PD0 
	MCUSR =0;
	
	// Timer 1 setup for  fast PWM mode for servo pulse generation 1 to 2ms 
	DDRB = 0x81;	// PB7 = output OC1_C, PB0 =LED
	TIMSK1 = 0;		// timer 1 running with no interrupts
	TCCR1A = 0x0F;	// use OC1_C pin for servo pulse
	TCCR1B = 0x0C;	// Fast PWM 10bit, clock/256
	OCR1C = SERVO_POS_RIGHT;
	
	EIFR = 0 ;		// clear external interrupt flag
	EICRA = 0x3;	// set interrupt to raising edge trigger
	EIMSK = 0x01;	// enable INT0 (pin pb0 alternate function)

	sei();		//enable external interrupts
	
	// set initial state 
	Signal_captured = 1;
	IR_code = VIZIO_GREEN_BTN;
	
    while(1)
    {

/*// for manual toggling via HWB button
		if (IsButtonPressed(BUTTONS_BUTTON1) )
		{

			// de-bounce -- 5 consecutive reads of switch open 
			for (tmp =0; tmp<=5; tmp++)
			{
				_delay_ms(1);
				if (IsButtonPressed(BUTTONS_BUTTON1))
					tmp = 0;
			}
			
			if (IR_code == VIZIO_GREEN_BTN )
				IR_code = VIZIO_RED_BTN;
			else
				IR_code = VIZIO_GREEN_BTN;
			Signal_captured = 1;	
				
		}			
	*/			
		if ( Signal_captured )
		{

			 switch (IR_code)
			 {
				 case VIZIO_GREEN_BTN:		// Vizio Code Green button
					 LEDs_TurnOnLEDs(LEDS_LED1);
					 OCR1C = SERVO_POS_LEFT;
					 break;
					 
				 case VIZIO_RED_BTN:		// Vizio Code Red button
					 LEDs_TurnOffLEDs(LEDS_LED1);
					 OCR1C = SERVO_POS_RIGHT;
					 break;
			 }
			 Signal_captured = 0;
		}				
    }
}
Ejemplo n.º 18
0
void commit(int addr) {
	LEDs_TurnOffLEDs(LEDS_PMODE);
	accessData(0x4C, addr, 0x00);
	_delay_ms(30);
	LEDs_TurnOnLEDs(LEDS_PMODE);
}
Ejemplo n.º 19
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(&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();
	}
}
Ejemplo n.º 20
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();
	}

}