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

Header function for rtc.c. More...

#include "terminal.h"

Go to the source code of this file.

Macros

#define RTC_INDEX   (0x70)
 
#define RTC_DATA   (0x71)
 
#define NMI_MASK   (0x80)
 
#define PIE_MASK   (0x40)
 
#define MAX_CLK   (0x06)
 
#define SQWE_MASK   (0x80)
 
#define REG_A   (0x0A)
 
#define REG_B   (0x0B)
 
#define REG_C   (0x0C)
 

Functions

void rtcInit ()
 Initializes RTC. 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

uint32_t counterRTC [MAX_NUM_TERMINALS]
 Counter array for active terminals, tracking RTC interrupt counts. More...
 
uint32_t ratesRTC [MAX_NUM_TERMINALS]
 Array storing the target rates for each active terminal. More...
 

Detailed Description

Header function for rtc.c.

Author
Group 8
Version
1.0
Date
2023-03-11

Macro Definition Documentation

◆ MAX_CLK

#define MAX_CLK   (0x06)

◆ NMI_MASK

#define NMI_MASK   (0x80)

◆ PIE_MASK

#define PIE_MASK   (0x40)

◆ REG_A

#define REG_A   (0x0A)

◆ REG_B

#define REG_B   (0x0B)

◆ REG_C

#define REG_C   (0x0C)

◆ RTC_DATA

#define RTC_DATA   (0x71)

◆ RTC_INDEX

#define RTC_INDEX   (0x70)

◆ SQWE_MASK

#define SQWE_MASK   (0x80)

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

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.
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.

◆ 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.
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

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

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.
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.
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

◆ counterRTC

Counter array for active terminals, tracking RTC interrupt counts.

Each counter increments upon RTC interrupt (1024Hz). Resets and returns when count > 1024 divided by the corresponding rate in ratesRTC.

◆ ratesRTC

Array storing the target rates for each active terminal.

Each rate determines the threshold for clearing its corresponding counter in counterRTC.