KernOS
pit.cpp
Go to the documentation of this file.
1 //
2 // Created on 7/5/20.
3 //
4 
5 #include <pit.h>
6 #include <common.h>
7 #include <utilities.h>
8 #include <interrupt.h>
9 #include <ports.h>
10 #include <pic.h>
11 
12 namespace INTRP
13 {
14  extern DescriptorEntry idt_table[IDT_ENTRIES];
15 
16  void TimerInterruptHandler();
17 }
18 
28 namespace TIMER
29 {
30  uint64_t timer_ticks;
31 
32  enum PORT : uint8_t
33  {
34  CHANNEL_O = 0x40,
35  CHANNEL_1 = 0x41,
36  CHANNEL_2 = 0x42,
37  COMMAND = 0x43
38  };
39 
40  enum CHANNEL : uint8_t
41  {
42  TIMER0 = 0x00,
43  TIMER1 = 0x40,
44  TIMER2 = 0x80
45  };
46 
47  enum ACCESSMODE : uint8_t
48  {
49  LATCH = 0x00,
50  LO_ONLY = 0x10,
51  HI_ONLY = 0x20,
52  HI_LO = 0x30
53  };
54 
55  enum OPMODE : uint8_t
56  {
57  COUNTDOWN = 0x0,
58  ONESHOT = 0x2,
65  };
66 
67  enum DECIMALMODE : uint8_t
68  {
69  BINARY = 0x0,
70  BCD = 0x1
71  };
72 
73  const uint32_t BASE_FREQ = 1'193'182;
74  const uint16_t TICKS_PER_SECOND = 1'000;
75 
76  void PIT_825x()
77  {
78  constexpr uint8_t PIT_SETTING = CHANNEL::TIMER0 | ACCESSMODE::HI_LO | OPMODE::SQUAREWAVE_GENERATOR | DECIMALMODE::BINARY;
79 
80  out8(PORT::COMMAND, PIT_SETTING);
81 
82  constexpr uint16_t timer_reload = (BASE_FREQ / TICKS_PER_SECOND);
83 
84  out8(PORT::CHANNEL_O, timer_reload & 0xFF);
85  out8(PORT::CHANNEL_O, (timer_reload >> 8) & 0xFF);
86  }
87 } // namespace TIMER
88 
89 namespace INIT
90 {
91  void PIT()
92  {
93  TIMER::PIT_825x();
94 
95  UnmaskInterrupt(INTRP::IVT::TIMER);
96  }
97 }
98 
I/O port for channel 1 data port.
Definition: pit.cpp:35
I/O port for channel 0 data port.
Definition: pit.cpp:34
0000&#39;(110)0
Definition: pit.cpp:63
DescriptorEntry idt_table[IDT_ENTRIES]
Definition: interrupt.cpp:20
00(10)&#39;0000 Low byte only
Definition: pit.cpp:51
interrupt namespace
Definition: interrupt.h:16
0000&#39;000(0) 16 bit binary
Definition: pit.cpp:69
ACCESSMODE
Definition: pit.cpp:47
const uint32_t BASE_FREQ
14.31818 MHz / 12 (due to hardware historical reason)
Definition: pit.cpp:73
const uint16_t TICKS_PER_SECOND
Definition: pit.cpp:74
00(01)&#39;0000 Low byte only
Definition: pit.cpp:50
OPMODE
Definition: pit.cpp:55
0000&#39;(011)0
Definition: pit.cpp:60
(10)00&#39;0000 Channel 2
Definition: pit.cpp:44
Mode/command register.
Definition: pit.cpp:37
I/O port for channel 2 data port.
Definition: pit.cpp:36
const uint16_t IDT_ENTRIES
Definition: interrupt.h:71
CHANNEL
Definition: pit.cpp:40
0000&#39;000(1) 4 digit binary coded decimal
Definition: pit.cpp:70
0000&#39;(111)0
Definition: pit.cpp:64
Timer namespace.
Definition: pit.h:10
00(11)&#39;0000 Low byte only
Definition: pit.cpp:52
0000&#39;(100)0
Definition: pit.cpp:61
0000&#39;(010)0
Definition: pit.cpp:59
(01)00&#39;0000 Channel 1
Definition: pit.cpp:43
uint64_t timer_ticks
Definition: pit.cpp:30
00(00)&#39;0000 Latch count value
Definition: pit.cpp:49
DECIMALMODE
Definition: pit.cpp:67
PORT
Definition: pit.cpp:32
0000&#39;(001)0
Definition: pit.cpp:58
0000&#39;(101)0
Definition: pit.cpp:62
0000&#39;(000)0
Definition: pit.cpp:57
void TimerInterruptHandler()
Timer interrupt handler.
Definition: interrupt.cpp:48
(00)00&#39;0000 Channel 0
Definition: pit.cpp:42