| 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 useful 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 |
bool isSuspended(); |
| 70 |
}; |
| 71 |
extern USBDevice_ USBDevice; |
| 72 |
|
| 73 |
//================================================================================ |
| 74 |
//================================================================================ |
| 75 |
// Serial over CDC (Serial1 is the physical port) |
| 76 |
|
| 77 |
struct ring_buffer; |
| 78 |
|
| 79 |
#ifndef SERIAL_BUFFER_SIZE |
| 80 |
#if ((RAMEND - RAMSTART) < 1023) |
| 81 |
#define SERIAL_BUFFER_SIZE 16 |
| 82 |
#else |
| 83 |
#define SERIAL_BUFFER_SIZE 64 |
| 84 |
#endif |
| 85 |
#endif |
| 86 |
#if (SERIAL_BUFFER_SIZE>256) |
| 87 |
#error Please lower the CDC Buffer size |
| 88 |
#endif |
| 89 |
|
| 90 |
class Serial_ : public Stream |
| 91 |
{ |
| 92 |
private: |
| 93 |
int peek_buffer; |
| 94 |
public: |
| 95 |
Serial_() { peek_buffer = -1; }; |
| 96 |
void begin(unsigned long); |
| 97 |
void begin(unsigned long, uint8_t); |
| 98 |
void end(void); |
| 99 |
|
| 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 |
| 108 |
operator bool(); |
| 109 |
|
| 110 |
volatile uint8_t _rx_buffer_head; |
| 111 |
volatile uint8_t _rx_buffer_tail; |
| 112 |
unsigned char _rx_buffer[SERIAL_BUFFER_SIZE]; |
| 113 |
|
| 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. |
| 129 |
int32_t readBreak(); |
| 130 |
|
| 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. |
| 134 |
uint32_t baud(); |
| 135 |
uint8_t stopbits(); |
| 136 |
uint8_t paritytype(); |
| 137 |
uint8_t numbits(); |
| 138 |
bool dtr(); |
| 139 |
bool rts(); |
| 140 |
enum { |
| 141 |
ONE_STOP_BIT = 0, |
| 142 |
ONE_AND_HALF_STOP_BIT = 1, |
| 143 |
TWO_STOP_BITS = 2, |
| 144 |
}; |
| 145 |
enum { |
| 146 |
NO_PARITY = 0, |
| 147 |
ODD_PARITY = 1, |
| 148 |
EVEN_PARITY = 2, |
| 149 |
MARK_PARITY = 3, |
| 150 |
SPACE_PARITY = 4, |
| 151 |
}; |
| 152 |
|
| 153 |
}; |
| 154 |
extern Serial_ Serial; |
| 155 |
|
| 156 |
#define HAVE_CDCSERIAL |
| 157 |
|
| 158 |
//================================================================================ |
| 159 |
//================================================================================ |
| 160 |
// Low level API |
| 161 |
|
| 162 |
typedef struct |
| 163 |
{ |
| 164 |
uint8_t bmRequestType; |
| 165 |
uint8_t bRequest; |
| 166 |
uint8_t wValueL; |
| 167 |
uint8_t wValueH; |
| 168 |
uint16_t wIndex; |
| 169 |
uint16_t wLength; |
| 170 |
} USBSetup; |
| 171 |
|
| 172 |
//================================================================================ |
| 173 |
//================================================================================ |
| 174 |
// MSC 'Driver' |
| 175 |
|
| 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); |
| 180 |
|
| 181 |
//================================================================================ |
| 182 |
//================================================================================ |
| 183 |
// CSC 'Driver' |
| 184 |
|
| 185 |
int CDC_GetInterface(uint8_t* interfaceNum); |
| 186 |
int CDC_GetDescriptor(int i); |
| 187 |
bool CDC_Setup(USBSetup& setup); |
| 188 |
|
| 189 |
//================================================================================ |
| 190 |
//================================================================================ |
| 191 |
|
| 192 |
#define TRANSFER_PGM 0x80 |
| 193 |
#define TRANSFER_RELEASE 0x40 |
| 194 |
#define TRANSFER_ZERO 0x20 |
| 195 |
|
| 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); |
| 199 |
|
| 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); |
| 206 |
|
| 207 |
#endif |
| 208 |
|
| 209 |
#endif /* if defined(USBCON) */ |