Skip to content
Open
Show file tree
Hide file tree
Changes from 9 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 7 additions & 1 deletion examples/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -43,4 +43,10 @@ add_example(boot_firmware boot_firmware.cpp)
add_example(xlink_server xlink_server.cpp)

# Server example
add_example(xlink_server2 xlink_server2.cpp)
add_example(xlink_server2 xlink_server2.cpp)

# Server example
add_example(xlink_usb_server xlink_usb_server.cpp)

# Client example
add_example(xlink_usb_client xlink_usb_client.cpp)
54 changes: 54 additions & 0 deletions examples/xlink_usb_client.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
#include <cstdio>
#include <string>
#include <vector>
#include <array>
#include <thread>
#include <stdexcept>
#include <iostream>
#include <cassert>
#include <chrono>
#include <thread>
#include <algorithm>
#include <cstring>
#include <atomic>

#include "XLink/XLink.h"
#include "XLink/XLinkPublicDefines.h"
#include "XLink/XLinkLog.h"

// Common constants
const uint8_t DUMMY_DATA[1024*128] = {};

int main(int argc, char** argv) {
XLinkGlobalHandler_t gHandler;
XLinkInitialize(&gHandler);

mvLogDefaultLevelSet(MVLOG_ERROR);

deviceDesc_t deviceDesc;
strcpy(deviceDesc.name, "usbdev");
deviceDesc.protocol = X_LINK_USB_EP;

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should be USB_VSC

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

USB_VSC will use EPs 81 and 1 to communicate, but those aren't the ones that are present in our gadget.
That's because in our gadget we create first the NCM function, then the XLink function, and after the ADB function.
I'll first modify the gadget and test it if it works.

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Then lets move the specialization for custom EP to usb vsc, and well then pick the right one afterwards. (Eg using state, or some other attributes of the device. We'll have to make the device discovery as well)


printf("Device name: %s\n", deviceDesc.name);

XLinkHandler_t handler;
handler.devicePath = deviceDesc.name;
handler.protocol = deviceDesc.protocol;
auto connRet = XLinkConnect(&handler);
printf("Connection returned: %s\n", XLinkErrorToStr(connRet));
if(connRet != X_LINK_SUCCESS) {
return -1;
}

auto s = XLinkOpenStream(handler.linkId, "test_0", sizeof(DUMMY_DATA) * 2);
if(s == INVALID_STREAM_ID){
printf("Open stream failed...\n");
} else {
printf("Open stream OK - id: 0x%08X\n", s);
}

auto w = XLinkWriteData(s, (uint8_t*) &s, sizeof(s));
assert(w == X_LINK_SUCCESS);

return 0;
}
62 changes: 62 additions & 0 deletions examples/xlink_usb_server.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
#include <cstdio>
#include <string>
#include <vector>
#include <array>
#include <thread>
#include <stdexcept>
#include <iostream>
#include <cassert>
#include <chrono>
#include <thread>
#include <algorithm>
#include <cstring>
#include <atomic>

#include "XLink/XLink.h"
#include "XLink/XLinkPublicDefines.h"
#include "XLink/XLinkLog.h"

// Common constants
constexpr static auto NUM_STREAMS = 16;
constexpr static auto NUM_PACKETS = 120;
const uint8_t DUMMY_DATA[1024*128] = {};
XLinkGlobalHandler_t xlinkGlobalHandler = {};

// Server
//

int main(int argc, const char** argv){

xlinkGlobalHandler.protocol = X_LINK_USB_EP;

// Initialize and suppress XLink logs
mvLogDefaultLevelSet(MVLOG_ERROR);
auto status = XLinkInitialize(&xlinkGlobalHandler);
if(X_LINK_SUCCESS != status) {
throw std::runtime_error("Couldn't initialize XLink");
}

XLinkHandler_t handler;
handler.devicePath = "/dev/usb-ffs/xlink";
handler.protocol = X_LINK_USB_EP;
XLinkServer(&handler, "eps", X_LINK_BOOTED, X_LINK_MYRIAD_X);


// loop through streams
auto s = XLinkOpenStream(0, "test_0", sizeof(DUMMY_DATA) * 2);
assert(s != INVALID_STREAM_ID);

// auto w = XLinkWriteData2(s, (uint8_t*) &s, sizeof(s/2), ((uint8_t*) &s) + sizeof(s/2), sizeof(s) - sizeof(s/2));
// assert(w == X_LINK_SUCCESS);

auto w = XLinkWriteData(s, (uint8_t*) &s, sizeof(s));
assert(w == X_LINK_SUCCESS);


streamPacketDesc_t p;
w = XLinkReadMoveData(s, &p);
assert(w == X_LINK_SUCCESS);
XLinkDeallocateMoveData(p.data, p.length);

return 0;
}
1 change: 1 addition & 0 deletions include/XLink/XLinkPublicDefines.h
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ typedef enum{
X_LINK_PCIE,
X_LINK_IPC,
X_LINK_TCP_IP,
X_LINK_USB_EP,
X_LINK_NMB_OF_PROTOCOLS,
X_LINK_ANY_PROTOCOL
} XLinkProtocol_t;
Expand Down
10 changes: 9 additions & 1 deletion src/pc/PlatformData.c
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,8 @@ extern int usbFdWrite;
extern int usbFdRead;
#endif /*USE_USB_VSC*/

#include "usb_host_ep.h"

// ------------------------------------
// Wrappers declaration. Begin.
// ------------------------------------
Expand All @@ -79,7 +81,7 @@ int XLinkPlatformWrite(xLinkDeviceHandle_t *deviceHandle, void *data, int size)
if(!XLinkIsProtocolInitialized(deviceHandle->protocol)) {
return X_LINK_PLATFORM_DRIVER_NOT_LOADED+deviceHandle->protocol;
}

switch (deviceHandle->protocol) {
case X_LINK_USB_VSC:
case X_LINK_USB_CDC:
Expand All @@ -91,6 +93,9 @@ int XLinkPlatformWrite(xLinkDeviceHandle_t *deviceHandle, void *data, int size)
case X_LINK_TCP_IP:
return tcpipPlatformWrite(deviceHandle->xLinkFD, data, size);

case X_LINK_USB_EP:
return usbEpPlatformWrite(deviceHandle->xLinkFD, data, size);

default:
return X_LINK_PLATFORM_INVALID_PARAMETERS;
}
Expand All @@ -113,6 +118,9 @@ int XLinkPlatformRead(xLinkDeviceHandle_t *deviceHandle, void *data, int size)
case X_LINK_TCP_IP:
return tcpipPlatformRead(deviceHandle->xLinkFD, data, size);

case X_LINK_USB_EP:
return usbEpPlatformRead(deviceHandle->xLinkFD, data, size);

default:
return X_LINK_PLATFORM_INVALID_PARAMETERS;
}
Expand Down
22 changes: 19 additions & 3 deletions src/pc/PlatformDeviceControl.c
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
#include "XLinkPlatform.h"
#include "XLinkPlatformErrorUtils.h"
#include "usb_host.h"
#include "usb_host_ep.h"
#include "pcie_host.h"
#include "tcpip_host.h"
#include "XLinkStringUtils.h"
Expand Down Expand Up @@ -82,17 +83,22 @@ xLinkPlatformErrorCode_t XLinkPlatformInit(XLinkGlobalHandler_t* globalHandler)
{
// Set that all protocols are initialized at first
for(int i = 0; i < X_LINK_NMB_OF_PROTOCOLS; i++) {
xlinkSetProtocolInitialized(i, 1);
xlinkSetProtocolInitialized(i, 1);
}

// check for failed initialization; LIBUSB_SUCCESS = 0
if (usbInitialize(globalHandler->options) != 0) {
xlinkSetProtocolInitialized(X_LINK_USB_VSC, 0);
xlinkSetProtocolInitialized(X_LINK_USB_VSC, 0);
}

// Initialize the USB EPs if present
if(usbEpInitialize() != 0) {
xlinkSetProtocolInitialized(X_LINK_USB_EP, 0);
}

// Initialize tcpip protocol if necessary
if(tcpip_initialize() != TCPIP_HOST_SUCCESS) {
xlinkSetProtocolInitialized(X_LINK_TCP_IP, 0);
xlinkSetProtocolInitialized(X_LINK_TCP_IP, 0);
}

return X_LINK_PLATFORM_SUCCESS;
Expand Down Expand Up @@ -170,6 +176,7 @@ xLinkPlatformErrorCode_t XLinkPlatformConnect(const char* devPathRead, const cha
if(!XLinkIsProtocolInitialized(protocol)) {
return X_LINK_PLATFORM_DRIVER_NOT_LOADED+protocol;
}

switch (protocol) {
case X_LINK_USB_VSC:
case X_LINK_USB_CDC:
Expand All @@ -181,6 +188,9 @@ xLinkPlatformErrorCode_t XLinkPlatformConnect(const char* devPathRead, const cha
case X_LINK_TCP_IP:
return tcpipPlatformConnect(devPathRead, devPathWrite, fd);

case X_LINK_USB_EP:
return usbEpPlatformConnect(devPathRead, devPathWrite, fd);

default:
return X_LINK_PLATFORM_INVALID_PARAMETERS;
}
Expand All @@ -192,6 +202,9 @@ xLinkPlatformErrorCode_t XLinkPlatformServer(const char* devPathRead, const char
case X_LINK_TCP_IP:
return tcpipPlatformServer(devPathRead, devPathWrite, fd);

case X_LINK_USB_EP:
return usbEpPlatformServer(devPathRead, devPathWrite, fd);

default:
return X_LINK_PLATFORM_INVALID_PARAMETERS;
}
Expand Down Expand Up @@ -239,6 +252,9 @@ xLinkPlatformErrorCode_t XLinkPlatformCloseRemote(xLinkDeviceHandle_t* deviceHan

case X_LINK_TCP_IP:
return tcpipPlatformClose(deviceHandle->xLinkFD);

case X_LINK_USB_EP:
return usbEpPlatformClose(deviceHandle->xLinkFD);

default:
return X_LINK_PLATFORM_INVALID_PARAMETERS;
Expand Down
Loading