0
0
Fork 0

chore: Refactor build dependencies

This commit is contained in:
windowsair 2021-06-05 20:16:56 +08:00
parent b2c8a832cc
commit 9876edd6ef
34 changed files with 2168 additions and 2154 deletions

View File

@ -1,9 +1,6 @@
# The following five lines of boilerplate have to be in your project's
# CMakeLists in this exact order for cmake to work correctly
cmake_minimum_required(VERSION 3.5) cmake_minimum_required(VERSION 3.5)
#set(COMPONENT_DIRS "${IDF_PATH}/components ${PROJECT_PATH}/components")
include_directories(${CMAKE_CURRENT_SOURCE_DIR}/main) include_directories(${CMAKE_CURRENT_SOURCE_DIR})
include($ENV{IDF_PATH}/tools/cmake/project.cmake) include($ENV{IDF_PATH}/tools/cmake/project.cmake)
project(esp8266_dap) project(esp8266_dap)

View File

@ -1,4 +1,4 @@
set(COMPONENT_ADD_INCLUDEDIRS "config include $ENV{IDF_PATH}/components/esp8266/include/esp8266/ $ENV{IDF_PATH}//components/esp_ringbuf/include/") set(COMPONENT_ADD_INCLUDEDIRS "config include $ENV{IDF_PATH}/components/esp8266/include/esp8266/ $ENV{IDF_PATH}/components/esp_ringbuf/include/")
set(COMPONENT_SRCS "./source/DAP.c ./source/DAP_vendor.c ./source/JTAG_DP.c ./source/SW_DP.c ./source/SWO.c ./source/uart_modify.c ./source/spi_op.c ./source/spi_switch.c ./source/dap_utility.c") set(COMPONENT_SRCS "./source/DAP.c ./source/DAP_vendor.c ./source/JTAG_DP.c ./source/SW_DP.c ./source/SWO.c ./source/uart_modify.c ./source/spi_op.c ./source/spi_switch.c ./source/dap_utility.c")

File diff suppressed because it is too large Load Diff

View File

@ -2,13 +2,13 @@
#define __CMSIS_COMPILER_H__ #define __CMSIS_COMPILER_H__
#ifndef __STATIC_FORCEINLINE #ifndef __STATIC_FORCEINLINE
#define __STATIC_FORCEINLINE static inline __attribute__((always_inline)) #define __STATIC_FORCEINLINE static inline __attribute__((always_inline))
#endif #endif
#ifndef __STATIC_INLINE #ifndef __STATIC_INLINE
#define __STATIC_INLINE static inline __attribute__((always_inline)) #define __STATIC_INLINE static inline __attribute__((always_inline))
#endif #endif
#ifndef __FORCEINLINE #ifndef __FORCEINLINE
#define __FORCEINLINE inline __attribute__((always_inline)) #define __FORCEINLINE inline __attribute__((always_inline))
#endif #endif
#ifndef __WEAK #ifndef __WEAK
#define __WEAK __attribute__((weak)) #define __WEAK __attribute__((weak))

View File

@ -4,7 +4,7 @@
#include <stdint.h> #include <stdint.h>
#ifndef __STATIC_FORCEINLINE #ifndef __STATIC_FORCEINLINE
#define __STATIC_FORCEINLINE static inline __attribute__((always_inline)) #define __STATIC_FORCEINLINE static inline __attribute__((always_inline))
#endif #endif
#ifndef __STATIC_INLINE #ifndef __STATIC_INLINE
#define __STATIC_INLINE static inline __attribute__((always_inline)) #define __STATIC_INLINE static inline __attribute__((always_inline))

View File

@ -1,7 +1,8 @@
#ifndef __GPIO_OP_H__ #ifndef __GPIO_OP_H__
#define __GPIO_OP_H__ #define __GPIO_OP_H__
#include "cmsis_compiler.h" #include "components/DAP/include/cmsis_compiler.h"
#include "gpio.h" #include "gpio.h"
#include "gpio_struct.h" #include "gpio_struct.h"
#include "timer_struct.h" #include "timer_struct.h"

View File

