I'm writing firmware for a PIC32MX, using HiTech PICC32. One of the problems I want to avoid is that since most of the pins have multiple names (eg. AN0 = RB0 = CN2 = PGED1), I or someone else might accidentally use RB0 without realising that AN0 is already used. (This can actually be catastrophic, since incorrectly configuring an analogue/digital pin can lead to excessive current draw and release of essential smoke.)
As well as comprehensively documenting every pin used, I was wondering if there was a quick way to head this issue off at the level of coding. I want a macro that people (mainly myself) can use, say CLAIM_PIN(58)
, that will issue a warning or error if it is run twice.
(I don't want this at all costs, if the only possible solution is too horrendous or unmaintainable then I'll forget about it and just develop a reputation for bursting into tears or setting myself on fire or something. I also saw this question about macro producing macros, which rules out that.)
I should clarify: the code IS written in multiple compilation units (at least, I think this is what the phrase means). I have a .h/.c file for my A2D code, similarly for SPI, and similarly for various peripherals that just use certain I/O ports. Space is not really a problem, my code leaves plenty of room on the PIC32MX; also I can use another __DEBUG flag to remove the pin checking code for final use.
#define CLAIM_PIN(n) char claimed_pin_##n;
Now when two pieces of code try to claim a pin, the symbol will be doubly defined and either the compiler or the linker will generate an error.
Edit: Based on comments, this might turn out better:
#define CLAIM_PIN(n) void claimed_pin_#nn(void) {}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With