2014-09-12 03:27:39 -07:00
|
|
|
/*
|
|
|
|
USBAPI.h
|
|
|
|
Copyright (c) 2005-2014 Arduino. All right reserved.
|
2011-08-11 11:08:38 -07:00
|
|
|
|
2014-09-12 03:27:39 -07:00
|
|
|
This library is free software; you can redistribute it and/or
|
|
|
|
modify it under the terms of the GNU Lesser General Public
|
|
|
|
License as published by the Free Software Foundation; either
|
|
|
|
version 2.1 of the License, or (at your option) any later version.
|
|
|
|
|
|
|
|
This library is distributed in the hope that it will be useful,
|
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
Lesser General Public License for more details.
|
|
|
|
|
|
|
|
You should have received a copy of the GNU Lesser General Public
|
|
|
|
License along with this library; if not, write to the Free Software
|
|
|
|
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
|
|
|
|
*/
|
2011-08-11 11:08:38 -07:00
|
|
|
|
|
|
|
#ifndef __USBAPI__
|
|
|
|
#define __USBAPI__
|
|
|
|
|
2014-09-12 03:27:39 -07:00
|
|
|
#include <inttypes.h>
|
|
|
|
#include <avr/pgmspace.h>
|
|
|
|
#include <avr/eeprom.h>
|
|
|
|
#include <avr/interrupt.h>
|
|
|
|
#include <util/delay.h>
|
|
|
|
|
|
|
|
typedef unsigned char u8;
|
|
|
|
typedef unsigned short u16;
|
|
|
|
typedef unsigned long u32;
|
|
|
|
|
|
|
|
#include "Arduino.h"
|
|
|
|
|
2015-08-07 10:45:18 -07:00
|
|
|
// This definitions is usefull if you want to reduce the EP_SIZE to 16
|
|
|
|
// at the moment only 64 and 16 as EP_SIZE for all EPs are supported except the control endpoint
|
|
|
|
#ifndef USB_EP_SIZE
|
|
|
|
#define USB_EP_SIZE 64
|
|
|
|
#endif
|
|
|
|
|
2011-08-30 08:50:08 -07:00
|
|
|
#if defined(USBCON)
|
|
|
|
|
2014-09-12 03:27:39 -07:00
|
|
|
#include "USBDesc.h"
|
|
|
|
#include "USBCore.h"
|
|
|
|
|
2011-08-11 11:08:38 -07:00
|
|
|
//================================================================================
|
|
|
|
//================================================================================
|
|
|
|
// USB
|
|
|
|
|
2015-08-07 10:45:51 -07:00
|
|
|
#define EP_TYPE_CONTROL (0x00)
|
|
|
|
#define EP_TYPE_BULK_IN ((1<<EPTYPE1) | (1<<EPDIR))
|
|
|
|
#define EP_TYPE_BULK_OUT (1<<EPTYPE1)
|
|
|
|
#define EP_TYPE_INTERRUPT_IN ((1<<EPTYPE1) | (1<<EPTYPE0) | (1<<EPDIR))
|
|
|
|
#define EP_TYPE_INTERRUPT_OUT ((1<<EPTYPE1) | (1<<EPTYPE0))
|
|
|
|
#define EP_TYPE_ISOCHRONOUS_IN ((1<<EPTYPE0) | (1<<EPDIR))
|
|
|
|
#define EP_TYPE_ISOCHRONOUS_OUT (1<<EPTYPE0)
|
2015-06-05 08:34:56 -07:00
|
|
|
|
2012-05-01 08:18:15 -07:00
|
|
|
class USBDevice_
|
2011-08-11 11:08:38 -07:00
|
|
|
{
|
|
|
|
public:
|
2012-05-01 08:18:15 -07:00
|
|
|
USBDevice_();
|
2011-08-11 11:08:38 -07:00
|
|
|
bool configured();
|
|
|
|
|
|
|
|
void attach();
|
|
|
|
void detach(); // Serial port goes down too...
|
|
|
|
void poll();
|
2015-06-26 06:05:57 -07:00
|
|
|
bool wakeupHost(); // returns false, when wakeup cannot be processed
|
2011-08-11 11:08:38 -07:00
|
|
|
};
|
2012-05-01 08:18:15 -07:00
|
|
|
extern USBDevice_ USBDevice;
|
2011-08-11 11:08:38 -07:00
|
|
|
|
|
|
|
//================================================================================
|
|
|
|
//================================================================================
|
|
|
|
// Serial over CDC (Serial1 is the physical port)
|
|
|
|
|
2013-07-26 03:50:17 -07:00
|
|
|
struct ring_buffer;
|
|
|
|
|
2015-02-02 11:27:17 -08:00
|
|
|
#ifndef SERIAL_BUFFER_SIZE
|
2015-09-20 02:08:49 -07:00
|
|
|
#if ((RAMEND - RAMSTART) < 1023)
|
2013-07-27 03:06:42 -07:00
|
|
|
#define SERIAL_BUFFER_SIZE 16
|
|
|
|
#else
|
|
|
|
#define SERIAL_BUFFER_SIZE 64
|
|
|
|
#endif
|
2015-02-02 11:27:17 -08:00
|
|
|
#endif
|
|
|
|
#if (SERIAL_BUFFER_SIZE>256)
|
|
|
|
#error Please lower the CDC Buffer size
|
|
|
|
#endif
|
2013-07-27 03:06:42 -07:00
|
|
|
|
2011-08-11 11:08:38 -07:00
|
|
|
class Serial_ : public Stream
|
|
|
|
{
|
2011-12-18 14:52:35 -08:00
|
|
|
private:
|
2014-06-19 07:52:48 -07:00
|
|
|
int peek_buffer;
|
2011-08-11 11:08:38 -07:00
|
|
|
public:
|
2014-06-19 07:52:48 -07:00
|
|
|
Serial_() { peek_buffer = -1; };
|
2013-09-02 10:58:28 -07:00
|
|
|
void begin(unsigned long);
|
2013-09-02 11:15:12 -07:00
|
|
|
void begin(unsigned long, uint8_t);
|
2011-08-11 11:08:38 -07:00
|
|
|
void end(void);
|
|
|
|
|
|
|
|
virtual int available(void);
|
|
|
|
virtual int peek(void);
|
|
|
|
virtual int read(void);
|
2015-07-03 23:28:52 -07:00
|
|
|
int availableForWrite(void);
|
2011-08-11 11:08:38 -07:00
|
|
|
virtual void flush(void);
|
2011-08-30 08:04:34 -07:00
|
|
|
virtual size_t write(uint8_t);
|
2014-03-18 14:15:41 -07:00
|
|
|
virtual size_t write(const uint8_t*, size_t);
|
2012-06-14 07:53:27 -07:00
|
|
|
using Print::write; // pull in write(str) and write(buf, size) from Print
|
2012-04-01 09:54:35 -07:00
|
|
|
operator bool();
|
2013-07-27 03:06:42 -07:00
|
|
|
|
|
|
|
volatile uint8_t _rx_buffer_head;
|
|
|
|
volatile uint8_t _rx_buffer_tail;
|
|
|
|
unsigned char _rx_buffer[SERIAL_BUFFER_SIZE];
|
2015-06-15 12:17:35 -07:00
|
|
|
|
2015-07-30 06:33:37 -07:00
|
|
|
// This method allows processing "SEND_BREAK" requests sent by
|
|
|
|
// the USB host. Those requests indicate that the host wants to
|
|
|
|
// send a BREAK signal and are accompanied by a single uint16_t
|
|
|
|
// value, specifying the duration of the break. The value 0
|
|
|
|
// means to end any current break, while the value 0xffff means
|
|
|
|
// to start an indefinite break.
|
|
|
|
// readBreak() will return the value of the most recent break
|
|
|
|
// request, but will return it at most once, returning -1 when
|
|
|
|
// readBreak() is called again (until another break request is
|
|
|
|
// received, which is again returned once).
|
|
|
|
// This also mean that if two break requests are received
|
|
|
|
// without readBreak() being called in between, the value of the
|
|
|
|
// first request is lost.
|
|
|
|
// Note that the value returned is a long, so it can return
|
|
|
|
// 0-0xffff as well as -1.
|
|
|
|
int32_t readBreak();
|
|
|
|
|
2015-06-15 12:17:35 -07:00
|
|
|
// These return the settings specified by the USB host for the
|
|
|
|
// serial port. These aren't really used, but are offered here
|
|
|
|
// in case a sketch wants to act on these settings.
|
|
|
|
uint32_t baud();
|
|
|
|
uint8_t stopbits();
|
|
|
|
uint8_t paritytype();
|
|
|
|
uint8_t numbits();
|
|
|
|
bool dtr();
|
|
|
|
bool rts();
|
|
|
|
enum {
|
|
|
|
ONE_STOP_BIT = 0,
|
|
|
|
ONE_AND_HALF_STOP_BIT = 1,
|
|
|
|
TWO_STOP_BITS = 2,
|
|
|
|
};
|
|
|
|
enum {
|
|
|
|
NO_PARITY = 0,
|
|
|
|
ODD_PARITY = 1,
|
|
|
|
EVEN_PARITY = 2,
|
|
|
|
MARK_PARITY = 3,
|
|
|
|
SPACE_PARITY = 4,
|
|
|
|
};
|
|
|
|
|
2011-08-11 11:08:38 -07:00
|
|
|
};
|
|
|
|
extern Serial_ Serial;
|
|
|
|
|
2013-12-01 08:21:54 -08:00
|
|
|
#define HAVE_CDCSERIAL
|
|
|
|
|
2011-08-11 11:08:38 -07:00
|
|
|
//================================================================================
|
|
|
|
//================================================================================
|
2015-06-05 08:33:38 -07:00
|
|
|
// Low level API
|
2011-08-11 11:08:38 -07:00
|
|
|
|
|
|
|
typedef struct
|
|
|
|
{
|
|
|
|
uint8_t bmRequestType;
|
|
|
|
uint8_t bRequest;
|
|
|
|
uint8_t wValueL;
|
|
|
|
uint8_t wValueH;
|
|
|
|
uint16_t wIndex;
|
|
|
|
uint16_t wLength;
|
2015-07-01 07:56:14 -07:00
|
|
|
} USBSetup;
|
2011-08-11 11:08:38 -07:00
|
|
|
|
|
|
|
//================================================================================
|
|
|
|
//================================================================================
|
|
|
|
// MSC 'Driver'
|
|
|
|
|
|
|
|
int MSC_GetInterface(uint8_t* interfaceNum);
|
|
|
|
int MSC_GetDescriptor(int i);
|
2015-07-01 07:56:14 -07:00
|
|
|
bool MSC_Setup(USBSetup& setup);
|
2011-08-11 11:08:38 -07:00
|
|
|
bool MSC_Data(uint8_t rx,uint8_t tx);
|
|
|
|
|
|
|
|
//================================================================================
|
|
|
|
//================================================================================
|
|
|
|
// CSC 'Driver'
|
|
|
|
|
|
|
|
int CDC_GetInterface(uint8_t* interfaceNum);
|
|
|
|
int CDC_GetDescriptor(int i);
|
2015-07-01 07:56:14 -07:00
|
|
|
bool CDC_Setup(USBSetup& setup);
|
2011-08-11 11:08:38 -07:00
|
|
|
|
|
|
|
//================================================================================
|
|
|
|
//================================================================================
|
|
|
|
|
|
|
|
#define TRANSFER_PGM 0x80
|
|
|
|
#define TRANSFER_RELEASE 0x40
|
|
|
|
#define TRANSFER_ZERO 0x20
|
|
|
|
|
|
|
|
int USB_SendControl(uint8_t flags, const void* d, int len);
|
|
|
|
int USB_RecvControl(void* d, int len);
|
|
|
|
|
|
|
|
uint8_t USB_Available(uint8_t ep);
|
2015-07-03 23:28:52 -07:00
|
|
|
uint8_t USB_SendSpace(uint8_t ep);
|
2011-08-11 11:08:38 -07:00
|
|
|
int USB_Send(uint8_t ep, const void* data, int len); // blocking
|
|
|
|
int USB_Recv(uint8_t ep, void* data, int len); // non-blocking
|
|
|
|
int USB_Recv(uint8_t ep); // non-blocking
|
|
|
|
void USB_Flush(uint8_t ep);
|
|
|
|
|
2011-08-30 08:50:08 -07:00
|
|
|
#endif
|
|
|
|
|
2013-07-26 03:50:17 -07:00
|
|
|
#endif /* if defined(USBCON) */
|