Max OS 0.3
Loading...
Searching...
No Matches
MaxOS::drivers::clock::Clock Class Reference

Driver for the APIC clock. More...

#include <clock.h>

Inheritance diagram for MaxOS::drivers::clock::Clock:
MaxOS::drivers::Driver MaxOS::hardwarecommunication::InterruptHandler

Public Member Functions

 Clock (hardwarecommunication::AdvancedProgrammableInterruptController *apic, uint16_t time_between_events=10)
 Constructor for the Clock class.
 
void activate () override
 Activates the clock, setting the binary coded decimal representation flag.
 
void delay (uint32_t milliseconds) const
 Delays the program for a specified number of milliseconds (rounded up to the nearest degree of accuracy - ensured the delay is at least the specified number of milliseconds). This on the kernel level is a busy wait, for user level see the sleep function in the Thread class.
 
void calibrate (uint64_t ms_per_tick=1)
 Configures the APIC clock to fire an interrupt at a specified interval in milliseconds.
 
void setup_apic_clock (hardwarecommunication::LocalAPIC *local_apic) const
 Sets up the APIC clock to fire interrupts at the desired rate.
 
string vendor_name () final
 Gets the vendor who created the device.
 
string device_name () final
 Gets the name of the device.
 
common::time_t get_time ()
 Reads the current time from the APIC clock (in 24hr time)
 
- Public Member Functions inherited from MaxOS::drivers::Driver
virtual void deactivate ()
 Deactivate the driver.
 
virtual void initialise ()
 Initialise the driver.
 
virtual uint32_t reset ()
 Reset the driver.
 
- Public Member Functions inherited from MaxOS::hardwarecommunication::InterruptHandler
virtual system::cpu_status_thandle_interrupt (system::cpu_status_t *status)
 Handles an interrupt and returns the status.
 

Static Public Member Functions

static Clockactive_clock ()
 Gets the currently active clock.
 

Public Attributes

uint64_t clock_accuracy = 1
 How accurate the clock is in microseconds (how often should an interrupt be raised)
 

Additional Inherited Members

- Protected Member Functions inherited from MaxOS::hardwarecommunication::InterruptHandler
 InterruptHandler (uint8_t interrupt_number, int64_t redirect=-1, uint64_t redirect_index=0)
 Creates a new interrupt handler and registers it with the interrupt manager.
 
 ~InterruptHandler ()
 Destroys the interrupt handler and unregisters it from the interrupt manager.
 
- Protected Attributes inherited from MaxOS::hardwarecommunication::InterruptHandler
uint8_t m_interrupt_number
 The interrupt number this handler handles.
 

Detailed Description

Driver for the APIC clock.

Definition at line 123 of file clock.h.

Constructor & Destructor Documentation

◆ Clock()

Clock::Clock ( hardwarecommunication::AdvancedProgrammableInterruptController apic,
uint16_t  time_between_events = 10 
)
explicit

Constructor for the Clock class.

Parameters
apicThe apic controller for this core
time_between_eventsThe time between events in 10ths of a second

Definition at line 24 of file clock.cpp.

25: InterruptHandler(0x20),
26 m_apic(apic),
27 m_ticks_between_events(time_between_events)
28{
29 Logger::INFO() << "Setting up Clock \n";
30}
static Logger INFO()
Gets active logger set to info level.
Definition logger.cpp:171
Stores the left, top, width and height of a rectangle.
Definition rectangle.h:22
Handles a certain interrupt number.
Definition interrupts.h:30

References MaxOS::Logger::INFO().

Member Function Documentation

◆ activate()

void Clock::activate ( )
overridevirtual

Activates the clock, setting the binary coded decimal representation flag.

Reimplemented from MaxOS::drivers::Driver.

Definition at line 88 of file clock.cpp.

88 {
89
90 s_active_clock = this;
91
92 // Get the stats from the clock
93 uint8_t status = read_hardware_clock(0xB);
94
95 // Store the clock status
96 m_24_hour_clock = status & 0x02;
97 m_binary = status & 0x04;
98}

◆ active_clock()

Clock * Clock::active_clock ( )
static

Gets the currently active clock.

Returns
The clock being used by the kernel

Definition at line 208 of file clock.cpp.

208 {
209 return s_active_clock;
210}

Referenced by MaxOS::filesystem::format::ext2::Ext2Volume::create_inode(), MaxOS::system::Core::init(), MaxOS::system::Core::wake_up(), and MaxOS::filesystem::format::ext2::Ext2File::write().

◆ calibrate()

void Clock::calibrate ( uint64_t  ms_per_tick = 1)

Configures the APIC clock to fire an interrupt at a specified interval in milliseconds.

