-
Notifications
You must be signed in to change notification settings - Fork 116
Implement Goldfish RTC #613
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
base: master
Are you sure you want to change the base?
Changes from all commits
9452d47
cb8e07e
1b91d35
0ae1bb2
4e7ed84
5ee534a
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
|
@@ -45,3 +45,8 @@ BR2_TARGET_ROOTFS_CPIO_NONE=y | |||||
BR2_PACKAGE_BUSYBOX_CONFIG="busybox.config" | ||||||
BR2_PACKAGE_COREMARK=y | ||||||
BR2_PACKAGE_DHRYSTONE=y | ||||||
BR2_PACKAGE_UTIL_LINUX=y | ||||||
BR2_PACKAGE_UTIL_LINUX_HWCLOCK=y | ||||||
|
||||||
BR2_PACKAGE_RTC_ALARM=y | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Unknown Buildroot symbol BR2_PACKAGE_RTC_ALARM is enabled but no package definition exists in the repo; this will be ignored by Kconfig or break reproducibility. Disable or add proper Buildroot package metadata. Prompt for AI agents
Suggested change
|
||||||
BR2_PACKAGE_RTC_SETTIME=y | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Unknown Buildroot symbol BR2_PACKAGE_RTC_SETTIME is enabled but no package definition exists in the repo; this will be ignored by Kconfig or break reproducibility. Disable or add proper Buildroot package metadata. Prompt for AI agents
Suggested change
|
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
|
@@ -29,7 +29,7 @@ | |||||
|
||||||
#define ARRAY_SIZE(arr) (sizeof(arr) / sizeof(arr[0])) | ||||||
|
||||||
#define MASK(n) (~((~0U << (n)))) | ||||||
#define MASK(n) (~((~0UL << (n)))) | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Left shift can be undefined when n >= bit-width of unsigned long; guard against out-of-range shift to avoid UB. Prompt for AI agents
Suggested change
|
||||||
|
||||||
#if defined(_MSC_VER) | ||||||
#include <intrin.h> | ||||||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,120 @@ | ||
/* | ||
* rv32emu is freely redistributable under the MIT License. See the file | ||
* "LICENSE" for information on usage and redistribution of this file. | ||
*/ | ||
|
||
#include <assert.h> | ||
#include <errno.h> | ||
#include <fcntl.h> | ||
#include <stdbool.h> | ||
#include <stdio.h> | ||
#include <stdlib.h> | ||
#include <string.h> | ||
#include <time.h> | ||
#include <unistd.h> | ||
|
||
#include "rtc.h" | ||
|
||
static uint64_t now_nsec; | ||
|
||
uint64_t get_now_nsec(rtc_t *rtc) | ||
{ | ||
/* TODO: | ||
* - detects timezone and use the correct UTC offset | ||
* - a new CLI option should be added to main.c to let user to select | ||
* [UTC] or [UTC + offset](localtime) time. E.g., -x rtc:utc or -x | ||
* rtc:localtime | ||
*/ | ||
struct timespec ts; | ||
clock_gettime(CLOCK_REALTIME, &ts); | ||
return (uint64_t) (ts.tv_sec * 1e9) + ts.tv_nsec + rtc->clock_offset; | ||
} | ||
|
||
uint32_t rtc_read(rtc_t *rtc, uint32_t addr) | ||
{ | ||
uint32_t rtc_read_val = 0; | ||
|
||
switch (addr) { | ||
case RTC_TIME_LOW: | ||
now_nsec = get_now_nsec(rtc); | ||
rtc->time_low = (uint32_t) (now_nsec & MASK(32)); | ||
rtc_read_val = rtc->time_low; | ||
break; | ||
case RTC_TIME_HIGH: | ||
/* reuse the now_nsec when reading RTC_TIME_LOW */ | ||
rtc->time_high = (uint32_t) (now_nsec >> 32); | ||
rtc_read_val = rtc->time_high; | ||
break; | ||
case RTC_ALARM_LOW: | ||
rtc_read_val = rtc->alarm_low; | ||
break; | ||
case RTC_ALARM_HIGH: | ||
rtc_read_val = rtc->alarm_high; | ||
break; | ||
case RTC_ALARM_STATUS: | ||
rtc_read_val = rtc->alarm_status; | ||
break; | ||
default: | ||
rv_log_error("Unsupported RTC read operation, 0x%x", addr); | ||
break; | ||
} | ||
|
||
return rtc_read_val; | ||
} | ||
|
||
void rtc_write(rtc_t *rtc, uint32_t addr, uint32_t value) | ||
{ | ||
switch (addr) { | ||
case RTC_TIME_LOW: | ||
now_nsec = get_now_nsec(rtc); | ||
rtc->clock_offset += (uint64_t) (value) - (now_nsec & MASK(32)); | ||
break; | ||
case RTC_TIME_HIGH: | ||
/* reuse the now_nsec when writing RTC_TIME_LOW */ | ||
rtc->clock_offset += ((uint64_t) (value) << 32) - | ||
(now_nsec & ((uint64_t) (MASK(32)) << 32)); | ||
break; | ||
case RTC_ALARM_LOW: | ||
rtc->alarm_low = value; | ||
break; | ||
case RTC_ALARM_HIGH: | ||
rtc->alarm_high = value; | ||
break; | ||
case RTC_IRQ_ENABLED: | ||
rtc->irq_enabled = value; | ||
break; | ||
case RTC_CLEAR_ALARM: | ||
rtc->clear_alarm = value; | ||
break; | ||
case RTC_CLEAR_INTERRUPT: | ||
rtc->clear_interrupt = value; | ||
rtc->interrupt_status = 0; | ||
break; | ||
default: | ||
rv_log_error("Unsupported RTC write operation, 0x%x", addr); | ||
break; | ||
} | ||
return; | ||
} | ||
|
||
rtc_t *rtc_new() | ||
{ | ||
rtc_t *rtc = calloc(1, sizeof(rtc_t)); | ||
assert(rtc); | ||
|
||
/* | ||
* The rtc->time_low/high values can be updated through the RTC_SET_TIME | ||
* ioctl operation. Therefore, they should be initialized to match the | ||
* host OS time during initialization. | ||
*/ | ||
now_nsec = get_now_nsec(rtc); | ||
rtc->time_low = (uint32_t) (now_nsec & MASK(32)); | ||
rtc->time_high = (uint32_t) (now_nsec >> 32); | ||
|
||
return rtc; | ||
} | ||
|
||
void rtc_delete(rtc_t *rtc) | ||
{ | ||
free(rtc); | ||
} |
Uh oh!
There was an error while loading. Please reload this page.
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.
Misleading exit code: use exit 3 for command timeout to match "Fail to run commands" message.
Prompt for AI agents