some minor improvements

This commit is contained in:
Robin Müller 2021-02-14 18:21:59 +01:00 committed by Robin Mueller
parent f6b624f41e
commit 60410fd6f8
5 changed files with 59 additions and 61 deletions

View File

@ -1,25 +1,23 @@
#include "GpioCookie.h" #include "GpioCookie.h"
#include <fsfw/serviceinterface/ServiceInterfaceStream.h> #include <fsfw/serviceinterface/ServiceInterface.h>
GpioCookie::GpioCookie() { GpioCookie::GpioCookie() {
} }
void GpioCookie::addGpio(gpioId_t gpioId, GpioConfig_t gpioConfig){ void GpioCookie::addGpio(gpioId_t gpioId, GpioConfig_t gpioConfig){
gpioMapIter = gpioMap.find(gpioId); auto gpioMapIter = gpioMap.find(gpioId);
if(gpioMapIter == gpioMap.end()) { if(gpioMapIter == gpioMap.end()) {
std::pair status = gpioMap.emplace(gpioId, gpioConfig); auto statusPair = gpioMap.emplace(gpioId, gpioConfig);
if (status.second == false) { if (statusPair.second == false) {
sif::error << "GpioCookie::addGpio: Failed to add GPIO " sif::error << "GpioCookie::addGpio: Failed to add GPIO " << gpioId <<
<< gpioId << "to GPIO map" << std::endl; "to GPIO map" << std::endl;
} }
return;
} }
else { sif::error << "GpioCookie::addGpio: GPIO already exists in GPIO map " << std::endl;
sif::error << "GpioCookie::addGpio: GPIO already exists in GPIO map "
<< std::endl;
}
} }
GpioMap& GpioCookie::getGpioMap() const { GpioMap GpioCookie::getGpioMap() const {
return gpioMap; return gpioMap;
} }

View File

@ -17,23 +17,22 @@ enum Direction {
/** /**
* @brief Struct containing information about the GPIO to use. This is * @brief Struct containing information about the GPIO to use. This is
* required by the libgpiod to access and drive a GPIO. * required by the libgpiod to access and drive a GPIO.
* @param chipname String of the chipname specifying the group which contains * @param chipname String of the chipname specifying the group which contains the GPIO to
* the GPIO to access. E.g. gpiochip0. To detect names of * access. E.g. gpiochip0. To detect names of GPIO groups run gpiodetect on
* GPIO groups run gpiodetect on the linux command line. * the linux command line.
* @param lineNum The offset of the GPIO within the GPIO group. * @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 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 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 * @param initValue Defines the initial state of the GPIO when configured as output.
* for output GPIOs. * Only required for output GPIOs.
* @param lineHandle The handle returned by gpiod_chip_get_line will be later written to this * @param lineHandle The handle returned by gpiod_chip_get_line will be later written to this
* pointer. * pointer.
*/ */
typedef struct GpioConfig { typedef struct GpioConfig {
GpioConfig(std::string chipname_, int lineNum_, std::string consumer_, GpioConfig(std::string chipname_, int lineNum_, std::string consumer_,
gpio::Direction direction_, int initValue_): gpio::Direction direction_, int initValue_):
chipname(chipname_), lineNum(lineNum_), consumer(consumer_), direction(direction_), chipname(chipname_), lineNum(lineNum_), consumer(consumer_),
initValue(initValue_) { direction(direction_), initValue(initValue_) {}
}
std::string chipname; std::string chipname;
int lineNum; int lineNum;
std::string consumer; std::string consumer;
@ -41,6 +40,7 @@ typedef struct GpioConfig {
int initValue; int initValue;
struct gpiod_line* lineHandle = nullptr; struct gpiod_line* lineHandle = nullptr;
} GpioConfig_t; } GpioConfig_t;
using GpioMap = std::unordered_map<gpioId_t, GpioConfig_t>; using GpioMap = std::unordered_map<gpioId_t, GpioConfig_t>;
using GpioMapIter = GpioMap::iterator; using GpioMapIter = GpioMap::iterator;
@ -65,12 +65,13 @@ public:
/** /**
* @brief Get map with registered GPIOs. * @brief Get map with registered GPIOs.
*/ */
GpioMap& getGpioMap() const; GpioMap getGpioMap() const;
private: private:
/**
* Returns a copy of the internal GPIO map.
*/
GpioMap gpioMap; GpioMap gpioMap;
GpioMapIter gpioMapIter;
}; };
#endif #endif

