X-Git-Url: http://git.linex4red.de/pub/USBasp.git/blobdiff_plain/2a0c28e6e47c8a173f32fc99cd8666a2633c5c12..5e3572d7d14bd4f7812003effc822c4957fd50b3:/Projects/Benito/Benito.c diff --git a/Projects/Benito/Benito.c b/Projects/Benito/Benito.c index 720a2245d..fc87d63f4 100644 --- a/Projects/Benito/Benito.c +++ b/Projects/Benito/Benito.c @@ -9,13 +9,13 @@ /* Copyright 2009 Dean Camera (dean [at] fourwalledcubicle [dot] com) - Permission to use, copy, modify, and distribute this software - and its documentation for any purpose and without fee is hereby - granted, provided that the above copyright notice appear in all - copies and that both that the copyright notice and this - permission notice and warranty disclaimer appear in supporting - documentation, and that the name of the author not be used in - advertising or publicity pertaining to distribution of the + Permission to use, copy, modify, distribute, and sell this + software and its documentation for any purpose is hereby granted + without fee, provided that the above copyright notice appear in + all copies and that both that the copyright notice and this + permission notice and warranty disclaimer appear in supporting + documentation, and that the name of the author not be used in + advertising or publicity pertaining to distribution of the software without specific, written prior permission. The author disclaim all warranties with regard to this @@ -31,19 +31,22 @@ /** \file * * Main source file for the Benito project. This file contains the main tasks of - * the demo and is responsible for the initial application hardware configuration. + * the project and is responsible for the initial application hardware configuration. */ #include "Benito.h" -/** Counter for the number of milliseconds remaining for the target /RESET pulse being generated. */ -volatile uint8_t ResetPulseMSRemaining = 0; +/** Circular buffer to hold data from the serial port before it is sent to the host. */ +RingBuff_t Tx_Buffer; -/** Counter for the number of milliseconds remaining for the TX activity LED pulse being generated. */ -volatile uint8_t TxPulseMSRemaining = 0; - -/** Counter for the number of milliseconds remaining for the RX activity LED pulse being generated. */ -volatile uint8_t RxPulseMSRemaining = 0; +/** Pulse generation counters to keep track of the number of milliseconds remaining for each pulse type */ +volatile struct +{ + uint8_t ResetPulse; /**< Milliseconds remaining for target /RESET pulse */ + uint8_t TxLEDPulse; /**< Milliseconds remaining for data Tx LED pulse */ + uint8_t RxLEDPulse; /**< Milliseconds remaining for data Rx LED pulse */ + uint8_t PingPongLEDPulse; /**< Milliseconds remaining for enumeration Tx/Rx ping-pong LED pulse */ +} PulseMSRemaining; /** LUFA CDC Class driver interface configuration and state information. This structure is * passed to all CDC Class driver functions, so that multiple instances of the same class @@ -53,22 +56,20 @@ USB_ClassInfo_CDC_Device_t VirtualSerial_CDC_Interface = { .Config = { - .ControlInterfaceNumber = 0, + .ControlInterfaceNumber = 0, - .DataINEndpointNumber = CDC_TX_EPNUM, - .DataINEndpointSize = CDC_TXRX_EPSIZE, + .DataINEndpointNumber = CDC_TX_EPNUM, + .DataINEndpointSize = CDC_TXRX_EPSIZE, + .DataINEndpointDoubleBank = false, - .DataOUTEndpointNumber = CDC_RX_EPNUM, - .DataOUTEndpointSize = CDC_TXRX_EPSIZE, + .DataOUTEndpointNumber = CDC_RX_EPNUM, + .DataOUTEndpointSize = CDC_TXRX_EPSIZE, + .DataOUTEndpointDoubleBank = false, - .NotificationEndpointNumber = CDC_NOTIFICATION_EPNUM, - .NotificationEndpointSize = CDC_NOTIFICATION_EPSIZE, + .NotificationEndpointNumber = CDC_NOTIFICATION_EPNUM, + .NotificationEndpointSize = CDC_NOTIFICATION_EPSIZE, + .NotificationEndpointDoubleBank = false, }, - - .State = - { - // Leave all state values to their defaults - } }; /** Main program entry point. This routine contains the overall program flow, including initial @@ -77,42 +78,52 @@ USB_ClassInfo_CDC_Device_t VirtualSerial_CDC_Interface = int main(void) { SetupHardware(); - - LEDs_SetAllLEDs(LEDMASK_USB_NOTREADY); + + Buffer_Initialize(&Tx_Buffer); for (;;) { /* Echo bytes from the host to the target via the hardware USART */ - if (CDC_Device_BytesReceived(&VirtualSerial_CDC_Interface)) + while (CDC_Device_BytesReceived(&VirtualSerial_CDC_Interface) > 0) { Serial_TxByte(CDC_Device_ReceiveByte(&VirtualSerial_CDC_Interface)); LEDs_TurnOnLEDs(LEDMASK_TX); - TxPulseMSRemaining = TX_RX_LED_PULSE_MS; + PulseMSRemaining.TxLEDPulse = TX_RX_LED_PULSE_MS; } /* Echo bytes from the target to the host via the virtual serial port */ - if (Serial_IsCharReceived()) + while (Tx_Buffer.Elements > 0) { - CDC_Device_SendByte(&VirtualSerial_CDC_Interface, Serial_RxByte()); + CDC_Device_SendByte(&VirtualSerial_CDC_Interface, Buffer_GetElement(&Tx_Buffer)); LEDs_TurnOnLEDs(LEDMASK_RX); - RxPulseMSRemaining = TX_RX_LED_PULSE_MS; + PulseMSRemaining.RxLEDPulse = 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 (ResetPulseMSRemaining && !(--ResetPulseMSRemaining)) - AVR_RESET_LINE_DDR &= ~AVR_RESET_LINE_MASK; - + 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 (TxPulseMSRemaining && !(--TxPulseMSRemaining)) + if (PulseMSRemaining.TxLEDPulse && !(--PulseMSRemaining.TxLEDPulse)) LEDs_TurnOffLEDs(LEDMASK_TX); /* Turn off RX LED(s) once the RX pulse period has elapsed */ - if (RxPulseMSRemaining && !(--RxPulseMSRemaining)) + if (PulseMSRemaining.RxLEDPulse && !(--PulseMSRemaining.RxLEDPulse)) LEDs_TurnOffLEDs(LEDMASK_RX); /* Clear the millisecond timer CTC flag (cleared by writing logic one to the register) */ @@ -135,6 +146,7 @@ void SetupHardware(void) clock_prescale_set(clock_div_1); /* Hardware Initialization */ + Serial_Init(9600, false); LEDs_Init(); USB_Init(); @@ -149,30 +161,33 @@ void SetupHardware(void) } /** Event handler for the library USB Connection event. */ -void EVENT_USB_Connect(void) +void EVENT_USB_Device_Connect(void) { - LEDs_SetAllLEDs(LEDMASK_USB_ENUMERATING); + PulseMSRemaining.PingPongLEDPulse = PING_PONG_LED_PULSE_MS; + LEDs_SetAllLEDs(LEDMASK_TX); } /** Event handler for the library USB Disconnection event. */ -void EVENT_USB_Disconnect(void) +void EVENT_USB_Device_Disconnect(void) { - LEDs_SetAllLEDs(LEDMASK_USB_NOTREADY); + PulseMSRemaining.PingPongLEDPulse = 0; + LEDs_SetAllLEDs(LEDS_NO_LEDS); } /** Event handler for the library USB Configuration Changed event. */ -void EVENT_USB_ConfigurationChanged(void) +void EVENT_USB_Device_ConfigurationChanged(void) { - LEDs_SetAllLEDs(LEDMASK_USB_READY); + PulseMSRemaining.PingPongLEDPulse = 0; + LEDs_SetAllLEDs(LEDS_NO_LEDS); if (!(CDC_Device_ConfigureEndpoints(&VirtualSerial_CDC_Interface))) - LEDs_SetAllLEDs(LEDMASK_USB_ERROR); + LEDs_SetAllLEDs(LEDMASK_ERROR); } -/** Event handler for the library USB Unhandled Control Packet event. */ -void EVENT_USB_UnhandledControlPacket(void) +/** Event handler for the library USB Unhandled Control Request event. */ +void EVENT_USB_Device_UnhandledControlRequest(void) { - CDC_Device_ProcessControlPacket(&VirtualSerial_CDC_Interface); + CDC_Device_ProcessControlRequest(&VirtualSerial_CDC_Interface); } /** Event handler for the CDC Class driver Line Encoding Changed event. @@ -210,11 +225,22 @@ void EVENT_CDC_Device_LineEncodingChanged(USB_ClassInfo_CDC_Device_t* const CDCI } UCSR1A = (1 << U2X1); - UCSR1B = ((1 << TXEN1) | (1 << RXEN1)); + UCSR1B = ((1 << RXCIE1) | (1 << TXEN1) | (1 << RXEN1)); UCSR1C = ConfigMask; UBRR1 = SERIAL_2X_UBBRVAL((uint16_t)CDCInterfaceInfo->State.LineEncoding.BaudRateBPS); } +/** ISR to manage the reception of data from the serial port, placing received bytes into a circular buffer + * for later transmission to the host. + */ +ISR(USART1_RX_vect, ISR_BLOCK) +{ + uint8_t ReceivedByte = UDR1; + + if (USB_DeviceState == DEVICE_STATE_Configured) + Buffer_StoreElement(&Tx_Buffer, ReceivedByte); +} + /** Event handler for the CDC Class driver Host-to-Device Line Encoding Changed event. * * \param[in] CDCInterfaceInfo Pointer to the CDC class interface configuration structure being referenced @@ -224,7 +250,9 @@ void EVENT_CDC_Device_ControLineStateChanged(USB_ClassInfo_CDC_Device_t* const C /* Check if the DTR line has been asserted - if so, start the target AVR's reset pulse */ if (CDCInterfaceInfo->State.ControlLineStates.HostToDevice & CDC_CONTROL_LINE_OUT_DTR) { + LEDs_SetAllLEDs(LEDMASK_BUSY); + AVR_RESET_LINE_DDR |= AVR_RESET_LINE_MASK; - ResetPulseMSRemaining = AVR_RESET_PULSE_MS; + PulseMSRemaining.ResetPulse = AVR_RESET_PULSE_MS; } }