From a4292e590668c30f78027573f59f1f4abca9dcb8 Mon Sep 17 00:00:00 2001 From: hathach Date: Thu, 12 Jul 2018 23:08:54 +0700 Subject: [PATCH] changing cdc device to support multiple interface (not yet) --- src/class/cdc/cdc_device.c | 62 ++++++++++++++++++-------------------- src/class/cdc/cdc_device.h | 41 ++++++++++++------------- src/device/usbd.c | 3 -- 3 files changed, 50 insertions(+), 56 deletions(-) diff --git a/src/class/cdc/cdc_device.c b/src/class/cdc/cdc_device.c index 585c8c685..c84e67aa8 100644 --- a/src/class/cdc/cdc_device.c +++ b/src/class/cdc/cdc_device.c @@ -51,9 +51,9 @@ //--------------------------------------------------------------------+ // MACRO CONSTANT TYPEDEF //--------------------------------------------------------------------+ -CFG_TUSB_ATTR_USBRAM STATIC_VAR cdc_line_coding_t cdcd_line_coding[CONTROLLER_DEVICE_NUMBER]; - typedef struct { + CFG_TUSB_MEM_ALIGN cdc_line_coding_t line_coding; + uint8_t itf_num; uint8_t ep_notif; uint8_t ep_in; @@ -65,55 +65,53 @@ typedef struct { uint8_t line_state; }cdcd_interface_t; -// TODO multiple rhport - - -CFG_TUSB_ATTR_USBRAM CFG_TUSB_MEM_ALIGN uint8_t _tmp_rx_buf[64]; -CFG_TUSB_ATTR_USBRAM CFG_TUSB_MEM_ALIGN uint8_t _tmp_tx_buf[64]; +//--------------------------------------------------------------------+ +// INTERNAL OBJECT & FUNCTION DECLARATION +//--------------------------------------------------------------------+ +CFG_TUSB_ATTR_USBRAM CFG_TUSB_MEM_ALIGN static uint8_t _tmp_rx_buf[64]; +CFG_TUSB_ATTR_USBRAM CFG_TUSB_MEM_ALIGN static uint8_t _tmp_tx_buf[64]; TU_FIFO_DEF(_rx_ff, CFG_TUD_CDC_RX_BUFSIZE, uint8_t, true); TU_FIFO_DEF(_tx_ff, CFG_TUD_CDC_TX_BUFSIZE, uint8_t, false); -//--------------------------------------------------------------------+ -// INTERNAL OBJECT & FUNCTION DECLARATION -//--------------------------------------------------------------------+ -STATIC_VAR cdcd_interface_t _cdcd_itf[CONTROLLER_DEVICE_NUMBER]; +CFG_TUSB_ATTR_USBRAM +static cdcd_interface_t _cdcd_itf[CFG_TUD_CDC]; //--------------------------------------------------------------------+ // APPLICATION API //--------------------------------------------------------------------+ -bool tud_n_cdc_connected(uint8_t rhport) +bool tud_cdc_n_connected(uint8_t rhport) { // DTR (bit 0) active isconsidered as connected return BIT_TEST_(_cdcd_itf[rhport].line_state, 0); } -uint8_t tud_n_cdc_get_line_state (uint8_t rhport) +uint8_t tud_cdc_n_get_line_state (uint8_t rhport) { return _cdcd_itf[rhport].line_state; } -void tud_n_cdc_get_line_coding (uint8_t rhport, cdc_line_coding_t* coding) +void tud_cdc_n_get_line_coding (uint8_t rhport, cdc_line_coding_t* coding) { - (*coding) = cdcd_line_coding[rhport]; + (*coding) = _cdcd_itf[0].line_coding; } //--------------------------------------------------------------------+ // READ API //--------------------------------------------------------------------+ -uint32_t tud_n_cdc_available(uint8_t rhport) +uint32_t tud_cdc_n_available(uint8_t rhport) { return tu_fifo_count(&_rx_ff); } -int8_t tud_n_cdc_read_char(uint8_t rhport) +int8_t tud_cdc_n_read_char(uint8_t rhport) { int8_t ch; return tu_fifo_read(&_rx_ff, &ch) ? ch : (-1); } -uint32_t tud_n_cdc_read(uint8_t rhport, void* buffer, uint32_t bufsize) +uint32_t tud_cdc_n_read(uint8_t rhport, void* buffer, uint32_t bufsize) { return tu_fifo_read_n(&_rx_ff, buffer, bufsize); } @@ -122,24 +120,24 @@ uint32_t tud_n_cdc_read(uint8_t rhport, void* buffer, uint32_t bufsize) // WRITE API //--------------------------------------------------------------------+ -uint32_t tud_n_cdc_write_char(uint8_t rhport, char ch) +uint32_t tud_cdc_n_write_char(uint8_t rhport, char ch) { return tu_fifo_write(&_tx_ff, &ch) ? 1 : 0; } -uint32_t tud_n_cdc_write(uint8_t rhport, void const* buffer, uint32_t bufsize) +uint32_t tud_cdc_n_write(uint8_t rhport, void const* buffer, uint32_t bufsize) { return tu_fifo_write_n(&_tx_ff, buffer, bufsize); } -bool tud_n_cdc_flush (uint8_t rhport) +bool tud_cdc_n_flush (uint8_t rhport) { uint8_t edpt = _cdcd_itf[rhport].ep_in; VERIFY( !dcd_edpt_busy(rhport, edpt) ); // skip if previous transfer not complete uint16_t count = tu_fifo_read_n(&_tx_ff, _tmp_tx_buf, sizeof(_tmp_tx_buf)); - VERIFY( tud_n_cdc_connected(rhport) ); // fifo is empty if not connected + VERIFY( tud_cdc_n_connected(rhport) ); // fifo is empty if not connected if ( count ) TU_ASSERT( dcd_edpt_xfer(rhport, edpt, _tmp_tx_buf, count) ); @@ -152,16 +150,15 @@ bool tud_n_cdc_flush (uint8_t rhport) //--------------------------------------------------------------------+ void cdcd_init(void) { - memclr_(_cdcd_itf, sizeof(cdcd_interface_t)*CONTROLLER_DEVICE_NUMBER); + arrclr_(_cdcd_itf); // default line coding is : stop bit = 1, parity = none, data bits = 8 - memclr_(cdcd_line_coding, sizeof(cdc_line_coding_t)*CONTROLLER_DEVICE_NUMBER); - for(uint8_t i=0; ibmRequestType_bit.type != TUSB_REQ_TYPE_CLASS) return TUSB_ERROR_DCD_CONTROL_REQUEST_NOT_SUPPORT; + // TODO Support multiple interface 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, (uint8_t*) &cdcd_line_coding[rhport], len); + usbd_control_xfer_st(rhport, p_request->bmRequestType_bit.direction, (uint8_t*) &_cdcd_itf[0].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(rhport, &cdcd_line_coding[rhport]); + if ( tud_cdc_line_coding_cb ) tud_cdc_line_coding_cb(rhport, &_cdcd_itf[0].line_coding); } } else if (CDC_REQUEST_SET_CONTROL_LINE_STATE == p_request->bRequest ) @@ -297,7 +295,7 @@ tusb_error_t cdcd_xfer_cb(uint8_t rhport, uint8_t ep_addr, tusb_event_t event, u #if CFG_TUD_CDC_FLUSH_ON_SOF void cdcd_sof(uint8_t rhport) { - tud_n_cdc_flush(rhport); + tud_cdc_n_flush(rhport); } #endif diff --git a/src/class/cdc/cdc_device.h b/src/class/cdc/cdc_device.h index c1a790fc8..8b098b579 100644 --- a/src/class/cdc/cdc_device.h +++ b/src/class/cdc/cdc_device.h @@ -53,36 +53,35 @@ * @{ */ //--------------------------------------------------------------------+ -// APPLICATION API (Multiple Root Ports) -// Should be used only with MCU that support more than 1 ports +// APPLICATION API (Multiple Interfaces) +// CFG_TUD_CDC > 1 //--------------------------------------------------------------------+ -bool tud_n_cdc_connected (uint8_t rhport); -uint8_t tud_n_cdc_get_line_state (uint8_t rhport); -void tud_n_cdc_get_line_coding (uint8_t rhport, cdc_line_coding_t* coding); +bool tud_cdc_n_connected (uint8_t rhport); +uint8_t tud_cdc_n_get_line_state (uint8_t rhport); +void tud_cdc_n_get_line_coding (uint8_t rhport, cdc_line_coding_t* coding); -uint32_t tud_n_cdc_available (uint8_t rhport); -int8_t tud_n_cdc_read_char (uint8_t rhport); -uint32_t tud_n_cdc_read (uint8_t rhport, void* buffer, uint32_t bufsize); +uint32_t tud_cdc_n_available (uint8_t rhport); +int8_t tud_cdc_n_read_char (uint8_t rhport); +uint32_t tud_cdc_n_read (uint8_t rhport, void* buffer, uint32_t bufsize); -uint32_t tud_n_cdc_write_char (uint8_t rhport, char ch); -uint32_t tud_n_cdc_write (uint8_t rhport, void const* buffer, uint32_t bufsize); -bool tud_n_cdc_flush (uint8_t rhport); +uint32_t tud_cdc_n_write_char (uint8_t rhport, char ch); +uint32_t tud_cdc_n_write (uint8_t rhport, void const* buffer, uint32_t bufsize); +bool tud_cdc_n_flush (uint8_t rhport); //--------------------------------------------------------------------+ -// APPLICATION API (Single Port) -// Should be used with MCU supporting only 1 USB port for code simplicity +// APPLICATION API (Interface0) //--------------------------------------------------------------------+ -static inline bool tud_cdc_connected (void) { return tud_n_cdc_connected(0); } -static inline uint8_t tud_cdc_get_line_state (uint8_t rhport) { return tud_n_cdc_get_line_state(0); } +static inline bool tud_cdc_connected (void) { return tud_cdc_n_connected(0); } +static inline uint8_t tud_cdc_get_line_state (uint8_t rhport) { return tud_cdc_n_get_line_state(0); } static inline void tud_cdc_get_line_coding (uint8_t rhport, cdc_line_coding_t* coding) { return tud_cdc_get_line_coding(0, coding); } -static inline uint32_t tud_cdc_available (void) { return tud_n_cdc_available(0); } -static inline int8_t tud_cdc_read_char (void) { return tud_n_cdc_read_char(0); } -static inline uint32_t tud_cdc_read (void* buffer, uint32_t bufsize) { return tud_n_cdc_read(0, buffer, bufsize); } +static inline uint32_t tud_cdc_available (void) { return tud_cdc_n_available(0); } +static inline int8_t tud_cdc_read_char (void) { return tud_cdc_n_read_char(0); } +static inline uint32_t tud_cdc_read (void* buffer, uint32_t bufsize) { return tud_cdc_n_read(0, buffer, bufsize); } -static inline uint32_t tud_cdc_write_char (char ch) { return tud_n_cdc_write_char(0, ch); } -static inline uint32_t tud_cdc_write (void const* buffer, uint32_t bufsize) { return tud_n_cdc_write(0, buffer, bufsize); } -static inline bool tud_cdc_flush (void) { return tud_n_cdc_flush(0); } +static inline uint32_t tud_cdc_write_char (char ch) { return tud_cdc_n_write_char(0, ch); } +static inline uint32_t tud_cdc_write (void const* buffer, uint32_t bufsize) { return tud_cdc_n_write(0, buffer, bufsize); } +static inline bool tud_cdc_flush (void) { return tud_cdc_n_flush(0); } //--------------------------------------------------------------------+ // APPLICATION CALLBACK API (WEAK is optional) diff --git a/src/device/usbd.c b/src/device/usbd.c index 5c3e889a7..b1649ab4b 100644 --- a/src/device/usbd.c +++ b/src/device/usbd.c @@ -516,9 +516,6 @@ static uint16_t get_descriptor(uint8_t rhport, tusb_control_request_t const * co return len; } -//--------------------------------------------------------------------+ -// USBD-CLASS API -//--------------------------------------------------------------------+ //--------------------------------------------------------------------+ // USBD-DCD Callback API