This thread has been locked.

If you have a related question, please click the "Ask a related question" button in the top right corner. The newly created question will be automatically linked to this question.

Assign a mnemonic name to PORT.PIN

With DriverLib there is a way to write code like 

GPIO_setAsOutputPin( MYPIN );

where MYPIN is for example GPIO_PORT_P3.GPIO_PIN0 ?

Thank you

  • Hi Leonardo,

    Simply using

    #define MYPIN GPIO_PORT_P3

    Should do. I usualy also do since the base normally is necessary (i use TivaWare which the API is about the same)

    #define MYPIN_Periph SYSCTL_PERIPH_GPIOF

    #definy MYPIN_Base GPIO_PORTF_BASE

     

    This are names for the Tiva, i don't know MSPWare names.

     

  • Thank you Luis,

    But I would a unique mnemonic for port + pin..

  • Try like this (again i use this in Tiva):

    #define MYPIN GPIO_PORTF_BASE,GPIO_PORT_P3

     

    I believe to write a output you still need the pin to set it to HIGH right? So you should also have

    #define MYPIN_ON GPIO_PORT_P3

  • Individual bits are not addressable, so you can’t define a symbol for a pin in a way you can do x=1 then.
    You can define a bitfield, and then a symbol for an element in a bitfield, but bitfields are inefficient if used on hardware registers (the compiler cannot do optimizations then).
    Also, the bit order in bitfields is not specified by the C language, so using them may be not portable.

    What you can do is to create symbols that perform a complete operation.

    #define MYPIN_SET() GPIO_PORT_P3|=GPIO_PIN0
    #define MYPIN_CLEAR() GPIO_PORT_P3&=~GPIO_PIN0

    MYPIN_SET();
    ...
    MYPIN_CLEAR();

  • Jens-Michael,

    Good information.

    The Driverlib API that OP is using is verbose in terms of the amount of typing required if you use the predefined macro constants.

    Example:

    //*****************************************************************************
    //
    // Prototypes for the APIs.
    //
    //*****************************************************************************
    extern void GPIO_setAsOutputPin(uint8_t selectedPort,
                                    uint16_t selectedPins);
    
    

    and the macros are defined like:

    #define GPIO_PORT_P1                                                          1
    #define GPIO_PORT_P2                                                          2
    #define GPIO_PORT_P3                                                          3
    .....
    
    #define GPIO_PIN0                                                      (0x0001)
    #define GPIO_PIN1                                                      (0x0002)
    #define GPIO_PIN2                                                      (0x0004)
    #define GPIO_PIN3                                                      (0x0008)
    #define GPIO_PIN4                                                      (0x0010)
    #define GPIO_PIN5                                                      (0x0020)
    .....

    so that an LED on Port 2.3 would require a function call like this:

    GPIO_setAsOutputPin(GPIO_PORT_P2, GPIO_PIN3);
    
    
    

    and the solution to the OP's question is to define a macro that expands a single identifier into the entire parameter set for the function call. Example:

    // MSP430Ware Driverlib GPIO macro example
    
    // Port/Pin Expansion Macro
    #define MY_LED    GPIO_PORT_P2,GPIO_PIN3
    
    // Configure the LED pin
    GPIO_setAsOutputPin(MY_LED);
    
    // Turn LED on
    GPIO_setOutputHighOnPin(MY_LED);
    
    // Turn LED off
    GPIO_setOutputLowOnPin(MY_LED);
    
    // Toggle the LED
    GPIO_toggleOutputOnPin(MY_LED);
    
    
    

  • Good idea.

    However, I don't like the fact that it still is a function call.

    Instead of a single 5-cycle 3-word instruction which is not even clobbering a processor register, it is a function call with clobbering of the parameter registers, and some runtime calculations that could be done by the compiler at compile time, if it were a macro.

     I wonder how a simple endless loop over the toggle function performs, compared to a loop over a simple XOR. A factor of ten times slower? or worse?

**Attention** This is a public forum