Skip to content

Commit

Permalink
Changed rtc so that we use UIE interrupts
Browse files Browse the repository at this point in the history
  • Loading branch information
Adriano-7 committed May 28, 2023
1 parent f80978c commit 255ad33
Show file tree
Hide file tree
Showing 19 changed files with 427 additions and 178 deletions.
Empty file added lab6/.gdbinit
Empty file.
4 changes: 2 additions & 2 deletions lab6/Makefile
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
# name of the program (Minix service)
PROG=lab5
PROG=lab6

# source code files to be compiled
SRCS = lab5.c video.c timer.c utils.c keyboard.c
SRCS = lab6.c rtc.c utils.c

# additional compilation flags
# "-Wall -Wextra -Werror -I . -std=c11 -Wno-unused-parameter" are already set
Expand Down
Binary file added lab6/lab5
Binary file not shown.
Binary file added lab6/lab6
Binary file not shown.
59 changes: 57 additions & 2 deletions lab6/lab6.c
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
#include <lcom/lcf.h>

#include <lcom/lab6.h>

#include <stdint.h>
#include <stdio.h>

#include "rtc.h"

extern Date_t date;

int main(int argc, char *argv[]) {
// sets the language of LCF messages (can be either EN-US or PT-PT)
Expand Down Expand Up @@ -59,10 +58,66 @@ int rtc_test_conf(){

int rtc_test_date(){
//Read the date/time registers of the RTC
if(rtc_update_date()){
printf("Error updating date\n");
return 1;
}

printf("Date: %d/%d/%d\n", date.day, date.month, date.year);
printf("Time: %d:%d:%d\n", date.hours, date.minutes, date.seconds);

return 0;
}
int rtc_test_int(){
//Handle one of the interrupt events generated by the RTC
int ipc_status, r;
message msg;

uint8_t keyboard_bit_no, rtc_bit_no;
if(keyboard_subscribe_int(&keyboard_bit_no)){
printf("Error subscribing keyboard\n");
return 1;
}

if(rtc_subscribe_int(&rtc_bit_no)){
printf("Error subscribing rtc\n");
return 1;
}

while(scancode != KEY_ESC_BREAK){
if((r = driver_receive(ANY, &msg, &ipc_status)) != 0){
printf("driver_receive failed with: %d", r);
continue;
}

if(is_ipc_notify(ipc_status)){
switch(_ENDPOINT_P(msg.m_source)){
case HARDWARE:
if(msg.m_notify.interrupts & BIT(keyboard_bit_no)){
keyboard_int_handler();
keyboard_parse_output();
}
if(msg.m_notify.interrupts & BIT(rtc_bit_no)){
rtc_int_handler();
printf("Date: %d/%d/%d\n", date.day, date.month, date.year);
printf("Time: %d:%d:%d\n", date.hours, date.minutes, date.seconds);
}
break;
default:
break;
}
}
}

if(keyboard_unsubscribe_int()){
printf("Error unsubscribing keyboard\n");
return 1;
}

if(rtc_unsubscribe_int()){
printf("Error unsubscribing rtc\n");
return 1;
}

return 0;
}
11 changes: 11 additions & 0 deletions lab6/macros.h
Original file line number Diff line number Diff line change
Expand Up @@ -93,4 +93,15 @@
#define RTC_REG_MONTH 8
#define RTC_REG_YEAR 9

#define RTC_REG_A 10
#define RTC_REG_B 11
#define RTC_REG_C 12
#define RTC_REG_D 13


#define RTC_A_UIP_BIT BIT(7)

#define RTC_B_SET_BIT BIT(7)
#define RTC_B_UIE_BIT BIT(4)

#define RTC_C_UF_BIT BIT(4)
108 changes: 107 additions & 1 deletion lab6/rtc.c
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
#include "rtc.h"

int rtc_hook_id = 3;
Date date;
Date_t date;

uint8_t regA, regB, regC, regD;

int (rtc_subscribe_int)(uint8_t *bit_no){
if(bit_no==NULL){printf("Bit_no is a null pointer\n"); return 1;}
Expand All @@ -12,6 +14,11 @@ int (rtc_subscribe_int)(uint8_t *bit_no){
return 1;
}

if(rtc_startup()){
printf("Error starting up the UIE interrupts\n");
return 1;
}

return 0;
}

Expand All @@ -21,6 +28,11 @@ int (rtc_unsubscribe_int)(){
return 1;
}

if(rtc_startdown()){
printf("Error disabiling the UIE interrupts\n");
return 1;
}

return 0;
}

Expand All @@ -37,6 +49,8 @@ int (rtc_read_register)(uint8_t reg, uint8_t *data){
return 1;
}

rtc_convert_bcd(data);

return 0;
}

Expand All @@ -54,6 +68,98 @@ int (rtc_write_register)(uint8_t reg, uint8_t data){
return 0;
}

