]> git.proxmox.com Git - mirror_edk2.git/commitdiff
Implement Read() and Poll()
authormdkinney <mdkinney@6f19259b-4bc3-4df7-8a09-765794883524>
Thu, 30 Oct 2008 05:30:00 +0000 (05:30 +0000)
committermdkinney <mdkinney@6f19259b-4bc3-4df7-8a09-765794883524>
Thu, 30 Oct 2008 05:30:00 +0000 (05:30 +0000)
git-svn-id: https://edk2.svn.sourceforge.net/svnroot/edk2/trunk/edk2@6294 6f19259b-4bc3-4df7-8a09-765794883524

DuetPkg/Library/DuetSerialIoLib/SerialPortLib.c

index 518172ae50c9d56d2490b04fe4e1413c4780c6fd..f0cdc4982f3ab72312b17b0b2b5ef19988f27ac1 100644 (file)
@@ -1,7 +1,7 @@
 /** @file\r
-  Serial I/O Port library functions with no library constructor/destructor\r
+  UART Serial Port library instance with empty functions.\r
 \r
-  Copyright (c) 2006, Intel Corporation\r
+  Copyright (c) 2006 - 2008, Intel Corporation\r
   All rights reserved. This program and the accompanying materials\r
   are licensed and made available under the terms and conditions of the BSD License\r
   which accompanies this distribution.  The full text of the license may be found at\r
 \r
 **/\r
 \r
-\r
 #include <Base.h>\r
 \r
-#include <Library/IoLib.h>\r
 #include <Library/SerialPortLib.h>\r
+#include <Library/IoLib.h>\r
 \r
 //---------------------------------------------\r
 // UART Register Offsets\r
 #define LSR_RXDA                0x01\r
 #define DLAB                    0x01\r
 \r
-UINT16  gComBase = 0x3f8;\r
-UINTN   gBps = 115200;\r
-UINT8   gData = 8;\r
-UINT8   gStop = 1;\r
-UINT8   gParity = 0;\r
+//---------------------------------------------\r
+// UART Settings\r
+//---------------------------------------------\r
+UINT16  gUartBase = 0x3F8;\r
+UINTN   gBps      = 115200;\r
+UINT8   gData     = 8;\r
+UINT8   gStop     = 1;\r
+UINT8   gParity   = 0;\r
 UINT8   gBreakSet = 0;\r
-/*\r
 \r
-  Programmed hardware of Serial port.\r
-\r
-  @return    Always return EFI_UNSUPPORTED.\r
+/**\r
+  Initialize the serial device hardware.\r
+  \r
+  If no initialization is required, then return RETURN_SUCCESS.\r
+  If the serial device was successfuly initialized, then return RETURN_SUCCESS.\r
+  If the serial device could not be initialized, then return RETURN_DEVICE_ERROR.\r
+  \r
+  @retval RETURN_SUCCESS        The serial device was initialized.\r
+  @retval RETURN_DEVICE_ERROR   The serail device could not be initialized.\r
 \r
 **/\r
 RETURN_STATUS\r
@@ -61,49 +68,58 @@ SerialPortInitialize (
   VOID\r
   )\r
 {\r
-  UINTN           Divisor;\r
-  UINT8           OutputData;\r
-  UINT8           Data;\r
+  UINTN  Divisor;\r
+  UINT8  OutputData;\r
+  UINT8  Data;\r
 \r
- //\r
 //\r
   // Map 5..8 to 0..3\r
   //\r
-  Data = (UINT8) (gData - (UINT8)5);\r
+  Data = (UINT8) (gData - (UINT8) 5);\r
 \r
   //\r
   // Calculate divisor for baud generator\r
   //\r
-  Divisor = 115200 / gBps; \r
+  Divisor = 115200 / gBps;\r
   \r
   //\r
   // Set communications format\r
   //\r
-  OutputData = (UINT8)((DLAB << 7) | ((gBreakSet << 6) | ((gParity << 3) | ((gStop << 2) | Data))));\r
-  IoWrite8 (gComBase + LCR_OFFSET, OutputData);\r
+  OutputData = (UINT8) ((DLAB << 7) | (gBreakSet << 6) | (gParity << 3) | (gStop << 2) | Data);\r
+  IoWrite8 ((UINTN) (gUartBase + LCR_OFFSET), OutputData);\r
 \r
   //\r
   // Configure baud rate\r
   //\r
-  IoWrite8 (gComBase + BAUD_HIGH_OFFSET, (UINT8)(Divisor >> 8));\r
-  IoWrite8 (gComBase + BAUD_LOW_OFFSET, (UINT8)(Divisor & 0xff));\r
+  IoWrite8 ((UINTN) (gUartBase + BAUD_HIGH_OFFSET), (UINT8) (Divisor >> 8));\r
+  IoWrite8 ((UINTN) (gUartBase + BAUD_LOW_OFFSET), (UINT8) (Divisor & 0xff));\r
 \r
   //\r
   // Switch back to bank 0\r
   //\r
-  OutputData = (UINT8)((~DLAB<<7)|((gBreakSet<<6)|((gParity<<3)|((gStop<<2)| Data))));\r
-  IoWrite8 (gComBase + LCR_OFFSET, OutputData);\r
+  OutputData = (UINT8) ((~DLAB << 7) | (gBreakSet << 6) | (gParity << 3) | (gStop << 2) | Data);\r
+  IoWrite8 ((UINTN) (gUartBase + LCR_OFFSET), OutputData);\r
 \r
   return RETURN_SUCCESS;\r
 }\r
 \r
 /**\r
-  Write data to serial device.\r
+  Write data from buffer to serial device. \r
\r
+  Writes NumberOfBytes data bytes from Buffer to the serial device.  \r
+  The number of bytes actually written to the serial device is returned.\r
+  If the return value is less than NumberOfBytes, then the write operation failed.\r
 \r
-  @param  Buffer           Point of data buffer which need to be writed.\r
-  @param  NumberOfBytes    Number of output bytes which are cached in Buffer.\r
+  If Buffer is NULL, then ASSERT(). \r
 \r
-  @retval 0                Write data failed.\r
-  @retval !0               Actual number of bytes writed to serial device.\r
+  If NumberOfBytes is zero, then return 0.\r
+\r
+  @param  Buffer           Pointer to the data buffer to be written.\r
+  @param  NumberOfBytes    Number of bytes to written to the serial device.\r
+\r
+  @retval 0                NumberOfBytes is 0.\r
+  @retval >0               The number of bytes written to the serial device.  \r
+                           If this value is less than NumberOfBytes, then the read operation failed.\r
 \r
 **/\r
 UINTN\r
