tmp1075handler and I2cComIF complete
This commit is contained in:
@ -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<uint8_t>(maxReplyLen), 0};
|
||||
i2cDeviceMap.emplace(i2cAddress, i2cInstance);
|
||||
I2cInstance_t i2cInstance = {std::vector<uint8_t>(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;
|
||||
}
|
||||
|
@ -32,7 +32,6 @@ public:
|
||||
private:
|
||||
|
||||
typedef struct I2cInstance {
|
||||
int fileDescriptor;
|
||||
std::vector<uint8_t> 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_ */
|
||||
|
Reference in New Issue
Block a user