From 2d39a03941880c3862d2b2cb0b4d9286ecb53ee1 Mon Sep 17 00:00:00 2001 From: Christian Pointner Date: Tue, 29 May 2012 21:56:38 +0000 Subject: first working version of usb-spi git-svn-id: https://svn.spreadspace.org/avr/trunk@43 aa12f405-d877-488e-9caf-2d797e2a1cc7 --- usb-spi/usb-spi.c | 130 ++++++++++++++++++++++++------------------------------ 1 file changed, 58 insertions(+), 72 deletions(-) (limited to 'usb-spi/usb-spi.c') 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 -#include #include #include + /* 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<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 + */ } -- cgit v1.2.3