From 19b6bbfd14778c5a1fd13d2c0665dfbaa49c9425 Mon Sep 17 00:00:00 2001 From: hathach Date: Sat, 14 Jul 2018 23:28:07 +0700 Subject: add device cdc wanted char callback, cdc peek --- src/class/cdc/cdc_device.c | 53 +++++++++++++++++++++++++++++++++++----------- src/class/cdc/cdc_device.h | 3 +++ 2 files changed, 44 insertions(+), 12 deletions(-) (limited to 'src/class') diff --git a/src/class/cdc/cdc_device.c b/src/class/cdc/cdc_device.c index d0c76d00f..04402c5bb 100644 --- a/src/class/cdc/cdc_device.c +++ b/src/class/cdc/cdc_device.c @@ -79,8 +79,10 @@ typedef struct //--------------------------------------------------------------------+ // 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]; + +// 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]; @@ -105,7 +107,7 @@ void tud_cdc_n_get_line_coding (uint8_t itf, cdc_line_coding_t* coding) void tud_cdc_n_set_wanted_char (uint8_t itf, char wanted) { - + _cdcd_itf[itf].intact.wanted_char = wanted; } @@ -128,6 +130,12 @@ 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); } +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); +} + //--------------------------------------------------------------------+ // WRITE API //--------------------------------------------------------------------+ @@ -147,11 +155,11 @@ bool tud_cdc_n_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 - uint16_t count = tu_fifo_read_n(&_cdcd_itf[itf].intact.tx_ff, _tmp_tx_buf, sizeof(_tmp_tx_buf)); + uint16_t count = tu_fifo_read_n(&_cdcd_itf[itf].intact.tx_ff, _tx_buf, sizeof(_tx_buf)); VERIFY( tud_cdc_n_connected(itf) ); // fifo is empty if not connected - if ( count ) TU_ASSERT( dcd_edpt_xfer(TUD_RHPORT, edpt, _tmp_tx_buf, count) ); + if ( count ) TU_ASSERT( dcd_edpt_xfer(TUD_RHPORT, edpt, _tx_buf, count) ); return true; } @@ -166,6 +174,15 @@ void cdcd_init(void) for(uint8_t i=0; iep_out, _tmp_rx_buf, sizeof(_tmp_rx_buf)), TUSB_ERROR_DCD_EDPT_XFER); + TU_ASSERT( dcd_edpt_xfer(rhport, p_cdc->ep_out, _rx_buf, sizeof(_rx_buf)), TUSB_ERROR_DCD_EDPT_XFER); return TUSB_ERROR_NONE; } @@ -297,18 +314,30 @@ tusb_error_t cdcd_xfer_cb(uint8_t rhport, uint8_t ep_addr, tusb_event_t event, u { // TODO Support multiple interfaces uint8_t const itf = 0; - cdcd_interface_t const * p_cdc = &_cdcd_itf[itf]; + cdcd_interface_t* p_cdc = &_cdcd_itf[itf]; // receive new data if ( ep_addr == p_cdc->ep_out ) { - tu_fifo_write_n(&_cdcd_itf[itf].intact.rx_ff, _tmp_rx_buf, xferred_bytes); + char const wanted = p_cdc->intact.wanted_char; + + for(uint32_t i=0; iintact.rx_ff, &_rx_buf[i]); + } + } - // preparing for next - TU_ASSERT( dcd_edpt_xfer(rhport, p_cdc->ep_out, _tmp_rx_buf, sizeof(_tmp_rx_buf)), TUSB_ERROR_DCD_EDPT_XFER ); + // 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); - // fire callback - if (tud_cdc_rx_cb) 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 ); } // 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 2ff822240..c815a42b6 100644 --- a/src/class/cdc/cdc_device.h +++ b/src/class/cdc/cdc_device.h @@ -64,6 +64,7 @@ void tud_cdc_n_set_wanted_char (uint8_t itf, char wanted); uint32_t tud_cdc_n_available (uint8_t itf); char tud_cdc_n_read_char (uint8_t itf); uint32_t tud_cdc_n_read (uint8_t itf, void* buffer, uint32_t bufsize); +char tud_cdc_n_peek (uint8_t itf, int pos); uint32_t tud_cdc_n_write_char (uint8_t itf, char ch); uint32_t tud_cdc_n_write (uint8_t itf, void const* buffer, uint32_t bufsize); @@ -80,6 +81,7 @@ static inline void tud_cdc_set_wanted_char (char wanted) static inline uint32_t tud_cdc_available (void) { return tud_cdc_n_available(0); } static inline char 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 char tud_cdc_peek (int pos) { return tud_cdc_n_peek(0, pos); } 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); } @@ -89,6 +91,7 @@ static inline bool tud_cdc_flush (void) // APPLICATION CALLBACK API (WEAK is optional) //--------------------------------------------------------------------+ ATTR_WEAK void tud_cdc_rx_cb(uint8_t itf); +ATTR_WEAK void tud_cdc_rx_wanted_cb(uint8_t itf, char wanted_char); ATTR_WEAK void tud_cdc_line_state_cb(uint8_t itf, bool dtr, bool rts); ATTR_WEAK void tud_cdc_line_coding_cb(uint8_t itf, cdc_line_coding_t const* p_line_coding); -- cgit v1.3.1