Merge branch 'master' into dcd_notif
Signed-off-by: HiFiPhile <admin@hifiphile.com>
This commit is contained in:
@@ -490,13 +490,13 @@ TU_ATTR_WEAK bool tud_audio_feedback_format_correction_cb(uint8_t func_id) {
|
||||
(void) func_id;
|
||||
return CFG_TUD_AUDIO_ENABLE_FEEDBACK_FORMAT_CORRECTION;
|
||||
}
|
||||
#endif
|
||||
|
||||
TU_ATTR_WEAK TU_ATTR_FAST_FUNC void tud_audio_feedback_interval_isr(uint8_t func_id, uint32_t frame_number, uint8_t interval_shift) {
|
||||
(void) func_id;
|
||||
(void) frame_number;
|
||||
(void) interval_shift;
|
||||
}
|
||||
#endif
|
||||
|
||||
#if CFG_TUD_AUDIO_ENABLE_INTERRUPT_EP
|
||||
TU_ATTR_WEAK void tud_audio_int_done_cb(uint8_t rhport) {
|
||||
@@ -1810,7 +1810,8 @@ static bool audiod_get_interface(uint8_t rhport, tusb_control_request_t const *
|
||||
uint8_t const *dummy;
|
||||
|
||||
TU_VERIFY(audiod_get_AS_interface_index_global(itf, &func_id, &idxItf, &dummy));
|
||||
TU_VERIFY(tud_control_xfer(rhport, p_request, &_audiod_fct[func_id].alt_setting[idxItf], 1));
|
||||
_audiod_fct[func_id].ctrl_buf[0] = _audiod_fct[func_id].alt_setting[idxItf];
|
||||
TU_VERIFY(tud_control_xfer(rhport, p_request, _audiod_fct[func_id].ctrl_buf, 1));
|
||||
|
||||
TU_LOG2(" Get itf: %u - current alt: %u\r\n", itf, _audiod_fct[func_id].alt_setting[idxItf]);
|
||||
|
||||
@@ -2137,6 +2138,13 @@ static bool audiod_control_complete(uint8_t rhport, tusb_control_request_t const
|
||||
// Check if entity is present and get corresponding driver index
|
||||
TU_VERIFY(audiod_verify_entity_exists(itf, entityID, &func_id));
|
||||
|
||||
#if CFG_TUD_AUDIO_ENABLE_EP_IN && CFG_TUD_AUDIO_EP_IN_FLOW_CONTROL
|
||||
uint8_t ctrlSel = TU_U16_HIGH(p_request->wValue);
|
||||
if (_audiod_fct[func_id].bclock_id_tx == entityID && ctrlSel == AUDIO_CS_CTRL_SAM_FREQ && p_request->bRequest == AUDIO_CS_REQ_CUR) {
|
||||
_audiod_fct[func_id].sample_rate_tx = tu_unaligned_read32(_audiod_fct[func_id].ctrl_buf);
|
||||
}
|
||||
#endif
|
||||
|
||||
// Invoke callback
|
||||
return tud_audio_set_req_entity_cb(rhport, p_request, _audiod_fct[func_id].ctrl_buf);
|
||||
}
|
||||
|
||||
@@ -37,8 +37,7 @@
|
||||
//--------------------------------------------------------------------+
|
||||
// MACRO CONSTANT TYPEDEF
|
||||
//--------------------------------------------------------------------+
|
||||
typedef struct
|
||||
{
|
||||
typedef struct {
|
||||
uint8_t itf_num;
|
||||
uint8_t ep_ev;
|
||||
uint8_t ep_acl_in;
|
||||
@@ -49,17 +48,18 @@ typedef struct
|
||||
|
||||
// Previous amount of bytes sent when issuing ZLP
|
||||
uint32_t prev_xferred_bytes;
|
||||
|
||||
// Endpoint Transfer buffer
|
||||
CFG_TUSB_MEM_ALIGN bt_hci_cmd_t hci_cmd;
|
||||
CFG_TUSB_MEM_ALIGN uint8_t epout_buf[CFG_TUD_BTH_DATA_EPSIZE];
|
||||
|
||||
} btd_interface_t;
|
||||
|
||||
typedef struct {
|
||||
TUD_EPBUF_DEF(epout_buf, CFG_TUD_BTH_DATA_EPSIZE);
|
||||
TUD_EPBUF_TYPE_DEF(bt_hci_cmd_t, hci_cmd);
|
||||
} btd_epbuf_t;
|
||||
|
||||
//--------------------------------------------------------------------+
|
||||
// INTERNAL OBJECT & FUNCTION DECLARATION
|
||||
//--------------------------------------------------------------------+
|
||||
CFG_TUD_MEM_SECTION btd_interface_t _btd_itf;
|
||||
static btd_interface_t _btd_itf;
|
||||
CFG_TUD_MEM_SECTION static btd_epbuf_t _btd_epbuf;
|
||||
|
||||
static bool bt_tx_data(uint8_t ep, void *data, uint16_t len)
|
||||
{
|
||||
@@ -152,7 +152,7 @@ uint16_t btd_open(uint8_t rhport, tusb_desc_interface_t const *itf_desc, uint16_
|
||||
itf_desc = (tusb_desc_interface_t const *)tu_desc_next(tu_desc_next(desc_ep));
|
||||
|
||||
// Prepare for incoming data from host
|
||||
TU_ASSERT(usbd_edpt_xfer(rhport, _btd_itf.ep_acl_out, _btd_itf.epout_buf, CFG_TUD_BTH_DATA_EPSIZE), 0);
|
||||
TU_ASSERT(usbd_edpt_xfer(rhport, _btd_itf.ep_acl_out, _btd_epbuf.epout_buf, CFG_TUD_BTH_DATA_EPSIZE), 0);
|
||||
|
||||
drv_len = hci_itf_size;
|
||||
|
||||
@@ -243,14 +243,16 @@ bool btd_control_xfer_cb(uint8_t rhport, uint8_t stage, tusb_control_request_t c
|
||||
}
|
||||
else return false;
|
||||
|
||||
return tud_control_xfer(rhport, request, &_btd_itf.hci_cmd, sizeof(_btd_itf.hci_cmd));
|
||||
return tud_control_xfer(rhport, request, &_btd_epbuf.hci_cmd, sizeof(bt_hci_cmd_t));
|
||||
}
|
||||
else if ( stage == CONTROL_STAGE_DATA )
|
||||
{
|
||||
// Handle class request only
|
||||
TU_VERIFY(request->bmRequestType_bit.type == TUSB_REQ_TYPE_CLASS);
|
||||
|
||||
if (tud_bt_hci_cmd_cb) tud_bt_hci_cmd_cb(&_btd_itf.hci_cmd, tu_min16(request->wLength, sizeof(_btd_itf.hci_cmd)));
|
||||
if (tud_bt_hci_cmd_cb) {
|
||||
tud_bt_hci_cmd_cb(&_btd_epbuf.hci_cmd, tu_min16(request->wLength, sizeof(bt_hci_cmd_t)));
|
||||
}
|
||||
}
|
||||
|
||||
return true;
|
||||
@@ -261,10 +263,10 @@ bool btd_xfer_cb(uint8_t rhport, uint8_t ep_addr, xfer_result_t result,
|
||||
// received new data from host
|
||||
if (ep_addr == _btd_itf.ep_acl_out)
|
||||
{
|
||||
if (tud_bt_acl_data_received_cb) tud_bt_acl_data_received_cb(_btd_itf.epout_buf, xferred_bytes);
|
||||
if (tud_bt_acl_data_received_cb) tud_bt_acl_data_received_cb(_btd_epbuf.epout_buf, xferred_bytes);
|
||||
|
||||
// prepare for next data
|
||||
TU_ASSERT(usbd_edpt_xfer(rhport, _btd_itf.ep_acl_out, _btd_itf.epout_buf, CFG_TUD_BTH_DATA_EPSIZE));
|
||||
TU_ASSERT(usbd_edpt_xfer(rhport, _btd_itf.ep_acl_out, _btd_epbuf.epout_buf, CFG_TUD_BTH_DATA_EPSIZE));
|
||||
}
|
||||
else if (ep_addr == _btd_itf.ep_ev)
|
||||
{
|
||||
|
||||
@@ -68,22 +68,28 @@ typedef struct {
|
||||
|
||||
OSAL_MUTEX_DEF(rx_ff_mutex);
|
||||
OSAL_MUTEX_DEF(tx_ff_mutex);
|
||||
|
||||
// Endpoint Transfer buffer
|
||||
CFG_TUSB_MEM_ALIGN cdc_notif_serial_state_t serial_state_buf;
|
||||
CFG_TUSB_MEM_ALIGN uint8_t epout_buf[CFG_TUD_CDC_EP_BUFSIZE];
|
||||
CFG_TUSB_MEM_ALIGN uint8_t epin_buf[CFG_TUD_CDC_EP_BUFSIZE];
|
||||
} cdcd_interface_t;
|
||||
|
||||
#define ITF_MEM_RESET_SIZE offsetof(cdcd_interface_t, wanted_char)
|
||||
|
||||
typedef struct {
|
||||
TUD_EPBUF_DEF(epout, CFG_TUD_CDC_EP_BUFSIZE);
|
||||
TUD_EPBUF_DEF(epin, CFG_TUD_CDC_EP_BUFSIZE);
|
||||
TUD_EPBUF_TYPE_DEF(cdc_notif_serial_state_t, epnotif);
|
||||
} cdcd_epbuf_t;
|
||||
|
||||
//--------------------------------------------------------------------+
|
||||
// INTERNAL OBJECT & FUNCTION DECLARATION
|
||||
//--------------------------------------------------------------------+
|
||||
CFG_TUD_MEM_SECTION static cdcd_interface_t _cdcd_itf[CFG_TUD_CDC];
|
||||
static cdcd_interface_t _cdcd_itf[CFG_TUD_CDC];
|
||||
CFG_TUD_MEM_SECTION static cdcd_epbuf_t _cdcd_epbuf[CFG_TUD_CDC];
|
||||
|
||||
static tud_cdc_configure_fifo_t _cdcd_fifo_cfg;
|
||||
|
||||
static bool _prep_out_transaction (cdcd_interface_t* p_cdc) {
|
||||
static bool _prep_out_transaction(uint8_t itf) {
|
||||
const uint8_t rhport = 0;
|
||||
cdcd_interface_t* p_cdc = &_cdcd_itf[itf];
|
||||
cdcd_epbuf_t* p_epbuf = &_cdcd_epbuf[itf];
|
||||
|
||||
// Skip if usb is not ready yet
|
||||
TU_VERIFY(tud_ready() && p_cdc->ep_out);
|
||||
@@ -94,7 +100,7 @@ static bool _prep_out_transaction (cdcd_interface_t* p_cdc) {
|
||||
// TODO Actually we can still carry out the transfer, keeping count of received bytes
|
||||
// and slowly move it to the FIFO when read().
|
||||
// This pre-check reduces endpoint claiming
|
||||
TU_VERIFY(available >= sizeof(p_cdc->epout_buf));
|
||||
TU_VERIFY(available >= CFG_TUD_CDC_EP_BUFSIZE);
|
||||
|
||||
// claim endpoint
|
||||
TU_VERIFY(usbd_edpt_claim(p_cdc->rhport, p_cdc->ep_out));
|
||||
@@ -102,9 +108,9 @@ static bool _prep_out_transaction (cdcd_interface_t* p_cdc) {
|
||||
// fifo can be changed before endpoint is claimed
|
||||
available = tu_fifo_remaining(&p_cdc->rx_ff);
|
||||
|
||||
if ( available >= sizeof(p_cdc->epout_buf) ) {
|
||||
return usbd_edpt_xfer(p_cdc->rhport, p_cdc->ep_out, p_cdc->epout_buf, sizeof(p_cdc->epout_buf));
|
||||
}else {
|
||||
if (available >= CFG_TUD_CDC_EP_BUFSIZE) {
|
||||
return usbd_edpt_xfer(rhport, p_cdc->ep_out, p_epbuf->epout, CFG_TUD_CDC_EP_BUFSIZE);
|
||||
} else {
|
||||
// Release endpoint since we don't make any transfer
|
||||
usbd_edpt_release(p_cdc->rhport, p_cdc->ep_out);
|
||||
return false;
|
||||
@@ -115,7 +121,7 @@ static bool _prep_out_transaction (cdcd_interface_t* p_cdc) {
|
||||
// APPLICATION API
|
||||
//--------------------------------------------------------------------+
|
||||
|
||||
bool tud_cdc_configure_fifo(tud_cdc_configure_fifo_t const* cfg) {
|
||||
bool tud_cdc_configure_fifo(const tud_cdc_configure_fifo_t* cfg) {
|
||||
TU_VERIFY(cfg);
|
||||
_cdcd_fifo_cfg = (*cfg);
|
||||
return true;
|
||||
@@ -140,6 +146,7 @@ void tud_cdc_n_get_line_coding(uint8_t itf, cdc_line_coding_t* coding) {
|
||||
|
||||
bool tud_cdc_n_send_uart_state (uint8_t itf, cdc_uart_state_t state) {
|
||||
cdcd_interface_t* p_cdc = &_cdcd_itf[itf];
|
||||
cdcd_epbuf_t* p_epbuf = &_cdcd_epbuf[itf];
|
||||
|
||||
// Skip if usb is not ready yet
|
||||
TU_VERIFY(tud_ready(), 0);
|
||||
@@ -147,15 +154,15 @@ bool tud_cdc_n_send_uart_state (uint8_t itf, cdc_uart_state_t state) {
|
||||
// claim endpoint
|
||||
TU_VERIFY(usbd_edpt_claim(p_cdc->rhport, p_cdc->ep_notif));
|
||||
|
||||
p_cdc->serial_state_buf.bmRequestType = CDC_REQ_TYPE_NOTIF;
|
||||
p_cdc->serial_state_buf.bNotification = CDC_NOTIF_SERIAL_STATE;
|
||||
p_cdc->serial_state_buf.wValue = 0;
|
||||
p_cdc->serial_state_buf.wIndex = p_cdc->itf_num;
|
||||
p_cdc->serial_state_buf.wLength = 2;
|
||||
p_cdc->serial_state_buf.bmUartState = state;
|
||||
p_epbuf->epnotif.bmRequestType = CDC_REQ_TYPE_NOTIF;
|
||||
p_epbuf->epnotif.bNotification = CDC_NOTIF_SERIAL_STATE;
|
||||
p_epbuf->epnotif.wValue = 0;
|
||||
p_epbuf->epnotif.wIndex = p_cdc->itf_num;
|
||||
p_epbuf->epnotif.wLength = 2;
|
||||
p_epbuf->epnotif.bmUartState = state;
|
||||
|
||||
// transfer
|
||||
return usbd_edpt_xfer(p_cdc->rhport, p_cdc->ep_notif, (uint8_t *)&p_cdc->serial_state_buf, sizeof(p_cdc->serial_state_buf));
|
||||
return usbd_edpt_xfer(p_cdc->rhport, p_cdc->ep_notif, (uint8_t *)&p_epbuf->epnotif, sizeof(cdc_notif_serial_state_t));
|
||||
}
|
||||
|
||||
void tud_cdc_n_set_wanted_char(uint8_t itf, char wanted) {
|
||||
@@ -172,7 +179,7 @@ uint32_t tud_cdc_n_available(uint8_t itf) {
|
||||
uint32_t tud_cdc_n_read(uint8_t itf, void* buffer, uint32_t bufsize) {
|
||||
cdcd_interface_t* p_cdc = &_cdcd_itf[itf];
|
||||
uint32_t num_read = tu_fifo_read_n(&p_cdc->rx_ff, buffer, (uint16_t) TU_MIN(bufsize, UINT16_MAX));
|
||||
_prep_out_transaction(p_cdc);
|
||||
_prep_out_transaction(itf);
|
||||
return num_read;
|
||||
}
|
||||
|
||||
@@ -183,13 +190,13 @@ bool tud_cdc_n_peek(uint8_t itf, uint8_t* chr) {
|
||||
void tud_cdc_n_read_flush(uint8_t itf) {
|
||||
cdcd_interface_t* p_cdc = &_cdcd_itf[itf];
|
||||
tu_fifo_clear(&p_cdc->rx_ff);
|
||||
_prep_out_transaction(p_cdc);
|
||||
_prep_out_transaction(itf);
|
||||
}
|
||||
|
||||
//--------------------------------------------------------------------+
|
||||
// WRITE API
|
||||
//--------------------------------------------------------------------+
|
||||
uint32_t tud_cdc_n_write(uint8_t itf, void const* buffer, uint32_t bufsize) {
|
||||
uint32_t tud_cdc_n_write(uint8_t itf, const void* buffer, uint32_t bufsize) {
|
||||
cdcd_interface_t* p_cdc = &_cdcd_itf[itf];
|
||||
uint16_t ret = tu_fifo_write_n(&p_cdc->tx_ff, buffer, (uint16_t) TU_MIN(bufsize, UINT16_MAX));
|
||||
|
||||
@@ -207,21 +214,26 @@ uint32_t tud_cdc_n_write(uint8_t itf, void const* buffer, uint32_t bufsize) {
|
||||
|
||||
uint32_t tud_cdc_n_write_flush(uint8_t itf) {
|
||||
cdcd_interface_t* p_cdc = &_cdcd_itf[itf];
|
||||
cdcd_epbuf_t* p_epbuf = &_cdcd_epbuf[itf];
|
||||
|
||||
// Skip if usb is not ready yet
|
||||
TU_VERIFY(tud_ready(), 0);
|
||||
|
||||
// No data to send
|
||||
if (!tu_fifo_count(&p_cdc->tx_ff)) return 0;
|
||||
if (!tu_fifo_count(&p_cdc->tx_ff)) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
const uint8_t rhport = 0;
|
||||
|
||||
// Claim the endpoint
|
||||
TU_VERIFY(usbd_edpt_claim(p_cdc->rhport, p_cdc->ep_in), 0);
|
||||
|
||||
// Pull data from FIFO
|
||||
uint16_t const count = tu_fifo_read_n(&p_cdc->tx_ff, p_cdc->epin_buf, sizeof(p_cdc->epin_buf));
|
||||
const uint16_t count = tu_fifo_read_n(&p_cdc->tx_ff, p_epbuf->epin, CFG_TUD_CDC_EP_BUFSIZE);
|
||||
|
||||
if (count) {
|
||||
TU_ASSERT(usbd_edpt_xfer(p_cdc->rhport, p_cdc->ep_in, p_cdc->epin_buf, count), 0);
|
||||
TU_ASSERT(usbd_edpt_xfer(rhport, p_cdc->ep_in, p_epbuf->epin, count), 0);
|
||||
return count;
|
||||
} else {
|
||||
// Release endpoint since we don't make any transfer
|
||||
@@ -305,33 +317,38 @@ void cdcd_reset(uint8_t rhport) {
|
||||
cdcd_interface_t* p_cdc = &_cdcd_itf[i];
|
||||
|
||||
tu_memclr(p_cdc, ITF_MEM_RESET_SIZE);
|
||||
if (!_cdcd_fifo_cfg.rx_persistent) tu_fifo_clear(&p_cdc->rx_ff);
|
||||
if (!_cdcd_fifo_cfg.tx_persistent) tu_fifo_clear(&p_cdc->tx_ff);
|
||||
if (!_cdcd_fifo_cfg.rx_persistent) {
|
||||
tu_fifo_clear(&p_cdc->rx_ff);
|
||||
}
|
||||
if (!_cdcd_fifo_cfg.tx_persistent) {
|
||||
tu_fifo_clear(&p_cdc->tx_ff);
|
||||
}
|
||||
tu_fifo_set_overwritable(&p_cdc->tx_ff, true);
|
||||
}
|
||||
}
|
||||
|
||||
uint16_t cdcd_open(uint8_t rhport, tusb_desc_interface_t const * itf_desc, uint16_t max_len) {
|
||||
uint16_t cdcd_open(uint8_t rhport, const tusb_desc_interface_t* itf_desc, uint16_t max_len) {
|
||||
// Only support ACM subclass
|
||||
TU_VERIFY( TUSB_CLASS_CDC == itf_desc->bInterfaceClass &&
|
||||
CDC_COMM_SUBCLASS_ABSTRACT_CONTROL_MODEL == itf_desc->bInterfaceSubClass, 0);
|
||||
|
||||
// Find available interface
|
||||
cdcd_interface_t* p_cdc = NULL;
|
||||
for (uint8_t cdc_id = 0; cdc_id < CFG_TUD_CDC; cdc_id++) {
|
||||
if (_cdcd_itf[cdc_id].ep_in == 0) {
|
||||
p_cdc = &_cdcd_itf[cdc_id];
|
||||
cdcd_interface_t* p_cdc;
|
||||
uint8_t cdc_id;
|
||||
for (cdc_id = 0; cdc_id < CFG_TUD_CDC; cdc_id++) {
|
||||
p_cdc = &_cdcd_itf[cdc_id];
|
||||
if (p_cdc->ep_in == 0) {
|
||||
break;
|
||||
}
|
||||
}
|
||||
TU_ASSERT(p_cdc, 0);
|
||||
TU_ASSERT(cdc_id < CFG_TUD_CDC, 0);
|
||||
|
||||
//------------- Control Interface -------------//
|
||||
p_cdc->rhport = rhport;
|
||||
p_cdc->itf_num = itf_desc->bInterfaceNumber;
|
||||
|
||||
uint16_t drv_len = sizeof(tusb_desc_interface_t);
|
||||
uint8_t const* p_desc = tu_desc_next(itf_desc);
|
||||
const uint8_t* p_desc = tu_desc_next(itf_desc);
|
||||
|
||||
// Communication Functional Descriptors
|
||||
while (TUSB_DESC_CS_INTERFACE == tu_desc_type(p_desc) && drv_len <= max_len) {
|
||||
@@ -341,7 +358,7 @@ uint16_t cdcd_open(uint8_t rhport, tusb_desc_interface_t const * itf_desc, uint1
|
||||
|
||||
if (TUSB_DESC_ENDPOINT == tu_desc_type(p_desc)) {
|
||||
// notification endpoint
|
||||
tusb_desc_endpoint_t const* desc_ep = (tusb_desc_endpoint_t const*) p_desc;
|
||||
const tusb_desc_endpoint_t* desc_ep = (const tusb_desc_endpoint_t*) p_desc;
|
||||
|
||||
TU_ASSERT(usbd_edpt_open(rhport, desc_ep), 0);
|
||||
p_cdc->ep_notif = desc_ep->bEndpointAddress;
|
||||
@@ -352,7 +369,7 @@ uint16_t cdcd_open(uint8_t rhport, tusb_desc_interface_t const * itf_desc, uint1
|
||||
|
||||
//------------- Data Interface (if any) -------------//
|
||||
if ((TUSB_DESC_INTERFACE == tu_desc_type(p_desc)) &&
|
||||
(TUSB_CLASS_CDC_DATA == ((tusb_desc_interface_t const*) p_desc)->bInterfaceClass)) {
|
||||
(TUSB_CLASS_CDC_DATA == ((const tusb_desc_interface_t*) p_desc)->bInterfaceClass)) {
|
||||
// next to endpoint descriptor
|
||||
drv_len += tu_desc_len(p_desc);
|
||||
p_desc = tu_desc_next(p_desc);
|
||||
@@ -364,7 +381,7 @@ uint16_t cdcd_open(uint8_t rhport, tusb_desc_interface_t const * itf_desc, uint1
|
||||
}
|
||||
|
||||
// Prepare for incoming data
|
||||
_prep_out_transaction(p_cdc);
|
||||
_prep_out_transaction(cdc_id);
|
||||
|
||||
return drv_len;
|
||||
}
|
||||
@@ -372,19 +389,21 @@ uint16_t cdcd_open(uint8_t rhport, tusb_desc_interface_t const * itf_desc, uint1
|
||||
// Invoked when a control transfer occurred on an interface of this class
|
||||
// Driver response accordingly to the request and the transfer stage (setup/data/ack)
|
||||
// return false to stall control endpoint (e.g unsupported request)
|
||||
bool cdcd_control_xfer_cb(uint8_t rhport, uint8_t stage, tusb_control_request_t const* request) {
|
||||
bool cdcd_control_xfer_cb(uint8_t rhport, uint8_t stage, const tusb_control_request_t* request) {
|
||||
// Handle class request only
|
||||
TU_VERIFY(request->bmRequestType_bit.type == TUSB_REQ_TYPE_CLASS);
|
||||
|
||||
uint8_t itf = 0;
|
||||
cdcd_interface_t* p_cdc = _cdcd_itf;
|
||||
uint8_t itf;
|
||||
cdcd_interface_t* p_cdc;
|
||||
|
||||
// Identify which interface to use
|
||||
for (;; itf++, p_cdc++) {
|
||||
if (itf >= TU_ARRAY_SIZE(_cdcd_itf)) return false;
|
||||
|
||||
if (p_cdc->itf_num == request->wIndex) break;
|
||||
for (itf = 0; itf < CFG_TUD_CDC; itf++) {
|
||||
p_cdc = &_cdcd_itf[itf];
|
||||
if (p_cdc->itf_num == request->wIndex) {
|
||||
break;
|
||||
}
|
||||
}
|
||||
TU_VERIFY(itf < CFG_TUD_CDC);
|
||||
|
||||
switch (request->bRequest) {
|
||||
case CDC_REQUEST_SET_LINE_CODING:
|
||||
@@ -392,7 +411,9 @@ bool cdcd_control_xfer_cb(uint8_t rhport, uint8_t stage, tusb_control_request_t
|
||||
TU_LOG_DRV(" Set Line Coding\r\n");
|
||||
tud_control_xfer(rhport, request, &p_cdc->line_coding, sizeof(cdc_line_coding_t));
|
||||
} else if (stage == CONTROL_STAGE_ACK) {
|
||||
if (tud_cdc_line_coding_cb) tud_cdc_line_coding_cb(itf, &p_cdc->line_coding);
|
||||
if (tud_cdc_line_coding_cb) {
|
||||
tud_cdc_line_coding_cb(itf, &p_cdc->line_coding);
|
||||
}
|
||||
}
|
||||
break;
|
||||
|
||||
@@ -423,7 +444,9 @@ bool cdcd_control_xfer_cb(uint8_t rhport, uint8_t stage, tusb_control_request_t
|
||||
TU_LOG_DRV(" Set Control Line State: DTR = %d, RTS = %d\r\n", dtr, rts);
|
||||
|
||||
// Invoke callback
|
||||
if (tud_cdc_line_state_cb) tud_cdc_line_state_cb(itf, dtr, rts);
|
||||
if (tud_cdc_line_state_cb) {
|
||||
tud_cdc_line_state_cb(itf, dtr, rts);
|
||||
}
|
||||
}
|
||||
break;
|
||||
|
||||
@@ -432,7 +455,9 @@ bool cdcd_control_xfer_cb(uint8_t rhport, uint8_t stage, tusb_control_request_t
|
||||
tud_control_status(rhport, request);
|
||||
} else if (stage == CONTROL_STAGE_ACK) {
|
||||
TU_LOG_DRV(" Send Break\r\n");
|
||||
if (tud_cdc_send_break_cb) tud_cdc_send_break_cb(itf, request->wValue);
|
||||
if (tud_cdc_send_break_cb) {
|
||||
tud_cdc_send_break_cb(itf, request->wValue);
|
||||
}
|
||||
}
|
||||
break;
|
||||
|
||||
@@ -452,28 +477,33 @@ bool cdcd_xfer_cb(uint8_t rhport, uint8_t ep_addr, xfer_result_t result, uint32_
|
||||
// Identify which interface to use
|
||||
for (itf = 0; itf < CFG_TUD_CDC; itf++) {
|
||||
p_cdc = &_cdcd_itf[itf];
|
||||
if ((ep_addr == p_cdc->ep_out) || (ep_addr == p_cdc->ep_in)|| (ep_addr == p_cdc->ep_notif)) break;
|
||||
if ((ep_addr == p_cdc->ep_out) || (ep_addr == p_cdc->ep_in) || (ep_addr == p_cdc->ep_notif)) {
|
||||
break;
|
||||
}
|
||||
}
|
||||
TU_ASSERT(itf < CFG_TUD_CDC);
|
||||
cdcd_epbuf_t* p_epbuf = &_cdcd_epbuf[itf];
|
||||
|
||||
// Received new data
|
||||
if (ep_addr == p_cdc->ep_out) {
|
||||
tu_fifo_write_n(&p_cdc->rx_ff, p_cdc->epout_buf, (uint16_t) xferred_bytes);
|
||||
tu_fifo_write_n(&p_cdc->rx_ff, p_epbuf->epout, (uint16_t) xferred_bytes);
|
||||
|
||||
// Check for wanted char and invoke callback if needed
|
||||
if (tud_cdc_rx_wanted_cb && (((signed char) p_cdc->wanted_char) != -1)) {
|
||||
for (uint32_t i = 0; i < xferred_bytes; i++) {
|
||||
if ((p_cdc->wanted_char == p_cdc->epout_buf[i]) && !tu_fifo_empty(&p_cdc->rx_ff)) {
|
||||
if ((p_cdc->wanted_char == p_epbuf->epout[i]) && !tu_fifo_empty(&p_cdc->rx_ff)) {
|
||||
tud_cdc_rx_wanted_cb(itf, p_cdc->wanted_char);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// invoke receive callback (if there is still data)
|
||||
if (tud_cdc_rx_cb && !tu_fifo_empty(&p_cdc->rx_ff)) tud_cdc_rx_cb(itf);
|
||||
if (tud_cdc_rx_cb && !tu_fifo_empty(&p_cdc->rx_ff)) {
|
||||
tud_cdc_rx_cb(itf);
|
||||
}
|
||||
|
||||
// prepare for OUT transaction
|
||||
_prep_out_transaction(p_cdc);
|
||||
_prep_out_transaction(itf);
|
||||
}
|
||||
|
||||
// Data sent to host, we continue to fetch from tx fifo to send.
|
||||
@@ -481,7 +511,9 @@ bool cdcd_xfer_cb(uint8_t rhport, uint8_t ep_addr, xfer_result_t result, uint32_
|
||||
// Though maybe the baudrate is not really important !!!
|
||||
if (ep_addr == p_cdc->ep_in) {
|
||||
// invoke transmit callback to possibly refill tx fifo
|
||||
if (tud_cdc_tx_complete_cb) tud_cdc_tx_complete_cb(itf);
|
||||
if (tud_cdc_tx_complete_cb) {
|
||||
tud_cdc_tx_complete_cb(itf);
|
||||
}
|
||||
|
||||
if (0 == tud_cdc_n_write_flush(itf)) {
|
||||
// If there is no data left, a ZLP should be sent if
|
||||
|
||||
@@ -212,6 +212,9 @@ TU_ATTR_WEAK void tud_cdc_line_state_cb(uint8_t itf, bool dtr, bool rts);
|
||||
TU_ATTR_WEAK void tud_cdc_line_coding_cb(uint8_t itf, cdc_line_coding_t const* p_line_coding);
|
||||
|
||||
// Invoked when received send break
|
||||
// \param[in] itf interface for which send break was received.
|
||||
// \param[in] duration_ms the length of time, in milliseconds, of the break signal. If a value of FFFFh, then the
|
||||
// device will send a break until another SendBreak request is received with value 0000h.
|
||||
TU_ATTR_WEAK void tud_cdc_send_break_cb(uint8_t itf, uint16_t duration_ms);
|
||||
|
||||
//--------------------------------------------------------------------+
|
||||
|
||||
@@ -73,15 +73,17 @@ typedef struct {
|
||||
tu_edpt_stream_t rx;
|
||||
|
||||
uint8_t tx_ff_buf[CFG_TUH_CDC_TX_BUFSIZE];
|
||||
CFG_TUH_MEM_ALIGN uint8_t tx_ep_buf[CFG_TUH_CDC_TX_EPSIZE];
|
||||
|
||||
uint8_t rx_ff_buf[CFG_TUH_CDC_TX_BUFSIZE];
|
||||
CFG_TUH_MEM_ALIGN uint8_t rx_ep_buf[CFG_TUH_CDC_TX_EPSIZE];
|
||||
} stream;
|
||||
} cdch_interface_t;
|
||||
|
||||
CFG_TUH_MEM_SECTION
|
||||
typedef struct {
|
||||
TUH_EPBUF_DEF(tx, CFG_TUH_CDC_TX_EPSIZE);
|
||||
TUH_EPBUF_DEF(rx, CFG_TUH_CDC_TX_EPSIZE);
|
||||
} cdch_epbuf_t;
|
||||
|
||||
static cdch_interface_t cdch_data[CFG_TUH_CDC];
|
||||
CFG_TUH_MEM_SECTION static cdch_epbuf_t cdch_epbuf[CFG_TUH_CDC];
|
||||
|
||||
//--------------------------------------------------------------------+
|
||||
// Serial Driver
|
||||
@@ -626,13 +628,14 @@ bool cdch_init(void) {
|
||||
tu_memclr(cdch_data, sizeof(cdch_data));
|
||||
for (size_t i = 0; i < CFG_TUH_CDC; i++) {
|
||||
cdch_interface_t* p_cdc = &cdch_data[i];
|
||||
cdch_epbuf_t* epbuf = &cdch_epbuf[i];
|
||||
tu_edpt_stream_init(&p_cdc->stream.tx, true, true, false,
|
||||
p_cdc->stream.tx_ff_buf, CFG_TUH_CDC_TX_BUFSIZE,
|
||||
p_cdc->stream.tx_ep_buf, CFG_TUH_CDC_TX_EPSIZE);
|
||||
epbuf->tx, CFG_TUH_CDC_TX_EPSIZE);
|
||||
|
||||
tu_edpt_stream_init(&p_cdc->stream.rx, true, false, false,
|
||||
p_cdc->stream.rx_ff_buf, CFG_TUH_CDC_RX_BUFSIZE,
|
||||
p_cdc->stream.rx_ep_buf, CFG_TUH_CDC_RX_EPSIZE);
|
||||
epbuf->rx, CFG_TUH_CDC_RX_EPSIZE);
|
||||
}
|
||||
|
||||
return true;
|
||||
@@ -654,7 +657,9 @@ void cdch_close(uint8_t daddr) {
|
||||
TU_LOG_DRV(" CDCh close addr = %u index = %u\r\n", daddr, idx);
|
||||
|
||||
// Invoke application callback
|
||||
if (tuh_cdc_umount_cb) tuh_cdc_umount_cb(idx);
|
||||
if (tuh_cdc_umount_cb) {
|
||||
tuh_cdc_umount_cb(idx);
|
||||
}
|
||||
|
||||
p_cdc->daddr = 0;
|
||||
p_cdc->bInterfaceNumber = 0;
|
||||
@@ -675,7 +680,9 @@ bool cdch_xfer_cb(uint8_t daddr, uint8_t ep_addr, xfer_result_t event, uint32_t
|
||||
|
||||
if ( ep_addr == p_cdc->stream.tx.ep_addr ) {
|
||||
// invoke tx complete callback to possibly refill tx fifo
|
||||
if (tuh_cdc_tx_complete_cb) tuh_cdc_tx_complete_cb(idx);
|
||||
if (tuh_cdc_tx_complete_cb) {
|
||||
tuh_cdc_tx_complete_cb(idx);
|
||||
}
|
||||
|
||||
if ( 0 == tu_edpt_stream_write_xfer(daddr, &p_cdc->stream.tx) ) {
|
||||
// If there is no data left, a ZLP should be sent if:
|
||||
@@ -695,7 +702,9 @@ bool cdch_xfer_cb(uint8_t daddr, uint8_t ep_addr, xfer_result_t event, uint32_t
|
||||
}
|
||||
|
||||
// invoke receive callback
|
||||
if (tuh_cdc_rx_cb) tuh_cdc_rx_cb(idx);
|
||||
if (tuh_cdc_rx_cb) {
|
||||
tuh_cdc_rx_cb(idx);
|
||||
}
|
||||
|
||||
// prepare for next transfer if needed
|
||||
tu_edpt_stream_read_xfer(daddr, &p_cdc->stream.rx);
|
||||
@@ -738,9 +747,8 @@ bool cdch_open(uint8_t rhport, uint8_t daddr, tusb_desc_interface_t const *itf_d
|
||||
if (TUSB_CLASS_CDC == itf_desc->bInterfaceClass &&
|
||||
CDC_COMM_SUBCLASS_ABSTRACT_CONTROL_MODEL == itf_desc->bInterfaceSubClass) {
|
||||
return acm_open(daddr, itf_desc, max_len);
|
||||
}
|
||||
else if (SERIAL_DRIVER_COUNT > 1 &&
|
||||
TUSB_CLASS_VENDOR_SPECIFIC == itf_desc->bInterfaceClass) {
|
||||
} else if (SERIAL_DRIVER_COUNT > 1 &&
|
||||
TUSB_CLASS_VENDOR_SPECIFIC == itf_desc->bInterfaceClass) {
|
||||
uint16_t vid, pid;
|
||||
TU_VERIFY(tuh_vid_pid_get(daddr, &vid, &pid));
|
||||
|
||||
@@ -760,7 +768,9 @@ bool cdch_open(uint8_t rhport, uint8_t daddr, tusb_desc_interface_t const *itf_d
|
||||
static void set_config_complete(cdch_interface_t * p_cdc, uint8_t idx, uint8_t itf_num) {
|
||||
TU_LOG_DRV("CDCh Set Configure complete\r\n");
|
||||
p_cdc->mounted = true;
|
||||
if (tuh_cdc_mount_cb) tuh_cdc_mount_cb(idx);
|
||||
if (tuh_cdc_mount_cb) {
|
||||
tuh_cdc_mount_cb(idx);
|
||||
}
|
||||
|
||||
// Prepare for incoming data
|
||||
tu_edpt_stream_read_xfer(p_cdc->daddr, &p_cdc->stream.rx);
|
||||
|
||||
@@ -89,8 +89,7 @@ bool tuh_cdc_get_dtr(uint8_t idx);
|
||||
bool tuh_cdc_get_rts(uint8_t idx);
|
||||
|
||||
// Check if interface is connected (DTR active)
|
||||
TU_ATTR_ALWAYS_INLINE static inline bool tuh_cdc_connected(uint8_t idx)
|
||||
{
|
||||
TU_ATTR_ALWAYS_INLINE static inline bool tuh_cdc_connected(uint8_t idx) {
|
||||
return tuh_cdc_get_dtr(idx);
|
||||
}
|
||||
|
||||
|
||||
@@ -47,8 +47,7 @@
|
||||
//--------------------------------------------------------------------+
|
||||
// INTERNAL OBJECT & FUNCTION DECLARATION
|
||||
//--------------------------------------------------------------------+
|
||||
typedef struct
|
||||
{
|
||||
typedef struct {
|
||||
uint8_t attrs;
|
||||
uint8_t alt;
|
||||
|
||||
@@ -58,69 +57,65 @@ typedef struct
|
||||
bool flashing_in_progress;
|
||||
uint16_t block;
|
||||
uint16_t length;
|
||||
|
||||
CFG_TUSB_MEM_ALIGN uint8_t transfer_buf[CFG_TUD_DFU_XFER_BUFSIZE];
|
||||
} dfu_state_ctx_t;
|
||||
|
||||
// Only a single dfu state is allowed
|
||||
CFG_TUD_MEM_SECTION tu_static dfu_state_ctx_t _dfu_ctx;
|
||||
static dfu_state_ctx_t _dfu_ctx;
|
||||
|
||||
static void reset_state(void)
|
||||
{
|
||||
CFG_TUD_MEM_SECTION static struct {
|
||||
TUD_EPBUF_DEF(transfer_buf, CFG_TUD_DFU_XFER_BUFSIZE);
|
||||
} _dfu_epbuf;
|
||||
|
||||
static void reset_state(void) {
|
||||
_dfu_ctx.state = DFU_IDLE;
|
||||
_dfu_ctx.status = DFU_STATUS_OK;
|
||||
_dfu_ctx.flashing_in_progress = false;
|
||||
}
|
||||
|
||||
static bool reply_getstatus(uint8_t rhport, tusb_control_request_t const * request, dfu_state_t state, dfu_status_t status, uint32_t timeout);
|
||||
static bool process_download_get_status(uint8_t rhport, uint8_t stage, tusb_control_request_t const * request);
|
||||
static bool process_manifest_get_status(uint8_t rhport, uint8_t stage, tusb_control_request_t const * request);
|
||||
static bool reply_getstatus(uint8_t rhport, const tusb_control_request_t* request, dfu_state_t state, dfu_status_t status, uint32_t timeout);
|
||||
static bool process_download_get_status(uint8_t rhport, uint8_t stage, const tusb_control_request_t* request);
|
||||
static bool process_manifest_get_status(uint8_t rhport, uint8_t stage, const tusb_control_request_t* request);
|
||||
|
||||
//--------------------------------------------------------------------+
|
||||
// Debug
|
||||
//--------------------------------------------------------------------+
|
||||
#if CFG_TUSB_DEBUG >= 2
|
||||
|
||||
tu_static tu_lookup_entry_t const _dfu_request_lookup[] =
|
||||
{
|
||||
{ .key = DFU_REQUEST_DETACH , .data = "DETACH" },
|
||||
{ .key = DFU_REQUEST_DNLOAD , .data = "DNLOAD" },
|
||||
{ .key = DFU_REQUEST_UPLOAD , .data = "UPLOAD" },
|
||||
{ .key = DFU_REQUEST_GETSTATUS , .data = "GETSTATUS" },
|
||||
{ .key = DFU_REQUEST_CLRSTATUS , .data = "CLRSTATUS" },
|
||||
{ .key = DFU_REQUEST_GETSTATE , .data = "GETSTATE" },
|
||||
{ .key = DFU_REQUEST_ABORT , .data = "ABORT" },
|
||||
tu_static tu_lookup_entry_t const _dfu_request_lookup[] = {
|
||||
{ .key = DFU_REQUEST_DETACH , .data = "DETACH" },
|
||||
{ .key = DFU_REQUEST_DNLOAD , .data = "DNLOAD" },
|
||||
{ .key = DFU_REQUEST_UPLOAD , .data = "UPLOAD" },
|
||||
{ .key = DFU_REQUEST_GETSTATUS, .data = "GETSTATUS" },
|
||||
{ .key = DFU_REQUEST_CLRSTATUS, .data = "CLRSTATUS" },
|
||||
{ .key = DFU_REQUEST_GETSTATE , .data = "GETSTATE" },
|
||||
{ .key = DFU_REQUEST_ABORT , .data = "ABORT" },
|
||||
};
|
||||
|
||||
tu_static tu_lookup_table_t const _dfu_request_table =
|
||||
{
|
||||
tu_static tu_lookup_table_t const _dfu_request_table = {
|
||||
.count = TU_ARRAY_SIZE(_dfu_request_lookup),
|
||||
.items = _dfu_request_lookup
|
||||
};
|
||||
|
||||
tu_static tu_lookup_entry_t const _dfu_state_lookup[] =
|
||||
{
|
||||
{ .key = APP_IDLE , .data = "APP_IDLE" },
|
||||
{ .key = APP_DETACH , .data = "APP_DETACH" },
|
||||
{ .key = DFU_IDLE , .data = "IDLE" },
|
||||
{ .key = DFU_DNLOAD_SYNC , .data = "DNLOAD_SYNC" },
|
||||
{ .key = DFU_DNBUSY , .data = "DNBUSY" },
|
||||
{ .key = DFU_DNLOAD_IDLE , .data = "DNLOAD_IDLE" },
|
||||
{ .key = DFU_MANIFEST_SYNC , .data = "MANIFEST_SYNC" },
|
||||
{ .key = DFU_MANIFEST , .data = "MANIFEST" },
|
||||
{ .key = DFU_MANIFEST_WAIT_RESET , .data = "MANIFEST_WAIT_RESET" },
|
||||
{ .key = DFU_UPLOAD_IDLE , .data = "UPLOAD_IDLE" },
|
||||
{ .key = DFU_ERROR , .data = "ERROR" },
|
||||
tu_static tu_lookup_entry_t const _dfu_state_lookup[] = {
|
||||
{ .key = APP_IDLE , .data = "APP_IDLE" },
|
||||
{ .key = APP_DETACH , .data = "APP_DETACH" },
|
||||
{ .key = DFU_IDLE , .data = "IDLE" },
|
||||
{ .key = DFU_DNLOAD_SYNC , .data = "DNLOAD_SYNC" },
|
||||
{ .key = DFU_DNBUSY , .data = "DNBUSY" },
|
||||
{ .key = DFU_DNLOAD_IDLE , .data = "DNLOAD_IDLE" },
|
||||
{ .key = DFU_MANIFEST_SYNC , .data = "MANIFEST_SYNC" },
|
||||
{ .key = DFU_MANIFEST , .data = "MANIFEST" },
|
||||
{ .key = DFU_MANIFEST_WAIT_RESET, .data = "MANIFEST_WAIT_RESET" },
|
||||
{ .key = DFU_UPLOAD_IDLE , .data = "UPLOAD_IDLE" },
|
||||
{ .key = DFU_ERROR , .data = "ERROR" },
|
||||
};
|
||||
|
||||
tu_static tu_lookup_table_t const _dfu_state_table =
|
||||
{
|
||||
tu_static tu_lookup_table_t const _dfu_state_table = {
|
||||
.count = TU_ARRAY_SIZE(_dfu_state_lookup),
|
||||
.items = _dfu_state_lookup
|
||||
};
|
||||
|
||||
tu_static tu_lookup_entry_t const _dfu_status_lookup[] =
|
||||
{
|
||||
tu_static tu_lookup_entry_t const _dfu_status_lookup[] = {
|
||||
{ .key = DFU_STATUS_OK , .data = "OK" },
|
||||
{ .key = DFU_STATUS_ERR_TARGET , .data = "errTARGET" },
|
||||
{ .key = DFU_STATUS_ERR_FILE , .data = "errFILE" },
|
||||
@@ -139,8 +134,7 @@ tu_static tu_lookup_entry_t const _dfu_status_lookup[] =
|
||||
{ .key = DFU_STATUS_ERR_STALLEDPKT , .data = "errSTALLEDPKT" },
|
||||
};
|
||||
|
||||
tu_static tu_lookup_table_t const _dfu_status_table =
|
||||
{
|
||||
tu_static tu_lookup_table_t const _dfu_status_table = {
|
||||
.count = TU_ARRAY_SIZE(_dfu_status_lookup),
|
||||
.items = _dfu_status_lookup
|
||||
};
|
||||
@@ -150,13 +144,10 @@ tu_static tu_lookup_table_t const _dfu_status_table =
|
||||
//--------------------------------------------------------------------+
|
||||
// USBD Driver API
|
||||
//--------------------------------------------------------------------+
|
||||
void dfu_moded_reset(uint8_t rhport)
|
||||
{
|
||||
void dfu_moded_reset(uint8_t rhport) {
|
||||
(void) rhport;
|
||||
|
||||
_dfu_ctx.attrs = 0;
|
||||
_dfu_ctx.alt = 0;
|
||||
|
||||
reset_state();
|
||||
}
|
||||
|
||||
@@ -168,19 +159,17 @@ bool dfu_moded_deinit(void) {
|
||||
return true;
|
||||
}
|
||||
|
||||
uint16_t dfu_moded_open(uint8_t rhport, tusb_desc_interface_t const * itf_desc, uint16_t max_len)
|
||||
{
|
||||
uint16_t dfu_moded_open(uint8_t rhport, const tusb_desc_interface_t* itf_desc, uint16_t max_len) {
|
||||
(void) rhport;
|
||||
|
||||
//------------- Interface (with Alt) descriptor -------------//
|
||||
uint8_t const itf_num = itf_desc->bInterfaceNumber;
|
||||
const uint8_t itf_num = itf_desc->bInterfaceNumber;
|
||||
uint8_t alt_count = 0;
|
||||
|
||||
uint16_t drv_len = 0;
|
||||
TU_VERIFY(itf_desc->bInterfaceSubClass == TUD_DFU_APP_SUBCLASS && itf_desc->bInterfaceProtocol == DFU_PROTOCOL_DFU, 0);
|
||||
|
||||
while(itf_desc->bInterfaceSubClass == TUD_DFU_APP_SUBCLASS && itf_desc->bInterfaceProtocol == DFU_PROTOCOL_DFU)
|
||||
{
|
||||
while(itf_desc->bInterfaceSubClass == TUD_DFU_APP_SUBCLASS && itf_desc->bInterfaceProtocol == DFU_PROTOCOL_DFU) {
|
||||
TU_ASSERT(max_len > drv_len, 0);
|
||||
|
||||
// Alternate must have the same interface number
|
||||
@@ -191,18 +180,18 @@ uint16_t dfu_moded_open(uint8_t rhport, tusb_desc_interface_t const * itf_desc,
|
||||
alt_count++;
|
||||
|
||||
drv_len += tu_desc_len(itf_desc);
|
||||
itf_desc = (tusb_desc_interface_t const *) tu_desc_next(itf_desc);
|
||||
itf_desc = (const tusb_desc_interface_t*) tu_desc_next(itf_desc);
|
||||
}
|
||||
|
||||
//------------- DFU Functional descriptor -------------//
|
||||
tusb_desc_dfu_functional_t const *func_desc = (tusb_desc_dfu_functional_t const *) itf_desc;
|
||||
const tusb_desc_dfu_functional_t*func_desc = (const tusb_desc_dfu_functional_t*) itf_desc;
|
||||
TU_ASSERT(tu_desc_type(func_desc) == TUSB_DESC_FUNCTIONAL, 0);
|
||||
drv_len += sizeof(tusb_desc_dfu_functional_t);
|
||||
|
||||
_dfu_ctx.attrs = func_desc->bAttributes;
|
||||
|
||||
// CFG_TUD_DFU_XFER_BUFSIZE has to be set to the buffer size used in TUD_DFU_DESCRIPTOR
|
||||
uint16_t const transfer_size = tu_le16toh( tu_unaligned_read16((uint8_t const*) func_desc + offsetof(tusb_desc_dfu_functional_t, wTransferSize)) );
|
||||
const uint16_t transfer_size = tu_le16toh( tu_unaligned_read16((const uint8_t*) func_desc + offsetof(tusb_desc_dfu_functional_t, wTransferSize)) );
|
||||
TU_ASSERT(transfer_size <= CFG_TUD_DFU_XFER_BUFSIZE, drv_len);
|
||||
|
||||
return drv_len;
|
||||
@@ -211,99 +200,85 @@ uint16_t dfu_moded_open(uint8_t rhport, tusb_desc_interface_t const * itf_desc,
|
||||
// Invoked when a control transfer occurred on an interface of this class
|
||||
// Driver response accordingly to the request and the transfer stage (setup/data/ack)
|
||||
// return false to stall control endpoint (e.g unsupported request)
|
||||
bool dfu_moded_control_xfer_cb(uint8_t rhport, uint8_t stage, tusb_control_request_t const * request)
|
||||
{
|
||||
bool dfu_moded_control_xfer_cb(uint8_t rhport, uint8_t stage, const tusb_control_request_t* request) {
|
||||
TU_VERIFY(request->bmRequestType_bit.recipient == TUSB_REQ_RCPT_INTERFACE);
|
||||
|
||||
TU_LOG_DRV(" DFU State : %s, Status: %s\r\n", tu_lookup_find(&_dfu_state_table, _dfu_ctx.state), tu_lookup_find(&_dfu_status_table, _dfu_ctx.status));
|
||||
|
||||
if ( request->bmRequestType_bit.type == TUSB_REQ_TYPE_STANDARD )
|
||||
{
|
||||
if (request->bmRequestType_bit.type == TUSB_REQ_TYPE_STANDARD) {
|
||||
// Standard request include GET/SET_INTERFACE
|
||||
switch ( request->bRequest )
|
||||
{
|
||||
switch (request->bRequest) {
|
||||
case TUSB_REQ_SET_INTERFACE:
|
||||
if ( stage == CONTROL_STAGE_SETUP )
|
||||
{
|
||||
if (stage == CONTROL_STAGE_SETUP) {
|
||||
// Switch Alt interface and reset state machine
|
||||
_dfu_ctx.alt = (uint8_t) request->wValue;
|
||||
_dfu_ctx.alt = (uint8_t)request->wValue;
|
||||
reset_state();
|
||||
return tud_control_status(rhport, request);
|
||||
}
|
||||
break;
|
||||
break;
|
||||
|
||||
case TUSB_REQ_GET_INTERFACE:
|
||||
if(stage == CONTROL_STAGE_SETUP)
|
||||
{
|
||||
if (stage == CONTROL_STAGE_SETUP) {
|
||||
return tud_control_xfer(rhport, request, &_dfu_ctx.alt, 1);
|
||||
}
|
||||
break;
|
||||
break;
|
||||
|
||||
// unsupported request
|
||||
default: return false;
|
||||
}
|
||||
}
|
||||
else if ( request->bmRequestType_bit.type == TUSB_REQ_TYPE_CLASS )
|
||||
{
|
||||
} else if (request->bmRequestType_bit.type == TUSB_REQ_TYPE_CLASS) {
|
||||
TU_LOG_DRV(" DFU Request: %s\r\n", tu_lookup_find(&_dfu_request_table, request->bRequest));
|
||||
|
||||
// Class request
|
||||
switch ( request->bRequest )
|
||||
{
|
||||
switch (request->bRequest) {
|
||||
case DFU_REQUEST_DETACH:
|
||||
if ( stage == CONTROL_STAGE_SETUP )
|
||||
{
|
||||
if (stage == CONTROL_STAGE_SETUP) {
|
||||
tud_control_status(rhport, request);
|
||||
} else if (stage == CONTROL_STAGE_ACK) {
|
||||
if (tud_dfu_detach_cb) {
|
||||
tud_dfu_detach_cb();
|
||||
}
|
||||
}
|
||||
else if ( stage == CONTROL_STAGE_ACK )
|
||||
{
|
||||
if ( tud_dfu_detach_cb ) tud_dfu_detach_cb();
|
||||
}
|
||||
break;
|
||||
break;
|
||||
|
||||
case DFU_REQUEST_CLRSTATUS:
|
||||
if ( stage == CONTROL_STAGE_SETUP )
|
||||
{
|
||||
if (stage == CONTROL_STAGE_SETUP) {
|
||||
reset_state();
|
||||
tud_control_status(rhport, request);
|
||||
}
|
||||
break;
|
||||
break;
|
||||
|
||||
case DFU_REQUEST_GETSTATE:
|
||||
if ( stage == CONTROL_STAGE_SETUP )
|
||||
{
|
||||
if (stage == CONTROL_STAGE_SETUP) {
|
||||
tud_control_xfer(rhport, request, &_dfu_ctx.state, 1);
|
||||
}
|
||||
break;
|
||||
break;
|
||||
|
||||
case DFU_REQUEST_ABORT:
|
||||
if ( stage == CONTROL_STAGE_SETUP )
|
||||
{
|
||||
if (stage == CONTROL_STAGE_SETUP) {
|
||||
reset_state();
|
||||
tud_control_status(rhport, request);
|
||||
} else if (stage == CONTROL_STAGE_ACK) {
|
||||
if (tud_dfu_abort_cb) {
|
||||
tud_dfu_abort_cb(_dfu_ctx.alt);
|
||||
}
|
||||
}
|
||||
else if ( stage == CONTROL_STAGE_ACK )
|
||||
{
|
||||
if ( tud_dfu_abort_cb ) tud_dfu_abort_cb(_dfu_ctx.alt);
|
||||
}
|
||||
break;
|
||||
break;
|
||||
|
||||
case DFU_REQUEST_UPLOAD:
|
||||
if ( stage == CONTROL_STAGE_SETUP )
|
||||
{
|
||||
if (stage == CONTROL_STAGE_SETUP) {
|
||||
TU_VERIFY(_dfu_ctx.attrs & DFU_ATTR_CAN_UPLOAD);
|
||||
TU_VERIFY(tud_dfu_upload_cb);
|
||||
TU_VERIFY(request->wLength <= CFG_TUD_DFU_XFER_BUFSIZE);
|
||||
|
||||
uint16_t const xfer_len = tud_dfu_upload_cb(_dfu_ctx.alt, request->wValue, _dfu_ctx.transfer_buf, request->wLength);
|
||||
const uint16_t xfer_len = tud_dfu_upload_cb(_dfu_ctx.alt, request->wValue, _dfu_epbuf.transfer_buf,
|
||||
request->wLength);
|
||||
|
||||
return tud_control_xfer(rhport, request, _dfu_ctx.transfer_buf, xfer_len);
|
||||
return tud_control_xfer(rhport, request, _dfu_epbuf.transfer_buf, xfer_len);
|
||||
}
|
||||
break;
|
||||
break;
|
||||
|
||||
case DFU_REQUEST_DNLOAD:
|
||||
if ( stage == CONTROL_STAGE_SETUP )
|
||||
{
|
||||
if (stage == CONTROL_STAGE_SETUP) {
|
||||
TU_VERIFY(_dfu_ctx.attrs & DFU_ATTR_CAN_DOWNLOAD);
|
||||
TU_VERIFY(_dfu_ctx.state == DFU_IDLE || _dfu_ctx.state == DFU_DNLOAD_IDLE);
|
||||
TU_VERIFY(request->wLength <= CFG_TUD_DFU_XFER_BUFSIZE);
|
||||
@@ -312,104 +287,86 @@ bool dfu_moded_control_xfer_cb(uint8_t rhport, uint8_t stage, tusb_control_reque
|
||||
_dfu_ctx.flashing_in_progress = true;
|
||||
|
||||
// save block and length for flashing
|
||||
_dfu_ctx.block = request->wValue;
|
||||
_dfu_ctx.block = request->wValue;
|
||||
_dfu_ctx.length = request->wLength;
|
||||
|
||||
if ( request->wLength )
|
||||
{
|
||||
if (request->wLength) {
|
||||
// Download with payload -> transition to DOWNLOAD SYNC
|
||||
_dfu_ctx.state = DFU_DNLOAD_SYNC;
|
||||
return tud_control_xfer(rhport, request, _dfu_ctx.transfer_buf, request->wLength);
|
||||
}
|
||||
else
|
||||
{
|
||||
return tud_control_xfer(rhport, request, _dfu_epbuf.transfer_buf, request->wLength);
|
||||
} else {
|
||||
// Download is complete -> transition to MANIFEST SYNC
|
||||
_dfu_ctx.state = DFU_MANIFEST_SYNC;
|
||||
return tud_control_status(rhport, request);
|
||||
}
|
||||
}
|
||||
break;
|
||||
break;
|
||||
|
||||
case DFU_REQUEST_GETSTATUS:
|
||||
switch ( _dfu_ctx.state )
|
||||
{
|
||||
switch (_dfu_ctx.state) {
|
||||
case DFU_DNLOAD_SYNC:
|
||||
return process_download_get_status(rhport, stage, request);
|
||||
break;
|
||||
break;
|
||||
|
||||
case DFU_MANIFEST_SYNC:
|
||||
return process_manifest_get_status(rhport, stage, request);
|
||||
break;
|
||||
break;
|
||||
|
||||
default:
|
||||
if ( stage == CONTROL_STAGE_SETUP ) return reply_getstatus(rhport, request, _dfu_ctx.state, _dfu_ctx.status, 0);
|
||||
break;
|
||||
if (stage == CONTROL_STAGE_SETUP) {
|
||||
return reply_getstatus(rhport, request, _dfu_ctx.state, _dfu_ctx.status, 0);
|
||||
}
|
||||
break;
|
||||
}
|
||||
break;
|
||||
break;
|
||||
|
||||
default: return false; // stall unsupported request
|
||||
}
|
||||
}else
|
||||
{
|
||||
} else {
|
||||
return false; // unsupported request
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
void tud_dfu_finish_flashing(uint8_t status)
|
||||
{
|
||||
void tud_dfu_finish_flashing(uint8_t status) {
|
||||
_dfu_ctx.flashing_in_progress = false;
|
||||
|
||||
if ( status == DFU_STATUS_OK )
|
||||
{
|
||||
if (_dfu_ctx.state == DFU_DNBUSY)
|
||||
{
|
||||
if (status == DFU_STATUS_OK) {
|
||||
if (_dfu_ctx.state == DFU_DNBUSY) {
|
||||
_dfu_ctx.state = DFU_DNLOAD_SYNC;
|
||||
}
|
||||
else if (_dfu_ctx.state == DFU_MANIFEST)
|
||||
{
|
||||
} else if (_dfu_ctx.state == DFU_MANIFEST) {
|
||||
_dfu_ctx.state = (_dfu_ctx.attrs & DFU_ATTR_MANIFESTATION_TOLERANT)
|
||||
? DFU_MANIFEST_SYNC : DFU_MANIFEST_WAIT_RESET;
|
||||
? DFU_MANIFEST_SYNC
|
||||
: DFU_MANIFEST_WAIT_RESET;
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
} else {
|
||||
// failed while flashing, move to dfuError
|
||||
_dfu_ctx.state = DFU_ERROR;
|
||||
_dfu_ctx.status = (dfu_status_t)status;
|
||||
}
|
||||
}
|
||||
|
||||
static bool process_download_get_status(uint8_t rhport, uint8_t stage, tusb_control_request_t const * request)
|
||||
{
|
||||
if ( stage == CONTROL_STAGE_SETUP )
|
||||
{
|
||||
static bool process_download_get_status(uint8_t rhport, uint8_t stage, const tusb_control_request_t* request) {
|
||||
if (stage == CONTROL_STAGE_SETUP) {
|
||||
// only transition to next state on CONTROL_STAGE_ACK
|
||||
dfu_state_t next_state;
|
||||
uint32_t timeout;
|
||||
|
||||
if ( _dfu_ctx.flashing_in_progress )
|
||||
{
|
||||
if (_dfu_ctx.flashing_in_progress) {
|
||||
next_state = DFU_DNBUSY;
|
||||
timeout = tud_dfu_get_timeout_cb(_dfu_ctx.alt, (uint8_t) next_state);
|
||||
}
|
||||
else
|
||||
{
|
||||
timeout = tud_dfu_get_timeout_cb(_dfu_ctx.alt, (uint8_t)next_state);
|
||||
} else {
|
||||
next_state = DFU_DNLOAD_IDLE;
|
||||
timeout = 0;
|
||||
}
|
||||
|
||||
return reply_getstatus(rhport, request, next_state, _dfu_ctx.status, timeout);
|
||||
}
|
||||
else if ( stage == CONTROL_STAGE_ACK )
|
||||
{
|
||||
if ( _dfu_ctx.flashing_in_progress )
|
||||
{
|
||||
} else if (stage == CONTROL_STAGE_ACK) {
|
||||
if (_dfu_ctx.flashing_in_progress) {
|
||||
_dfu_ctx.state = DFU_DNBUSY;
|
||||
tud_dfu_download_cb(_dfu_ctx.alt, _dfu_ctx.block, _dfu_ctx.transfer_buf, _dfu_ctx.length);
|
||||
}else
|
||||
{
|
||||
tud_dfu_download_cb(_dfu_ctx.alt, _dfu_ctx.block, _dfu_epbuf.transfer_buf, _dfu_ctx.length);
|
||||
} else {
|
||||
_dfu_ctx.state = DFU_DNLOAD_IDLE;
|
||||
}
|
||||
}
|
||||
@@ -417,36 +374,26 @@ static bool process_download_get_status(uint8_t rhport, uint8_t stage, tusb_cont
|
||||
return true;
|
||||
}
|
||||
|
||||
static bool process_manifest_get_status(uint8_t rhport, uint8_t stage, tusb_control_request_t const * request)
|
||||
{
|
||||
if ( stage == CONTROL_STAGE_SETUP )
|
||||
{
|
||||
static bool process_manifest_get_status(uint8_t rhport, uint8_t stage, const tusb_control_request_t* request) {
|
||||
if (stage == CONTROL_STAGE_SETUP) {
|
||||
// only transition to next state on CONTROL_STAGE_ACK
|
||||
dfu_state_t next_state;
|
||||
uint32_t timeout;
|
||||
|
||||
if ( _dfu_ctx.flashing_in_progress )
|
||||
{
|
||||
if (_dfu_ctx.flashing_in_progress) {
|
||||
next_state = DFU_MANIFEST;
|
||||
timeout = tud_dfu_get_timeout_cb(_dfu_ctx.alt, next_state);
|
||||
}
|
||||
else
|
||||
{
|
||||
} else {
|
||||
next_state = DFU_IDLE;
|
||||
timeout = 0;
|
||||
}
|
||||
|
||||
return reply_getstatus(rhport, request, next_state, _dfu_ctx.status, timeout);
|
||||
}
|
||||
else if ( stage == CONTROL_STAGE_ACK )
|
||||
{
|
||||
if ( _dfu_ctx.flashing_in_progress )
|
||||
{
|
||||
} else if (stage == CONTROL_STAGE_ACK) {
|
||||
if (_dfu_ctx.flashing_in_progress) {
|
||||
_dfu_ctx.state = DFU_MANIFEST;
|
||||
tud_dfu_manifest_cb(_dfu_ctx.alt);
|
||||
}
|
||||
else
|
||||
{
|
||||
} else {
|
||||
_dfu_ctx.state = DFU_IDLE;
|
||||
}
|
||||
}
|
||||
@@ -454,15 +401,15 @@ static bool process_manifest_get_status(uint8_t rhport, uint8_t stage, tusb_cont
|
||||
return true;
|
||||
}
|
||||
|
||||
static bool reply_getstatus(uint8_t rhport, tusb_control_request_t const * request, dfu_state_t state, dfu_status_t status, uint32_t timeout)
|
||||
{
|
||||
static bool reply_getstatus(uint8_t rhport, const tusb_control_request_t* request, dfu_state_t state,
|
||||
dfu_status_t status, uint32_t timeout) {
|
||||
dfu_status_response_t resp;
|
||||
resp.bStatus = (uint8_t) status;
|
||||
resp.bStatus = (uint8_t)status;
|
||||
resp.bwPollTimeout[0] = TU_U32_BYTE0(timeout);
|
||||
resp.bwPollTimeout[1] = TU_U32_BYTE1(timeout);
|
||||
resp.bwPollTimeout[2] = TU_U32_BYTE2(timeout);
|
||||
resp.bState = (uint8_t) state;
|
||||
resp.iString = 0;
|
||||
resp.bState = (uint8_t)state;
|
||||
resp.iString = 0;
|
||||
|
||||
return tud_control_xfer(rhport, request, &resp, sizeof(dfu_status_response_t));
|
||||
}
|
||||
|
||||
@@ -58,9 +58,8 @@ bool dfu_rtd_deinit(void) {
|
||||
return true;
|
||||
}
|
||||
|
||||
void dfu_rtd_reset(uint8_t rhport)
|
||||
{
|
||||
(void) rhport;
|
||||
void dfu_rtd_reset(uint8_t rhport) {
|
||||
(void) rhport;
|
||||
}
|
||||
|
||||
uint16_t dfu_rtd_open(uint8_t rhport, tusb_desc_interface_t const * itf_desc, uint16_t max_len)
|
||||
|
||||
@@ -51,14 +51,17 @@ typedef struct {
|
||||
|
||||
// TODO save hid descriptor since host can specifically request this after enumeration
|
||||
// Note: HID descriptor may be not available from application after enumeration
|
||||
tusb_hid_descriptor_hid_t const *hid_descriptor;
|
||||
|
||||
uint8_t ctrl_buf[CFG_TUD_HID_EP_BUFSIZE];
|
||||
CFG_TUSB_MEM_ALIGN uint8_t epin_buf[CFG_TUD_HID_EP_BUFSIZE];
|
||||
CFG_TUSB_MEM_ALIGN uint8_t epout_buf[CFG_TUD_HID_EP_BUFSIZE];
|
||||
const tusb_hid_descriptor_hid_t*hid_descriptor;
|
||||
} hidd_interface_t;
|
||||
|
||||
CFG_TUD_MEM_SECTION tu_static hidd_interface_t _hidd_itf[CFG_TUD_HID];
|
||||
typedef struct {
|
||||
TUD_EPBUF_DEF(ctrl , CFG_TUD_HID_EP_BUFSIZE);
|
||||
TUD_EPBUF_DEF(epin , CFG_TUD_HID_EP_BUFSIZE);
|
||||
TUD_EPBUF_DEF(epout, CFG_TUD_HID_EP_BUFSIZE);
|
||||
} hidd_epbuf_t;
|
||||
|
||||
static hidd_interface_t _hidd_itf[CFG_TUD_HID];
|
||||
CFG_TUD_MEM_SECTION static hidd_epbuf_t _hidd_epbuf[CFG_TUD_HID];
|
||||
|
||||
/*------------- Helpers -------------*/
|
||||
TU_ATTR_ALWAYS_INLINE static inline uint8_t get_index_by_itfnum(uint8_t itf_num) {
|
||||
@@ -108,22 +111,24 @@ bool tud_hid_n_ready(uint8_t instance) {
|
||||
}
|
||||
|
||||
bool tud_hid_n_report(uint8_t instance, uint8_t report_id, void const *report, uint16_t len) {
|
||||
uint8_t const rhport = 0;
|
||||
TU_VERIFY(instance < CFG_TUD_HID);
|
||||
const uint8_t rhport = 0;
|
||||
hidd_interface_t *p_hid = &_hidd_itf[instance];
|
||||
hidd_epbuf_t *p_epbuf = &_hidd_epbuf[instance];
|
||||
|
||||
// claim endpoint
|
||||
TU_VERIFY(usbd_edpt_claim(rhport, p_hid->ep_in));
|
||||
|
||||
// prepare data
|
||||
if (report_id) {
|
||||
p_hid->epin_buf[0] = report_id;
|
||||
TU_VERIFY(0 == tu_memcpy_s(p_hid->epin_buf + 1, CFG_TUD_HID_EP_BUFSIZE - 1, report, len));
|
||||
p_epbuf->epin[0] = report_id;
|
||||
TU_VERIFY(0 == tu_memcpy_s(p_epbuf->epin + 1, CFG_TUD_HID_EP_BUFSIZE - 1, report, len));
|
||||
len++;
|
||||
} else {
|
||||
TU_VERIFY(0 == tu_memcpy_s(p_hid->epin_buf, CFG_TUD_HID_EP_BUFSIZE, report, len));
|
||||
TU_VERIFY(0 == tu_memcpy_s(p_epbuf->epin, CFG_TUD_HID_EP_BUFSIZE, report, len));
|
||||
}
|
||||
|
||||
return usbd_edpt_xfer(rhport, p_hid->ep_in, p_hid->epin_buf, len);
|
||||
return usbd_edpt_xfer(rhport, p_hid->ep_in, p_epbuf->epin, len);
|
||||
}
|
||||
|
||||
uint8_t tud_hid_n_interface_protocol(uint8_t instance) {
|
||||
@@ -214,15 +219,16 @@ uint16_t hidd_open(uint8_t rhport, tusb_desc_interface_t const *desc_itf, uint16
|
||||
TU_ASSERT(max_len >= drv_len, 0);
|
||||
|
||||
// Find available interface
|
||||
hidd_interface_t *p_hid = NULL;
|
||||
hidd_interface_t *p_hid;
|
||||
uint8_t hid_id;
|
||||
for (hid_id = 0; hid_id < CFG_TUD_HID; hid_id++) {
|
||||
if (_hidd_itf[hid_id].ep_in == 0) {
|
||||
p_hid = &_hidd_itf[hid_id];
|
||||
p_hid = &_hidd_itf[hid_id];
|
||||
if (p_hid->ep_in == 0) {
|
||||
break;
|
||||
}
|
||||
}
|
||||
TU_ASSERT(p_hid, 0);
|
||||
TU_ASSERT(hid_id < CFG_TUD_HID, 0);
|
||||
hidd_epbuf_t *p_epbuf = &_hidd_epbuf[hid_id];
|
||||
|
||||
uint8_t const *p_desc = (uint8_t const *)desc_itf;
|
||||
|
||||
@@ -247,10 +253,7 @@ uint16_t hidd_open(uint8_t rhport, tusb_desc_interface_t const *desc_itf, uint16
|
||||
|
||||
// Prepare for output endpoint
|
||||
if (p_hid->ep_out) {
|
||||
if (!usbd_edpt_xfer(rhport, p_hid->ep_out, p_hid->epout_buf, sizeof(p_hid->epout_buf))) {
|
||||
TU_LOG_FAILED();
|
||||
TU_BREAKPOINT();
|
||||
}
|
||||
TU_ASSERT(usbd_edpt_xfer(rhport, p_hid->ep_out, p_epbuf->epout, CFG_TUD_HID_EP_BUFSIZE), drv_len);
|
||||
}
|
||||
|
||||
return drv_len;
|
||||
@@ -264,8 +267,8 @@ bool hidd_control_xfer_cb(uint8_t rhport, uint8_t stage, tusb_control_request_t
|
||||
|
||||
uint8_t const hid_itf = get_index_by_itfnum((uint8_t)request->wIndex);
|
||||
TU_VERIFY(hid_itf < CFG_TUD_HID);
|
||||
|
||||
hidd_interface_t *p_hid = &_hidd_itf[hid_itf];
|
||||
hidd_epbuf_t *p_epbuf = &_hidd_epbuf[hid_itf];
|
||||
|
||||
if (request->bmRequestType_bit.type == TUSB_REQ_TYPE_STANDARD) {
|
||||
//------------- STD Request -------------//
|
||||
@@ -291,7 +294,7 @@ bool hidd_control_xfer_cb(uint8_t rhport, uint8_t stage, tusb_control_request_t
|
||||
uint8_t const report_type = tu_u16_high(request->wValue);
|
||||
uint8_t const report_id = tu_u16_low(request->wValue);
|
||||
|
||||
uint8_t* report_buf = p_hid->ctrl_buf;
|
||||
uint8_t* report_buf = p_epbuf->ctrl;
|
||||
uint16_t req_len = tu_min16(request->wLength, CFG_TUD_HID_EP_BUFSIZE);
|
||||
uint16_t xferlen = 0;
|
||||
|
||||
@@ -305,19 +308,19 @@ bool hidd_control_xfer_cb(uint8_t rhport, uint8_t stage, tusb_control_request_t
|
||||
xferlen += tud_hid_get_report_cb(hid_itf, report_id, (hid_report_type_t) report_type, report_buf, req_len);
|
||||
TU_ASSERT(xferlen > 0);
|
||||
|
||||
tud_control_xfer(rhport, request, p_hid->ctrl_buf, xferlen);
|
||||
tud_control_xfer(rhport, request, p_epbuf->ctrl, xferlen);
|
||||
}
|
||||
break;
|
||||
|
||||
case HID_REQ_CONTROL_SET_REPORT:
|
||||
if (stage == CONTROL_STAGE_SETUP) {
|
||||
TU_VERIFY(request->wLength <= sizeof(p_hid->ctrl_buf));
|
||||
tud_control_xfer(rhport, request, p_hid->ctrl_buf, request->wLength);
|
||||
TU_VERIFY(request->wLength <= CFG_TUD_HID_EP_BUFSIZE);
|
||||
tud_control_xfer(rhport, request, p_epbuf->ctrl, request->wLength);
|
||||
} else if (stage == CONTROL_STAGE_ACK) {
|
||||
uint8_t const report_type = tu_u16_high(request->wValue);
|
||||
uint8_t const report_id = tu_u16_low(request->wValue);
|
||||
|
||||
uint8_t const* report_buf = p_hid->ctrl_buf;
|
||||
uint8_t const* report_buf = p_epbuf->ctrl;
|
||||
uint16_t report_len = tu_min16(request->wLength, CFG_TUD_HID_EP_BUFSIZE);
|
||||
|
||||
// If host request a specific Report ID, extract report ID in buffer before invoking callback
|
||||
@@ -371,8 +374,8 @@ bool hidd_control_xfer_cb(uint8_t rhport, uint8_t stage, tusb_control_request_t
|
||||
}
|
||||
|
||||
bool hidd_xfer_cb(uint8_t rhport, uint8_t ep_addr, xfer_result_t result, uint32_t xferred_bytes) {
|
||||
uint8_t instance = 0;
|
||||
hidd_interface_t *p_hid = _hidd_itf;
|
||||
uint8_t instance;
|
||||
hidd_interface_t *p_hid;
|
||||
|
||||
// Identify which interface to use
|
||||
for (instance = 0; instance < CFG_TUD_HID; instance++) {
|
||||
@@ -382,24 +385,25 @@ bool hidd_xfer_cb(uint8_t rhport, uint8_t ep_addr, xfer_result_t result, uint32_
|
||||
}
|
||||
}
|
||||
TU_ASSERT(instance < CFG_TUD_HID);
|
||||
hidd_epbuf_t *p_epbuf = &_hidd_epbuf[instance];
|
||||
|
||||
if (ep_addr == p_hid->ep_in) {
|
||||
// Input report
|
||||
if (XFER_RESULT_SUCCESS == result) {
|
||||
tud_hid_report_complete_cb(instance, p_hid->epin_buf, (uint16_t) xferred_bytes);
|
||||
tud_hid_report_complete_cb(instance, p_epbuf->epin, (uint16_t) xferred_bytes);
|
||||
} else {
|
||||
tud_hid_report_failed_cb(instance, HID_REPORT_TYPE_INPUT, p_hid->epin_buf, (uint16_t) xferred_bytes);
|
||||
tud_hid_report_failed_cb(instance, HID_REPORT_TYPE_INPUT, p_epbuf->epin, (uint16_t) xferred_bytes);
|
||||
}
|
||||
} else {
|
||||
// Output report
|
||||
if (XFER_RESULT_SUCCESS == result) {
|
||||
tud_hid_set_report_cb(instance, 0, HID_REPORT_TYPE_OUTPUT, p_hid->epout_buf, (uint16_t)xferred_bytes);
|
||||
tud_hid_set_report_cb(instance, 0, HID_REPORT_TYPE_OUTPUT, p_epbuf->epout, (uint16_t)xferred_bytes);
|
||||
} else {
|
||||
tud_hid_report_failed_cb(instance, HID_REPORT_TYPE_OUTPUT, p_hid->epout_buf, (uint16_t) xferred_bytes);
|
||||
tud_hid_report_failed_cb(instance, HID_REPORT_TYPE_OUTPUT, p_epbuf->epout, (uint16_t) xferred_bytes);
|
||||
}
|
||||
|
||||
// prepare for new transfer
|
||||
TU_ASSERT(usbd_edpt_xfer(rhport, p_hid->ep_out, p_hid->epout_buf, sizeof(p_hid->epout_buf)));
|
||||
TU_ASSERT(usbd_edpt_xfer(rhport, p_hid->ep_out, p_epbuf->epout, CFG_TUD_HID_EP_BUFSIZE));
|
||||
}
|
||||
|
||||
return true;
|
||||
|
||||
@@ -45,12 +45,11 @@
|
||||
//--------------------------------------------------------------------+
|
||||
typedef struct {
|
||||
uint8_t daddr;
|
||||
|
||||
uint8_t itf_num;
|
||||
uint8_t ep_in;
|
||||
uint8_t ep_out;
|
||||
bool mounted; // Enumeration is complete
|
||||
|
||||
bool mounted; // Enumeration is complete
|
||||
uint8_t itf_protocol; // None, Keyboard, Mouse
|
||||
uint8_t protocol_mode; // Boot (0) or Report protocol (1)
|
||||
|
||||
@@ -59,15 +58,17 @@ typedef struct {
|
||||
|
||||
uint16_t epin_size;
|
||||
uint16_t epout_size;
|
||||
|
||||
CFG_TUH_MEM_ALIGN uint8_t epin_buf[CFG_TUH_HID_EPIN_BUFSIZE];
|
||||
CFG_TUH_MEM_ALIGN uint8_t epout_buf[CFG_TUH_HID_EPOUT_BUFSIZE];
|
||||
} hidh_interface_t;
|
||||
|
||||
CFG_TUH_MEM_SECTION
|
||||
tu_static hidh_interface_t _hidh_itf[CFG_TUH_HID];
|
||||
typedef struct {
|
||||
TUH_EPBUF_DEF(epin, CFG_TUH_HID_EPIN_BUFSIZE);
|
||||
TUH_EPBUF_DEF(epout, CFG_TUH_HID_EPOUT_BUFSIZE);
|
||||
} hidh_epbuf_t;
|
||||
|
||||
tu_static uint8_t _hidh_default_protocol = HID_PROTOCOL_BOOT;
|
||||
static hidh_interface_t _hidh_itf[CFG_TUH_HID];
|
||||
CFG_TUH_MEM_SECTION static hidh_epbuf_t _hidh_epbuf[CFG_TUH_HID];
|
||||
|
||||
static uint8_t _hidh_default_protocol = HID_PROTOCOL_BOOT;
|
||||
|
||||
//--------------------------------------------------------------------+
|
||||
// Helper
|
||||
@@ -78,6 +79,10 @@ TU_ATTR_ALWAYS_INLINE static inline hidh_interface_t* get_hid_itf(uint8_t daddr,
|
||||
return (p_hid->daddr == daddr) ? p_hid : NULL;
|
||||
}
|
||||
|
||||
TU_ATTR_ALWAYS_INLINE static inline hidh_epbuf_t* get_hid_epbuf(uint8_t idx) {
|
||||
return &_hidh_epbuf[idx];
|
||||
}
|
||||
|
||||
// Get instance ID by endpoint address
|
||||
static uint8_t get_idx_by_epaddr(uint8_t daddr, uint8_t ep_addr) {
|
||||
for (uint8_t idx = 0; idx < CFG_TUH_HID; idx++) {
|
||||
@@ -353,11 +358,12 @@ bool tuh_hid_receive_ready(uint8_t dev_addr, uint8_t idx) {
|
||||
bool tuh_hid_receive_report(uint8_t daddr, uint8_t idx) {
|
||||
hidh_interface_t* p_hid = get_hid_itf(daddr, idx);
|
||||
TU_VERIFY(p_hid);
|
||||
hidh_epbuf_t* epbuf = get_hid_epbuf(idx);
|
||||
|
||||
// claim endpoint
|
||||
TU_VERIFY(usbh_edpt_claim(daddr, p_hid->ep_in));
|
||||
|
||||
if (!usbh_edpt_xfer(daddr, p_hid->ep_in, p_hid->epin_buf, p_hid->epin_size)) {
|
||||
if (!usbh_edpt_xfer(daddr, p_hid->ep_in, epbuf->epin, p_hid->epin_size)) {
|
||||
usbh_edpt_release(daddr, p_hid->ep_in);
|
||||
return false;
|
||||
}
|
||||
@@ -381,6 +387,7 @@ bool tuh_hid_send_report(uint8_t daddr, uint8_t idx, uint8_t report_id, const vo
|
||||
|
||||
hidh_interface_t* p_hid = get_hid_itf(daddr, idx);
|
||||
TU_VERIFY(p_hid);
|
||||
hidh_epbuf_t* epbuf = get_hid_epbuf(idx);
|
||||
|
||||
if (p_hid->ep_out == 0) {
|
||||
// This HID does not have an out endpoint (other than control)
|
||||
@@ -396,16 +403,16 @@ bool tuh_hid_send_report(uint8_t daddr, uint8_t idx, uint8_t report_id, const vo
|
||||
|
||||
if (report_id == 0) {
|
||||
// No report ID in transmission
|
||||
memcpy(&p_hid->epout_buf[0], report, len);
|
||||
memcpy(&epbuf->epout[0], report, len);
|
||||
} else {
|
||||
p_hid->epout_buf[0] = report_id;
|
||||
memcpy(&p_hid->epout_buf[1], report, len);
|
||||
epbuf->epout[0] = report_id;
|
||||
memcpy(&epbuf->epout[1], report, len);
|
||||
++len; // 1 more byte for report_id
|
||||
}
|
||||
|
||||
TU_LOG3_MEM(p_hid->epout_buf, len, 2);
|
||||
|
||||
if (!usbh_edpt_xfer(daddr, p_hid->ep_out, p_hid->epout_buf, len)) {
|
||||
if (!usbh_edpt_xfer(daddr, p_hid->ep_out, epbuf->epout, len)) {
|
||||
usbh_edpt_release(daddr, p_hid->ep_out);
|
||||
return false;
|
||||
}
|
||||
@@ -434,14 +441,15 @@ bool hidh_xfer_cb(uint8_t daddr, uint8_t ep_addr, xfer_result_t result, uint32_t
|
||||
|
||||
hidh_interface_t* p_hid = get_hid_itf(daddr, idx);
|
||||
TU_VERIFY(p_hid);
|
||||
hidh_epbuf_t* epbuf = get_hid_epbuf(idx);
|
||||
|
||||
if (dir == TUSB_DIR_IN) {
|
||||
TU_LOG_DRV(" Get Report callback (%u, %u)\r\n", daddr, idx);
|
||||
TU_LOG3_MEM(p_hid->epin_buf, xferred_bytes, 2);
|
||||
tuh_hid_report_received_cb(daddr, idx, p_hid->epin_buf, (uint16_t) xferred_bytes);
|
||||
tuh_hid_report_received_cb(daddr, idx, epbuf->epin, (uint16_t) xferred_bytes);
|
||||
} else {
|
||||
if (tuh_hid_report_sent_cb) {
|
||||
tuh_hid_report_sent_cb(daddr, idx, p_hid->epout_buf, (uint16_t) xferred_bytes);
|
||||
tuh_hid_report_sent_cb(daddr, idx, epbuf->epout, (uint16_t) xferred_bytes);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -40,15 +40,13 @@
|
||||
// MACRO CONSTANT TYPEDEF
|
||||
//--------------------------------------------------------------------+
|
||||
|
||||
typedef struct
|
||||
{
|
||||
typedef struct {
|
||||
uint8_t buffer[4];
|
||||
uint8_t index;
|
||||
uint8_t total;
|
||||
}midid_stream_t;
|
||||
} midid_stream_t;
|
||||
|
||||
typedef struct
|
||||
{
|
||||
typedef struct {
|
||||
uint8_t itf_num;
|
||||
uint8_t ep_in;
|
||||
uint8_t ep_out;
|
||||
@@ -70,36 +68,36 @@ typedef struct
|
||||
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_MIDI_EP_BUFSIZE];
|
||||
CFG_TUSB_MEM_ALIGN uint8_t epin_buf[CFG_TUD_MIDI_EP_BUFSIZE];
|
||||
|
||||
} midid_interface_t;
|
||||
|
||||
#define ITF_MEM_RESET_SIZE offsetof(midid_interface_t, rx_ff)
|
||||
|
||||
// Endpoint Transfer buffer
|
||||
CFG_TUD_MEM_SECTION static struct {
|
||||
TUD_EPBUF_DEF(epin, CFG_TUD_MIDI_EP_BUFSIZE);
|
||||
TUD_EPBUF_DEF(epout, CFG_TUD_MIDI_EP_BUFSIZE);
|
||||
} _midid_epbuf[CFG_TUD_MIDI];
|
||||
|
||||
//--------------------------------------------------------------------+
|
||||
// INTERNAL OBJECT & FUNCTION DECLARATION
|
||||
//--------------------------------------------------------------------+
|
||||
CFG_TUD_MEM_SECTION midid_interface_t _midid_itf[CFG_TUD_MIDI];
|
||||
static midid_interface_t _midid_itf[CFG_TUD_MIDI];
|
||||
|
||||
bool tud_midi_n_mounted (uint8_t itf)
|
||||
{
|
||||
bool tud_midi_n_mounted (uint8_t itf) {
|
||||
midid_interface_t* midi = &_midid_itf[itf];
|
||||
return midi->ep_in && midi->ep_out;
|
||||
}
|
||||
|
||||
static void _prep_out_transaction (midid_interface_t* p_midi)
|
||||
{
|
||||
uint8_t const rhport = 0;
|
||||
static void _prep_out_transaction(uint8_t idx) {
|
||||
const uint8_t rhport = 0;
|
||||
midid_interface_t* p_midi = &_midid_itf[idx];
|
||||
uint16_t available = tu_fifo_remaining(&p_midi->rx_ff);
|
||||
|
||||
// Prepare for incoming data but only allow what we can store in the ring buffer.
|
||||
// TODO Actually we can still carry out the transfer, keeping count of received bytes
|
||||
// and slowly move it to the FIFO when read().
|
||||
// This pre-check reduces endpoint claiming
|
||||
TU_VERIFY(available >= sizeof(p_midi->epout_buf), );
|
||||
TU_VERIFY(available >= CFG_TUD_MIDI_EP_BUFSIZE, );
|
||||
|
||||
// claim endpoint
|
||||
TU_VERIFY(usbd_edpt_claim(rhport, p_midi->ep_out), );
|
||||
@@ -107,8 +105,8 @@ static void _prep_out_transaction (midid_interface_t* p_midi)
|
||||
// fifo can be changed before endpoint is claimed
|
||||
available = tu_fifo_remaining(&p_midi->rx_ff);
|
||||
|
||||
if ( available >= sizeof(p_midi->epout_buf) ) {
|
||||
usbd_edpt_xfer(rhport, p_midi->ep_out, p_midi->epout_buf, sizeof(p_midi->epout_buf));
|
||||
if ( available >= CFG_TUD_MIDI_EP_BUFSIZE ) {
|
||||
usbd_edpt_xfer(rhport, p_midi->ep_out, _midid_epbuf[idx].epout, CFG_TUD_MIDI_EP_BUFSIZE);
|
||||
}else
|
||||
{
|
||||
// Release endpoint since we don't make any transfer
|
||||
@@ -124,7 +122,7 @@ uint32_t tud_midi_n_available(uint8_t itf, uint8_t cable_num)
|
||||
(void) cable_num;
|
||||
|
||||
midid_interface_t* midi = &_midid_itf[itf];
|
||||
midid_stream_t const* stream = &midi->stream_read;
|
||||
const midid_stream_t* stream = &midi->stream_read;
|
||||
|
||||
// when using with packet API stream total & index are both zero
|
||||
return tu_fifo_count(&midi->rx_ff) + (uint8_t) (stream->total - stream->index);
|
||||
@@ -205,8 +203,8 @@ bool tud_midi_n_packet_read (uint8_t itf, uint8_t packet[4])
|
||||
midid_interface_t* midi = &_midid_itf[itf];
|
||||
TU_VERIFY(midi->ep_out);
|
||||
|
||||
uint32_t const num_read = tu_fifo_read_n(&midi->rx_ff, packet, 4);
|
||||
_prep_out_transaction(midi);
|
||||
const uint32_t num_read = tu_fifo_read_n(&midi->rx_ff, packet, 4);
|
||||
_prep_out_transaction(itf);
|
||||
return (num_read == 4);
|
||||
}
|
||||
|
||||
@@ -214,31 +212,31 @@ bool tud_midi_n_packet_read (uint8_t itf, uint8_t packet[4])
|
||||
// WRITE API
|
||||
//--------------------------------------------------------------------+
|
||||
|
||||
static uint32_t write_flush(midid_interface_t* midi)
|
||||
{
|
||||
// No data to send
|
||||
if ( !tu_fifo_count(&midi->tx_ff) ) return 0;
|
||||
static uint32_t write_flush(uint8_t idx) {
|
||||
midid_interface_t* midi = &_midid_itf[idx];
|
||||
|
||||
uint8_t const rhport = 0;
|
||||
if (!tu_fifo_count(&midi->tx_ff)) {
|
||||
return 0; // No data to send
|
||||
}
|
||||
|
||||
const uint8_t rhport = 0;
|
||||
|
||||
// skip if previous transfer not complete
|
||||
TU_VERIFY( usbd_edpt_claim(rhport, midi->ep_in), 0 );
|
||||
|
||||
uint16_t count = tu_fifo_read_n(&midi->tx_ff, midi->epin_buf, CFG_TUD_MIDI_EP_BUFSIZE);
|
||||
uint16_t count = tu_fifo_read_n(&midi->tx_ff, _midid_epbuf[idx].epin, CFG_TUD_MIDI_EP_BUFSIZE);
|
||||
|
||||
if (count)
|
||||
{
|
||||
TU_ASSERT( usbd_edpt_xfer(rhport, midi->ep_in, midi->epin_buf, count), 0 );
|
||||
if (count) {
|
||||
TU_ASSERT( usbd_edpt_xfer(rhport, midi->ep_in, _midid_epbuf[idx].epin, count), 0 );
|
||||
return count;
|
||||
}else
|
||||
{
|
||||
}else {
|
||||
// Release endpoint since we don't make any transfer
|
||||
usbd_edpt_release(rhport, midi->ep_in);
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
|
||||
uint32_t tud_midi_n_stream_write(uint8_t itf, uint8_t cable_num, uint8_t const* buffer, uint32_t bufsize)
|
||||
uint32_t tud_midi_n_stream_write(uint8_t itf, uint8_t cable_num, const uint8_t* buffer, uint32_t bufsize)
|
||||
{
|
||||
midid_interface_t* midi = &_midid_itf[itf];
|
||||
TU_VERIFY(midi->ep_in, 0);
|
||||
@@ -248,14 +246,13 @@ uint32_t tud_midi_n_stream_write(uint8_t itf, uint8_t cable_num, uint8_t const*
|
||||
uint32_t i = 0;
|
||||
while ( (i < bufsize) && (tu_fifo_remaining(&midi->tx_ff) >= 4) )
|
||||
{
|
||||
uint8_t const data = buffer[i];
|
||||
const uint8_t data = buffer[i];
|
||||
i++;
|
||||
|
||||
if ( stream->index == 0 )
|
||||
{
|
||||
//------------- New event packet -------------//
|
||||
|
||||
uint8_t const msg = data >> 4;
|
||||
const uint8_t msg = data >> 4;
|
||||
|
||||
stream->index = 2;
|
||||
stream->buffer[1] = data;
|
||||
@@ -340,9 +337,11 @@ uint32_t tud_midi_n_stream_write(uint8_t itf, uint8_t cable_num, uint8_t const*
|
||||
if ( stream->index == stream->total )
|
||||
{
|
||||
// zeroes unused bytes
|
||||
for(uint8_t idx = stream->total; idx < 4; idx++) stream->buffer[idx] = 0;
|
||||
for (uint8_t idx = stream->total; idx < 4; idx++) {
|
||||
stream->buffer[idx] = 0;
|
||||
}
|
||||
|
||||
uint16_t const count = tu_fifo_write_n(&midi->tx_ff, stream->buffer, 4);
|
||||
const uint16_t count = tu_fifo_write_n(&midi->tx_ff, stream->buffer, 4);
|
||||
|
||||
// complete current event packet, reset stream
|
||||
stream->index = stream->total = 0;
|
||||
@@ -352,20 +351,21 @@ uint32_t tud_midi_n_stream_write(uint8_t itf, uint8_t cable_num, uint8_t const*
|
||||
}
|
||||
}
|
||||
|
||||
write_flush(midi);
|
||||
write_flush(itf);
|
||||
|
||||
return i;
|
||||
}
|
||||
|
||||
bool tud_midi_n_packet_write (uint8_t itf, uint8_t const packet[4])
|
||||
{
|
||||
bool tud_midi_n_packet_write (uint8_t itf, const uint8_t packet[4]) {
|
||||
midid_interface_t* midi = &_midid_itf[itf];
|
||||
TU_VERIFY(midi->ep_in);
|
||||
|
||||
if (tu_fifo_remaining(&midi->tx_ff) < 4) return false;
|
||||
if (tu_fifo_remaining(&midi->tx_ff) < 4) {
|
||||
return false;
|
||||
}
|
||||
|
||||
tu_fifo_write_n(&midi->tx_ff, packet, 4);
|
||||
write_flush(midi);
|
||||
write_flush(itf);
|
||||
|
||||
return true;
|
||||
}
|
||||
@@ -429,7 +429,7 @@ void midid_reset(uint8_t rhport)
|
||||
}
|
||||
}
|
||||
|
||||
uint16_t midid_open(uint8_t rhport, tusb_desc_interface_t const * desc_itf, uint16_t max_len)
|
||||
uint16_t midid_open(uint8_t rhport, const tusb_desc_interface_t* desc_itf, uint16_t max_len)
|
||||
{
|
||||
// 1st Interface is Audio Control v1
|
||||
TU_VERIFY(TUSB_CLASS_AUDIO == desc_itf->bInterfaceClass &&
|
||||
@@ -437,7 +437,7 @@ uint16_t midid_open(uint8_t rhport, tusb_desc_interface_t const * desc_itf, uint
|
||||
AUDIO_FUNC_PROTOCOL_CODE_UNDEF == desc_itf->bInterfaceProtocol, 0);
|
||||
|
||||
uint16_t drv_len = tu_desc_len(desc_itf);
|
||||
uint8_t const * p_desc = tu_desc_next(desc_itf);
|
||||
const uint8_t* p_desc = tu_desc_next(desc_itf);
|
||||
|
||||
// Skip Class Specific descriptors
|
||||
while ( TUSB_DESC_CS_INTERFACE == tu_desc_type(p_desc) && drv_len <= max_len )
|
||||
@@ -448,7 +448,7 @@ uint16_t midid_open(uint8_t rhport, tusb_desc_interface_t const * desc_itf, uint
|
||||
|
||||
// 2nd Interface is MIDI Streaming
|
||||
TU_VERIFY(TUSB_DESC_INTERFACE == tu_desc_type(p_desc), 0);
|
||||
tusb_desc_interface_t const * desc_midi = (tusb_desc_interface_t const *) p_desc;
|
||||
const tusb_desc_interface_t* desc_midi = (const tusb_desc_interface_t*) p_desc;
|
||||
|
||||
TU_VERIFY(TUSB_CLASS_AUDIO == desc_midi->bInterfaceClass &&
|
||||
AUDIO_SUBCLASS_MIDI_STREAMING == desc_midi->bInterfaceSubClass &&
|
||||
@@ -456,11 +456,10 @@ uint16_t midid_open(uint8_t rhport, tusb_desc_interface_t const * desc_itf, uint
|
||||
|
||||
// Find available interface
|
||||
midid_interface_t * p_midi = NULL;
|
||||
for(uint8_t i=0; i<CFG_TUD_MIDI; i++)
|
||||
{
|
||||
if ( _midid_itf[i].ep_in == 0 && _midid_itf[i].ep_out == 0 )
|
||||
{
|
||||
p_midi = &_midid_itf[i];
|
||||
uint8_t idx;
|
||||
for(idx=0; idx<CFG_TUD_MIDI; idx++) {
|
||||
if ( _midid_itf[idx].ep_in == 0 && _midid_itf[idx].ep_out == 0 ) {
|
||||
p_midi = &_midid_itf[idx];
|
||||
break;
|
||||
}
|
||||
}
|
||||
@@ -479,8 +478,8 @@ uint16_t midid_open(uint8_t rhport, tusb_desc_interface_t const * desc_itf, uint
|
||||
{
|
||||
if ( TUSB_DESC_ENDPOINT == tu_desc_type(p_desc) )
|
||||
{
|
||||
TU_ASSERT(usbd_edpt_open(rhport, (tusb_desc_endpoint_t const *) p_desc), 0);
|
||||
uint8_t ep_addr = ((tusb_desc_endpoint_t const *) p_desc)->bEndpointAddress;
|
||||
TU_ASSERT(usbd_edpt_open(rhport, (const tusb_desc_endpoint_t*) p_desc), 0);
|
||||
uint8_t ep_addr = ((const tusb_desc_endpoint_t*) p_desc)->bEndpointAddress;
|
||||
|
||||
if (tu_edpt_dir(ep_addr) == TUSB_DIR_IN)
|
||||
{
|
||||
@@ -501,7 +500,7 @@ uint16_t midid_open(uint8_t rhport, tusb_desc_interface_t const * desc_itf, uint
|
||||
}
|
||||
|
||||
// Prepare for incoming data
|
||||
_prep_out_transaction(p_midi);
|
||||
_prep_out_transaction(idx);
|
||||
|
||||
return drv_len;
|
||||
}
|
||||
@@ -509,14 +508,9 @@ uint16_t midid_open(uint8_t rhport, tusb_desc_interface_t const * desc_itf, uint
|
||||
// Invoked when a control transfer occurred on an interface of this class
|
||||
// Driver response accordingly to the request and the transfer stage (setup/data/ack)
|
||||
// return false to stall control endpoint (e.g unsupported request)
|
||||
bool midid_control_xfer_cb(uint8_t rhport, uint8_t stage, tusb_control_request_t const * request)
|
||||
{
|
||||
(void) rhport;
|
||||
(void) stage;
|
||||
(void) request;
|
||||
|
||||
// driver doesn't support any request yet
|
||||
return false;
|
||||
bool midid_control_xfer_cb(uint8_t rhport, uint8_t stage, const tusb_control_request_t* request) {
|
||||
(void) rhport; (void) stage; (void) request;
|
||||
return false; // driver doesn't support any request yet
|
||||
}
|
||||
|
||||
bool midid_xfer_cb(uint8_t rhport, uint8_t ep_addr, xfer_result_t result, uint32_t xferred_bytes)
|
||||
@@ -524,40 +518,37 @@ bool midid_xfer_cb(uint8_t rhport, uint8_t ep_addr, xfer_result_t result, uint32
|
||||
(void) result;
|
||||
(void) rhport;
|
||||
|
||||
uint8_t itf;
|
||||
uint8_t idx;
|
||||
midid_interface_t* p_midi;
|
||||
|
||||
// Identify which interface to use
|
||||
for (itf = 0; itf < CFG_TUD_MIDI; itf++)
|
||||
{
|
||||
p_midi = &_midid_itf[itf];
|
||||
if ( ( ep_addr == p_midi->ep_out ) || ( ep_addr == p_midi->ep_in ) ) break;
|
||||
for (idx = 0; idx < CFG_TUD_MIDI; idx++) {
|
||||
p_midi = &_midid_itf[idx];
|
||||
if ((ep_addr == p_midi->ep_out) || (ep_addr == p_midi->ep_in)) {
|
||||
break;
|
||||
}
|
||||
}
|
||||
TU_ASSERT(itf < CFG_TUD_MIDI);
|
||||
TU_ASSERT(idx < CFG_TUD_MIDI);
|
||||
|
||||
// receive new data
|
||||
if ( ep_addr == p_midi->ep_out )
|
||||
{
|
||||
tu_fifo_write_n(&p_midi->rx_ff, p_midi->epout_buf, (uint16_t) xferred_bytes);
|
||||
if (ep_addr == p_midi->ep_out) {
|
||||
tu_fifo_write_n(&p_midi->rx_ff, _midid_epbuf[idx].epout, (uint16_t)xferred_bytes);
|
||||
|
||||
// invoke receive callback if available
|
||||
if (tud_midi_rx_cb) tud_midi_rx_cb(itf);
|
||||
if (tud_midi_rx_cb) {
|
||||
tud_midi_rx_cb(idx);
|
||||
}
|
||||
|
||||
// prepare for next
|
||||
// TODO for now ep_out is not used by public API therefore there is no race condition,
|
||||
// and does not need to claim like ep_in
|
||||
_prep_out_transaction(p_midi);
|
||||
}
|
||||
else if ( ep_addr == p_midi->ep_in )
|
||||
{
|
||||
if (0 == write_flush(p_midi))
|
||||
{
|
||||
_prep_out_transaction(idx);
|
||||
} else if (ep_addr == p_midi->ep_in) {
|
||||
if (0 == write_flush(idx)) {
|
||||
// If there is no data left, a ZLP should be sent if
|
||||
// xferred_bytes is multiple of EP size and not zero
|
||||
if ( !tu_fifo_count(&p_midi->tx_ff) && xferred_bytes && (0 == (xferred_bytes % CFG_TUD_MIDI_EP_BUFSIZE)) )
|
||||
{
|
||||
if ( usbd_edpt_claim(rhport, p_midi->ep_in) )
|
||||
{
|
||||
if (!tu_fifo_count(&p_midi->tx_ff) && xferred_bytes && (0 == (xferred_bytes % CFG_TUD_MIDI_EP_BUFSIZE))) {
|
||||
if (usbd_edpt_claim(rhport, p_midi->ep_in)) {
|
||||
usbd_edpt_xfer(rhport, p_midi->ep_in, NULL, 0);
|
||||
}
|
||||
}
|
||||
|
||||
File diff suppressed because it is too large
Load Diff
@@ -54,38 +54,37 @@ typedef struct {
|
||||
uint8_t itf_num;
|
||||
uint8_t ep_in;
|
||||
uint8_t ep_out;
|
||||
|
||||
uint8_t max_lun;
|
||||
|
||||
volatile bool configured; // Receive SET_CONFIGURE
|
||||
volatile bool mounted; // Enumeration is complete
|
||||
|
||||
struct {
|
||||
uint32_t block_size;
|
||||
uint32_t block_count;
|
||||
} capacity[CFG_TUH_MSC_MAXLUN];
|
||||
|
||||
//------------- SCSI -------------//
|
||||
// SCSI command data
|
||||
uint8_t stage;
|
||||
void* buffer;
|
||||
tuh_msc_complete_cb_t complete_cb;
|
||||
uintptr_t complete_arg;
|
||||
|
||||
CFG_TUH_MEM_ALIGN msc_cbw_t cbw;
|
||||
CFG_TUH_MEM_ALIGN msc_csw_t csw;
|
||||
struct {
|
||||
uint32_t block_size;
|
||||
uint32_t block_count;
|
||||
} capacity[CFG_TUH_MSC_MAXLUN];
|
||||
} msch_interface_t;
|
||||
|
||||
CFG_TUH_MEM_SECTION static msch_interface_t _msch_itf[CFG_TUH_DEVICE_MAX];
|
||||
typedef struct {
|
||||
TUH_EPBUF_TYPE_DEF(msc_cbw_t, cbw);
|
||||
TUH_EPBUF_TYPE_DEF(msc_csw_t, csw);
|
||||
} msch_epbuf_t;
|
||||
|
||||
// buffer used to read scsi information when mounted
|
||||
// largest response data currently is inquiry TODO Inquiry is not part of enum anymore
|
||||
CFG_TUH_MEM_SECTION CFG_TUH_MEM_ALIGN
|
||||
static uint8_t _msch_buffer[sizeof(scsi_inquiry_resp_t)];
|
||||
static msch_interface_t _msch_itf[CFG_TUH_DEVICE_MAX];
|
||||
CFG_TUH_MEM_SECTION static msch_epbuf_t _msch_epbuf[CFG_TUH_DEVICE_MAX];
|
||||
|
||||
// FIXME potential nul reference
|
||||
TU_ATTR_ALWAYS_INLINE
|
||||
static inline msch_interface_t* get_itf(uint8_t dev_addr) {
|
||||
return &_msch_itf[dev_addr - 1];
|
||||
TU_ATTR_ALWAYS_INLINE static inline msch_interface_t* get_itf(uint8_t daddr) {
|
||||
return &_msch_itf[daddr - 1];
|
||||
}
|
||||
|
||||
TU_ATTR_ALWAYS_INLINE static inline msch_epbuf_t* get_epbuf(uint8_t daddr) {
|
||||
return &_msch_epbuf[daddr - 1];
|
||||
}
|
||||
|
||||
//--------------------------------------------------------------------+
|
||||
@@ -133,14 +132,15 @@ bool tuh_msc_scsi_command(uint8_t daddr, msc_cbw_t const* cbw, void* data,
|
||||
|
||||
// claim endpoint
|
||||
TU_VERIFY(usbh_edpt_claim(daddr, p_msc->ep_out));
|
||||
msch_epbuf_t* epbuf = get_epbuf(daddr);
|
||||
|
||||
p_msc->cbw = *cbw;
|
||||
p_msc->stage = MSC_STAGE_CMD;
|
||||
epbuf->cbw = *cbw;
|
||||
p_msc->buffer = data;
|
||||
p_msc->complete_cb = complete_cb;
|
||||
p_msc->complete_arg = arg;
|
||||
p_msc->stage = MSC_STAGE_CMD;
|
||||
|
||||
if (!usbh_edpt_xfer(daddr, p_msc->ep_out, (uint8_t*) &p_msc->cbw, sizeof(msc_cbw_t))) {
|
||||
if (!usbh_edpt_xfer(daddr, p_msc->ep_out, (uint8_t*) &epbuf->cbw, sizeof(msc_cbw_t))) {
|
||||
usbh_edpt_release(daddr, p_msc->ep_out);
|
||||
return false;
|
||||
}
|
||||
@@ -286,6 +286,7 @@ bool tuh_msc_reset(uint8_t dev_addr) {
|
||||
//--------------------------------------------------------------------+
|
||||
bool msch_init(void) {
|
||||
TU_LOG_DRV("sizeof(msch_interface_t) = %u\r\n", sizeof(msch_interface_t));
|
||||
TU_LOG_DRV("sizeof(msch_epbuf_t) = %u\r\n", sizeof(msch_epbuf_t));
|
||||
tu_memclr(_msch_itf, sizeof(_msch_itf));
|
||||
return true;
|
||||
}
|
||||
@@ -303,7 +304,9 @@ void msch_close(uint8_t dev_addr) {
|
||||
|
||||
// invoke Application Callback
|
||||
if (p_msc->mounted) {
|
||||
if (tuh_msc_umount_cb) tuh_msc_umount_cb(dev_addr);
|
||||
if (tuh_msc_umount_cb) {
|
||||
tuh_msc_umount_cb(dev_addr);
|
||||
}
|
||||
}
|
||||
|
||||
tu_memclr(p_msc, sizeof(msch_interface_t));
|
||||
@@ -311,30 +314,28 @@ void msch_close(uint8_t dev_addr) {
|
||||
|
||||
bool msch_xfer_cb(uint8_t dev_addr, uint8_t ep_addr, xfer_result_t event, uint32_t xferred_bytes) {
|
||||
msch_interface_t* p_msc = get_itf(dev_addr);
|
||||
msc_cbw_t const * cbw = &p_msc->cbw;
|
||||
msc_csw_t * csw = &p_msc->csw;
|
||||
msch_epbuf_t* epbuf = get_epbuf(dev_addr);
|
||||
msc_cbw_t const * cbw = &epbuf->cbw;
|
||||
msc_csw_t * csw = &epbuf->csw;
|
||||
|
||||
switch (p_msc->stage) {
|
||||
case MSC_STAGE_CMD:
|
||||
// Must be Command Block
|
||||
TU_ASSERT(ep_addr == p_msc->ep_out && event == XFER_RESULT_SUCCESS && xferred_bytes == sizeof(msc_cbw_t));
|
||||
|
||||
if (cbw->total_bytes && p_msc->buffer) {
|
||||
// Data stage if any
|
||||
p_msc->stage = MSC_STAGE_DATA;
|
||||
uint8_t const ep_data = (cbw->dir & TUSB_DIR_IN_MASK) ? p_msc->ep_in : p_msc->ep_out;
|
||||
TU_ASSERT(usbh_edpt_xfer(dev_addr, ep_data, p_msc->buffer, (uint16_t) cbw->total_bytes));
|
||||
} else {
|
||||
// Status stage
|
||||
p_msc->stage = MSC_STAGE_STATUS;
|
||||
TU_ASSERT(usbh_edpt_xfer(dev_addr, p_msc->ep_in, (uint8_t*) &p_msc->csw, (uint16_t) sizeof(msc_csw_t)));
|
||||
break;
|
||||
}
|
||||
break;
|
||||
|
||||
TU_ATTR_FALLTHROUGH; // fallthrough to status stage
|
||||
|
||||
case MSC_STAGE_DATA:
|
||||
// Status stage
|
||||
p_msc->stage = MSC_STAGE_STATUS;
|
||||
TU_ASSERT(usbh_edpt_xfer(dev_addr, p_msc->ep_in, (uint8_t*) &p_msc->csw, (uint16_t) sizeof(msc_csw_t)));
|
||||
TU_ASSERT(usbh_edpt_xfer(dev_addr, p_msc->ep_in, (uint8_t*) csw, (uint16_t) sizeof(msc_csw_t)));
|
||||
break;
|
||||
|
||||
case MSC_STAGE_STATUS:
|
||||
@@ -399,10 +400,9 @@ bool msch_open(uint8_t rhport, uint8_t dev_addr, tusb_desc_interface_t const* de
|
||||
return true;
|
||||
}
|
||||
|
||||
bool msch_set_config(uint8_t dev_addr, uint8_t itf_num) {
|
||||
msch_interface_t* p_msc = get_itf(dev_addr);
|
||||
bool msch_set_config(uint8_t daddr, uint8_t itf_num) {
|
||||
msch_interface_t* p_msc = get_itf(daddr);
|
||||
TU_ASSERT(p_msc->itf_num == itf_num);
|
||||
|
||||
p_msc->configured = true;
|
||||
|
||||
//------------- Get Max Lun -------------//
|
||||
@@ -419,11 +419,12 @@ bool msch_set_config(uint8_t dev_addr, uint8_t itf_num) {
|
||||
.wLength = 1
|
||||
};
|
||||
|
||||
uint8_t* enum_buf = usbh_get_enum_buf();
|
||||
tuh_xfer_t xfer = {
|
||||
.daddr = dev_addr,
|
||||
.daddr = daddr,
|
||||
.ep_addr = 0,
|
||||
.setup = &request,
|
||||
.buffer = _msch_buffer,
|
||||
.buffer = enum_buf,
|
||||
.complete_cb = config_get_maxlun_complete,
|
||||
.user_data = 0
|
||||
};
|
||||
@@ -436,9 +437,13 @@ static void config_get_maxlun_complete(tuh_xfer_t* xfer) {
|
||||
uint8_t const daddr = xfer->daddr;
|
||||
msch_interface_t* p_msc = get_itf(daddr);
|
||||
|
||||
// STALL means zero
|
||||
p_msc->max_lun = (XFER_RESULT_SUCCESS == xfer->result) ? _msch_buffer[0] : 0;
|
||||
p_msc->max_lun++; // MAX LUN is minus 1 by specs
|
||||
// MAXLUN's response is minus 1 by specs, STALL means 1
|
||||
if (XFER_RESULT_SUCCESS == xfer->result) {
|
||||
uint8_t* enum_buf = usbh_get_enum_buf();
|
||||
p_msc->max_lun = enum_buf[0] + 1;
|
||||
} else {
|
||||
p_msc->max_lun = 1;
|
||||
}
|
||||
|
||||
TU_LOG_DRV(" Max LUN = %u\r\n", p_msc->max_lun);
|
||||
|
||||
@@ -451,18 +456,19 @@ static void config_get_maxlun_complete(tuh_xfer_t* xfer) {
|
||||
static bool config_test_unit_ready_complete(uint8_t dev_addr, tuh_msc_complete_data_t const* cb_data) {
|
||||
msc_cbw_t const* cbw = cb_data->cbw;
|
||||
msc_csw_t const* csw = cb_data->csw;
|
||||
uint8_t* enum_buf = usbh_get_enum_buf();
|
||||
|
||||
if (csw->status == 0) {
|
||||
// Unit is ready, read its capacity
|
||||
TU_LOG_DRV("SCSI Read Capacity\r\n");
|
||||
tuh_msc_read_capacity(dev_addr, cbw->lun, (scsi_read_capacity10_resp_t*) ((void*) _msch_buffer),
|
||||
tuh_msc_read_capacity(dev_addr, cbw->lun, (scsi_read_capacity10_resp_t*) (uintptr_t) enum_buf,
|
||||
config_read_capacity_complete, 0);
|
||||
} else {
|
||||
// Note: During enumeration, some device fails Test Unit Ready and require a few retries
|
||||
// with Request Sense to start working !!
|
||||
// TODO limit number of retries
|
||||
TU_LOG_DRV("SCSI Request Sense\r\n");
|
||||
TU_ASSERT(tuh_msc_request_sense(dev_addr, cbw->lun, _msch_buffer, config_request_sense_complete, 0));
|
||||
TU_ASSERT(tuh_msc_request_sense(dev_addr, cbw->lun, enum_buf, config_request_sense_complete, 0));
|
||||
}
|
||||
|
||||
return true;
|
||||
@@ -480,19 +486,20 @@ static bool config_request_sense_complete(uint8_t dev_addr, tuh_msc_complete_dat
|
||||
static bool config_read_capacity_complete(uint8_t dev_addr, tuh_msc_complete_data_t const* cb_data) {
|
||||
msc_cbw_t const* cbw = cb_data->cbw;
|
||||
msc_csw_t const* csw = cb_data->csw;
|
||||
|
||||
TU_ASSERT(csw->status == 0);
|
||||
|
||||
msch_interface_t* p_msc = get_itf(dev_addr);
|
||||
uint8_t* enum_buf = usbh_get_enum_buf();
|
||||
|
||||
// Capacity response field: Block size and Last LBA are both Big-Endian
|
||||
scsi_read_capacity10_resp_t* resp = (scsi_read_capacity10_resp_t*) ((void*) _msch_buffer);
|
||||
scsi_read_capacity10_resp_t* resp = (scsi_read_capacity10_resp_t*) (uintptr_t) enum_buf;
|
||||
p_msc->capacity[cbw->lun].block_count = tu_ntohl(resp->last_lba) + 1;
|
||||
p_msc->capacity[cbw->lun].block_size = tu_ntohl(resp->block_size);
|
||||
|
||||
// Mark enumeration is complete
|
||||
p_msc->mounted = true;
|
||||
if (tuh_msc_mount_cb) tuh_msc_mount_cb(dev_addr);
|
||||
if (tuh_msc_mount_cb) {
|
||||
tuh_msc_mount_cb(dev_addr);
|
||||
}
|
||||
|
||||
// notify usbh that driver enumeration is complete
|
||||
usbh_driver_set_config_complete(dev_addr, p_msc->itf_num);
|
||||
|
||||
@@ -73,10 +73,12 @@ uint32_t tuh_msc_get_block_size(uint8_t dev_addr, uint8_t lun);
|
||||
// Perform a full SCSI command (cbw, data, csw) in non-blocking manner.
|
||||
// Complete callback is invoked when SCSI op is complete.
|
||||
// return true if success, false if there is already pending operation.
|
||||
// NOTE: buffer must be accessible by USB/DMA controller, aligned correctly and multiple of cache line if enabled
|
||||
bool tuh_msc_scsi_command(uint8_t daddr, msc_cbw_t const* cbw, void* data, tuh_msc_complete_cb_t complete_cb, uintptr_t arg);
|
||||
|
||||
// Perform SCSI Inquiry command
|
||||
// Complete callback is invoked when SCSI op is complete.
|
||||
// NOTE: response must be accessible by USB/DMA controller, aligned correctly and multiple of cache line if enabled
|
||||
bool tuh_msc_inquiry(uint8_t dev_addr, uint8_t lun, scsi_inquiry_resp_t* response, tuh_msc_complete_cb_t complete_cb, uintptr_t arg);
|
||||
|
||||
// Perform SCSI Test Unit Ready command
|
||||
@@ -85,14 +87,17 @@ bool tuh_msc_test_unit_ready(uint8_t dev_addr, uint8_t lun, tuh_msc_complete_cb_
|
||||
|
||||
// Perform SCSI Request Sense 10 command
|
||||
// Complete callback is invoked when SCSI op is complete.
|
||||
// NOTE: response must be accessible by USB/DMA controller, aligned correctly and multiple of cache line if enabled
|
||||
bool tuh_msc_request_sense(uint8_t dev_addr, uint8_t lun, void *response, tuh_msc_complete_cb_t complete_cb, uintptr_t arg);
|
||||
|
||||
// Perform SCSI Read 10 command. Read n blocks starting from LBA to buffer
|
||||
// Complete callback is invoked when SCSI op is complete.
|
||||
// NOTE: buffer must be accessible by USB/DMA controller, aligned correctly and multiple of cache line if enabled
|
||||
bool tuh_msc_read10(uint8_t dev_addr, uint8_t lun, void * buffer, uint32_t lba, uint16_t block_count, tuh_msc_complete_cb_t complete_cb, uintptr_t arg);
|
||||
|
||||
// Perform SCSI Write 10 command. Write n blocks starting from LBA to device
|
||||
// Complete callback is invoked when SCSI op is complete.
|
||||
// NOTE: buffer must be accessible by USB/DMA controller, aligned correctly and multiple of cache line if enabled
|
||||
bool tuh_msc_write10(uint8_t dev_addr, uint8_t lun, void const * buffer, uint32_t lba, uint16_t block_count, tuh_msc_complete_cb_t complete_cb, uintptr_t arg);
|
||||
|
||||
// Perform SCSI Read Capacity 10 command
|
||||
@@ -116,7 +121,7 @@ TU_ATTR_WEAK void tuh_msc_umount_cb(uint8_t dev_addr);
|
||||
bool msch_init (void);
|
||||
bool msch_deinit (void);
|
||||
bool msch_open (uint8_t rhport, uint8_t dev_addr, tusb_desc_interface_t const *desc_itf, uint16_t max_len);
|
||||
bool msch_set_config (uint8_t dev_addr, uint8_t itf_num);
|
||||
bool msch_set_config (uint8_t daddr, uint8_t itf_num);
|
||||
void msch_close (uint8_t dev_addr);
|
||||
bool msch_xfer_cb (uint8_t dev_addr, uint8_t ep_addr, xfer_result_t event, uint32_t xferred_bytes);
|
||||
|
||||
|
||||
@@ -35,13 +35,18 @@
|
||||
#include "net_device.h"
|
||||
#include "rndis_protocol.h"
|
||||
|
||||
void rndis_class_set_handler(uint8_t *data, int size); /* found in ./misc/networking/rndis_reports.c */
|
||||
extern void rndis_class_set_handler(uint8_t *data, int size); /* found in ./misc/networking/rndis_reports.c */
|
||||
|
||||
#define CFG_TUD_NET_PACKET_PREFIX_LEN sizeof(rndis_data_packet_t)
|
||||
#define CFG_TUD_NET_PACKET_SUFFIX_LEN 0
|
||||
|
||||
#define NETD_PACKET_SIZE (CFG_TUD_NET_PACKET_PREFIX_LEN + CFG_TUD_NET_MTU + CFG_TUD_NET_PACKET_PREFIX_LEN)
|
||||
#define NETD_CONTROL_SIZE 120
|
||||
|
||||
//--------------------------------------------------------------------+
|
||||
// MACRO CONSTANT TYPEDEF
|
||||
//--------------------------------------------------------------------+
|
||||
typedef struct
|
||||
{
|
||||
typedef struct {
|
||||
uint8_t itf_num; // Index number of Management Interface, +1 for Data Interface
|
||||
uint8_t itf_data_alt; // Alternate setting of Data Interface. 0 : inactive, 1 : active
|
||||
|
||||
@@ -55,78 +60,44 @@ typedef struct
|
||||
// TODO since configuration descriptor may not be long-lived memory, we should
|
||||
// keep a copy of endpoint attribute instead
|
||||
uint8_t const * ecm_desc_epdata;
|
||||
|
||||
} netd_interface_t;
|
||||
|
||||
#define CFG_TUD_NET_PACKET_PREFIX_LEN sizeof(rndis_data_packet_t)
|
||||
#define CFG_TUD_NET_PACKET_SUFFIX_LEN 0
|
||||
|
||||
CFG_TUD_MEM_SECTION CFG_TUSB_MEM_ALIGN tu_static
|
||||
uint8_t received[CFG_TUD_NET_PACKET_PREFIX_LEN + CFG_TUD_NET_MTU + CFG_TUD_NET_PACKET_PREFIX_LEN];
|
||||
|
||||
CFG_TUD_MEM_SECTION CFG_TUSB_MEM_ALIGN tu_static
|
||||
uint8_t transmitted[CFG_TUD_NET_PACKET_PREFIX_LEN + CFG_TUD_NET_MTU + CFG_TUD_NET_PACKET_PREFIX_LEN];
|
||||
|
||||
struct ecm_notify_struct
|
||||
{
|
||||
typedef struct ecm_notify_struct {
|
||||
tusb_control_request_t header;
|
||||
uint32_t downlink, uplink;
|
||||
};
|
||||
} ecm_notify_t;
|
||||
|
||||
tu_static const struct ecm_notify_struct ecm_notify_nc =
|
||||
{
|
||||
.header = {
|
||||
.bmRequestType = 0xA1,
|
||||
.bRequest = 0 /* NETWORK_CONNECTION aka NetworkConnection */,
|
||||
.wValue = 1 /* Connected */,
|
||||
.wLength = 0,
|
||||
},
|
||||
};
|
||||
typedef struct {
|
||||
TUD_EPBUF_DEF(rx, NETD_PACKET_SIZE);
|
||||
TUD_EPBUF_DEF(tx, NETD_PACKET_SIZE);
|
||||
|
||||
tu_static const struct ecm_notify_struct ecm_notify_csc =
|
||||
{
|
||||
.header = {
|
||||
.bmRequestType = 0xA1,
|
||||
.bRequest = 0x2A /* CONNECTION_SPEED_CHANGE aka ConnectionSpeedChange */,
|
||||
.wLength = 8,
|
||||
},
|
||||
.downlink = 9728000,
|
||||
.uplink = 9728000,
|
||||
};
|
||||
|
||||
// TODO remove CFG_TUD_MEM_SECTION, control internal buffer is already in this special section
|
||||
CFG_TUD_MEM_SECTION CFG_TUSB_MEM_ALIGN tu_static union
|
||||
{
|
||||
uint8_t rndis_buf[120];
|
||||
struct ecm_notify_struct ecm_buf;
|
||||
} notify;
|
||||
TUD_EPBUF_DEF(notify, sizeof(ecm_notify_t));
|
||||
TUD_EPBUF_DEF(ctrl, NETD_CONTROL_SIZE);
|
||||
} netd_epbuf_t;
|
||||
|
||||
//--------------------------------------------------------------------+
|
||||
// INTERNAL OBJECT & FUNCTION DECLARATION
|
||||
//--------------------------------------------------------------------+
|
||||
// TODO remove CFG_TUD_MEM_SECTION
|
||||
CFG_TUD_MEM_SECTION tu_static netd_interface_t _netd_itf;
|
||||
static netd_interface_t _netd_itf;
|
||||
CFG_TUD_MEM_SECTION static netd_epbuf_t _netd_epbuf;
|
||||
static bool can_xmit;
|
||||
|
||||
tu_static bool can_xmit;
|
||||
|
||||
void tud_network_recv_renew(void)
|
||||
{
|
||||
usbd_edpt_xfer(0, _netd_itf.ep_out, received, sizeof(received));
|
||||
void tud_network_recv_renew(void) {
|
||||
usbd_edpt_xfer(0, _netd_itf.ep_out, _netd_epbuf.rx, NETD_PACKET_SIZE);
|
||||
}
|
||||
|
||||
static void do_in_xfer(uint8_t *buf, uint16_t len)
|
||||
{
|
||||
static void do_in_xfer(uint8_t *buf, uint16_t len) {
|
||||
can_xmit = false;
|
||||
usbd_edpt_xfer(0, _netd_itf.ep_in, buf, len);
|
||||
}
|
||||
|
||||
void netd_report(uint8_t *buf, uint16_t len)
|
||||
{
|
||||
uint8_t const rhport = 0;
|
||||
void netd_report(uint8_t *buf, uint16_t len) {
|
||||
const uint8_t rhport = 0;
|
||||
len = tu_min16(len, sizeof(ecm_notify_t));
|
||||
|
||||
// skip if previous report not yet acknowledged by host
|
||||
if ( usbd_edpt_busy(rhport, _netd_itf.ep_notif) ) return;
|
||||
usbd_edpt_xfer(rhport, _netd_itf.ep_notif, buf, len);
|
||||
TU_VERIFY(usbd_edpt_claim(rhport, _netd_itf.ep_notif), );
|
||||
memcpy(_netd_epbuf.notify, buf, len);
|
||||
usbd_edpt_xfer(rhport, _netd_itf.ep_notif, _netd_epbuf.notify, len);
|
||||
}
|
||||
|
||||
//--------------------------------------------------------------------+
|
||||
@@ -140,15 +111,12 @@ bool netd_deinit(void) {
|
||||
return true;
|
||||
}
|
||||
|
||||
void netd_reset(uint8_t rhport)
|
||||
{
|
||||
void netd_reset(uint8_t rhport) {
|
||||
(void) rhport;
|
||||
|
||||
netd_init();
|
||||
}
|
||||
|
||||
uint16_t netd_open(uint8_t rhport, tusb_desc_interface_t const * itf_desc, uint16_t max_len)
|
||||
{
|
||||
uint16_t netd_open(uint8_t rhport, tusb_desc_interface_t const * itf_desc, uint16_t max_len) {
|
||||
bool const is_rndis = (TUD_RNDIS_ITF_CLASS == itf_desc->bInterfaceClass &&
|
||||
TUD_RNDIS_ITF_SUBCLASS == itf_desc->bInterfaceSubClass &&
|
||||
TUD_RNDIS_ITF_PROTOCOL == itf_desc->bInterfaceProtocol);
|
||||
@@ -172,21 +140,19 @@ uint16_t netd_open(uint8_t rhport, tusb_desc_interface_t const * itf_desc, uint1
|
||||
uint8_t const * p_desc = tu_desc_next( itf_desc );
|
||||
|
||||
// Communication Functional Descriptors
|
||||
while ( TUSB_DESC_CS_INTERFACE == tu_desc_type(p_desc) && drv_len <= max_len )
|
||||
{
|
||||
while (TUSB_DESC_CS_INTERFACE == tu_desc_type(p_desc) && drv_len <= max_len) {
|
||||
drv_len += tu_desc_len(p_desc);
|
||||
p_desc = tu_desc_next(p_desc);
|
||||
}
|
||||
|
||||
// notification endpoint (if any)
|
||||
if ( TUSB_DESC_ENDPOINT == tu_desc_type(p_desc) )
|
||||
{
|
||||
TU_ASSERT( usbd_edpt_open(rhport, (tusb_desc_endpoint_t const *) p_desc), 0 );
|
||||
if (TUSB_DESC_ENDPOINT == tu_desc_type(p_desc)) {
|
||||
TU_ASSERT(usbd_edpt_open(rhport, (tusb_desc_endpoint_t const *) p_desc), 0);
|
||||
|
||||
_netd_itf.ep_notif = ((tusb_desc_endpoint_t const *) p_desc)->bEndpointAddress;
|
||||
_netd_itf.ep_notif = ((tusb_desc_endpoint_t const*)p_desc)->bEndpointAddress;
|
||||
|
||||
drv_len += tu_desc_len(p_desc);
|
||||
p_desc = tu_desc_next(p_desc);
|
||||
p_desc = tu_desc_next(p_desc);
|
||||
}
|
||||
|
||||
//------------- Data Interface -------------//
|
||||
@@ -196,27 +162,24 @@ uint16_t netd_open(uint8_t rhport, tusb_desc_interface_t const * itf_desc, uint1
|
||||
// - 1 : IN & OUT endpoints for active networking
|
||||
TU_ASSERT(TUSB_DESC_INTERFACE == tu_desc_type(p_desc), 0);
|
||||
|
||||
do
|
||||
{
|
||||
do {
|
||||
tusb_desc_interface_t const * data_itf_desc = (tusb_desc_interface_t const *) p_desc;
|
||||
TU_ASSERT(TUSB_CLASS_CDC_DATA == data_itf_desc->bInterfaceClass, 0);
|
||||
|
||||
drv_len += tu_desc_len(p_desc);
|
||||
p_desc = tu_desc_next(p_desc);
|
||||
}while( _netd_itf.ecm_mode && (TUSB_DESC_INTERFACE == tu_desc_type(p_desc)) && (drv_len <= max_len) );
|
||||
} while (_netd_itf.ecm_mode && (TUSB_DESC_INTERFACE == tu_desc_type(p_desc)) && (drv_len <= max_len));
|
||||
|
||||
// Pair of endpoints
|
||||
TU_ASSERT(TUSB_DESC_ENDPOINT == tu_desc_type(p_desc), 0);
|
||||
|
||||
if ( _netd_itf.ecm_mode )
|
||||
{
|
||||
if (_netd_itf.ecm_mode) {
|
||||
// ECM by default is in-active, save the endpoint attribute
|
||||
// to open later when received setInterface
|
||||
_netd_itf.ecm_desc_epdata = p_desc;
|
||||
}else
|
||||
{
|
||||
} else {
|
||||
// Open endpoint pair for RNDIS
|
||||
TU_ASSERT( usbd_open_edpt_pair(rhport, p_desc, 2, TUSB_XFER_BULK, &_netd_itf.ep_out, &_netd_itf.ep_in), 0 );
|
||||
TU_ASSERT(usbd_open_edpt_pair(rhport, p_desc, 2, TUSB_XFER_BULK, &_netd_itf.ep_out, &_netd_itf.ep_in), 0);
|
||||
|
||||
tud_network_init_cb();
|
||||
|
||||
@@ -232,38 +195,50 @@ uint16_t netd_open(uint8_t rhport, tusb_desc_interface_t const * itf_desc, uint1
|
||||
return drv_len;
|
||||
}
|
||||
|
||||
static void ecm_report(bool nc)
|
||||
{
|
||||
notify.ecm_buf = (nc) ? ecm_notify_nc : ecm_notify_csc;
|
||||
notify.ecm_buf.header.wIndex = _netd_itf.itf_num;
|
||||
netd_report((uint8_t *)¬ify.ecm_buf, (nc) ? sizeof(notify.ecm_buf.header) : sizeof(notify.ecm_buf));
|
||||
static void ecm_report(bool nc) {
|
||||
const ecm_notify_t ecm_notify_nc = {
|
||||
.header = {
|
||||
.bmRequestType = 0xA1,
|
||||
.bRequest = 0, /* NETWORK_CONNECTION aka NetworkConnection */
|
||||
.wValue = 1, /* Connected */
|
||||
.wLength = 0,
|
||||
},
|
||||
};
|
||||
|
||||
const ecm_notify_t ecm_notify_csc = {
|
||||
.header = {
|
||||
.bmRequestType = 0xA1,
|
||||
.bRequest = 0x2A, /* CONNECTION_SPEED_CHANGE aka ConnectionSpeedChange */
|
||||
.wLength = 8,
|
||||
},
|
||||
.downlink = 9728000,
|
||||
.uplink = 9728000,
|
||||
};
|
||||
|
||||
ecm_notify_t notify = (nc) ? ecm_notify_nc : ecm_notify_csc;
|
||||
notify.header.wIndex = _netd_itf.itf_num;
|
||||
netd_report((uint8_t *)¬ify, (nc) ? sizeof(notify.header) : sizeof(notify));
|
||||
}
|
||||
|
||||
// Invoked when a control transfer occurred on an interface of this class
|
||||
// Driver response accordingly to the request and the transfer stage (setup/data/ack)
|
||||
// return false to stall control endpoint (e.g unsupported request)
|
||||
bool netd_control_xfer_cb (uint8_t rhport, uint8_t stage, tusb_control_request_t const * request)
|
||||
{
|
||||
if ( stage == CONTROL_STAGE_SETUP )
|
||||
{
|
||||
switch ( request->bmRequestType_bit.type )
|
||||
{
|
||||
bool netd_control_xfer_cb (uint8_t rhport, uint8_t stage, tusb_control_request_t const * request) {
|
||||
if (stage == CONTROL_STAGE_SETUP) {
|
||||
switch (request->bmRequestType_bit.type) {
|
||||
case TUSB_REQ_TYPE_STANDARD:
|
||||
switch ( request->bRequest )
|
||||
{
|
||||
case TUSB_REQ_GET_INTERFACE:
|
||||
{
|
||||
uint8_t const req_itfnum = (uint8_t) request->wIndex;
|
||||
switch (request->bRequest) {
|
||||
case TUSB_REQ_GET_INTERFACE: {
|
||||
uint8_t const req_itfnum = (uint8_t)request->wIndex;
|
||||
TU_VERIFY(_netd_itf.itf_num+1 == req_itfnum);
|
||||
|
||||
tud_control_xfer(rhport, request, &_netd_itf.itf_data_alt, 1);
|
||||
}
|
||||
break;
|
||||
|
||||
case TUSB_REQ_SET_INTERFACE:
|
||||
{
|
||||
uint8_t const req_itfnum = (uint8_t) request->wIndex;
|
||||
uint8_t const req_alt = (uint8_t) request->wValue;
|
||||
case TUSB_REQ_SET_INTERFACE: {
|
||||
uint8_t const req_itfnum = (uint8_t)request->wIndex;
|
||||
uint8_t const req_alt = (uint8_t)request->wValue;
|
||||
|
||||
// Only valid for Data Interface with Alternate is either 0 or 1
|
||||
TU_VERIFY(_netd_itf.itf_num+1 == req_itfnum && req_alt < 2);
|
||||
@@ -273,14 +248,14 @@ bool netd_control_xfer_cb (uint8_t rhport, uint8_t stage, tusb_control_request_t
|
||||
|
||||
_netd_itf.itf_data_alt = req_alt;
|
||||
|
||||
if ( _netd_itf.itf_data_alt )
|
||||
{
|
||||
if (_netd_itf.itf_data_alt) {
|
||||
// TODO since we don't actually close endpoint
|
||||
// hack here to not re-open it
|
||||
if ( _netd_itf.ep_in == 0 && _netd_itf.ep_out == 0 )
|
||||
{
|
||||
if (_netd_itf.ep_in == 0 && _netd_itf.ep_out == 0) {
|
||||
TU_ASSERT(_netd_itf.ecm_desc_epdata);
|
||||
TU_ASSERT( usbd_open_edpt_pair(rhport, _netd_itf.ecm_desc_epdata, 2, TUSB_XFER_BULK, &_netd_itf.ep_out, &_netd_itf.ep_in) );
|
||||
TU_ASSERT(
|
||||
usbd_open_edpt_pair(rhport, _netd_itf.ecm_desc_epdata, 2, TUSB_XFER_BULK, &_netd_itf.ep_out, &
|
||||
_netd_itf.ep_in));
|
||||
|
||||
// TODO should be merge with RNDIS's after endpoint opened
|
||||
// Also should have opposite callback for application to disable network !!
|
||||
@@ -288,8 +263,7 @@ bool netd_control_xfer_cb (uint8_t rhport, uint8_t stage, tusb_control_request_t
|
||||
can_xmit = true; // we are ready to transmit a packet
|
||||
tud_network_recv_renew(); // prepare for incoming packets
|
||||
}
|
||||
}else
|
||||
{
|
||||
} else {
|
||||
// TODO close the endpoint pair
|
||||
// For now pretend that we did, this should have no harm since host won't try to
|
||||
// communicate with the endpoints again
|
||||
@@ -303,50 +277,39 @@ bool netd_control_xfer_cb (uint8_t rhport, uint8_t stage, tusb_control_request_t
|
||||
// unsupported request
|
||||
default: return false;
|
||||
}
|
||||
break;
|
||||
break;
|
||||
|
||||
case TUSB_REQ_TYPE_CLASS:
|
||||
TU_VERIFY (_netd_itf.itf_num == request->wIndex);
|
||||
TU_VERIFY(_netd_itf.itf_num == request->wIndex);
|
||||
|
||||
if (_netd_itf.ecm_mode)
|
||||
{
|
||||
if (_netd_itf.ecm_mode) {
|
||||
/* the only required CDC-ECM Management Element Request is SetEthernetPacketFilter */
|
||||
if (0x43 /* SET_ETHERNET_PACKET_FILTER */ == request->bRequest)
|
||||
{
|
||||
if (0x43 /* SET_ETHERNET_PACKET_FILTER */ == request->bRequest) {
|
||||
tud_control_xfer(rhport, request, NULL, 0);
|
||||
ecm_report(true);
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
if (request->bmRequestType_bit.direction == TUSB_DIR_IN)
|
||||
{
|
||||
rndis_generic_msg_t *rndis_msg = (rndis_generic_msg_t *) ((void*) notify.rndis_buf);
|
||||
} else {
|
||||
if (request->bmRequestType_bit.direction == TUSB_DIR_IN) {
|
||||
rndis_generic_msg_t* rndis_msg = (rndis_generic_msg_t*)((void*)_netd_epbuf.ctrl);
|
||||
uint32_t msglen = tu_le32toh(rndis_msg->MessageLength);
|
||||
TU_ASSERT(msglen <= sizeof(notify.rndis_buf));
|
||||
tud_control_xfer(rhport, request, notify.rndis_buf, (uint16_t) msglen);
|
||||
}
|
||||
else
|
||||
{
|
||||
tud_control_xfer(rhport, request, notify.rndis_buf, (uint16_t) sizeof(notify.rndis_buf));
|
||||
TU_ASSERT(msglen <= NETD_CONTROL_SIZE);
|
||||
tud_control_xfer(rhport, request, _netd_epbuf.ctrl, (uint16_t)msglen);
|
||||
} else {
|
||||
tud_control_xfer(rhport, request, _netd_epbuf.ctrl, NETD_CONTROL_SIZE);
|
||||
}
|
||||
}
|
||||
break;
|
||||
break;
|
||||
|
||||
// unsupported request
|
||||
default: return false;
|
||||
}
|
||||
}
|
||||
else if ( stage == CONTROL_STAGE_DATA )
|
||||
{
|
||||
} else if (stage == CONTROL_STAGE_DATA) {
|
||||
// Handle RNDIS class control OUT only
|
||||
if (request->bmRequestType_bit.type == TUSB_REQ_TYPE_CLASS &&
|
||||
request->bmRequestType_bit.direction == TUSB_DIR_OUT &&
|
||||
_netd_itf.itf_num == request->wIndex)
|
||||
{
|
||||
if ( !_netd_itf.ecm_mode )
|
||||
{
|
||||
rndis_class_set_handler(notify.rndis_buf, request->wLength);
|
||||
request->bmRequestType_bit.direction == TUSB_DIR_OUT &&
|
||||
_netd_itf.itf_num == request->wIndex) {
|
||||
if (!_netd_itf.ecm_mode) {
|
||||
rndis_class_set_handler(_netd_epbuf.ctrl, request->wLength);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -354,92 +317,77 @@ bool netd_control_xfer_cb (uint8_t rhport, uint8_t stage, tusb_control_request_t
|
||||
return true;
|
||||
}
|
||||
|
||||
static void handle_incoming_packet(uint32_t len)
|
||||
{
|
||||
uint8_t *pnt = received;
|
||||
static void handle_incoming_packet(uint32_t len) {
|
||||
uint8_t* pnt = _netd_epbuf.rx;
|
||||
uint32_t size = 0;
|
||||
|
||||
if (_netd_itf.ecm_mode)
|
||||
{
|
||||
if (_netd_itf.ecm_mode) {
|
||||
size = len;
|
||||
}
|
||||
else
|
||||
{
|
||||
rndis_data_packet_t *r = (rndis_data_packet_t *) ((void*) pnt);
|
||||
if (len >= sizeof(rndis_data_packet_t))
|
||||
if ( (r->MessageType == REMOTE_NDIS_PACKET_MSG) && (r->MessageLength <= len))
|
||||
if ( (r->DataOffset + offsetof(rndis_data_packet_t, DataOffset) + r->DataLength) <= len)
|
||||
{
|
||||
pnt = &received[r->DataOffset + offsetof(rndis_data_packet_t, DataOffset)];
|
||||
} else {
|
||||
rndis_data_packet_t* r = (rndis_data_packet_t*)((void*)pnt);
|
||||
if (len >= sizeof(rndis_data_packet_t)) {
|
||||
if ((r->MessageType == REMOTE_NDIS_PACKET_MSG) && (r->MessageLength <= len)) {
|
||||
if ((r->DataOffset + offsetof(rndis_data_packet_t, DataOffset) + r->DataLength) <= len) {
|
||||
pnt = &_netd_epbuf.rx[r->DataOffset + offsetof(rndis_data_packet_t, DataOffset)];
|
||||
size = r->DataLength;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (!tud_network_recv_cb(pnt, (uint16_t) size))
|
||||
{
|
||||
if (!tud_network_recv_cb(pnt, (uint16_t)size)) {
|
||||
/* if a buffer was never handled by user code, we must renew on the user's behalf */
|
||||
tud_network_recv_renew();
|
||||
}
|
||||
}
|
||||
|
||||
bool netd_xfer_cb(uint8_t rhport, uint8_t ep_addr, xfer_result_t result, uint32_t xferred_bytes)
|
||||
{
|
||||
(void) rhport;
|
||||
(void) result;
|
||||
bool netd_xfer_cb(uint8_t rhport, uint8_t ep_addr, xfer_result_t result, uint32_t xferred_bytes) {
|
||||
(void)rhport;
|
||||
(void)result;
|
||||
|
||||
/* new packet received */
|
||||
if ( ep_addr == _netd_itf.ep_out )
|
||||
{
|
||||
if (ep_addr == _netd_itf.ep_out) {
|
||||
handle_incoming_packet(xferred_bytes);
|
||||
}
|
||||
|
||||
/* data transmission finished */
|
||||
if ( ep_addr == _netd_itf.ep_in )
|
||||
{
|
||||
if (ep_addr == _netd_itf.ep_in) {
|
||||
/* TinyUSB requires the class driver to implement ZLP (since ZLP usage is class-specific) */
|
||||
|
||||
if ( xferred_bytes && (0 == (xferred_bytes % CFG_TUD_NET_ENDPOINT_SIZE)) )
|
||||
{
|
||||
if (xferred_bytes && (0 == (xferred_bytes % CFG_TUD_NET_ENDPOINT_SIZE))) {
|
||||
do_in_xfer(NULL, 0); /* a ZLP is needed */
|
||||
}
|
||||
else
|
||||
{
|
||||
} else {
|
||||
/* we're finally finished */
|
||||
can_xmit = true;
|
||||
}
|
||||
}
|
||||
|
||||
if ( _netd_itf.ecm_mode && (ep_addr == _netd_itf.ep_notif) )
|
||||
{
|
||||
if (sizeof(notify.ecm_buf.header) == xferred_bytes) ecm_report(false);
|
||||
if (_netd_itf.ecm_mode && (ep_addr == _netd_itf.ep_notif)) {
|
||||
if (sizeof(tusb_control_request_t) == xferred_bytes) {
|
||||
ecm_report(false);
|
||||
}
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
bool tud_network_can_xmit(uint16_t size)
|
||||
{
|
||||
bool tud_network_can_xmit(uint16_t size) {
|
||||
(void)size;
|
||||
|
||||
return can_xmit;
|
||||
}
|
||||
|
||||
void tud_network_xmit(void *ref, uint16_t arg)
|
||||
{
|
||||
uint8_t *data;
|
||||
uint16_t len;
|
||||
|
||||
if (!can_xmit)
|
||||
void tud_network_xmit(void *ref, uint16_t arg) {
|
||||
if (!can_xmit) {
|
||||
return;
|
||||
}
|
||||
|
||||
len = (_netd_itf.ecm_mode) ? 0 : CFG_TUD_NET_PACKET_PREFIX_LEN;
|
||||
data = transmitted + len;
|
||||
uint16_t len = (_netd_itf.ecm_mode) ? 0 : CFG_TUD_NET_PACKET_PREFIX_LEN;
|
||||
uint8_t* data = _netd_epbuf.tx + len;
|
||||
|
||||
len += tud_network_xmit_cb(data, ref, arg);
|
||||
|
||||
if (!_netd_itf.ecm_mode)
|
||||
{
|
||||
rndis_data_packet_t *hdr = (rndis_data_packet_t *) ((void*) transmitted);
|
||||
if (!_netd_itf.ecm_mode) {
|
||||
rndis_data_packet_t *hdr = (rndis_data_packet_t *) ((void*) _netd_epbuf.tx);
|
||||
memset(hdr, 0, sizeof(rndis_data_packet_t));
|
||||
hdr->MessageType = REMOTE_NDIS_PACKET_MSG;
|
||||
hdr->MessageLength = len;
|
||||
@@ -447,7 +395,7 @@ void tud_network_xmit(void *ref, uint16_t arg)
|
||||
hdr->DataLength = len - sizeof(rndis_data_packet_t);
|
||||
}
|
||||
|
||||
do_in_xfer(transmitted, len);
|
||||
do_in_xfer(_netd_epbuf.tx, len);
|
||||
}
|
||||
|
||||
#endif
|
||||
|
||||
@@ -155,9 +155,10 @@ typedef union TU_ATTR_PACKED {
|
||||
uint8_t data[CFG_TUD_NCM_OUT_NTB_MAX_SIZE];
|
||||
} recv_ntb_t;
|
||||
|
||||
struct ncm_notify_t {
|
||||
typedef struct {
|
||||
tusb_control_request_t header;
|
||||
uint32_t downlink, uplink;
|
||||
};
|
||||
uint32_t downlink;
|
||||
uint32_t uplink;
|
||||
} ncm_notify_t;
|
||||
|
||||
#endif
|
||||
|
||||
@@ -89,7 +89,6 @@ typedef struct {
|
||||
uint8_t rhport; // storage of \a rhport because some callbacks are done without it
|
||||
|
||||
// recv handling
|
||||
CFG_TUSB_MEM_ALIGN recv_ntb_t recv_ntb[RECV_NTB_N]; // actual recv NTBs
|
||||
recv_ntb_t *recv_free_ntb[RECV_NTB_N]; // free list of recv NTBs
|
||||
recv_ntb_t *recv_ready_ntb[RECV_NTB_N]; // NTBs waiting for transmission to glue logic
|
||||
recv_ntb_t *recv_tinyusb_ntb; // buffer for the running transfer TinyUSB -> driver
|
||||
@@ -97,7 +96,6 @@ typedef struct {
|
||||
uint16_t recv_glue_ntb_datagram_ndx; // index into \a recv_glue_ntb_datagram
|
||||
|
||||
// xmit handling
|
||||
CFG_TUSB_MEM_ALIGN xmit_ntb_t xmit_ntb[XMIT_NTB_N]; // actual xmit NTBs
|
||||
xmit_ntb_t *xmit_free_ntb[XMIT_NTB_N]; // free list of xmit NTBs
|
||||
xmit_ntb_t *xmit_ready_ntb[XMIT_NTB_N]; // NTBs waiting for transmission to TinyUSB
|
||||
xmit_ntb_t *xmit_tinyusb_ntb; // buffer for the running transfer driver -> TinyUSB
|
||||
@@ -110,11 +108,28 @@ typedef struct {
|
||||
NOTIFICATION_SPEED,
|
||||
NOTIFICATION_CONNECTED,
|
||||
NOTIFICATION_DONE
|
||||
} notification_xmit_state; // state of notification transmission
|
||||
bool notification_xmit_is_running; // notification is currently transmitted
|
||||
} notification_xmit_state; // state of notification transmission
|
||||
bool notification_xmit_is_running; // notification is currently transmitted
|
||||
|
||||
// misc
|
||||
bool tud_network_recv_renew_active; // tud_network_recv_renew() is active (avoid recursive invocations)
|
||||
bool tud_network_recv_renew_process_again; // tud_network_recv_renew() should process again
|
||||
} ncm_interface_t;
|
||||
|
||||
CFG_TUD_MEM_SECTION CFG_TUD_MEM_ALIGN tu_static ncm_interface_t ncm_interface;
|
||||
typedef struct {
|
||||
struct {
|
||||
TUD_EPBUF_TYPE_DEF(recv_ntb_t, ntb);
|
||||
} recv[RECV_NTB_N];
|
||||
|
||||
struct {
|
||||
TUD_EPBUF_TYPE_DEF(xmit_ntb_t, ntb);
|
||||
} xmit[XMIT_NTB_N];
|
||||
|
||||
TUD_EPBUF_TYPE_DEF(ncm_notify_t, epnotif);
|
||||
} ncm_epbuf_t;
|
||||
|
||||
static ncm_interface_t ncm_interface;
|
||||
CFG_TUD_MEM_SECTION static ncm_epbuf_t ncm_epbuf;
|
||||
|
||||
/**
|
||||
* This is the NTB parameter structure
|
||||
@@ -122,7 +137,7 @@ CFG_TUD_MEM_SECTION CFG_TUD_MEM_ALIGN tu_static ncm_interface_t ncm_interface;
|
||||
* \attention
|
||||
* We are lucky, that byte order is correct
|
||||
*/
|
||||
CFG_TUD_MEM_SECTION CFG_TUD_MEM_ALIGN tu_static const ntb_parameters_t ntb_parameters = {
|
||||
TU_ATTR_ALIGNED(4) static const ntb_parameters_t ntb_parameters = {
|
||||
.wLength = sizeof(ntb_parameters_t),
|
||||
.bmNtbFormatsSupported = 0x01,// 16-bit NTB supported
|
||||
.dwNtbInMaxSize = CFG_TUD_NCM_IN_NTB_MAX_SIZE,
|
||||
@@ -152,30 +167,6 @@ CFG_TUD_MEM_SECTION CFG_TUD_MEM_ALIGN tu_static const ntb_parameters_t ntb_param
|
||||
//
|
||||
// everything about notifications
|
||||
//
|
||||
tu_static struct ncm_notify_t ncm_notify_connected = {
|
||||
.header = {
|
||||
.bmRequestType_bit = {
|
||||
.recipient = TUSB_REQ_RCPT_INTERFACE,
|
||||
.type = TUSB_REQ_TYPE_CLASS,
|
||||
.direction = TUSB_DIR_IN},
|
||||
.bRequest = CDC_NOTIF_NETWORK_CONNECTION,
|
||||
.wValue = 1 /* Connected */,
|
||||
.wLength = 0,
|
||||
},
|
||||
};
|
||||
|
||||
tu_static struct ncm_notify_t ncm_notify_speed_change = {
|
||||
.header = {
|
||||
.bmRequestType_bit = {
|
||||
.recipient = TUSB_REQ_RCPT_INTERFACE,
|
||||
.type = TUSB_REQ_TYPE_CLASS,
|
||||
.direction = TUSB_DIR_IN},
|
||||
.bRequest = CDC_NOTIF_CONNECTION_SPEED_CHANGE,
|
||||
.wLength = 8,
|
||||
},
|
||||
.downlink = TUD_OPT_HIGH_SPEED ? 480000000 : 12000000,
|
||||
.uplink = TUD_OPT_HIGH_SPEED ? 480000000 : 12000000,
|
||||
};
|
||||
|
||||
/**
|
||||
* Transmit next notification to the host (if appropriate).
|
||||
@@ -190,14 +181,53 @@ static void notification_xmit(uint8_t rhport, bool force_next) {
|
||||
|
||||
if (ncm_interface.notification_xmit_state == NOTIFICATION_SPEED) {
|
||||
TU_LOG_DRV(" NOTIFICATION_SPEED\n");
|
||||
ncm_notify_speed_change.header.wIndex = ncm_interface.itf_num;
|
||||
usbd_edpt_xfer(rhport, ncm_interface.ep_notif, (uint8_t *) &ncm_notify_speed_change, sizeof(ncm_notify_speed_change));
|
||||
ncm_notify_t notify_speed_change = {
|
||||
.header = {
|
||||
.bmRequestType_bit = {
|
||||
.recipient = TUSB_REQ_RCPT_INTERFACE,
|
||||
.type = TUSB_REQ_TYPE_CLASS,
|
||||
.direction = TUSB_DIR_IN
|
||||
},
|
||||
.bRequest = CDC_NOTIF_CONNECTION_SPEED_CHANGE,
|
||||
.wValue = 0,
|
||||
.wIndex = ncm_interface.itf_num,
|
||||
.wLength = 8
|
||||
}
|
||||
};
|
||||
if (tud_speed_get() == TUSB_SPEED_HIGH) {
|
||||
notify_speed_change.downlink = 480000000;
|
||||
notify_speed_change.uplink = 480000000;
|
||||
} else {
|
||||
notify_speed_change.downlink = 12000000;
|
||||
notify_speed_change.uplink = 12000000;
|
||||
}
|
||||
|
||||
uint16_t notif_len = sizeof(notify_speed_change.header) + notify_speed_change.header.wLength;
|
||||
ncm_epbuf.epnotif = notify_speed_change;
|
||||
usbd_edpt_xfer(rhport, ncm_interface.ep_notif, (uint8_t*) &ncm_epbuf.epnotif, notif_len);
|
||||
|
||||
ncm_interface.notification_xmit_state = NOTIFICATION_CONNECTED;
|
||||
ncm_interface.notification_xmit_is_running = true;
|
||||
} else if (ncm_interface.notification_xmit_state == NOTIFICATION_CONNECTED) {
|
||||
TU_LOG_DRV(" NOTIFICATION_CONNECTED\n");
|
||||
ncm_notify_connected.header.wIndex = ncm_interface.itf_num;
|
||||
usbd_edpt_xfer(rhport, ncm_interface.ep_notif, (uint8_t *) &ncm_notify_connected, sizeof(ncm_notify_connected));
|
||||
ncm_notify_t notify_connected = {
|
||||
.header = {
|
||||
.bmRequestType_bit = {
|
||||
.recipient = TUSB_REQ_RCPT_INTERFACE,
|
||||
.type = TUSB_REQ_TYPE_CLASS,
|
||||
.direction = TUSB_DIR_IN
|
||||
},
|
||||
.bRequest = CDC_NOTIF_NETWORK_CONNECTION,
|
||||
.wValue = 1 /* Connected */,
|
||||
.wIndex = ncm_interface.itf_num,
|
||||
.wLength = 0,
|
||||
},
|
||||
};
|
||||
|
||||
uint16_t notif_len = sizeof(notify_connected.header) + notify_connected.header.wLength;
|
||||
ncm_epbuf.epnotif = notify_connected;
|
||||
usbd_edpt_xfer(rhport, ncm_interface.ep_notif, (uint8_t *) &ncm_epbuf.epnotif, notif_len);
|
||||
|
||||
ncm_interface.notification_xmit_state = NOTIFICATION_DONE;
|
||||
ncm_interface.notification_xmit_is_running = true;
|
||||
} else {
|
||||
@@ -689,18 +719,36 @@ void tud_network_xmit(void *ref, uint16_t arg) {
|
||||
|
||||
/**
|
||||
* Keep the receive logic busy and transfer pending packets to the glue logic.
|
||||
* Avoid recursive calls due to wrong expectations of the net glue logic,
|
||||
* see https://github.com/hathach/tinyusb/issues/2711
|
||||
*/
|
||||
void tud_network_recv_renew(void) {
|
||||
TU_LOG_DRV("tud_network_recv_renew()\n");
|
||||
|
||||
recv_transfer_datagram_to_glue_logic();
|
||||
ncm_interface.tud_network_recv_renew_process_again = true;
|
||||
|
||||
if (ncm_interface.tud_network_recv_renew_active) {
|
||||
TU_LOG_DRV("Re-entrant into tud_network_recv_renew, will process later\n");
|
||||
return;
|
||||
}
|
||||
|
||||
while (ncm_interface.tud_network_recv_renew_process_again) {
|
||||
ncm_interface.tud_network_recv_renew_process_again = false;
|
||||
|
||||
// If the current function is called within recv_transfer_datagram_to_glue_logic,
|
||||
// tud_network_recv_renew_process_again will become true, and the loop will run again
|
||||
// Otherwise the loop will not run again
|
||||
ncm_interface.tud_network_recv_renew_active = true;
|
||||
recv_transfer_datagram_to_glue_logic();
|
||||
ncm_interface.tud_network_recv_renew_active = false;
|
||||
}
|
||||
recv_try_to_start_new_reception(ncm_interface.rhport);
|
||||
} // tud_network_recv_renew
|
||||
|
||||
/**
|
||||
* Same as tud_network_recv_renew() but knows \a rhport
|
||||
*/
|
||||
void tud_network_recv_renew_r(uint8_t rhport) {
|
||||
static void tud_network_recv_renew_r(uint8_t rhport) {
|
||||
TU_LOG_DRV("tud_network_recv_renew_r(%d)\n", rhport);
|
||||
|
||||
ncm_interface.rhport = rhport;
|
||||
@@ -721,10 +769,10 @@ void netd_init(void) {
|
||||
memset(&ncm_interface, 0, sizeof(ncm_interface));
|
||||
|
||||
for (int i = 0; i < XMIT_NTB_N; ++i) {
|
||||
ncm_interface.xmit_free_ntb[i] = ncm_interface.xmit_ntb + i;
|
||||
ncm_interface.xmit_free_ntb[i] = &ncm_epbuf.xmit[i].ntb;
|
||||
}
|
||||
for (int i = 0; i < RECV_NTB_N; ++i) {
|
||||
ncm_interface.recv_free_ntb[i] = ncm_interface.recv_ntb + i;
|
||||
ncm_interface.recv_free_ntb[i] = &ncm_epbuf.recv[i].ntb;
|
||||
}
|
||||
} // netd_init
|
||||
|
||||
|
||||
@@ -131,14 +131,6 @@ typedef struct
|
||||
uint8_t ep_int_in;
|
||||
uint32_t ep_bulk_in_wMaxPacketSize;
|
||||
uint32_t ep_bulk_out_wMaxPacketSize;
|
||||
// IN buffer is only used for first packet, not the remainder
|
||||
// in order to deal with prepending header
|
||||
CFG_TUSB_MEM_ALIGN uint8_t ep_bulk_in_buf[USBTMCD_BUFFER_SIZE];
|
||||
// OUT buffer receives one packet at a time
|
||||
CFG_TUSB_MEM_ALIGN uint8_t ep_bulk_out_buf[USBTMCD_BUFFER_SIZE];
|
||||
// Buffer int msg to ensure alignment and placement correctness
|
||||
CFG_TUSB_MEM_ALIGN uint8_t ep_int_in_buf[CFG_TUD_USBTMC_INT_EP_SIZE];
|
||||
|
||||
uint32_t transfer_size_remaining; // also used for requested length for bulk IN.
|
||||
uint32_t transfer_size_sent; // To keep track of data bytes that have been queued in FIFO (not header bytes)
|
||||
|
||||
@@ -150,11 +142,23 @@ typedef struct
|
||||
usbtmc_capabilities_specific_t const * capabilities;
|
||||
} usbtmc_interface_state_t;
|
||||
|
||||
CFG_TUD_MEM_SECTION tu_static usbtmc_interface_state_t usbtmc_state =
|
||||
{
|
||||
.itf_id = 0xFF,
|
||||
typedef struct {
|
||||
// IN buffer is only used for first packet, not the remainder in order to deal with prepending header
|
||||
TUD_EPBUF_DEF(epin, USBTMCD_BUFFER_SIZE);
|
||||
|
||||
// OUT buffer receives one packet at a time
|
||||
TUD_EPBUF_DEF(epout, USBTMCD_BUFFER_SIZE);
|
||||
|
||||
// Buffer int msg
|
||||
TUD_EPBUF_DEF(epnotif, CFG_TUD_USBTMC_INT_EP_SIZE);
|
||||
} usbtmc_epbuf_t;
|
||||
|
||||
static usbtmc_interface_state_t usbtmc_state = {
|
||||
.itf_id = 0xFF,
|
||||
};
|
||||
|
||||
CFG_TUD_MEM_SECTION static usbtmc_epbuf_t usbtmc_epbuf;
|
||||
|
||||
// We need all headers to fit in a single packet in this implementation, 32 bytes will fit all standard USBTMC headers
|
||||
TU_VERIFY_STATIC(USBTMCD_BUFFER_SIZE >= 32u,"USBTMC dev buffer size too small");
|
||||
|
||||
@@ -176,7 +180,7 @@ osal_mutex_t usbtmcLock;
|
||||
#define criticalEnter() do { (void) osal_mutex_lock(usbtmcLock,OSAL_TIMEOUT_WAIT_FOREVER); } while (0)
|
||||
#define criticalLeave() do { (void) osal_mutex_unlock(usbtmcLock); } while (0)
|
||||
|
||||
bool atomicChangeState(usbtmcd_state_enum expectedState, usbtmcd_state_enum newState)
|
||||
static bool atomicChangeState(usbtmcd_state_enum expectedState, usbtmcd_state_enum newState)
|
||||
{
|
||||
bool ret = true;
|
||||
criticalEnter();
|
||||
@@ -205,7 +209,7 @@ bool tud_usbtmc_transmit_dev_msg_data(
|
||||
bool endOfMessage,
|
||||
bool usingTermChar)
|
||||
{
|
||||
const unsigned int txBufLen = sizeof(usbtmc_state.ep_bulk_in_buf);
|
||||
const unsigned int txBufLen = USBTMCD_BUFFER_SIZE;
|
||||
|
||||
#ifndef NDEBUG
|
||||
TU_ASSERT(len > 0u);
|
||||
@@ -220,7 +224,7 @@ bool tud_usbtmc_transmit_dev_msg_data(
|
||||
#endif
|
||||
|
||||
TU_VERIFY(usbtmc_state.state == STATE_TX_REQUESTED);
|
||||
usbtmc_msg_dev_dep_msg_in_header_t *hdr = (usbtmc_msg_dev_dep_msg_in_header_t*)usbtmc_state.ep_bulk_in_buf;
|
||||
usbtmc_msg_dev_dep_msg_in_header_t *hdr = (usbtmc_msg_dev_dep_msg_in_header_t*)usbtmc_epbuf.epin;
|
||||
tu_varclr(hdr);
|
||||
hdr->header.MsgID = USBTMC_MSGID_DEV_DEP_MSG_IN;
|
||||
hdr->header.bTag = usbtmc_state.lastBulkInTag;
|
||||
@@ -235,7 +239,7 @@ bool tud_usbtmc_transmit_dev_msg_data(
|
||||
len : (txBufLen - headerLen);
|
||||
const size_t packetLen = headerLen + dataLen;
|
||||
|
||||
memcpy((uint8_t*)(usbtmc_state.ep_bulk_in_buf) + headerLen, data, dataLen);
|
||||
memcpy((uint8_t*)(usbtmc_epbuf.epin) + headerLen, data, dataLen);
|
||||
usbtmc_state.transfer_size_remaining = len - dataLen;
|
||||
usbtmc_state.transfer_size_sent = dataLen;
|
||||
usbtmc_state.devInBuffer = (uint8_t const*) data + (dataLen);
|
||||
@@ -243,7 +247,7 @@ bool tud_usbtmc_transmit_dev_msg_data(
|
||||
bool stateChanged =
|
||||
atomicChangeState(STATE_TX_REQUESTED, (packetLen >= txBufLen) ? STATE_TX_INITIATED : STATE_TX_SHORTED);
|
||||
TU_VERIFY(stateChanged);
|
||||
TU_VERIFY(usbd_edpt_xfer(usbtmc_state.rhport, usbtmc_state.ep_bulk_in, usbtmc_state.ep_bulk_in_buf, (uint16_t)packetLen));
|
||||
TU_VERIFY(usbd_edpt_xfer(usbtmc_state.rhport, usbtmc_state.ep_bulk_in, usbtmc_epbuf.epin, (uint16_t)packetLen));
|
||||
return true;
|
||||
}
|
||||
|
||||
@@ -255,8 +259,8 @@ bool tud_usbtmc_transmit_notification_data(const void * data, size_t len)
|
||||
#endif
|
||||
TU_VERIFY(usbd_edpt_busy(usbtmc_state.rhport, usbtmc_state.ep_int_in));
|
||||
|
||||
TU_VERIFY(tu_memcpy_s(usbtmc_state.ep_int_in_buf, sizeof(usbtmc_state.ep_int_in_buf), data, len) == 0);
|
||||
TU_VERIFY(usbd_edpt_xfer(usbtmc_state.rhport, usbtmc_state.ep_int_in, usbtmc_state.ep_int_in_buf, (uint16_t)len));
|
||||
TU_VERIFY(tu_memcpy_s(usbtmc_epbuf.epnotif, CFG_TUD_USBTMC_INT_EP_SIZE, data, len) == 0);
|
||||
TU_VERIFY(usbd_edpt_xfer(usbtmc_state.rhport, usbtmc_state.ep_int_in, usbtmc_epbuf.epnotif, (uint16_t)len));
|
||||
return true;
|
||||
}
|
||||
|
||||
@@ -396,7 +400,7 @@ bool tud_usbtmc_start_bus_read(void)
|
||||
default:
|
||||
return false;
|
||||
}
|
||||
TU_VERIFY(usbd_edpt_xfer(usbtmc_state.rhport, usbtmc_state.ep_bulk_out, usbtmc_state.ep_bulk_out_buf, (uint16_t)usbtmc_state.ep_bulk_out_wMaxPacketSize));
|
||||
TU_VERIFY(usbd_edpt_xfer(usbtmc_state.rhport, usbtmc_state.ep_bulk_out, usbtmc_epbuf.epout, (uint16_t)usbtmc_state.ep_bulk_out_wMaxPacketSize));
|
||||
return true;
|
||||
}
|
||||
|
||||
@@ -501,7 +505,7 @@ bool usbtmcd_xfer_cb(uint8_t rhport, uint8_t ep_addr, xfer_result_t result, uint
|
||||
case STATE_IDLE:
|
||||
{
|
||||
TU_VERIFY(xferred_bytes >= sizeof(usbtmc_msg_generic_t));
|
||||
msg = (usbtmc_msg_generic_t*)(usbtmc_state.ep_bulk_out_buf);
|
||||
msg = (usbtmc_msg_generic_t*)(usbtmc_epbuf.epout);
|
||||
uint8_t invInvTag = (uint8_t)~(msg->header.bTagInverse);
|
||||
TU_VERIFY(msg->header.bTag == invInvTag);
|
||||
TU_VERIFY(msg->header.bTag != 0x00);
|
||||
@@ -536,7 +540,7 @@ bool usbtmcd_xfer_cb(uint8_t rhport, uint8_t ep_addr, xfer_result_t result, uint
|
||||
return true;
|
||||
}
|
||||
case STATE_RCV:
|
||||
if(!handle_devMsgOut(rhport, usbtmc_state.ep_bulk_out_buf, xferred_bytes, xferred_bytes))
|
||||
if(!handle_devMsgOut(rhport, usbtmc_epbuf.epout, xferred_bytes, xferred_bytes))
|
||||
{
|
||||
usbd_edpt_stall(rhport, usbtmc_state.ep_bulk_out);
|
||||
return false;
|
||||
@@ -565,24 +569,23 @@ bool usbtmcd_xfer_cb(uint8_t rhport, uint8_t ep_addr, xfer_result_t result, uint
|
||||
break;
|
||||
|
||||
case STATE_TX_INITIATED:
|
||||
if(usbtmc_state.transfer_size_remaining >= sizeof(usbtmc_state.ep_bulk_in_buf))
|
||||
if(usbtmc_state.transfer_size_remaining >= USBTMCD_BUFFER_SIZE)
|
||||
{
|
||||
// Copy buffer to ensure alignment correctness
|
||||
memcpy(usbtmc_state.ep_bulk_in_buf, usbtmc_state.devInBuffer, sizeof(usbtmc_state.ep_bulk_in_buf));
|
||||
TU_VERIFY( usbd_edpt_xfer(rhport, usbtmc_state.ep_bulk_in,
|
||||
usbtmc_state.ep_bulk_in_buf, sizeof(usbtmc_state.ep_bulk_in_buf)));
|
||||
usbtmc_state.devInBuffer += sizeof(usbtmc_state.ep_bulk_in_buf);
|
||||
usbtmc_state.transfer_size_remaining -= sizeof(usbtmc_state.ep_bulk_in_buf);
|
||||
usbtmc_state.transfer_size_sent += sizeof(usbtmc_state.ep_bulk_in_buf);
|
||||
memcpy(usbtmc_epbuf.epin, usbtmc_state.devInBuffer, USBTMCD_BUFFER_SIZE);
|
||||
TU_VERIFY(usbd_edpt_xfer(rhport, usbtmc_state.ep_bulk_in, usbtmc_epbuf.epin, USBTMCD_BUFFER_SIZE));
|
||||
usbtmc_state.devInBuffer += USBTMCD_BUFFER_SIZE;
|
||||
usbtmc_state.transfer_size_remaining -= USBTMCD_BUFFER_SIZE;
|
||||
usbtmc_state.transfer_size_sent += USBTMCD_BUFFER_SIZE;
|
||||
}
|
||||
else // last packet
|
||||
{
|
||||
size_t packetLen = usbtmc_state.transfer_size_remaining;
|
||||
memcpy(usbtmc_state.ep_bulk_in_buf, usbtmc_state.devInBuffer, usbtmc_state.transfer_size_remaining);
|
||||
memcpy(usbtmc_epbuf.epin, usbtmc_state.devInBuffer, usbtmc_state.transfer_size_remaining);
|
||||
usbtmc_state.transfer_size_sent += sizeof(usbtmc_state.transfer_size_remaining);
|
||||
usbtmc_state.transfer_size_remaining = 0;
|
||||
usbtmc_state.devInBuffer = NULL;
|
||||
TU_VERIFY( usbd_edpt_xfer(rhport, usbtmc_state.ep_bulk_in, usbtmc_state.ep_bulk_in_buf, (uint16_t)packetLen) );
|
||||
TU_VERIFY( usbd_edpt_xfer(rhport, usbtmc_state.ep_bulk_in, usbtmc_epbuf.epin, (uint16_t)packetLen) );
|
||||
if(((packetLen % usbtmc_state.ep_bulk_in_wMaxPacketSize) != 0) || (packetLen == 0 ))
|
||||
{
|
||||
usbtmc_state.state = STATE_TX_SHORTED;
|
||||
@@ -592,7 +595,7 @@ bool usbtmcd_xfer_cb(uint8_t rhport, uint8_t ep_addr, xfer_result_t result, uint
|
||||
|
||||
case STATE_ABORTING_BULK_IN:
|
||||
// need to send short packet (ZLP?)
|
||||
TU_VERIFY( usbd_edpt_xfer(rhport, usbtmc_state.ep_bulk_in, usbtmc_state.ep_bulk_in_buf,(uint16_t)0u));
|
||||
TU_VERIFY( usbd_edpt_xfer(rhport, usbtmc_state.ep_bulk_in, usbtmc_epbuf.epin,(uint16_t)0u));
|
||||
usbtmc_state.state = STATE_ABORTING_BULK_IN_SHORTED;
|
||||
return true;
|
||||
|
||||
@@ -744,7 +747,7 @@ bool usbtmcd_control_xfer_cb(uint8_t rhport, uint8_t stage, tusb_control_request
|
||||
if(usbtmc_state.transfer_size_sent == 0)
|
||||
{
|
||||
// Send short packet, nothing is in the buffer yet
|
||||
TU_VERIFY( usbd_edpt_xfer(rhport, usbtmc_state.ep_bulk_in, usbtmc_state.ep_bulk_in_buf,(uint16_t)0u));
|
||||
TU_VERIFY( usbd_edpt_xfer(rhport, usbtmc_state.ep_bulk_in, usbtmc_epbuf.epin,(uint16_t)0u));
|
||||
usbtmc_state.state = STATE_ABORTING_BULK_IN_SHORTED;
|
||||
}
|
||||
TU_VERIFY(tud_usbtmc_initiate_abort_bulk_in_cb(&(rsp.USBTMC_status)));
|
||||
|
||||
64
src/class/vendor/vendor_device.c
vendored
64
src/class/vendor/vendor_device.c
vendored
@@ -40,16 +40,12 @@ typedef struct {
|
||||
uint8_t itf_num;
|
||||
|
||||
/*------------- From this point, data is not cleared by bus reset -------------*/
|
||||
// Endpoint Transfer buffer
|
||||
CFG_TUD_MEM_ALIGN uint8_t epout_buf[CFG_TUD_VENDOR_EPSIZE];
|
||||
CFG_TUD_MEM_ALIGN uint8_t epin_buf[CFG_TUD_VENDOR_EPSIZE];
|
||||
|
||||
struct {
|
||||
tu_edpt_stream_t stream;
|
||||
#if CFG_TUD_VENDOR_TX_BUFSIZE > 0
|
||||
uint8_t ff_buf[CFG_TUD_VENDOR_TX_BUFSIZE];
|
||||
#endif
|
||||
}tx;
|
||||
} tx;
|
||||
|
||||
struct {
|
||||
tu_edpt_stream_t stream;
|
||||
@@ -60,10 +56,17 @@ typedef struct {
|
||||
|
||||
} vendord_interface_t;
|
||||
|
||||
CFG_TUD_MEM_SECTION static vendord_interface_t _vendord_itf[CFG_TUD_VENDOR];
|
||||
|
||||
#define ITF_MEM_RESET_SIZE (offsetof(vendord_interface_t, itf_num) + sizeof(((vendord_interface_t *)0)->itf_num))
|
||||
|
||||
static vendord_interface_t _vendord_itf[CFG_TUD_VENDOR];
|
||||
|
||||
typedef struct {
|
||||
TUD_EPBUF_DEF(epout, CFG_TUD_VENDOR_EPSIZE);
|
||||
TUD_EPBUF_DEF(epin, CFG_TUD_VENDOR_EPSIZE);
|
||||
} vendord_epbuf_t;
|
||||
|
||||
CFG_TUD_MEM_SECTION static vendord_epbuf_t _vendord_epbuf[CFG_TUD_VENDOR];
|
||||
|
||||
//--------------------------------------------------------------------
|
||||
// Application API
|
||||
//--------------------------------------------------------------------
|
||||
@@ -94,7 +97,7 @@ bool tud_vendor_n_peek(uint8_t itf, uint8_t* u8) {
|
||||
uint32_t tud_vendor_n_read (uint8_t itf, void* buffer, uint32_t bufsize) {
|
||||
TU_VERIFY(itf < CFG_TUD_VENDOR, 0);
|
||||
vendord_interface_t* p_itf = &_vendord_itf[itf];
|
||||
uint8_t const rhport = 0;
|
||||
const uint8_t rhport = 0;
|
||||
|
||||
return tu_edpt_stream_read(rhport, &p_itf->rx.stream, buffer, bufsize);
|
||||
}
|
||||
@@ -102,7 +105,7 @@ uint32_t tud_vendor_n_read (uint8_t itf, void* buffer, uint32_t bufsize) {
|
||||
void tud_vendor_n_read_flush (uint8_t itf) {
|
||||
TU_VERIFY(itf < CFG_TUD_VENDOR, );
|
||||
vendord_interface_t* p_itf = &_vendord_itf[itf];
|
||||
uint8_t const rhport = 0;
|
||||
const uint8_t rhport = 0;
|
||||
|
||||
tu_edpt_stream_clear(&p_itf->rx.stream);
|
||||
tu_edpt_stream_read_xfer(rhport, &p_itf->rx.stream);
|
||||
@@ -111,10 +114,10 @@ void tud_vendor_n_read_flush (uint8_t itf) {
|
||||
//--------------------------------------------------------------------+
|
||||
// Write API
|
||||
//--------------------------------------------------------------------+
|
||||
uint32_t tud_vendor_n_write (uint8_t itf, void const* buffer, uint32_t bufsize) {
|
||||
uint32_t tud_vendor_n_write (uint8_t itf, const void* buffer, uint32_t bufsize) {
|
||||
TU_VERIFY(itf < CFG_TUD_VENDOR, 0);
|
||||
vendord_interface_t* p_itf = &_vendord_itf[itf];
|
||||
uint8_t const rhport = 0;
|
||||
const uint8_t rhport = 0;
|
||||
|
||||
return tu_edpt_stream_write(rhport, &p_itf->tx.stream, buffer, (uint16_t) bufsize);
|
||||
}
|
||||
@@ -122,7 +125,7 @@ uint32_t tud_vendor_n_write (uint8_t itf, void const* buffer, uint32_t bufsize)
|
||||
uint32_t tud_vendor_n_write_flush (uint8_t itf) {
|
||||
TU_VERIFY(itf < CFG_TUD_VENDOR, 0);
|
||||
vendord_interface_t* p_itf = &_vendord_itf[itf];
|
||||
uint8_t const rhport = 0;
|
||||
const uint8_t rhport = 0;
|
||||
|
||||
return tu_edpt_stream_write_xfer(rhport, &p_itf->tx.stream);
|
||||
}
|
||||
@@ -130,7 +133,7 @@ uint32_t tud_vendor_n_write_flush (uint8_t itf) {
|
||||
uint32_t tud_vendor_n_write_available (uint8_t itf) {
|
||||
TU_VERIFY(itf < CFG_TUD_VENDOR, 0);
|
||||
vendord_interface_t* p_itf = &_vendord_itf[itf];
|
||||
uint8_t const rhport = 0;
|
||||
const uint8_t rhport = 0;
|
||||
|
||||
return tu_edpt_stream_write_available(rhport, &p_itf->tx.stream);
|
||||
}
|
||||
@@ -143,6 +146,7 @@ void vendord_init(void) {
|
||||
|
||||
for(uint8_t i=0; i<CFG_TUD_VENDOR; i++) {
|
||||
vendord_interface_t* p_itf = &_vendord_itf[i];
|
||||
vendord_epbuf_t* p_epbuf = &_vendord_epbuf[i];
|
||||
|
||||
uint8_t* rx_ff_buf =
|
||||
#if CFG_TUD_VENDOR_RX_BUFSIZE > 0
|
||||
@@ -153,7 +157,7 @@ void vendord_init(void) {
|
||||
|
||||
tu_edpt_stream_init(&p_itf->rx.stream, false, false, false,
|
||||
rx_ff_buf, CFG_TUD_VENDOR_RX_BUFSIZE,
|
||||
p_itf->epout_buf, CFG_TUD_VENDOR_EPSIZE);
|
||||
p_epbuf->epout, CFG_TUD_VENDOR_EPSIZE);
|
||||
|
||||
uint8_t* tx_ff_buf =
|
||||
#if CFG_TUD_VENDOR_TX_BUFSIZE > 0
|
||||
@@ -164,7 +168,7 @@ void vendord_init(void) {
|
||||
|
||||
tu_edpt_stream_init(&p_itf->tx.stream, false, true, false,
|
||||
tx_ff_buf, CFG_TUD_VENDOR_TX_BUFSIZE,
|
||||
p_itf->epin_buf, CFG_TUD_VENDOR_EPSIZE);
|
||||
p_epbuf->epin, CFG_TUD_VENDOR_EPSIZE);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -190,7 +194,7 @@ void vendord_reset(uint8_t rhport) {
|
||||
}
|
||||
}
|
||||
|
||||
uint16_t vendord_open(uint8_t rhport, tusb_desc_interface_t const * desc_itf, uint16_t max_len) {
|
||||
uint16_t vendord_open(uint8_t rhport, const tusb_desc_interface_t* desc_itf, uint16_t max_len) {
|
||||
TU_VERIFY(TUSB_CLASS_VENDOR_SPECIFIC == desc_itf->bInterfaceClass, 0);
|
||||
const uint8_t* p_desc = tu_desc_next(desc_itf);
|
||||
const uint8_t* desc_end = p_desc + max_len;
|
||||
@@ -237,25 +241,29 @@ uint16_t vendord_open(uint8_t rhport, tusb_desc_interface_t const * desc_itf, ui
|
||||
bool vendord_xfer_cb(uint8_t rhport, uint8_t ep_addr, xfer_result_t result, uint32_t xferred_bytes) {
|
||||
(void) result;
|
||||
|
||||
uint8_t itf = 0;
|
||||
vendord_interface_t* p_itf = _vendord_itf;
|
||||
uint8_t itf;
|
||||
vendord_interface_t* p_vendor;
|
||||
|
||||
for ( ; ; itf++, p_itf++) {
|
||||
if (itf >= CFG_TUD_VENDOR) return false;
|
||||
if ((ep_addr == p_itf->rx.stream.ep_addr) || (ep_addr == p_itf->tx.stream.ep_addr)) break;
|
||||
for (itf = 0; itf < CFG_TUD_VENDOR; itf++) {
|
||||
p_vendor = &_vendord_itf[itf];
|
||||
if ((ep_addr == p_vendor->rx.stream.ep_addr) || (ep_addr == p_vendor->tx.stream.ep_addr)) {
|
||||
break;
|
||||
}
|
||||
}
|
||||
TU_VERIFY(itf < CFG_TUD_VENDOR);
|
||||
vendord_epbuf_t* p_epbuf = &_vendord_epbuf[itf];
|
||||
|
||||
if ( ep_addr == p_itf->rx.stream.ep_addr ) {
|
||||
if ( ep_addr == p_vendor->rx.stream.ep_addr ) {
|
||||
// Received new data: put into stream's fifo
|
||||
tu_edpt_stream_read_xfer_complete(&p_itf->rx.stream, xferred_bytes);
|
||||
tu_edpt_stream_read_xfer_complete(&p_vendor->rx.stream, xferred_bytes);
|
||||
|
||||
// Invoked callback if any
|
||||
if (tud_vendor_rx_cb) {
|
||||
tud_vendor_rx_cb(itf, p_itf->epout_buf, (uint16_t) xferred_bytes);
|
||||
tud_vendor_rx_cb(itf, p_epbuf->epout, (uint16_t) xferred_bytes);
|
||||
}
|
||||
|
||||
tu_edpt_stream_read_xfer(rhport, &p_itf->rx.stream);
|
||||
} else if ( ep_addr == p_itf->tx.stream.ep_addr ) {
|
||||
tu_edpt_stream_read_xfer(rhport, &p_vendor->rx.stream);
|
||||
} else if ( ep_addr == p_vendor->tx.stream.ep_addr ) {
|
||||
// Send complete
|
||||
if (tud_vendor_tx_cb) {
|
||||
tud_vendor_tx_cb(itf, (uint16_t) xferred_bytes);
|
||||
@@ -263,9 +271,9 @@ bool vendord_xfer_cb(uint8_t rhport, uint8_t ep_addr, xfer_result_t result, uint
|
||||
|
||||
#if CFG_TUD_VENDOR_TX_BUFSIZE > 0
|
||||
// try to send more if possible
|
||||
if ( 0 == tu_edpt_stream_write_xfer(rhport, &p_itf->tx.stream) ) {
|
||||
if ( 0 == tu_edpt_stream_write_xfer(rhport, &p_vendor->tx.stream) ) {
|
||||
// If there is no data left, a ZLP should be sent if xferred_bytes is multiple of EP Packet size and not zero
|
||||
tu_edpt_stream_write_zlp_if_needed(rhport, &p_itf->tx.stream, xferred_bytes);
|
||||
tu_edpt_stream_write_zlp_if_needed(rhport, &p_vendor->tx.stream, xferred_bytes);
|
||||
}
|
||||
#endif
|
||||
}
|
||||
|
||||
@@ -116,34 +116,32 @@ typedef struct TU_ATTR_PACKED {
|
||||
uint8_t state; /* 0:probing 1:committed 2:streaming */
|
||||
|
||||
video_probe_and_commit_control_t probe_commit_payload; /* Probe and Commit control */
|
||||
/*------------- From this point, data is not cleared by bus reset -------------*/
|
||||
CFG_TUSB_MEM_ALIGN uint8_t ep_buf[CFG_TUD_VIDEO_STREAMING_EP_BUFSIZE]; /* EP transfer buffer for streaming */
|
||||
} videod_streaming_interface_t;
|
||||
|
||||
typedef struct {
|
||||
TUD_EPBUF_DEF(buf, CFG_TUD_VIDEO_STREAMING_EP_BUFSIZE);
|
||||
} videod_streaming_epbuf_t;
|
||||
|
||||
/* video control interface */
|
||||
typedef struct TU_ATTR_PACKED {
|
||||
uint8_t const *beg; /* The head of the first video control interface descriptor */
|
||||
uint16_t len; /* Byte length of the descriptors */
|
||||
uint16_t cur; /* offset for current video control interface */
|
||||
uint8_t stm[CFG_TUD_VIDEO_STREAMING]; /* Indices of streaming interface */
|
||||
uint8_t error_code; /* error code */
|
||||
uint8_t power_mode;
|
||||
|
||||
/*------------- From this point, data is not cleared by bus reset -------------*/
|
||||
// CFG_TUSB_MEM_ALIGN uint8_t ctl_buf[64]; /* EP transfer buffer for interrupt transfer */
|
||||
|
||||
const uint8_t*beg; /* The head of the first video control interface descriptor */
|
||||
uint16_t len; /* Byte length of the descriptors */
|
||||
uint16_t cur; /* offset for current video control interface */
|
||||
uint8_t stm[CFG_TUD_VIDEO_STREAMING]; /* Indices of streaming interface */
|
||||
uint8_t error_code; /* error code */
|
||||
uint8_t power_mode;
|
||||
} videod_interface_t;
|
||||
|
||||
#define ITF_STM_MEM_RESET_SIZE offsetof(videod_streaming_interface_t, ep_buf)
|
||||
|
||||
//--------------------------------------------------------------------+
|
||||
// INTERNAL OBJECT & FUNCTION DECLARATION
|
||||
//--------------------------------------------------------------------+
|
||||
CFG_TUD_MEM_SECTION tu_static videod_interface_t _videod_itf[CFG_TUD_VIDEO];
|
||||
CFG_TUD_MEM_SECTION tu_static videod_streaming_interface_t _videod_streaming_itf[CFG_TUD_VIDEO_STREAMING];
|
||||
static videod_interface_t _videod_itf[CFG_TUD_VIDEO];
|
||||
|
||||
tu_static uint8_t const _cap_get = 0x1u; /* support for GET */
|
||||
tu_static uint8_t const _cap_get_set = 0x3u; /* support for GET and SET */
|
||||
static videod_streaming_interface_t _videod_streaming_itf[CFG_TUD_VIDEO_STREAMING];
|
||||
CFG_TUD_MEM_SECTION static videod_streaming_epbuf_t _videod_streaming_epbuf[CFG_TUD_VIDEO_STREAMING];
|
||||
|
||||
static uint8_t const _cap_get = 0x1u; /* support for GET */
|
||||
static uint8_t const _cap_get_set = 0x3u; /* support for GET and SET */
|
||||
|
||||
//--------------------------------------------------------------------+
|
||||
// Debug
|
||||
@@ -816,21 +814,20 @@ static bool _open_vs_itf(uint8_t rhport, videod_streaming_interface_t *stm, uint
|
||||
}
|
||||
|
||||
/** Prepare the next packet payload. */
|
||||
static uint_fast16_t _prepare_in_payload(videod_streaming_interface_t *stm)
|
||||
{
|
||||
static uint_fast16_t _prepare_in_payload(videod_streaming_interface_t *stm, uint8_t* ep_buf) {
|
||||
uint_fast16_t remaining = stm->bufsize - stm->offset;
|
||||
uint_fast16_t hdr_len = stm->ep_buf[0];
|
||||
uint_fast16_t hdr_len = ep_buf[0];
|
||||
uint_fast16_t pkt_len = stm->max_payload_transfer_size;
|
||||
if (hdr_len + remaining < pkt_len) {
|
||||
pkt_len = hdr_len + remaining;
|
||||
}
|
||||
TU_ASSERT(pkt_len >= hdr_len);
|
||||
uint_fast16_t data_len = pkt_len - hdr_len;
|
||||
memcpy(&stm->ep_buf[hdr_len], stm->buffer + stm->offset, data_len);
|
||||
memcpy(&ep_buf[hdr_len], stm->buffer + stm->offset, data_len);
|
||||
stm->offset += data_len;
|
||||
remaining -= data_len;
|
||||
if (!remaining) {
|
||||
tusb_video_payload_header_t *hdr = (tusb_video_payload_header_t*)stm->ep_buf;
|
||||
tusb_video_payload_header_t *hdr = (tusb_video_payload_header_t*) ep_buf;
|
||||
hdr->EndOfFrame = 1;
|
||||
}
|
||||
return hdr_len + data_len;
|
||||
@@ -1001,7 +998,8 @@ static int handle_video_stm_cs_req(uint8_t rhport, uint8_t stage,
|
||||
tusb_control_request_t const *request,
|
||||
uint_fast8_t stm_idx) {
|
||||
(void)rhport;
|
||||
videod_streaming_interface_t *self = &_videod_streaming_itf[stm_idx];
|
||||
videod_streaming_interface_t *stm = &_videod_streaming_itf[stm_idx];
|
||||
videod_streaming_epbuf_t *stm_epbuf = &_videod_streaming_epbuf[stm_idx];
|
||||
|
||||
uint8_t const ctrl_sel = TU_U16_HIGH(request->wValue);
|
||||
TU_LOG_DRV("%s_Control(%s)\r\n", tu_str_video_vs_control_selector[ctrl_sel], tu_lookup_find(&tu_table_video_request, request->bRequest));
|
||||
@@ -1013,7 +1011,7 @@ static int handle_video_stm_cs_req(uint8_t rhport, uint8_t stage,
|
||||
case VIDEO_REQUEST_GET_CUR:
|
||||
if (stage == CONTROL_STAGE_SETUP) {
|
||||
/* TODO */
|
||||
TU_VERIFY(tud_control_xfer(rhport, request, &self->error_code, sizeof(uint8_t)), VIDEO_ERROR_UNKNOWN);
|
||||
TU_VERIFY(tud_control_xfer(rhport, request, &stm->error_code, sizeof(uint8_t)), VIDEO_ERROR_UNKNOWN);
|
||||
}
|
||||
return VIDEO_ERROR_NONE;
|
||||
|
||||
@@ -1028,17 +1026,17 @@ static int handle_video_stm_cs_req(uint8_t rhport, uint8_t stage,
|
||||
break;
|
||||
|
||||
case VIDEO_VS_CTL_PROBE:
|
||||
if (self->state != VS_STATE_PROBING) {
|
||||
self->state = VS_STATE_PROBING;
|
||||
if (stm->state != VS_STATE_PROBING) {
|
||||
stm->state = VS_STATE_PROBING;
|
||||
}
|
||||
|
||||
switch (request->bRequest) {
|
||||
case VIDEO_REQUEST_SET_CUR:
|
||||
if (stage == CONTROL_STAGE_SETUP) {
|
||||
TU_VERIFY(tud_control_xfer(rhport, request, &self->probe_commit_payload, sizeof(video_probe_and_commit_control_t)),
|
||||
TU_VERIFY(tud_control_xfer(rhport, request, &stm->probe_commit_payload, sizeof(video_probe_and_commit_control_t)),
|
||||
VIDEO_ERROR_UNKNOWN);
|
||||
} else if (stage == CONTROL_STAGE_DATA) {
|
||||
TU_VERIFY(_update_streaming_parameters(self, &self->probe_commit_payload),
|
||||
TU_VERIFY(_update_streaming_parameters(stm, &stm->probe_commit_payload),
|
||||
VIDEO_ERROR_INVALID_VALUE_WITHIN_RANGE);
|
||||
}
|
||||
return VIDEO_ERROR_NONE;
|
||||
@@ -1046,7 +1044,7 @@ static int handle_video_stm_cs_req(uint8_t rhport, uint8_t stage,
|
||||
case VIDEO_REQUEST_GET_CUR:
|
||||
if (stage == CONTROL_STAGE_SETUP) {
|
||||
TU_VERIFY(request->wLength, VIDEO_ERROR_UNKNOWN);
|
||||
TU_VERIFY(tud_control_xfer(rhport, request, &self->probe_commit_payload, sizeof(video_probe_and_commit_control_t)), VIDEO_ERROR_UNKNOWN);
|
||||
TU_VERIFY(tud_control_xfer(rhport, request, &stm->probe_commit_payload, sizeof(video_probe_and_commit_control_t)), VIDEO_ERROR_UNKNOWN);
|
||||
}
|
||||
return VIDEO_ERROR_NONE;
|
||||
|
||||
@@ -1056,8 +1054,8 @@ static int handle_video_stm_cs_req(uint8_t rhport, uint8_t stage,
|
||||
case VIDEO_REQUEST_GET_DEF:
|
||||
if (stage == CONTROL_STAGE_SETUP) {
|
||||
TU_VERIFY(request->wLength, VIDEO_ERROR_UNKNOWN);
|
||||
video_probe_and_commit_control_t tmp = self->probe_commit_payload;
|
||||
TU_VERIFY(_negotiate_streaming_parameters(self, request->bRequest, &tmp), VIDEO_ERROR_INVALID_VALUE_WITHIN_RANGE);
|
||||
video_probe_and_commit_control_t tmp = stm->probe_commit_payload;
|
||||
TU_VERIFY(_negotiate_streaming_parameters(stm, request->bRequest, &tmp), VIDEO_ERROR_INVALID_VALUE_WITHIN_RANGE);
|
||||
TU_VERIFY(tud_control_xfer(rhport, request, &tmp, sizeof(tmp)), VIDEO_ERROR_UNKNOWN);
|
||||
}
|
||||
return VIDEO_ERROR_NONE;
|
||||
@@ -1085,23 +1083,23 @@ static int handle_video_stm_cs_req(uint8_t rhport, uint8_t stage,
|
||||
switch (request->bRequest) {
|
||||
case VIDEO_REQUEST_SET_CUR:
|
||||
if (stage == CONTROL_STAGE_SETUP) {
|
||||
TU_VERIFY(tud_control_xfer(rhport, request, &self->probe_commit_payload, sizeof(video_probe_and_commit_control_t)), VIDEO_ERROR_UNKNOWN);
|
||||
TU_VERIFY(tud_control_xfer(rhport, request, &stm->probe_commit_payload, sizeof(video_probe_and_commit_control_t)), VIDEO_ERROR_UNKNOWN);
|
||||
} else if (stage == CONTROL_STAGE_DATA) {
|
||||
video_probe_and_commit_control_t *param = &self->probe_commit_payload;
|
||||
TU_VERIFY(_update_streaming_parameters(self, param), VIDEO_ERROR_INVALID_VALUE_WITHIN_RANGE);
|
||||
video_probe_and_commit_control_t *param = &stm->probe_commit_payload;
|
||||
TU_VERIFY(_update_streaming_parameters(stm, param), VIDEO_ERROR_INVALID_VALUE_WITHIN_RANGE);
|
||||
/* Set the negotiated value */
|
||||
self->max_payload_transfer_size = param->dwMaxPayloadTransferSize;
|
||||
stm->max_payload_transfer_size = param->dwMaxPayloadTransferSize;
|
||||
int ret = VIDEO_ERROR_NONE;
|
||||
if (tud_video_commit_cb) {
|
||||
ret = tud_video_commit_cb(self->index_vc, self->index_vs, param);
|
||||
ret = tud_video_commit_cb(stm->index_vc, stm->index_vs, param);
|
||||
}
|
||||
if (VIDEO_ERROR_NONE == ret) {
|
||||
self->state = VS_STATE_COMMITTED;
|
||||
self->buffer = NULL;
|
||||
self->bufsize = 0;
|
||||
self->offset = 0;
|
||||
stm->state = VS_STATE_COMMITTED;
|
||||
stm->buffer = NULL;
|
||||
stm->bufsize = 0;
|
||||
stm->offset = 0;
|
||||
/* initialize payload header */
|
||||
tusb_video_payload_header_t *hdr = (tusb_video_payload_header_t*)self->ep_buf;
|
||||
tusb_video_payload_header_t *hdr = (tusb_video_payload_header_t*)stm_epbuf->buf;
|
||||
hdr->bHeaderLength = sizeof(*hdr);
|
||||
hdr->bmHeaderInfo = 0;
|
||||
}
|
||||
@@ -1111,7 +1109,7 @@ static int handle_video_stm_cs_req(uint8_t rhport, uint8_t stage,
|
||||
case VIDEO_REQUEST_GET_CUR:
|
||||
if (stage == CONTROL_STAGE_SETUP) {
|
||||
TU_VERIFY(request->wLength, VIDEO_ERROR_UNKNOWN);
|
||||
TU_VERIFY(tud_control_xfer(rhport, request, &self->probe_commit_payload, sizeof(video_probe_and_commit_control_t)), VIDEO_ERROR_UNKNOWN);
|
||||
TU_VERIFY(tud_control_xfer(rhport, request, &stm->probe_commit_payload, sizeof(video_probe_and_commit_control_t)), VIDEO_ERROR_UNKNOWN);
|
||||
}
|
||||
return VIDEO_ERROR_NONE;
|
||||
|
||||
@@ -1199,12 +1197,14 @@ bool tud_video_n_streaming(uint_fast8_t ctl_idx, uint_fast8_t stm_idx)
|
||||
return true;
|
||||
}
|
||||
|
||||
bool tud_video_n_frame_xfer(uint_fast8_t ctl_idx, uint_fast8_t stm_idx, void *buffer, size_t bufsize)
|
||||
{
|
||||
bool tud_video_n_frame_xfer(uint_fast8_t ctl_idx, uint_fast8_t stm_idx, void *buffer, size_t bufsize) {
|
||||
TU_ASSERT(ctl_idx < CFG_TUD_VIDEO);
|
||||
TU_ASSERT(stm_idx < CFG_TUD_VIDEO_STREAMING);
|
||||
|
||||
if (!buffer || !bufsize) return false;
|
||||
videod_streaming_interface_t *stm = _get_instance_streaming(ctl_idx, stm_idx);
|
||||
videod_streaming_epbuf_t *stm_epbuf = &_videod_streaming_epbuf[ctl_idx];
|
||||
|
||||
if (!stm || !stm->desc.ep[0] || stm->buffer) return false;
|
||||
if (stm->state == VS_STATE_PROBING) return false;
|
||||
|
||||
@@ -1221,14 +1221,14 @@ bool tud_video_n_frame_xfer(uint_fast8_t ctl_idx, uint_fast8_t stm_idx, void *bu
|
||||
|
||||
TU_VERIFY( usbd_edpt_claim(0, ep_addr) );
|
||||
/* update the packet header */
|
||||
tusb_video_payload_header_t *hdr = (tusb_video_payload_header_t*)stm->ep_buf;
|
||||
tusb_video_payload_header_t *hdr = (tusb_video_payload_header_t*)stm_epbuf->buf;
|
||||
hdr->FrameID ^= 1;
|
||||
hdr->EndOfFrame = 0;
|
||||
/* update the packet data */
|
||||
stm->buffer = (uint8_t*)buffer;
|
||||
stm->bufsize = bufsize;
|
||||
uint_fast16_t pkt_len = _prepare_in_payload(stm);
|
||||
TU_ASSERT( usbd_edpt_xfer(0, ep_addr, stm->ep_buf, (uint16_t) pkt_len), 0);
|
||||
uint_fast16_t pkt_len = _prepare_in_payload(stm, stm_epbuf->buf);
|
||||
TU_ASSERT( usbd_edpt_xfer(0, ep_addr, stm_epbuf->buf, (uint16_t) pkt_len), 0);
|
||||
return true;
|
||||
}
|
||||
|
||||
@@ -1242,7 +1242,7 @@ void videod_init(void) {
|
||||
}
|
||||
for (uint_fast8_t i = 0; i < CFG_TUD_VIDEO_STREAMING; ++i) {
|
||||
videod_streaming_interface_t *stm = &_videod_streaming_itf[i];
|
||||
tu_memclr(stm, ITF_STM_MEM_RESET_SIZE);
|
||||
tu_memclr(stm, sizeof(videod_streaming_interface_t));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1258,7 +1258,7 @@ void videod_reset(uint8_t rhport) {
|
||||
}
|
||||
for (uint_fast8_t i = 0; i < CFG_TUD_VIDEO_STREAMING; ++i) {
|
||||
videod_streaming_interface_t *stm = &_videod_streaming_itf[i];
|
||||
tu_memclr(stm, ITF_STM_MEM_RESET_SIZE);
|
||||
tu_memclr(stm, sizeof(videod_streaming_interface_t));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1307,7 +1307,7 @@ uint16_t videod_open(uint8_t rhport, tusb_desc_interface_t const * itf_desc, uin
|
||||
#ifdef TUP_DCD_EDPT_ISO_ALLOC
|
||||
/* Allocate ISO endpoints */
|
||||
uint16_t ep_size = 0;
|
||||
uint16_t ep_addr = 0;
|
||||
uint8_t ep_addr = 0;
|
||||
uint8_t const *p_desc = (uint8_t const*)itf_desc + stm->desc.beg;
|
||||
uint8_t const *p_desc_end = (uint8_t const*)itf_desc + stm->desc.end;
|
||||
while (p_desc < p_desc_end) {
|
||||
@@ -1392,13 +1392,14 @@ bool videod_xfer_cb(uint8_t rhport, uint8_t ep_addr, xfer_result_t result, uint3
|
||||
uint8_t const *desc = ctl->beg;
|
||||
if (ep_addr == _desc_ep_addr(desc + ep_ofs)) break;
|
||||
}
|
||||
|
||||
TU_ASSERT(itf < CFG_TUD_VIDEO_STREAMING);
|
||||
videod_streaming_epbuf_t *stm_epbuf = &_videod_streaming_epbuf[itf];
|
||||
|
||||
if (stm->offset < stm->bufsize) {
|
||||
/* Claim the endpoint */
|
||||
TU_VERIFY( usbd_edpt_claim(rhport, ep_addr), 0);
|
||||
uint_fast16_t pkt_len = _prepare_in_payload(stm);
|
||||
TU_ASSERT( usbd_edpt_xfer(rhport, ep_addr, stm->ep_buf, (uint16_t) pkt_len), 0);
|
||||
uint_fast16_t pkt_len = _prepare_in_payload(stm, stm_epbuf->buf);
|
||||
TU_ASSERT( usbd_edpt_xfer(rhport, ep_addr, stm_epbuf->buf, (uint16_t) pkt_len), 0);
|
||||
} else {
|
||||
stm->buffer = NULL;
|
||||
stm->bufsize = 0;
|
||||
|
||||
@@ -40,6 +40,8 @@ extern "C" {
|
||||
// CFG_TUD_VIDEO > 1
|
||||
//--------------------------------------------------------------------+
|
||||
|
||||
bool tud_video_n_connected(uint_fast8_t ctl_idx);
|
||||
|
||||
/** Return true if streaming
|
||||
*
|
||||
* @param[in] ctl_idx Destination control interface index
|
||||
|
||||
Reference in New Issue
Block a user