title: LUT in a macro C
body: <p>I am currently working on setting up a framework in C for usage between several microcontrollers.
  The framework has to carry all device specific code, so the application only contains the abstract usage of peripherals (like SerialPort_Read, write, SetBaudRate, etc etc.)</p>
  
  <p>One of the things I am struggling with to find a solution for in C are the I/O pin map. I've seen projects (like the very very popular Arduino) where the pin map is putten in a LUT  (look up table) which is used during runtime. However, this LUT will never be modified during runtime, so there is no use to have this in the memory. 
  For example, this function resolves some bit indexes and registers from some 'const uint' tables, and either sets or clears a bit:</p>
  
  <pre><code>void pinMode(uint8_t pin, uint8_t mode)
  {
          uint8_t bit = digitalPinToBitMask(pin);
          uint8_t port = digitalPinToPort(pin);
          volatile uint8_t *reg;
  
          if (port == NOT_A_PIN) return;
  
          // JWS: can I let the optimizer do this?
          reg = portModeRegister(port);
  
          if (mode == INPUT) { 
                  uint8_t oldSREG = SREG;
                  cli();
                  *reg &amp;= ~bit;
                  SREG = oldSREG;
          } else {
                  uint8_t oldSREG = SREG;
                  cli();
                  *reg |= bit;
                  SREG = oldSREG;
          }
  }
  </code></pre>
  
  <p>Because this is actual C code running on the controller it's draining effiency and speed. I'd rather define some sort of macro that does the same thing, but is already resolved during compilation to a 'one-liner' that can be compiled much more efficiently:</p>
  
  <pre><code>GPIO_Write(PORTA, 5, 1); // Write '1' to pin 5 on PORTA
  &gt; LATA |= 1&lt;&lt;5; // Sets bit 5 high
  GPIO_Tris(PORTA, 4, OUTPUT); // Set pin 4 on PORTA to output
  &gt; PORTA &amp;= ~(1&lt;&lt;4); // sets pin 4 as output I/O type
  </code></pre>
  
  <p>Does anyone know if it's possible (and how) to define and use a look-up table with a macro in C?</p>
  
  <p>At this moment I am using the MicroChip C30 compiler, which I believe is based in GCC. It's supposed to be portable between different compilers, including MicroChip C18, C32 and in further also ARM and AVR.</p>