Skip to content

Commit

Permalink
Removed makefile and added updated I2C files
Browse files Browse the repository at this point in the history
  • Loading branch information
William Gerhard committed Jan 2, 2018
1 parent c5a78ad commit 09fa3f5
Show file tree
Hide file tree
Showing 3 changed files with 135 additions and 100 deletions.
181 changes: 121 additions & 60 deletions src/I2C.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,84 +26,145 @@
#include <stdio.h> /* Standard I/O functions */
#include <fcntl.h>
#include <unistd.h>
#include <linux/i2c.h>
#include <linux/i2c-dev.h>
#include <syslog.h> /* Syslog functionality */
#include "I2C.h"

I2C::I2C(int bus, int address) {
_i2cbus = bus;
_i2caddr = address;
snprintf(busfile, sizeof(busfile), "/dev/i2c-%d", bus);
openfd();
I2C::I2C(int bus, int address)
{
_i2cbus = bus;
_i2caddr = address;
snprintf(busfile, sizeof(busfile), "/dev/i2c-%d", bus);
openfd();
}

I2C::~I2C() {
close(fd);
I2C::~I2C()
{
close(fd);
}
//! Read a single byte from I2C Bus
/*!
\param address register address to read from
*/
uint8_t I2C::read_byte(uint8_t address) {
if (fd != -1) {
uint8_t buff[BUFFER_SIZE];
buff[0] = address;
if (write(fd, buff, BUFFER_SIZE) != BUFFER_SIZE) {
syslog(LOG_ERR,
"I2C slave 0x%x failed to go to register 0x%x [read_byte():write %d]",
_i2caddr, address, errno);
return (-1);
} else {
if (read(fd, dataBuffer, BUFFER_SIZE) != BUFFER_SIZE) {
syslog(LOG_ERR,
"Could not read from I2C slave 0x%x, register 0x%x [read_byte():read %d]",
_i2caddr, address, errno);
return (-1);
} else {
return dataBuffer[0];
}
}
} else {
syslog(LOG_ERR, "Device File not available. Aborting read");
return (-1);
}
uint8_t I2C::read_byte(uint8_t address)
{
if (fd != -1)
{
uint8_t buff[BUFFER_SIZE];
buff[0] = address;
if (write(fd, buff, BUFFER_SIZE) != BUFFER_SIZE)
{
syslog(LOG_ERR,
"I2C slave 0x%x failed to go to register 0x%x [read_byte():write %d]",
_i2caddr, address, errno);
return (-1);
}
else
{
if (read(fd, dataBuffer, BUFFER_SIZE) != BUFFER_SIZE)
{
syslog(LOG_ERR,
"Could not read from I2C slave 0x%x, register 0x%x [read_byte():read %d]",
_i2caddr, address, errno);
return (-1);
}
else
{
return dataBuffer[0];
}
}
}
else
{
syslog(LOG_ERR, "Device File not available. Aborting read");
return (-1);
}
}


uint8_t I2C::read_length(uint8_t address, uint8_t readlength, uint8_t* buffer)
{
if (fd != -1)
{
uint8_t buff[BUFFER_SIZE];
buff[0] = address;
if (write(fd, buff, BUFFER_SIZE) != BUFFER_SIZE)
{
syslog(LOG_ERR,
"I2C slave 0x%x failed to go to register 0x%x [read_byte():write %d]",
_i2caddr, address, errno);
return (-1);
}
else
{
if (read(fd, buffer, readlength) != readlength)
{
syslog(LOG_ERR,
"Could not read from I2C slave 0x%x, register 0x%x [read_byte():read %d]",
_i2caddr, address, errno);
return (-1);
}
else
{
return 1;
}
}
}
else
{
syslog(LOG_ERR, "Device File not available. Aborting read");
return (-1);
}



}
//! Write a single byte from a I2C Device
/*!
\param address register address to write to
\param data 8 bit data to write
*/
uint8_t I2C::write_byte(uint8_t address, uint8_t data) {
if (fd != -1) {
uint8_t buff[2];
buff[0] = address;
buff[1] = data;
if (write(fd, buff, sizeof(buff)) != 2) {
syslog(LOG_ERR,
"Failed to write to I2C Slave 0x%x @ register 0x%x [write_byte():write %d]",
_i2caddr, address, errno);
return (-1);
} else {
syslog(LOG_INFO, "Wrote to I2C Slave 0x%x @ register 0x%x [0x%x]",
_i2caddr, address, data);
return (-1);
}
} else {
syslog(LOG_INFO, "Device File not available. Aborting write");
return (-1);
}
return 0;
uint8_t I2C::write_byte(uint8_t address, uint8_t data)
{
if (fd != -1)
{
uint8_t buff[2];
buff[0] = address;
buff[1] = data;
if (write(fd, buff, sizeof(buff)) != 2)
{
syslog(LOG_ERR,
"Failed to write to I2C Slave 0x%x @ register 0x%x [write_byte():write %d]",
_i2caddr, address, errno);
return (-1);
}
else
{
syslog(LOG_INFO, "Wrote to I2C Slave 0x%x @ register 0x%x [0x%x]",
_i2caddr, address, data);
return (-1);
}
}
else
{
syslog(LOG_INFO, "Device File not available. Aborting write");
return (-1);
}
return 0;
}
//! Open device file for I2C Device
void I2C::openfd() {
if ((fd = open(busfile, O_RDWR)) < 0) {
syslog(LOG_ERR, "Couldn't open I2C Bus %d [openfd():open %d]", _i2cbus,
errno);
}
if (ioctl(fd, I2C_SLAVE, _i2caddr) < 0) {
syslog(LOG_ERR, "I2C slave %d failed [openfd():ioctl %d]", _i2caddr,
errno);
}
void I2C::openfd()
{
if ((fd = open(busfile, O_RDWR)) < 0)
{
syslog(LOG_ERR, "Couldn't open I2C Bus %d [openfd():open %d]", _i2cbus,
errno);
}
if (ioctl(fd, I2C_SLAVE, _i2caddr) < 0)
{
syslog(LOG_ERR, "I2C slave %d failed [openfd():ioctl %d]", _i2caddr,
errno);
}
}


25 changes: 14 additions & 11 deletions src/I2C.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,19 +28,22 @@
#define BUFFER_SIZE 0x01 //1 byte buffer


class I2C {
class I2C
{
public:
I2C(int, int);
virtual ~I2C();
uint8_t dataBuffer[BUFFER_SIZE];
uint8_t read_byte(uint8_t);
uint8_t write_byte(uint8_t, uint8_t);
I2C(int, int);
virtual ~I2C();
uint8_t dataBuffer[BUFFER_SIZE];
uint8_t read_byte(uint8_t);
uint8_t write_byte(uint8_t, uint8_t);
uint8_t read_length(uint8_t, uint8_t, uint8_t*);

private:
int _i2caddr;
int _i2cbus;
void openfd();
char busfile[64];
int fd;
int _i2caddr;
int _i2cbus;
void openfd();
char busfile[64];
int fd;
};

#endif /* I2C_H_ */
29 changes: 0 additions & 29 deletions src/Makefile

This file was deleted.

0 comments on commit 09fa3f5

Please sign in to comment.