Setting bits in DIRA, OUTA, etc.
Rayman
Posts: 14,665
Looking for the best way to tidy up my spin2cpp'd code...
The original spin looked like this:
After spin2cpp, it works, but looks like this:
I did notice that PropGCC has added setpin, but I don't think this will help for DIRA...
Was looking at this page: http://www.coranac.com/documents/working-with-bits-and-bitfields/
and think I like the bit macros there:
So, now my code looks like this:
Anybody know a more clear way?
The original spin looked like this:
outa[FlashEnable]~~ dira[FlashEnable]~~
After spin2cpp, it works, but looks like this:
OUTA = ((OUTA & 0xfffffdff) | 0x200); DIRA = ((DIRA & 0xfffffdff) | 0x200);
I did notice that PropGCC has added setpin, but I don't think this will help for DIRA...
Was looking at this page: http://www.coranac.com/documents/working-with-bits-and-bitfields/
and think I like the bit macros there:
#define BIT(n) ( 1<<(n) ) #define BIT_SET(y, mask) ( y |= (mask) ) #define BIT_CLEAR(y, mask) ( y &= ~(mask) ) #define BIT_FLIP(y, mask) ( y ^= (mask) )
So, now my code looks like this:
BIT_SET(OUTA,BIT(Flashenable)); BIT_SET(DIRA,BIT(Flashenable));
Anybody know a more clear way?
Comments
setpin(pin)
clearpin(pin)
instead of:
setpin(pin,value)
Wouldn't that make the inline assembly 2X faster too?
Also, if you're going to add something to propeller2.h, you should add the same thing to propeller1.h. The whole idea of these functions is to allow writing code that works on both chips.
If you wanted to be really creative, you could define a new "operator"
SRLM, I like that!
But, I guess I should get in line and use setpin...
I'm not sure I like offp though because it sounds like it toggles DIR instead of actually turning it off...
And so, making that as simple and clear as possible (and fast too) should be important...
As it's currently written it will be two instructions, but there are other ways to write it. I'm sure we could re-work it so that it'll come out to 1 instruction on constants and 2 on unknown quantities. I think this would be preferable to introducing a bunch of additional macros.
Eric
Now, I think they should be kept seperate from setpin, if you want P1 and P2 code to do the same things...
never mind...
Actually, after looking at Propeller1.h, I see you've already made it so setpin works the same for P1 and P2.
getpin can't really work the same because the hardware is different.
The P2 version returns the output state, not the actual input state. But, I suppose these would
usually be the same.
The prop1 version makes the pin an output too, maybe the real P2 does this too.
I think it'd be nice if there were a P2 command to return the input state of any pin.
If I'm reading it right, that only comes from reading the PINX registers...
Update: pedward just said I have getpin wrong and it returns the input state.
In this case, I think the P1 version of getpin shouldn't be setting OUTA...