-
Notifications
You must be signed in to change notification settings - Fork 87
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
Serializing NaiveDatetime
on AVR crashes
#99
Comments
I think this code: postcard/src/ser/serializer.rs Lines 321 to 388 in 62c0547
collect_str method) were added due to issues with chrono (see #32).
If I had to guess, I would suspect an AVR miscompilation of |
What do you mean by
I didn't notice any I tried to probe this thing some more, but the only thing I discovered, is that But I'm not sure how to proceed from here. Should I try to somehow inline |
As far as I am aware, there are quite a few parts under the hood of the An interesting test might be something like: use core::fmt::Write;
let mut buf: heapless::String<256> = heapless::String::new();
let data: NaiveDateTime = ...; // your test data here
write!(&mut buf, "{}", &data).unwrap(); And see if that works. This would use the same If that works on other targets (desktop, cortex-m), and NOT on AVR, I'd be pretty convinced it was something wrong with the AVR code generation. If that DOES work on AVR, then I'd certainly be confused! |
When I said that
I meant that I already tried something similar. This is what i did: struct SerialWriter<'a, T> {
serial: &'a mut T,
}
impl<'a, T> Write for SerialWriter<'a, T> where T: uWrite {
fn write_str(&mut self, s: &str) -> core::result::Result<(), core::fmt::Error> {
ufmt::uwrite!(&mut self.serial, "{}", s).map_err(|_| core::fmt::Error::default())?;
Ok(())
}
}
let mut serial_writer = SerialWriter { serial: &mut serial };
let entry = NaiveDateTime::MIN;
write!(&mut serial_writer, "{}", entry).unwrap();
// This outputs:
// -262144-01-01 00:00:00
// over the serial connection this works on AVR. I tried out your code as well. It produces the same output when printed out with Btw, I searched for "AVR miscompilation" in the Rust repo and found this rust-lang/rust#74743 which does indicate that the AVR backend has issues with |
It might be worth trying without using |
Here's how I invoked your code. It doesn't crash. use core::fmt::Write;
let mut buf: heapless::String<256> = heapless::String::new();
let data: NaiveDateTime = NaiveDateTime::MIN; // your test data here
// This uses core::fmt::Write
write!(&mut buf, "{}", &data).unwrap();
// This uses ufmt::Write. Added for convenience to check the results
ufmt::uwriteln!(&mut serial, "{}", buf.as_str()).unwrap(); AFAIK I'm exercising |
btw, the newest nightly (since rust-lang/rust#114048) brought lots of fixes for the AVR backend - it might be worthwhile to recheck on it 🙂 |
Thanks, I'll have a look as soon as I get a chance. |
Whenever i try to serialize a
chrono::NaiveDateTime
or any struct that contains it internally, the program resets, if it's being run on an AVR microcontroller (ATmega328p specifically). It can be as simple as:Serializing other stuff seems to work fine, e.g., this example code from the docs works as expected:
Also, the same code runs without issue on x64, which makes me suspect a miscompilation on AVR but verifying that seems to be beyond my ability currently (tried looking at the AVR disassembly but there's simply too much going on there).
I'm building the code using the
nightly-2023-03-24
toolchain.Minimal reproducible example
main.rs
Cargo.toml
Other things to note
I'm using
postcard
rev62c05473f76ead781aaa9d4a3e4057b86e543747
with the following diff applied to it to work around #82heapless
is at rev644653bf3b831c6bb4963be2de24804acf5e5001
.The text was updated successfully, but these errors were encountered: