Max OS 0.3
Loading...
Searching...
No Matches
clock.h
Go to the documentation of this file.
1
9#ifndef MAXOS_DRIVERS_CLOCK_CLOCK_H
10#define MAXOS_DRIVERS_CLOCK_CLOCK_H
11
12#include <cstdint>
13#include <common/time.h>
14#include <drivers/driver.h>
17#include <common/vector.h>
18#include <common/eventHandler.h>
19
20
21namespace MaxOS::drivers::clock {
22
29 enum class Channel {
30 INTERRUPT,
31 DRAM_REFRESH,
32 SPEAKER,
33 };
34
39 enum class AccessMode {
40 LATCH_COUNT,
41 LOW_BYTE,
42 HIGH_BYTE,
43 LOW_HIGH_BYTE,
44 };
45
50 enum class OperatingMode {
51 INTERRUPT_ON_TERMINAL_COUNT,
52 ONE_SHOT,
53 RATE_GENERATOR,
54 SQUARE_WAVE,
55 SOFTWARE_STROBE,
56 HARDWARE_STROBE,
57 };
58
63 enum class BCDMode {
64 BINARY,
65 BCD,
66 };
67
75 typedef struct PITCommand {
76
77 uint8_t bcd_mode : 1;
78 uint8_t operating_mode : 3;
79 uint8_t access_mode : 2;
80 uint8_t channel : 2;
81
82 } pit_command_t;
83
84
85 // Forward declaration
86 class Clock;
87
89 constexpr uint16_t TICK_CALIBRATE_LENGTH = 10;
90
96 friend Clock;
97
98 private:
99 uint64_t m_ticks { 0 };
100
101 // Ports
103 hardwarecommunication::Port8Bit m_command_port;
104
105 // APIC
108
109 // Funcs
110 void handle_interrupt() final;
111 uint32_t ticks_per_ms();
112
113 public:
115 ~PIT();
116
117 };
118
124 private:
125
126 uint64_t m_ticks { 0 };
127
128 bool m_binary = true;
129 bool m_24_hour_clock = true;
130
131 // Ports
132 hardwarecommunication::Port8Bit m_data_port { 0x71 };
133 hardwarecommunication::Port8Bit m_command_port { 0x70 };
134
135 // APIC
137
138 // Time between events
139 uint16_t m_ticks_between_events = 0;
140 uint16_t m_ticks_until_next_event = 1;
141 uint64_t m_pit_ticks_per_ms = 0;
142
143 // Other functions
144 void handle_interrupt() final;
145 uint8_t read_hardware_clock(uint8_t address);
146 [[nodiscard]] uint8_t binary_representation(uint8_t number) const;
147
148 inline static Clock* s_active_clock = nullptr;
149
150 public:
151 explicit Clock(hardwarecommunication::AdvancedProgrammableInterruptController* apic, uint16_t time_between_events = 10);
152 ~Clock();
153
155 uint64_t clock_accuracy = 1;
156
157 void activate() override;
158 void delay(uint32_t milliseconds) const;
159
160 void calibrate(uint64_t ms_per_tick = 1);
162
163 string vendor_name() final;
164 string device_name() final;
165
166 static Clock* active_clock();
167 common::time_t get_time();
168 };
169
170}
171
172
173#endif //MAXOS_DRIVERS_CLOCK_CLOCK_H
Defines classes and structures for handling the Advanced Programmable Interrupt Controller (APIC) in ...
base class for all drivers, handles the activation, deactivation, initialisation and reset of the dri...
Definition driver.h:27
Driver for the APIC clock.
Definition clock.h:123
common::time_t get_time()
Reads the current time from the APIC clock (in 24hr time)
Definition clock.cpp:182
void activate() override
Activates the clock, setting the binary coded decimal representation flag.
Definition clock.cpp:88
string device_name() final
Gets the name of the device.
Definition clock.cpp:135
void setup_apic_clock(hardwarecommunication::LocalAPIC *local_apic) const
Sets up the APIC clock to fire interrupts at the desired rate.
Definition clock.cpp:163
uint64_t clock_accuracy
How accurate the clock is in microseconds (how often should an interrupt be raised)
Definition clock.h:155
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:110
string vendor_name() final
Gets the vendor who created the device.
Definition clock.cpp:126
static Clock * active_clock()
Gets the currently active clock.
Definition clock.cpp:208
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:144
Driver for the Programmable Interval Timer.
Definition clock.h:95
Handles both the Local APIC (boot core) and IO APIC.
Definition apic.h:228
Handles the IO APIC in the system (one per system)
Definition apic.h:193
Handles a certain interrupt number.
Definition interrupts.h:30
Handles the local APIC for the current core.
Definition apic.h:24
BCDMode
Specifies the data encoding mode used by the PIT.
Definition clock.h:63
constexpr uint16_t TICK_CALIBRATE_LENGTH
How many loops to spin for calibration.
Definition clock.h:89
OperatingMode
What should the PIT do.
Definition clock.h:50
Channel
Where should the PIT raise an event to.
Definition clock.h:29
AccessMode
What mode should the PIT use for the selected channel.
Definition clock.h:39
Defines a base Driver class and DriverManager for managing drivers.
Defines classes for handling events and event managers.
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:75
uint8_t access_mode
How the data is accessed.
Definition clock.h:79
uint8_t bcd_mode
0 = binary, 1 = BCD
Definition clock.h:77
uint8_t operating_mode
What mode the PIT should operate in.
Definition clock.h:78
uint8_t channel
Which channel to configure.
Definition clock.h:80
Defines a Time struct for storing date and time information, along with functions for time manipulati...
Defines a Vector class for dynamically storing an array of elements.