first working i2c (only for read&write 8bits) for linux

master
Kerem Yollu 4 years ago
parent ae81a1f11d
commit 016f1ad4e6

@ -1,10 +1,10 @@
cpp_src = $(wildcard *.cpp)
cpp_src = $(wildcard *.cpp)
#c_src = $(wildcard ../c/algorithms/*.c) $(wildcard driver/ma120x0/*.c)
cpp_obj = $(cpp_src:.cpp=.o)
c_obj = $(c_src:.c=.o)
CC = g++
CFLAGS = -Wall -pedantic
CFLAGS = -Wall -pedantic -li2c
LDFLAGS =
EXEC = runtest

@ -1,16 +1,66 @@
#include "i2c_driver.h"
#include "systemCall.h"
I2C_Driver::I2C_Driver()
{
#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"
I2C_Driver::I2C_Driver(const uint8_t& channel, const uint8_t& mode)
{
char filename[20];
snprintf(filename, 19, "/dev/i2c-%d", channel);
deviceDescriptor = open(filename, O_RDWR);
if (deviceDescriptor < 0) {
std::cout << "unable to open : "<< deviceDescriptor << " ! quiting" << std::endl;
exit(1);
}
std::cout << "I2C initialized sucsessfully" << std::endl;
}
std::string I2C_Driver::readByte(const uint8_t& address, const uint8_t& reg)
uint8_t I2C_Driver::writeByte(const uint8_t& address, const uint8_t& data) // retuns 0 when a sucsessful transation ocures
{
std::string addrs = std::to_string(address);
std::string regs = std::to_string(reg);
std::string command = "i2cget -y 1 " + addrs + " " + regs;
char* c = const_cast<char*>(command.c_str());
return execBash(c);
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 1;
}
uint8_t I2C_Driver::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);
}
if(writeByte(address,reg))
{
if (read(deviceDescriptor, recieve_buffer, 1) != 1)
{
std::cout << "Unable to read quiting" << std::endl;
exit(1); // ERROR HANDLING: i2c transaction failed
}
}
return recieve_buffer[0] ;
}

@ -1,24 +1,28 @@
#include <stdint.h>
#include <iostream>
#include <string>
#include "errorHandling.h"
class I2C_Driver
{
public:
I2C_Driver();
std::string readByte(const uint8_t& address, const uint8_t& reg);
I2C_Driver(const uint8_t& channel, const uint8_t& mode);
uint8_t readByte(const uint8_t& address, const uint8_t& reg);
uint16_t readWord(const uint8_t& address, const uint8_t& reg);
bool writeByte(const uint8_t& address, const uint8_t& reg, const uint8_t& data); // retuns 0 when a sucsessful transation ocures
bool writeWord(const uint8_t& address, const uint8_t& reg, const uint16_t& data); // retuns 0 when a sucsessful transation ocures
uint8_t writeByte(const uint8_t& address, const uint8_t& data); // retuns 0 when a sucsessful transation ocures
uint8_t writeWord(const uint8_t& address, const uint8_t& reg, const uint16_t& data); // retuns 0 when a sucsessful transation ocures
private:
/* unsigned char device_address;
unsigned char device_reg;
unsigned char send_buffer[100];
unsigned char recieve_buffer[100];
unsigned char blocks;
*/
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;
};

@ -15,13 +15,9 @@
#include "i2c_driver.h"
ErrorHandler errorHandle;
CommandManager commander;
I2C_Driver i2cDriver;
I2C_Driver i2cDriver(1,1);
int initPlatform()
@ -33,7 +29,7 @@ int initPlatform()
void dummy()
{
std::cout<< i2cDriver.readByte(0x40,0x00) <<std::endl;
std::cout<< unsigned(i2cDriver.readByte(0x40,0x00)) <<std::endl;
}
int main(int argc, char *argv[])

Binary file not shown.
Loading…
Cancel
Save