I2C Max518
From GumstixDocsWiki
The MAX518 is a dual i2c digital to analog converter.
On the host computer fetch the lm_sensors-2.9.2.tar.gz package.
Extract the i2c-dev.h file from the kernel/include directory and place it in your project directory. There is a minor bug in the i2c-dev.h file that comes with the lm-senors package (related to the I2C_RDWR call). A patched version is available from SVN.
Credit for this codes origins goes to the authors of http://www2.lm-sensors.nu/~lm78/cvs/i2c/doc/dev-interface .
Copy i2cRead.c:
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <fcntl.h>
#include <errno.h>
#include <string.h>
#include "i2c-dev.h"
/* Intended to allow the reading of I2C packets to arbitrary addresses */
int main(int argc, char *argv[])
{
int file;
int adapter_nr = 0; /* probably dynamically determined */
char filename[20];
int i2c_addr;
int read_bytes;
char buf[256];
int i;
printf("I2CRead system\n");
if(argc-1<2)
{
printf("Usage: i2cRead i2c_addr read_bytes\n");
return(1);
}
i2c_addr = strtol(argv[1],NULL,16);
read_bytes = strtol(argv[2],NULL,10);
sprintf(filename,"/dev/i2c-%d",adapter_nr);
if ((file = open(filename,O_RDWR)) < 0) {
printf("ERRNO: %s\n",strerror(errno));
exit(1);
} else {
printf("Opened /dev/i2c-%d\n",adapter_nr);
}
if (ioctl(file,I2C_SLAVE,i2c_addr) < 0) {
printf("ERRNO: %s\n",strerror(errno));
exit(1);
}
if ( read(file,buf,read_bytes) != read_bytes) {
printf("I2C Send %x Failed\n",i2c_addr);
}
printf("Addr: %x Data: ",i2c_addr);
for(i=0; i<read_bytes; i++)
{
printf("%x ",buf[i]);
}
printf("\n");
if(file >= 0)
close(file);
}
Copy i2cWrite.c:
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <fcntl.h>
#include <errno.h>
#include <string.h>
#include "i2c-dev.h"
/* Intended to allow the writing of I2C packets to arbitrary addresses */
int main(int argc, char *argv[])
{
int file;
int adapter_nr = 0; /* probably dynamically determined */
char filename[20];
int i2c_addr;
char rec_bytes;
int sbuf[256];
char buf[256];
int i;
printf("I2CWrite system\n");
if(argc-1<2)
{
printf("Usage: i2cWrite i2c_addr i2c_byte1 i2c_byte2 ...\n");
return(1);
}
i2c_addr = strtol(argv[1],NULL,16);
for(i=2; i<argc; i++)
{
sbuf[i-1] = strtol(argv[i],NULL,16);
buf[i-2] = sbuf[i-1];
}
printf("Addr: %x Data: ",i2c_addr);
for(i=0; i<argc-2; i++)
{
printf("%x ",buf[i]);
}
printf("\n");
sprintf(filename,"/dev/i2c-%d",adapter_nr);
if ((file = open(filename,O_RDWR)) < 0) {
printf("ERRNO: %s\n",strerror(errno));
exit(1);
} else {
printf("Opened /dev/i2c-%d\n",adapter_nr);
}
if (ioctl(file,I2C_SLAVE,i2c_addr) < 0) {
printf("ERRNO: %s\n",strerror(errno));
exit(1);
}
if ( write(file,buf,argc-2) != argc-2) {
printf("I2C Send %x Failed\n",i2c_addr);
}
if(file >= 0)
close(file);
}
Compile the code with <path>/arm-linux-gcc -o i2cRead i2cRead.c and <path>/arm-linux-gcc -o i2cWrite i2cWrite.c. Note that you need to FIND arm-linux-gcc first.
Copy the executables to the Gumstix and run ./i2cWrite 2c 00 ff
Place an oscilloscope probe (or multimeter) on the DAC0 output and observe a 5v output on the DAC0 pin.
run ./i2cWrite 2c 00 00
Place an oscilloscope probe on the DAC0 output and observe a 0v output on the DAC0 pin.
Troubleshooting: If the compile fails with i2cRead.c: In function `main': i2cRead.c:40: error: `I2C_SLAVE' undeclared (first use in this function) you have the wrong i2c-dev.h. It should be 12-13k in size and its in the lm_sensors-2.9.2.tar.gz package at the moment.

