]> git.proxmox.com Git - mirror_edk2.git/blame - ArmPkg/Library/SemiHostingSerialPortLib/SerialPortLib.c
ArmPkg: Fix various typos
[mirror_edk2.git] / ArmPkg / Library / SemiHostingSerialPortLib / SerialPortLib.c
CommitLineData
2ef2b01e
A
1/** @file\r
2 Serial I/O Port library functions with no library constructor/destructor\r
3\r
d6ebcab7 4 Copyright (c) 2008 - 2009, Apple Inc. All rights reserved.<BR>\r
3402aac7 5\r
4059386c 6 SPDX-License-Identifier: BSD-2-Clause-Patent\r
2ef2b01e
A
7\r
8**/\r
9\r
10#include <Uefi.h>\r
11#include <Library/DebugLib.h>\r
12#include <Library/SemihostLib.h>\r
13#include <Library/SerialPortLib.h>\r
14\r
15\r
16/*\r
17\r
18 Programmed hardware of Serial port.\r
19\r
20 @return Always return EFI_UNSUPPORTED.\r
21\r
22**/\r
23RETURN_STATUS\r
24EFIAPI\r
25SerialPortInitialize (\r
26 VOID\r
27 )\r
28{\r
29 if (SemihostConnectionSupported ()) {\r
30 return RETURN_SUCCESS;\r
31 } else {\r
32 return RETURN_UNSUPPORTED;\r
33 }\r
34}\r
35\r
36/**\r
37 Write data to serial device.\r
38\r
39 @param Buffer Point of data buffer which need to be writed.\r
40 @param NumberOfBytes Number of output bytes which are cached in Buffer.\r
41\r
42 @retval 0 Write data failed.\r
43 @retval !0 Actual number of bytes writed to serial device.\r
44\r
45**/\r
46\r
47#define PRINT_BUFFER_SIZE 512\r
48#define PRINT_BUFFER_THRESHOLD (PRINT_BUFFER_SIZE - 4)\r
49\r
50UINTN\r
51EFIAPI\r
52SerialPortWrite (\r
53 IN UINT8 *Buffer,\r
54 IN UINTN NumberOfBytes\r
55)\r
56{\r
57 UINT8 PrintBuffer[PRINT_BUFFER_SIZE];\r
58 UINTN SourceIndex = 0;\r
59 UINTN DestinationIndex = 0;\r
60 UINT8 CurrentCharacter;\r
61\r
62 while (SourceIndex < NumberOfBytes)\r
63 {\r
64 CurrentCharacter = Buffer[SourceIndex++];\r
3402aac7 65\r
2ef2b01e
A
66 switch (CurrentCharacter)\r
67 {\r
68 case '\r':\r
69 continue;\r
70\r
71 case '\n':\r
72 PrintBuffer[DestinationIndex++] = ' ';\r
73 // fall through\r
74\r
75 default:\r
76 PrintBuffer[DestinationIndex++] = CurrentCharacter;\r
77 break;\r
78 }\r
79\r
80 if (DestinationIndex > PRINT_BUFFER_THRESHOLD)\r
81 {\r
82 PrintBuffer[DestinationIndex] = '\0';\r
83 SemihostWriteString ((CHAR8 *) PrintBuffer);\r
84\r
85 DestinationIndex = 0;\r
86 }\r
87 }\r
3402aac7 88\r
2ef2b01e
A
89 if (DestinationIndex > 0)\r
90 {\r
91 PrintBuffer[DestinationIndex] = '\0';\r
92 SemihostWriteString ((CHAR8 *) PrintBuffer);\r
93 }\r
94\r
5f4f1334 95 return NumberOfBytes;\r
2ef2b01e
A
96}\r
97\r
98\r
99/**\r
100 Read data from serial device and save the datas in buffer.\r
101\r
102 @param Buffer Point of data buffer which need to be writed.\r
103 @param NumberOfBytes Number of output bytes which are cached in Buffer.\r
104\r
105 @retval 0 Read data failed.\r
ff5fef14 106 @retval !0 Actual number of bytes read from serial device.\r
2ef2b01e
A
107\r
108**/\r
109UINTN\r
110EFIAPI\r
111SerialPortRead (\r
112 OUT UINT8 *Buffer,\r
113 IN UINTN NumberOfBytes\r
114)\r
115{\r
116 *Buffer = SemihostReadCharacter ();\r
117 return 1;\r
118}\r
119\r
120\r
121\r
122/**\r
ff5fef14 123 Check to see if any data is available to be read from the debug device.\r
2ef2b01e 124\r
ff5fef14
AC
125 @retval TRUE At least one byte of data is available to be read\r
126 @retval FALSE No data is available to be read\r
2ef2b01e
A
127\r
128**/\r
129BOOLEAN\r
130EFIAPI\r
131SerialPortPoll (\r
132 VOID\r
133 )\r
134{\r
135 // Since SemiHosting read character is blocking always say we have a char ready?\r
136 return SemihostConnectionSupported ();\r
137}\r
138\r