I2C Driver.

Hello Everbody,

Currently i am working on I2C driver , and to check wether my driver is working as i have designed it I have to write an application which i am finding it little hard as for that i have to use Devctl command which my driver supports , the following posted application code i am using but every time i compile this it says invalid argument ( in send section ) and same for receive ( recv ) …

int main()
{
int file;
int add = 0x90; /* The I2C address */

    char buf[40] = {0};
    char buf1[50]={0};
    char buf2[50]={0};
    char buf3[90];
    int error;
    int speed =100000;
            
    i2c_send_t send; 
    i2c_recv_t recv;
    i2c_send_t send1; 
    i2c_addr_t address;
    

    
    send.slave.addr = 0x90; 
    send.slave.fmt = 2; 
    send.len = 1; 
    send.stop = 1;  
    
    send1.slave.addr = 0x0; 
    send1.slave.fmt = 2; 
    send1.len = 1; 
    send1.stop =1;  
    
    
    recv.slave.addr = 90; 
    recv.slave.fmt = 002; 
    recv.len = 2; 
    recv.stop = 1;
    
    address.addr = add;
    address.fmt=2;
    
    
    

           if ((file = open("/dev/i2c0",O_RDWR)) < 0) 
           {
            /* ERROR HANDLING; you can check errno to see what went wrong */
            printf("Error Occured while opening Device File.!!\n");
            
           } 
           else
            {
            printf("Device File Opened Successfully !!\n");
            }

            
            if (error = devctl(file,DCMD_I2C_SET_BUS_SPEED,&(speed),sizeof(speed),NULL))
            
             {
             	/* ERROR HANDLING; you can check errno to see what went wrong */
                fprintf(stderr, "Error setting the bus speed: %s\n",strerror ( error ));
                exit(EXIT_FAILURE);
                            
             }
           
          
        
        if (error = devctl(file,DCMD_I2C_SET_SLAVE_ADDR,&address,sizeof(address),NULL))
        
           {
            /* ERROR HANDLING; you can check errno to see what went wrong */
                fprintf(stderr, "Error setting the slave address: %s\n",strerror ( error ));
               exit(EXIT_FAILURE);
          }
       

   
       if (error = devctl(file,DCMD_I2C_SEND,&send,sizeof(send),NULL))
         {
              /* ERROR HANDLING; you can check errno to see what went wrong */
                fprintf(stderr, "Error in sending the data 0: %s\n",strerror ( error ));
                exit(EXIT_FAILURE);
         }
 
       if (error = devctl(file,DCMD_I2C_SEND,&send1,sizeof(send1),NULL))
         {
              /* ERROR HANDLING; you can check errno to see what went wrong */
                fprintf(stderr, "Error in sending the data 1: %s\n",strerror ( error ));
                exit(EXIT_FAILURE);
         }
        
  
         
  
     if (error =devctl(file,DCMD_I2C_RECV,&buf2,sizeof(buf2),NULL))
        {
     	    /* ERROR HANDLING; you can check errno to see what went wrong */
                fprintf(stderr, "Error in receiving the data: %s\n",strerror ( error ));
                exit(EXIT_FAILURE);
           
   
        } 
    
    else
        {       
   	  
     
      	    read(file,buf3,40);
                puts(buf3);
                printf(" the received temperature %x", buf2[0]);
            	                	          	   	
     	   		  

      } 
      	   

      if (error =devctl(file,DCMD_I2C_DRIVER_INFO,&buf,sizeof(buf),NULL))
         {
         	
              /* ERROR HANDLING; you can check errno to see what went wrong */
                fprintf(stderr, "Error in getting driver info data: %s\n",strerror ( error ));
                exit(EXIT_FAILURE);
         }
     else
      {
         	printf(" i2c version: buf[0] = %d\n",buf[0]);
            printf(" i2c address mode :buf[1] = %d\n", buf[1]); 
      }



    printf("Successfull !!\n");
    close(file);
    return 0;

}

Please reply any help would be appreciated

Thanks

Regards

Can you be a little more specific about where the error occurs?

In the send ( where some data is passed using I2C_SEND) its gives an error saying invalid arguement and same is for thye receive when i use command I2C_RECV , and for setting the slave address and setting up the bus speed .by using their respective coomand , its actually gets nothing done on the respective registers of the board means when SET_SLAVE_ADDRESS gets executed it should put some contents in its respective register but it will not

so there are two things thats come in to my mind

  1. either i am not using these devctl properly as they should be .
  2. or there is some linking problem of devctl commands with the driver …
    actually how will devctl will call these functions ( written by me ) is i am not getting .

Hoping for a good reply

Regards

I am new to developing resource manager,I have gr8 interset in learning them can you post your resource manager for this I2C serial communication, how you mapped open,read,write for file operations in resource manager.
gagan if you can help me i will be very happy.

How true…

i’ve got a working I2C application code which make use of i2c1.
PM me with your email address if you need it :slight_smile:

Regards,
Kiddo

For i2c_send devctl command the input arguments are i2c_send_t(target address,len) and _Uint8t[](data buffer).If the size of data buffer is fixed i can define structure like this

struct mystruct
{
i2c_send_t data;
_Uint8t data[10];
};

and pass the address of object of type struct mystruct to devctl.I want to know how a client can send a buffer of data to i2c server whose size is not know at compile time.