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 
21  : Timer()
22 {
24 }
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 
77 {
79  return Success;
80 }
IntelPIT::Channel0
@ Channel0
Definition: IntelPIT.h:64
Timer::Success
@ Success
Definition: Timer.h:54
Timer::m_int
Size m_int
Timer interrupt number.
Definition: Timer.h:165
flags
u32 flags
Definition: IntelACPI.h:66
IntelPIT.h
IntelPIT::Channel0Data
@ Channel0Data
Definition: IntelPIT.h:56
IntelIO::inb
u8 inb(u16 port) const
Read a byte from a port.
Definition: IntelIO.h:68
IntelPIT::ControlFlags
ControlFlags
Control Register Flags.
Definition: IntelPIT.h:62
IntelPIT::LatchedRead
@ LatchedRead
Definition: IntelPIT.h:65
Timer
Represents a configurable timer device.
Definition: Timer.h:35
Timer::m_frequency
Size m_frequency
Frequency of the Timer.
Definition: Timer.h:162
uint
unsigned int uint
Unsigned integer number.
Definition: Types.h:44
IntelPIT::RateGenerator
@ RateGenerator
Definition: IntelPIT.h:68
IntelIO::outb
void outb(u16 port, u8 byte)
Output a byte to a port.
Definition: IntelIO.h:97
IntelPIT::m_io
IntelIO m_io
I/O instance.
Definition: IntelPIT.h:126
IntelPIT::OscillatorFreq
static const uint OscillatorFreq
Oscillator frequency in hertz used by the PIT.
Definition: IntelPIT.h:45
IntelPIT::InterruptNumber
static const uint InterruptNumber
The IRQ vector for channel 0 is fixed to IRQ0.
Definition: IntelPIT.h:48
IntelPIT::IntelPIT
IntelPIT()
Constructor.
Definition: IntelPIT.cpp:20
IntelPIT::Control
@ Control
Definition: IntelPIT.h:55
IntelPIT::setControl
Result setControl(ControlFlags flags)
Set Control register.
Definition: IntelPIT.cpp:76
Timer::InvalidFrequency
@ InvalidFrequency
Definition: Timer.h:57
Timer::Result
Result
Result codes.
Definition: Timer.h:52
IntelPIT::AccessLowHigh
@ AccessLowHigh
Definition: IntelPIT.h:66
IntelPIT::setFrequency
virtual Result setFrequency(Size hertz)
Set interrupt frequency.
Definition: IntelPIT.cpp:37
IntelPIT::waitTrigger
Result waitTrigger()
Busy wait for one trigger period.
Definition: IntelPIT.cpp:58
IntelPIT::getCounter
uint getCounter()
Get current timer counter.
Definition: IntelPIT.cpp:26