3 /* Copyright (c) 2011, Peter Barrett
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.
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
21 #include <util/atomic.h>
34 static volatile LineInfo _usbLineInfo = { 57600, 0x00, 0x00, 0x00, 0x00 };
35 static volatile int32_t breakValue = -1;
37 static u8 wdtcsr_save;
39 #define WEAK __attribute__ ((weak))
41 extern const CDCDescriptor _cdcInterface PROGMEM;
42 const CDCDescriptor _cdcInterface =
44 D_IAD(0,2,CDC_COMMUNICATION_INTERFACE_CLASS,CDC_ABSTRACT_CONTROL_MODEL,0),
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),
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)
60 bool isLUFAbootloader()
62 return pgm_read_word(FLASHEND - 1) == NEW_LUFA_SIGNATURE;
65 int CDC_GetInterface(u8* interfaceNum)
67 interfaceNum[0] += 2; // uses 2
68 return USB_SendControl(TRANSFER_PGM,&_cdcInterface,sizeof(_cdcInterface));
71 bool CDC_Setup(USBSetup& setup)
73 u8 r = setup.bRequest;
74 u8 requestType = setup.bmRequestType;
76 if (REQUEST_DEVICETOHOST_CLASS_INTERFACE == requestType)
78 if (CDC_GET_LINE_CODING == r)
80 USB_SendControl(0,(void*)&_usbLineInfo,7);
85 if (REQUEST_HOSTTODEVICE_CLASS_INTERFACE == requestType)
87 if (CDC_SEND_BREAK == r)
89 breakValue = ((uint16_t)setup.wValueH << 8) | setup.wValueL;
92 if (CDC_SET_LINE_CODING == r)
94 USB_RecvControl((void*)&_usbLineInfo,7);
97 if (CDC_SET_CONTROL_LINE_STATE == r)
99 _usbLineInfo.lineState = setup.wValueL;
101 // auto-reset into the bootloader is triggered when the port, already
102 // open at 1200 bps, is closed. this is the signal to start the watchdog
103 // with a relatively long period so it can finish housekeeping tasks
104 // like servicing endpoints before the sketch ends
106 uint16_t magic_key_pos = MAGIC_KEY_POS;
108 // If we don't use the new RAMEND directly, check manually if we have a newer bootloader.
109 // This is used to keep compatible with the old leonardo bootloaders.
110 // You are still able to set the magic key position manually to RAMEND-1 to save a few bytes for this check.
111 #if MAGIC_KEY_POS != (RAMEND-1)
112 // For future boards save the key in the inproblematic RAMEND
113 // Which is reserved for the main() return value (which will never return)
114 if (isLUFAbootloader()) {
115 // horray, we got a new bootloader!
116 magic_key_pos = (RAMEND-1);
120 // We check DTR state to determine if host port is open (bit 0 of lineState).
121 if (1200 == _usbLineInfo.dwDTERate && (_usbLineInfo.lineState & 0x01) == 0)
123 #if MAGIC_KEY_POS != (RAMEND-1)
124 // Backup ram value if its not a newer bootloader and it hasn't already been saved.
125 // This should avoid memory corruption at least a bit, not fully
126 if (magic_key_pos != (RAMEND-1) && *(uint16_t *)magic_key_pos != MAGIC_KEY) {
127 *(uint16_t *)(RAMEND-1) = *(uint16_t *)magic_key_pos;
131 *(uint16_t *)magic_key_pos = MAGIC_KEY;
132 // Save the watchdog state in case the reset is aborted.
133 wdtcsr_save = WDTCSR;
134 wdt_enable(WDTO_120MS);
136 else if (*(uint16_t *)magic_key_pos == MAGIC_KEY)
138 // Most OSs do some intermediate steps when configuring ports and DTR can
139 // twiggle more than once before stabilizing.
140 // To avoid spurious resets we set the watchdog to 120ms and eventually
141 // cancel if DTR goes back high.
142 // Cancellation is only done if an auto-reset was started, which is
143 // indicated by the magic key having been set.
146 // Restore the watchdog state in case the sketch was using it.
147 WDTCSR |= (1<<WDCE) | (1<<WDE);
148 WDTCSR = wdtcsr_save;
149 #if MAGIC_KEY_POS != (RAMEND-1)
150 // Restore backed up (old bootloader) magic key data
151 if (magic_key_pos != (RAMEND-1)) {
152 *(uint16_t *)magic_key_pos = *(uint16_t *)(RAMEND-1);
156 // Clean up RAMEND key
157 *(uint16_t *)magic_key_pos = 0x0000;
167 void Serial_::begin(unsigned long /* baud_count */)
172 void Serial_::begin(unsigned long /* baud_count */, byte /* config */)
177 void Serial_::end(void)
181 int Serial_::available(void)
183 if (peek_buffer >= 0) {
184 return 1 + USB_Available(CDC_RX);
186 return USB_Available(CDC_RX);
189 int Serial_::peek(void)
192 peek_buffer = USB_Recv(CDC_RX);
196 int Serial_::read(void)
198 if (peek_buffer >= 0) {
203 return USB_Recv(CDC_RX);
206 int Serial_::availableForWrite(void)
208 return USB_SendSpace(CDC_TX);
211 void Serial_::flush(void)
216 size_t Serial_::write(uint8_t c)
221 size_t Serial_::write(const uint8_t *buffer, size_t size)
223 /* only try to send bytes if the high-level CDC connection itself
224 is open (not just the pipe) - the OS should set lineState when the port
225 is opened and clear lineState when the port is closed.
226 bytes sent before the user opens the connection or after
227 the connection is closed are lost - just like with a UART. */
229 // TODO - ZE - check behavior on different OSes and test what happens if an
230 // open connection isn't broken cleanly (cable is yanked out, host dies
231 // or locks up, or host virtual serial port hangs)
232 if (_usbLineInfo.lineState > 0) {
233 int r = USB_Send(CDC_TX,buffer,size);
245 // This operator is a convenient way for a sketch to check whether the
246 // port has actually been configured and opened by the host (as opposed
247 // to just being connected to the host). It can be used, for example, in
248 // setup() before printing to ensure that an application on the host is
249 // actually ready to receive and display the data.
250 // We add a short delay before returning to fix a bug observed by Federico
251 // where the port is configured (lineState != 0) but not quite opened.
252 Serial_::operator bool() {
254 if (_usbLineInfo.lineState > 0)
260 unsigned long Serial_::baud() {
261 // Disable interrupts while reading a multi-byte value
263 ATOMIC_BLOCK(ATOMIC_RESTORESTATE) {
264 baudrate = _usbLineInfo.dwDTERate;
269 uint8_t Serial_::stopbits() {
270 return _usbLineInfo.bCharFormat;
273 uint8_t Serial_::paritytype() {
274 return _usbLineInfo.bParityType;
277 uint8_t Serial_::numbits() {
278 return _usbLineInfo.bDataBits;
281 bool Serial_::dtr() {
282 return _usbLineInfo.lineState & 0x1;
285 bool Serial_::rts() {
286 return _usbLineInfo.lineState & 0x2;
289 int32_t Serial_::readBreak() {
291 // Disable IRQs while reading and clearing breakValue to make
292 // sure we don't overwrite a value just set by the ISR.
293 ATOMIC_BLOCK(ATOMIC_RESTORESTATE) {
302 #endif /* if defined(USBCON) */