simplified i2c code
This commit is contained in:
parent
101a7696c3
commit
0045cd2062
@ -6,6 +6,7 @@
|
|||||||
#include <fsfw/objectmanager/SystemObject.h>
|
#include <fsfw/objectmanager/SystemObject.h>
|
||||||
|
|
||||||
class GpioCookie;
|
class GpioCookie;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @brief This class implements the GpioIF for a linux based system. The
|
* @brief This class implements the GpioIF for a linux based system. The
|
||||||
* implementation is based on the libgpiod lib which requires linux 4.8
|
* implementation is based on the libgpiod lib which requires linux 4.8
|
||||||
|
@ -14,191 +14,184 @@ I2cComIF::I2cComIF(object_id_t objectId): SystemObject(objectId){
|
|||||||
|
|
||||||
I2cComIF::~I2cComIF() {}
|
I2cComIF::~I2cComIF() {}
|
||||||
|
|
||||||
ReturnValue_t I2cComIF::initializeInterface(CookieIF * cookie) {
|
ReturnValue_t I2cComIF::initializeInterface(CookieIF* cookie) {
|
||||||
|
|
||||||
address_t i2cAddress;
|
address_t i2cAddress;
|
||||||
std::string deviceFile;
|
std::string deviceFile;
|
||||||
|
|
||||||
if(cookie == nullptr) {
|
if(cookie == nullptr) {
|
||||||
return NULLPOINTER;
|
sif::error << "I2cComIF::initializeInterface: Invalid cookie!" << std::endl;
|
||||||
}
|
return NULLPOINTER;
|
||||||
I2cCookie* i2cCookie = dynamic_cast<I2cCookie*>(cookie);
|
}
|
||||||
if(i2cCookie == nullptr) {
|
I2cCookie* i2cCookie = dynamic_cast<I2cCookie*>(cookie);
|
||||||
sif::error << "I2cComIF: Invalid I2C Cookie!"
|
if(i2cCookie == nullptr) {
|
||||||
<< std::endl;
|
sif::error << "I2cComIF::initializeInterface: Invalid I2C cookie!" << std::endl;
|
||||||
return NULLPOINTER;
|
return NULLPOINTER;
|
||||||
}
|
}
|
||||||
|
|
||||||
i2cAddress = i2cCookie->getAddress();
|
i2cAddress = i2cCookie->getAddress();
|
||||||
|
|
||||||
i2cDeviceMapIter = i2cDeviceMap.find(i2cAddress);
|
i2cDeviceMapIter = i2cDeviceMap.find(i2cAddress);
|
||||||
if(i2cDeviceMapIter == i2cDeviceMap.end()) {
|
if(i2cDeviceMapIter == i2cDeviceMap.end()) {
|
||||||
size_t maxReplyLen = i2cCookie->getMaxReplyLen();
|
size_t maxReplyLen = i2cCookie->getMaxReplyLen();
|
||||||
I2cInstance_t i2cInstance = {std::vector<uint8_t>(maxReplyLen), 0};
|
I2cInstance_t i2cInstance = {std::vector<uint8_t>(maxReplyLen), 0};
|
||||||
std::pair status = i2cDeviceMap.emplace(i2cAddress, i2cInstance);
|
auto statusPair = i2cDeviceMap.emplace(i2cAddress, i2cInstance);
|
||||||
if (status.second == false) {
|
if (not statusPair.second) {
|
||||||
sif::error << "I2cComIF::initializeInterface: Failed to insert "
|
sif::error << "I2cComIF::initializeInterface: Failed to insert device with address " <<
|
||||||
<< "device with address " << i2cAddress << "to I2C device "
|
i2cAddress << "to I2C device " << "map" << std::endl;
|
||||||
<< "map" << std::endl;
|
return HasReturnvaluesIF::RETURN_FAILED;
|
||||||
return HasReturnvaluesIF::RETURN_OK;
|
}
|
||||||
}
|
return HasReturnvaluesIF::RETURN_OK;
|
||||||
}
|
}
|
||||||
else {
|
|
||||||
sif::error << "I2cComIF: Device with address " << i2cAddress
|
|
||||||
<< "already in use" << std::endl;
|
|
||||||
}
|
|
||||||
|
|
||||||
i2cDeviceMapIter = i2cDeviceMap.find(i2cAddress);
|
sif::error << "I2cComIF::initializeInterface: Device with address " << i2cAddress <<
|
||||||
if(i2cDeviceMapIter == i2cDeviceMap.end()) {
|
"already in use" << std::endl;
|
||||||
sif::error << "Failure" << std::endl;
|
return HasReturnvaluesIF::RETURN_FAILED;
|
||||||
}
|
|
||||||
|
|
||||||
return HasReturnvaluesIF::RETURN_OK;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
ReturnValue_t I2cComIF::sendMessage(CookieIF *cookie,
|
ReturnValue_t I2cComIF::sendMessage(CookieIF *cookie,
|
||||||
const uint8_t *sendData, size_t sendLen) {
|
const uint8_t *sendData, size_t sendLen) {
|
||||||
|
|
||||||
ReturnValue_t result;
|
ReturnValue_t result;
|
||||||
int fd;
|
int fd;
|
||||||
std::string deviceFile;
|
std::string deviceFile;
|
||||||
|
|
||||||
if(sendData == nullptr) {
|
if(sendData == nullptr) {
|
||||||
sif::error << "I2cComIF::sendMessage: Send Data is nullptr"
|
sif::error << "I2cComIF::sendMessage: Send Data is nullptr"
|
||||||
<< std::endl;
|
<< std::endl;
|
||||||
return HasReturnvaluesIF::RETURN_FAILED;
|
return HasReturnvaluesIF::RETURN_FAILED;
|
||||||
}
|
}
|
||||||
|
|
||||||
if(sendLen == 0) {
|
if(sendLen == 0) {
|
||||||
return HasReturnvaluesIF::RETURN_OK;
|
return HasReturnvaluesIF::RETURN_OK;
|
||||||
}
|
}
|
||||||
|
|
||||||
I2cCookie* i2cCookie = dynamic_cast<I2cCookie*>(cookie);
|
I2cCookie* i2cCookie = dynamic_cast<I2cCookie*>(cookie);
|
||||||
if(i2cCookie == nullptr) {
|
if(i2cCookie == nullptr) {
|
||||||
sif::error << "I2cComIF::sendMessasge: Invalid I2C Cookie!"
|
sif::error << "I2cComIF::sendMessasge: Invalid I2C Cookie!"
|
||||||
<< std::endl;
|
<< std::endl;
|
||||||
return NULLPOINTER;
|
return NULLPOINTER;
|
||||||
}
|
}
|
||||||
|
|
||||||
address_t i2cAddress = i2cCookie->getAddress();
|
address_t i2cAddress = i2cCookie->getAddress();
|
||||||
i2cDeviceMapIter = i2cDeviceMap.find(i2cAddress);
|
i2cDeviceMapIter = i2cDeviceMap.find(i2cAddress);
|
||||||
if (i2cDeviceMapIter == i2cDeviceMap.end()) {
|
if (i2cDeviceMapIter == i2cDeviceMap.end()) {
|
||||||
sif::error << "I2cComIF::sendMessage: i2cAddress of Cookie not "
|
sif::error << "I2cComIF::sendMessage: i2cAddress of Cookie not "
|
||||||
<< "registered in i2cDeviceMap" << std::endl;
|
<< "registered in i2cDeviceMap" << std::endl;
|
||||||
return HasReturnvaluesIF::RETURN_FAILED;
|
return HasReturnvaluesIF::RETURN_FAILED;
|
||||||
}
|
}
|
||||||
|
|
||||||
deviceFile = i2cCookie->getDeviceFile();
|
deviceFile = i2cCookie->getDeviceFile();
|
||||||
result = openDevice(deviceFile, i2cAddress, &fd);
|
result = openDevice(deviceFile, i2cAddress, &fd);
|
||||||
if (result != HasReturnvaluesIF::RETURN_OK){
|
if (result != HasReturnvaluesIF::RETURN_OK){
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (write(fd, sendData, sendLen) != (int)sendLen) {
|
if (write(fd, sendData, sendLen) != (int)sendLen) {
|
||||||
sif::error << "I2cComIF::sendMessage: Failed to send data to I2C "
|
sif::error << "I2cComIF::sendMessage: Failed to send data to I2C "
|
||||||
"device with error code " << errno << ". Error description: "
|
"device with error code " << errno << ". Error description: "
|
||||||
<< strerror(errno) << std::endl;
|
<< strerror(errno) << std::endl;
|
||||||
close(fd);
|
close(fd);
|
||||||
return HasReturnvaluesIF::RETURN_FAILED;
|
return HasReturnvaluesIF::RETURN_FAILED;
|
||||||
}
|
}
|
||||||
close(fd);
|
close(fd);
|
||||||
return HasReturnvaluesIF::RETURN_OK;
|
return HasReturnvaluesIF::RETURN_OK;
|
||||||
}
|
}
|
||||||
|
|
||||||
ReturnValue_t I2cComIF::getSendSuccess(CookieIF *cookie) {
|
ReturnValue_t I2cComIF::getSendSuccess(CookieIF *cookie) {
|
||||||
return HasReturnvaluesIF::RETURN_OK;
|
return HasReturnvaluesIF::RETURN_OK;
|
||||||
}
|
}
|
||||||
|
|
||||||
ReturnValue_t I2cComIF::requestReceiveMessage(CookieIF *cookie,
|
ReturnValue_t I2cComIF::requestReceiveMessage(CookieIF *cookie,
|
||||||
size_t requestLen) {
|
size_t requestLen) {
|
||||||
|
|
||||||
ReturnValue_t result;
|
ReturnValue_t result;
|
||||||
int fd;
|
int fd;
|
||||||
std::string deviceFile;
|
std::string deviceFile;
|
||||||
|
|
||||||
if (requestLen == 0) {
|
if (requestLen == 0) {
|
||||||
return HasReturnvaluesIF::RETURN_OK;
|
return HasReturnvaluesIF::RETURN_OK;
|
||||||
}
|
}
|
||||||
|
|
||||||
I2cCookie* i2cCookie = dynamic_cast<I2cCookie*>(cookie);
|
I2cCookie* i2cCookie = dynamic_cast<I2cCookie*>(cookie);
|
||||||
if(i2cCookie == nullptr) {
|
if(i2cCookie == nullptr) {
|
||||||
sif::error << "I2cComIF::requestReceiveMessage: Invalid I2C Cookie!"
|
sif::error << "I2cComIF::requestReceiveMessage: Invalid I2C Cookie!"
|
||||||
<< std::endl;
|
<< std::endl;
|
||||||
i2cDeviceMapIter->second.replyLen = 0;
|
i2cDeviceMapIter->second.replyLen = 0;
|
||||||
return NULLPOINTER;
|
return NULLPOINTER;
|
||||||
}
|
}
|
||||||
|
|
||||||
address_t i2cAddress = i2cCookie->getAddress();
|
address_t i2cAddress = i2cCookie->getAddress();
|
||||||
i2cDeviceMapIter = i2cDeviceMap.find(i2cAddress);
|
i2cDeviceMapIter = i2cDeviceMap.find(i2cAddress);
|
||||||
if (i2cDeviceMapIter == i2cDeviceMap.end()) {
|
if (i2cDeviceMapIter == i2cDeviceMap.end()) {
|
||||||
sif::error << "I2cComIF::requestReceiveMessage: i2cAddress of Cookie not "
|
sif::error << "I2cComIF::requestReceiveMessage: i2cAddress of Cookie not "
|
||||||
<< "registered in i2cDeviceMap" << std::endl;
|
<< "registered in i2cDeviceMap" << std::endl;
|
||||||
i2cDeviceMapIter->second.replyLen = 0;
|
i2cDeviceMapIter->second.replyLen = 0;
|
||||||
return HasReturnvaluesIF::RETURN_FAILED;
|
return HasReturnvaluesIF::RETURN_FAILED;
|
||||||
}
|
}
|
||||||
|
|
||||||
deviceFile = i2cCookie->getDeviceFile();
|
deviceFile = i2cCookie->getDeviceFile();
|
||||||
result = openDevice(deviceFile, i2cAddress, &fd);
|
result = openDevice(deviceFile, i2cAddress, &fd);
|
||||||
if (result != HasReturnvaluesIF::RETURN_OK){
|
if (result != HasReturnvaluesIF::RETURN_OK){
|
||||||
i2cDeviceMapIter->second.replyLen = 0;
|
i2cDeviceMapIter->second.replyLen = 0;
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
uint8_t* replyBuffer = i2cDeviceMapIter->second.replyBuffer.data();
|
uint8_t* replyBuffer = i2cDeviceMapIter->second.replyBuffer.data();
|
||||||
|
|
||||||
if (read(fd, replyBuffer, requestLen) != (int)requestLen) {
|
if (read(fd, replyBuffer, requestLen) != (int)requestLen) {
|
||||||
sif::error << "I2cComIF::requestReceiveMessage: Reading from I2C "
|
sif::error << "I2cComIF::requestReceiveMessage: Reading from I2C "
|
||||||
<< "device failed with error code " << errno <<". Description"
|
<< "device failed with error code " << errno <<". Description"
|
||||||
<< " of error: " << strerror(errno) << std::endl;
|
<< " of error: " << strerror(errno) << std::endl;
|
||||||
close(fd);
|
close(fd);
|
||||||
i2cDeviceMapIter->second.replyLen = 0;
|
i2cDeviceMapIter->second.replyLen = 0;
|
||||||
return HasReturnvaluesIF::RETURN_FAILED;
|
return HasReturnvaluesIF::RETURN_FAILED;
|
||||||
}
|
}
|
||||||
|
|
||||||
i2cDeviceMapIter->second.replyLen = requestLen;
|
i2cDeviceMapIter->second.replyLen = requestLen;
|
||||||
|
|
||||||
close(fd);
|
close(fd);
|
||||||
|
|
||||||
return HasReturnvaluesIF::RETURN_OK;
|
return HasReturnvaluesIF::RETURN_OK;
|
||||||
}
|
}
|
||||||
|
|
||||||
ReturnValue_t I2cComIF::readReceivedMessage(CookieIF *cookie,
|
ReturnValue_t I2cComIF::readReceivedMessage(CookieIF *cookie,
|
||||||
uint8_t **buffer, size_t* size) {
|
uint8_t **buffer, size_t* size) {
|
||||||
I2cCookie* i2cCookie = dynamic_cast<I2cCookie*>(cookie);
|
I2cCookie* i2cCookie = dynamic_cast<I2cCookie*>(cookie);
|
||||||
if(i2cCookie == nullptr) {
|
if(i2cCookie == nullptr) {
|
||||||
sif::error << "I2cComIF::readReceivedMessage: Invalid I2C Cookie!"
|
sif::error << "I2cComIF::readReceivedMessage: Invalid I2C Cookie!"
|
||||||
<< std::endl;
|
<< std::endl;
|
||||||
return NULLPOINTER;
|
return NULLPOINTER;
|
||||||
}
|
}
|
||||||
|
|
||||||
address_t i2cAddress = i2cCookie->getAddress();
|
address_t i2cAddress = i2cCookie->getAddress();
|
||||||
i2cDeviceMapIter = i2cDeviceMap.find(i2cAddress);
|
i2cDeviceMapIter = i2cDeviceMap.find(i2cAddress);
|
||||||
if (i2cDeviceMapIter == i2cDeviceMap.end()) {
|
if (i2cDeviceMapIter == i2cDeviceMap.end()) {
|
||||||
sif::error << "I2cComIF::readReceivedMessage: i2cAddress of Cookie not "
|
sif::error << "I2cComIF::readReceivedMessage: i2cAddress of Cookie not "
|
||||||
<< "found in i2cDeviceMap" << std::endl;
|
<< "found in i2cDeviceMap" << std::endl;
|
||||||
return HasReturnvaluesIF::RETURN_FAILED;
|
return HasReturnvaluesIF::RETURN_FAILED;
|
||||||
}
|
}
|
||||||
*buffer = i2cDeviceMapIter->second.replyBuffer.data();
|
*buffer = i2cDeviceMapIter->second.replyBuffer.data();
|
||||||
*size = i2cDeviceMapIter->second.replyLen;
|
*size = i2cDeviceMapIter->second.replyLen;
|
||||||
|
|
||||||
return HasReturnvaluesIF::RETURN_OK;
|
return HasReturnvaluesIF::RETURN_OK;
|
||||||
}
|
}
|
||||||
|
|
||||||
ReturnValue_t I2cComIF::openDevice(std::string deviceFile,
|
ReturnValue_t I2cComIF::openDevice(std::string deviceFile,
|
||||||
address_t i2cAddress, int* fileDescriptor) {
|
address_t i2cAddress, int* fileDescriptor) {
|
||||||
*fileDescriptor = open(deviceFile.c_str(), O_RDWR);
|
*fileDescriptor = open(deviceFile.c_str(), O_RDWR);
|
||||||
if (*fileDescriptor < 0) {
|
if (*fileDescriptor < 0) {
|
||||||
sif::error << "I2cComIF: Opening i2c device failed with error code "
|
sif::error << "I2cComIF: Opening i2c device failed with error code "
|
||||||
<< errno << ". Error description: " << strerror(errno)
|
<< errno << ". Error description: " << strerror(errno)
|
||||||
<< std::endl;
|
<< std::endl;
|
||||||
return HasReturnvaluesIF::RETURN_FAILED;
|
return HasReturnvaluesIF::RETURN_FAILED;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (ioctl(*fileDescriptor, I2C_SLAVE, i2cAddress) < 0) {
|
if (ioctl(*fileDescriptor, I2C_SLAVE, i2cAddress) < 0) {
|
||||||
sif::error << "I2cComIF: Specifying target device failed with error "
|
sif::error << "I2cComIF: Specifying target device failed with error "
|
||||||
<< "code " << errno << ". Error description "
|
<< "code " << errno << ". Error description "
|
||||||
<< strerror(errno) << std::endl;
|
<< strerror(errno) << std::endl;
|
||||||
return HasReturnvaluesIF::RETURN_FAILED;
|
return HasReturnvaluesIF::RETURN_FAILED;
|
||||||
}
|
}
|
||||||
return HasReturnvaluesIF::RETURN_OK;
|
return HasReturnvaluesIF::RETURN_OK;
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user