Microcontroller has many pins, every defined like
const Leg PA29 { PIOA, BIT(29) }; // struct Pin is already taken
// ... about 120 more: 4 port x 32 pin
I wrote a simple define
to make alias in shorter form
#define P(Port,PinN) \
const Leg P##Port##PinN { PIO##Port, BIT(PinN) }
Using it as
P(D,2); //produces PD2 with { PIOD, BIT(2) }
Nice.
Now I need-wont call P
120+ times for 4 ports with 32 pins in each. I would like to see something like
FOR_EACH( X in A,B,C,D ) \
FOR_EACH( i in 0..31 ) \
P(X,i);
Please do not suggest TCL, python etc. to generate C++ code.
I found an answer, but it is to complicated to understand how to use it in my case.
The main idea is to avoid 120 rows with copy-paste. All 120+ pins should be defined in about 10 lines.
upd. How BIT
is defined:
///@param n is value from 0 to number of bits in unsigned value
template<typename UnsignedIntegerT=unsigned>
constexpr UnsignedIntegerT BIT(UnsignedIntegerT n){ return 1<<n; }
upd2. Minimal example
///////////////////
// Vendor provides something like:
///////////////////
struct Pio
{
unsigned reg1;
unsigned reg2;
unsigned reg3;
//...
unsigned regN;
};
// values not from datasheet but from lantern
#define PIOA ((Pio*)0xAABB6789)
#define PIOB ((Pio*)0xAABB2569)
#define PIOC ((Pio*)0xAABB2566)
#define PIOD ((Pio*)0xAABB2323)
//...
/////////////
// Kyb's code
/////////////
class Leg
{
public:
Pio *pio;
unsigned pinN;
//methods...
};
///@param n is value from 0 to number of bits in unsigned value
template<typename UnsignedIntegerT=unsigned>
constexpr UnsignedIntegerT BIT(UnsignedIntegerT n){ return 1u<<n; }
//////////////
// Now need to define 120+ pins-legs
// like this
const Leg PA29 { PIOA, BIT(29) };
// or with shortener alias
/// Alias to shortly produce leg definition
/// Example: `P(A,2)` will define `PA2` with proper PIO and bit.
#define P(Port,PinN) \
const Leg P##Port##PinN { PIO##Port, BIT<unsigned>(PinN) }
//P(D,4); //produces PD4
//P(C,3);