]> git.proxmox.com Git - mirror_edk2.git/blob - SourceLevelDebugPkg/Library/DebugCommunicationLibSerialPort/DebugCommunicationLibSerialPort.c
Import SourceLevelDebugPkg.
[mirror_edk2.git] / SourceLevelDebugPkg / Library / DebugCommunicationLibSerialPort / DebugCommunicationLibSerialPort.c
1 /** @file
2 Debug Port Library implementation based on serial port.
3
4 Copyright (c) 2010, Intel Corporation. All rights reserved.<BR>
5 This program and the accompanying materials
6 are licensed and made available under the terms and conditions of the BSD License
7 which accompanies this distribution. The full text of the license may be found at
8 http://opensource.org/licenses/bsd-license.php.
9
10 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
11 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
12
13 **/
14
15 #include <Base.h>
16
17 #include <Library/DebugCommunicationLib.h>
18 #include <Library/SerialPortLib.h>
19 #include <Library/TimerLib.h>
20
21 /**
22 Initialize the debug port.
23
24 This function will initialize debug port to get it ready for data transmition. If
25 certain Debug Communication Library instance has to save some private data in the
26 stack, this function must work on the mode that doesn't return to the caller, then
27 the caller needs to wrap up all rest of logic after DebugPortInitialize() into one
28 function and pass it into DebugPortInitialize(). DebugPortInitialize() is
29 responsible to invoke the passing-in funciton at the end of DebugPortInitialize().
30
31 If the paramter Function is not NULL, Debug Communication Libary instance will
32 invoke it by passing in the Context to be the first parameter. Debug Communication
33 Library instance could create one debug port handle to be the second parameter
34 passing into the Function. Debug Communication Library instance also could pass
35 NULL to be the second parameter if it doesn't create the debug port handle.
36
37 If the parameter Function is NULL, and Context is not NULL. At this time, Context
38 is the debug port handle created by the previous Debug Communication Library
39 instance.
40 a) If the instance can understand and continue use the private data of the previous
41 instance, it could return the same handle as passed in (as Context parameter).
42 b) If the instance does not understand, or does not want to continue use the
43 private data of the previous instance, it could ignore the input Context parameter
44 and create the new hanlde to be returned.
45
46 If Function() is NULL and Context is NULL, Debug Communication Library could create a
47 new handle and return it. NULL is also a valid handle to be returned.
48
49 @param[in] Context Context needed by callback function; it was optional.
50 @param[in] Function Continue function called by Debug Communication library;
51 it was optional.
52
53 @return The debug port handle created by Debug Communication Library if Function
54 is not NULL.
55
56 **/
57 DEBUG_PORT_HANDLE
58 EFIAPI
59 DebugPortInitialize (
60 IN VOID *Context,
61 IN DEBUG_PORT_CONTINUE Function
62 )
63 {
64 SerialPortInitialize ();
65
66 if (Function != NULL) {
67 Function (Context, NULL);
68 }
69
70 return NULL;
71 }
72
73 /**
74 Read data from debug device and save the datas in buffer.
75
76 Reads NumberOfBytes data bytes from a debug device into the buffer
77 specified by Buffer. The number of bytes actually read is returned.
78 If the return value is less than NumberOfBytes, then the rest operation failed.
79 If NumberOfBytes is zero, then return 0.
80
81 @param Handle Debug port handle.
82 @param Buffer Pointer to the data buffer to store the data read from the debug device.
83 @param NumberOfBytes Number of bytes which will be read.
84 @param Timeout Timeout value for reading from debug device. It unit is Microsecond.
85
86 @retval 0 Read data failed, no data is to be read.
87 @retval >0 Actual number of bytes read from debug device.
88
89 **/
90 UINTN
91 EFIAPI
92 DebugPortReadBuffer (
93 IN DEBUG_PORT_HANDLE Handle,
94 IN UINT8 *Buffer,
95 IN UINTN NumberOfBytes,
96 IN UINTN Timeout
97 )
98 {
99 UINTN Index;
100 INTN Elapsed;
101
102 for (Index = 0; Index < NumberOfBytes; Index ++) {
103 Elapsed = (INTN) Timeout;
104 while (TRUE) {
105 if (SerialPortPoll () || Timeout == 0) {
106 SerialPortRead (Buffer + Index, 1);
107 break;
108 }
109 MicroSecondDelay (1000);
110 Elapsed -= 1000;
111 if (Elapsed < 0) {
112 return 0;
113 }
114 }
115 }
116
117 return NumberOfBytes;
118 }
119
120 /**
121 Write data from buffer to debug device.
122
123 Writes NumberOfBytes data bytes from Buffer to the debug device.
124 The number of bytes actually written to the debug device is returned.
125 If the return value is less than NumberOfBytes, then the write operation failed.
126 If NumberOfBytes is zero, then return 0.
127
128 @param Handle Debug port handle.
129 @param Buffer Pointer to the data buffer to be written.
130 @param NumberOfBytes Number of bytes to written to the debug device.
131
132 @retval 0 NumberOfBytes is 0.
133 @retval >0 The number of bytes written to the debug device.
134 If this value is less than NumberOfBytes, then the read operation failed.
135
136 **/
137 UINTN
138 EFIAPI
139 DebugPortWriteBuffer (
140 IN DEBUG_PORT_HANDLE Handle,
141 IN UINT8 *Buffer,
142 IN UINTN NumberOfBytes
143 )
144 {
145 return SerialPortWrite (Buffer, NumberOfBytes);
146 }
147
148 /**
149 Polls a debug device to see if there is any data waiting to be read.
150
151 Polls a debug device to see if there is any data waiting to be read.
152 If there is data waiting to be read from the debug device, then TRUE is returned.
153 If there is no data waiting to be read from the debug device, then FALSE is returned.
154
155 @param Handle Debug port handle.
156
157 @retval TRUE Data is waiting to be read from the debug device.
158 @retval FALSE There is no data waiting to be read from the serial device.
159
160 **/
161 BOOLEAN
162 EFIAPI
163 DebugPortPollBuffer (
164 IN DEBUG_PORT_HANDLE Handle
165 )
166 {
167 return SerialPortPoll ();
168 }
169