I2c Implementation is working, but error management is not clear, must chekc writeBuffer & readBuffer functions
parent
066aac8f6d
commit
d4f74ab927
@ -0,0 +1,40 @@
|
|||||||
|
#ifndef _I2C_H_
|
||||||
|
#define _I2C_H_
|
||||||
|
|
||||||
|
#include <unistd.h>
|
||||||
|
#include <stdint.h>
|
||||||
|
#include <memory>
|
||||||
|
#include <ostream>
|
||||||
|
#include <iostream>
|
||||||
|
#include "../../config.h"
|
||||||
|
|
||||||
|
class I2C
|
||||||
|
{
|
||||||
|
public:
|
||||||
|
enum errors
|
||||||
|
{
|
||||||
|
noError,
|
||||||
|
writeFailed,
|
||||||
|
readFailed,
|
||||||
|
bufferFull,
|
||||||
|
fasleAddrs,
|
||||||
|
initFailed
|
||||||
|
};
|
||||||
|
|
||||||
|
I2C();
|
||||||
|
~I2C();
|
||||||
|
uint8_t readByte(const uint8_t& address, const uint8_t& reg);
|
||||||
|
uint16_t readWord(const uint8_t& address, const uint8_t& reg);
|
||||||
|
uint8_t writeByte(const uint8_t& address, const uint8_t& data);
|
||||||
|
uint8_t writeWord(const uint8_t& address, const uint8_t& reg, const uint8_t& data);
|
||||||
|
uint8_t writeBuffer(const uint8_t& address, const uint8_t& buffer, uint8_t len);
|
||||||
|
uint8_t readBuffer(const uint8_t& address, const uint8_t& buffer, uint8_t len);
|
||||||
|
int8_t getError();
|
||||||
|
|
||||||
|
private:
|
||||||
|
struct i2cImpl;
|
||||||
|
std::unique_ptr<i2cImpl> i2cPimpl;
|
||||||
|
|
||||||
|
};
|
||||||
|
#endif // _I2C_H_
|
||||||
|
#define _I2C_H_
|
@ -0,0 +1,52 @@
|
|||||||
|
#include "i2c.hpp"
|
||||||
|
|
||||||
|
/*
|
||||||
|
#if PLATFORM == LINUX
|
||||||
|
#include "i2c_linux.hpp"
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#if PLATFORM == LINUX
|
||||||
|
#include "i2c_stm.hpp"
|
||||||
|
#endif
|
||||||
|
*/
|
||||||
|
|
||||||
|
#include "i2c_linux.hpp"
|
||||||
|
|
||||||
|
I2C::I2C():i2cPimpl(new i2cImpl()){}
|
||||||
|
|
||||||
|
I2C::~I2C(){}
|
||||||
|
|
||||||
|
uint8_t I2C::readByte(const uint8_t& address, const uint8_t& reg)
|
||||||
|
{
|
||||||
|
return i2cPimpl->readByte(address,reg);
|
||||||
|
}
|
||||||
|
|
||||||
|
uint16_t I2C::readWord(const uint8_t& address, const uint8_t& reg)
|
||||||
|
{
|
||||||
|
return i2cPimpl->readWord(address,reg);
|
||||||
|
}
|
||||||
|
|
||||||
|
uint8_t I2C::writeByte(const uint8_t& address, const uint8_t& data)
|
||||||
|
{
|
||||||
|
return i2cPimpl->writeByte(address,data);
|
||||||
|
}
|
||||||
|
|
||||||
|
uint8_t I2C::writeWord(const uint8_t& address, const uint8_t& reg, const uint8_t& data)
|
||||||
|
{
|
||||||
|
return i2cPimpl->writeWord(address,reg,data);
|
||||||
|
}
|
||||||
|
|
||||||
|
uint8_t I2C::writeBuffer(const uint8_t& address, const uint8_t& buffer, uint8_t len)
|
||||||
|
{
|
||||||
|
return i2cPimpl->writeBuffer(address,buffer,len);
|
||||||
|
}
|
||||||
|
|
||||||
|
uint8_t I2C::readBuffer(const uint8_t& address, const uint8_t& buffer, uint8_t len)
|
||||||
|
{
|
||||||
|
return i2cPimpl->readBuffer(address,buffer,len);
|
||||||
|
}
|
||||||
|
|
||||||
|
int8_t I2C::getError()
|
||||||
|
{
|
||||||
|
return i2cPimpl->getError();
|
||||||
|
}
|
@ -0,0 +1,40 @@
|
|||||||
|
#ifndef _I2C_H_
|
||||||
|
#define _I2C_H_
|
||||||
|
|
||||||
|
#include <unistd.h>
|
||||||
|
#include <stdint.h>
|
||||||
|
#include <memory>
|
||||||
|
#include <ostream>
|
||||||
|
#include <iostream>
|
||||||
|
#include "../../config.h"
|
||||||
|
|
||||||
|
class I2C
|
||||||
|
{
|
||||||
|
public:
|
||||||
|
enum errors
|
||||||
|
{
|
||||||
|
noError,
|
||||||
|
writeFailed,
|
||||||
|
readFailed,
|
||||||
|
bufferFull,
|
||||||
|
falseAddrs,
|
||||||
|
initFailed
|
||||||
|
};
|
||||||
|
|
||||||
|
I2C();
|
||||||
|
~I2C();
|
||||||
|
uint8_t readByte(const uint8_t& address, const uint8_t& reg);
|
||||||
|
uint16_t readWord(const uint8_t& address, const uint8_t& reg);
|
||||||
|
uint8_t writeByte(const uint8_t& address, const uint8_t& data);
|
||||||
|
uint8_t writeWord(const uint8_t& address, const uint8_t& reg, const uint8_t& data);
|
||||||
|
uint8_t writeBuffer(const uint8_t& address, const uint8_t& buffer, uint8_t len);
|
||||||
|
uint8_t readBuffer(const uint8_t& address, const uint8_t& buffer, uint8_t len);
|
||||||
|
int8_t getError();
|
||||||
|
|
||||||
|
private:
|
||||||
|
struct i2cImpl;
|
||||||
|
std::unique_ptr<i2cImpl> i2cPimpl;
|
||||||
|
|
||||||
|
};
|
||||||
|
#endif // _I2C_H_
|
||||||
|
#define _I2C_H_
|
@ -0,0 +1,126 @@
|
|||||||
|
#ifndef _I2C_LINUX_H_
|
||||||
|
#define _I2C_LINUX_H_
|
||||||
|
#include "i2c.hpp"
|
||||||
|
|
||||||
|
#include <linux/i2c-dev.h>
|
||||||
|
#include <fcntl.h>
|
||||||
|
#include <sys/ioctl.h>
|
||||||
|
#include <sys/types.h>
|
||||||
|
#include <sys/stat.h>
|
||||||
|
|
||||||
|
struct I2C::i2cImpl
|
||||||
|
{
|
||||||
|
int16_t deviceDescriptor;
|
||||||
|
uint8_t device_address;
|
||||||
|
uint8_t send_buffer[32];
|
||||||
|
uint8_t recieve_buffer[32];
|
||||||
|
uint8_t blocks;
|
||||||
|
uint8_t channel;
|
||||||
|
uint8_t mode;
|
||||||
|
int8_t error;
|
||||||
|
|
||||||
|
i2cImpl()
|
||||||
|
{
|
||||||
|
char filename[20];
|
||||||
|
errors currentError = noError;
|
||||||
|
snprintf(filename, 19, "/dev/i2c-%d", 1);
|
||||||
|
|
||||||
|
deviceDescriptor = open(filename, O_RDWR);
|
||||||
|
if (deviceDescriptor < 0) {
|
||||||
|
error = initFailed;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
uint8_t readByte(const uint8_t& address, const uint8_t& reg)
|
||||||
|
{
|
||||||
|
if (ioctl(deviceDescriptor, I2C_SLAVE, address) < 0)
|
||||||
|
{
|
||||||
|
error = falseAddrs;
|
||||||
|
}
|
||||||
|
|
||||||
|
writeByte(address,reg); //Initiate a write to indicate the desired register to read
|
||||||
|
|
||||||
|
if (read(deviceDescriptor, recieve_buffer, 1) != 1) // An then initare a read request of 1 byte
|
||||||
|
{
|
||||||
|
error = readFailed;
|
||||||
|
}
|
||||||
|
return recieve_buffer[0] ;
|
||||||
|
}
|
||||||
|
|
||||||
|
uint16_t readWord(const uint8_t& address, const uint8_t& reg)
|
||||||
|
{
|
||||||
|
uint16_t result = 0 ;
|
||||||
|
if (ioctl(deviceDescriptor, I2C_SLAVE, address) < 0)
|
||||||
|
{
|
||||||
|
error = falseAddrs;
|
||||||
|
}
|
||||||
|
|
||||||
|
writeByte(address,reg); //Initiate a write to indicate the desired register to read
|
||||||
|
|
||||||
|
if (read(deviceDescriptor, recieve_buffer, 2) != 2) // An then initare a read request of 2 bytes
|
||||||
|
{
|
||||||
|
error = readFailed;
|
||||||
|
}
|
||||||
|
result = (recieve_buffer[0] << 8) + recieve_buffer[1] ;
|
||||||
|
return result ;
|
||||||
|
}
|
||||||
|
|
||||||
|
uint8_t writeByte(const uint8_t& address, const uint8_t& data)
|
||||||
|
{
|
||||||
|
if (ioctl(deviceDescriptor, I2C_SLAVE, address) < 0)
|
||||||
|
{
|
||||||
|
error = falseAddrs;
|
||||||
|
}
|
||||||
|
|
||||||
|
send_buffer[0] = data;
|
||||||
|
|
||||||
|
if ((write(deviceDescriptor, send_buffer, 1)) != 1)
|
||||||
|
{
|
||||||
|
error = writeFailed;
|
||||||
|
}
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
uint8_t writeWord(const uint8_t& address, const uint8_t& reg, const uint8_t& data)
|
||||||
|
{
|
||||||
|
if (ioctl(deviceDescriptor, I2C_SLAVE, address) < 0)
|
||||||
|
{
|
||||||
|
error = falseAddrs;
|
||||||
|
}
|
||||||
|
|
||||||
|
send_buffer[0] = reg;
|
||||||
|
send_buffer[1] = data;
|
||||||
|
|
||||||
|
if ((write(deviceDescriptor, send_buffer, 2)) != 2)
|
||||||
|
{
|
||||||
|
error = writeFailed;
|
||||||
|
}
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
uint8_t writeBuffer(const uint8_t& address, const uint8_t& buffer, uint8_t len)
|
||||||
|
{
|
||||||
|
if (ioctl(deviceDescriptor, I2C_SLAVE, address) < 0)
|
||||||
|
{
|
||||||
|
error = falseAddrs;
|
||||||
|
}
|
||||||
|
|
||||||
|
send_buffer[0] = buffer;
|
||||||
|
|
||||||
|
if ((write(deviceDescriptor, send_buffer, len)) != len)
|
||||||
|
{
|
||||||
|
error = writeFailed;
|
||||||
|
}
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
uint8_t readBuffer(const uint8_t& address, const uint8_t& buffer, uint8_t len)
|
||||||
|
{
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
int8_t getError()
|
||||||
|
{
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
};
|
||||||
|
#endif // _I2C_LINUX_H_
|
Binary file not shown.
Loading…
Reference in new issue