添加usb_detect

This commit is contained in:
HEYAHONG 2024-04-04 12:23:54 +08:00
parent 1dcf5cf201
commit 57bf82e330
No known key found for this signature in database
GPG Key ID: BD5679034F83A8A9
8 changed files with 330 additions and 2 deletions

1
.gitignore vendored
View File

@ -1 +1,2 @@
*~
build

View File

@ -35,5 +35,6 @@ Core可通过以下方法与上位机通信:
# 目录结构
- [HW](HW):硬件设计
- [FW](FW):固件设计,一般用于硬件的相关MCU。
- [HW](HW):硬件设计。
- [FW](FW):固件设计,一般用于硬件的相关MCU。
- [SOFT](SOFT):运行上位机的相关软件。

1
SOFT/usb_detect/.gitignore vendored Normal file
View File

@ -0,0 +1 @@
Version.h

View File

@ -0,0 +1,46 @@
cmake_minimum_required(VERSION 3.20)
#
Set(PROJECT_NAME extboard_001_usb_detect)
#
set(PROJECT_MAJOR_VERSION_STR 1)
#
set(PROJECT_MINOR_VERSION_STR 0)
#
set(PROJECT_REVISION_VERSION_STR 0)
#
string(TIMESTAMP PROJECT_BUILD_TIME_SECOND "%s" UTC)
math(EXPR PROJECT_BUILD_VERSION_STR "${PROJECT_BUILD_TIME_SECOND}/60/60/24" OUTPUT_FORMAT DECIMAL)
#
set(PROJECT_VERSION_STR "${PROJECT_MAJOR_VERSION_STR}.${PROJECT_MINOR_VERSION_STR}.${PROJECT_REVISION_VERSION_STR}.${PROJECT_BUILD_VERSION_STR}")
message(STATUS "Version:${PROJECT_VERSION_STR}")
configure_file(Version.h.in ${CMAKE_CURRENT_SOURCE_DIR}/Version.h)
#
Project(${PROJECT_NAME} C CXX ASM)
Project(${PROJECT_NAME} VERSION "${PROJECT_VERSION_STR}")
#C++
set(CMAKE_CXX_STANDARD 20)
#MSVC使UTF-8
if(MSVC)
add_compile_options(-utf-8 )
endif()
#
file(GLOB MAIN_CXX_C_FILES *.cpp *.c *.h)
#
add_executable(${PROJECT_NAME} ${MAIN_CXX_C_FILES})
include(FindPkgConfig)
#libusb-1.0
pkg_check_modules(LibUSB REQUIRED IMPORTED_TARGET libusb-1.0)
target_link_libraries(${PROJECT_NAME} PkgConfig::LibUSB)
#
include(GNUInstallDirs)
install(TARGETS ${PROJECT_NAME})

15
SOFT/usb_detect/ReadMe.md Normal file
View File

