summaryrefslogtreecommitdiff
path: root/examples
diff options
context:
space:
mode:
authorhathach <[email protected]>2026-03-11 16:20:14 +0700
committerhathach <[email protected]>2026-03-11 16:20:14 +0700
commitd13f600721671a603579350be1e9c935a0d40547 (patch)
treefbb9402972c3a2925efc554b94e79855d7364dce /examples
parent5efa72cd57325615efae6a26ff8c9fe8a9c7b3b5 (diff)
parentf2450788b13f8424b2f75e33240ed21329a875ad (diff)
Merge branch 'master' into zlp-hs-on-fs
Diffstat (limited to 'examples')
-rw-r--r--examples/device/CMakeLists.txt1
-rw-r--r--examples/device/audio_4_channel_mic/src/main.c4
-rw-r--r--examples/device/audio_4_channel_mic_freertos/skip.txt1
-rw-r--r--examples/device/audio_test/src/main.c4
-rw-r--r--examples/device/audio_test_freertos/skip.txt1
-rw-r--r--examples/device/audio_test_multi_rate/src/main.c4
-rw-r--r--examples/device/board_test/src/main.c122
-rw-r--r--examples/device/cdc_dual_ports/src/main.c2
-rw-r--r--examples/device/cdc_dual_ports/src/tusb_config.h2
-rw-r--r--examples/device/cdc_msc/src/main.c2
-rw-r--r--examples/device/cdc_msc/src/msc_disk.c2
-rw-r--r--examples/device/cdc_msc/src/tusb_config.h2
-rw-r--r--examples/device/cdc_msc_freertos/skip.txt1
-rw-r--r--examples/device/cdc_msc_freertos/src/msc_disk.c15
-rw-r--r--examples/device/cdc_msc_freertos/src/tusb_config.h2
-rw-r--r--examples/device/cdc_uac2/src/main.c2
-rw-r--r--examples/device/cdc_uac2/src/tusb_config.h2
-rw-r--r--examples/device/cdc_uac2/src/uac2_app.c4
-rw-r--r--examples/device/dfu/src/main.c2
-rw-r--r--examples/device/dfu_runtime/src/main.c2
-rw-r--r--examples/device/dynamic_configuration/src/main.c4
-rw-r--r--examples/device/dynamic_configuration/src/msc_disk.c15
-rw-r--r--examples/device/hid_boot_interface/src/main.c6
-rw-r--r--examples/device/hid_composite/src/main.c4
-rw-r--r--examples/device/hid_composite_freertos/skip.txt1
-rw-r--r--examples/device/hid_generic_inout/src/main.c2
-rw-r--r--examples/device/hid_multiple_interface/src/main.c4
-rw-r--r--examples/device/midi_test/src/main.c4
-rw-r--r--examples/device/midi_test_freertos/skip.txt1
-rw-r--r--examples/device/msc_dual_lun/src/main.c158
-rw-r--r--examples/device/mtp/src/main.c2
-rw-r--r--examples/device/net_lwip_webserver/src/main.c4
-rw-r--r--examples/device/printer_to_cdc/CMakeLists.txt29
-rw-r--r--examples/device/printer_to_cdc/Makefile11
-rw-r--r--examples/device/printer_to_cdc/README.md80
-rw-r--r--examples/device/printer_to_cdc/src/main.c117
-rw-r--r--examples/device/printer_to_cdc/src/tusb_config.h117
-rw-r--r--examples/device/printer_to_cdc/src/usb_descriptors.c208
-rw-r--r--examples/device/printer_to_cdc/src/usb_descriptors.h37
-rw-r--r--examples/device/uac2_headset/src/main.c6
-rw-r--r--examples/device/uac2_speaker_fb/src/main.c12
-rw-r--r--examples/device/usbtmc/src/main.c6
-rw-r--r--examples/device/usbtmc/src/usbtmc_app.c8
-rw-r--r--examples/device/video_capture/src/main.c6
-rw-r--r--examples/device/video_capture_2ch/src/main.c6
-rw-r--r--examples/device/webusb_serial/src/main.c2
-rw-r--r--examples/dual/CMakeLists.txt1
-rw-r--r--examples/dual/dynamic_switch/CMakeLists.txt30
-rw-r--r--examples/dual/dynamic_switch/CMakePresets.json6
-rw-r--r--examples/dual/dynamic_switch/Makefile16
-rw-r--r--examples/dual/dynamic_switch/README.md60
-rw-r--r--examples/dual/dynamic_switch/only.txt12
-rw-r--r--examples/dual/dynamic_switch/src/CMakeLists.txt4
-rw-r--r--examples/dual/dynamic_switch/src/main.c496
-rw-r--r--examples/dual/dynamic_switch/src/tusb_config.h158
-rw-r--r--examples/dual/dynamic_switch/src/usb_descriptors.c199
-rw-r--r--examples/dual/host_hid_to_device_cdc/src/main.c4
-rw-r--r--examples/dual/host_info_to_device_cdc/src/main.c6
-rw-r--r--examples/host/bare_api/only.txt2
-rw-r--r--examples/host/bare_api/skip.txt1
-rw-r--r--examples/host/bare_api/src/main.c26
-rw-r--r--examples/host/cdc_msc_hid/only.txt2
-rw-r--r--examples/host/cdc_msc_hid/skip.txt1
-rw-r--r--examples/host/cdc_msc_hid/src/main.c2
-rw-r--r--examples/host/cdc_msc_hid_freertos/only.txt2
-rw-r--r--examples/host/cdc_msc_hid_freertos/skip.txt1
-rw-r--r--examples/host/device_info/only.txt2
-rw-r--r--examples/host/device_info/skip.txt1
-rw-r--r--examples/host/device_info/src/main.c3
-rw-r--r--examples/host/hid_controller/only.txt2
-rw-r--r--examples/host/hid_controller/skip.txt1
-rw-r--r--examples/host/hid_controller/src/hid_app.c2
-rw-r--r--examples/host/hid_controller/src/main.c2
-rw-r--r--examples/host/midi_rx/only.txt2
-rw-r--r--examples/host/midi_rx/skip.txt1
-rw-r--r--examples/host/midi_rx/src/main.c4
-rw-r--r--examples/host/msc_file_explorer/only.txt2
-rw-r--r--examples/host/msc_file_explorer/skip.txt1
-rw-r--r--examples/host/msc_file_explorer/src/main.c4
-rw-r--r--examples/host/msc_file_explorer/src/msc_app.c472
-rw-r--r--examples/typec/power_delivery/src/main.c2
81 files changed, 2128 insertions, 429 deletions
diff --git a/examples/device/CMakeLists.txt b/examples/device/CMakeLists.txt
index 660df67cb..7173f455e 100644
--- a/examples/device/CMakeLists.txt
+++ b/examples/device/CMakeLists.txt
@@ -30,6 +30,7 @@ set(EXAMPLE_LIST
msc_dual_lun
mtp
net_lwip_webserver
+ printer_to_cdc
uac2_headset
uac2_speaker_fb
usbtmc
diff --git a/examples/device/audio_4_channel_mic/src/main.c b/examples/device/audio_4_channel_mic/src/main.c
index 5767c7453..c9c6dd46c 100644
--- a/examples/device/audio_4_channel_mic/src/main.c
+++ b/examples/device/audio_4_channel_mic/src/main.c
@@ -155,7 +155,7 @@ void tud_resume_cb(void) {
// In a real application, this would be replaced with actual I2S receive callback.
void audio_task(void) {
static uint32_t start_ms = 0;
- uint32_t curr_ms = board_millis();
+ uint32_t curr_ms = tusb_time_millis_api();
if (start_ms == curr_ms) {
return; // not enough time
}
@@ -408,7 +408,7 @@ void led_blinking_task(void) {
static bool led_state = false;
// Blink every interval ms
- if (board_millis() - start_ms < blink_interval_ms) {
+ if (tusb_time_millis_api() - start_ms < blink_interval_ms) {
return; // not enough time
}
start_ms += blink_interval_ms;
diff --git a/examples/device/audio_4_channel_mic_freertos/skip.txt b/examples/device/audio_4_channel_mic_freertos/skip.txt
index 4c2780096..db01347ed 100644
--- a/examples/device/audio_4_channel_mic_freertos/skip.txt
+++ b/examples/device/audio_4_channel_mic_freertos/skip.txt
@@ -9,7 +9,6 @@ mcu:MKL25ZXX
mcu:MSP430x5xx
mcu:RP2040
mcu:SAMD11
-mcu:SAMX7X
mcu:VALENTYUSB_EPTRI
mcu:RAXXX
mcu:STM32L0
diff --git a/examples/device/audio_test/src/main.c b/examples/device/audio_test/src/main.c
index 2441eefbc..876a41d06 100644
--- a/examples/device/audio_test/src/main.c
+++ b/examples/device/audio_test/src/main.c
@@ -138,7 +138,7 @@ void tud_resume_cb(void) {
// In a real application, this would be replaced with actual I2S receive callback.
void audio_task(void) {
static uint32_t start_ms = 0;
- uint32_t curr_ms = board_millis();
+ uint32_t curr_ms = tusb_time_millis_api();
if (start_ms == curr_ms) {
return; // not enough time
}
@@ -402,7 +402,7 @@ void led_blinking_task(void) {
static bool led_state = false;
// Blink every interval ms
- if (board_millis() - start_ms < blink_interval_ms) {
+ if (tusb_time_millis_api() - start_ms < blink_interval_ms) {
return; // not enough time
}
start_ms += blink_interval_ms;
diff --git a/examples/device/audio_test_freertos/skip.txt b/examples/device/audio_test_freertos/skip.txt
index 2da7808e7..7463aa86a 100644
--- a/examples/device/audio_test_freertos/skip.txt
+++ b/examples/device/audio_test_freertos/skip.txt
@@ -9,7 +9,6 @@ mcu:MKL25ZXX
mcu:MSP430x5xx
mcu:RP2040
mcu:SAMD11
-mcu:SAMX7X
mcu:VALENTYUSB_EPTRI
mcu:RAXXX
family:broadcom_32bit
diff --git a/examples/device/audio_test_multi_rate/src/main.c b/examples/device/audio_test_multi_rate/src/main.c
index baeec870f..952176997 100644
--- a/examples/device/audio_test_multi_rate/src/main.c
+++ b/examples/device/audio_test_multi_rate/src/main.c
@@ -146,7 +146,7 @@ void tud_resume_cb(void) {
// In a real application, this would be replaced with actual I2S receive callback.
void audio_task(void) {
static uint32_t start_ms = 0;
- uint32_t curr_ms = board_millis();
+ uint32_t curr_ms = tusb_time_millis_api();
if (start_ms == curr_ms) {
return; // not enough time
}
@@ -614,7 +614,7 @@ void led_blinking_task(void) {
static bool led_state = false;
// Blink every interval ms
- if (board_millis() - start_ms < blink_interval_ms) {
+ if (tusb_time_millis_api() - start_ms < blink_interval_ms) {
return; // not enough time
}
start_ms += blink_interval_ms;
diff --git a/examples/device/board_test/src/main.c b/examples/device/board_test/src/main.c
index ee0829e5b..71e7e1da7 100644
--- a/examples/device/board_test/src/main.c
+++ b/examples/device/board_test/src/main.c
@@ -39,11 +39,35 @@ enum {
#define HELLO_STR "Hello from TinyUSB\r\n"
-int main(void) {
- board_init();
- board_led_write(true);
+// board test example does not use both device and host stack
+#if CFG_TUSB_OS != OPT_OS_NONE
+uint32_t tusb_time_millis_api(void) {
+ return osal_time_millis();
+}
+
+void tusb_time_delay_ms_api(uint32_t ms) {
+ osal_task_delay(ms);
+}
+#endif
+
+//--------------------------------------------------------------------+
+//
+//--------------------------------------------------------------------+
+
+// Task parameter type: ULONG for ThreadX, void* for FreeRTOS and noos
+#if CFG_TUSB_OS == OPT_OS_THREADX
+ #define RTOS_PARAM ULONG
+#elif CFG_TUSB_OS == OPT_OS_FREERTOS
+ #define RTOS_PARAM void*
+ static void freertos_init(void);
+#else
+ #define RTOS_PARAM void*
+#endif
+static void board_test_loop(RTOS_PARAM param) {
+ (void) param;
uint32_t start_ms = 0;
+ (void) start_ms;
bool led_state = false;
while (1) {
@@ -58,26 +82,94 @@ int main(void) {
}
// Blink and print every interval ms
- if (!(board_millis() - start_ms < interval_ms)) {
- start_ms = board_millis();
-
- if (ch < 0) {
- // skip if echoing
- printf(HELLO_STR);
+ #if CFG_TUSB_OS == OPT_OS_FREERTOS
+ vTaskDelay(interval_ms / portTICK_PERIOD_MS);
+ #elif CFG_TUSB_OS == OPT_OS_THREADX
+ tx_thread_sleep(_osal_ms2tick(interval_ms));
+ #else
+ if (tusb_time_millis_api() - start_ms < interval_ms) {
+ continue; // not enough time
+ }
+ #endif
+ start_ms = tusb_time_millis_api();
- #ifndef LOGGER_UART
- board_uart_write(HELLO_STR, sizeof(HELLO_STR)-1);
- #endif
- }
+ if (ch < 0) {
+ // skip if echoing
+ printf(HELLO_STR);
- board_led_write(led_state);
- led_state = !led_state; // toggle
+ #ifndef LOGGER_UART
+ board_uart_write(HELLO_STR, sizeof(HELLO_STR)-1);
+ #endif
}
+
+ board_led_write(led_state);
+ led_state = !led_state; // toggle
}
}
+int main(void) {
+ board_init();
+ board_led_write(true);
+
+#if CFG_TUSB_OS == OPT_OS_FREERTOS
+ freertos_init();
+#elif CFG_TUSB_OS == OPT_OS_THREADX
+ tx_kernel_enter();
+#else
+ board_test_loop(NULL);
+#endif
+
+ return 0;
+}
+
#ifdef ESP_PLATFORM
void app_main(void) {
main();
}
#endif
+
+//--------------------------------------------------------------------+
+// FreeRTOS
+//--------------------------------------------------------------------+
+#if CFG_TUSB_OS == OPT_OS_FREERTOS
+
+#ifdef ESP_PLATFORM
+#define MAIN_STACK_SIZE 4096
+#else
+#define MAIN_STACK_SIZE 512
+#endif
+
+#if configSUPPORT_STATIC_ALLOCATION
+static StackType_t _main_stack[MAIN_STACK_SIZE];
+static StaticTask_t _main_taskdef;
+#endif
+
+static void freertos_init(void) {
+ #if configSUPPORT_STATIC_ALLOCATION
+ xTaskCreateStatic(board_test_loop, "main", MAIN_STACK_SIZE, NULL, 1, _main_stack, &_main_taskdef);
+ #else
+ xTaskCreate(board_test_loop, "main", MAIN_STACK_SIZE, NULL, 1, NULL);
+ #endif
+
+ #ifndef ESP_PLATFORM
+ vTaskStartScheduler();
+ #endif
+}
+
+//--------------------------------------------------------------------+
+// ThreadX
+//--------------------------------------------------------------------+
+#elif CFG_TUSB_OS == OPT_OS_THREADX
+
+#define MAIN_TASK_STACK_SIZE 1024
+static TX_THREAD _main_thread;
+static ULONG _main_thread_stack[MAIN_TASK_STACK_SIZE / sizeof(ULONG)];
+
+void tx_application_define(void *first_unused_memory) {
+ (void) first_unused_memory;
+ static CHAR main_thread_name[] = "main";
+ tx_thread_create(&_main_thread, main_thread_name, board_test_loop, 0,
+ _main_thread_stack, MAIN_TASK_STACK_SIZE,
+ 1, 1, TX_NO_TIME_SLICE, TX_AUTO_START);
+}
+#endif
diff --git a/examples/device/cdc_dual_ports/src/main.c b/examples/device/cdc_dual_ports/src/main.c
index 5ccb06a8a..6f918218d 100644
--- a/examples/device/cdc_dual_ports/src/main.c
+++ b/examples/device/cdc_dual_ports/src/main.c
@@ -157,7 +157,7 @@ void led_blinking_task(void) {
static bool led_state = false;
// Blink every interval ms
- if (board_millis() - start_ms < blink_interval_ms) {
+ if (tusb_time_millis_api() - start_ms < blink_interval_ms) {
return; // not enough time
}
start_ms += blink_interval_ms;
diff --git a/examples/device/cdc_dual_ports/src/tusb_config.h b/examples/device/cdc_dual_ports/src/tusb_config.h
index 0da4032a7..710c01ee2 100644
--- a/examples/device/cdc_dual_ports/src/tusb_config.h
+++ b/examples/device/cdc_dual_ports/src/tusb_config.h
@@ -104,6 +104,8 @@
#define CFG_TUD_CDC_TX_BUFSIZE (TUD_OPT_HIGH_SPEED ? 512 : 64)
// CDC Endpoint transfer buffer size, more is faster
+// Leave it as default size (512 for HS, 64 for FS) unless your host application
+// is able to send ZLP (Zero Length Packet) to terminate transfer !
#define CFG_TUD_CDC_EP_BUFSIZE (TUD_OPT_HIGH_SPEED ? 512 : 64)
#ifdef __cplusplus
diff --git a/examples/device/cdc_msc/src/main.c b/examples/device/cdc_msc/src/main.c
index 06a4f732f..b00a0e3a9 100644
--- a/examples/device/cdc_msc/src/main.c
+++ b/examples/device/cdc_msc/src/main.c
@@ -160,7 +160,7 @@ void led_blinking_task(void) {
if (blink_enable) {
// Blink every interval ms
- if (board_millis() - start_ms < blink_interval_ms) {
+ if (tusb_time_millis_api() - start_ms < blink_interval_ms) {
return; // not enough time
}
start_ms += blink_interval_ms;
diff --git a/examples/device/cdc_msc/src/msc_disk.c b/examples/device/cdc_msc/src/msc_disk.c
index e091c2985..017acd039 100644
--- a/examples/device/cdc_msc/src/msc_disk.c
+++ b/examples/device/cdc_msc/src/msc_disk.c
@@ -238,7 +238,7 @@ int32_t tud_msc_scsi_cb(uint8_t lun, uint8_t const scsi_cmd[16], void *buffer, u
(void) buffer;
(void) bufsize;
- // currently no other commands is supported
+ // currently no other commands are supported
// Set Sense = Invalid Command Operation
(void) tud_msc_set_sense(lun, SCSI_SENSE_ILLEGAL_REQUEST, 0x20, 0x00);
diff --git a/examples/device/cdc_msc/src/tusb_config.h b/examples/device/cdc_msc/src/tusb_config.h
index fdb2ddf18..3f2f05f20 100644
--- a/examples/device/cdc_msc/src/tusb_config.h
+++ b/examples/device/cdc_msc/src/tusb_config.h
@@ -104,6 +104,8 @@
#define CFG_TUD_CDC_TX_BUFSIZE (TUD_OPT_HIGH_SPEED ? 512 : 64)
// CDC Endpoint transfer buffer size, more is faster
+// Leave it as default size (512 for HS, 64 for FS) unless your host application
+// is able to send ZLP (Zero Length Packet) to terminate transfer !
#define CFG_TUD_CDC_EP_BUFSIZE (TUD_OPT_HIGH_SPEED ? 512 : 64)
// MSC Buffer size of Device Mass storage
diff --git a/examples/device/cdc_msc_freertos/skip.txt b/examples/device/cdc_msc_freertos/skip.txt
index d90741df7..d3a096eb0 100644
--- a/examples/device/cdc_msc_freertos/skip.txt
+++ b/examples/device/cdc_msc_freertos/skip.txt
@@ -9,7 +9,6 @@ mcu:MKL25ZXX
mcu:MSP430x5xx
mcu:RP2040
mcu:SAMD11
-mcu:SAMX7X
mcu:VALENTYUSB_EPTRI
mcu:RAXXX
mcu:STM32L0
diff --git a/examples/device/cdc_msc_freertos/src/msc_disk.c b/examples/device/cdc_msc_freertos/src/msc_disk.c
index 29ff86281..ff918205e 100644
--- a/examples/device/cdc_msc_freertos/src/msc_disk.c
+++ b/examples/device/cdc_msc_freertos/src/msc_disk.c
@@ -324,20 +324,17 @@ int32_t tud_msc_write10_cb(uint8_t lun, uint32_t lba, uint32_t offset, uint8_t*
// - READ_CAPACITY10, READ_FORMAT_CAPACITY, INQUIRY, MODE_SENSE6, REQUEST_SENSE
// - READ10 and WRITE10 has their own callbacks
int32_t tud_msc_scsi_cb (uint8_t lun, uint8_t const scsi_cmd[16], void* buffer, uint16_t bufsize) {
- // read10 & write10 has their own callback and MUST not be handled here
+ (void) lun;
+ (void) scsi_cmd;
(void) buffer;
(void) bufsize;
- switch (scsi_cmd[0]) {
- default:
- // Set Sense = Invalid Command Operation
- tud_msc_set_sense(lun, SCSI_SENSE_ILLEGAL_REQUEST, 0x20, 0x00);
+ // currently no other commands are supported
- // negative means error -> tinyusb could stall and/or response with failed status
- return -1;
- }
+ // Set Sense = Invalid Command Operation
+ (void) tud_msc_set_sense(lun, SCSI_SENSE_ILLEGAL_REQUEST, 0x20, 0x00);
- return -1;
+ return -1; // stall/failed command request;
}
#endif
diff --git a/examples/device/cdc_msc_freertos/src/tusb_config.h b/examples/device/cdc_msc_freertos/src/tusb_config.h
index 6b1937a8d..8277b1604 100644
--- a/examples/device/cdc_msc_freertos/src/tusb_config.h
+++ b/examples/device/cdc_msc_freertos/src/tusb_config.h
@@ -111,6 +111,8 @@
#define CFG_TUD_CDC_TX_BUFSIZE (TUD_OPT_HIGH_SPEED ? 512 : 64)
// CDC Endpoint transfer buffer size, more is faster
+// Leave it as default size (512 for HS, 64 for FS) unless your host application
+// is able to send ZLP (Zero Length Packet) to terminate transfer !
#define CFG_TUD_CDC_EP_BUFSIZE (TUD_OPT_HIGH_SPEED ? 512 : 64)
// MSC Buffer size of Device Mass storage
diff --git a/examples/device/cdc_uac2/src/main.c b/examples/device/cdc_uac2/src/main.c
index 22c462be7..cb7b3a142 100644
--- a/examples/device/cdc_uac2/src/main.c
+++ b/examples/device/cdc_uac2/src/main.c
@@ -65,8 +65,6 @@ int main(void)
// printf("Hello, world!\r\n");
#endif
}
-
- return 0;
}
//--------------------------------------------------------------------+
diff --git a/examples/device/cdc_uac2/src/tusb_config.h b/examples/device/cdc_uac2/src/tusb_config.h
index b7ece8b7c..5eb2e8f74 100644
--- a/examples/device/cdc_uac2/src/tusb_config.h
+++ b/examples/device/cdc_uac2/src/tusb_config.h
@@ -160,6 +160,8 @@ extern "C" {
#define CFG_TUD_CDC_TX_BUFSIZE (TUD_OPT_HIGH_SPEED ? 512 : 64)
// CDC Endpoint transfer buffer size, more is faster
+// Leave it as default size (512 for HS, 64 for FS) unless your host application
+// is able to send ZLP (Zero Length Packet) to terminate transfer !
#define CFG_TUD_CDC_EP_BUFSIZE (TUD_OPT_HIGH_SPEED ? 512 : 64)
#ifdef __cplusplus
diff --git a/examples/device/cdc_uac2/src/uac2_app.c b/examples/device/cdc_uac2/src/uac2_app.c
index 73a262d0c..7760c402b 100644
--- a/examples/device/cdc_uac2/src/uac2_app.c
+++ b/examples/device/cdc_uac2/src/uac2_app.c
@@ -66,7 +66,7 @@ uint8_t current_resolution;
// In a real application, this would be replaced with actual I2S send/receive callback.
void audio_task(void) {
static uint32_t start_ms = 0;
- uint32_t curr_ms = board_millis();
+ uint32_t curr_ms = tusb_time_millis_api();
if (start_ms == curr_ms) {
return; // not enough time
}
@@ -303,7 +303,7 @@ void led_blinking_task(void)
static bool led_state = false;
// Blink every interval ms
- if (board_millis() - start_ms < blink_interval_ms) {
+ if (tusb_time_millis_api() - start_ms < blink_interval_ms) {
return;
}
start_ms += blink_interval_ms;
diff --git a/examples/device/dfu/src/main.c b/examples/device/dfu/src/main.c
index 77632bf1a..fb3c22630 100644
--- a/examples/device/dfu/src/main.c
+++ b/examples/device/dfu/src/main.c
@@ -199,7 +199,7 @@ void led_blinking_task(void) {
static bool led_state = false;
// Blink every interval ms
- if (board_millis() - start_ms < blink_interval_ms) {
+ if (tusb_time_millis_api() - start_ms < blink_interval_ms) {
return; // not enough time
}
start_ms += blink_interval_ms;
diff --git a/examples/device/dfu_runtime/src/main.c b/examples/device/dfu_runtime/src/main.c
index 5de651bcd..6f412e8de 100644
--- a/examples/device/dfu_runtime/src/main.c
+++ b/examples/device/dfu_runtime/src/main.c
@@ -132,7 +132,7 @@ void led_blinking_task(void)
static bool led_state = false;
// Blink every interval ms
- if (board_millis() - start_ms < blink_interval_ms) {
+ if (tusb_time_millis_api() - start_ms < blink_interval_ms) {
return; // not enough time
}
start_ms += blink_interval_ms;
diff --git a/examples/device/dynamic_configuration/src/main.c b/examples/device/dynamic_configuration/src/main.c
index dac74bb7a..8ebb42f9a 100644
--- a/examples/device/dynamic_configuration/src/main.c
+++ b/examples/device/dynamic_configuration/src/main.c
@@ -170,7 +170,7 @@ void midi_task(void) {
while( tud_midi_available() ) tud_midi_packet_read(packet);
// send note every 1000 ms
- if (board_millis() - start_ms < 286) {
+ if (tusb_time_millis_api() - start_ms < 286) {
return; // not enough time
}
start_ms += 286;
@@ -209,7 +209,7 @@ void led_blinking_task(void) {
static bool led_state = false;
// Blink every interval ms
- if (board_millis() - start_ms < blink_interval_ms) {
+ if (tusb_time_millis_api() - start_ms < blink_interval_ms) {
return;// not enough time
}
start_ms += blink_interval_ms;
diff --git a/examples/device/dynamic_configuration/src/msc_disk.c b/examples/device/dynamic_configuration/src/msc_disk.c
index e95b2e197..b545e4652 100644
--- a/examples/device/dynamic_configuration/src/msc_disk.c
+++ b/examples/device/dynamic_configuration/src/msc_disk.c
@@ -215,20 +215,17 @@ int32_t tud_msc_write10_cb(uint8_t lun, uint32_t lba, uint32_t offset, uint8_t*
// - READ_CAPACITY10, READ_FORMAT_CAPACITY, INQUIRY, MODE_SENSE6, REQUEST_SENSE
// - READ10 and WRITE10 has their own callbacks
int32_t tud_msc_scsi_cb (uint8_t lun, uint8_t const scsi_cmd[16], void* buffer, uint16_t bufsize) {
- // read10 & write10 has their own callback and MUST not be handled here
+ (void) lun;
+ (void) scsi_cmd;
(void) buffer;
(void) bufsize;
- switch (scsi_cmd[0]) {
- default:
- // Set Sense = Invalid Command Operation
- tud_msc_set_sense(lun, SCSI_SENSE_ILLEGAL_REQUEST, 0x20, 0x00);
+ // currently no other commands are supported
- // negative means error -> tinyusb could stall and/or response with failed status
- return -1;
- }
+ // Set Sense = Invalid Command Operation
+ (void) tud_msc_set_sense(lun, SCSI_SENSE_ILLEGAL_REQUEST, 0x20, 0x00);
- return -1;
+ return -1; // stall/failed command request;
}
#endif
diff --git a/examples/device/hid_boot_interface/src/main.c b/examples/device/hid_boot_interface/src/main.c
index 44a91db67..7f2153ae9 100644
--- a/examples/device/hid_boot_interface/src/main.c
+++ b/examples/device/hid_boot_interface/src/main.c
@@ -67,8 +67,6 @@ int main(void) {
hid_task();
}
-
- return 0;
}
//--------------------------------------------------------------------+
@@ -109,7 +107,7 @@ void hid_task(void) {
const uint32_t interval_ms = 10;
static uint32_t start_ms = 0;
- if (board_millis() - start_ms < interval_ms) {
+ if (tusb_time_millis_api() - start_ms < interval_ms) {
return; // not enough time
}
start_ms += interval_ms;
@@ -238,7 +236,7 @@ void led_blinking_task(void) {
}
// Blink every interval ms
- if (board_millis() - start_ms < blink_interval_ms) {
+ if (tusb_time_millis_api() - start_ms < blink_interval_ms) {
return; // not enough time
}
start_ms += blink_interval_ms;
diff --git a/examples/device/hid_composite/src/main.c b/examples/device/hid_composite/src/main.c
index 9693d564d..7c9d5af8d 100644
--- a/examples/device/hid_composite/src/main.c
+++ b/examples/device/hid_composite/src/main.c
@@ -209,7 +209,7 @@ void hid_task(void) {
const uint32_t interval_ms = 10;
static uint32_t start_ms = 0;
- if (board_millis() - start_ms < interval_ms) {
+ if (tusb_time_millis_api() - start_ms < interval_ms) {
return; // not enough time
}
start_ms += interval_ms;
@@ -298,7 +298,7 @@ void led_blinking_task(void) {
}
// Blink every interval ms
- if (board_millis() - start_ms < blink_interval_ms) {
+ if (tusb_time_millis_api() - start_ms < blink_interval_ms) {
return; // not enough time
}
start_ms += blink_interval_ms;
diff --git a/examples/device/hid_composite_freertos/skip.txt b/examples/device/hid_composite_freertos/skip.txt
index 01990a5b4..06920db67 100644
--- a/examples/device/hid_composite_freertos/skip.txt
+++ b/examples/device/hid_composite_freertos/skip.txt
@@ -9,7 +9,6 @@ mcu:MKL25ZXX
mcu:MSP430x5xx
mcu:RP2040
mcu:SAMD11
-mcu:SAMX7X
mcu:VALENTYUSB_EPTRI
mcu:RAXXX
family:broadcom_32bit
diff --git a/examples/device/hid_generic_inout/src/main.c b/examples/device/hid_generic_inout/src/main.c
index 9837a47d9..2a6b91a8a 100644
--- a/examples/device/hid_generic_inout/src/main.c
+++ b/examples/device/hid_generic_inout/src/main.c
@@ -168,7 +168,7 @@ 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 ( tusb_time_millis_api() - start_ms < blink_interval_ms) return; // not enough time
start_ms += blink_interval_ms;
board_led_write(led_state);
diff --git a/examples/device/hid_multiple_interface/src/main.c b/examples/device/hid_multiple_interface/src/main.c
index 0bccd13c1..c71a9cbad 100644
--- a/examples/device/hid_multiple_interface/src/main.c
+++ b/examples/device/hid_multiple_interface/src/main.c
@@ -120,7 +120,7 @@ void hid_task(void)
const uint32_t interval_ms = 10;
static uint32_t start_ms = 0;
- if ( board_millis() - start_ms < interval_ms) return; // not enough time
+ if ( tusb_time_millis_api() - start_ms < interval_ms) return; // not enough time
start_ms += interval_ms;
uint32_t const btn = board_button_read();
@@ -205,7 +205,7 @@ 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 ( tusb_time_millis_api() - start_ms < blink_interval_ms) return; // not enough time
start_ms += blink_interval_ms;
board_led_write(led_state);
diff --git a/examples/device/midi_test/src/main.c b/examples/device/midi_test/src/main.c
index fd58e3021..1154c1d60 100644
--- a/examples/device/midi_test/src/main.c
+++ b/examples/device/midi_test/src/main.c
@@ -134,7 +134,7 @@ void midi_task(void)
}
// send note periodically
- if (board_millis() - start_ms < 286) {
+ if (tusb_time_millis_api() - start_ms < 286) {
return; // not enough time
}
start_ms += 286;
@@ -174,7 +174,7 @@ 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 ( tusb_time_millis_api() - start_ms < blink_interval_ms) return; // not enough time
start_ms += blink_interval_ms;
board_led_write(led_state);
diff --git a/examples/device/midi_test_freertos/skip.txt b/examples/device/midi_test_freertos/skip.txt
index 01990a5b4..06920db67 100644
--- a/examples/device/midi_test_freertos/skip.txt
+++ b/examples/device/midi_test_freertos/skip.txt
@@ -9,7 +9,6 @@ mcu:MKL25ZXX
mcu:MSP430x5xx
mcu:RP2040
mcu:SAMD11
-mcu:SAMX7X
mcu:VALENTYUSB_EPTRI
mcu:RAXXX
family:broadcom_32bit
diff --git a/examples/device/msc_dual_lun/src/main.c b/examples/device/msc_dual_lun/src/main.c
index 62b1c872a..a4ade6f9b 100644
--- a/examples/device/msc_dual_lun/src/main.c
+++ b/examples/device/msc_dual_lun/src/main.c
@@ -31,7 +31,7 @@
#include "tusb.h"
//--------------------------------------------------------------------+
-// MACRO CONSTANT TYPEDEF PROTYPES
+// MACRO CONSTANT TYPEDEF PROTOTYPES
//--------------------------------------------------------------------+
/* Blink pattern
@@ -41,71 +41,177 @@
*/
enum {
BLINK_NOT_MOUNTED = 250,
- BLINK_MOUNTED = 1000,
- BLINK_SUSPENDED = 2500,
+ BLINK_MOUNTED = 1000,
+ BLINK_SUSPENDED = 2500,
};
static uint32_t blink_interval_ms = BLINK_NOT_MOUNTED;
-void led_blinking_task(void);
+// Task parameter type: ULONG for ThreadX, void* for FreeRTOS and noos
+#if CFG_TUSB_OS == OPT_OS_THREADX
+ #define RTOS_PARAM ULONG
+#elif CFG_TUSB_OS == OPT_OS_FREERTOS
+ #define RTOS_PARAM void*
+ static void freertos_init(void);
+#else
+ #define RTOS_PARAM void*
+#endif
-/*------------- MAIN -------------*/
-int main(void) {
- board_init();
+void led_blinking_task(RTOS_PARAM param);
- // init device stack on configured roothub port
+//--------------------------------------------------------------------+
+// USB Device Task
+//--------------------------------------------------------------------+
+static void usb_device_init(void) {
tusb_rhport_init_t dev_init = {
- .role = TUSB_ROLE_DEVICE,
+ .role = TUSB_ROLE_DEVICE,
.speed = TUSB_SPEED_AUTO
};
tusb_init(BOARD_TUD_RHPORT, &dev_init);
-
board_init_after_tusb();
+}
+
+#if CFG_TUSB_OS != OPT_OS_NONE && CFG_TUSB_OS != OPT_OS_PICO
+static void usb_device_task(RTOS_PARAM param) {
+ (void) param;
+ usb_device_init();
while (1) {
- tud_task(); // tinyusb device task
- led_blinking_task();
+ tud_task();
}
}
+#endif
//--------------------------------------------------------------------+
-// Device callbacks
+// Main
//--------------------------------------------------------------------+
+int main(void) {
+ board_init();
-// Invoked when device is mounted
+#if CFG_TUSB_OS == OPT_OS_FREERTOS
+ freertos_init();
+
+#elif CFG_TUSB_OS == OPT_OS_THREADX
+ tx_kernel_enter();
+
+#else
+ // noos + pico-sdk: init USB then run polling loop
+ usb_device_init();
+
+ while (1) {
+ tud_task();
+ led_blinking_task(NULL);
+ }
+#endif
+}
+
+#ifdef ESP_PLATFORM
+void app_main(void) {
+ main();
+}
+#endif
+
+//--------------------------------------------------------------------+
+// Device callbacks
+//--------------------------------------------------------------------+
void tud_mount_cb(void) {
blink_interval_ms = BLINK_MOUNTED;
}
-// Invoked when device is unmounted
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;
blink_interval_ms = BLINK_SUSPENDED;
}
-// Invoked when usb bus is resumed
void tud_resume_cb(void) {
blink_interval_ms = tud_mounted() ? BLINK_MOUNTED : BLINK_NOT_MOUNTED;
}
//--------------------------------------------------------------------+
-// BLINKING TASK
+// Blinking Task
//--------------------------------------------------------------------+
-void led_blinking_task(void) {
+void led_blinking_task(RTOS_PARAM param) {
+ (void) param;
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
- start_ms += blink_interval_ms;
+ while (1) {
+#if CFG_TUSB_OS == OPT_OS_FREERTOS
+ vTaskDelay(blink_interval_ms / portTICK_PERIOD_MS);
+#elif CFG_TUSB_OS == OPT_OS_THREADX
+ tx_thread_sleep(_osal_ms2tick(blink_interval_ms));
+#else
+ if (tusb_time_millis_api() - start_ms < blink_interval_ms) {
+ return; // not enough time
+ }
+#endif
+
+ start_ms += blink_interval_ms;
+ board_led_write(led_state);
+ led_state = 1 - led_state; // toggle
+ }
+}
+
+//--------------------------------------------------------------------+
+// FreeRTOS
+//--------------------------------------------------------------------+
+#if CFG_TUSB_OS == OPT_OS_FREERTOS
+
+#ifdef ESP_PLATFORM
+#define USBD_STACK_SIZE 4096
+#else
+#define USBD_STACK_SIZE (3*configMINIMAL_STACK_SIZE/2 * (CFG_TUSB_DEBUG ? 2 : 1))
+#endif
+#define BLINKY_STACK_SIZE configMINIMAL_STACK_SIZE
- board_led_write(led_state);
- led_state = 1 - led_state; // toggle
+#if configSUPPORT_STATIC_ALLOCATION
+static StackType_t _usb_device_stack[USBD_STACK_SIZE];
+static StaticTask_t _usb_device_taskdef;
+static StackType_t _blinky_stack[BLINKY_STACK_SIZE];
+static StaticTask_t _blinky_taskdef;
+#endif
+
+
+static void freertos_init(void) {
+ #if configSUPPORT_STATIC_ALLOCATION
+ xTaskCreateStatic(usb_device_task, "usbd", USBD_STACK_SIZE, NULL, configMAX_PRIORITIES - 1, _usb_device_stack, &_usb_device_taskdef);
+ xTaskCreateStatic(led_blinking_task, "blinky", BLINKY_STACK_SIZE, NULL, 1, _blinky_stack, &_blinky_taskdef);
+ #else
+ xTaskCreate(usb_device_task, "usbd", USBD_STACK_SIZE, NULL, configMAX_PRIORITIES - 1, NULL);
+ xTaskCreate(led_blinking_task, "blinky", BLINKY_STACK_SIZE, NULL, 1, NULL);
+ #endif
+ #ifndef ESP_PLATFORM
+ vTaskStartScheduler();
+ #endif
+}
+
+//--------------------------------------------------------------------+
+// ThreadX
+//--------------------------------------------------------------------+
+#elif CFG_TUSB_OS == OPT_OS_THREADX
+
+#define USBD_STACK_SIZE 4096
+#define BLINKY_STACK_SIZE 512
+
+static TX_THREAD _usb_device_thread;
+static ULONG _usb_device_stack[USBD_STACK_SIZE / sizeof(ULONG)];
+static TX_THREAD _blinky_thread;
+static ULONG _blinky_stack[BLINKY_STACK_SIZE / sizeof(ULONG)];
+
+void tx_application_define(void *first_unused_memory) {
+ (void) first_unused_memory;
+ static CHAR usbd_name[] = "usbd";
+ static CHAR blinky_name[] = "blinky";
+ tx_thread_create(&_usb_device_thread, usbd_name, usb_device_task, 0,
+ _usb_device_stack, USBD_STACK_SIZE,
+ 0, 0, TX_NO_TIME_SLICE, TX_AUTO_START);
+ tx_thread_create(&_blinky_thread, blinky_name, led_blinking_task, 0,
+ _blinky_stack, BLINKY_STACK_SIZE,
+ 1, 1, TX_NO_TIME_SLICE, TX_AUTO_START);
}
+
+#endif
diff --git a/examples/device/mtp/src/main.c b/examples/device/mtp/src/main.c
index 57d1535b2..6ffa435d1 100644
--- a/examples/device/mtp/src/main.c
+++ b/examples/device/mtp/src/main.c
@@ -102,7 +102,7 @@ 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 (tusb_time_millis_api() - start_ms < blink_interval_ms) return; // not enough time
start_ms += blink_interval_ms;
board_led_write(led_state);
diff --git a/examples/device/net_lwip_webserver/src/main.c b/examples/device/net_lwip_webserver/src/main.c
index 867cf2812..8bd8a8c21 100644
--- a/examples/device/net_lwip_webserver/src/main.c
+++ b/examples/device/net_lwip_webserver/src/main.c
@@ -276,8 +276,6 @@ int main(void) {
sys_check_timeouts(); // service lwip
handle_link_state_switch();
}
-
- return 0;
}
/* lwip has provision for using a mutex, when applicable */
@@ -291,5 +289,5 @@ void sys_arch_unprotect(sys_prot_t pval) {
/* lwip needs a millisecond time source, and the TinyUSB board support code has one available */
uint32_t sys_now(void) {
- return board_millis();
+ return tusb_time_millis_api();
}
diff --git a/examples/device/printer_to_cdc/CMakeLists.txt b/examples/device/printer_to_cdc/CMakeLists.txt
new file mode 100644
index 000000000..3c8ab3653
--- /dev/null
+++ b/examples/device/printer_to_cdc/CMakeLists.txt
@@ -0,0 +1,29 @@
+cmake_minimum_required(VERSION 3.20)
+
+include(${CMAKE_CURRENT_SOURCE_DIR}/../../../hw/bsp/family_support.cmake)
+
+project(printer_to_cdc C CXX ASM)
+
+# Checks this example is valid for the family and initializes the project
+family_initialize_project(${PROJECT_NAME} ${CMAKE_CURRENT_LIST_DIR})
+# Espressif has its own cmake build system
+if(FAMILY STREQUAL "espressif")
+ return()
+endif()
+
+add_executable(${PROJECT_NAME})
+
+# Example source
+target_sources(${PROJECT_NAME} PUBLIC
+ ${CMAKE_CURRENT_SOURCE_DIR}/src/main.c
+ ${CMAKE_CURRENT_SOURCE_DIR}/src/usb_descriptors.c
+ )
+
+# Example include
+target_include_directories(${PROJECT_NAME} PUBLIC
+ ${CMAKE_CURRENT_SOURCE_DIR}/src
+ )
+
+# Configure compilation flags and libraries for the example without RTOS.
+# See the corresponding function in hw/bsp/FAMILY/family.cmake for details.
+family_configure_device_example(${PROJECT_NAME} noos)
diff --git a/examples/device/printer_to_cdc/Makefile b/examples/device/printer_to_cdc/Makefile
new file mode 100644
index 000000000..1a4b428dc
--- /dev/null
+++ b/examples/device/printer_to_cdc/Makefile
@@ -0,0 +1,11 @@
+include ../../../hw/bsp/family_support.mk
+
+INC += \
+ src \
+
+
+# Example source
+EXAMPLE_SOURCE += $(wildcard src/*.c)
+SRC_C += $(addprefix $(EXAMPLE_PATH)/, $(EXAMPLE_SOURCE))
+
+include ../../../hw/bsp/family_rules.mk
diff --git a/examples/device/printer_to_cdc/README.md b/examples/device/printer_to_cdc/README.md
new file mode 100644
index 000000000..ecb9a678f
--- /dev/null
+++ b/examples/device/printer_to_cdc/README.md
@@ -0,0 +1,80 @@
+#### Printer to CDC
+
+This example demonstrates a USB composite device with a Printer class interface and a CDC serial interface. Data flows bidirectionally between the two:
+
+- Data sent to the Printer (from host) is forwarded to the CDC serial port
+- Data sent to the CDC serial port (from host) is forwarded to the Printer IN endpoint
+
+This is useful for debugging printer class communication or as a reference for implementing printer class devices.
+
+#### USB Interfaces
+
+| Interface | Class | Description |
+|-----------|-------|-------------|
+| 0 | CDC ACM | Virtual serial port |
+| 2 | Printer | USB Printer (bidirectional, protocol 2) |
+
+#### How to Test
+
+The device exposes two endpoints on the host:
+- `/dev/ttyACM0` (CDC serial port)
+- `/dev/usb/lp0` (USB printer)
+
+Note: the actual device numbers may vary depending on your system.
+
+**Prerequisites (Linux):**
+
+```bash
+# Load the USB printer kernel module if not already loaded
+sudo modprobe usblp
+
+# Check devices exist
+ls /dev/ttyACM* /dev/usb/lp*
+```
+
+**Test Printer to CDC (host writes to printer, reads from CDC):**
+
+```bash
+# Terminal 1: read from CDC
+cat /dev/ttyACM0
+
+# Terminal 2: write to printer
+echo "hello from printer" > /dev/usb/lp0
+# "hello from printer" appears in Terminal 1
+```
+
+**Test CDC to Printer (host writes to CDC, reads from printer):**
+
+```bash
+# Terminal 1: read from printer IN endpoint
+cat /dev/usb/lp0
+
+# Terminal 2: write to CDC
+echo "hello from cdc" > /dev/ttyACM0
+# "hello from cdc" appears in Terminal 1
+```
+
+**Interactive bidirectional test:**
+
+```bash
+# Terminal 1: open CDC serial port
+minicom -D /dev/ttyACM0
+
+# Terminal 2: send to printer
+echo "tinyusb print example" > /dev/usb/lp0
+# Text appears in minicom. Type in minicom to send data back through printer TX.
+```
+
+#### IEEE 1284 Device ID
+
+The device responds to GET_DEVICE_ID requests with:
+
+```
+MFG:TinyUSB;MDL:Printer to CDC;CMD:PS;CLS:PRINTER;
+```
+
+Verify with:
+
+```bash
+cat /sys/class/usbmisc/lp0/device/ieee1284_id
+```
diff --git a/examples/device/printer_to_cdc/src/main.c b/examples/device/printer_to_cdc/src/main.c
new file mode 100644
index 000000000..ffaf709b4
--- /dev/null
+++ b/examples/device/printer_to_cdc/src/main.c
@@ -0,0 +1,117 @@
+/*
+ * The MIT License (MIT)
+ *
+ * Copyright (c) 2026 Ha Thach (tinyusb.org)
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
+ * of this software and associated documentation files (the "Software"), to deal
+ * in the Software without restriction, including without limitation the rights
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+ * copies of the Software, and to permit persons to whom the Software is
+ * furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included in
+ * all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+ * THE SOFTWARE.
+ *
+ */
+
+/* This example demonstrates a USB Printer + CDC composite device.
+ * Data received on the Printer interface is forwarded to the CDC serial port,
+ * and data received on the CDC serial port is forwarded back to the Printer interface.
+ *
+ * To test:
+ * 1. Flash the device
+ * 2. Open a serial terminal on the CDC port (e.g. /dev/ttyACM0)
+ * 3. Send data to the printer: echo "hello" > /dev/usb/lp0
+ * 4. The data appears on the CDC serial terminal
+ * 5. Type in the serial terminal to send data back through the printer TX
+ */
+
+#include <stdlib.h>
+#include <stdio.h>
+#include <string.h>
+
+#include "bsp/board_api.h"
+#include "tusb.h"
+
+#include "usb_descriptors.h"
+
+// -------------------------------------------------------------------+
+// Tasks
+// -------------------------------------------------------------------+
+
+// Forward data from Printer RX to CDC TX
+static void printer_to_cdc_task(void) {
+ uint32_t avail = tud_printer_read_available();
+ if (avail == 0 || !tud_cdc_write_available()) {
+ return;
+ }
+
+ uint8_t buf[64];
+ uint32_t count = tud_printer_read(buf, TU_MIN(sizeof(buf), tud_cdc_write_available()));
+ if (count > 0) {
+ tud_cdc_write(buf, count);
+ tud_cdc_write_flush();
+ }
+}
+
+// Forward data from CDC RX to Printer TX
+static void cdc_to_printer_task(void) {
+ uint32_t avail = tud_printer_write_available();
+ if (tud_cdc_available() == 0 || avail == 0) {
+ return;
+ }
+
+ uint8_t buf[64];
+ uint32_t count = tud_cdc_read(buf, TU_MIN(sizeof(buf), avail));
+ if (count > 0) {
+ tud_printer_write(buf, count);
+ tud_printer_write_flush();
+ }
+}
+
+int main(void) {
+ board_init();
+ // init device stack on configured roothub port
+ tusb_rhport_init_t dev_init = {.role = TUSB_ROLE_DEVICE, .speed = TUSB_SPEED_AUTO};
+ tusb_init(BOARD_TUD_RHPORT, &dev_init);
+ board_init_after_tusb();
+
+ while (1) {
+ tud_task(); // tinyusb device task
+ printer_to_cdc_task(); // forward printer data to CDC
+ cdc_to_printer_task(); // forward CDC data to printer
+ }
+}
+
+//--------------------------------------------------------------------+
+// Printer callbacks
+//--------------------------------------------------------------------+
+
+void tud_printer_rx_cb(uint8_t itf) {
+ (void)itf;
+}
+
+// IEEE 1284 Device ID: first 2 bytes are big-endian total length (including the 2 length bytes).
+// The rest is the Device ID string using standard abbreviated keys.
+static const char printer_device_id[] =
+ "\x00\x34" // total length = 52 = 0x0034 (big-endian)
+ "MFG:TinyUSB;"
+ "MDL:Printer to CDC;"
+ "CMD:PS;"
+ "CLS:PRINTER;";
+
+TU_VERIFY_STATIC(sizeof(printer_device_id) - 1 == 52, "device ID length mismatch");
+
+uint8_t const *tud_printer_get_device_id_cb(uint8_t itf) {
+ (void)itf;
+ return (uint8_t const *)printer_device_id;
+}
diff --git a/examples/device/printer_to_cdc/src/tusb_config.h b/examples/device/printer_to_cdc/src/tusb_config.h
new file mode 100644
index 000000000..c38e8e1ee
--- /dev/null
+++ b/examples/device/printer_to_cdc/src/tusb_config.h
@@ -0,0 +1,117 @@
+/*
+ * The MIT License (MIT)
+ *
+ * Copyright (c) 2026 Ha Thach (tinyusb.org)
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
+ * of this software and associated documentation files (the "Software"), to deal
+ * in the Software without restriction, including without limitation the rights
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+ * copies of the Software, and to permit persons to whom the Software is
+ * furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included in
+ * all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+ * THE SOFTWARE.
+ *
+ */
+
+#ifndef _TUSB_CONFIG_H_
+#define _TUSB_CONFIG_H_
+
+#ifdef __cplusplus
+extern "C" {
+#endif
+
+//--------------------------------------------------------------------+
+// Board Specific Configuration
+//--------------------------------------------------------------------+
+
+// RHPort number used for device can be defined by board.mk, default to port 0
+#ifndef BOARD_TUD_RHPORT
+ #define BOARD_TUD_RHPORT 0
+#endif
+
+// RHPort max operational speed can defined by board.mk
+#ifndef BOARD_TUD_MAX_SPEED
+ #define BOARD_TUD_MAX_SPEED OPT_MODE_DEFAULT_SPEED
+#endif
+
+//--------------------------------------------------------------------
+// COMMON CONFIGURATION
+//--------------------------------------------------------------------
+
+// defined by compiler flags for flexibility
+#ifndef CFG_TUSB_MCU
+ #error CFG_TUSB_MCU must be defined
+#endif
+
+#ifndef CFG_TUSB_OS
+ #define CFG_TUSB_OS OPT_OS_NONE
+#endif
+
+#ifndef CFG_TUSB_DEBUG
+ #define CFG_TUSB_DEBUG 0
+#endif
+
+// Enable Device stack
+#define CFG_TUD_ENABLED 1
+
+// Default is max speed that hardware controller could support with on-chip PHY
+#define CFG_TUD_MAX_SPEED BOARD_TUD_MAX_SPEED
+
+/* USB DMA on some MCUs can only access a specific SRAM region with restriction on alignment.
+ * Tinyusb use follows macros to declare transferring memory so that they can be put
+ * into those specific section.
+ * e.g
+ * - CFG_TUSB_MEM SECTION : __attribute__ (( section(".usb_ram") ))
+ * - CFG_TUSB_MEM_ALIGN : __attribute__ ((aligned(4)))
+ */
+#ifndef CFG_TUSB_MEM_SECTION
+ #define CFG_TUSB_MEM_SECTION
+#endif
+
+#ifndef CFG_TUSB_MEM_ALIGN
+ #define CFG_TUSB_MEM_ALIGN __attribute__((aligned(4)))
+#endif
+
+//--------------------------------------------------------------------
+// DEVICE CONFIGURATION
+//--------------------------------------------------------------------
+
+#ifndef CFG_TUD_ENDPOINT0_SIZE
+ #define CFG_TUD_ENDPOINT0_SIZE 64
+#endif
+
+//------------- CLASS -------------//
+#define CFG_TUD_HID 0
+#define CFG_TUD_CDC 1
+#define CFG_TUD_MSC 0
+#define CFG_TUD_MIDI 0
+#define CFG_TUD_VENDOR 0
+#define CFG_TUD_PRINTER 1
+
+// CDC FIFO size of TX and RX
+#define CFG_TUD_CDC_RX_BUFSIZE (TUD_OPT_HIGH_SPEED ? 512 : 64)
+#define CFG_TUD_CDC_TX_BUFSIZE (TUD_OPT_HIGH_SPEED ? 512 : 64)
+
+// CDC Endpoint transfer buffer size
+#define CFG_TUD_CDC_EP_BUFSIZE (TUD_OPT_HIGH_SPEED ? 512 : 64)
+
+// Printer buffer sizes
+#define CFG_TUD_PRINTER_RX_BUFSIZE (TUD_OPT_HIGH_SPEED ? 512 : 64)
+#define CFG_TUD_PRINTER_TX_BUFSIZE (TUD_OPT_HIGH_SPEED ? 512 : 64)
+#define CFG_TUD_PRINTER_EP_BUFSIZE (TUD_OPT_HIGH_SPEED ? 512 : 64)
+
+#ifdef __cplusplus
+}
+#endif
+
+#endif /* _TUSB_CONFIG_H_ */
diff --git a/examples/device/printer_to_cdc/src/usb_descriptors.c b/examples/device/printer_to_cdc/src/usb_descriptors.c
new file mode 100644
index 000000000..30d309ed4
--- /dev/null
+++ b/examples/device/printer_to_cdc/src/usb_descriptors.c
@@ -0,0 +1,208 @@
+/*
+ * The MIT License (MIT)
+ *
+ * Copyright (c) 2026 Ha Thach (tinyusb.org)
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
+ * of this software and associated documentation files (the "Software"), to deal
+ * in the Software without restriction, including without limitation the rights
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+ * copies of the Software, and to permit persons to whom the Software is
+ * furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included in
+ * all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+ * THE SOFTWARE.
+ *
+ */
+
+#include "bsp/board_api.h"
+#include "tusb.h"
+
+#include "usb_descriptors.h"
+
+#define USB_VID 0xCafe
+#define USB_PID 0x4005
+#define USB_BCD 0x0200
+
+//--------------------------------------------------------------------+
+// Device Descriptors
+//--------------------------------------------------------------------+
+static tusb_desc_device_t const desc_device = {
+ .bLength = sizeof(tusb_desc_device_t),
+ .bDescriptorType = TUSB_DESC_DEVICE,
+ .bcdUSB = USB_BCD,
+
+ // Use Interface Association Descriptor (IAD) for CDC
+ // As required by USB Specs IAD's subclass must be common class (2) and protocol must be IAD (1)
+ .bDeviceClass = TUSB_CLASS_MISC,
+ .bDeviceSubClass = MISC_SUBCLASS_COMMON,
+ .bDeviceProtocol = MISC_PROTOCOL_IAD,
+ .bMaxPacketSize0 = CFG_TUD_ENDPOINT0_SIZE,
+
+ .idVendor = USB_VID,
+ .idProduct = USB_PID,
+ .bcdDevice = 0x0100,
+
+ .iManufacturer = 0x01,
+ .iProduct = 0x02,
+ .iSerialNumber = 0x03,
+
+ .bNumConfigurations = 0x01
+};
+
+uint8_t const *tud_descriptor_device_cb(void) {
+ return (uint8_t const *) &desc_device;
+}
+
+//--------------------------------------------------------------------+
+// Configuration Descriptor
+//--------------------------------------------------------------------+
+
+// Endpoint numbers
+#if defined(TUD_ENDPOINT_ONE_DIRECTION_ONLY)
+ #define EPNUM_CDC_NOTIF 0x81
+ #define EPNUM_CDC_OUT 0x02
+ #define EPNUM_CDC_IN 0x83
+ #define EPNUM_PRINTER_OUT 0x04
+ #define EPNUM_PRINTER_IN 0x85
+#else
+ #define EPNUM_CDC_NOTIF 0x81
+ #define EPNUM_CDC_OUT 0x02
+ #define EPNUM_CDC_IN 0x82
+ #define EPNUM_PRINTER_OUT 0x03
+ #define EPNUM_PRINTER_IN 0x83
+#endif
+
+// full speed configuration
+static uint8_t const desc_fs_configuration[] = {
+ // Config number, interface count, string index, total length, attribute, power in mA
+ TUD_CONFIG_DESCRIPTOR(1, ITF_NUM_TOTAL, 0, CONFIG_TOTAL_LEN, 0x00, 100),
+
+ // Interface number, string index, EP notification address and size, EP data address (out, in) and size.
+ TUD_CDC_DESCRIPTOR(ITF_NUM_CDC, 4, EPNUM_CDC_NOTIF, 16, EPNUM_CDC_OUT, EPNUM_CDC_IN, 64),
+
+ // Interface number, string index, EP Bulk Out address, EP Bulk In address, EP size
+ TUD_PRINTER_DESCRIPTOR(ITF_NUM_PRINTER, 5, EPNUM_PRINTER_OUT, EPNUM_PRINTER_IN, 64),
+};
+
+#if TUD_OPT_HIGH_SPEED
+// high speed configuration
+static uint8_t const desc_hs_configuration[] = {
+ TUD_CONFIG_DESCRIPTOR(1, ITF_NUM_TOTAL, 0, CONFIG_TOTAL_LEN, 0x00, 100),
+
+ TUD_CDC_DESCRIPTOR(ITF_NUM_CDC, 4, EPNUM_CDC_NOTIF, 16, EPNUM_CDC_OUT, EPNUM_CDC_IN, 512),
+
+ TUD_PRINTER_DESCRIPTOR(ITF_NUM_PRINTER, 5, EPNUM_PRINTER_OUT, EPNUM_PRINTER_IN, 512),
+};
+
+// other speed configuration
+static uint8_t desc_other_speed_config[CONFIG_TOTAL_LEN];
+
+// device qualifier is mostly similar to device descriptor since we don't change configuration based on speed
+static tusb_desc_device_qualifier_t const desc_device_qualifier = {
+ .bLength = sizeof(tusb_desc_device_qualifier_t),
+ .bDescriptorType = TUSB_DESC_DEVICE_QUALIFIER,
+ .bcdUSB = USB_BCD,
+
+ .bDeviceClass = TUSB_CLASS_MISC,
+ .bDeviceSubClass = MISC_SUBCLASS_COMMON,
+ .bDeviceProtocol = MISC_PROTOCOL_IAD,
+
+ .bMaxPacketSize0 = CFG_TUD_ENDPOINT0_SIZE,
+ .bNumConfigurations = 0x01,
+ .bReserved = 0x00
+};
+
+uint8_t const *tud_descriptor_device_qualifier_cb(void) {
+ return (uint8_t const *) &desc_device_qualifier;
+}
+
+uint8_t const *tud_descriptor_other_speed_configuration_cb(uint8_t index) {
+ (void) index;
+
+ // if link speed is high return fullspeed config, and vice versa
+ memcpy(desc_other_speed_config,
+ (tud_speed_get() == TUSB_SPEED_HIGH) ? desc_fs_configuration : desc_hs_configuration,
+ CONFIG_TOTAL_LEN);
+
+ desc_other_speed_config[1] = TUSB_DESC_OTHER_SPEED_CONFIG;
+
+ return desc_other_speed_config;
+}
+
+#endif // TUD_OPT_HIGH_SPEED
+
+uint8_t const *tud_descriptor_configuration_cb(uint8_t index) {
+ (void) index;
+
+#if TUD_OPT_HIGH_SPEED
+ return (tud_speed_get() == TUSB_SPEED_HIGH) ? desc_hs_configuration : desc_fs_configuration;
+#else
+ return desc_fs_configuration;
+#endif
+}
+
+//--------------------------------------------------------------------+
+// String Descriptors
+//--------------------------------------------------------------------+
+
+enum {
+ STRID_LANGID = 0,
+ STRID_MANUFACTURER,
+ STRID_PRODUCT,
+ STRID_SERIAL,
+ STRID_CDC,
+ STRID_PRINTER,
+};
+
+static char const *string_desc_arr[] = {
+ (const char[]) { 0x09, 0x04 }, // 0: supported language is English (0x0409)
+ "TinyUSB", // 1: Manufacturer
+ "TinyUSB Device", // 2: Product
+ NULL, // 3: Serial, use unique ID if possible
+ "TinyUSB CDC", // 4: CDC Interface
+ "TinyUSB Printer", // 5: Printer Interface
+};
+
+static uint16_t _desc_str[32 + 1];
+
+uint16_t const *tud_descriptor_string_cb(uint8_t index, uint16_t langid) {
+ (void) langid;
+ size_t chr_count;
+
+ switch (index) {
+ case STRID_LANGID:
+ memcpy(&_desc_str[1], string_desc_arr[0], 2);
+ chr_count = 1;
+ break;
+
+ case STRID_SERIAL:
+ chr_count = board_usb_get_serial(_desc_str + 1, 32);
+ break;
+
+ default:
+ if (!(index < sizeof(string_desc_arr) / sizeof(string_desc_arr[0]))) { return NULL; }
+
+ const char *str = string_desc_arr[index];
+
+ chr_count = strlen(str);
+ size_t const max_count = sizeof(_desc_str) / sizeof(_desc_str[0]) - 1;
+ if (chr_count > max_count) { chr_count = max_count; }
+
+ for (size_t i = 0; i < chr_count; i++) {
+ _desc_str[1 + i] = str[i];
+ }
+ break;
+ }
+
+ _desc_str[0] = (uint16_t) ((TUSB_DESC_STRING << 8) | (2 * chr_count + 2));
+ return _desc_str;
+}
diff --git a/examples/device/printer_to_cdc/src/usb_descriptors.h b/examples/device/printer_to_cdc/src/usb_descriptors.h
new file mode 100644
index 000000000..830593b4d
--- /dev/null
+++ b/examples/device/printer_to_cdc/src/usb_descriptors.h
@@ -0,0 +1,37 @@
+/*
+ * The MIT License (MIT)
+ *
+ * Copyright (c) 2026 Ha Thach (tinyusb.org)
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
+ * of this software and associated documentation files (the "Software"), to deal
+ * in the Software without restriction, including without limitation the rights
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+ * copies of the Software, and to permit persons to whom the Software is
+ * furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included in
+ * all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+ * THE SOFTWARE.
+ */
+
+#ifndef USB_DESCRIPTORS_H_
+#define USB_DESCRIPTORS_H_
+
+enum {
+ ITF_NUM_CDC,
+ ITF_NUM_CDC_DATA,
+ ITF_NUM_PRINTER,
+ ITF_NUM_TOTAL,
+};
+
+#define CONFIG_TOTAL_LEN (TUD_CONFIG_DESC_LEN + TUD_CDC_DESC_LEN + TUD_PRINTER_DESC_LEN)
+
+#endif /* USB_DESCRIPTORS_H_ */
diff --git a/examples/device/uac2_headset/src/main.c b/examples/device/uac2_headset/src/main.c
index 96fa66f1e..0ea63d8f7 100644
--- a/examples/device/uac2_headset/src/main.c
+++ b/examples/device/uac2_headset/src/main.c
@@ -559,7 +559,7 @@ bool tud_audio_set_itf_cb(uint8_t rhport, tusb_control_request_t const *p_reques
// In a real application, this would be replaced with actual I2S send/receive callback.
void audio_task(void) {
static uint32_t start_ms = 0;
- uint32_t curr_ms = board_millis();
+ uint32_t curr_ms = tusb_time_millis_api();
if (start_ms == curr_ms) return;// not enough time
start_ms = curr_ms;
// When new data arrived, copy data from speaker buffer, to microphone buffer
@@ -605,7 +605,7 @@ void audio_control_task(void) {
static uint32_t start_ms = 0;
static uint32_t btn_prev = 0;
- if (board_millis() - start_ms < interval_ms) return;// not enough time
+ if (tusb_time_millis_api() - start_ms < interval_ms) return;// not enough time
start_ms += interval_ms;
uint32_t btn = board_button_read();
@@ -644,7 +644,7 @@ void led_blinking_task(void) {
static bool led_state = false;
// Blink every interval ms
- if (board_millis() - start_ms < blink_interval_ms) return;
+ if (tusb_time_millis_api() - start_ms < blink_interval_ms) return;
start_ms += blink_interval_ms;
board_led_write(led_state);
diff --git a/examples/device/uac2_speaker_fb/src/main.c b/examples/device/uac2_speaker_fb/src/main.c
index 7b4e2d64c..c3e97bb28 100644
--- a/examples/device/uac2_speaker_fb/src/main.c
+++ b/examples/device/uac2_speaker_fb/src/main.c
@@ -534,8 +534,9 @@ bool tud_audio_set_itf_close_ep_cb(uint8_t rhport, tusb_control_request_t const
uint8_t const itf = tu_u16_low(tu_le16toh(p_request->wIndex));
uint8_t const alt = tu_u16_low(tu_le16toh(p_request->wValue));
- if (ITF_NUM_AUDIO_STREAMING == itf && alt == 0)
+ if (ITF_NUM_AUDIO_STREAMING == itf && alt == 0) {
blink_interval_ms = BLINK_MOUNTED;
+ }
return true;
}
@@ -569,7 +570,8 @@ bool tud_audio_rx_done_isr(uint8_t rhport, uint16_t n_bytes_received, uint8_t fu
fifo_count = tud_audio_available();
// Same averaging method used in UAC2 class
- fifo_count_avg = (uint32_t) (((uint64_t) fifo_count_avg * 63 + ((uint32_t) fifo_count << 16)) >> 6);
+ const uint32_t ff_count32 = (uint32_t) fifo_count << 16;
+ fifo_count_avg = (uint32_t) (((uint64_t) fifo_count_avg * 63 + ff_count32) >> 6);
return true;
}
@@ -583,7 +585,7 @@ bool tud_audio_rx_done_isr(uint8_t rhport, uint16_t n_bytes_received, uint8_t fu
// In a real application, this would be replaced with actual I2S transmit callback.
void audio_task(void) {
static uint32_t start_ms = 0;
- uint32_t curr_ms = board_millis();
+ uint32_t curr_ms = tusb_time_millis_api();
if (start_ms == curr_ms) return;// not enough time
start_ms = curr_ms;
@@ -610,7 +612,7 @@ void led_blinking_task(void) {
static bool led_state = false;
// Blink every interval ms
- if (board_millis() - start_ms < blink_interval_ms) return;
+ if (tusb_time_millis_api() - start_ms < blink_interval_ms) return;
start_ms += blink_interval_ms;
board_led_write(led_state);
@@ -624,7 +626,7 @@ void led_blinking_task(void) {
// Every 1ms, we will sent 1 debug information report
void audio_debug_task(void) {
static uint32_t start_ms = 0;
- uint32_t curr_ms = board_millis();
+ uint32_t curr_ms = tusb_time_millis_api();
if (start_ms == curr_ms) return;// not enough time
start_ms = curr_ms;
diff --git a/examples/device/usbtmc/src/main.c b/examples/device/usbtmc/src/main.c
index 5cbbb85ef..b1269b117 100644
--- a/examples/device/usbtmc/src/main.c
+++ b/examples/device/usbtmc/src/main.c
@@ -124,12 +124,12 @@ void led_blinking_task(void)
{
led_state = true;
board_led_write(true);
- start_ms = board_millis();
+ start_ms = tusb_time_millis_api();
doPulse = false;
}
else if (led_state == true)
{
- if ( board_millis() - start_ms < 750) //Spec says blink must be between 500 and 1000 ms.
+ if ( tusb_time_millis_api() - start_ms < 750) //Spec says blink must be between 500 and 1000 ms.
{
return; // not enough time
}
@@ -140,7 +140,7 @@ void led_blinking_task(void)
else
{
// Blink every interval ms
- if ( board_millis() - start_ms < blink_interval_ms) return; // not enough time
+ if ( tusb_time_millis_api() - start_ms < blink_interval_ms) return; // not enough time
start_ms += blink_interval_ms;
board_led_write(led_state);
diff --git a/examples/device/usbtmc/src/usbtmc_app.c b/examples/device/usbtmc/src/usbtmc_app.c
index 4c3724ac4..35e8618f5 100644
--- a/examples/device/usbtmc/src/usbtmc_app.c
+++ b/examples/device/usbtmc/src/usbtmc_app.c
@@ -209,19 +209,19 @@ void usbtmc_app_task_iter(void) {
case 0:
break;
case 1:
- queryDelayStart = board_millis();
+ queryDelayStart = tusb_time_millis_api();
queryState = 2;
break;
case 2:
- if( (board_millis() - queryDelayStart) > resp_delay) {
- queryDelayStart = board_millis();
+ if( (tusb_time_millis_api() - queryDelayStart) > resp_delay) {
+ queryDelayStart = tusb_time_millis_api();
queryState=3;
status |= 0x10u; // MAV
status |= 0x40u; // SRQ
}
break;
case 3:
- if( (board_millis() - queryDelayStart) > resp_delay) {
+ if( (tusb_time_millis_api() - queryDelayStart) > resp_delay) {
queryState = 4;
}
break;
diff --git a/examples/device/video_capture/src/main.c b/examples/device/video_capture/src/main.c
index ffa2a7afa..df9f77a1c 100644
--- a/examples/device/video_capture/src/main.c
+++ b/examples/device/video_capture/src/main.c
@@ -231,7 +231,7 @@ static void video_send_frame(void) {
if (!already_sent) {
already_sent = 1;
tx_busy = 1;
- start_ms = board_millis();
+ start_ms = tusb_time_millis_api();
#if defined(CFG_EXAMPLE_VIDEO_BUFFERLESS)
tud_video_n_frame_xfer(0, 0, NULL, FRAME_WIDTH * FRAME_HEIGHT * 16 / 8);
#elif defined (CFG_EXAMPLE_VIDEO_READONLY)
@@ -247,7 +247,7 @@ static void video_send_frame(void) {
#endif
}
- unsigned cur = board_millis();
+ unsigned cur = tusb_time_millis_api();
if (cur - start_ms < interval_ms) {
return; // not enough time
}
@@ -316,7 +316,7 @@ void led_blinking_task(void* param) {
#if CFG_TUSB_OS == OPT_OS_FREERTOS
vTaskDelay(blink_interval_ms / portTICK_PERIOD_MS);
#else
- if (board_millis() - start_ms < blink_interval_ms) {
+ if (tusb_time_millis_api() - start_ms < blink_interval_ms) {
return; // not enough time
}
#endif
diff --git a/examples/device/video_capture_2ch/src/main.c b/examples/device/video_capture_2ch/src/main.c
index 79b149f2b..debd336fd 100644
--- a/examples/device/video_capture_2ch/src/main.c
+++ b/examples/device/video_capture_2ch/src/main.c
@@ -221,13 +221,13 @@ static void video_send_frame(uint_fast8_t ctl_idx, uint_fast8_t stm_idx) {
if (!(already_sent & (1u << idx))) {
already_sent |= 1u << idx;
tx_busy |= 1u << idx;
- start_ms[idx] = board_millis();
+ start_ms[idx] = tusb_time_millis_api();
fb_size = get_framebuf(ctl_idx, stm_idx, frame_num[idx], &fp);
tud_video_n_frame_xfer(ctl_idx, stm_idx, fp, fb_size);
}
- unsigned cur = board_millis();
+ unsigned cur = tusb_time_millis_api();
if (cur - start_ms[idx] < interval_ms[idx]) return; // not enough time
if (tx_busy & (1u << idx)) return;
start_ms[idx] += interval_ms[idx];
@@ -280,7 +280,7 @@ void led_blinking_task(void* param) {
#if CFG_TUSB_OS == OPT_OS_FREERTOS
vTaskDelay(blink_interval_ms / portTICK_PERIOD_MS);
#else
- if (board_millis() - start_ms < blink_interval_ms) return; // not enough time
+ if (tusb_time_millis_api() - start_ms < blink_interval_ms) return; // not enough time
#endif
start_ms += blink_interval_ms;
diff --git a/examples/device/webusb_serial/src/main.c b/examples/device/webusb_serial/src/main.c
index 155768b76..4be5e4db4 100644
--- a/examples/device/webusb_serial/src/main.c
+++ b/examples/device/webusb_serial/src/main.c
@@ -257,7 +257,7 @@ void led_blinking_task(void) {
static bool led_state = false;
// Blink every interval ms
- if (board_millis() - start_ms < blink_interval_ms) {
+ if (tusb_time_millis_api() - start_ms < blink_interval_ms) {
return; // not enough time
}
start_ms += blink_interval_ms;
diff --git a/examples/dual/CMakeLists.txt b/examples/dual/CMakeLists.txt
index 4978f1fab..8727ea938 100644
--- a/examples/dual/CMakeLists.txt
+++ b/examples/dual/CMakeLists.txt
@@ -12,6 +12,7 @@ else ()
set(EXAMPLE_LIST
host_hid_to_device_cdc
host_info_to_device_cdc
+ dynamic_switch
)
foreach (example ${EXAMPLE_LIST})
diff --git a/examples/dual/dynamic_switch/CMakeLists.txt b/examples/dual/dynamic_switch/CMakeLists.txt
new file mode 100644
index 000000000..7cad30727
--- /dev/null
+++ b/examples/dual/dynamic_switch/CMakeLists.txt
@@ -0,0 +1,30 @@
+cmake_minimum_required(VERSION 3.20)
+
+include(${CMAKE_CURRENT_SOURCE_DIR}/../../../hw/bsp/family_support.cmake)
+
+project(dynamic_switch C CXX ASM)
+
+# Checks this example is valid for the family and initializes the project
+family_initialize_project(${PROJECT_NAME} ${CMAKE_CURRENT_LIST_DIR})
+
+# Espressif has its own cmake build system
+if(FAMILY STREQUAL "espressif")
+ return()
+endif()
+
+add_executable(${PROJECT_NAME})
+
+# Example source
+target_sources(${PROJECT_NAME} PUBLIC
+ ${CMAKE_CURRENT_SOURCE_DIR}/src/main.c
+ ${CMAKE_CURRENT_SOURCE_DIR}/src/usb_descriptors.c
+ )
+
+# Example include
+target_include_directories(${PROJECT_NAME} PUBLIC
+ ${CMAKE_CURRENT_SOURCE_DIR}/src
+ )
+
+# Configure compilation flags and libraries for the example without RTOS.
+# See the corresponding function in hw/bsp/FAMILY/family.cmake for details.
+family_configure_dual_usb_example(${PROJECT_NAME} noos)
diff --git a/examples/dual/dynamic_switch/CMakePresets.json b/examples/dual/dynamic_switch/CMakePresets.json
new file mode 100644
index 000000000..5cd8971e9
--- /dev/null
+++ b/examples/dual/dynamic_switch/CMakePresets.json
@@ -0,0 +1,6 @@
+{
+ "version": 6,
+ "include": [
+ "../../../hw/bsp/BoardPresets.json"
+ ]
+}
diff --git a/examples/dual/dynamic_switch/Makefile b/examples/dual/dynamic_switch/Makefile
new file mode 100644
index 000000000..44ab54ad7
--- /dev/null
+++ b/examples/dual/dynamic_switch/Makefile
@@ -0,0 +1,16 @@
+include ../../../hw/bsp/family_support.mk
+
+INC += \
+ src \
+
+# Example source
+EXAMPLE_SOURCE += $(wildcard src/*.c)
+SRC_C += $(addprefix $(EXAMPLE_PATH)/, $(EXAMPLE_SOURCE))
+
+# Include device and host stack
+SRC_C += \
+ src/class/cdc/cdc_device.c \
+ src/host/hub.c \
+ src/host/usbh.c
+
+include ../../../hw/bsp/family_rules.mk
diff --git a/examples/dual/dynamic_switch/README.md b/examples/dual/dynamic_switch/README.md
new file mode 100644
index 000000000..034787f18
--- /dev/null
+++ b/examples/dual/dynamic_switch/README.md
@@ -0,0 +1,60 @@
+# Dynamic Switch Example
+
+This example demonstrates TinyUSB's dual-role capability by allowing runtime switching between USB device and host modes.
+
+## Features
+
+- **Button-triggered mode switching**: Press the board button to switch between device and host modes
+- **Device Mode**: Acts as a USB CDC (Virtual Serial Port) that echoes all received data
+- **Host Mode**: Enumerates connected USB devices and prints device information
+- **Dynamic switching**: Deinitializes the current stack and reinitializes in the new mode
+
+## Usage
+
+1. **Build and flash** the example to your board
+2. **Default behavior**: The board starts in **Device mode**
+3. **Device mode**:
+ - Connect the board to a PC
+ - Open a serial terminal (e.g., `screen /dev/ttyACM0` or PuTTY)
+ - Type characters - they will be echoed back to you
+4. **Switch to Host mode**:
+ - Press the board button
+ - Connect a USB device to the board
+ - The board will enumerate the device and print its descriptors to the debug console
+5. **Switch back to Device mode**: Press the button again
+
+## LED Patterns
+
+The onboard LED indicates the USB connection status:
+
+- **Fast blink (250ms)**: Not mounted/connected
+- **Slow blink (1000ms)**: Successfully mounted/connected
+- **Very slow blink (2500ms)**: Suspended (device mode only)
+
+## Serial Output
+
+The example prints status messages to the debug UART:
+
+```
+======================================
+TinyUSB Dynamic Switch Example
+Press button to switch between device and host modes
+Starting in DEVICE mode...
+======================================
+
+[DEVICE] Mounted
+
+--- Switching USB mode ---
+Stopping DEVICE mode...
+Starting HOST mode...
+Mode switch complete!
+
+[HOST] Device attached, address = 1
+Device 1: ID 1234:5678 SN ABC123
+Device Descriptor:
+ bLength 18
+ bDescriptorType 1
+ bcdUSB 0200
+ bDeviceClass 239
+ ...
+```
diff --git a/examples/dual/dynamic_switch/only.txt b/examples/dual/dynamic_switch/only.txt
new file mode 100644
index 000000000..70be49b28
--- /dev/null
+++ b/examples/dual/dynamic_switch/only.txt
@@ -0,0 +1,12 @@
+family:espressif
+mcu:LPC43XX
+mcu:MIMXRT1XXX
+mcu:STM32C0
+mcu:STM32G0
+mcu:STM32H5
+mcu:STM32F2
+mcu:STM32F4
+mcu:STM32U5
+mcu:STM32F7
+mcu:STM32H7
+mcu:STM32H7RS
diff --git a/examples/dual/dynamic_switch/src/CMakeLists.txt b/examples/dual/dynamic_switch/src/CMakeLists.txt
new file mode 100644
index 000000000..cef2b46ee
--- /dev/null
+++ b/examples/dual/dynamic_switch/src/CMakeLists.txt
@@ -0,0 +1,4 @@
+# This file is for ESP-IDF only
+idf_component_register(SRCS "main.c" "usb_descriptors.c"
+ INCLUDE_DIRS "."
+ REQUIRES boards tinyusb_src)
diff --git a/examples/dual/dynamic_switch/src/main.c b/examples/dual/dynamic_switch/src/main.c
new file mode 100644
index 000000000..c9ad2a835
--- /dev/null
+++ b/examples/dual/dynamic_switch/src/main.c
@@ -0,0 +1,496 @@
+/*
+ * The MIT License (MIT)
+ *
+ * Copyright (c) 2019 Ha Thach (tinyusb.org)
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
+ * of this software and associated documentation files (the "Software"), to deal
+ * in the Software without restriction, including without limitation the rights
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+ * copies of the Software, and to permit persons to whom the Software is
+ * furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included in
+ * all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+ * THE SOFTWARE.
+ *
+ */
+
+/* This example demonstrates dynamic switching between device and host modes:
+ * - Press button to switch between device and host modes
+ * - Device mode: CDC echo (echoes input back to output)
+ * - Host mode: Prints connected device information
+ */
+
+#include <stdlib.h>
+#include <stdio.h>
+#include <string.h>
+
+#include "bsp/board_api.h"
+#include "tusb.h"
+
+#if CFG_TUSB_OS == OPT_OS_FREERTOS
+ #ifdef ESP_PLATFORM
+ #define USBD_STACK_SIZE 4096
+ #define USBH_STACK_SIZE 4096
+ #else
+ // Increase stack size when debug log is enabled
+ #define USBD_STACK_SIZE (3*configMINIMAL_STACK_SIZE/2) * (CFG_TUSB_DEBUG ? 2 : 1)
+ #define USBH_STACK_SIZE (3*configMINIMAL_STACK_SIZE/2) * (CFG_TUSB_DEBUG ? 2 : 1)
+ #endif
+
+ #define CDC_STACK_SIZE (configMINIMAL_STACK_SIZE * (CFG_TUSB_DEBUG ? 2 : 1))
+ #define BLINKY_STACK_SIZE configMINIMAL_STACK_SIZE
+#endif
+
+//--------------------------------------------------------------------+
+// MACRO CONSTANT TYPEDEF PROTOTYPES
+//--------------------------------------------------------------------+
+
+// English
+#define LANGUAGE_ID 0x0409
+
+/* Blink pattern
+ * - 250 ms : not mounted
+ * - 1000 ms : mounted
+ * - 2500 ms : suspended
+ */
+enum {
+ BLINK_NOT_MOUNTED = 250,
+ BLINK_MOUNTED = 1000,
+ BLINK_SUSPENDED = 2500,
+};
+
+#if CFG_TUSB_OS == OPT_OS_FREERTOS
+// static task for FreeRTOS
+#if configSUPPORT_STATIC_ALLOCATION
+StackType_t blinky_stack[BLINKY_STACK_SIZE];
+StaticTask_t blinky_taskdef;
+
+StackType_t usb_stack[USBD_STACK_SIZE > USBH_STACK_SIZE ? USBD_STACK_SIZE : USBH_STACK_SIZE];
+StaticTask_t usb_taskdef;
+
+StackType_t cdc_stack[CDC_STACK_SIZE];
+StaticTask_t cdc_taskdef;
+#endif
+#endif
+
+static uint32_t blink_interval_ms = BLINK_NOT_MOUNTED;
+static tusb_role_t current_role = TUSB_ROLE_DEVICE;
+
+#if CFG_TUSB_OS == OPT_OS_FREERTOS
+static void usb_task(void *param);
+void led_blinking_task(void *param);
+void cdc_task(void *params);
+#else
+void led_blinking_task(void);
+void cdc_task(void);
+#endif
+void usb_mode_switch(void);
+static void print_device_info(uint8_t daddr);
+static void print_utf16(uint16_t* temp_buf, size_t buf_len);
+
+// Declare buffer for USB transfer
+CFG_TUH_MEM_SECTION struct {
+ TUH_EPBUF_TYPE_DEF(tusb_desc_device_t, device);
+ TUH_EPBUF_DEF(serial, 64*sizeof(uint16_t));
+ TUH_EPBUF_DEF(buf, 128*sizeof(uint16_t));
+} desc;
+
+//--------------------------------------------------------------------+
+// Main
+//--------------------------------------------------------------------+
+
+int main(void) {
+ board_init();
+
+ printf("\r\n======================================\r\n");
+ printf("TinyUSB Dynamic Switch Example\r\n");
+ printf("Press button to switch between device and host modes\r\n");
+ printf("Starting in DEVICE mode...\r\n");
+ printf("======================================\r\n\r\n");
+
+#if CFG_TUSB_OS == OPT_OS_FREERTOS
+ // Create FreeRTOS tasks
+#if configSUPPORT_STATIC_ALLOCATION
+ xTaskCreateStatic(led_blinking_task, "blinky", BLINKY_STACK_SIZE, NULL, 1, blinky_stack, &blinky_taskdef);
+ xTaskCreateStatic(usb_task, "usb", USBD_STACK_SIZE > USBH_STACK_SIZE ? USBD_STACK_SIZE : USBH_STACK_SIZE,
+ NULL, configMAX_PRIORITIES-1, usb_stack, &usb_taskdef);
+ xTaskCreateStatic(cdc_task, "cdc", CDC_STACK_SIZE, NULL, configMAX_PRIORITIES - 2, cdc_stack, &cdc_taskdef);
+#else
+ xTaskCreate(led_blinking_task, "blinky", BLINKY_STACK_SIZE, NULL, 1, NULL);
+ xTaskCreate(usb_task, "usb", USBD_STACK_SIZE > USBH_STACK_SIZE ? USBD_STACK_SIZE : USBH_STACK_SIZE,
+ NULL, configMAX_PRIORITIES - 1, NULL);
+ xTaskCreate(cdc_task, "cdc", CDC_STACK_SIZE, NULL, configMAX_PRIORITIES - 2, NULL);
+#endif
+
+#ifndef ESP_PLATFORM
+ // only start scheduler for non-espressif mcu
+ vTaskStartScheduler();
+#endif
+
+#else
+ // Initialize in device mode by default
+ tusb_rhport_init_t dev_init = {
+ .role = TUSB_ROLE_DEVICE,
+ .speed = TUSB_SPEED_AUTO
+ };
+ tusb_init(BOARD_RHPORT, &dev_init);
+ current_role = TUSB_ROLE_DEVICE;
+
+ board_init_after_tusb();
+
+ while (1) {
+ // Check for button press to switch modes
+ static bool pending_switch = false;
+ if (board_button_read()) {
+ if (!pending_switch) {
+ pending_switch = true;
+ usb_mode_switch();
+ }
+ } else {
+ pending_switch = false;
+ }
+
+ // Process USB tasks based on current mode
+ if (current_role == TUSB_ROLE_DEVICE) {
+ tud_task();
+ cdc_task();
+ } else {
+ tuh_task();
+ }
+
+ led_blinking_task();
+ }
+#endif
+}
+
+#ifdef ESP_PLATFORM
+void app_main(void) {
+ main();
+}
+#endif
+
+#if CFG_TUSB_OS == OPT_OS_FREERTOS
+// USB Task for FreeRTOS
+// This top level thread processes all usb events and mode switching
+static void usb_task(void *param) {
+ (void) param;
+
+ // init device stack on configured roothub port
+ // This should be called after scheduler/kernel is started.
+ // Otherwise it could cause kernel issue since USB IRQ handler does use RTOS queue API.
+ tusb_rhport_init_t dev_init = {
+ .role = TUSB_ROLE_DEVICE,
+ .speed = TUSB_SPEED_AUTO
+ };
+ tusb_init(BOARD_RHPORT, &dev_init);
+ current_role = TUSB_ROLE_DEVICE;
+
+ board_init_after_tusb();
+
+ // RTOS forever loop
+ while (1) {
+ // Check for button press to switch modes
+ static bool pending_switch = false;
+ if (board_button_read()) {
+ if (!pending_switch) {
+ pending_switch = true;
+ usb_mode_switch();
+ }
+ } else {
+ pending_switch = false;
+ }
+
+ // Process USB tasks based on current mode
+ // Use _ext version to allow return and read button state
+ if (current_role == TUSB_ROLE_DEVICE) {
+ tud_task_ext(10, false);
+ } else {
+ tuh_task_ext(10, false);
+ }
+ }
+}
+#endif
+
+//--------------------------------------------------------------------+
+// Mode Switching
+//--------------------------------------------------------------------+
+
+void usb_mode_switch(void) {
+ printf("\r\n--- Switching USB mode ---\r\n");
+
+ // Deinitialize current mode
+ if (current_role == TUSB_ROLE_DEVICE) {
+ printf("Stopping DEVICE mode...\r\n");
+ tusb_deinit(BOARD_RHPORT);
+ } else {
+ printf("Stopping HOST mode...\r\n");
+ tusb_deinit(BOARD_RHPORT);
+ }
+
+#if CFG_TUSB_OS == OPT_OS_FREERTOS
+ vTaskDelay(pdMS_TO_TICKS(100)); // Small delay for clean transition
+#else
+ tusb_time_delay_ms_api(100); // Small delay for clean transition
+#endif // Switch to the other mode
+ if (current_role == TUSB_ROLE_DEVICE) {
+ printf("Starting HOST mode...\r\n");
+ tusb_rhport_init_t host_init = {
+ .role = TUSB_ROLE_HOST,
+ .speed = TUSB_SPEED_AUTO
+ };
+ tusb_init(BOARD_RHPORT, &host_init);
+ current_role = TUSB_ROLE_HOST;
+ } else {
+ printf("Starting DEVICE mode...\r\n");
+ tusb_rhport_init_t dev_init = {
+ .role = TUSB_ROLE_DEVICE,
+ .speed = TUSB_SPEED_AUTO
+ };
+ tusb_init(BOARD_RHPORT, &dev_init);
+ current_role = TUSB_ROLE_DEVICE;
+ }
+
+ blink_interval_ms = BLINK_NOT_MOUNTED;
+ printf("Mode switch complete!\r\n\r\n");
+}
+
+//--------------------------------------------------------------------+
+// Device Mode: CDC Task
+//--------------------------------------------------------------------+
+
+#if CFG_TUSB_OS == OPT_OS_FREERTOS
+void cdc_task(void *params) {
+ (void) params;
+
+ // RTOS forever loop
+ while (1) {
+ // Only process CDC when in device mode
+ if (current_role == TUSB_ROLE_DEVICE) {
+ // Connected and there are data available
+ while (tud_cdc_available()) {
+ uint8_t buf[64];
+
+ // Read data
+ uint32_t count = tud_cdc_read(buf, sizeof(buf));
+
+ // Echo back
+ tud_cdc_write(buf, count);
+
+ // Add newline for carriage return
+ for (uint32_t i = 0; i < count; i++) {
+ if (buf[i] == '\r') {
+ tud_cdc_write_char('\n');
+ break;
+ }
+ }
+ }
+
+ tud_cdc_write_flush();
+ }
+
+ vTaskDelay(pdMS_TO_TICKS(10));
+ }
+}
+#else
+void cdc_task(void) {
+ // Connected and there are data available
+ if (tud_cdc_available()) {
+ uint8_t buf[64];
+
+ // Read data
+ uint32_t count = tud_cdc_read(buf, sizeof(buf));
+
+ // Echo back
+ 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();
+ }
+}
+#endif
+
+//--------------------------------------------------------------------+
+// Device Callbacks
+//--------------------------------------------------------------------+
+
+// Invoked when device is mounted
+void tud_mount_cb(void) {
+ printf("[DEVICE] Mounted\r\n");
+ blink_interval_ms = BLINK_MOUNTED;
+}
+
+// Invoked when device is unmounted
+void tud_umount_cb(void) {
+ printf("[DEVICE] Unmounted\r\n");
+ blink_interval_ms = BLINK_NOT_MOUNTED;
+}
+
+// Invoked when usb bus is suspended
+void tud_suspend_cb(bool remote_wakeup_en) {
+ (void) remote_wakeup_en;
+ printf("[DEVICE] Suspended\r\n");
+ blink_interval_ms = BLINK_SUSPENDED;
+}
+
+// Invoked when usb bus is resumed
+void tud_resume_cb(void) {
+ printf("[DEVICE] Resumed\r\n");
+ blink_interval_ms = tud_mounted() ? BLINK_MOUNTED : BLINK_NOT_MOUNTED;
+}
+
+//--------------------------------------------------------------------+
+// Host Callbacks
+//--------------------------------------------------------------------+
+
+// Invoked when device is mounted (configured)
+void tuh_mount_cb(uint8_t daddr) {
+ printf("[HOST] Device attached, address = %d\r\n", daddr);
+ blink_interval_ms = BLINK_MOUNTED;
+ print_device_info(daddr);
+}
+
+// Invoked when device is unmounted (unplugged)
+void tuh_umount_cb(uint8_t daddr) {
+ printf("[HOST] Device removed, address = %d\r\n", daddr);
+ blink_interval_ms = BLINK_NOT_MOUNTED;
+}
+
+//--------------------------------------------------------------------+
+// Host Device Info
+//--------------------------------------------------------------------+
+
+static void print_device_info(uint8_t daddr) {
+ // Get Device Descriptor
+ uint8_t xfer_result = tuh_descriptor_get_device_sync(daddr, &desc.device, 18);
+ if (XFER_RESULT_SUCCESS != xfer_result) {
+ printf("Failed to get device descriptor\r\n");
+ return;
+ }
+
+ printf("Device %u: ID %04x:%04x SN ", daddr, desc.device.idVendor, desc.device.idProduct);
+
+ xfer_result = XFER_RESULT_FAILED;
+ if (desc.device.iSerialNumber != 0) {
+ xfer_result = tuh_descriptor_get_serial_string_sync(daddr, LANGUAGE_ID, desc.serial, sizeof(desc.serial));
+ }
+ if (XFER_RESULT_SUCCESS != xfer_result) {
+ uint16_t* serial = (uint16_t*)(uintptr_t) desc.serial;
+ serial[0] = (uint16_t) ((TUSB_DESC_STRING << 8) | (2 * 3 + 2));
+ serial[1] = 'n';
+ serial[2] = '/';
+ serial[3] = 'a';
+ serial[4] = 0;
+ }
+ print_utf16((uint16_t*)(uintptr_t) desc.serial, sizeof(desc.serial)/2);
+ printf("\r\n");
+
+ printf("Device Descriptor:\r\n");
+ printf(" bLength %u\r\n", desc.device.bLength);
+ printf(" bDescriptorType %u\r\n", desc.device.bDescriptorType);
+ printf(" bcdUSB %04x\r\n", desc.device.bcdUSB);
+ printf(" bDeviceClass %u\r\n", desc.device.bDeviceClass);
+ printf(" bDeviceSubClass %u\r\n", desc.device.bDeviceSubClass);
+ printf(" bDeviceProtocol %u\r\n", desc.device.bDeviceProtocol);
+ printf(" bMaxPacketSize0 %u\r\n", desc.device.bMaxPacketSize0);
+ printf(" idVendor 0x%04x\r\n", desc.device.idVendor);
+ printf(" idProduct 0x%04x\r\n", desc.device.idProduct);
+ printf(" bcdDevice %04x\r\n", desc.device.bcdDevice);
+
+ // Get Manufacturer string
+ if (desc.device.iManufacturer) {
+ if (XFER_RESULT_SUCCESS == tuh_descriptor_get_manufacturer_string_sync(daddr, LANGUAGE_ID, desc.buf, sizeof(desc.buf))) {
+ printf(" iManufacturer %u ", desc.device.iManufacturer);
+ print_utf16((uint16_t*)(uintptr_t) desc.buf, sizeof(desc.buf)/2);
+ printf("\r\n");
+ }
+ }
+
+ // Get Product string
+ if (desc.device.iProduct) {
+ if (XFER_RESULT_SUCCESS == tuh_descriptor_get_product_string_sync(daddr, LANGUAGE_ID, desc.buf, sizeof(desc.buf))) {
+ printf(" iProduct %u ", desc.device.iProduct);
+ print_utf16((uint16_t*)(uintptr_t) desc.buf, sizeof(desc.buf)/2);
+ printf("\r\n");
+ }
+ }
+
+ // Get Serial string
+ if (desc.device.iSerialNumber) {
+ printf(" iSerialNumber %u ", desc.device.iSerialNumber);
+ print_utf16((uint16_t*)(uintptr_t) desc.serial, sizeof(desc.serial)/2);
+ printf("\r\n");
+ } else {
+ printf(" iSerialNumber 0\r\n");
+ }
+
+ printf(" bNumConfigurations %u\r\n", desc.device.bNumConfigurations);
+ printf("\r\n");
+}
+
+static void print_utf16(uint16_t* temp_buf, size_t buf_len) {
+ if (temp_buf[0] == 0 || (temp_buf[0] >> 8) != TUSB_DESC_STRING) {
+ printf("(invalid)");
+ return;
+ }
+
+ size_t chr_count = (temp_buf[0] & 0xff) / 2 - 1;
+ if (chr_count > buf_len - 1) {
+ chr_count = buf_len - 1;
+ }
+
+ for (size_t i = 0; i < chr_count; i++) {
+ uint16_t ch = temp_buf[1 + i];
+ if (ch <= 0x7F) {
+ putchar((char) ch);
+ } else {
+ // TODO support UTF16 to UTF8 conversion
+ putchar('?');
+ }
+ }
+}
+
+//--------------------------------------------------------------------+
+// Blinking Task
+//--------------------------------------------------------------------+
+
+#if CFG_TUSB_OS == OPT_OS_FREERTOS
+void led_blinking_task(void *param) {
+ (void) param;
+ static bool led_state = false;
+
+ // RTOS forever loop
+ while (1) {
+ board_led_write(led_state);
+ led_state = 1 - led_state; // toggle
+ vTaskDelay(pdMS_TO_TICKS(blink_interval_ms));
+ }
+}
+#else
+void led_blinking_task(void) {
+ static uint32_t start_ms = 0;
+ static bool led_state = false;
+
+ // Blink every interval ms
+ if (tusb_time_millis_api() - start_ms < blink_interval_ms) {
+ return; // not enough time
+ }
+ start_ms += blink_interval_ms;
+
+ board_led_write(led_state);
+ led_state = 1 - led_state; // toggle
+}
+#endif
diff --git a/examples/dual/dynamic_switch/src/tusb_config.h b/examples/dual/dynamic_switch/src/tusb_config.h
new file mode 100644
index 000000000..af0d55f14
--- /dev/null
+++ b/examples/dual/dynamic_switch/src/tusb_config.h
@@ -0,0 +1,158 @@
+/*
+ * The MIT License (MIT)
+ *
+ * Copyright (c) 2019 Ha Thach (tinyusb.org)
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
+ * of this software and associated documentation files (the "Software"), to deal
+ * in the Software without restriction, including without limitation the rights
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+ * copies of the Software, and to permit persons to whom the Software is
+ * furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included in
+ * all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+ * THE SOFTWARE.
+ *
+ */
+
+#ifndef _TUSB_CONFIG_H_
+#define _TUSB_CONFIG_H_
+
+#ifdef __cplusplus
+extern "C" {
+#endif
+
+//--------------------------------------------------------------------+
+// Board Specific Configuration
+//--------------------------------------------------------------------+
+
+// RHPort number used can be defined by board.mk, default to port 0
+#ifndef BOARD_RHPORT
+ #if defined(BOARD_TUD_RHPORT)
+ #define BOARD_RHPORT BOARD_TUD_RHPORT
+ #else
+ #define BOARD_RHPORT 0
+ #define BOARD_TUD_RHPORT 0
+ #endif
+#endif
+
+#if defined(BOARD_TUH_RHPORT)
+ #if BOARD_TUH_RHPORT != BOARD_RHPORT
+ #undef BOARD_TUH_RHPORT
+ #define BOARD_TUH_RHPORT BOARD_RHPORT
+ #endif
+#else
+ #define BOARD_TUH_RHPORT BOARD_RHPORT
+#endif
+
+// RHPort max operational speed can defined by board.mk
+#ifndef BOARD_MAX_SPEED
+ #if defined(BOARD_TUD_MAX_SPEED)
+ #define BOARD_MAX_SPEED BOARD_TUD_MAX_SPEED
+ #else
+ #define BOARD_MAX_SPEED OPT_MODE_DEFAULT_SPEED
+ #endif
+#endif
+
+//--------------------------------------------------------------------
+// COMMON CONFIGURATION
+//--------------------------------------------------------------------
+
+// defined by compiler flags for flexibility
+#ifndef CFG_TUSB_MCU
+#error CFG_TUSB_MCU must be defined
+#endif
+
+#ifndef CFG_TUSB_OS
+#define CFG_TUSB_OS OPT_OS_NONE
+#endif
+
+#ifndef CFG_TUSB_DEBUG
+#define CFG_TUSB_DEBUG 0
+#endif
+
+// Enable Device and Host stacks (dual role)
+#define CFG_TUD_ENABLED 1
+#define CFG_TUH_ENABLED 1
+
+// Default is max speed that hardware controller could support with on-chip PHY
+#define CFG_TUD_MAX_SPEED BOARD_MAX_SPEED
+#define CFG_TUH_MAX_SPEED BOARD_MAX_SPEED
+
+/* USB DMA on some MCUs can only access a specific SRAM region with restriction on alignment.
+ * Tinyusb use follows macros to declare transferring memory so that they can be put
+ * into those specific section.
+ * e.g
+ * - CFG_TUSB_MEM SECTION : __attribute__ (( section(".usb_ram") ))
+ * - CFG_TUSB_MEM_ALIGN : __attribute__ ((aligned(4)))
+ */
+#ifndef CFG_TUD_MEM_SECTION
+#define CFG_TUD_MEM_SECTION
+#endif
+
+#ifndef CFG_TUD_MEM_ALIGN
+#define CFG_TUD_MEM_ALIGN __attribute__((aligned(4)))
+#endif
+
+#ifndef CFG_TUH_MEM_SECTION
+#define CFG_TUH_MEM_SECTION CFG_TUD_MEM_SECTION
+#endif
+
+#ifndef CFG_TUH_MEM_ALIGN
+#define CFG_TUH_MEM_ALIGN CFG_TUD_MEM_ALIGN
+#endif
+
+//--------------------------------------------------------------------
+// DEVICE CONFIGURATION
+//--------------------------------------------------------------------
+
+#ifndef CFG_TUD_ENDPOINT0_SIZE
+#define CFG_TUD_ENDPOINT0_SIZE 64
+#endif
+
+//------------- CLASS -------------//
+#define CFG_TUD_CDC 1
+#define CFG_TUD_MSC 0
+#define CFG_TUD_HID 0
+#define CFG_TUD_MIDI 0
+#define CFG_TUD_VENDOR 0
+
+// CDC FIFO size of TX and RX
+#define CFG_TUD_CDC_RX_BUFSIZE (TUD_OPT_HIGH_SPEED ? 512 : 64)
+#define CFG_TUD_CDC_TX_BUFSIZE (TUD_OPT_HIGH_SPEED ? 512 : 64)
+
+// CDC Endpoint transfer buffer size, more is faster
+#define CFG_TUD_CDC_EP_BUFSIZE (TUD_OPT_HIGH_SPEED ? 512 : 64)
+
+//--------------------------------------------------------------------
+// HOST CONFIGURATION
+//--------------------------------------------------------------------
+
+// Size of buffer to hold descriptors and other data used for enumeration
+#define CFG_TUH_ENUMERATION_BUFSIZE 256
+
+#define CFG_TUH_HUB 1
+// max device support (excluding hub device)
+#define CFG_TUH_DEVICE_MAX (CFG_TUH_HUB ? 4 : 1) // hub typically has 4 ports
+
+#define CFG_TUH_CDC 0
+#define CFG_TUH_HID 0
+#define CFG_TUH_MSC 0
+#define CFG_TUH_VENDOR 0
+
+// max endpoint pair supported by each device
+#define CFG_TUH_ENDPOINT_MAX 16
+
+#ifdef __cplusplus
+}
+#endif
+
+#endif /* _TUSB_CONFIG_H_ */
diff --git a/examples/dual/dynamic_switch/src/usb_descriptors.c b/examples/dual/dynamic_switch/src/usb_descriptors.c
new file mode 100644
index 000000000..54ffc2c18
--- /dev/null
+++ b/examples/dual/dynamic_switch/src/usb_descriptors.c
@@ -0,0 +1,199 @@
+/*
+ * The MIT License (MIT)
+ *
+ * Copyright (c) 2019 Ha Thach (tinyusb.org)
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
+ * of this software and associated documentation files (the "Software"), to deal
+ * in the Software without restriction, including without limitation the rights
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+ * copies of the Software, and to permit persons to whom the Software is
+ * furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included in
+ * all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+ * THE SOFTWARE.
+ *
+ */
+
+#include "bsp/board_api.h"
+#include "tusb.h"
+
+/* A combination of interfaces must have a unique product id, since PC will save device driver after the first plug.
+ * Same VID/PID with different interface e.g MSC (first), then CDC (later) will possibly cause system error on PC.
+ *
+ * Auto ProductID layout's Bitmap:
+ * [MSB] AUDIO | MIDI | HID | MSC | CDC [LSB]
+ */
+#define PID_MAP(itf, n) ((CFG_TUD_##itf) ? (1 << (n)) : 0)
+#define USB_PID (0x4000 | PID_MAP(CDC, 0) | PID_MAP(MSC, 1) | PID_MAP(HID, 2) | \
+ PID_MAP(MIDI, 3) | PID_MAP(VENDOR, 4))
+
+#define USB_VID 0xCafe
+#define USB_BCD 0x0200
+
+//--------------------------------------------------------------------+
+// Device Descriptors
+//--------------------------------------------------------------------+
+static tusb_desc_device_t const desc_device = {
+ .bLength = sizeof(tusb_desc_device_t),
+ .bDescriptorType = TUSB_DESC_DEVICE,
+ .bcdUSB = USB_BCD,
+ .bDeviceClass = TUSB_CLASS_MISC,
+ .bDeviceSubClass = MISC_SUBCLASS_COMMON,
+ .bDeviceProtocol = MISC_PROTOCOL_IAD,
+
+ .bMaxPacketSize0 = CFG_TUD_ENDPOINT0_SIZE,
+
+ .idVendor = USB_VID,
+ .idProduct = USB_PID,
+ .bcdDevice = 0x0100,
+
+ .iManufacturer = 0x01,
+ .iProduct = 0x02,
+ .iSerialNumber = 0x03,
+
+ .bNumConfigurations = 0x01
+};
+
+// Invoked when received GET DEVICE DESCRIPTOR
+// Application return pointer to descriptor
+uint8_t const *tud_descriptor_device_cb(void) {
+ return (uint8_t const *) &desc_device;
+}
+
+//--------------------------------------------------------------------+
+// Configuration Descriptor
+//--------------------------------------------------------------------+
+
+enum {
+ ITF_NUM_CDC = 0,
+ ITF_NUM_CDC_DATA,
+ ITF_NUM_TOTAL
+};
+
+#if CFG_TUSB_MCU == OPT_MCU_LPC175X_6X || CFG_TUSB_MCU == OPT_MCU_LPC177X_8X || CFG_TUSB_MCU == OPT_MCU_LPC40XX
+ // LPC 17xx and 40xx endpoint type (bulk/interrupt/iso) are fixed by its number
+ // 0 control, 1 In, 2 Bulk, 3 Iso, 4 In, 5 Bulk etc ...
+ #define EPNUM_CDC_NOTIF 0x81
+ #define EPNUM_CDC_OUT 0x02
+ #define EPNUM_CDC_IN 0x82
+
+#elif defined(TUD_ENDPOINT_ONE_DIRECTION_ONLY)
+ // MCUs that don't support a same endpoint number with different direction IN and OUT defined in tusb_mcu.h
+ // e.g EP1 OUT & EP1 IN cannot exist together
+ #define EPNUM_CDC_NOTIF 0x81
+ #define EPNUM_CDC_OUT 0x02
+ #define EPNUM_CDC_IN 0x83
+
+#else
+ #define EPNUM_CDC_NOTIF 0x81
+ #define EPNUM_CDC_OUT 0x02
+ #define EPNUM_CDC_IN 0x82
+
+#endif
+
+#define CONFIG_TOTAL_LEN (TUD_CONFIG_DESC_LEN + TUD_CDC_DESC_LEN)
+
+static uint8_t const desc_fs_configuration[] = {
+ // Config number, interface count, string index, total length, attribute, power in mA
+ TUD_CONFIG_DESCRIPTOR(1, ITF_NUM_TOTAL, 0, CONFIG_TOTAL_LEN, TUSB_DESC_CONFIG_ATT_REMOTE_WAKEUP, 100),
+
+ // Interface number, string index, EP notification address and size, EP data address (out, in) and size.
+ TUD_CDC_DESCRIPTOR(ITF_NUM_CDC, 4, EPNUM_CDC_NOTIF, 8, EPNUM_CDC_OUT, EPNUM_CDC_IN, 64),
+};
+
+#if TUD_OPT_HIGH_SPEED
+static uint8_t const desc_hs_configuration[] = {
+ // Config number, interface count, string index, total length, attribute, power in mA
+ TUD_CONFIG_DESCRIPTOR(1, ITF_NUM_TOTAL, 0, CONFIG_TOTAL_LEN, TUSB_DESC_CONFIG_ATT_REMOTE_WAKEUP, 100),
+
+ // Interface number, string index, EP notification address and size, EP data address (out, in) and size.
+ TUD_CDC_DESCRIPTOR(ITF_NUM_CDC, 4, EPNUM_CDC_NOTIF, 8, EPNUM_CDC_OUT, EPNUM_CDC_IN, 512),
+};
+#endif
+
+// Invoked when received GET CONFIGURATION DESCRIPTOR
+// Application return pointer to descriptor
+// Descriptor contents must exist long enough for transfer to complete
+uint8_t const *tud_descriptor_configuration_cb(uint8_t index) {
+ (void) index; // for multiple configurations
+
+#if TUD_OPT_HIGH_SPEED
+ // Although we are highspeed, host may be fullspeed.
+ return (tud_speed_get() == TUSB_SPEED_HIGH) ? desc_hs_configuration : desc_fs_configuration;
+#else
+ return desc_fs_configuration;
+#endif
+}
+
+//--------------------------------------------------------------------+
+// String Descriptors
+//--------------------------------------------------------------------+
+
+// String Descriptor Index
+enum {
+ STRID_LANGID = 0,
+ STRID_MANUFACTURER,
+ STRID_PRODUCT,
+ STRID_SERIAL,
+};
+
+// array of pointer to string descriptors
+static char const *string_desc_arr[] = {
+ (const char[]) { 0x09, 0x04 }, // 0: is supported language is English (0x0409)
+ "TinyUSB", // 1: Manufacturer
+ "TinyUSB Device", // 2: Product
+ NULL, // 3: Serials, will use unique ID if possible
+ "TinyUSB CDC", // 4: CDC Interface
+};
+
+static uint16_t _desc_str[32 + 1];
+
+// Invoked when received GET STRING DESCRIPTOR request
+// Application return pointer to descriptor, whose contents must exist long enough for transfer to complete
+uint16_t const *tud_descriptor_string_cb(uint8_t index, uint16_t langid) {
+ (void) langid;
+ size_t chr_count;
+
+ switch (index) {
+ case STRID_LANGID:
+ memcpy(&_desc_str[1], string_desc_arr[0], 2);
+ chr_count = 1;
+ break;
+
+ case STRID_SERIAL:
+ chr_count = board_usb_get_serial(_desc_str + 1, 32);
+ break;
+
+ default:
+ // Note: the 0xEE index string is a Microsoft OS 1.0 Descriptors.
+ // https://docs.microsoft.com/en-us/windows-hardware/drivers/usbcon/microsoft-defined-usb-descriptors
+
+ if (!(index < sizeof(string_desc_arr) / sizeof(string_desc_arr[0]))) { return NULL; }
+
+ const char *str = string_desc_arr[index];
+
+ // Cap at max char
+ chr_count = strlen(str);
+ size_t const max_count = sizeof(_desc_str) / sizeof(_desc_str[0]) - 1; // -1 for string type
+ if (chr_count > max_count) { chr_count = max_count; }
+
+ // Convert ASCII string into UTF-16
+ for (size_t i = 0; i < chr_count; i++) {
+ _desc_str[1 + i] = str[i];
+ }
+ break;
+ }
+
+ // first byte is length (including header), second byte is string type
+ _desc_str[0] = (uint16_t) ((TUSB_DESC_STRING << 8) | (2 * chr_count + 2));
+ return _desc_str;
+}
diff --git a/examples/dual/host_hid_to_device_cdc/src/main.c b/examples/dual/host_hid_to_device_cdc/src/main.c
index 8c53588c3..c8fca48f8 100644
--- a/examples/dual/host_hid_to_device_cdc/src/main.c
+++ b/examples/dual/host_hid_to_device_cdc/src/main.c
@@ -98,8 +98,6 @@ int main(void) {
tuh_task(); // tinyusb host task
led_blinking_task();
}
-
- return 0;
}
//--------------------------------------------------------------------+
@@ -284,7 +282,7 @@ 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 (tusb_time_millis_api() - start_ms < blink_interval_ms) return; // not enough time
start_ms += blink_interval_ms;
board_led_write(led_state);
diff --git a/examples/dual/host_info_to_device_cdc/src/main.c b/examples/dual/host_info_to_device_cdc/src/main.c
index fffb54d58..7cf43aef3 100644
--- a/examples/dual/host_info_to_device_cdc/src/main.c
+++ b/examples/dual/host_info_to_device_cdc/src/main.c
@@ -213,13 +213,13 @@ void cdc_task(void) {
static uint32_t connected_ms = 0;
if (!tud_cdc_connected()) {
- connected_ms = board_millis();
+ connected_ms = tusb_time_millis_api();
return;
}
// delay a bit otherwise we can outpace host's terminal. Linux will set LineState (DTR) then Line Coding.
// If we send data before Linux's terminal set Line Coding, it can be ignored --> missing data with hardware test loop
- if (board_millis() - connected_ms < 100) {
+ if (tusb_time_millis_api() - connected_ms < 100) {
return; // wait for stable connection
}
@@ -309,7 +309,7 @@ void led_blinking_task(void) {
static bool led_state = false;
// Blink every interval ms
- if (board_millis() - start_ms < blink_interval_ms) {
+ if (tusb_time_millis_api() - start_ms < blink_interval_ms) {
return;// not enough time
}
start_ms += blink_interval_ms;
diff --git a/examples/host/bare_api/only.txt b/examples/host/bare_api/only.txt
index f07d63f9d..1ddfc2b5c 100644
--- a/examples/host/bare_api/only.txt
+++ b/examples/host/bare_api/only.txt
@@ -8,6 +8,8 @@ mcu:LPC177X_8X
mcu:LPC18XX
mcu:LPC40XX
mcu:LPC43XX
+mcu:LPC54
+mcu:LPC55
mcu:MAX3421
mcu:MIMXRT10XX
mcu:MIMXRT11XX
diff --git a/examples/host/bare_api/skip.txt b/examples/host/bare_api/skip.txt
new file mode 100644
index 000000000..308796869
--- /dev/null
+++ b/examples/host/bare_api/skip.txt
@@ -0,0 +1 @@
+board:lpcxpresso54114
diff --git a/examples/host/bare_api/src/main.c b/examples/host/bare_api/src/main.c
index c693d6b00..544f38102 100644
--- a/examples/host/bare_api/src/main.c
+++ b/examples/host/bare_api/src/main.c
@@ -37,11 +37,13 @@
#define BUF_COUNT 4
-tusb_desc_device_t desc_device;
+CFG_TUH_MEM_SECTION tusb_desc_device_t desc_device;
-uint8_t buf_pool[BUF_COUNT][64];
+CFG_TUH_MEM_SECTION uint8_t buf_pool[BUF_COUNT][64];
uint8_t buf_owner[BUF_COUNT] = { 0 }; // device address that owns buffer
+CFG_TUH_MEM_SECTION uint16_t temp_buf[128]; // temp buffer for string descriptor
+
//--------------------------------------------------------------------+
// MACRO CONSTANT TYPEDEF PROTYPES
//--------------------------------------------------------------------+
@@ -74,8 +76,6 @@ int main(void) {
tuh_task();
led_blinking_task();
}
-
- return 0;
}
/*------------- TinyUSB Callbacks -------------*/
@@ -120,8 +120,6 @@ void print_device_descriptor(tuh_xfer_t *xfer) {
printf(" bcdDevice %04x\r\n" , desc_device.bcdDevice);
// Get String descriptor using Sync API
- uint16_t temp_buf[128];
-
printf(" iManufacturer %u ", desc_device.iManufacturer);
if (XFER_RESULT_SUCCESS == tuh_descriptor_get_manufacturer_string_sync(daddr, LANGUAGE_ID, temp_buf, sizeof(temp_buf))) {
print_utf16(temp_buf, TU_ARRAY_SIZE(temp_buf));
@@ -335,7 +333,7 @@ void led_blinking_task(void) {
static bool led_state = false;
// Blink every interval ms
- if (board_millis() - start_ms < interval_ms) {
+ if (tusb_time_millis_api() - start_ms < interval_ms) {
return; // not enough time
}
start_ms += interval_ms;
@@ -386,12 +384,12 @@ static int _count_utf8_bytes(const uint16_t *buf, size_t len) {
return (int) total_bytes;
}
-static void print_utf16(uint16_t *temp_buf, size_t buf_len) {
- if ((temp_buf[0] & 0xff) == 0) return;// empty
- size_t utf16_len = ((temp_buf[0] & 0xff) - 2) / sizeof(uint16_t);
- size_t utf8_len = (size_t) _count_utf8_bytes(temp_buf + 1, utf16_len);
- _convert_utf16le_to_utf8(temp_buf + 1, utf16_len, (uint8_t *) temp_buf, sizeof(uint16_t) * buf_len);
- ((uint8_t *) temp_buf)[utf8_len] = '\0';
+static void print_utf16(uint16_t *buf, size_t buf_len) {
+ if ((buf[0] & 0xff) == 0) return;// empty
+ size_t utf16_len = ((buf[0] & 0xff) - 2) / sizeof(uint16_t);
+ size_t utf8_len = (size_t) _count_utf8_bytes(buf + 1, utf16_len);
+ _convert_utf16le_to_utf8(buf + 1, utf16_len, (uint8_t *) buf, sizeof(uint16_t) * buf_len);
+ ((uint8_t *) buf)[utf8_len] = '\0';
- printf("%s", (char *) temp_buf);
+ printf("%s", (char *) buf);
}
diff --git a/examples/host/cdc_msc_hid/only.txt b/examples/host/cdc_msc_hid/only.txt
index f07d63f9d..1ddfc2b5c 100644
--- a/examples/host/cdc_msc_hid/only.txt
+++ b/examples/host/cdc_msc_hid/only.txt
@@ -8,6 +8,8 @@ mcu:LPC177X_8X
mcu:LPC18XX
mcu:LPC40XX
mcu:LPC43XX
+mcu:LPC54
+mcu:LPC55
mcu:MAX3421
mcu:MIMXRT10XX
mcu:MIMXRT11XX
diff --git a/examples/host/cdc_msc_hid/skip.txt b/examples/host/cdc_msc_hid/skip.txt
new file mode 100644
index 000000000..308796869
--- /dev/null
+++ b/examples/host/cdc_msc_hid/skip.txt
@@ -0,0 +1 @@
+board:lpcxpresso54114
diff --git a/examples/host/cdc_msc_hid/src/main.c b/examples/host/cdc_msc_hid/src/main.c
index c309a7cae..c27ad93fe 100644
--- a/examples/host/cdc_msc_hid/src/main.c
+++ b/examples/host/cdc_msc_hid/src/main.c
@@ -92,7 +92,7 @@ void led_blinking_task(void) {
static bool led_state = false;
// Blink every interval ms
- if (board_millis() - start_ms < interval_ms) {
+ if (tusb_time_millis_api() - start_ms < interval_ms) {
return;// not enough time
}
start_ms += interval_ms;
diff --git a/examples/host/cdc_msc_hid_freertos/only.txt b/examples/host/cdc_msc_hid_freertos/only.txt
index b0460b362..753fa7cd3 100644
--- a/examples/host/cdc_msc_hid_freertos/only.txt
+++ b/examples/host/cdc_msc_hid_freertos/only.txt
@@ -6,6 +6,8 @@ mcu:LPC177X_8X
mcu:LPC18XX
mcu:LPC40XX
mcu:LPC43XX
+mcu:LPC54
+mcu:LPC55
mcu:MAX3421
mcu:MIMXRT10XX
mcu:MIMXRT11XX
diff --git a/examples/host/cdc_msc_hid_freertos/skip.txt b/examples/host/cdc_msc_hid_freertos/skip.txt
index 2ba4438fd..54e7be1ba 100644
--- a/examples/host/cdc_msc_hid_freertos/skip.txt
+++ b/examples/host/cdc_msc_hid_freertos/skip.txt
@@ -1 +1,2 @@
mcu:RP2040
+board:lpcxpresso54114
diff --git a/examples/host/device_info/only.txt b/examples/host/device_info/only.txt
index 714c1078a..742935dcf 100644
--- a/examples/host/device_info/only.txt
+++ b/examples/host/device_info/only.txt
@@ -9,6 +9,8 @@ mcu:LPC177X_8X
mcu:LPC18XX
mcu:LPC40XX
mcu:LPC43XX
+mcu:LPC54
+mcu:LPC55
mcu:MAX3421
mcu:MIMXRT10XX
mcu:MIMXRT11XX
diff --git a/examples/host/device_info/skip.txt b/examples/host/device_info/skip.txt
new file mode 100644
index 000000000..308796869
--- /dev/null
+++ b/examples/host/device_info/skip.txt
@@ -0,0 +1 @@
+board:lpcxpresso54114
diff --git a/examples/host/device_info/src/main.c b/examples/host/device_info/src/main.c
index ab617e989..b0e38dd6b 100644
--- a/examples/host/device_info/src/main.c
+++ b/examples/host/device_info/src/main.c
@@ -105,7 +105,6 @@ int main(void) {
tuh_task(); // tinyusb host task
led_blinking_task(NULL);
}
- return 0;
#endif
}
@@ -246,7 +245,7 @@ void led_blinking_task(void* param) {
#if CFG_TUSB_OS == OPT_OS_FREERTOS
vTaskDelay(blink_interval_ms / portTICK_PERIOD_MS);
#else
- if (board_millis() - start_ms < blink_interval_ms) {
+ if (tusb_time_millis_api() - start_ms < blink_interval_ms) {
return; // not enough time
}
#endif
diff --git a/examples/host/hid_controller/only.txt b/examples/host/hid_controller/only.txt
index 8ec2f5cfe..45ca6846f 100644
--- a/examples/host/hid_controller/only.txt
+++ b/examples/host/hid_controller/only.txt
@@ -8,6 +8,8 @@ mcu:LPC177X_8X
mcu:LPC18XX
mcu:LPC40XX
mcu:LPC43XX
+mcu:LPC54
+mcu:LPC55
mcu:MAX3421
mcu:MIMXRT10XX
mcu:MIMXRT11XX
diff --git a/examples/host/hid_controller/skip.txt b/examples/host/hid_controller/skip.txt
new file mode 100644
index 000000000..308796869
--- /dev/null
+++ b/examples/host/hid_controller/skip.txt
@@ -0,0 +1 @@
+board:lpcxpresso54114
diff --git a/examples/host/hid_controller/src/hid_app.c b/examples/host/hid_controller/src/hid_app.c
index f8c3d029b..5417811f0 100644
--- a/examples/host/hid_controller/src/hid_app.c
+++ b/examples/host/hid_controller/src/hid_app.c
@@ -168,7 +168,7 @@ void hid_app_task(void)
const uint32_t interval_ms = 200;
static uint32_t start_ms = 0;
- uint32_t current_time_ms = board_millis();
+ uint32_t current_time_ms = tusb_time_millis_api();
if ( current_time_ms - start_ms >= interval_ms)
{
start_ms = current_time_ms;
diff --git a/examples/host/hid_controller/src/main.c b/examples/host/hid_controller/src/main.c
index fa70d7d1a..a9eebc90b 100644
--- a/examples/host/hid_controller/src/main.c
+++ b/examples/host/hid_controller/src/main.c
@@ -79,7 +79,7 @@ void led_blinking_task(void) {
static bool led_state = false;
// Blink every interval ms
- if ( board_millis() - start_ms < interval_ms) return; // not enough time
+ if ( tusb_time_millis_api() - start_ms < interval_ms) return; // not enough time
start_ms += interval_ms;
board_led_write(led_state);
diff --git a/examples/host/midi_rx/only.txt b/examples/host/midi_rx/only.txt
index 9d647a340..c71aacd87 100644
--- a/examples/host/midi_rx/only.txt
+++ b/examples/host/midi_rx/only.txt
@@ -11,6 +11,8 @@ mcu:LPC177X_8X
mcu:LPC18XX
mcu:LPC40XX
mcu:LPC43XX
+mcu:LPC54
+mcu:LPC55
mcu:MAX3421
mcu:MIMXRT10XX
mcu:MIMXRT11XX
diff --git a/examples/host/midi_rx/skip.txt b/examples/host/midi_rx/skip.txt
new file mode 100644
index 000000000..308796869
--- /dev/null
+++ b/examples/host/midi_rx/skip.txt
@@ -0,0 +1 @@
+board:lpcxpresso54114
diff --git a/examples/host/midi_rx/src/main.c b/examples/host/midi_rx/src/main.c
index 78b1a11b9..fb36906c6 100644
--- a/examples/host/midi_rx/src/main.c
+++ b/examples/host/midi_rx/src/main.c
@@ -58,8 +58,6 @@ int main(void) {
led_blinking_task();
midi_host_rx_task();
}
-
- return 0;
}
//--------------------------------------------------------------------+
@@ -72,7 +70,7 @@ void led_blinking_task(void) {
static bool led_state = false;
// Blink every interval ms
- if (board_millis() - start_ms < interval_ms) return;// not enough time
+ if (tusb_time_millis_api() - start_ms < interval_ms) return;// not enough time
start_ms += interval_ms;
board_led_write(led_state);
diff --git a/examples/host/msc_file_explorer/only.txt b/examples/host/msc_file_explorer/only.txt
index f07d63f9d..1ddfc2b5c 100644
--- a/examples/host/msc_file_explorer/only.txt
+++ b/examples/host/msc_file_explorer/only.txt
@@ -8,6 +8,8 @@ mcu:LPC177X_8X
mcu:LPC18XX
mcu:LPC40XX
mcu:LPC43XX
+mcu:LPC54
+mcu:LPC55
mcu:MAX3421
mcu:MIMXRT10XX
mcu:MIMXRT11XX
diff --git a/examples/host/msc_file_explorer/skip.txt b/examples/host/msc_file_explorer/skip.txt
new file mode 100644
index 000000000..308796869
--- /dev/null
+++ b/examples/host/msc_file_explorer/skip.txt
@@ -0,0 +1 @@
+board:lpcxpresso54114
diff --git a/examples/host/msc_file_explorer/src/main.c b/examples/host/msc_file_explorer/src/main.c
index f9ec0ff5f..f6bf9a60a 100644
--- a/examples/host/msc_file_explorer/src/main.c
+++ b/examples/host/msc_file_explorer/src/main.c
@@ -92,8 +92,6 @@ int main(void) {
msc_app_task();
led_blinking_task();
}
-
- return 0;
}
//--------------------------------------------------------------------+
@@ -118,7 +116,7 @@ void led_blinking_task(void) {
static bool led_state = false;
// Blink every interval ms
- if (board_millis() - start_ms < interval_ms) return; // not enough time
+ if (tusb_time_millis_api() - start_ms < interval_ms) return; // not enough time
start_ms += interval_ms;
board_led_write(led_state);
diff --git a/examples/host/msc_file_explorer/src/msc_app.c b/examples/host/msc_file_explorer/src/msc_app.c
index 40a9ef57e..6ac63e937 100644
--- a/examples/host/msc_file_explorer/src/msc_app.c
+++ b/examples/host/msc_file_explorer/src/msc_app.c
@@ -49,11 +49,14 @@
#define CLI_BINDING_COUNT 8
static EmbeddedCli *_cli;
-static CLI_UINT cli_buffer[BYTES_TO_CLI_UINTS(CLI_BUFFER_SIZE)];
+static CLI_UINT cli_buffer[BYTES_TO_CLI_UINTS(CLI_BUFFER_SIZE)];
//------------- Elm Chan FatFS -------------//
-static FATFS fatfs[CFG_TUH_DEVICE_MAX]; // for simplicity only support 1 LUN per device
-static volatile bool _disk_busy[CFG_TUH_DEVICE_MAX];
+static CFG_TUH_MEM_SECTION FATFS fatfs[CFG_TUH_DEVICE_MAX]; // for simplicity only support 1 LUN per device
+static volatile bool _disk_busy[CFG_TUH_DEVICE_MAX];
+
+static CFG_TUH_MEM_SECTION FIL file1, file2;
+static CFG_TUH_MEM_SECTION uint8_t rw_buf[512];
// define the buffer to be place in USB/DMA memory with correct alignment/cache line size
CFG_TUH_MEM_SECTION static struct {
@@ -67,34 +70,30 @@ CFG_TUH_MEM_SECTION static struct {
bool cli_init(void);
-bool msc_app_init(void)
-{
- for(size_t i=0; i<CFG_TUH_DEVICE_MAX; i++) {
+bool msc_app_init(void) {
+ for (size_t i = 0; i < CFG_TUH_DEVICE_MAX; i++) {
_disk_busy[i] = false;
}
- // disable stdout buffered for echoing typing command
- #ifndef __ICCARM__ // TODO IAR doesn't support stream control ?
+// disable stdout buffered for echoing typing command
+#ifndef __ICCARM__ // TODO IAR doesn't support stream control ?
setbuf(stdout, NULL);
- #endif
+#endif
cli_init();
return true;
}
-void msc_app_task(void)
-{
+void msc_app_task(void) {
if (!_cli) {
return;
}
int ch = board_getchar();
- if ( ch > 0 )
- {
- while( ch > 0 )
- {
- embeddedCliReceiveChar(_cli, (char) ch);
+ if (ch > 0) {
+ while (ch > 0) {
+ embeddedCliReceiveChar(_cli, (char)ch);
ch = board_getchar();
}
embeddedCliProcess(_cli);
@@ -105,33 +104,32 @@ void msc_app_task(void)
//
//--------------------------------------------------------------------+
-static bool inquiry_complete_cb(uint8_t dev_addr, tuh_msc_complete_data_t const * cb_data) {
- msc_cbw_t const* cbw = cb_data->cbw;
- msc_csw_t const* csw = cb_data->csw;
+static bool inquiry_complete_cb(uint8_t dev_addr, const tuh_msc_complete_data_t *cb_data) {
+ const msc_cbw_t *cbw = cb_data->cbw;
+ const msc_csw_t *csw = cb_data->csw;
- if (csw->status != 0)
- {
+ if (csw->status != 0) {
printf("Inquiry failed\r\n");
return false;
}
// Print out Vendor ID, Product ID and Rev
- printf("%.8s %.16s rev %.4s\r\n", scsi_resp.inquiry.vendor_id, scsi_resp.inquiry.product_id, scsi_resp.inquiry.product_rev);
+ printf("%.8s %.16s rev %.4s\r\n", scsi_resp.inquiry.vendor_id, scsi_resp.inquiry.product_id,
+ scsi_resp.inquiry.product_rev);
// Get capacity of device
- uint32_t const block_count = tuh_msc_get_block_count(dev_addr, cbw->lun);
- uint32_t const block_size = tuh_msc_get_block_size(dev_addr, cbw->lun);
+ const uint32_t block_count = tuh_msc_get_block_count(dev_addr, cbw->lun);
+ const uint32_t block_size = tuh_msc_get_block_size(dev_addr, cbw->lun);
- printf("Disk Size: %" PRIu32 " MB\r\n", block_count / ((1024*1024)/block_size));
+ printf("Disk Size: %" PRIu32 " MB\r\n", block_count / ((1024 * 1024) / block_size));
// printf("Block Count = %lu, Block Size: %lu\r\n", block_count, block_size);
// For simplicity: we only mount 1 LUN per device
- uint8_t const drive_num = dev_addr-1;
- char drive_path[3] = "0:";
+ const uint8_t drive_num = dev_addr - 1;
+ char drive_path[3] = "0:";
drive_path[0] += drive_num;
- if ( f_mount(&fatfs[drive_num], drive_path, 1) != FR_OK )
- {
+ if (f_mount(&fatfs[drive_num], drive_path, 1) != FR_OK) {
puts("mount failed");
}
@@ -139,151 +137,134 @@ static bool inquiry_complete_cb(uint8_t dev_addr, tuh_msc_complete_data_t const
f_chdir(drive_path);
// print the drive label
-// char label[34];
-// if ( FR_OK == f_getlabel(drive_path, label, NULL) )
-// {
-// puts(label);
-// }
+ // char label[34];
+ // if ( FR_OK == f_getlabel(drive_path, label, NULL) )
+ // {
+ // puts(label);
+ // }
return true;
}
//------------- IMPLEMENTATION -------------//
-void tuh_msc_mount_cb(uint8_t dev_addr)
-{
+void tuh_msc_mount_cb(uint8_t dev_addr) {
printf("A MassStorage device is mounted\r\n");
- uint8_t const lun = 0;
+ const uint8_t lun = 0;
tuh_msc_inquiry(dev_addr, lun, &scsi_resp.inquiry, inquiry_complete_cb, 0);
}
-void tuh_msc_umount_cb(uint8_t dev_addr)
-{
+void tuh_msc_umount_cb(uint8_t dev_addr) {
printf("A MassStorage device is unmounted\r\n");
- uint8_t const drive_num = dev_addr-1;
- char drive_path[3] = "0:";
+ const uint8_t drive_num = dev_addr - 1;
+ char drive_path[3] = "0:";
drive_path[0] += drive_num;
f_unmount(drive_path);
-// if ( phy_disk == f_get_current_drive() )
-// { // active drive is unplugged --> change to other drive
-// for(uint8_t i=0; i<CFG_TUH_DEVICE_MAX; i++)
-// {
-// if ( disk_is_ready(i) )
-// {
-// f_chdrive(i);
-// cli_init(); // refractor, rename
-// }
-// }
-// }
+ // if ( phy_disk == f_get_current_drive() )
+ // { // active drive is unplugged --> change to other drive
+ // for(uint8_t i=0; i<CFG_TUH_DEVICE_MAX; i++)
+ // {
+ // if ( disk_is_ready(i) )
+ // {
+ // f_chdrive(i);
+ // cli_init(); // refractor, rename
+ // }
+ // }
+ // }
}
//--------------------------------------------------------------------+
// DiskIO
//--------------------------------------------------------------------+
-static void wait_for_disk_io(BYTE pdrv)
-{
- while(_disk_busy[pdrv])
- {
+static void wait_for_disk_io(BYTE pdrv) {
+ while (_disk_busy[pdrv]) {
tuh_task();
}
}
-static bool disk_io_complete(uint8_t dev_addr, tuh_msc_complete_data_t const * cb_data)
-{
- (void) dev_addr; (void) cb_data;
- _disk_busy[dev_addr-1] = false;
+static bool disk_io_complete(uint8_t dev_addr, const tuh_msc_complete_data_t *cb_data) {
+ (void)dev_addr;
+ (void)cb_data;
+ _disk_busy[dev_addr - 1] = false;
return true;
}
-DSTATUS disk_status (
- BYTE pdrv /* Physical drive nmuber to identify the drive */
-)
-{
+DSTATUS disk_status(BYTE pdrv /* Physical drive nmuber to identify the drive */
+) {
uint8_t dev_addr = pdrv + 1;
return tuh_msc_mounted(dev_addr) ? 0 : STA_NODISK;
}
-DSTATUS disk_initialize (
- BYTE pdrv /* Physical drive nmuber to identify the drive */
-)
-{
- (void) pdrv;
- return 0; // nothing to do
+DSTATUS disk_initialize(BYTE pdrv /* Physical drive nmuber to identify the drive */
+) {
+ (void)pdrv;
+ return 0; // nothing to do
}
-DRESULT disk_read (
- BYTE pdrv, /* Physical drive nmuber to identify the drive */
- BYTE *buff, /* Data buffer to store read data */
- LBA_t sector, /* Start sector in LBA */
- UINT count /* Number of sectors to read */
-)
-{
- uint8_t const dev_addr = pdrv + 1;
- uint8_t const lun = 0;
+DRESULT disk_read(BYTE pdrv, /* Physical drive nmuber to identify the drive */
+ BYTE *buff, /* Data buffer to store read data */
+ LBA_t sector, /* Start sector in LBA */
+ UINT count /* Number of sectors to read */
+) {
+ const uint8_t dev_addr = pdrv + 1;
+ const uint8_t lun = 0;
- _disk_busy[pdrv] = true;
- tuh_msc_read10(dev_addr, lun, buff, sector, (uint16_t) count, disk_io_complete, 0);
- wait_for_disk_io(pdrv);
+ _disk_busy[pdrv] = true;
+ tuh_msc_read10(dev_addr, lun, buff, sector, (uint16_t)count, disk_io_complete, 0);
+ wait_for_disk_io(pdrv);
- return RES_OK;
+ return RES_OK;
}
#if FF_FS_READONLY == 0
-DRESULT disk_write (
- BYTE pdrv, /* Physical drive nmuber to identify the drive */
- const BYTE *buff, /* Data to be written */
- LBA_t sector, /* Start sector in LBA */
- UINT count /* Number of sectors to write */
-)
-{
- uint8_t const dev_addr = pdrv + 1;
- uint8_t const lun = 0;
+DRESULT disk_write(BYTE pdrv, /* Physical drive nmuber to identify the drive */
+ const BYTE *buff, /* Data to be written */
+ LBA_t sector, /* Start sector in LBA */
+ UINT count /* Number of sectors to write */
+) {
+ const uint8_t dev_addr = pdrv + 1;
+ const uint8_t lun = 0;
- _disk_busy[pdrv] = true;
- tuh_msc_write10(dev_addr, lun, buff, sector, (uint16_t) count, disk_io_complete, 0);
- wait_for_disk_io(pdrv);
+ _disk_busy[pdrv] = true;
+ tuh_msc_write10(dev_addr, lun, buff, sector, (uint16_t)count, disk_io_complete, 0);
+ wait_for_disk_io(pdrv);
- return RES_OK;
+ return RES_OK;
}
#endif
-DRESULT disk_ioctl (
- BYTE pdrv, /* Physical drive nmuber (0..) */
- BYTE cmd, /* Control code */
- void *buff /* Buffer to send/receive control data */
-)
-{
- uint8_t const dev_addr = pdrv + 1;
- uint8_t const lun = 0;
- switch ( cmd )
- {
+DRESULT disk_ioctl(BYTE pdrv, /* Physical drive nmuber (0..) */
+ BYTE cmd, /* Control code */
+ void *buff /* Buffer to send/receive control data */
+) {
+ const uint8_t dev_addr = pdrv + 1;
+ const uint8_t lun = 0;
+ switch (cmd) {
case CTRL_SYNC:
// nothing to do since we do blocking
return RES_OK;
case GET_SECTOR_COUNT:
- *((DWORD*) buff) = (WORD) tuh_msc_get_block_count(dev_addr, lun);
+ *((DWORD *)buff) = (WORD)tuh_msc_get_block_count(dev_addr, lun);
return RES_OK;
case GET_SECTOR_SIZE:
- *((WORD*) buff) = (WORD) tuh_msc_get_block_size(dev_addr, lun);
+ *((WORD *)buff) = (WORD)tuh_msc_get_block_size(dev_addr, lun);
return RES_OK;
case GET_BLOCK_SIZE:
- *((DWORD*) buff) = 1; // erase block size in units of sector size
+ *((DWORD *)buff) = 1; // erase block size in units of sector size
return RES_OK;
default:
return RES_PARERR;
}
-
- return RES_OK;
}
//--------------------------------------------------------------------+
@@ -300,12 +281,11 @@ void cli_cmd_mv(EmbeddedCli *cli, char *args, void *context);
void cli_cmd_rm(EmbeddedCli *cli, char *args, void *context);
static void cli_write_char(EmbeddedCli *cli, char c) {
- (void) cli;
- putchar((int) c);
+ (void)cli;
+ putchar((int)c);
}
-bool cli_init(void)
-{
+bool cli_init(void) {
EmbeddedCliConfig *config = embeddedCliDefaultConfig();
config->cliBuffer = cli_buffer;
config->cliBufferSize = CLI_BUFFER_SIZE;
@@ -321,230 +301,173 @@ bool cli_init(void)
_cli->writeChar = cli_write_char;
- embeddedCliAddBinding(_cli, (CliCommandBinding) {
- "cat",
- "Usage: cat [FILE]...\r\n\tConcatenate FILE(s) to standard output..",
- true,
- NULL,
- cli_cmd_cat
- });
+ embeddedCliAddBinding(_cli,
+ (CliCommandBinding){"cat", "Usage: cat [FILE]...\r\n\tConcatenate FILE(s) to standard output..",
+ true, NULL, cli_cmd_cat});
- embeddedCliAddBinding(_cli, (CliCommandBinding) {
- "cd",
- "Usage: cd [DIR]...\r\n\tChange the current directory to DIR.",
- true,
- NULL,
- cli_cmd_cd
- });
+ embeddedCliAddBinding(_cli, (CliCommandBinding){"cd", "Usage: cd [DIR]...\r\n\tChange the current directory to DIR.",
+ true, NULL, cli_cmd_cd});
- embeddedCliAddBinding(_cli, (CliCommandBinding) {
- "cp",
- "Usage: cp SOURCE DEST\r\n\tCopy SOURCE to DEST.",
- true,
- NULL,
- cli_cmd_cp
- });
+ embeddedCliAddBinding(_cli, (CliCommandBinding){"cp", "Usage: cp SOURCE DEST\r\n\tCopy SOURCE to DEST.", true, NULL,
+ cli_cmd_cp});
- embeddedCliAddBinding(_cli, (CliCommandBinding) {
- "ls",
- "Usage: ls [DIR]...\r\n\tList information about the FILEs (the current directory by default).",
- true,
- NULL,
- cli_cmd_ls
- });
+ embeddedCliAddBinding(_cli, (CliCommandBinding){"ls",
+ "Usage: ls [DIR]...\r\n\tList information about the FILEs (the "
+ "current directory by default).",
+ true, NULL, cli_cmd_ls});
- embeddedCliAddBinding(_cli, (CliCommandBinding) {
- "pwd",
- "Usage: pwd\r\n\tPrint the name of the current working directory.",
- true,
- NULL,
- cli_cmd_pwd
- });
+ embeddedCliAddBinding(_cli,
+ (CliCommandBinding){"pwd", "Usage: pwd\r\n\tPrint the name of the current working directory.",
+ true, NULL, cli_cmd_pwd});
- embeddedCliAddBinding(_cli, (CliCommandBinding) {
- "mkdir",
- "Usage: mkdir DIR...\r\n\tCreate the DIRECTORY(ies), if they do not already exist..",
- true,
- NULL,
- cli_cmd_mkdir
- });
+ embeddedCliAddBinding(_cli, (CliCommandBinding){"mkdir",
+ "Usage: mkdir DIR...\r\n\tCreate the DIRECTORY(ies), if they do not "
+ "already exist..",
+ true, NULL, cli_cmd_mkdir});
- embeddedCliAddBinding(_cli, (CliCommandBinding) {
- "mv",
- "Usage: mv SOURCE DEST...\r\n\tRename SOURCE to DEST.",
- true,
- NULL,
- cli_cmd_mv
- });
+ embeddedCliAddBinding(_cli, (CliCommandBinding){"mv", "Usage: mv SOURCE DEST...\r\n\tRename SOURCE to DEST.", true,
+ NULL, cli_cmd_mv});
- embeddedCliAddBinding(_cli, (CliCommandBinding) {
- "rm",
- "Usage: rm [FILE]...\r\n\tRemove (unlink) the FILE(s).",
- true,
- NULL,
- cli_cmd_rm
- });
+ embeddedCliAddBinding(_cli, (CliCommandBinding){"rm", "Usage: rm [FILE]...\r\n\tRemove (unlink) the FILE(s).", true,
+ NULL, cli_cmd_rm});
return true;
}
-void cli_cmd_cat(EmbeddedCli *cli, char *args, void *context)
-{
- (void) cli; (void) context;
+void cli_cmd_cat(EmbeddedCli *cli, char *args, void *context) {
+ (void)cli;
+ (void)context;
uint16_t argc = embeddedCliGetTokenCount(args);
// need at least 1 argument
- if ( argc == 0 )
- {
+ if (argc == 0) {
printf("invalid arguments\r\n");
return;
}
- for(uint16_t i=0; i<argc; i++)
- {
- FIL fi;
- const char* fpath = embeddedCliGetToken(args, i+1); // token count from 1
+ for (uint16_t i = 0; i < argc; i++) {
+ FIL *fi = &file1;
+ const char *fpath = embeddedCliGetToken(args, i + 1); // token count from 1
- if ( FR_OK != f_open(&fi, fpath, FA_READ) )
- {
+ if (FR_OK != f_open(fi, fpath, FA_READ)) {
printf("%s: No such file or directory\r\n", fpath);
- }else
- {
- uint8_t buf[512];
+ } else {
UINT count = 0;
- while ( (FR_OK == f_read(&fi, buf, sizeof(buf), &count)) && (count > 0) )
- {
- for(UINT c = 0; c < count; c++)
- {
- const uint8_t ch = buf[c];
- if (isprint(ch) || iscntrl(ch))
- {
+ while ((FR_OK == f_read(fi, rw_buf, sizeof(rw_buf), &count)) && (count > 0)) {
+ for (UINT c = 0; c < count; c++) {
+ const uint8_t ch = rw_buf[c];
+ if (isprint(ch) || iscntrl(ch)) {
putchar(ch);
- }else
- {
+ } else {
putchar('.');
}
}
}
}
- f_close(&fi);
+ f_close(fi);
}
}
-void cli_cmd_cd(EmbeddedCli *cli, char *args, void *context)
-{
- (void) cli; (void) context;
+void cli_cmd_cd(EmbeddedCli *cli, char *args, void *context) {
+ (void)cli;
+ (void)context;
uint16_t argc = embeddedCliGetTokenCount(args);
// only support 1 argument
- if ( argc != 1 )
- {
+ if (argc != 1) {
printf("invalid arguments\r\n");
return;
}
// default is current directory
- const char* dpath = args;
+ const char *dpath = args;
- if ( FR_OK != f_chdir(dpath) )
- {
+ if (FR_OK != f_chdir(dpath)) {
printf("%s: No such file or directory\r\n", dpath);
return;
}
}
-void cli_cmd_cp(EmbeddedCli *cli, char *args, void *context)
-{
- (void) cli; (void) context;
+void cli_cmd_cp(EmbeddedCli *cli, char *args, void *context) {
+ (void)cli;
+ (void)context;
uint16_t argc = embeddedCliGetTokenCount(args);
- if ( argc != 2 )
- {
+ if (argc != 2) {
printf("invalid arguments\r\n");
return;
}
// default is current directory
- const char* src = embeddedCliGetToken(args, 1);
- const char* dst = embeddedCliGetToken(args, 2);
+ const char *src = embeddedCliGetToken(args, 1);
+ const char *dst = embeddedCliGetToken(args, 2);
- FIL f_src;
- FIL f_dst;
+ FIL *f_src = &file1;
+ FIL *f_dst = &file2;
- if ( FR_OK != f_open(&f_src, src, FA_READ) )
- {
+ if (FR_OK != f_open(f_src, src, FA_READ)) {
printf("cannot stat '%s': No such file or directory\r\n", src);
return;
}
- if ( FR_OK != f_open(&f_dst, dst, FA_WRITE | FA_CREATE_ALWAYS) )
- {
+ if (FR_OK != f_open(f_dst, dst, FA_WRITE | FA_CREATE_ALWAYS)) {
printf("cannot create '%s'\r\n", dst);
return;
- }else
- {
- uint8_t buf[512];
+ } else {
UINT rd_count = 0;
- while ( (FR_OK == f_read(&f_src, buf, sizeof(buf), &rd_count)) && (rd_count > 0) )
- {
+ while ((FR_OK == f_read(f_src, rw_buf, sizeof(rw_buf), &rd_count)) && (rd_count > 0)) {
UINT wr_count = 0;
- if ( FR_OK != f_write(&f_dst, buf, rd_count, &wr_count) )
- {
+ if (FR_OK != f_write(f_dst, rw_buf, rd_count, &wr_count)) {
printf("cannot write to '%s'\r\n", dst);
break;
}
}
}
- f_close(&f_src);
- f_close(&f_dst);
+ f_close(f_src);
+ f_close(f_dst);
}
-void cli_cmd_ls(EmbeddedCli *cli, char *args, void *context)
-{
- (void) cli; (void) context;
+void cli_cmd_ls(EmbeddedCli *cli, char *args, void *context) {
+ (void)cli;
+ (void)context;
uint16_t argc = embeddedCliGetTokenCount(args);
// only support 1 argument
- if ( argc > 1 )
- {
+ if (argc > 1) {
printf("invalid arguments\r\n");
return;
}
// default is current directory
- const char* dpath = ".";
- if (argc) dpath = args;
+ const char *dpath = ".";
+ if (argc) {
+ dpath = args;
+ }
DIR dir;
- if ( FR_OK != f_opendir(&dir, dpath) )
- {
+ if (FR_OK != f_opendir(&dir, dpath)) {
printf("cannot access '%s': No such file or directory\r\n", dpath);
return;
}
FILINFO fno;
- while( (f_readdir(&dir, &fno) == FR_OK) && (fno.fname[0] != 0) )
- {
- if ( fno.fname[0] != '.' ) // ignore . and .. entry
+ while ((f_readdir(&dir, &fno) == FR_OK) && (fno.fname[0] != 0)) {
+ if (fno.fname[0] != '.') // ignore . and .. entry
{
- if ( fno.fattrib & AM_DIR )
- {
+ if (fno.fattrib & AM_DIR) {
// directory
printf("/%s\r\n", fno.fname);
- }else
- {
+ } else {
printf("%-40s", fno.fname);
- if (fno.fsize < 1024)
- {
+ if (fno.fsize < 1024) {
printf("%" PRIu32 " B\r\n", fno.fsize);
- }else
- {
+ } else {
printf("%" PRIu32 " KB\r\n", fno.fsize / 1024);
}
}
@@ -554,90 +477,81 @@ void cli_cmd_ls(EmbeddedCli *cli, char *args, void *context)
f_closedir(&dir);
}
-void cli_cmd_pwd(EmbeddedCli *cli, char *args, void *context)
-{
- (void) cli; (void) context;
+void cli_cmd_pwd(EmbeddedCli *cli, char *args, void *context) {
+ (void)cli;
+ (void)context;
uint16_t argc = embeddedCliGetTokenCount(args);
- if (argc != 0)
- {
+ if (argc != 0) {
printf("invalid arguments\r\n");
return;
}
char path[256];
- if (FR_OK != f_getcwd(path, sizeof(path)))
- {
+ if (FR_OK != f_getcwd(path, sizeof(path))) {
printf("cannot get current working directory\r\n");
}
puts(path);
}
-void cli_cmd_mkdir(EmbeddedCli *cli, char *args, void *context)
-{
- (void) cli; (void) context;
+void cli_cmd_mkdir(EmbeddedCli *cli, char *args, void *context) {
+ (void)cli;
+ (void)context;
uint16_t argc = embeddedCliGetTokenCount(args);
// only support 1 argument
- if ( argc != 1 )
- {
+ if (argc != 1) {
printf("invalid arguments\r\n");
return;
}
// default is current directory
- const char* dpath = args;
+ const char *dpath = args;
- if ( FR_OK != f_mkdir(dpath) )
- {
+ if (FR_OK != f_mkdir(dpath)) {
printf("%s: cannot create this directory\r\n", dpath);
return;
}
}
-void cli_cmd_mv(EmbeddedCli *cli, char *args, void *context)
-{
- (void) cli; (void) context;
+void cli_cmd_mv(EmbeddedCli *cli, char *args, void *context) {
+ (void)cli;
+ (void)context;
uint16_t argc = embeddedCliGetTokenCount(args);
- if ( argc != 2 )
- {
+ if (argc != 2) {
printf("invalid arguments\r\n");
return;
}
// default is current directory
- const char* src = embeddedCliGetToken(args, 1);
- const char* dst = embeddedCliGetToken(args, 2);
+ const char *src = embeddedCliGetToken(args, 1);
+ const char *dst = embeddedCliGetToken(args, 2);
- if ( FR_OK != f_rename(src, dst) )
- {
+ if (FR_OK != f_rename(src, dst)) {
printf("cannot mv %s to %s\r\n", src, dst);
return;
}
}
-void cli_cmd_rm(EmbeddedCli *cli, char *args, void *context)
-{
- (void) cli; (void) context;
+void cli_cmd_rm(EmbeddedCli *cli, char *args, void *context) {
+ (void)cli;
+ (void)context;
uint16_t argc = embeddedCliGetTokenCount(args);
// need at least 1 argument
- if ( argc == 0 )
- {
+ if (argc == 0) {
printf("invalid arguments\r\n");
return;
}
- for(uint16_t i=0; i<argc; i++)
- {
- const char* fpath = embeddedCliGetToken(args, i+1); // token count from 1
+ for (uint16_t i = 0; i < argc; i++) {
+ const char *fpath = embeddedCliGetToken(args, i + 1); // token count from 1
- if ( FR_OK != f_unlink(fpath) )
- {
+ if (FR_OK != f_unlink(fpath)) {
printf("cannot remove '%s': No such file or directory\r\n", fpath);
}
}
diff --git a/examples/typec/power_delivery/src/main.c b/examples/typec/power_delivery/src/main.c
index de0db4721..f6191bfe8 100644
--- a/examples/typec/power_delivery/src/main.c
+++ b/examples/typec/power_delivery/src/main.c
@@ -184,7 +184,7 @@ 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 ( tusb_time_millis_api() - start_ms < blink_interval_ms) return; // not enough time
start_ms += blink_interval_ms;
board_led_write(led_state);