@ -27,6 +27,7 @@ extern "C" {
#include "esp_err.h" #include "esp_err.h"
#include "esp_log.h" #include "esp_log.h"
#include "freertos/FreeRTOS.h"
#include "freertos/queue.h" #include "freertos/queue.h"
#include "freertos/semphr.h" #include "freertos/semphr.h"

View File

@ -26,10 +26,10 @@
*---------------------------------------------------------------------------*/ *---------------------------------------------------------------------------*/
#include <string.h> #include <string.h>
#include "DAP_config.h"
#include "DAP.h"
#include "spi_switch.h" #include "components/DAP/config/DAP_config.h"
#include "components/DAP/include/DAP.h"
#include "components/DAP/include/spi_switch.h"
#if (DAP_PACKET_SIZE < 64U) #if (DAP_PACKET_SIZE < 64U)
@ -388,6 +388,8 @@ static uint32_t DAP_SWJ_Clock(const uint8_t *request, uint8_t *response) {
if (DAP_Data.debug_port != DAP_PORT_JTAG) { if (DAP_Data.debug_port != DAP_PORT_JTAG) {
DAP_SPI_Init(); DAP_SPI_Init();
SWD_TransferSpeed = kTransfer_SPI; SWD_TransferSpeed = kTransfer_SPI;
} else {
SWD_TransferSpeed = kTransfer_GPIO_fast;
} }
DAP_Data.fast_clock = 1U; DAP_Data.fast_clock = 1U;
DAP_Data.clock_delay = 1U; DAP_Data.clock_delay = 1U;

View File

@ -24,18 +24,18 @@
* Title: DAP_vendor.c CMSIS-DAP Vendor Commands * Title: DAP_vendor.c CMSIS-DAP Vendor Commands
* *
*---------------------------------------------------------------------------*/ *---------------------------------------------------------------------------*/
#include "DAP_config.h" #include "components/DAP/config/DAP_config.h"
#include "DAP.h" #include "components/DAP/include/DAP.h"
//************************************************************************************************** //**************************************************************************************************
/** /**
\defgroup DAP_Vendor_Adapt_gr Adapt Vendor Commands \defgroup DAP_Vendor_Adapt_gr Adapt Vendor Commands
\ingroup DAP_Vendor_gr \ingroup DAP_Vendor_gr
@{ @{
The file DAP_vendor.c provides template source code for extension of a Debug Unit with The file DAP_vendor.c provides template source code for extension of a Debug Unit with
Vendor Commands. Copy this file to the project folder of the Debug Unit and add the Vendor Commands. Copy this file to the project folder of the Debug Unit and add the
file to the MDK-ARM project under the file group Configuration. file to the MDK-ARM project under the file group Configuration.
*/ */

View File

@ -25,8 +25,8 @@
* *
*---------------------------------------------------------------------------*/ *---------------------------------------------------------------------------*/
#include "DAP_config.h" #include "components/DAP/config/DAP_config.h"
#include "DAP.h" #include "components/DAP/include/DAP.h"
// JTAG Macros // JTAG Macros

View File

@ -38,16 +38,16 @@
* *
*---------------------------------------------------------------------------*/ *---------------------------------------------------------------------------*/
#include "DAP_config.h" #include "components/DAP/config/DAP_config.h"
#include "DAP.h" #include "components/DAP/include/DAP.h"
#include "components/DAP/include/uart_modify.h"
#include "components/DAP/include/swo.h"
#include "esp_err.h" #include "esp_err.h"
#include "freertos/FreeRTOS.h" #include "freertos/FreeRTOS.h"
#include "freertos/task.h" #include "freertos/task.h"
#include "freertos/event_groups.h" #include "freertos/event_groups.h"
#include "uart_modify.h"
#include "swo.h"
EventGroupHandle_t kSwoThreadEventGroup; EventGroupHandle_t kSwoThreadEventGroup;

File diff suppressed because it is too large Load Diff

View File

@ -1,6 +1,6 @@
#include "dap_utility.h" #include "components/DAP/include/dap_utility.h"
const uint8_t kParityByteTable[256] = const uint8_t kParityByteTable[256] =
{ {
#define P2(n) n, n^1, n^1, n #define P2(n) n, n^1, n^1, n
#define P4(n) P2(n), P2(n^1), P2(n^1), P2(n) #define P4(n) P2(n), P2(n^1), P2(n^1), P2(n)

View File

@ -14,10 +14,13 @@
#include <stdio.h> #include <stdio.h>
#include <stdbool.h> #include <stdbool.h>
#include "main/dap_configuration.h"
#include "components/DAP/include/cmsis_compiler.h"
#include "components/DAP/include/spi_op.h"
#include "esp8266/spi_struct.h" #include "esp8266/spi_struct.h"
#include "cmsis_compiler.h"
#include "spi_op.h"
#include "dap_configuration.h"
#define DAP_SPI SPI1 #define DAP_SPI SPI1

View File

@ -12,14 +12,13 @@
*/ */
#include <stdbool.h> #include <stdbool.h>
#include "components/DAP/include/cmsis_compiler.h"
#include "components/DAP/include/spi_switch.h"
#include "esp8266/spi_struct.h" #include "esp8266/spi_struct.h"
#include "esp8266/pin_mux_register.h" #include "esp8266/pin_mux_register.h"
#include "esp8266/gpio_struct.h" #include "esp8266/gpio_struct.h"
#include "cmsis_compiler.h"
#include "spi_switch.h"
#include "dap_configuration.h"
#define DAP_SPI SPI1 #define DAP_SPI SPI1

View File

@ -19,6 +19,11 @@
#include <stdlib.h> #include <stdlib.h>
#include <string.h> #include <string.h>
#include "components/DAP/config/DAP_config.h"
#include "components/DAP/include/uart_modify.h"
#include "components/DAP/include/swo.h"
#include "freertos/FreeRTOS.h" #include "freertos/FreeRTOS.h"
#include "freertos/task.h" #include "freertos/task.h"
#include "freertos/queue.h" #include "freertos/queue.h"
@ -26,12 +31,9 @@
#include "freertos/ringbuf.h" #include "freertos/ringbuf.h"
#include "freertos/event_groups.h" #include "freertos/event_groups.h"
#include "esp_err.h"
#include "esp_log.h"
#include "esp_attr.h" #include "esp_attr.h"
// SWO modify // SWO modify
#include "DAP_config.h"
#include "esp8266/uart_struct.h" #include "esp8266/uart_struct.h"
#include "esp8266/uart_register.h" #include "esp8266/uart_register.h"
#include "esp8266/pin_mux_register.h" #include "esp8266/pin_mux_register.h"
@ -39,9 +41,6 @@
#include "esp8266/rom_functions.h" #include "esp8266/rom_functions.h"
#include "rom/ets_sys.h" #include "rom/ets_sys.h"
#include "uart_modify.h"
#include "swo.h"
#include "driver/uart_select.h" #include "driver/uart_select.h"
#define portYIELD_FROM_ISR() taskYIELD() #define portYIELD_FROM_ISR() taskYIELD()

View File

@ -1,4 +1,4 @@
set(COMPONENT_ADD_INCLUDEDIRS ". ../../main") set(COMPONENT_ADD_INCLUDEDIRS ". ../../main")
set(COMPONENT_SRCS "MSOS20Descriptors.c USB_handle.c USBd_config.c") set(COMPONENT_SRCS "MSOS20_descriptor.c usb_handle.c usb_descriptor.c")
register_component() register_component()

View File

@ -1,111 +1,112 @@
/** /**
* @file MSOS20Descriptors.c * @file MSOS20_descriptor.c
* @author windowsair * @author windowsair
* @brief Store related data of Microsoft OS 2.0 descriptor * @brief Store related data of Microsoft OS 2.0 descriptor
* @change: 2021-5-12 Add support for USB 3.0 * @change: 2021-5-12 Add support for USB 3.0
* @version 0.2 * @version 0.2
* @date 2021-5-12 * @date 2021-5-12
* *
* @copyright Copyright (c) 2021 * @copyright Copyright (c) 2021
* *
*/ */
////TODO: refactoring into structure ////TODO: refactoring into structure
#include <stdint.h> #include <stdint.h>
#include "MSOS20Descriptors.h"
#include "components/USBIP/MSOS20_descriptor.h"
#define USBShort(ui16Value) ((ui16Value) & 0xff), ((ui16Value) >> 8) //((ui16Value) & 0xFF),(((ui16Value) >> 8) & 0xFF)
#define USBShort(ui16Value) ((ui16Value) & 0xff), ((ui16Value) >> 8) //((ui16Value) & 0xFF),(((ui16Value) >> 8) & 0xFF)
// Microsoft OS 2.0 descriptor set header
const uint8_t msOs20DescriptorSetHeader[kLengthOfMsOS20] = // Microsoft OS 2.0 descriptor set header
{ const uint8_t msOs20DescriptorSetHeader[kLengthOfMsOS20] =
// Microsoft OS 2.0 Descriptor Set header (Table 10) {
0x0A, 0x00, // wLength (Shall be set to 0x0A) // Microsoft OS 2.0 Descriptor Set header (Table 10)
MS_OS_20_SET_HEADER_DESCRIPTOR, 0x00, 0x0A, 0x00, // wLength (Shall be set to 0x0A)
0x00, 0x00, 0x03, 0x06, // dwWindowsVersion: Windows 8.1 (NTDDI_WINBLUE) MS_OS_20_SET_HEADER_DESCRIPTOR, 0x00,
USBShort(kLengthOfMsOS20), // wTotalLength 0x00, 0x00, 0x03, 0x06, // dwWindowsVersion: Windows 8.1 (NTDDI_WINBLUE)
USBShort(kLengthOfMsOS20), // wTotalLength
// Support WinUSB
// See https://docs.microsoft.com/en-us/windows-hardware/drivers/usbcon/automatic-installation-of-winusb // Support WinUSB
// See https://docs.microsoft.com/en-us/windows-hardware/drivers/usbcon/automatic-installation-of-winusb
// Microsoft OS 2.0 compatible ID descriptor (Table 13)
0x14, 0x00, // wLength // Microsoft OS 2.0 compatible ID descriptor (Table 13)
USBShort(MS_OS_20_FEATURE_COMPATIBLE_ID), // wDescriptorType 0x14, 0x00, // wLength
'W', 'I', 'N', 'U', 'S', 'B', 0x00, 0x00, // compatibleID USBShort(MS_OS_20_FEATURE_COMPATIBLE_ID), // wDescriptorType
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // subCompatibleID 'W', 'I', 'N', 'U', 'S', 'B', 0x00, 0x00, // compatibleID
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // subCompatibleID
// Microsoft OS 2.0 registry property descriptor (Table 14)
0x84, 0x00, // wLength // Microsoft OS 2.0 registry property descriptor (Table 14)
USBShort(MS_OS_20_FEATURE_REG_PROPERTY), 0x84, 0x00, // wLength
0x07, 0x00, // wPropertyDataType: REG_MULTI_SZ (Unicode Strings) USBShort(MS_OS_20_FEATURE_REG_PROPERTY),
0x2A, 0x00, // wPropertyNameLength 0x07, 0x00, // wPropertyDataType: REG_MULTI_SZ (Unicode Strings)
'D',0,'e',0,'v',0,'i',0,'c',0,'e',0,'I',0,'n',0,'t',0,'e',0,'r',0, 0x2A, 0x00, // wPropertyNameLength
'f',0,'a',0,'c',0,'e',0,'G',0,'U',0,'I',0,'D',0,'s',0,0,0, 'D',0,'e',0,'v',0,'i',0,'c',0,'e',0,'I',0,'n',0,'t',0,'e',0,'r',0,
// Set to "DeviceInterfaceGUID" to support WinUSB 'f',0,'a',0,'c',0,'e',0,'G',0,'U',0,'I',0,'D',0,'s',0,0,0,
0x50, 0x00, // wPropertyDataLength // Set to "DeviceInterfaceGUID" to support WinUSB
// WinUSB GUID 0x50, 0x00, // wPropertyDataLength
'{',0,'C',0,'D',0,'B',0,'3',0,'B',0,'5',0,'A',0,'D',0,'-',0, // WinUSB GUID
'2',0,'9',0,'3',0,'B',0,'-',0,'4',0,'6',0,'6',0,'3',0,'-',0, '{',0,'C',0,'D',0,'B',0,'3',0,'B',0,'5',0,'A',0,'D',0,'-',0,
'A',0,'A',0,'3',0,'6',0,'-',0,'1',0,'A',0,'A',0,'E',0,'4',0, '2',0,'9',0,'3',0,'B',0,'-',0,'4',0,'6',0,'6',0,'3',0,'-',0,
'6',0,'4',0,'6',0,'3',0,'7',0,'7',0,'6',0,'}',0,0,0,0,0, 'A',0,'A',0,'3',0,'6',0,'-',0,'1',0,'A',0,'A',0,'E',0,'4',0,
// identify a CMSIS-DAP V2 configuration, '6',0,'4',0,'6',0,'3',0,'7',0,'7',0,'6',0,'}',0,0,0,0,0,
// must set to "{CDB3B5AD-293B-4663-AA36-1AAE46463776}" // identify a CMSIS-DAP V2 configuration,
// must set to "{CDB3B5AD-293B-4663-AA36-1AAE46463776}"
};
};
const uint8_t bosDescriptor[kLengthOfBos] =
{ const uint8_t bosDescriptor[kLengthOfBos] =
// USB 3.0 Specification, Table 9-9. {
0x05, // bLength of this descriptor // USB 3.0 Specification, Table 9-9.
USB_DESCRIPTOR_TYPE_BOS, // BOS Descriptor type(Constant) 0x05, // bLength of this descriptor
USBShort(kLengthOfBos), // wLength 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 #if (USE_USB_3_0 == 1)
#else 0x03, // bNumDeviceCaps -> USB2.0 extension & SuperSpeed USB Device & OS2.0 descriptor
0x01, // bNumDeviceCaps -> only 0x01 for OS2.0 descriptor #else
#endif // USE_USB_3_0 == 1 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. #if (USE_USB_3_0 == 1)
0x07, // bLength of this descriptor // USB 2.0 extension, USB 3.0 Specification, Table 9-12.
USB_DESCRIPTOR_TYPE_DEVICE_CAPABILITY, // DEVICE CAPABILITY Descriptor type 0x07, // bLength of this descriptor
USB_DEVICE_CAPABILITY_TYPE_USB2_0_EXTENSION, // Capability type: USB 2.0 EXTENSION USB_DESCRIPTOR_TYPE_DEVICE_CAPABILITY, // DEVICE CAPABILITY Descriptor type
0x02, 0x00, 0x00, 0x00, // bmAttributes -> LPM Support 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 // SuperSpeed USB Device, USB 3.0 Specification, Table 9-13.
USB_DESCRIPTOR_TYPE_DEVICE_CAPABILITY, // DEVICE CAPABILITY Descriptor type 0x0A, // bLength of this descriptor
USB_DEVICE_CAPABILITY_TYPE_SUPERSPEED_USB, // Capability type: SUPERSPEED_USB USB_DESCRIPTOR_TYPE_DEVICE_CAPABILITY, // DEVICE CAPABILITY Descriptor type
0x00, // bmAttributes -> LTM Capable USB_DEVICE_CAPABILITY_TYPE_SUPERSPEED_USB, // Capability type: SUPERSPEED_USB
0x08, 0x00, // wSpeedsSupported -> only support SuperSpeed 0x00, // bmAttributes -> LTM Capable
0x03, // bFunctionalitySupport 0x08, 0x00, // wSpeedsSupported -> only support SuperSpeed
0x00, // bU1DevExitLat -> 0 may be ok 0x03, // bFunctionalitySupport
0x00, 0x00, // wU2DevExitLat -> 0 may be ok 0x00, // bU1DevExitLat -> 0 may be ok
#endif // USE_USB_3_0 == 1 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: // Microsoft OS 2.0 platform capability descriptor header (Table 4)
// USB 3.0 Specification : Format of a Device Capability Descriptor, Table 9-10. // See also:
// 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 0x1C, // bLength of this first device capability descriptor
USB_DESCRIPTOR_TYPE_DEVICE_CAPABILITY, // bDescriptorType // bLength -> The total length of the remaining arrays containing this field
USB_DEVICE_CAPABILITY_TYPE_PLATFORM, // bDevCapabilityType USB_DESCRIPTOR_TYPE_DEVICE_CAPABILITY, // bDescriptorType
USB_DEVICE_CAPABILITY_TYPE_PLATFORM, // bDevCapabilityType
// Capability-Dependent (See USB 3.0 Specification Table 9-10.)
0x00, // bReserved // Capability-Dependent (See USB 3.0 Specification Table 9-10.)
USB_DEVICE_CAPABILITY_UUID, // MS_OS_20_Platform_Capability_ID 0x00, // bReserved
USB_DEVICE_CAPABILITY_UUID, // MS_OS_20_Platform_Capability_ID
0x00, 0x00, 0x03, 0x06, // dwWindowsVersion: Windows 8.1 (NTDDI_WINBLUE)
USBShort(kLengthOfMsOS20), // wMSOSDescriptorSetTotalLength(length of descriptor set header) 0x00, 0x00, 0x03, 0x06, // dwWindowsVersion: Windows 8.1 (NTDDI_WINBLUE)
kValueOfbMS_VendorCode, // bMS_VendorCode (0x01 will be ok) USBShort(kLengthOfMsOS20), // wMSOSDescriptorSetTotalLength(length of descriptor set header)
////TODO:change this kValueOfbMS_VendorCode, // bMS_VendorCode (0x01 will be ok)
0, // bAltEnumCode ////TODO:change this
0, // bAltEnumCode
}; };

View File

@ -1,94 +1,93 @@
/** /**
* @file MSOS20Descriptors.h * @file MSOS20_descriptor.h
* @author windowsair * @author windowsair
* @brief * @brief
* @version 0.2 * @version 0.2
* @date 2021-5-12 * @date 2021-5-12
* *
* @copyright Copyright (c) 2021 * @copyright Copyright (c) 2021
* *
*/ */
#ifndef __MSOS20DESCRIPTORS_H__ #ifndef __MSOS20_DESCRIPTOR_H__
#define __MSOS20DESCRIPTORS_H__ #define __MSOS20_DESCRIPTOR_H__
#include "dap_configuration.h"
#define kLengthOfMsOS20 0xA2
#define kLengthOfMsOS20 0xA2
#if (USE_USB_3_0 == 1)
#if (USE_USB_3_0 == 1) #define kLengthOfBos 0x32
#define kLengthOfBos 0x32 #else
#else #define kLengthOfBos 0x21
#define kLengthOfBos 0x21 #endif // USE_USB_3_0 == 1
#endif // USE_USB_3_0 == 1
#define kValueOfbMS_VendorCode 0x01// Just set to 0x01
#define kValueOfbMS_VendorCode 0x01// Just set to 0x01 extern const uint8_t bosDescriptor[kLengthOfBos];
extern const uint8_t bosDescriptor[kLengthOfBos]; extern const uint8_t msOs20DescriptorSetHeader[kLengthOfMsOS20];
extern const uint8_t msOs20DescriptorSetHeader[kLengthOfMsOS20];
/* Microsoft OS 2.0 Descriptors BEGIN */
/* Microsoft OS 2.0 Descriptors BEGIN */
// Platform capability BOS descriptor, Table 1.
// Platform capability BOS descriptor, Table 1. #define USB_DEVICE_CAPABILITY_TYPE_PLATFORM 5
#define USB_DEVICE_CAPABILITY_TYPE_PLATFORM 5
// USB 2.0 Extension Descriptor, USB3.0 Specification Table 9-11
// USB 2.0 Extension Descriptor, USB3.0 Specification Table 9-11 #define USB_DEVICE_CAPABILITY_TYPE_USB2_0_EXTENSION 2
#define USB_DEVICE_CAPABILITY_TYPE_USB2_0_EXTENSION 2 // SuperSpeed USB specific device level capabilities, USB3.0 Specification Table 9-11
// SuperSpeed USB specific device level capabilities, USB3.0 Specification Table 9-11 #define USB_DEVICE_CAPABILITY_TYPE_SUPERSPEED_USB 3
#define USB_DEVICE_CAPABILITY_TYPE_SUPERSPEED_USB 3
// Platform capability UUID, Table 3.
// Platform capability UUID, Table 3. // {D8DD60DF-4589-4CC7-9CD2-659D9E648A9F}
// {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
#define USB_DEVICE_CAPABILITY_UUID 0xDF, 0x60, 0xDD, 0xD8, 0x89, 0x45, 0xC7, 0x4C, 0x9C, 0xD2, 0x65, 0x9D, 0x9E, 0x64, 0x8A, 0x9F
// Microsoft OS 2.0 descriptor wIndex values enum, Table 8.
// Microsoft OS 2.0 descriptor wIndex values enum, Table 8. #define MS_OS_20_DESCRIPTOR_INDEX 7
#define MS_OS_20_DESCRIPTOR_INDEX 7 #define MS_OS_20_SET_ALT_ENUMERATION 8
#define MS_OS_20_SET_ALT_ENUMERATION 8
// Microsoft OS 2.0 descriptor types enum for wDescriptorType values, Table 9.
// Microsoft OS 2.0 descriptor types enum for wDescriptorType values, Table 9. #define MS_OS_20_SET_HEADER_DESCRIPTOR 0x00
#define MS_OS_20_SET_HEADER_DESCRIPTOR 0x00 #define MS_OS_20_SUBSET_HEADER_CONFIGURATION 0x01
#define MS_OS_20_SUBSET_HEADER_CONFIGURATION 0x01 #define MS_OS_20_SUBSET_HEADER_FUNCTION 0x02
#define MS_OS_20_SUBSET_HEADER_FUNCTION 0x02 #define MS_OS_20_FEATURE_COMPATIBLE_ID 0x03
#define MS_OS_20_FEATURE_COMPATIBLE_ID 0x03 #define MS_OS_20_FEATURE_REG_PROPERTY 0x04
#define MS_OS_20_FEATURE_REG_PROPERTY 0x04 #define MS_OS_20_FEATURE_MIN_RESUME_TIME 0x05
#define MS_OS_20_FEATURE_MIN_RESUME_TIME 0x05 #define MS_OS_20_FEATURE_MODEL_ID 0x06
#define MS_OS_20_FEATURE_MODEL_ID 0x06 #define MS_OS_20_FEATURE_CCGP_DEVICE 0x07
#define MS_OS_20_FEATURE_CCGP_DEVICE 0x07
/* Microsoft OS 2.0 Descriptors END */
/* Microsoft OS 2.0 Descriptors END */
/* Wireless USB Standard Extension Descriptor Types BEGIN */
/* Wireless USB Standard Extension Descriptor Types BEGIN */
// Wireless USB Specification 1.1 revison 1.1, Table 7-21.
// Wireless USB Specification 1.1 revison 1.1, Table 7-21. #define USB_DESCRIPTOR_TYPE_SECURITY 12
#define USB_DESCRIPTOR_TYPE_SECURITY 12 #define USB_DESCRIPTOR_TYPE_KEY 13
#define USB_DESCRIPTOR_TYPE_KEY 13 #define USB_DESCRIPTOR_TYPE_ENCRYPTION_TYPE 14
#define USB_DESCRIPTOR_TYPE_ENCRYPTION_TYPE 14 #define USB_DESCRIPTOR_TYPE_BOS 15
#define USB_DESCRIPTOR_TYPE_BOS 15 #define USB_DESCRIPTOR_TYPE_DEVICE_CAPABILITY 16
#define USB_DESCRIPTOR_TYPE_DEVICE_CAPABILITY 16 #define USB_DESCRIPTOR_TYPE_WIRELESS_ENDPOINT_COMPANION 17
#define USB_DESCRIPTOR_TYPE_WIRELESS_ENDPOINT_COMPANION 17
/* Wireless USB Standard Extension Descriptor Types END */
/* Wireless USB Standard Extension Descriptor Types END */
/* Microsoft Extended Compat ID OS Feature Descriptor BEGIN */
/* Microsoft Extended Compat ID OS Feature Descriptor BEGIN */
#define USB_MS_EXTENDED_COMPAT_ID_VERSION 0x0100
#define USB_MS_EXTENDED_COMPAT_ID_VERSION 0x0100 #define USB_MS_EXTENDED_COMPAT_ID_TYPE 0x04
#define USB_MS_EXTENDED_COMPAT_ID_TYPE 0x04
#define USB_COMPATID_NONE {0}
#define USB_COMPATID_NONE {0} #define USB_SUBCOMPATID_NONE {0}
#define USB_SUBCOMPATID_NONE {0} #define USB_COMPATID_WINUSB "WINUSB\0"
#define USB_COMPATID_WINUSB "WINUSB\0" #define USB_COMPATID_RNDIS "RNDIS\0\0"
#define USB_COMPATID_RNDIS "RNDIS\0\0" #define USB_COMPATID_PTP "PTP\0\0\0\0"
#define USB_COMPATID_PTP "PTP\0\0\0\0" #define USB_COMPATID_MTP "MTP\0\0\0\0"
#define USB_COMPATID_MTP "MTP\0\0\0\0" #define USB_COMPATID_BLUETOOTH "BLUTUTH"
#define USB_COMPATID_BLUETOOTH "BLUTUTH" #define USB_SUBCOMPATID_BT_V11 "11\0\0\0\0\0"
#define USB_SUBCOMPATID_BT_V11 "11\0\0\0\0\0" #define USB_SUBCOMPATID_BT_V12 "12\0\0\0\0\0"
#define USB_SUBCOMPATID_BT_V12 "12\0\0\0\0\0" #define USB_SUBCOMPATID_BT_V20EDR "EDR\0\0\0\0"
#define USB_SUBCOMPATID_BT_V20EDR "EDR\0\0\0\0"
/* Microsoft Extended Compat ID OS Feature Descriptor END */
/* Microsoft Extended Compat ID OS Feature Descriptor END */
#endif #endif

View File

@ -1,6 +1,6 @@
////TODO: refactoring into structure ////TODO: refactoring into structure
/** /**
* @file USBd_config.c * @file usb_descriptor.c
* @brief Standard USB Descriptor Definitions * @brief Standard USB Descriptor Definitions
* @change: 2020-1-23 : fix bugs * @change: 2020-1-23 : fix bugs
* 2021-5-12 : Add support for USB 3.0 * 2021-5-12 : Add support for USB 3.0
@ -11,8 +11,9 @@
*/ */
#include <stdint.h> #include <stdint.h>
#include <stdbool.h> #include <stdbool.h>
#include "USBd_config.h"
#include "usb_defs.h" #include "components/USBIP/usb_descriptor.h"
#include "components/USBIP/usb_defs.h"
#define USBShort(ui16Value) ((ui16Value) & 0xff), ((ui16Value) >> 8) #define USBShort(ui16Value) ((ui16Value) & 0xff), ((ui16Value) >> 8)

View File

@ -1,7 +1,7 @@
#ifndef __USBD_CONFIG_H__ #ifndef __USB_DESCRIPTOR_H__
#define __USBD_CONFIG_H__ #define __USB_DESCRIPTOR_H__
#include "dap_configuration.h" #include "main/dap_configuration.h"
// Vendor ID assigned by USB-IF (idVendor). // Vendor ID assigned by USB-IF (idVendor).
#define USBD0_DEV_DESC_IDVENDOR 0xC251 #define USBD0_DEV_DESC_IDVENDOR 0xC251

View File

@ -3,21 +3,23 @@
* @brief Handle all Standard Device Requests on endpoint 0 * @brief Handle all Standard Device Requests on endpoint 0
* @version 0.1 * @version 0.1
* @date 2020-01-23 * @date 2020-01-23
* *
* @copyright Copyright (c) 2020 * @copyright Copyright (c) 2020
* *
*/ */
#include <stdint.h> #include <stdint.h>
#include <string.h> #include <string.h>
#include "main/usbip_server.h"
#include "components/USBIP/usb_handle.h"
#include "components/USBIP/usb_descriptor.h"
#include "components/USBIP/MSOS20_descriptor.h"
#include "lwip/err.h" #include "lwip/err.h"
#include "lwip/sockets.h" #include "lwip/sockets.h"
#include "lwip/sys.h" #include "lwip/sys.h"
#include <lwip/netdb.h> #include <lwip/netdb.h>
#include "USB_handle.h"
#include "USBd_config.h"
#include "usbip_server.h"
#include "usb_defs.h"
#include "MSOS20Descriptors.h"
const char *strings_list[] = { const char *strings_list[] = {
@ -263,7 +265,7 @@ static void handleGetDescriptor(usbip_stage2_header *header)
for (int i = 0; i < slen; i++) for (int i = 0; i < slen; i++)
{ {
desc->wData[i] = strings_list[header->u.cmd_submit.request.wValue.u8lo][i]; desc->wData[i] = strings_list[header->u.cmd_submit.request.wValue.u8lo][i];
} }
send_stage2_submit_data(header, 0, (uint8_t *)temp_buff, buff_len); send_stage2_submit_data(header, 0, (uint8_t *)temp_buff, buff_len);
} }

View File

@ -1,7 +1,8 @@
#ifndef __USB_HANDLE_H__ #ifndef __USB_HANDLE_H__
#define __USB_HANDLE_H__ #define __USB_HANDLE_H__
#include "usbip_defs.h" #include "components/USBIP/usbip_defs.h"
void handleUSBControlRequest(usbip_stage2_header *header); void handleUSBControlRequest(usbip_stage2_header *header);
#endif #endif

View File

@ -3,9 +3,9 @@
* @brief Simple modification * @brief Simple modification
* @version 0.1 * @version 0.1
* @date 2020-01-22 * @date 2020-01-22
* *
* @copyright Copyright (c) 2020 * @copyright Copyright (c) 2020
* *
*/ */
// Focus on the following structures in this file: // Focus on the following structures in this file:
@ -20,7 +20,8 @@
#define __USBIP_DEFS_H__ #define __USBIP_DEFS_H__
#include <stdint.h> #include <stdint.h>
#include "usb_defs.h"
#include "components/USBIP/usb_defs.h"
#define USBIP_SYSFS_PATH_SIZE 256 #define USBIP_SYSFS_PATH_SIZE 256
#define USBIP_BUSID_SIZE 32 #define USBIP_BUSID_SIZE 32
@ -33,7 +34,7 @@ enum usbip_stage1_command
}; };
enum usbip_stager2_command enum usbip_stager2_command
{ {
//Offset 0 //Offset 0
USBIP_STAGE2_REQ_SUBMIT = 0x0001, USBIP_STAGE2_REQ_SUBMIT = 0x0001,
USBIP_STAGE2_REQ_UNLINK = 0x0002, USBIP_STAGE2_REQ_UNLINK = 0x0002,
@ -106,7 +107,7 @@ typedef struct
/** /**
* struct usbip_header_basic - data pertinent to every URB request * struct usbip_header_basic - data pertinent to every URB request
* RESPONSE & REQUEST * RESPONSE & REQUEST
* *
* @command: the usbip request type * @command: the usbip request type
* @seqnum: sequential number that identifies requests; incremented per * @seqnum: sequential number that identifies requests; incremented per
* connection * connection
@ -127,7 +128,7 @@ typedef struct
/** /**
* struct usbip_header_cmd_submit - USBIP_CMD_SUBMIT packet header * struct usbip_header_cmd_submit - USBIP_CMD_SUBMIT packet header
* >>>REQUEST * >>>REQUEST
* *
* @transfer_flags: URB flags * @transfer_flags: URB flags
* @transfer_buffer_length: the data size for (in) or (out) transfer * @transfer_buffer_length: the data size for (in) or (out) transfer
* @start_frame: initial frame for isochronous or interrupt transfers * @start_frame: initial frame for isochronous or interrupt transfers
@ -154,7 +155,7 @@ typedef struct
/** /**
* struct usbip_header_ret_submit - USBIP_RET_SUBMIT packet header * struct usbip_header_ret_submit - USBIP_RET_SUBMIT packet header
* <<<RESPONSE * <<<RESPONSE
* *
* @status: return status of a non-iso request * @status: return status of a non-iso request
* @actual_length: number of bytes transferred * @actual_length: number of bytes transferred
* @start_frame: initial frame for isochronous or interrupt transfers * @start_frame: initial frame for isochronous or interrupt transfers

View File

@ -1,4 +1,4 @@
set(COMPONENT_ADD_INCLUDEDIRS ". $ENV{IDF_PATH}/components/esp8266/include/esp8266/ ../components/USBIP") set(COMPONENT_ADD_INCLUDEDIRS "${PROJECT_PATH}")
set(COMPONENT_SRCS "main.c timer.c tcp_server.c usbip_server.c DAP_handle.c") set(COMPONENT_SRCS "main.c timer.c tcp_server.c usbip_server.c DAP_handle.c")
register_component() register_component()

View File

@ -1,268 +1,268 @@
/** /**
* @file DAP_handle.c * @file DAP_handle.c
* @brief Handle DAP packets and transaction push * @brief Handle DAP packets and transaction push
* @version 0.4 * @version 0.4
* @change: 2020.02.04 first version * @change: 2020.02.04 first version
* 2020.11.11 support WinUSB mode * 2020.11.11 support WinUSB mode
* 2021.02.17 support SWO * 2021.02.17 support SWO
* *
* @copyright Copyright (c) 2021 * @copyright Copyright (c) 2021
* *
*/ */
#include <stdint.h> #include <stdint.h>
#include <string.h> #include <string.h>
#include "usbip_server.h" #include "main/usbip_server.h"
#include "DAP_handle.h" #include "main/DAP_handle.h"
#include "DAP.h" #include "main/dap_configuration.h"
#include "esp_libc.h"
#include "USBd_config.h" #include "components/USBIP/usb_descriptor.h"
#include "swo.h" #include "components/DAP/include/DAP.h"
#include "dap_configuration.h" #include "components/DAP/include/swo.h"
#include "freertos/FreeRTOS.h" #include "freertos/FreeRTOS.h"
#include "freertos/task.h" #include "freertos/task.h"
#include "freertos/ringbuf.h" #include "freertos/ringbuf.h"
#include "freertos/semphr.h" #include "freertos/semphr.h"
#include "lwip/err.h" #include "lwip/err.h"
#include "lwip/sockets.h" #include "lwip/sockets.h"
#include "lwip/sys.h" #include "lwip/sys.h"
#include <lwip/netdb.h> #include <lwip/netdb.h>
#if (USE_WINUSB == 1) #if (USE_WINUSB == 1)
typedef struct typedef struct
{ {
uint32_t length; uint32_t length;
uint8_t buf[DAP_PACKET_SIZE]; uint8_t buf[DAP_PACKET_SIZE];
} DapPacket_t; } DapPacket_t;
#else #else
typedef struct typedef struct
{ {
uint8_t buf[DAP_PACKET_SIZE]; uint8_t buf[DAP_PACKET_SIZE];
} DapPacket_t; } DapPacket_t;
#endif #endif
#define DAP_HANDLE_SIZE (sizeof(DapPacket_t)) #define DAP_HANDLE_SIZE (sizeof(DapPacket_t))
extern int kSock; extern int kSock;
extern TaskHandle_t kDAPTaskHandle; extern TaskHandle_t kDAPTaskHandle;
int kRestartDAPHandle = 0; int kRestartDAPHandle = 0;
static DapPacket_t DAPDataProcessed; static DapPacket_t DAPDataProcessed;
static int dap_respond = 0; static int dap_respond = 0;
// SWO Trace // SWO Trace
static uint8_t *swo_data_to_send = NULL; static uint8_t *swo_data_to_send = NULL;
static uint32_t swo_data_num; static uint32_t swo_data_num;
// DAP handle // DAP handle
static RingbufHandle_t dap_dataIN_handle = NULL; static RingbufHandle_t dap_dataIN_handle = NULL;
static RingbufHandle_t dap_dataOUT_handle = NULL; static RingbufHandle_t dap_dataOUT_handle = NULL;
static SemaphoreHandle_t data_response_mux = NULL; static SemaphoreHandle_t data_response_mux = NULL;
void handle_dap_data_request(usbip_stage2_header *header, uint32_t length) void handle_dap_data_request(usbip_stage2_header *header, uint32_t length)
{ {
uint8_t *data_in = (uint8_t *)header; uint8_t *data_in = (uint8_t *)header;
data_in = &(data_in[sizeof(usbip_stage2_header)]); data_in = &(data_in[sizeof(usbip_stage2_header)]);
// Point to the beginning of the URB packet // Point to the beginning of the URB packet
#if (USE_WINUSB == 1) #if (USE_WINUSB == 1)
send_stage2_submit(header, 0, 0); send_stage2_submit(header, 0, 0);
// always send constant size buf -> cuz we don't care about the IN packet size // 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 // 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); xRingbufferSend(dap_dataIN_handle, data_in - sizeof(uint32_t), DAP_HANDLE_SIZE, portMAX_DELAY);
xTaskNotifyGive(kDAPTaskHandle); xTaskNotifyGive(kDAPTaskHandle);
#else #else
send_stage2_submit(header, 0, 0); send_stage2_submit(header, 0, 0);
xRingbufferSend(dap_dataIN_handle, data_in, DAP_HANDLE_SIZE, portMAX_DELAY); xRingbufferSend(dap_dataIN_handle, data_in, DAP_HANDLE_SIZE, portMAX_DELAY);
xTaskNotifyGive(kDAPTaskHandle); xTaskNotifyGive(kDAPTaskHandle);
#endif #endif
// dap_respond = DAP_ProcessCommand((uint8_t *)data_in, (uint8_t *)data_out); // dap_respond = DAP_ProcessCommand((uint8_t *)data_in, (uint8_t *)data_out);
// //handle_dap_data_response(header); // //handle_dap_data_response(header);
// send_stage2_submit(header, 0, 0); // send_stage2_submit(header, 0, 0);
} }
void handle_dap_data_response(usbip_stage2_header *header) void handle_dap_data_response(usbip_stage2_header *header)
{ {
return; return;
// int resLength = dap_respond & 0xFFFF; // int resLength = dap_respond & 0xFFFF;
// if (resLength) // if (resLength)
// { // {
// send_stage2_submit_data(header, 0, (void *)DAPDataProcessed.buf, resLength); // send_stage2_submit_data(header, 0, (void *)DAPDataProcessed.buf, resLength);
// dap_respond = 0; // dap_respond = 0;
// } // }
// else // else
// { // {
// send_stage2_submit(header, 0, 0); // send_stage2_submit(header, 0, 0);
// } // }
} }
void handle_swo_trace_response(usbip_stage2_header *header) void handle_swo_trace_response(usbip_stage2_header *header)
{ {
#if (SWO_FUNCTION_ENABLE == 1) #if (SWO_FUNCTION_ENABLE == 1)
if (kSwoTransferBusy) if (kSwoTransferBusy)
{ {
// busy indicates that there is data to be send // busy indicates that there is data to be send
printf("swo use data\r\n"); printf("swo use data\r\n");
send_stage2_submit_data(header, 0, (void *)swo_data_to_send, swo_data_num); send_stage2_submit_data(header, 0, (void *)swo_data_to_send, swo_data_num);
SWO_TransferComplete(); SWO_TransferComplete();
} }
else else
{ {
// nothing to send. // nothing to send.
send_stage2_submit(header, 0, 0); send_stage2_submit(header, 0, 0);
} }
#else #else
send_stage2_submit(header, 0, 0); send_stage2_submit(header, 0, 0);
#endif #endif
} }
// SWO Data Queue Transfer // SWO Data Queue Transfer
// buf: pointer to buffer with data // buf: pointer to buffer with data
// num: number of bytes to transfer // num: number of bytes to transfer
void SWO_QueueTransfer(uint8_t *buf, uint32_t num) void SWO_QueueTransfer(uint8_t *buf, uint32_t num)
{ {
swo_data_to_send = buf; swo_data_to_send = buf;
swo_data_num = num; swo_data_num = num;
} }
void DAP_Thread(void *argument) void DAP_Thread(void *argument)
{ {
dap_dataIN_handle = xRingbufferCreate(DAP_HANDLE_SIZE * 20, 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); dap_dataOUT_handle = xRingbufferCreate(DAP_HANDLE_SIZE * 20, RINGBUF_TYPE_BYTEBUF);
data_response_mux = xSemaphoreCreateMutex(); data_response_mux = xSemaphoreCreateMutex();
size_t packetSize; size_t packetSize;
int resLength; int resLength;
DapPacket_t *item; DapPacket_t *item;
if (dap_dataIN_handle == NULL || dap_dataIN_handle == NULL || if (dap_dataIN_handle == NULL || dap_dataIN_handle == NULL ||
data_response_mux == NULL) data_response_mux == NULL)
{ {
os_printf("Can not create DAP ringbuf/mux!\r\n"); os_printf("Can not create DAP ringbuf/mux!\r\n");
vTaskDelete(NULL); vTaskDelete(NULL);
} }
for (;;) for (;;)
{ {
while (1) while (1)
{ {
if (kRestartDAPHandle) if (kRestartDAPHandle)
{ {
vRingbufferDelete(dap_dataIN_handle); vRingbufferDelete(dap_dataIN_handle);
vRingbufferDelete(dap_dataOUT_handle); vRingbufferDelete(dap_dataOUT_handle);
dap_dataIN_handle = dap_dataOUT_handle = NULL; dap_dataIN_handle = dap_dataOUT_handle = NULL;
dap_dataIN_handle = xRingbufferCreate(DAP_HANDLE_SIZE * 20, 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); dap_dataOUT_handle = xRingbufferCreate(DAP_HANDLE_SIZE * 20, RINGBUF_TYPE_BYTEBUF);
if (dap_dataIN_handle == NULL || dap_dataIN_handle == NULL) if (dap_dataIN_handle == NULL || dap_dataIN_handle == NULL)
{ {
os_printf("Can not create DAP ringbuf/mux!\r\n"); os_printf("Can not create DAP ringbuf/mux!\r\n");
vTaskDelete(NULL); vTaskDelete(NULL);
} }
kRestartDAPHandle = 0; kRestartDAPHandle = 0;
} }
ulTaskNotifyTake(pdFALSE, portMAX_DELAY); ulTaskNotifyTake(pdFALSE, portMAX_DELAY);
packetSize = 0; packetSize = 0;
item = (DapPacket_t *)xRingbufferReceiveUpTo(dap_dataIN_handle, &packetSize, item = (DapPacket_t *)xRingbufferReceiveUpTo(dap_dataIN_handle, &packetSize,
pdMS_TO_TICKS(1), DAP_HANDLE_SIZE); pdMS_TO_TICKS(1), DAP_HANDLE_SIZE);
if (packetSize == 0) if (packetSize == 0)
{ {
break; break;
} }
else if (packetSize < DAP_HANDLE_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)); 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); vRingbufferReturnItem(dap_dataIN_handle, (void *)item);
break; break;
// This may not happen because there is a semaphore acquisition // This may not happen because there is a semaphore acquisition
} }
if (item->buf[0] == ID_DAP_QueueCommands) if (item->buf[0] == ID_DAP_QueueCommands)
{ {
item->buf[0] = ID_DAP_ExecuteCommands; item->buf[0] = ID_DAP_ExecuteCommands;
} }
resLength = DAP_ProcessCommand((uint8_t *)item->buf, (uint8_t *)DAPDataProcessed.buf); // use first 4 byte to save length 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. vRingbufferReturnItem(dap_dataIN_handle, (void *)item); // process done.
// now prepare to reply // now prepare to reply
#if (USE_WINUSB == 1) #if (USE_WINUSB == 1)
DAPDataProcessed.length = resLength; DAPDataProcessed.length = resLength;
#endif #endif
xRingbufferSend(dap_dataOUT_handle, (void *)&DAPDataProcessed, DAP_HANDLE_SIZE, portMAX_DELAY); xRingbufferSend(dap_dataOUT_handle, (void *)&DAPDataProcessed, DAP_HANDLE_SIZE, portMAX_DELAY);
if (xSemaphoreTake(data_response_mux, portMAX_DELAY) == pdTRUE) if (xSemaphoreTake(data_response_mux, portMAX_DELAY) == pdTRUE)
{ {
++dap_respond; ++dap_respond;
xSemaphoreGive(data_response_mux); xSemaphoreGive(data_response_mux);
} }
} }
} }
} }
int fast_reply(uint8_t *buf, uint32_t length) int fast_reply(uint8_t *buf, uint32_t length)
{ {
usbip_stage2_header *buf_header = (usbip_stage2_header *)buf; usbip_stage2_header *buf_header = (usbip_stage2_header *)buf;
if (length == 48 && if (length == 48 &&
buf_header->base.command == PP_HTONL(USBIP_STAGE2_REQ_SUBMIT) && buf_header->base.command == PP_HTONL(USBIP_STAGE2_REQ_SUBMIT) &&
buf_header->base.direction == PP_HTONL(USBIP_DIR_IN) && buf_header->base.direction == PP_HTONL(USBIP_DIR_IN) &&
buf_header->base.ep == PP_HTONL(1)) buf_header->base.ep == PP_HTONL(1))
{ {
if (dap_respond > 0) if (dap_respond > 0)
{ {
DapPacket_t *item; DapPacket_t *item;
size_t packetSize = 0; size_t packetSize = 0;
item = (DapPacket_t *)xRingbufferReceiveUpTo(dap_dataOUT_handle, &packetSize, item = (DapPacket_t *)xRingbufferReceiveUpTo(dap_dataOUT_handle, &packetSize,
pdMS_TO_TICKS(10), DAP_HANDLE_SIZE); pdMS_TO_TICKS(10), DAP_HANDLE_SIZE);
if (packetSize == DAP_HANDLE_SIZE) if (packetSize == DAP_HANDLE_SIZE)
{ {
#if (USE_WINUSB == 1) #if (USE_WINUSB == 1)
send_stage2_submit_data_fast((usbip_stage2_header *)buf, item->buf, item->length); send_stage2_submit_data_fast((usbip_stage2_header *)buf, item->buf, item->length);
#else #else
send_stage2_submit_data_fast((usbip_stage2_header *)buf, item->buf, DAP_HANDLE_SIZE); send_stage2_submit_data_fast((usbip_stage2_header *)buf, item->buf, DAP_HANDLE_SIZE);
#endif #endif
if (xSemaphoreTake(data_response_mux, portMAX_DELAY) == pdTRUE) if (xSemaphoreTake(data_response_mux, portMAX_DELAY) == pdTRUE)
{ {
--dap_respond; --dap_respond;
xSemaphoreGive(data_response_mux); xSemaphoreGive(data_response_mux);
} }
vRingbufferReturnItem(dap_dataOUT_handle, (void *)item); vRingbufferReturnItem(dap_dataOUT_handle, (void *)item);
return 1; return 1;
} }
else if (packetSize > 0) else if (packetSize > 0)
{ {
os_printf("Wrong data out packet size:%d!\r\n", packetSize); os_printf("Wrong data out packet size:%d!\r\n", packetSize);
} }
////TODO: fast reply ////TODO: fast reply
} }
else else
{ {
buf_header->base.command = PP_HTONL(USBIP_STAGE2_RSP_SUBMIT); buf_header->base.command = PP_HTONL(USBIP_STAGE2_RSP_SUBMIT);
buf_header->base.direction = PP_HTONL(USBIP_DIR_OUT); buf_header->base.direction = PP_HTONL(USBIP_DIR_OUT);
buf_header->u.ret_submit.status = 0; buf_header->u.ret_submit.status = 0;
buf_header->u.ret_submit.data_length = 0; buf_header->u.ret_submit.data_length = 0;
buf_header->u.ret_submit.error_count = 0; buf_header->u.ret_submit.error_count = 0;
send(kSock, buf, 48, 0); send(kSock, buf, 48, 0);
return 1; return 1;
} }
} }
return 0; return 0;
} }

