0

I defined this macro:

#define DEF_CONCAT(a, b)        a ## b
#define _internal_RCC(gpio, io) DEF_CONCAT(RCC_GPIO, gpio)
#define _internal_IO(gpio, io)  DEF_CONCAT(GPIO, io)

#define IO_CFG_OUTPUT(gpio) {rcc_periph_clock_enable(_internal_RCC(gpio));gpio_set_mode(gpio, GPIO_MODE_OUTPUT_50_MHZ, GPIO_CNF_OUTPUT_PUSHPULL, _internal_IO(gpio));}

And I want to call this with:

IO_CFG_OUTPUT(LED_STATE);

LED_STATE is defined as: #define LED_STATE C,12

But when I run my program, the compiler tells me:

In file included from inc/Robot.hpp:6:0,
                 from src/Robot.cpp:1:
src/Robot.cpp: In member function 'void Robot::setup()':
inc/IODefines.hpp:13:19: error: 'C' was not declared in this scope
 #define LED_STATE C,12
                   ^

What did I do wrong?

Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
Martin Fischer
  • 697
  • 1
  • 6
  • 27

1 Answers1

2

Run gcc -E on the source to see what your macros expand to...

What it expands to is {rcc_periph_clock_enable(RCC_GPIOC);gpio_set_mode(C,12, GPIO_MODE_OUTPUT_50_MHZ, GPIO_CNF_OUTPUT_PUSHPULL, GPIO12);};. Or with more readable formatting:

{
    rcc_periph_clock_enable(RCC_GPIOC);
    gpio_set_mode(C,12, 
                  GPIO_MODE_OUTPUT_50_MHZ, 
                  GPIO_CNF_OUTPUT_PUSHPULL, 
                  GPIO12);
};

Note the questionable practice of putting a semi-colon after the }, it could result in surprising behavior. The normal way to expand to a compound statement that works as a normal is to use do { body; } while(0)

The problem is that C is not defined anywhere.

The error message looks a bit confusing though. There's nothing formally wrong with your define directive. Whether they do what you want on the other hand I cannot tell, you should be able to tell that.

skyking
  • 13,817
  • 1
  • 35
  • 57
  • Absolutely not the solution, btw, you should read comments. it was already solved. the error was that i used gpio in gpio_set_mode instead of _internal_... – Martin Fischer Aug 08 '15 at 18:32
  • Perhaps not the solution to your problem, but I think it's the answer to the actual question (ie "What did I do wrong"). There's no chance in hell to tell from the question that it should be *internal...*. – skyking Aug 10 '15 at 05:39