views:

130

answers:

3

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.

+6  A: 

Ok, here. No runtime cost.

#define CLAIM(n) struct busy##n {}

CLAIM(58);
CLAIM(58);

If run twice it will error out:

z.c:4: error: redefinition of ‘struct busy58’

To extend the check to multiple compilation units you will want to wrap the macro in #if DEBUG because we would be using the linker to detect the clash and hence would have a runtime footprint.

#define CLAIM(n) char busy##n = 1;
#define CLAIM(n) void busy##n() {} // bdonlan
DigitalRoss
However this only errors if they're in the same translation unit.
bdonlan
+8  A: 
#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) {}
Mark Ransom
This uses one byte per pin. If that's a problem, the OP might want to look into linker options to put these definitions into their own section, and drop them from the final image.
bdonlan
It has been so long since I used an architecture where one byte mattered, I hadn't even thought of that.
Mark Ransom
But it doesn't error. It's perfectly legal C to do that, even in multiple compilation units.
DigitalRoss
How about using a function instead of a char?
bdonlan
A function, good idea!
DigitalRoss
+1 Very nice idea ~~
Alphaneo
+2  A: 

If you can afford the runtime overhead or if this is just for debugging, I'd just create something like an IOPinOpen() function that kept track of pins in use instead of dealing with macro trickery.

On the other hand, Mark Ransom's updated answer was worth a +1.

Michael Burr