ReactOS  0.4.15-dev-1197-g8081ba9
rs232.c
Go to the documentation of this file.
1 /*
2  * FreeLoader
3  * Copyright (C) 2001 Brian Palmer <brianp@sginet.com>
4  * Copyright (C) 2001 Eric Kohl
5  * Copyright (C) 2001 Emanuele Aliberti
6  *
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.
11  *
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.
16  *
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.
20  */
21 
22 #ifndef _M_ARM
23 
24 /* INCLUDES *******************************************************************/
25 
26 #include <freeldr.h>
27 #include <cportlib/cportlib.h>
28 
29 #if DBG
30 
31 /* STATIC VARIABLES ***********************************************************/
32 
33 #if defined(SARCH_PC98)
34 #define DEFAULT_BAUD_RATE 9600
35 #else
36 #define DEFAULT_BAUD_RATE 19200
37 #endif
38 
39 #if defined(_M_IX86) || defined(_M_AMD64)
40 #if defined(SARCH_PC98)
41 static const ULONG BaseArray[] = {0, 0x30, 0x238};
42 #else
43 static const ULONG BaseArray[] = {0, 0x3F8, 0x2F8, 0x3E8, 0x2E8};
44 #endif
45 #elif defined(_M_PPC)
46 static const ULONG BaseArray[] = {0, 0x800003F8};
47 #elif defined(_M_MIPS)
48 static const ULONG BaseArray[] = {0, 0x80006000, 0x80007000};
49 #elif defined(_M_ARM)
50 static const ULONG BaseArray[] = {0, 0xF1012000};
51 #else
52 #error Unknown architecture
53 #endif
54 
55 #define MAX_COM_PORTS (sizeof(BaseArray) / sizeof(BaseArray[0]) - 1)
56 
57 /* The COM port must only be initialized once! */
58 static ULONG Rs232ComPort = 0;
59 static CPPORT Rs232ComPortInfo;
60 
61 /* FUNCTIONS ******************************************************************/
62 
64  IN ULONG BaudRate)
65 {
68 
69  /*
70  * Check whether it's the first time we initialize a COM port.
71  * If not, check whether the specified one was already initialized.
72  */
73  if ((Rs232ComPort != 0) && (Rs232ComPort == ComPort))
74  return TRUE;
75 
76  if (BaudRate == 0)
77  BaudRate = DEFAULT_BAUD_RATE;
78 
79  if (ComPort == 0)
80  {
81  /*
82  * Start enumerating COM ports from the last one to the first one,
83  * and break when we find a valid port.
84  * If we reach the first element of the list, the invalid COM port,
85  * then it means that no valid port was found.
86  */
87  for (ComPort = MAX_COM_PORTS; ComPort > 0; ComPort--)
88  {
89  if (CpDoesPortExist(UlongToPtr(BaseArray[ComPort])))
90  {
91  Address = UlongToPtr(BaseArray[ComPort]);
92  break;
93  }
94  }
95  if (ComPort == 0)
96  return FALSE;
97  }
98  else if (ComPort <= MAX_COM_PORTS)
99  {
100  if (CpDoesPortExist(UlongToPtr(BaseArray[ComPort])))
101  Address = UlongToPtr(BaseArray[ComPort]);
102  else
103  return FALSE;
104  }
105  else
106  {
107  return FALSE;
108  }
109 
110  Status = CpInitialize(&Rs232ComPortInfo, Address, BaudRate);
111  if (!NT_SUCCESS(Status)) return FALSE;
112 
113  Rs232ComPort = ComPort;
114 
115  return TRUE;
116 }
117 
118 BOOLEAN Rs232PortGetByte(PUCHAR ByteReceived)
119 {
120  if (Rs232ComPort == 0) return FALSE;
121  return (CpGetByte(&Rs232ComPortInfo, ByteReceived, TRUE, FALSE) == CP_GET_SUCCESS);
122 }
123 
124 /*
125 BOOLEAN Rs232PortPollByte(PUCHAR ByteReceived)
126 {
127  if (Rs232ComPort == 0) return FALSE;
128  return (CpGetByte(&Rs232ComPortInfo, ByteReceived, FALSE, FALSE) == CP_GET_SUCCESS);
129 }
130 */
131 
132 VOID Rs232PortPutByte(UCHAR ByteToSend)
133 {
134  if (Rs232ComPort == 0) return;
135  CpPutByte(&Rs232ComPortInfo, ByteToSend);
136 }
137 
138 #endif /* DBG */
139 
141 {
142 #if DBG
143  return ( ((Rs232ComPort != 0) && (Rs232ComPortInfo.Address == Base)) ? TRUE : FALSE );
144 #else
145  return FALSE;
146 #endif
147 }
148 
149 #endif /* not _M_ARM */
BOOLEAN Rs232PortGetByte(PUCHAR ByteReceived)
#define IN
Definition: typedefs.h:39
#define TRUE
Definition: types.h:120
unsigned char * PUCHAR
Definition: retypes.h:3
BOOLEAN Rs232PortInitialize(ULONG ComPort, ULONG BaudRate)
LONG NTSTATUS
Definition: precomp.h:26
_In_opt_ ULONG Base
Definition: rtlfuncs.h:2373
BOOLEAN Rs232PortInUse(PUCHAR Base)
Definition: rs232.c:140
VOID Rs232PortPutByte(UCHAR ByteToSend)
USHORT NTAPI CpGetByte(IN PCPPORT Port, OUT PUCHAR Byte, IN BOOLEAN Wait, IN BOOLEAN Poll)
Definition: cport.c:256
BOOLEAN NTAPI CpDoesPortExist(IN PUCHAR Address)
Definition: cport.c:227
#define FALSE
Definition: types.h:117
unsigned char BOOLEAN
static WCHAR Address[46]
Definition: ping.c:68
#define UlongToPtr(u)
Definition: config.h:106
static struct @4030 Rs232ComPort[]
#define NT_SUCCESS(StatCode)
Definition: apphelp.c:32
VOID NTAPI CpPutByte(IN PCPPORT Port, IN UCHAR Byte)
Definition: cport.c:306
unsigned char UCHAR
Definition: xmlstorage.h:181
Status
Definition: gdiplustypes.h:24
#define MAX_COM_PORTS
Definition: machpc.c:27
NTSTATUS NTAPI CpInitialize(IN PCPPORT Port, IN PUCHAR Address, IN ULONG BaudRate)
Definition: cport.c:88
PUCHAR Address
Definition: cportlib.h:29
static const ULONG BaseArray[]
Definition: hwide.c:41
#define CP_GET_SUCCESS
Definition: cportlib.h:18
#define DEFAULT_BAUD_RATE
Definition: kdcom.c:29
unsigned int ULONG
Definition: retypes.h:1