Open Model Railroad Network (OpenMRN)
Loading...
Searching...
No Matches
LinuxGpio.hxx File Reference
#include "freertos_drivers/common/GpioWrapper.hxx"
#include "os/Gpio.hxx"
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <stdio.h>
#include <unistd.h>

Go to the source code of this file.

Classes

class  LinuxGpio< PIN_NUM >
 Defines a GPIO output pin. More...
 
struct  GpioOutputPin< Base, SAFE_VALUE, INVERT >
 Parametric GPIO output class. More...
 
struct  GpioOutputSafeLow< Defs >
 Defines a GPIO output pin, initialized to be an output pin with low level. More...
 
struct  GpioOutputSafeLowInvert< Defs >
 Defines a GPIO output pin, initialized to be an output pin with low level. More...
 
struct  GpioOutputSafeHigh< Defs >
 Defines a GPIO output pin, initialized to be an output pin with high level. More...
 
struct  GpioOutputSafeHighInvert< Defs >
 Defines a GPIO output pin, initialized to be an output pin with high level. More...
 
struct  GpioInputPin< Base, ACTIVE_HIGH >
 Parametric GPIO input class. More...
 
struct  GpioInputActiveHigh< Defs >
 Defines a GPIO input pin, Active High (High == true). More...
 
struct  GpioInputActiveLow< Defs >
 Defines a GPIO input pin, Active Low (Low == true). More...
 

Macros

#define GPIO_PIN(NAME, BaseClass, NUM)    typedef BaseClass<LinuxGpio<NUM>> NAME##_Pin
 Helper macro for defining GPIO pins on Linux-based microcontrollers (like the Raspberry Pi or Bagle Bone.
 

Detailed Description

Copyright (C) 2018 Robert Heller D/B/A Deepwoods Software 51 Locke Hill Road Wendell, MA 01379-9728

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.

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.

Defines GPIO pins using the Linux sysfs ABI.

How to use

You need to use the GPIO_PIN macro at the bottom, like this:

GPIO_PIN(LED1, GpioOutputSafeLow, 27); // Defines LED1_Pin, for GPIO 27, initialized low. GPIO_PIN(CSLow, GpioOutputSafeHighInvert, 5); // Defines CSLow_Pin, for GPIO 5, initialized high, with the set logic inverted. GPIO_PIN(Button1, GpioInputActiveLow, 20); // Defines Button1_Pin, for GPIO 20, avtive low – return true when shorted to ground.

Classes available for the second macro parameter are:

Be sure to use GpioInitializer to create an Initializer class:

typedef GpioInitializer<LED1_Pin, CSLow_Pin, Button1_Pin> GpioInit;

somewhere in main.cxx, and then in appl_main():

GpioInit::hw_init();

This makes sure the GPIO pins are properly set up (eg exported to /sys/class/gpio/). Also, the process running the node needs to be in group gpio.

Author
Robert Heller
Date
10 October 2018

Definition in file LinuxGpio.hxx.

Macro Definition Documentation

◆ GPIO_PIN

#define GPIO_PIN (   NAME,
  BaseClass,
  NUM 
)     typedef BaseClass<LinuxGpio<NUM>> NAME##_Pin

Helper macro for defining GPIO pins on Linux-based microcontrollers (like the Raspberry Pi or Bagle Bone.

Parameters
NAMEis the basename of the declaration. For NAME==FOO the macro declared FOO_Pin as a structure on which the read-write functions will be available.
BaseClassis the initialization structure, such as LedPin, or GpioOutputSafeHigh or GpioOutputSafeLow.
NUMis the pin number, such as 3

Example: GPIO_PIN(FOO, GpioOutputSafeLow, 3); ... FOO_Pin::set(true);

Definition at line 324 of file LinuxGpio.hxx.