2021-02-14 12:35:08 +01:00
|
|
|
#include "LinuxLibgpioIF.h"
|
|
|
|
#include <fsfw/serviceinterface/ServiceInterface.h>
|
2021-02-01 11:17:20 +01:00
|
|
|
|
|
|
|
#include <utility>
|
2021-01-23 17:22:40 +01:00
|
|
|
#include <unistd.h>
|
2021-02-01 11:17:20 +01:00
|
|
|
#include <gpiod.h>
|
2021-01-23 17:22:40 +01:00
|
|
|
|
|
|
|
LinuxLibgpioIF::LinuxLibgpioIF(object_id_t objectId) : SystemObject(objectId) {
|
|
|
|
}
|
|
|
|
|
|
|
|
LinuxLibgpioIF::~LinuxLibgpioIF() {
|
|
|
|
}
|
|
|
|
|
|
|
|
ReturnValue_t LinuxLibgpioIF::initialize(CookieIF * cookie){
|
2021-02-14 12:57:09 +01:00
|
|
|
ReturnValue_t result;
|
|
|
|
GpioMap mapToAdd;
|
|
|
|
|
|
|
|
if(cookie == nullptr) {
|
|
|
|
sif::error << "LinuxLibgpioIF::initialize: Invalid cookie" << std::endl;
|
|
|
|
return RETURN_FAILED;
|
|
|
|
}
|
|
|
|
GpioCookie* gpioCookie = dynamic_cast<GpioCookie*>(cookie);
|
|
|
|
if(gpioCookie == nullptr) {
|
|
|
|
sif::error << "LinuxLibgpioIF: Invalid Gpio Cookie!"
|
|
|
|
<< std::endl;
|
|
|
|
return RETURN_FAILED;
|
|
|
|
}
|
|
|
|
|
|
|
|
mapToAdd = gpioCookie->getGpioMap();
|
|
|
|
|
|
|
|
result = checkForConflicts(mapToAdd);
|
|
|
|
if (result != RETURN_OK){
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
result = configureGpios(&mapToAdd);
|
|
|
|
if (result != RETURN_OK) {
|
|
|
|
return RETURN_FAILED;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Register new GPIOs in gpioMap*/
|
|
|
|
gpioMap.insert(mapToAdd.begin(), mapToAdd.end());
|
|
|
|
|
|
|
|
return RETURN_OK;
|
2021-02-12 21:23:35 +01:00
|
|
|
}
|
|
|
|
|
2021-02-14 09:25:40 +01:00
|
|
|
ReturnValue_t LinuxLibgpioIF::configureGpios(GpioMap* mapToAdd) {
|
2021-02-12 21:23:35 +01:00
|
|
|
GpioMapIter mapToAddIter;
|
|
|
|
std::string chipname;
|
|
|
|
unsigned int lineNum;
|
|
|
|
struct gpiod_chip *chip;
|
2021-02-14 12:35:08 +01:00
|
|
|
gpio::Direction direction;
|
2021-02-12 21:23:35 +01:00
|
|
|
std::string consumer;
|
|
|
|
struct gpiod_line *lineHandle;
|
|
|
|
int result;
|
|
|
|
|
2021-02-14 09:25:40 +01:00
|
|
|
mapToAddIter = mapToAdd->begin();
|
|
|
|
for (; mapToAddIter != mapToAdd->end(); mapToAddIter++) {
|
2021-02-12 21:23:35 +01:00
|
|
|
|
2021-02-14 09:25:40 +01:00
|
|
|
chipname = mapToAddIter->second.chipname;
|
2021-02-12 21:23:35 +01:00
|
|
|
chip = gpiod_chip_open_by_name(chipname.c_str());
|
|
|
|
if (!chip) {
|
|
|
|
sif::error << "LinuxLibgpioIF::configureGpios: Failed to open chip "
|
2021-02-14 09:25:40 +01:00
|
|
|
<< chipname << ". Gpio ID: " << mapToAddIter->first << std::endl;
|
2021-02-12 21:23:35 +01:00
|
|
|
return RETURN_FAILED;
|
|
|
|
}
|
|
|
|
|
2021-02-14 09:25:40 +01:00
|
|
|
lineNum = mapToAddIter->second.lineNum;
|
2021-02-12 21:23:35 +01:00
|
|
|
lineHandle = gpiod_chip_get_line(chip, lineNum);
|
|
|
|
if (!lineHandle) {
|
2021-02-27 11:07:42 +01:00
|
|
|
sif::error << "LinuxLibgpioIF::configureGpios: Failed to open line for GPIO with id "
|
|
|
|
<< mapToAddIter->first << std::endl;
|
2021-02-12 21:23:35 +01:00
|
|
|
gpiod_chip_close(chip);
|
|
|
|
return RETURN_FAILED;
|
|
|
|
}
|
|
|
|
|
2021-02-14 09:25:40 +01:00
|
|
|
direction = mapToAddIter->second.direction;
|
|
|
|
consumer = mapToAddIter->second.consumer;
|
2021-02-12 21:23:35 +01:00
|
|
|
/* Configure direction and add a description to the GPIO */
|
|
|
|
switch (direction) {
|
2021-02-14 12:35:08 +01:00
|
|
|
case gpio::OUT:
|
2021-02-12 21:23:35 +01:00
|
|
|
result = gpiod_line_request_output(lineHandle, consumer.c_str(),
|
2021-02-14 09:25:40 +01:00
|
|
|
mapToAddIter->second.initValue);
|
2021-02-12 21:23:35 +01:00
|
|
|
if (result < 0) {
|
|
|
|
sif::error << "LinuxLibgpioIF::configureGpios: Failed to request line "
|
2021-02-14 09:25:40 +01:00
|
|
|
<< lineNum << " from GPIO instance with ID: " << mapToAddIter->first
|
2021-02-12 21:23:35 +01:00
|
|
|
<< std::endl;
|
|
|
|
gpiod_line_release(lineHandle);
|
|
|
|
return RETURN_FAILED;
|
|
|
|
}
|
|
|
|
break;
|
2021-02-14 12:35:08 +01:00
|
|
|
case gpio::IN:
|
2021-02-12 21:23:35 +01:00
|
|
|
result = gpiod_line_request_input(lineHandle, consumer.c_str());
|
|
|
|
if (result < 0) {
|
|
|
|
sif::error << "LinuxLibgpioIF::configureGpios: Failed to request line "
|
2021-02-14 09:25:40 +01:00
|
|
|
<< lineNum << " from GPIO instance with ID: " << mapToAddIter->first
|
2021-02-12 21:23:35 +01:00
|
|
|
<< std::endl;
|
|
|
|
gpiod_line_release(lineHandle);
|
|
|
|
return RETURN_FAILED;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
sif::error << "LinuxLibgpioIF::configureGpios: Invalid direction specified"
|
2021-02-14 12:57:09 +01:00
|
|
|
<< std::endl;
|
2021-02-12 21:23:35 +01:00
|
|
|
return RETURN_FAILED;
|
|
|
|
}
|
|
|
|
/**
|
|
|
|
* Write line handle to GPIO configuration instance so it can later be used to set or
|
|
|
|
* read states of GPIOs.
|
|
|
|
*/
|
2021-02-14 09:25:40 +01:00
|
|
|
mapToAddIter->second.lineHandle = lineHandle;
|
2021-02-12 21:23:35 +01:00
|
|
|
}
|
|
|
|
return RETURN_OK;
|
2021-01-23 17:22:40 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
ReturnValue_t LinuxLibgpioIF::pullHigh(gpioId_t gpioId){
|
2021-02-14 12:57:09 +01:00
|
|
|
return driveGpio(gpioId, 1);
|
2021-01-23 17:22:40 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
ReturnValue_t LinuxLibgpioIF::pullLow(gpioId_t gpioId){
|
2021-02-14 12:57:09 +01:00
|
|
|
return driveGpio(gpioId, 0);
|
2021-01-23 17:22:40 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
ReturnValue_t LinuxLibgpioIF::driveGpio(gpioId_t gpioId,
|
2021-02-14 12:57:09 +01:00
|
|
|
unsigned int logiclevel) {
|
2021-02-12 21:23:35 +01:00
|
|
|
int result;
|
2021-02-14 12:57:09 +01:00
|
|
|
struct gpiod_line *lineHandle;
|
2021-02-12 21:23:35 +01:00
|
|
|
|
2021-02-14 12:57:09 +01:00
|
|
|
gpioMapIter = gpioMap.find(gpioId);
|
2021-02-12 21:23:35 +01:00
|
|
|
if (gpioMapIter == gpioMap.end()){
|
|
|
|
sif::debug << "LinuxLibgpioIF::driveGpio: Unknown gpio id " << gpioId << std::endl;
|
|
|
|
return RETURN_FAILED;
|
|
|
|
}
|
2021-01-23 17:22:40 +01:00
|
|
|
|
2021-02-12 21:23:35 +01:00
|
|
|
lineHandle = gpioMapIter->second.lineHandle;
|
2021-02-14 12:57:09 +01:00
|
|
|
result = gpiod_line_set_value(lineHandle, logiclevel);
|
|
|
|
if (result < 0) {
|
|
|
|
sif::error << "LinuxLibgpioIF::driveGpio: Failed to pull GPIO with ID "
|
|
|
|
<< gpioId << " to logic level " << logiclevel << std::endl;
|
|
|
|
return DRIVE_GPIO_FAILURE;
|
|
|
|
}
|
|
|
|
|
|
|
|
return RETURN_OK;
|
2021-02-12 21:23:35 +01:00
|
|
|
}
|
2021-01-23 17:22:40 +01:00
|
|
|
|
2021-02-12 21:23:35 +01:00
|
|
|
ReturnValue_t LinuxLibgpioIF::readGpio(gpioId_t gpioId, int* gpioState) {
|
|
|
|
struct gpiod_line *lineHandle;
|
2021-01-23 17:22:40 +01:00
|
|
|
|
2021-02-14 09:25:40 +01:00
|
|
|
gpioMapIter = gpioMap.find(gpioId);
|
2021-02-12 21:23:35 +01:00
|
|
|
if (gpioMapIter == gpioMap.end()){
|
|
|
|
sif::debug << "LinuxLibgpioIF::readGpio: Unknown gpio id " << gpioId << std::endl;
|
|
|
|
return RETURN_FAILED;
|
|
|
|
}
|
|
|
|
|
|
|
|
lineHandle = gpioMapIter->second.lineHandle;
|
|
|
|
*gpioState = gpiod_line_get_value(lineHandle);
|
2021-01-23 17:22:40 +01:00
|
|
|
|
2021-02-12 21:23:35 +01:00
|
|
|
return RETURN_OK;
|
2021-01-23 17:22:40 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
ReturnValue_t LinuxLibgpioIF::checkForConflicts(GpioMap mapToAdd){
|
2021-02-14 12:57:09 +01:00
|
|
|
gpioId_t gpioId;
|
|
|
|
GpioMapIter mapToAddIter = mapToAdd.begin();
|
|
|
|
for(; mapToAddIter != mapToAdd.end(); mapToAddIter++){
|
|
|
|
gpioId = mapToAddIter->first;
|
|
|
|
gpioMapIter = gpioMap.find(gpioId);
|
|
|
|
if(gpioMapIter != mapToAdd.end()){
|
|
|
|
/* An entry for this GPIO already exists. Check if configuration
|
|
|
|
* of direction is equivalent */
|
|
|
|
if (mapToAddIter->second.direction != gpioMapIter->second.direction){
|
|
|
|
sif::error << "LinuxLibgpioIF::checkForConflicts: Detected conflict "
|
|
|
|
<< "for GPIO " << mapToAddIter->first << std::endl;
|
|
|
|
return RETURN_OK;
|
|
|
|
}
|
|
|
|
/* Remove element from map to add because a entry for this GPIO
|
|
|
|
* already exists */
|
|
|
|
mapToAdd.erase(mapToAddIter);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return RETURN_OK;
|
2021-01-23 17:22:40 +01:00
|
|
|
}
|