update host/device_info to build with freertos for espressif board. Add hil test for host/device_info
This commit is contained in:
@@ -1,3 +1,6 @@
|
||||
mcu:ESP32S2
|
||||
mcu:ESP32S3
|
||||
mcu:ESP32P4
|
||||
mcu:KINETIS_KL
|
||||
mcu:LPC175X_6X
|
||||
mcu:LPC177X_8X
|
||||
|
4
examples/host/device_info/src/CMakeLists.txt
Normal file
4
examples/host/device_info/src/CMakeLists.txt
Normal file
@@ -0,0 +1,4 @@
|
||||
# This file is for ESP-IDF only
|
||||
idf_component_register(SRCS "main.c"
|
||||
INCLUDE_DIRS "."
|
||||
REQUIRES boards tinyusb_src)
|
@@ -24,7 +24,7 @@
|
||||
*/
|
||||
|
||||
/* Host example will get device descriptors of attached devices and print it out via uart/rtt (logger) as follows:
|
||||
* Device 1: ID 046d:c52f
|
||||
* Device 1: ID 046d:c52f SN 11223344
|
||||
Device Descriptor:
|
||||
bLength 18
|
||||
bDescriptorType 1
|
||||
@@ -56,15 +56,20 @@
|
||||
//--------------------------------------------------------------------+
|
||||
// MACRO CONSTANT TYPEDEF PROTYPES
|
||||
//--------------------------------------------------------------------+
|
||||
void led_blinking_task(void);
|
||||
|
||||
static uint32_t blink_interval_ms = 1000;
|
||||
|
||||
void led_blinking_task(void* param);
|
||||
static void print_utf16(uint16_t* temp_buf, size_t buf_len);
|
||||
|
||||
/*------------- MAIN -------------*/
|
||||
int main(void) {
|
||||
board_init();
|
||||
|
||||
printf("TinyUSB Device Info Example\r\n");
|
||||
#if CFG_TUSB_OS == OPT_OS_FREERTOS
|
||||
void init_freertos_task(void);
|
||||
#endif
|
||||
|
||||
//--------------------------------------------------------------------
|
||||
// Main
|
||||
//--------------------------------------------------------------------
|
||||
void init_tinyusb(void) {
|
||||
// init host stack on configured roothub port
|
||||
tusb_rhport_init_t host_init = {
|
||||
.role = TUSB_ROLE_HOST,
|
||||
@@ -75,14 +80,22 @@ int main(void) {
|
||||
if (board_init_after_tusb) {
|
||||
board_init_after_tusb();
|
||||
}
|
||||
}
|
||||
|
||||
int main(void) {
|
||||
board_init();
|
||||
printf("TinyUSB Device Info Example\r\n");
|
||||
|
||||
#if CFG_TUSB_OS == OPT_OS_FREERTOS
|
||||
init_freertos_task();
|
||||
#else
|
||||
init_tinyusb();
|
||||
while (1) {
|
||||
// tinyusb host task
|
||||
tuh_task();
|
||||
tuh_task(); // tinyusb host task
|
||||
led_blinking_task();
|
||||
}
|
||||
|
||||
return 0;
|
||||
#endif
|
||||
}
|
||||
|
||||
/*------------- TinyUSB Callbacks -------------*/
|
||||
@@ -97,9 +110,20 @@ void tuh_mount_cb(uint8_t daddr) {
|
||||
return;
|
||||
}
|
||||
|
||||
uint16_t serial[64];
|
||||
uint16_t buf[256];
|
||||
|
||||
printf("Device %u: ID %04x:%04x\r\n", daddr, desc_device.idVendor, desc_device.idProduct);
|
||||
printf("Device %u: ID %04x:%04x SN ", daddr, desc_device.idVendor, desc_device.idProduct);
|
||||
xfer_result = tuh_descriptor_get_serial_string_sync(daddr, LANGUAGE_ID, serial, sizeof(serial));
|
||||
if (XFER_RESULT_SUCCESS != xfer_result) {
|
||||
serial[0] = 'n';
|
||||
serial[1] = '/';
|
||||
serial[2] = 'a';
|
||||
serial[3] = 0;
|
||||
}
|
||||
print_utf16(serial, TU_ARRAY_SIZE(serial));
|
||||
printf("\r\n");
|
||||
|
||||
printf("Device Descriptor:\r\n");
|
||||
printf(" bLength %u\r\n", desc_device.bLength);
|
||||
printf(" bDescriptorType %u\r\n", desc_device.bDescriptorType);
|
||||
@@ -129,37 +153,17 @@ void tuh_mount_cb(uint8_t daddr) {
|
||||
printf("\r\n");
|
||||
|
||||
printf(" iSerialNumber %u ", desc_device.iSerialNumber);
|
||||
xfer_result = tuh_descriptor_get_serial_string_sync(daddr, LANGUAGE_ID, buf, sizeof(buf));
|
||||
if (XFER_RESULT_SUCCESS == xfer_result) {
|
||||
print_utf16(buf, TU_ARRAY_SIZE(buf));
|
||||
}
|
||||
printf((char*)serial); // serial is already to UTF-8
|
||||
printf("\r\n");
|
||||
|
||||
printf(" bNumConfigurations %u\r\n", desc_device.bNumConfigurations);
|
||||
}
|
||||
|
||||
/// Invoked when device is unmounted (bus reset/unplugged)
|
||||
// Invoked when device is unmounted (bus reset/unplugged)
|
||||
void tuh_umount_cb(uint8_t daddr) {
|
||||
printf("Device removed, address = %d\r\n", daddr);
|
||||
}
|
||||
|
||||
//--------------------------------------------------------------------+
|
||||
// Blinking Task
|
||||
//--------------------------------------------------------------------+
|
||||
void led_blinking_task(void) {
|
||||
const uint32_t interval_ms = 1000;
|
||||
static uint32_t start_ms = 0;
|
||||
|
||||
static bool led_state = false;
|
||||
|
||||
// Blink every interval ms
|
||||
if (board_millis() - start_ms < interval_ms) return; // not enough time
|
||||
start_ms += interval_ms;
|
||||
|
||||
board_led_write(led_state);
|
||||
led_state = 1 - led_state; // toggle
|
||||
}
|
||||
|
||||
//--------------------------------------------------------------------+
|
||||
// String Descriptor Helper
|
||||
//--------------------------------------------------------------------+
|
||||
@@ -212,3 +216,88 @@ static void print_utf16(uint16_t* temp_buf, size_t buf_len) {
|
||||
|
||||
printf("%s", (char*) temp_buf);
|
||||
}
|
||||
|
||||
//--------------------------------------------------------------------+
|
||||
// Blinking Task
|
||||
//--------------------------------------------------------------------+
|
||||
void led_blinking_task(void* param) {
|
||||
(void) param;
|
||||
static uint32_t start_ms = 0;
|
||||
static bool led_state = false;
|
||||
|
||||
while (1) {
|
||||
#if CFG_TUSB_OS == OPT_OS_FREERTOS
|
||||
if (blink_interval_ms == 0) {
|
||||
vTaskSuspend(NULL);
|
||||
} else {
|
||||
vTaskDelay(blink_interval_ms / portTICK_PERIOD_MS);
|
||||
}
|
||||
#else
|
||||
if (blink_interval_ms == 0) {
|
||||
return;
|
||||
}
|
||||
if (board_millis() - 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
|
||||
|
||||
#define BLINKY_STACK_SIZE configMINIMAL_STACK_SIZE
|
||||
|
||||
#if TUSB_MCU_VENDOR_ESPRESSIF
|
||||
#define USB_STACK_SIZE 4096
|
||||
#else
|
||||
// Increase stack size when debug log is enabled
|
||||
#define USB_STACK_SIZE (3*configMINIMAL_STACK_SIZE/2) * (CFG_TUSB_DEBUG ? 2 : 1)
|
||||
#endif
|
||||
|
||||
|
||||
// static task
|
||||
#if configSUPPORT_STATIC_ALLOCATION
|
||||
StackType_t blinky_stack[BLINKY_STACK_SIZE];
|
||||
StaticTask_t blinky_taskdef;
|
||||
|
||||
StackType_t usb_stack[USB_STACK_SIZE];
|
||||
StaticTask_t usb_taskdef;
|
||||
#endif
|
||||
|
||||
#if TUSB_MCU_VENDOR_ESPRESSIF
|
||||
void app_main(void) {
|
||||
main();
|
||||
}
|
||||
#endif
|
||||
|
||||
void usb_host_task(void *param) {
|
||||
(void) param;
|
||||
init_tinyusb();
|
||||
while (1) {
|
||||
tuh_task();
|
||||
}
|
||||
}
|
||||
|
||||
void init_freertos_task(void) {
|
||||
#if configSUPPORT_STATIC_ALLOCATION
|
||||
xTaskCreateStatic(led_blinking_task, "blinky", BLINKY_STACK_SIZE, NULL, 1, blinky_stack, &blinky_taskdef);
|
||||
xTaskCreateStatic(usb_host_task, "usbh", USB_STACK_SIZE, NULL, configMAX_PRIORITIES-1, usb_stack, &usb_taskdef);
|
||||
#else
|
||||
xTaskCreate(led_blinking_task, "blinky", BLINKY_STACK_SIZE, NULL, 1, NULL);
|
||||
xTaskCreate(usb_host_task, "usbh", USB_STACK_SIZE, NULL, configMAX_PRIORITIES - 1, NULL);
|
||||
#endif
|
||||
|
||||
// skip starting scheduler (and return) for ESP32-S2 or ESP32-S3
|
||||
#if !TUSB_MCU_VENDOR_ESPRESSIF
|
||||
vTaskStartScheduler();
|
||||
#endif
|
||||
}
|
||||
|
||||
#endif
|
||||
|
@@ -39,6 +39,11 @@
|
||||
#error CFG_TUSB_MCU must be defined
|
||||
#endif
|
||||
|
||||
// Espressif IDF requires "freertos/" prefix in include path
|
||||
#if TUSB_MCU_VENDOR_ESPRESSIF
|
||||
#define CFG_TUSB_OS_INC_PATH freertos/
|
||||
#endif
|
||||
|
||||
#ifndef CFG_TUSB_OS
|
||||
#define CFG_TUSB_OS OPT_OS_NONE
|
||||
#endif
|
||||
|
Reference in New Issue
Block a user