USBCore.cpp
Go to the documentation of this file.
1 
2 
3 /* Copyright (c) 2010, Peter Barrett
4 ** Sleep/Wakeup support added by Michael Dreher
5 **
6 ** Permission to use, copy, modify, and/or distribute this software for
7 ** any purpose with or without fee is hereby granted, provided that the
8 ** above copyright notice and this permission notice appear in all copies.
9 **
10 ** THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL
11 ** WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED
12 ** WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR
13 ** BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES
14 ** OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS,
15 ** WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION,
16 ** ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
17 ** SOFTWARE.
18 */
19 
20 #include "USBAPI.h"
21 #include "PluggableUSB.h"
22 #include <stdlib.h>
23 
24 #if defined(USBCON)
25 
27 #define TX_RX_LED_PULSE_MS 100
28 volatile u8 TxLEDPulse;
29 volatile u8 RxLEDPulse;
31 //==================================================================
32 //==================================================================
33 
34 extern const u16 STRING_LANGUAGE[] PROGMEM;
35 extern const u8 STRING_PRODUCT[] PROGMEM;
36 extern const u8 STRING_MANUFACTURER[] PROGMEM;
37 extern const DeviceDescriptor USB_DeviceDescriptorIAD PROGMEM;
38 extern bool _updatedLUFAbootloader;
39 
40 const u16 STRING_LANGUAGE[2] = {
41  (3<<8) | (2+2),
42  0x0409 // English
43 };
44 
45 #ifndef USB_PRODUCT
46 // If no product is provided, use USB IO Board
47 #define USB_PRODUCT "USB IO Board"
48 #endif
49 
50 const u8 STRING_PRODUCT[] PROGMEM = USB_PRODUCT;
51 
52 #if USB_VID == 0x2341
53 # if defined(USB_MANUFACTURER)
54 # undef USB_MANUFACTURER
55 # endif
56 # define USB_MANUFACTURER "Arduino LLC"
57 #elif USB_VID == 0x1b4f
58 # if defined(USB_MANUFACTURER)
59 # undef USB_MANUFACTURER
60 # endif
61 # define USB_MANUFACTURER "SparkFun"
62 #elif !defined(USB_MANUFACTURER)
63 // Fall through to unknown if no manufacturer name was provided in a macro
64 # define USB_MANUFACTURER "Unknown"
65 #endif
66 
67 const u8 STRING_MANUFACTURER[] PROGMEM = USB_MANUFACTURER;
68 
69 
70 #define DEVICE_CLASS 0x02
71 
72 // DEVICE DESCRIPTOR
73 const DeviceDescriptor USB_DeviceDescriptorIAD =
74  D_DEVICE(0xEF,0x02,0x01,64,USB_VID,USB_PID,0x100,IMANUFACTURER,IPRODUCT,ISERIAL,1);
75 
76 //==================================================================
77 //==================================================================
78 
79 volatile u8 _usbConfiguration = 0;
80 volatile u8 _usbCurrentStatus = 0; // meaning of bits see usb_20.pdf, Figure 9-4. Information Returned by a GetStatus() Request to a Device
81 volatile u8 _usbSuspendState = 0; // copy of UDINT to check SUSPI and WAKEUPI bits
82 
83 static inline void WaitIN(void)
84 {
85  while (!(UEINTX & (1<<TXINI)))
86  ;
87 }
88 
89 static inline void ClearIN(void)
90 {
91  UEINTX = ~(1<<TXINI);
92 }
93 
94 static inline void WaitOUT(void)
95 {
96  while (!(UEINTX & (1<<RXOUTI)))
97  ;
98 }
99 
100 static inline u8 WaitForINOrOUT()
101 {
102  while (!(UEINTX & ((1<<TXINI)|(1<<RXOUTI))))
103  ;
104  return (UEINTX & (1<<RXOUTI)) == 0;
105 }
106 
107 static inline void ClearOUT(void)
108 {
109  UEINTX = ~(1<<RXOUTI);
110 }
111 
112 static inline void Recv(volatile u8* data, u8 count)
113 {
114  while (count--)
115  *data++ = UEDATX;
116 
117  RXLED1; // light the RX LED
118  RxLEDPulse = TX_RX_LED_PULSE_MS;
119 }
120 
121 static inline u8 Recv8()
122 {
123  RXLED1; // light the RX LED
124  RxLEDPulse = TX_RX_LED_PULSE_MS;
125 
126  return UEDATX;
127 }
128 
129 static inline void Send8(u8 d)
130 {
131  UEDATX = d;
132 }
133 
134 static inline void SetEP(u8 ep)
135 {
136  UENUM = ep;
137 }
138 
139 static inline u8 FifoByteCount()
140 {
141  return UEBCLX;
142 }
143 
144 static inline u8 ReceivedSetupInt()
145 {
146  return UEINTX & (1<<RXSTPI);
147 }
148 
149 static inline void ClearSetupInt()
150 {
151  UEINTX = ~((1<<RXSTPI) | (1<<RXOUTI) | (1<<TXINI));
152 }
153 
154 static inline void Stall()
155 {
156  UECONX = (1<<STALLRQ) | (1<<EPEN);
157 }
158 
159 static inline u8 ReadWriteAllowed()
160 {
161  return UEINTX & (1<<RWAL);
162 }
163 
164 static inline u8 Stalled()
165 {
166  return UEINTX & (1<<STALLEDI);
167 }
168 
169 static inline u8 FifoFree()
170 {
171  return UEINTX & (1<<FIFOCON);
172 }
173 
174 static inline void ReleaseRX()
175 {
176  UEINTX = 0x6B; // FIFOCON=0 NAKINI=1 RWAL=1 NAKOUTI=0 RXSTPI=1 RXOUTI=0 STALLEDI=1 TXINI=1
177 }
178 
179 static inline void ReleaseTX()
180 {
181  UEINTX = 0x3A; // FIFOCON=0 NAKINI=0 RWAL=1 NAKOUTI=1 RXSTPI=1 RXOUTI=0 STALLEDI=1 TXINI=0
182 }
183 
184 static inline u8 FrameNumber()
185 {
186  return UDFNUML;
187 }
188 
189 //==================================================================
190 //==================================================================
191 
192 u8 USBGetConfiguration(void)
193 {
194  return _usbConfiguration;
195 }
196 
197 #define USB_RECV_TIMEOUT
198 class LockEP
199 {
200  u8 _sreg;
201 public:
202  LockEP(u8 ep) : _sreg(SREG)
203  {
204  cli();
205  SetEP(ep & 7);
206  }
207  ~LockEP()
208  {
209  SREG = _sreg;
210  }
211 };
212 
213 // Number of bytes, assumes a rx endpoint
214 u8 USB_Available(u8 ep)
215 {
216  LockEP lock(ep);
217  return FifoByteCount();
218 }
219 
220 // Non Blocking receive
221 // Return number of bytes read
222 int USB_Recv(u8 ep, void* d, int len)
223 {
224  if (!_usbConfiguration || len < 0)
225  return -1;
226 
227  LockEP lock(ep);
228  u8 n = FifoByteCount();
229  len = min(n,len);
230  n = len;
231  u8* dst = (u8*)d;
232  while (n--)
233  *dst++ = Recv8();
234  if (len && !FifoByteCount()) // release empty buffer
235  ReleaseRX();
236 
237  return len;
238 }
239 
240 // Recv 1 byte if ready
241 int USB_Recv(u8 ep)
242 {
243  u8 c;
244  if (USB_Recv(ep,&c,1) != 1)
245  return -1;
246  return c;
247 }
248 
249 // Space in send EP
250 u8 USB_SendSpace(u8 ep)
251 {
252  LockEP lock(ep);
253  if (!ReadWriteAllowed())
254  return 0;
255  return USB_EP_SIZE - FifoByteCount();
256 }
257 
258 // Blocking Send of data to an endpoint
259 int USB_Send(u8 ep, const void* d, int len)
260 {
261  if (!_usbConfiguration)
262  return -1;
263 
264  if (_usbSuspendState & (1<<SUSPI)) {
265  //send a remote wakeup
266  UDCON |= (1 << RMWKUP);
267  }
268 
269  int r = len;
270  const u8* data = (const u8*)d;
271  u8 timeout = 250; // 250ms timeout on send? TODO
272  bool sendZlp = false;
273 
274  while (len || sendZlp)
275  {
276  u8 n = USB_SendSpace(ep);
277  if (n == 0)
278  {
279  if (!(--timeout))
280  return -1;
281  delay(1);
282  continue;
283  }
284 
285  if (n > len) {
286  n = len;
287  }
288 
289  {
290  LockEP lock(ep);
291  // Frame may have been released by the SOF interrupt handler
292  if (!ReadWriteAllowed())
293  continue;
294 
295  len -= n;
296  if (ep & TRANSFER_ZERO)
297  {
298  while (n--)
299  Send8(0);
300  }
301  else if (ep & TRANSFER_PGM)
302  {
303  while (n--)
304  Send8(pgm_read_byte(data++));
305  }
306  else
307  {
308  while (n--)
309  Send8(*data++);
310  }
311 
312  if (sendZlp) {
313  ReleaseTX();
314  sendZlp = false;
315  } else if (!ReadWriteAllowed()) { // ...release if buffer is full...
316  ReleaseTX();
317  if (len == 0) sendZlp = true;
318  } else if ((len == 0) && (ep & TRANSFER_RELEASE)) { // ...or if forced with TRANSFER_RELEASE
319  // XXX: TRANSFER_RELEASE is never used can be removed?
320  ReleaseTX();
321  }
322  }
323  }
324  TXLED1; // light the TX LED
325  TxLEDPulse = TX_RX_LED_PULSE_MS;
326  return r;
327 }
328 
329 u8 _initEndpoints[USB_ENDPOINTS] =
330 {
331  0, // Control Endpoint
332 
333  EP_TYPE_INTERRUPT_IN, // CDC_ENDPOINT_ACM
334  EP_TYPE_BULK_OUT, // CDC_ENDPOINT_OUT
335  EP_TYPE_BULK_IN, // CDC_ENDPOINT_IN
336 
337  // Following endpoints are automatically initialized to 0
338 };
339 
340 #define EP_SINGLE_64 0x32 // EP0
341 #define EP_DOUBLE_64 0x36 // Other endpoints
342 #define EP_SINGLE_16 0x12
343 
344 static
345 void InitEP(u8 index, u8 type, u8 size)
346 {
347  UENUM = index;
348  UECONX = (1<<EPEN);
349  UECFG0X = type;
350  UECFG1X = size;
351 }
352 
353 static
354 void InitEndpoints()
355 {
356  for (u8 i = 1; i < sizeof(_initEndpoints) && _initEndpoints[i] != 0; i++)
357  {
358  UENUM = i;
359  UECONX = (1<<EPEN);
360  UECFG0X = _initEndpoints[i];
361 #if USB_EP_SIZE == 16
362  UECFG1X = EP_SINGLE_16;
363 #elif USB_EP_SIZE == 64
364  UECFG1X = EP_DOUBLE_64;
365 #else
366 #error Unsupported value for USB_EP_SIZE
367 #endif
368  }
369  UERST = 0x7E; // And reset them
370  UERST = 0;
371 }
372 
373 // Handle CLASS_INTERFACE requests
374 static
375 bool ClassInterfaceRequest(USBSetup& setup)
376 {
377  u8 i = setup.wIndex;
378 
379  if (CDC_ACM_INTERFACE == i)
380  return CDC_Setup(setup);
381 
382 #ifdef PLUGGABLE_USB_ENABLED
383  return PluggableUSB().setup(setup);
384 #endif
385  return false;
386 }
387 
388 static int _cmark;
389 static int _cend;
390 void InitControl(int end)
391 {
392  SetEP(0);
393  _cmark = 0;
394  _cend = end;
395 }
396 
397 static
398 bool SendControl(u8 d)
399 {
400  if (_cmark < _cend)
401  {
402  if (!WaitForINOrOUT())
403  return false;
404  Send8(d);
405  if (!((_cmark + 1) & 0x3F))
406  ClearIN(); // Fifo is full, release this packet
407  }
408  _cmark++;
409  return true;
410 }
411 
412 // Clipped by _cmark/_cend
413 int USB_SendControl(u8 flags, const void* d, int len)
414 {
415  int sent = len;
416  const u8* data = (const u8*)d;
417  bool pgm = flags & TRANSFER_PGM;
418  while (len--)
419  {
420  u8 c = pgm ? pgm_read_byte(data++) : *data++;
421  if (!SendControl(c))
422  return -1;
423  }
424  return sent;
425 }
426 
427 // Send a USB descriptor string. The string is stored in PROGMEM as a
428 // plain ASCII string but is sent out as UTF-16 with the correct 2-byte
429 // prefix
430 static bool USB_SendStringDescriptor(const u8*string_P, u8 string_len, uint8_t flags) {
431  SendControl(2 + string_len * 2);
432  SendControl(3);
433  bool pgm = flags & TRANSFER_PGM;
434  for(u8 i = 0; i < string_len; i++) {
435  bool r = SendControl(pgm ? pgm_read_byte(&string_P[i]) : string_P[i]);
436  r &= SendControl(0); // high byte
437  if(!r) {
438  return false;
439  }
440  }
441  return true;
442 }
443 
444 // Does not timeout or cross fifo boundaries
445 int USB_RecvControl(void* d, int len)
446 {
447  auto length = len;
448  while(length)
449  {
450  // Dont receive more than the USB Control EP has to offer
451  // Use fixed 64 because control EP always have 64 bytes even on 16u2.
452  auto recvLength = length;
453  if(recvLength > 64){
454  recvLength = 64;
455  }
456 
457  // Write data to fit to the end (not the beginning) of the array
458  WaitOUT();
459  Recv((u8*)d + len - length, recvLength);
460  ClearOUT();
461  length -= recvLength;
462  }
463  return len;
464 }
465 
466 static u8 SendInterfaces()
467 {
468  u8 interfaces = 0;
469 
470  CDC_GetInterface(&interfaces);
471 
472 #ifdef PLUGGABLE_USB_ENABLED
473  PluggableUSB().getInterface(&interfaces);
474 #endif
475 
476  return interfaces;
477 }
478 
479 // Construct a dynamic configuration descriptor
480 // This really needs dynamic endpoint allocation etc
481 // TODO
482 static
483 bool SendConfiguration(int maxlen)
484 {
485  // Count and measure interfaces
486  InitControl(0);
487  u8 interfaces = SendInterfaces();
488  ConfigDescriptor config = D_CONFIG(_cmark + sizeof(ConfigDescriptor),interfaces);
489 
490  // Now send them
491  InitControl(maxlen);
492  USB_SendControl(0,&config,sizeof(ConfigDescriptor));
493  SendInterfaces();
494  return true;
495 }
496 
497 static
498 bool SendDescriptor(USBSetup& setup)
499 {
500  int ret;
501  u8 t = setup.wValueH;
503  return SendConfiguration(setup.wLength);
504 
505  InitControl(setup.wLength);
506 #ifdef PLUGGABLE_USB_ENABLED
507  ret = PluggableUSB().getDescriptor(setup);
508  if (ret != 0) {
509  return (ret > 0 ? true : false);
510  }
511 #endif
512 
513  const u8* desc_addr = 0;
515  {
516  desc_addr = (const u8*)&USB_DeviceDescriptorIAD;
517  }
518  else if (USB_STRING_DESCRIPTOR_TYPE == t)
519  {
520  if (setup.wValueL == 0) {
521  desc_addr = (const u8*)&STRING_LANGUAGE;
522  }
523  else if (setup.wValueL == IPRODUCT) {
524  return USB_SendStringDescriptor(STRING_PRODUCT, strlen(USB_PRODUCT), TRANSFER_PGM);
525  }
526  else if (setup.wValueL == IMANUFACTURER) {
527  return USB_SendStringDescriptor(STRING_MANUFACTURER, strlen(USB_MANUFACTURER), TRANSFER_PGM);
528  }
529  else if (setup.wValueL == ISERIAL) {
530 #ifdef PLUGGABLE_USB_ENABLED
531  char name[ISERIAL_MAX_LEN];
532  PluggableUSB().getShortName(name);
533  return USB_SendStringDescriptor((uint8_t*)name, strlen(name), 0);
534 #endif
535  }
536  else
537  return false;
538  }
539 
540  if (desc_addr == 0)
541  return false;
542  u8 desc_length = pgm_read_byte(desc_addr);
543 
544  USB_SendControl(TRANSFER_PGM,desc_addr,desc_length);
545  return true;
546 }
547 
548 // Endpoint 0 interrupt
549 ISR(USB_COM_vect)
550 {
551  SetEP(0);
552  if (!ReceivedSetupInt())
553  return;
554 
555  USBSetup setup;
556  Recv((u8*)&setup,8);
557  ClearSetupInt();
558 
559  u8 requestType = setup.bmRequestType;
560  if (requestType & REQUEST_DEVICETOHOST)
561  WaitIN();
562  else
563  ClearIN();
564 
565  bool ok = true;
566  if (REQUEST_STANDARD == (requestType & REQUEST_TYPE))
567  {
568  // Standard Requests
569  u8 r = setup.bRequest;
570  u16 wValue = setup.wValueL | (setup.wValueH << 8);
571  if (GET_STATUS == r)
572  {
573  if (requestType == (REQUEST_DEVICETOHOST | REQUEST_STANDARD | REQUEST_DEVICE))
574  {
575  Send8(_usbCurrentStatus);
576  Send8(0);
577  }
578  else
579  {
580  // TODO: handle the HALT state of an endpoint here
581  // see "Figure 9-6. Information Returned by a GetStatus() Request to an Endpoint" in usb_20.pdf for more information
582  Send8(0);
583  Send8(0);
584  }
585  }
586  else if (CLEAR_FEATURE == r)
587  {
588  if((requestType == (REQUEST_HOSTTODEVICE | REQUEST_STANDARD | REQUEST_DEVICE))
589  && (wValue == DEVICE_REMOTE_WAKEUP))
590  {
591  _usbCurrentStatus &= ~FEATURE_REMOTE_WAKEUP_ENABLED;
592  }
593  }
594  else if (SET_FEATURE == r)
595  {
596  if((requestType == (REQUEST_HOSTTODEVICE | REQUEST_STANDARD | REQUEST_DEVICE))
597  && (wValue == DEVICE_REMOTE_WAKEUP))
598  {
599  _usbCurrentStatus |= FEATURE_REMOTE_WAKEUP_ENABLED;
600  }
601  }
602  else if (SET_ADDRESS == r)
603  {
604  WaitIN();
605  UDADDR = setup.wValueL | (1<<ADDEN);
606  }
607  else if (GET_DESCRIPTOR == r)
608  {
609  ok = SendDescriptor(setup);
610  }
611  else if (SET_DESCRIPTOR == r)
612  {
613  ok = false;
614  }
615  else if (GET_CONFIGURATION == r)
616  {
617  Send8(1);
618  }
619  else if (SET_CONFIGURATION == r)
620  {
621  if (REQUEST_DEVICE == (requestType & REQUEST_RECIPIENT))
622  {
623  InitEndpoints();
624  _usbConfiguration = setup.wValueL;
625  } else
626  ok = false;
627  }
628  else if (GET_INTERFACE == r)
629  {
630  }
631  else if (SET_INTERFACE == r)
632  {
633  }
634  }
635  else
636  {
637  InitControl(setup.wLength); // Max length of transfer
638  ok = ClassInterfaceRequest(setup);
639  }
640 
641  if (ok)
642  ClearIN();
643  else
644  {
645  Stall();
646  }
647 }
648 
649 void USB_Flush(u8 ep)
650 {
651  SetEP(ep);
652  if (FifoByteCount())
653  ReleaseTX();
654 }
655 
656 static inline void USB_ClockDisable()
657 {
658 #if defined(OTGPADE)
659  USBCON = (USBCON & ~(1<<OTGPADE)) | (1<<FRZCLK); // freeze clock and disable VBUS Pad
660 #else // u2 Series
661  USBCON = (1 << FRZCLK); // freeze clock
662 #endif
663  PLLCSR &= ~(1<<PLLE); // stop PLL
664 }
665 
666 static inline void USB_ClockEnable()
667 {
668 #if defined(UHWCON)
669  UHWCON |= (1<<UVREGE); // power internal reg
670 #endif
671  USBCON = (1<<USBE) | (1<<FRZCLK); // clock frozen, usb enabled
672 
673 // ATmega32U4
674 #if defined(PINDIV)
675 #if F_CPU == 16000000UL
676  PLLCSR |= (1<<PINDIV); // Need 16 MHz xtal
677 #elif F_CPU == 8000000UL
678  PLLCSR &= ~(1<<PINDIV); // Need 8 MHz xtal
679 #else
680 #error "Clock rate of F_CPU not supported"
681 #endif
682 
683 #elif defined(__AVR_AT90USB82__) || defined(__AVR_AT90USB162__) || defined(__AVR_ATmega32U2__) || defined(__AVR_ATmega16U2__) || defined(__AVR_ATmega8U2__)
684  // for the u2 Series the datasheet is confusing. On page 40 its called PINDIV and on page 290 its called PLLP0
685 #if F_CPU == 16000000UL
686  // Need 16 MHz xtal
687  PLLCSR |= (1 << PLLP0);
688 #elif F_CPU == 8000000UL
689  // Need 8 MHz xtal
690  PLLCSR &= ~(1 << PLLP0);
691 #endif
692 
693 // AT90USB646, AT90USB647, AT90USB1286, AT90USB1287
694 #elif defined(PLLP2)
695 #if F_CPU == 16000000UL
696 #if defined(__AVR_AT90USB1286__) || defined(__AVR_AT90USB1287__)
697  // For Atmel AT90USB128x only. Do not use with Atmel AT90USB64x.
698  PLLCSR = (PLLCSR & ~(1<<PLLP1)) | ((1<<PLLP2) | (1<<PLLP0)); // Need 16 MHz xtal
699 #elif defined(__AVR_AT90USB646__) || defined(__AVR_AT90USB647__)
700  // For AT90USB64x only. Do not use with AT90USB128x.
701  PLLCSR = (PLLCSR & ~(1<<PLLP0)) | ((1<<PLLP2) | (1<<PLLP1)); // Need 16 MHz xtal
702 #else
703 #error "USB Chip not supported, please defined method of USB PLL initialization"
704 #endif
705 #elif F_CPU == 8000000UL
706  // for Atmel AT90USB128x and AT90USB64x
707  PLLCSR = (PLLCSR & ~(1<<PLLP2)) | ((1<<PLLP1) | (1<<PLLP0)); // Need 8 MHz xtal
708 #else
709 #error "Clock rate of F_CPU not supported"
710 #endif
711 #else
712 #error "USB Chip not supported, please defined method of USB PLL initialization"
713 #endif
714 
715  PLLCSR |= (1<<PLLE);
716  while (!(PLLCSR & (1<<PLOCK))) // wait for lock pll
717  {
718  }
719 
720  // Some tests on specific versions of macosx (10.7.3), reported some
721  // strange behaviors when the board is reset using the serial
722  // port touch at 1200 bps. This delay fixes this behavior.
723  delay(1);
724 #if defined(OTGPADE)
725  USBCON = (USBCON & ~(1<<FRZCLK)) | (1<<OTGPADE); // start USB clock, enable VBUS Pad
726 #else
727  USBCON &= ~(1 << FRZCLK); // start USB clock
728 #endif
729 
730 #if defined(RSTCPU)
731 #if defined(LSM)
732  UDCON &= ~((1<<RSTCPU) | (1<<LSM) | (1<<RMWKUP) | (1<<DETACH)); // enable attach resistor, set full speed mode
733 #else // u2 Series
734  UDCON &= ~((1 << RSTCPU) | (1 << RMWKUP) | (1 << DETACH)); // enable attach resistor, set full speed mode
735 #endif
736 #else
737  // AT90USB64x and AT90USB128x don't have RSTCPU
738  UDCON &= ~((1<<LSM) | (1<<RMWKUP) | (1<<DETACH)); // enable attach resistor, set full speed mode
739 #endif
740 }
741 
742 // General interrupt
743 ISR(USB_GEN_vect)
744 {
745  u8 udint = UDINT;
746  UDINT &= ~((1<<EORSTI) | (1<<SOFI)); // clear the IRQ flags for the IRQs which are handled here, except WAKEUPI and SUSPI (see below)
747 
748  // End of Reset
749  if (udint & (1<<EORSTI))
750  {
751  InitEP(0,EP_TYPE_CONTROL,EP_SINGLE_64); // init ep0
752  _usbConfiguration = 0; // not configured yet
753  UEIENX = 1 << RXSTPE; // Enable interrupts for ep0
754  }
755 
756  // Start of Frame - happens every millisecond so we use it for TX and RX LED one-shot timing, too
757  if (udint & (1<<SOFI))
758  {
759  USB_Flush(CDC_TX); // Send a tx frame if found
760 
761  // check whether the one-shot period has elapsed. if so, turn off the LED
762  if (TxLEDPulse && !(--TxLEDPulse))
763  TXLED0;
764  if (RxLEDPulse && !(--RxLEDPulse))
765  RXLED0;
766  }
767 
768  // the WAKEUPI interrupt is triggered as soon as there are non-idle patterns on the data
769  // lines. Thus, the WAKEUPI interrupt can occur even if the controller is not in the "suspend" mode.
770  // Therefore the we enable it only when USB is suspended
771  if (udint & (1<<WAKEUPI))
772  {
773  UDIEN = (UDIEN & ~(1<<WAKEUPE)) | (1<<SUSPE); // Disable interrupts for WAKEUP and enable interrupts for SUSPEND
774 
775  //TODO
776  // WAKEUPI shall be cleared by software (USB clock inputs must be enabled before).
777  //USB_ClockEnable();
778  UDINT &= ~(1<<WAKEUPI);
779  _usbSuspendState = (_usbSuspendState & ~(1<<SUSPI)) | (1<<WAKEUPI);
780  }
781  else if (udint & (1<<SUSPI)) // only one of the WAKEUPI / SUSPI bits can be active at time
782  {
783  UDIEN = (UDIEN & ~(1<<SUSPE)) | (1<<WAKEUPE); // Disable interrupts for SUSPEND and enable interrupts for WAKEUP
784 
785  //TODO
786  //USB_ClockDisable();
787 
788  UDINT &= ~((1<<WAKEUPI) | (1<<SUSPI)); // clear any already pending WAKEUP IRQs and the SUSPI request
789  _usbSuspendState = (_usbSuspendState & ~(1<<WAKEUPI)) | (1<<SUSPI);
790  }
791 }
792 
793 // VBUS or counting frames
794 // Any frame counting?
795 u8 USBConnected()
796 {
797  u8 f = UDFNUML;
798  delay(3);
799  return f != UDFNUML;
800 }
801 
802 //=======================================================================
803 //=======================================================================
804 
805 USBDevice_ USBDevice;
806 
807 USBDevice_::USBDevice_()
808 {
809 }
810 
811 void USBDevice_::attach()
812 {
813  _usbConfiguration = 0;
814  _usbCurrentStatus = 0;
815  _usbSuspendState = 0;
816  USB_ClockEnable();
817 
818  UDINT &= ~((1<<WAKEUPI) | (1<<SUSPI)); // clear already pending WAKEUP / SUSPEND requests
819  UDIEN = (1<<EORSTE) | (1<<SOFE) | (1<<SUSPE); // Enable interrupts for EOR (End of Reset), SOF (start of frame) and SUSPEND
820 
821  TX_RX_LED_INIT;
822 
823 #if MAGIC_KEY_POS != (RAMEND-1)
824  if (pgm_read_word(FLASHEND - 1) == NEW_LUFA_SIGNATURE) {
825  _updatedLUFAbootloader = true;
826  }
827 #endif
828 }
829 
830 void USBDevice_::detach()
831 {
832 }
833 
834 // Check for interrupts
835 // TODO: VBUS detection
836 bool USBDevice_::configured()
837 {
838  return _usbConfiguration;
839 }
840 
841 void USBDevice_::poll()
842 {
843 }
844 
845 bool USBDevice_::wakeupHost()
846 {
847  // clear any previous wakeup request which might have been set but could be processed at that time
848  // e.g. because the host was not suspended at that time
849  UDCON &= ~(1 << RMWKUP);
850 
851  if(!(UDCON & (1 << RMWKUP))
852  && (_usbSuspendState & (1<<SUSPI))
853  && (_usbCurrentStatus & FEATURE_REMOTE_WAKEUP_ENABLED))
854  {
855  // This short version will only work, when the device has not been suspended. Currently the
856  // Arduino core doesn't handle SUSPEND at all, so this is ok.
857  USB_ClockEnable();
858  UDCON |= (1 << RMWKUP); // send the wakeup request
859  return true;
860  }
861 
862  return false;
863 }
864 
865 #endif /* if defined(USBCON) */
GLuint GLsizei GLsizei * length
#define CDC_TX
Definition: USBDesc.h:42
#define USB_CONFIGURATION_DESCRIPTOR_TYPE
Definition: USBCore.h:74
#define REQUEST_DEVICE
Definition: USBCore.h:46
unsigned short u16
Definition: USBAPI.h:30
#define USB_DEVICE_DESCRIPTOR_TYPE
Definition: USBCore.h:73
#define GET_INTERFACE
Definition: USBCore.h:32
#define IMANUFACTURER
Definition: USBDesc.h:44
#define REQUEST_RECIPIENT
Definition: USBCore.h:50
#define D_DEVICE(_class, _subClass, _proto, _packetSize0, _vid, _pid, _version, _im, _ip, _is, _configs)
Definition: USBCore.h:266
const GLfloat * c
GLdouble GLdouble t
#define IPRODUCT
Definition: USBDesc.h:45
#define REQUEST_DEVICETOHOST
Definition: USBCore.h:38
#define REQUEST_STANDARD
Definition: USBCore.h:41
#define REQUEST_TYPE
Definition: USBCore.h:44
#define CDC_ACM_INTERFACE
Definition: USBDesc.h:32
#define DEVICE_REMOTE_WAKEUP
Definition: USBCore.h:80
ISR(TIMER2_COMPA_vect)
Definition: Tone.cpp:538
GLuint GLuint end
GLsizei n
#define GET_STATUS
Definition: USBCore.h:24
#define D_CONFIG(_totalLength, _interfaces)
Definition: USBCore.h:269
GLuint dst
GLenum GLsizei len
#define REQUEST_HOSTTODEVICE
Definition: USBCore.h:37
unsigned char u8
Definition: USBAPI.h:29
#define SET_DESCRIPTOR
Definition: USBCore.h:29
#define NEW_LUFA_SIGNATURE
Definition: USBCore.h:299
GLuint const GLchar * name
GLint GLenum GLsizei GLint GLsizei const GLvoid * data
void setup()
ROSCPP_DECL bool ok()
void delay(unsigned long)
Definition: wiring.c:106
#define FEATURE_REMOTE_WAKEUP_ENABLED
Definition: USBCore.h:86
#define ISERIAL_MAX_LEN
Definition: USBDesc.h:27
#define USB_STRING_DESCRIPTOR_TYPE
Definition: USBCore.h:75
GLsizeiptr size
#define GET_CONFIGURATION
Definition: USBCore.h:30
GLuint index
#define CLEAR_FEATURE
Definition: USBCore.h:25
#define SET_CONFIGURATION
Definition: USBCore.h:31
GLdouble GLdouble GLdouble r
#define SET_ADDRESS
Definition: USBCore.h:27
GLuint GLuint GLsizei count
#define GET_DESCRIPTOR
Definition: USBCore.h:28
#define USB_ENDPOINTS
Definition: USBDesc.h:24
GLuint GLuint GLsizei GLenum type
#define min(a, b)
Definition: Arduino.h:92
#define USB_EP_SIZE
Definition: USBAPI.h:38
#define SET_INTERFACE
Definition: USBCore.h:33
#define SET_FEATURE
Definition: USBCore.h:26
GLdouble GLdouble GLdouble GLdouble GLdouble GLdouble f


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