APIC Timer: Difference between revisions
New APIC timer setup example code.
[unchecked revision] | [unchecked revision] |
m (Nuke repeated newlines) |
(New APIC timer setup example code.) |
||
Line 90:
* interrupt 39: spurious irq, IRQ7
If you've already changed this, modify accordingly.
=== Example code in ASM ===
Line 198 ⟶ 199:
mov dword [apic+APIC_TMRDIV], 03h
</source>
=== Example code in C ===
This code is an example of how to initialize the APIC timer so that it ticks every 10 milliseconds. This is done by letting the APIC timer run, waiting for 10ms using the PIT and then getting the number of ticks that were done from the APIC timer. It assumes that you have functions to "write"/"read" the APIC's registers and "pit_prepare_sleep"/"pit_perform_sleep" to perform an as accurate as possible measuring of the timers frequency.
<source lang="c">
void apic_start_timer() {
// Tell APIC timer to use divider 16
write(APIC_REGISTER_TIMER_DIV, 0x3);
// Prepare the PIT to sleep for 10ms (10000µs)
pit_prepare_sleep(10000);
write(APIC_REGISTER_TIMER_INITCNT, 0xFFFFFFFF);
// Perform PIT-supported sleep
pit_perform_sleep();
write(APIC_REGISTER_LVT_TIMER, APIC_LVT_INT_MASKED);
// Now we know how often the APIC timer has ticked in 10ms
uint32_t ticksIn10ms = 0xFFFFFFFF - read(APIC_REGISTER_TIMER_CURRCNT);
// Start timer as periodic on IRQ 0, divider 16, with the number of ticks we counted
write(APIC_REGISTER_LVT_TIMER, 32 | APIC_LVT_TIMER_MODE_PERIODIC);
write(APIC_REGISTER_TIMER_DIV, 0x3);
write(APIC_REGISTER_TIMER_INITCNT, ticksIn10ms);
▲ //reset APIC timer (set counter to -1)
▲ //stop APIC timer
}
</source>
|