-
Notifications
You must be signed in to change notification settings - Fork 19
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
Add time_rtc example #23
Conversation
I'll put this briefly on hold until #22 is closed, since that will require small changes in the Edit: Also, though it may not look like because of the work done to work with |
Got it, thanks. Re:
I guess that calling the |
I don't see how |
Yeah, I thought so too, but the devkitpro-included example in C worked fine, and the Rust example didn't work until I switched from using The implementation of |
That chain of calls is pretty interesting! I found out that there's some other initialization that seems to happen automatically: |
Okay, I think I found the actual cause of the problem. All that reentrancy stuff seems like a red herring, and this was a simple ABI mismatch. With this diff in diff --git a/src/unix/newlib/mod.rs b/src/unix/newlib/mod.rs
index b1300f169..f1d738cb0 100644
--- a/src/unix/newlib/mod.rs
+++ b/src/unix/newlib/mod.rs
@@ -28,9 +28,16 @@ pub type socklen_t = u32;
pub type speed_t = u32;
pub type suseconds_t = i32;
pub type tcflag_t = ::c_uint;
-pub type time_t = i32;
pub type useconds_t = u32;
+cfg_if! {
+ if #[cfg(target_os = "horizon")] {
+ pub type time_t = ::c_longlong;
+ } else {
+ pub type time_t = i32;
+ }
+}
+
s! {
// The order of the `ai_addr` field in this struct is crucial
// for converting between the Rust and C types. The code works as expected with I suppose it's to be expected that bindgen would catch something like this vs handwritten bindings. To that end, I wonder if there's an easy way to automate type checking between the two and update |
Good find! Definitely make a PR against our libc fork |
That is also interesting, as it seems to imply there is not truly a need for the Rust code: let hid = Hid::init().expect("Couldn't obtain HID controller");
let apt = Apt::init().expect("Couldn't obtain APT controller"); That said, the API handle those provide makes sense to me, so I'm not sure this should change much in the way of "idiomatic" code. Rust is all about explicit and those appInit calls seem a bit ... magic, in my mind. But good to know about in general! |
Yes, many |
@ian-h-chamberlain Looking back your code I noticed something a bit obvious: you are not using This obviously depends on the |
Yes, I think when I tried before that |
Opened Meziu/libc#2 for libc changes |
|
||
// Get the current time. ctru_sys bindings should be used rather than | ||
// plain libc ones, for reasons I don't understand yet... | ||
let unix_time: ctru_sys::time_t = unsafe { ctru_sys::time(std::ptr::null_mut()) }; |
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.
It would be great if this used std, though I see there's some discussion about how that might require some more work...
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.
It wouldn't really require more work. It requires libc
's PR to be merged...
ctru-rs/examples/time_rtc.rs
Outdated
// Get the current time. ctru_sys bindings should be used rather than | ||
// plain libc ones, for reasons I don't understand yet... | ||
let unix_time: ctru_sys::time_t = unsafe { ctru_sys::time(std::ptr::null_mut()) }; | ||
let time = unsafe { ptr::read(ctru_sys::gmtime(&unix_time as *const _)) }; |
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.
If you aren't able to use std, then maybe replace this ptr::read
with a simple dereference?
let time = unsafe { ptr::read(ctru_sys::gmtime(&unix_time as *const _)) }; | |
let time = unsafe { *ctru_sys::gmtime(&unix_time as *const _) }; |
Co-authored-by: Mark Drobnak <[email protected]>
Wait, |
@ian-h-chamberlain make this compatible with the latest |
@Meziu done, let me know if there's anything else needed. I can look into the |
Ref #18
Finally, I was able to get an example running for
time/rtc
, which as far as I can tell is functionally the same as the devkitpro version! It took me a while to figure out I should be usingctru_sys::*
instead oflibc::*
– with the latter I was getting garbage data fromgmtime
. Any idea why that is? I suspect it has something to do with the linkage or initialization of these two reentrancy structures, but that's just a suspicion.I also switched the dep to be
libc = "0.2"
since there is already a patch in the workspace root and it takes precedence AFAIK. If we want a more specific version, or e.g. differentfeatures
, I can make that change as well.Edit: oops, I meant to attach screenshots.
ctru_sys
functions:libc
functions (plus a debug print which shows the result fromtime()
seems to be correct):