The Pedigree Project  0.1
mach_pc/Serial.cc
1 /*
2  * Copyright (c) 2008-2014, Pedigree Developers
3  *
4  * Please see the CONTRIB file in the root of the source tree for a full
5  * list of contributors.
6  *
7  * Permission to use, copy, modify, and distribute this software for any
8  * purpose with or without fee is hereby granted, provided that the above
9  * copyright notice and this permission notice appear in all copies.
10  *
11  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
12  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
13  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
14  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
15  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
16  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
17  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
18  */
19 
20 #include "Serial.h"
21 #include "pedigree/kernel/Log.h"
22 
23 X86Serial::X86Serial() : m_Port("COM")
24 {
25 }
26 
27 X86Serial::~X86Serial()
28 {
29 }
30 
31 void X86Serial::setBase(uintptr_t nBaseAddr)
32 {
33  m_Port.allocate(nBaseAddr, 8);
34 
35  m_Port.write8(0x00, serial::inten); // Disable all interrupts
36  m_Port.write8(0x80, serial::lctrl); // Enable DLAB (set baud rate divisor)
37  m_Port.write8(
38  0x01, serial::rxtx); // Set divisor to 3 (lo byte) 115200 baud
39  m_Port.write8(0x00, serial::inten); // (hi byte)
40  m_Port.write8(0x03, serial::lctrl); // 8 bits, no parity, one stop bit
41  m_Port.write8(
42  0xC7,
43  serial::iififo); // Enable FIFO, clear them, with 14-byte threshold
44  m_Port.write8(0x0B, serial::mctrl); // IRQs enabled, RTS/DSR set
45  m_Port.write8(0x0C, serial::inten); // enable all interrupts.
46 
47  NOTICE("Modem status: " << Hex << m_Port.read8(serial::mstat));
48  NOTICE("Line status: " << Hex << m_Port.read8(serial::lstat));
49 }
50 
51 char X86Serial::read()
52 {
53  if (!isConnected())
54  return 0;
55  while (!(m_Port.read8(serial::lstat) & 0x1))
56  ;
57 
58  return m_Port.read8(serial::rxtx);
59 }
60 
61 char X86Serial::readNonBlock()
62 {
63  if (!isConnected())
64  return 0;
65  if (m_Port.read8(serial::lstat) & 0x1)
66  return m_Port.read8(serial::rxtx);
67  else
68  return '\0';
69 }
70 
71 void X86Serial::write(char c)
72 {
73  if (!isConnected())
74  return;
75  while (!(m_Port.read8(serial::lstat) & 0x20))
76  ;
77 
78  m_Port.write8(static_cast<unsigned char>(c), serial::rxtx);
79 }
80 
81 bool X86Serial::isConnected()
82 {
83  return true;
84  /*
85  uint8_t nStatus = m_Port.read8(serial::mstat);
86  // Bits 0x30 = Clear to send & Data set ready.
87  // Mstat seems to be 0xFF when the device isn't present.
88  if ((nStatus & 0x30) && nStatus != 0xFF)
89  return true;
90  else
91  return false;
92  */
93 }
virtual void setBase(uintptr_t nBaseAddr)
#define NOTICE(text)
Definition: Log.h:74
Definition: Log.h:136