moved some files
This commit is contained in:
@ -1,5 +1,6 @@
|
||||
target_sources(${TARGET_NAME} PUBLIC
|
||||
GpioCookie.cpp
|
||||
LinuxLibgpioIF.cpp
|
||||
)
|
||||
|
||||
|
||||
|
180
linux/gpio/LinuxLibgpioIF.cpp
Normal file
180
linux/gpio/LinuxLibgpioIF.cpp
Normal file
@ -0,0 +1,180 @@
|
||||
#include "LinuxLibgpioIF.h"
|
||||
#include <fsfw/serviceinterface/ServiceInterface.h>
|
||||
|
||||
#include <utility>
|
||||
#include <unistd.h>
|
||||
#include <gpiod.h>
|
||||
|
||||
LinuxLibgpioIF::LinuxLibgpioIF(object_id_t objectId) : SystemObject(objectId) {
|
||||
}
|
||||
|
||||
LinuxLibgpioIF::~LinuxLibgpioIF() {
|
||||
}
|
||||
|
||||
ReturnValue_t LinuxLibgpioIF::initialize(CookieIF * cookie){
|
||||
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;
|
||||
}
|
||||
|
||||
ReturnValue_t LinuxLibgpioIF::configureGpios(GpioMap* mapToAdd) {
|
||||
GpioMapIter mapToAddIter;
|
||||
std::string chipname;
|
||||
unsigned int lineNum;
|
||||
struct gpiod_chip *chip;
|
||||
gpio::Direction direction;
|
||||
std::string consumer;
|
||||
struct gpiod_line *lineHandle;
|
||||
int result;
|
||||
|
||||
mapToAddIter = mapToAdd->begin();
|
||||
for (; mapToAddIter != mapToAdd->end(); mapToAddIter++) {
|
||||
|
||||
chipname = mapToAddIter->second.chipname;
|
||||
chip = gpiod_chip_open_by_name(chipname.c_str());
|
||||
if (!chip) {
|
||||
sif::error << "LinuxLibgpioIF::configureGpios: Failed to open chip "
|
||||
<< chipname << ". Gpio ID: " << mapToAddIter->first << std::endl;
|
||||
return RETURN_FAILED;
|
||||
}
|
||||
|
||||
lineNum = mapToAddIter->second.lineNum;
|
||||
lineHandle = gpiod_chip_get_line(chip, lineNum);
|
||||
if (!lineHandle) {
|
||||
sif::error << "LinuxLibgpioIF::configureGpios: Failed to open line" << std::endl;
|
||||
gpiod_chip_close(chip);
|
||||
return RETURN_FAILED;
|
||||
}
|
||||
|
||||
direction = mapToAddIter->second.direction;
|
||||
consumer = mapToAddIter->second.consumer;
|
||||
/* Configure direction and add a description to the GPIO */
|
||||
switch (direction) {
|
||||
case gpio::OUT:
|
||||
result = gpiod_line_request_output(lineHandle, consumer.c_str(),
|
||||
mapToAddIter->second.initValue);
|
||||
if (result < 0) {
|
||||
sif::error << "LinuxLibgpioIF::configureGpios: Failed to request line "
|
||||
<< lineNum << " from GPIO instance with ID: " << mapToAddIter->first
|
||||
<< std::endl;
|
||||
gpiod_line_release(lineHandle);
|
||||
return RETURN_FAILED;
|
||||
}
|
||||
break;
|
||||
case gpio::IN:
|
||||
result = gpiod_line_request_input(lineHandle, consumer.c_str());
|
||||
if (result < 0) {
|
||||
sif::error << "LinuxLibgpioIF::configureGpios: Failed to request line "
|
||||
<< lineNum << " from GPIO instance with ID: " << mapToAddIter->first
|
||||
<< std::endl;
|
||||
gpiod_line_release(lineHandle);
|
||||
return RETURN_FAILED;
|
||||
}
|
||||
break;
|
||||
default:
|
||||
sif::error << "LinuxLibgpioIF::configureGpios: Invalid direction specified"
|
||||
<< std::endl;
|
||||
return RETURN_FAILED;
|
||||
}
|
||||
/**
|
||||
* Write line handle to GPIO configuration instance so it can later be used to set or
|
||||
* read states of GPIOs.
|
||||
*/
|
||||
mapToAddIter->second.lineHandle = lineHandle;
|
||||
}
|
||||
return RETURN_OK;
|
||||
}
|
||||
|
||||
ReturnValue_t LinuxLibgpioIF::pullHigh(gpioId_t gpioId){
|
||||
return driveGpio(gpioId, 1);
|
||||
}
|
||||
|
||||
ReturnValue_t LinuxLibgpioIF::pullLow(gpioId_t gpioId){
|
||||
return driveGpio(gpioId, 0);
|
||||
}
|
||||
|
||||
ReturnValue_t LinuxLibgpioIF::driveGpio(gpioId_t gpioId,
|
||||
unsigned int logiclevel) {
|
||||
int result;
|
||||
struct gpiod_line *lineHandle;
|
||||
|
||||
gpioMapIter = gpioMap.find(gpioId);
|
||||
if (gpioMapIter == gpioMap.end()){
|
||||
sif::debug << "LinuxLibgpioIF::driveGpio: Unknown gpio id " << gpioId << std::endl;
|
||||
return RETURN_FAILED;
|
||||
}
|
||||
|
||||
lineHandle = gpioMapIter->second.lineHandle;
|
||||
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;
|
||||
}
|
||||
|
||||
ReturnValue_t LinuxLibgpioIF::readGpio(gpioId_t gpioId, int* gpioState) {
|
||||
struct gpiod_line *lineHandle;
|
||||
|
||||
gpioMapIter = gpioMap.find(gpioId);
|
||||
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);
|
||||
|
||||
return RETURN_OK;
|
||||
}
|
||||
|
||||
ReturnValue_t LinuxLibgpioIF::checkForConflicts(GpioMap mapToAdd){
|
||||
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;
|
||||
}
|
63
linux/gpio/LinuxLibgpioIF.h
Normal file
63
linux/gpio/LinuxLibgpioIF.h
Normal file
@ -0,0 +1,63 @@
|
||||
#ifndef BSP_Q7S_GPIO_LINUXLIBGPIOIF_H_
|
||||
#define BSP_Q7S_GPIO_LINUXLIBGPIOIF_H_
|
||||
|
||||
#include <linux/gpio/GpioIF.h>
|
||||
#include <linux/gpio/GpioCookie.h>
|
||||
#include <fsfwconfig/returnvalues/classIds.h>
|
||||
#include <fsfw/objectmanager/SystemObject.h>
|
||||
|
||||
/**
|
||||
* @brief This class implements the GpioIF for a linux based system. The
|
||||
* implementation is based on the libgpiod lib which requires linux 4.8
|
||||
* or higher.
|
||||
* @note The Petalinux SDK from Xilinx supports libgpiod since Petalinux
|
||||
* 2019.1.
|
||||
*/
|
||||
class LinuxLibgpioIF : public GpioIF, public SystemObject {
|
||||
public:
|
||||
|
||||
static const uint8_t INTERFACE_ID = CLASS_ID::LINUX_LIBGPIO_IF;
|
||||
|
||||
static const ReturnValue_t DRIVE_GPIO_FAILURE = MAKE_RETURN_CODE(0x2);
|
||||
|
||||
LinuxLibgpioIF(object_id_t objectId);
|
||||
virtual ~LinuxLibgpioIF();
|
||||
|
||||
ReturnValue_t initialize(CookieIF * cookie) override;
|
||||
ReturnValue_t pullHigh(gpioId_t gpioId) override;
|
||||
ReturnValue_t pullLow(gpioId_t gpioId) override;
|
||||
ReturnValue_t readGpio(gpioId_t gpioId, int* gpioState) override;
|
||||
|
||||
private:
|
||||
|
||||
/*Holds the information and configuration of all used GPIOs */
|
||||
GpioMap gpioMap;
|
||||
GpioMapIter gpioMapIter;
|
||||
|
||||
/**
|
||||
* @brief This functions drives line of a GPIO specified by the GPIO ID.
|
||||
*
|
||||
* @param gpioId The GPIO ID of the GPIO to drive.
|
||||
* @param logiclevel The logic level to set. O or 1.
|
||||
*/
|
||||
ReturnValue_t driveGpio(gpioId_t gpioId,
|
||||
unsigned int logiclevel);
|
||||
|
||||
/**
|
||||
* @brief This function checks if GPIOs are already registered and whether
|
||||
* there exists a conflict in the GPIO configuration. E.g. the
|
||||
* direction.
|
||||
*
|
||||
* @param mapToAdd The GPIOs which shall be added to the gpioMap.
|
||||
*
|
||||
* @return RETURN_OK if successful, otherwise RETURN_FAILED
|
||||
*/
|
||||
ReturnValue_t checkForConflicts(GpioMap mapToAdd);
|
||||
|
||||
/**
|
||||
* @brief Performs the initial configuration of all GPIOs specified in the GpioMap mapToAdd.
|
||||
*/
|
||||
ReturnValue_t configureGpios(GpioMap* mapToAdd);
|
||||
};
|
||||
|
||||
#endif /* BSP_Q7S_GPIO_LINUXLIBGPIOIF_H_ */
|
Reference in New Issue
Block a user