https://git.reactos.org/?p=reactos.git;a=commitdiff;h=3bbcc16d8e76ecf4296bdfc57e461a18523198a6

commit 3bbcc16d8e76ecf4296bdfc57e461a18523198a6
Author:     Timo Kreuzer <[email protected]>
AuthorDate: Tue Dec 31 09:39:55 2019 +0100
Commit:     Timo Kreuzer <[email protected]>
CommitDate: Sun Sep 20 23:08:17 2020 +0200

    [HAL] Fix calculation of timer increment
---
 hal/halx86/apic/rtctimer.c | 42 +++++++++++++++++++++++++++++++++++-------
 1 file changed, 35 insertions(+), 7 deletions(-)

diff --git a/hal/halx86/apic/rtctimer.c b/hal/halx86/apic/rtctimer.c
index afd3d9dab5a..62318f724ba 100644
--- a/hal/halx86/apic/rtctimer.c
+++ b/hal/halx86/apic/rtctimer.c
@@ -4,7 +4,9 @@
  * FILE:            hal/halx86/apic/rtctimer.c
  * PURPOSE:         HAL APIC Management and Control Code
  * PROGRAMMERS:     Timo Kreuzer ([email protected])
- * REFERENCES:
+ * REFERENCES:      https://wiki.osdev.org/RTC
+ *                  https://forum.osdev.org/viewtopic.php?f=13&t=20825&start=0
+ *                  http://www.bioscentral.com/misc/cmosmap.htm
  */
 
 /* INCLUDES 
*******************************************************************/
@@ -24,16 +26,42 @@ BOOLEAN HalpClockSetMSRate;
 UCHAR HalpNextMSRate;
 UCHAR HalpCurrentRate = 9;  /* Initial rate  9: 128 Hz / 7.8 ms */
 ULONG HalpCurrentTimeIncrement;
-static UCHAR RtcMinimumClockRate = 6;  /* Minimum rate  6:  16 Hz / 62.5 ms */
-static UCHAR RtcMaximumClockRate = 10; /* Maximum rate 10: 256 Hz / 3.9 ms */
-
-
+static UCHAR RtcMinimumClockRate = 8;  /* Minimum rate  8: 256 Hz / 3.9 ms */
+static UCHAR RtcMaximumClockRate = 12; /* Maximum rate 12: 16 Hz / 62.5 ms */
+
+/*!
+    \brief Converts the CMOS RTC rate into the time increment in 100ns 
intervals.
+
+    Rate Freqency Interval (ms)  Result
+    -------------------------------------
+     0   disabled
+     1   32768      0.03052          305
+     2   16384      0.06103          610
+     3    8192      0.12207         1221
+     4    4096      0.24414         2441
+     5    2048      0.48828         4883
+     6    1024      0.97656         9766
+     7     512      1.95313        19531
+     8     256      3.90625        39063
+     9     128      7.8125         78125
+    10      64     15.6250        156250
+    11      32     31.25          312500
+    12      16     62.5           625000
+    13       8    125            1250000
+    14       4    250            2500000
+    15       2    500            5000000
+
+*/
 FORCEINLINE
 ULONG
 RtcClockRateToIncrement(UCHAR Rate)
 {
-    ULONG Freqency = ((32768 << 1) >> Rate);
-    return (1000000 + (Freqency/2)) / Freqency;
+    /* Calculate frequency */
+    ULONG Freqency = 32768 >> (Rate - 1);
+
+    /* Calculate interval in 100ns interval: Interval = (1 / Frequency) * 
10000000
+       This formula will round properly, instead of truncating. */
+    return (10000000 + (Freqency/2)) / Freqency;
 }
 
 VOID

Reply via email to