I started to look into how components could be shared by multiple boards. This would help board designers and capsule authors by making it easier to [correctly] include a new feature for a platform, and reduce the amount of duplicated code to include a capsule.
I ran into a issue where my attempt to make the types generic does not sit well with the Rust compiler. I tried starting with the LED component, and I was trying to use it for imix, hail, and nrf52:
The idea is that the Output type is not fixed, and is instead a template value. When trying what I thought was my best attempt, I get this error:
Compiling components v0.1.0 (file:///Users/bradjc/git/tock/boards/components)
error[E0401]: can't use type parameters from outer function
--> /Users/bradjc/git/tock/boards/components/src/led.rs:40:31
|
35 | impl<T: 'static + gpio::Pin + gpio::PinCtl> Component for LedComponent<T> {
| - type variable from outer function
...
38 | unsafe fn finalize(&mut self) -> Self::Output {
| -------- try adding a local type parameter in this method instead
39 | static_init!(
40 | led::LED<'static, T>,
| ^ use of type variable from outer function
error: aborting due to previous error
For more information about this error, try `rustc --explain E0401`.
error: Could not compile `components`.
warning: build failed, waiting for other jobs to finish...
error: build failed
make: *** [target/thumbv7em-none-eabi/release/imix] Error 101
I had no luck in trying to fix that error, or try a different approach. The full branch is here: https://github.com/tock/tock/compare/shared-components
Anyone have an idea of what structure might work with Rust? Or if I am just doing something silly?
Having this is still a dream of mine. Creating a new board would be so easy.
Never seen this error before looks like a rustc bug to me, for example this works
Hmm...but I guess the problem is this doesn't
OK yes so the issue is that creating a static variable cannot be based on a type parameter. With a little finagling I have come up with this:
...which is actually kind of reasonable. I'm not sure how to support different numbers of LEDs, but ignoring that for a minute this is what the main.rs code looks like:
...which is pretty close to what I was picturing when starting this. That annoying "helper" line is there only to statically allocate space for the pins array and the led capsule state. I _think_ that could be removed if the entire component was made in to a macro...but that might be too much.
Anyone have any comments? I'd like to try this on something more complicated (and find a way around the array length issue...) because while not ideal, it would still make setting up a new board pretty slick and hopefully much more foolproof than without components, while saving a lot of code duplication.
This has actually seen some progress in #1338 and should be something to merge after 1.4.
Can we mark this closed?
No, we still don't have a way to do gpio, button, led, or any other variable length components.
Can this be closed now? Or are there any other components that still need to be made generic? ADC maybe?
Yes! This issue is just to be able to have shared components, we will eventually actually use shared components.