Linux - SoftwareThis forum is for Software issues.
Having a problem installing a new program? Want to know which application is best for the job? Post your question in this forum.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
Hi this is SrikanthPilli, i was struggling with i2c write/read operation. i2c write successfully completed. below is my program
****************************
/* Read input line for args of offset and bytes to read from IIC device.
Do read and dump contents.
Modified to have EEPROM_ADDR as parameter
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <fcntl.h>
#include <linux/i2c-dev.h>
#include <unistd.h>
#include <stdint.h>
#include "buffDump.h"
#include "buffDump.c"
int mdelay(int);
int main(int argc, char** argv)
{
unsigned offsetAddr;
unsigned SlaveAddr;
int count, bytesToRead;
unsigned char *i2c_buffer;
unsigned char i2c_ptr[3];
unsigned bytesRead;
int dev_i2c;
int adapter_nr;
char i2c_name[40];
int ret;
unsigned char buff[10]={0};
int i;
int res=0;
//int *val;
uint8_t val = 0x30;
adapter_nr=4;/* i2c4 number declaration*/
sprintf(i2c_name,"/dev/i2c-%d",adapter_nr);
printf("device opened at: %d\n",adapter_nr);
if ((dev_i2c = open(i2c_name, O_RDWR)) < 0) // device opening(smbus)
{
printf("Cannot Open I2C4 Master Device\n");
exit(1);
}
else { //start 1st else
printf("I2C4 open successfully\n");
SlaveAddr=0x50;//Generally this is eeprom address, later mention actual slave address
res=ioctl(dev_i2c, I2C_SLAVE, SlaveAddr);
if(res < 0)
{
printf("Cannot set I2C4 address\n");
exit(1);
}
else//start 2nd else
{
printf("I2C4 SET THE EEPROM ADDRESS\n");
i2c_ptr[0] = 0x30;
i2c_ptr[1] = 0xdd;
if (write(dev_i2c, i2c_ptr, 2)!=2)
{
printf("i2c data write Failed\n");
}
else{
printf("i2c data write success %x %x\n ",i2c_ptr[0],i2c_ptr[1]);
}
mdelay(500);
//Reading data from slave device**********************
printf("dev_i2c =%d\n",dev_i2c);
/******** Delay creation*/
int mdelay(int k)
{
int j=0;
while(k>0)
{
for(j=0;j<=500;j++);
k--;
}
}
****************************
As per my program, slave add=0x50, offset addr=0x30,data=0xdd.
after run this program, with the help of "i2cget -y 4 0x50 0x30", in console i was observed "0xdd". not only this data, i able to write any data successfully.
but i am facing problem with reading data from the particular offset address(0x30), i am getting garbage data.
i searches different sites in search engine, but i am not able to find the solution.
so please help me, how to read data from the slave with the specified memory location.
if possible please post entire write and read code in linux c?
<<<< snip >>>>
As per my program, slave add=0x50, offset addr=0x30,data=0xdd.
after run this program, with the help of "i2cget -y 4 0x50 0x30", in console i was observed "0xdd". not only this data, i able to write any data successfully.
but i am facing problem with reading data from the particular offset address(0x30), i am getting garbage data.
i searches different sites in search engine, but i am not able to find the solution.
so please help me, how to read data from the slave with the specified memory location.
if possible please post entire write and read code in linux c?
sorry about my english
Thanks in advance
SrikanthPilli
What is the device? In a different post you mentioned an eeprom. Is this it and what is the part number and manufacturer? Also, have you tried an identical device? Eeprom do develop defects after long usage. You might also want to check the voltage levels (and stability) at all the eeprom inputs as marginal signals can have poor results. Another possibility is signal timing. Does this occur at only one address or does it happen at several?
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.