View File

@ -1,7 +1,7 @@
#ifndef __DAP_HANDLE_H__ #ifndef __DAP_HANDLE_H__
#define __DAP_HANDLE_H__ #define __DAP_HANDLE_H__
#include "usbip_defs.h" #include "components/USBIP/usbip_defs.h"
void handle_dap_data_request(usbip_stage2_header *header, uint32_t length); void handle_dap_data_request(usbip_stage2_header *header, uint32_t length);
void handle_dap_data_response(usbip_stage2_header *header); void handle_dap_data_response(usbip_stage2_header *header);

View File

@ -1,181 +1,181 @@
/* BSD Socket API Example /* BSD Socket API Example
This example code is in the Public Domain (or CC0 licensed, at your option.) This example code is in the Public Domain (or CC0 licensed, at your option.)
Unless required by applicable law or agreed to in writing, this Unless required by applicable law or agreed to in writing, this
software is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR software is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
CONDITIONS OF ANY KIND, either express or implied. CONDITIONS OF ANY KIND, either express or implied.
*/ */
#include <string.h> #include <string.h>
#include <stdint.h> #include <stdint.h>
#include <sys/param.h> #include <sys/param.h>
#include "freertos/FreeRTOS.h"
#include "freertos/task.h" #include "main/tcp_server.h"
#include "freertos/event_groups.h" #include "main/timer.h"
#include "esp_system.h" #include "main/wifi_configuration.h"
#include "esp_wifi.h"
#include "esp_event_loop.h" #include "freertos/FreeRTOS.h"
#include "esp_log.h" #include "freertos/task.h"
#include "nvs_flash.h" #include "freertos/event_groups.h"
#include "esp_system.h"
#include "lwip/err.h" #include "esp_wifi.h"
#include "lwip/sockets.h" #include "esp_event_loop.h"
#include "lwip/sys.h" #include "esp_log.h"
#include <lwip/netdb.h> #include "nvs_flash.h"
#include "tcp_server.h" #include "lwip/err.h"
#include "timer.h" #include "lwip/sockets.h"
#include "wifi_configuration.h" #include "lwip/sys.h"
#include "DAP_config.h" #include <lwip/netdb.h>
extern void DAP_Setup(void); extern void DAP_Setup(void);
extern void DAP_Thread(void *argument); extern void DAP_Thread(void *argument);
extern void SWO_Thread(); extern void SWO_Thread();
TaskHandle_t kDAPTaskHandle = NULL; TaskHandle_t kDAPTaskHandle = NULL;
/* FreeRTOS event group to signal when we are connected & ready to make a request */ /* FreeRTOS event group to signal when we are connected & ready to make a request */
static EventGroupHandle_t wifi_event_group; static EventGroupHandle_t wifi_event_group;
const int IPV4_GOTIP_BIT = BIT0; const int IPV4_GOTIP_BIT = BIT0;
#ifdef CONFIG_EXAMPLE_IPV6 #ifdef CONFIG_EXAMPLE_IPV6
const int IPV6_GOTIP_BIT = BIT1; const int IPV6_GOTIP_BIT = BIT1;
#endif #endif
static esp_err_t event_handler(void *ctx, system_event_t *event) static esp_err_t event_handler(void *ctx, system_event_t *event)
{ {
/* For accessing reason codes in case of disconnection */ /* For accessing reason codes in case of disconnection */
system_event_info_t *info = &event->event_info; system_event_info_t *info = &event->event_info;
switch (event->event_id) switch (event->event_id)
{ {
case SYSTEM_EVENT_STA_START: case SYSTEM_EVENT_STA_START:
esp_wifi_connect(); esp_wifi_connect();
os_printf("SYSTEM_EVENT_STA_START\r\n"); os_printf("SYSTEM_EVENT_STA_START\r\n");
break; break;
case SYSTEM_EVENT_STA_CONNECTED: case SYSTEM_EVENT_STA_CONNECTED:
#ifdef CONFIG_EXAMPLE_IPV6 #ifdef CONFIG_EXAMPLE_IPV6
/* enable ipv6 */ /* enable ipv6 */
tcpip_adapter_create_ip6_linklocal(TCPIP_ADAPTER_IF_STA); tcpip_adapter_create_ip6_linklocal(TCPIP_ADAPTER_IF_STA);
#endif #endif
break; break;
case SYSTEM_EVENT_STA_GOT_IP: case SYSTEM_EVENT_STA_GOT_IP:
xEventGroupSetBits(wifi_event_group, IPV4_GOTIP_BIT); xEventGroupSetBits(wifi_event_group, IPV4_GOTIP_BIT);
os_printf("SYSTEM EVENT STA GOT IP : %s\r\n", ip4addr_ntoa(&event->event_info.got_ip.ip_info.ip)); os_printf("SYSTEM EVENT STA GOT IP : %s\r\n", ip4addr_ntoa(&event->event_info.got_ip.ip_info.ip));
break; break;
case SYSTEM_EVENT_STA_DISCONNECTED: case SYSTEM_EVENT_STA_DISCONNECTED:
os_printf("Disconnect reason : %d\r\n", info->disconnected.reason); os_printf("Disconnect reason : %d\r\n", info->disconnected.reason);
if (info->disconnected.reason == WIFI_REASON_BASIC_RATE_NOT_SUPPORT) if (info->disconnected.reason == WIFI_REASON_BASIC_RATE_NOT_SUPPORT)
{ {
/*Switch to 802.11 bgn mode */ /*Switch to 802.11 bgn mode */
esp_wifi_set_protocol(ESP_IF_WIFI_STA, WIFI_PROTOCOL_11B | WIFI_PROTOCOL_11G | WIFI_PROTOCOL_11N); esp_wifi_set_protocol(ESP_IF_WIFI_STA, WIFI_PROTOCOL_11B | WIFI_PROTOCOL_11G | WIFI_PROTOCOL_11N);
} }
esp_wifi_connect(); esp_wifi_connect();
xEventGroupClearBits(wifi_event_group, IPV4_GOTIP_BIT); xEventGroupClearBits(wifi_event_group, IPV4_GOTIP_BIT);
#ifdef CONFIG_EXAMPLE_IPV6 #ifdef CONFIG_EXAMPLE_IPV6
xEventGroupClearBits(wifi_event_group, IPV6_GOTIP_BIT); xEventGroupClearBits(wifi_event_group, IPV6_GOTIP_BIT);
#endif #endif
break; break;
case SYSTEM_EVENT_AP_STA_GOT_IP6: case SYSTEM_EVENT_AP_STA_GOT_IP6:
#ifdef CONFIG_EXAMPLE_IPV6 #ifdef CONFIG_EXAMPLE_IPV6
xEventGroupSetBits(wifi_event_group, IPV6_GOTIP_BIT); xEventGroupSetBits(wifi_event_group, IPV6_GOTIP_BIT);
os_printf("SYSTEM_EVENT_STA_GOT_IP6\r\n"); os_printf("SYSTEM_EVENT_STA_GOT_IP6\r\n");
char *ip6 = ip6addr_ntoa(&event->event_info.got_ip6.ip6_info.ip); char *ip6 = ip6addr_ntoa(&event->event_info.got_ip6.ip6_info.ip);
os_printf("IPv6: %s\r\n", ip6); os_printf("IPv6: %s\r\n", ip6);
#endif #endif
default: default:
break; break;
} }
return ESP_OK; return ESP_OK;
} }
static void initialise_wifi(void) static void initialise_wifi(void)
{ {
tcpip_adapter_init(); tcpip_adapter_init();
#if (USE_STATIC_IP == 1) #if (USE_STATIC_IP == 1)
tcpip_adapter_dhcps_stop(TCPIP_ADAPTER_IF_STA); tcpip_adapter_dhcps_stop(TCPIP_ADAPTER_IF_STA);
tcpip_adapter_ip_info_t ip_info; tcpip_adapter_ip_info_t ip_info;
#define MY_IP4_ADDR(...) IP4_ADDR(__VA_ARGS__) #define MY_IP4_ADDR(...) IP4_ADDR(__VA_ARGS__)
MY_IP4_ADDR(&ip_info.ip, DAP_IP_ADDRESS); MY_IP4_ADDR(&ip_info.ip, DAP_IP_ADDRESS);
MY_IP4_ADDR(&ip_info.gw, DAP_IP_GATEWAY); MY_IP4_ADDR(&ip_info.gw, DAP_IP_GATEWAY);
MY_IP4_ADDR(&ip_info.netmask, DAP_IP_NETMASK); MY_IP4_ADDR(&ip_info.netmask, DAP_IP_NETMASK);
#undef MY_IP4_ADDR #undef MY_IP4_ADDR
tcpip_adapter_set_ip_info(TCPIP_ADAPTER_IF_STA, &ip_info); tcpip_adapter_set_ip_info(TCPIP_ADAPTER_IF_STA, &ip_info);
#endif // (USE_STATIC_IP == 1) #endif // (USE_STATIC_IP == 1)
wifi_event_group = xEventGroupCreate(); wifi_event_group = xEventGroupCreate();
ESP_ERROR_CHECK(esp_event_loop_init(event_handler, NULL)); ESP_ERROR_CHECK(esp_event_loop_init(event_handler, NULL));
wifi_init_config_t cfg = WIFI_INIT_CONFIG_DEFAULT(); wifi_init_config_t cfg = WIFI_INIT_CONFIG_DEFAULT();
ESP_ERROR_CHECK(esp_wifi_init(&cfg)); ESP_ERROR_CHECK(esp_wifi_init(&cfg));
ESP_ERROR_CHECK(esp_wifi_set_storage(WIFI_STORAGE_RAM)); ESP_ERROR_CHECK(esp_wifi_set_storage(WIFI_STORAGE_RAM));
wifi_config_t wifi_config = { wifi_config_t wifi_config = {
.sta = { .sta = {
.ssid = WIFI_SSID, .ssid = WIFI_SSID,
.password = WIFI_PASS, .password = WIFI_PASS,
}, },
}; };
os_printf("Setting WiFi configuration SSID %s...\r\n", wifi_config.sta.ssid); os_printf("Setting WiFi configuration SSID %s...\r\n", wifi_config.sta.ssid);
ESP_ERROR_CHECK(esp_wifi_set_mode(WIFI_MODE_STA)); 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_set_config(ESP_IF_WIFI_STA, &wifi_config));
ESP_ERROR_CHECK(esp_wifi_start()); ESP_ERROR_CHECK(esp_wifi_start());
} }
static void wait_for_ip() static void wait_for_ip()
{ {
#ifdef CONFIG_EXAMPLE_IPV6 #ifdef CONFIG_EXAMPLE_IPV6
uint32_t bits = IPV4_GOTIP_BIT | IPV6_GOTIP_BIT; uint32_t bits = IPV4_GOTIP_BIT | IPV6_GOTIP_BIT;
#else #else
uint32_t bits = IPV4_GOTIP_BIT; uint32_t bits = IPV4_GOTIP_BIT;
#endif #endif
os_printf("Waiting for AP connection...\r\n"); os_printf("Waiting for AP connection...\r\n");
xEventGroupWaitBits(wifi_event_group, bits, false, true, portMAX_DELAY); xEventGroupWaitBits(wifi_event_group, bits, false, true, portMAX_DELAY);
os_printf("Connected to AP\r\n"); os_printf("Connected to AP\r\n");
} }
void app_main() void app_main()
{ {
// struct rst_info *rtc_info = system_get_rst_info(); // struct rst_info *rtc_info = system_get_rst_info();
// os_printf("reset reason: %x\n", rtc_info->reason); // os_printf("reset reason: %x\n", rtc_info->reason);
// if (rtc_info->reason == REASON_WDT_RST || // if (rtc_info->reason == REASON_WDT_RST ||
// rtc_info->reason == REASON_EXCEPTION_RST || // rtc_info->reason == REASON_EXCEPTION_RST ||
// rtc_info->reason == REASON_SOFT_WDT_RST) // rtc_info->reason == REASON_SOFT_WDT_RST)
// { // {
// if (rtc_info->reason == REASON_EXCEPTION_RST) // if (rtc_info->reason == REASON_EXCEPTION_RST)
// { // {
// os_printf("Fatal exception (%d):\n", rtc_info->exccause); // os_printf("Fatal exception (%d):\n", rtc_info->exccause);
// } // }
// os_printf("epc1=0x%08x, epc2=0x%08x, epc3=0x%08x,excvaddr=0x%08x, depc=0x%08x\n", // os_printf("epc1=0x%08x, epc2=0x%08x, epc3=0x%08x,excvaddr=0x%08x, depc=0x%08x\n",
// rtc_info->epc1, rtc_info->epc2, rtc_info->epc3, // rtc_info->epc1, rtc_info->epc2, rtc_info->epc3,
// rtc_info->excvaddr, rtc_info->depc); // rtc_info->excvaddr, rtc_info->depc);
// } // }
ESP_ERROR_CHECK(nvs_flash_init()); ESP_ERROR_CHECK(nvs_flash_init());
initialise_wifi(); initialise_wifi();
wait_for_ip(); wait_for_ip();
DAP_Setup(); DAP_Setup();
timer_init(); timer_init();
xTaskCreate(tcp_server_task, "tcp_server", 4096, NULL, 14, NULL); xTaskCreate(tcp_server_task, "tcp_server", 4096, NULL, 14, NULL);
xTaskCreate(DAP_Thread, "DAP_Task", 2048, NULL, 10, &kDAPTaskHandle); xTaskCreate(DAP_Thread, "DAP_Task", 2048, NULL, 10, &kDAPTaskHandle);
// SWO Trace Task // SWO Trace Task
#if (SWO_FUNCTION_ENABLE == 1) #if (SWO_FUNCTION_ENABLE == 1)
xTaskCreate(SWO_Thread, "SWO_Task", 512, NULL, 10, NULL); xTaskCreate(SWO_Thread, "SWO_Task", 512, NULL, 10, NULL);
#endif #endif
// It seems that the task is overly stressful... // It seems that the task is overly stressful...
} }

