:pserver:cvsanon@mok.lvcm.com:/CVS/ReactOS reactos
[reactos.git] / apps / utils / pice / module / debug.c
1 /*++
2
3 Copyright (c) 1998-2001 Klaus P. Gerlicher
4
5 Module Name:
6
7     debug.c
8
9 Abstract:
10
11     debug output
12
13 Environment:
14
15     LINUX 2.2.X
16     Kernel mode only
17
18 Author:
19
20     Klaus P. Gerlicher
21
22 Revision History:
23
24     04-Feb-1999:        created
25     15-Nov-2000:    general cleanup of source files
26
27 Copyright notice:
28
29   This file may be distributed under the terms of the GNU Public License.
30
31 --*/
32
33 ////////////////////////////////////////////////////
34 // INCLUDES
35 ////
36 #ifdef DEBUG
37 #include "remods.h"
38
39 #include "precomp.h"
40 #include <stdarg.h>
41 #include "serial.h"
42 #include "serial_port.h"
43
44 #define STANDARD_DEBUG_PREFIX "pICE: "
45
46 ////////////////////////////////////////////////////
47 // GLOBALS
48 ////
49 LONG lDebugLevel = 10;
50 ULONG ulDebugFlags;
51 char tempDebug[2048];
52 USHORT usDebugPortBase;
53
54 extern BOOLEAN bIsPrintkPatched;
55 ////////////////////////////////////////////////////
56 // FUNCTIONS
57 ////
58 void DebugSendString(LPSTR s);
59
60
61 //*************************************************************************
62 // Pice_dprintf()
63 //
64 // internal debug print
65 //*************************************************************************
66 VOID Pice_dprintf(ULONG DebugLevel, PCHAR DebugMessage, ...)
67 {
68         va_list         ap;
69
70         va_start(ap, DebugMessage);
71         if (/*DebugLevel <= lDebugLevel*/ DebugLevel == 2)
72         {
73           save_flags(ulDebugFlags);
74           cli();
75           PICE_vsprintf(tempDebug, DebugMessage, ap);
76           //ei DebugSendString(tempDebug);
77           Print(OUTPUT_WINDOW, tempDebug);
78           //DbgPrint("%s", tempDebug);
79           restore_flags(ulDebugFlags);
80         }
81         va_end(ap);
82 }
83
84 //************************************************************************
85 // SendByte()
86 //
87 // Output a character to the serial port
88 //************************************************************************
89 BOOLEAN DebugSendByte(UCHAR x)
90 {
91     ULONG timeout;
92
93     timeout = 0x00FFFFL;
94
95     // Wait for transmitter to clear
96     while ((inportb((USHORT)(usDebugPortBase + LSR)) & XMTRDY) == 0)
97         if (!(--timeout))
98         {
99                         return FALSE;
100         }
101
102     outportb((USHORT)(usDebugPortBase + TXR), x);
103
104         return TRUE;
105 }
106
107 ///************************************************************************
108 // DebugSetSpeed()
109 //
110 ///************************************************************************
111 void DebugSendString(LPSTR s)
112 {
113     ULONG len = PICE_strlen(s),i;
114
115     for(i=0;i<len;i++)
116     {
117         DebugSendByte(s[i]);
118     }
119     DebugSendByte('\r');
120 }
121
122 ///************************************************************************
123 // DebugSetSpeed()
124 //
125 ///************************************************************************
126 void DebugSetSpeed(ULONG baudrate)
127 {
128     UCHAR c;
129     ULONG divisor;
130
131     divisor = (ULONG) (115200L/baudrate);
132
133     c = inportb((USHORT)(usDebugPortBase + LCR));
134     outportb((USHORT)(usDebugPortBase + LCR), (UCHAR)(c | 0x80)); // Set DLAB
135     outportb((USHORT)(usDebugPortBase + DLL), (UCHAR)(divisor & 0x00FF));
136     outportb((USHORT)(usDebugPortBase + DLH), (UCHAR)((divisor >> 8) & 0x00FF));
137     outportb((USHORT)(usDebugPortBase + LCR), c);          // Reset DLAB
138
139 }
140
141 ///************************************************************************
142 // DebugSetOthers()
143 //
144 // Set other communications parameters
145 //************************************************************************
146 void DebugSetOthers(ULONG Parity, ULONG Bits, ULONG StopBit)
147 {
148     ULONG setting;
149     UCHAR c;
150
151     if (usDebugPortBase == 0)                                   return ;
152     if (Bits < 5 || Bits > 8)                           return ;
153     if (StopBit != 1 && StopBit != 2)                   return ;
154     if (Parity != NO_PARITY && Parity != ODD_PARITY && Parity != EVEN_PARITY)
155                                                         return;
156
157     setting  = Bits-5;
158     setting |= ((StopBit == 1) ? 0x00 : 0x04);
159     setting |= Parity;
160
161     c = inportb((USHORT)(usDebugPortBase + LCR));
162     outportb((USHORT)(usDebugPortBase + LCR), (UCHAR)(c & ~0x80)); // Reset DLAB
163
164     // no ints
165     outportb((USHORT)(usDebugPortBase + IER), (UCHAR)0);
166
167     outportb((USHORT)(usDebugPortBase + FCR), (UCHAR)0);
168
169     outportb((USHORT)(usDebugPortBase + LCR), (UCHAR)setting);
170
171     outportb((USHORT)(usDebugPortBase + MCR),  DTR | RTS);
172
173
174     return ;
175 }
176
177 ///************************************************************************
178 // DebugSetupSerial()
179 //
180 ///************************************************************************
181 void DebugSetupSerial(ULONG port,ULONG baudrate)
182 {
183         USHORT ports[]={COM1BASE,COM2BASE};
184 #if 0 //ei temporary
185     usDebugPortBase = ports[port-1];
186         DebugSetOthers(NO_PARITY,8,1);
187         DebugSetSpeed(baudrate);
188 #endif
189 }
190 #endif // DEBUG
191
192 // EOF