From 79936a33356d021ca99b27d2cbe1576f2a99afea Mon Sep 17 00:00:00 2001 From: Jakob Meier Date: Thu, 3 Feb 2022 10:14:47 +0100 Subject: [PATCH 1/7] uio mapper --- hal/src/fsfw_hal/linux/uio/CMakeLists.txt | 3 + hal/src/fsfw_hal/linux/uio/UioMapper.cpp | 80 +++++++++++++++++++++++ hal/src/fsfw_hal/linux/uio/UioMapper.h | 58 ++++++++++++++++ 3 files changed, 141 insertions(+) create mode 100644 hal/src/fsfw_hal/linux/uio/CMakeLists.txt create mode 100644 hal/src/fsfw_hal/linux/uio/UioMapper.cpp create mode 100644 hal/src/fsfw_hal/linux/uio/UioMapper.h diff --git a/hal/src/fsfw_hal/linux/uio/CMakeLists.txt b/hal/src/fsfw_hal/linux/uio/CMakeLists.txt new file mode 100644 index 000000000..e98a08657 --- /dev/null +++ b/hal/src/fsfw_hal/linux/uio/CMakeLists.txt @@ -0,0 +1,3 @@ +target_sources(${LIB_FSFW_NAME} PUBLIC + UioMapper.cpp +) diff --git a/hal/src/fsfw_hal/linux/uio/UioMapper.cpp b/hal/src/fsfw_hal/linux/uio/UioMapper.cpp new file mode 100644 index 000000000..bc2d06eb9 --- /dev/null +++ b/hal/src/fsfw_hal/linux/uio/UioMapper.cpp @@ -0,0 +1,80 @@ +#include "UioMapper.h" + +#include +#include + +#include +#include +#include + +#include "fsfw/serviceinterface.h" + +const char UioMapper::UIO_PATH_PREFIX[] = "/sys/class/uio/"; +const char UioMapper::MAP_SUBSTR[] = "/maps/map"; +const char UioMapper::SIZE_FILE_PATH[] = "/size"; + +UioMapper::UioMapper(std::string uioFile, int mapNum) : uioFile(uioFile), mapNum(mapNum) {} + +UioMapper::~UioMapper() {} + +ReturnValue_t UioMapper::getMappedAdress(uint32_t** address, Permissions permissions) { + ReturnValue_t result = HasReturnvaluesIF::RETURN_OK; + int fd = open(uioFile.c_str(), O_RDWR); + if (fd < 1) { +#if FSFW_CPP_OSTREAM_ENABLED == 1 + sif::warning << "PtmeAxiConfig::initialize: Invalid UIO device file" << std::endl; +#endif + return HasReturnvaluesIF::RETURN_FAILED; + } + size_t size = 0; + result = getMapSize(&size); + if (result != HasReturnvaluesIF::RETURN_OK) { + return result; + } + *address = static_cast( + mmap(NULL, size, static_cast(permissions), MAP_SHARED, fd, mapNum * getpagesize())); + + if (*address == MAP_FAILED) { +#if FSFW_CPP_OSTREAM_ENABLED == 1 + sif::warning << "UioMapper::getMappedAdress: Failed to map physical address of uio device " + << uioFile.c_str() << " and map" << static_cast(mapNum) << std::endl; +#endif + return HasReturnvaluesIF::RETURN_FAILED; + } + return HasReturnvaluesIF::RETURN_OK; +} + +ReturnValue_t UioMapper::getMapSize(size_t* size) { + std::stringstream namestream; + namestream << UIO_PATH_PREFIX << uioFile.substr(5, std::string::npos) << MAP_SUBSTR << mapNum + << SIZE_FILE_PATH; + FILE* fp; + fp = fopen(namestream.str().c_str(), "r"); + if (fp == nullptr) { +#if FSFW_CPP_OSTREAM_ENABLED == 1 + sif::warning << "UioMapper::getMapSize: Failed to open file " << namestream.str() << std::endl; +#endif + return HasReturnvaluesIF::RETURN_FAILED; + } + char hexstring[SIZE_HEX_STRING] = ""; + int items = fscanf(fp, "%s", hexstring); + if (items != 1) { +#if FSFW_CPP_OSTREAM_ENABLED == 1 + sif::warning << "UioMapper::getMapSize: Failed with error code " << errno + << " to read size " + "string from file " + << namestream.str() << std::endl; +#endif + return HasReturnvaluesIF::RETURN_FAILED; + } + items = sscanf(hexstring, "%lx", size); + if (items != 1) { +#if FSFW_CPP_OSTREAM_ENABLED == 1 + sif::warning << "UioMapper::getMapSize: Failed with error code " << errno << "to convert " + << "size of map" << mapNum << " to integer" << std::endl; +#endif + return HasReturnvaluesIF::RETURN_FAILED; + } + fclose(fp); + return HasReturnvaluesIF::RETURN_OK; +} diff --git a/hal/src/fsfw_hal/linux/uio/UioMapper.h b/hal/src/fsfw_hal/linux/uio/UioMapper.h new file mode 100644 index 000000000..20c90b4d2 --- /dev/null +++ b/hal/src/fsfw_hal/linux/uio/UioMapper.h @@ -0,0 +1,58 @@ +#ifndef FSFW_HAL_SRC_FSFW_HAL_LINUX_UIO_UIOMAPPER_H_ +#define FSFW_HAL_SRC_FSFW_HAL_LINUX_UIO_UIOMAPPER_H_ + +#include + +#include + +#include "fsfw/returnvalues/HasReturnvaluesIF.h" + +/** + * @brief Class to help opening uio device files and mapping the physical addresses into the user + * address space. + * + * @author J. Meier + */ +class UioMapper { + public: + enum class Permissions : int { + READ_ONLY = PROT_READ, + WRITE_ONLY = PROT_WRITE, + READ_WRITE = PROT_READ | PROT_WRITE + }; + + /** + * @brief Constructor + * + * @param uioFile The device file of the uiO to open + * @param uioMap Number of memory map. Most UIO drivers have only one map which has than 0. + */ + UioMapper(std::string uioFile, int mapNum = 0); + virtual ~UioMapper(); + + /** + * @brief Maps the physical address into user address space and returns the mapped address + * + * @address The mapped user space address + * @permissions Specifies the read/write permissions of the address region + */ + ReturnValue_t getMappedAdress(uint32_t** address, Permissions permissions); + + private: + static const char UIO_PATH_PREFIX[]; + static const char MAP_SUBSTR[]; + static const char SIZE_FILE_PATH[]; + static constexpr int SIZE_HEX_STRING = 10; + + std::string uioFile; + int mapNum = 0; + + /** + * @brief Reads the map size from the associated sysfs size file + * + * @param size The read map size + */ + ReturnValue_t getMapSize(size_t* size); +}; + +#endif /* FSFW_HAL_SRC_FSFW_HAL_LINUX_UIO_UIOMAPPER_H_ */ From 2d52042ed6923103ac4942b6961a00b77f52830c Mon Sep 17 00:00:00 2001 From: Jakob Meier Date: Thu, 3 Feb 2022 10:16:06 +0100 Subject: [PATCH 2/7] add uio subdir --- hal/src/fsfw_hal/linux/CMakeLists.txt | 13 +++++++++---- 1 file changed, 9 insertions(+), 4 deletions(-) diff --git a/hal/src/fsfw_hal/linux/CMakeLists.txt b/hal/src/fsfw_hal/linux/CMakeLists.txt index 5944b0e5d..0fb2d385c 100644 --- a/hal/src/fsfw_hal/linux/CMakeLists.txt +++ b/hal/src/fsfw_hal/linux/CMakeLists.txt @@ -4,10 +4,15 @@ endif() target_sources(${LIB_FSFW_NAME} PRIVATE UnixFileGuard.cpp + CommandExecutor.cpp utility.cpp ) -add_subdirectory(gpio) -add_subdirectory(spi) -add_subdirectory(i2c) -add_subdirectory(uart) \ No newline at end of file +if(FSFW_HAL_LINUX_ADD_PERIPHERAL_DRIVERS) + add_subdirectory(gpio) + add_subdirectory(spi) + add_subdirectory(i2c) + add_subdirectory(uart) +endif() + +add_subdirectory(uio) From 40329a33b2b28b0d96e80ba8dc005f23a00f2245 Mon Sep 17 00:00:00 2001 From: Jakob Meier Date: Thu, 3 Feb 2022 10:19:33 +0100 Subject: [PATCH 3/7] prepared for proper pr --- hal/src/fsfw_hal/linux/CMakeLists.txt | 12 ++++-------- 1 file changed, 4 insertions(+), 8 deletions(-) diff --git a/hal/src/fsfw_hal/linux/CMakeLists.txt b/hal/src/fsfw_hal/linux/CMakeLists.txt index 0fb2d385c..056b59c83 100644 --- a/hal/src/fsfw_hal/linux/CMakeLists.txt +++ b/hal/src/fsfw_hal/linux/CMakeLists.txt @@ -4,15 +4,11 @@ endif() target_sources(${LIB_FSFW_NAME} PRIVATE UnixFileGuard.cpp - CommandExecutor.cpp utility.cpp ) -if(FSFW_HAL_LINUX_ADD_PERIPHERAL_DRIVERS) - add_subdirectory(gpio) - add_subdirectory(spi) - add_subdirectory(i2c) - add_subdirectory(uart) -endif() - +add_subdirectory(gpio) +add_subdirectory(spi) +add_subdirectory(i2c) +add_subdirectory(uart) add_subdirectory(uio) From f08d291e3e22950e729c327c101d22a943910e9b Mon Sep 17 00:00:00 2001 From: Jakob Meier Date: Thu, 3 Feb 2022 11:07:51 +0100 Subject: [PATCH 4/7] fix to remove compiler warning --- hal/src/fsfw_hal/linux/uio/UioMapper.cpp | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/hal/src/fsfw_hal/linux/uio/UioMapper.cpp b/hal/src/fsfw_hal/linux/uio/UioMapper.cpp index bc2d06eb9..b29ed63e4 100644 --- a/hal/src/fsfw_hal/linux/uio/UioMapper.cpp +++ b/hal/src/fsfw_hal/linux/uio/UioMapper.cpp @@ -67,7 +67,11 @@ ReturnValue_t UioMapper::getMapSize(size_t* size) { #endif return HasReturnvaluesIF::RETURN_FAILED; } - items = sscanf(hexstring, "%lx", size); + uint32_t sizeTmp = 0; + items = sscanf(hexstring, "%x", &sizeTmp); + if(size != nullptr) { + *size = sizeTmp; + } if (items != 1) { #if FSFW_CPP_OSTREAM_ENABLED == 1 sif::warning << "UioMapper::getMapSize: Failed with error code " << errno << "to convert " From 1b41153ee6f4002c6215493bd50ec076fd0806df Mon Sep 17 00:00:00 2001 From: Robin Mueller Date: Fri, 4 Feb 2022 10:16:37 +0100 Subject: [PATCH 5/7] add uio subdirectory --- hal/src/fsfw_hal/linux/CMakeLists.txt | 2 ++ 1 file changed, 2 insertions(+) diff --git a/hal/src/fsfw_hal/linux/CMakeLists.txt b/hal/src/fsfw_hal/linux/CMakeLists.txt index 5ec8af060..0fb2d385c 100644 --- a/hal/src/fsfw_hal/linux/CMakeLists.txt +++ b/hal/src/fsfw_hal/linux/CMakeLists.txt @@ -14,3 +14,5 @@ if(FSFW_HAL_LINUX_ADD_PERIPHERAL_DRIVERS) add_subdirectory(i2c) add_subdirectory(uart) endif() + +add_subdirectory(uio) From 9897f5130720fd5a6c9a1dc40da3bf5b895fb29f Mon Sep 17 00:00:00 2001 From: Jakob Meier Date: Mon, 14 Feb 2022 08:43:10 +0100 Subject: [PATCH 6/7] added flose and changed warning message to error message --- hal/src/fsfw_hal/linux/uio/UioMapper.cpp | 13 ++++++++----- 1 file changed, 8 insertions(+), 5 deletions(-) diff --git a/hal/src/fsfw_hal/linux/uio/UioMapper.cpp b/hal/src/fsfw_hal/linux/uio/UioMapper.cpp index b29ed63e4..6a7673e3e 100644 --- a/hal/src/fsfw_hal/linux/uio/UioMapper.cpp +++ b/hal/src/fsfw_hal/linux/uio/UioMapper.cpp @@ -22,7 +22,7 @@ ReturnValue_t UioMapper::getMappedAdress(uint32_t** address, Permissions permiss int fd = open(uioFile.c_str(), O_RDWR); if (fd < 1) { #if FSFW_CPP_OSTREAM_ENABLED == 1 - sif::warning << "PtmeAxiConfig::initialize: Invalid UIO device file" << std::endl; + sif::error << "PtmeAxiConfig::initialize: Invalid UIO device file" << std::endl; #endif return HasReturnvaluesIF::RETURN_FAILED; } @@ -36,7 +36,7 @@ ReturnValue_t UioMapper::getMappedAdress(uint32_t** address, Permissions permiss if (*address == MAP_FAILED) { #if FSFW_CPP_OSTREAM_ENABLED == 1 - sif::warning << "UioMapper::getMappedAdress: Failed to map physical address of uio device " + sif::error << "UioMapper::getMappedAdress: Failed to map physical address of uio device " << uioFile.c_str() << " and map" << static_cast(mapNum) << std::endl; #endif return HasReturnvaluesIF::RETURN_FAILED; @@ -52,19 +52,21 @@ ReturnValue_t UioMapper::getMapSize(size_t* size) { fp = fopen(namestream.str().c_str(), "r"); if (fp == nullptr) { #if FSFW_CPP_OSTREAM_ENABLED == 1 - sif::warning << "UioMapper::getMapSize: Failed to open file " << namestream.str() << std::endl; + sif::error << "UioMapper::getMapSize: Failed to open file " << namestream.str() << std::endl; #endif + fclose(fp); return HasReturnvaluesIF::RETURN_FAILED; } char hexstring[SIZE_HEX_STRING] = ""; int items = fscanf(fp, "%s", hexstring); if (items != 1) { #if FSFW_CPP_OSTREAM_ENABLED == 1 - sif::warning << "UioMapper::getMapSize: Failed with error code " << errno + sif::error << "UioMapper::getMapSize: Failed with error code " << errno << " to read size " "string from file " << namestream.str() << std::endl; #endif + fclose(fp); return HasReturnvaluesIF::RETURN_FAILED; } uint32_t sizeTmp = 0; @@ -74,9 +76,10 @@ ReturnValue_t UioMapper::getMapSize(size_t* size) { } if (items != 1) { #if FSFW_CPP_OSTREAM_ENABLED == 1 - sif::warning << "UioMapper::getMapSize: Failed with error code " << errno << "to convert " + sif::error << "UioMapper::getMapSize: Failed with error code " << errno << "to convert " << "size of map" << mapNum << " to integer" << std::endl; #endif + fclose(fp); return HasReturnvaluesIF::RETURN_FAILED; } fclose(fp); From 120750f22aab31cfa6f3d8edec1fe784a707b64e Mon Sep 17 00:00:00 2001 From: Jakob Meier Date: Mon, 14 Feb 2022 08:51:53 +0100 Subject: [PATCH 7/7] removed one fclose --- hal/src/fsfw_hal/linux/uio/UioMapper.cpp | 1 - 1 file changed, 1 deletion(-) diff --git a/hal/src/fsfw_hal/linux/uio/UioMapper.cpp b/hal/src/fsfw_hal/linux/uio/UioMapper.cpp index 6a7673e3e..5eb44c5e5 100644 --- a/hal/src/fsfw_hal/linux/uio/UioMapper.cpp +++ b/hal/src/fsfw_hal/linux/uio/UioMapper.cpp @@ -54,7 +54,6 @@ ReturnValue_t UioMapper::getMapSize(size_t* size) { #if FSFW_CPP_OSTREAM_ENABLED == 1 sif::error << "UioMapper::getMapSize: Failed to open file " << namestream.str() << std::endl; #endif - fclose(fp); return HasReturnvaluesIF::RETURN_FAILED; } char hexstring[SIZE_HEX_STRING] = "";