0
0
Fork 0

feat: add winusb support

This commit is contained in:
windowsair 2020-11-12 13:20:57 +08:00
parent 1016da0803
commit 88c12b5332
8 changed files with 117 additions and 68 deletions

View File

@ -1,12 +1,13 @@
language: c
sudo: required
before_install:
- sudo apt install -y gcc git wget make libncurses-dev flex bison python python-serial
- sudo apt install -y gcc git wget make libncurses-dev flex bison python python-serial ninja-build
- wget https://dl.espressif.com/dl/xtensa-lx106-elf-linux64-1.22.0-100-ge567ec7-5.2.0.tar.gz
install:
- tar -xzf ./xtensa-lx106-elf-linux64-1.22.0-100-ge567ec7-5.2.0.tar.gz
- git clone --recursive https://github.com/espressif/ESP8266_RTOS_SDK.git
- wget https://github.com/espressif/ESP8266_RTOS_SDK/releases/download/v3.3-rc1/ESP8266_RTOS_SDK-v3.3-rc1.zip
- unzip ESP8266_RTOS_SDK-v3.3-rc1.zip
- python -m pip install --user -r ./ESP8266_RTOS_SDK/requirements.txt
before_script:

View File

@ -61,7 +61,7 @@ const uint8_t bosDescriptor[kLengthOfBos] =
0x05, // bLength of this descriptor
USB_DESCRIPTOR_TYPE_BOS, // BOS Descriptor type(Constant)
USBShort(kLengthOfBos), // wLength
0x01, // bNumDeviceCaps
0x01, // bNumDeviceCaps -> only 0x01 for OS2.0 descriptor
// Microsoft OS 2.0 platform capability descriptor header (Table 4)
// See also:

View File

