0
0
Fork 0

Merge pull request #2 from windowsair/develop

Develop
This commit is contained in:
windowsair 2021-05-14 21:28:55 +08:00 committed by GitHub
commit 9318c1603f
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
13 changed files with 246 additions and 176 deletions

View File

@ -1,9 +0,0 @@
#
# This is a project Makefile. It is assumed the directory this Makefile resides in is a
# project subdirectory.
#
PROJECT_NAME := tcp_server
include $(IDF_PATH)/make/project.mk

View File

@ -182,7 +182,7 @@ __FORCEINLINE void DAP_SPI_Send_Header(const uint8_t packetHeaderData, uint8_t *
*/
__FORCEINLINE void DAP_SPI_Read_Data(uint32_t *resData, uint8_t *resParity)
{
uint64_t dataBuf;
volatile uint64_t dataBuf;
uint32_t *pU32Data = (uint32_t *)&dataBuf;
DAP_SPI.user.usr_mosi = 0;

View File

@ -2,10 +2,11 @@
* @file MSOS20Descriptors.c
* @author windowsair
* @brief Store related data of Microsoft OS 2.0 descriptor
* @version 0.1
* @date 2019-11-21
* @change: 2021-5-12 Add support for USB 3.0
* @version 0.2
* @date 2021-5-12
*
* @copyright Copyright (c) 2019
* @copyright Copyright (c) 2021
*
*/
@ -57,22 +58,48 @@ const uint8_t msOs20DescriptorSetHeader[kLengthOfMsOS20] =
const uint8_t bosDescriptor[kLengthOfBos] =
{
// Universal Serial Bus 3.0 Specification, Table 9-9.
// USB 3.0 Specification, Table 9-9.
0x05, // bLength of this descriptor
USB_DESCRIPTOR_TYPE_BOS, // BOS Descriptor type(Constant)
USBShort(kLengthOfBos), // wLength
#if (USE_USB_3_0 == 1)
0x03, // bNumDeviceCaps -> USB2.0 extension & SuperSpeed USB Device & OS2.0 descriptor
#else
0x01, // bNumDeviceCaps -> only 0x01 for OS2.0 descriptor
#endif // USE_USB_3_0 == 1
#if (USE_USB_3_0 == 1)
// USB 2.0 extension, USB 3.0 Specification, Table 9-12.
0x07, // bLength of this descriptor
USB_DESCRIPTOR_TYPE_DEVICE_CAPABILITY, // DEVICE CAPABILITY Descriptor type
USB_DEVICE_CAPABILITY_TYPE_USB2_0_EXTENSION, // Capability type: USB 2.0 EXTENSION
0x02, 0x00, 0x00, 0x00, // bmAttributes -> LPM Support
// SuperSpeed USB Device, USB 3.0 Specification, Table 9-13.
0x0A, // bLength of this descriptor
USB_DESCRIPTOR_TYPE_DEVICE_CAPABILITY, // DEVICE CAPABILITY Descriptor type
USB_DEVICE_CAPABILITY_TYPE_SUPERSPEED_USB, // Capability type: SUPERSPEED_USB
0x00, // bmAttributes -> LTM Capable
0x08, 0x00, // wSpeedsSupported -> only support SuperSpeed
0x03, // bFunctionalitySupport
0x00, // bU1DevExitLat -> 0 may be ok
0x00, 0x00, // wU2DevExitLat -> 0 may be ok
#endif // USE_USB_3_0 == 1
// Microsoft OS 2.0 platform capability descriptor header (Table 4)
// See also:
// Universal Serial Bus 3.0 Specification : Format of a Device Capability Descriptor, Table 9-10.
// USB 3.0 Specification : Format of a Device Capability Descriptor, Table 9-10.
0x1C, // bLength of this first device capability descriptor
// bLength -> The total length of the remaining arrays containing this field
USB_DESCRIPTOR_TYPE_DEVICE_CAPABILITY, // bDescriptorType
USB_DEVICE_CAPABILITY_TYPE_PLATFORM, // bDevCapabilityType
// Capability-Dependent (See USB3.0 Specification Table 9-10.)
// Capability-Dependent (See USB 3.0 Specification Table 9-10.)
0x00, // bReserved
USB_DEVICE_CAPABILITY_UUID, // MS_OS_20_Platform_Capability_ID

View File

@ -2,18 +2,26 @@
* @file MSOS20Descriptors.h
* @author windowsair
* @brief
* @version 0.1
* @date 2019-11-21
* @version 0.2
* @date 2021-5-12
*
* @copyright Copyright (c) 2019
* @copyright Copyright (c) 2021
*
*/
#ifndef __MSOS20DESCRIPTORS_H__
#define __MSOS20DESCRIPTORS_H__
#include "dap_configuration.h"
#define kLengthOfMsOS20 0xA2
#if (USE_USB_3_0 == 1)
#define kLengthOfBos 0x32
#else
#define kLengthOfBos 0x21
#endif // USE_USB_3_0 == 1
#define kValueOfbMS_VendorCode 0x01// Just set to 0x01
extern const uint8_t bosDescriptor[kLengthOfBos];
extern const uint8_t msOs20DescriptorSetHeader[kLengthOfMsOS20];
@ -23,6 +31,11 @@ extern const uint8_t msOs20DescriptorSetHeader[kLengthOfMsOS20];
// Platform capability BOS descriptor, Table 1.
#define USB_DEVICE_CAPABILITY_TYPE_PLATFORM 5
// USB 2.0 Extension Descriptor, USB3.0 Specification Table 9-11
#define USB_DEVICE_CAPABILITY_TYPE_USB2_0_EXTENSION 2
// SuperSpeed USB specific device level capabilities, USB3.0 Specification Table 9-11
#define USB_DEVICE_CAPABILITY_TYPE_SUPERSPEED_USB 3
// Platform capability UUID, Table 3.
// {D8DD60DF-4589-4CC7-9CD2-659D9E648A9F}
#define USB_DEVICE_CAPABILITY_UUID 0xDF, 0x60, 0xDD, 0xD8, 0x89, 0x45, 0xC7, 0x4C, 0x9C, 0xD2, 0x65, 0x9D, 0x9E, 0x64, 0x8A, 0x9F

View File

@ -2,7 +2,8 @@
/**
* @file USBd_config.c
* @brief Standard USB Descriptor Definitions
fix bugs 2020-1-23
* @change: 2020-1-23 : fix bugs
* 2021-5-12 : Add support for USB 3.0
* @version 0.2
* @date 2020-1-23
*
@ -13,7 +14,7 @@
#include "USBd_config.h"
#include "usb_defs.h"
#define USBShort(ui16Value) ((ui16Value) & 0xff), ((ui16Value) >> 8) //((ui16Value) & 0xFF),(((ui16Value) >> 8) & 0xFF)
#define USBShort(ui16Value) ((ui16Value) & 0xff), ((ui16Value) >> 8)
/**
@ -28,10 +29,16 @@ const uint8_t kUSBd0DeviceDescriptor[0x12] =
USB_DT_DEVICE, // bDescriptorType
#if (USE_WINUSB == 1)
USBShort(0x0210), // bcdUSB
#if (USE_USB_3_0 == 1)
USBShort(0x0300), // bcdUSB
#else
USBShort(0x210), // bcdUSB
#endif // USE_USB_3_0 == 1
#else
USBShort(0x0200), // bcdUSB
#endif
#endif // (USE_WINUSB == 1)
////TODO: Is it also available elsewhere?
// We need to use a device other than the USB-IF standard, set to 0x00
@ -39,7 +46,11 @@ const uint8_t kUSBd0DeviceDescriptor[0x12] =
0x00, // bDeviceSubClass
0x00, // bDeviceProtocol
#if (USE_USB_3_0 == 1)
0x09, // bMaxPacketSize0, for USB 3.0 must set to 0x09(2^9)
#else
USBD0_MAX_PACKET0, // bMaxPacketSize0 Maximum packet size for default pipe.
#endif
USBShort(USBD0_DEV_DESC_IDVENDOR), // idVendor Vendor ID (VID).
USBShort(USBD0_DEV_DESC_IDPRODUCT), // idProduct Product ID (PID).
USBShort(USBD0_DEV_DESC_BCDDEVICE), // bcdDevice Device Version BCD.
@ -61,7 +72,7 @@ const uint8_t kUSBd0DeviceDescriptor[0x12] =
// Standard Interface Descriptor
#if (USE_WINUSB ==1)
const uint8_t kUSBd0InterfaceDescriptor[0x1E]=
const uint8_t kUSBd0InterfaceDescriptor[]=
{
0x09, // bLength
USB_DT_INTERFACE, // bDescriptorType
@ -98,10 +109,19 @@ const uint8_t kUSBd0InterfaceDescriptor[0x1E]=
USB_DT_ENDPOINT, // bDescriptorType
0x01, // bEndpointAddress
USB_ENDPOINT_ATTR_BULK, // bmAttributes
USBShort(512), // wMaxPacketSize
// We assume that it always runs in High Speed.
USBShort(USB_ENDPOINT_SIZE), // wMaxPacketSize
0x00, // bInterval
/* SuperSpeed Endpoint Companion */
#if (USE_USB_3_0 == 1)
0x06, // bLength
USB_DT_SUPERSPEED_USB_ENDPOINT_COMPANION, // bDescriptorType
0x00, // bMaxBurst
0x00, // bmAttributes(MaxStream for Bulk)
0x00, 0x00, // wBytesPerInterval -> 0 for Bulk
#endif // USE_USB_3_0 == 1
/* Pysical endpoint 1 */
// "Endpoint 2: Bulk In used for responses send to host PC." Device -> PC
@ -109,9 +129,19 @@ const uint8_t kUSBd0InterfaceDescriptor[0x1E]=
USB_DT_ENDPOINT, // bDescriptorType
0x81, // bEndpointAddress
USB_ENDPOINT_ATTR_BULK, // bmAttributes
USBShort(512), // wMaxPacketSize
USBShort(USB_ENDPOINT_SIZE), // wMaxPacketSize
0x00, // bInterval
/* SuperSpeed Endpoint Companion */
#if (USE_USB_3_0 == 1)
0x06, // bLength
USB_DT_SUPERSPEED_USB_ENDPOINT_COMPANION, // bDescriptorType
0x00, // bMaxBurst
0x00, // bmAttributes(MaxStream for Bulk)
0x00, 0x00, // wBytesPerInterval -> 0 for Bulk
#endif // USE_USB_3_0 == 1
/* Pysical endpoint 2 */
// "Endpoint 3: Bulk In (optional) used for streaming SWO trace" Device -> PC
@ -119,9 +149,17 @@ const uint8_t kUSBd0InterfaceDescriptor[0x1E]=
USB_DT_ENDPOINT, // bDescriptorType
0x82, // bEndpointAddress
USB_ENDPOINT_ATTR_BULK, // bmAttributes
USBShort(512), // wMaxPacketSize
USBShort(USB_ENDPOINT_SIZE), // wMaxPacketSize
0x00, // bInterval
/* SuperSpeed Endpoint Companion */
#if (USE_USB_3_0 == 1)
0x06, // bLength
USB_DT_SUPERSPEED_USB_ENDPOINT_COMPANION, // bDescriptorType
0x00, // bMaxBurst
0x00, // bmAttributes(MaxStream for Bulk)
0x00, 0x00, // wBytesPerInterval -> 0 for Bulk
#endif // USE_USB_3_0 == 1
};

View File

@ -51,7 +51,13 @@ extern const uint8_t kProductString[0x18];
extern const uint8_t kSerialNumberString[0x1A];
#if (USE_WINUSB == 1)
#if (USE_USB_3_0 == 1)
extern const uint8_t kUSBd0InterfaceDescriptor[0x30];
#else
extern const uint8_t kUSBd0InterfaceDescriptor[0x1E];
#endif // USE_USB_3_0 == 1
extern const uint8_t kUSBd0ConfigDescriptor[0x09];
extern const uint8_t kInterfaceString[0x2C];

View File

@ -119,6 +119,7 @@ typedef struct
#define USB_DT_OTHER_SPEED_CONFIGURATION 7
#define USB_DT_INTERFACE_POWER 8
#define USB_DT_BOS 15
#define USB_DT_SUPERSPEED_USB_ENDPOINT_COMPANION 48
/* From ECNs */
#define USB_DT_OTG 9
#define USB_DT_DEBUG 10

View File

@ -31,44 +31,40 @@
#include "lwip/sys.h"
#include <lwip/netdb.h>
#if (USE_WINUSB == 1)
typedef struct
{
uint32_t length;
uint8_t buf[DAP_PACKET_SIZE];
} DapPacket_t;
#else
typedef struct
{
uint8_t buf[DAP_PACKET_SIZE];
} DapPacket_t;
#endif
#define DAP_HANDLE_SIZE (sizeof(DapPacket_t))
extern int kSock;
extern TaskHandle_t kDAPTaskHandle;
int kRestartDAPHandle = 0;
#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 DapPacket_t DAPDataProcessed;
static int dap_respond = 0;
// SWO Trace
static uint8_t *swo_data_to_send = NULL;
static uint32_t swo_data_num;
// DAP handle
static RingbufHandle_t dap_dataIN_handle = NULL;
static RingbufHandle_t dap_dataOUT_handle = NULL;
static SemaphoreHandle_t data_response_mux = NULL;
static void unpack(void *data, int size);
void handle_dap_data_request(usbip_stage2_header *header, uint32_t length)
{
@ -149,7 +145,7 @@ void DAP_Thread(void *argument)
data_response_mux = xSemaphoreCreateMutex();
size_t packetSize;
int resLength;
DAPPacetDataType *item;
DapPacket_t *item;
if (dap_dataIN_handle == NULL || dap_dataIN_handle == NULL ||
data_response_mux == NULL)
@ -180,8 +176,8 @@ void DAP_Thread(void *argument)
ulTaskNotifyTake(pdFALSE, portMAX_DELAY);
packetSize = 0;
item = (DAPPacetDataType *)xRingbufferReceiveUpTo(dap_dataIN_handle, &packetSize,
pdMS_TO_TICKS(1), DAP_HANDLE_SIZE);
item = (DapPacket_t *)xRingbufferReceiveUpTo(dap_dataIN_handle, &packetSize,
pdMS_TO_TICKS(1), DAP_HANDLE_SIZE);
if (packetSize == 0)
{
break;
@ -201,14 +197,14 @@ void DAP_Thread(void *argument)
}
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
resLength &= 0xFFFF; // res length in lower 16 bits
vRingbufferReturnItem(dap_dataIN_handle, (void *)item); // process done.
// now prepare to reply
#if (USE_WINUSB == 1)
#if (USE_WINUSB == 1)
DAPDataProcessed.length = resLength;
#endif
#endif
xRingbufferSend(dap_dataOUT_handle, (void *)&DAPDataProcessed, DAP_HANDLE_SIZE, portMAX_DELAY);
if (xSemaphoreTake(data_response_mux, portMAX_DELAY) == pdTRUE)
@ -222,25 +218,25 @@ void DAP_Thread(void *argument)
int fast_reply(uint8_t *buf, uint32_t length)
{
if (length == 48 && buf[3] == 1 && buf[15] == 1 && buf[19] == 1)
usbip_stage2_header *buf_header = (usbip_stage2_header *)buf;
if (length == 48 &&
buf_header->base.command == PP_HTONL(USBIP_STAGE2_REQ_SUBMIT) &&
buf_header->base.direction == PP_HTONL(USBIP_DIR_IN) &&
buf_header->base.ep == PP_HTONL(1))
{
if (dap_respond > 0)
{
DAPPacetDataType *item;
DapPacket_t *item;
size_t packetSize = 0;
item = (DAPPacetDataType *)xRingbufferReceiveUpTo(dap_dataOUT_handle, &packetSize,
pdMS_TO_TICKS(10), DAP_HANDLE_SIZE);
item = (DapPacket_t *)xRingbufferReceiveUpTo(dap_dataOUT_handle, &packetSize,
pdMS_TO_TICKS(10), DAP_HANDLE_SIZE);
if (packetSize == DAP_HANDLE_SIZE)
{
unpack((uint32_t *)buf, sizeof(usbip_stage2_header));
#if (USE_WINUSB == 1)
uint32_t resLength = item->length;
send_stage2_submit_data_fast((usbip_stage2_header *)buf, 0, item->buf, resLength);
#else
send_stage2_submit_data_fast((usbip_stage2_header *)buf, 0, item->buf, DAP_HANDLE_SIZE);
#endif
#if (USE_WINUSB == 1)
send_stage2_submit_data_fast((usbip_stage2_header *)buf, item->buf, item->length);
#else
send_stage2_submit_data_fast((usbip_stage2_header *)buf, item->buf, DAP_HANDLE_SIZE);
#endif
if (xSemaphoreTake(data_response_mux, portMAX_DELAY) == pdTRUE)
{
@ -259,28 +255,14 @@ int fast_reply(uint8_t *buf, uint32_t length)
}
else
{
//// TODO: ep0 dir 0 ?
buf[0x3] = 0x3; // command
buf[0xF] = 0; // direction
buf[0x16] = 0;
buf[0x17] = 0;
buf[27] = 0;
buf[39] = 0;
buf_header->base.command = PP_HTONL(USBIP_STAGE2_RSP_SUBMIT);
buf_header->base.direction = PP_HTONL(USBIP_DIR_OUT);
buf_header->u.ret_submit.status = 0;
buf_header->u.ret_submit.data_length = 0;
buf_header->u.ret_submit.error_count = 0;
send(kSock, buf, 48, 0);
return 1;
}
}
return 0;
}
static void unpack(void *data, int size)
{
// Ignore the setup field
int sz = (size / sizeof(uint32_t)) - 2;
uint32_t *ptr = (uint32_t *)data;
for (int i = 0; i < sz; i++)
{
ptr[i] = ntohl(ptr[i]);
}
}

View File

@ -13,6 +13,21 @@
*/
#define USE_SPI_SIO 1
/**
* @brief Specify to enable USB 3.0
*
*/
#define USE_USB_3_0 0
// For USB 3.0, it must be 1024 byte.
#if (USE_USB_3_0 == 1)
#define USB_ENDPOINT_SIZE 1024U
#else
#define USB_ENDPOINT_SIZE 512U
#endif
/// Maximum Package Size for Command and Response data.
/// This configuration settings is used to optimize the communication performance with the
/// debugger and depends on the USB peripheral. Typical vales are 64 for Full-speed USB HID or WinUSB,

View File

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

View File

@ -339,7 +339,7 @@ void send_stage2_submit(usbip_stage2_header *req_header, int32_t status, int32_t
req_header->u.ret_submit.status = status;
req_header->u.ret_submit.data_length = data_length;
// already unpacked
pack(req_header, sizeof(usbip_stage2_header));
send(kSock, req_header, sizeof(usbip_stage2_header), 0);
}
@ -355,19 +355,16 @@ void send_stage2_submit_data(usbip_stage2_header *req_header, int32_t status, co
}
}
void send_stage2_submit_data_fast(usbip_stage2_header *req_header, int32_t status, const void *const data, int32_t data_length)
void send_stage2_submit_data_fast(usbip_stage2_header *req_header, const void *const data, int32_t data_length)
{
uint8_t * send_buf = (uint8_t *)req_header;
req_header->base.command = USBIP_STAGE2_RSP_SUBMIT;
req_header->base.direction = !(req_header->base.direction);
req_header->base.command = PP_HTONL(USBIP_STAGE2_RSP_SUBMIT);
req_header->base.direction = htonl(!(req_header->base.direction));
memset(&(req_header->u.ret_submit), 0, sizeof(usbip_stage2_header_ret_submit));
req_header->u.ret_submit.data_length = htonl(data_length);
req_header->u.ret_submit.status = status;
req_header->u.ret_submit.data_length = data_length;
pack(req_header, sizeof(usbip_stage2_header));
// payload
memcpy(&send_buf[sizeof(usbip_stage2_header)], data, data_length);

View File

@ -15,7 +15,7 @@ int attach(uint8_t *buffer, uint32_t length);
int emulate(uint8_t *buffer, uint32_t length);
void send_stage2_submit_data(usbip_stage2_header *req_header, int32_t status, const void * const data, int32_t data_length);
void send_stage2_submit(usbip_stage2_header *req_header, int32_t status, int32_t data_length);
void send_stage2_submit_data_fast(usbip_stage2_header *req_header, int32_t status, const void *const data, int32_t data_length);
void send_stage2_submit_data_fast(usbip_stage2_header *req_header, const void *const data, int32_t data_length);
#endif