Ref::GPIO

Leo Selavo edited this page Feb 4, 2016 · 9 revisions

GPIO controls general purpose pins, for setting their levels low and high, as well as for reading their level values.

Location

Functions

Note, that port and pin may be symbolic values for certain platforms. Strictly speaking, the implementations of the functions below are often macros.

Commonly used functions

void pinAsInput( port, pin );
void pinAsOutput( port, pin );
int pinRead( port, pin );
void pinWrite( port, pin, value );

All functions

Pins

// Configure the pin in data output mode (writing possible)
#define pinAsOutput( po, pi )
// Configure the pin in data input mode (reading possible)
#define pinAsInput( po, pi )
// Configure the pin in data (the default) mode. Reading/writing possible
#define pinAsData( po, pi )
// Configure the pin in function mode. The function depends on the MCU and the pin.
#define pinAsFunction( po, pi )

// Set a digital output pin to 1
#define pinSet( po, pi )
// Set a digital output pin using the pin's bitmask
#define pinSetMask( po, mask )
// Clear a digital output pin to 0
#define pinClear( po, pi )
// Clear a digital output pin using the pin's bitmask
#define pinClearMask( po, mask )
// Toggle (change the value of) a digital output pin
#define pinToggle( po, pi )
// Toggle a digital output pin using the pin's bitmask
#define pinToggleMask( po, mask )
// Read the value of a digital input pin. Returns either 1 or 0
#define pinRead( po, pi )
// Set a digital pin output to a specific value (logical 0 or 1). Interprets 'val' as a boolean
#define pinWrite( po, pi, val )

Ports

// Configure the whole port in output mode
#define portAsOutput( po )
// Configure the whole port in input mode
#define portAsInput( po )
// Read the whole port at once
#define portRead( po )
// Write the whole port at once
#define portWrite( po, val )

Interrupts

// Enable interrupt on a digital pin
#define pinEnableInt( po, pi )
// Disable interrupt on a digital pin
#define pinDisableInt( po, pi )
// Configure interrupt in rising edge mode
#define pinIntRising( po, pi )
// Configure interrupt in falling edge mode
#define pinIntFalling( po, pi )
// Check if interrupt is configured in rising edge mode on the pin
#define pinIsIntRising( po, pi )
// Check if interrupt flag is set on a pin
#define pinReadIntFlag( po, pi )
// Clear the interrupt flag on a pin. Note: until the flag is cleared, no further interrupts can be received on the pin.
#define pinClearIntFlag( po, pi )
// Put a pin in data output mode and write a binary value on it
#define digitalWrite(po, pi, val)
// Put a pin in data input mode and read a binary value from it
#define digitalRead(po, pi)

Named pins and functions

There is a mechanism for defining meaningful pin names that are extended with typical pin functions. First, define the name for the pin with PIN_DEFINE(), then use it with the function names attached.

    PIN_DEFINE( name, port, pin );

The full list of the named pin functions is as follows:

    static inline void nameAsOutput();
    static inline void nameAsInput();
    static inline uint_t nameRead();
    static inline void nameWrite(uint8_t val);
    static inline void nameHigh();
    static inline void nameLow();
    static inline void nameToggle();

Example

Using port and pin number

#include "digital.h"

int val;

// Initialize pins
pinAsInput( 1, 2 );    // Declare Port 1 pin 2 as input
pinAsOutput( 3, 4 );   // Declare Port 1 pin 2 as output

// Use pins
val = pinRead( 1, 2);  // Read the pin value
pinWrite( 3, 4, val);  // Write a value to the pin

Using named pins

PIN_DEFINE( myEnable, 2, 3 );
myEnableAsOutput();
myEnableWrite(1);
You can’t perform that action at this time.
You signed in with another tab or window. Reload to refresh your session. You signed out in another tab or window. Reload to refresh your session.
Press h to open a hovercard with more details.