3 * Copyright (C) 2001 Brian Palmer <brianp@sginet.com>
4 * Copyright (C) 2001 Eric Kohl
5 * Copyright (C) 2001 Emanuele Aliberti
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License along
18 * with this program; if not, write to the Free Software Foundation, Inc.,
19 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
24 /* INCLUDES *******************************************************************/
27 #include <cportlib/cportlib.h>
31 /* STATIC VARIABLES ***********************************************************/
33 #define DEFAULT_BAUD_RATE 19200
35 #if defined(_M_IX86) || defined(_M_AMD64)
36 static const ULONG BaseArray
[] = {0, 0x3F8, 0x2F8, 0x3E8, 0x2E8};
38 static const ULONG BaseArray
[] = {0, 0x800003F8};
39 #elif defined(_M_MIPS)
40 static const ULONG BaseArray
[] = {0, 0x80006000, 0x80007000};
42 static const ULONG BaseArray
[] = {0, 0xF1012000};
44 #error Unknown architecture
47 #define MAX_COM_PORTS (sizeof(BaseArray) / sizeof(BaseArray[0]) - 1)
49 /* The COM port must only be initialized once! */
50 static ULONG Rs232ComPort
= 0;
51 static CPPORT Rs232ComPortInfo
;
53 /* FUNCTIONS ******************************************************************/
55 BOOLEAN
Rs232PortInitialize(IN ULONG ComPort
,
62 * Check whether it's the first time we initialize a COM port.
63 * If not, check whether the specified one was already initialized.
65 if ((Rs232ComPort
!= 0) && (Rs232ComPort
== ComPort
))
69 BaudRate
= DEFAULT_BAUD_RATE
;
74 * Start enumerating COM ports from the last one to the first one,
75 * and break when we find a valid port.
76 * If we reach the first element of the list, the invalid COM port,
77 * then it means that no valid port was found.
79 for (ComPort
= MAX_COM_PORTS
; ComPort
> 0; ComPort
--)
81 if (CpDoesPortExist(UlongToPtr(BaseArray
[ComPort
])))
83 Address
= UlongToPtr(BaseArray
[ComPort
]);
90 else if (ComPort
<= MAX_COM_PORTS
)
92 if (CpDoesPortExist(UlongToPtr(BaseArray
[ComPort
])))
93 Address
= UlongToPtr(BaseArray
[ComPort
]);
102 Status
= CpInitialize(&Rs232ComPortInfo
, Address
, BaudRate
);
103 if (!NT_SUCCESS(Status
)) return FALSE
;
105 Rs232ComPort
= ComPort
;
110 BOOLEAN
Rs232PortGetByte(PUCHAR ByteReceived
)
112 if (Rs232ComPort
== 0) return FALSE
;
113 return (CpGetByte(&Rs232ComPortInfo
, ByteReceived
, TRUE
, FALSE
) == CP_GET_SUCCESS
);
117 BOOLEAN Rs232PortPollByte(PUCHAR ByteReceived)
119 if (Rs232ComPort == 0) return FALSE;
120 return (CpGetByte(&Rs232ComPortInfo, ByteReceived, FALSE, FALSE) == CP_GET_SUCCESS);
124 VOID
Rs232PortPutByte(UCHAR ByteToSend
)
126 if (Rs232ComPort
== 0) return;
127 CpPutByte(&Rs232ComPortInfo
, ByteToSend
);
132 BOOLEAN
Rs232PortInUse(PUCHAR Base
)
135 return ( ((Rs232ComPort
!= 0) && (Rs232ComPortInfo
.Address
== Base
)) ? TRUE
: FALSE
);
141 #endif /* not _M_ARM */