1 | /* |
2 | USBAPI.h |
3 | Copyright (c) 2005-2014 Arduino. All right reserved. |
4 | |
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. |
9 | |
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. |
14 | |
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 |
18 | */ |
19 |
|
20 | #ifndef __USBAPI__ |
21 | #define __USBAPI__ |
22 |
|
23 | #include <inttypes.h> |
24 | #include <avr/pgmspace.h> |
25 | #include <avr/eeprom.h> |
26 | #include <avr/interrupt.h> |
27 | #include <util/delay.h> |
28 |
|
29 | typedef unsigned char u8; |
30 | typedef unsigned short u16; |
31 | typedef unsigned long u32; |
32 |
|
33 | #include "Arduino.h" |
34 |
|
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 |
37 | #ifndef USB_EP_SIZE |
38 | #define USB_EP_SIZE 64 |
39 | #endif |
40 |
|
41 | #if defined(USBCON) |
42 |
|
43 | #include "USBDesc.h" |
44 | #include "USBCore.h" |
45 |
|
46 | //================================================================================ |
47 | //================================================================================ |
48 | // USB |
49 |
|
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) |
57 |
|
58 | class USBDevice_ |
59 | { |
60 | public: |
61 | USBDevice_(); |
62 | bool configured(); |
63 |
|
64 | void attach(); |
65 | void detach(); // Serial port goes down too... |
66 | void poll(); |
67 | bool wakeupHost(); // returns false, when wakeup cannot be processed |
68 | }; |
69 | extern USBDevice_ USBDevice; |
70 |
|
71 | //================================================================================ |
72 | //================================================================================ |
73 | // Serial over CDC (Serial1 is the physical port) |
74 |
|
75 | struct ring_buffer; |
76 |
|
77 | #ifndef SERIAL_BUFFER_SIZE |
78 | #if ((RAMEND - RAMSTART) < 1023) |
79 | #define SERIAL_BUFFER_SIZE 16 |
80 | #else |
81 | #define SERIAL_BUFFER_SIZE 64 |
82 | #endif |
83 | #endif |
84 | #if (SERIAL_BUFFER_SIZE>256) |
85 | #error Please lower the CDC Buffer size |
86 | #endif |
87 |
|
88 | class Serial_ : public Stream |
89 | { |
90 | private: |
91 | int peek_buffer; |
92 | public: |
93 | Serial_() { peek_buffer = -1; }; |
94 | void begin(unsigned long); |
95 | void begin(unsigned long, uint8_t); |
96 | void end(void); |
97 |
|
98 | virtual int available(void); |
99 | virtual int peek(void); |
100 | virtual int read(void); |
101 | virtual int availableForWrite(void); |
102 | virtual void flush(void); |
103 | virtual size_t write(uint8_t); |
104 | virtual size_t write(const uint8_t*, size_t); |
105 | using Print::write; // pull in write(str) and write(buf, size) from Print |
106 | operator bool(); |
107 |
|
108 | volatile uint8_t _rx_buffer_head; |
109 | volatile uint8_t _rx_buffer_tail; |
110 | unsigned char _rx_buffer[SERIAL_BUFFER_SIZE]; |
111 |
|
112 | // This method allows processing "SEND_BREAK" requests sent by |
113 | // the USB host. Those requests indicate that the host wants to |
114 | // send a BREAK signal and are accompanied by a single uint16_t |
115 | // value, specifying the duration of the break. The value 0 |
116 | // means to end any current break, while the value 0xffff means |
117 | // to start an indefinite break. |
118 | // readBreak() will return the value of the most recent break |
119 | // request, but will return it at most once, returning -1 when |
120 | // readBreak() is called again (until another break request is |
121 | // received, which is again returned once). |
122 | // This also mean that if two break requests are received |
123 | // without readBreak() being called in between, the value of the |
124 | // first request is lost. |
125 | // Note that the value returned is a long, so it can return |
126 | // 0-0xffff as well as -1. |
127 | int32_t readBreak(); |
128 |
|
129 | // These return the settings specified by the USB host for the |
130 | // serial port. These aren't really used, but are offered here |
131 | // in case a sketch wants to act on these settings. |
132 | uint32_t baud(); |
133 | uint8_t stopbits(); |
134 | uint8_t paritytype(); |
135 | uint8_t numbits(); |
136 | bool dtr(); |
137 | bool rts(); |
138 | enum { |
139 | ONE_STOP_BIT = 0, |
140 | ONE_AND_HALF_STOP_BIT = 1, |
141 | TWO_STOP_BITS = 2, |
142 | }; |
143 | enum { |
144 | NO_PARITY = 0, |
145 | ODD_PARITY = 1, |
146 | EVEN_PARITY = 2, |
147 | MARK_PARITY = 3, |
148 | SPACE_PARITY = 4, |
149 | }; |
150 |
|
151 | }; |
152 | extern Serial_ Serial; |
153 |
|
154 | #define HAVE_CDCSERIAL |
155 |
|
156 | //================================================================================ |
157 | //================================================================================ |
158 | // Low level API |
159 |
|
160 | typedef struct |
161 | { |
162 | uint8_t bmRequestType; |
163 | uint8_t bRequest; |
164 | uint8_t wValueL; |
165 | uint8_t wValueH; |
166 | uint16_t wIndex; |
167 | uint16_t wLength; |
168 | } USBSetup; |
169 |
|
170 | //================================================================================ |
171 | //================================================================================ |
172 | // MSC 'Driver' |
173 |
|
174 | int MSC_GetInterface(uint8_t* interfaceNum); |
175 | int MSC_GetDescriptor(int i); |
176 | bool MSC_Setup(USBSetup& setup); |
177 | bool MSC_Data(uint8_t rx,uint8_t tx); |
178 |
|
179 | //================================================================================ |
180 | //================================================================================ |
181 | // CSC 'Driver' |
182 |
|
183 | int CDC_GetInterface(uint8_t* interfaceNum); |
184 | int CDC_GetDescriptor(int i); |
185 | bool CDC_Setup(USBSetup& setup); |
186 |
|
187 | //================================================================================ |
188 | //================================================================================ |
189 |
|
190 | #define TRANSFER_PGM 0x80 |
191 | #define TRANSFER_RELEASE 0x40 |
192 | #define TRANSFER_ZERO 0x20 |
193 |
|
194 | int USB_SendControl(uint8_t flags, const void* d, int len); |
195 | int USB_RecvControl(void* d, int len); |
196 | int USB_RecvControlLong(void* d, int len); |
197 |
|
198 | uint8_t USB_Available(uint8_t ep); |
199 | uint8_t USB_SendSpace(uint8_t ep); |
200 | int USB_Send(uint8_t ep, const void* data, int len); // blocking |
201 | int USB_Recv(uint8_t ep, void* data, int len); // non-blocking |
202 | int USB_Recv(uint8_t ep); // non-blocking |
203 | void USB_Flush(uint8_t ep); |
204 |
|
205 | #endif |
206 |
|
207 | #endif /* if defined(USBCON) */ |
208 |
|