Skip to content

Commit

Permalink
Merge pull request #2875 from hathach/hil-dwc2
Browse files Browse the repository at this point in the history
  • Loading branch information
hathach authored Nov 13, 2024
2 parents a8903d3 + 401a704 commit 6f11f99
Show file tree
Hide file tree
Showing 21 changed files with 625 additions and 255 deletions.
13 changes: 10 additions & 3 deletions .idea/cmake.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion examples/device/cdc_msc/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
cmake_minimum_required(VERSION 3.17)
set_property(GLOBAL PROPERTY USE_FOLDERS ON)
#set_property(GLOBAL PROPERTY USE_FOLDERS ON)

include(${CMAKE_CURRENT_SOURCE_DIR}/../../../hw/bsp/family_support.cmake)

Expand Down
3 changes: 3 additions & 0 deletions examples/host/device_info/only.txt
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
mcu:ESP32S2
mcu:ESP32S3
mcu:ESP32P4
mcu:KINETIS_KL
mcu:LPC175X_6X
mcu:LPC177X_8X
Expand Down
4 changes: 4 additions & 0 deletions examples/host/device_info/src/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
# This file is for ESP-IDF only
idf_component_register(SRCS "main.c"
INCLUDE_DIRS "."
REQUIRES boards tinyusb_src)
157 changes: 123 additions & 34 deletions examples/host/device_info/src/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -56,15 +56,24 @@
//--------------------------------------------------------------------+
// MACRO CONSTANT TYPEDEF PROTYPES
//--------------------------------------------------------------------+
void led_blinking_task(void);
static void print_utf16(uint16_t* temp_buf, size_t buf_len);
enum {
BLINK_NOT_MOUNTED = 250,
BLINK_MOUNTED = 1000,
BLINK_SUSPENDED = 2500,
};
static uint32_t blink_interval_ms = BLINK_NOT_MOUNTED;

/*------------- MAIN -------------*/
int main(void) {
board_init();
void led_blinking_task(void* param);
static void print_utf16(uint16_t* temp_buf, size_t buf_len);

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,
Expand All @@ -75,20 +84,30 @@ 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();
led_blinking_task();
tuh_task(); // tinyusb host task
led_blinking_task(NULL);
}

return 0;
#endif
}

/*------------- TinyUSB Callbacks -------------*/

