2 Serial I/O Port library functions with no library constructor/destructor
4 Copyright (c) 2006 - 2017, Intel Corporation. All rights reserved.<BR>
6 This program and the accompanying materials are licensed and made available under
7 the terms and conditions of the BSD License that accompanies this distribution.
8 The full text of the license may be found at
9 http://opensource.org/licenses/bsd-license.php.
11 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
12 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
16 #include "PlatformSerialPortLib.h"
18 UINT16 gComBase
= 0x3f8;
26 Initialize Serial Port
28 The Baud Rate Divisor registers are programmed and the LCR
29 is used to configure the communications format. Hard coded
30 UART config comes from globals in DebugSerialPlatform lib.
50 Data
= (UINT8
) (gData
- (UINT8
) 5);
53 // Calculate divisor for baud generator
55 Divisor
= 115200 / gBps
;
58 // Set communications format
60 OutputData
= (UINT8
) ((DLAB
<< 7) | ((gBreakSet
<< 6) | ((gParity
<< 3) | ((gStop
<< 2) | Data
))));
61 IoWrite8 (gComBase
+ LCR_OFFSET
, OutputData
);
64 // Configure baud rate
66 IoWrite8 (gComBase
+ BAUD_HIGH_OFFSET
, (UINT8
) (Divisor
>> 8));
67 IoWrite8 (gComBase
+ BAUD_LOW_OFFSET
, (UINT8
) (Divisor
& 0xff));
70 // Switch back to bank 0
72 OutputData
= (UINT8
) ((~DLAB
<< 7) | ((gBreakSet
<< 6) | ((gParity
<< 3) | ((gStop
<< 2) | Data
))));
73 IoWrite8 (gComBase
+ LCR_OFFSET
, OutputData
);
75 return RETURN_SUCCESS
;
79 Common function to initialize UART Serial device and USB Serial device.
88 SerialPortInitialize (
96 return RETURN_SUCCESS
;
100 Write data to serial device.
102 If the buffer is NULL, then return 0;
103 if NumberOfBytes is zero, then return 0.
105 @param Buffer Point of data buffer which need to be writed.
106 @param NumberOfBytes Number of output bytes which are cached in Buffer.
108 @retval 0 Write data failed.
109 @retval !0 Actual number of bytes writed to serial device.
116 IN UINTN NumberOfBytes
122 if (NULL
== Buffer
) {
126 Result
= NumberOfBytes
;
128 while (NumberOfBytes
--) {
130 // Wait for the serial port to be ready.
133 Data
= IoRead8 ((UINT16
) PcdGet64 (PcdSerialRegisterBase
) + LSR_OFFSET
);
134 } while ((Data
& LSR_TXRDY
) == 0);
135 IoWrite8 ((UINT16
) PcdGet64 (PcdSerialRegisterBase
), *Buffer
++);
142 Common function to write data to UART Serial device and USB Serial device.
144 @param Buffer Point of data buffer which need to be writed.
145 @param NumberOfBytes Number of output bytes which are cached in Buffer.
152 IN UINTN NumberOfBytes
155 if (FeaturePcdGet (PcdStatusCodeUseIsaSerial
)) {
156 UARTDbgOut (Buffer
, NumberOfBytes
);
159 return RETURN_SUCCESS
;
163 Read data from serial device and save the datas in buffer.
165 If the buffer is NULL, then return 0;
166 if NumberOfBytes is zero, then return 0.
168 @param Buffer Point of data buffer which need to be writed.
169 @param NumberOfBytes Number of output bytes which are cached in Buffer.
171 @retval 0 Read data failed.
172 @retval !0 Actual number of bytes raed to serial device.
179 IN UINTN NumberOfBytes
185 if (NULL
== Buffer
) {
189 Result
= NumberOfBytes
;
191 while (NumberOfBytes
--) {
193 // Wait for the serial port to be ready.
196 Data
= IoRead8 ((UINT16
) PcdGet64 (PcdSerialRegisterBase
) + LSR_OFFSET
);
197 } while ((Data
& LSR_RXDA
) == 0);
199 *Buffer
++ = IoRead8 ((UINT16
) PcdGet64 (PcdSerialRegisterBase
));
206 Common function to Read data from UART serial device, USB serial device and save the datas in buffer.
208 @param Buffer Point of data buffer which need to be writed.
209 @param NumberOfBytes Number of output bytes which are cached in Buffer.
216 IN UINTN NumberOfBytes
219 if (FeaturePcdGet (PcdStatusCodeUseIsaSerial
)) {
220 UARTDbgIn (Buffer
, NumberOfBytes
);
223 return RETURN_SUCCESS
;
228 Polls a serial device to see if there is any data waiting to be read.
230 Polls aserial device to see if there is any data waiting to be read.
231 If there is data waiting to be read from the serial device, then TRUE is returned.
232 If there is no data waiting to be read from the serial device, then FALSE is returned.
234 @retval TRUE Data is waiting to be read from the serial device.
235 @retval FALSE There is no data waiting to be read from the serial device.
247 // Read the serial port status.
249 Data
= IoRead8 ((UINT16
) PcdGet64 (PcdSerialRegisterBase
) + LSR_OFFSET
);
251 return (BOOLEAN
) ((Data
& LSR_RXDA
) != 0);