CDC.cpp
Go to the documentation of this file.
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 typedef struct
26 {
27  u32 dwDTERate;
28  u8 bCharFormat;
29  u8 bParityType;
30  u8 bDataBits;
31  u8 lineState;
32 } LineInfo;
33 
34 static volatile LineInfo _usbLineInfo = { 57600, 0x00, 0x00, 0x00, 0x00 };
35 static volatile int32_t breakValue = -1;
36 
37 bool _updatedLUFAbootloader = false;
38 
39 #define WEAK __attribute__ ((weak))
40 
41 extern const CDCDescriptor _cdcInterface PROGMEM;
42 const CDCDescriptor _cdcInterface =
43 {
45 
46  // CDC communication interface
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
53 
54  // CDC data interface
58 };
59 
60 int CDC_GetInterface(u8* interfaceNum)
61 {
62  interfaceNum[0] += 2; // uses 2
63  return USB_SendControl(TRANSFER_PGM,&_cdcInterface,sizeof(_cdcInterface));
64 }
65 
66 bool 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 
93  {
94  _usbLineInfo.lineState = setup.wValueL;
95  }
96 
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 
159 void Serial_::begin(unsigned long /* baud_count */)
160 {
161  peek_buffer = -1;
162 }
163 
164 void Serial_::begin(unsigned long /* baud_count */, byte /* config */)
165 {
166  peek_buffer = -1;
167 }
168 
169 void Serial_::end(void)
170 {
171 }
172 
173 int 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 
181 int Serial_::peek(void)
182 {
183  if (peek_buffer < 0)
184  peek_buffer = USB_Recv(CDC_RX);
185  return peek_buffer;
186 }
187 
188 int 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 
198 int Serial_::availableForWrite(void)
199 {
200  return USB_SendSpace(CDC_TX);
201 }
202 
203 void Serial_::flush(void)
204 {
205  USB_Flush(CDC_TX);
206 }
207 
208 size_t Serial_::write(uint8_t c)
209 {
210  return write(&c, 1);
211 }
212 
213 size_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.
244 Serial_::operator bool() {
245  bool result = false;
246  if (_usbLineInfo.lineState > 0)
247  result = true;
248  delay(10);
249  return result;
250 }
251 
252 unsigned 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 
261 uint8_t Serial_::stopbits() {
262  return _usbLineInfo.bCharFormat;
263 }
264 
265 uint8_t Serial_::paritytype() {
266  return _usbLineInfo.bParityType;
267 }
268 
269 uint8_t Serial_::numbits() {
270  return _usbLineInfo.bDataBits;
271 }
272 
273 bool Serial_::dtr() {
274  return _usbLineInfo.lineState & 0x1;
275 }
276 
277 bool Serial_::rts() {
278  return _usbLineInfo.lineState & 0x2;
279 }
280 
281 int32_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 
292 Serial_ Serial;
293 
294 #endif /* if defined(USBCON) */
#define CDC_CALL_MANAGEMENT
Definition: USBCore.h:117
#define CDC_TX
Definition: USBDesc.h:42
#define CDC_GET_LINE_CODING
Definition: USBCore.h:59
#define D_CDCCS4(_subtype, _d0)
Definition: USBCore.h:282
const GLfloat * c
#define CDC_ABSTRACT_CONTROL_MANAGEMENT
Definition: USBCore.h:120
#define USB_ENDPOINT_TYPE_BULK
Definition: USBCore.h:109
#define CDC_DATA_INTERFACE_CLASS
Definition: USBCore.h:124
unsigned int uint32_t
#define CDC_HEADER
Definition: USBCore.h:119
#define CDC_ACM_INTERFACE
Definition: USBDesc.h:32
unsigned long u32
Definition: USBAPI.h:31
#define D_ENDPOINT(_addr, _attr, _packetSize, _interval)
Definition: USBCore.h:275
unsigned char u8
Definition: USBAPI.h:29
GLuint buffer
#define REQUEST_HOSTTODEVICE_CLASS_INTERFACE
Definition: USBCore.h:53
#define CDC_SET_LINE_CODING
Definition: USBCore.h:58
#define CDC_ABSTRACT_CONTROL_MODEL
Definition: USBCore.h:118
#define CDC_ENDPOINT_IN
Definition: USBDesc.h:37
void delay(unsigned long)
Definition: wiring.c:106
#define CDC_COMMUNICATION_INTERFACE_CLASS
Definition: USBCore.h:115
GLsizeiptr size
#define MAGIC_KEY_POS
Definition: USBCore.h:295
#define CDC_DATA_INTERFACE
Definition: USBDesc.h:33
#define CDC_ENDPOINT_ACM
Definition: USBDesc.h:35
#define D_CDCCS(_subtype, _d0, _d1)
Definition: USBCore.h:281
#define REQUEST_DEVICETOHOST_CLASS_INTERFACE
Definition: USBCore.h:52
#define CDC_UNION
Definition: USBCore.h:121
#define CDC_SET_CONTROL_LINE_STATE
Definition: USBCore.h:60
#define CDC_RX
Definition: USBDesc.h:41
GLdouble GLdouble GLdouble r
#define USB_ENDPOINT_TYPE_INTERRUPT
Definition: USBCore.h:110
#define CDC_SEND_BREAK
Definition: USBCore.h:61
unsigned char byte
#define D_IAD(_firstInterface, _count, _class, _subClass, _protocol)
Definition: USBCore.h:278
#define MAGIC_KEY
Definition: USBCore.h:291
#define USB_EP_SIZE
Definition: USBAPI.h:38
#define USB_ENDPOINT_OUT(addr)
Definition: USBCore.h:103
signed int int32_t
#define CDC_ENDPOINT_OUT
Definition: USBDesc.h:36
#define D_INTERFACE(_n, _numEndpoints, _class, _subClass, _protocol)
Definition: USBCore.h:272
#define USB_ENDPOINT_IN(addr)
Definition: USBCore.h:104


arduino_daq
Author(s):
autogenerated on Mon Jun 10 2019 12:46:03