Skip to content
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions examples/device/cdc_msc/src/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,7 @@ void cdc_task(void) {

if ((btn_prev == 0u) && (btn != 0u)) {
uart_state.dsr ^= 1;
uart_state.dcd ^= 1;
tud_cdc_notify_uart_state(&uart_state);
}
btn_prev = btn;
Expand Down
57 changes: 24 additions & 33 deletions examples/device/webusb_serial/src/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ int main(void) {

while (1) {
tud_task(); // tinyusb device task
cdc_task();
tud_cdc_write_flush();
led_blinking_task();
}
}
Expand All @@ -116,13 +116,7 @@ static void echo_all(const uint8_t buf[], uint32_t count) {

// echo to cdc
if (tud_cdc_connected()) {
for (uint32_t i = 0; i < count; i++) {
tud_cdc_write_char(buf[i]);
if (buf[i] == '\r') {
tud_cdc_write_char('\n');
}
}
tud_cdc_write_flush();
tud_cdc_write(buf, count);
}
}

Expand Down Expand Up @@ -162,7 +156,9 @@ void tud_resume_cb(void) {
// return false to stall control endpoint (e.g unsupported request)
bool tud_vendor_control_xfer_cb(uint8_t rhport, uint8_t stage, tusb_control_request_t const* request) {
// nothing to with DATA & ACK stage
if (stage != CONTROL_STAGE_SETUP) return true;
if (stage != CONTROL_STAGE_SETUP) {
return true;
}

switch (request->bmRequestType_bit.type) {
case TUSB_REQ_TYPE_VENDOR:
Expand Down Expand Up @@ -215,33 +211,21 @@ bool tud_vendor_control_xfer_cb(uint8_t rhport, uint8_t stage, tusb_control_requ
return false;
}

void tud_vendor_rx_cb(uint8_t itf, uint8_t const* buffer, uint16_t bufsize) {
(void) itf;
void tud_vendor_rx_cb(uint8_t idx, const uint8_t *buffer, uint32_t bufsize) {
(void)idx;
(void)buffer;
(void)bufsize;

echo_all(buffer, bufsize);

// if using RX buffered is enabled, we need to flush the buffer to make room for new data
#if CFG_TUD_VENDOR_RX_BUFSIZE > 0
tud_vendor_read_flush();
#endif
while (tud_vendor_available()) {
uint8_t buf[64];
const uint32_t count = tud_vendor_read(buf, sizeof(buf));
echo_all(buf, count);
}
}

//--------------------------------------------------------------------+
// USB CDC
//--------------------------------------------------------------------+
void cdc_task(void) {
if (tud_cdc_connected()) {
// connected and there are data available
if (tud_cdc_available()) {
uint8_t buf[64];

uint32_t count = tud_cdc_read(buf, sizeof(buf));

// echo back to both web serial and cdc
echo_all(buf, count);
}
}
}

// Invoked when cdc when line state changed e.g connected/disconnected
void tud_cdc_line_state_cb(uint8_t itf, bool dtr, bool rts) {
Expand All @@ -255,8 +239,13 @@ void tud_cdc_line_state_cb(uint8_t itf, bool dtr, bool rts) {
}

// Invoked when CDC interface received data from host
void tud_cdc_rx_cb(uint8_t itf) {
(void)itf;
void tud_cdc_rx_cb(uint8_t idx) {
(void)idx;
while (tud_cdc_available()) {
uint8_t buf[64];
const uint32_t count = tud_cdc_read(buf, sizeof(buf));
echo_all(buf, count); // echo back to both web serial and cdc
}
}

//--------------------------------------------------------------------+
Expand All @@ -267,7 +256,9 @@ void led_blinking_task(void) {
static bool led_state = false;

// Blink every interval ms
if (board_millis() - start_ms < blink_interval_ms) return; // not enough time
if (board_millis() - start_ms < blink_interval_ms) {
return; // not enough time
}
start_ms += blink_interval_ms;

board_led_write(led_state);
Expand Down
11 changes: 4 additions & 7 deletions src/class/cdc/cdc_device.c
Original file line number Diff line number Diff line change
Expand Up @@ -266,9 +266,8 @@ void cdcd_init(void) {
uint8_t *epout_buf = NULL;
uint8_t *epin_buf = NULL;
#else
cdcd_epbuf_t *p_epbuf = &_cdcd_epbuf[i];
uint8_t *epout_buf = p_epbuf->epout;
uint8_t *epin_buf = p_epbuf->epin;
uint8_t *epout_buf = _cdcd_epbuf[i].epout;
uint8_t *epin_buf = _cdcd_epbuf[i].epin;
#endif

tu_edpt_stream_init(&p_cdc->stream.rx, false, false, false, p_cdc->stream.rx_ff_buf, CFG_TUD_CDC_RX_BUFSIZE,
Expand Down Expand Up @@ -516,11 +515,9 @@ bool cdcd_xfer_cb(uint8_t rhport, uint8_t ep_addr, xfer_result_t result, uint32_
}

// Data sent to host, we continue to fetch from tx fifo to send.
// Note: This will cause incorrect baudrate set in line coding.
// Though maybe the baudrate is not really important !!!
// Note: This will cause incorrect baudrate set in line coding. Though maybe the baudrate is not really important !
if (ep_addr == stream_tx->ep_addr) {
// invoke transmit callback to possibly refill tx fifo
tud_cdc_tx_complete_cb(itf);
tud_cdc_tx_complete_cb(itf); // invoke callback to possibly refill tx fifo

if (0 == tu_edpt_stream_write_xfer(rhport, stream_tx)) {
// If there is no data left, a ZLP should be sent if needed
Expand Down
Loading