3 Copyright (c) 1998-2001 Klaus P. Gerlicher
11 serial terminal for pICE headless mode
27 This file may be distributed under the terms of the GNU Public License.
35 #define CONSOLE_WIDTH (80)
36 #define CONSOLE_HEIGHT (25)
38 USHORT major_version
=0xFFFF,minor_version
=0xFFFF,build_number
=0xFFFF;
42 USHORT usCurX
,usCurY
,xSize
,ySize
;
44 USHORT foreground_color_map
[]=
48 USHORT background_color_map
[]=
54 struct termios oldtio
;
56 //************************************************************************
59 //************************************************************************
60 UCHAR
CheckSum(LPSTR p
,ULONG Len
)
73 ///************************************************************************
76 ///************************************************************************
77 BOOLEAN
ReadByte(PUCHAR pc
)
79 return (read(fd_comm
,pc
,1) > 0);
82 ///************************************************************************
85 ///************************************************************************
86 BOOLEAN
SendByte(UCHAR c
)
88 return (write(fd_comm
,&c
,1) > 0);
92 ///************************************************************************
95 ///************************************************************************
96 PSERIAL_PACKET
ReadPacket(void)
100 SERIAL_PACKET_HEADER header
;
101 PUCHAR pHeaderRaw
,pData
;
105 // read a packet header
106 pHeaderRaw
= (PUCHAR
)&header
;
107 for(i
=0;i
<sizeof(SERIAL_PACKET_HEADER
);i
++)
109 // //printf("reading()\n");
110 if(! ReadByte(pHeaderRaw
))
112 // //printf("no header byte read!\n");
119 //printf("received header!\n");
121 ulCheckSum
= header
.packet_header_chksum
;
122 header
.packet_header_chksum
= 0;
124 if(ulCheckSum
!= CheckSum((PUCHAR
)&header
,sizeof(SERIAL_PACKET_HEADER
)) )
126 //printf("header checksum mismatch!\n");
127 tcflush(fd_comm
, TCIFLUSH
);
131 p
= malloc(sizeof(SERIAL_PACKET_HEADER
) + header
.packet_size
);
134 //printf("out of memory!\n");
137 PICE_memcpy(p
,&header
,sizeof(SERIAL_PACKET_HEADER
));
139 sprintf(temp
,"size %X chksum %x\n",header
.packet_size
,header
.packet_chksum
);
142 // read the attached data
143 pData
= (PUCHAR
)p
+ sizeof(header
);
144 for(i
=0;i
<header
.packet_size
;i
++)
146 if(! ReadByte(pData
))
148 //printf("no data byte read!\n");
155 //printf("received data!\n");
157 pData
= (PUCHAR
)p
+ sizeof(header
);
158 if(header
.packet_chksum
!= CheckSum(pData
,header
.packet_size
))
162 //printf("data checksum mismatch!\n");
166 while(!SendByte(ACK
));
171 ///************************************************************************
174 ///************************************************************************
175 BOOLEAN
SendPacket(PSERIAL_PACKET p
)
180 void DeletePacket(PSERIAL_PACKET p
)
185 //************************************************************************
188 //************************************************************************
189 BOOLEAN
SetupSerial(ULONG port
,ULONG baudrate
)
191 struct termios newtio
;
192 char* ports
[]={"/dev/ttyS0","/dev/ttyS1","/dev/ttyS2","/dev/ttyS3"};
195 Open modem device for reading and writing and not as controlling tty
196 because we don't want to get killed if linenoise sends CTRL-C.
198 //printf("opening comm %s\n",ports[port-1]);
199 fd_comm
= open(ports
[port
-1], O_RDWR
| O_NOCTTY
);
202 perror(ports
[port
-1]);
206 //printf("tcgetattr()\n");
207 tcgetattr(fd_comm
,&oldtio
); /* save current modem settings */
210 Set bps rate and hardware flow control and 8n1 (8bit,no parity,1 stopbit).
211 Also don't hangup automatically and ignore modem status.
212 Finally enable receiving characters.
214 newtio
.c_cflag
= baudrate
| CS8
| CLOCAL
| CREAD
;
217 Ignore bytes with parity errors and make terminal raw and dumb.
219 newtio
.c_iflag
= IGNPAR
;
227 Don't echo characters because if you connect to a host it or your
228 modem will echo characters for you. Don't generate signals.
232 /* blocking read until 1 char arrives */
234 newtio
.c_cc
[VTIME
]=0;
236 /* now clean the modem line and activate the settings for modem */
237 //printf("tcflush()\n");
238 tcflush(fd_comm
, TCIFLUSH
);
239 //printf("tcsetattr()\n");
240 tcsetattr(fd_comm
,TCSANOW
,&newtio
);
249 //************************************************************************
252 //************************************************************************
253 void CloseSerial(void)
258 tcsetattr(fd_comm
,TCSANOW
,&oldtio
); /* save current modem settings */
262 //************************************************************************
265 //************************************************************************
266 void ClrLine(UCHAR line
)
271 //************************************************************************
274 //************************************************************************
275 void InvertLine(UCHAR line
)
280 //************************************************************************
281 // SetCursorPosition()
283 //************************************************************************
284 void SetCursorPosition(USHORT x
, USHORT y
)
289 //************************************************************************
290 // GetCursorPosition()
292 //************************************************************************
293 void GetCursorPosition(PUSHORT px
,PUSHORT py
)
297 //************************************************************************
300 //************************************************************************
301 void SetCursorState(UCHAR c
)
306 //************************************************************************
309 //************************************************************************
310 void Print(LPSTR p
,USHORT x
,USHORT y
)
312 // save the cursor pos
313 GetCursorPosition(&usCurX
,&usCurY
);
317 SetCursorPosition(x
,y
);
322 SetCursorPosition(usCurX
,usCurY
);
326 //************************************************************************
329 //************************************************************************
330 void ProcessPacket(PSERIAL_PACKET p
)
333 PSERIAL_DATA_PACKET pData
;
335 pData
= (PSERIAL_DATA_PACKET
)((PUCHAR
)p
+ sizeof(SERIAL_PACKET_HEADER
));
336 ulSize
= p
->header
.packet_size
;
340 case PACKET_TYPE_CONNECT
:
342 PSERIAL_DATA_PACKET_CONNECT pDataConnect
= (PSERIAL_DATA_PACKET_CONNECT
)pData
;
349 SetCursorPosition(0,0);
350 // ResizeConsole(hConsole,pDataConnect->xsize,pDataConnect->ysize);
351 xSize
= pDataConnect
->xsize
;
352 ySize
= pDataConnect
->ysize
;
355 case PACKET_TYPE_CLRLINE
:
357 PSERIAL_DATA_PACKET_CLRLINE pDataClrLine
= (PSERIAL_DATA_PACKET_CLRLINE
)pData
;
359 ClrLine(pDataClrLine
->line
);
362 case PACKET_TYPE_INVERTLINE
:
364 PSERIAL_DATA_PACKET_INVERTLINE pDataInvertLine
= (PSERIAL_DATA_PACKET_INVERTLINE
)pData
;
366 InvertLine(pDataInvertLine
->line
);
369 case PACKET_TYPE_PRINT
:
371 PSERIAL_DATA_PACKET_PRINT pDataPrint
= (PSERIAL_DATA_PACKET_PRINT
)pData
;
373 Print(pDataPrint
->string
,pDataPrint
->x
,pDataPrint
->y
);
376 case PACKET_TYPE_CURSOR
:
378 PSERIAL_DATA_PACKET_CURSOR pDataCursor
= (PSERIAL_DATA_PACKET_CURSOR
)pData
;
380 SetCursorPosition(pDataCursor
->x
,pDataCursor
->y
);
381 SetCursorState(pDataCursor
->state
);
384 case PACKET_TYPE_POLL
:
386 PSERIAL_DATA_PACKET_POLL pDataPoll
= (PSERIAL_DATA_PACKET_POLL
)pData
;
388 if( (major_version
!= pDataPoll
->major_version
) ||
389 (minor_version
!= pDataPoll
->minor_version
) ||
390 (build_number
!= pDataPoll
->build_number
) )
392 major_version
= pDataPoll
->major_version
;
393 minor_version
= pDataPoll
->minor_version
;
394 build_number
= pDataPoll
->build_number
;
402 //printf("UNHANDLED\n");
407 //************************************************************************
410 //************************************************************************
411 void DebuggerShell(void)
415 //printf("DebuggerShell()\n");