@ -19,15 +19,9 @@
#include "usb_defs.h"
#include "MSOS20Descriptors.h"
// const char *strings_list[] = {
// 0, // reserved: available languages
// "windowsair",
// "CMSIS-DAP v2",
// "1234",
// };
const char *strings_list[] = {
0, // reserved: available languages
0, // reserved: available languages -> iInterface
"windowsair",
"esp8266 CMSIS-DAP",
"1234",
@ -241,8 +235,7 @@ static void handleGetDescriptor(usbip_stage2_header *header)
else
{
os_printf("Sending ALL CONFIG\r\n");
send_stage2_submit(header, 0, header->u.cmd_submit.data_length);
send_stage2_submit(header, 0, sizeof(kUSBd0ConfigDescriptor) + sizeof(kUSBd0InterfaceDescriptor));
send(kSock, kUSBd0ConfigDescriptor, sizeof(kUSBd0ConfigDescriptor), 0);
send(kSock, kUSBd0InterfaceDescriptor, sizeof(kUSBd0InterfaceDescriptor), 0);
}
@ -315,16 +308,19 @@ static void handleGetDescriptor(usbip_stage2_header *header)
////TODO:UNIMPLEMENTED
send_stage2_submit(header, 0, 0);
break;
#if (USE_WINUSB == 1)
case USB_DT_BOS:
os_printf("* GET 0x0F BOS DESCRIPTOR\r\n");
send_stage2_submit_data(header, 0, bosDescriptor, sizeof(bosDescriptor));
break;
#else
case USB_DT_HID_REPORT:
os_printf("* GET 0x22 HID REPORT DESCRIPTOR\r\n");
send_stage2_submit_data(header, 0, (void *)kHidReportDescriptor, sizeof(kHidReportDescriptor));
break;
#endif
default:
//// TODO: ms os 1.0 descriptor
os_printf("USB unknown Get Descriptor requested:%d\r\n", header->u.cmd_submit.request.wValue.u8lo);
os_printf("low bit :%d\r\n",header->u.cmd_submit.request.wValue.u8lo);
os_printf("high bit :%d\r\n",header->u.cmd_submit.request.wValue.u8hi);

View File

@ -26,7 +26,12 @@ const uint8_t kUSBd0DeviceDescriptor[0x12] =
{
0x12, // bLength
USB_DT_DEVICE, // bDescriptorType
#if (USE_WINUSB == 1)
USBShort(0x0210), // bcdUSB
#else
USBShort(0x0200), // bcdUSB
#endif
////TODO: Is it also available elsewhere?
// We need to use a device other than the USB-IF standard, set to 0x00

View File

@ -1,8 +1,9 @@
/**
* @file DAP_handle.c
* @brief Handle DAP packets and transaction push
* @version 0.2
* @date 2020-02-04
* @version 0.3
* @date 2020-02-04 first version
* 2020-11-11 support WinUSB mode
*
* @copyright Copyright (c) 2020
*
@ -31,9 +32,26 @@ extern TaskHandle_t kDAPTaskHandle;
////TODO: Merge this
#define DAP_PACKET_SIZE 255
static uint8_t data_out[DAP_PACKET_SIZE];
#if (USE_WINUSB == 1)
typedef struct
{
uint32_t length;
uint8_t buf[DAP_PACKET_SIZE];
} DAPPacetDataType;
#else
typedef struct
{
uint8_t buf[DAP_PACKET_SIZE];
} DAPPacetDataType;
#endif
#define DAP_HANDLE_SIZE (sizeof(DAPPacetDataType))
static DAPPacetDataType DAPDataProcessed;
static int dap_respond = 0;
// SWO Trace
static int swo_trace_respond = 0;
static uint8_t *swo_data_to_send;
@ -52,37 +70,52 @@ void handle_dap_data_request(usbip_stage2_header *header, uint32_t length)
// Point to the beginning of the URB packet
#if (USE_WINUSB == 1)
dap_respond = DAP_ProcessCommand((uint8_t *)data_in, (uint8_t *)data_out);
//handle_dap_data_response(header);
send_stage2_submit(header, 0, 0);
#else
xRingbufferSend(dap_dataIN_handle, data_in, length - sizeof(usbip_stage2_header), portMAX_DELAY);
//os_printf("LENGTH: %d\r\n", length - sizeof(usbip_stage2_header));
xTaskNotifyGive(kDAPTaskHandle);
send_stage2_submit(header, 0, 0);
// always send constant size buf -> cuz we don't care about the IN packet size
// and to unify the style, we set aside the length of the section
xRingbufferSend(dap_dataIN_handle, data_in - sizeof(uint32_t), DAP_HANDLE_SIZE, portMAX_DELAY);
xTaskNotifyGive(kDAPTaskHandle);
#else
send_stage2_submit(header, 0, 0);
xRingbufferSend(dap_dataIN_handle, data_in, DAP_HANDLE_SIZE, portMAX_DELAY);
xTaskNotifyGive(kDAPTaskHandle);
#endif
// dap_respond = DAP_ProcessCommand((uint8_t *)data_in, (uint8_t *)data_out);
// //handle_dap_data_response(header);
// send_stage2_submit(header, 0, 0);
}
void handle_dap_data_response(usbip_stage2_header *header)
{
if (dap_respond)
{
send_stage2_submit_data(header, 0, data_out, DAP_PACKET_SIZE);
dap_respond = 0;
}
else
{
send_stage2_submit(header, 0, 0);
}
return;
// int resLength = dap_respond & 0xFFFF;
// if (resLength)
// {
// send_stage2_submit_data(header, 0, (void *)DAPDataProcessed.buf, resLength);
// dap_respond = 0;
// }
// else
// {
// send_stage2_submit(header, 0, 0);
// }
}
void handle_swo_trace_response(usbip_stage2_header *header)
{
// TODO:
send_stage2_submit(header, 0, 0);
return;
if (swo_trace_respond)
{
swo_trace_respond = 0;
send_stage2_submit_data(header, 0, data_out, DAP_PACKET_SIZE);
//send_stage2_submit_data(header, 0, (void *)DAPDataProcessed.buf, DAP_PACKET_SIZE);
}
else
{
@ -109,11 +142,12 @@ void SWO_AbortTransfer(void)
void DAP_Thread(void *argument)
{
dap_dataIN_handle = xRingbufferCreate(DAP_PACKET_SIZE * 20, RINGBUF_TYPE_BYTEBUF);
dap_dataOUT_handle = xRingbufferCreate(DAP_PACKET_SIZE * 10, RINGBUF_TYPE_BYTEBUF);
dap_dataIN_handle = xRingbufferCreate(DAP_HANDLE_SIZE * 20, RINGBUF_TYPE_BYTEBUF);
dap_dataOUT_handle = xRingbufferCreate(DAP_HANDLE_SIZE * 20, RINGBUF_TYPE_BYTEBUF);
data_response_mux = xSemaphoreCreateMutex();
size_t packetSize;
uint8_t *item;
int resLength;
DAPPacetDataType *item;
if (dap_dataIN_handle == NULL || dap_dataIN_handle == NULL ||
data_response_mux == NULL)
@ -128,14 +162,14 @@ void DAP_Thread(void *argument)
{
ulTaskNotifyTake(pdFALSE, portMAX_DELAY);
packetSize = 0;
item = (uint8_t *)xRingbufferReceiveUpTo(dap_dataIN_handle, &packetSize,
(1 / portTICK_RATE_MS), DAP_PACKET_SIZE);
item = (DAPPacetDataType *)xRingbufferReceiveUpTo(dap_dataIN_handle, &packetSize,
(1 / portTICK_RATE_MS), DAP_HANDLE_SIZE);
if (packetSize == 0)
{
break;
}
else if (packetSize < DAP_PACKET_SIZE)
else if (packetSize < DAP_HANDLE_SIZE)
{
os_printf("Wrong data in packet size:%d , data in remain: %d\r\n", packetSize, (int)xRingbufferGetMaxItemSize(dap_dataIN_handle));
vRingbufferReturnItem(dap_dataIN_handle, (void *)item);
@ -143,12 +177,22 @@ void DAP_Thread(void *argument)
// This may not happen because there is a semaphore acquisition
}
if (item[0] == ID_DAP_QueueCommands)
item[0] = ID_DAP_ExecuteCommands;
DAP_ProcessCommand(item, data_out);
if (item->buf[0] == ID_DAP_QueueCommands)
{
item->buf[0] = ID_DAP_ExecuteCommands;
}
vRingbufferReturnItem(dap_dataIN_handle, (void *)item);
xRingbufferSend(dap_dataOUT_handle, data_out, DAP_PACKET_SIZE, portMAX_DELAY);
resLength = DAP_ProcessCommand((uint8_t *)item->buf, (uint8_t *)DAPDataProcessed.buf); // use first 4 byte to save length
resLength &= 0xFFFF; // res length in lower 16 bits
vRingbufferReturnItem(dap_dataIN_handle, (void *)item); // process done.
// now prepare to reply
#if (USE_WINUSB == 1)
DAPDataProcessed.length = resLength;
#endif
xRingbufferSend(dap_dataOUT_handle, (void *)&DAPDataProcessed, DAP_HANDLE_SIZE, portMAX_DELAY);
if (xSemaphoreTake(data_response_mux, portMAX_DELAY) == pdTRUE)
{
++dap_respond;
@ -164,19 +208,28 @@ int fast_reply(uint8_t *buf, uint32_t length)
{
if (dap_respond > 0)
{
uint8_t *item;
DAPPacetDataType *item;
size_t packetSize = 0;
item = (uint8_t *)xRingbufferReceiveUpTo(dap_dataOUT_handle, &packetSize,
(10 / portTICK_RATE_MS), DAP_PACKET_SIZE);
if (packetSize == DAP_PACKET_SIZE)
item = (DAPPacetDataType *)xRingbufferReceiveUpTo(dap_dataOUT_handle, &packetSize,
(10 / portTICK_RATE_MS), DAP_HANDLE_SIZE);
if (packetSize == DAP_HANDLE_SIZE)
{
unpack((uint32_t *)buf, sizeof(usbip_stage2_header));
send_stage2_submit_data((usbip_stage2_header *)buf, 0, item, DAP_PACKET_SIZE);
#if (USE_WINUSB == 1)
uint32_t resLength = item->length;
send_stage2_submit_data((usbip_stage2_header *)buf, 0, item->buf, resLength);
#else
send_stage2_submit_data((usbip_stage2_header *)buf, 0, item->buf, DAP_HANDLE_SIZE);
#endif
if (xSemaphoreTake(data_response_mux, portMAX_DELAY) == pdTRUE)
{
--dap_respond;
xSemaphoreGive(data_response_mux);
}
vRingbufferReturnItem(dap_dataOUT_handle, (void *)item);
return 1;
}
@ -188,8 +241,9 @@ int fast_reply(uint8_t *buf, uint32_t length)
}
else
{
buf[3] = 0x3; // command
buf[15] = 0; // direction
//// TODO: ep0 dir 0 ?
buf[0x3] = 0x3; // command
buf[0xF] = 0; // direction
buf[0x16] = 0;
buf[0x17] = 0;
buf[27] = 0;
@ -203,7 +257,6 @@ int fast_reply(uint8_t *buf, uint32_t length)
static void unpack(void *data, int size)
{
// Ignore the setup field
int sz = (size / sizeof(uint32_t)) - 2;
uint32_t *ptr = (uint32_t *)data;

View File

@ -28,9 +28,8 @@
#include "wifi_configuration.h"
extern void SWO_Thread(void *argument);
extern void usart_monitor_task(void *argument);
extern void usart_monitor_task(void *argument);
extern void DAP_Setup(void);
extern void DAP_Thread(void *argument);
@ -63,14 +62,14 @@ static esp_err_t event_handler(void *ctx, system_event_t *event)
break;
case SYSTEM_EVENT_STA_GOT_IP:
xEventGroupSetBits(wifi_event_group, IPV4_GOTIP_BIT);
os_printf("SYSTEM_EVENT_STA_GOT_IP\r\n");
os_printf("SYSTEM EVENT STA GOT IP : %s\r\n", ip4addr_ntoa(&event->event_info.got_ip.ip_info.ip));
break;
case SYSTEM_EVENT_STA_DISCONNECTED:
os_printf("Disconnect reason : %d\r\n", info->disconnected.reason);
if (info->disconnected.reason == WIFI_REASON_BASIC_RATE_NOT_SUPPORT)
{
/*Switch to 802.11 bgn mode */
esp_wifi_set_protocol(ESP_IF_WIFI_STA, WIFI_PROTOCAL_11B | WIFI_PROTOCAL_11G | WIFI_PROTOCAL_11N);
esp_wifi_set_protocol(ESP_IF_WIFI_STA, WIFI_PROTOCOL_11B | WIFI_PROTOCOL_11G | WIFI_PROTOCOL_11N);
}
esp_wifi_connect();
xEventGroupClearBits(wifi_event_group, IPV4_GOTIP_BIT);
@ -96,7 +95,7 @@ static void initialise_wifi(void)
{
tcpip_adapter_init();
wifi_event_group = xEventGroupCreate();
ESP_ERROR_CHECK(esp_event_loop_init(event_handler, NULL));
wifi_init_config_t cfg = WIFI_INIT_CONFIG_DEFAULT();
ESP_ERROR_CHECK(esp_wifi_init(&cfg));
@ -111,7 +110,6 @@ static void initialise_wifi(void)
ESP_ERROR_CHECK(esp_wifi_set_mode(WIFI_MODE_STA));
ESP_ERROR_CHECK(esp_wifi_set_config(ESP_IF_WIFI_STA, &wifi_config));
ESP_ERROR_CHECK(esp_wifi_start());
}
static void wait_for_ip()
@ -128,21 +126,17 @@ static void wait_for_ip()
}
void app_main()
{
ESP_ERROR_CHECK(nvs_flash_init());
initialise_wifi();
wait_for_ip();
DAP_Setup(); // DAP Setup
DAP_Setup(); // DAP Setup
xTaskCreate(timer_create_task, "timer_create", 512, NULL, 10, NULL);
xTaskCreate(tcp_server_task, "tcp_server", 4096, NULL, 20, NULL);
xTaskCreate(DAP_Thread, "DAP_Task", 2048, NULL, 22, &kDAPTaskHandle);
// SWO Trace Task
//xTaskCreate(SWO_Thread, "swo_task", 1024, NULL, 6, NULL);
//xTaskCreate(usart_monitor_task, "uart_task", 512, NULL, 6, NULL);
}

View File

@ -34,7 +34,7 @@ int kSock = -1;
void tcp_server_task(void *pvParameters)
{
uint8_t tcp_rx_buffer[305];
uint8_t tcp_rx_buffer[768];
char addr_str[128];
int addr_family;
int ip_protocol;

View File

@ -181,13 +181,12 @@ static void send_interface_info()
int emulate(uint8_t *buffer, uint32_t length)
{
// usbip_stage2_header header;
#if (USE_WINUSB == 0)
if(fast_reply(buffer, length))
{
return 0;
}
#endif
int command = read_stage2_command((usbip_stage2_header *)buffer, length);
if (command < 0)
{
@ -281,6 +280,7 @@ static int handle_submit(usbip_stage2_header *header, uint32_t length)
{
// control endpoint(endpoint 0)
case 0x00:
//// TODO: judge usb setup 8 byte?
handleUSBControlRequest(header);
break;