source: arduino-1-6-7/trunk/fuentes/arduino-ide-amd64/hardware/arduino/avr/cores/arduino/CDC.cpp @ 4837

Last change on this file since 4837 was 4837, checked in by daduve, 2 years ago

Adding new version

File size: 7.9 KB
Line 
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
25typedef struct
26{
27        u32     dwDTERate;
28        u8      bCharFormat;
29        u8      bParityType;
30        u8      bDataBits;
31        u8      lineState;
32} LineInfo;
33
34static volatile LineInfo _usbLineInfo = { 57600, 0x00, 0x00, 0x00, 0x00 };
35static volatile int32_t breakValue = -1;
36
37bool _updatedLUFAbootloader = false;
38
39#define WEAK __attribute__ ((weak))
40
41extern const CDCDescriptor _cdcInterface PROGMEM;
42const CDCDescriptor _cdcInterface =
43{
44        D_IAD(0,2,CDC_COMMUNICATION_INTERFACE_CLASS,CDC_ABSTRACT_CONTROL_MODEL,1),
45
46        //      CDC communication interface
47        D_INTERFACE(CDC_ACM_INTERFACE,1,CDC_COMMUNICATION_INTERFACE_CLASS,CDC_ABSTRACT_CONTROL_MODEL,0),
48        D_CDCCS(CDC_HEADER,0x10,0x01),                                                          // Header (1.10 bcd)
49        D_CDCCS(CDC_CALL_MANAGEMENT,1,1),                                                       // Device handles call management (not)
50        D_CDCCS4(CDC_ABSTRACT_CONTROL_MANAGEMENT,6),                            // SET_LINE_CODING, GET_LINE_CODING, SET_CONTROL_LINE_STATE supported
51        D_CDCCS(CDC_UNION,CDC_ACM_INTERFACE,CDC_DATA_INTERFACE),        // Communication interface is master, data interface is slave 0
52        D_ENDPOINT(USB_ENDPOINT_IN (CDC_ENDPOINT_ACM),USB_ENDPOINT_TYPE_INTERRUPT,0x10,0x40),
53
54        //      CDC data interface
55        D_INTERFACE(CDC_DATA_INTERFACE,2,CDC_DATA_INTERFACE_CLASS,0,0),
56        D_ENDPOINT(USB_ENDPOINT_OUT(CDC_ENDPOINT_OUT),USB_ENDPOINT_TYPE_BULK,USB_EP_SIZE,0),
57        D_ENDPOINT(USB_ENDPOINT_IN (CDC_ENDPOINT_IN ),USB_ENDPOINT_TYPE_BULK,USB_EP_SIZE,0)
58};
59
60int CDC_GetInterface(u8* interfaceNum)
61{
62        interfaceNum[0] += 2;   // uses 2
63        return USB_SendControl(TRANSFER_PGM,&_cdcInterface,sizeof(_cdcInterface));
64}
65
66bool CDC_Setup(USBSetup& setup)
67{
68        u8 r = setup.bRequest;
69        u8 requestType = setup.bmRequestType;
70
71        if (REQUEST_DEVICETOHOST_CLASS_INTERFACE == requestType)
72        {
73                if (CDC_GET_LINE_CODING == r)
74                {
75                        USB_SendControl(0,(void*)&_usbLineInfo,7);
76                        return true;
77                }
78        }
79
80        if (REQUEST_HOSTTODEVICE_CLASS_INTERFACE == requestType)
81        {
82                if (CDC_SEND_BREAK == r)
83                {
84                        breakValue = ((uint16_t)setup.wValueH << 8) | setup.wValueL;
85                }
86
87                if (CDC_SET_LINE_CODING == r)
88                {
89                        USB_RecvControl((void*)&_usbLineInfo,7);
90                }
91
92                if (CDC_SET_CONTROL_LINE_STATE == r)
93                {
94                        _usbLineInfo.lineState = setup.wValueL;
95                }
96
97                if (CDC_SET_LINE_CODING == r || CDC_SET_CONTROL_LINE_STATE == r)
98                {
99                        // auto-reset into the bootloader is triggered when the port, already
100                        // open at 1200 bps, is closed.  this is the signal to start the watchdog
101                        // with a relatively long period so it can finish housekeeping tasks
102                        // like servicing endpoints before the sketch ends
103
104                        uint16_t magic_key_pos = MAGIC_KEY_POS;
105
106// If we don't use the new RAMEND directly, check manually if we have a newer bootloader.
107// This is used to keep compatible with the old leonardo bootloaders.
108// You are still able to set the magic key position manually to RAMEND-1 to save a few bytes for this check.
109#if MAGIC_KEY_POS != (RAMEND-1)
110                        // For future boards save the key in the inproblematic RAMEND
111                        // Which is reserved for the main() return value (which will never return)
112                        if (_updatedLUFAbootloader) {
113                                // horray, we got a new bootloader!
114                                magic_key_pos = (RAMEND-1);
115                        }
116#endif
117
118                        // We check DTR state to determine if host port is open (bit 0 of lineState).
119                        if (1200 == _usbLineInfo.dwDTERate && (_usbLineInfo.lineState & 0x01) == 0)
120                        {
121#if MAGIC_KEY_POS != (RAMEND-1)
122                                // Backup ram value if its not a newer bootloader.
123                                // This should avoid memory corruption at least a bit, not fully
124                                if (magic_key_pos != (RAMEND-1)) {
125                                        *(uint16_t *)(RAMEND-1) = *(uint16_t *)magic_key_pos;
126                                }
127#endif
128                                // Store boot key
129                                *(uint16_t *)magic_key_pos = MAGIC_KEY;
130                                wdt_enable(WDTO_120MS);
131                        }
132                        else
133                        {
134                                // Most OSs do some intermediate steps when configuring ports and DTR can
135                                // twiggle more than once before stabilizing.
136                                // To avoid spurious resets we set the watchdog to 250ms and eventually
137                                // cancel if DTR goes back high.
138
139                                wdt_disable();
140                                wdt_reset();
141#if MAGIC_KEY_POS != (RAMEND-1)
142                                // Restore backed up (old bootloader) magic key data
143                                if (magic_key_pos != (RAMEND-1)) {
144                                        *(uint16_t *)magic_key_pos = *(uint16_t *)(RAMEND-1);
145                                } else
146#endif
147                                {
148                                // Clean up RAMEND key
149                                        *(uint16_t *)magic_key_pos = 0x0000;
150                                }
151                        }
152                }
153                return true;
154        }
155        return false;
156}
157
158
159void Serial_::begin(unsigned long /* baud_count */)
160{
161        peek_buffer = -1;
162}
163
164void Serial_::begin(unsigned long /* baud_count */, byte /* config */)
165{
166        peek_buffer = -1;
167}
168
169void Serial_::end(void)
170{
171}
172
173int Serial_::available(void)
174{
175        if (peek_buffer >= 0) {
176                return 1 + USB_Available(CDC_RX);
177        }
178        return USB_Available(CDC_RX);
179}
180
181int Serial_::peek(void)
182{
183        if (peek_buffer < 0)
184                peek_buffer = USB_Recv(CDC_RX);
185        return peek_buffer;
186}
187
188int Serial_::read(void)
189{
190        if (peek_buffer >= 0) {
191                int c = peek_buffer;
192                peek_buffer = -1;
193                return c;
194        }
195        return USB_Recv(CDC_RX);
196}
197
198int Serial_::availableForWrite(void)
199{
200        return USB_SendSpace(CDC_TX);
201}
202
203void Serial_::flush(void)
204{
205        USB_Flush(CDC_TX);
206}
207
208size_t Serial_::write(uint8_t c)
209{
210        return write(&c, 1);
211}
212
213size_t Serial_::write(const uint8_t *buffer, size_t size)
214{
215        /* only try to send bytes if the high-level CDC connection itself
216         is open (not just the pipe) - the OS should set lineState when the port
217         is opened and clear lineState when the port is closed.
218         bytes sent before the user opens the connection or after
219         the connection is closed are lost - just like with a UART. */
220       
221        // TODO - ZE - check behavior on different OSes and test what happens if an
222        // open connection isn't broken cleanly (cable is yanked out, host dies
223        // or locks up, or host virtual serial port hangs)
224        if (_usbLineInfo.lineState > 0) {
225                int r = USB_Send(CDC_TX,buffer,size);
226                if (r > 0) {
227                        return r;
228                } else {
229                        setWriteError();
230                        return 0;
231                }
232        }
233        setWriteError();
234        return 0;
235}
236
237// This operator is a convenient way for a sketch to check whether the
238// port has actually been configured and opened by the host (as opposed
239// to just being connected to the host).  It can be used, for example, in
240// setup() before printing to ensure that an application on the host is
241// actually ready to receive and display the data.
242// We add a short delay before returning to fix a bug observed by Federico
243// where the port is configured (lineState != 0) but not quite opened.
244Serial_::operator bool() {
245        bool result = false;
246        if (_usbLineInfo.lineState > 0) 
247                result = true;
248        delay(10);
249        return result;
250}
251
252unsigned long Serial_::baud() {
253        // Disable interrupts while reading a multi-byte value
254        uint32_t baudrate;
255        ATOMIC_BLOCK(ATOMIC_RESTORESTATE) {
256                baudrate =  _usbLineInfo.dwDTERate;
257        }
258        return baudrate;
259}
260
261uint8_t Serial_::stopbits() {
262        return _usbLineInfo.bCharFormat;
263}
264
265uint8_t Serial_::paritytype() {
266        return _usbLineInfo.bParityType;
267}
268
269uint8_t Serial_::numbits() {
270        return _usbLineInfo.bDataBits;
271}
272
273bool Serial_::dtr() {
274        return _usbLineInfo.lineState & 0x1;
275}
276
277bool Serial_::rts() {
278        return _usbLineInfo.lineState & 0x2;
279}
280
281int32_t Serial_::readBreak() {
282        int32_t ret;
283        // Disable IRQs while reading and clearing breakValue to make
284        // sure we don't overwrite a value just set by the ISR.
285        ATOMIC_BLOCK(ATOMIC_RESTORESTATE) {
286                ret = breakValue;
287                breakValue = -1;
288        }
289        return ret;
290}
291
292Serial_ Serial;
293
294#endif /* if defined(USBCON) */
Note: See TracBrowser for help on using the repository browser.