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
29i8250::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
70}
71
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;
97 }
98 else
99 {
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 {
124 }
125}
static T * create()
Abstract function to create an instance of T.
String m_identifier
Unique identifier for this Device.
Definition Device.h:79
Abstract Input/Output buffer.
Definition IOBuffer.h:38
FileSystem::Result bufferedWrite(const void *buffer, const Size size)
Buffered write bytes to the I/O buffer.
Definition IOBuffer.cpp:146
Provides sequential byte stream of incoming (RX) and outgoing (TX) data.
const u32 m_irq
interrupt vector
Arch::IO m_io
I/O instance.
i8250 serial UART.
Definition i8250.h:37
@ BAUDRATE
Definition i8250.h:58
@ DIVISORHIGH
Definition i8250.h:48
@ MODEMCONTROL
Definition i8250.h:54
@ FIFOCONTROL
Definition i8250.h:52
@ DIVISORLOW
Definition i8250.h:47
@ TXREADY
Definition i8250.h:56
@ LINECONTROL
Definition i8250.h:53
@ DLAB
Definition i8250.h:57
@ LINESTATUS
Definition i8250.h:55
@ RXREADY
Definition i8250.h:49
@ RECEIVE
Definition i8250.h:46
@ TRANSMIT
Definition i8250.h:45
@ IRQCONTROL
Definition i8250.h:50
i8250(const u32 irq, const u16 base)
Constructor function.
Definition i8250.cpp:29
virtual FileSystem::Result read(IOBuffer &buffer, Size &size, const Size offset)
Read bytes from the device.
Definition i8250.cpp:78
virtual FileSystem::Result initialize()
Initializes the i8250 serial UART.
Definition i8250.cpp:36
virtual FileSystem::Result write(IOBuffer &buffer, Size &size, const Size offset)
Write bytes to the device.
Definition i8250.cpp:104
virtual FileSystem::Result interrupt(const Size vector)
Called when an interrupt has been triggered for this device.
Definition i8250.cpp:72
#define SELF
Definition ProcessID.h:35
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
@ DisableIRQ
Definition ProcessCtl.h:45
@ EnableIRQ
Definition ProcessCtl.h:44
C uint isKernel
Non-zero if this executable is linked as the kernel.
unsigned int u32
Unsigned 32-bit number.
Definition Types.h:53
unsigned short u16
Unsigned 16-bit number.
Definition Types.h:56
unsigned int Size
Any sane size indicator cannot go negative.
Definition Types.h:128
unsigned char u8
Unsigned 8-bit number.
Definition Types.h:59
Result
Result code for filesystem Actions.
Definition FileSystem.h:53