For a few years now, I’ve been going round-and-round about how to reference pin names. My main objectives are:
- It has to be easy (e.g., short pin names require less typing).
- It has to be clear (i.e., the pin name should indicate the function being accessed without anything extraneous).
- It has to be correct (i.e., there should be no chance of accidentally connecting pins other than the ones you want).
I initially started with just simple name matching. That’s great if you have a simple part like an opamp with I/O names like “
IN-”, etc. But modern parts like microcontrollers can have long pin names that contain every function a pin supports such as “
GP21/USBD+/PWM5/ADC2/VREF”. Typing all that makes reading the code harder and it isn’t clear which pin function you’re trying to access.
That’s when I started using regular expression (regex) matching. If you wanted to reference the second analog-to-digital input, you could use “
.*ADC2.*”. But what if there was another pin with “
ADC22” in its name? Then you’d be picking that one up, too. To avoid that, you could reference it using “
.*/ADC2/.*”. But this makes the pin reference even messier with all the wildcards and slashes.
Then I moved to using a two-phase lookup:
- If the reference matched a pin name exactly, then that’s the pin that was selected.
- If an exact match wasn’t available, then all pins that had a matching substring in their name would be selected.
Two-phase lookup got rid of the need for the wildcards, but you still needed some delimiting characters to prevent inadvertent pin matches. And since regex matching was still being used, you had to escape certain characters that had special meanings (e.g., change “
/USBD+/” to “
/USBD\+/” because “+” means “one or more of the preceding character” in a regex).
For the latest release (0.0.30), I kept the regex matching but implemented a
match_pin_substring flag for
Part objects that enables/disables substring matching for pin names. Substring matching can be enabled for microcontrollers, FPGAs, SOCs that have long pin names. I’m not as happy with that solution as I’d hoped.
In the development branch of SKiDL, I’m trying a new technique using pin aliases. I’ve introduced a new
Part method called
split_pin_names() that will split the part’s pin names into a set of substrings that are assigned as aliases to each pin. So the pin “
GP21/USBD+/PWM5/ADC2/VREF” would keep its original name, but would also be assigned aliases “
ADC2”, and “
VREF”. Then you could reference “
ADC2” and get an exact match without worrying about accidentally matching “
ADC22” from another pin. You could also reference “
USBD+” without needing to escape the “+” character. The one drawback to this is you need to explicitly call
split_pin_names() for any part where you want this to happen. (There’s also a
match_pin_regex flag on each part that you can enable if you absolutely want/need to use regex pin matching.)
Any way, that’s my current thinking. If anyone has any thoughts, I’d like to hear them.