3 Copyright (C) Dean Camera, 2009.
5 dean [at] fourwalledcubicle [dot] com
6 www.fourwalledcubicle.com
10 Copyright 2009 Dean Camera (dean [at] fourwalledcubicle [dot] com)
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.
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
31 #include "USBtoSerial.h"
36 USB_ClassInfo_CDC_t VirtualSerial_CDC_Interface
=
38 .ControlInterfaceNumber
= 0,
40 .DataINEndpointNumber
= CDC_TX_EPNUM
,
41 .DataINEndpointSize
= CDC_TXRX_EPSIZE
,
43 .DataOUTEndpointNumber
= CDC_RX_EPNUM
,
44 .DataOUTEndpointSize
= CDC_TXRX_EPSIZE
,
46 .NotificationEndpointNumber
= CDC_NOTIFICATION_EPNUM
,
47 .NotificationEndpointSize
= CDC_NOTIFICATION_EPSIZE
,
54 Buffer_Initialize(&Rx_Buffer
);
55 Buffer_Initialize(&Tx_Buffer
);
57 LEDs_SetAllLEDs(LEDMASK_USB_NOTREADY
);
61 for (uint8_t DataBytesRem
= USB_CDC_BytesReceived(&VirtualSerial_CDC_Interface
); DataBytesRem
!= 0; DataBytesRem
--)
63 if (!(BUFF_STATICSIZE
- Rx_Buffer
.Elements
))
66 Buffer_StoreElement(&Rx_Buffer
, USB_CDC_ReceiveByte(&VirtualSerial_CDC_Interface
));
69 if (Tx_Buffer
.Elements
)
70 USB_CDC_SendByte(&VirtualSerial_CDC_Interface
, Buffer_GetElement(&Rx_Buffer
));
72 if (Rx_Buffer
.Elements
)
73 Serial_TxByte(Buffer_GetElement(&Rx_Buffer
));
75 USB_CDC_USBTask(&VirtualSerial_CDC_Interface
);
80 void SetupHardware(void)
82 /* Disable watchdog if enabled by bootloader/fuses */
83 MCUSR
&= ~(1 << WDRF
);
86 /* Disable clock division */
87 clock_prescale_set(clock_div_1
);
89 /* Hardware Initialization */
95 void EVENT_USB_Connect(void)
97 LEDs_SetAllLEDs(LEDMASK_USB_ENUMERATING
);
100 void EVENT_USB_Disconnect(void)
102 LEDs_SetAllLEDs(LEDMASK_USB_NOTREADY
);
105 void EVENT_USB_ConfigurationChanged(void)
107 LEDs_SetAllLEDs(LEDMASK_USB_READY
);
109 if (!(USB_CDC_ConfigureEndpoints(&VirtualSerial_CDC_Interface
)))
110 LEDs_SetAllLEDs(LEDMASK_USB_ERROR
);
113 void EVENT_USB_UnhandledControlPacket(void)
115 USB_CDC_ProcessControlPacket(&VirtualSerial_CDC_Interface
);
118 ISR(USART1_RX_vect
, ISR_BLOCK
)
121 Buffer_StoreElement(&Tx_Buffer
, UDR1
);
124 void EVENT_USB_CDC_LineEncodingChanged(USB_ClassInfo_CDC_t
* CDCInterfaceInfo
)
126 uint8_t ConfigMask
= 0;
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
);
133 if (CDCInterfaceInfo
->LineEncoding
.CharFormat
== TwoStopBits
)
134 ConfigMask
|= (1 << USBS1
);
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
));
143 UCSR1A
= (1 << U2X1
);
144 UCSR1B
= ((1 << RXCIE1
) | (1 << TXEN1
) | (1 << RXEN1
));
146 UBRR1
= SERIAL_2X_UBBRVAL((uint16_t)CDCInterfaceInfo
->LineEncoding
.BaudRateBPS
);