]> git.proxmox.com Git - mirror_edk2.git/blob - SourceLevelDebugPkg/Library/DebugCommunicationLibSerialPort/DebugCommunicationLibSerialPort.c
SourceLevelDebugPkg: Fix spelling errors
[mirror_edk2.git] / SourceLevelDebugPkg / Library / DebugCommunicationLibSerialPort / DebugCommunicationLibSerialPort.c
1 /** @file
2 Debug Port Library implementation based on serial port.
3
4 Copyright (c) 2010 - 2018, Intel Corporation. All rights reserved.<BR>
5 SPDX-License-Identifier: BSD-2-Clause-Patent
6
7 **/
8
9 #include <Base.h>
10
11 #include <Library/DebugCommunicationLib.h>
12 #include <Library/SerialPortLib.h>
13 #include <Library/DebugLib.h>
14
15 /**
16 Initialize the debug port.
17
18 This function will initialize debug port to get it ready for data transmission. If
19 certain Debug Communication Library instance has to save some private data in the
20 stack, this function must work on the mode that doesn't return to the caller, then
21 the caller needs to wrap up all rest of logic after DebugPortInitialize() into one
22 function and pass it into DebugPortInitialize(). DebugPortInitialize() is
23 responsible to invoke the passing-in function at the end of DebugPortInitialize().
24
25 If the parameter Function is not NULL, Debug Communication Library instance will
26 invoke it by passing in the Context to be the first parameter. Debug Communication
27 Library instance could create one debug port handle to be the second parameter
28 passing into the Function. Debug Communication Library instance also could pass
29 NULL to be the second parameter if it doesn't create the debug port handle.
30
31 If the parameter Function is NULL, and Context is not NULL. At this time, Context
32 is the debug port handle created by the previous Debug Communication Library
33 instance.
34 a) If the instance can understand and continue use the private data of the previous
35 instance, it could return the same handle as passed in (as Context parameter).
36 b) If the instance does not understand, or does not want to continue use the
37 private data of the previous instance, it could ignore the input Context parameter
38 and create the new handle to be returned.
39
40 If Function() is NULL and Context is NULL, Debug Communication Library could create a
41 new handle and return it. NULL is also a valid handle to be returned.
42
43 @param[in] Context Context needed by callback function; it was optional.
44 @param[in] Function Continue function called by Debug Communication library;
45 it was optional.
46
47 @return The debug port handle created by Debug Communication Library if Function
48 is not NULL.
49
50 **/
51 DEBUG_PORT_HANDLE
52 EFIAPI
53 DebugPortInitialize (
54 IN VOID *Context,
55 IN DEBUG_PORT_CONTINUE Function
56 )
57 {
58 RETURN_STATUS Status;
59
60 Status = SerialPortInitialize ();
61 if (RETURN_ERROR (Status)) {
62 DEBUG ((DEBUG_ERROR, "Debug Serial Port: Initialization failed!\n"));
63 }
64
65 if (Function != NULL) {
66 Function (Context, NULL);
67 }
68
69 return NULL;
70 }
71
72 /**
73 Read data from debug device and save the data in a buffer.
74
75 Reads NumberOfBytes data bytes from a debug device into the buffer
76 specified by Buffer. The number of bytes actually read is returned.
77 If the return value is less than NumberOfBytes, then the rest operation failed.
78 If NumberOfBytes is zero, then return 0.
79
80 @param Handle Debug port handle.
81 @param Buffer Pointer to the data buffer to store the data read from the debug device.
82 @param NumberOfBytes Number of bytes which will be read.
83 @param Timeout Timeout value for reading from debug device. It unit is Microsecond.
84
85 @retval 0 Read data failed, no data is to be read.
86 @retval >0 Actual number of bytes read from debug device.
87
88 **/
89 UINTN
90 EFIAPI
91 DebugPortReadBuffer (
92 IN DEBUG_PORT_HANDLE Handle,
93 IN UINT8 *Buffer,
94 IN UINTN NumberOfBytes,
95 IN UINTN Timeout
96 )
97 {
98 if ((NumberOfBytes != 1) || (Buffer == NULL) || (Timeout != 0)) {
99 return 0;
100 }
101
102 return SerialPortRead (Buffer, 1);
103 }
104
105 /**
106 Write data from buffer to debug device.
107
108 Writes NumberOfBytes data bytes from Buffer to the debug device.
109 The number of bytes actually written to the debug device is returned.
110 If the return value is less than NumberOfBytes, then the write operation failed.
111 If NumberOfBytes is zero, then return 0.
112
113 @param Handle Debug port handle.
114 @param Buffer Pointer to the data buffer to be written.
115 @param NumberOfBytes Number of bytes to written to the debug device.
116
117 @retval 0 NumberOfBytes is 0.
118 @retval >0 The number of bytes written to the debug device.
119 If this value is less than NumberOfBytes, then the read operation failed.
120
121 **/
122 UINTN
123 EFIAPI
124 DebugPortWriteBuffer (
125 IN DEBUG_PORT_HANDLE Handle,
126 IN UINT8 *Buffer,
127 IN UINTN NumberOfBytes
128 )
129 {
130 return SerialPortWrite (Buffer, NumberOfBytes);
131 }
132
133 /**
134 Polls a debug device to see if there is any data waiting to be read.
135
136 Polls a debug device to see if there is any data waiting to be read.
137 If there is data waiting to be read from the debug device, then TRUE is returned.
138 If there is no data waiting to be read from the debug device, then FALSE is returned.
139
140 @param Handle Debug port handle.
141
142 @retval TRUE Data is waiting to be read from the debug device.
143 @retval FALSE There is no data waiting to be read from the serial device.
144
145 **/
146 BOOLEAN
147 EFIAPI
148 DebugPortPollBuffer (
149 IN DEBUG_PORT_HANDLE Handle
150 )
151 {
152 return SerialPortPoll ();
153 }