From 798ce59ebd46118c7d64e9cfa54319787715ef7d Mon Sep 17 00:00:00 2001 From: hathach Date: Tue, 17 Jul 2018 16:04:55 +0700 Subject: 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 --- src/class/cdc/cdc_device.c | 91 +++++++++++++++++++++++----------------------- src/class/cdc/cdc_device.h | 8 ++++ src/class/msc/msc_device.c | 39 +------------------- src/class/msc/msc_device.h | 47 +++++++++++++++++++++--- 4 files changed, 97 insertions(+), 88 deletions(-) (limited to 'src/class') diff --git a/src/class/cdc/cdc_device.c b/src/class/cdc/cdc_device.c index bcef47136..6d5ad4e85 100644 --- a/src/class/cdc/cdc_device.c +++ b/src/class/cdc/cdc_device.c @@ -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; iep_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; iepin_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 diff --git a/src/class/cdc/cdc_device.h b/src/class/cdc/cdc_device.h index 0a1b2ca8f..046878680 100644 --- a/src/class/cdc/cdc_device.h +++ b/src/class/cdc/cdc_device.h @@ -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 diff --git a/src/class/msc/msc_device.c b/src/class/msc/msc_device.c index d82958202..b0839ecb0 100644 --- a/src/class/msc/msc_device.c +++ b/src/class/msc/msc_device.c @@ -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 diff --git a/src/class/msc/msc_device.h b/src/class/msc/msc_device.h index 994e71c36..a32d64a93 100644 --- a/src/class/msc/msc_device.h +++ b/src/class/msc/msc_device.h @@ -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) //--------------------------------------------------------------------+ -- cgit v1.3.1