#include #include #include #include #include #include #include #include #include #ifdef WITH_I2C_SMBUS #include #endif #include #include "deftypes.h" #include "lm75.h" #include "hih61xx.h" #include "hmc5883.h" #define VERSION 1.032 #ifndef _BUILD_TIME_ #define _BUILD_TIME_ __DATE__" "__TIME__ #endif // extern void lm75_print_help(void); // extern void lm75_read_all(const char *opts); // extern void lm75_read_one(const char *opts); // extern void hih61xx_print_help(void); // extern void hih61xx_read_all(const char *opts); // extern void hih61xx_read_one(const char *opts); // >> i2sensor () // >> i2sensor read_all 0 lm75 4f 1,-1.0;2,+0.2;3,+10;4,-2 // read_all, list_all, // ...read_all..., // read_one..., // ...read_reg_hex... , // ...read_reg_dec... , // ...set_param... , // ...set_reg... , __int16_t devicef=0; //# Bus-Device file long addr=0, funcs=0; //__uint8_t temp[256]; void (*print_all)(void) = NULL; void (*read_all)(const uchar *opts) = NULL; void (*read_one)(const uchar *opts) = NULL; void (*conf_set)(const uchar *opts) = NULL; void print_help(void) { printf ("\n" " i2sensors ()\n" "\n" "Aviable options: \n" " print_all -- list all available measurement parameters of the device\n" " read_all -- read all measured parameters from device\n" " read_one -- read the selected parameters from device\n" " read_repeat -- read measurements continously\n" " conf_set -- set the configuration register\n" " get_info -- List I2C driver functions\n" "\n" "Usage:\n" " i2sensors get_info \n" " i2sensors read_all
\n" " i2sensors read_one
\n" " i2sensors conf_set
\n" "\n" "Examples:\n" " i2sensors get_info 2 \n" " i2sensors read_all 1 lm75 0x4f \n" " i2sensors read_one 1 lm75 0x4f 00,-1.35\n" "\n" ); printf ("Version: v%1.3f, build: [%s] (gcc %s) \n\n",VERSION,_BUILD_TIME_,__VERSION__); } __uint8_t *xchg_data (__uint8_t *buf, __uint16_t wrlen, __uint16_t waitlen, __uint16_t rdlen){ //# [buf] = 32 byte #ifdef WITH_I2C_SMBUS __int32_t data=0; if( funcs & I2C_FUNC_I2C ){ #endif if(write(devicef, buf, 1) != 1){ //# write one byte to device fprintf (stderr, "I2C write error: [NO: %d, MSG: %s] at %s, line %d. (function: %s)\n", errno, strerror(errno), __FILE__, __LINE__, __func__); } usleep(waitlen*1000); //# Wait 10ms for reading if(read(devicef, buf, rdlen) != rdlen) { //# read the result fprintf (stderr, "I2C read error: [NO: %d, MSG: %s] at %s, line %d. (function: %s)\n", errno, strerror(errno), __FILE__, __LINE__, __func__); close(devicef); exit(EXIT_FAILURE); } #ifdef WITH_I2C_SMBUS } else if ( funcs && I2C_FUNC_SMBUS_WORD_DATA ){ if(i2c_smbus_write_byte(devicef, buf[0] ) < 0){ //# write one byte to device fprintf (stderr, "I2C write error: [NO: %d, MSG: %s] at %s, line %d. (function: %s)\n", errno, strerror(errno), __FILE__, __LINE__, __func__); } usleep(waitlen*1000); //# Wait 10ms for reading if((data = i2c_smbus_read_word_data(devicef, buf[0])) < 0) { //# read the result fprintf (stderr, "I2C read error: [NO: %d, MSG: %s] at %s, line %d. (function: %s)\n", errno, strerror(errno), __FILE__, __LINE__, __func__); close(devicef); exit(EXIT_FAILURE); } else { buf[0] = data & 0x0FF; buf[1] = (data >> 8) & 0x0FF; } } #endif return buf; } void preinit(const char *dev_type){ if(!strcmp("lm75", (char *)dev_type)){ print_all = lm75_print_all; read_all = lm75_read_all; read_one = lm75_read_one; conf_set = lm75_conf_set; } else if(!strcmp("hih61xx", (char *)dev_type)) { print_all = hih61xx_print_all; read_all = hih61xx_read_all; read_one = hih61xx_read_one; conf_set = hih61xx_conf_set; } // else if(!strcmp("hmc5883", dev_type)) { // print_all = hmc5883_print_all; // read_all = hmc5883_read_all; // read_one = hmc5883_read_one; // conf_set = hmc5883_conf_set; // } } int main( int argc, char *argv[] ){ if( (argc == 3 && !strcmp("get_info", argv[1])) || (4 < argc && argc < 10)){ //Minimal 5 arguments should be passed. (or get_info is called) char filename[256]; int i, file1; snprintf(filename, 256, "/dev/i2c-%s", argv[2]); devicef = open(filename, O_RDWR); //# Open the i2c bus if (devicef < 0){ fprintf(stderr, "\n\tCan't open \"%s\" BUS. (Have you any permission?)\n\n", filename); exit(EXIT_FAILURE); } if (ioctl(devicef, I2C_FUNCS, &funcs) < 0) { //Get i2c device features fprintf (stderr, "\n\tAdapter function error [ERR: %d, MSG: %s] at %s, line %d. \n", errno, strerror(errno), __FILE__, __LINE__); close(devicef); exit(EXIT_FAILURE); } if( argc > 4 ){ //If not get_info was called if(argv[4][0]=='0' && argv[4][1]=='x'){ //# Hexadecimal representetion of device address addr = (long)strtol(argv[4], NULL, 0); //# strtol() is working with hex-string correctly if (ioctl(devicef, I2C_SLAVE_FORCE, addr) < 0){ //# Seeking/Opening the device on the Bus //printf("\n\tCan't open i2c DEVICE, errno: %d\n\n",errno); fprintf (stderr, "\n\tCan't open i2c DEVICE! [ERR: %d, MSG: %s] at %s, line %d. \n", errno, strerror(errno), __FILE__, __LINE__); close(devicef); exit(EXIT_FAILURE); } } else { fprintf(stderr, "\n\tThe DEVICE address must be a hexadecimal number\n\n"); close(devicef); exit(EXIT_FAILURE); } preinit((char *)argv[3]); //Initialization of subcommands } if (!strcmp("print_all",argv[1]) && print_all != NULL){ //# Gets the device's help message. print_all(); } else if (!strcmp("read_all",argv[1]) && read_all != NULL){ //# Pulls all readable registers data from device // read_all(argv[5]); read_all(NULL); } else if (!strcmp("read_one",argv[1]) && read_one != NULL && argc > 5) { //# Prints out the selected register read_one((uchar*)argv[5]); } else if (!strcmp("conf_set",argv[1]) && conf_set != NULL && argc > 5){ //# Prints out the selected register conf_set((uchar*)argv[5]); } else if ( !strcmp("get_info", argv[1]) ){ snprintf(filename, 256, "/sys/class/i2c-dev/i2c-%s/name", argv[2]); file1 = open(filename, O_RDONLY); i = read(file1, filename, 256 ); if(i > 0) { filename[i-1] = '\0'; } close(file1); printf("\n Functions, supported by the [%s] adapter:\n\n", filename); printf("\tI2C_FUNC_I2C -- "); if (funcs & I2C_FUNC_I2C){ printf("YES\n"); } else { printf("NO\n"); } printf("\tI2C_FUNC_SMBUS_BYTE -- "); if (funcs & I2C_FUNC_SMBUS_BYTE){ printf("YES\n"); } else { printf("NO\n"); } printf("\tI2C_FUNC_SMBUS_BYTE_DATA -- "); if (funcs & I2C_FUNC_SMBUS_BYTE_DATA){ printf("YES\n"); } else { printf("NO\n"); } printf("\tI2C_FUNC_SMBUS_WORD_DATA -- "); if (funcs & I2C_FUNC_SMBUS_WORD_DATA){ printf("YES\n"); } else { printf("NO\n"); } printf("\tI2C_FUNC_SMBUS_BLOCK_DATA -- "); if (funcs & I2C_FUNC_SMBUS_BLOCK_DATA){ printf("YES\n"); } else { printf("NO\n"); } printf("\tI2C_FUNC_SMBUS_I2C_BLOCK -- "); if (funcs & I2C_FUNC_SMBUS_I2C_BLOCK){ printf("YES\n"); } else { printf("NO\n"); } printf("\tI2C_FUNC_SMBUS_EMUL -- "); if (funcs & I2C_FUNC_SMBUS_EMUL){ printf("YES\n"); } else { printf("NO\n"); } printf("\tI2C_FUNC_NOSTART -- "); if (funcs & I2C_FUNC_NOSTART){ printf("YES\n"); } else { printf("NO\n"); } printf("\tI2C_FUNC_10BIT_ADDR -- "); if (funcs & I2C_FUNC_10BIT_ADDR){ printf("YES\n"); } else { printf("NO\n"); } printf("\tI2C_FUNC_PROTOCOL_MANGLING -- "); if (funcs & I2C_FUNC_PROTOCOL_MANGLING){ printf(" YES\n"); } else { printf("NO\n"); } printf("\n\tMore info in Linux Kernel Documentation: i2c/functionality.rst\n\n"); } else { //# No other option print_help(); } close(devicef); } else { print_help(); } return 0; }