Removed new Start of Frame event from the library; performance suffered far too much...
[pub/USBasp.git] / Demos / Device / USBtoSerial / USBtoSerial.c
1 /*
2 LUFA Library
3 Copyright (C) Dean Camera, 2009.
4
5 dean [at] fourwalledcubicle [dot] com
6 www.fourwalledcubicle.com
7 */
8
9 /*
10 Copyright 2009 Dean Camera (dean [at] fourwalledcubicle [dot] com)
11
12 Permission to use, copy, modify, and distribute this software
13 and its documentation for any purpose and without fee is hereby
14 granted, provided that the above copyright notice appear in all
15 copies and that both that the copyright notice and this
16 permission notice and warranty disclaimer appear in supporting
17 documentation, and that the name of the author not be used in
18 advertising or publicity pertaining to distribution of the
19 software without specific, written prior permission.
20
21 The author disclaim all warranties with regard to this
22 software, including all implied warranties of merchantability
23 and fitness. In no event shall the author be liable for any
24 special, indirect or consequential damages or any damages
25 whatsoever resulting from loss of use, data or profits, whether
26 in an action of contract, negligence or other tortious action,
27 arising out of or in connection with the use or performance of
28 this software.
29 */
30
31 #include "USBtoSerial.h"
32
33 RingBuff_t Rx_Buffer;
34 RingBuff_t Tx_Buffer;
35
36 USB_ClassInfo_CDC_t VirtualSerial_CDC_Interface =
37 {
38 .ControlInterfaceNumber = 0,
39
40 .DataINEndpointNumber = CDC_TX_EPNUM,
41 .DataINEndpointSize = CDC_TXRX_EPSIZE,
42
43 .DataOUTEndpointNumber = CDC_RX_EPNUM,
44 .DataOUTEndpointSize = CDC_TXRX_EPSIZE,
45
46 .NotificationEndpointNumber = CDC_NOTIFICATION_EPNUM,
47 .NotificationEndpointSize = CDC_NOTIFICATION_EPSIZE,
48 };
49
50 int main(void)
51 {
52 SetupHardware();
53
54 Buffer_Initialize(&Rx_Buffer);
55 Buffer_Initialize(&Tx_Buffer);
56
57 LEDs_SetAllLEDs(LEDMASK_USB_NOTREADY);
58
59 for (;;)
60 {
61 for (uint8_t DataBytesRem = USB_CDC_BytesReceived(&VirtualSerial_CDC_Interface); DataBytesRem != 0; DataBytesRem--)
62 {
63 if (!(BUFF_STATICSIZE - Rx_Buffer.Elements))
64 break;
65
66 Buffer_StoreElement(&Rx_Buffer, USB_CDC_ReceiveByte(&VirtualSerial_CDC_Interface));
67 }
68
69 if (Tx_Buffer.Elements)
70 USB_CDC_SendByte(&VirtualSerial_CDC_Interface, Buffer_GetElement(&Rx_Buffer));
71
72 if (Rx_Buffer.Elements)
73 Serial_TxByte(Buffer_GetElement(&Rx_Buffer));
74
75 USB_CDC_USBTask(&VirtualSerial_CDC_Interface);
76 USB_USBTask();
77 }
78 }
79
80 void SetupHardware(void)
81 {
82 /* Disable watchdog if enabled by bootloader/fuses */
83 MCUSR &= ~(1 << WDRF);
84 wdt_disable();
85
86 /* Disable clock division */
87 clock_prescale_set(clock_div_1);
88
89 /* Hardware Initialization */
90 Joystick_Init();
91 LEDs_Init();
92 USB_Init();
93 }
94
95 void EVENT_USB_Connect(void)
96 {
97 LEDs_SetAllLEDs(LEDMASK_USB_ENUMERATING);
98 }
99
100 void EVENT_USB_Disconnect(void)
101 {
102 LEDs_SetAllLEDs(LEDMASK_USB_NOTREADY);
103 }
104
105 void EVENT_USB_ConfigurationChanged(void)
106 {
107 LEDs_SetAllLEDs(LEDMASK_USB_READY);
108
109 if (!(USB_CDC_ConfigureEndpoints(&VirtualSerial_CDC_Interface)))
110 LEDs_SetAllLEDs(LEDMASK_USB_ERROR);
111 }
112
113 void EVENT_USB_UnhandledControlPacket(void)
114 {
115 USB_CDC_ProcessControlPacket(&VirtualSerial_CDC_Interface);
116 }
117
118 ISR(USART1_RX_vect, ISR_BLOCK)
119 {
120 if (USB_IsConnected)
121 Buffer_StoreElement(&Tx_Buffer, UDR1);
122 }
123
124 void EVENT_USB_CDC_LineEncodingChanged(USB_ClassInfo_CDC_t* CDCInterfaceInfo)
125 {
126 uint8_t ConfigMask = 0;
127
128 if (CDCInterfaceInfo->LineEncoding.ParityType == Parity_Odd)
129 ConfigMask = ((1 << UPM11) | (1 << UPM10));
130 else if (CDCInterfaceInfo->LineEncoding.ParityType == Parity_Even)
131 ConfigMask = (1 << UPM11);
132
133 if (CDCInterfaceInfo->LineEncoding.CharFormat == TwoStopBits)
134 ConfigMask |= (1 << USBS1);
135
136 if (CDCInterfaceInfo->LineEncoding.DataBits == 6)
137 ConfigMask |= (1 << UCSZ10);
138 else if (CDCInterfaceInfo->LineEncoding.DataBits == 7)
139 ConfigMask |= (1 << UCSZ11);
140 else if (CDCInterfaceInfo->LineEncoding.DataBits == 8)
141 ConfigMask |= ((1 << UCSZ11) | (1 << UCSZ10));
142
143 UCSR1A = (1 << U2X1);
144 UCSR1B = ((1 << RXCIE1) | (1 << TXEN1) | (1 << RXEN1));
145 UCSR1C = ConfigMask;
146 UBRR1 = SERIAL_2X_UBBRVAL((uint16_t)CDCInterfaceInfo->LineEncoding.BaudRateBPS);
147 }