summaryrefslogtreecommitdiff
path: root/usb-spi
diff options
context:
space:
mode:
authorChristian Pointner <equinox@spreadspace.org>2012-05-29 21:56:38 +0000
committerChristian Pointner <equinox@spreadspace.org>2012-05-29 21:56:38 +0000
commit2d39a03941880c3862d2b2cb0b4d9286ecb53ee1 (patch)
tree8fc04b3af8aa33e14ac3f08e109b59f2bc864605 /usb-spi
parentstarted usb spi project as copy of usb-serial (diff)
first working version of usb-spi
git-svn-id: https://svn.spreadspace.org/avr/trunk@43 aa12f405-d877-488e-9caf-2d797e2a1cc7
Diffstat (limited to 'usb-spi')
-rw-r--r--usb-spi/usb-spi.c130
1 files changed, 58 insertions, 72 deletions
diff --git a/usb-spi/usb-spi.c b/usb-spi/usb-spi.c
index a4d578b..db85730 100644
--- a/usb-spi/usb-spi.c
+++ b/usb-spi/usb-spi.c
@@ -72,12 +72,21 @@
#include "led.h"
#include <LUFA/Version.h>
-#include <LUFA/Drivers/Peripheral/Serial.h>
#include <LUFA/Drivers/Misc/RingBuffer.h>
#include <LUFA/Drivers/USB/USB.h>
+ /* Hardware Defines: */
+#define SPI_DDR DDRB
+#define SPI_PORT PORTB
+#define SPI_PINB_REG PINB
+#define CS 0
+#define SCK 1
+#define MOSI 2
+#define MISO 3
+
/* Function Prototypes: */
void SetupHardware(void);
+void SPI_TransferBuffer(void);
void EVENT_USB_Device_Connect(void);
void EVENT_USB_Device_Disconnect(void);
@@ -87,16 +96,16 @@ 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 USBtoUSART_Buffer;
+static RingBuffer_t USBtoSPI_Buffer;
-/** Underlying data buffer for \ref USBtoUSART_Buffer, where the stored bytes are located. */
-static uint8_t USBtoUSART_Buffer_Data[128];
+/** 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 USARTtoUSB_Buffer;
+static RingBuffer_t SPItoUSB_Buffer;
-/** Underlying data buffer for \ref USARTtoUSB_Buffer, where the stored bytes are located. */
-static uint8_t USARTtoUSB_Buffer_Data[128];
+/** 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
* passed to all CDC Class driver functions, so that multiple instances of the same class
@@ -130,48 +139,48 @@ 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));
+ RingBuffer_InitBuffer(&USBtoSPI_Buffer, USBtoSPI_Buffer_Data, sizeof(USBtoSPI_Buffer_Data));
+ RingBuffer_InitBuffer(&SPItoUSB_Buffer, SPItoUSB_Buffer_Data, sizeof(SPItoUSB_Buffer_Data));
sei();
for (;;)
{
/* Only try to read in bytes from the CDC interface if the transmit buffer is not full */
- if (!(RingBuffer_IsFull(&USBtoUSART_Buffer)))
+ if (!(RingBuffer_IsFull(&USBtoSPI_Buffer)))
{
int16_t ReceivedByte = CDC_Device_ReceiveByte(&VirtualSerial_CDC_Interface);
- /* Read bytes from the USB OUT endpoint into the USART transmit buffer */
+ /* Read bytes from the USB OUT endpoint into the SPI transmit buffer */
if (!(ReceivedByte < 0))
- RingBuffer_Insert(&USBtoUSART_Buffer, ReceivedByte);
+ RingBuffer_Insert(&USBtoSPI_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)))
+ /* 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 */
TIFR0 |= (1 << TOV0);
- /* Read bytes from the USART receive buffer into the USB IN endpoint */
+ /* 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 */
if (CDC_Device_SendByte(&VirtualSerial_CDC_Interface,
- RingBuffer_Peek(&USARTtoUSB_Buffer)) != ENDPOINT_READYWAIT_NoError)
+ 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(&USARTtoUSB_Buffer);
+ RingBuffer_Remove(&SPItoUSB_Buffer);
}
}
- /* Load the next byte from the USART transmit buffer into the USART */
- if (!(RingBuffer_IsEmpty(&USBtoUSART_Buffer)))
- Serial_SendByte(RingBuffer_Remove(&USBtoUSART_Buffer));
+ /* Load the next byte from the SPI transmit buffer into the SPI Interface */
+ if (!(RingBuffer_IsEmpty(&USBtoSPI_Buffer)))
+ SPI_TransferBuffer();
CDC_Device_USBTask(&VirtualSerial_CDC_Interface);
USB_USBTask();
@@ -191,6 +200,31 @@ void SetupHardware(void)
/* 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);
+}
+
+void SPI_TransferBuffer()
+{
+ SPI_PORT &= ~(1<<CS);
+
+ 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);
+ if(RingBuffer_IsFull(&USBtoSPI_Buffer))
+ break;
+ }
+ }
+
+ SPI_PORT |= (1<<CS);
}
/** Event handler for the library USB Connection event. */
@@ -223,62 +257,14 @@ void EVENT_USB_Device_ControlRequest(void)
CDC_Device_ProcessControlRequest(&VirtualSerial_CDC_Interface);
}
-/** 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)
- RingBuffer_Insert(&USARTtoUSB_Buffer, ReceivedByte);
-}
-
/** 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)
{
- uint8_t ConfigMask = 0;
-
- switch (CDCInterfaceInfo->State.LineEncoding.ParityType)
- {
- case CDC_PARITY_Odd:
- ConfigMask = ((1 << UPM11) | (1 << UPM10));
- break;
- case CDC_PARITY_Even:
- ConfigMask = (1 << UPM11);
- break;
- }
-
- if (CDCInterfaceInfo->State.LineEncoding.CharFormat == CDC_LINEENCODING_TwoStopBits)
- ConfigMask |= (1 << USBS1);
-
- switch (CDCInterfaceInfo->State.LineEncoding.DataBits)
- {
- case 6:
- ConfigMask |= (1 << UCSZ10);
- break;
- case 7:
- ConfigMask |= (1 << UCSZ11);
- break;
- case 8:
- ConfigMask |= ((1 << UCSZ11) | (1 << UCSZ10));
- break;
- }
-
- /* Must turn off USART before reconfiguring it, otherwise incorrect operation may occur */
- UCSR1B = 0;
- UCSR1A = 0;
- UCSR1C = 0;
-
- /* Set the new baud rate before configuring the USART */
- UBRR1 = SERIAL_2X_UBBRVAL(CDCInterfaceInfo->State.LineEncoding.BaudRateBPS);
-
- /* Reconfigure the USART in double speed mode for a wider baud rate range at the expense of accuracy */
- UCSR1C = ConfigMask;
- UCSR1A = (1 << U2X1);
- UCSR1B = ((1 << RXCIE1) | (1 << TXEN1) | (1 << RXEN1));
+/*
+ * Ignore config change for SPI
+ */
}