]> git.proxmox.com Git - mirror_edk2.git/blame_incremental - DuetPkg/Library/DuetSerialIoLib/SerialPortLib.c
Implement Read() and Poll()
[mirror_edk2.git] / DuetPkg / Library / DuetSerialIoLib / SerialPortLib.c
... / ...
CommitLineData
1/** @file\r
2 UART Serial Port library instance with empty functions.\r
3\r
4 Copyright (c) 2006 - 2008, Intel Corporation\r
5 All rights reserved. This program and the accompanying materials\r
6 are licensed and made available under the terms and conditions of the BSD License\r
7 which accompanies this distribution. The full text of the license may be found at\r
8 http://opensource.org/licenses/bsd-license.php\r
9\r
10 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,\r
11 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.\r
12\r
13**/\r
14\r
15#include <Base.h>\r
16\r
17#include <Library/SerialPortLib.h>\r
18#include <Library/IoLib.h>\r
19\r
20//---------------------------------------------\r
21// UART Register Offsets\r
22//---------------------------------------------\r
23#define BAUD_LOW_OFFSET 0x00\r
24#define BAUD_HIGH_OFFSET 0x01\r
25#define IER_OFFSET 0x01\r
26#define LCR_SHADOW_OFFSET 0x01\r
27#define FCR_SHADOW_OFFSET 0x02\r
28#define IR_CONTROL_OFFSET 0x02\r
29#define FCR_OFFSET 0x02\r
30#define EIR_OFFSET 0x02\r
31#define BSR_OFFSET 0x03\r
32#define LCR_OFFSET 0x03\r
33#define MCR_OFFSET 0x04\r
34#define LSR_OFFSET 0x05\r
35#define MSR_OFFSET 0x06\r
36\r
37//---------------------------------------------\r
38// UART Register Bit Defines\r
39//---------------------------------------------\r
40#define LSR_TXRDY 0x20\r
41#define LSR_RXDA 0x01\r
42#define DLAB 0x01\r
43\r
44//---------------------------------------------\r
45// UART Settings\r
46//---------------------------------------------\r
47UINT16 gUartBase = 0x3F8;\r
48UINTN gBps = 115200;\r
49UINT8 gData = 8;\r
50UINT8 gStop = 1;\r
51UINT8 gParity = 0;\r
52UINT8 gBreakSet = 0;\r
53\r
54/**\r
55 Initialize the serial device hardware.\r
56 \r
57 If no initialization is required, then return RETURN_SUCCESS.\r
58 If the serial device was successfuly initialized, then return RETURN_SUCCESS.\r
59 If the serial device could not be initialized, then return RETURN_DEVICE_ERROR.\r
60 \r
61 @retval RETURN_SUCCESS The serial device was initialized.\r
62 @retval RETURN_DEVICE_ERROR The serail device could not be initialized.\r
63\r
64**/\r
65RETURN_STATUS\r
66EFIAPI\r
67SerialPortInitialize (\r
68 VOID\r
69 )\r
70{\r
71 UINTN Divisor;\r
72 UINT8 OutputData;\r
73 UINT8 Data;\r
74\r
75 //\r
76 // Map 5..8 to 0..3\r
77 //\r
78 Data = (UINT8) (gData - (UINT8) 5);\r
79\r
80 //\r
81 // Calculate divisor for baud generator\r
82 //\r
83 Divisor = 115200 / gBps;\r
84 \r
85 //\r
86 // Set communications format\r
87 //\r
88 OutputData = (UINT8) ((DLAB << 7) | (gBreakSet << 6) | (gParity << 3) | (gStop << 2) | Data);\r
89 IoWrite8 ((UINTN) (gUartBase + LCR_OFFSET), OutputData);\r
90\r
91 //\r
92 // Configure baud rate\r
93 //\r
94 IoWrite8 ((UINTN) (gUartBase + BAUD_HIGH_OFFSET), (UINT8) (Divisor >> 8));\r
95 IoWrite8 ((UINTN) (gUartBase + BAUD_LOW_OFFSET), (UINT8) (Divisor & 0xff));\r
96\r
97 //\r
98 // Switch back to bank 0\r
99 //\r
100 OutputData = (UINT8) ((~DLAB << 7) | (gBreakSet << 6) | (gParity << 3) | (gStop << 2) | Data);\r
101 IoWrite8 ((UINTN) (gUartBase + LCR_OFFSET), OutputData);\r
102\r
103 return RETURN_SUCCESS;\r
104}\r
105\r
106/**\r
107 Write data from buffer to serial device. \r
108 \r
109 Writes NumberOfBytes data bytes from Buffer to the serial device. \r
110 The number of bytes actually written to the serial device is returned.\r
111 If the return value is less than NumberOfBytes, then the write operation failed.\r
112\r
113 If Buffer is NULL, then ASSERT(). \r
114\r
115 If NumberOfBytes is zero, then return 0.\r
116\r
117 @param Buffer Pointer to the data buffer to be written.\r
118 @param NumberOfBytes Number of bytes to written to the serial device.\r
119\r
120 @retval 0 NumberOfBytes is 0.\r
121 @retval >0 The number of bytes written to the serial device. \r
122 If this value is less than NumberOfBytes, then the read operation failed.\r
123\r
124**/\r
125UINTN\r
126EFIAPI\r
127SerialPortWrite (\r
128 IN UINT8 *Buffer,\r
129 IN UINTN NumberOfBytes\r
130)\r
131{\r
132 UINTN Result;\r
133 UINT8 Data;\r
134\r
135 if (Buffer == NULL) {\r
136 return 0;\r
137 }\r
138\r
139 Result = NumberOfBytes;\r
140\r
141 while (NumberOfBytes--) {\r
142 //\r
143 // Wait for the serail port to be ready.\r
144 //\r
145 do {\r
146 Data = IoRead8 ((UINT16) gUartBase + LSR_OFFSET);\r
147 } while ((Data & LSR_TXRDY) == 0);\r
148 IoWrite8 ((UINT16) gUartBase, *Buffer++);\r
149 }\r
150\r
151 return Result;\r
152}\r
153\r
154\r
155/**\r
156 Reads data from a serial device into a buffer.\r
157\r
158 @param Buffer Pointer to the data buffer to store the data read from the serial device.\r
159 @param NumberOfBytes Number of bytes to read from the serial device.\r
160\r
161 @retval 0 NumberOfBytes is 0.\r
162 @retval >0 The number of bytes read from the serial device. \r
163 If this value is less than NumberOfBytes, then the read operation failed.\r
164\r
165**/\r
166UINTN\r
167EFIAPI\r
168SerialPortRead (\r
169 OUT UINT8 *Buffer,\r
170 IN UINTN NumberOfBytes\r
171)\r
172{\r
173 UINTN Result;\r
174 UINT8 Data;\r
175\r
176 if (NULL == Buffer) {\r
177 return 0;\r
178 }\r
179\r
180 Result = NumberOfBytes;\r
181\r
182 while (NumberOfBytes--) {\r
183 //\r
184 // Wait for the serail port to be ready.\r
185 //\r
186 do {\r
187 Data = IoRead8 ((UINT16) gUartBase + LSR_OFFSET);\r
188 } while ((Data & LSR_RXDA) == 0);\r
189\r
190 *Buffer++ = IoRead8 ((UINT16) gUartBase);\r
191 }\r
192\r
193 return Result;\r
194}\r
195\r
196/**\r
197 Polls a serial device to see if there is any data waiting to be read.\r
198\r
199 Polls aserial device to see if there is any data waiting to be read.\r
200 If there is data waiting to be read from the serial device, then TRUE is returned.\r
201 If there is no data waiting to be read from the serial device, then FALSE is returned.\r
202\r
203 @retval TRUE Data is waiting to be read from the serial device.\r
204 @retval FALSE There is no data waiting to be read from the serial device.\r
205\r
206**/\r
207BOOLEAN\r
208EFIAPI\r
209SerialPortPoll (\r
210 VOID\r
211 )\r
212{\r
213 UINT8 Data;\r
214\r
215 //\r
216 // Read the serial port status.\r
217 //\r
218 Data = IoRead8 ((UINT16) gUartBase + LSR_OFFSET);\r
219\r
220 return (BOOLEAN) ((Data & LSR_RXDA) != 0);\r
221}\r
222\r