Parameters
ms_per_tickHow many milliseconds per interrupt

Definition at line 144 of file clock.cpp.

144 {
145
146 Logger::INFO() << "Calibrating Clock \n";
148
149 // Get the ticks per ms
150 PIT pit(m_apic);
151 m_pit_ticks_per_ms = pit.ticks_per_ms();
152
153 // Calibrate the BSP apic to the desired time
154 setup_apic_clock(m_apic->local_apic());
155
156 Logger::DEBUG() << "Clock: Calibrated to " << ms_per_tick << "ms per kernel tick\n";
157}
static Logger DEBUG()
Gets active logger set to DEBUG level.
Definition logger.cpp:193
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
Driver for the Programmable Interval Timer.
Definition clock.h:95

References clock_accuracy, MaxOS::Logger::DEBUG(), MaxOS::Logger::INFO(), MaxOS::hardwarecommunication::AdvancedProgrammableInterruptController::local_apic(), and setup_apic_clock().

◆ delay()

void Clock::delay ( uint32_t  milliseconds) const

Delays the program for a specified number of milliseconds (rounded up to the nearest degree of accuracy - ensured the delay is at least the specified number of milliseconds). This on the kernel level is a busy wait, for user level see the sleep function in the Thread class.

See also
Thread::sleep
Parameters
millisecondsHow many milliseconds to delay the program for.

Definition at line 110 of file clock.cpp.

110 {
111
112 // Round the number of milliseconds UP to the nearest clock accuracy
114
115 // Wait until the time has passed
117 while (m_ticks < ticks_until_delay_is_over)
118 asm volatile("nop");
119}

References clock_accuracy.

◆ device_name()

string Clock::device_name ( )
finalvirtual

Gets the name of the device.

Returns
The name of the device

Reimplemented from MaxOS::drivers::Driver.

Definition at line 135 of file clock.cpp.

135 {
136 return "Clock";
137}

◆ get_time()

common::time_t Clock::get_time ( )

Reads the current time from the APIC clock (in 24hr time)

Returns
The current time in a Time struct format

Definition at line 182 of file clock.cpp.

182 {
183
184 // Wait for the clock to be ready
185 while (read_hardware_clock(0xA) & 0x80)
186 asm volatile("nop");
187
188 // Read the time from the clock
189 time_t time{};
190 time.year = binary_representation(read_hardware_clock(0x9)) + 2000;
191 time.month = binary_representation(read_hardware_clock(0x8));
192 time.day = binary_representation(read_hardware_clock(0x7));
193 time.hour = binary_representation(read_hardware_clock(0x4));
194 time.minute = binary_representation(read_hardware_clock(0x2));
195 time.second = binary_representation(read_hardware_clock(0x0));
196
197 // If the clock is using 12hr format and PM is set then add 12 to the hour
198 if (!m_24_hour_clock && (time.hour & 0x80) != 0)
199 time.hour = ((time.hour & 0x7F) + 12) % 24;
200
201 return time;
202}
Stores the year, month, day, hour, minute and second of a time.
Definition time.h:24

◆ setup_apic_clock()

void Clock::setup_apic_clock ( hardwarecommunication::LocalAPIC local_apic) const

Sets up the APIC clock to fire interrupts at the desired rate.

Parameters
local_apicThe local APIC to setup the clock on

Definition at line 163 of file clock.cpp.

163 {
164
165 // Configure the clock to periodic mode
166 uint32_t lvt = 0x20 | (1 << 17);
167 local_apic->write(0x320, lvt);
168
169 // Set the initial count
170 local_apic->write(0x380, m_pit_ticks_per_ms * clock_accuracy);
171
172 // Clear the interrupt mask for the clock
173 lvt &= ~(1 << 16);
174 local_apic->write(0x320, lvt);
175}
void write(uint32_t reg, uint32_t value) const
write a value to the apic register using the MSR or memory I/O depending on the local apic version
Definition apic.cpp:91

References clock_accuracy, and MaxOS::hardwarecommunication::LocalAPIC::write().

Referenced by calibrate().

◆ vendor_name()

string Clock::vendor_name ( )
finalvirtual

Gets the vendor who created the device.

Returns
The name of the vendor

Reimplemented from MaxOS::drivers::Driver.

Definition at line 126 of file clock.cpp.

126 {
127 return "Generic";
128}

Member Data Documentation

◆ clock_accuracy

uint64_t MaxOS::drivers::clock::Clock::clock_accuracy = 1

How accurate the clock is in microseconds (how often should an interrupt be raised)

Definition at line 155 of file clock.h.

Referenced by calibrate(), delay(), and setup_apic_clock().


The documentation for this class was generated from the following files: