FreeNOS
IntelPIT.cpp
Go to the documentation of this file.
1/*
2 * Copyright (C) 2015 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 "IntelPIT.h"
19
25
27{
28 uint count = 0;
29
31 count = m_io.inb(Channel0Data);
32 count |= m_io.inb(Channel0Data) << 8;
33
34 return count;
35}
36
38{
39 uint divisor;
40
41 // Frequency must be non-zero and cannot exceed the oscillator
42 if (hertz == 0 || hertz > OscillatorFreq)
43 return InvalidFrequency;
44
45 // Frequency must be within bounds of the 16-bit counter
46 divisor = OscillatorFreq / hertz;
47 if (divisor <= 2 || divisor > 0xffff)
48 return InvalidFrequency;
49
50 // Let the i8254 timer run continuously
52 m_io.outb(Channel0Data, divisor & 0xff);
53 m_io.outb(Channel0Data, (divisor >> 8) & 0xff);
54 m_frequency = hertz;
55 return Success;
56}
57
59{
60 uint previous, current;
61
62 // Wait until the 16-bit counter restarts
63 // at its initial counter value.
64 current = getCounter();
65 do
66 {
67 previous = current;
68 current = getCounter();
69 }
70 while (previous >= current);
71
72 // Now at the trigger moment.
73 return Success;
74}
75
u32 flags
Definition IntelACPI.h:3
u8 inb(u16 port) const
Read a byte from a port.
Definition IntelIO.h:68
void outb(u16 port, u8 byte)
Output a byte to a port.
Definition IntelIO.h:97
Result setControl(ControlFlags flags)
Set Control register.
Definition IntelPIT.cpp:76
ControlFlags
Control Register Flags.
Definition IntelPIT.h:63
@ RateGenerator
Definition IntelPIT.h:68
@ LatchedRead
Definition IntelPIT.h:65
@ AccessLowHigh
Definition IntelPIT.h:66
@ Channel0
Definition IntelPIT.h:64
Result waitTrigger()
Busy wait for one trigger period.
Definition IntelPIT.cpp:58
IntelIO m_io
I/O instance.
Definition IntelPIT.h:126
static const uint OscillatorFreq
Oscillator frequency in hertz used by the PIT.
Definition IntelPIT.h:45
uint getCounter()
Get current timer counter.
Definition IntelPIT.cpp:26
static const uint InterruptNumber
The IRQ vector for channel 0 is fixed to IRQ0.
Definition IntelPIT.h:48
IntelPIT()
Constructor.
Definition IntelPIT.cpp:20
@ Channel0Data
Definition IntelPIT.h:56
@ Control
Definition IntelPIT.h:55
virtual Result setFrequency(Size hertz)
Set interrupt frequency.
Definition IntelPIT.cpp:37
Represents a configurable timer device.
Definition Timer.h:36
Size m_int
Timer interrupt number.
Definition Timer.h:165
Result
Result codes.
Definition Timer.h:53
@ Success
Definition Timer.h:54
@ InvalidFrequency
Definition Timer.h:57
Size m_frequency
Frequency of the Timer.
Definition Timer.h:162
unsigned int uint
Unsigned integer number.
Definition Types.h:44