minor clean up

This commit is contained in:
hathach
2024-09-10 18:13:38 +07:00
parent bbeae09259
commit 7373a02396
3 changed files with 51 additions and 89 deletions

View File

@@ -73,8 +73,7 @@ static uint32_t blink_interval_ms = BLINK_NOT_MOUNTED;
#define URL "example.tinyusb.org/webusb-serial/index.html" #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, .bLength = 3 + sizeof(URL) - 1,
.bDescriptorType = 3, // WEBUSB URL type .bDescriptorType = 3, // WEBUSB URL type
.bScheme = 1, // 0: http, 1: https .bScheme = 1, // 0: http, 1: https
@@ -86,11 +85,9 @@ static bool web_serial_connected = false;
//------------- prototypes -------------// //------------- prototypes -------------//
void led_blinking_task(void); void led_blinking_task(void);
void cdc_task(void); void cdc_task(void);
void webserial_task(void);
/*------------- MAIN -------------*/ /*------------- MAIN -------------*/
int main(void) int main(void) {
{
board_init(); board_init();
// init device stack on configured roothub port // init device stack on configured roothub port
@@ -100,33 +97,28 @@ int main(void)
board_init_after_tusb(); board_init_after_tusb();
} }
while (1) while (1) {
{
tud_task(); // tinyusb device task tud_task(); // tinyusb device task
cdc_task(); cdc_task();
webserial_task();
led_blinking_task(); led_blinking_task();
} }
} }
// send characters to both CDC and WebUSB // 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 // echo to web serial
if ( web_serial_connected ) if (web_serial_connected) {
{
tud_vendor_write(buf, count); tud_vendor_write(buf, count);
tud_vendor_write_flush(); tud_vendor_write_flush();
} }
// echo to cdc // echo to cdc
if ( tud_cdc_connected() ) if (tud_cdc_connected()) {
{ for (uint32_t i = 0; i < count; i++) {
for(uint32_t i=0; i<count; i++)
{
tud_cdc_write_char(buf[i]); tud_cdc_write_char(buf[i]);
if (buf[i] == '\r') {
if ( buf[i] == '\r' ) tud_cdc_write_char('\n'); tud_cdc_write_char('\n');
}
} }
tud_cdc_write_flush(); tud_cdc_write_flush();
} }
@@ -137,29 +129,25 @@ void echo_all(uint8_t buf[], uint32_t count)
//--------------------------------------------------------------------+ //--------------------------------------------------------------------+
// Invoked when device is mounted // Invoked when device is mounted
void tud_mount_cb(void) void tud_mount_cb(void) {
{
blink_interval_ms = BLINK_MOUNTED; blink_interval_ms = BLINK_MOUNTED;
} }
// Invoked when device is unmounted // Invoked when device is unmounted
void tud_umount_cb(void) void tud_umount_cb(void) {
{
blink_interval_ms = BLINK_NOT_MOUNTED; blink_interval_ms = BLINK_NOT_MOUNTED;
} }
// Invoked when usb bus is suspended // Invoked when usb bus is suspended
// remote_wakeup_en : if host allow us to perform remote wakeup // 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 // 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 tud_suspend_cb(bool remote_wakeup_en) {
{
(void)remote_wakeup_en; (void)remote_wakeup_en;
blink_interval_ms = BLINK_SUSPENDED; blink_interval_ms = BLINK_SUSPENDED;
} }
// Invoked when usb bus is resumed // 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; blink_interval_ms = tud_mounted() ? BLINK_MOUNTED : BLINK_NOT_MOUNTED;
} }
@@ -170,31 +158,26 @@ void tud_resume_cb(void)
// Invoked when a control transfer occurred on an interface of this class // 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) // Driver response accordingly to the request and the transfer stage (setup/data/ack)
// return false to stall control endpoint (e.g unsupported request) // 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 // 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) switch (request->bmRequestType_bit.type) {
{
case TUSB_REQ_TYPE_VENDOR: case TUSB_REQ_TYPE_VENDOR:
switch (request->bRequest) switch (request->bRequest) {
{
case VENDOR_REQUEST_WEBUSB: case VENDOR_REQUEST_WEBUSB:
// match vendor request in BOS descriptor // match vendor request in BOS descriptor
// Get landing page url // 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: case VENDOR_REQUEST_MICROSOFT:
if ( request->wIndex == 7 ) if (request->wIndex == 7) {
{
// Get Microsoft OS 2.0 compatible descriptor // Get Microsoft OS 2.0 compatible descriptor
uint16_t total_len; 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); return tud_control_xfer(rhport, request, (void*)(uintptr_t)desc_ms_os_20, total_len);
}else } else {
{
return false; return false;
} }
@@ -203,21 +186,18 @@ bool tud_vendor_control_xfer_cb(uint8_t rhport, uint8_t stage, tusb_control_requ
break; break;
case TUSB_REQ_TYPE_CLASS: 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. // Webserial simulate the CDC_REQUEST_SET_CONTROL_LINE_STATE (0x22) to connect and disconnect.
web_serial_connected = (request->wValue != 0); web_serial_connected = (request->wValue != 0);
// Always lit LED if connected // Always lit LED if connected
if ( web_serial_connected ) if (web_serial_connected) {
{
board_led_write(true); board_led_write(true);
blink_interval_ms = BLINK_ALWAYS_ON; blink_interval_ms = BLINK_ALWAYS_ON;
tud_vendor_write_str("\r\nWebUSB interface connected\r\n"); tud_vendor_write_str("\r\nWebUSB interface connected\r\n");
tud_vendor_write_flush(); tud_vendor_write_flush();
}else } else {
{
blink_interval_ms = BLINK_MOUNTED; blink_interval_ms = BLINK_MOUNTED;
} }
@@ -233,32 +213,24 @@ bool tud_vendor_control_xfer_cb(uint8_t rhport, uint8_t stage, tusb_control_requ
return false; return false;
} }
void webserial_task(void) void tud_vendor_rx_cb(uint8_t itf, uint8_t const* buffer, uint16_t bufsize) {
{ (void) itf;
if ( web_serial_connected )
{
if ( tud_vendor_available() )
{
uint8_t buf[64];
uint32_t count = tud_vendor_read(buf, sizeof(buf));
// echo back to both web serial and cdc echo_all(buffer, bufsize);
echo_all(buf, count);
}
}
}
// 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 // USB CDC
//--------------------------------------------------------------------+ //--------------------------------------------------------------------+
void cdc_task(void) void cdc_task(void) {
{ if (tud_cdc_connected()) {
if ( tud_cdc_connected() )
{
// connected and there are data available // connected and there are data available
if ( tud_cdc_available() ) if (tud_cdc_available()) {
{
uint8_t buf[64]; uint8_t buf[64];
uint32_t count = tud_cdc_read(buf, sizeof(buf)); uint32_t count = tud_cdc_read(buf, sizeof(buf));
@@ -270,29 +242,25 @@ void cdc_task(void)
} }
// Invoked when cdc when line state changed e.g connected/disconnected // 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 tud_cdc_line_state_cb(uint8_t itf, bool dtr, bool rts) {
{
(void)itf; (void)itf;
// connected // connected
if ( dtr && rts ) if (dtr && rts) {
{
// print initial message when connected // print initial message when connected
tud_cdc_write_str("\r\nTinyUSB WebUSB device example\r\n"); tud_cdc_write_str("\r\nTinyUSB WebUSB device example\r\n");
} }
} }
// Invoked when CDC interface received data from host // Invoked when CDC interface received data from host
void tud_cdc_rx_cb(uint8_t itf) void tud_cdc_rx_cb(uint8_t itf) {
{
(void)itf; (void)itf;
} }
//--------------------------------------------------------------------+ //--------------------------------------------------------------------+
// BLINKING TASK // BLINKING TASK
//--------------------------------------------------------------------+ //--------------------------------------------------------------------+
void led_blinking_task(void) void led_blinking_task(void) {
{
static uint32_t start_ms = 0; static uint32_t start_ms = 0;
static bool led_state = false; static bool led_state = false;

View File

@@ -102,7 +102,7 @@
#define CFG_TUD_CDC_TX_BUFSIZE (TUD_OPT_HIGH_SPEED ? 512 : 64) #define CFG_TUD_CDC_TX_BUFSIZE (TUD_OPT_HIGH_SPEED ? 512 : 64)
// Vendor FIFO size of TX and RX // Vendor FIFO size of TX and RX
// If not configured vendor endpoints will not be buffered // If zero: vendor endpoints will not be buffered
#define CFG_TUD_VENDOR_RX_BUFSIZE (TUD_OPT_HIGH_SPEED ? 512 : 64) #define CFG_TUD_VENDOR_RX_BUFSIZE (TUD_OPT_HIGH_SPEED ? 512 : 64)
#define CFG_TUD_VENDOR_TX_BUFSIZE (TUD_OPT_HIGH_SPEED ? 512 : 64) #define CFG_TUD_VENDOR_TX_BUFSIZE (TUD_OPT_HIGH_SPEED ? 512 : 64)

View File

@@ -34,29 +34,24 @@
extern "C" { extern "C" {
#endif #endif
typedef struct TU_ATTR_PACKED typedef struct TU_ATTR_PACKED {
{
volatile uint8_t busy : 1; volatile uint8_t busy : 1;
volatile uint8_t stalled : 1; volatile uint8_t stalled : 1;
volatile uint8_t claimed : 1; volatile uint8_t claimed : 1;
}tu_edpt_state_t; }tu_edpt_state_t;
typedef struct { typedef struct {
struct { uint8_t is_host; // 1: host, 0: device
uint8_t is_host : 1; // host or device
uint8_t is_highspeed: 1; // is highspeed
};
uint8_t ep_addr; uint8_t ep_addr;
uint16_t ep_packetsize; uint16_t ep_packetsize;
uint16_t ep_bufsize; uint16_t ep_bufsize;
// TODO xfer_fifo can skip this buffer uint8_t* ep_buf; // TODO xfer_fifo can skip this buffer
uint8_t* ep_buf;
tu_fifo_t ff; tu_fifo_t ff;
// mutex: read if ep rx, write if e tx // mutex: read if rx, otherwise write
OSAL_MUTEX_DEF(ff_mutexdef); OSAL_MUTEX_DEF(ff_mutexdef);
}tu_edpt_stream_t; }tu_edpt_stream_t;
@@ -92,7 +87,6 @@ bool tu_edpt_stream_init(tu_edpt_stream_t* s, bool is_host, bool is_tx, bool ove
bool tu_edpt_stream_deinit(tu_edpt_stream_t* s); bool tu_edpt_stream_deinit(tu_edpt_stream_t* s);
// Open an stream for an endpoint // Open an stream for an endpoint
// hwid is either device address (host mode) or rhport (device mode)
TU_ATTR_ALWAYS_INLINE static inline TU_ATTR_ALWAYS_INLINE static inline
void tu_edpt_stream_open(tu_edpt_stream_t* s, tusb_desc_endpoint_t const *desc_ep) { void tu_edpt_stream_open(tu_edpt_stream_t* s, tusb_desc_endpoint_t const *desc_ep) {
tu_fifo_clear(&s->ff); tu_fifo_clear(&s->ff);