Skip to content

Commit 5a0ea7e

Browse files
committed
feature(esp_tinyusb): Remote wakeup device test application
1 parent 4c7f3ef commit 5a0ea7e

File tree

8 files changed

+457
-0
lines changed

8 files changed

+457
-0
lines changed
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
# The following lines of boilerplate have to be in your project's
2+
# CMakeLists in this exact order for cmake to work correctly
3+
cmake_minimum_required(VERSION 3.16)
4+
include($ENV{IDF_PATH}/tools/cmake/project.cmake)
5+
6+
# "Trim" the build. Include the minimal set of components, main, and anything it depends on.
7+
idf_build_set_property(MINIMAL_BUILD ON)
8+
9+
project(test_app_remote_wakeup)
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
idf_component_register(SRC_DIRS .
2+
INCLUDE_DIRS .
3+
REQUIRES unity
4+
PRIV_REQUIRES fatfs wear_levelling esp_partition esp_timer
5+
WHOLE_ARCHIVE)
Lines changed: 111 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,111 @@
1+
/*
2+
* SPDX-FileCopyrightText: 2025 Espressif Systems (Shanghai) CO LTD
3+
*
4+
* SPDX-License-Identifier: Apache-2.0
5+
*/
6+
7+
#include "soc/soc_caps.h"
8+
9+
#if SOC_USB_OTG_SUPPORTED
10+
//
11+
#include <stdio.h>
12+
#include <string.h>
13+
//
14+
#include "freertos/FreeRTOS.h"
15+
#include "freertos/task.h"
16+
#include "freertos/semphr.h"
17+
//
18+
#include "esp_system.h"
19+
#include "esp_log.h"
20+
#include "esp_err.h"
21+
//
22+
#include "unity.h"
23+
#include "tinyusb.h"
24+
#include "device_common.h"
25+
26+
typedef struct {
27+
union {
28+
tinyusb_event_id_t tinyusb_event_id; /*!< Event ID, comes from tinyusb.h */
29+
tinyusb_extra_event_id_t extra_event_id; /*!< Extra Event ID, extra, that isn't handled inside the driver */
30+
int id; /*!< Common field to get the data of event_id */
31+
};
32+
} test_device_event_t;
33+
34+
#define TEST_QUEUE_LEN 6 // Length of the queue for storage events
35+
#define TEST_EVENT_TIMEOUT_MS 30000 // Timeout for waiting storage events
36+
37+
static QueueHandle_t _test_device_event_queue = NULL;
38+
39+
void test_device_setup(void)
40+
{
41+
_test_device_event_queue = xQueueCreate(TEST_QUEUE_LEN, sizeof(test_device_event_t));
42+
TEST_ASSERT_NOT_NULL(_test_device_event_queue);
43+
}
44+
45+
void test_device_teardown(void)
46+
{
47+
TEST_ASSERT_NOT_NULL(_test_device_event_queue);
48+
vQueueDelete(_test_device_event_queue);
49+
_test_device_event_queue = NULL;
50+
}
51+
52+
void test_device_wait_event(int event_id)
53+
{
54+
TEST_ASSERT_NOT_NULL(_test_device_event_queue);
55+
// Wait for port callback to send an event message
56+
test_device_event_t evt;
57+
BaseType_t ret = xQueueReceive(_test_device_event_queue, &evt, pdMS_TO_TICKS(TEST_EVENT_TIMEOUT_MS));
58+
TEST_ASSERT_EQUAL_MESSAGE(pdPASS, ret, "Device event not generated on time");
59+
// Check the contents of that event message
60+
TEST_ASSERT_EQUAL_MESSAGE(event_id, evt.id, "Unexpected device event type received");
61+
}
62+
63+
/**
64+
* @brief TinyUSB callback for device mount.
65+
*
66+
* @note
67+
* For Linux-based Hosts: Reflects the SetConfiguration() request from the Host Driver.
68+
* For Win-based Hosts: SetConfiguration() request is present only with available Class in device descriptor.
69+
*/
70+
void test_device_event_handler(tinyusb_event_t *event, void *arg)
71+
{
72+
test_device_event_t evt = {
73+
.tinyusb_event_id = event->id,
74+
};
75+
xQueueSend(_test_device_event_queue, &evt, portMAX_DELAY);
76+
}
77+
78+
// Some events are still not handled inside the TinyUSB driver, so we need to forward them manually
79+
/**
80+
* @brief TinyUSB callback for device suspend event.
81+
*
82+
* This function is called when the USB host suspends the device.
83+
*
84+
* @param remote_wakeup_en Indicates whether the host has enabled remote wakeup for the device.
85+
* If true, the device is permitted to signal the host to resume communication.
86+
*/
87+
void tud_suspend_cb(bool remote_wakeup_en)
88+
{
89+
printf("USB Host suspended the device, remote wakeup enabled: %s\n", remote_wakeup_en ? "true" : "false");
90+
91+
test_device_event_t evt = {
92+
.extra_event_id = TINYUSB_EVENT_SUSPENDED,
93+
};
94+
xQueueSend(_test_device_event_queue, &evt, portMAX_DELAY);
95+
}
96+
97+
/**
98+
* @brief TinyUSB callback for device resume event.
99+
*
100+
* This function is called by the TinyUSB stack when the USB host resumes the device
101+
* from a suspended state. It forwards the resume event to the test event queue.
102+
*/
103+
void tud_resume_cb(void)
104+
{
105+
test_device_event_t evt = {
106+
.extra_event_id = TINYUSB_EVENT_RESUMED,
107+
};
108+
xQueueSend(_test_device_event_queue, &evt, portMAX_DELAY);
109+
}
110+
111+
#endif // SOC_USB_OTG_SUPPORTED
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
/*
2+
* SPDX-FileCopyrightText: 2025 Espressif Systems (Shanghai) CO LTD
3+
*
4+
* SPDX-License-Identifier: Apache-2.0
5+
*/
6+
#pragma once
7+
8+
#include "tinyusb.h"
9+
10+
11+
#define TINYUSB_EXTRA_EVENT_BASE 10 /*!< Base value to avoid overlap with tinyusb_event_id_t */
12+
13+
typedef enum {
14+
TINYUSB_EVENT_SUSPENDED = TINYUSB_EXTRA_EVENT_BASE, /*!< USB device suspended event, base to not overlap with tinyusb_event_id_t */
15+
TINYUSB_EVENT_RESUMED, /*!< USB device resumed event */
16+
} tinyusb_extra_event_id_t;
17+
18+
/**
19+
* @brief Test device setup
20+
*/
21+
void test_device_setup(void);
22+
23+
/**
24+
* @brief Test device teardown
25+
*/
26+
void test_device_teardown(void);
27+
28+
/**
29+
* @brief Test device wait
30+
*
31+
* @param event_id Event ID to wait for
32+
*/
33+
void test_device_wait_event(int event_id);
34+
35+
/**
36+
* @brief TinyUSB callback for device mount.
37+
*
38+
* @note
39+
* For Linux-based Hosts: Reflects the SetConfiguration() request from the Host Driver.
40+
* For Win-based Hosts: SetConfiguration() request is present only with available Class in device descriptor.
41+
*/
42+
void test_device_event_handler(tinyusb_event_t *event, void *arg);
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
## IDF Component Manager Manifest File
2+
dependencies:
3+
espressif/esp_tinyusb:
4+
version: "*"
5+
override_path: "../../../"
6+
# espressif/tinyusb:
7+
# version: "0.18.0~5"
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
/*
2+
* SPDX-FileCopyrightText: 2025 Espressif Systems (Shanghai) CO LTD
3+
*
4+
* SPDX-License-Identifier: Apache-2.0
5+
*/
6+
7+
#include <stdio.h>
8+
#include <string.h>
9+
#include "unity.h"
10+
#include "freertos/FreeRTOS.h"
11+
#include "freertos/task.h"
12+
#include "unity_test_runner.h"
13+
#include "unity_test_utils_memory.h"
14+
#include "device_common.h"
15+
16+
/* setUp runs before every test */
17+
void setUp(void)
18+
{
19+
unity_utils_record_free_mem();
20+
test_device_setup();
21+
}
22+
23+
/* tearDown runs after every test */
24+
void tearDown(void)
25+
{
26+
// Short delay to allow task to be cleaned up
27+
vTaskDelay(10);
28+
test_device_teardown();
29+
unity_utils_evaluate_leaks();
30+
}
31+
32+
33+
void app_main(void)
34+
{
35+
/*
36+
_ _ _
37+
| | (_) | |
38+
___ ___ _ __ | |_ _ _ __ _ _ _ _ ___| |__
39+
/ _ \/ __| '_ \| __| | '_ \| | | | | | / __| '_ \
40+
| __/\__ \ |_) | |_| | | | | |_| | |_| \__ \ |_) |
41+
\___||___/ .__/ \__|_|_| |_|\__, |\__,_|___/_.__/
42+
| |______ __/ |
43+
|_|______| |___/
44+
_____ _____ _____ _____
45+
|_ _| ___/ ___|_ _|
46+
| | | |__ \ `--. | |
47+
| | | __| `--. \ | |
48+
| | | |___/\__/ / | |
49+
\_/ \____/\____/ \_/
50+
*/
51+
52+
printf(" _ _ _ \n");
53+
printf(" | | (_) | | \n");
54+
printf(" ___ ___ _ __ | |_ _ _ __ _ _ _ _ ___| |__ \n");
55+
printf(" / _ \\/ __| '_ \\| __| | '_ \\| | | | | | / __| '_ \\ \n");
56+
printf("| __/\\__ \\ |_) | |_| | | | | |_| | |_| \\__ \\ |_) |\n");
57+
printf(" \\___||___/ .__/ \\__|_|_| |_|\\__, |\\__,_|___/_.__/ \n");
58+
printf(" | |______ __/ | \n");
59+
printf(" |_|______| |___/ \n");
60+
printf(" _____ _____ _____ _____ \n");
61+
printf("|_ _| ___/ ___|_ _| \n");
62+
printf(" | | | |__ \\ `--. | | \n");
63+
printf(" | | | __| `--. \\ | | \n");
64+
printf(" | | | |___/\\__/ / | | \n");
65+
printf(" \\_/ \\____/\\____/ \\_/ \n");
66+
67+
unity_utils_setup_heap_record(80);
68+
unity_utils_set_leak_level(128); // 128 (default)
69+
unity_run_menu();
70+
}

0 commit comments

Comments
 (0)