mraa  2.0.0
Low Level Skeleton Library for Communication on GNU/Linux platforms
Data Structures | Functions | Typedefs | Enumerations
gpio.h File Reference

API Description

Gpio is the General Purpose IO interface to libmraa. Its features depend on the board type used, it can use gpiolibs (exported via a kernel module through sysfs), or memory mapped IO via a /dev/uio device or /dev/mem depending again on the board configuration.

/* initialize GPIO pin */
gpio_1 = mraa_gpio_init(GPIO_PIN_1);
if (gpio_1 == NULL) {
fprintf(stderr, "Failed to initialize GPIO %d\n", GPIO_PIN_1);
return EXIT_FAILURE;
}
/* initialize GPIO pin */
gpio_2 = mraa_gpio_init(GPIO_PIN_2);
if (gpio_2 == NULL) {
fprintf(stderr, "Failed to initialize GPIO %d\n", GPIO_PIN_2);
return EXIT_FAILURE;
}
/* set GPIO to output */
status = mraa_gpio_dir(gpio_1, MRAA_GPIO_OUT);
if (status != MRAA_SUCCESS) {
goto err_exit;
}
/* set GPIO to output */
status = mraa_gpio_dir(gpio_2, MRAA_GPIO_OUT);
if (status != MRAA_SUCCESS) {
goto err_exit;
}
/* toggle both GPIO's */
while (flag) {
status = mraa_gpio_write(gpio_1, 1);
if (status != MRAA_SUCCESS) {
goto err_exit;
}
status = mraa_gpio_write(gpio_2, 0);
if (status != MRAA_SUCCESS) {
goto err_exit;
}
sleep(1);
status = mraa_gpio_write(gpio_1, 0);
if (status != MRAA_SUCCESS) {
goto err_exit;
}
status = mraa_gpio_write(gpio_2, 1);
if (status != MRAA_SUCCESS) {
goto err_exit;
}
sleep(1);
}
/* release gpio's */
status = mraa_gpio_close(gpio_1);
if (status != MRAA_SUCCESS) {
goto err_exit;
}
/* close GPIO */
status = mraa_gpio_close(gpio_2);
if (status != MRAA_SUCCESS) {
goto err_exit;
}

Go to the source code of this file.

Data Structures

struct  mraa_gpio_event
 

Functions

mraa_gpio_context mraa_gpio_init (int pin)
 
mraa_gpio_context mraa_gpio_init_multi (int pins[], int num_pins)
 
mraa_gpio_context mraa_gpio_init_raw (int gpiopin)
 
mraa_result_t mraa_gpio_edge_mode (mraa_gpio_context dev, mraa_gpio_edge_t mode)
 
mraa_result_t mraa_gpio_isr (mraa_gpio_context dev, mraa_gpio_edge_t edge, void(*fptr)(void *), void *args)
 
mraa_gpio_events_t mraa_gpio_get_events (mraa_gpio_context dev)
 
mraa_result_t mraa_gpio_isr_exit (mraa_gpio_context dev)
 
mraa_result_t mraa_gpio_mode (mraa_gpio_context dev, mraa_gpio_mode_t mode)
 
mraa_result_t mraa_gpio_dir (mraa_gpio_context dev, mraa_gpio_dir_t dir)
 
mraa_result_t mraa_gpio_read_dir (mraa_gpio_context dev, mraa_gpio_dir_t *dir)
 
mraa_result_t mraa_gpio_close (mraa_gpio_context dev)
 
int mraa_gpio_read (mraa_gpio_context dev)
 
mraa_result_t mraa_gpio_read_multi (mraa_gpio_context dev, int output_values[])
 
mraa_result_t mraa_gpio_write (mraa_gpio_context dev, int value)
 
mraa_result_t mraa_gpio_write_multi (mraa_gpio_context dev, int input_values[])
 
mraa_result_t mraa_gpio_owner (mraa_gpio_context dev, mraa_boolean_t owner)
 
DEPRECATED mraa_result_t mraa_gpio_use_mmaped (mraa_gpio_context dev, mraa_boolean_t mmap)
 
int mraa_gpio_get_pin (mraa_gpio_context dev)
 
int mraa_gpio_get_pin_raw (mraa_gpio_context dev)
 
mraa_result_t mraa_gpio_input_mode (mraa_gpio_context dev, mraa_gpio_input_mode_t mode)
 
mraa_result_t mraa_gpio_out_driver_mode (mraa_gpio_context dev, mraa_gpio_out_driver_mode_t mode)
 

Typedefs

typedef struct _gpio * mraa_gpio_context
 
typedef long long unsigned int mraa_timestamp_t
 
typedef mraa_gpio_eventmraa_gpio_events_t
 

Enumerations

