FreeNOS
i8250.cpp
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2009 Niek Linnenbank
3  *
4  * This program is free software: you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation, either version 3 of the License, or
7  * (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program. If not, see <http://www.gnu.org/licenses/>.
16  */
17 
18 #include <FreeNOS/User.h>
19 #include <FreeNOS/Config.h>
20 #include <Macros.h>
21 #include <Types.h>
22 #include "i8250.h"
23 
25 {
26  return new i8250(4, 0x3f8);
27 }
28 
29 i8250::i8250(const u32 irq, const u16 base)
30  : SerialDevice(irq)
31 {
32  m_io.setPortBase(base);
33  m_identifier << "serial0";
34 }
35 
37 {
38  // Temporary disable interrupts
39  if (!isKernel)
40  {
42  }
43 
44  // 8bit Words, no parity
45  m_io.outb(LINECONTROL, 3);
46 
47  // Enable interrupts
48  m_io.outb(IRQCONTROL, 1);
49 
50  // No FIFO
51  m_io.outb(FIFOCONTROL, 0);
52 
53  // Data Ready, Request to Send
54  m_io.outb(MODEMCONTROL, 3);
55 
56  // Set baudrate
57  m_io.outb(LINECONTROL, m_io.inb(LINECONTROL) | DLAB);
58  m_io.outb(DIVISORLOW, (11500 / BAUDRATE) & 0xff);
59  m_io.outb(DIVISORHIGH, (11500 / BAUDRATE) >> 8);
60  m_io.outb(LINECONTROL, m_io.inb(LINECONTROL) & ~(DLAB));
61 
62  // Re-Enable interrupts
63  if (!isKernel)
64  {
66  }
67 
68  // Done
69  return FileSystem::Success;
70 }
71 
73 {
75  return FileSystem::Success;
76 }
77 
79  Size & size,
80  const Size offset)
81 {
82  Size bytes = 0;
83  u8 byte;
84 
85  // Read as much bytes as possible
86  while (m_io.inb(LINESTATUS) & RXREADY && bytes < size)
87  {
88  byte = m_io.inb(RECEIVE);
89  buffer.bufferedWrite(&byte, 1);
90  bytes++;
91  }
92 
93  if (bytes)
94  {
95  size = bytes;
96  return FileSystem::Success;
97  }
98  else
99  {
100  return FileSystem::RetryAgain;
101  }
102 }
103 
105  Size & size,
106  const Size offset)
107 {
108  Size bytes = 0;
109 
110  // Write as much bytes as possible
111  while (m_io.inb(LINESTATUS) & TXREADY && bytes < size)
112  {
113  m_io.outb(TRANSMIT, buffer[bytes++]);
114  }
115 
116  if (bytes)
117  {
118  size = bytes;
119  return FileSystem::Success;
120  }
121  else
122  {
123  return FileSystem::RetryAgain;
124  }
125 }
i8250::read
virtual FileSystem::Result read(IOBuffer &buffer, Size &size, const Size offset)
Read bytes from the device.
Definition: i8250.cpp:78
i8250::DIVISORHIGH
@ DIVISORHIGH
Definition: i8250.h:48
Macros.h
Types.h
EnableIRQ
@ EnableIRQ
Definition: ProcessCtl.h:44
SerialDevice::m_io
Arch::IO m_io
I/O instance.
Definition: SerialDevice.h:65
i8250::initialize
virtual FileSystem::Result initialize()
Initializes the i8250 serial UART.
Definition: i8250.cpp:36
i8250::BAUDRATE
@ BAUDRATE
Definition: i8250.h:58
IOBuffer::bufferedWrite
FileSystem::Result bufferedWrite(const void *buffer, const Size size)
Buffered write bytes to the I/O buffer.
Definition: IOBuffer.cpp:146
isKernel
C uint isKernel
Non-zero if this executable is linked as the kernel.
i8250::LINECONTROL
@ LINECONTROL
Definition: i8250.h:53
FileSystem::Success
@ Success
Definition: FileSystem.h:54
ProcessCtl
API::Result ProcessCtl(const ProcessID proc, const ProcessOperation op, const Address addr=0, const Address output=0)
Prototype for user applications.
Definition: ProcessCtl.h:93
i8250::RXREADY
@ RXREADY
Definition: i8250.h:49
i8250::TXREADY
@ TXREADY
Definition: i8250.h:56
i8250::FIFOCONTROL
@ FIFOCONTROL
Definition: i8250.h:52
SELF
#define SELF
Definition: ProcessID.h:35
i8250::DIVISORLOW
@ DIVISORLOW
Definition: i8250.h:47
u16
unsigned short u16
Unsigned 16-bit number.
Definition: Types.h:56
IOBuffer
Abstract Input/Output buffer.
Definition: IOBuffer.h:37
u32
unsigned int u32
Unsigned 32-bit number.
Definition: Types.h:53
Size
unsigned int Size
Any sane size indicator cannot go negative.
Definition: Types.h:128
AbstractFactory::create
static T * create()
Abstract function to create an instance of T.
i8250::DLAB
@ DLAB
Definition: i8250.h:57
SerialDevice
Provides sequential byte stream of incoming (RX) and outgoing (TX) data.
Definition: SerialDevice.h:37
Device::m_identifier
String m_identifier
Unique identifier for this Device.
Definition: Device.h:79
i8250::RECEIVE
@ RECEIVE
Definition: i8250.h:46
FileSystem::Result
Result
Result code for filesystem Actions.
Definition: FileSystem.h:52
u8
unsigned char u8
Unsigned 8-bit number.
Definition: Types.h:59
i8250::interrupt
virtual FileSystem::Result interrupt(const Size vector)
Called when an interrupt has been triggered for this device.
Definition: i8250.cpp:72
FileSystem::RetryAgain
@ RetryAgain
Definition: FileSystem.h:57
i8250::i8250
i8250(const u32 irq, const u16 base)
Constructor function.
Definition: i8250.cpp:29
i8250::MODEMCONTROL
@ MODEMCONTROL
Definition: i8250.h:54
i8250.h
i8250::TRANSMIT
@ TRANSMIT
Definition: i8250.h:45
i8250::LINESTATUS
@ LINESTATUS
Definition: i8250.h:55
DisableIRQ
@ DisableIRQ
Definition: ProcessCtl.h:45
SerialDevice::m_irq
const u32 m_irq
interrupt vector
Definition: SerialDevice.h:62
i8250::write
virtual FileSystem::Result write(IOBuffer &buffer, Size &size, const Size offset)
Write bytes to the device.
Definition: i8250.cpp:104
i8250::IRQCONTROL
@ IRQCONTROL
Definition: i8250.h:50
i8250
i8250 serial UART.
Definition: i8250.h:36