RocketLogger 2.1.1
gpio.c File Reference
#include <errno.h>
#include <stdint.h>
#include <string.h>
#include <gpiod.h>
#include <time.h>
#include <unistd.h>
#include "log.h"
#include "rl.h"
#include "gpio.h"
+ Include dependency graph for gpio.c:

Go to the source code of this file.

Macros

#define GPIO_CHIP_COUNT   4
 Number of GPIO chips.
 
#define GPIO_LINES_PER_CHIP   32
 Number of GPIO lines per GPIO chip.
 
#define GPIO_DEBOUNCE_DELAY_US   50
 Minimal time a button needs to be pressed (in microseconds)
 

Functions

int gpio_init ()
 
void gpio_deinit ()
 
gpio_tgpio_setup (int gpio_number, gpio_mode_t mode, const char *name)
 
void gpio_release (gpio_t *gpio)
 
int gpio_set_value (gpio_t *gpio, int value)
 
int gpio_get_value (gpio_t *gpio)
 
gpio_tgpio_setup_interrupt (int gpio_number, gpio_interrupt_t edge, const char *name)
 
int gpio_wait_interrupt (gpio_t *gpio, const struct timespec *timeout)
 

Variables

struct gpiod_chip * gpio_chip [GPIO_CHIP_COUNT] = {NULL}
 

Macro Definition Documentation

◆ GPIO_CHIP_COUNT

#define GPIO_CHIP_COUNT   4

Number of GPIO chips.

Copyright (c) 2016-2020, ETH Zurich, Computer Engineering Group All rights reserved.

Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:

  • Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
  • Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.
  • Neither the name of the copyright holder nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

Definition at line 46 of file gpio.c.

◆ GPIO_DEBOUNCE_DELAY_US

#define GPIO_DEBOUNCE_DELAY_US   50

Minimal time a button needs to be pressed (in microseconds)

Definition at line 52 of file gpio.c.

◆ GPIO_LINES_PER_CHIP

#define GPIO_LINES_PER_CHIP   32

Number of GPIO lines per GPIO chip.

Definition at line 49 of file gpio.c.

Function Documentation

◆ gpio_deinit()

void gpio_deinit ( )

Denitialize GPIO module.

Note
release any used GPIO pin first.

Definition at line 92 of file gpio.c.

References gpio_chip, and GPIO_CHIP_COUNT.

Referenced by hw_deinit(), and main().

◆ gpio_get_value()

int gpio_get_value ( gpio_t gpio)

Get the GPIO value.

Parameters
gpioGPIO resource to get the value
Returns
The read GPIO value, negative on failure with errno set accordingly

Definition at line 146 of file gpio.c.

◆ gpio_init()

int gpio_init ( )

Initialize GPIO module.

Returns
Returns 0 on success, negative on failure with errno set accordingly

Definition at line 77 of file gpio.c.

References ERROR, gpio_chip, GPIO_CHIP_COUNT, rl_log(), RL_LOG_ERROR, and SUCCESS.

Referenced by hw_init(), and main().

+ Here is the call graph for this function:

◆ gpio_release()

void gpio_release ( gpio_t gpio)

Release an aquired GPIO resource.

Parameters
gpioThe GPIO resource to release

Definition at line 130 of file gpio.c.

Referenced by hw_deinit(), and main().

◆ gpio_set_value()

int gpio_set_value ( gpio_t gpio,
int  value 
)

Set the GPIO value.

Parameters
gpioGPIO resource to set the output value
valueGPIO state to set (0 or 1)
Returns
Returns 0 on success, negative on failure with errno set accordingly

Definition at line 135 of file gpio.c.

References ERROR.

Referenced by hw_deinit(), hw_init(), hw_sample(), and main().

◆ gpio_setup()

gpio_t * gpio_setup ( int  gpio_number,
gpio_mode_t  mode,
const char *  name 
)

Set up a specific GPIO pin for input/output.

Note
need to initialize GPIO module first.
Parameters
gpio_numberResource number of the GPIO to set up
modeGPIO mode (input or output) to configure
nameName to label the set up GPIO
Returns
Returns GPIO resource on success, NULL on failure with errno set accordingly

Definition at line 103 of file gpio.c.

References GPIO_MODE_IN, GPIO_MODE_OUT, rl_log(), and RL_LOG_ERROR.

Referenced by hw_init(), and main().

+ Here is the call graph for this function:

◆ gpio_setup_interrupt()

gpio_t * gpio_setup_interrupt ( int  gpio_number,
gpio_interrupt_t  edge,
const char *  name 
)

Set up a specific GPIO pin for interrupt event.

Parameters
gpio_numberResource number of the GPIO to set up
edgeGPIO interrupt edge to configure
nameName to label the set up GPIO
Returns
Returns GPIO resource on success, NULL on failure with errno set accordingly

Definition at line 148 of file gpio.c.

References GPIO_INTERRUPT_BOTH, GPIO_INTERRUPT_FALLING, GPIO_INTERRUPT_NONE, GPIO_INTERRUPT_RISING, rl_log(), and RL_LOG_ERROR.

Referenced by main().

+ Here is the call graph for this function:

◆ gpio_wait_interrupt()

int gpio_wait_interrupt ( gpio_t gpio,
const struct timespec *  timeout 
)

Wait for interrupt on GPIO pin.

Parameters
gpio_numberLinux sysfs GPIO resource number
timeoutPointer to timeout timespec, NULL for infinite timeout
Returns
Returns the GPIO pin value (0 or 1) on success, negative on failure with errno set accordingly

Definition at line 187 of file gpio.c.

References ERROR, GPIO_DEBOUNCE_DELAY_US, rl_log(), and RL_LOG_ERROR.

Referenced by main().

+ Here is the call graph for this function:

Variable Documentation

◆ gpio_chip

struct gpiod_chip* gpio_chip[GPIO_CHIP_COUNT] = {NULL}

Definition at line 54 of file gpio.c.

Referenced by gpio_deinit(), and gpio_init().