-
Notifications
You must be signed in to change notification settings - Fork 2
Datetime using the Real Time Clock #3
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
ed5e0a2
Merge with upstream
238eb94
trying to merge
abceb0b
Rollback to this
4d58c4f
attr test
beaf61b
For datetime
f7518cc
Merge branch 'datetime' into new
631c9b8
Merged datetime and printf
6b10aa9
merged printf and datetime
52702a8
Merge branch 'sandbox-science:main' into datetime
shade5144 e63cb31
Added real-time date and time checking
d053704
Header formatting changes
628fea3
reverse TODO comment back
chrisdedman 9f54125
Refactor date calculations in getdate function for clarity and mainta…
chrisdedman File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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,54 @@ | ||
| \documentclass{article} | ||
| \usepackage{listings} | ||
| \usepackage{geometry} | ||
| \geometry{margin=1in} | ||
| \title{Timekeeping API} | ||
| \date{} | ||
| \begin{document} | ||
|
|
||
| \maketitle | ||
|
|
||
| \section*{Structure Descriptions} | ||
|
|
||
| \begin{lstlisting}[language=C] | ||
|
|
||
| typedef struct | ||
| { | ||
| uint32_t hrs; | ||
| uint32_t mins; | ||
| uint32_t secs; | ||
| } timeval; | ||
|
|
||
| typedef struct | ||
| { | ||
| uint32_t day; | ||
| uint32_t month; | ||
| uint32_t year; | ||
|
|
||
| } dateval; | ||
|
|
||
| \end{lstlisting} | ||
|
|
||
| \section*{Implementation Note} | ||
|
|
||
| The number of seconds since epoch is retrieved from 926EJ-S's Real Time Clock Data Register(\textbf{RTCDR}). This is a 32 bit register, and therefore \underline{subject to the Year 2038 Problem} | ||
|
|
||
| \section*{uint32_t getdate(dateval *date)} | ||
|
|
||
| Fetch current date in days, months and years into \texttt{date}. Returns number of seconds since epoch. | ||
|
|
||
| \section*{uint32_t gettime(timeval *time)} | ||
|
|
||
| Fetch current time in hours, minutes and seconds into \texttt{time}. Returns number of seconds since epoch. | ||
|
|
||
| \section*{Examples} | ||
|
|
||
| \begin{lstlisting}[language=C,showstringspaces=false] | ||
| gettime(&time_struct); | ||
| puts("Current time(GMT): %d:%d:%d\n", time_struct.hrs, time_struct.mins, time_struct.secs); | ||
|
|
||
| getdate(&date_struct); | ||
| puts("Current date(MM-DD-YYYY): %d-%d-%d\n", date_struct.month, date_struct.day, date_struct.year); | ||
| \end{lstlisting} | ||
|
|
||
| \end{document} |
This file contains hidden or 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,33 @@ | ||
| #ifndef DATETIME_H | ||
| #define DATETIME_H | ||
|
|
||
| #include <stdint.h> | ||
|
|
||
| #ifdef __cplusplus | ||
| extern "C" | ||
| { | ||
| #endif | ||
|
|
||
| typedef struct | ||
| { | ||
| uint32_t hrs; | ||
| uint32_t mins; | ||
| uint32_t secs; | ||
| } timeval; | ||
|
|
||
| typedef struct | ||
| { | ||
| uint32_t day; | ||
| uint32_t month; | ||
| uint32_t year; | ||
|
|
||
| } dateval; | ||
|
|
||
| uint32_t getdate(dateval *date); | ||
| uint32_t gettime(timeval *time); | ||
|
|
||
| #ifdef __cplusplus | ||
| } | ||
| #endif | ||
|
|
||
| #endif // DATETIME_H |
This file contains hidden or 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
This file contains hidden or 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
This file contains hidden or 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,64 @@ | ||
| #include <stddef.h> | ||
| #include <stdint.h> | ||
| #include "datetime.h" | ||
|
|
||
| _Static_assert(sizeof(uint32_t) == 4, "uint32_t must be 4 bytes"); | ||
|
|
||
| #define RTCDR (*(volatile uint32_t *)0x101e8000) // RTC Register | ||
|
|
||
| #define SECONDS_IN_YEAR 31556926 | ||
| #define SECONDS_IN_MONTH 2629743 | ||
| #define SECONDS_IN_DAY 86400 | ||
| #define LEAP_YEARS_BEFORE_1970 477 | ||
|
|
||
| uint32_t getdate(dateval *date_struct) | ||
| { | ||
| static uint32_t day_arr[12] = {31, 27, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}; // Initialize only once | ||
|
|
||
| if (date_struct != NULL) | ||
| { | ||
| uint32_t since_epoch = RTCDR; | ||
|
|
||
| date_struct->year = 1970 + since_epoch / SECONDS_IN_YEAR; | ||
| date_struct->month = 1 + (since_epoch / SECONDS_IN_MONTH) % 12; | ||
|
|
||
| uint32_t days_since_epoch = since_epoch / SECONDS_IN_DAY; | ||
| uint32_t leap_years_since_epoch = (date_struct->year / 4) - (date_struct->year / 100) + (date_struct->year / 400) - LEAP_YEARS_BEFORE_1970; | ||
| uint32_t not_leap_years = date_struct->year - 1970 - leap_years_since_epoch; | ||
|
|
||
| date_struct->day = days_since_epoch - (leap_years_since_epoch * 366) - (not_leap_years * 365); | ||
|
|
||
| uint32_t is_leap_year = ((date_struct->year % 4 == 0) && (date_struct->year % 100 != 0)) || (date_struct->year % 400 == 0); | ||
| day_arr[1] += is_leap_year; // Extra day if leap year | ||
|
|
||
| for (uint32_t i = 0; i < 12; i++) | ||
| { | ||
| if (date_struct->day > day_arr[i]) | ||
| { | ||
| date_struct->day -= day_arr[i]; | ||
| } | ||
| } | ||
|
|
||
| day_arr[1] -= is_leap_year; // Undo the leap year effect, if any | ||
| } | ||
|
|
||
| return RTCDR; | ||
| } | ||
|
|
||
| uint32_t gettime(timeval *time_struct) | ||
| { | ||
| if (time_struct != NULL) | ||
| { | ||
| uint32_t since_epoch = RTCDR; | ||
|
|
||
| time_struct->secs = since_epoch % 60; | ||
| since_epoch /= 60; | ||
|
|
||
| time_struct->mins = since_epoch % 60; | ||
| since_epoch /= 60; | ||
|
|
||
| time_struct->hrs = since_epoch % 24; | ||
| } | ||
|
|
||
| return RTCDR; | ||
| } |
This file contains hidden or 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
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I feel like this getlines function could be simplified by adding modularity. This would help with improving readability. This is okay for now, but I will probably refactor it in the future.