fsfw-hal/common/gpio/gpioDefinitions.h

103 lines
3.1 KiB
C
Raw Normal View History

2021-06-05 14:42:20 +02:00
#ifndef COMMON_GPIO_GPIODEFINITIONS_H_
#define COMMON_GPIO_GPIODEFINITIONS_H_
2021-03-23 15:41:49 +01:00
#include <string>
#include <unordered_map>
2021-06-05 14:42:20 +02:00
#include <map>
2021-03-23 15:41:49 +01:00
using gpioId_t = uint16_t;
namespace gpio {
2021-04-01 15:14:20 +02:00
enum Levels {
LOW = 0,
HIGH = 1
};
2021-03-23 15:41:49 +01:00
enum Direction {
IN = 0,
OUT = 1
};
enum GpioOperation {
READ,
WRITE
};
enum GpioTypes {
NONE,
2021-06-05 14:42:20 +02:00
GPIO_REGULAR,
2021-03-23 15:41:49 +01:00
CALLBACK
};
static constexpr gpioId_t NO_GPIO = -1;
2021-06-05 14:42:20 +02:00
using gpio_cb_t = void (*) (gpioId_t gpioId, gpio::GpioOperation gpioOp, int value, void* args);
2021-03-23 15:41:49 +01:00
}
/**
* @brief Struct containing information about the GPIO to use. This is
* required by the libgpiod to access and drive a GPIO.
* @param chipname String of the chipname specifying the group which contains the GPIO to
* access. E.g. gpiochip0. To detect names of GPIO groups run gpiodetect on
* the linux command line.
* @param lineNum The offset of the GPIO within the GPIO group.
* @param consumer Name of the consumer. Simply a description of the GPIO configuration.
* @param direction Specifies whether the GPIO should be used as in- or output.
* @param initValue Defines the initial state of the GPIO when configured as output.
* Only required for output GPIOs.
* @param lineHandle The handle returned by gpiod_chip_get_line will be later written to this
* pointer.
*/
class GpioBase {
public:
GpioBase() = default;
GpioBase(gpio::GpioTypes gpioType, std::string consumer, gpio::Direction direction,
int initValue):
gpioType(gpioType), consumer(consumer),direction(direction), initValue(initValue) {}
virtual~ GpioBase() {};
/* Can be used to cast GpioBase to a concrete child implementation */
gpio::GpioTypes gpioType = gpio::GpioTypes::NONE;
std::string consumer;
gpio::Direction direction = gpio::Direction::IN;
int initValue = 0;
};
class GpiodRegular: public GpioBase {
public:
2021-06-05 14:42:20 +02:00
GpiodRegular(): GpioBase(gpio::GpioTypes::GPIO_REGULAR, std::string(),
2021-03-23 15:41:49 +01:00
gpio::Direction::IN, 0) {};
GpiodRegular(std::string chipname_, int lineNum_, std::string consumer_,
gpio::Direction direction_, int initValue_):
2021-06-05 14:42:20 +02:00
GpioBase(gpio::GpioTypes::GPIO_REGULAR, consumer_, direction_, initValue_),
2021-03-23 15:41:49 +01:00
chipname(chipname_), lineNum(lineNum_) {}
std::string chipname;
int lineNum = 0;
struct gpiod_line* lineHandle = nullptr;
};
class GpioCallback: public GpioBase {
public:
GpioCallback(std::string consumer, gpio::Direction direction_, int initValue_,
2021-06-05 14:42:20 +02:00
gpio::gpio_cb_t callback, void* callbackArgs):
2021-03-23 15:41:49 +01:00
GpioBase(gpio::GpioTypes::CALLBACK, consumer, direction_, initValue_),
callback(callback), callbackArgs(callbackArgs) {}
2021-06-05 14:42:20 +02:00
gpio::gpio_cb_t callback = nullptr;
2021-03-23 15:41:49 +01:00
void* callbackArgs = nullptr;
};
2021-06-05 14:42:20 +02:00
using GpioMap = std::map<gpioId_t, GpioBase*>;
using GpioUnorderedMap = std::unordered_map<gpioId_t, GpioBase*>;
2021-03-23 15:41:49 +01:00
using GpioMapIter = GpioMap::iterator;
2021-06-05 14:42:20 +02:00
using GpioUnorderedMapIter = GpioUnorderedMap::iterator;
2021-03-23 15:41:49 +01:00
#endif /* LINUX_GPIO_GPIODEFINITIONS_H_ */