發表文章

目前顯示的是 5月, 2012的文章

在Blogger內貼source code

下面這個網站可以幫忙轉格式  Format my Source Code for Blogging reference: http://marieyuen.blogspot.com/2007/12/bloggersource-code.html

用 c 讀取 i2c 的 register

C example 底下是個範例, 詳細資料參考 http://www.mjmwired.net/kernel/Documentation/i2c/dev-interface  會更清楚. write.c #include <stdio.h> #include <fcntl.h> #include <linux/i2c-dev.h> #include <errno.h> #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; }

linux 下 用I2C tools 來讀取 i2c 上的 register

前陣子再測試 TI DM8168 時, 發現 i2c bus 好像有標準的 tool 可以測試,  找了一下, 發現了一套 i2c-tools, 請到 lm-sensors 網站下載. compile 後應該有四個執行檔 i2cdetect – 用來列舉I2C bus和上面所有的裝置 i2cdump – 顯示裝置上所有register的值 i2cget – 讀取裝置上某個register的值 i2cset – 寫入裝置上某個register example: #i2cdetect -l i2c-1 i2c       OMAP I2C adapter                 I2C adapter i2c-2 i2c       OMAP I2C adapter                 I2C adapter #i2cdetect -y -r 1      0  1  2  3  4  5  6  7  8  9  a  b  c  d  e  f 00:          -- -- -- -- -- -- -- -- -- -- -- -- --  10: -- -- -- -- -- -- -- -- UU -- -- -- -- -- -- --  20: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --  30: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --  40: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --  50: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --  60: -- -- -- -- -- -- -- -- UU -- -- -- -- -- -- --  70: -- -- -- -- -- -- -- -- #i2cdump -f -y 1 0x18 No size specified (using byte-data access)      0  1  2  3  4  5  6  7  8  9  a  b  c  d  e  f    01