From 218876a8d5ed904583968d1c9afa8309c9e4faee Mon Sep 17 00:00:00 2001 From: bboulet Date: Mon, 22 Jul 2019 15:42:58 +0200 Subject: [PATCH 1/2] HAL accepts to work with both temperature sensors --- libloragw/inc/loragw_stts751.h | 3 ++- libloragw/src/loragw_hal.c | 7 ++--- libloragw/src/loragw_stts751.c | 49 ++++++++++++++++++++++------------ 3 files changed, 38 insertions(+), 21 deletions(-) diff --git a/libloragw/inc/loragw_stts751.h b/libloragw/inc/loragw_stts751.h index 54ae164..03612f2 100644 --- a/libloragw/inc/loragw_stts751.h +++ b/libloragw/inc/loragw_stts751.h @@ -33,7 +33,8 @@ License: Revised BSD License, see LICENSE.TXT file include in the project /* -------------------------------------------------------------------------- */ /* --- PUBLIC CONSTANTS ----------------------------------------------------- */ -#define I2C_PORT_TEMP_SENSOR 0x39 +#define I2C_PORT_TEMP_SENSOR_1 0x39 +#define I2C_PORT_TEMP_SENSOR_2 0x3B /* -------------------------------------------------------------------------- */ /* --- PUBLIC FUNCTIONS ----------------------------------------------------- */ diff --git a/libloragw/src/loragw_hal.c b/libloragw/src/loragw_hal.c index 50e37e2..affe532 100644 --- a/libloragw/src/loragw_hal.c +++ b/libloragw/src/loragw_hal.c @@ -564,7 +564,7 @@ int lgw_debug_setconf(struct lgw_conf_debug_s * conf) { /* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ */ int lgw_start(void) { - int i, err; + int i, err, err_ID1,err_ID2; int reg_stat; if (CONTEXT_STARTED == true) { @@ -714,8 +714,9 @@ int lgw_start(void) { #endif /* Open I2C */ - err = i2c_linuxdev_open(I2C_DEVICE, I2C_PORT_TEMP_SENSOR, &lgw_i2c_target); - if ((err != 0) || (lgw_i2c_target <= 0)) { + err_ID1 = i2c_linuxdev_open(I2C_DEVICE, I2C_PORT_TEMP_SENSOR_1, &lgw_i2c_target); + err_ID2 = i2c_linuxdev_open(I2C_DEVICE, I2C_PORT_TEMP_SENSOR_2, &lgw_i2c_target); + if (((err_ID1 != 0) || (lgw_i2c_target <= 0)) && ((err_ID2 != 0) || (lgw_i2c_target <= 0))) { printf("ERROR: failed to open I2C device %s (err=%i)\n", I2C_DEVICE, err); return LGW_HAL_ERROR; } diff --git a/libloragw/src/loragw_stts751.c b/libloragw/src/loragw_stts751.c index b2b42ff..776524e 100644 --- a/libloragw/src/loragw_stts751.c +++ b/libloragw/src/loragw_stts751.c @@ -75,13 +75,14 @@ License: Revised BSD License, see LICENSE.TXT file include in the project /* --- INTERNAL SHARED VARIABLES -------------------------------------------- */ extern int lgw_i2c_target; +uint8_t SlaveAddr; /* -------------------------------------------------------------------------- */ /* --- PRIVATE FUNCTIONS ---------------------------------------------------- */ int stts751_configure( int i2c_fd ) { - int err; + int err,err_1,err_2; uint8_t val; /* Check Input Params */ @@ -93,13 +94,27 @@ int stts751_configure( int i2c_fd ) DEBUG_MSG("INFO: configuring STTS751 temperature sensor...\n"); - /* Get product ID */ - err = i2c_linuxdev_read( i2c_fd, I2C_PORT_TEMP_SENSOR, STTS751_REG_PROD_ID, &val ); - if ( err != 0 ) + /* Get product ID and test which sensor is mounted */ + err_1 = i2c_linuxdev_read( i2c_fd, I2C_PORT_TEMP_SENSOR_1, STTS751_REG_PROD_ID, &val ); + err_2 = i2c_linuxdev_read( i2c_fd, I2C_PORT_TEMP_SENSOR_2, STTS751_REG_PROD_ID, &val ); + + if ( (err_1 != 0) && (err_2 != 0) ) { - printf( "ERROR: failed to read I2C device 0x%02X (err=%i)\n", I2C_PORT_TEMP_SENSOR, err ); + printf( "ERROR: failed to read I2C device 0x%02X (err=%i) or 0x%02X (err=%i) \n", I2C_PORT_TEMP_SENSOR_1, err_1, I2C_PORT_TEMP_SENSOR_2, err_2 ); return LGW_I2C_ERROR; } + else + { + if ( err_1 == 0 ) + { + SlaveAddr = I2C_PORT_TEMP_SENSOR_1; + } + if( err_2 == 0 ) + { + SlaveAddr = I2C_PORT_TEMP_SENSOR_2; + } + } + switch( val ) { case STTS751_0_PROD_ID: @@ -114,10 +129,10 @@ int stts751_configure( int i2c_fd ) } /* Get Manufacturer ID */ - err = i2c_linuxdev_read( i2c_fd, I2C_PORT_TEMP_SENSOR, STTS751_REG_MAN_ID, &val ); + err = i2c_linuxdev_read( i2c_fd, SlaveAddr, STTS751_REG_MAN_ID, &val ); if ( err != 0 ) { - printf( "ERROR: failed to read I2C device 0x%02X (err=%i)\n", I2C_PORT_TEMP_SENSOR, err ); + printf( "ERROR: failed to read I2C device 0x%02X (err=%i)\n", SlaveAddr, err ); return LGW_I2C_ERROR; } if ( val != ST_MAN_ID ) @@ -131,27 +146,27 @@ int stts751_configure( int i2c_fd ) } /* Get revision number */ - err = i2c_linuxdev_read( i2c_fd, I2C_PORT_TEMP_SENSOR, STTS751_REG_REV_ID, &val ); + err = i2c_linuxdev_read( i2c_fd, SlaveAddr, STTS751_REG_REV_ID, &val ); if ( err != 0 ) { - printf( "ERROR: failed to read I2C device 0x%02X (err=%i)\n", I2C_PORT_TEMP_SENSOR, err ); + printf( "ERROR: failed to read I2C device 0x%02X (err=%i)\n", SlaveAddr, err ); return LGW_I2C_ERROR; } DEBUG_PRINTF("INFO: Revision number: 0x%02X\n", val); /* Set conversion resolution to 12 bits */ - err = i2c_linuxdev_write( i2c_fd, I2C_PORT_TEMP_SENSOR, STTS751_REG_CONF, 0x8C ); /* TODO: do not hardcode the whole byte */ + err = i2c_linuxdev_write( i2c_fd, SlaveAddr, STTS751_REG_CONF, 0x8C ); /* TODO: do not hardcode the whole byte */ if ( err != 0 ) { - printf( "ERROR: failed to write I2C device 0x%02X (err=%i)\n", I2C_PORT_TEMP_SENSOR, err ); + printf( "ERROR: failed to write I2C device 0x%02X (err=%i)\n", SlaveAddr, err ); return LGW_I2C_ERROR; } /* Set conversion rate to 1 / second */ - err = i2c_linuxdev_write( i2c_fd, I2C_PORT_TEMP_SENSOR, STTS751_REG_RATE, 0x04 ); + err = i2c_linuxdev_write( i2c_fd, SlaveAddr, STTS751_REG_RATE, 0x04 ); if ( err != 0 ) { - printf( "ERROR: failed to write I2C device 0x%02X (err=%i)\n", I2C_PORT_TEMP_SENSOR, err ); + printf( "ERROR: failed to write I2C device 0x%02X (err=%i)\n", SlaveAddr, err ); return LGW_I2C_ERROR; } @@ -174,18 +189,18 @@ int stts751_get_temperature( int i2c_fd, float * temperature) } /* Read Temperature LSB */ - err = i2c_linuxdev_read( i2c_fd, I2C_PORT_TEMP_SENSOR, STTS751_REG_TEMP_L, &low_byte ); + err = i2c_linuxdev_read( i2c_fd, SlaveAddr, STTS751_REG_TEMP_L, &low_byte ); if ( err != 0 ) { - printf( "ERROR: failed to read I2C device 0x%02X (err=%i)\n", I2C_PORT_TEMP_SENSOR, err ); + printf( "ERROR: failed to read I2C device 0x%02X (err=%i)\n", SlaveAddr, err ); return LGW_I2C_ERROR; } /* Read Temperature MSB */ - err = i2c_linuxdev_read( i2c_fd, I2C_PORT_TEMP_SENSOR, STTS751_REG_TEMP_H, &high_byte ); + err = i2c_linuxdev_read( i2c_fd, SlaveAddr, STTS751_REG_TEMP_H, &high_byte ); if ( err != 0 ) { - printf( "ERROR: failed to read I2C device 0x%02X (err=%i)\n", I2C_PORT_TEMP_SENSOR, err ); + printf( "ERROR: failed to read I2C device 0x%02X (err=%i)\n", SlaveAddr, err ); return LGW_I2C_ERROR; } From d43bd8fe8ec10eae625263e5d2d65e82e5646a52 Mon Sep 17 00:00:00 2001 From: bboulet Date: Mon, 22 Jul 2019 16:50:17 +0200 Subject: [PATCH 2/2] coding style modification --- libloragw/src/loragw_hal.c | 8 ++++---- libloragw/src/loragw_stts751.c | 30 +++++++++++++++--------------- 2 files changed, 19 insertions(+), 19 deletions(-) diff --git a/libloragw/src/loragw_hal.c b/libloragw/src/loragw_hal.c index affe532..5b8f47b 100644 --- a/libloragw/src/loragw_hal.c +++ b/libloragw/src/loragw_hal.c @@ -564,7 +564,7 @@ int lgw_debug_setconf(struct lgw_conf_debug_s * conf) { /* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ */ int lgw_start(void) { - int i, err, err_ID1,err_ID2; + int i, err, err_id_1,err_id_2; int reg_stat; if (CONTEXT_STARTED == true) { @@ -714,9 +714,9 @@ int lgw_start(void) { #endif /* Open I2C */ - err_ID1 = i2c_linuxdev_open(I2C_DEVICE, I2C_PORT_TEMP_SENSOR_1, &lgw_i2c_target); - err_ID2 = i2c_linuxdev_open(I2C_DEVICE, I2C_PORT_TEMP_SENSOR_2, &lgw_i2c_target); - if (((err_ID1 != 0) || (lgw_i2c_target <= 0)) && ((err_ID2 != 0) || (lgw_i2c_target <= 0))) { + err_id_1 = i2c_linuxdev_open(I2C_DEVICE, I2C_PORT_TEMP_SENSOR_1, &lgw_i2c_target); + err_id_2 = i2c_linuxdev_open(I2C_DEVICE, I2C_PORT_TEMP_SENSOR_2, &lgw_i2c_target); + if (((err_id_1 != 0) || (lgw_i2c_target <= 0)) && ((err_id_2 != 0) || (lgw_i2c_target <= 0))) { printf("ERROR: failed to open I2C device %s (err=%i)\n", I2C_DEVICE, err); return LGW_HAL_ERROR; } diff --git a/libloragw/src/loragw_stts751.c b/libloragw/src/loragw_stts751.c index 776524e..788c569 100644 --- a/libloragw/src/loragw_stts751.c +++ b/libloragw/src/loragw_stts751.c @@ -75,7 +75,7 @@ License: Revised BSD License, see LICENSE.TXT file include in the project /* --- INTERNAL SHARED VARIABLES -------------------------------------------- */ extern int lgw_i2c_target; -uint8_t SlaveAddr; +uint8_t slave_addr; /* -------------------------------------------------------------------------- */ /* --- PRIVATE FUNCTIONS ---------------------------------------------------- */ @@ -107,11 +107,11 @@ int stts751_configure( int i2c_fd ) { if ( err_1 == 0 ) { - SlaveAddr = I2C_PORT_TEMP_SENSOR_1; + slave_addr = I2C_PORT_TEMP_SENSOR_1; } if( err_2 == 0 ) { - SlaveAddr = I2C_PORT_TEMP_SENSOR_2; + slave_addr = I2C_PORT_TEMP_SENSOR_2; } } @@ -129,10 +129,10 @@ int stts751_configure( int i2c_fd ) } /* Get Manufacturer ID */ - err = i2c_linuxdev_read( i2c_fd, SlaveAddr, STTS751_REG_MAN_ID, &val ); + err = i2c_linuxdev_read( i2c_fd, slave_addr, STTS751_REG_MAN_ID, &val ); if ( err != 0 ) { - printf( "ERROR: failed to read I2C device 0x%02X (err=%i)\n", SlaveAddr, err ); + printf( "ERROR: failed to read I2C device 0x%02X (err=%i)\n", slave_addr, err ); return LGW_I2C_ERROR; } if ( val != ST_MAN_ID ) @@ -146,27 +146,27 @@ int stts751_configure( int i2c_fd ) } /* Get revision number */ - err = i2c_linuxdev_read( i2c_fd, SlaveAddr, STTS751_REG_REV_ID, &val ); + err = i2c_linuxdev_read( i2c_fd, slave_addr, STTS751_REG_REV_ID, &val ); if ( err != 0 ) { - printf( "ERROR: failed to read I2C device 0x%02X (err=%i)\n", SlaveAddr, err ); + printf( "ERROR: failed to read I2C device 0x%02X (err=%i)\n", slave_addr, err ); return LGW_I2C_ERROR; } DEBUG_PRINTF("INFO: Revision number: 0x%02X\n", val); /* Set conversion resolution to 12 bits */ - err = i2c_linuxdev_write( i2c_fd, SlaveAddr, STTS751_REG_CONF, 0x8C ); /* TODO: do not hardcode the whole byte */ + err = i2c_linuxdev_write( i2c_fd, slave_addr, STTS751_REG_CONF, 0x8C ); /* TODO: do not hardcode the whole byte */ if ( err != 0 ) { - printf( "ERROR: failed to write I2C device 0x%02X (err=%i)\n", SlaveAddr, err ); + printf( "ERROR: failed to write I2C device 0x%02X (err=%i)\n", slave_addr, err ); return LGW_I2C_ERROR; } /* Set conversion rate to 1 / second */ - err = i2c_linuxdev_write( i2c_fd, SlaveAddr, STTS751_REG_RATE, 0x04 ); + err = i2c_linuxdev_write( i2c_fd, slave_addr, STTS751_REG_RATE, 0x04 ); if ( err != 0 ) { - printf( "ERROR: failed to write I2C device 0x%02X (err=%i)\n", SlaveAddr, err ); + printf( "ERROR: failed to write I2C device 0x%02X (err=%i)\n", slave_addr, err ); return LGW_I2C_ERROR; } @@ -189,18 +189,18 @@ int stts751_get_temperature( int i2c_fd, float * temperature) } /* Read Temperature LSB */ - err = i2c_linuxdev_read( i2c_fd, SlaveAddr, STTS751_REG_TEMP_L, &low_byte ); + err = i2c_linuxdev_read( i2c_fd, slave_addr, STTS751_REG_TEMP_L, &low_byte ); if ( err != 0 ) { - printf( "ERROR: failed to read I2C device 0x%02X (err=%i)\n", SlaveAddr, err ); + printf( "ERROR: failed to read I2C device 0x%02X (err=%i)\n", slave_addr, err ); return LGW_I2C_ERROR; } /* Read Temperature MSB */ - err = i2c_linuxdev_read( i2c_fd, SlaveAddr, STTS751_REG_TEMP_H, &high_byte ); + err = i2c_linuxdev_read( i2c_fd, slave_addr, STTS751_REG_TEMP_H, &high_byte ); if ( err != 0 ) { - printf( "ERROR: failed to read I2C device 0x%02X (err=%i)\n", SlaveAddr, err ); + printf( "ERROR: failed to read I2C device 0x%02X (err=%i)\n", slave_addr, err ); return LGW_I2C_ERROR; }