Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- -------------------------------------------------------------------------------------------------
- #include <Arduino.h>
- #include "usb/usb_host.h"
- // Pin definitions
- #define USB_DP_PIN 20
- #define USB_DM_PIN 19
- #define LED_PIN 2
- #define VBUS_EN_PIN 18
- // Task configuration
- #define TASK_STACK_SIZE 4096
- static TaskHandle_t usb_task_handle;
- void client_event_callback(const usb_host_client_event_msg_t *event_msg, void *arg) {
- if (event_msg->event == USB_HOST_CLIENT_EVENT_NEW_DEV) {
- Serial.println("\n*** New Device Connected! ***");
- digitalWrite(LED_PIN, HIGH);
- } else if (event_msg->event == USB_HOST_CLIENT_EVENT_DEV_GONE) {
- Serial.println("\n*** Device Disconnected ***");
- digitalWrite(LED_PIN, LOW);
- }
- }
- void usb_host_task(void *pvParameters) {
- // Initialize USB Host with default PHY setup
- usb_host_config_t host_config = {
- .skip_phy_setup = false,
- .intr_flags = ESP_INTR_FLAG_LEVEL1
- };
- esp_err_t err = usb_host_install(&host_config);
- if (err != ESP_OK) {
- Serial.printf("USB Host install failed: %d\n", err);
- return;
- }
- usb_host_client_config_t client_config = {
- .is_synchronous = false,
- .max_num_event_msg = 5,
- .async = {
- .client_event_callback = client_event_callback,
- .callback_arg = NULL
- }
- };
- usb_host_client_handle_t client_hdl;
- err = usb_host_client_register(&client_config, &client_hdl);
- if (err != ESP_OK) {
- Serial.printf("Client register failed: %d\n", err);
- return;
- }
- Serial.println("USB Host stack initialized");
- Serial.println("Waiting for device...");
- // Main USB event loop
- while (1) {
- err = usb_host_client_handle_events(client_hdl, portMAX_DELAY);
- if (err != ESP_OK) {
- Serial.printf("Event handling error: %d\n", err);
- }
- vTaskDelay(pdMS_TO_TICKS(100));
- }
- }
- void setup() {
- Serial.begin(115200);
- delay(1000);
- pinMode(LED_PIN, OUTPUT);
- pinMode(VBUS_EN_PIN, OUTPUT);
- pinMode(USB_DP_PIN, INPUT);
- pinMode(USB_DM_PIN, INPUT);
- Serial.println("\n=== ESP32-S3 USB Host Test ===\n");
- // Power sequence
- digitalWrite(VBUS_EN_PIN, LOW);
- delay(100);
- digitalWrite(VBUS_EN_PIN, HIGH);
- Serial.println("VBUS enabled");
- delay(500);
- // Start USB host task
- xTaskCreate(usb_host_task, "usb_host", TASK_STACK_SIZE, NULL, 5, &usb_task_handle);
- }
- void loop() {
- static unsigned long last_status = 0;
- static uint8_t last_dp = 2;
- static uint8_t last_dm = 2;
- // Read USB pins
- uint8_t dp = digitalRead(USB_DP_PIN);
- uint8_t dm = digitalRead(USB_DM_PIN);
- uint8_t vbus = digitalRead(VBUS_EN_PIN);
- // Report pin changes
- if (dp != last_dp || dm != last_dm) {
- Serial.println("\nPin State Change:");
- Serial.printf("D+: %d D-: %d VBUS: %d\n", dp, dm, vbus);
- last_dp = dp;
- last_dm = dm;
- }
- // Status update every 5 seconds
- if (millis() - last_status >= 5000) {
- Serial.printf("\nStatus:\n");
- Serial.printf("D+: %d D-: %d VBUS: %d\n", dp, dm, vbus);
- Serial.printf("Uptime: %lu ms\n", millis());
- last_status = millis();
- }
- delay(100);
- }
- ------------------------------------------------------------------------------------------------------
- And here is what serial monitor outputs
- -------------------------------------------------------------------------------------------------------
- 22:36:05.387 -> USB Host stack initialized
- 22:36:05.387 -> Waiting for device...
- 22:36:08.693 ->
- 22:36:08.693 -> Status:
- 22:36:08.693 -> D+: 0 D-: 0 VBUS: 1
- 22:36:08.693 -> Uptime: 5013 ms
- 22:36:12.694 ->
- 22:36:12.694 -> Pin State Change:
- 22:36:12.694 -> D+: 1 D-: 0 VBUS: 1
- 22:36:13.692 ->
- 22:36:13.692 -> Status:
- 22:36:13.692 -> D+: 1 D-: 0 VBUS: 1
- 22:36:13.692 -> Uptime: 10013 ms
- As you can see i get a status change when i plug the usb device in but that is all i can get it to do
Advertisement
Add Comment
Please, Sign In to add comment