-
Notifications
You must be signed in to change notification settings - Fork 21
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 #15 from lhw2002426/rtc
Rtc support
- Loading branch information
Showing
23 changed files
with
566 additions
and
8 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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
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
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
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
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 @@ | ||
rtc |
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,39 @@ | ||
#include <stdio.h> | ||
#include <sys/time.h> | ||
|
||
int main() | ||
{ | ||
struct timeval tv; | ||
if (gettimeofday(&tv, NULL) != 0 ) { | ||
perror("gettimeofday"); | ||
return -1; | ||
} | ||
|
||
printf("now time: %ld : %ld\n", tv.tv_sec,tv.tv_usec); | ||
|
||
usleep(3000000); | ||
|
||
if (gettimeofday(&tv, NULL) != 0 ) { | ||
perror("gettimeofday"); | ||
return -1; | ||
} | ||
|
||
printf("now time: %ld : %ld\n", tv.tv_sec,tv.tv_usec); | ||
|
||
struct timeval new_time; | ||
new_time.tv_sec = 1731110400; | ||
new_time.tv_usec = 0; | ||
|
||
if (settimeofday(&new_time, NULL) != 0 ) { | ||
perror("settimeofday"); | ||
return -1; | ||
} | ||
if (gettimeofday(&tv, NULL) != 0 ) { | ||
perror("gettimeofday"); | ||
return -1; | ||
} | ||
|
||
printf("now time: %ld : %ld\n", tv.tv_sec,tv.tv_usec); | ||
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
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
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,74 @@ | ||
/* Copyright (c) [2023] [Syswonder Community] | ||
* [Rukos] is licensed under Mulan PSL v2. | ||
* You can use this software according to the terms and conditions of the Mulan PSL v2. | ||
* You may obtain a copy of Mulan PSL v2 at: | ||
* http://license.coscl.org.cn/MulanPSL2 | ||
* THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE. | ||
* See the Mulan PSL v2 for more details. | ||
*/ | ||
|
||
//! PL031 RTC. | ||
static RTC_DR: u32 = 0x000; //Data Register | ||
static RTC_MR: u32 = 0x004; //Match Register | ||
static RTC_LR: u32 = 0x008; //Load Register | ||
static RTC_CR: u32 = 0x00c; //Control Register | ||
static RTC_IMSC: u32 = 0x010; //Interrupt Mask Set or Clear register | ||
|
||
const PHYS_RTC: usize = axconfig::PHYS_VIRT_OFFSET + 0x09010000; | ||
|
||
static PL031_RTC: Pl031rtc = Pl031rtc { address: PHYS_RTC }; | ||
|
||
pub fn init() { | ||
info!("Initialize pl031 rtc..."); | ||
PL031_RTC.init(); | ||
debug!("{}", rtc_read_time()); | ||
} | ||
|
||
struct Pl031rtc { | ||
address: usize, | ||
} | ||
|
||
impl core::fmt::Display for Pl031rtc { | ||
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result { | ||
write!( | ||
f, | ||
"RTC DR: {}\nRTC MR: {}\nRTC LR: {}\nRTC CR: {}\nRTC_IMSC: {}", | ||
unsafe { self.read(RTC_DR) } as u64, | ||
unsafe { self.read(RTC_MR) } as u64, | ||
unsafe { self.read(RTC_LR) } as u64, | ||
unsafe { self.read(RTC_CR) } as u64, | ||
unsafe { self.read(RTC_IMSC) } as u64 | ||
) | ||
} | ||
} | ||
|
||
impl Pl031rtc { | ||
fn init(&self) { | ||
unsafe { | ||
if self.read(RTC_CR) != 1 { | ||
self.write(RTC_CR, 1); | ||
} | ||
} | ||
} | ||
|
||
pub unsafe fn read(&self, reg: u32) -> u32 { | ||
core::ptr::read_volatile((PHYS_RTC + reg as usize) as *const u32) | ||
} | ||
|
||
pub unsafe fn write(&self, reg: u32, value: u32) { | ||
core::ptr::write_volatile((PHYS_RTC + reg as usize) as *mut u32, value); | ||
} | ||
|
||
pub fn time(&self) -> u64 { | ||
unsafe { self.read(RTC_DR) as u64 } | ||
} | ||
} | ||
|
||
pub fn rtc_read_time() -> u64 { | ||
PL031_RTC.time() | ||
} | ||
|
||
pub fn rtc_write_time(seconds: u32) { | ||
unsafe { PL031_RTC.write(RTC_LR, seconds) }; | ||
} |
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
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
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
Oops, something went wrong.