3 Copyright (c) 1998-2001 Klaus P. Gerlicher
11 serial debugger connection
25 15-Nov-2000: general cleanup of source files
29 This file may be distributed under the terms of the GNU Public License.
34 #include "serial_port.h"
36 BOOLEAN
SerialReadByte(PUCHAR px
);
39 // used for SERIAL window creation
40 // NB: at the moment the terminal is 60 lines high.
41 WINDOW wWindowSerial
[4]=
49 PUCHAR pScreenBufferSerial
;
51 USHORT usSerialPortBase
;
53 UCHAR packet
[_PAGE_SIZE
];
54 UCHAR assemble_packet
[_PAGE_SIZE
];
56 UCHAR flush_buffer
[_PAGE_SIZE
],g_x
,g_y
;
57 ULONG ulFlushBufferPos
= 0;
60 ECOLORS eForegroundColor
=WHITE
,eBackgroundColor
=BLACK
;
62 ///************************************************************************
65 ///************************************************************************
66 void SerialSetSpeed(ULONG baudrate
)
71 divisor
= (ULONG
) (115200L/baudrate
);
73 c
= inportb((USHORT
)(usSerialPortBase
+ LCR
));
74 outportb((USHORT
)(usSerialPortBase
+ LCR
), (UCHAR
)(c
| 0x80)); // Set DLAB
75 outportb((USHORT
)(usSerialPortBase
+ DLL
), (UCHAR
)(divisor
& 0x00FF));
76 outportb((USHORT
)(usSerialPortBase
+ DLH
), (UCHAR
)((divisor
>> 8) & 0x00FF));
77 outportb((USHORT
)(usSerialPortBase
+ LCR
), c
); // Reset DLAB
81 ///************************************************************************
84 // Set other communications parameters
85 //************************************************************************
86 void SerialSetOthers(ULONG Parity
, ULONG Bits
, ULONG StopBit
)
91 if (usSerialPortBase
== 0) return ;
92 if (Bits
< 5 || Bits
> 8) return ;
93 if (StopBit
!= 1 && StopBit
!= 2) return ;
94 if (Parity
!= NO_PARITY
&& Parity
!= ODD_PARITY
&& Parity
!= EVEN_PARITY
)
98 setting
|= ((StopBit
== 1) ? 0x00 : 0x04);
101 c
= inportb((USHORT
)(usSerialPortBase
+ LCR
));
102 outportb((USHORT
)(usSerialPortBase
+ LCR
), (UCHAR
)(c
& ~0x80)); // Reset DLAB
105 outportb((USHORT
)(usSerialPortBase
+ IER
), (UCHAR
)0);
107 // clear FIFO and disable them
108 outportb((USHORT
)(usSerialPortBase
+ FCR
), (UCHAR
)0);
110 outportb((USHORT
)(usSerialPortBase
+ LCR
), (UCHAR
)setting
);
112 outportb((USHORT
)(usSerialPortBase
+ MCR
), DTR
| RTS
);
118 ///************************************************************************
119 // FlushSerialBuffer()
121 ///************************************************************************
122 void FlushSerialBuffer(void)
126 while(SerialReadByte(&c
));
129 ///************************************************************************
132 ///************************************************************************
133 void SetupSerial(ULONG port
,ULONG baudrate
)
135 USHORT ports
[]={COM1BASE
,COM2BASE
,COM3BASE
,COM4BASE
};
137 usSerialPortBase
= ports
[port
-1];
138 SerialSetOthers(NO_PARITY
,8,1);
139 SerialSetSpeed(baudrate
);
141 // clear out received bytes
142 // else we would think there's a terminal connected
147 ///************************************************************************
150 // Output a character to the serial port
151 //************************************************************************
152 BOOLEAN
SerialReadByte(PUCHAR px
)
158 // Wait for transmitter to clear
159 while ((inportb((USHORT
)(usSerialPortBase
+ LSR
)) & RCVRDY
) == 0)
165 *px
= inportb((USHORT
)(usSerialPortBase
+ RXR
));
170 ///************************************************************************
173 // Output a character to the serial port
174 //************************************************************************
175 BOOLEAN
SerialSendByte(UCHAR x
)
181 // Wait for transmitter to clear
182 while ((inportb((USHORT
)(usSerialPortBase
+ LSR
)) & XMTRDY
) == 0)
188 outportb((USHORT
)(usSerialPortBase
+ TXR
), x
);
193 //************************************************************************
196 //************************************************************************
197 UCHAR
CheckSum(LPSTR p
,ULONG Len
)
199 UCHAR ucCheckSum
= 0;
211 ///************************************************************************
214 ///************************************************************************
215 BOOLEAN
ReadPacket(PSERIAL_PACKET p
)
221 ///************************************************************************
224 ///************************************************************************
225 BOOLEAN
SendPacket(PSERIAL_PACKET p
)
227 PUCHAR pHeader
= (PUCHAR
)&p
->header
;
235 pHeader
= (PUCHAR
)&p
->header
;
236 for(i
=0;i
<(sizeof(SERIAL_PACKET_HEADER
)+p
->header
.packet_size
);i
++)
238 if(!SerialSendByte(*pHeader
++))
250 }while(c
!= ACK
&& timeout
--);
257 ///************************************************************************
258 // SendPacketTimeout()
260 ///************************************************************************
261 BOOLEAN
SendPacketTimeout(PSERIAL_PACKET p
)
263 PUCHAR pHeader
= (PUCHAR
)&p
->header
;
267 BOOLEAN bResult
= TRUE
;
269 pHeader
= (PUCHAR
)&p
->header
;
270 for(i
=0;i
<(sizeof(SERIAL_PACKET_HEADER
)+p
->header
.packet_size
);i
++)
272 if(!SerialSendByte(*pHeader
++))
282 }while(c
!= ACK
&& timeout
--);
291 //************************************************************************
294 //************************************************************************
295 PSERIAL_PACKET
AssemblePacket(PUCHAR pData
,ULONG ulSize
)
300 p
= (PSERIAL_PACKET
)assemble_packet
;
303 p
->header
.packet_chksum
= CheckSum(pData
,ulSize
);
304 p
->header
.packet_size
= ulSize
;
305 p
->header
.packet_header_chksum
= 0;
306 ulCheckSum
= (ULONG
)CheckSum((PUCHAR
)p
,sizeof(SERIAL_PACKET_HEADER
));
307 p
->header
.packet_header_chksum
= ulCheckSum
;
308 // attach data to packet
309 PICE_memcpy(p
->data
,pData
,ulSize
);
317 //*************************************************************************
318 // SetForegroundColorVga()
320 //*************************************************************************
321 void SetForegroundColorSerial(ECOLORS col
)
323 eForegroundColor
= col
;
326 //*************************************************************************
327 // SetBackgroundColorVga()
329 //*************************************************************************
330 void SetBackgroundColorSerial(ECOLORS col
)
332 eBackgroundColor
= col
;
336 //*************************************************************************
339 //*************************************************************************
340 void PrintGrafSerial(ULONG x
,ULONG y
,UCHAR c
)
342 // put this into memory
343 pScreenBufferSerial
[y
*GLOBAL_SCREEN_WIDTH
+ x
] = c
;
345 // put this into cache
346 if(ulFlushBufferPos
== 0)
352 flush_buffer
[ulFlushBufferPos
++] = c
;
355 //*************************************************************************
358 //*************************************************************************
359 void FlushSerial(void)
361 PSERIAL_DATA_PACKET_PRINT pPrint
;
364 pPrint
= (PSERIAL_DATA_PACKET_PRINT
)packet
;
365 pPrint
->type
= PACKET_TYPE_PRINT
;
368 pPrint
->fgcol
= eForegroundColor
;
369 pPrint
->bkcol
= eBackgroundColor
;
370 flush_buffer
[ulFlushBufferPos
++] = 0;
371 PICE_strcpy(pPrint
->string
,flush_buffer
);
372 ulFlushBufferPos
= 0;
374 p
= AssemblePacket((PUCHAR
)pPrint
,sizeof(SERIAL_DATA_PACKET_PRINT
)+PICE_strlen(flush_buffer
));
378 //*************************************************************************
379 // ShowCursorSerial()
381 // show hardware cursor
382 //*************************************************************************
383 void ShowCursorSerial(void)
385 PSERIAL_DATA_PACKET_CURSOR pCursor
;
390 bCursorEnabled
= TRUE
;
392 pCursor
= (PSERIAL_DATA_PACKET_CURSOR
)packet
;
393 pCursor
->type
= PACKET_TYPE_CURSOR
;
394 pCursor
->state
= (UCHAR
)TRUE
;
395 pCursor
->x
= (UCHAR
)wWindow
[OUTPUT_WINDOW
].usCurX
;
396 pCursor
->y
= (UCHAR
)wWindow
[OUTPUT_WINDOW
].usCurY
;
398 p
= AssemblePacket((PUCHAR
)pCursor
,sizeof(SERIAL_DATA_PACKET_CURSOR
));
404 //*************************************************************************
405 // HideCursorSerial()
407 // hide hardware cursor
408 //*************************************************************************
409 void HideCursorSerial(void)
411 PSERIAL_DATA_PACKET_CURSOR pCursor
;
416 bCursorEnabled
= FALSE
;
418 pCursor
= (PSERIAL_DATA_PACKET_CURSOR
)packet
;
419 pCursor
->type
= PACKET_TYPE_CURSOR
;
420 pCursor
->state
= (UCHAR
)TRUE
;
422 p
= AssemblePacket((PUCHAR
)pCursor
,sizeof(SERIAL_DATA_PACKET_CURSOR
));
428 //*************************************************************************
429 // CopyLineToSerial()
431 // copy a line from src to dest
432 //*************************************************************************
433 void CopyLineToSerial(USHORT dest
,USHORT src
)
438 //*************************************************************************
439 // InvertLineSerial()
441 // invert a line on the screen
442 //*************************************************************************
443 void InvertLineSerial(ULONG line
)
445 PSERIAL_DATA_PACKET_INVERTLINE pInvertLine
;
448 pInvertLine
= (PSERIAL_DATA_PACKET_INVERTLINE
)packet
;
449 pInvertLine
->type
= PACKET_TYPE_INVERTLINE
;
450 pInvertLine
->line
= line
;
452 p
= AssemblePacket((PUCHAR
)pInvertLine
,sizeof(SERIAL_DATA_PACKET_INVERTLINE
));
456 //*************************************************************************
459 // hatches a line on the screen
460 //*************************************************************************
461 void HatchLineSerial(ULONG line
)
466 //*************************************************************************
469 // clear a line on the screen
470 //*************************************************************************
471 void ClrLineSerial(ULONG line
)
473 PSERIAL_DATA_PACKET_CLRLINE pClrLine
;
476 pClrLine
= (PSERIAL_DATA_PACKET_CLRLINE
)packet
;
477 pClrLine
->type
= PACKET_TYPE_CLRLINE
;
478 pClrLine
->fgcol
= eForegroundColor
;
479 pClrLine
->bkcol
= eBackgroundColor
;
480 pClrLine
->line
= line
;
482 p
= AssemblePacket((PUCHAR
)pClrLine
,sizeof(SERIAL_DATA_PACKET_CLRLINE
));
486 //*************************************************************************
489 //*************************************************************************
490 void PrintLogoSerial(BOOLEAN bShow
)
495 //*************************************************************************
496 // PrintCursorSerial()
498 // emulate a blinking cursor block
499 //*************************************************************************
500 void PrintCursorSerial(BOOLEAN bForce
)
505 //*************************************************************************
506 // SaveGraphicsStateSerial()
508 //*************************************************************************
509 void SaveGraphicsStateSerial(void)
514 //*************************************************************************
515 // RestoreGraphicsStateSerial()
517 //*************************************************************************
518 void RestoreGraphicsStateSerial(void)
524 //*************************************************************************
525 // GetKeyPolledSerial()
527 //*************************************************************************
528 UCHAR
GetKeyPolledSerial(void)
531 PSERIAL_DATA_PACKET_POLL pPoll
;
534 pPoll
= (PSERIAL_DATA_PACKET_POLL
)packet
;
535 pPoll
->type
= PACKET_TYPE_POLL
;
536 pPoll
->major_version
= PICE_MAJOR_VERSION
;
537 pPoll
->minor_version
= PICE_MINOR_VERSION
;
538 pPoll
->build_number
= PICE_BUILD
;
540 p
= AssemblePacket((PUCHAR
)pPoll
,sizeof(SERIAL_DATA_PACKET_POLL
));
543 ucResult
= ucLastKeyRead
;
550 //*************************************************************************
551 // FlushKeyboardQueueSerial()
553 //*************************************************************************
554 void FlushKeyboardQueueSerial(void)
559 //*************************************************************************
562 //*************************************************************************
563 BOOLEAN
Connect(USHORT xSize
,USHORT ySize
)
565 PSERIAL_DATA_PACKET_CONNECT pConnect
;
568 pConnect
= (PSERIAL_DATA_PACKET_CONNECT
)packet
;
569 pConnect
->type
= PACKET_TYPE_CONNECT
;
570 pConnect
->xsize
= xSize
;
571 pConnect
->ysize
= ySize
;
573 p
= AssemblePacket((PUCHAR
)pConnect
,sizeof(SERIAL_DATA_PACKET_CONNECT
));
574 return SendPacketTimeout(p
);
577 //*************************************************************************
578 // ConsoleInitSerial()
580 // init terminal screen
581 //*************************************************************************
582 BOOLEAN
ConsoleInitSerial(void)
584 BOOLEAN bResult
= FALSE
;
588 ohandlers
.CopyLineTo
= CopyLineToSerial
;
589 ohandlers
.PrintGraf
= PrintGrafSerial
;
590 ohandlers
.Flush
= FlushSerial
;
591 ohandlers
.ClrLine
= ClrLineSerial
;
592 ohandlers
.InvertLine
= InvertLineSerial
;
593 ohandlers
.HatchLine
= HatchLineSerial
;
594 ohandlers
.PrintLogo
= PrintLogoSerial
;
595 ohandlers
.PrintCursor
= PrintCursorSerial
;
596 ohandlers
.SaveGraphicsState
= SaveGraphicsStateSerial
;
597 ohandlers
.RestoreGraphicsState
= RestoreGraphicsStateSerial
;
598 ohandlers
.ShowCursor
= ShowCursorSerial
;
599 ohandlers
.HideCursor
= HideCursorSerial
;
600 ohandlers
.SetForegroundColor
= SetForegroundColorSerial
;
601 ohandlers
.SetBackgroundColor
= SetBackgroundColorSerial
;
603 ihandlers
.GetKeyPolled
= GetKeyPolledSerial
;
604 ihandlers
.FlushKeyboardQueue
= FlushKeyboardQueueSerial
;
606 SetWindowGeometry(wWindowSerial
);
608 GLOBAL_SCREEN_WIDTH
= 80;
609 GLOBAL_SCREEN_HEIGHT
= 60;
611 pScreenBufferSerial
= PICE_malloc(FRAMEBUFFER_SIZE
, NONPAGEDPOOL
);
613 if(pScreenBufferSerial
)
619 SetupSerial(1,115200);
621 // connect to terminal, if none's there, we give up
622 bResult
= Connect(GLOBAL_SCREEN_WIDTH
,GLOBAL_SCREEN_HEIGHT
);
626 GetKeyPolledSerial();
636 //*************************************************************************
637 // ConsoleShutdownSerial()
639 // exit terminal screen
640 //*************************************************************************
641 void ConsoleShutdownSerial(void)
649 if(pScreenBufferSerial
)
650 PICE_free(pScreenBufferSerial
);