Skip to content

Commit

Permalink
Merge pull request #158 from hathach/tinyusb-webusb
Browse files Browse the repository at this point in the history
update Tinyusb to support webusb
  • Loading branch information
ladyada authored Aug 1, 2019
2 parents 553d3c8 + 96adc67 commit 6ff862e
Show file tree
Hide file tree
Showing 33 changed files with 929 additions and 533 deletions.
8 changes: 7 additions & 1 deletion cores/arduino/Adafruit_TinyUSB_Core/Adafruit_USBD_CDC.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,8 @@ int Adafruit_USBD_CDC::available(void)

int Adafruit_USBD_CDC::peek(void)
{
return tud_cdc_peek(0);
uint8_t ch;
return tud_cdc_peek(0, &ch) ? (int) ch : -1;
}

int Adafruit_USBD_CDC::read(void)
Expand Down Expand Up @@ -125,6 +126,11 @@ size_t Adafruit_USBD_CDC::write(const uint8_t *buffer, size_t size)
return size - remain;
}

int Adafruit_USBD_CDC::availableForWrite(void)
{
return tud_cdc_write_available();
}

extern "C"
{

Expand Down
10 changes: 6 additions & 4 deletions cores/arduino/Adafruit_TinyUSB_Core/Adafruit_USBD_CDC.h
Original file line number Diff line number Diff line change
Expand Up @@ -41,15 +41,17 @@ class Adafruit_USBD_CDC : public Stream, Adafruit_USBD_Interface
void begin(uint32_t baud, uint8_t config);
void end(void);

virtual int available(void);
virtual int peek(void);
virtual int read(void);
virtual void flush(void);
virtual int available(void);
virtual int peek(void);
virtual int read(void);
virtual void flush(void);
virtual size_t write(uint8_t);

virtual size_t write(const uint8_t *buffer, size_t size);
size_t write(const char *buffer, size_t size) {
return write((const uint8_t *)buffer, size);
}
virtual int availableForWrite(void);
operator bool();
};

Expand Down
16 changes: 9 additions & 7 deletions cores/arduino/Adafruit_TinyUSB_Core/Adafruit_USBD_Device.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ Adafruit_USBD_Device USBDevice;

Adafruit_USBD_Device::Adafruit_USBD_Device(void)
{
tusb_desc_device_t desc_dev =
tusb_desc_device_t const desc_dev =
{
.bLength = sizeof(tusb_desc_device_t),
.bDescriptorType = TUSB_DESC_DEVICE,
Expand All @@ -57,25 +57,22 @@ Adafruit_USBD_Device::Adafruit_USBD_Device(void)
.idVendor = 0,
.idProduct = 0,
.bcdDevice = 0x0100,

.iManufacturer = 0x01,
.iProduct = 0x02,
.iSerialNumber = 0x03,

.bNumConfigurations = 0x01
};

_desc_device = desc_dev;

tusb_desc_configuration_t dev_cfg =
tusb_desc_configuration_t const dev_cfg =
{
.bLength = sizeof(tusb_desc_configuration_t),
.bDescriptorType = TUSB_DESC_CONFIGURATION,

// Total Length & Interface Number will be updated later
.wTotalLength = 0,
.bNumInterfaces = 0,

.bConfigurationValue = 1,
.iConfiguration = 0x00,
.bmAttributes = TU_BIT(7) | TUSB_DESC_CONFIG_ATT_REMOTE_WAKEUP,
Expand Down Expand Up @@ -132,6 +129,11 @@ void Adafruit_USBD_Device::setID(uint16_t vid, uint16_t pid)
_desc_device.idProduct = pid;
}

void Adafruit_USBD_Device::setVersion(uint16_t bcd)
{
_desc_device.bcdUSB = bcd;
}

bool Adafruit_USBD_Device::begin(void)
{
return true;
Expand Down Expand Up @@ -196,8 +198,8 @@ uint16_t const* tud_descriptor_string_cb(uint8_t index)
default: return NULL;
}

// first byte is len, second byte is string type
_desc_str[0] = TUD_DESC_STR_HEADER(chr_count);
// first byte is length (including header), second byte is string type
_desc_str[0] = (TUSB_DESC_STRING << 8 ) | (2*chr_count + 2);

return _desc_str;
}
Expand Down
9 changes: 5 additions & 4 deletions cores/arduino/Adafruit_TinyUSB_Core/Adafruit_USBD_Device.h
Original file line number Diff line number Diff line change
Expand Up @@ -52,12 +52,13 @@ class Adafruit_USBD_Device
bool addInterface(Adafruit_USBD_Interface& itf);

