Skip to content

Instantly share code, notes, and snippets.

@chegewara
Created December 11, 2021 12:36
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save chegewara/8f497fe8d8cf6b4597ea74f601307ff4 to your computer and use it in GitHub Desktop.
Save chegewara/8f497fe8d8cf6b4597ea74f601307ff4 to your computer and use it in GitHub Desktop.
usb host test app
#include <stdio.h>
#include "freertos/FreeRTOS.h"
#include "freertos/task.h"
#include "esp_log.h"
#include "usb/usb_host.h"
usb_host_client_handle_t client_hdl;
uint8_t dev_addr = 2;
usb_device_handle_t dev_hdl;
TaskHandle_t async_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)
{
ESP_LOGI("", "client event: %d, address: %d", event_msg->event, event_msg->new_dev.address);
} else {
ESP_LOGI("", "USB_HOST_CLIENT_EVENT_DEV_GONE");
usb_host_device_close(client_hdl, dev_hdl);
}
}
void app_main(void)
{
do{
const usb_host_config_t config = {
.intr_flags = ESP_INTR_FLAG_LEVEL1,
};
esp_err_t err = usb_host_install(&config);
ESP_LOGI("", "install status: %d", err);
const usb_host_client_config_t client_config = {
.async = {
.client_event_callback = client_event_callback,
.callback_arg = NULL,
},
.max_num_event_msg = 5
};
err = usb_host_client_register(&client_config, &client_hdl);
ESP_LOGI("", "client register status: %d", err);
while (1)
{
uint32_t event_flags;
if (ESP_OK == usb_host_lib_handle_events(1, &event_flags))
{
printf("usb_host_lib_handle_events event_flags: %d\n", event_flags & 0xff);
usb_host_client_handle_events(client_hdl, 0);
if (event_flags & USB_HOST_LIB_EVENT_FLAGS_NO_CLIENTS)
{
printf("No more clients\n");
do{
if(usb_host_device_free_all() != ESP_ERR_NOT_FINISHED) break;
}while(1);
break;
}
if (event_flags & USB_HOST_LIB_EVENT_FLAGS_ALL_FREE)
{
printf("USB_HOST_LIB_EVENT_FLAGS_ALL_FREE\n");
usb_host_client_deregister(client_hdl);
}
}
}
usb_host_uninstall();
}while(1);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment