Update Linux GPIO #20

Merged
meierj merged 26 commits from mueller/update-linux-gpio into eive/develop 2021-09-27 15:28:20 +02:00
2 changed files with 108 additions and 68 deletions
Showing only changes of commit 9546495507 - Show all commits

View File

@ -122,7 +122,7 @@ ReturnValue_t LinuxLibgpioIF::configureGpioByLineName(gpioId_t gpioId,
int result = gpiod_ctxless_find_line(lineName.c_str(), chipname, MAX_CHIPNAME_LENGTH, int result = gpiod_ctxless_find_line(lineName.c_str(), chipname, MAX_CHIPNAME_LENGTH,
&lineOffset); &lineOffset);
if (result != LINE_FOUND) { if (result != LINE_FOUND) {
parseFindeLineResult(result, lineName); parseFindeLineResult(result, lineName);
return RETURN_FAILED; return RETURN_FAILED;
} }
@ -140,7 +140,7 @@ ReturnValue_t LinuxLibgpioIF::configureGpioByLineName(gpioId_t gpioId,
} }
ReturnValue_t LinuxLibgpioIF::configureRegularGpio(gpioId_t gpioId, struct gpiod_chip* chip, ReturnValue_t LinuxLibgpioIF::configureRegularGpio(gpioId_t gpioId, struct gpiod_chip* chip,
GpiodRegularBase& regularGpio, std::string failOutput) { GpiodRegularBase& regularGpio, std::string failOutput) {
unsigned int lineNum; unsigned int lineNum;
gpio::Direction direction; gpio::Direction direction;
std::string consumer; std::string consumer;
@ -165,22 +165,10 @@ ReturnValue_t LinuxLibgpioIF::configureRegularGpio(gpioId_t gpioId, struct gpiod
case(gpio::OUT): { case(gpio::OUT): {
result = gpiod_line_request_output(lineHandle, consumer.c_str(), result = gpiod_line_request_output(lineHandle, consumer.c_str(),
regularGpio.initValue); regularGpio.initValue);
if (result < 0) {
sif::error << "LinuxLibgpioIF::configureRegularGpio: Failed to request line " << lineNum <<
" from GPIO instance with ID: " << gpioId << std::endl;
gpiod_line_release(lineHandle);
return RETURN_FAILED;
}
break; break;
} }
case(gpio::IN): { case(gpio::IN): {
result = gpiod_line_request_input(lineHandle, consumer.c_str()); 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: " << gpioId << std::endl;
gpiod_line_release(lineHandle);
return RETURN_FAILED;
}
break; break;
} }
default: { default: {
@ -189,6 +177,18 @@ ReturnValue_t LinuxLibgpioIF::configureRegularGpio(gpioId_t gpioId, struct gpiod
return GPIO_INVALID_INSTANCE; return GPIO_INVALID_INSTANCE;
} }
if (result < 0) {
#if FSFW_CPP_OSTREAM_ENABLED == 1
sif::error << "LinuxLibgpioIF::configureRegularGpio: Failed to request line " <<
lineNum << " from GPIO instance with ID: " << gpioId << std::endl;
#else
sif::printError("LinuxLibgpioIF::configureRegularGpio: "
"Failed to request line %d from GPIO instance with ID: %d\n", lineNum, gpioId);
#endif
gpiod_line_release(lineHandle);
return RETURN_FAILED;
}
} }
/** /**
* 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
@ -230,7 +230,11 @@ ReturnValue_t LinuxLibgpioIF::pullHigh(gpioId_t gpioId) {
ReturnValue_t LinuxLibgpioIF::pullLow(gpioId_t gpioId) { ReturnValue_t LinuxLibgpioIF::pullLow(gpioId_t gpioId) {
gpioMapIter = gpioMap.find(gpioId); gpioMapIter = gpioMap.find(gpioId);
if (gpioMapIter == gpioMap.end()) { if (gpioMapIter == gpioMap.end()) {
#if FSFW_CPP_OSTREAM_ENABLED == 1
sif::warning << "LinuxLibgpioIF::pullLow: Unknown GPIO ID " << gpioId << std::endl; sif::warning << "LinuxLibgpioIF::pullLow: Unknown GPIO ID " << gpioId << std::endl;
#else
sif::printWarning("LinuxLibgpioIF::pullLow: Unknown GPIO ID %d\n", gpioId);
#endif
return UNKNOWN_GPIO_ID; return UNKNOWN_GPIO_ID;
} }
@ -260,8 +264,13 @@ ReturnValue_t LinuxLibgpioIF::driveGpio(gpioId_t gpioId,
GpiodRegularBase& regularGpio, gpio::Levels logicLevel) { GpiodRegularBase& regularGpio, gpio::Levels logicLevel) {
int result = gpiod_line_set_value(regularGpio.lineHandle, logicLevel); int result = gpiod_line_set_value(regularGpio.lineHandle, logicLevel);
if (result < 0) { if (result < 0) {
#if FSFW_CPP_OSTREAM_ENABLED == 1
sif::warning << "LinuxLibgpioIF::driveGpio: Failed to pull GPIO with ID " << gpioId << sif::warning << "LinuxLibgpioIF::driveGpio: Failed to pull GPIO with ID " << gpioId <<
" to logic level " << logicLevel << std::endl; " to logic level " << logicLevel << std::endl;
#else
sif::printWarning("LinuxLibgpioIF::driveGpio: Failed to pull GPIO with ID %d to "
"logic level %d\n", gpioId, logicLevel);
#endif
return DRIVE_GPIO_FAILURE; return DRIVE_GPIO_FAILURE;
} }
@ -271,7 +280,11 @@ 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) {
gpioMapIter = gpioMap.find(gpioId); gpioMapIter = gpioMap.find(gpioId);
if (gpioMapIter == gpioMap.end()){ if (gpioMapIter == gpioMap.end()){
#if FSFW_CPP_OSTREAM_ENABLED == 1
sif::warning << "LinuxLibgpioIF::readGpio: Unknown GPIOD ID " << gpioId << std::endl; sif::warning << "LinuxLibgpioIF::readGpio: Unknown GPIOD ID " << gpioId << std::endl;
#else
sif::printWarning("LinuxLibgpioIF::readGpio: Unknown GPIOD ID %d\n", gpioId);
#endif
return UNKNOWN_GPIO_ID; return UNKNOWN_GPIO_ID;
} }
@ -299,13 +312,14 @@ ReturnValue_t LinuxLibgpioIF::checkForConflicts(GpioMap& mapToAdd){
for(auto& gpioConfig: mapToAdd) { for(auto& gpioConfig: mapToAdd) {
switch(gpioConfig.second->gpioType) { switch(gpioConfig.second->gpioType) {
case(gpio::GpioTypes::GPIO_REGULAR_BY_CHIP): case(gpio::GpioTypes::GPIO_REGULAR_BY_CHIP):
case(gpio::GpioTypes::GPIO_REGULAR_BY_LABEL): { case(gpio::GpioTypes::GPIO_REGULAR_BY_LABEL):
case(gpio::GpioTypes::GPIO_REGULAR_BY_LINE_NAME): {
auto regularGpio = dynamic_cast<GpiodRegularBase*>(gpioConfig.second); auto regularGpio = dynamic_cast<GpiodRegularBase*>(gpioConfig.second);
if(regularGpio == nullptr) { if(regularGpio == nullptr) {
return GPIO_TYPE_FAILURE; return GPIO_TYPE_FAILURE;
} }
/* Check for conflicts and remove duplicates if necessary */ // Check for conflicts and remove duplicates if necessary
result = checkForConflictsRegularGpio(gpioConfig.first, *regularGpio, mapToAdd); result = checkForConflictsById(gpioConfig.first, gpioConfig.second->gpioType, mapToAdd);
if(result != HasReturnvaluesIF::RETURN_OK) { if(result != HasReturnvaluesIF::RETURN_OK) {
status = result; status = result;
} }
@ -316,15 +330,22 @@ ReturnValue_t LinuxLibgpioIF::checkForConflicts(GpioMap& mapToAdd){
if(callbackGpio == nullptr) { if(callbackGpio == nullptr) {
return GPIO_TYPE_FAILURE; return GPIO_TYPE_FAILURE;
} }
/* Check for conflicts and remove duplicates if necessary */ // Check for conflicts and remove duplicates if necessary
result = checkForConflictsCallbackGpio(gpioConfig.first, callbackGpio, mapToAdd); result = checkForConflictsById(gpioConfig.first,
gpioConfig.second->gpioType, mapToAdd);
if(result != HasReturnvaluesIF::RETURN_OK) { if(result != HasReturnvaluesIF::RETURN_OK) {
status = result; status = result;
} }
break; break;
} }
default: { default: {
#if FSFW_CPP_OSTREAM_ENABLED == 1
sif::warning << "Invalid GPIO type detected for GPIO ID " << gpioConfig.first
<< std::endl;
#else
sif::printWarning("Invalid GPIO type detected for GPIO ID %d\n", gpioConfig.first);
#endif
status = GPIO_TYPE_FAILURE;
} }
} }
} }
@ -332,68 +353,86 @@ ReturnValue_t LinuxLibgpioIF::checkForConflicts(GpioMap& mapToAdd){
} }
ReturnValue_t LinuxLibgpioIF::checkForConflictsRegularGpio(gpioId_t gpioIdToCheck, ReturnValue_t LinuxLibgpioIF::checkForConflictsById(gpioId_t gpioIdToCheck,
GpiodRegularBase& gpioToCheck, GpioMap& mapToAdd) { gpio::GpioTypes expectedType, GpioMap& mapToAdd) {
/* Cross check with private map */ // Cross check with private map
gpioMapIter = gpioMap.find(gpioIdToCheck); gpioMapIter = gpioMap.find(gpioIdToCheck);
if(gpioMapIter != gpioMap.end()) { if(gpioMapIter != gpioMap.end()) {
auto& gpioType = gpioMapIter->second->gpioType; auto& gpioType = gpioMapIter->second->gpioType;
if(gpioType != gpio::GpioTypes::GPIO_REGULAR_BY_CHIP and bool eraseDuplicateDifferentType = false;
gpioType != gpio::GpioTypes::GPIO_REGULAR_BY_LABEL) { switch(expectedType) {
sif::warning << "LinuxLibgpioIF::checkForConflicts: ID already exists for different " case(gpio::GpioTypes::NONE): {
"GPIO type" << gpioIdToCheck << ". Removing duplicate." << std::endl; break;
}
case(gpio::GpioTypes::GPIO_REGULAR_BY_CHIP):
case(gpio::GpioTypes::GPIO_REGULAR_BY_LABEL):
case(gpio::GpioTypes::GPIO_REGULAR_BY_LINE_NAME): {
if(gpioType == gpio::GpioTypes::NONE or gpioType == gpio::GpioTypes::CALLBACK) {
eraseDuplicateDifferentType = true;
}
break;
}
case(gpio::GpioTypes::CALLBACK): {
if(gpioType != gpio::GpioTypes::CALLBACK) {
eraseDuplicateDifferentType = true;
}
}
}
if(eraseDuplicateDifferentType) {
#if FSFW_CPP_OSTREAM_ENABLED == 1
sif::warning << "LinuxLibgpioIF::checkForConflicts: ID already exists for "
"different GPIO type " << gpioIdToCheck <<
". Removing duplicate from map to add" << std::endl;
#else
sif::printWarning("LinuxLibgpioIF::checkForConflicts: ID already exists for "
"different GPIO type %d. Removing duplicate from map to add\n", gpioIdToCheck);
#endif
mapToAdd.erase(gpioIdToCheck); mapToAdd.erase(gpioIdToCheck);
return HasReturnvaluesIF::RETURN_OK; return GPIO_DUPLICATE_DETECTED;
}
auto ownRegularGpio = dynamic_cast<GpiodRegularBase*>(gpioMapIter->second);
if(ownRegularGpio == nullptr) {
return GPIO_TYPE_FAILURE;
} }
/* 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 */ #if FSFW_CPP_OSTREAM_ENABLED == 1
sif::warning << "LinuxLibgpioIF::checkForConflictsRegularGpio: Duplicate GPIO definition" sif::warning << "LinuxLibgpioIF::checkForConflictsRegularGpio: Duplicate GPIO "
<< " detected. Duplicate will be removed from map to add." << std::endl; "definition with ID " << gpioIdToCheck << " detected. " <<
mapToAdd.erase(gpioIdToCheck); "Duplicate will be removed from map to add" << std::endl;
} #else
return HasReturnvaluesIF::RETURN_OK; sif::printWarning("LinuxLibgpioIF::checkForConflictsRegularGpio: Duplicate GPIO definition "
} "with ID %d detected. Duplicate will be removed from map to add\n", gpioIdToCheck);
#endif
ReturnValue_t LinuxLibgpioIF::checkForConflictsCallbackGpio(gpioId_t gpioIdToCheck,
GpioCallback *callbackGpio, GpioMap& mapToAdd) {
/* Cross check with private map */
gpioMapIter = gpioMap.find(gpioIdToCheck);
if(gpioMapIter != gpioMap.end()) {
if(gpioMapIter->second->gpioType != gpio::GpioTypes::CALLBACK) {
sif::warning << "LinuxLibgpioIF::checkForConflicts: ID already exists for different "
"GPIO type" << gpioIdToCheck << ". Removing duplicate." << std::endl;
mapToAdd.erase(gpioIdToCheck);
return HasReturnvaluesIF::RETURN_OK;
}
/* Remove element from map to add because a entry for this GPIO
already exists */
sif::warning << "LinuxLibgpioIF::checkForConflictsRegularGpio: Duplicate GPIO definition"
<< " detected. Duplicate will be removed from map to add." << std::endl;
mapToAdd.erase(gpioIdToCheck); mapToAdd.erase(gpioIdToCheck);
return GPIO_DUPLICATE_DETECTED;
} }
return HasReturnvaluesIF::RETURN_OK; return HasReturnvaluesIF::RETURN_OK;
} }
void LinuxLibgpioIF::parseFindeLineResult(int result, std::string& lineName) { void LinuxLibgpioIF::parseFindeLineResult(int result, std::string& lineName) {
switch (result) { switch (result) {
#if FSFW_CPP_OSTREAM_ENABLED == 1
case LINE_NOT_EXISTS: case LINE_NOT_EXISTS:
sif::warning << "LinuxLibgpioIF::parseFindeLineResult: Line with name " << lineName case LINE_ERROR: {
<< " does not exist" << std::endl; sif::warning << "LinuxLibgpioIF::parseFindeLineResult: Line with name " << lineName <<
" does not exist" << std::endl;
break; break;
case LINE_ERROR: }
sif::warning << "LinuxLibgpioIF::parseFindeLineResult: " << "Line with name " default: {
<< lineName << " does not exist" << std::endl; sif::warning << "LinuxLibgpioIF::parseFindeLineResult: Unknown return code for line "
"with name " << lineName << std::endl;
break; break;
default: }
sif::warning << "LinuxLibgpioIF::parseFindeLineResult: Unknown return code for line with " #else
<< "name " << lineName << std::endl; case LINE_NOT_EXISTS:
case LINE_ERROR: {
sif::printWarning("LinuxLibgpioIF::parseFindeLineResult: Line with name %s "
"does not exist\n", lineName);
break; break;
} }
default: {
sif::printWarning("LinuxLibgpioIF::parseFindeLineResult: Unknown return code for line "
"with name %s\n", lineName);
break;
}
#endif
}
} }

