I2C is a multi-master serial single-ended bus invented by Philips that is used to attach low-speed peripherals to an embedded system. SMBus, defined by Intel in 1995, is a subset of I2C. This article is a practical guide to use the I2C bus on the CORE9G25 Systems Linux embedded boards

To have a more in depth idea of how the I2C works under Linux please read the Linux Kernel documentation on:

 C code example

The following example sends a sequence of values from 0 to 255 to the PCF8574 I2C I/O expander at address 0x20 in C language.

  • write.c This code example is downloadable from the CD. File: CD: /linux/example/i2c/write.c
    #include 
    #include 
    #include 
    #include 
    
    #define I2C_ADDR 0x20
     
    int main (void) {
    	int value;
    	int fd;
    
    	fd = open("/dev/i2c-0", O_RDWR);
    
    	if (fd < 0) {
    		printf("Error opening file: %s\n", strerror(errno));
    		return 1;
    	}
    
    	if (ioctl(fd, I2C_SLAVE, I2C_ADDR) < 0) {
    		printf("ioctl error: %s\n", strerror(errno));
    		return 1;
    	}
    
    	for (value=0; value<=255; value++) {
    		if (write(fd, &value, 1) != 1) {
    			printf("Error writing file: %s\n", strerror(errno));
    		}
    		usleep(100000);
    	}
    	return 0;
    }
    

Related links

 



 


Market Mail : market@armdevs.com
Support Mail: support@armdevs.com Sales Phone: +86-755-29638421


CoreWind Online Chat>
Work time: 09:00-18:00
Copyright @ 2014 to 2020 - CoreWind Tech.