]> git.proxmox.com Git - mirror_edk2.git/blob - EmbeddedPkg/Library/SemiHostingSerialPortLib/SerialPortLib.c
Adding support for BeagleBoard.
[mirror_edk2.git] / EmbeddedPkg / Library / SemiHostingSerialPortLib / SerialPortLib.c
1 /** @file
2 Serial I/O Port library functions with no library constructor/destructor
3
4
5 Copyright (c) 2008-2009, Apple Inc. All rights reserved.
6
7 All rights reserved. This program and the accompanying materials
8 are licensed and made available under the terms and conditions of the BSD License
9 which accompanies this distribution. The full text of the license may be found at
10 http://opensource.org/licenses/bsd-license.php
11
12 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
13 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
14
15 **/
16
17 #include <Uefi.h>
18 #include <Library/DebugLib.h>
19 #include <Library/SemihostLib.h>
20 #include <Library/SerialPortLib.h>
21
22
23 /*
24
25 Programmed hardware of Serial port.
26
27 @return Always return EFI_UNSUPPORTED.
28
29 **/
30 RETURN_STATUS
31 EFIAPI
32 SerialPortInitialize (
33 VOID
34 )
35 {
36 if (SemihostConnectionSupported ()) {
37 return RETURN_SUCCESS;
38 } else {
39 return RETURN_UNSUPPORTED;
40 }
41 }
42
43 /**
44 Write data to serial device.
45
46 @param Buffer Point of data buffer which need to be writed.
47 @param NumberOfBytes Number of output bytes which are cached in Buffer.
48
49 @retval 0 Write data failed.
50 @retval !0 Actual number of bytes writed to serial device.
51
52 **/
53
54 #define PRINT_BUFFER_SIZE 512
55 #define PRINT_BUFFER_THRESHOLD (PRINT_BUFFER_SIZE - 4)
56
57 UINTN
58 EFIAPI
59 SerialPortWrite (
60 IN UINT8 *Buffer,
61 IN UINTN NumberOfBytes
62 )
63 {
64 UINT8 PrintBuffer[PRINT_BUFFER_SIZE];
65 UINTN SourceIndex = 0;
66 UINTN DestinationIndex = 0;
67 UINT8 CurrentCharacter;
68
69 while (SourceIndex < NumberOfBytes)
70 {
71 CurrentCharacter = Buffer[SourceIndex++];
72
73 switch (CurrentCharacter)
74 {
75 case '\r':
76 continue;
77
78 case '\n':
79 PrintBuffer[DestinationIndex++] = ' ';
80 // fall through
81
82 default:
83 PrintBuffer[DestinationIndex++] = CurrentCharacter;
84 break;
85 }
86
87 if (DestinationIndex > PRINT_BUFFER_THRESHOLD)
88 {
89 PrintBuffer[DestinationIndex] = '\0';
90 SemihostWriteString ((CHAR8 *) PrintBuffer);
91
92 DestinationIndex = 0;
93 }
94 }
95
96 if (DestinationIndex > 0)
97 {
98 PrintBuffer[DestinationIndex] = '\0';
99 SemihostWriteString ((CHAR8 *) PrintBuffer);
100 }
101
102 return 0;
103 }
104
105
106 /**
107 Read data from serial device and save the datas in buffer.
108
109 @param Buffer Point of data buffer which need to be writed.
110 @param NumberOfBytes Number of output bytes which are cached in Buffer.
111
112 @retval 0 Read data failed.
113 @retval !0 Aactual number of bytes read from serial device.
114
115 **/
116 UINTN
117 EFIAPI
118 SerialPortRead (
119 OUT UINT8 *Buffer,
120 IN UINTN NumberOfBytes
121 )
122 {
123 *Buffer = SemihostReadCharacter ();
124 return 1;
125 }
126
127
128
129 /**
130 Check to see if any data is avaiable to be read from the debug device.
131
132 @retval TRUE At least one byte of data is avaiable to be read
133 @retval FALS No data is avaiable to be read
134
135 **/
136 BOOLEAN
137 EFIAPI
138 SerialPortPoll (
139 VOID
140 )
141 {
142 // Since SemiHosting read character is blocking always say we have a char ready?
143 return SemihostConnectionSupported ();
144 }
145