View File

@ -28,6 +28,8 @@ public:
HasReturnvaluesIF::makeReturnCode(gpioRetvalId, 3); HasReturnvaluesIF::makeReturnCode(gpioRetvalId, 3);
static constexpr ReturnValue_t GPIO_INVALID_INSTANCE = static constexpr ReturnValue_t GPIO_INVALID_INSTANCE =
HasReturnvaluesIF::makeReturnCode(gpioRetvalId, 4); HasReturnvaluesIF::makeReturnCode(gpioRetvalId, 4);
static constexpr ReturnValue_t GPIO_DUPLICATE_DETECTED =
HasReturnvaluesIF::makeReturnCode(gpioRetvalId, 5);
LinuxLibgpioIF(object_id_t objectId); LinuxLibgpioIF(object_id_t objectId);
virtual ~LinuxLibgpioIF(); virtual ~LinuxLibgpioIF();
@ -75,10 +77,9 @@ private:
*/ */
ReturnValue_t checkForConflicts(GpioMap& mapToAdd); ReturnValue_t checkForConflicts(GpioMap& mapToAdd);
ReturnValue_t checkForConflictsRegularGpio(gpioId_t gpiodId, GpiodRegularBase& regularGpio, ReturnValue_t checkForConflictsById(gpioId_t gpiodId, gpio::GpioTypes type,
GpioMap& mapToAdd); GpioMap& mapToAdd);
ReturnValue_t checkForConflictsCallbackGpio(gpioId_t gpiodId, GpioCallback* regularGpio, //ReturnValue_t checkForConflictsCallbackGpio(gpioId_t gpiodId, GpioMap& mapToAdd);
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.