int (rtc_startup)(){
if(rtc_read_register(RTC_REG_B, &regB)){
printf("Error reading register B\n");
return 1;
}

regB |= RTC_B_UIE_BIT;

if(rtc_write_register(RTC_REG_B, regB)){
printf("Error writing register B\n");
return 1;
}

return 0;
}

int (rtc_startdown)(){
if(rtc_read_register(RTC_REG_B, &regB)){
printf("Error reading register B\n");
return 1;
}

regB &= ~RTC_B_UIE_BIT;

if(rtc_write_register(RTC_REG_B, regB)){
printf("Error writing register B\n");
return 1;
}

return 0;
}

int (rtc_convert_bcd)(uint8_t *data){
if(data==NULL){printf("Data is a null pointer\n"); return 1;}

*data = (*data >> 4) * 10 + (*data & 0x0F);

return 0;
}

int (rtc_update_date)(){
regA = 0;

while (regA & RTC_A_UIP_BIT) {
if(rtc_read_register(RTC_REG_A, &regA)){
printf("Error reading register A\n");
return 1;
}
}

if(rtc_read_register(RTC_REG_SECONDS, &date.seconds)){
printf("Error reading seconds\n");
return 1;
}

if(rtc_read_register(RTC_REG_MINUTES, &date.minutes)){
printf("Error reading minutes\n");
return 1;
}

if(rtc_read_register(RTC_REG_HOURS, &date.hours)){
printf("Error reading hours\n");
return 1;
}

if(rtc_read_register(RTC_REG_DAY_MONTH, &date.day)){
printf("Error reading day\n");
return 1;
}

if(rtc_read_register(RTC_REG_MONTH, &date.month)){
printf("Error reading month\n");
return 1;
}

if(rtc_read_register(RTC_REG_YEAR, &date.year)){
printf("Error reading year\n");
return 1;
}
return 0;
}

void (rtc_int_handler)(){
if(rtc_read_register(RTC_REG_C, &regC)){
printf("Error reading register C\n");
return;
}

if(regC & RTC_C_UF_BIT){
if(rtc_update_date()){
printf("Error updating the date\n");
return;
}
}
}
16 changes: 10 additions & 6 deletions lab6/rtc.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,24 +9,28 @@ int (rtc_subscribe_int)(uint8_t *bit_no);

int (rtc_unsubscribe_int)();

int (rtc_startup)();

void (rtc_int_handler)();

int (rtc_read_register)(uint8_t reg, uint8_t *data);

int (rtc_write_register)(uint8_t reg, uint8_t data);

int (rtc_startup)();

int (rtc_startdown)();

int (rtc_convert_bcd)(uint8_t *data);

int (rtc_update_date)();

typedef struct Date_t{
void (rtc_int_handler)();

typedef struct Date{
uint8_t seconds;
uint8_t minutes;
uint8_t hours;
uint8_t day;
uint8_t month;
uint8_t year;
} Date;
} Date_t;

#endif

25 changes: 25 additions & 0 deletions lab6/utils.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
#include <lcom/lcf.h>

#include <stdint.h>

int(util_get_LSB)(uint16_t val, uint8_t *lsb) {
if(lsb==NULL){printf("Lsb is a null pointer\n"); return 1;}
*lsb = (uint8_t) val;
return 0;
}

int(util_get_MSB)(uint16_t val, uint8_t *msb) {
if(msb==NULL){printf("Msb is a null pointer\n"); return 1;}
*msb = (uint8_t) (val >> 8);
return 0;
}

int (util_sys_inb)(int port, uint8_t *value) {
if(value==NULL){printf("Value is a null pointer\n"); return 1;}
uint32_t val;
if(sys_inb(port, &val)){printf("Error reading from the port\n"); return 1;}

util_get_LSB(val, value);

return 0;
}
29 changes: 28 additions & 1 deletion proj/src/devices/macros.h
Original file line number Diff line number Diff line change
Expand Up @@ -76,5 +76,32 @@
#define VIDEO_DIRECT_COLOR_MODE 0x06
#define VIDEO_1024x768_MODE 0x105

#define TRANSPARENCY_COLOR 13
/*RTC*/
#define RTC_IRQ 8

#define RTC_ADDR_REG 0x70
#define RTC_DATA_REG 0x71

#define RTC_REG_SECONDS 0
#define RTC_REG_SECONDS_ALARM 1
#define RTC_REG_MINUTES 2
#define RTC_REG_MINUTES_ALARM 3
#define RTC_REG_HOURS 4
#define RTC_REG_HOURS_ALARM 5
#define RTC_REG_DAY_WEEK 6
#define RTC_REG_DAY_MONTH 7
#define RTC_REG_MONTH 8
#define RTC_REG_YEAR 9

#define RTC_REG_A 10
#define RTC_REG_B 11
#define RTC_REG_C 12
#define RTC_REG_D 13


#define RTC_A_UIP_BIT BIT(7)

#define RTC_B_SET_BIT BIT(7)
#define RTC_B_UIE_BIT BIT(4)

#define RTC_C_UF_BIT BIT(4)
Loading

0 comments on commit 255ad33

Please sign in to comment.