From dffa37c77d2d5ce30ec408626502541861637d9d Mon Sep 17 00:00:00 2001 From: Gergő J. Miklós Date: Thu, 15 Oct 2020 05:03:18 +0200 Subject: i2sensors SMBUS read OK (but not too pretty) --- src/main.c | 40 ++++++++++++++++++++++++++++------------ 1 file changed, 28 insertions(+), 12 deletions(-) diff --git a/src/main.c b/src/main.c index 10ca27b..4a4b15d 100644 --- a/src/main.c +++ b/src/main.c @@ -44,7 +44,7 @@ -__int16_t devicef; //# Bus-Device file +__int16_t devicef=0; //# Bus-Device file long addr=0, funcs=0; //__uint8_t temp[256]; void (*print_all)(void) = NULL; @@ -66,35 +66,51 @@ void print_help(void) { " 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.2f, build: [%s] (gcc %s) \n\n",VERSION,_BUILD_TIME_,__VERSION__); } -/* +/* void bus_err(int ern){ printf("I2C communication(rd) error. errno: %d\n",errno); //fprintf (stderr, "Internal error %d at %s, line %d. (function: %s/%s)\n", ern, __FILE__, __LINE__, __func__, __FUNCTION__); } */ -uchar *xchg_data (uchar *buf, uint16 wrlen, uint16 waitlen, uint16 rdlen){ //# [buf] = 32 byte +__uint8_t *xchg_data (__uint8_t *buf, __uint16_t wrlen, __uint16_t waitlen, __uint16_t rdlen){ //# [buf] = 32 byte - if(write(devicef, buf, 1) != 1){ //# write one byte to device - //perror("I2C communication(wrr) error."); - 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 + __int32_t data=0; + + if( funcs & I2C_FUNC_I2C ){ - if(read(devicef, buf, rdlen) != rdlen) { //# read the result - //perror("I2C communication(rd) error."); - fprintf (stderr, "I2C read error: [NO: %d, MSG: %s] at %s, line %d. (function: %s)\n", errno, strerror(errno), __FILE__, __LINE__, __func__); + 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__); + } + + } else if ( funcs && I2C_FUNC_SMBUS_WORD_DATA ){ + if(i2c_smbus_write_byte(devicef, buf[0] ) == -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((data = i2c_smbus_read_word_data(devicef, buf[0])) == -1) { //# 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__); + } else { + buf[0] = data & 0x0FF; + buf[1] = (data >> 8) & 0x0FF; + } } return buf; } @@ -154,7 +170,7 @@ int main( int argc, char *argv[] ){ } - if( argc > 5 ){ //If not get_info was called + if( argc > 4 ){ //If not get_info was called if(argv[4][0]=='0' && argv[4][1]=='x'){ //# Hexadecimal representetion of device address addr = (int)strtol(argv[4], NULL, 0); //# strtol() is working with hex-string correctly -- cgit v1.2.3