-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfast-usbserial.c
203 lines (175 loc) · 6.08 KB
/
fast-usbserial.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
/* Based on arduino-usbserial and LUFA. but made
* into a stand-alone LUFA subset and heavily modified
* by Urja Rannikko 2015. My modifications are under the LUFA
* License below. */
/*
LUFA Library
Copyright (C) Dean Camera, 2010.
dean [at] fourwalledcubicle [dot] com
www.fourwalledcubicle.com
*/
/*
Copyright 2010 Dean Camera (dean [at] fourwalledcubicle [dot] com)
Permission to use, copy, modify, distribute, and sell this
software and its documentation for any purpose is hereby granted
without fee, provided that the above copyright notice appear in
all copies and that both that the copyright notice and this
permission notice and warranty disclaimer appear in supporting
documentation, and that the name of the author not be used in
advertising or publicity pertaining to distribution of the
software without specific, written prior permission.
The author disclaim all warranties with regard to this
software, including all implied warranties of merchantability
and fitness. In no event shall the author be liable for any
special, indirect or consequential damages or any damages
whatsoever resulting from loss of use, data or profits, whether
in an action of contract, negligence or other tortious action,
arising out of or in connection with the use or performance of
this software.
*/
#include "main.h"
#include "fast-usbserial.h"
/* We implement the old uart.h API for libfrser */
#include "uart.h"
USB_ClassInfo_CDC_Device_t VirtualSerial_CDC_Interface;
static uint8_t usb_rxpacket_leftb = 0;
static uint8_t usb_txpacket_leftb = 0;
static void usb_process(void) {
Endpoint_SelectEndpoint(ENDPOINT_CONTROLEP);
do {
if (Endpoint_IsSETUPReceived())
USB_Device_ProcessControlRequest();
} while (USB_DeviceState != DEVICE_STATE_Configured);
}
uint8_t uart_recv(void) {
do {
if (usb_rxpacket_leftb) {
uint8_t d;
Endpoint_SelectEndpoint(CDC_RX_EPNUM);
d = Endpoint_Read_Byte();
usb_rxpacket_leftb--;
if (!usb_rxpacket_leftb)
Endpoint_ClearOUT();
return d;
}
usb_process();
usb_rxpacket_leftb = CDC_Device_BytesReceived(&VirtualSerial_CDC_Interface);
if ((!usb_rxpacket_leftb)&&(usb_txpacket_leftb)) {
Endpoint_SelectEndpoint(CDC_TX_EPNUM);
Endpoint_ClearIN(); /* Go data, GO. */
usb_txpacket_leftb = 0;
}
} while (1);
}
void uart_send(uint8_t d) {
do {
if (usb_txpacket_leftb) {
Endpoint_SelectEndpoint(CDC_TX_EPNUM);
Endpoint_Write_Byte(d);
usb_txpacket_leftb--;
if (usb_txpacket_leftb == 1) {
Endpoint_ClearIN(); /* Go data, GO. */
usb_txpacket_leftb = 0;
}
return;
}
usb_process();
if (CDC_Device_SendByte_Prep(&VirtualSerial_CDC_Interface) == 0)
usb_txpacket_leftb = CDC_IN_EPSIZE;
} while (1);
}
uint8_t uart_isdata(void) {
if (usb_rxpacket_leftb) {
Endpoint_SelectEndpoint(CDC_RX_EPNUM);
return usb_rxpacket_leftb;
}
usb_process();
usb_rxpacket_leftb = CDC_Device_BytesReceived(&VirtualSerial_CDC_Interface);
return usb_rxpacket_leftb;
}
uint8_t uart_send_getfree(void) {
if (usb_txpacket_leftb) {
Endpoint_SelectEndpoint(CDC_TX_EPNUM);
return usb_txpacket_leftb-1;
}
do {
usb_process();
if (CDC_Device_SendByte_Prep(&VirtualSerial_CDC_Interface) == 0) {
usb_txpacket_leftb = CDC_IN_EPSIZE;
return usb_txpacket_leftb-1;
}
} while (1);
}
void uart_wait_txdone(void) {
if (usb_txpacket_leftb) {
Endpoint_SelectEndpoint(CDC_TX_EPNUM);
Endpoint_ClearIN(); /* Go data, GO. */
usb_txpacket_leftb = 0;
}
}
/* These are to be used very responsibly by code that knows what its doing. */
void uart_recv_ctrl_cnt(uint8_t b) {
usb_rxpacket_leftb -= b;
if (!usb_rxpacket_leftb)
Endpoint_ClearOUT();
}
void uart_send_ctrl_cnt(uint8_t b) {
usb_txpacket_leftb -= b;
if (usb_txpacket_leftb == 1) {
Endpoint_ClearIN(); /* Go data, GO. */
usb_txpacket_leftb = 0;
}
}
/** Configures the board hardware and chip peripherals for the demo's functionality. */
void SetupHardware(void)
{
/* Disable watchdog if enabled by bootloader/fuses */
MCUSR &= ~(1 << WDRF);
wdt_disable();
/* Hardware Initialization */
USB_Init();
}
/** Event handler for the library USB Configuration Changed event. */
void EVENT_USB_Device_ConfigurationChanged(void)
{
CDC_Device_ConfigureEndpoints(&VirtualSerial_CDC_Interface);
}
/** Event handler for the library USB Unhandled Control Request event. */
void EVENT_USB_Device_UnhandledControlRequest(void)
{
CDC_Device_ProcessControlRequest(&VirtualSerial_CDC_Interface);
}
static void bl_1200_check(void) {
#if (defined __AVR_ATmega16U4__) || (defined __AVR_ATmega32U4__)
// We check DTR state to determine if host port is open (bit 0 of lineState).
if ((VirtualSerial_CDC_Interface.State.LineEncoding.BaudRateBPS == 1200) && (VirtualSerial_CDC_Interface.State.ControlLineStates.HostToDevice & 0x01) == 0)
{
/* This has to be the stupidest thing i've heard, but we'll live with it, i guess. *sigh* */
/* Btw it means: do not use more than 1792 bytes of .data+.bss or 766 bytes of stack. geeze. */
*(uint16_t *)0x0800 = 0x7777;
wdt_enable(WDTO_120MS);
}
else
{
wdt_disable();
wdt_reset();
*(uint16_t *)0x0800 = 0x0;
}
#endif
}
/** Event handler for the CDC Class driver Line Encoding Changed event.
*
* \param[in] CDCInterfaceInfo Pointer to the CDC class interface configuration structure being referenced
*/
void EVENT_CDC_Device_LineEncodingChanged(USB_ClassInfo_CDC_Device_t* const CDCInterfaceInfo)
{
bl_1200_check();
}
/** Event handler for the CDC Class driver Host-to-Device Line Encoding Changed event.
*
* \param[in] CDCInterfaceInfo Pointer to the CDC class interface configuration structure being referenced
*/
void EVENT_CDC_Device_ControLineStateChanged(USB_ClassInfo_CDC_Device_t* const CDCInterfaceInfo)
{
bl_1200_check();
}