2 Copyright (c) 2013, IIT Madras
5 Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
7 * Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
8 * Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.
9 * Neither the name of IIT Madras nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission.
11 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
12 ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
15 #include <arpa/inet.h>
22 #include "RBB_Shakti.h"
27 char frame
= 0; // frame = {NA,NA,NA,reset,request_tdo,tck,tms,tdi}
31 client_fd
= accept(socket_fd
, NULL
, NULL
);
32 if (client_fd
== -1) {
33 if (errno
== EAGAIN
) {
35 // No client waiting to connect right now.
37 fprintf(stderr
, "failed to accept on socket: %s (%d)\n", strerror(errno
),
41 fcntl(client_fd
, F_SETFL
, O_NONBLOCK
);
49 socket_fd
= socket(AF_INET
, SOCK_STREAM
, 0);
50 if (socket_fd
== -1) {
51 fprintf(stderr
, "remote_bitbang failed to make socket: %s (%d)\n",
52 strerror(errno
), errno
);
56 fcntl(socket_fd
, F_SETFL
, O_NONBLOCK
);
58 if (setsockopt(socket_fd
, SOL_SOCKET
, SO_REUSEADDR
, &reuseaddr
,
60 fprintf(stderr
, "remote_bitbang failed setsockopt: %s (%d)\n",
61 strerror(errno
), errno
);
65 struct sockaddr_in addr
;
66 memset(&addr
, 0, sizeof(addr
));
67 addr
.sin_family
= AF_INET
;
68 addr
.sin_addr
.s_addr
= INADDR_ANY
;
69 addr
.sin_port
= htons(port
);
71 if (bind(socket_fd
, (struct sockaddr
*) &addr
, sizeof(addr
)) == -1) {
72 fprintf(stderr
, "remote_bitbang failed to bind socket: %s (%d)\n",
73 strerror(errno
), errno
);
77 if (listen(socket_fd
, 1) == -1) {
78 fprintf(stderr
, "remote_bitbang failed to listen on socket: %s (%d)\n",
79 strerror(errno
), errno
);
83 socklen_t addrlen
= sizeof(addr
);
84 if (getsockname(socket_fd
, (struct sockaddr
*) &addr
, &addrlen
) == -1) {
85 fprintf(stderr
, "remote_bitbang getsockname failed: %s (%d)\n",
86 strerror(errno
), errno
);
90 printf("Listening for remote bitbang connection on port %d.\n",
91 ntohs(addr
.sin_port
));
93 printf("Waiting for OpenOCD .... \n");
94 while (accept_cxn() != 1);
99 char decode_frame(char command
){
100 // frame = {NA,NA,NA,reset,request_tdo,tck,tms,tdi}
102 case 'B': /* fprintf(stderr, "*BLINK*\n"); */ break; // not supported in spike
103 case 'b': /* fprintf(stderr, "_______\n"); */ break; // not supported in spike
104 case 'r': frame
&= ~((char)24); frame
|= 16 ; break;
105 case '0': frame
= 0; break;
106 case '1': frame
= 1; break;
107 case '2': frame
= 2; break;
108 case '3': frame
= 3; break;
109 case '4': frame
= 4; break;
110 case '5': frame
= 5; break;
111 case '6': frame
= 6; break;
112 case '7': frame
= 7; break;
113 case 'R': frame
&= ~((char)24); frame
|= 8 ; break; // push out a word with the previous state held with the read bit enabled maintain previous state and just push enable the read bit
114 case 'Q': break; // Not Supporting Q right now
116 frame
&= ~((char)24); //fprintf(stderr, "remote_bitbang got unsupported command '%d'\n",
117 // command); // essentially de assert the read bit if it was ever up;
122 // frame = {NA,NA,NA,reset,request_tdo,tck,tms,tdi}
125 read(client_fd
,&packet
, 1);
126 //printf("%d\n",decode_frame(packet));
127 return decode_frame(packet
);
130 void send_tdo(bool tdo
){
131 char a
= (tdo
)? '1' :'0' ;
132 write(client_fd
,&a
,1);
136 void main(){ // Test Bench of sorts
139 char fr = get_frame();
140 if((fr & 8) == 8)send_tdo(1);
141 if((fr & 16) == 16) printf("Reset Requested \n");