這是的exynos4412 I2C EEPROM測試程序。
- /***************************************************************************
- copyright : (C) by 2003-2004 Stefano Barbato
- email : [url=mailto:stefano@codesink.org]stefano@codesink.org[/url]
- $Id: 24cXX.c,v 1.5 02/29 11:05:28 tat Exp $
- ***************************************************************************/
- /***************************************************************************
- * *
- * This program is free software; you can redistribute it and/or modify *
- * it under the terms of the GNU General Public License as published by *
- * the Free Software Foundation; either version 2 of the License, or *
- * (at your option) any later version. *
- * *
- ***************************************************************************/
- #include <stdio.h>
- #include <fcntl.h>
- #include <unistd.h>
- #include <stdlib.h>
- #include <linux/fs.h>
- #include <sys/types.h>
- #include <sys/ioctl.h>
- #include <errno.h>
- #include <assert.h>
- #include <string.h>
- #include <linux/i2c-dev.h>
- #include <linux/i2c.h>
- static inline __s32 i2c_smbus_access(int file, char read_write, __u8 command,
- int size, union i2c_smbus_data *data)
- {
- struct i2c_smbus_ioctl_data args;
- args.read_write = read_write;
- args.command = command;
- args.size = size;
- args.data = data;
- return ioctl(file,I2C_SMBUS,&args);
- }
- static inline __s32 i2c_smbus_write_quick(int file, __u8 value)
- {
- return i2c_smbus_access(file,value,0,I2C_SMBUS_QUICK,NULL);
- }
-
- static inline __s32 i2c_smbus_read_byte(int file)
- {
- union i2c_smbus_data data;
- if (i2c_smbus_access(file,I2C_SMBUS_READ,0,I2C_SMBUS_BYTE,&data))
- return -1;
- else
- return 0x0FF & data.byte;
- }
- static inline __s32 i2c_smbus_write_byte(int file, __u8 value)
- {
- return i2c_smbus_access(file,I2C_SMBUS_WRITE,value,
- I2C_SMBUS_BYTE,NULL);
- }
- static inline __s32 i2c_smbus_read_byte_data(int file, __u8 command)
- {
- union i2c_smbus_data data;
- if (i2c_smbus_access(file,I2C_SMBUS_READ,command,
- I2C_SMBUS_BYTE_DATA,&data))
- return -1;
- else
- return 0x0FF & data.byte;
- }
- static inline __s32 i2c_smbus_write_byte_data(int file, __u8 command,
- __u8 value)
- {
- union i2c_smbus_data data;
- data.byte = value;
- return i2c_smbus_access(file,I2C_SMBUS_WRITE,command,
- I2C_SMBUS_BYTE_DATA, &data);
- }
- static inline __s32 i2c_smbus_read_word_data(int file, __u8 command)
- {
- union i2c_smbus_data data;
- if (i2c_smbus_access(file,I2C_SMBUS_READ,command,
- I2C_SMBUS_WORD_DATA,&data))
- return -1;
- else
- return 0x0FFFF & data.word;
- }
- static inline __s32 i2c_smbus_write_word_data(int file, __u8 command,
- __u16 value)
- {
- union i2c_smbus_data data;
- data.word = value;
- return i2c_smbus_access(file,I2C_SMBUS_WRITE,command,
- I2C_SMBUS_WORD_DATA, &data);
- }
- static inline __s32 i2c_smbus_process_call(int file, __u8 command, __u16 value)
- {
- union i2c_smbus_data data;
- data.word = value;
- if (i2c_smbus_access(file,I2C_SMBUS_WRITE,command,
- I2C_SMBUS_PROC_CALL,&data))
- return -1;
- else
- return 0x0FFFF & data.word;
- }
- /* Returns the number of read bytes */
- static inline __s32 i2c_smbus_read_block_data(int file, __u8 command,
- __u8 *values)
- {
- union i2c_smbus_data data;
- int i;
- if (i2c_smbus_access(file,I2C_SMBUS_READ,command,
- I2C_SMBUS_BLOCK_DATA,&data))
- return -1;
- else {
- for (i = 1; i <= data.block[0]; i++)
- values[i-1] = data.block[i];
- return data.block[0];
- }
- }
- static inline __s32 i2c_smbus_write_block_data(int file, __u8 command,
- __u8 length, __u8 *values)
- {
- union i2c_smbus_data data;
- int i;
- if (length > 32)
- length = 32;
- for (i = 1; i <= length; i++)
- data.block[i] = values[i-1];
- data.block[0] = length;
- return i2c_smbus_access(file,I2C_SMBUS_WRITE,command,
- I2C_SMBUS_BLOCK_DATA, &data);
- }
- /* Returns the number of read bytes */
- static inline __s32 i2c_smbus_read_i2c_block_data(int file, __u8 command,
- __u8 *values)
- {
- union i2c_smbus_data data;
- int i;
- if (i2c_smbus_access(file,I2C_SMBUS_READ,command,
- I2C_SMBUS_I2C_BLOCK_DATA,&data))
- return -1;
- else {
- for (i = 1; i <= data.block[0]; i++)
- values[i-1] = data.block[i];
- return data.block[0];
- }
- }
- static inline __s32 i2c_smbus_write_i2c_block_data(int file, __u8 command,
- __u8 length, __u8 *values)
- {
- union i2c_smbus_data data;
- int i;
- if (length > 32)
- length = 32;
- for (i = 1; i <= length; i++)
- data.block[i] = values[i-1];
- data.block[0] = length;
- return i2c_smbus_access(file,I2C_SMBUS_WRITE,command,
- I2C_SMBUS_I2C_BLOCK_DATA, &data);
- }
- /* Returns the number of read bytes */
- static inline __s32 i2c_smbus_block_process_call(int file, __u8 command,
- __u8 length, __u8 *values)
- {
- union i2c_smbus_data data;
- int i;
- if (length > 32)
- length = 32;
- for (i = 1; i <= length; i++)
- data.block[i] = values[i-1];
- data.block[0] = length;
- if (i2c_smbus_access(file,I2C_SMBUS_WRITE,command,
- I2C_SMBUS_BLOCK_PROC_CALL,&data))
- return -1;
- else {
- for (i = 1; i <= data.block[0]; i++)
- values[i-1] = data.block[i];
- return data.block[0];
- }
- }
- #define CHECK_I2C_FUNC( var, label ) \
- do { if(0 == (var & label)) { \
- fprintf(stderr, "\nError: " \
- #label " function is required. Program halted.\n\n"); \
- exit(1); } \
- } while(0);
- int main(int argc, char** argv)
- {
- int funcs, fd, r;
- //int addr = 0x68;
- int addr = 0x50;
- fd = 0;
- __u8 buf[2];
- __u8 dat[2];
-
- fd = open("/dev/i2c/0", O_RDWR);
- if(fd <= 0)
- {
- fprintf(stderr, "Error sensor_open: %s\n", strerror(errno));
- return -1;
- }
- if((r = ioctl(fd, I2C_FUNCS, &funcs) < 0))
- {
- fprintf(stderr, "Error sensor_open: %s\n", strerror(errno));
- return -1;
- }
-
- // check for req funcs
- CHECK_I2C_FUNC( funcs, I2C_FUNC_SMBUS_READ_BYTE );
- CHECK_I2C_FUNC( funcs, I2C_FUNC_SMBUS_WRITE_BYTE );
- CHECK_I2C_FUNC( funcs, I2C_FUNC_SMBUS_READ_BYTE_DATA );
- CHECK_I2C_FUNC( funcs, I2C_FUNC_SMBUS_WRITE_BYTE_DATA );
- CHECK_I2C_FUNC( funcs, I2C_FUNC_SMBUS_READ_WORD_DATA );
- CHECK_I2C_FUNC( funcs, I2C_FUNC_SMBUS_WRITE_WORD_DATA );
- // set working device
- if( ( r = ioctl(fd, I2C_SLAVE, addr)) < 0)
- {
- fprintf(stderr, "Error sensor_open: %s\n", strerror(errno));
- return -1;
- }
- #if 0
- buf[0] = 0x1A;
- buf[1] = 0x07;
- i2c_write_2b(fd, buf);
- i2c_write_1b(fd, 0x41);
- dat[1] = i2c_smbus_read_byte(fd);
- i2c_write_1b(fd, 0x42);
- dat[0] = i2c_smbus_read_byte(fd);
- #endif
-
- printf("begin to test...\n");
- buf[0] = 0x01;
- buf[1] = 0x6c;
- r = i2c_smbus_write_byte_data(fd, buf[0], buf[1]);
- if(r < 0){printf(" current smbus_wirte_byte_data error.\n");};
- printf("write data ok.\n");
- usleep(1000);
- fflush(stdout);
-
- r = i2c_smbus_read_byte_data(fd, 0x01);
- if(r < 0){
- printf("current smbus_wirte_byte error1.\n");
- }else{
- printf("set high address ok.\n");
- }
-
- …………余下代碼請下載附件…………
- }
復制代碼 下載:
exynos4412 I2C EEPROM測試程序.rar
(1.8 KB, 下載次數: 5)
2017-3-15 22:48 上傳
點擊文件名下載附件
|