@@ -113,39 +129,38 @@ SerialPortWrite (
   IN UINTN     NumberOfBytes\r
 )\r
 {\r
-  UINTN Result;\r
-  UINT8 Data;\r
+  UINTN  Result;\r
+  UINT8  Data;\r
 \r
-  if (NULL == Buffer) {\r
+  if (Buffer == NULL) {\r
     return 0;\r
   }\r
 \r
   Result = NumberOfBytes;\r
 \r
   while (NumberOfBytes--) {\r
-      //\r
-      // Wait for the serail port to be ready.\r
-      //\r
-      do {\r
-        Data = IoRead8 (gComBase + LSR_OFFSET);\r
-      } while ((Data & LSR_TXRDY) == 0);\r
-\r
-      IoWrite8 (gComBase, *Buffer++);\r
+    //\r
+    // Wait for the serail port to be ready.\r
+    //\r
+    do {\r
+      Data = IoRead8 ((UINT16) gUartBase + LSR_OFFSET);\r
+    } while ((Data & LSR_TXRDY) == 0);\r
+    IoWrite8 ((UINT16) gUartBase, *Buffer++);\r
   }\r
 \r
   return Result;\r
-\r
 }\r
 \r
 \r
 /**\r
-  Read data from serial device and save the datas in buffer.\r
+  Reads data from a serial device into a buffer.\r
 \r
-  @param  Buffer           Point of data buffer which need to be writed.\r
-  @param  NumberOfBytes    Number of output bytes which are cached in Buffer.\r
+  @param  Buffer           Pointer to the data buffer to store the data read from the serial device.\r
+  @param  NumberOfBytes    Number of bytes to read from the serial device.\r
 \r
-  @retval 0                Read data failed.\r
-  @retval !0               Aactual number of bytes read from serial device.\r
+  @retval 0                NumberOfBytes is 0.\r
+  @retval >0               The number of bytes read from the serial device.  \r
+                           If this value is less than NumberOfBytes, then the read operation failed.\r
 \r
 **/\r
 UINTN\r
@@ -155,6 +170,53 @@ SerialPortRead (
   IN  UINTN     NumberOfBytes\r
 )\r
 {\r
-  return 0;\r
+  UINTN  Result;\r
+  UINT8  Data;\r
+\r
+  if (NULL == Buffer) {\r
+    return 0;\r
+  }\r
+\r
+  Result = NumberOfBytes;\r
+\r
+  while (NumberOfBytes--) {\r
+    //\r
+    // Wait for the serail port to be ready.\r
+    //\r
+    do {\r
+      Data = IoRead8 ((UINT16) gUartBase + LSR_OFFSET);\r
+    } while ((Data & LSR_RXDA) == 0);\r
+\r
+    *Buffer++ = IoRead8 ((UINT16) gUartBase);\r
+  }\r
+\r
+  return Result;\r
+}\r
+\r
+/**\r
+  Polls a serial device to see if there is any data waiting to be read.\r
+\r
+  Polls aserial device to see if there is any data waiting to be read.\r
+  If there is data waiting to be read from the serial device, then TRUE is returned.\r
+  If there is no data waiting to be read from the serial device, then FALSE is returned.\r
+\r
+  @retval TRUE             Data is waiting to be read from the serial device.\r
+  @retval FALSE            There is no data waiting to be read from the serial device.\r
+\r
+**/\r
+BOOLEAN\r
+EFIAPI\r
+SerialPortPoll (\r
+  VOID\r
+  )\r
+{\r
+  UINT8  Data;\r
+\r
+  //\r
+  // Read the serial port status.\r
+  //\r
+  Data = IoRead8 ((UINT16) gUartBase + LSR_OFFSET);\r
+\r
+  return (BOOLEAN) ((Data & LSR_RXDA) != 0);\r
 }\r
 \r