Loading ...
Sorry, an error occurred while loading the content.

6736Re: RTC adress?

Expand Messages
  • joakimmaartensson
    Sep 9, 2007
    • 0 Attachment
      Thanks for sharing the code!
      Unfortunatly my attempts on installing optware-devel whent haywire...
      Guess I'll have to go buy a small HDD instead of a thumbdrive!

      Still rewrote the code however! ;-)

      A few questions first.. In clock.c, is buf[0] = 0x00 the first of two
      bytes to get to the timetable?

      Second, doesn't ioctl() care about the last bit (R/W) in the address or
      is it added when it gets a write() or read()?

      If someone feels like compiling this code, feel free to do so (and
      please post the resulting files ;) )

      ds2482.c
      #include <stdio.h>
      #include <sys/types.h>
      #include <fcntl.h>
      #include <linux/i2c-dev.h>


      int main(){

      int file;
      int addr = 0x18; /* DS2482-800 address (hopefully)*/
      unsigned short reg = 0xf0; /* Status Register */
      char buf[10];
      int n;


      buf[0] = 0xe1; /* Set Read Pointer */
      buf[1] = reg; /* Status Register */

      file = openi2c(addr);
      if(file < 0)
      exit(0);

      printf("Set Read Pointer (E1h), and select Status Register (F0h)... ");
      n = writei2cbuf(file, buf, 2);
      if (n < 0)
      exit(1);

      printf("Reading Status Register: ");
      n = readi2cbuf(file, buf, 1);
      if (n < 0) {
      exit(1);
      } else {
      printf("0x%x",buf[0]);
      }

      closei2c(file);
      }
      i2clib.c

      #include <stdio.h>
      #include <sys/types.h>
      #include <fcntl.h>
      #include <linux/i2c-dev.h>

      int file;

      int openi2c(int dev_addr){

      if ((file = open("/dev/i2c0", O_RDWR)) < 0) {
      printf("open error!\n");
      return(-1);
      }

      if (ioctl(file,I2C_SLAVE_FORCE,dev_addr) < 0) {
      printf("address error!\n");
      return(-2);
      }
      return(file);
      }

      int writei2cbuf(int file, char *buffer, int count){

      int n;

      n=write(file, buffer, count);
      if (n != count) {
      printf("write error! %d\n",n);
      return(-1);
      }
      return(0);
      }

      int writei2cchar(int addr, char data){

      int n;
      char databuf[8];
      databuf[0]=(addr>>8 & 0xff);
      databuf[1]=(addr&0xff);
      databuf[2]=data;

      n=write(file,databuf,3);
      if (n != 3) {
      printf("write error! %d\n",n);
      return(-1);
      }
      return(data);
      }

      int readi2cbuf(int file, char *buffer, int count){

      int n;

      n = read(file, buffer, count);
      if (n != count) {
      printf("read error! %d\n",n);
      return(-2);
      } else {
      return(n);
      }
      }

      int readi2cchar(int addr){

      int n;
      char databuf[8];
      databuf[0]=(addr>>8 & 0xff);
      databuf[1]=(addr&0xff);

      n=write(file,databuf,2); /* set address for random read */
      if (n != 2) {
      printf("addr write error! %d\n",n);
      return(-1);
      }

      n=read(file,databuf,1);
      if (n != 1) {
      printf("read error! %d\n",n);
      return(-2);
      } else {
      return(databuf[0]);
      }
      }


      int closei2c(){

      close(file);
      }

      Am I assuming correctly that read/writei2cchar() isn't needed for my
      source? Didn't wan't to remove it just in case..

      make
      gcc -c i2clib.c -fpic
      #gcc -shared i2clib.o -Xlinker -export-dynamic -o i2c.so
      gcc clock.c i2clib.o -o clock





      [Non-text portions of this message have been removed]
    • Show all 9 messages in this topic