void setID(uint16_t vid, uint16_t pid);
void setVersion(uint16_t bcd);
bool begin(void);

bool mounted(void) { return tud_mounted(); }
bool suspended(void) { return tud_suspended(); }
bool ready(void) { return tud_ready(); }
bool remoteWakeup(void) { return tud_remote_wakeup(); }
bool mounted (void) { return tud_mounted(); }
bool suspended (void) { return tud_suspended(); }
bool ready (void) { return tud_ready(); }
bool remoteWakeup (void) { return tud_remote_wakeup(); }

friend uint8_t const * tud_descriptor_device_cb(void);
friend uint8_t const * tud_descriptor_configuration_cb(uint8_t index);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -121,23 +121,16 @@ uint32_t tud_cdc_n_available(uint8_t itf)
return tu_fifo_count(&_cdcd_itf[itf].rx_ff);
}

signed char tud_cdc_n_read_char(uint8_t itf)
{
signed char ch;
return tud_cdc_n_read(itf, &ch, 1) ? ch : (-1);
}

uint32_t tud_cdc_n_read(uint8_t itf, void* buffer, uint32_t bufsize)
{
uint32_t num_read = tu_fifo_read_n(&_cdcd_itf[itf].rx_ff, buffer, bufsize);
_prep_out_transaction(itf);
return num_read;
}

signed char tud_cdc_n_peek(uint8_t itf, int pos)
bool tud_cdc_n_peek(uint8_t itf, int pos, uint8_t* chr)
{
signed char ch;
return tu_fifo_peek_at(&_cdcd_itf[itf].rx_ff, pos, &ch) ? ch : (-1);
return tu_fifo_peek_at(&_cdcd_itf[itf].rx_ff, pos, chr);
}

void tud_cdc_n_read_flush (uint8_t itf)
Expand All @@ -149,17 +142,6 @@ void tud_cdc_n_read_flush (uint8_t itf)
//--------------------------------------------------------------------+
// WRITE API
//--------------------------------------------------------------------+

uint32_t tud_cdc_n_write_char(uint8_t itf, char ch)
{
return tud_cdc_n_write(itf, &ch, 1);
}

uint32_t tud_cdc_n_write_str (uint8_t itf, char const* str)
{
return tud_cdc_n_write(itf, str, strlen(str));
}

uint32_t tud_cdc_n_write(uint8_t itf, void const* buffer, uint32_t bufsize)
{
uint16_t ret = tu_fifo_write_n(&_cdcd_itf[itf].tx_ff, buffer, bufsize);
Expand Down Expand Up @@ -190,6 +172,11 @@ bool tud_cdc_n_write_flush (uint8_t itf)
return true;
}

uint32_t tud_cdc_n_write_available (uint8_t itf)
{
return tu_fifo_remaining(&_cdcd_itf[itf].tx_ff);
}