View File

@ -33,7 +33,7 @@ ReturnValue_t LinuxLibgpioIF::initialize(CookieIF * cookie){
return result; return result;
} }
result = configureGpios(&mapToAdd); result = configureGpios(mapToAdd);
if (result != RETURN_OK) { if (result != RETURN_OK) {
return RETURN_FAILED; return RETURN_FAILED;
} }
@ -44,8 +44,8 @@ ReturnValue_t LinuxLibgpioIF::initialize(CookieIF * cookie){
return RETURN_OK; return RETURN_OK;
} }
ReturnValue_t LinuxLibgpioIF::configureGpios(GpioMap* mapToAdd) { ReturnValue_t LinuxLibgpioIF::configureGpios(GpioMap& mapToAdd) {
GpioMapIter mapToAddIter; //GpioMapIter mapToAddIter;
std::string chipname; std::string chipname;
unsigned int lineNum; unsigned int lineNum;
struct gpiod_chip *chip; struct gpiod_chip *chip;
@ -54,18 +54,18 @@ ReturnValue_t LinuxLibgpioIF::configureGpios(GpioMap* mapToAdd) {
struct gpiod_line *lineHandle; struct gpiod_line *lineHandle;
int result; int result;
mapToAddIter = mapToAdd->begin(); //mapToAddIter = mapToAdd->begin();
for (; mapToAddIter != mapToAdd->end(); mapToAddIter++) { for(auto& gpioConfig: mapToAdd) {
//for (; mapToAddIter != mapToAdd->end(); mapToAddIter++) {
chipname = mapToAddIter->second.chipname; chipname = gpioConfig.second.chipname;
chip = gpiod_chip_open_by_name(chipname.c_str()); chip = gpiod_chip_open_by_name(chipname.c_str());
if (!chip) { if (!chip) {
sif::error << "LinuxLibgpioIF::configureGpios: Failed to open chip " sif::error << "LinuxLibgpioIF::configureGpios: Failed to open chip "
<< chipname << ". Gpio ID: " << mapToAddIter->first << std::endl; << chipname << ". Gpio ID: " << gpioConfig.first << std::endl;
return RETURN_FAILED; return RETURN_FAILED;
} }
lineNum = mapToAddIter->second.lineNum; lineNum = gpioConfig.second.lineNum;
lineHandle = gpiod_chip_get_line(chip, lineNum); lineHandle = gpiod_chip_get_line(chip, lineNum);
if (!lineHandle) { if (!lineHandle) {
sif::error << "LinuxLibgpioIF::configureGpios: Failed to open line" << std::endl; sif::error << "LinuxLibgpioIF::configureGpios: Failed to open line" << std::endl;
@ -73,16 +73,16 @@ ReturnValue_t LinuxLibgpioIF::configureGpios(GpioMap* mapToAdd) {
return RETURN_FAILED; return RETURN_FAILED;
} }
direction = mapToAddIter->second.direction; direction = gpioConfig.second.direction;
consumer = mapToAddIter->second.consumer; consumer = gpioConfig.second.consumer;
/* Configure direction and add a description to the GPIO */ /* Configure direction and add a description to the GPIO */
switch (direction) { switch (direction) {
case gpio::OUT: case gpio::OUT:
result = gpiod_line_request_output(lineHandle, consumer.c_str(), result = gpiod_line_request_output(lineHandle, consumer.c_str(),
mapToAddIter->second.initValue); gpioConfig.second.initValue);
if (result < 0) { if (result < 0) {
sif::error << "LinuxLibgpioIF::configureGpios: Failed to request line " sif::error << "LinuxLibgpioIF::configureGpios: Failed to request line "
<< lineNum << " from GPIO instance with ID: " << mapToAddIter->first << lineNum << " from GPIO instance with ID: " << gpioConfig.first
<< std::endl; << std::endl;
gpiod_line_release(lineHandle); gpiod_line_release(lineHandle);
return RETURN_FAILED; return RETURN_FAILED;
@ -92,7 +92,7 @@ ReturnValue_t LinuxLibgpioIF::configureGpios(GpioMap* mapToAdd) {
result = gpiod_line_request_input(lineHandle, consumer.c_str()); result = gpiod_line_request_input(lineHandle, consumer.c_str());
if (result < 0) { if (result < 0) {
sif::error << "LinuxLibgpioIF::configureGpios: Failed to request line " sif::error << "LinuxLibgpioIF::configureGpios: Failed to request line "
<< lineNum << " from GPIO instance with ID: " << mapToAddIter->first << lineNum << " from GPIO instance with ID: " << gpioConfig.first
<< std::endl; << std::endl;
gpiod_line_release(lineHandle); gpiod_line_release(lineHandle);
return RETURN_FAILED; return RETURN_FAILED;
@ -107,7 +107,7 @@ ReturnValue_t LinuxLibgpioIF::configureGpios(GpioMap* mapToAdd) {
* Write line handle to GPIO configuration instance so it can later be used to set or * Write line handle to GPIO configuration instance so it can later be used to set or
* read states of GPIOs. * read states of GPIOs.
*/ */
mapToAddIter->second.lineHandle = lineHandle; gpioConfig.second.lineHandle = lineHandle;
} }
return RETURN_OK; return RETURN_OK;
} }
@ -125,7 +125,7 @@ ReturnValue_t LinuxLibgpioIF::driveGpio(gpioId_t gpioId,
int result; int result;
struct gpiod_line *lineHandle; struct gpiod_line *lineHandle;
gpioMapIter = gpioMap.find(gpioId); auto gpioMapIter = gpioMap.find(gpioId);
if (gpioMapIter == gpioMap.end()){ if (gpioMapIter == gpioMap.end()){
sif::debug << "LinuxLibgpioIF::driveGpio: Unknown gpio id " << gpioId << std::endl; sif::debug << "LinuxLibgpioIF::driveGpio: Unknown gpio id " << gpioId << std::endl;
return RETURN_FAILED; return RETURN_FAILED;
@ -145,7 +145,7 @@ ReturnValue_t LinuxLibgpioIF::driveGpio(gpioId_t gpioId,
ReturnValue_t LinuxLibgpioIF::readGpio(gpioId_t gpioId, int* gpioState) { ReturnValue_t LinuxLibgpioIF::readGpio(gpioId_t gpioId, int* gpioState) {
struct gpiod_line *lineHandle; struct gpiod_line *lineHandle;
gpioMapIter = gpioMap.find(gpioId); auto gpioMapIter = gpioMap.find(gpioId);
if (gpioMapIter == gpioMap.end()){ if (gpioMapIter == gpioMap.end()){
sif::debug << "LinuxLibgpioIF::readGpio: Unknown gpio id " << gpioId << std::endl; sif::debug << "LinuxLibgpioIF::readGpio: Unknown gpio id " << gpioId << std::endl;
return RETURN_FAILED; return RETURN_FAILED;
@ -157,23 +157,24 @@ ReturnValue_t LinuxLibgpioIF::readGpio(gpioId_t gpioId, int* gpioState) {
return RETURN_OK; return RETURN_OK;
} }
ReturnValue_t LinuxLibgpioIF::checkForConflicts(GpioMap mapToAdd){ ReturnValue_t LinuxLibgpioIF::checkForConflicts(GpioMap& mapToAdd){
gpioId_t gpioId; gpioId_t gpioId;
GpioMapIter mapToAddIter = mapToAdd.begin(); auto gpioMapIter = gpioMap.begin();
for(; mapToAddIter != mapToAdd.end(); mapToAddIter++){ for(auto& gpioConfig: mapToAdd) {
gpioId = mapToAddIter->first; gpioId = gpioConfig.first;
/* Cross check with private map */
gpioMapIter = gpioMap.find(gpioId); gpioMapIter = gpioMap.find(gpioId);
if(gpioMapIter != mapToAdd.end()) { if(gpioMapIter != mapToAdd.end()) {
/* An entry for this GPIO already exists. Check if configuration /* An entry for this GPIO already exists. Check if configuration
* of direction is equivalent */ * of direction is equivalent */
if (mapToAddIter->second.direction != gpioMapIter->second.direction){ if (gpioConfig.second.direction != gpioMapIter->second.direction){
sif::error << "LinuxLibgpioIF::checkForConflicts: Detected conflict " sif::error << "LinuxLibgpioIF::checkForConflicts: Detected conflict for GPIO " <<
<< "for GPIO " << mapToAddIter->first << std::endl; gpioConfig.first << std::endl;
return RETURN_OK; return RETURN_FAILED;
} }
/* Remove element from map to add because a entry for this GPIO /* Remove element from map to add because a entry for this GPIO
* already exists */ * already exists */
mapToAdd.erase(mapToAddIter); mapToAdd.erase(gpioConfig.first);
} }
} }
return RETURN_OK; return RETURN_OK;

View File

@ -23,16 +23,14 @@ public:
LinuxLibgpioIF(object_id_t objectId); LinuxLibgpioIF(object_id_t objectId);
virtual ~LinuxLibgpioIF(); virtual ~LinuxLibgpioIF();
ReturnValue_t initialize(CookieIF * cookie) override; ReturnValue_t initialize(CookieIF* gpioCookie) override;
ReturnValue_t pullHigh(gpioId_t gpioId) override; ReturnValue_t pullHigh(gpioId_t gpioId) override;
ReturnValue_t pullLow(gpioId_t gpioId) override; ReturnValue_t pullLow(gpioId_t gpioId) override;
ReturnValue_t readGpio(gpioId_t gpioId, int* gpioState) override; ReturnValue_t readGpio(gpioId_t gpioId, int* gpioState) override;
private: private:
/* Holds the information and configuration of all used GPIOs */ /* Holds the information and configuration of all used GPIOs */
GpioMap gpioMap; GpioMap gpioMap;
GpioMapIter gpioMapIter;
/** /**
* @brief This functions drives line of a GPIO specified by the GPIO ID. * @brief This functions drives line of a GPIO specified by the GPIO ID.
@ -52,12 +50,12 @@ private:
* *
* @return RETURN_OK if successful, otherwise RETURN_FAILED * @return RETURN_OK if successful, otherwise RETURN_FAILED
*/ */
ReturnValue_t checkForConflicts(GpioMap mapToAdd); ReturnValue_t checkForConflicts(GpioMap& mapToAdd);
/** /**
* @brief Performs the initial configuration of all GPIOs specified in the GpioMap mapToAdd. * @brief Performs the initial configuration of all GPIOs specified in the GpioMap mapToAdd.
*/ */
ReturnValue_t configureGpios(GpioMap* mapToAdd); ReturnValue_t configureGpios(GpioMap& mapToAdd);
}; };
#endif /* BSP_Q7S_GPIO_LINUXLIBGPIOIF_H_ */ #endif /* BSP_Q7S_GPIO_LINUXLIBGPIOIF_H_ */