From: Nathael Pajani Date: Wed, 2 Sep 2015 09:10:41 +0000 (+0200) Subject: Move all static information to a structure to allow the use of multiple sensors. X-Git-Url: http://git.techno-innov.fr/?a=commitdiff_plain;h=264a4fdfeecad740a77a0b020b9b3598890bcc12;p=soft%2Flpc122x%2Fcore Move all static information to a structure to allow the use of multiple sensors. --- diff --git a/extdrv/tmp101_temp_sensor.c b/extdrv/tmp101_temp_sensor.c index 4d585d0..75a99d8 100644 --- a/extdrv/tmp101_temp_sensor.c +++ b/extdrv/tmp101_temp_sensor.c @@ -59,25 +59,19 @@ enum tmp10x_internal_reg_numbers { -/* This static value is used to keep track of the last register accessed to - * prevent sending the pointer register again if we want to read the same - * register again. */ -static int last_accessed_register = 0; - /* Check the sensor presence, return 1 if sensor was found. * This is a basic check, it could be anything with the same address ... * addr : the sensor address on most significant bits. */ -int tmp101_probe_sensor(uint8_t addr) +int tmp101_probe_sensor(struct tmp101_sensor_config* conf) { - static int ret = -1; - char cmd_buf = (addr | I2C_READ_BIT); + char cmd_buf = (conf->addr | I2C_READ_BIT); /* Did we already probe the sensor ? */ - if (ret != 1) { - ret = i2c_read(&cmd_buf, 1, NULL, NULL, 0); + if (conf->probe_ok != 1) { + conf->probe_ok = i2c_read(&cmd_buf, 1, NULL, NULL, 0); } - return ret; + return conf->probe_ok; } /* Convert raw temperature data (expressed as signed value of 16 times the @@ -92,7 +86,6 @@ int tmp101_convert_to_deci_degrees(uint16_t raw) /* Temp Read * Performs a non-blocking read of the temperature from the sensor. - * addr : the sensor address on most significant bits. * 'raw' and 'deci_degrees' : integer addresses for conversion result, may be NULL. * Return value(s): * Upon successfull completion, returns 0 and the temperature read is placed in the @@ -105,25 +98,25 @@ int tmp101_convert_to_deci_degrees(uint16_t raw) * -EIO : Bad one: Illegal start or stop, or illegal state in i2c state machine */ #define CMD_BUF_SIZE 3 -int tmp101_sensor_read(uint8_t addr, uint16_t* raw, int* deci_degrees) +int tmp101_sensor_read(struct tmp101_sensor_config* conf, uint16_t* raw, int* deci_degrees) { int ret = 0; uint16_t temp = 0; - char cmd_buf[CMD_BUF_SIZE] = { addr, TMP_REG_TEMPERATURE, (addr | I2C_READ_BIT), }; + char cmd_buf[CMD_BUF_SIZE] = { conf->addr, TMP_REG_TEMPERATURE, (conf->addr | I2C_READ_BIT), }; char ctrl_buf[CMD_BUF_SIZE] = { I2C_CONT, I2C_DO_REPEATED_START, I2C_CONT, }; - if (tmp101_probe_sensor(addr) != 1) { + if (tmp101_probe_sensor(conf) != 1) { return -ENODEV; } /* Read the requested data */ - if (last_accessed_register == TMP_REG_TEMPERATURE) { + if (conf->last_accessed_register == TMP_REG_TEMPERATURE) { /* No need to switch back to temperature register */ ret = i2c_read((cmd_buf + 2), 1, (ctrl_buf + 2), (char*)&temp, 2); } else { /* Send (write) temperature register address to TMP101 internal pointer */ ret = i2c_read(cmd_buf, CMD_BUF_SIZE, ctrl_buf, (char*)&temp, 2); - last_accessed_register = TMP_REG_TEMPERATURE; + conf->last_accessed_register = TMP_REG_TEMPERATURE; } if (ret == 2) { @@ -143,8 +136,6 @@ int tmp101_sensor_read(uint8_t addr, uint16_t* raw, int* deci_degrees) * Performs default configuration of the temperature sensor. * The sensor is thus placed in shutdown mode, the thermostat is in interrupt mode, * and the polarity is set to active high. - * The conversion resolution is set to the provided "resolution". - * addr : the sensor address on most significant bits. * Return value : * Upon successfull completion, returns 0. On error, returns a negative integer * equivalent to errors from glibc. @@ -155,45 +146,42 @@ int tmp101_sensor_read(uint8_t addr, uint16_t* raw, int* deci_degrees) * -EREMOTEIO : Device did not acknowledge : Any device present ? * -EIO : Bad one: Illegal start or stop, or illegal state in i2c state machine */ -static uint8_t actual_config = 0; #define CONF_BUF_SIZE 4 -int tmp101_sensor_config(uint8_t addr, uint32_t resolution) +int tmp101_sensor_config(struct tmp101_sensor_config* conf) { int ret = 0; - char cmd[CONF_BUF_SIZE] = { addr, TMP_REG_CONFIG, }; + char cmd[CONF_BUF_SIZE] = { conf->addr, TMP_REG_CONFIG, }; - if (tmp101_probe_sensor(addr) != 1) { + if (tmp101_probe_sensor(conf) != 1) { return -ENODEV; } /* Store the new configuration */ - actual_config = (TMP_SHUTDOWN_MODE_ON | TMP_THERMOSTAT_INTERRUPT_MODE | TMP_ALERT_POLARITY_HIGH); - actual_config |= (resolution & (0x03 << 5)); - cmd[2] = actual_config; + conf->actual_config = (TMP_SHUTDOWN_MODE_ON | TMP_THERMOSTAT_INTERRUPT_MODE | TMP_ALERT_POLARITY_HIGH); + conf->actual_config |= (conf->resolution & (0x03 << 5)); + cmd[2] = conf->actual_config; ret = i2c_write(cmd, 3, NULL); - last_accessed_register = TMP_REG_CONFIG; + conf->last_accessed_register = TMP_REG_CONFIG; if (ret == 3) { return 0; /* Config success */ } return ret; } -/* Start a conversion when the sensor is in shutdown mode. - * addr : the sensor address on most significant bits. - */ -int tmp101_sensor_start_conversion(uint8_t addr) +/* Start a conversion when the sensor is in shutdown mode. */ +int tmp101_sensor_start_conversion(struct tmp101_sensor_config* conf) { int ret = 0; - char cmd[CONF_BUF_SIZE] = { addr, TMP_REG_CONFIG, }; + char cmd[CONF_BUF_SIZE] = { conf->addr, TMP_REG_CONFIG, }; - if (tmp101_probe_sensor(addr) != 1) { + if (tmp101_probe_sensor(conf) != 1) { return -ENODEV; } - cmd[2] = actual_config; + cmd[2] = conf->actual_config; cmd[2] |= TMP_ONE_SHOT_TRIGGER; ret = i2c_write(cmd, 3, NULL); - last_accessed_register = TMP_REG_CONFIG; + conf->last_accessed_register = TMP_REG_CONFIG; if (ret == 3) { return 0; /* Conversion start success */ } diff --git a/include/extdrv/tmp101_temp_sensor.h b/include/extdrv/tmp101_temp_sensor.h index a7b9594..1892a75 100644 --- a/include/extdrv/tmp101_temp_sensor.h +++ b/include/extdrv/tmp101_temp_sensor.h @@ -48,11 +48,27 @@ #define TMP_RES_TWELVE_BITS ((0x03 & 0x03) << 5) /* 320ms */ +/* TMP101 sensor instance data. + * Use one of this for each sensor you want to access. + * - addr is the sensor address on most significant bits. + * - last_accessed_register is used to keep track of the last register accessed to + * prevent sending the pointer register again if we want to read the same register again. + * - resolution is one of the above resolution defined values. + */ +struct tmp101_sensor_config { + uint8_t addr; + uint8_t probe_ok; + uint8_t actual_config; + int last_accessed_register; + uint32_t resolution; +}; + + /* Check the sensor presence, return 1 if found * This is a basic check, it could be anything with the same address ... * addr: the sensor address on most significant bits. */ -int tmp101_probe_sensor(uint8_t addr); +int tmp101_probe_sensor(struct tmp101_sensor_config* conf); /* Convert raw temperature data (expressed as signed value of 16 times the @@ -78,7 +94,7 @@ int tmp101_convert_to_deci_degrees(uint16_t raw); * -EREMOTEIO : Device did not acknowledge : Any device present ? * -EIO : Bad one: Illegal start or stop, or illegal state in i2c state machine */ -int tmp101_sensor_read(uint8_t addr, uint16_t* raw, int* deci_degrees); +int tmp101_sensor_read(struct tmp101_sensor_config* conf, uint16_t* raw, int* deci_degrees); /* Sensor config @@ -97,12 +113,12 @@ int tmp101_sensor_read(uint8_t addr, uint16_t* raw, int* deci_degrees); * -EREMOTEIO : Device did not acknowledge : Any device present ? * -EIO : Bad one: Illegal start or stop, or illegal state in i2c state machine */ -int tmp101_sensor_config(uint8_t addr, uint32_t resolution); +int tmp101_sensor_config(struct tmp101_sensor_config* conf); /* Start a conversion when the sensor is in shutdown mode. * addr : the sensor address on most significant bits. */ -int tmp101_sensor_start_conversion(uint8_t addr); +int tmp101_sensor_start_conversion(struct tmp101_sensor_config* conf); #endif /* EXTDRV_TEMP_H */