LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Software
User Name
Password
Linux - Software This 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


Reply
  Search this Thread
Old 12-28-2011, 05:59 AM   #1
pillisrikanth
LQ Newbie
 
Registered: May 2011
Location: india,andhrapradesh,guntur
Posts: 29
Blog Entries: 3

Rep: Reputation: 0
Question need i2c read/write in linux c?


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);

i2c_ptr[1] = 0x00;
i2c_ptr[0] = 0x30;
//i2c_ptr[2]=val;

/*if (read (dev_i2c,i2c_ptr,2)!=2)
{
printf("I2C4 READ FAILED\n");
}
else
{
printf("I2C4 read success, data=%x \t data=%x \t data=%x\n",i2c_ptr[0],i2c_ptr[1],i2c_ptr[2]);

}*/
bytesToRead=0x01;
bytesRead = read(dev_i2c, i2c_ptr, bytesToRead);

printf("I2C bytesRead. Return code = %d\n", bytesRead);
printf("I2C bytesToRead. Return code = %d\n", bytesToRead);
if(bytesRead != bytesToRead)
{
printf("I2C read failed. Return code = %u\n", bytesRead);
exit(1);
}
// Dump what we read

buffDump(i2c_ptr, bytesToRead);
//return(0);


}//2nd else close

}//1st else close


}

/******** 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?


sorry about my english

Thanks in advance

SrikanthPilli
 
Old 01-02-2012, 11:58 PM   #2
tommyttt
Member
 
Registered: Oct 2009
Location: Federal Way, WA
Distribution: openSUSE 11.4 x86_64, openSuSE 12.1, Fedora 15
Posts: 207

Rep: Reputation: 34
pillisrikanth:
Please only submit one post per question. If needed for clarification or additional information, add a reply to the original post.

Thanks, Tom
 
Old 01-03-2012, 12:08 AM   #3
tommyttt
Member
 
Registered: Oct 2009
Location: Federal Way, WA
Distribution: openSUSE 11.4 x86_64, openSuSE 12.1, Fedora 15
Posts: 207

Rep: Reputation: 34
Quote:
Originally Posted by pillisrikanth View Post
<<<< 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?

Tom
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
how to write i2c drver for keypad in linux for at91sam9261 board raulapati Linux - Hardware 1 03-08-2011 07:09 PM
Read Write access to a iso9660 filesystem..mount a .iso image as read write ceazar123 Linux - Newbie 16 09-01-2010 09:07 AM
Read Write access to a iso9660 filesystem..mount a .iso image as read write ceazar123 Linux - General 2 08-26-2010 03:32 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Software

All times are GMT -5. The time now is 10:46 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration