From fd41c3b4dbaa6d0a0c6b9909479678e789329b56 Mon Sep 17 00:00:00 2001 From: "Jakob.Meier" Date: Tue, 12 Jan 2021 10:39:24 +0100 Subject: [PATCH] tmp1075handler and I2cComIF complete --- .gitignore | 1 + bsp_q7s/comIF/I2cComIF.cpp | 92 ++++++++++++++++++++++++-------- bsp_q7s/comIF/I2cComIF.h | 12 ++++- eive_obsw cmake debug/makefile | 56 ------------------- eive_obsw cmake debug/sources.mk | 56 ------------------- fsfwconfig/devices/addresses.h | 7 ++- 6 files changed, 86 insertions(+), 138 deletions(-) diff --git a/.gitignore b/.gitignore index ff23c0fd..2ccfca61 100644 --- a/.gitignore +++ b/.gitignore @@ -19,3 +19,4 @@ __pycache__ !misc/eclipse/**/.cproject !misc/eclipse/**/.project +/eive_obsw cmake debug/ diff --git a/bsp_q7s/comIF/I2cComIF.cpp b/bsp_q7s/comIF/I2cComIF.cpp index 906e5350..ccf4ee2a 100644 --- a/bsp_q7s/comIF/I2cComIF.cpp +++ b/bsp_q7s/comIF/I2cComIF.cpp @@ -14,6 +14,10 @@ I2cComIF::I2cComIF(object_id_t objectId): SystemObject(objectId){ I2cComIF::~I2cComIF() {} ReturnValue_t I2cComIF::initializeInterface(CookieIF * cookie) { + + address_t i2cAddress; + std::string deviceFile; + if(cookie == nullptr) { return NULLPOINTER; } @@ -24,40 +28,40 @@ ReturnValue_t I2cComIF::initializeInterface(CookieIF * cookie) { return NULLPOINTER; } - address_t i2cAddress = i2cCookie->getAddress(); - - int fd; - std::string deviceFile = i2cCookie->getDeviceFile(); - fd = open(deviceFile.c_str(), O_RDWR); - if (fd < 0) { - sif::error << "I2cComIF: Opening i2c device failed with error code " - << errno << ". Error description: " << strerror(errno) - << std::endl; - } - - if (ioctl(fd, I2C_SLAVE, i2cAddress) < 0) { - sif::error << "I2cComIF: Specifying target device failed with error " - << "code " << errno << ". Error description " - << strerror(errno) << std::endl; - } + i2cAddress = i2cCookie->getAddress(); i2cDeviceMapIter = i2cDeviceMap.find(i2cAddress); if(i2cDeviceMapIter == i2cDeviceMap.end()) { size_t maxReplyLen = i2cCookie->getMaxReplyLen(); - I2cInstance_t i2cInstance = {fd, std::vector(maxReplyLen), 0}; - i2cDeviceMap.emplace(i2cAddress, i2cInstance); + I2cInstance_t i2cInstance = {std::vector(maxReplyLen), 0}; + std::pair status = i2cDeviceMap.emplace(i2cAddress, i2cInstance); + if (status.second == false) { + sif::error << "I2cComIF::initializeInterface: Failed to insert " + << "device with address " << i2cAddress << "to I2C device " + << "map" << std::endl; + return HasReturnvaluesIF::RETURN_OK; + } } else { sif::error << "I2cComIF: Device with address " << i2cAddress << "already in use" << std::endl; } + i2cDeviceMapIter = i2cDeviceMap.find(i2cAddress); + if(i2cDeviceMapIter == i2cDeviceMap.end()) { + sif::error << "Failure" << std::endl; + } + return HasReturnvaluesIF::RETURN_OK; } ReturnValue_t I2cComIF::sendMessage(CookieIF *cookie, const uint8_t *sendData, size_t sendLen) { + ReturnValue_t result; + int fd; + std::string deviceFile; + if(sendData == nullptr) { sif::error << "I2cComIF::sendMessage: Send Data is nullptr" << std::endl; @@ -82,15 +86,21 @@ ReturnValue_t I2cComIF::sendMessage(CookieIF *cookie, << "registered in i2cDeviceMap" << std::endl; return HasReturnvaluesIF::RETURN_FAILED; } - int fd = i2cDeviceMapIter->second.fileDescriptor; + + deviceFile = i2cCookie->getDeviceFile(); + result = openDevice(deviceFile, i2cAddress, &fd); + if (result != HasReturnvaluesIF::RETURN_OK){ + return result; + } if (write(fd, sendData, sendLen) != (int)sendLen) { sif::error << "I2cComIF::sendMessage: Failed to send data to I2C " "device with error code " << errno << ". Error description: " << strerror(errno) << std::endl; + close(fd); return HasReturnvaluesIF::RETURN_FAILED; } - + close(fd); return HasReturnvaluesIF::RETURN_OK; } @@ -101,6 +111,10 @@ ReturnValue_t I2cComIF::getSendSuccess(CookieIF *cookie) { ReturnValue_t I2cComIF::requestReceiveMessage(CookieIF *cookie, size_t requestLen) { + ReturnValue_t result; + int fd; + std::string deviceFile; + if (requestLen == 0) { return HasReturnvaluesIF::RETURN_OK; } @@ -109,6 +123,7 @@ ReturnValue_t I2cComIF::requestReceiveMessage(CookieIF *cookie, if(i2cCookie == nullptr) { sif::error << "I2cComIF::sendMessage: Invalid I2C Cookie!" << std::endl; + i2cDeviceMapIter->second.replyLen = 0; return NULLPOINTER; } @@ -117,20 +132,32 @@ ReturnValue_t I2cComIF::requestReceiveMessage(CookieIF *cookie, if (i2cDeviceMapIter == i2cDeviceMap.end()) { sif::error << "I2cComIF::requestReceiveMessage: i2cAddress of Cookie not " << "registered in i2cDeviceMap" << std::endl; + i2cDeviceMapIter->second.replyLen = 0; return HasReturnvaluesIF::RETURN_FAILED; } - int fd = i2cDeviceMapIter->second.fileDescriptor; + + deviceFile = i2cCookie->getDeviceFile(); + result = openDevice(deviceFile, i2cAddress, &fd); + if (result != HasReturnvaluesIF::RETURN_OK){ + i2cDeviceMapIter->second.replyLen = 0; + return result; + } + uint8_t* replyBuffer = i2cDeviceMapIter->second.replyBuffer.data(); if (read(fd, replyBuffer, requestLen) != (int)requestLen) { sif::error << "I2cComIF::requestReceiveMessage: Reading from I2C " << "device failed with error code " << errno <<". Description" << " of error: " << strerror(errno) << std::endl; + close(fd); + i2cDeviceMapIter->second.replyLen = 0; return HasReturnvaluesIF::RETURN_FAILED; } i2cDeviceMapIter->second.replyLen = requestLen; + close(fd); + return HasReturnvaluesIF::RETURN_OK; } @@ -146,8 +173,8 @@ ReturnValue_t I2cComIF::readReceivedMessage(CookieIF *cookie, address_t i2cAddress = i2cCookie->getAddress(); i2cDeviceMapIter = i2cDeviceMap.find(i2cAddress); if (i2cDeviceMapIter == i2cDeviceMap.end()) { - sif::error << "I2cComIF::getReplyBuffer: i2cAddress of Cookie not " - << "registered in i2cDeviceMap" << std::endl; + sif::error << "I2cComIF::readReceivedMessage: i2cAddress of Cookie not " + << "found in i2cDeviceMap" << std::endl; return HasReturnvaluesIF::RETURN_FAILED; } *buffer = i2cDeviceMapIter->second.replyBuffer.data(); @@ -155,3 +182,22 @@ ReturnValue_t I2cComIF::readReceivedMessage(CookieIF *cookie, return HasReturnvaluesIF::RETURN_OK; } + +ReturnValue_t I2cComIF::openDevice(std::string deviceFile, + address_t i2cAddress, int* fileDescriptor) { + *fileDescriptor = open(deviceFile.c_str(), O_RDWR); + if (*fileDescriptor < 0) { + sif::error << "I2cComIF: Opening i2c device failed with error code " + << errno << ". Error description: " << strerror(errno) + << std::endl; + return HasReturnvaluesIF::RETURN_FAILED; + } + + if (ioctl(*fileDescriptor, I2C_SLAVE, i2cAddress) < 0) { + sif::error << "I2cComIF: Specifying target device failed with error " + << "code " << errno << ". Error description " + << strerror(errno) << std::endl; + return HasReturnvaluesIF::RETURN_FAILED; + } + return HasReturnvaluesIF::RETURN_OK; +} diff --git a/bsp_q7s/comIF/I2cComIF.h b/bsp_q7s/comIF/I2cComIF.h index f643bc76..7bf339ad 100644 --- a/bsp_q7s/comIF/I2cComIF.h +++ b/bsp_q7s/comIF/I2cComIF.h @@ -32,7 +32,6 @@ public: private: typedef struct I2cInstance { - int fileDescriptor; std::vector replyBuffer; size_t replyLen; } I2cInstance_t; @@ -44,6 +43,17 @@ private: * the appropriate file descriptor will be stored */ I2cDeviceMap i2cDeviceMap; I2cDeviceMapIter i2cDeviceMapIter; + + /** + * @brief This function opens an i2c device and binds the opened file + * to a specific i2c address. + * @param deviceFile The name of the device file. E.g. i2c-0 + * @param i2cAddress The address of the i2c slave device. + * @param fileDescriptor Pointer to device descriptor. + * @return RETURN_OK if successful, otherwise RETURN_FAILED. + */ + ReturnValue_t openDevice(std::string deviceFile, + address_t i2cAddress, int* fileDescriptor); }; #endif /* BSP_Q7S_COMIF_I2COMIF_H_ */ diff --git a/eive_obsw cmake debug/makefile b/eive_obsw cmake debug/makefile index fdd6d5ea..7d7dbadf 100644 --- a/eive_obsw cmake debug/makefile +++ b/eive_obsw cmake debug/makefile @@ -229,62 +229,6 @@ RM := del -include _obj/linux/devel/bsp_q7s/comIF/subdir.mk -include _obj/linux/devel/bsp_q7s/boardconfig/subdir.mk -include _obj/linux/devel/bsp_q7s/subdir.mk --include Debug/libcsp/CMakeFiles/libcsp.dir/src/transport/subdir.mk --include Debug/libcsp/CMakeFiles/libcsp.dir/src/rtable/subdir.mk --include Debug/libcsp/CMakeFiles/libcsp.dir/src/interfaces/subdir.mk --include Debug/libcsp/CMakeFiles/libcsp.dir/src/drivers/can/subdir.mk --include Debug/libcsp/CMakeFiles/libcsp.dir/src/crypto/subdir.mk --include Debug/libcsp/CMakeFiles/libcsp.dir/src/arch/posix/subdir.mk --include Debug/libcsp/CMakeFiles/libcsp.dir/src/subdir.mk --include Debug/fsfw/CMakeFiles/fsfw.dir/tmtcservices/subdir.mk --include Debug/fsfw/CMakeFiles/fsfw.dir/tmtcpacket/pus/subdir.mk --include Debug/fsfw/CMakeFiles/fsfw.dir/tmtcpacket/packetmatcher/subdir.mk --include Debug/fsfw/CMakeFiles/fsfw.dir/tmtcpacket/subdir.mk --include Debug/fsfw/CMakeFiles/fsfw.dir/tmstorage/subdir.mk --include Debug/fsfw/CMakeFiles/fsfw.dir/timemanager/subdir.mk --include Debug/fsfw/CMakeFiles/fsfw.dir/thermal/subdir.mk --include Debug/fsfw/CMakeFiles/fsfw.dir/tcdistribution/subdir.mk --include Debug/fsfw/CMakeFiles/fsfw.dir/tasks/subdir.mk --include Debug/fsfw/CMakeFiles/fsfw.dir/subsystem/modes/subdir.mk --include Debug/fsfw/CMakeFiles/fsfw.dir/subsystem/subdir.mk --include Debug/fsfw/CMakeFiles/fsfw.dir/storagemanager/subdir.mk --include Debug/fsfw/CMakeFiles/fsfw.dir/serviceinterface/subdir.mk --include Debug/fsfw/CMakeFiles/fsfw.dir/serialize/subdir.mk --include Debug/fsfw/CMakeFiles/fsfw.dir/rmap/subdir.mk --include Debug/fsfw/CMakeFiles/fsfw.dir/pus/subdir.mk --include Debug/fsfw/CMakeFiles/fsfw.dir/power/subdir.mk --include Debug/fsfw/CMakeFiles/fsfw.dir/parameters/subdir.mk --include Debug/fsfw/CMakeFiles/fsfw.dir/osal/linux/subdir.mk --include Debug/fsfw/CMakeFiles/fsfw.dir/objectmanager/subdir.mk --include Debug/fsfw/CMakeFiles/fsfw.dir/monitoring/subdir.mk --include Debug/fsfw/CMakeFiles/fsfw.dir/modes/subdir.mk --include Debug/fsfw/CMakeFiles/fsfw.dir/memory/subdir.mk --include Debug/fsfw/CMakeFiles/fsfw.dir/ipc/subdir.mk --include Debug/fsfw/CMakeFiles/fsfw.dir/internalError/subdir.mk --include Debug/fsfw/CMakeFiles/fsfw.dir/housekeeping/subdir.mk --include Debug/fsfw/CMakeFiles/fsfw.dir/health/subdir.mk --include Debug/fsfw/CMakeFiles/fsfw.dir/globalfunctions/math/subdir.mk --include Debug/fsfw/CMakeFiles/fsfw.dir/globalfunctions/subdir.mk --include Debug/fsfw/CMakeFiles/fsfw.dir/fdir/subdir.mk --include Debug/fsfw/CMakeFiles/fsfw.dir/events/eventmatching/subdir.mk --include Debug/fsfw/CMakeFiles/fsfw.dir/events/subdir.mk --include Debug/fsfw/CMakeFiles/fsfw.dir/devicehandlers/subdir.mk --include Debug/fsfw/CMakeFiles/fsfw.dir/datapoollocal/subdir.mk --include Debug/fsfw/CMakeFiles/fsfw.dir/datapool/subdir.mk --include Debug/fsfw/CMakeFiles/fsfw.dir/datalinklayer/subdir.mk --include Debug/fsfw/CMakeFiles/fsfw.dir/coordinates/subdir.mk --include Debug/fsfw/CMakeFiles/fsfw.dir/controller/subdir.mk --include Debug/fsfw/CMakeFiles/fsfw.dir/container/subdir.mk --include Debug/fsfw/CMakeFiles/fsfw.dir/action/subdir.mk --include Debug/CMakeFiles/eive_obsw.dir/mission/utility/subdir.mk --include Debug/CMakeFiles/eive_obsw.dir/mission/devices/subdir.mk --include Debug/CMakeFiles/eive_obsw.dir/mission/core/subdir.mk --include Debug/CMakeFiles/eive_obsw.dir/fsfwconfig/pollingsequence/subdir.mk --include Debug/CMakeFiles/eive_obsw.dir/fsfwconfig/ipc/subdir.mk --include Debug/CMakeFiles/eive_obsw.dir/bsp_q7s/comIF/cookies/subdir.mk --include Debug/CMakeFiles/eive_obsw.dir/bsp_q7s/comIF/subdir.mk --include Debug/CMakeFiles/eive_obsw.dir/bsp_q7s/boardconfig/subdir.mk --include Debug/CMakeFiles/eive_obsw.dir/bsp_q7s/subdir.mk -include Debug/CMakeFiles/3.19.1/CompilerIdCXX/subdir.mk -include Debug/CMakeFiles/3.19.1/CompilerIdC/subdir.mk -include subdir.mk diff --git a/eive_obsw cmake debug/sources.mk b/eive_obsw cmake debug/sources.mk index 8143a557..1ef862fa 100644 --- a/eive_obsw cmake debug/sources.mk +++ b/eive_obsw cmake debug/sources.mk @@ -25,62 +25,6 @@ CPP_DEPS := SUBDIRS := \ Debug/CMakeFiles/3.19.1/CompilerIdC \ Debug/CMakeFiles/3.19.1/CompilerIdCXX \ -Debug/CMakeFiles/eive_obsw.dir/bsp_q7s \ -Debug/CMakeFiles/eive_obsw.dir/bsp_q7s/boardconfig \ -Debug/CMakeFiles/eive_obsw.dir/bsp_q7s/comIF \ -Debug/CMakeFiles/eive_obsw.dir/bsp_q7s/comIF/cookies \ -Debug/CMakeFiles/eive_obsw.dir/fsfwconfig/ipc \ -Debug/CMakeFiles/eive_obsw.dir/fsfwconfig/pollingsequence \ -Debug/CMakeFiles/eive_obsw.dir/mission/core \ -Debug/CMakeFiles/eive_obsw.dir/mission/devices \ -Debug/CMakeFiles/eive_obsw.dir/mission/utility \ -Debug/fsfw/CMakeFiles/fsfw.dir/action \ -Debug/fsfw/CMakeFiles/fsfw.dir/container \ -Debug/fsfw/CMakeFiles/fsfw.dir/controller \ -Debug/fsfw/CMakeFiles/fsfw.dir/coordinates \ -Debug/fsfw/CMakeFiles/fsfw.dir/datalinklayer \ -Debug/fsfw/CMakeFiles/fsfw.dir/datapool \ -Debug/fsfw/CMakeFiles/fsfw.dir/datapoollocal \ -Debug/fsfw/CMakeFiles/fsfw.dir/devicehandlers \ -Debug/fsfw/CMakeFiles/fsfw.dir/events \ -Debug/fsfw/CMakeFiles/fsfw.dir/events/eventmatching \ -Debug/fsfw/CMakeFiles/fsfw.dir/fdir \ -Debug/fsfw/CMakeFiles/fsfw.dir/globalfunctions \ -Debug/fsfw/CMakeFiles/fsfw.dir/globalfunctions/math \ -Debug/fsfw/CMakeFiles/fsfw.dir/health \ -Debug/fsfw/CMakeFiles/fsfw.dir/housekeeping \ -Debug/fsfw/CMakeFiles/fsfw.dir/internalError \ -Debug/fsfw/CMakeFiles/fsfw.dir/ipc \ -Debug/fsfw/CMakeFiles/fsfw.dir/memory \ -Debug/fsfw/CMakeFiles/fsfw.dir/modes \ -Debug/fsfw/CMakeFiles/fsfw.dir/monitoring \ -Debug/fsfw/CMakeFiles/fsfw.dir/objectmanager \ -Debug/fsfw/CMakeFiles/fsfw.dir/osal/linux \ -Debug/fsfw/CMakeFiles/fsfw.dir/parameters \ -Debug/fsfw/CMakeFiles/fsfw.dir/power \ -Debug/fsfw/CMakeFiles/fsfw.dir/pus \ -Debug/fsfw/CMakeFiles/fsfw.dir/rmap \ -Debug/fsfw/CMakeFiles/fsfw.dir/serialize \ -Debug/fsfw/CMakeFiles/fsfw.dir/serviceinterface \ -Debug/fsfw/CMakeFiles/fsfw.dir/storagemanager \ -Debug/fsfw/CMakeFiles/fsfw.dir/subsystem \ -Debug/fsfw/CMakeFiles/fsfw.dir/subsystem/modes \ -Debug/fsfw/CMakeFiles/fsfw.dir/tasks \ -Debug/fsfw/CMakeFiles/fsfw.dir/tcdistribution \ -Debug/fsfw/CMakeFiles/fsfw.dir/thermal \ -Debug/fsfw/CMakeFiles/fsfw.dir/timemanager \ -Debug/fsfw/CMakeFiles/fsfw.dir/tmstorage \ -Debug/fsfw/CMakeFiles/fsfw.dir/tmtcpacket \ -Debug/fsfw/CMakeFiles/fsfw.dir/tmtcpacket/packetmatcher \ -Debug/fsfw/CMakeFiles/fsfw.dir/tmtcpacket/pus \ -Debug/fsfw/CMakeFiles/fsfw.dir/tmtcservices \ -Debug/libcsp/CMakeFiles/libcsp.dir/src/arch/posix \ -Debug/libcsp/CMakeFiles/libcsp.dir/src/crypto \ -Debug/libcsp/CMakeFiles/libcsp.dir/src \ -Debug/libcsp/CMakeFiles/libcsp.dir/src/drivers/can \ -Debug/libcsp/CMakeFiles/libcsp.dir/src/interfaces \ -Debug/libcsp/CMakeFiles/libcsp.dir/src/rtable \ -Debug/libcsp/CMakeFiles/libcsp.dir/src/transport \ _obj/linux/devel/bsp_q7s \ _obj/linux/devel/bsp_q7s/boardconfig \ _obj/linux/devel/bsp_q7s/comIF \ diff --git a/fsfwconfig/devices/addresses.h b/fsfwconfig/devices/addresses.h index c16f588d..c894fcc7 100644 --- a/fsfwconfig/devices/addresses.h +++ b/fsfwconfig/devices/addresses.h @@ -15,14 +15,17 @@ namespace addresses { enum logicalAddresses: address_t { PCDU, - TMP1075_TCS_1 = 72, - TMP1075_TCS_2 = 73, /* Dummy and Test Addresses */ DUMMY_ECHO = 129, DUMMY_GPS0 = 130, DUMMY_GPS1 = 131, }; + enum i2cAddresses: address_t { + TMP1075_TCS_1 = 72, + TMP1075_TCS_2 = 73, + }; + /* Addresses of devices supporting the CSP protocol */ enum cspAddresses: uint8_t { P60DOCK = 4,