diff options
| author | hathach <[email protected]> | 2019-07-30 10:24:36 +0700 |
|---|---|---|
| committer | GitHub <[email protected]> | 2019-07-30 10:24:36 +0700 |
| commit | 1e7f1de4d766e846ec43d5b345a7dc545d98335b (patch) | |
| tree | e09333dd599e92d0686ca5e2bcf928b1cf60165a /src/class | |
| parent | 1ee9ef4f2b7c6acfab6c398a4f57ca22036958f7 (diff) | |
| parent | 00a9e492cdf04ea17379218996d2713bcaaaff51 (diff) | |
Merge pull request #89 from hathach/develop
resolve #35 (vendor class), implement #86 (webusb)
Diffstat (limited to 'src/class')
| -rw-r--r-- | src/class/cdc/cdc_device.c | 8 | ||||
| -rw-r--r-- | src/class/cdc/cdc_device.h | 12 | ||||
| -rw-r--r-- | src/class/custom/custom_device.c | 93 | ||||
| -rw-r--r-- | src/class/custom/custom_device.h | 58 | ||||
| -rw-r--r-- | src/class/hid/hid_device.c | 16 | ||||
| -rw-r--r-- | src/class/hid/hid_device.h | 12 | ||||
| -rw-r--r-- | src/class/hid/hid_host.h | 4 | ||||
| -rw-r--r-- | src/class/midi/midi_device.c | 18 | ||||
| -rw-r--r-- | src/class/midi/midi_device.h | 12 | ||||
| -rw-r--r-- | src/class/msc/msc_device.c | 10 | ||||
| -rw-r--r-- | src/class/msc/msc_device.h | 12 | ||||
| -rw-r--r-- | src/class/vendor/vendor_device.c | 214 | ||||
| -rw-r--r-- | src/class/vendor/vendor_device.h | 121 | ||||
| -rw-r--r-- | src/class/vendor/vendor_host.c (renamed from src/class/custom/custom_host.c) | 4 | ||||
| -rw-r--r-- | src/class/vendor/vendor_host.h (renamed from src/class/custom/custom_host.h) | 6 |
15 files changed, 394 insertions, 206 deletions
diff --git a/src/class/cdc/cdc_device.c b/src/class/cdc/cdc_device.c index 6d13dafc0..d4952d115 100644 --- a/src/class/cdc/cdc_device.c +++ b/src/class/cdc/cdc_device.c @@ -300,7 +300,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;
@@ -334,11 +334,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:
@@ -349,7 +349,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));
diff --git a/src/class/cdc/cdc_device.h b/src/class/cdc/cdc_device.h index 8d09094a0..f1ac08e73 100644 --- a/src/class/cdc/cdc_device.h +++ b/src/class/cdc/cdc_device.h @@ -176,12 +176,12 @@ static inline bool tud_cdc_write_flush (void) //--------------------------------------------------------------------+
// 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
}
diff --git a/src/class/custom/custom_device.c b/src/class/custom/custom_device.c deleted file mode 100644 index 02a711621..000000000 --- a/src/class/custom/custom_device.c +++ /dev/null @@ -1,93 +0,0 @@ -/* - * The MIT License (MIT) - * - * Copyright (c) 2019 Ha Thach (tinyusb.org) - * - * Permission is hereby granted, free of charge, to any person obtaining a copy - * of this software and associated documentation files (the "Software"), to deal - * in the Software without restriction, including without limitation the rights - * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell - * copies of the Software, and to permit persons to whom the Software is - * furnished to do so, subject to the following conditions: - * - * The above copyright notice and this permission notice shall be included in - * all copies or substantial portions of the Software. - * - * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR - * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, - * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE - * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER - * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, - * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN - * THE SOFTWARE. - * - * This file is part of the TinyUSB stack. - */ - -#include "tusb_option.h" - -#if (TUSB_OPT_DEVICE_ENABLED && CFG_TUD_CUSTOM_CLASS) - -#include "common/tusb_common.h" -#include "custom_device.h" -#include "device/usbd_pvt.h" - -/*------------------------------------------------------------------*/ -/* MACRO TYPEDEF CONSTANT ENUM - *------------------------------------------------------------------*/ - -/*------------------------------------------------------------------*/ -/* VARIABLE DECLARATION - *------------------------------------------------------------------*/ -typedef struct { - uint8_t itf_num; - - uint8_t ep_in; - uint8_t ep_out; - -} cusd_interface_t; - -static cusd_interface_t _cusd_itf; - -/*------------------------------------------------------------------*/ -/* FUNCTION DECLARATION - *------------------------------------------------------------------*/ -void cusd_init(void) -{ - tu_varclr(&_cusd_itf); -} - -bool cusd_open(uint8_t rhport, tusb_desc_interface_t const * p_desc_itf, uint16_t *p_len) -{ - cusd_interface_t* p_itf = &_cusd_itf; - - // Open endpoint pair with usbd helper - tusb_desc_endpoint_t const *p_desc_ep = (tusb_desc_endpoint_t const *) tu_desc_next(p_desc_itf); - TU_ASSERT( usbd_open_edpt_pair(rhport, p_desc_ep, TUSB_XFER_BULK, &p_itf->ep_out, &p_itf->ep_in) ); - - p_itf->itf_num = p_desc_itf->bInterfaceNumber; - - (*p_len) = sizeof(tusb_desc_interface_t) + 2*sizeof(tusb_desc_endpoint_t); - - // TODO Prepare for incoming data -// TU_ASSERT( usbd_edpt_xfer(rhport, p_itf->ep_out, (uint8_t*) &p_msc->cbw, sizeof(msc_cbw_t)) ); - - return true; -} - -bool cusd_control_request(uint8_t rhport, tusb_control_request_t const * p_request) -{ - return false; -} - -bool cusd_xfer_cb(uint8_t rhport, uint8_t ep_addr, xfer_result_t event, uint32_t xferred_bytes) -{ - return true; -} - -void cusd_reset(uint8_t rhport) -{ - -} - -#endif diff --git a/src/class/custom/custom_device.h b/src/class/custom/custom_device.h deleted file mode 100644 index 02cdba7e5..000000000 --- a/src/class/custom/custom_device.h +++ /dev/null @@ -1,58 +0,0 @@ -/* - * The MIT License (MIT) - * - * Copyright (c) 2019 Ha Thach (tinyusb.org) - * - * Permission is hereby granted, free of charge, to any person obtaining a copy - * of this software and associated documentation files (the "Software"), to deal - * in the Software without restriction, including without limitation the rights - * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell - * copies of the Software, and to permit persons to whom the Software is - * furnished to do so, subject to the following conditions: - * - * The above copyright notice and this permission notice shall be included in - * all copies or substantial portions of the Software. - * - * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR - * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, - * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE - * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER - * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, - * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN - * THE SOFTWARE. - * - * This file is part of the TinyUSB stack. - */ - -#ifndef _TUSB_CUSTOM_DEVICE_H_ -#define _TUSB_CUSTOM_DEVICE_H_ - -#include "common/tusb_common.h" -#include "device/usbd.h" - -//--------------------------------------------------------------------+ -// APPLICATION API (Multiple Root Ports) -// Should be used only with MCU that support more than 1 ports -//--------------------------------------------------------------------+ - -//--------------------------------------------------------------------+ -// APPLICATION API (Single Port) -// Should be used with MCU supporting only 1 USB port for code simplicity -//--------------------------------------------------------------------+ - - -//--------------------------------------------------------------------+ -// APPLICATION CALLBACK API (WEAK is optional) -//--------------------------------------------------------------------+ - -//--------------------------------------------------------------------+ -// Internal Class Driver API -//--------------------------------------------------------------------+ -void cusd_init(void); -bool cusd_open(uint8_t rhport, tusb_desc_interface_t const * itf_desc, uint16_t *p_length); -bool cusd_control_request_st(uint8_t rhport, tusb_control_request_t const * p_request); -bool cusd_control_request_complete (uint8_t rhport, tusb_control_request_t const * p_request); -bool cusd_xfer_cb(uint8_t rhport, uint8_t ep_addr, xfer_result_t event, uint32_t xferred_bytes); -void cusd_reset(uint8_t rhport); - -#endif /* _TUSB_CUSTOM_DEVICE_H_ */ diff --git a/src/class/hid/hid_device.c b/src/class/hid/hid_device.c index d3f59ed62..949fd183d 100644 --- a/src/class/hid/hid_device.c +++ b/src/class/hid/hid_device.c @@ -205,7 +205,7 @@ bool hidd_control_request(uint8_t rhport, tusb_control_request_t const * p_reque if (p_request->bRequest == TUSB_REQ_GET_DESCRIPTOR && desc_type == HID_DESC_TYPE_REPORT)
{
uint8_t const * desc_report = tud_hid_descriptor_report_cb();
- usbd_control_xfer(rhport, p_request, (void*) desc_report, p_hid->reprot_desc_len);
+ tud_control_xfer(rhport, p_request, (void*) desc_report, p_hid->reprot_desc_len);
}else
{
return false; // stall unsupported request
@@ -225,12 +225,12 @@ bool hidd_control_request(uint8_t rhport, tusb_control_request_t const * p_reque uint16_t xferlen = tud_hid_get_report_cb(report_id, (hid_report_type_t) report_type, p_hid->epin_buf, p_request->wLength);
TU_ASSERT( xferlen > 0 );
- usbd_control_xfer(rhport, p_request, p_hid->epin_buf, xferlen);
+ tud_control_xfer(rhport, p_request, p_hid->epin_buf, xferlen);
}
break;
case HID_REQ_CONTROL_SET_REPORT:
- usbd_control_xfer(rhport, p_request, p_hid->epout_buf, p_request->wLength);
+ tud_control_xfer(rhport, p_request, p_hid->epout_buf, p_request->wLength);
break;
case HID_REQ_CONTROL_SET_IDLE:
@@ -241,18 +241,18 @@ bool hidd_control_request(uint8_t rhport, tusb_control_request_t const * p_reque if ( !tud_hid_set_idle_cb(p_hid->idle_rate) ) return false;
}
- usbd_control_status(rhport, p_request);
+ tud_control_status(rhport, p_request);
break;
case HID_REQ_CONTROL_GET_IDLE:
// TODO idle rate of report
- usbd_control_xfer(rhport, p_request, &p_hid->idle_rate, 1);
+ tud_control_xfer(rhport, p_request, &p_hid->idle_rate, 1);
break;
case HID_REQ_CONTROL_GET_PROTOCOL:
{
uint8_t protocol = 1-p_hid->boot_mode; // 0 is Boot, 1 is Report protocol
- usbd_control_xfer(rhport, p_request, &protocol, 1);
+ tud_control_xfer(rhport, p_request, &protocol, 1);
}
break;
@@ -261,7 +261,7 @@ bool hidd_control_request(uint8_t rhport, tusb_control_request_t const * p_reque if (tud_hid_boot_mode_cb) tud_hid_boot_mode_cb(p_hid->boot_mode);
- usbd_control_status(rhport, p_request);
+ tud_control_status(rhport, p_request);
break;
default: return false; // stall unsupported request
@@ -276,7 +276,7 @@ bool hidd_control_request(uint8_t rhport, tusb_control_request_t const * p_reque // Invoked when class request DATA stage is finished.
// return false to stall control endpoint (e.g Host send non-sense DATA)
-bool hidd_control_request_complete(uint8_t rhport, tusb_control_request_t const * p_request)
+bool hidd_control_complete(uint8_t rhport, tusb_control_request_t const * p_request)
{
(void) rhport;
hidd_interface_t* p_hid = get_interface_by_itfnum( (uint8_t) p_request->wIndex );
diff --git a/src/class/hid/hid_device.h b/src/class/hid/hid_device.h index da239cc84..577b9ca8e 100644 --- a/src/class/hid/hid_device.h +++ b/src/class/hid/hid_device.h @@ -300,12 +300,12 @@ TU_ATTR_WEAK bool tud_hid_set_idle_cb(uint8_t idle_rate); //--------------------------------------------------------------------+
// Internal Class Driver API
//--------------------------------------------------------------------+
-void hidd_init(void);
-bool hidd_open(uint8_t rhport, tusb_desc_interface_t const * itf_desc, uint16_t *p_length);
-bool hidd_control_request(uint8_t rhport, tusb_control_request_t const * p_request);
-bool hidd_control_request_complete (uint8_t rhport, tusb_control_request_t const * p_request);
-bool hidd_xfer_cb(uint8_t rhport, uint8_t ep_addr, xfer_result_t event, uint32_t xferred_bytes);
-void hidd_reset(uint8_t rhport);
+void hidd_init (void);
+void hidd_reset (uint8_t rhport);
+bool hidd_open (uint8_t rhport, tusb_desc_interface_t const * itf_desc, uint16_t *p_length);
+bool hidd_control_request (uint8_t rhport, tusb_control_request_t const * request);
+bool hidd_control_complete (uint8_t rhport, tusb_control_request_t const * request);
+bool hidd_xfer_cb (uint8_t rhport, uint8_t ep_addr, xfer_result_t event, uint32_t xferred_bytes);
#ifdef __cplusplus
}
diff --git a/src/class/hid/hid_host.h b/src/class/hid/hid_host.h index 50eabba3e..1c619c570 100644 --- a/src/class/hid/hid_host.h +++ b/src/class/hid/hid_host.h @@ -55,7 +55,7 @@ extern uint8_t const hid_keycode_to_ascii_tbl[2][128]; // TODO used weak attr if * \retval true if device supports Keyboard interface
* \retval false if device does not support Keyboard interface or is not mounted
*/
-bool tuh_hid_keyboard_is_mounted(uint8_t dev_addr);
+bool tuh_hid_keyboard_is_mounted(uint8_t dev_addr);
/** \brief Check if the interface is currently busy or not
* \param[in] dev_addr device address
@@ -64,7 +64,7 @@ bool tuh_hid_keyboard_is_mounted(uint8_t dev_addr); * \note This function is primarily used for polling/waiting result after \ref tuh_hid_keyboard_get_report.
* Alternatively, asynchronous event API can be used
*/
-bool tuh_hid_keyboard_is_busy(uint8_t dev_addr);
+bool tuh_hid_keyboard_is_busy(uint8_t dev_addr);
/** \brief Perform a get report from Keyboard interface
* \param[in] dev_addr device address
diff --git a/src/class/midi/midi_device.c b/src/class/midi/midi_device.c index 267167720..03b0cae57 100644 --- a/src/class/midi/midi_device.c +++ b/src/class/midi/midi_device.c @@ -44,6 +44,7 @@ typedef struct uint8_t ep_in;
uint8_t ep_out;
+ /*------------- From this point, data is not cleared by bus reset -------------*/
// FIFO
tu_fifo_t rx_ff;
tu_fifo_t tx_ff;
@@ -77,7 +78,7 @@ CFG_TUSB_MEM_SECTION midid_interface_t _midid_itf[CFG_TUD_MIDI]; bool tud_midi_n_mounted (uint8_t itf)
{
midid_interface_t* midi = &_midid_itf[itf];
- return midi->itf_num != 0;
+ return midi->ep_in && midi->ep_out;
}
//--------------------------------------------------------------------+
@@ -127,7 +128,8 @@ void midi_rx_done_cb(midid_interface_t* midi, uint8_t const* buffer, uint32_t bu static bool maybe_transmit(midid_interface_t* midi, uint8_t itf_index)
{
- TU_VERIFY( !usbd_edpt_busy(TUD_OPT_RHPORT, midi->ep_in) ); // skip if previous transfer not complete
+ // skip if previous transfer not complete
+ TU_VERIFY( !usbd_edpt_busy(TUD_OPT_RHPORT, midi->ep_in) );
uint16_t count = tu_fifo_read_n(&midi->tx_ff, midi->epin_buf, CFG_TUD_MIDI_EPSIZE);
if (count > 0)
@@ -222,6 +224,7 @@ void midid_init(void) // config fifo
tu_fifo_config(&midi->rx_ff, midi->rx_ff_buf, CFG_TUD_MIDI_RX_BUFSIZE, 1, true);
tu_fifo_config(&midi->tx_ff, midi->tx_ff_buf, CFG_TUD_MIDI_TX_BUFSIZE, 1, true);
+
#if CFG_FIFO_MUTEX
tu_fifo_config_mutex(&midi->rx_ff, osal_mutex_create(&midi->rx_ff_mutex));
tu_fifo_config_mutex(&midi->tx_ff, osal_mutex_create(&midi->tx_ff_mutex));
@@ -275,7 +278,8 @@ bool midid_open(uint8_t rhport, tusb_desc_interface_t const * p_interface_desc, (*p_length) = sizeof(tusb_desc_interface_t);
uint8_t found_endpoints = 0;
- while (found_endpoints < p_interface_desc->bNumEndpoints) {
+ while (found_endpoints < p_interface_desc->bNumEndpoints)
+ {
if ( TUSB_DESC_ENDPOINT == p_desc[DESC_OFFSET_TYPE])
{
TU_ASSERT( dcd_edpt_open(rhport, (tusb_desc_endpoint_t const *) p_desc), false);
@@ -300,7 +304,7 @@ bool midid_open(uint8_t rhport, tusb_desc_interface_t const * p_interface_desc, return true;
}
-bool midid_control_request_complete(uint8_t rhport, tusb_control_request_t const * p_request)
+bool midid_control_complete(uint8_t rhport, tusb_control_request_t const * p_request)
{
return false;
}
@@ -313,20 +317,20 @@ bool midid_control_request(uint8_t rhport, tusb_control_request_t const * p_requ return false;
}
-bool midid_xfer_cb(uint8_t rhport, uint8_t edpt_addr, xfer_result_t result, uint32_t xferred_bytes)
+bool midid_xfer_cb(uint8_t rhport, uint8_t ep_addr, xfer_result_t result, uint32_t xferred_bytes)
{
// TODO Support multiple interfaces
uint8_t const itf = 0;
midid_interface_t* p_midi = &_midid_itf[itf];
// receive new data
- if ( edpt_addr == p_midi->ep_out )
+ if ( ep_addr == p_midi->ep_out )
{
midi_rx_done_cb(p_midi, p_midi->epout_buf, xferred_bytes);
// prepare for next
TU_ASSERT( usbd_edpt_xfer(rhport, p_midi->ep_out, p_midi->epout_buf, CFG_TUD_MIDI_EPSIZE), false );
- } else if ( edpt_addr == p_midi->ep_in ) {
+ } else if ( ep_addr == p_midi->ep_in ) {
maybe_transmit(p_midi, itf);
}
diff --git a/src/class/midi/midi_device.h b/src/class/midi/midi_device.h index 490abaf89..6d9b292a3 100644 --- a/src/class/midi/midi_device.h +++ b/src/class/midi/midi_device.h @@ -121,12 +121,12 @@ static inline uint32_t tudi_midi_write24 (uint8_t jack_id, uint8_t b1, uint8_t b //--------------------------------------------------------------------+
// Internal Class Driver API
//--------------------------------------------------------------------+
-void midid_init (void);
-bool midid_open (uint8_t rhport, tusb_desc_interface_t const * p_interface_desc, uint16_t *p_length);
-bool midid_control_request (uint8_t rhport, tusb_control_request_t const * p_request);
-bool midid_control_request_complete (uint8_t rhport, tusb_control_request_t const * p_request);
-bool midid_xfer_cb (uint8_t rhport, uint8_t edpt_addr, xfer_result_t result, uint32_t xferred_bytes);
-void midid_reset (uint8_t rhport);
+void midid_init (void);
+void midid_reset (uint8_t rhport);
+bool midid_open (uint8_t rhport, tusb_desc_interface_t const * itf_desc, uint16_t *p_length);
+bool midid_control_request (uint8_t rhport, tusb_control_request_t const * request);
+bool midid_control_complete (uint8_t rhport, tusb_control_request_t const * request);
+bool midid_xfer_cb (uint8_t rhport, uint8_t edpt_addr, xfer_result_t result, uint32_t xferred_bytes);
#ifdef __cplusplus
}
diff --git a/src/class/msc/msc_device.c b/src/class/msc/msc_device.c index 1e651459e..2ec8962ae 100644 --- a/src/class/msc/msc_device.c +++ b/src/class/msc/msc_device.c @@ -111,7 +111,7 @@ bool tud_msc_set_sense(uint8_t lun, uint8_t sense_key, uint8_t add_sense_code, u }
//--------------------------------------------------------------------+
-// USBD-CLASS API
+// USBD Driver API
//--------------------------------------------------------------------+
void mscd_init(void)
{
@@ -154,7 +154,7 @@ bool mscd_control_request(uint8_t rhport, tusb_control_request_t const * p_reque {
case MSC_REQ_RESET:
// TODO: Actually reset interface.
- usbd_control_status(rhport, p_request);
+ tud_control_status(rhport, p_request);
break;
case MSC_REQ_GET_MAX_LUN:
@@ -166,7 +166,7 @@ bool mscd_control_request(uint8_t rhport, tusb_control_request_t const * p_reque // MAX LUN is minus 1 by specs
maxlun--;
- usbd_control_xfer(rhport, p_request, &maxlun, 1);
+ tud_control_xfer(rhport, p_request, &maxlun, 1);
}
break;
@@ -178,10 +178,10 @@ bool mscd_control_request(uint8_t rhport, tusb_control_request_t const * p_reque // Invoked when class request DATA stage is finished.
// return false to stall control endpoint (e.g Host send non-sense DATA)
-bool mscd_control_request_complete(uint8_t rhport, tusb_control_request_t const * p_request)
+bool mscd_control_complete(uint8_t rhport, tusb_control_request_t const * request)
{
(void) rhport;
- (void) p_request;
+ (void) request;
// nothing to do
return true;
diff --git a/src/class/msc/msc_device.h b/src/class/msc/msc_device.h index c07008c55..8da9cfaa3 100644 --- a/src/class/msc/msc_device.h +++ b/src/class/msc/msc_device.h @@ -151,12 +151,12 @@ TU_ATTR_WEAK bool tud_msc_is_writable_cb(uint8_t lun); //--------------------------------------------------------------------+
// Internal Class Driver API
//--------------------------------------------------------------------+
-void mscd_init(void);
-bool mscd_open(uint8_t rhport, tusb_desc_interface_t const * itf_desc, uint16_t *p_length);
-bool mscd_control_request(uint8_t rhport, tusb_control_request_t const * p_request);
-bool mscd_control_request_complete (uint8_t rhport, tusb_control_request_t const * p_request);
-bool mscd_xfer_cb(uint8_t rhport, uint8_t ep_addr, xfer_result_t event, uint32_t xferred_bytes);
-void mscd_reset(uint8_t rhport);
+void mscd_init (void);
+void mscd_reset (uint8_t rhport);
+bool mscd_open (uint8_t rhport, tusb_desc_interface_t const * itf_desc, uint16_t *p_length);
+bool mscd_control_request (uint8_t rhport, tusb_control_request_t const * p_request);
+bool mscd_control_complete (uint8_t rhport, tusb_control_request_t const * p_request);
+bool mscd_xfer_cb (uint8_t rhport, uint8_t ep_addr, xfer_result_t event, uint32_t xferred_bytes);
#ifdef __cplusplus
}
diff --git a/src/class/vendor/vendor_device.c b/src/class/vendor/vendor_device.c new file mode 100644 index 000000000..33fb98470 --- /dev/null +++ b/src/class/vendor/vendor_device.c @@ -0,0 +1,214 @@ +/* + * The MIT License (MIT) + * + * Copyright (c) 2019 Ha Thach (tinyusb.org) + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + * THE SOFTWARE. + * + * This file is part of the TinyUSB stack. + */ + +#include "tusb_option.h" + +#if (TUSB_OPT_DEVICE_ENABLED && CFG_TUD_VENDOR) + +#include "vendor_device.h" +#include "device/usbd_pvt.h" + +//--------------------------------------------------------------------+ +// MACRO CONSTANT TYPEDEF +//--------------------------------------------------------------------+ +typedef struct +{ + uint8_t itf_num; + uint8_t ep_in; + uint8_t ep_out; + + /*------------- From this point, data is not cleared by bus reset -------------*/ + tu_fifo_t rx_ff; + tu_fifo_t tx_ff; + + uint8_t rx_ff_buf[CFG_TUD_VENDOR_RX_BUFSIZE]; + uint8_t tx_ff_buf[CFG_TUD_VENDOR_TX_BUFSIZE]; + +#if CFG_FIFO_MUTEX + osal_mutex_def_t rx_ff_mutex; + osal_mutex_def_t tx_ff_mutex; +#endif + + // Endpoint Transfer buffer + CFG_TUSB_MEM_ALIGN uint8_t epout_buf[CFG_TUD_VENDOR_EPSIZE]; + CFG_TUSB_MEM_ALIGN uint8_t epin_buf[CFG_TUD_VENDOR_EPSIZE]; +} vendord_interface_t; + +CFG_TUSB_MEM_SECTION static vendord_interface_t _vendord_itf[CFG_TUD_VENDOR]; + +#define ITF_MEM_RESET_SIZE offsetof(vendord_interface_t, rx_ff) + + + +bool tud_vendor_n_mounted (uint8_t itf) +{ + return _vendord_itf[itf].ep_in && _vendord_itf[itf].ep_out; +} + +uint32_t tud_vendor_n_available (uint8_t itf) +{ + return tu_fifo_count(&_vendord_itf[itf].rx_ff); +} + +//--------------------------------------------------------------------+ +// Read API +//--------------------------------------------------------------------+ +static void _prep_out_transaction (vendord_interface_t* p_itf) +{ + // skip if previous transfer not complete + if ( usbd_edpt_busy(TUD_OPT_RHPORT, p_itf->ep_out) ) return; + + // Prepare for incoming data but only allow what we can store in the ring buffer. + uint16_t max_read = tu_fifo_remaining(&p_itf->rx_ff); + if ( max_read >= CFG_TUD_VENDOR_EPSIZE ) + { + usbd_edpt_xfer(TUD_OPT_RHPORT, p_itf->ep_out, p_itf->epout_buf, CFG_TUD_VENDOR_EPSIZE); + } +} + +uint32_t tud_vendor_n_read (uint8_t itf, void* buffer, uint32_t bufsize) +{ + vendord_interface_t* p_itf = &_vendord_itf[itf]; + uint32_t num_read = tu_fifo_read_n(&p_itf->rx_ff, buffer, bufsize); + _prep_out_transaction(p_itf); + return num_read; +} + +//--------------------------------------------------------------------+ +// Write API +//--------------------------------------------------------------------+ +static bool maybe_transmit(vendord_interface_t* p_itf) +{ + // skip if previous transfer not complete + TU_VERIFY( !usbd_edpt_busy(TUD_OPT_RHPORT, p_itf->ep_in) ); + + uint16_t count = tu_fifo_read_n(&p_itf->tx_ff, p_itf->epin_buf, CFG_TUD_VENDOR_EPSIZE); + if (count > 0) + { + TU_ASSERT( usbd_edpt_xfer(TUD_OPT_RHPORT, p_itf->ep_in, p_itf->epin_buf, count) ); + } + return true; +} + +uint32_t tud_vendor_n_write (uint8_t itf, void const* buffer, uint32_t bufsize) +{ + vendord_interface_t* p_itf = &_vendord_itf[itf]; + uint16_t ret = tu_fifo_write_n(&p_itf->tx_ff, buffer, bufsize); + maybe_transmit(p_itf); + return ret; +} + +//--------------------------------------------------------------------+ +// USBD Driver API +//--------------------------------------------------------------------+ +void vendord_init(void) +{ + tu_memclr(_vendord_itf, sizeof(_vendord_itf)); + + for(uint8_t i=0; i<CFG_TUD_VENDOR; i++) + { + vendord_interface_t* p_itf = &_vendord_itf[i]; + + // config fifo + tu_fifo_config(&p_itf->rx_ff, p_itf->rx_ff_buf, CFG_TUD_VENDOR_RX_BUFSIZE, 1, false); + tu_fifo_config(&p_itf->tx_ff, p_itf->tx_ff_buf, CFG_TUD_VENDOR_TX_BUFSIZE, 1, false); + +#if CFG_FIFO_MUTEX + tu_fifo_config_mutex(&p_itf->rx_ff, osal_mutex_create(&p_itf->rx_ff_mutex)); + tu_fifo_config_mutex(&p_itf->tx_ff, osal_mutex_create(&p_itf->tx_ff_mutex)); +#endif + } +} + +void vendord_reset(uint8_t rhport) +{ + (void) rhport; + + for(uint8_t i=0; i<CFG_TUD_VENDOR; i++) + { + vendord_interface_t* p_itf = &_vendord_itf[i]; + + tu_memclr(p_itf, ITF_MEM_RESET_SIZE); + tu_fifo_clear(&p_itf->rx_ff); + tu_fifo_clear(&p_itf->tx_ff); + } +} + +bool vendord_open(uint8_t rhport, tusb_desc_interface_t const * itf_desc, uint16_t *p_len) +{ + // Find available interface + vendord_interface_t* p_vendor = NULL; + for(uint8_t i=0; i<CFG_TUD_VENDOR; i++) + { + if ( _vendord_itf[i].ep_in == 0 && _vendord_itf[i].ep_out == 0 ) + { + p_vendor = &_vendord_itf[i]; + break; + } + } + TU_VERIFY(p_vendor); + + // Open endpoint pair with usbd helper + TU_ASSERT(usbd_open_edpt_pair(rhport, tu_desc_next(itf_desc), 2, TUSB_XFER_BULK, &p_vendor->ep_out, &p_vendor->ep_in)); + + p_vendor->itf_num = itf_desc->bInterfaceNumber; + (*p_len) = sizeof(tusb_desc_interface_t) + 2*sizeof(tusb_desc_endpoint_t); + + // Prepare for incoming data + TU_ASSERT(usbd_edpt_xfer(rhport, p_vendor->ep_out, p_vendor->epout_buf, sizeof(p_vendor->epout_buf))); + + return true; +} + +bool vendord_xfer_cb(uint8_t rhport, uint8_t ep_addr, xfer_result_t result, uint32_t xferred_bytes) +{ + (void) rhport; + (void) result; + + // TODO Support multiple interfaces + uint8_t const itf = 0; + vendord_interface_t* p_itf = &_vendord_itf[itf]; + + if ( ep_addr == p_itf->ep_out ) + { + // Receive new data + tu_fifo_write_n(&p_itf->rx_ff, p_itf->epout_buf, xferred_bytes); + + // Invoked callback if any + if (tud_vendor_rx_cb) tud_vendor_rx_cb(itf); + + _prep_out_transaction(p_itf); + } + else if ( ep_addr == p_itf->ep_in ) + { + // Send complete, try to send more if possible + maybe_transmit(p_itf); + } + + return true; +} + +#endif diff --git a/src/class/vendor/vendor_device.h b/src/class/vendor/vendor_device.h new file mode 100644 index 000000000..d214c29ee --- /dev/null +++ b/src/class/vendor/vendor_device.h @@ -0,0 +1,121 @@ +/* + * The MIT License (MIT) + * + * Copyright (c) 2019 Ha Thach (tinyusb.org) + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + * THE SOFTWARE. + * + * This file is part of the TinyUSB stack. + */ + +#ifndef _TUSB_VENDOR_DEVICE_H_ +#define _TUSB_VENDOR_DEVICE_H_ + +#include "common/tusb_common.h" +#include "device/usbd.h" + +#ifndef CFG_TUD_VENDOR_EPSIZE +#define CFG_TUD_VENDOR_EPSIZE 64 +#endif + +#ifndef CFG_TUD_VENDOR_RX_BUFSIZE +#define CFG_TUD_VENDOR_RX_BUFSIZE 0 +#endif + +#ifndef CFG_TUD_VENDOR_TX_BUFSIZE +#define CFG_TUD_VENDOR_TX_BUFSIZE 0 +#endif + +#ifdef __cplusplus + extern "C" { +#endif + +//--------------------------------------------------------------------+ +// Application API (Multiple Interfaces) +//--------------------------------------------------------------------+ +bool tud_vendor_n_mounted (uint8_t itf); +uint32_t tud_vendor_n_available (uint8_t itf); +uint32_t tud_vendor_n_read (uint8_t itf, void* buffer, uint32_t bufsize); +uint32_t tud_vendor_n_write (uint8_t itf, void const* buffer, uint32_t bufsize); + +static inline uint32_t tud_vendor_n_write_str (uint8_t itf, char const* str); + +//--------------------------------------------------------------------+ +// Application API (Single Port) +//--------------------------------------------------------------------+ +static inline bool tud_vendor_mounted (void); +static inline uint32_t tud_vendor_available (void); +static inline uint32_t tud_vendor_read (void* buffer, uint32_t bufsize); +static inline uint32_t tud_vendor_write (void const* buffer, uint32_t bufsize); +static inline uint32_t tud_vendor_write_str (char const* str); + +//--------------------------------------------------------------------+ +// Application Callback API (weak is optional) +//--------------------------------------------------------------------+ + +// Invoked when received new data +TU_ATTR_WEAK void tud_vendor_rx_cb(uint8_t itf); + +//--------------------------------------------------------------------+ +// Inline Functions +//--------------------------------------------------------------------+ + +static inline uint32_t tud_vendor_n_write_str (uint8_t itf, char const* str) +{ + return tud_vendor_n_write(itf, str, strlen(str)); +} + +static inline bool tud_vendor_mounted (void) +{ + return tud_vendor_n_mounted(0); +} + +static inline uint32_t tud_vendor_available (void) +{ + return tud_vendor_n_available(0); +} + +static inline uint32_t tud_vendor_read (void* buffer, uint32_t bufsize) +{ + return tud_vendor_n_read(0, buffer, bufsize); +} + +static inline uint32_t tud_vendor_write (void const* buffer, uint32_t bufsize) +{ + return tud_vendor_n_write(0, buffer, bufsize); +} + +static inline uint32_t tud_vendor_write_str (char const* str) +{ + return tud_vendor_n_write_str(0, str); +} + +//--------------------------------------------------------------------+ +// Internal Class Driver API +//--------------------------------------------------------------------+ +void vendord_init(void); +void vendord_reset(uint8_t rhport); +bool vendord_open(uint8_t rhport, tusb_desc_interface_t const * itf_desc, uint16_t *p_length); +bool vendord_xfer_cb(uint8_t rhport, uint8_t ep_addr, xfer_result_t event, uint32_t xferred_bytes); + +#ifdef __cplusplus + } +#endif + +#endif /* _TUSB_VENDOR_DEVICE_H_ */ diff --git a/src/class/custom/custom_host.c b/src/class/vendor/vendor_host.c index a4f04af31..3e8dac0f6 100644 --- a/src/class/custom/custom_host.c +++ b/src/class/vendor/vendor_host.c @@ -26,13 +26,13 @@ #include "tusb_option.h" -#if (TUSB_OPT_HOST_ENABLED && CFG_TUSB_HOST_CUSTOM_CLASS) +#if (TUSB_OPT_HOST_ENABLED && CFG_TUH_VENDOR) //--------------------------------------------------------------------+ // INCLUDE //--------------------------------------------------------------------+ #include "common/tusb_common.h" -#include "custom_host.h" +#include "vendor_host.h" //--------------------------------------------------------------------+ // MACRO CONSTANT TYPEDEF diff --git a/src/class/custom/custom_host.h b/src/class/vendor/vendor_host.h index 2ca16222a..fa1879376 100644 --- a/src/class/custom/custom_host.h +++ b/src/class/vendor/vendor_host.h @@ -28,8 +28,8 @@ * \defgroup Group_Custom Custom Class (not supported yet) * @{ */ -#ifndef _TUSB_CUSTOM_HOST_H_ -#define _TUSB_CUSTOM_HOST_H_ +#ifndef _TUSB_VENDOR_HOST_H_ +#define _TUSB_VENDOR_HOST_H_ #include "common/tusb_common.h" #include "host/usbh.h" @@ -69,6 +69,6 @@ void cush_close(uint8_t dev_addr); } #endif -#endif /* _TUSB_CUSTOM_HOST_H_ */ +#endif /* _TUSB_VENDOR_HOST_H_ */ /** @} */ |