enum  mraa_gpio_mode_t {
  MRAA_GPIO_STRONG = 0, MRAA_GPIO_PULLUP = 1, MRAA_GPIO_PULLDOWN = 2, MRAA_GPIO_HIZ = 3,
  MRAA_GPIOD_ACTIVE_LOW = 4, MRAA_GPIOD_OPEN_DRAIN = 5, MRAA_GPIOD_OPEN_SOURCE = 6
}
 
enum  mraa_gpio_dir_t { MRAA_GPIO_OUT = 0, MRAA_GPIO_IN = 1, MRAA_GPIO_OUT_HIGH = 2, MRAA_GPIO_OUT_LOW = 3 }
 
enum  mraa_gpio_edge_t { MRAA_GPIO_EDGE_NONE = 0, MRAA_GPIO_EDGE_BOTH = 1, MRAA_GPIO_EDGE_RISING = 2, MRAA_GPIO_EDGE_FALLING = 3 }
 
enum  mraa_gpio_input_mode_t { MRAA_GPIO_ACTIVE_HIGH = 0, MRAA_GPIO_ACTIVE_LOW = 1 }
 
enum  mraa_gpio_out_driver_mode_t { MRAA_GPIO_OPEN_DRAIN = 0, MRAA_GPIO_PUSH_PULL = 1 }
 

Function Documentation

mraa_gpio_context mraa_gpio_init ( int  pin)

Initialise gpio_context, based on board number

Parameters
pinPin number read from the board, i.e IO3 is 3
Returns
gpio context or NULL

Here is the caller graph for this function:

mraa_gpio_context mraa_gpio_init_multi ( int  pins[],
int  num_pins 
)

Initialise gpio_context, based on board number, for multiple pins (can be one).

Parameters
pinsPin array read from the board
num_pinsNumber of pins - must be the same as the pins array length provided as the first argument.
Returns
gpio context or NULL
mraa_gpio_context mraa_gpio_init_raw ( int  gpiopin)

Initialise gpio context without any mapping to a pin

Parameters
gpiopingpio pin as listed in SYSFS
Returns
gpio context or NULL

Here is the caller graph for this function:

mraa_result_t mraa_gpio_edge_mode ( mraa_gpio_context  dev,
mraa_gpio_edge_t  mode 
)

Set the edge mode on the gpio

Parameters
devThe Gpio context
modeThe edge mode to set the gpio into
Returns
Result of operation

Here is the caller graph for this function:

mraa_result_t mraa_gpio_isr ( mraa_gpio_context  dev,
mraa_gpio_edge_t  edge,
void(*)(void *)  fptr,
void *  args 
)

Set an interrupt on pin(s).

Parameters
devThe Gpio context
edgeThe edge mode to set the gpio(s) into
fptrFunction pointer to function to be called when interrupt is triggered
argsArguments passed to the interrupt handler (fptr)
Returns
Result of operation

Here is the caller graph for this function:

mraa_gpio_events_t mraa_gpio_get_events ( mraa_gpio_context  dev)

Get an array of structures describing triggered events.

Parameters
devThe Gpio context
Returns
Array of structures containing pairs of pin id's and the associated timestamp. An event with negative id value indicates that no event was triggered for the respective pin. The array length is that of the number of pins provided in mraa_gpio_init_multi().
mraa_result_t mraa_gpio_isr_exit ( mraa_gpio_context  dev)

Stop the current interrupt watcher on this Gpio, and set the Gpio edge mode to MRAA_GPIO_EDGE_NONE(only for sysfs interface).

Parameters
devThe Gpio context
Returns
Result of operation

Here is the caller graph for this function:

mraa_result_t mraa_gpio_mode ( mraa_gpio_context  dev,
mraa_gpio_mode_t  mode 
)

Set Gpio(s) Output Mode,

Parameters
devThe Gpio context
modeThe Gpio(s) Output Mode
Returns
Result of operation

Here is the caller graph for this function:

mraa_result_t mraa_gpio_dir ( mraa_gpio_context  dev,
mraa_gpio_dir_t  dir 
)

Set Gpio(s) direction

Parameters
devThe Gpio context
dirThe direction of the Gpio(s)
Returns
Result of operation

Here is the caller graph for this function:

mraa_result_t mraa_gpio_read_dir ( mraa_gpio_context  dev,
mraa_gpio_dir_t dir 
)

Read Gpio(s) direction

Parameters
devThe Gpio context
dirThe address where to store the Gpio(s) direction
Returns
Result of operation

Here is the caller graph for this function:

mraa_result_t mraa_gpio_close ( mraa_gpio_context  dev)

Close the Gpio context

  • Will free the memory for the context and unexport the Gpio - sysfs interface.
  • Will free up the memory used by context and close related file descriptors - chardev interface.
