From c799eccf0b9aca0ee9a88651afd887756d0aa30c Mon Sep 17 00:00:00 2001 From: Christian Pointner Date: Wed, 30 May 2012 03:02:35 +0000 Subject: cleaned usb-spi example git-svn-id: https://svn.spreadspace.org/avr/trunk@46 aa12f405-d877-488e-9caf-2d797e2a1cc7 --- usb-spi/usb-spi.c | 65 +++++++------------------------------------------------ 1 file changed, 8 insertions(+), 57 deletions(-) (limited to 'usb-spi/usb-spi.c') diff --git a/usb-spi/usb-spi.c b/usb-spi/usb-spi.c index 950f4f3..513df00 100644 --- a/usb-spi/usb-spi.c +++ b/usb-spi/usb-spi.c @@ -21,7 +21,7 @@ */ -/* this is more or less the same as the LUFA example Project USBtoSerial and +/* this is based on the LUFA example Project USBtoSerial this is their LICENSE Header */ /* @@ -93,18 +93,9 @@ void EVENT_USB_Device_Disconnect(void); void EVENT_USB_Device_ConfigurationChanged(void); void EVENT_USB_Device_ControlRequest(void); -void EVENT_CDC_Device_LineEncodingChanged(USB_ClassInfo_CDC_Device_t* const CDCInterfaceInfo); - -/** Circular buffer to hold data from the host before it is sent to the device via the serial port. */ static RingBuffer_t USBtoSPI_Buffer; - -/** Underlying data buffer for \ref USBtoSPI_Buffer, where the stored bytes are located. */ static uint8_t USBtoSPI_Buffer_Data[128]; - -/** Circular buffer to hold data from the serial port before it is sent to the host. */ static RingBuffer_t SPItoUSB_Buffer; - -/** Underlying data buffer for \ref SPItoUSB_Buffer, where the stored bytes are located. */ static uint8_t SPItoUSB_Buffer_Data[128]; /** LUFA CDC Class driver interface configuration and state information. This structure is @@ -132,9 +123,6 @@ USB_ClassInfo_CDC_Device_t VirtualSerial_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(); @@ -143,17 +131,12 @@ int main(void) RingBuffer_InitBuffer(&SPItoUSB_Buffer, SPItoUSB_Buffer_Data, sizeof(SPItoUSB_Buffer_Data)); sei(); - - for (;;) - { + for (;;) { int16_t BytesReceived = CDC_Device_BytesReceived(&VirtualSerial_CDC_Interface); - while(BytesReceived > 0) - { - if(!(RingBuffer_IsFull(&USBtoSPI_Buffer))) - { + while(BytesReceived > 0) { + if(!(RingBuffer_IsFull(&USBtoSPI_Buffer))) { int16_t ReceivedByte = CDC_Device_ReceiveByte(&VirtualSerial_CDC_Interface); - /* Read bytes from the USB OUT endpoint into the SPI transmit buffer */ if (!(ReceivedByte < 0)) RingBuffer_Insert(&USBtoSPI_Buffer, ReceivedByte); @@ -161,29 +144,18 @@ int main(void) } } - /* Check if the SPI receive buffer flush timer has expired or the buffer is nearly full */ uint16_t BufferCount = RingBuffer_GetCount(&SPItoUSB_Buffer); - if ((TIFR0 & (1 << TOV0)) || (BufferCount > (uint8_t)(sizeof(SPItoUSB_Buffer_Data) * .75))) - { - /* Clear flush timer expiry flag */ + if ((TIFR0 & (1 << TOV0)) || (BufferCount > (uint8_t)(sizeof(SPItoUSB_Buffer_Data) * .75))) { TIFR0 |= (1 << TOV0); - - /* Read bytes from the SPI 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 */ + while (BufferCount--) { if (CDC_Device_SendByte(&VirtualSerial_CDC_Interface, RingBuffer_Peek(&SPItoUSB_Buffer)) != ENDPOINT_READYWAIT_NoError) - { break; - } - /* Dequeue the already sent byte from the buffer now we have confirmed that no transmission error occurred */ RingBuffer_Remove(&SPItoUSB_Buffer); } } - /* Load the next byte from the SPI transmit buffer into the SPI Interface */ if (!(RingBuffer_IsEmpty(&USBtoSPI_Buffer))) SPI_TransferBuffer(); @@ -192,10 +164,8 @@ int main(void) } } -/** Configures the board hardware and chip peripherals for the demo's functionality. */ void SetupHardware(void) { - /* Disable watchdog if enabled by bootloader/fuses */ MCUSR &= ~(1 << WDRF); wdt_disable(); @@ -203,11 +173,8 @@ void SetupHardware(void) led_init(); USB_Init(); - /* Start the flush timer so that overflows occur rapidly to push received bytes to the USB interface */ TCCR0B = (1 << CS02); - /* Initialize SPI Interfaces - configure Direction of SS / PB0 , MOSI and SCLK as Output */ SPI_DDR = (1<