Ok, so I have a number of hardware pins which are defined by port+pin
numbers. Each pin can be connected to different peripheral (e.g. generic
purpose i/o, serial, analog in) which defines it's function.
So, to configure that I have a few u32 registers where each pair of bits
defines a function for a pin, so my configuration function is:
fn set_mode(port: u8, pin: u8, fun: Function)
You pass in port, pin and it's function, and i/o is being reconfigured
appropriately.
What I have now is this enum:
enum Function {
GPIO = 0,
F1 = 1,
F2 = 2,
F3 = 3,
}
which defines pin's function (function 0 is always GPIO on all the pins,
functions 1-3 are different based on the pin).
Now, in some peripheral init code I write something like:
gpio::Pin::new(0, 15, gpio::Out, gpio::F1); // p13 -> TXD
and I know that function 1 on port 0 pin 15 is TXD for UART1. I could have
misread the docs and write F2, which would compile, but the code wouldn't
configure the UART pin but do something else, so I want the compiler to
check code for me so that
gpio::Pin::new(0, 15, gpio::Out, gpio::UART1_TXD); // p13 -> TXD
compiles, but something like
gpio::Pin::new(0, 15, gpio::Out, gpio::SPI_MISO); // p13 -> TXD
would fail, because there's no SPI_MISO defined for port 0 pin 15.
On Sun, Mar 30, 2014 at 11:41 AM, Simon Sapin <[email protected]> wrote:
> On 30/03/2014 10:30, Vladimir Pouzanov wrote:
>
>> That shifts the issue even more into runtime, doesn't it?
>>
>> My goal is to find a way to resolve a tuple of (u8, u8, FuncNameEnum) to
>> u8, where FuncNameEnum is a long enum of all possible functions. And I
>> need to do that in compile time.
>>
>> One way I see that is to make a really long list of enums like:
>>
>> PORT0_PIN0_GPIO,
>> PORT0_PIN0_RD1,
>> ...
>>
>> for all possible port/pin/function combinations, then I can make a macro
>> to match on that to port/pin/fun u8 (that are actually used to configure
>> registers). The only thing is that I'd like to pass port and pin in as
>> u8 values, that makes it much more readable.
>>
>> Well... Maybe it's time to do some big macro for matching all the stuff
>> and hope that compiler will optimise it anyway.
>>
>
> I’m sorry, it looks like I completely misunderstood your original message.
> But then it’s still not clear to me what you’re trying to do. Could you
> explain in more details, with more context? For example, what is it you
> call a "function"? It seems not to be Rust function declared with the `fn`
> keyword.
>
> --
> Simon Sapin
>
--
Sincerely,
Vladimir "Farcaller" Pouzanov
http://farcaller.net/
_______________________________________________
Rust-dev mailing list
[email protected]
https://mail.mozilla.org/listinfo/rust-dev