@ -0,0 +1,15 @@
# 说明
本程序用于检测ExtBoard-001的存在。
主要依赖以下第三方库:
- [libusb-1.0](https://libusb.info/):提供通用的USB设备访问方式的C库
# 编译
本程序采用[CMake](https://cmake.org)管理构建。
# 截图
![usb_detect_no_core_usb](usb_detect_no_core_usb.png)

View File

@ -0,0 +1,14 @@
#ifndef __PROJECT_VERSION_H__
#define __PROJECT_VERSION_H__
#define PROJECT_MAJOR_VERSION ${PROJECT_MAJOR_VERSION_STR}
#define PROJECT_MINOR_VERSION ${PROJECT_MINOR_VERSION_STR}
#define PROJECT_REVISION_VERSION ${PROJECT_REVISION_VERSION_STR}
#define PROJECT_BUILD_VERSION ${PROJECT_BUILD_VERSION_STR}
#define PROJECT_VERSION_STR "${PROJECT_VERSION_STR}"
#endif

View File

@ -0,0 +1,250 @@

#include "stdio.h"
#include "stdint.h"
#include "libusb.h"
#include "Version.h"
#include "string"
#include "vector"
#ifndef LIBUSB_API_VERSION
#error "libusb is too old!"
#endif // LIBUSB_API_VERSION
typedef struct
{
uint8_t bus_number;//ExtBoard-001所在的usb总线号
uint8_t port_path[8];//ExtBoard-001所在的usb总线路径,即USB Hub芯片连接的端口
bool core_is_use_usb;//ExtBoard-001是否使用usb总线
} extboard_001_info_t;
std::vector<extboard_001_info_t> extboard_001_list;
int main(int argc,const char *argv[])
{
printf("version:%s\nlibusb(%d.%d.%d) api version:%08X \n",PROJECT_VERSION_STR,libusb_get_version()->major,libusb_get_version()->minor,libusb_get_version()->micro,LIBUSB_API_VERSION);
printf("\nusb enum start!\n");
//初始化libusb上下文
libusb_context *ctx=NULL;
libusb_init(&ctx);
if(ctx == NULL)
{
printf("libusb init failed!\n");
return -1;
}
{
//查找ExtBoard-001的DAPLink
ssize_t usb_device_count=0;
libusb_device **usb_device_list=NULL;
//获取usb设备列表
usb_device_count=libusb_get_device_list(ctx,&usb_device_list);
if(usb_device_count > 0 && usb_device_list !=NULL)
{
for(ssize_t i=0; i<usb_device_count; i++)
{
libusb_device *usb_device=usb_device_list[i];
libusb_device_descriptor usb_device_desc= {0};
if(0!=libusb_get_device_descriptor(usb_device,&usb_device_desc))
{
continue;
}
//通过DAPLink的vid与pid查找ExtBoard-001
if(usb_device_desc.idVendor == 0x0D28 && usb_device_desc.idProduct == 0x0204)
{
libusb_device_handle *usb_device_handle=NULL;
libusb_open(usb_device,&usb_device_handle);
if(usb_device_handle == NULL)
{
continue;
}
//制造商
std::string Manufacturer;
{
//读取制造商
char buff[256]= {0};
libusb_get_string_descriptor_ascii(usb_device_handle,usb_device_desc.iManufacturer,(uint8_t *)buff,sizeof(buff));
Manufacturer=buff;
}
//产品名称
std::string Product;
{
//读取产品名称
char buff[256]= {0};
libusb_get_string_descriptor_ascii(usb_device_handle,usb_device_desc.iProduct,(uint8_t *)buff,sizeof(buff));
Product=buff;
}
libusb_close(usb_device_handle);
if(Manufacturer == "HYH" && Product == "ExtBoard-001")
{
//已找到ExtBoard-001的DAPLink
printf("DAPLink is found!\n");
//读取bus_number
uint8_t usb_bus=libusb_get_bus_number(usb_device);
printf("\tbus number:\t%d\n",(int)usb_bus);
//读取port_path
uint8_t usb_port_path[8]= {0};
libusb_get_port_path(ctx,usb_device,usb_port_path,sizeof(usb_port_path));
uint8_t extboard_usb_port_length=0;
bool is_extboard_daplink=true;
{
printf("\tport_path:\t");
for(size_t i=0; i<sizeof(usb_port_path); i++)
{
if(usb_port_path[i] == 0)
{
break;
}
if(usb_port_path[i+1] == 0)
{
extboard_usb_port_length=i;
if(usb_port_path[i] != 1)
{
// DAPLink不在端口1,可能不是ExtBoard-001的DAPLink
is_extboard_daplink=false;
}
}
if(i > 0)
{
printf(".");
}
printf("%d",usb_port_path[i]);
}
printf("\n");
}
if(is_extboard_daplink && extboard_usb_port_length > 0)
{
//添加到ExtBoard-001列表
extboard_001_info_t info;
info.bus_number=usb_bus;
memset(info.port_path,0,sizeof(info.port_path));
memcpy(info.port_path,usb_port_path,extboard_usb_port_length);
info.core_is_use_usb=false;
extboard_001_list.push_back(info);
}
}
}
}
//释放设备列表
libusb_free_device_list(usb_device_list,usb_device_count);
}
}
{
//判断ExtBoard-001的Core是否使用USB
ssize_t usb_device_count=0;
libusb_device **usb_device_list=NULL;
//获取usb设备列表
usb_device_count=libusb_get_device_list(ctx,&usb_device_list);
if(usb_device_count > 0 && usb_device_list !=NULL)
{
for(ssize_t i=0; i<usb_device_count; i++)
{
libusb_device *usb_device=usb_device_list[i];
//读取bus_number
uint8_t usb_bus=libusb_get_bus_number(usb_device);
//读取port_path
uint8_t usb_port_path[8]= {0};
libusb_get_port_path(ctx,usb_device,usb_port_path,sizeof(usb_port_path));
for(std::vector<extboard_001_info_t>::iterator it=extboard_001_list.begin(); it!=extboard_001_list.end(); it++)
{
extboard_001_info_t & info=(*it);
if(info.bus_number == usb_bus)
{
uint8_t core_port_path[8] = {0};
memcpy(core_port_path,info.port_path,sizeof(info.port_path));
for(size_t i =0 ; i < sizeof(core_port_path); i++)
{
if(core_port_path[i]==0)
{
//Core连接到端口2
core_port_path[i]=2;
break;
}
}
if(memcmp(core_port_path,usb_port_path,sizeof(core_port_path)) == 0)
{
//Core连接到USB如需对Core进行操作,可在此处打开usb_device
info.core_is_use_usb=true;
}
}
}
}
//释放设备列表
libusb_free_device_list(usb_device_list,usb_device_count);
}
}
//销毁libusb上下文
libusb_exit(ctx);
printf("\nusb enum done!\n");
{
printf("\nlist ExtBoard-001!\n");
//打印ExtBoard-001信息
for(size_t i =0 ; i<extboard_001_list.size(); i++)
{
extboard_001_info_t & info=extboard_001_list[i];
printf("ExtBoard-001 %d:\n",(int)i);
printf("\tusb bus number:\t%d\n",(int)info.bus_number);
printf("\tusb port path:\t");
for(size_t i=0; i<sizeof(info.port_path); i++)
{
if(info.port_path[i] == 0)
{
break;
}
if(i > 0)
{
printf(".");
}
printf("%d",info.port_path[i]);
}
printf("\n");
printf("\tcore is use usb:%s\n",info.core_is_use_usb?"true":"false");
}
}
return 0;
}

Binary file not shown.

After

Width:  |  Height:  |  Size: 107 KiB