stm32-rs / bxcan

bxCAN peripheral driver for STM32 chips
Apache License 2.0
31 stars 22 forks source link

Implement `free()` method #28

Closed Sh3Rm4n closed 3 years ago

Sh3Rm4n commented 3 years ago

In the case I understood the API correctly, it is currently not possible to retrieve the peripheral consumed with Can::new associated with the Instance trait

https://github.com/stm32-rs/bxcan/blob/f4d5d9676b1653eaf7e51beb222e5af72e6baf1a/src/lib.rs#L49

To retrieve the peripheral, maybe a free() method should be implemented like that:

diff --git a/src/lib.rs b/src/lib.rs
index 2584107..f589e00 100644
--- a/src/lib.rs
+++ b/src/lib.rs
@@ -470,6 +470,10 @@ where
     pub fn split(self) -> (Tx<I>, Rx<I>) {
         unsafe { (Tx::conjure(), Rx::conjure()) }
     }
+
+    pub fn free(self) ->  I {
+        self.instance
+    }
 }

 impl<I: FilterOwner> Can<I> {

This is probably not enough, I guess. I have only glanced over the implementation, but I imagine, that the peripheral has to be deconfigured to safely free it.

jonas-schievink commented 3 years ago

Sounds reasonable to have this, yeah. I'm not entirely sure what free should do exactly, since some stuff is up to the HAL (like turning off the clock).

Sh3Rm4n commented 3 years ago

Because free in the HAL is usually about freeing the consumed pins, it has to wrap this free function anyways, so leave it up entirely to the HAL?

jonas-schievink commented 3 years ago

Yeah, that could work