Max OS 0.3
Loading...
Searching...
No Matches
clock.h
Go to the documentation of this file.
1
9#ifndef MAXOS_HARDWARECOMMUNICATION_CLOCK_H
10#define MAXOS_HARDWARECOMMUNICATION_CLOCK_H
11
12#include <cstdint>
13
14#include <libcommon/time.h>
15#include <libcommon/vector.h>
16#include <libcommon/event.h>
17
20
21
22namespace MaxOS::hardwarecommunication {
23
30 enum class Channel {
31 INTERRUPT,
32 DRAM_REFRESH,
33 SPEAKER,
34 };
35
40 enum class AccessMode {
41 LATCH_COUNT,
42 LOW_BYTE,
43 HIGH_BYTE,
44 LOW_HIGH_BYTE,
45 };
46
51 enum class OperatingMode {
52 INTERRUPT_ON_TERMINAL_COUNT,
53 ONE_SHOT,
54 RATE_GENERATOR,
55 SQUARE_WAVE,
56 SOFTWARE_STROBE,
57 HARDWARE_STROBE,
58 };
59
64 enum class BCDMode {
65 BINARY,
66 BCD,
67 };
68
76 typedef struct PITCommand {
77
78 uint8_t bcd_mode : 1;
79 uint8_t operating_mode : 3;
80 uint8_t access_mode : 2;
81 uint8_t channel : 2;
82
83 } pit_command_t;
84
85
86 // Forward declaration
87 class Clock;
88
90 constexpr uint16_t TICK_CALIBRATE_LENGTH = 10;
91
96 class PIT : public InterruptHandler {
97 friend Clock;
98
99 private:
100 uint64_t m_ticks { 0 };
101
102 // Ports
103 common::Port8Bit m_data_port;
104 common::Port8Bit m_command_port;
105
106 // APIC
107 LocalAPIC* m_local_apic;
108 IOAPIC* m_io_apic;
109
110 // Funcs
111 void handle_interrupt() final;
112 uint32_t ticks_per_ms();
113
114 public:
116 ~PIT();
117
118 };
119
124 class Clock : public InterruptHandler {
125 private:
126
127 uint64_t m_ticks { 0 };
128
129 bool m_binary = true;
130 bool m_24_hour_clock = true;
131
132 // Ports
133 common::Port8Bit m_data_port { 0x71 };
134 common::Port8Bit m_command_port { 0x70 };
135
136 // APIC
138
139 // Time between events
140 uint16_t m_ticks_between_events = 0;
141 uint16_t m_ticks_until_next_event = 1;
142 uint64_t m_pit_ticks_per_ms = 0;
143
144 // Other functions
145 void handle_interrupt() final;
146 uint8_t read_hardware_clock(uint8_t address);
147 [[nodiscard]] uint8_t binary_representation(uint8_t number) const;
148
149 inline static Clock* s_active_clock = nullptr;
150
151 public:
152 explicit Clock(AdvancedProgrammableInterruptController* apic, uint16_t time_between_events = 10);
153 ~Clock();
154
156 uint64_t clock_accuracy = 1;
157
158 void delay(uint32_t milliseconds) const;
159
160 void calibrate(uint64_t ms_per_tick = 1);
161 void setup_apic_clock(LocalAPIC* local_apic) const;
162
163 static Clock* active_clock();
164 common::time_t get_time();
165 };
166
167}
168
169
170#endif //MAXOS_HARDWARECOMMUNICATION_CLOCK_H
Defines classes and structures for handling the Advanced Programmable Interrupt Controller (APIC) in ...
Handles both the Local APIC (boot core) and IO APIC.
Definition apic.h:240
Driver for the APIC clock.
Definition clock.h:124
common::time_t get_time()
Reads the current time from the APIC clock (in 24hr time)
Definition clock.cpp:156
uint64_t clock_accuracy
How accurate the clock is in microseconds (how often should an interrupt be raised)
Definition clock.h:156
void setup_apic_clock(LocalAPIC *local_apic) const
Sets up the APIC clock to fire interrupts at the desired rate.
Definition clock.cpp:137
void delay(uint32_t milliseconds) const
Delays the program for a specified number of milliseconds (rounded up to the nearest degree of accura...
Definition clock.cpp:102
static Clock * active_clock()
Gets the currently active clock.
Definition clock.cpp:182
void calibrate(uint64_t ms_per_tick=1)
Configures the APIC clock to fire an interrupt at a specified interval in milliseconds.
Definition clock.cpp:118
Handles the IO APIC in the system (one per system)
Definition apic.h:205
Handles a certain interrupt number.
Definition interrupts.h:44
Handles the local APIC for the current core.
Definition apic.h:34
Driver for the Programmable Interval Timer.
Definition clock.h:96
AccessMode
What mode should the PIT use for the selected channel.
Definition clock.h:40
BCDMode
Specifies the data encoding mode used by the PIT.
Definition clock.h:64
OperatingMode
What should the PIT do.
Definition clock.h:51
constexpr uint16_t TICK_CALIBRATE_LENGTH
How many loops to spin for calibration.
Definition clock.h:90
Channel
Where should the PIT raise an event to.
Definition clock.h:30
Defines a InterruptManager and InterruptHandler for managing hardware and software interrupts.
The command byte to send to the PIT, specifies the channel, access mode, operating mode and BCD mode.
Definition clock.h:76
uint8_t channel
Which channel to configure.
Definition clock.h:81
uint8_t operating_mode
What mode the PIT should operate in.
Definition clock.h:79
uint8_t access_mode
How the data is accessed.
Definition clock.h:80
uint8_t bcd_mode
0 = binary, 1 = BCD
Definition clock.h:78