2009-12-06 02:57:05 +01:00
|
|
|
/** @file
|
|
|
|
Serial I/O Port library functions with no library constructor/destructor
|
|
|
|
|
2010-04-29 14:15:47 +02:00
|
|
|
Copyright (c) 2008 - 2009, Apple Inc. All rights reserved.<BR>
|
2020-12-10 14:13:23 +01:00
|
|
|
Copyright (c) 2021, Arm Limited. All rights reserved.<BR>
|
2014-08-19 15:29:52 +02:00
|
|
|
|
2019-04-04 01:03:18 +02:00
|
|
|
SPDX-License-Identifier: BSD-2-Clause-Patent
|
2009-12-06 02:57:05 +01:00
|
|
|
|
|
|
|
**/
|
|
|
|
|
|
|
|
#include <Uefi.h>
|
|
|
|
#include <Library/DebugLib.h>
|
|
|
|
#include <Library/SemihostLib.h>
|
|
|
|
#include <Library/SerialPortLib.h>
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
|
|
|
Programmed hardware of Serial port.
|
|
|
|
|
|
|
|
@return Always return EFI_UNSUPPORTED.
|
|
|
|
|
|
|
|
**/
|
|
|
|
RETURN_STATUS
|
|
|
|
EFIAPI
|
|
|
|
SerialPortInitialize (
|
|
|
|
VOID
|
|
|
|
)
|
|
|
|
{
|
|
|
|
if (SemihostConnectionSupported ()) {
|
|
|
|
return RETURN_SUCCESS;
|
|
|
|
} else {
|
|
|
|
return RETURN_UNSUPPORTED;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
Write data to serial device.
|
|
|
|
|
2021-04-20 16:25:17 +02:00
|
|
|
@param Buffer Point of data buffer which need to be written.
|
2009-12-06 02:57:05 +01:00
|
|
|
@param NumberOfBytes Number of output bytes which are cached in Buffer.
|
|
|
|
|
|
|
|
@retval 0 Write data failed.
|
2021-04-20 16:25:17 +02:00
|
|
|
@retval !0 Actual number of bytes written to serial device.
|
2009-12-06 02:57:05 +01:00
|
|
|
|
|
|
|
**/
|
|
|
|
|
|
|
|
#define PRINT_BUFFER_SIZE 512
|
|
|
|
#define PRINT_BUFFER_THRESHOLD (PRINT_BUFFER_SIZE - 4)
|
|
|
|
|
|
|
|
UINTN
|
|
|
|
EFIAPI
|
|
|
|
SerialPortWrite (
|
|
|
|
IN UINT8 *Buffer,
|
|
|
|
IN UINTN NumberOfBytes
|
|
|
|
)
|
|
|
|
{
|
|
|
|
UINT8 PrintBuffer[PRINT_BUFFER_SIZE];
|
2020-12-10 14:13:23 +01:00
|
|
|
UINTN SourceIndex;
|
|
|
|
UINTN DestinationIndex;
|
2009-12-06 02:57:05 +01:00
|
|
|
UINT8 CurrentCharacter;
|
|
|
|
|
2020-12-10 14:13:23 +01:00
|
|
|
SourceIndex = 0;
|
|
|
|
DestinationIndex = 0;
|
|
|
|
|
2009-12-06 02:57:05 +01:00
|
|
|
while (SourceIndex < NumberOfBytes) {
|
|
|
|
CurrentCharacter = Buffer[SourceIndex++];
|
2014-08-19 15:29:52 +02:00
|
|
|
|
2009-12-06 02:57:05 +01:00
|
|
|
switch (CurrentCharacter) {
|
|
|
|
case '\r':
|
|
|
|
continue;
|
|
|
|
|
|
|
|
case '\n':
|
|
|
|
PrintBuffer[DestinationIndex++] = ' ';
|
|
|
|
// fall through
|
|
|
|
|
|
|
|
default:
|
|
|
|
PrintBuffer[DestinationIndex++] = CurrentCharacter;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (DestinationIndex > PRINT_BUFFER_THRESHOLD) {
|
|
|
|
PrintBuffer[DestinationIndex] = '\0';
|
|
|
|
SemihostWriteString ((CHAR8 *)PrintBuffer);
|
|
|
|
|
|
|
|
DestinationIndex = 0;
|
|
|
|
}
|
|
|
|
}
|
2014-08-19 15:29:52 +02:00
|
|
|
|
2009-12-06 02:57:05 +01:00
|
|
|
if (DestinationIndex > 0) {
|
|
|
|
PrintBuffer[DestinationIndex] = '\0';
|
|
|
|
SemihostWriteString ((CHAR8 *)PrintBuffer);
|
|
|
|
}
|
|
|
|
|
2011-07-19 17:01:27 +02:00
|
|
|
return NumberOfBytes;
|
2009-12-06 02:57:05 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
Read data from serial device and save the datas in buffer.
|
|
|
|
|
2021-04-20 16:25:17 +02:00
|
|
|
@param Buffer Point of data buffer which need to be written.
|
2009-12-06 02:57:05 +01:00
|
|
|
@param NumberOfBytes Number of output bytes which are cached in Buffer.
|
|
|
|
|
|
|
|
@retval 0 Read data failed.
|
2019-02-06 16:39:35 +01:00
|
|
|
@retval !0 Actual number of bytes read from serial device.
|
2009-12-06 02:57:05 +01:00
|
|
|
|
|
|
|
**/
|
|
|
|
UINTN
|
|
|
|
EFIAPI
|
|
|
|
SerialPortRead (
|
|
|
|
OUT UINT8 *Buffer,
|
|
|
|
IN UINTN NumberOfBytes
|
|
|
|
)
|
|
|
|
{
|
|
|
|
*Buffer = SemihostReadCharacter ();
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2019-02-06 16:39:35 +01:00
|
|
|
Check to see if any data is available to be read from the debug device.
|
2009-12-06 02:57:05 +01:00
|
|
|
|
2019-02-06 16:39:35 +01:00
|
|
|
@retval TRUE At least one byte of data is available to be read
|
|
|
|
@retval FALSE No data is available to be read
|
2009-12-06 02:57:05 +01:00
|
|
|
|
|
|
|
**/
|
|
|
|
BOOLEAN
|
|
|
|
EFIAPI
|
|
|
|
SerialPortPoll (
|
|
|
|
VOID
|
|
|
|
)
|
|
|
|
{
|
|
|
|
// Since SemiHosting read character is blocking always say we have a char ready?
|
|
|
|
return SemihostConnectionSupported ();
|
|
|
|
}
|