Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. How can I pass macros with commas into other macros?

How can I pass macros with commas into other macros?

Scheduled Pinned Locked Moved C / C++ / MFC
questionhardwarehelptutorial
3 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • A Offline
    A Offline
    arnold_w
    wrote on last edited by
    #1

    I am writing code for an embedded ARM processor and I am compiling with GNU GCC. I want to do the following:

    #define PA0 GPIOA, GPIO_PIN0

    #define PIN_DEF(PORT, PIN) \

    GPIO_TypeDef* PORT; \

    uint16_t PIN;

    PIN_DEF(PA0)

    However, I get an error message saying PIN_DEF requires 2 arguments but I'm only giving it 1. Does anybody know how to solve this?

    L L 2 Replies Last reply
    0
    • A arnold_w

      I am writing code for an embedded ARM processor and I am compiling with GNU GCC. I want to do the following:

      #define PA0 GPIOA, GPIO_PIN0

      #define PIN_DEF(PORT, PIN) \

      GPIO_TypeDef* PORT; \

      uint16_t PIN;

      PIN_DEF(PA0)

      However, I get an error message saying PIN_DEF requires 2 arguments but I'm only giving it 1. Does anybody know how to solve this?

      L Offline
      L Offline
      Lost User
      wrote on last edited by
      #2

      You have defined it as PIN_DEF(PORT, PIN), so it requires two parameters. Either change the definition, or provide the second parameter. You could try

      #define PA0 (GPIOA, GPIO_PIN0)

      1 Reply Last reply
      0
      • A arnold_w

        I am writing code for an embedded ARM processor and I am compiling with GNU GCC. I want to do the following:

        #define PA0 GPIOA, GPIO_PIN0

        #define PIN_DEF(PORT, PIN) \

        GPIO_TypeDef* PORT; \

        uint16_t PIN;

        PIN_DEF(PA0)

        However, I get an error message saying PIN_DEF requires 2 arguments but I'm only giving it 1. Does anybody know how to solve this?

        L Offline
        L Offline
        leon de boer
        wrote on last edited by
        #3

        The generic answer is you can use __VA_ARGS__ its used to pass variable arguments thru macros, something like this is generally how it used to feed a multi variable function

        #define LOG_DEBUG(...) printf(__VA_ARGS__)

        For what you are doing you pass the arguments thru two macros the first take any number of variables the second macro enforces it is using just two values.

        #define PA0 GPIOA, GPIO_PIN0
        #define PIN_DEF_A(A, B) \
        GPIO_TypeDef* PORT; \
        uint16_t PIN;
        #define PIN_DEF(...) PIN_DEFA(__VA_ARGS__)

        Your code doesn't make a lot of sense to me because I can't see the types. I am guessing GPIO_TypeDef* is a volatile to a 16 bit port and what you are trying to do is MACRO the output of a 16 bit value which would usually look actually like this

        *((volatile uint16_t*) 0xXXXXXXXXX = 0x????;
        // So something like this ... send 0x1234 to port 0x3F000000
        *((volatile uint16_t*) 0x3F000000) = 0x1234;

        Using the above macro form for that would be

        #define PA0 0x3F000000,0x1234
        #define PIN_DEFA(A, B) *((volatile uint16_t*) (A)) = (B)
        #define PIN_DEF(...) PIN_DEFA(__VA_ARGS__)

        //USING IT
        PIN_DEF(PA0); // This expands to *((volatile uint16_t*) 0x3F000000) = 0x1234;

        In vino veritas

        1 Reply Last reply
        0
        Reply
        • Reply as topic
        Log in to reply
        • Oldest to Newest
        • Newest to Oldest
        • Most Votes


        • Login

        • Don't have an account? Register

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • World
        • Users
        • Groups