sanguino/firmwares/arduino-usbserial/Arduino-usbserial.c

Fri, 17 Nov 2017 10:13:31 +0100

author
mdd
date
Fri, 17 Nov 2017 10:13:31 +0100
changeset 3
770b218a4931
parent 2
b373b0288715
permissions
-rw-r--r--

proper configuration, homing and planner optimization

2
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
1 /*
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
2 LUFA Library
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
3 Copyright (C) Dean Camera, 2010.
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
4
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
5 dean [at] fourwalledcubicle [dot] com
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
6 www.fourwalledcubicle.com
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
7 */
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
8
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
9 /*
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
10 Copyright 2010 Dean Camera (dean [at] fourwalledcubicle [dot] com)
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
11
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
12 Permission to use, copy, modify, distribute, and sell this
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
13 software and its documentation for any purpose is hereby granted
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
14 without fee, provided that the above copyright notice appear in
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
15 all copies and that both that the copyright notice and this
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
16 permission notice and warranty disclaimer appear in supporting
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
17 documentation, and that the name of the author not be used in
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
18 advertising or publicity pertaining to distribution of the
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
19 software without specific, written prior permission.
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
20
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
21 The author disclaim all warranties with regard to this
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
22 software, including all implied warranties of merchantability
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
23 and fitness. In no event shall the author be liable for any
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
24 special, indirect or consequential damages or any damages
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
25 whatsoever resulting from loss of use, data or profits, whether
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
26 in an action of contract, negligence or other tortious action,
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
27 arising out of or in connection with the use or performance of
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
28 this software.
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
29 */
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
30
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
31 /** \file
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
32 *
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
33 * Main source file for the Arduino-usbserial project. This file contains the main tasks of
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
34 * the project and is responsible for the initial application hardware configuration.
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
35 */
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
36
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
37 #include "Arduino-usbserial.h"
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
38
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
39 /** Circular buffer to hold data from the host before it is sent to the device via the serial port. */
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
40 RingBuff_t USBtoUSART_Buffer;
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
41
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
42 /** Circular buffer to hold data from the serial port before it is sent to the host. */
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
43 RingBuff_t USARTtoUSB_Buffer;
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
44
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
45 /** Pulse generation counters to keep track of the number of milliseconds remaining for each pulse type */
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
46 volatile struct
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
47 {
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
48 uint8_t TxLEDPulse; /**< Milliseconds remaining for data Tx LED pulse */
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
49 uint8_t RxLEDPulse; /**< Milliseconds remaining for data Rx LED pulse */
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
50 uint8_t PingPongLEDPulse; /**< Milliseconds remaining for enumeration Tx/Rx ping-pong LED pulse */
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
51 } PulseMSRemaining;
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
52
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
53 /** LUFA CDC Class driver interface configuration and state information. This structure is
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
54 * passed to all CDC Class driver functions, so that multiple instances of the same class
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
55 * within a device can be differentiated from one another.
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
56 */
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
57 USB_ClassInfo_CDC_Device_t VirtualSerial_CDC_Interface =
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
58 {
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
59 .Config =
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
60 {
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
61 .ControlInterfaceNumber = 0,
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
62
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
63 .DataINEndpointNumber = CDC_TX_EPNUM,
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
64 .DataINEndpointSize = CDC_TXRX_EPSIZE,
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
65 .DataINEndpointDoubleBank = false,
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
66
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
67 .DataOUTEndpointNumber = CDC_RX_EPNUM,
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
68 .DataOUTEndpointSize = CDC_TXRX_EPSIZE,
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
69 .DataOUTEndpointDoubleBank = false,
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
70
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
71 .NotificationEndpointNumber = CDC_NOTIFICATION_EPNUM,
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
72 .NotificationEndpointSize = CDC_NOTIFICATION_EPSIZE,
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
73 .NotificationEndpointDoubleBank = false,
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
74 },
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
75 };
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
76
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
77 /** Main program entry point. This routine contains the overall program flow, including initial
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
78 * setup of all components and the main program loop.
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
79 */
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
80 int main(void)
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
81 {
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
82 SetupHardware();
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
83
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
84 RingBuffer_InitBuffer(&USBtoUSART_Buffer);
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
85 RingBuffer_InitBuffer(&USARTtoUSB_Buffer);
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
86
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
87 sei();
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
88
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
89 for (;;)
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
90 {
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
91 /* Only try to read in bytes from the CDC interface if the transmit buffer is not full */
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
92 if (!(RingBuffer_IsFull(&USBtoUSART_Buffer)))
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
93 {
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
94 int16_t ReceivedByte = CDC_Device_ReceiveByte(&VirtualSerial_CDC_Interface);
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
95
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
96 /* Read bytes from the USB OUT endpoint into the USART transmit buffer */
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
97 if (!(ReceivedByte < 0))
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
98 RingBuffer_Insert(&USBtoUSART_Buffer, ReceivedByte);
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
99 }
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
100
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
101 /* Check if the UART receive buffer flush timer has expired or the buffer is nearly full */
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
102 RingBuff_Count_t BufferCount = RingBuffer_GetCount(&USARTtoUSB_Buffer);
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
103 if ((TIFR0 & (1 << TOV0)) || (BufferCount > BUFFER_NEARLY_FULL))
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
104 {
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
105 TIFR0 |= (1 << TOV0);
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
106
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
107 if (USARTtoUSB_Buffer.Count) {
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
108 LEDs_TurnOnLEDs(LEDMASK_TX);
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
109 PulseMSRemaining.TxLEDPulse = TX_RX_LED_PULSE_MS;
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
110 }
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
111
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
112 /* Read bytes from the USART receive buffer into the USB IN endpoint */
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
113 while (BufferCount--)
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
114 CDC_Device_SendByte(&VirtualSerial_CDC_Interface, RingBuffer_Remove(&USARTtoUSB_Buffer));
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
115
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
116 /* Turn off TX LED(s) once the TX pulse period has elapsed */
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
117 if (PulseMSRemaining.TxLEDPulse && !(--PulseMSRemaining.TxLEDPulse))
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
118 LEDs_TurnOffLEDs(LEDMASK_TX);
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
119
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
120 /* Turn off RX LED(s) once the RX pulse period has elapsed */
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
121 if (PulseMSRemaining.RxLEDPulse && !(--PulseMSRemaining.RxLEDPulse))
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
122 LEDs_TurnOffLEDs(LEDMASK_RX);
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
123 }
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
124
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
125 /* Load the next byte from the USART transmit buffer into the USART */
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
126 if (!(RingBuffer_IsEmpty(&USBtoUSART_Buffer))) {
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
127 Serial_TxByte(RingBuffer_Remove(&USBtoUSART_Buffer));
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
128
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
129 LEDs_TurnOnLEDs(LEDMASK_RX);
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
130 PulseMSRemaining.RxLEDPulse = TX_RX_LED_PULSE_MS;
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
131 }
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
132
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
133 CDC_Device_USBTask(&VirtualSerial_CDC_Interface);
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
134 USB_USBTask();
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
135 }
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
136 }
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
137
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
138 /** Configures the board hardware and chip peripherals for the demo's functionality. */
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
139 void SetupHardware(void)
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
140 {
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
141 /* Disable watchdog if enabled by bootloader/fuses */
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
142 MCUSR &= ~(1 << WDRF);
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
143 wdt_disable();
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
144
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
145 /* Hardware Initialization */
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
146 Serial_Init(9600, false);
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
147 LEDs_Init();
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
148 USB_Init();
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
149
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
150 /* Start the flush timer so that overflows occur rapidly to push received bytes to the USB interface */
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
151 TCCR0B = (1 << CS02);
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
152
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
153 /* Pull target /RESET line high */
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
154 AVR_RESET_LINE_PORT |= AVR_RESET_LINE_MASK;
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
155 AVR_RESET_LINE_DDR |= AVR_RESET_LINE_MASK;
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
156 }
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
157
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
158 /** Event handler for the library USB Configuration Changed event. */
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
159 void EVENT_USB_Device_ConfigurationChanged(void)
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
160 {
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
161 CDC_Device_ConfigureEndpoints(&VirtualSerial_CDC_Interface);
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
162 }
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
163
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
164 /** Event handler for the library USB Unhandled Control Request event. */
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
165 void EVENT_USB_Device_UnhandledControlRequest(void)
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
166 {
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
167 CDC_Device_ProcessControlRequest(&VirtualSerial_CDC_Interface);
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
168 }
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
169
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
170 /** Event handler for the CDC Class driver Line Encoding Changed event.
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
171 *
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
172 * \param[in] CDCInterfaceInfo Pointer to the CDC class interface configuration structure being referenced
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
173 */
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
174 void EVENT_CDC_Device_LineEncodingChanged(USB_ClassInfo_CDC_Device_t* const CDCInterfaceInfo)
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
175 {
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
176 uint8_t ConfigMask = 0;
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
177
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
178 switch (CDCInterfaceInfo->State.LineEncoding.ParityType)
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
179 {
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
180 case CDC_PARITY_Odd:
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
181 ConfigMask = ((1 << UPM11) | (1 << UPM10));
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
182 break;
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
183 case CDC_PARITY_Even:
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
184 ConfigMask = (1 << UPM11);
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
185 break;
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
186 }
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
187
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
188 if (CDCInterfaceInfo->State.LineEncoding.CharFormat == CDC_LINEENCODING_TwoStopBits)
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
189 ConfigMask |= (1 << USBS1);
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
190
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
191 switch (CDCInterfaceInfo->State.LineEncoding.DataBits)
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
192 {
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
193 case 6:
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
194 ConfigMask |= (1 << UCSZ10);
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
195 break;
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
196 case 7:
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
197 ConfigMask |= (1 << UCSZ11);
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
198 break;
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
199 case 8:
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
200 ConfigMask |= ((1 << UCSZ11) | (1 << UCSZ10));
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
201 break;
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
202 }
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
203
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
204 /* Must turn off USART before reconfiguring it, otherwise incorrect operation may occur */
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
205 UCSR1B = 0;
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
206 UCSR1A = 0;
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
207 UCSR1C = 0;
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
208
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
209 /* Special case 57600 baud for compatibility with the ATmega328 bootloader. */
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
210 UBRR1 = (CDCInterfaceInfo->State.LineEncoding.BaudRateBPS == 57600)
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
211 ? SERIAL_UBBRVAL(CDCInterfaceInfo->State.LineEncoding.BaudRateBPS)
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
212 : SERIAL_2X_UBBRVAL(CDCInterfaceInfo->State.LineEncoding.BaudRateBPS);
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
213
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
214 UCSR1C = ConfigMask;
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
215 UCSR1A = (CDCInterfaceInfo->State.LineEncoding.BaudRateBPS == 57600) ? 0 : (1 << U2X1);
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
216 UCSR1B = ((1 << RXCIE1) | (1 << TXEN1) | (1 << RXEN1));
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
217 }
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
218
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
219 /** ISR to manage the reception of data from the serial port, placing received bytes into a circular buffer
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
220 * for later transmission to the host.
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
221 */
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
222 ISR(USART1_RX_vect, ISR_BLOCK)
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
223 {
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
224 uint8_t ReceivedByte = UDR1;
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
225
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
226 if (USB_DeviceState == DEVICE_STATE_Configured)
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
227 RingBuffer_Insert(&USARTtoUSB_Buffer, ReceivedByte);
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
228 }
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
229
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
230 /** Event handler for the CDC Class driver Host-to-Device Line Encoding Changed event.
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
231 *
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
232 * \param[in] CDCInterfaceInfo Pointer to the CDC class interface configuration structure being referenced
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
233 */
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
234 void EVENT_CDC_Device_ControLineStateChanged(USB_ClassInfo_CDC_Device_t* const CDCInterfaceInfo)
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
235 {
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
236 bool CurrentDTRState = (CDCInterfaceInfo->State.ControlLineStates.HostToDevice & CDC_CONTROL_LINE_OUT_DTR);
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
237
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
238 if (CurrentDTRState)
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
239 AVR_RESET_LINE_PORT &= ~AVR_RESET_LINE_MASK;
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
240 else
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
241 AVR_RESET_LINE_PORT |= AVR_RESET_LINE_MASK;
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
242 }

mercurial