Merge branch 'Branch_Multise' into 'master'

HAL accepts to work with both temperature sensors

See merge request LoRa-Gateway/sx1302_hal!1
This commit is contained in:
Miguel Luis 2019-07-22 17:13:01 +02:00
commit e63d9a345b
3 changed files with 38 additions and 21 deletions

View file

@ -33,7 +33,8 @@ License: Revised BSD License, see LICENSE.TXT file include in the project
/* -------------------------------------------------------------------------- */ /* -------------------------------------------------------------------------- */
/* --- PUBLIC CONSTANTS ----------------------------------------------------- */ /* --- PUBLIC CONSTANTS ----------------------------------------------------- */
#define I2C_PORT_TEMP_SENSOR 0x39 #define I2C_PORT_TEMP_SENSOR_1 0x39
#define I2C_PORT_TEMP_SENSOR_2 0x3B
/* -------------------------------------------------------------------------- */ /* -------------------------------------------------------------------------- */
/* --- PUBLIC FUNCTIONS ----------------------------------------------------- */ /* --- PUBLIC FUNCTIONS ----------------------------------------------------- */

View file

@ -564,7 +564,7 @@ int lgw_debug_setconf(struct lgw_conf_debug_s * conf) {
/* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ */ /* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ */
int lgw_start(void) { int lgw_start(void) {
int i, err; int i, err, err_id_1,err_id_2;
int reg_stat; int reg_stat;
if (CONTEXT_STARTED == true) { if (CONTEXT_STARTED == true) {
@ -714,8 +714,9 @@ int lgw_start(void) {
#endif #endif
/* Open I2C */ /* Open I2C */
err = i2c_linuxdev_open(I2C_DEVICE, I2C_PORT_TEMP_SENSOR, &lgw_i2c_target); err_id_1 = i2c_linuxdev_open(I2C_DEVICE, I2C_PORT_TEMP_SENSOR_1, &lgw_i2c_target);
if ((err != 0) || (lgw_i2c_target <= 0)) { 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); printf("ERROR: failed to open I2C device %s (err=%i)\n", I2C_DEVICE, err);
return LGW_HAL_ERROR; return LGW_HAL_ERROR;
} }

View file

@ -75,13 +75,14 @@ License: Revised BSD License, see LICENSE.TXT file include in the project
/* --- INTERNAL SHARED VARIABLES -------------------------------------------- */ /* --- INTERNAL SHARED VARIABLES -------------------------------------------- */
extern int lgw_i2c_target; extern int lgw_i2c_target;
uint8_t slave_addr;
/* -------------------------------------------------------------------------- */ /* -------------------------------------------------------------------------- */
/* --- PRIVATE FUNCTIONS ---------------------------------------------------- */ /* --- PRIVATE FUNCTIONS ---------------------------------------------------- */
int stts751_configure( int i2c_fd ) int stts751_configure( int i2c_fd )
{ {
int err; int err,err_1,err_2;
uint8_t val; uint8_t val;
/* Check Input Params */ /* Check Input Params */
@ -93,13 +94,27 @@ int stts751_configure( int i2c_fd )
DEBUG_MSG("INFO: configuring STTS751 temperature sensor...\n"); DEBUG_MSG("INFO: configuring STTS751 temperature sensor...\n");
/* Get product ID */ /* Get product ID and test which sensor is mounted */
err = i2c_linuxdev_read( i2c_fd, I2C_PORT_TEMP_SENSOR, STTS751_REG_PROD_ID, &val ); err_1 = i2c_linuxdev_read( i2c_fd, I2C_PORT_TEMP_SENSOR_1, STTS751_REG_PROD_ID, &val );
if ( err != 0 ) 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; return LGW_I2C_ERROR;
} }
else
{
if ( err_1 == 0 )
{
slave_addr = I2C_PORT_TEMP_SENSOR_1;
}
if( err_2 == 0 )
{
slave_addr = I2C_PORT_TEMP_SENSOR_2;
}
}
switch( val ) switch( val )
{ {
case STTS751_0_PROD_ID: case STTS751_0_PROD_ID:
@ -114,10 +129,10 @@ int stts751_configure( int i2c_fd )
} }
/* Get Manufacturer ID */ /* Get Manufacturer ID */
err = i2c_linuxdev_read( i2c_fd, I2C_PORT_TEMP_SENSOR, STTS751_REG_MAN_ID, &val ); err = i2c_linuxdev_read( i2c_fd, slave_addr, STTS751_REG_MAN_ID, &val );
if ( err != 0 ) 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", slave_addr, err );
return LGW_I2C_ERROR; return LGW_I2C_ERROR;
} }
if ( val != ST_MAN_ID ) if ( val != ST_MAN_ID )
@ -131,27 +146,27 @@ int stts751_configure( int i2c_fd )
} }
/* Get revision number */ /* Get revision number */
err = i2c_linuxdev_read( i2c_fd, I2C_PORT_TEMP_SENSOR, STTS751_REG_REV_ID, &val ); err = i2c_linuxdev_read( i2c_fd, slave_addr, STTS751_REG_REV_ID, &val );
if ( err != 0 ) 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", slave_addr, err );
return LGW_I2C_ERROR; return LGW_I2C_ERROR;
} }
DEBUG_PRINTF("INFO: Revision number: 0x%02X\n", val); DEBUG_PRINTF("INFO: Revision number: 0x%02X\n", val);
/* Set conversion resolution to 12 bits */ /* 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, slave_addr, STTS751_REG_CONF, 0x8C ); /* TODO: do not hardcode the whole byte */
if ( err != 0 ) 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", slave_addr, err );
return LGW_I2C_ERROR; return LGW_I2C_ERROR;
} }
/* Set conversion rate to 1 / second */ /* 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, slave_addr, STTS751_REG_RATE, 0x04 );
if ( err != 0 ) 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", slave_addr, err );
return LGW_I2C_ERROR; return LGW_I2C_ERROR;
} }
@ -174,18 +189,18 @@ int stts751_get_temperature( int i2c_fd, float * temperature)
} }
/* Read Temperature LSB */ /* 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, slave_addr, STTS751_REG_TEMP_L, &low_byte );
if ( err != 0 ) 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", slave_addr, err );
return LGW_I2C_ERROR; return LGW_I2C_ERROR;
} }
/* Read Temperature MSB */ /* 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, slave_addr, STTS751_REG_TEMP_H, &high_byte );
if ( err != 0 ) 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", slave_addr, err );
return LGW_I2C_ERROR; return LGW_I2C_ERROR;
} }