summaryrefslogtreecommitdiff
path: root/examples/device/webusb_serial/src/main.c
diff options
context:
space:
mode:
Diffstat (limited to 'examples/device/webusb_serial/src/main.c')
-rw-r--r--examples/device/webusb_serial/src/main.c130
1 files changed, 51 insertions, 79 deletions
diff --git a/examples/device/webusb_serial/src/main.c b/examples/device/webusb_serial/src/main.c
index 800d435b8..d189af91f 100644
--- a/examples/device/webusb_serial/src/main.c
+++ b/examples/device/webusb_serial/src/main.c
@@ -73,8 +73,7 @@ static uint32_t blink_interval_ms = BLINK_NOT_MOUNTED;
#define URL "example.tinyusb.org/webusb-serial/index.html"
-const tusb_desc_webusb_url_t desc_url =
-{
+const tusb_desc_webusb_url_t desc_url = {
.bLength = 3 + sizeof(URL) - 1,
.bDescriptorType = 3, // WEBUSB URL type
.bScheme = 1, // 0: http, 1: https
@@ -86,47 +85,44 @@ static bool web_serial_connected = false;
//------------- prototypes -------------//
void led_blinking_task(void);
void cdc_task(void);
-void webserial_task(void);
/*------------- MAIN -------------*/
-int main(void)
-{
+int main(void) {
board_init();
// init device stack on configured roothub port
- tud_init(BOARD_TUD_RHPORT);
+ tusb_rhport_init_t dev_init = {
+ .role = TUSB_ROLE_DEVICE,
+ .speed = TUSB_SPEED_AUTO
+ };
+ tusb_init(BOARD_TUD_RHPORT, &dev_init);
if (board_init_after_tusb) {
board_init_after_tusb();
}
- while (1)
- {
+ while (1) {
tud_task(); // tinyusb device task
cdc_task();
- webserial_task();
led_blinking_task();
}
}
// send characters to both CDC and WebUSB
-void echo_all(uint8_t buf[], uint32_t count)
-{
+void echo_all(const uint8_t buf[], uint32_t count) {
// echo to web serial
- if ( web_serial_connected )
- {
+ if (web_serial_connected) {
tud_vendor_write(buf, count);
tud_vendor_write_flush();
}
// echo to cdc
- if ( tud_cdc_connected() )
- {
- for(uint32_t i=0; i<count; i++)
- {
+ 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');
+ if (buf[i] == '\r') {
+ tud_cdc_write_char('\n');
+ }
}
tud_cdc_write_flush();
}
@@ -137,29 +133,25 @@ void echo_all(uint8_t buf[], uint32_t count)
//--------------------------------------------------------------------+
// Invoked when device is mounted
-void tud_mount_cb(void)
-{
+void tud_mount_cb(void) {
blink_interval_ms = BLINK_MOUNTED;
}
// Invoked when device is unmounted
-void tud_umount_cb(void)
-{
+void tud_umount_cb(void) {
blink_interval_ms = BLINK_NOT_MOUNTED;
}
// Invoked when usb bus is suspended
// remote_wakeup_en : if host allow us to perform remote wakeup
// Within 7ms, device must draw an average of current less than 2.5 mA from bus
-void tud_suspend_cb(bool remote_wakeup_en)
-{
- (void) remote_wakeup_en;
+void tud_suspend_cb(bool remote_wakeup_en) {
+ (void)remote_wakeup_en;
blink_interval_ms = BLINK_SUSPENDED;
}
// Invoked when usb bus is resumed
-void tud_resume_cb(void)
-{
+void tud_resume_cb(void) {
blink_interval_ms = tud_mounted() ? BLINK_MOUNTED : BLINK_NOT_MOUNTED;
}
@@ -170,61 +162,53 @@ void tud_resume_cb(void)
// Invoked when a control transfer occurred on an interface of this class
// Driver response accordingly to the request and the transfer stage (setup/data/ack)
// 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)
-{
+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;
- switch (request->bmRequestType_bit.type)
- {
+ switch (request->bmRequestType_bit.type) {
case TUSB_REQ_TYPE_VENDOR:
- switch (request->bRequest)
- {
+ switch (request->bRequest) {
case VENDOR_REQUEST_WEBUSB:
// match vendor request in BOS descriptor
// Get landing page url
- return tud_control_xfer(rhport, request, (void*)(uintptr_t) &desc_url, desc_url.bLength);
+ return tud_control_xfer(rhport, request, (void*)(uintptr_t)&desc_url, desc_url.bLength);
case VENDOR_REQUEST_MICROSOFT:
- if ( request->wIndex == 7 )
- {
+ if (request->wIndex == 7) {
// Get Microsoft OS 2.0 compatible descriptor
uint16_t total_len;
- memcpy(&total_len, desc_ms_os_20+8, 2);
+ memcpy(&total_len, desc_ms_os_20 + 8, 2);
- return tud_control_xfer(rhport, request, (void*)(uintptr_t) desc_ms_os_20, total_len);
- }else
- {
+ return tud_control_xfer(rhport, request, (void*)(uintptr_t)desc_ms_os_20, total_len);
+ } else {
return false;
}
default: break;
}
- break;
+ break;
case TUSB_REQ_TYPE_CLASS:
- if (request->bRequest == 0x22)
- {
+ if (request->bRequest == 0x22) {
// Webserial simulate the CDC_REQUEST_SET_CONTROL_LINE_STATE (0x22) to connect and disconnect.
web_serial_connected = (request->wValue != 0);
// Always lit LED if connected
- if ( web_serial_connected )
- {
+ if (web_serial_connected) {
board_led_write(true);
blink_interval_ms = BLINK_ALWAYS_ON;
tud_vendor_write_str("\r\nWebUSB interface connected\r\n");
tud_vendor_write_flush();
- }else
- {
+ } else {
blink_interval_ms = BLINK_MOUNTED;
}
// response with status OK
return tud_control_status(rhport, request);
}
- break;
+ break;
default: break;
}
@@ -233,32 +217,24 @@ bool tud_vendor_control_xfer_cb(uint8_t rhport, uint8_t stage, tusb_control_requ
return false;
}
-void webserial_task(void)
-{
- if ( web_serial_connected )
- {
- if ( tud_vendor_available() )
- {
- uint8_t buf[64];
- uint32_t count = tud_vendor_read(buf, sizeof(buf));
+void tud_vendor_rx_cb(uint8_t itf, uint8_t const* buffer, uint16_t bufsize) {
+ (void) itf;
- // echo back to both web serial and cdc
- echo_all(buf, count);
- }
- }
-}
+ 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
+}
//--------------------------------------------------------------------+
// USB CDC
//--------------------------------------------------------------------+
-void cdc_task(void)
-{
- if ( tud_cdc_connected() )
- {
+void cdc_task(void) {
+ if (tud_cdc_connected()) {
// connected and there are data available
- if ( tud_cdc_available() )
- {
+ if (tud_cdc_available()) {
uint8_t buf[64];
uint32_t count = tud_cdc_read(buf, sizeof(buf));
@@ -270,34 +246,30 @@ void cdc_task(void)
}
// Invoked when cdc when line state changed e.g connected/disconnected
-void tud_cdc_line_state_cb(uint8_t itf, bool dtr, bool rts)
-{
- (void) itf;
+void tud_cdc_line_state_cb(uint8_t itf, bool dtr, bool rts) {
+ (void)itf;
// connected
- if ( dtr && rts )
- {
+ if (dtr && rts) {
// print initial message when connected
tud_cdc_write_str("\r\nTinyUSB WebUSB device example\r\n");
}
}
// 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 itf) {
+ (void)itf;
}
//--------------------------------------------------------------------+
// BLINKING TASK
//--------------------------------------------------------------------+
-void led_blinking_task(void)
-{
+void led_blinking_task(void) {
static uint32_t start_ms = 0;
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);