parent
805597a71b
commit
a504c85e79
@ -0,0 +1,96 @@
|
||||
#ifndef _I2C_H_
|
||||
#define _I2C_H_
|
||||
|
||||
namespace serial
|
||||
{
|
||||
enum i2c_id
|
||||
{
|
||||
i2c_ch0,
|
||||
i2c_ch1
|
||||
};
|
||||
|
||||
//
|
||||
// Base template class
|
||||
//
|
||||
|
||||
template<i2c_id id>
|
||||
class I2C
|
||||
{
|
||||
public:
|
||||
I2C(const uint8_t& mode); // Mode : Master or Slave
|
||||
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 uint16_t& data);
|
||||
|
||||
private:
|
||||
};
|
||||
|
||||
//
|
||||
// Template for channel 0
|
||||
//
|
||||
template<>
|
||||
class I2C <i2c_ch0>
|
||||
{
|
||||
public:
|
||||
I2C(const uint8_t& mode):pImpL(mode)
|
||||
{
|
||||
}
|
||||
uint8_t readByte(const uint8_t& address, const uint8_t& reg)
|
||||
{
|
||||
return pImpL -> readByte(address, reg);
|
||||
}
|
||||
uint16_t readWord(const uint8_t& address, const uint8_t& reg)
|
||||
{
|
||||
return pImpL -> readWord(address,reg);
|
||||
}
|
||||
uint8_t writeByte(const uint8_t& address, const uint8_t& data)
|
||||
{
|
||||
return pImpL -> writeByte(address, data);
|
||||
}
|
||||
uint8_t writeWord(const uint8_t& address, const uint8_t& reg, const uint16_t& data)
|
||||
{
|
||||
return pImpL -> writeWord(address, reg, data);
|
||||
}
|
||||
|
||||
private:
|
||||
class i2c_ch0_pImpL;
|
||||
std::unique_ptr<i2c_ch0_pImpL> pImpL;
|
||||
};
|
||||
|
||||
//
|
||||
// Template for channel 1
|
||||
//
|
||||
template<>
|
||||
class I2C <i2c_ch1>
|
||||
{
|
||||
public:
|
||||
I2C(const uint8_t& mode):pImpL(mode)
|
||||
{
|
||||
}
|
||||
uint8_t readByte(const uint8_t& address, const uint8_t& reg)
|
||||
{
|
||||
return pImpL -> readByte(address, reg);
|
||||
}
|
||||
uint16_t readWord(const uint8_t& address, const uint8_t& reg)
|
||||
{
|
||||
return pImpL -> readWord(address,reg);
|
||||
}
|
||||
uint8_t writeByte(const uint8_t& address, const uint8_t& data)
|
||||
{
|
||||
return pImpL -> writeByte(address, data);
|
||||
}
|
||||
uint8_t writeWord(const uint8_t& address, const uint8_t& reg, const uint16_t& data)
|
||||
{
|
||||
return pImpL -> writeWord(address, reg, data);
|
||||
}
|
||||
|
||||
private:
|
||||
class i2c_ch1_pImpL;
|
||||
std::unique_ptr<i2c_ch1_pImpL> pImpL;
|
||||
};
|
||||
|
||||
}// Namespace serial
|
||||
|
||||
|
||||
#endif // _I2C_H_
|
@ -0,0 +1,107 @@
|
||||
#include "i2c_ch0_pImpL.h"
|
||||
#include "systemCall.h"
|
||||
|
||||
#include <cstdio>
|
||||
#include <ostream>
|
||||
#include <iostream>
|
||||
#include <linux/i2c-dev.h>
|
||||
#include <fcntl.h>
|
||||
#include <unistd.h>
|
||||
#include <sys/ioctl.h>
|
||||
#include <sys/types.h>
|
||||
#include <sys/stat.h>
|
||||
|
||||
|
||||
#define PORT_I2C "/dev/i2c-1"
|
||||
|
||||
// some curious things https://www.john.geek.nz/2012/12/update-reading-data-from-a-bosch-bmp085-with-a-raspberry-pi/
|
||||
|
||||
|
||||
i2c_ch0_pImpL::i2c_ch0_pImpL(const uint8_t& mode)
|
||||
{
|
||||
char filename[20];
|
||||
snprintf(filename, 19, "/dev/i2c-%d", 0);
|
||||
|
||||
deviceDescriptor = open(filename, O_RDWR);
|
||||
if (deviceDescriptor < 0) {
|
||||
std::cout << "unable to open : "<< deviceDescriptor << " ! quiting" << std::endl;
|
||||
exit(1);
|
||||
}
|
||||
}
|
||||
|
||||
uint8_t i2c_ch0_pImpL::writeByte(const uint8_t& address, const uint8_t& data) // retuns 0 when a sucsessful transation ocures
|
||||
{
|
||||
if (ioctl(deviceDescriptor, I2C_SLAVE, address) < 0)
|
||||
{
|
||||
std::cout << "Unable to reach device : "<< address << " ! quiting" << std::endl;
|
||||
exit(1);
|
||||
}
|
||||
|
||||
send_buffer[0] = data;
|
||||
|
||||
if ((write(deviceDescriptor, send_buffer, 1)) != 1)
|
||||
{
|
||||
std::cout << "Unable to write quiting" << std::endl;
|
||||
exit(0);
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
uint8_t i2c_ch0_pImpL::readByte(const uint8_t& address, const uint8_t& reg)
|
||||
{
|
||||
if (ioctl(deviceDescriptor, I2C_SLAVE, address) < 0)
|
||||
{
|
||||
std::cout << "Unable to reach device : "<< address << " ! quiting" << std::endl;
|
||||
exit(1);
|
||||
}
|
||||
|
||||
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
|
||||
{
|
||||
std::cout << "Unable to read quiting" << std::endl;
|
||||
exit(1);
|
||||
}
|
||||
return recieve_buffer[0] ;
|
||||
}
|
||||
|
||||
|
||||
uint8_t i2c_ch0_pImpL::writeWord(const uint8_t& address, const uint8_t& reg, const uint16_t& data) // retuns 0 when a sucsessful transation ocures
|
||||
{
|
||||
|
||||
if (ioctl(deviceDescriptor, I2C_SLAVE, address) < 0)
|
||||
{
|
||||
std::cout << "Unable to reach device : "<< address << " ! quiting" << std::endl;
|
||||
exit(1);
|
||||
}
|
||||
|
||||
send_buffer[0] = reg;
|
||||
send_buffer[1] = data;
|
||||
|
||||
if ((write(deviceDescriptor, send_buffer, 2)) != 2)
|
||||
{
|
||||
std::cout << "Unable to write quiting" << std::endl;
|
||||
exit(0);
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
uint16_t i2c_ch0_pImpL::readWord(const uint8_t& address, const uint8_t& reg)
|
||||
{
|
||||
uint16_t result = 0 ;
|
||||
if (ioctl(deviceDescriptor, I2C_SLAVE, address) < 0)
|
||||
{
|
||||
std::cout << "Unable to reach device : "<< address << " ! quiting" << std::endl;
|
||||
exit(1);
|
||||
}
|
||||
|
||||
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
|
||||
{
|
||||
std::cout << "Unable to read quiting" << std::endl;
|
||||
exit(1);
|
||||
}
|
||||
result = (recieve_buffer[0] << 8) + recieve_buffer[1] ;
|
||||
return result ;
|
||||
}
|
@ -0,0 +1,31 @@
|
||||
#ifndef _I2C_CH0_PIMPL_H_
|
||||
#define _I2C_CH0_PIMPL_H_
|
||||
|
||||
#include <stdint.h>
|
||||
#include <iostream>
|
||||
#include <string>
|
||||
#include "errorHandling.h"
|
||||
|
||||
class i2c_ch0_pImpL
|
||||
{
|
||||
public:
|
||||
i2c_ch0_pImpL(const uint8_t& mode); // Mode : Master or Slave
|
||||
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 uint16_t& data);
|
||||
|
||||
|
||||
private:
|
||||
int16_t deviceDescriptor;
|
||||
uint8_t device_address;
|
||||
uint8_t device_reg;
|
||||
uint8_t send_buffer[32];
|
||||
uint8_t recieve_buffer[32];
|
||||
uint8_t blocks;
|
||||
uint8_t channel;
|
||||
uint8_t mode;
|
||||
};
|
||||
|
||||
|
||||
#endif // _I2C_CH0_PIMPL_H_
|
@ -0,0 +1,107 @@
|
||||
#include "i2c_ch1_pImpL.hpp"
|
||||
#include "systemCall.h"
|
||||
|
||||
#include <cstdio>
|
||||
#include <ostream>
|
||||
#include <iostream>
|
||||
#include <linux/i2c-dev.h>
|
||||
#include <fcntl.h>
|
||||
#include <unistd.h>
|
||||
#include <sys/ioctl.h>
|
||||
#include <sys/types.h>
|
||||
#include <sys/stat.h>
|
||||
|
||||
|
||||
#define PORT_I2C "/dev/i2c-1"
|
||||
|
||||
// some curious things https://www.john.geek.nz/2012/12/update-reading-data-from-a-bosch-bmp085-with-a-raspberry-pi/
|
||||
|
||||
|
||||
i2c_ch1_pImpL::i2c_ch1_pImpL(const uint8_t& mode)
|
||||
{
|
||||
char filename[20];
|
||||
snprintf(filename, 19, "/dev/i2c-%d", 1);
|
||||
|
||||
deviceDescriptor = open(filename, O_RDWR);
|
||||
if (deviceDescriptor < 0) {
|
||||
std::cout << "unable to open : "<< deviceDescriptor << " ! quiting" << std::endl;
|
||||
exit(1);
|
||||
}
|
||||
}
|
||||
|
||||
uint8_t i2c_ch1_pImpL::writeByte(const uint8_t& address, const uint8_t& data) // retuns 0 when a sucsessful transation ocures
|
||||
{
|
||||
if (ioctl(deviceDescriptor, I2C_SLAVE, address) < 0)
|
||||
{
|
||||
std::cout << "Unable to reach device : "<< address << " ! quiting" << std::endl;
|
||||
exit(1);
|
||||
}
|
||||
|
||||
send_buffer[0] = data;
|
||||
|
||||
if ((write(deviceDescriptor, send_buffer, 1)) != 1)
|
||||
{
|
||||
std::cout << "Unable to write quiting" << std::endl;
|
||||
exit(0);
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
uint8_t i2c_ch1_pImpL::readByte(const uint8_t& address, const uint8_t& reg)
|
||||
{
|
||||
if (ioctl(deviceDescriptor, I2C_SLAVE, address) < 0)
|
||||
{
|
||||
std::cout << "Unable to reach device : "<< address << " ! quiting" << std::endl;
|
||||
exit(1);
|
||||
}
|
||||
|
||||
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
|
||||
{
|
||||
std::cout << "Unable to read quiting" << std::endl;
|
||||
exit(1);
|
||||
}
|
||||
return recieve_buffer[0] ;
|
||||
}
|
||||
|
||||
|
||||
uint8_t i2c_ch1_pImpL::writeWord(const uint8_t& address, const uint8_t& reg, const uint16_t& data) // retuns 0 when a sucsessful transation ocures
|
||||
{
|
||||
|
||||
if (ioctl(deviceDescriptor, I2C_SLAVE, address) < 0)
|
||||
{
|
||||
std::cout << "Unable to reach device : "<< address << " ! quiting" << std::endl;
|
||||
exit(1);
|
||||
}
|
||||
|
||||
send_buffer[0] = reg;
|
||||
send_buffer[1] = data;
|
||||
|
||||
if ((write(deviceDescriptor, send_buffer, 2)) != 2)
|
||||
{
|
||||
std::cout << "Unable to write quiting" << std::endl;
|
||||
exit(0);
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
uint16_t i2c_ch1_pImpL::readWord(const uint8_t& address, const uint8_t& reg)
|
||||
{
|
||||
uint16_t result = 0 ;
|
||||
if (ioctl(deviceDescriptor, I2C_SLAVE, address) < 0)
|
||||
{
|
||||
std::cout << "Unable to reach device : "<< address << " ! quiting" << std::endl;
|
||||
exit(1);
|
||||
}
|
||||
|
||||
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
|
||||
{
|
||||
std::cout << "Unable to read quiting" << std::endl;
|
||||
exit(1);
|
||||
}
|
||||
result = (recieve_buffer[0] << 8) + recieve_buffer[1] ;
|
||||
return result ;
|
||||
}
|
@ -0,0 +1,31 @@
|
||||
#ifndef _I2C_CH1_PIMPL_H_
|
||||
#define _I2C_CH1_PIMPL_H_
|
||||
|
||||
#include <stdint.h>
|
||||
#include <iostream>
|
||||
#include <string>
|
||||
#include "errorHandling.h"
|
||||
|
||||
class i2c_ch1_pImpL
|
||||
{
|
||||
public:
|
||||
i2c_ch1_pImpL(const uint8_t& mode); // Mode : Master or Slave
|
||||
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 uint16_t& data);
|
||||
|
||||
|
||||
private:
|
||||
int16_t deviceDescriptor;
|
||||
uint8_t device_address;
|
||||
uint8_t device_reg;
|
||||
uint8_t send_buffer[32];
|
||||
uint8_t recieve_buffer[32];
|
||||
uint8_t blocks;
|
||||
uint8_t channel;
|
||||
uint8_t mode;
|
||||
};
|
||||
|
||||
|
||||
#endif // _I2C_CH1_PIMPL_H_
|
Loading…
Reference in new issue