//--------------------------------------------------------------------+
// USBD Driver API
Expand Down Expand Up @@ -300,7 +287,7 @@ bool cdcd_open(uint8_t rhport, tusb_desc_interface_t const * itf_desc, uint16_t

// Invoked when class request DATA stage is finished.
// return false to stall control endpoint (e.g Host send non-sense DATA)
bool cdcd_control_request_complete(uint8_t rhport, tusb_control_request_t const * request)
bool cdcd_control_complete(uint8_t rhport, tusb_control_request_t const * request)
{
(void) rhport;

Expand Down Expand Up @@ -334,11 +321,11 @@ bool cdcd_control_request(uint8_t rhport, tusb_control_request_t const * request
switch ( request->bRequest )
{
case CDC_REQUEST_SET_LINE_CODING:
usbd_control_xfer(rhport, request, &p_cdc->line_coding, sizeof(cdc_line_coding_t));
tud_control_xfer(rhport, request, &p_cdc->line_coding, sizeof(cdc_line_coding_t));
break;

case CDC_REQUEST_GET_LINE_CODING:
usbd_control_xfer(rhport, request, &p_cdc->line_coding, sizeof(cdc_line_coding_t));
tud_control_xfer(rhport, request, &p_cdc->line_coding, sizeof(cdc_line_coding_t));
break;

case CDC_REQUEST_SET_CONTROL_LINE_STATE:
Expand All @@ -349,7 +336,7 @@ bool cdcd_control_request(uint8_t rhport, tusb_control_request_t const * request
// This signal corresponds to V.24 signal 105 and RS-232 signal RTS (Request to Send)
p_cdc->line_state = (uint8_t) request->wValue;

usbd_control_status(rhport, request);
tud_control_status(rhport, request);

// Invoke callback
if ( tud_cdc_line_state_cb) tud_cdc_line_state_cb(itf, tu_bit_test(request->wValue, 0), tu_bit_test(request->wValue, 1));
Expand All @@ -370,7 +357,7 @@ bool cdcd_xfer_cb(uint8_t rhport, uint8_t ep_addr, xfer_result_t result, uint32_
uint8_t const itf = 0;
cdcd_interface_t* p_cdc = &_cdcd_itf[itf];

// receive new data
// Received new data
if ( ep_addr == p_cdc->ep_out )
{
for(uint32_t i=0; i<xferred_bytes; i++)
Expand All @@ -391,7 +378,9 @@ bool cdcd_xfer_cb(uint8_t rhport, uint8_t ep_addr, xfer_result_t result, uint32_
_prep_out_transaction(itf);
}

// sent data
// Data sent to host, we could continue to fetch data tx fifo to send.
// But it will cause incorrect baudrate set in line coding.
// Though maybe the baudrate is not really important !!!
// if ( ep_addr == p_cdc->ep_in )
// {
//
Expand Down
101 changes: 62 additions & 39 deletions cores/arduino/Adafruit_TinyUSB_Core/tinyusb/src/class/cdc/cdc_device.h
Original file line number Diff line number Diff line change
Expand Up @@ -51,40 +51,42 @@
// Application API (Multiple Interfaces)
// CFG_TUD_CDC > 1
//--------------------------------------------------------------------+
bool tud_cdc_n_connected (uint8_t itf);
uint8_t tud_cdc_n_get_line_state (uint8_t itf);
void tud_cdc_n_get_line_coding (uint8_t itf, cdc_line_coding_t* coding);
void tud_cdc_n_set_wanted_char (uint8_t itf, char wanted);

uint32_t tud_cdc_n_available (uint8_t itf);
signed char tud_cdc_n_read_char (uint8_t itf);
uint32_t tud_cdc_n_read (uint8_t itf, void* buffer, uint32_t bufsize);
void tud_cdc_n_read_flush (uint8_t itf);
signed char tud_cdc_n_peek (uint8_t itf, int pos);

uint32_t tud_cdc_n_write_char (uint8_t itf, char ch);
uint32_t tud_cdc_n_write (uint8_t itf, void const* buffer, uint32_t bufsize);
uint32_t tud_cdc_n_write_str (uint8_t itf, char const* str);
bool tud_cdc_n_write_flush (uint8_t itf);
bool tud_cdc_n_connected (uint8_t itf);
uint8_t tud_cdc_n_get_line_state (uint8_t itf);
void tud_cdc_n_get_line_coding (uint8_t itf, cdc_line_coding_t* coding);
void tud_cdc_n_set_wanted_char (uint8_t itf, char wanted);

uint32_t tud_cdc_n_available (uint8_t itf);
uint32_t tud_cdc_n_read (uint8_t itf, void* buffer, uint32_t bufsize);
void tud_cdc_n_read_flush (uint8_t itf);
bool tud_cdc_n_peek (uint8_t itf, int pos, uint8_t* u8);
static inline int32_t tud_cdc_n_read_char (uint8_t itf);

uint32_t tud_cdc_n_write (uint8_t itf, void const* buffer, uint32_t bufsize);
bool tud_cdc_n_write_flush (uint8_t itf);
uint32_t tud_cdc_n_write_available (uint8_t itf);
static inline uint32_t tud_cdc_n_write_char (uint8_t itf, char ch);
static inline uint32_t tud_cdc_n_write_str (uint8_t itf, char const* str);

//--------------------------------------------------------------------+
// Application API (Interface0)
//--------------------------------------------------------------------+
static inline bool tud_cdc_connected (void);
static inline uint8_t tud_cdc_get_line_state (void);
static inline void tud_cdc_get_line_coding (cdc_line_coding_t* coding);
static inline void tud_cdc_set_wanted_char (char wanted);

static inline uint32_t tud_cdc_available (void);
static inline signed char tud_cdc_read_char (void);
static inline uint32_t tud_cdc_read (void* buffer, uint32_t bufsize);
static inline void tud_cdc_read_flush (void);
static inline signed char tud_cdc_peek (int pos);

static inline uint32_t tud_cdc_write_char (char ch);
static inline uint32_t tud_cdc_write (void const* buffer, uint32_t bufsize);
static inline uint32_t tud_cdc_write_str (char const* str);
static inline bool tud_cdc_write_flush (void);
static inline bool tud_cdc_connected (void);
static inline uint8_t tud_cdc_get_line_state (void);
static inline void tud_cdc_get_line_coding (cdc_line_coding_t* coding);
static inline void tud_cdc_set_wanted_char (char wanted);

static inline uint32_t tud_cdc_available (void);
static inline int32_t tud_cdc_read_char (void);
static inline uint32_t tud_cdc_read (void* buffer, uint32_t bufsize);
static inline void tud_cdc_read_flush (void);
static inline bool tud_cdc_peek (int pos, uint8_t* u8);

static inline uint32_t tud_cdc_write_char (char ch);
static inline uint32_t tud_cdc_write (void const* buffer, uint32_t bufsize);
static inline uint32_t tud_cdc_write_str (char const* str);
static inline bool tud_cdc_write_flush (void);
static inline uint32_t tud_cdc_write_available (void);

//--------------------------------------------------------------------+
// Application Callback API (weak is optional)
Expand All @@ -105,6 +107,22 @@ TU_ATTR_WEAK void tud_cdc_line_coding_cb(uint8_t itf, cdc_line_coding_t const* p
//--------------------------------------------------------------------+
// Inline Functions
//--------------------------------------------------------------------+
static inline int32_t tud_cdc_n_read_char (uint8_t itf)
{
uint8_t ch;
return tud_cdc_n_read(itf, &ch, 1) ? (int32_t) ch : -1;
}

static inline uint32_t tud_cdc_n_write_char(uint8_t itf, char ch)
{
return tud_cdc_n_write(itf, &ch, 1);
}

static inline uint32_t tud_cdc_n_write_str (uint8_t itf, char const* str)
{
return tud_cdc_n_write(itf, str, strlen(str));
}

static inline bool tud_cdc_connected (void)
{
return tud_cdc_n_connected(0);
Expand All @@ -130,7 +148,7 @@ static inline uint32_t tud_cdc_available (void)
return tud_cdc_n_available(0);
}

static inline signed char tud_cdc_read_char (void)
static inline int32_t tud_cdc_read_char (void)
{
return tud_cdc_n_read_char(0);
}
Expand All @@ -145,9 +163,9 @@ static inline void tud_cdc_read_flush (void)
tud_cdc_n_read_flush(0);
}

static inline signed char tud_cdc_peek (int pos)
static inline bool tud_cdc_peek (int pos, uint8_t* u8)
{
return tud_cdc_n_peek(0, pos);
return tud_cdc_n_peek(0, pos, u8);
}

static inline uint32_t tud_cdc_write_char (char ch)
Expand All @@ -170,18 +188,23 @@ static inline bool tud_cdc_write_flush (void)
return tud_cdc_n_write_flush(0);
}

static inline uint32_t tud_cdc_write_available(void)
{
return tud_cdc_n_write_available(0);
}

/** @} */
/** @} */

//--------------------------------------------------------------------+
// INTERNAL USBD-CLASS DRIVER API
//--------------------------------------------------------------------+
void cdcd_init (void);
bool cdcd_open (uint8_t rhport, tusb_desc_interface_t const * p_interface_desc, uint16_t *p_length);
bool cdcd_control_request (uint8_t rhport, tusb_control_request_t const * p_request);
bool cdcd_control_request_complete (uint8_t rhport, tusb_control_request_t const * p_request);
bool cdcd_xfer_cb (uint8_t rhport, uint8_t ep_addr, xfer_result_t result, uint32_t xferred_bytes);
void cdcd_reset (uint8_t rhport);
void cdcd_init (void);
void cdcd_reset (uint8_t rhport);
bool cdcd_open (uint8_t rhport, tusb_desc_interface_t const * itf_desc, uint16_t *p_length);
bool cdcd_control_request (uint8_t rhport, tusb_control_request_t const * request);
bool cdcd_control_complete (uint8_t rhport, tusb_control_request_t const * request);
bool cdcd_xfer_cb (uint8_t rhport, uint8_t ep_addr, xfer_result_t result, uint32_t xferred_bytes);

#ifdef __cplusplus
}
Expand Down
Loading

0 comments on commit 6ff862e

Please sign in to comment.