Parameters
devThe Gpio context
Returns
Result of operation

Here is the caller graph for this function:

int mraa_gpio_read ( mraa_gpio_context  dev)

Read the Gpio value. This can be 0 or 1. A resonse of -1 means that there was a fatal error.

Parameters
devThe Gpio context
Returns
Result of operation

Here is the caller graph for this function:

mraa_result_t mraa_gpio_read_multi ( mraa_gpio_context  dev,
int  output_values[] 
)

Read the Gpio(s) value. The user must provide an integer array with a length equal to the number of pins provided to mraa_gpio_init_multi() function.

Parameters
devThe Gpio context
output_valuesThe array provided by the user. Existing values will be overwritten with the newly read ones.
Returns
Result of operation
mraa_result_t mraa_gpio_write ( mraa_gpio_context  dev,
int  value 
)

Write to the Gpio Value.

Parameters
devThe Gpio context
valueInteger value to write
Returns
Result of operation

Here is the caller graph for this function:

mraa_result_t mraa_gpio_write_multi ( mraa_gpio_context  dev,
int  input_values[] 
)

Write to the Gpio(s) Value. The user must provide an integer array with a length equal to the number of pins provided to mraa_gpio_init_multi() function.

Parameters
devThe Gpio context
output_valuesThe array provided by the user. It must contain the values intended to be written to the gpio pins, in the same order as the init function.
Returns
Result of operation
mraa_result_t mraa_gpio_owner ( mraa_gpio_context  dev,
mraa_boolean_t  owner 
)

Change ownership of the context.

Parameters
devThe Gpio context
ownerDoes this context own the pin
Returns
Result of operation

Here is the caller graph for this function:

DEPRECATED mraa_result_t mraa_gpio_use_mmaped ( mraa_gpio_context  dev,
mraa_boolean_t  mmap 
)

Enable using memory mapped io instead of sysfs, chardev based I/O can be considered memorymapped

Deprecated:
Parameters
devThe Gpio context
mmapUse mmap instead of sysfs
Returns
Result of operation

Here is the caller graph for this function:

int mraa_gpio_get_pin ( mraa_gpio_context  dev)

Get a pin number of the gpio, invalid will return -1

Parameters
devThe Gpio context
Returns
Pin number

Here is the caller graph for this function:

int mraa_gpio_get_pin_raw ( mraa_gpio_context  dev)

Get a gpio number as used within sysfs, invalid will return -1

Parameters
devThe Gpio context
Returns
gpio number

Here is the caller graph for this function:

mraa_result_t mraa_gpio_input_mode ( mraa_gpio_context  dev,
mraa_gpio_input_mode_t  mode 
)

Set Gpio input mode

Parameters
devThe Gpio context
modeMode to set input pin state
Returns
Result of operation

Here is the caller graph for this function:

mraa_result_t mraa_gpio_out_driver_mode ( mraa_gpio_context  dev,
mraa_gpio_out_driver_mode_t  mode 
)

Set Gpio output driver mode. This is not a standard feature, it needs custom implementation for each board

Parameters
devThe Gpio context
modeSet output driver mode
Returns
Result of operation

Here is the caller graph for this function:

Typedef Documentation

typedef struct _gpio* mraa_gpio_context

Opaque pointer definition to the internal struct _gpio

Enumeration Type Documentation

Gpio Output modes

Enumerator
MRAA_GPIO_STRONG 

Default. Strong high and low

MRAA_GPIO_PULLUP 

Resistive High

MRAA_GPIO_PULLDOWN 

Resistive Low

MRAA_GPIO_HIZ 

High Z State

Gpio Direction options

Enumerator
MRAA_GPIO_OUT 

Output. A Mode can also be set

MRAA_GPIO_IN 

Input

MRAA_GPIO_OUT_HIGH 

Output. Init High

MRAA_GPIO_OUT_LOW 

Output. Init Low

Gpio Edge types for interrupts

Enumerator
MRAA_GPIO_EDGE_NONE 

No interrupt on Gpio

MRAA_GPIO_EDGE_BOTH 

Interrupt on rising & falling

MRAA_GPIO_EDGE_RISING 

Interrupt on rising only

MRAA_GPIO_EDGE_FALLING 

Interrupt on falling only

Gpio input modes

Enumerator
MRAA_GPIO_ACTIVE_HIGH 

Resistive High

MRAA_GPIO_ACTIVE_LOW 

Resistive Low

Gpio output driver modes

Enumerator
MRAA_GPIO_OPEN_DRAIN 

Open Drain Configuration

MRAA_GPIO_PUSH_PULL 

Push Pull Configuration

Include dependency graph for gpio.h: