3 Copyright (c) 2005-2014 Arduino. All right reserved.
5 This library is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Lesser General Public
7 License as published by the Free Software Foundation; either
8 version 2.1 of the License, or (at your option) any later version.
10 This library is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Lesser General Public License for more details.
15 You should have received a copy of the GNU Lesser General Public
16 License along with this library; if not, write to the Free Software
17 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
24 #include <avr/pgmspace.h>
25 #include <avr/eeprom.h>
26 #include <avr/interrupt.h>
27 #include <util/delay.h>
29 typedef unsigned char u8;
30 typedef unsigned short u16;
31 typedef unsigned long u32;
35 // This definitions is usefull if you want to reduce the EP_SIZE to 16
36 // at the moment only 64 and 16 as EP_SIZE for all EPs are supported except the control endpoint
38 #define USB_EP_SIZE 64
46 //================================================================================
47 //================================================================================
50 #define EP_TYPE_CONTROL (0x00)
51 #define EP_TYPE_BULK_IN ((1<<EPTYPE1) | (1<<EPDIR))
52 #define EP_TYPE_BULK_OUT (1<<EPTYPE1)
53 #define EP_TYPE_INTERRUPT_IN ((1<<EPTYPE1) | (1<<EPTYPE0) | (1<<EPDIR))
54 #define EP_TYPE_INTERRUPT_OUT ((1<<EPTYPE1) | (1<<EPTYPE0))
55 #define EP_TYPE_ISOCHRONOUS_IN ((1<<EPTYPE0) | (1<<EPDIR))
56 #define EP_TYPE_ISOCHRONOUS_OUT (1<<EPTYPE0)
65 void detach(); // Serial port goes down too...
67 bool wakeupHost(); // returns false, when wakeup cannot be processed
71 extern USBDevice_ USBDevice;
73 //================================================================================
74 //================================================================================
75 // Serial over CDC (Serial1 is the physical port)
79 #ifndef SERIAL_BUFFER_SIZE
80 #if ((RAMEND - RAMSTART) < 1023)
81 #define SERIAL_BUFFER_SIZE 16
83 #define SERIAL_BUFFER_SIZE 64
86 #if (SERIAL_BUFFER_SIZE>256)
87 #error Please lower the CDC Buffer size
90 class Serial_ : public Stream
95 Serial_() { peek_buffer = -1; };
96 void begin(unsigned long);
97 void begin(unsigned long, uint8_t);
100 virtual int available(void);
101 virtual int peek(void);
102 virtual int read(void);
103 virtual int availableForWrite(void);
104 virtual void flush(void);
105 virtual size_t write(uint8_t);
106 virtual size_t write(const uint8_t*, size_t);
107 using Print::write; // pull in write(str) and write(buf, size) from Print
110 volatile uint8_t _rx_buffer_head;
111 volatile uint8_t _rx_buffer_tail;
112 unsigned char _rx_buffer[SERIAL_BUFFER_SIZE];
114 // This method allows processing "SEND_BREAK" requests sent by
115 // the USB host. Those requests indicate that the host wants to
116 // send a BREAK signal and are accompanied by a single uint16_t
117 // value, specifying the duration of the break. The value 0
118 // means to end any current break, while the value 0xffff means
119 // to start an indefinite break.
120 // readBreak() will return the value of the most recent break
121 // request, but will return it at most once, returning -1 when
122 // readBreak() is called again (until another break request is
123 // received, which is again returned once).
124 // This also mean that if two break requests are received
125 // without readBreak() being called in between, the value of the
126 // first request is lost.
127 // Note that the value returned is a long, so it can return
128 // 0-0xffff as well as -1.
131 // These return the settings specified by the USB host for the
132 // serial port. These aren't really used, but are offered here
133 // in case a sketch wants to act on these settings.
136 uint8_t paritytype();
142 ONE_AND_HALF_STOP_BIT = 1,
154 extern Serial_ Serial;
156 #define HAVE_CDCSERIAL
158 //================================================================================
159 //================================================================================
164 uint8_t bmRequestType;
172 //================================================================================
173 //================================================================================
176 int MSC_GetInterface(uint8_t* interfaceNum);
177 int MSC_GetDescriptor(int i);
178 bool MSC_Setup(USBSetup& setup);
179 bool MSC_Data(uint8_t rx,uint8_t tx);
181 //================================================================================
182 //================================================================================
185 int CDC_GetInterface(uint8_t* interfaceNum);
186 int CDC_GetDescriptor(int i);
187 bool CDC_Setup(USBSetup& setup);
189 //================================================================================
190 //================================================================================
192 #define TRANSFER_PGM 0x80
193 #define TRANSFER_RELEASE 0x40
194 #define TRANSFER_ZERO 0x20
196 int USB_SendControl(uint8_t flags, const void* d, int len);
197 int USB_RecvControl(void* d, int len);
198 int USB_RecvControlLong(void* d, int len);
200 uint8_t USB_Available(uint8_t ep);
201 uint8_t USB_SendSpace(uint8_t ep);
202 int USB_Send(uint8_t ep, const void* data, int len); // blocking
203 int USB_Recv(uint8_t ep, void* data, int len); // non-blocking
204 int USB_Recv(uint8_t ep); // non-blocking
205 void USB_Flush(uint8_t ep);
209 #endif /* if defined(USBCON) */