summaryrefslogtreecommitdiff
path: root/usb-spi
diff options
context:
space:
mode:
authorChristian Pointner <equinox@spreadspace.org>2012-05-30 03:02:35 +0000
committerChristian Pointner <equinox@spreadspace.org>2012-05-30 03:02:35 +0000
commitc799eccf0b9aca0ee9a88651afd887756d0aa30c (patch)
tree06f297e3e11bf21fb6ffe95e205cbc38f7c07979 /usb-spi
parenttabs vs. space (diff)
cleaned usb-spi example
git-svn-id: https://svn.spreadspace.org/avr/trunk@46 aa12f405-d877-488e-9caf-2d797e2a1cc7
Diffstat (limited to 'usb-spi')
-rw-r--r--usb-spi/usb-spi.c65
1 files changed, 8 insertions, 57 deletions
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<<MOSI)|(1<<SCK)|(1<<CS);
SPI_PORT = (1<<CS);
SPCR = (1<<SPE)|(1<<MSTR);
@@ -220,7 +187,7 @@ void SPI_TransferBuffer()
while(!(RingBuffer_IsEmpty(&USBtoSPI_Buffer))) {
SPDR = RingBuffer_Remove(&USBtoSPI_Buffer);
while(!(SPSR & (1<<SPIF)));
-
+
uint8_t ReceivedByte = SPDR;
if (USB_DeviceState == DEVICE_STATE_Configured) {
RingBuffer_Insert(&SPItoUSB_Buffer, ReceivedByte);
@@ -228,23 +195,20 @@ void SPI_TransferBuffer()
break;
}
}
-
+
SPI_PORT |= (1<<CS);
}
-/** Event handler for the library USB Connection event. */
void EVENT_USB_Device_Connect(void)
{
led_on();
}
-/** Event handler for the library USB Disconnection event. */
void EVENT_USB_Device_Disconnect(void)
{
led_off();
}
-/** Event handler for the library USB Configuration Changed event. */
void EVENT_USB_Device_ConfigurationChanged(void)
{
bool ConfigSuccess = true;
@@ -256,20 +220,7 @@ void EVENT_USB_Device_ConfigurationChanged(void)
led_toggle();
}
-/** Event handler for the library USB Control Request reception event. */
void EVENT_USB_Device_ControlRequest(void)
{
CDC_Device_ProcessControlRequest(&VirtualSerial_CDC_Interface);
}
-
-/** Event handler for the CDC Class driver Line Encoding Changed event.
- *
- * \param[in] CDCInterfaceInfo Pointer to the CDC class interface configuration structure being referenced
- */
-void EVENT_CDC_Device_LineEncodingChanged(USB_ClassInfo_CDC_Device_t* const CDCInterfaceInfo)
-{
-/*
- * Ignore config change for SPI
- */
-}
-