View File

@ -1,5 +1,6 @@
#include <stdio.h> #include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
#include "freertos/FreeRTOS.h" #include "freertos/FreeRTOS.h"
#include "freertos/task.h" #include "freertos/task.h"
#include "esp_system.h" #include "esp_system.h"

View File

@ -1,181 +1,182 @@
/** /**
* @file tcp_server.c * @file tcp_server.c
* @brief Handle main tcp tasks * @brief Handle main tcp tasks
* @version 0.1 * @version 0.1
* @date 2020-01-22 * @date 2020-01-22
* *
* @copyright Copyright (c) 2020 * @copyright Copyright (c) 2020
* *
*/ */
#include "tcp_server.h" #include "tcp_server.h"
#include <string.h> #include <string.h>
#include <stdint.h> #include <stdint.h>
#include <sys/param.h> #include <sys/param.h>
#include "freertos/FreeRTOS.h"
#include "freertos/task.h" #include "main/wifi_configuration.h"
#include "freertos/event_groups.h" #include "main/usbip_server.h"
#include "esp_system.h"
#include "esp_wifi.h" #include "freertos/FreeRTOS.h"
#include "esp_event_loop.h" #include "freertos/task.h"
#include "esp_log.h" #include "freertos/event_groups.h"
#include "nvs_flash.h" #include "esp_system.h"
#include "esp_wifi.h"
#include "lwip/err.h" #include "esp_event_loop.h"
#include "lwip/sockets.h" #include "esp_log.h"
#include "lwip/sys.h" #include "nvs_flash.h"
#include <lwip/netdb.h>
#include "lwip/err.h"
#include "wifi_configuration.h" #include "lwip/sockets.h"
#include "usbip_server.h" #include "lwip/sys.h"
#include <lwip/netdb.h>
extern TaskHandle_t kDAPTaskHandle;
extern int kRestartDAPHandle; extern TaskHandle_t kDAPTaskHandle;
extern int kRestartDAPHandle;
uint8_t kState = ACCEPTING;
int kSock = -1; uint8_t kState = ACCEPTING;
int kSock = -1;
void tcp_server_task(void *pvParameters)
{ void tcp_server_task(void *pvParameters)
uint8_t tcp_rx_buffer[1500]; {
char addr_str[128]; uint8_t tcp_rx_buffer[1500];
int addr_family; char addr_str[128];
int ip_protocol; int addr_family;
int ip_protocol;
int on = 1;
while (1) int on = 1;
{ while (1)
{
#ifdef CONFIG_EXAMPLE_IPV4
struct sockaddr_in destAddr; #ifdef CONFIG_EXAMPLE_IPV4
destAddr.sin_addr.s_addr = htonl(INADDR_ANY); struct sockaddr_in destAddr;
destAddr.sin_family = AF_INET; destAddr.sin_addr.s_addr = htonl(INADDR_ANY);
destAddr.sin_port = htons(PORT); destAddr.sin_family = AF_INET;
addr_family = AF_INET; destAddr.sin_port = htons(PORT);
ip_protocol = IPPROTO_IP; addr_family = AF_INET;
inet_ntoa_r(destAddr.sin_addr, addr_str, sizeof(addr_str) - 1); ip_protocol = IPPROTO_IP;
#else // IPV6 inet_ntoa_r(destAddr.sin_addr, addr_str, sizeof(addr_str) - 1);
struct sockaddr_in6 destAddr; #else // IPV6
bzero(&destAddr.sin6_addr.un, sizeof(destAddr.sin6_addr.un)); struct sockaddr_in6 destAddr;
destAddr.sin6_family = AF_INET6; bzero(&destAddr.sin6_addr.un, sizeof(destAddr.sin6_addr.un));
destAddr.sin6_port = htons(PORT); destAddr.sin6_family = AF_INET6;
addr_family = AF_INET6; destAddr.sin6_port = htons(PORT);
ip_protocol = IPPROTO_IPV6; addr_family = AF_INET6;
inet6_ntoa_r(destAddr.sin6_addr, addr_str, sizeof(addr_str) - 1); ip_protocol = IPPROTO_IPV6;
#endif inet6_ntoa_r(destAddr.sin6_addr, addr_str, sizeof(addr_str) - 1);
#endif
int listen_sock = socket(addr_family, SOCK_STREAM, ip_protocol);
if (listen_sock < 0) int listen_sock = socket(addr_family, SOCK_STREAM, ip_protocol);
{ if (listen_sock < 0)
os_printf("Unable to create socket: errno %d\r\n", errno); {
break; os_printf("Unable to create socket: errno %d\r\n", errno);
} break;
os_printf("Socket created\r\n"); }
os_printf("Socket created\r\n");
setsockopt(listen_sock, SOL_SOCKET, SO_KEEPALIVE, (void *)&on, sizeof(on));
setsockopt(listen_sock, IPPROTO_TCP, TCP_NODELAY, (void *)&on, sizeof(on)); setsockopt(listen_sock, SOL_SOCKET, SO_KEEPALIVE, (void *)&on, sizeof(on));
setsockopt(listen_sock, IPPROTO_TCP, TCP_NODELAY, (void *)&on, sizeof(on));
int err = bind(listen_sock, (struct sockaddr *)&destAddr, sizeof(destAddr));
if (err != 0) int err = bind(listen_sock, (struct sockaddr *)&destAddr, sizeof(destAddr));
{ if (err != 0)
os_printf("Socket unable to bind: errno %d\r\n", errno); {
break; os_printf("Socket unable to bind: errno %d\r\n", errno);
} break;
os_printf("Socket binded\r\n"); }
os_printf("Socket binded\r\n");
err = listen(listen_sock, 1);
if (err != 0) err = listen(listen_sock, 1);
{ if (err != 0)
os_printf("Error occured during listen: errno %d\r\n", errno); {
break; os_printf("Error occured during listen: errno %d\r\n", errno);
} break;
os_printf("Socket listening\r\n"); }
os_printf("Socket listening\r\n");
#ifdef CONFIG_EXAMPLE_IPV6
struct sockaddr_in6 sourceAddr; // Large enough for both IPv4 or IPv6 #ifdef CONFIG_EXAMPLE_IPV6
#else struct sockaddr_in6 sourceAddr; // Large enough for both IPv4 or IPv6
struct sockaddr_in sourceAddr; #else
#endif struct sockaddr_in sourceAddr;
uint32_t addrLen = sizeof(sourceAddr); #endif
while (1) uint32_t addrLen = sizeof(sourceAddr);
{ while (1)
kSock = accept(listen_sock, (struct sockaddr *)&sourceAddr, &addrLen); {
if (kSock < 0) kSock = accept(listen_sock, (struct sockaddr *)&sourceAddr, &addrLen);
{ if (kSock < 0)
os_printf("Unable to accept connection: errno %d\r\n", errno); {
break; os_printf("Unable to accept connection: errno %d\r\n", errno);
} break;
setsockopt(kSock, SOL_SOCKET, SO_KEEPALIVE, (void *)&on, sizeof(on)); }
setsockopt(kSock, IPPROTO_TCP, TCP_NODELAY, (void *)&on, sizeof(on)); setsockopt(kSock, SOL_SOCKET, SO_KEEPALIVE, (void *)&on, sizeof(on));
os_printf("Socket accepted\r\n"); setsockopt(kSock, IPPROTO_TCP, TCP_NODELAY, (void *)&on, sizeof(on));
os_printf("Socket accepted\r\n");
while (1)
{ while (1)
int len = recv(kSock, tcp_rx_buffer, sizeof(tcp_rx_buffer), 0); {
// Error occured during receiving int len = recv(kSock, tcp_rx_buffer, sizeof(tcp_rx_buffer), 0);
if (len < 0) // Error occured during receiving
{ if (len < 0)
os_printf("recv failed: errno %d\r\n", errno); {
break; os_printf("recv failed: errno %d\r\n", errno);
} break;
// Connection closed }
else if (len == 0) // Connection closed
{ else if (len == 0)
os_printf("Connection closed\r\n"); {
break; os_printf("Connection closed\r\n");
} break;
// Data received }
else // Data received
{ else
// #ifdef CONFIG_EXAMPLE_IPV6 {
// // Get the sender's ip address as string // #ifdef CONFIG_EXAMPLE_IPV6
// if (sourceAddr.sin6_family == PF_INET) // // Get the sender's ip address as string
// { // if (sourceAddr.sin6_family == PF_INET)
// inet_ntoa_r(((struct sockaddr_in *)&sourceAddr)->sin_addr.s_addr, addr_str, sizeof(addr_str) - 1); // {
// } // inet_ntoa_r(((struct sockaddr_in *)&sourceAddr)->sin_addr.s_addr, addr_str, sizeof(addr_str) - 1);
// else if (sourceAddr.sin6_family == PF_INET6) // }
// { // else if (sourceAddr.sin6_family == PF_INET6)
// inet6_ntoa_r(sourceAddr.sin6_addr, addr_str, sizeof(addr_str) - 1); // {
// } // inet6_ntoa_r(sourceAddr.sin6_addr, addr_str, sizeof(addr_str) - 1);
// #else // }
// inet_ntoa_r(((struct sockaddr_in *)&sourceAddr)->sin_addr.s_addr, addr_str, sizeof(addr_str) - 1); // #else
// #endif // inet_ntoa_r(((struct sockaddr_in *)&sourceAddr)->sin_addr.s_addr, addr_str, sizeof(addr_str) - 1);
// #endif
switch (kState)
{ switch (kState)
case ACCEPTING: {
kState = ATTACHING; case ACCEPTING:
kState = ATTACHING;
case ATTACHING:
attach(tcp_rx_buffer, len); case ATTACHING:
break; attach(tcp_rx_buffer, len);
break;
case EMULATING:
emulate(tcp_rx_buffer, len); case EMULATING:
break; emulate(tcp_rx_buffer, len);
default: break;
os_printf("unkonw kstate!\r\n"); default:
} os_printf("unkonw kstate!\r\n");
} }
} }
// kState = ACCEPTING; }
if (kSock != -1) // kState = ACCEPTING;
{ if (kSock != -1)
os_printf("Shutting down socket and restarting...\r\n"); {
//shutdown(kSock, 0); os_printf("Shutting down socket and restarting...\r\n");
close(kSock); //shutdown(kSock, 0);
if (kState == EMULATING) close(kSock);
kState = ACCEPTING; if (kState == EMULATING)
kState = ACCEPTING;
// Restart DAP Handle
kRestartDAPHandle = 1; // Restart DAP Handle
xTaskNotifyGive(kDAPTaskHandle); kRestartDAPHandle = 1;
xTaskNotifyGive(kDAPTaskHandle);
//shutdown(listen_sock, 0);
//close(listen_sock); //shutdown(listen_sock, 0);
//vTaskDelay(5); //close(listen_sock);
} //vTaskDelay(5);
} }
} }
vTaskDelete(NULL); }
vTaskDelete(NULL);
} }

View File

@ -3,4 +3,4 @@
void tcp_server_task(void *pvParameters); void tcp_server_task(void *pvParameters);
#endif #endif

View File

@ -12,12 +12,9 @@
#include <stdint.h> #include <stdint.h>
#include <stdbool.h> #include <stdbool.h>
#include "timer.h" #include "main/timer.h"
#include "hw_timer.h" #include "hw_timer.h"
#include "timer_struct.h"
#include "cmsis_compiler.h"
#include "freertos/FreeRTOS.h" #include "freertos/FreeRTOS.h"
#include "freertos/task.h" #include "freertos/task.h"
#include "freertos/event_groups.h" #include "freertos/event_groups.h"

View File

@ -1,17 +1,17 @@
#include <stdint.h> #include <stdint.h>
#include <string.h> #include <string.h>
#include "main/usbip_server.h"
#include "main/DAP_handle.h"
#include "components/USBIP/usb_handle.h"
#include "components/USBIP/usb_descriptor.h"
#include "lwip/err.h" #include "lwip/err.h"
#include "lwip/sockets.h" #include "lwip/sockets.h"
#include "lwip/sys.h" #include "lwip/sys.h"
#include <lwip/netdb.h> #include <lwip/netdb.h>
#include "usbip_server.h"
#include "usbip_defs.h"
#include "usb_defs.h"
#include "USBd_config.h"
#include "DAP_handle.h"
#include "USB_handle.h"
#include "USBd_config.h"
// attach helper function // attach helper function
static int read_stage1_command(uint8_t *buffer, uint32_t length); static int read_stage1_command(uint8_t *buffer, uint32_t length);

View File

@ -1,7 +1,9 @@
#ifndef __USBIP_SERVER_H__ #ifndef __USBIP_SERVER_H__
#define __USBIP_SERVER_H__ #define __USBIP_SERVER_H__
#include <stdint.h> #include <stdint.h>
#include "usbip_defs.h"
#include "components/USBIP/usbip_defs.h"
enum state_t enum state_t
{ {
ACCEPTING, ACCEPTING,