emerent has quit [Ping timeout: 252 seconds]
emerent has joined #rust-embedded
<re_irc> <@j​ohn_socha:m​atrix.org> Let's say I have the following:
<re_irc> <@j​ohn_socha:m​atrix.org> ```rs
<re_irc> <@j​ohn_socha:m​atrix.org> gpioc.pc7.into_open_drain_output(&mut gpioc.crl);
<re_irc> <@j​ohn_socha:m​atrix.org> As I understand, this implements the trait `Output<OpenDrain>`. How would I pass that to a function? I'm new to Rust, so not sure if this is lack of Rust knowledge, or if this is lack of hal knowledge.
<re_irc> <@h​enrik_alser:m​atrix.org> john_socha: Do you mean what the function signature would look like?
<re_irc> <@g​rantm11235:m​atrix.org> The `into_open_drain_output` function returns the *type* `stm32f1xx_hal::gpio::gpioc::PC7<Output<OpenDrain>>`, which implements the `embedded_hal::blocking::digital::OutputPin` trait
<re_irc> <@j​ohn_socha:m​atrix.org> Thanks Grant. Let me try that.
<re_irc> <@g​rantm11235:m​atrix.org> You can write a function that takes `stm32f1xx_hal::gpio::gpioc::PC7<Output<OpenDrain>>` or you can write a generic function that can take anything that implements the `OutputPin` trait
emerent has quit [Read error: Connection reset by peer]
<re_irc> <@j​ohn_socha:m​atrix.org> I'm trying to make it so it can take different pins rather than a specific pin. This is what I tried:
<re_irc> <@j​ohn_socha:m​atrix.org> ```rs
<re_irc> <@j​ohn_socha:m​atrix.org> pin.is_low().unwrap()
<re_irc> <@j​ohn_socha:m​atrix.org> fn test_pin(&mut self, pin: OutputPin) {
emerent has joined #rust-embedded
<re_irc> <@b​urrbull:m​atrix.org> `OutputPin<Error=core::convert::Infallible>`
<re_irc> <@g​rantm11235:m​atrix.org> ```rust
<re_irc> <@g​rantm11235:m​atrix.org> where
<re_irc> <@g​rantm11235:m​atrix.org> P: InputPin;
<re_irc> <@g​rantm11235:m​atrix.org> fn test_pin<P>(&mut self, pin: P)
<re_irc> <@j​ohn_socha:m​atrix.org> ```rs
<re_irc> <@j​ohn_socha:m​atrix.org> fn test_pin(&mut self, pin: OutputPin<Error = core::convert::Infallible>) {
<re_irc> <@j​ohn_socha:m​atrix.org> pin.is_low().unwrap()
<re_irc> <@j​ohn_socha:m​atrix.org> }
<re_irc> <@g​rantm11235:m​atrix.org> `is_low()` comes from the `InputPin` trait
<re_irc> <@j​ohn_socha:m​atrix.org> Doh!
<re_irc> <@j​ohn_socha:m​atrix.org> fn test_pin(&mut self, pin: InputPin<Error = core::convert::Infallible>) {
<re_irc> <@j​ohn_socha:m​atrix.org> pin.is_low()
<re_irc> <@j​ohn_socha:m​atrix.org> }
<re_irc> <@j​ohn_socha:m​atrix.org> error[E0277]: the size for values of type `(dyn embedded_hal::digital::v2::InputPin<Error = Infallible> + 'static)` cannot be known at compilation time
<re_irc> <@g​rantm11235:m​atrix.org> But because that is a trait not a type, you can't just put it in the function signature like that
<re_irc> <@g​rantm11235:m​atrix.org> GrantM11235: The `<P>` is how you declare a generic type
<re_irc> <@j​ohn_socha:m​atrix.org> Yea, I'm coming from the world of interfaces, assuming I can use a trait as an interface like I would in C++ or a higher-level language like C#.
<re_irc> <@j​ohn_socha:m​atrix.org> GrantM11235: I've tried to figure out what the second where, or what the syntax should be. When I use exactly what you have above, I get:
<re_irc> <@j​ohn_socha:m​atrix.org> error: expected one of `!` or `::`, found `:`
<re_irc> <@j​ohn_socha:m​atrix.org> --> src\main.rs:118:17
<re_irc> <@j​ohn_socha:m​atrix.org> |
fabic_ has joined #rust-embedded
fabic has quit [Ping timeout: 255 seconds]
<re_irc> <@o​sten_music:m​atrix.org> Hey is there an example for the stm32-rs to adjust the startup code so that I can initialize variables in RAM sections?
fabic_ has quit [Ping timeout: 252 seconds]
<re_irc> <@m​arcuss2:t​edomum.net> john_socha: Its closer to a concept in C++20
<re_irc> <@m​arcuss2:t​edomum.net> The extra features over a traditional interface is default implementation based on other methods
<re_irc> <@m​arcuss2:t​edomum.net> And the fact that you can statically require a structure to implement some interface, negating the need late binding
fabic_ has joined #rust-embedded
fabic_ has quit [Ping timeout: 268 seconds]
<re_irc> <@a​damgreig:m​atrix.org> john_socha: you can also use trait objects, which will be more like using an interface in c++ - it does dynamic dispatch at runtime using a vtable, whereas making the function generic over P monomorphises at compile-time, e.g.:...
<re_irc> <@a​damgreig:m​atrix.org> (basically the difference between `fn foo<T: InputPin>(x: &T)` and `fn foo(x: &dyn InputPin)`)
rjframe has joined #rust-embedded
fabic_ has joined #rust-embedded
rjframe has quit [Remote host closed the connection]
rjframe has joined #rust-embedded
tokomak has quit [Ping timeout: 250 seconds]
rjframe has quit [Ping timeout: 268 seconds]
starblue1 has quit [Ping timeout: 245 seconds]
starblue1 has joined #rust-embedded
<re_irc> <@f​irefrommoonlight:m​atrix.org> I always wondered why the pin: OutputPin doesn't work. I understand there are reasons, but it would be a nicer API
<re_irc> <@f​irefrommoonlight:m​atrix.org> It makes refactoring a pain
fabic_ has quit [Ping timeout: 258 seconds]
<re_irc> <@f​irefrommoonlight:m​atrix.org> @agg - that looks like nicer syntax, but it sounds like there are downsides?
<re_irc> <@f​irefrommoonlight:m​atrix.org> Eg the normal trait syntax creates throwaway symbols, and adds additional syntax not normally present
<re_irc> <@f​irefrommoonlight:m​atrix.org> It does so in a way that adds no useful information from the user's perspective
<re_irc> <@f​irefrommoonlight:m​atrix.org> Ie syntactic noise and clutter
crabbedhaloablut has quit [Remote host closed the connection]
crabbedhaloablut has joined #rust-embedded
<re_irc> <@m​athias_koch:m​atrix.org> What are today's options for debugging stack overflows? I cannot understand what hugs all my stack, and i can't seem to make any of japarics stack-size tools work with a recent compiler version?
<re_irc> <@d​irbaio:m​atrix.org> The stack-sizes should work, I used it a few weeks ago
<re_irc> <@f​irefrommoonlight:m​atrix.org> Uh.... Comment out lines of code or add empty loops at diff places until it works???
<re_irc> <@f​irefrommoonlight:m​atrix.org> (I am also curious)
<re_irc> <@d​irbaio:m​atrix.org> https://github.com/japaric/stack-sizes
<re_irc> <@f​irefrommoonlight:m​atrix.org> Man, I'm really considering RTIC for my next project. I think it would be just be syntactic changes from my current flow. I'm adapting a bluetooth example that uses RTIC, and it's a 1:1 translation to the Mutex-based approach I'm using
<re_irc> <@f​irefrommoonlight:m​atrix.org> eg:
<re_irc> <@f​irefrommoonlight:m​atrix.org> ```rust
<re_irc> <@f​irefrommoonlight:m​atrix.org> rc: RadioCoprocessor<'static>,
<re_irc> <@f​irefrommoonlight:m​atrix.org> struct Resources {
<re_irc> <@f​irefrommoonlight:m​atrix.org> And
<re_irc> <@f​irefrommoonlight:m​atrix.org> ```rust
<re_irc> <@f​irefrommoonlight:m​atrix.org> rc,
<re_irc> <@f​irefrommoonlight:m​atrix.org> init::LateResources {
<re_irc> <@f​irefrommoonlight:m​atrix.org> `fn idle(mut cx: idle::Context) -> ! {` is `loop {}`
<re_irc> <@f​irefrommoonlight:m​atrix.org> ```rust
<re_irc> <@f​irefrommoonlight:m​atrix.org> #[task(binds = IPCC_C1_RX_IT, resources = [rc])]
<re_irc> <@f​irefrommoonlight:m​atrix.org> fn mbox_rx(cx: mbox_rx::Context) {
<re_irc> <@f​irefrommoonlight:m​atrix.org> cx.resources.rc.handle_ipcc_rx();
<re_irc> <@f​irefrommoonlight:m​atrix.org> The RTIC syntax looks well-designed for this pattern, and is more elegant. Although it has all the caveats DSLs do. Then again, the macros I use are a DSL too
<re_irc> <@m​athias_koch:m​atrix.org> dirbaio: I just run into https://github.com/japaric/stack-sizes/issues/9
<re_irc> <@d​irbaio:m​atrix.org> Yeah I saw that too, iirc it was that I didn't do the linker script thing correctly
<re_irc> <@m​athias_koch:m​atrix.org> The additional sections to keep the .stack-sizes? I thought i did that 🤔
<re_irc> <@d​irbaio:m​atrix.org> 🤔
<re_irc> <@d​irbaio:m​atrix.org> I'm afk, can try it again later and report back if you want
<re_irc> <@d​irbaio:m​atrix.org> But it did definitely work
<re_irc> <@m​athias_koch:m​atrix.org> But knowing that you had it working on recent compiler versions, makes me a bit more hopeful! I'll go back and try again 👍
<re_irc> <@d​irbaio:m​atrix.org> It was on small embassy examples. Maybe it's broken on some edge case that only happens in bigger / more complex bins dunno
<re_irc> <@m​athias_koch:m​atrix.org> I'll give it a go! Thanks
<re_irc> <@f​irefrommoonlight:m​atrix.org> RTIC question, where my 1:1 mapping fails: What do `tasks` that aren't associated with an interrupt do? Eg:
<re_irc> <@f​irefrommoonlight:m​atrix.org> ```rust
<re_irc> <@f​irefrommoonlight:m​atrix.org> fn exec_hci(mut cx: exec_hci::Context) {
<re_irc> <@f​irefrommoonlight:m​atrix.org> #[task(resources = [rc, hci_commands_queue, ble_context])]
<re_irc> <@f​irefrommoonlight:m​atrix.org> Ie equiv of:
<re_irc> <@f​irefrommoonlight:m​atrix.org> ```rust
<re_irc> <@f​irefrommoonlight:m​atrix.org> free(|cs| {
<re_irc> <@f​irefrommoonlight:m​atrix.org> fn exec_hci() {
<re_irc> <@t​halesfragoso:m​atrix.org> firefrommoonlight: One is a trait and the other is a type, and functions take types, you can use `impl Trait` as a way to use generics in functions arguments
<re_irc> <@t​halesfragoso:m​atrix.org> firefrommoonlight: Iirc you need to give RTIC interrupts to use tasks, since they're dispatched through interrupts
<re_irc> <@t​halesfragoso:m​atrix.org> If you don't bind to a specific interrupt you still need to provide one interrupt per task priority
<re_irc> <@b​radleyharden:m​atrix.org> firefrommoonlight, the search term for tasks not bound to an interrupt is software tasks. As mentioned, each priority level must have an associated interrupt that is responsible for dispatching software tasks.
<re_irc> <@b​radleyharden:m​atrix.org> By the way, you might consider working from v0.6alpha5 rather than v0.5. I think v0.6 is nearing release, and there are a lot of nice features and upgrades to be had
<re_irc> <@b​radleyharden:m​atrix.org> For instance, you can spawn software tasks from anywhere, rather than having to enumerate which tasks will be spawned from which other tasks
<re_irc> <@b​radleyharden:m​atrix.org> The resource syntax is also better.
<re_irc> <@b​radleyharden:m​atrix.org> Oh, and declaring which interrupts are used for software tasks is also way better.
<re_irc> <@f​irefrommoonlight:m​atrix.org> Thank you for the clarifications and info. I'll go with `impl Trait` from here on. I had memories of trying before and it not working, but I evidently was doing something else wrong
<re_irc> <@f​irefrommoonlight:m​atrix.org> In this case, those functions I was copying from RTIC were called in the idle loop, eg:
<re_irc> <@f​irefrommoonlight:m​atrix.org> ```rust
<re_irc> <@f​irefrommoonlight:m​atrix.org> cx.spawn.setup().unwrap();
<re_irc> <@f​irefrommoonlight:m​atrix.org> for a task wrapping a `setup` function. I'm assuming the caller is where the interrupt context comes from? Or in this case, the main loop context?
<re_irc> <@f​irefrommoonlight:m​atrix.org> That software tasks link is v helpful
<re_irc> <@t​halesfragoso:m​atrix.org> firefrommoonlight: It has its downsides too, namely no turbofish
<re_irc> <@f​irefrommoonlight:m​atrix.org> From that link, it appears the software tasks are more nuanced than my mapping. Eg with priorities
<re_irc> <@f​irefrommoonlight:m​atrix.org> The <T: OutputPin> syntax isn't too bad - I was confused about why the simpler variant wasn't used instead since it seems to be neater from an API perspective, but that's idealistic
<cr1901> https://twitter.com/cr1901/status/1418345211550384135 Does this ring a bell to anyone? I want to say it involves volatile ops, but I can't find the relevant issue or RFC
<cr1901> It would've been a semi-recent RFC/issue (2021), but I can't find it for the life of me
<cr1901> In any case, I'm giving up on this. Wish it was easier to reach Ralf than email or Zulip
<re_irc> <@f​irefrommoonlight:m​atrix.org> Link is broken
<re_irc> <@t​halesfragoso:m​atrix.org> Not even sure if UnsafeCell really relax the noalias stuff
<re_irc> <@t​halesfragoso:m​atrix.org> Iirc there are talks to have a type that did that, they even did that for !Unpin types when they really enabled the noalias on LLVM (it last like a week)
<re_irc> <@t​halesfragoso:m​atrix.org> If you get a `&mut T` from an `UnsafeCell` you still invalidate all others references to it, also raw pointers
<re_irc> <@b​radleyharden:m​atrix.org> cr1901 it definitely rings a bell. I'm pretty sure it was a proposal to fix the Pin soundness issue
<re_irc> <@b​radleyharden:m​atrix.org> But I can't find it on a quick search
<re_irc> <@t​halesfragoso:m​atrix.org> bradleyharden: this one https://github.com/rust-lang/rust/issues/63818 ?
<re_irc> <@b​radleyharden:m​atrix.org> Yes, I think so
<re_irc> <@b​radleyharden:m​atrix.org> It was UnsafeAliasedCell
<re_irc> <@b​radleyharden:m​atrix.org> firefrommoonlight, yes software tasks are pretty useful
<re_irc> <@b​radleyharden:m​atrix.org> FYI, there have been big changes to the Monotonic trait for scheduling in v0.6
<cr1901> bradleyharden: YES! THIS WAS IT!!
<cr1901> Thank you
<cr1901> And link works fine for me
<cr1901> err thalesfragoso, thank you for the link
<cr1901> and thank you bradleyharden for jogging the memories :)
<cr1901> I was unhappy that I couldn't find it after some effort to review browser history