diff options
Diffstat (limited to 'usb-spi')
-rw-r--r-- | usb-spi/Makefile | 6 | ||||
-rw-r--r-- | usb-spi/usb-spi.c | 74 |
2 files changed, 43 insertions, 37 deletions
diff --git a/usb-spi/Makefile b/usb-spi/Makefile index 67e99aa..4d18d3a 100644 --- a/usb-spi/Makefile +++ b/usb-spi/Makefile @@ -27,7 +27,7 @@ LIBS := util led lufa-descriptor-usbserial EXTERNAL_LIBS := lufa SPREADAVR_PATH := .. -LUFA_PATH := $(SPREADAVR_PATH)/contrib/LUFA-120219 +LUFA_PATH := $(SPREADAVR_PATH)/contrib/lufa-LUFA-140928 LUFA_OPTS = -D USB_DEVICE_ONLY LUFA_OPTS += -D DEVICE_STATE_AS_GPIOR=0 LUFA_OPTS += -D ORDERED_EP_CONFIG @@ -37,8 +37,8 @@ LUFA_OPTS += -D USE_FLASH_DESCRIPTORS LUFA_OPTS += -D USE_STATIC_OPTIONS="(USB_DEVICE_OPT_FULLSPEED | USB_OPT_REG_ENABLED | USB_OPT_AUTO_PLL)" LUFA_OPTS += -D INTERRUPT_CONTROL_ENDPOINT -LUFA_OPTS += -D USB_MANUFACTURER="L\"equinox\"" -D USB_MANUFACTURER_LEN=7 -LUFA_OPTS += -D USB_PRODUCT="L\"spreadspace usb-spi example\"" -D USB_PRODUCT_LEN=27 +LUFA_OPTS += -D USB_MANUFACTURER="L\"equinox\"" +LUFA_OPTS += -D USB_PRODUCT="L\"spreadspace usb-spi example\"" LUFA_COMPONENTS := USB USBCLASS diff --git a/usb-spi/usb-spi.c b/usb-spi/usb-spi.c index 6ae785a..3db169a 100644 --- a/usb-spi/usb-spi.c +++ b/usb-spi/usb-spi.c @@ -2,7 +2,7 @@ * spreadspace avr utils * * - * Copyright (C) 2013-2014 Christian Pointner <equinox@spreadspace.org> + * Copyright (C) 2013-2015 Christian Pointner <equinox@spreadspace.org> * * This file is part of spreadspace avr utils. * @@ -30,7 +30,7 @@ /* LUFA Library - Copyright (C) Dean Camera, 2012. + Copyright (C) Dean Camera, 2014. dean [at] fourwalledcubicle [dot] com www.lufa-lib.org @@ -55,19 +55,25 @@ USB_ClassInfo_CDC_Device_t VirtualSerial_CDC_Interface = { .Config = { - .ControlInterfaceNumber = 0, - - .DataINEndpointNumber = CDC_TX_EPNUM, - .DataINEndpointSize = CDC_TXRX_EPSIZE, - .DataINEndpointDoubleBank = false, - - .DataOUTEndpointNumber = CDC_RX_EPNUM, - .DataOUTEndpointSize = CDC_TXRX_EPSIZE, - .DataOUTEndpointDoubleBank = false, - - .NotificationEndpointNumber = CDC_NOTIFICATION_EPNUM, - .NotificationEndpointSize = CDC_NOTIFICATION_EPSIZE, - .NotificationEndpointDoubleBank = false, + .ControlInterfaceNumber = INTERFACE_ID_CDC_CCI, + .DataINEndpoint = + { + .Address = CDC_TX_EPADDR, + .Size = CDC_TXRX_EPSIZE, + .Banks = 1, + }, + .DataOUTEndpoint = + { + .Address = CDC_RX_EPADDR, + .Size = CDC_TXRX_EPSIZE, + .Banks = 1, + }, + .NotificationEndpoint = + { + .Address = CDC_NOTIFICATION_EPADDR, + .Size = CDC_NOTIFICATION_EPSIZE, + .Banks = 1, + }, }, }; @@ -101,8 +107,6 @@ void SetupHardware(void) led_init(); USB_Init(); - TCCR0B = (1 << CS02); - SPI_Init(SPI_SPEED_FCPU_DIV_16 | SPI_MODE_MASTER | SPI_ORDER_MSB_FIRST | SPI_SCK_LEAD_RISING | SPI_SAMPLE_LEADING); SPI_CS_DDR |= (1<<CS); @@ -167,26 +171,28 @@ int main(void) sei(); for (;;) { - int16_t BytesReceived = CDC_Device_BytesReceived(&VirtualSerial_CDC_Interface); - while(BytesReceived > 0) { - if(!(RingBuffer_IsFull(&USBtoSPI_Buffer))) { - int16_t ReceivedByte = CDC_Device_ReceiveByte(&VirtualSerial_CDC_Interface); - if (!(ReceivedByte < 0)) - RingBuffer_Insert(&USBtoSPI_Buffer, ReceivedByte); - - BytesReceived--; - } + /* Only try to read in bytes from the CDC interface if the transmit buffer is not full */ + if(!(RingBuffer_IsFull(&USBtoSPI_Buffer))) { + int16_t ReceivedByte = CDC_Device_ReceiveByte(&VirtualSerial_CDC_Interface); + if (!(ReceivedByte < 0)) + RingBuffer_Insert(&USBtoSPI_Buffer, ReceivedByte); } + uint16_t BufferCount = RingBuffer_GetCount(&SPItoUSB_Buffer); - if ((TIFR0 & (1 << TOV0)) || (BufferCount > (uint8_t)(sizeof(SPItoUSB_Buffer_Data) * .75))) { - TIFR0 |= (1 << TOV0); - while (BufferCount--) { - if (CDC_Device_SendByte(&VirtualSerial_CDC_Interface, - RingBuffer_Peek(&SPItoUSB_Buffer)) != ENDPOINT_READYWAIT_NoError) - break; - - RingBuffer_Remove(&SPItoUSB_Buffer); + if (BufferCount) { + Endpoint_SelectEndpoint(VirtualSerial_CDC_Interface.Config.DataINEndpoint.Address); + if (Endpoint_IsINReady()) { + uint8_t BytesToSend = MIN(BufferCount, (CDC_TXRX_EPSIZE - 1)); + + while (BytesToSend--) { + if (CDC_Device_SendByte(&VirtualSerial_CDC_Interface, + RingBuffer_Peek(&SPItoUSB_Buffer)) != ENDPOINT_READYWAIT_NoError) { + break; + } + + RingBuffer_Remove(&SPItoUSB_Buffer); + } } } |