Loading [MathJax]/extensions/tex2jax.js
CS350 COS
COS
All Data Structures Files Functions Variables Typedefs Macros
rtc.c File Reference
#include <stdbool.h>
#include <stdint.h>
#include <string.h>
#include <sys/kassert.h>
#include <sys/kdebug.h>
#include <sys/ktime.h>
#include "ioport.h"
Include dependency graph for rtc.c:

Go to the source code of this file.

Macros

#define RTC_SECONDS   0x00
 
#define RTC_MINUTES   0x02
 
#define RTC_HOURS   0x04
 
#define RTC_WEEKDAY   0x06
 
#define RTC_DAY   0x07
 
#define RTC_MONTH   0x08
 
#define RTC_YEAR   0x09
 
#define BCD_TO_BIN(_B)   ((_B & 0x0F) + ((_B >> 4) * 10))
 

Functions

UnixEpoch RTC_ReadTime ()
 
void RTC_Init ()
 
static uint8_t RTC_ReadReg (uint8_t reg)
 

Macro Definition Documentation

◆ BCD_TO_BIN

#define BCD_TO_BIN (   _B)    ((_B & 0x0F) + ((_B >> 4) * 10))

◆ RTC_DAY

#define RTC_DAY   0x07

Definition at line 16 of file rtc.c.

◆ RTC_HOURS

#define RTC_HOURS   0x04

Definition at line 14 of file rtc.c.

◆ RTC_MINUTES

#define RTC_MINUTES   0x02

Definition at line 13 of file rtc.c.

◆ RTC_MONTH

#define RTC_MONTH   0x08

Definition at line 17 of file rtc.c.

◆ RTC_SECONDS

#define RTC_SECONDS   0x00

Definition at line 12 of file rtc.c.

◆ RTC_WEEKDAY

#define RTC_WEEKDAY   0x06

Definition at line 15 of file rtc.c.

◆ RTC_YEAR

#define RTC_YEAR   0x09

Definition at line 18 of file rtc.c.

Function Documentation

◆ RTC_Init()

void RTC_Init ( )

Definition at line 23 of file rtc.c.

24{
25 uint64_t startTSC, stopTSC;
26 UnixEpoch first, second;
27
28 kprintf("RTC: Measuring CPU clock...\n");
29
30 first = RTC_ReadTime();
31 while (1) {
32 second = RTC_ReadTime();
33 if (first != second)
34 break;
35 first = second;
36 }
37 startTSC = Time_GetTSC();
38
39 first = RTC_ReadTime();
40 while (1) {
41 second = RTC_ReadTime();
42 if (first != second)
43 break;
44 first = second;
45 }
46 stopTSC = Time_GetTSC();
47
48 kprintf("RTC: %lld Ticks Per Second: %lld\n", second, stopTSC - startTSC);
49
50 KTime_SetTime(second, stopTSC, stopTSC - startTSC);
51}
int kprintf(const char *fmt,...)
Definition: printf.c:210
uint64_t UnixEpoch
Definition: ktime.h:18
void KTime_SetTime(UnixEpoch epoch, uint64_t tsc, uint64_t tps)
Definition: ktime.c:163
UnixEpoch RTC_ReadTime()
Definition: rtc.c:61
uint64_t Time_GetTSC()
Definition: time.c:13
unsigned long uint64_t
Definition: types.h:13
Here is the call graph for this function:
Here is the caller graph for this function:

◆ RTC_ReadReg()

static uint8_t RTC_ReadReg ( uint8_t  reg)
inlinestatic

Definition at line 54 of file rtc.c.

55{
56 outb(0x70, reg);
57 return inb(0x71);
58}
static INLINE uint8_t inb(uint16_t port)
Definition: amd64op.h:452
static INLINE void outb(uint16_t port, uint8_t data)
Definition: amd64op.h:431
Here is the call graph for this function:
Here is the caller graph for this function:

◆ RTC_ReadTime()

UnixEpoch RTC_ReadTime ( )

Definition at line 61 of file rtc.c.

62{
63 KTime tm;
64 bool isPM = false;
65 uint8_t flags = RTC_ReadReg(0x0B);
66
67 // Read RTC
70 tm.hour = RTC_ReadReg(RTC_HOURS);
72 tm.mday = RTC_ReadReg(RTC_DAY);
73 tm.month = RTC_ReadReg(RTC_MONTH);
74 tm.year = RTC_ReadReg(RTC_YEAR);
75
76 // Convert BCD & 24-hour checks
77 if (tm.hour & 0x80) {
78 isPM = true;
79 }
80 if ((flags & 0x04) == 0) {
81#define BCD_TO_BIN(_B) ((_B & 0x0F) + ((_B >> 4) * 10))
82 tm.sec = BCD_TO_BIN(tm.sec);
83 tm.min = BCD_TO_BIN(tm.min);
84 tm.hour = BCD_TO_BIN((tm.hour & 0x7F));
85 tm.wday = BCD_TO_BIN(tm.wday);
86 tm.mday = BCD_TO_BIN(tm.mday);
87 tm.month = BCD_TO_BIN(tm.month);
88 tm.year = BCD_TO_BIN(tm.year);
89 }
90 if (((flags & 0x02) == 0) && isPM) {
91 tm.hour = (tm.hour + 12) % 24;
92 }
93
94 tm.year += 2000;
95 tm.yday = -1;
96
97 tm.wday -= 1;
98 tm.month -= 1;
99
100// KTime_SetTime(&tm);
101 return KTime_ToEpoch(&tm);
102}
Definition: time.h:7
UnixEpoch KTime_ToEpoch(const KTime *tm)
Definition: ktime.c:76
Definition: ktime.h:7
#define RTC_YEAR
Definition: rtc.c:18
#define RTC_SECONDS
Definition: rtc.c:12
#define RTC_HOURS
Definition: rtc.c:14
static uint8_t RTC_ReadReg(uint8_t reg)
Definition: rtc.c:54
#define RTC_WEEKDAY
Definition: rtc.c:15
#define RTC_DAY
Definition: rtc.c:16
#define BCD_TO_BIN(_B)
#define RTC_MINUTES
Definition: rtc.c:13
#define RTC_MONTH
Definition: rtc.c:17
unsigned char uint8_t
Definition: types.h:10
Here is the call graph for this function:
Here is the caller graph for this function: