sajattack / bitbang-hal

Implements embedded-hal traits by bitbanging
MIT License
40 stars 10 forks source link

Added function so the spi frequency can be changed after its creation. #20

Closed diondokter closed 4 years ago

diondokter commented 4 years ago

I'm using this SPI to talk to a device that needs a slow frequency during initialization. But after that I do want more speed. With this function that's possible.

sajattack commented 4 years ago

Could the same thing be implemented with a mutable reference and either a destroy or replace method, without unsafe?

diondokter commented 4 years ago

@sajattack That would be nice, yes. But the thing is that we don't know what the timer looks like. It may not have a function to change the speed. In that case, the timer needs to be freed and constructed again. That can only happen when you get ownership.

I ran into that when I needed to change the SPI frequency of a peripheral. https://docs.rs/stm32h7xx-hal/0.5.0/stm32h7xx_hal/spi/struct.Spi.html As you can see, there's no function to change the speed other than to call free and build it again.

And like the comment says, the zeroed value is never used, so it is safe. If you want, I could change it to return an &mut Timer, but that'd decrease the usefulness IMHO.

sajattack commented 4 years ago

I see. Well I hope you can understand I'm a bit wary of unsafe code. But if there's really no better way I'll accept it.

diondokter commented 4 years ago

Made a change. The code still does the same, but it better shows what's actually unsafe. Maybe this makes it more palatable for you?

I'm a bit wary of unsafe code And so you should! But I think this is a valid usecase for unsafe.

sajattack commented 4 years ago

Ok thanks for understanding. I'll sleep on it, how's that? Remind me if I haven't merged or commented again in 24 hours.