Line 10... |
Line 10... |
10 |
// |
10 |
// |
11 |
// 1.00 2012_09 Proof of concept (no configuration, not for public release) |
11 |
// 1.00 2012_09 Proof of concept (no configuration, not for public release) |
12 |
// 1.01 2012_09 Added parameter for device selection |
12 |
// 1.01 2012_09 Added parameter for device selection |
13 |
// 1.02 2012_12 Error handling and debugged |
13 |
// 1.02 2012_12 Error handling and debugged |
14 |
// 1.03 2012_12 Release version ready to publish |
14 |
// 1.03 2012_12 Release version ready to publish |
15 |
// 1.04 2013_04 Socket Bind Error with eplanation (multiple instance of XVC Server) |
15 |
// 1.04 2013_04 Socket Bind Error with explanation (multiple instance of XVC Server) |
- |
|
16 |
// 1.05 2013-04 Test FTDI cable during wait for Accept (to stop the server immediately when cable is disconnected) |
16 |
// |
17 |
// |
17 |
// |
18 |
// |
18 |
// Purpose: |
19 |
// Purpose: |
19 |
// |
20 |
// |
20 |
// XILINX development software (ISE, WebPack) supports several types of JTAG programming |
21 |
// XILINX development software (ISE, WebPack) supports several types of JTAG programming |
Line 56... |
Line 57... |
56 |
// XVC protocol is documented (you have to ask XILINX support to gain access). |
57 |
// XVC protocol is documented (you have to ask XILINX support to gain access). |
57 |
// The program is inspired by the work http://debugmo.de/2012/02/xvcd-the-xilinx-virtual-cable-daemon/ |
58 |
// The program is inspired by the work http://debugmo.de/2012/02/xvcd-the-xilinx-virtual-cable-daemon/ |
58 |
// Ask Google about Xilinx Virtual Cable. |
59 |
// Ask Google about Xilinx Virtual Cable. |
59 |
// |
60 |
// |
60 |
// |
61 |
// |
61 |
// Translation: |
62 |
// Compilation: |
62 |
// |
63 |
// |
63 |
// MS Visual C++ 2010 Express (free, registration required) |
64 |
// MS Visual C++ 2010 Express (free, registration required) |
64 |
// Create new empty project for Win32 Console Application and name project mlab_xvcd (to build mlab_xvcd.exe) |
65 |
// Create new empty project for Win32 Console Application and name project mlab_xvcd (to build mlab_xvcd.exe) |
65 |
// Header Files / Add / Existing Items - all .h files |
66 |
// Header Files / Add / Existing Items - all .h files |
66 |
// Resource Files / Add / Existing Items - all .lib files |
67 |
// Resource Files / Add / Existing Items - all .lib files |
Line 467... |
Line 468... |
467 |
iResult = bind(ListenSocket, result->ai_addr, (int)result->ai_addrlen); |
468 |
iResult = bind(ListenSocket, result->ai_addr, (int)result->ai_addrlen); |
468 |
if (iResult == SOCKET_ERROR) |
469 |
if (iResult == SOCKET_ERROR) |
469 |
{ |
470 |
{ |
470 |
int LastError=WSAGetLastError(); |
471 |
int LastError=WSAGetLastError(); |
471 |
fprintf(stderr, "Bind failed with error: %d\n", LastError); |
472 |
fprintf(stderr, "Bind failed with error: %d\n", LastError); |
472 |
if (LastError==10048) fprintf(stderr, "Trying to start second instance of XVC Server?\n"); |
473 |
if (LastError==WSAEADDRINUSE) fprintf(stderr, "Trying to start second instance of XVC Server?\n"); |
473 |
freeaddrinfo(result); |
474 |
freeaddrinfo(result); |
474 |
closesocket(ListenSocket); |
475 |
closesocket(ListenSocket); |
475 |
WSACleanup(); |
476 |
WSACleanup(); |
476 |
jtagClosePort(); |
477 |
jtagClosePort(); |
477 |
return -2; |
478 |
return -2; |
Line 503... |
Line 504... |
503 |
do |
504 |
do |
504 |
{ |
505 |
{ |
505 |
printf(" Listen\n"); |
506 |
printf(" Listen\n"); |
506 |
jtagSetLED(true); |
507 |
jtagSetLED(true); |
507 |
|
508 |
|
- |
|
509 |
// Set ListenSocket to non-blocking mode |
- |
|
510 |
// We need during waiting for Accept to detect FTDI disconnect |
- |
|
511 |
u_long iMode = 1; |
- |
|
512 |
iResult = ioctlsocket(ListenSocket, FIONBIO, &iMode); |
- |
|
513 |
if (iResult != NO_ERROR) |
- |
|
514 |
{ |
- |
|
515 |
fprintf(stderr, "ioctlsocket failed with error: %ld\n", iResult); |
- |
|
516 |
WSACleanup(); |
- |
|
517 |
jtagClosePort(); |
- |
|
518 |
return -2; |
- |
|
519 |
} |
- |
|
520 |
|
508 |
// Accept a client SOCKET |
521 |
// Accept a client SOCKET (wait for Accept) |
509 |
sockaddr ClientSocetAddr; |
522 |
sockaddr ClientSocetAddr; |
510 |
int ClientSocetAddrLen = sizeof(sockaddr); |
523 |
int ClientSocetAddrLen = sizeof(sockaddr); |
- |
|
524 |
do |
- |
|
525 |
{ |
- |
|
526 |
// Try Accept (non-blocking) |
511 |
ClientSocket = accept(ListenSocket, &ClientSocetAddr, &ClientSocetAddrLen); |
527 |
ClientSocket = accept(ListenSocket, &ClientSocetAddr, &ClientSocetAddrLen); |
512 |
if (ClientSocket == INVALID_SOCKET) |
528 |
if (ClientSocket == INVALID_SOCKET) |
- |
|
529 |
{ |
- |
|
530 |
// Accept Error |
- |
|
531 |
if (WSAGetLastError() != WSAEWOULDBLOCK) |
- |
|
532 |
{ |
- |
|
533 |
fprintf(stderr, "accept failed with error: %d\n", WSAGetLastError()); |
- |
|
534 |
closesocket(ListenSocket); |
- |
|
535 |
WSACleanup(); |
- |
|
536 |
jtagClosePort(); |
- |
|
537 |
return -2; |
- |
|
538 |
} |
- |
|
539 |
// Not yet Accepted |
- |
|
540 |
{ |
- |
|
541 |
// Check FTDI |
- |
|
542 |
if (!CheckCable()) |
- |
|
543 |
{ |
- |
|
544 |
fprintf(stderr, "XVC Cable unexpectadly disconnected\n"); |
- |
|
545 |
closesocket(ListenSocket); |
- |
|
546 |
WSACleanup(); |
- |
|
547 |
jtagClosePort(); |
- |
|
548 |
return -2; |
- |
|
549 |
} |
- |
|
550 |
// Sleep some time (do not eat CPU time for nothong) |
- |
|
551 |
//nanosleep(); |
- |
|
552 |
Sleep(100); //ms |
- |
|
553 |
} |
- |
|
554 |
} |
- |
|
555 |
} |
- |
|
556 |
while (ClientSocket == INVALID_SOCKET); |
- |
|
557 |
|
- |
|
558 |
// Set (Accepted) Socket to blocking mode |
- |
|
559 |
iMode = 0; |
- |
|
560 |
iResult = ioctlsocket(ClientSocket, FIONBIO, &iMode); |
- |
|
561 |
if (iResult != NO_ERROR) |
513 |
{ |
562 |
{ |
514 |
fprintf(stderr, "accept failed with error: %d\n", WSAGetLastError()); |
563 |
fprintf(stderr, "ioctlsocket failed with error: %ld\n", iResult); |
515 |
closesocket(ListenSocket); |
- |
|
516 |
WSACleanup(); |
564 |
WSACleanup(); |
517 |
jtagClosePort(); |
565 |
jtagClosePort(); |
518 |
return -2; |
566 |
return -2; |
519 |
} |
567 |
} |
520 |
|
568 |
|