// Invoked when device is mounted (configured)
void tuh_mount_cb(uint8_t daddr) {
blink_interval_ms = BLINK_MOUNTED;

// Get Device Descriptor
tusb_desc_device_t desc_device;
uint8_t xfer_result = tuh_descriptor_get_device_sync(daddr, &desc_device, 18);
Expand All @@ -97,9 +116,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);
Expand Down Expand Up @@ -129,37 +159,18 @@ 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) {
blink_interval_ms = BLINK_NOT_MOUNTED;
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
//--------------------------------------------------------------------+
Expand Down Expand Up @@ -212,3 +223,81 @@ 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
vTaskDelay(blink_interval_ms / portTICK_PERIOD_MS);
#else
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
5 changes: 5 additions & 0 deletions examples/host/device_info/src/tusb_config.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 4 additions & 0 deletions hw/bsp/espressif/boards/espressif_p4_function_ev/board.h
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,10 @@
#define BUTTON_PIN 0
#define BUTTON_STATE_ACTIVE 0

// For CI hardware test, to test both device and host on the same HS port with help of
#define HIL_DEVICE_HOST_MUX_PIN 47
#define HIL_DEVICE_STATE 1

#ifdef __cplusplus
}
#endif
Expand Down
6 changes: 6 additions & 0 deletions hw/bsp/espressif/boards/family.c
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,12 @@ void board_init(void) {
usb_init();
#endif

#ifdef HIL_DEVICE_HOST_MUX_PIN
gpio_reset_pin(HIL_DEVICE_HOST_MUX_PIN);
gpio_set_direction(HIL_DEVICE_HOST_MUX_PIN, GPIO_MODE_OUTPUT);
gpio_set_level(HIL_DEVICE_HOST_MUX_PIN, CFG_TUD_ENABLED ? HIL_DEVICE_STATE : (1-HIL_DEVICE_STATE));
#endif

#if CFG_TUH_ENABLED && CFG_TUH_MAX3421
max3421_init();
#endif
Expand Down
5 changes: 5 additions & 0 deletions hw/bsp/espressif/components/tinyusb_src/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,11 @@ if (DEFINED LOG)
endif ()
endif()

if(DEFINED CFLAGS_CLI)
list(APPEND compile_definitions ${CFLAGS_CLI})
endif()


idf_component_register(SRCS ${srcs}
INCLUDE_DIRS ${tusb_src}
REQUIRES src
Expand Down
6 changes: 3 additions & 3 deletions hw/bsp/stm32f7/boards/stm32f723disco/board.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,12 @@ set(LD_FILE_GNU ${CMAKE_CURRENT_LIST_DIR}/STM32F723xE_FLASH.ld)

set(RHPORT_SPEED OPT_MODE_FULL_SPEED OPT_MODE_HIGH_SPEED)

# device default to PORT 1 High Speed
# For Hardware test: device default to PORT 0, Host to port 1
if (NOT DEFINED RHPORT_DEVICE)
set(RHPORT_DEVICE 1)
set(RHPORT_DEVICE 0)
endif()
if (NOT DEFINED RHPORT_HOST)
set(RHPORT_HOST 0)
set(RHPORT_HOST 1)
endif()

function(update_board TARGET)
Expand Down
5 changes: 3 additions & 2 deletions hw/bsp/stm32f7/boards/stm32f723disco/board.mk
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
MCU_VARIANT = stm32f723xx

# For Hardware test: device default to PORT 0, Host to port 1
RHPORT_SPEED = OPT_MODE_FULL_SPEED OPT_MODE_HIGH_SPEED
RHPORT_DEVICE ?= 1
RHPORT_HOST ?= 0
RHPORT_DEVICE ?= 0
RHPORT_HOST ?= 1

CFLAGS += \
-DSTM32F723xx \
Expand Down
29 changes: 19 additions & 10 deletions hw/bsp/stm32f7/family.c
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,25 @@ typedef struct {

#include "board.h"

//--------------------------------------------------------------------+
// MACRO TYPEDEF CONSTANT ENUM
//--------------------------------------------------------------------+

#ifdef UART_DEV
UART_HandleTypeDef UartHandle = {
.Instance = UART_DEV,
.Init = {
.BaudRate = CFG_BOARD_UART_BAUDRATE,
.WordLength = UART_WORDLENGTH_8B,
.StopBits = UART_STOPBITS_1,
.Parity = UART_PARITY_NONE,
.HwFlowCtl = UART_HWCONTROL_NONE,
.Mode = UART_MODE_TX_RX,
.OverSampling = UART_OVERSAMPLING_16,
}
};
#endif

//--------------------------------------------------------------------+
// Forward USB interrupt events to TinyUSB IRQ Handler
//--------------------------------------------------------------------+
Expand All @@ -54,8 +73,6 @@ void OTG_HS_IRQHandler(void) {
// MACRO TYPEDEF CONSTANT ENUM
//--------------------------------------------------------------------+

UART_HandleTypeDef UartHandle;

void board_init(void) {
board_clock_init();

Expand Down Expand Up @@ -89,14 +106,6 @@ void board_init(void) {
#endif

#ifdef UART_DEV
UartHandle.Instance = UART_DEV;
UartHandle.Init.BaudRate = CFG_BOARD_UART_BAUDRATE;
UartHandle.Init.WordLength = UART_WORDLENGTH_8B;
UartHandle.Init.StopBits = UART_STOPBITS_1;
UartHandle.Init.Parity = UART_PARITY_NONE;
UartHandle.Init.HwFlowCtl = UART_HWCONTROL_NONE;
UartHandle.Init.Mode = UART_MODE_TX_RX;
UartHandle.Init.OverSampling = UART_OVERSAMPLING_16;
HAL_UART_Init(&UartHandle);
#endif

Expand Down
Loading

0 comments on commit 6f11f99

Please sign in to comment.