-
Notifications
You must be signed in to change notification settings - Fork 67
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
serial_dma in a loop #155
Comments
I'm not sure about that one, maybe @ra-kete has an idea? |
Yes, that's an unfortunate limitation. Seems like it might be resolved soon-ish though: rust-lang/rfcs#2909 Until then you'll have to do the mutable assignments manually, which is more verbose but not too bad otherwise. loop {
let sending = tx.write_all(tx_buf, tx_channel);
let receiving = rx.read_exact(rx_buf, rx_channel);
let (buf, channel, x) = sending.wait();
tx_buf = buf;
tx_channel = channel;
tx = x;
let (buf, channel, x) = receiving.wait();
rx_buf = buf;
rx_channel = channel;
rx = x;
assert_eq!(tx_buf, rx_buf);
} To limit the verbosity you can pull the individual parts together into tuples and move these between loop iterations instead: let mut send = (tx_buf, tx_channel, tx);
let mut recv = (rx_buf, rx_channel, rx);
loop {
let (tx_buf, tx_channel, tx) = send;
let (rx_buf, rx_channel, rx) = recv;
let sending = tx.write_all(tx_buf, tx_channel);
let receiving = rx.read_exact(rx_buf, rx_channel);
send = sending.wait();
recv = receiving.wait();
assert_eq!(send.0, recv.0);
} This is what you are doing in your example if I'm not mistaken. Or, another alternative would be to move the let mut sending = tx.write_all(tx_buf, tx_channel);
let mut receiving = rx.read_exact(rx_buf, rx_channel);
loop {
let (tx_buf, tx_channel, tx) = sending.wait();
let (rx_buf, rx_channel, rx) = receiving.wait();
assert_eq!(tx_buf, rx_buf);
sending = tx.write_all(tx_buf, tx_channel);
receiving = rx.read_exact(rx_buf, rx_channel);
} All three approaches are readable enough I would say. I hope I didn't totally misunderstand the issue at hand here ;) If not I'd be happy to extend the serial_dma example with one of the above code snippets (probably the last one as that looks best to me personally). |
I look forward to the destructing proposed by rust-lang/rfcs#2909 , but with practice I am becoming less bothered by needing to keep the structure intact while looping. Thank you for your response. If you have a chance, I have some related questions. The first is that I have been trying to set up mut variables before the loop and avoid The second question is concerning I think the start vs trigger process may be a hint to the instability (need to type slowly) in the example in my original post. Should the Finally, I am finding that different hals have different functionally, and different names for similar functionality around dma serial transfer. The embedded-dma crate seems to be either used in or base on stm32f3xx-hal? Is there any hope that might lead to a common approach in other stm32 hals? |
Following the example
serial_dma
which does not loop I have worked out an example that in a loop reads and echos a string of characters (see below). This seems uglier and more fragile than I expected, and I am wondering if I have missed something that I should be doing. (Perhaps my novice Rust status means I just don't properly appreciate beauty yet?) The difficulty is that looping seems to require variables be modified rather than re-assigned, but because of difficulty destructuring assignments the tuples returned byrx1.read_exact()
andtx1.write_all()
cannot be pulled apart. My work around is to keep the tuples assend
andrecv
and just reference the parts for function calls, but I would prefer more readable code. There are more comments in the code below.So this issue is only that it would be nice if the example
serial_dma
could be improved to show how to properly read/write in a loop. The code below does work, but does not handle fast typing very well.The text was updated successfully, but these errors were encountered: