MP3 1.0
This is the Doxygen document for ECE391 MP3.
Loading...
Searching...
No Matches
Functions | Variables
rtc.c File Reference

Implementations for RTC (real time clock) More...

#include "i8259.h"
#include "rtc.h"
#include "lib.h"

Functions

void rtcInit ()
 Initializes RTC. More...
 
void rtcInterrupt ()
 Handle RTC interrupt. More...
 
int setRate (int rate)
 Set the interrupt rate of the RTC. More...
 
char translateRateToBits (int rate)
 Translates given RTC rate to corresponding bits to write in register A. More...
 
unsigned char isPowerOfTwo (int x)
 Checks if a number is a power of two. More...
 
int32_t rtcOpen (const uint8_t *filename)
 Sets the RTC to its slowest rate. (2Hz) More...
 
int32_t rtcClose (int32_t fd)
 Needed per MP spec. More...
 
int rtcRead (int32_t fd, void *buf, int32_t nbytes)
 Other functions will use this to tell when an RTC interrupt occurs. More...
 
int rtcWrite (int32_t fd, const void *buf, int32_t nbytes)
 Set the rate of the RTC thru a sys call. More...
 

Variables

static volatile int rtcIntCaught = 0
 Flag for interrupt being handled as specified in doc. More...
 

Detailed Description

Implementations for RTC (real time clock)

Author
Group 8
Version
1.0
Date
2023-03-11

Function Documentation

◆ isPowerOfTwo()

unsigned char isPowerOfTwo ( int  x)

Checks if a number is a power of two.

Helper function since RTC only supports rates that are a power of 2. Taken from stackoverflow.

Parameters
xThe number to check.
Returns
True if is a power of two.
False if not a power of two.
See also
https://stackoverflow.com/questions/600293/how-to-check-if-a-number-is-a-power-of-2

◆ rtcClose()

int32_t rtcClose ( int32_t  fd)

Needed per MP spec.

Parameters
fdFile descriptor. Never used.
Returns
0
Note
This code doesn't do anything.

◆ rtcInit()

void rtcInit ( )

Initializes RTC.

Initializes RTC by unmasking relevant IRQ#, setting clock to 1024Hz, putting in square wave mode, and setting periodic interrupt enable bit.

◆ rtcInterrupt()

void rtcInterrupt ( )

Handle RTC interrupt.

Increment counterRTC by one.

Note
contents of register C must be read. If we did not read C after interrupt, that interrupt can not happen again.
See also
counterRTC

◆ rtcOpen()

int32_t rtcOpen ( const uint8_t filename)

Sets the RTC to its slowest rate. (2Hz)

Parameters
filenameNever used.
Returns
0
Note
This code doesn't do anything.

◆ rtcRead()

int rtcRead ( int32_t  fd,
void *  buf,
int32_t  nbytes 
)

Other functions will use this to tell when an RTC interrupt occurs.

Wait until the executingTerminal receives enough RTC interrupt at 1024Hz, such that counter < 1024/rates. This virtualizes the RTC.

Parameters
fdNot used.
bufNot used.
nbytesNot used.
Returns
0
See also
executingTerminalID, counterRTC, ratesRTC

◆ rtcWrite()

int rtcWrite ( int32_t  fd,
const void *  buf,
int32_t  nbytes 
)

Set the rate of the RTC thru a sys call.

Changes the rate for the current active process, which is the process calling the RTC.

Parameters
fdNot used.
bufInteger pointer.
nbytesNot used.
Returns
0 On successful rate change.
-1 On failed rate change.
Note
Instead of setRate(), rtcWrite() changes the ratesRTC array.
See also
executingTerminalID, ratesRTC

◆ setRate()

int setRate ( int  rate)

Set the interrupt rate of the RTC.

Parameters
rateRTC rate to be set in Hz. Must be a power of 2 and supported by RTC.
Returns
0 on success.
-1 if rate setting failed. This happens if rate not supported by RTC or our kernel limits it. (no greater than 1024Hz)
Note
Function will error check the rate and fail if cannot set rate.
Warning
Since we are virtualizing the RTC, do not use this function to set the rate of user processes. This should always be kept at 1024Hz.

◆ translateRateToBits()

char translateRateToBits ( int  rate)

Translates given RTC rate to corresponding bits to write in register A.

Parameters
rateThe rate in Hz to set the RTC to.
Returns
The bits to write into reg A of RTC for to set to given rate.
-1 If failed. This happens if rate is not supported by RTC or kernel.

Variable Documentation

◆ rtcIntCaught

volatile int rtcIntCaught = 0
static

Flag for interrupt being handled as specified in doc.

1 = busy, 0 = free