XscarfX

Untitled

Nov 18th, 2024
190
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.42 KB | None | 0 0
  1.  
  2. -------------------------------------------------------------------------------------------------
  3. #include <Arduino.h>
  4. #include "usb/usb_host.h"
  5.  
  6. // Pin definitions
  7. #define USB_DP_PIN       20    
  8. #define USB_DM_PIN       19    
  9. #define LED_PIN          2    
  10. #define VBUS_EN_PIN     18    
  11.  
  12. // Task configuration
  13. #define TASK_STACK_SIZE 4096
  14. static TaskHandle_t usb_task_handle;
  15.  
  16. void client_event_callback(const usb_host_client_event_msg_t *event_msg, void *arg) {
  17.     if (event_msg->event == USB_HOST_CLIENT_EVENT_NEW_DEV) {
  18.         Serial.println("\n*** New Device Connected! ***");
  19.         digitalWrite(LED_PIN, HIGH);
  20.     } else if (event_msg->event == USB_HOST_CLIENT_EVENT_DEV_GONE) {
  21.         Serial.println("\n*** Device Disconnected ***");
  22.         digitalWrite(LED_PIN, LOW);
  23.     }
  24. }
  25.  
  26. void usb_host_task(void *pvParameters) {
  27.     // Initialize USB Host with default PHY setup
  28.     usb_host_config_t host_config = {
  29.         .skip_phy_setup = false,
  30.         .intr_flags = ESP_INTR_FLAG_LEVEL1
  31.     };
  32.    
  33.     esp_err_t err = usb_host_install(&host_config);
  34.     if (err != ESP_OK) {
  35.         Serial.printf("USB Host install failed: %d\n", err);
  36.         return;
  37.     }
  38.    
  39.     usb_host_client_config_t client_config = {
  40.         .is_synchronous = false,
  41.         .max_num_event_msg = 5,
  42.         .async = {
  43.             .client_event_callback = client_event_callback,
  44.             .callback_arg = NULL
  45.         }
  46.     };
  47.    
  48.     usb_host_client_handle_t client_hdl;
  49.     err = usb_host_client_register(&client_config, &client_hdl);
  50.     if (err != ESP_OK) {
  51.         Serial.printf("Client register failed: %d\n", err);
  52.         return;
  53.     }
  54.    
  55.     Serial.println("USB Host stack initialized");
  56.     Serial.println("Waiting for device...");
  57.    
  58.     // Main USB event loop
  59.     while (1) {
  60.         err = usb_host_client_handle_events(client_hdl, portMAX_DELAY);
  61.         if (err != ESP_OK) {
  62.             Serial.printf("Event handling error: %d\n", err);
  63.         }
  64.         vTaskDelay(pdMS_TO_TICKS(100));
  65.     }
  66. }
  67.  
  68. void setup() {
  69.     Serial.begin(115200);
  70.     delay(1000);
  71.    
  72.     pinMode(LED_PIN, OUTPUT);
  73.     pinMode(VBUS_EN_PIN, OUTPUT);
  74.     pinMode(USB_DP_PIN, INPUT);
  75.     pinMode(USB_DM_PIN, INPUT);
  76.    
  77.     Serial.println("\n=== ESP32-S3 USB Host Test ===\n");
  78.    
  79.     // Power sequence
  80.     digitalWrite(VBUS_EN_PIN, LOW);
  81.     delay(100);
  82.    
  83.     digitalWrite(VBUS_EN_PIN, HIGH);
  84.     Serial.println("VBUS enabled");
  85.     delay(500);
  86.    
  87.     // Start USB host task
  88.     xTaskCreate(usb_host_task, "usb_host", TASK_STACK_SIZE, NULL, 5, &usb_task_handle);
  89. }
  90.  
  91. void loop() {
  92.     static unsigned long last_status = 0;
  93.     static uint8_t last_dp = 2;
  94.     static uint8_t last_dm = 2;
  95.    
  96.     // Read USB pins
  97.     uint8_t dp = digitalRead(USB_DP_PIN);
  98.     uint8_t dm = digitalRead(USB_DM_PIN);
  99.     uint8_t vbus = digitalRead(VBUS_EN_PIN);
  100.    
  101.     // Report pin changes
  102.     if (dp != last_dp || dm != last_dm) {
  103.         Serial.println("\nPin State Change:");
  104.         Serial.printf("D+: %d  D-: %d  VBUS: %d\n", dp, dm, vbus);
  105.         last_dp = dp;
  106.         last_dm = dm;
  107.     }
  108.    
  109.     // Status update every 5 seconds
  110.     if (millis() - last_status >= 5000) {
  111.         Serial.printf("\nStatus:\n");
  112.         Serial.printf("D+: %d  D-: %d  VBUS: %d\n", dp, dm, vbus);
  113.         Serial.printf("Uptime: %lu ms\n", millis());
  114.         last_status = millis();
  115.     }
  116.    
  117.     delay(100);
  118. }
  119. ------------------------------------------------------------------------------------------------------
  120.  
  121. And here is what serial monitor outputs
  122. -------------------------------------------------------------------------------------------------------
  123. 22:36:05.387 -> USB Host stack initialized
  124. 22:36:05.387 -> Waiting for device...
  125. 22:36:08.693 ->
  126. 22:36:08.693 -> Status:
  127. 22:36:08.693 -> D+: 0 D-: 0 VBUS: 1
  128. 22:36:08.693 -> Uptime: 5013 ms
  129. 22:36:12.694 ->
  130. 22:36:12.694 -> Pin State Change:
  131. 22:36:12.694 -> D+: 1 D-: 0 VBUS: 1
  132. 22:36:13.692 ->
  133. 22:36:13.692 -> Status:
  134. 22:36:13.692 -> D+: 1 D-: 0 VBUS: 1
  135. 22:36:13.692 -> Uptime: 10013 ms
  136. 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