-
Notifications
You must be signed in to change notification settings - Fork 89
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #334 from Remus7/master
Real Time Clock support and examples
- Loading branch information
Showing
8 changed files
with
156 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
# Makefile for user application | ||
|
||
# Specify this directory relative to the current application. | ||
TOCK_USERLAND_BASE_DIR = ../../ | ||
|
||
# Which files to compile. | ||
C_SRCS := $(wildcard *.c) | ||
|
||
# Include userland master makefile. Contains rules and flags for actually | ||
# building the application. | ||
include $(TOCK_USERLAND_BASE_DIR)/AppMakefile.mk |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
#include <rtc.h> | ||
#include <stdio.h> | ||
#include <timer.h> | ||
|
||
int main(void){ | ||
// Initialises a date struct with a certain timestamp | ||
struct Date date = { | ||
.year = 2023, | ||
.month = JANUARY, | ||
.day = 1, | ||
|
||
.day_of_week = MONDAY, | ||
.hour = 12, | ||
.minute = 30, | ||
.seconds = 1 | ||
}; | ||
|
||
set_date(&date); | ||
// Clock has a small delay before starting to count seconds | ||
delay_ms(2000); | ||
|
||
while (1) { | ||
get_date(&date); | ||
printf("Date: {year: %d, month: %d, day: %d, day_of_week: %d, hour: %d, minute: %d, seconds: %d}\n\n", date.year, | ||
date.month, date.day, date.day_of_week, date.hour, date.minute, date.seconds); | ||
|
||
// This delay uses an underlying timer in the kernel | ||
delay_ms(1000); | ||
} | ||
return 0; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
#include "rtc.h" | ||
#include <stdio.h> | ||
#include <stdlib.h> | ||
|
||
int date, time; | ||
|
||
static void rtc_cb(int status __attribute__ ((unused)), | ||
int upcall1, | ||
int upcall2, | ||
void* ud __attribute__ ((unused))){ | ||
date = upcall1; | ||
time = upcall2; | ||
} | ||
|
||
int get_date(struct Date *put_date){ | ||
subscribe_return_t sub = subscribe(DRIVER_NUM_RTC, 0, rtc_cb, malloc(sizeof(struct DateTime))); | ||
if (!sub.success) { | ||
return tock_status_to_returncode(sub.status); | ||
} | ||
|
||
syscall_return_t rval = command(DRIVER_NUM_RTC, 1, 0, 0); | ||
if (!(rval.type == 128)) { | ||
printf("%d", rval.type); | ||
} | ||
|
||
struct Date date_result = { | ||
.year = date % (1 << 21) / (1 << 9), | ||
.month = date % (1 << 9) / (1 << 5), | ||
.day = date % (1 << 5), | ||
|
||
.day_of_week = time % (1 << 20) / (1 << 17), | ||
.hour = time % (1 << 17) / (1 << 12), | ||
.minute = time % (1 << 12) / (1 << 6), | ||
.seconds = time % (1 << 6) | ||
}; | ||
|
||
*put_date = date_result; | ||
return tock_command_return_novalue_to_returncode(rval); | ||
} | ||
|
||
int set_date(const struct Date *set_date){ | ||
date = set_date->year * (1 << 9) + set_date->month * (1 << 5) + set_date->day; | ||
time = set_date->day_of_week * | ||
(1 << 17) + set_date->hour * (1 << 12) + set_date->minute * (1 << 6) + set_date->seconds; | ||
|
||
syscall_return_t rval = command(DRIVER_NUM_RTC, 2, date, time); | ||
return tock_command_return_novalue_to_returncode(rval); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
#pragma once | ||
|
||
#include "tock.h" | ||
|
||
#ifdef __cplusplus | ||
extern "C" { | ||
#endif | ||
|
||
#define DRIVER_NUM_RTC 0x00090007 | ||
|
||
#define JANUARY 1 | ||
#define FEBRUARY 2 | ||
#define MARCH 3 | ||
#define APRIL 4 | ||
#define MAY 5 | ||
#define JUNE 6 | ||
#define JULY 7 | ||
#define AUGUST 8 | ||
#define SEPTEMBER 9 | ||
#define OCTOBER 10 | ||
#define NOVEMBER 11 | ||
#define DECEMBER 12 | ||
|
||
#define SUNDAY 0 | ||
#define MONDAY 1 | ||
#define TUESDAY 2 | ||
#define WENSDAY 3 | ||
#define THURSDAY 4 | ||
#define FRIDAY 5 | ||
#define SATURDAY 6 | ||
|
||
// Date structure to store date and time | ||
struct Date{ | ||
int year; | ||
int month; | ||
int day; | ||
int day_of_week; | ||
int hour; | ||
int minute; | ||
int seconds; | ||
}; | ||
|
||
// DateTime codifies Date structure into two u32 (int) numbers | ||
// date: first number (year, month, day_of_the_month): | ||
// - last 5 bits store the day_of_the_month | ||
// - previous 4 bits store the month | ||
// - previous 12 bits store the year | ||
// time: second number (day_of_the_week, hour, minute, seconds): | ||
// - last 6 bits store the seconds | ||
// - previous 6 store the minute | ||
// - previous 5 store the hour | ||
// - previous 3 store the day_of_the_week | ||
struct DateTime{ | ||
int date; | ||
int time; | ||
}; | ||
|
||
// Fetches current date from registers and writes it to get_date | ||
int get_date(struct Date *get_date); | ||
|
||
// Writes set_date into the registers | ||
int set_date(const struct Date *set_date); | ||
|
||
#ifdef __cplusplus | ||
} | ||
#endif |
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.