Commit 046a4eab authored by Davide Giovanelli's avatar Davide Giovanelli
Browse files

Reducing sensortag's code size by removing functions

parent d0596855
......@@ -121,7 +121,7 @@ typedef struct bmp_280_calibration {
int32_t t_fine;
} bmp_280_calibration_t;
/*---------------------------------------------------------------------------*/
static uint8_t calibration_data[CALIB_DATA_SIZE];
//static uint8_t calibration_data[CALIB_DATA_SIZE];
/*---------------------------------------------------------------------------*/
#define SENSOR_STATUS_DISABLED 0
#define SENSOR_STATUS_INITIALISED 1
......@@ -138,13 +138,13 @@ static uint8_t sensor_value[SENSOR_DATA_BUF_SIZE];
/* Wait SENSOR_STARTUP_DELAY clock ticks for the sensor to be ready - ~80ms */
#define SENSOR_STARTUP_DELAY 3
static struct ctimer startup_timer;
// static struct ctimer startup_timer;
/*---------------------------------------------------------------------------*/
static void
notify_ready(void *not_used)
{
enabled = SENSOR_STATUS_READY;
sensors_changed(&bmp_280_sensor);
// enabled = SENSOR_STATUS_READY;
// sensors_changed(&bmp_280_sensor);
}
/*---------------------------------------------------------------------------*/
static void
......@@ -165,7 +165,7 @@ init(void)
select_on_bus();
/* Read and store calibration data */
sensor_common_read_reg(ADDR_CALIB, calibration_data, CALIB_DATA_SIZE);
// sensor_common_read_reg(ADDR_CALIB, calibration_data, CALIB_DATA_SIZE);
/* Reset the sensor */
val = VAL_RESET_EXECUTE;
......@@ -205,12 +205,12 @@ read_data(uint8_t *data)
{
bool success;
select_on_bus();
success = sensor_common_read_reg(ADDR_PRESS_MSB, data, MEAS_DATA_SIZE);
if(!success) {
sensor_common_set_error_data(data, MEAS_DATA_SIZE);
}
// select_on_bus();
//
// success = sensor_common_read_reg(ADDR_PRESS_MSB, data, MEAS_DATA_SIZE);
// if(!success) {
// sensor_common_set_error_data(data, MEAS_DATA_SIZE);
// }
return success;
}
......@@ -226,62 +226,62 @@ read_data(uint8_t *data)
static void
convert(uint8_t *data, int32_t *temp, uint32_t *press)
{
int32_t utemp, upress;
bmp_280_calibration_t *p = (bmp_280_calibration_t *)calibration_data;
int32_t v_x1_u32r;
int32_t v_x2_u32r;
int32_t temperature;
uint32_t pressure;
/* Pressure */
upress = (int32_t)((((uint32_t)(data[0])) << 12)
| (((uint32_t)(data[1])) << 4) | ((uint32_t)data[2] >> 4));
/* Temperature */
utemp = (int32_t)((((uint32_t)(data[3])) << 12) | (((uint32_t)(data[4])) << 4)
| ((uint32_t)data[5] >> 4));
/* Compensate temperature */
v_x1_u32r = ((((utemp >> 3) - ((int32_t)p->dig_t1 << 1)))
* ((int32_t)p->dig_t2)) >> 11;
v_x2_u32r = (((((utemp >> 4) - ((int32_t)p->dig_t1))
* ((utemp >> 4) - ((int32_t)p->dig_t1))) >> 12)
* ((int32_t)p->dig_t3))
>> 14;
p->t_fine = v_x1_u32r + v_x2_u32r;
temperature = (p->t_fine * 5 + 128) >> 8;
*temp = temperature;
/* Compensate pressure */
v_x1_u32r = (((int32_t)p->t_fine) >> 1) - (int32_t)64000;
v_x2_u32r = (((v_x1_u32r >> 2) * (v_x1_u32r >> 2)) >> 11)
* ((int32_t)p->dig_p6);
v_x2_u32r = v_x2_u32r + ((v_x1_u32r * ((int32_t)p->dig_p5)) << 1);
v_x2_u32r = (v_x2_u32r >> 2) + (((int32_t)p->dig_p4) << 16);
v_x1_u32r =
(((p->dig_p3 * (((v_x1_u32r >> 2) * (v_x1_u32r >> 2)) >> 13)) >> 3)
+ ((((int32_t)p->dig_p2) * v_x1_u32r) >> 1)) >> 18;
v_x1_u32r = ((((32768 + v_x1_u32r)) * ((int32_t)p->dig_p1)) >> 15);
if(v_x1_u32r == 0) {
return; /* Avoid exception caused by division by zero */
}
pressure = (((uint32_t)(((int32_t)1048576) - upress) - (v_x2_u32r >> 12)))
* 3125;
if(pressure < 0x80000000) {
pressure = (pressure << 1) / ((uint32_t)v_x1_u32r);
} else {
pressure = (pressure / (uint32_t)v_x1_u32r) * 2;
}
v_x1_u32r = (((int32_t)p->dig_p9)
* ((int32_t)(((pressure >> 3) * (pressure >> 3)) >> 13))) >> 12;
v_x2_u32r = (((int32_t)(pressure >> 2)) * ((int32_t)p->dig_p8)) >> 13;
pressure = (uint32_t)((int32_t)pressure
+ ((v_x1_u32r + v_x2_u32r + p->dig_p7) >> 4));
*press = pressure;
// int32_t utemp, upress;
// bmp_280_calibration_t *p = (bmp_280_calibration_t *)calibration_data;
// int32_t v_x1_u32r;
// int32_t v_x2_u32r;
// int32_t temperature;
// uint32_t pressure;
//
// /* Pressure */
// upress = (int32_t)((((uint32_t)(data[0])) << 12)
// | (((uint32_t)(data[1])) << 4) | ((uint32_t)data[2] >> 4));
//
// /* Temperature */
// utemp = (int32_t)((((uint32_t)(data[3])) << 12) | (((uint32_t)(data[4])) << 4)
// | ((uint32_t)data[5] >> 4));
//
// /* Compensate temperature */
// v_x1_u32r = ((((utemp >> 3) - ((int32_t)p->dig_t1 << 1)))
// * ((int32_t)p->dig_t2)) >> 11;
// v_x2_u32r = (((((utemp >> 4) - ((int32_t)p->dig_t1))
// * ((utemp >> 4) - ((int32_t)p->dig_t1))) >> 12)
// * ((int32_t)p->dig_t3))
// >> 14;
// p->t_fine = v_x1_u32r + v_x2_u32r;
// temperature = (p->t_fine * 5 + 128) >> 8;
// *temp = temperature;
//
// /* Compensate pressure */
// v_x1_u32r = (((int32_t)p->t_fine) >> 1) - (int32_t)64000;
// v_x2_u32r = (((v_x1_u32r >> 2) * (v_x1_u32r >> 2)) >> 11)
// * ((int32_t)p->dig_p6);
// v_x2_u32r = v_x2_u32r + ((v_x1_u32r * ((int32_t)p->dig_p5)) << 1);
// v_x2_u32r = (v_x2_u32r >> 2) + (((int32_t)p->dig_p4) << 16);
// v_x1_u32r =
// (((p->dig_p3 * (((v_x1_u32r >> 2) * (v_x1_u32r >> 2)) >> 13)) >> 3)
// + ((((int32_t)p->dig_p2) * v_x1_u32r) >> 1)) >> 18;
// v_x1_u32r = ((((32768 + v_x1_u32r)) * ((int32_t)p->dig_p1)) >> 15);
//
// if(v_x1_u32r == 0) {
// return; /* Avoid exception caused by division by zero */
// }
//
// pressure = (((uint32_t)(((int32_t)1048576) - upress) - (v_x2_u32r >> 12)))
// * 3125;
// if(pressure < 0x80000000) {
// pressure = (pressure << 1) / ((uint32_t)v_x1_u32r);
// } else {
// pressure = (pressure / (uint32_t)v_x1_u32r) * 2;
// }
//
// v_x1_u32r = (((int32_t)p->dig_p9)
// * ((int32_t)(((pressure >> 3) * (pressure >> 3)) >> 13))) >> 12;
// v_x2_u32r = (((int32_t)(pressure >> 2)) * ((int32_t)p->dig_p8)) >> 13;
// pressure = (uint32_t)((int32_t)pressure
// + ((v_x1_u32r + v_x2_u32r + p->dig_p7) >> 4));
//
// *press = pressure;
}
/*---------------------------------------------------------------------------*/
/**
......@@ -293,38 +293,38 @@ static int
value(int type)
{
int rv;
int32_t temp = 0;
uint32_t pres = 0;
if(enabled != SENSOR_STATUS_READY) {
PRINTF("Sensor disabled or starting up (%d)\n", enabled);
return CC26XX_SENSOR_READING_ERROR;
}
if((type != BMP_280_SENSOR_TYPE_TEMP) && type != BMP_280_SENSOR_TYPE_PRESS) {
PRINTF("Invalid type\n");
return CC26XX_SENSOR_READING_ERROR;
} else {
memset(sensor_value, 0, SENSOR_DATA_BUF_SIZE);
rv = read_data(sensor_value);
if(rv == 0) {
return CC26XX_SENSOR_READING_ERROR;
}
PRINTF("val: %02x%02x%02x %02x%02x%02x\n",
sensor_value[0], sensor_value[1], sensor_value[2],
sensor_value[3], sensor_value[4], sensor_value[5]);
convert(sensor_value, &temp, &pres);
if(type == BMP_280_SENSOR_TYPE_TEMP) {
rv = (int)temp;
} else if(type == BMP_280_SENSOR_TYPE_PRESS) {
rv = (int)pres;
}
}
// int32_t temp = 0;
// uint32_t pres = 0;
//
// if(enabled != SENSOR_STATUS_READY) {
// PRINTF("Sensor disabled or starting up (%d)\n", enabled);
// return CC26XX_SENSOR_READING_ERROR;
// }
//
// if((type != BMP_280_SENSOR_TYPE_TEMP) && type != BMP_280_SENSOR_TYPE_PRESS) {
// PRINTF("Invalid type\n");
// return CC26XX_SENSOR_READING_ERROR;
// } else {
// memset(sensor_value, 0, SENSOR_DATA_BUF_SIZE);
//
// rv = read_data(sensor_value);
//
// if(rv == 0) {
// return CC26XX_SENSOR_READING_ERROR;
// }
//
// PRINTF("val: %02x%02x%02x %02x%02x%02x\n",
// sensor_value[0], sensor_value[1], sensor_value[2],
// sensor_value[3], sensor_value[4], sensor_value[5]);
//
// convert(sensor_value, &temp, &pres);
//
// if(type == BMP_280_SENSOR_TYPE_TEMP) {
// rv = (int)temp;
// } else if(type == BMP_280_SENSOR_TYPE_PRESS) {
// rv = (int)pres;
// }
// }
return rv;
}
/*---------------------------------------------------------------------------*/
......@@ -354,10 +354,10 @@ configure(int type, int enable)
}
if(enable) {
enable_sensor(1);
ctimer_set(&startup_timer, SENSOR_STARTUP_DELAY, notify_ready, NULL);
//ctimer_set(&startup_timer, SENSOR_STARTUP_DELAY, notify_ready, NULL);
enabled = SENSOR_STATUS_NOT_READY;
} else {
ctimer_stop(&startup_timer);
//ctimer_stop(&startup_timer);
enable_sensor(0);
enabled = SENSOR_STATUS_INITIALISED;
}
......
......@@ -113,7 +113,7 @@ static int enabled = HDC_1000_SENSOR_STATUS_DISABLED;
*/
#define SENSOR_STARTUP_DELAY 3
static struct ctimer startup_timer;
//static struct ctimer startup_timer;
/*---------------------------------------------------------------------------*/
/**
* \brief Initialise the humidity sensor driver
......@@ -157,22 +157,22 @@ static bool
read_data()
{
bool valid;
if(success) {
SENSOR_SELECT();
success = board_i2c_read((uint8_t *)&data, sizeof(data));
SENSOR_DESELECT();
/* Store temperature */
raw_temp = SWAP(data.temp);
/* Store humidity */
raw_hum = SWAP(data.hum);
}
valid = success;
success = true;
//
// if(success) {
// SENSOR_SELECT();
//
// success = board_i2c_read((uint8_t *)&data, sizeof(data));
// SENSOR_DESELECT();
//
// /* Store temperature */
// raw_temp = SWAP(data.temp);
//
// /* Store humidity */
// raw_hum = SWAP(data.hum);
// }
//
// valid = success;
// success = true;
return valid;
}
......@@ -185,22 +185,22 @@ read_data()
static void
convert(float *temp, float *hum)
{
/* Convert temperature to degrees C */
*temp = ((double)raw_temp / 65536) * 165 - 40;
/* Convert relative humidity to a %RH value */
*hum = ((double)raw_hum / 65536) * 100;
// /* Convert temperature to degrees C */
// *temp = ((double)raw_temp / 65536) * 165 - 40;
//
// /* Convert relative humidity to a %RH value */
// *hum = ((double)raw_hum / 65536) * 100;
}
/*---------------------------------------------------------------------------*/
static void
notify_ready(void *not_used)
{
enabled = HDC_1000_SENSOR_STATUS_READINGS_READY;
/* Latch readings */
read_data();
sensors_changed(&hdc_1000_sensor);
// enabled = HDC_1000_SENSOR_STATUS_READINGS_READY;
//
// /* Latch readings */
// read_data();
//
// sensors_changed(&hdc_1000_sensor);
}
/*---------------------------------------------------------------------------*/
/**
......@@ -212,29 +212,29 @@ static int
value(int type)
{
int rv;
float temp;
float hum;
if(enabled != HDC_1000_SENSOR_STATUS_READINGS_READY) {
PRINTF("Sensor disabled or starting up (%d)\n", enabled);
return CC26XX_SENSOR_READING_ERROR;
}
if((type != HDC_1000_SENSOR_TYPE_TEMP) &&
type != HDC_1000_SENSOR_TYPE_HUMID) {
PRINTF("Invalid type\n");
return CC26XX_SENSOR_READING_ERROR;
} else {
convert(&temp, &hum);
PRINTF("HDC: %04X %04X t=%d h=%d\n", raw_temp, raw_hum,
(int)(temp * 100), (int)(hum * 100));
if(type == HDC_1000_SENSOR_TYPE_TEMP) {
rv = (int)(temp * 100);
} else if(type == HDC_1000_SENSOR_TYPE_HUMID) {
rv = (int)(hum * 100);
}
}
// float temp;
// float hum;
//
// if(enabled != HDC_1000_SENSOR_STATUS_READINGS_READY) {
// PRINTF("Sensor disabled or starting up (%d)\n", enabled);
// return CC26XX_SENSOR_READING_ERROR;
// }
//
// if((type != HDC_1000_SENSOR_TYPE_TEMP) &&
// type != HDC_1000_SENSOR_TYPE_HUMID) {
// PRINTF("Invalid type\n");
// return CC26XX_SENSOR_READING_ERROR;
// } else {
// convert(&temp, &hum);
// PRINTF("HDC: %04X %04X t=%d h=%d\n", raw_temp, raw_hum,
// (int)(temp * 100), (int)(hum * 100));
//
// if(type == HDC_1000_SENSOR_TYPE_TEMP) {
// rv = (int)(temp * 100);
// } else if(type == HDC_1000_SENSOR_TYPE_HUMID) {
// rv = (int)(hum * 100);
// }
// }
return rv;
}
/*---------------------------------------------------------------------------*/
......@@ -266,12 +266,12 @@ configure(int type, int enable)
return HDC_1000_SENSOR_STATUS_DISABLED;
}
if(enable) {
start();
ctimer_set(&startup_timer, SENSOR_STARTUP_DELAY, notify_ready, NULL);
enabled = HDC_1000_SENSOR_STATUS_TAKING_READINGS;
// start();
// ctimer_set(&startup_timer, SENSOR_STARTUP_DELAY, notify_ready, NULL);
// enabled = HDC_1000_SENSOR_STATUS_TAKING_READINGS;
} else {
ctimer_stop(&startup_timer);
enabled = HDC_1000_SENSOR_STATUS_INITIALISED;
// ctimer_stop(&startup_timer);
// enabled = HDC_1000_SENSOR_STATUS_INITIALISED;
}
break;
default:
......
......@@ -222,7 +222,7 @@ static int elements = MPU_9250_SENSOR_TYPE_NONE;
/* 3 16-byte words for all sensor readings */
#define SENSOR_DATA_BUF_SIZE 3
static uint16_t sensor_value[SENSOR_DATA_BUF_SIZE];
// static uint16_t sensor_value[SENSOR_DATA_BUF_SIZE];
/*---------------------------------------------------------------------------*/
/*
* Wait SENSOR_BOOT_DELAY ticks for the sensor to boot and
......@@ -232,10 +232,10 @@ static uint16_t sensor_value[SENSOR_DATA_BUF_SIZE];
#define SENSOR_BOOT_DELAY 8
#define SENSOR_STARTUP_DELAY 5
static struct ctimer startup_timer;
// static struct ctimer startup_timer;
/*---------------------------------------------------------------------------*/
/* Wait for the MPU to have data ready */
rtimer_clock_t t0;
// rtimer_clock_t t0;
/*
* Wait timeout in rtimer ticks. This is just a random low number, since the
......@@ -288,22 +288,22 @@ sensor_wakeup(void)
static void
select_axes(void)
{
val = ~mpu_config;
SENSOR_SELECT();
sensor_common_write_reg(PWR_MGMT_2, &val, 1);
SENSOR_DESELECT();
// val = ~mpu_config;
// SENSOR_SELECT();
// sensor_common_write_reg(PWR_MGMT_2, &val, 1);
// SENSOR_DESELECT();
}
/*---------------------------------------------------------------------------*/
static void
convert_to_le(uint8_t *data, uint8_t len)
{
int i;
for(i = 0; i < len; i += 2) {
uint8_t tmp;
tmp = data[i];
data[i] = data[i + 1];
data[i + 1] = tmp;
}
// int i;
// for(i = 0; i < len; i += 2) {
// uint8_t tmp;
// tmp = data[i];
// data[i] = data[i + 1];
// data[i + 1] = tmp;
// }
}
/*---------------------------------------------------------------------------*/
/**
......@@ -316,22 +316,22 @@ acc_set_range(uint8_t new_range)
{
bool success;
if(new_range == acc_range) {
return true;
}
success = false;
acc_range_reg = (new_range << 3);
/* Apply the range */
SENSOR_SELECT();
success = sensor_common_write_reg(ACCEL_CONFIG, &acc_range_reg, 1);
SENSOR_DESELECT();
if(success) {
acc_range = new_range;
}
// if(new_range == acc_range) {
// return true;
// }
//
// success = false;
//
// acc_range_reg = (new_range << 3);
//
// /* Apply the range */
// SENSOR_SELECT();
// success = sensor_common_write_reg(ACCEL_CONFIG, &acc_range_reg, 1);
// SENSOR_DESELECT();
//
// if(success) {
// acc_range = new_range;
// }
return success;
}
......@@ -386,21 +386,21 @@ acc_read(uint16_t *data)
{
bool success;
if(interrupt_status & BIT_RAW_RDY_EN) {
/* Burst read of all accelerometer values */
SENSOR_SELECT();
success = sensor_common_read_reg(ACCEL_XOUT_H, (uint8_t *)data, DATA_SIZE);
SENSOR_DESELECT();
if(success) {
convert_to_le((uint8_t *)data, DATA_SIZE);
} else {
sensor_common_set_error_data((uint8_t *)data, DATA_SIZE);
}
} else {
/* Data not ready */
success = false;
}
// if(interrupt_status & BIT_RAW_RDY_EN) {
// /* Burst read of all accelerometer values */
// SENSOR_SELECT();
// success = sensor_common_read_reg(ACCEL_XOUT_H, (uint8_t *)data, DATA_SIZE);
// SENSOR_DESELECT();
//
// if(success) {
// convert_to_le((uint8_t *)data, DATA_SIZE);
// } else {
// sensor_common_set_error_data((uint8_t *)data, DATA_SIZE);
// }
// } else {
// /* Data not ready */
// success = false;
// }
return success;
}
......@@ -414,23 +414,23 @@ gyro_read(uint16_t *data)
{
bool success;
if(interrupt_status & BIT_RAW_RDY_EN) {
/* Select this sensor */
SENSOR_SELECT();
/* Burst read of all gyroscope values */
success = sensor_common_read_reg(GYRO_XOUT_H, (uint8_t *)data, DATA_SIZE);
if(success) {
convert_to_le((uint8_t *)data, DATA_SIZE);
} else {
sensor_common_set_error_data((uint8_t *)data, DATA_SIZE);
}
SENSOR_DESELECT();
} else {
success = false;
}
// if(interrupt_status & BIT_RAW_RDY_EN) {
// /* Select this sensor */
// SENSOR_SELECT();
//
// /* Burst read of all gyroscope values */
// success = sensor_common_read_reg(GYRO_XOUT_H, (uint8_t *)data, DATA_SIZE);
//
// if(success) {
// convert_to_le((uint8_t *)data, DATA_SIZE);
// } else {
// sensor_common_set_error_data((uint8_t *)data, DATA_SIZE);
// }
//
// SENSOR_DESELECT();
// } else {
// success = false;
// }
return success;
}
......@@ -445,27 +445,27 @@ acc_convert(int16_t raw_data)
{
float v = 0;
switch(acc_range) {
case ACC_RANGE_2G:
/* Calculate acceleration, unit G, range -2, +2 */
v = (raw_data * 1.0) / (32768 / 2);
break;
case ACC_RANGE_4G:
/* Calculate acceleration, unit G, range -4, +4 */
v = (raw_data * 1.0) / (32768 / 4);
break;
case ACC_RANGE_8G:
/* Calculate acceleration, unit G, range -8, +8 */
v = (raw_data * 1.0) / (32768 / 8);
break;
case ACC_RANGE_16G:
/* Calculate acceleration, unit G, range -16, +16 */
v = (raw_data * 1.0) / (32768 / 16);
break;
default:
v = 0;
break;
}
// switch(acc_range) {