Debian Codegraph @

cores/arduino/CDC.cpp

raw
1
2
3 /* Copyright (c) 2011, Peter Barrett
4 **
5 ** Permission to use, copy, modify, and/or distribute this software for
6 ** any purpose with or without fee is hereby granted, provided that the
7 ** above copyright notice and this permission notice appear in all copies.
8 **
9 ** THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL
10 ** WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED
11 ** WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR
12 ** BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES
13 ** OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS,
14 ** WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION,
15 ** ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
16 ** SOFTWARE.
17 */
18
19 #include "USBAPI.h"
20 #include <avr/wdt.h>
21 #include <util/atomic.h>
22
23 #if defined(USBCON)
24
25 #ifndef CDC_ENABLED
26
27 #warning "! Disabled serial console via USB (CDC)!"
28 #warning "! With this change you'll have to use the Arduino's reset button/pin to flash (upload)!"
29
30 #else // CDC not disabled
31
32 typedef struct
33 {
34 u32 dwDTERate;
35 u8 bCharFormat;
36 u8 bParityType;
37 u8 bDataBits;
38 u8 lineState;
39 } LineInfo;
40
41 static volatile LineInfo _usbLineInfo = { 57600, 0x00, 0x00, 0x00, 0x00 };
42 static volatile int32_t breakValue = -1;
43
44 static u8 wdtcsr_save;
45
46 #define WEAK __attribute__ ((weak))
47
48 extern const CDCDescriptor _cdcInterface PROGMEM;
49 const CDCDescriptor _cdcInterface =
50 {
51 D_IAD(0,2,CDC_COMMUNICATION_INTERFACE_CLASS,CDC_ABSTRACT_CONTROL_MODEL,0),
52
53 // CDC communication interface
54 D_INTERFACE(CDC_ACM_INTERFACE,1,CDC_COMMUNICATION_INTERFACE_CLASS,CDC_ABSTRACT_CONTROL_MODEL,0),
55 D_CDCCS(CDC_HEADER,0x10,0x01), // Header (1.10 bcd)
56 D_CDCCS(CDC_CALL_MANAGEMENT,1,1), // Device handles call management (not)
57 D_CDCCS4(CDC_ABSTRACT_CONTROL_MANAGEMENT,6), // SET_LINE_CODING, GET_LINE_CODING, SET_CONTROL_LINE_STATE supported
58 D_CDCCS(CDC_UNION,CDC_ACM_INTERFACE,CDC_DATA_INTERFACE), // Communication interface is master, data interface is slave 0
59 D_ENDPOINT(USB_ENDPOINT_IN (CDC_ENDPOINT_ACM),USB_ENDPOINT_TYPE_INTERRUPT,0x10,0x40),
60
61 // CDC data interface
62 D_INTERFACE(CDC_DATA_INTERFACE,2,CDC_DATA_INTERFACE_CLASS,0,0),
63 D_ENDPOINT(USB_ENDPOINT_OUT(CDC_ENDPOINT_OUT),USB_ENDPOINT_TYPE_BULK,USB_EP_SIZE,0),
64 D_ENDPOINT(USB_ENDPOINT_IN (CDC_ENDPOINT_IN ),USB_ENDPOINT_TYPE_BULK,USB_EP_SIZE,0)
65 };
66
67 bool isLUFAbootloader()
68 {
69 return pgm_read_word(FLASHEND - 1) == NEW_LUFA_SIGNATURE;
70 }
71
72 int CDC_GetInterface(u8* interfaceNum)
73 {
74 interfaceNum[0] += 2; // uses 2
75 return USB_SendControl(TRANSFER_PGM,&_cdcInterface,sizeof(_cdcInterface));
76 }
77
78 bool CDC_Setup(USBSetup& setup)
79 {
80 u8 r = setup.bRequest;
81 u8 requestType = setup.bmRequestType;
82
83 if (REQUEST_DEVICETOHOST_CLASS_INTERFACE == requestType)
84 {
85 if (CDC_GET_LINE_CODING == r)
86 {
87 USB_SendControl(0,(void*)&_usbLineInfo,7);
88 return true;
89 }
90 }
91
92 if (REQUEST_HOSTTODEVICE_CLASS_INTERFACE == requestType)
93 {
94 if (CDC_SEND_BREAK == r)
95 {
96 breakValue = ((uint16_t)setup.wValueH << 8) | setup.wValueL;
97 }
98
99 if (CDC_SET_LINE_CODING == r)
100 {
101 USB_RecvControl((void*)&_usbLineInfo,7);
102 }
103
104 if (CDC_SET_CONTROL_LINE_STATE == r)
105 {
106 _usbLineInfo.lineState = setup.wValueL;
107
108 // auto-reset into the bootloader is triggered when the port, already
109 // open at 1200 bps, is closed. this is the signal to start the watchdog
110 // with a relatively long period so it can finish housekeeping tasks
111 // like servicing endpoints before the sketch ends
112
113 uint16_t magic_key_pos = MAGIC_KEY_POS;
114
115 // If we don't use the new RAMEND directly, check manually if we have a newer bootloader.
116 // This is used to keep compatible with the old leonardo bootloaders.
117 // You are still able to set the magic key position manually to RAMEND-1 to save a few bytes for this check.
118 #if MAGIC_KEY_POS != (RAMEND-1)
119 // For future boards save the key in the inproblematic RAMEND
120 // Which is reserved for the main() return value (which will never return)
121 if (isLUFAbootloader()) {
122 // hooray, we got a new bootloader!
123 magic_key_pos = (RAMEND-1);
124 }
125 #endif
126
127 // We check DTR state to determine if host port is open (bit 0 of lineState).
128 if (1200 == _usbLineInfo.dwDTERate && (_usbLineInfo.lineState & 0x01) == 0)
129 {
130 #if MAGIC_KEY_POS != (RAMEND-1)
131 // Backup ram value if its not a newer bootloader and it hasn't already been saved.
132 // This should avoid memory corruption at least a bit, not fully
133 if (magic_key_pos != (RAMEND-1) && *(uint16_t *)magic_key_pos != MAGIC_KEY) {
134 *(uint16_t *)(RAMEND-1) = *(uint16_t *)magic_key_pos;
135 }
136 #endif
137 // Store boot key
138 *(uint16_t *)magic_key_pos = MAGIC_KEY;
139 // Save the watchdog state in case the reset is aborted.
140 wdtcsr_save = WDTCSR;
141 wdt_enable(WDTO_120MS);
142 }
143 else if (*(uint16_t *)magic_key_pos == MAGIC_KEY)
144 {
145 // Most OSs do some intermediate steps when configuring ports and DTR can
146 // twiggle more than once before stabilizing.
147 // To avoid spurious resets we set the watchdog to 120ms and eventually
148 // cancel if DTR goes back high.
149 // Cancellation is only done if an auto-reset was started, which is
150 // indicated by the magic key having been set.
151
152 wdt_reset();
153 // Restore the watchdog state in case the sketch was using it.
154 WDTCSR |= (1<<WDCE) | (1<<WDE);
155 WDTCSR = wdtcsr_save;
156 #if MAGIC_KEY_POS != (RAMEND-1)
157 // Restore backed up (old bootloader) magic key data
158 if (magic_key_pos != (RAMEND-1)) {
159 *(uint16_t *)magic_key_pos = *(uint16_t *)(RAMEND-1);
160 } else
161 #endif
162 {
163 // Clean up RAMEND key
164 *(uint16_t *)magic_key_pos = 0x0000;
165 }
166 }
167 }
168 return true;
169 }
170 return false;
171 }
172
173
174 void Serial_::begin(unsigned long /* baud_count */)
175 {
176 peek_buffer = -1;
177 }
178
179 void Serial_::begin(unsigned long /* baud_count */, byte /* config */)
180 {
181 peek_buffer = -1;
182 }
183
184 void Serial_::end(void)
185 {
186 }
187
188 int Serial_::available(void)
189 {
190 if (peek_buffer >= 0) {
191 return 1 + USB_Available(CDC_RX);
192 }
193 return USB_Available(CDC_RX);
194 }
195
196 int Serial_::peek(void)
197 {
198 if (peek_buffer < 0)
199 peek_buffer = USB_Recv(CDC_RX);
200 return peek_buffer;
201 }
202
203 int Serial_::read(void)
204 {
205 if (peek_buffer >= 0) {
206 int c = peek_buffer;
207 peek_buffer = -1;
208 return c;
209 }
210 return USB_Recv(CDC_RX);
211 }
212
213 int Serial_::availableForWrite(void)
214 {
215 return USB_SendSpace(CDC_TX);
216 }
217
218 void Serial_::flush(void)
219 {
220 USB_Flush(CDC_TX);
221 }
222
223 size_t Serial_::write(uint8_t c)
224 {
225 return write(&c, 1);
226 }
227
228 size_t Serial_::write(const uint8_t *buffer, size_t size)
229 {
230 /* only try to send bytes if the high-level CDC connection itself
231 is open (not just the pipe) - the OS should set lineState when the port
232 is opened and clear lineState when the port is closed.
233 bytes sent before the user opens the connection or after
234 the connection is closed are lost - just like with a UART. */
235
236 // TODO - ZE - check behavior on different OSes and test what happens if an
237 // open connection isn't broken cleanly (cable is yanked out, host dies
238 // or locks up, or host virtual serial port hangs)
239 if (_usbLineInfo.lineState > 0) {
240 int r = USB_Send(CDC_TX,buffer,size);
241 if (r > 0) {
242 return r;
243 } else {
244 setWriteError();
245 return 0;
246 }
247 }
248 setWriteError();
249 return 0;
250 }
251
252 // This operator is a convenient way for a sketch to check whether the
253 // port has actually been configured and opened by the host (as opposed
254 // to just being connected to the host). It can be used, for example, in
255 // setup() before printing to ensure that an application on the host is
256 // actually ready to receive and display the data.
257 // We add a short delay before returning to fix a bug observed by Federico
258 // where the port is configured (lineState != 0) but not quite opened.
259 Serial_::operator bool() {
260 bool result = false;
261 if (_usbLineInfo.lineState > 0)
262 result = true;
263 delay(10);
264 return result;
265 }
266
267 unsigned long Serial_::baud() {
268 // Disable interrupts while reading a multi-byte value
269 uint32_t baudrate;
270 ATOMIC_BLOCK(ATOMIC_RESTORESTATE) {
271 baudrate = _usbLineInfo.dwDTERate;
272 }
273 return baudrate;
274 }
275
276 uint8_t Serial_::stopbits() {
277 return _usbLineInfo.bCharFormat;
278 }
279
280 uint8_t Serial_::paritytype() {
281 return _usbLineInfo.bParityType;
282 }
283
284 uint8_t Serial_::numbits() {
285 return _usbLineInfo.bDataBits;
286 }
287
288 bool Serial_::dtr() {
289 return _usbLineInfo.lineState & 0x1;
290 }
291
292 bool Serial_::rts() {
293 return _usbLineInfo.lineState & 0x2;
294 }
295
296 int32_t Serial_::readBreak() {
297 int32_t ret;
298 // Disable IRQs while reading and clearing breakValue to make
299 // sure we don't overwrite a value just set by the ISR.
300 ATOMIC_BLOCK(ATOMIC_RESTORESTATE) {
301 ret = breakValue;
302 breakValue = -1;
303 }
304 return ret;
305 }
306
307 Serial_ Serial;
308
309 #endif /* if defined(CDC_ENABLED) */
310 #endif /* if defined(USBCON) */