revert usbd_control_xfer_st() implementation

enhance cdc with better multiple interfaces support
add default ep size for cdc and msc device CFG_TUD_CDC_EPSIZE,
CFG_TUD_MSC_EPSIZE
This commit is contained in:
hathach 2018-07-17 16:04:55 +07:00
parent abb37e98ba
commit 798ce59ebd
7 changed files with 105 additions and 98 deletions

View File

@ -61,30 +61,29 @@ typedef struct
// Bit 0: DTR (Data Terminal Ready), Bit 1: RTS (Request to Send)
uint8_t line_state;
// Data that is not cleared by usb bus reset
struct {
cdc_line_coding_t line_coding;
/*------------- From this point, data is not cleared by bus reset -------------*/
cdc_line_coding_t line_coding;
char wanted_char;
char wanted_char;
// FIFO
tu_fifo_t rx_ff;
tu_fifo_t tx_ff;
uint8_t rx_ff_buf[CFG_TUD_CDC_RX_BUFSIZE];
uint8_t tx_ff_buf[CFG_TUD_CDC_RX_BUFSIZE];
uint8_t rx_ff_buf[CFG_TUD_CDC_RX_BUFSIZE];
uint8_t tx_ff_buf[CFG_TUD_CDC_RX_BUFSIZE];
tu_fifo_t rx_ff;
tu_fifo_t tx_ff;
}intact;
// Endpoint Transfer buffer
CFG_TUSB_MEM_ALIGN uint8_t epin_buf[CFG_TUD_CDC_EPSIZE];
CFG_TUSB_MEM_ALIGN uint8_t epout_buf[CFG_TUD_CDC_EPSIZE];
}cdcd_interface_t;
#define ITF_BUS_RESET_SZ offsetof(cdcd_interface_t, line_coding)
//--------------------------------------------------------------------+
// INTERNAL OBJECT & FUNCTION DECLARATION
//--------------------------------------------------------------------+
// TODO multiple interfaces
CFG_TUSB_ATTR_USBRAM CFG_TUSB_MEM_ALIGN static uint8_t _rx_buf[64];
CFG_TUSB_ATTR_USBRAM CFG_TUSB_MEM_ALIGN static uint8_t _tx_buf[64];
static cdcd_interface_t _cdcd_itf[CFG_TUD_CDC];
CFG_TUSB_ATTR_USBRAM static cdcd_interface_t _cdcd_itf[CFG_TUD_CDC];
//--------------------------------------------------------------------+
// APPLICATION API
@ -102,12 +101,12 @@ uint8_t tud_cdc_n_get_line_state (uint8_t itf)
void tud_cdc_n_get_line_coding (uint8_t itf, cdc_line_coding_t* coding)
{
(*coding) = _cdcd_itf[itf].intact.line_coding;
(*coding) = _cdcd_itf[itf].line_coding;
}
void tud_cdc_n_set_wanted_char (uint8_t itf, char wanted)
{
_cdcd_itf[itf].intact.wanted_char = wanted;
_cdcd_itf[itf].wanted_char = wanted;
}
@ -116,29 +115,29 @@ void tud_cdc_n_set_wanted_char (uint8_t itf, char wanted)
//--------------------------------------------------------------------+
uint32_t tud_cdc_n_available(uint8_t itf)
{
return tu_fifo_count(&_cdcd_itf[itf].intact.rx_ff);
return tu_fifo_count(&_cdcd_itf[itf].rx_ff);
}
char tud_cdc_n_read_char(uint8_t itf)
{
char ch;
return tu_fifo_read(&_cdcd_itf[itf].intact.rx_ff, &ch) ? ch : (-1);
return tu_fifo_read(&_cdcd_itf[itf].rx_ff, &ch) ? ch : (-1);
}
uint32_t tud_cdc_n_read(uint8_t itf, void* buffer, uint32_t bufsize)
{
return tu_fifo_read_n(&_cdcd_itf[itf].intact.rx_ff, buffer, bufsize);
return tu_fifo_read_n(&_cdcd_itf[itf].rx_ff, buffer, bufsize);
}
char tud_cdc_n_peek(uint8_t itf, int pos)
{
char ch;
return tu_fifo_peek_at(&_cdcd_itf[itf].intact.rx_ff, pos, &ch) ? ch : (-1);
return tu_fifo_peek_at(&_cdcd_itf[itf].rx_ff, pos, &ch) ? ch : (-1);
}
void tud_cdc_n_read_flush (uint8_t itf)
{
tu_fifo_clear(&_cdcd_itf[itf].intact.rx_ff);
tu_fifo_clear(&_cdcd_itf[itf].rx_ff);
}
//--------------------------------------------------------------------+
@ -147,24 +146,24 @@ void tud_cdc_n_read_flush (uint8_t itf)
uint32_t tud_cdc_n_write_char(uint8_t itf, char ch)
{
return tu_fifo_write(&_cdcd_itf[itf].intact.tx_ff, &ch) ? 1 : 0;
return tu_fifo_write(&_cdcd_itf[itf].tx_ff, &ch) ? 1 : 0;
}
uint32_t tud_cdc_n_write(uint8_t itf, void const* buffer, uint32_t bufsize)
{
return tu_fifo_write_n(&_cdcd_itf[itf].intact.tx_ff, buffer, bufsize);
return tu_fifo_write_n(&_cdcd_itf[itf].tx_ff, buffer, bufsize);
}
bool tud_cdc_n_write_flush (uint8_t itf)
{
uint8_t edpt = _cdcd_itf[itf].ep_in;
VERIFY( !dcd_edpt_busy(TUD_RHPORT, edpt) ); // skip if previous transfer not complete
cdcd_interface_t* p_cdc = &_cdcd_itf[itf];
VERIFY( !dcd_edpt_busy(TUD_RHPORT, p_cdc->ep_in) ); // skip if previous transfer not complete
uint16_t count = tu_fifo_read_n(&_cdcd_itf[itf].intact.tx_ff, _tx_buf, sizeof(_tx_buf));
uint16_t count = tu_fifo_read_n(&_cdcd_itf[itf].tx_ff, p_cdc->epout_buf, CFG_TUD_CDC_EPSIZE);
VERIFY( tud_cdc_n_connected(itf) ); // fifo is empty if not connected
if ( count ) TU_ASSERT( dcd_edpt_xfer(TUD_RHPORT, edpt, _tx_buf, count) );
if ( count ) TU_ASSERT( dcd_edpt_xfer(TUD_RHPORT, p_cdc->ep_in, p_cdc->epout_buf, count) );
return true;
}
@ -179,17 +178,17 @@ void cdcd_init(void)
for(uint8_t i=0; i<CFG_TUD_CDC; i++)
{
_cdcd_itf[i].intact.wanted_char = -1;
_cdcd_itf[i].wanted_char = -1;
// default line coding is : stop bit = 1, parity = none, data bits = 8
_cdcd_itf[i].intact.line_coding.bit_rate = 115200;
_cdcd_itf[i].intact.line_coding.stop_bits = 0;
_cdcd_itf[i].intact.line_coding.parity = 0;
_cdcd_itf[i].intact.line_coding.data_bits = 8;
_cdcd_itf[i].line_coding.bit_rate = 115200;
_cdcd_itf[i].line_coding.stop_bits = 0;
_cdcd_itf[i].line_coding.parity = 0;
_cdcd_itf[i].line_coding.data_bits = 8;
// config fifo
tu_fifo_config(&_cdcd_itf[i].intact.rx_ff, _cdcd_itf[i].intact.rx_ff_buf, CFG_TUD_CDC_RX_BUFSIZE, 1, true);
tu_fifo_config(&_cdcd_itf[i].intact.tx_ff, _cdcd_itf[i].intact.tx_ff_buf, CFG_TUD_CDC_TX_BUFSIZE, 1, false);
tu_fifo_config(&_cdcd_itf[i].rx_ff, _cdcd_itf[i].rx_ff_buf, CFG_TUD_CDC_RX_BUFSIZE, 1, true);
tu_fifo_config(&_cdcd_itf[i].tx_ff, _cdcd_itf[i].tx_ff_buf, CFG_TUD_CDC_TX_BUFSIZE, 1, false);
}
}
@ -199,9 +198,9 @@ void cdcd_reset(uint8_t rhport)
for(uint8_t i=0; i<CFG_TUD_CDC; i++)
{
memclr_(&_cdcd_itf[i], offsetof(cdcd_interface_t, intact));
tu_fifo_clear(&_cdcd_itf[i].intact.rx_ff);
tu_fifo_clear(&_cdcd_itf[i].intact.tx_ff);
memclr_(&_cdcd_itf[i], ITF_BUS_RESET_SZ);
tu_fifo_clear(&_cdcd_itf[i].rx_ff);
tu_fifo_clear(&_cdcd_itf[i].tx_ff);
}
}
@ -265,7 +264,7 @@ tusb_error_t cdcd_open(uint8_t rhport, tusb_desc_interface_t const * p_interface
}
// Prepare for incoming data
TU_ASSERT( dcd_edpt_xfer(rhport, p_cdc->ep_out, _rx_buf, sizeof(_rx_buf)), TUSB_ERROR_DCD_EDPT_XFER);
TU_ASSERT( dcd_edpt_xfer(rhport, p_cdc->ep_out, p_cdc->epin_buf, CFG_TUD_CDC_EPSIZE), TUSB_ERROR_DCD_EDPT_XFER);
return TUSB_ERROR_NONE;
}
@ -284,12 +283,12 @@ tusb_error_t cdcd_control_request_st(uint8_t rhport, tusb_control_request_t cons
if ( (CDC_REQUEST_GET_LINE_CODING == p_request->bRequest) || (CDC_REQUEST_SET_LINE_CODING == p_request->bRequest) )
{
uint16_t len = min16_of(sizeof(cdc_line_coding_t), p_request->wLength);
usbd_control_xfer_st(rhport, p_request->bmRequestType_bit.direction, &p_cdc->intact.line_coding, len);
usbd_control_xfer_st(rhport, p_request->bmRequestType_bit.direction, &p_cdc->line_coding, len);
// Invoke callback
if (CDC_REQUEST_SET_LINE_CODING == p_request->bRequest)
{
if ( tud_cdc_line_coding_cb ) tud_cdc_line_coding_cb(itf, &p_cdc->intact.line_coding);
if ( tud_cdc_line_coding_cb ) tud_cdc_line_coding_cb(itf, &p_cdc->line_coding);
}
}
else if (CDC_REQUEST_SET_CONTROL_LINE_STATE == p_request->bRequest )
@ -324,25 +323,25 @@ tusb_error_t cdcd_xfer_cb(uint8_t rhport, uint8_t ep_addr, tusb_event_t event, u
// receive new data
if ( ep_addr == p_cdc->ep_out )
{
char const wanted = p_cdc->intact.wanted_char;
char const wanted = p_cdc->wanted_char;
for(uint32_t i=0; i<xferred_bytes; i++)
{
// Check for wanted char and invoke callback if needed
if ( tud_cdc_rx_wanted_cb && ( wanted != -1 ) && ( wanted == _rx_buf[i] ) )
if ( tud_cdc_rx_wanted_cb && ( wanted != -1 ) && ( wanted == p_cdc->epin_buf[i] ) )
{
tud_cdc_rx_wanted_cb(itf, wanted);
}else
{
tu_fifo_write(&p_cdc->intact.rx_ff, &_rx_buf[i]);
tu_fifo_write(&p_cdc->rx_ff, &p_cdc->epin_buf[i]);
}
}
// invoke receive callback (if there is still data)
if (tud_cdc_rx_cb && tu_fifo_count(&p_cdc->intact.rx_ff) ) tud_cdc_rx_cb(itf);
if (tud_cdc_rx_cb && tu_fifo_count(&p_cdc->rx_ff) ) tud_cdc_rx_cb(itf);
// prepare for next
TU_ASSERT( dcd_edpt_xfer(rhport, p_cdc->ep_out, _rx_buf, sizeof(_rx_buf)), TUSB_ERROR_DCD_EDPT_XFER );
TU_ASSERT( dcd_edpt_xfer(rhport, p_cdc->ep_out, p_cdc->epin_buf, CFG_TUD_CDC_EPSIZE), TUSB_ERROR_DCD_EDPT_XFER );
}
// nothing to do with in and notif endpoint

View File

@ -43,6 +43,14 @@
#include "device/usbd.h"
#include "cdc.h"
//--------------------------------------------------------------------+
// Class Driver Configuration
//--------------------------------------------------------------------+
#ifndef CFG_TUD_CDC_EPSIZE
#define CFG_TUD_CDC_EPSIZE 64
#endif
#ifdef __cplusplus
extern "C" {
#endif

View File

@ -49,41 +49,6 @@
#include "msc_device.h"
#include "device/usbd_pvt.h"
//--------------------------------------------------------------------+
// Config Verification
//--------------------------------------------------------------------+
VERIFY_STATIC(CFG_TUD_MSC_BUFSIZE < UINT16_MAX, "Size is not correct");
#ifndef CFG_TUD_MSC_MAXLUN
#define CFG_TUD_MSC_MAXLUN 1
#elif CFG_TUD_MSC_MAXLUN == 0 || CFG_TUD_MSC_MAXLUN > 16
#error MSC Device: Incorrect setting of MAX LUN
#endif
#ifndef CFG_TUD_MSC_BLOCK_NUM
#error CFG_TUD_MSC_BLOCK_NUM must be defined
#endif
#ifndef CFG_TUD_MSC_BLOCK_SZ
#error CFG_TUD_MSC_BLOCK_SZ must be defined
#endif
#ifndef CFG_TUD_MSC_BUFSIZE
#error CFG_TUD_MSC_BUFSIZE must be defined, value of CFG_TUD_MSC_BLOCK_SZ should work well, the more the better
#endif
#ifndef CFG_TUD_MSC_VENDOR
#error CFG_TUD_MSC_VENDOR 8-byte name must be defined
#endif
#ifndef CFG_TUD_MSC_PRODUCT
#error CFG_TUD_MSC_PRODUCT 16-byte name must be defined
#endif
#ifndef CFG_TUD_MSC_PRODUCT_REV
#error CFG_TUD_MSC_PRODUCT_REV 4-byte string must be defined
#endif
//--------------------------------------------------------------------+
// MACRO CONSTANT TYPEDEF
//--------------------------------------------------------------------+
@ -193,8 +158,8 @@ tusb_error_t mscd_control_request_st(uint8_t rhport, tusb_control_request_t cons
else if (MSC_REQUEST_GET_MAX_LUN == p_request->bRequest)
{
// returned MAX LUN is minus 1 by specs
uint8_t lun = CFG_TUD_MSC_MAXLUN-1;
usbd_control_xfer_st(rhport, p_request->bmRequestType_bit.direction, &lun, 1);
_usbd_ctrl_buf[0] = CFG_TUD_MSC_MAXLUN-1;
usbd_control_xfer_st(rhport, p_request->bmRequestType_bit.direction, _usbd_ctrl_buf, 1);
}else
{
dcd_control_stall(rhport); // stall unsupported request

View File

@ -43,6 +43,48 @@
#include "device/usbd.h"
#include "msc.h"
//--------------------------------------------------------------------+
// Class Driver Configuration
//--------------------------------------------------------------------+
VERIFY_STATIC(CFG_TUD_MSC_BUFSIZE < UINT16_MAX, "Size is not correct");
#ifndef CFG_TUD_MSC_MAXLUN
#define CFG_TUD_MSC_MAXLUN 1
#elif CFG_TUD_MSC_MAXLUN == 0 || CFG_TUD_MSC_MAXLUN > 16
#error MSC Device: Incorrect setting of MAX LUN
#endif
#ifndef CFG_TUD_MSC_BLOCK_NUM
#error CFG_TUD_MSC_BLOCK_NUM must be defined
#endif
#ifndef CFG_TUD_MSC_BLOCK_SZ
#error CFG_TUD_MSC_BLOCK_SZ must be defined
#endif
#ifndef CFG_TUD_MSC_BUFSIZE
#error CFG_TUD_MSC_BUFSIZE must be defined, value of CFG_TUD_MSC_BLOCK_SZ should work well, the more the better
#endif
#ifndef CFG_TUD_MSC_VENDOR
#error CFG_TUD_MSC_VENDOR 8-byte name must be defined
#endif
#ifndef CFG_TUD_MSC_PRODUCT
#error CFG_TUD_MSC_PRODUCT 16-byte name must be defined
#endif
#ifndef CFG_TUD_MSC_PRODUCT_REV
#error CFG_TUD_MSC_PRODUCT_REV 4-byte string must be defined
#endif
// TODO highspeed device is 512
#ifndef CFG_TUD_MSC_EPSIZE
#define CFG_TUD_MSC_EPSIZE 64
#endif
#ifdef __cplusplus
extern "C" {
#endif
@ -52,11 +94,6 @@
* \defgroup MSC_Device Device
* @{ */
//--------------------------------------------------------------------+
// APPLICATION API (Multiple Root Hub Ports)
// Should be used only with MCU that support more than 1 ports
//--------------------------------------------------------------------+
//--------------------------------------------------------------------+
// APPLICATION CALLBACK API (WEAK is optional)
//--------------------------------------------------------------------+

View File

@ -334,7 +334,8 @@ static tusb_error_t proc_control_request_st(uint8_t rhport, tusb_control_request
if ( len )
{
STASK_ASSERT( len <= CFG_TUD_CTRL_BUFSIZE );
usbd_control_xfer_st(rhport, p_request->bmRequestType_bit.direction, buffer, len );
memcpy(_usbd_ctrl_buf, buffer, len);
usbd_control_xfer_st(rhport, p_request->bmRequestType_bit.direction, _usbd_ctrl_buf, len);
}else
{
dcd_control_stall(rhport); // stall unsupported descriptor
@ -342,7 +343,8 @@ static tusb_error_t proc_control_request_st(uint8_t rhport, tusb_control_request
}
else if (TUSB_REQ_GET_CONFIGURATION == p_request->bRequest )
{
usbd_control_xfer_st(rhport, p_request->bmRequestType_bit.direction, &_usbd_dev.config_num, 1);
memcpy(_usbd_ctrl_buf, &_usbd_dev.config_num, 1);
usbd_control_xfer_st(rhport, p_request->bmRequestType_bit.direction, _usbd_ctrl_buf, 1);
}
else if ( TUSB_REQ_SET_ADDRESS == p_request->bRequest )
{

View File

@ -88,12 +88,10 @@
#define EP_CDC_OUT _EP_OUT(ITF_NUM_CDC+2)
#define EP_CDC_IN _EP_IN (ITF_NUM_CDC+2)
#define EP_CDC_SIZE 64
// Mass Storage
#define EP_MSC_OUT _EP_OUT(ITF_NUM_MSC+1)
#define EP_MSC_IN _EP_IN (ITF_NUM_MSC+1)
#define EP_MSC_SIZE 64 // TODO usb highspeed is 512
#if 0
// HID Keyboard
@ -323,7 +321,7 @@ desc_auto_cfg_t const _desc_auto_config_struct =
.bDescriptorType = TUSB_DESC_ENDPOINT,
.bEndpointAddress = EP_CDC_OUT,
.bmAttributes = { .xfer = TUSB_XFER_BULK },
.wMaxPacketSize = { .size = EP_CDC_SIZE },
.wMaxPacketSize = { .size = CFG_TUD_CDC_EPSIZE },
.bInterval = 0
},
@ -333,7 +331,7 @@ desc_auto_cfg_t const _desc_auto_config_struct =
.bDescriptorType = TUSB_DESC_ENDPOINT,
.bEndpointAddress = EP_CDC_IN,
.bmAttributes = { .xfer = TUSB_XFER_BULK },
.wMaxPacketSize = { .size = EP_CDC_SIZE },
.wMaxPacketSize = { .size = CFG_TUD_CDC_EPSIZE },
.bInterval = 0
},
},
@ -361,7 +359,7 @@ desc_auto_cfg_t const _desc_auto_config_struct =
.bDescriptorType = TUSB_DESC_ENDPOINT,
.bEndpointAddress = EP_MSC_OUT,
.bmAttributes = { .xfer = TUSB_XFER_BULK },
.wMaxPacketSize = { .size = EP_MSC_SIZE},
.wMaxPacketSize = { .size = CFG_TUD_MSC_EPSIZE},
.bInterval = 1
},
@ -371,7 +369,7 @@ desc_auto_cfg_t const _desc_auto_config_struct =
.bDescriptorType = TUSB_DESC_ENDPOINT,
.bEndpointAddress = EP_MSC_IN,
.bmAttributes = { .xfer = TUSB_XFER_BULK },
.wMaxPacketSize = { .size = EP_MSC_SIZE},
.wMaxPacketSize = { .size = CFG_TUD_MSC_EPSIZE},
.bInterval = 1
}
},

View File

@ -64,11 +64,9 @@ tusb_error_t usbd_open_edpt_pair(uint8_t rhport, tusb_desc_endpoint_t const* p_d
do { \
if (_len) { \
tusb_error_t err; \
if ( _dir ) memcpy(_usbd_ctrl_buf, _buffer, _len); \
dcd_control_xfer(_rhport, _dir, _usbd_ctrl_buf, _len); \
osal_semaphore_wait( _usbd_ctrl_sem, OSAL_TIMEOUT_CONTROL_XFER, &err ); \
STASK_ASSERT_ERR( err ); \
if (!_dir) memcpy((uint8_t*) _buffer, _usbd_ctrl_buf, _len); \
} \
dcd_control_status(_rhport, _dir); \
/* No need to wait for status phase to complete */ \