Implement independent fitted/not fitted for BMEe1 & BMEe2

pull/4/head
bob 2018-06-23 11:17:23 +10:00
rodzic 1df211198f
commit 782d3205e0
4 zmienionych plików z 215 dodań i 210 usunięć

Wyświetl plik

@ -1,202 +1,202 @@
/***************************************************************************
This is a library for the BME280 humidity, temperature & pressure sensor
Designed specifically to work with the Adafruit BME280 Breakout
----> http://www.adafruit.com/products/2650
These sensors use I2C or SPI to communicate, 2 or 4 pins are required
to interface.
Adafruit invests time and resources providing this open source code,
please support Adafruit andopen-source hardware by purchasing products
from Adafruit!
Written by Limor Fried & Kevin Townsend for Adafruit Industries.
BSD license, all text above must be included in any redistribution
***************************************************************************/
#include "ch.h"
#include "hal.h"
#include "bme280.h"
#include "pi2c.h"
#include "ei2c.h"
#include <math.h>
#include "debug.h"
bool BME280_isAvailable(uint8_t id)
{
uint8_t val;
if(( (id == 0 && I2C_read8(0x77, BME280_REGISTER_CHIPID, &val))
|| (id >= 1 && eI2C_read8(id==1 ? 0x76 : 0x77, BME280_REGISTER_CHIPID, &val))
) && id < 3)
return val == 0x60;
else
return false;
}
/**
* Initializes BME280 and reads calibration data
* @handle Handle for the BME280 of type bme280_t
* @id ID of the BME280, 0: internal, 1 and 2: external
*/
void BME280_Init(bme280_t *handle, uint8_t id)
{
uint8_t tmp1;
uint8_t tmp2;
switch(id)
{
//Use internal I2C
case 0:
handle->i2c_read8 = &I2C_read8;
handle->i2c_read16 = &I2C_read16;
handle->i2c_read16_LE = &I2C_read16_LE;
handle->i2c_write8 = &I2C_write8;
handle->i2c_address = 0x77;
break;
//Use external I2C
case 1:
case 2:
handle->i2c_read8 = &eI2C_read8;
handle->i2c_read16 = &eI2C_read16;
handle->i2c_read16_LE = &eI2C_read16_LE;
handle->i2c_write8 = &eI2C_write8;
handle->i2c_address = id==1 ? 0x76 : 0x77;
break;
}
(*handle->i2c_read16_LE)(handle->i2c_address, BME280_REGISTER_DIG_T1, (uint16_t*)&handle->calib.dig_T1);
(*handle->i2c_read16_LE)(handle->i2c_address, BME280_REGISTER_DIG_T2, (uint16_t*)&handle->calib.dig_T2);
(*handle->i2c_read16_LE)(handle->i2c_address, BME280_REGISTER_DIG_T3, (uint16_t*)&handle->calib.dig_T3);
(*handle->i2c_read16_LE)(handle->i2c_address, BME280_REGISTER_DIG_P1, (uint16_t*)&handle->calib.dig_P1);
(*handle->i2c_read16_LE)(handle->i2c_address, BME280_REGISTER_DIG_P2, (uint16_t*)&handle->calib.dig_P2);
(*handle->i2c_read16_LE)(handle->i2c_address, BME280_REGISTER_DIG_P3, (uint16_t*)&handle->calib.dig_P3);
(*handle->i2c_read16_LE)(handle->i2c_address, BME280_REGISTER_DIG_P4, (uint16_t*)&handle->calib.dig_P4);
(*handle->i2c_read16_LE)(handle->i2c_address, BME280_REGISTER_DIG_P5, (uint16_t*)&handle->calib.dig_P5);
(*handle->i2c_read16_LE)(handle->i2c_address, BME280_REGISTER_DIG_P6, (uint16_t*)&handle->calib.dig_P6);
(*handle->i2c_read16_LE)(handle->i2c_address, BME280_REGISTER_DIG_P7, (uint16_t*)&handle->calib.dig_P7);
(*handle->i2c_read16_LE)(handle->i2c_address, BME280_REGISTER_DIG_P8, (uint16_t*)&handle->calib.dig_P8);
(*handle->i2c_read16_LE)(handle->i2c_address, BME280_REGISTER_DIG_P9, (uint16_t*)&handle->calib.dig_P9);
(*handle->i2c_read8)(handle->i2c_address, BME280_REGISTER_DIG_H1, &handle->calib.dig_H1);
(*handle->i2c_read16_LE)(handle->i2c_address, BME280_REGISTER_DIG_H2, (uint16_t*)&handle->calib.dig_H2);
(*handle->i2c_read8)(handle->i2c_address, BME280_REGISTER_DIG_H3, &handle->calib.dig_H3);
(*handle->i2c_read8)(handle->i2c_address, BME280_REGISTER_DIG_H4, &tmp1);
(*handle->i2c_read8)(handle->i2c_address, BME280_REGISTER_DIG_H5, &tmp2);
handle->calib.dig_H4 = (((int8_t)tmp1) << 4) | (tmp2 & 0x0F);
(*handle->i2c_read8)(handle->i2c_address, BME280_REGISTER_DIG_H6, &tmp1);
(*handle->i2c_read8)(handle->i2c_address, BME280_REGISTER_DIG_H5, &tmp2);
handle->calib.dig_H5 = (((int8_t)tmp1) << 4) | (tmp2 >> 4 & 0x0F);
(*handle->i2c_read8)(handle->i2c_address, BME280_REGISTER_DIG_H6, (uint8_t*)&handle->calib.dig_H6);
(*handle->i2c_write8)(handle->i2c_address, BME280_REGISTER_CONTROLHUMID, 0x03); // Set before CONTROL (DS 5.4.3)
(*handle->i2c_write8)(handle->i2c_address, BME280_REGISTER_CONTROL, 0x3F);
chThdSleep(TIME_MS2I(50)); // Wait for BME280
BME280_getTemperature(handle); // Set t_fine
}
/**
* Reads the temperature
* @return Temperature in degC * 100
*/
int16_t BME280_getTemperature(bme280_t *handle)
{
int32_t var1, var2, adc_T;
uint16_t tmp;
(*handle->i2c_read16)(handle->i2c_address, BME280_REGISTER_TEMPDATA, &tmp);
adc_T = tmp;
(*handle->i2c_read8)(handle->i2c_address, BME280_REGISTER_TEMPDATA+2, (uint8_t*)&tmp);
adc_T <<= 8;
adc_T |= tmp & 0xFF;
adc_T >>= 4;
var1 = ((((adc_T>>3) - ((int32_t)handle->calib.dig_T1 <<1))) * ((int32_t)handle->calib.dig_T2)) >> 11;
var2 = (((((adc_T>>4) - ((int32_t)handle->calib.dig_T1)) * ((adc_T>>4) - ((int32_t)handle->calib.dig_T1))) >> 12) * ((int32_t)handle->calib.dig_T3)) >> 14;
handle->t_fine = var1 + var2;
return (handle->t_fine * 5 + 128) >> 8;
}
/**
* Reads the barometric pressure
* @param Values to be sampled
* @return Pressure in Pa * 10
*/
uint32_t BME280_getPressure(bme280_t *handle, uint16_t means) {
int64_t var1, var2, p;
uint16_t tmp;
uint64_t sum = 0;
for(uint16_t i=0; i<means; i++) {
int32_t adc_P;
(*handle->i2c_read16)(handle->i2c_address, BME280_REGISTER_PRESSUREDATA, &tmp);
adc_P = tmp;
(*handle->i2c_read8)(handle->i2c_address, BME280_REGISTER_PRESSUREDATA+2, (uint8_t*)&tmp);
adc_P <<= 8;
adc_P |= tmp & 0xFF;
adc_P >>= 4;
var1 = ((int64_t)handle->t_fine) - 128000;
var2 = var1 * var1 * (int64_t)handle->calib.dig_P6;
var2 = var2 + ((var1*(int64_t)handle->calib.dig_P5)<<17);
var2 = var2 + (((int64_t)handle->calib.dig_P4)<<35);
var1 = ((var1 * var1 * (int64_t)handle->calib.dig_P3)>>8) + ((var1 * (int64_t)handle->calib.dig_P2)<<12);
var1 = (((((int64_t)1)<<47)+var1))*((int64_t)handle->calib.dig_P1)>>33;
if (var1 == 0)
return 0; // avoid exception caused by division by zero
p = 1048576 - adc_P;
p = (((p<<31) - var2)*3125) / var1;
var1 = (((int64_t)handle->calib.dig_P9) * (p>>13) * (p>>13)) >> 25;
var2 = (((int64_t)handle->calib.dig_P8) * p) >> 19;
sum += ((p + var1 + var2) >> 8) + (((int64_t)handle->calib.dig_P7)<<4);
}
return sum/(means*26);
}
/**
* Reads the relative humidity
* @return rel. humidity in % * 10
*/
uint8_t BME280_getHumidity(bme280_t *handle) {
int32_t adc_H;
uint16_t tmp;
(*handle->i2c_read16)(handle->i2c_address, BME280_REGISTER_HUMIDDATA, &tmp);
adc_H = tmp;
int32_t v_x1_u32r;
v_x1_u32r = (handle->t_fine - ((int32_t)76800));
v_x1_u32r = (((((adc_H << 14) - (((int32_t)handle->calib.dig_H4) << 20) -
(((int32_t)handle->calib.dig_H5) * v_x1_u32r)) + ((int32_t)16384)) >> 15) *
(((((((v_x1_u32r * ((int32_t)handle->calib.dig_H6)) >> 10) *
(((v_x1_u32r * ((int32_t)handle->calib.dig_H3)) >> 11) + ((int32_t)32768))) >> 10) +
((int32_t)2097152)) * ((int32_t)handle->calib.dig_H2) + 8192) >> 14));
v_x1_u32r = (v_x1_u32r - (((((v_x1_u32r >> 15) * (v_x1_u32r >> 15)) >> 7) * ((int32_t)handle->calib.dig_H1)) >> 4));
v_x1_u32r = (v_x1_u32r < 0) ? 0 : v_x1_u32r;
v_x1_u32r = (v_x1_u32r > 419430400) ? 419430400 : v_x1_u32r;
float h = (v_x1_u32r>>12);
return h / 1020;
}
/**
* Calculates the altitude (in meters) from the specified atmospheric
* pressure (in Pa*10), and sea-level pressure (in Pa).
* @param seaLevel Sea-level pressure in Pa
* @param atmospheric Airpressure in Pa*10
* @return altitude in cm
*/
int32_t BME280_getAltitude(uint32_t seaLevel, uint32_t atmospheric)
{
return (1.0-pow((float)atmospheric/(float)(seaLevel*10), 1.0/5.255)) * 288150000 / 65;
}
/***************************************************************************
This is a library for the BME280 humidity, temperature & pressure sensor
Designed specifically to work with the Adafruit BME280 Breakout
----> http://www.adafruit.com/products/2650
These sensors use I2C or SPI to communicate, 2 or 4 pins are required
to interface.
Adafruit invests time and resources providing this open source code,
please support Adafruit andopen-source hardware by purchasing products
from Adafruit!
Written by Limor Fried & Kevin Townsend for Adafruit Industries.
BSD license, all text above must be included in any redistribution
***************************************************************************/
#include "ch.h"
#include "hal.h"
#include "bme280.h"
#include "pi2c.h"
#include "ei2c.h"
#include <math.h>
#include "debug.h"
bool BME280_isAvailable(uint8_t id)
{
uint8_t val;
if(( (id == 0 && I2C_read8(0x77, BME280_REGISTER_CHIPID, &val))
|| (id >= 1 && eI2C_read8(id==1 ? 0x76 : 0x77, BME280_REGISTER_CHIPID, &val))
) && id < 3)
return val == 0x60;
else
return false;
}
/**
* Initializes BME280 and reads calibration data
* @handle Handle for the BME280 of type bme280_t
* @id ID of the BME280, 0: internal, 1 and 2: external
*/
void BME280_Init(bme280_t *handle, uint8_t id)
{
uint8_t tmp1;
uint8_t tmp2;
switch(id)
{
//Use internal I2C
case 0:
handle->i2c_read8 = &I2C_read8;
handle->i2c_read16 = &I2C_read16;
handle->i2c_read16_LE = &I2C_read16_LE;
handle->i2c_write8 = &I2C_write8;
handle->i2c_address = 0x77;
break;
//Use external I2C
case 1:
case 2:
handle->i2c_read8 = &eI2C_read8;
handle->i2c_read16 = &eI2C_read16;
handle->i2c_read16_LE = &eI2C_read16_LE;
handle->i2c_write8 = &eI2C_write8;
handle->i2c_address = id==1 ? 0x77 : 0x76;
break;
}
(*handle->i2c_read16_LE)(handle->i2c_address, BME280_REGISTER_DIG_T1, (uint16_t*)&handle->calib.dig_T1);
(*handle->i2c_read16_LE)(handle->i2c_address, BME280_REGISTER_DIG_T2, (uint16_t*)&handle->calib.dig_T2);
(*handle->i2c_read16_LE)(handle->i2c_address, BME280_REGISTER_DIG_T3, (uint16_t*)&handle->calib.dig_T3);
(*handle->i2c_read16_LE)(handle->i2c_address, BME280_REGISTER_DIG_P1, (uint16_t*)&handle->calib.dig_P1);
(*handle->i2c_read16_LE)(handle->i2c_address, BME280_REGISTER_DIG_P2, (uint16_t*)&handle->calib.dig_P2);
(*handle->i2c_read16_LE)(handle->i2c_address, BME280_REGISTER_DIG_P3, (uint16_t*)&handle->calib.dig_P3);
(*handle->i2c_read16_LE)(handle->i2c_address, BME280_REGISTER_DIG_P4, (uint16_t*)&handle->calib.dig_P4);
(*handle->i2c_read16_LE)(handle->i2c_address, BME280_REGISTER_DIG_P5, (uint16_t*)&handle->calib.dig_P5);
(*handle->i2c_read16_LE)(handle->i2c_address, BME280_REGISTER_DIG_P6, (uint16_t*)&handle->calib.dig_P6);
(*handle->i2c_read16_LE)(handle->i2c_address, BME280_REGISTER_DIG_P7, (uint16_t*)&handle->calib.dig_P7);
(*handle->i2c_read16_LE)(handle->i2c_address, BME280_REGISTER_DIG_P8, (uint16_t*)&handle->calib.dig_P8);
(*handle->i2c_read16_LE)(handle->i2c_address, BME280_REGISTER_DIG_P9, (uint16_t*)&handle->calib.dig_P9);
(*handle->i2c_read8)(handle->i2c_address, BME280_REGISTER_DIG_H1, &handle->calib.dig_H1);
(*handle->i2c_read16_LE)(handle->i2c_address, BME280_REGISTER_DIG_H2, (uint16_t*)&handle->calib.dig_H2);
(*handle->i2c_read8)(handle->i2c_address, BME280_REGISTER_DIG_H3, &handle->calib.dig_H3);
(*handle->i2c_read8)(handle->i2c_address, BME280_REGISTER_DIG_H4, &tmp1);
(*handle->i2c_read8)(handle->i2c_address, BME280_REGISTER_DIG_H5, &tmp2);
handle->calib.dig_H4 = (((int8_t)tmp1) << 4) | (tmp2 & 0x0F);
(*handle->i2c_read8)(handle->i2c_address, BME280_REGISTER_DIG_H6, &tmp1);
(*handle->i2c_read8)(handle->i2c_address, BME280_REGISTER_DIG_H5, &tmp2);
handle->calib.dig_H5 = (((int8_t)tmp1) << 4) | (tmp2 >> 4 & 0x0F);
(*handle->i2c_read8)(handle->i2c_address, BME280_REGISTER_DIG_H6, (uint8_t*)&handle->calib.dig_H6);
(*handle->i2c_write8)(handle->i2c_address, BME280_REGISTER_CONTROLHUMID, 0x03); // Set before CONTROL (DS 5.4.3)
(*handle->i2c_write8)(handle->i2c_address, BME280_REGISTER_CONTROL, 0x3F);
chThdSleep(TIME_MS2I(50)); // Wait for BME280
BME280_getTemperature(handle); // Set t_fine
}
/**
* Reads the temperature
* @return Temperature in degC * 100
*/
int16_t BME280_getTemperature(bme280_t *handle)
{
int32_t var1, var2, adc_T;
uint16_t tmp;
(*handle->i2c_read16)(handle->i2c_address, BME280_REGISTER_TEMPDATA, &tmp);
adc_T = tmp;
(*handle->i2c_read8)(handle->i2c_address, BME280_REGISTER_TEMPDATA+2, (uint8_t*)&tmp);
adc_T <<= 8;
adc_T |= tmp & 0xFF;
adc_T >>= 4;
var1 = ((((adc_T>>3) - ((int32_t)handle->calib.dig_T1 <<1))) * ((int32_t)handle->calib.dig_T2)) >> 11;
var2 = (((((adc_T>>4) - ((int32_t)handle->calib.dig_T1)) * ((adc_T>>4) - ((int32_t)handle->calib.dig_T1))) >> 12) * ((int32_t)handle->calib.dig_T3)) >> 14;
handle->t_fine = var1 + var2;
return (handle->t_fine * 5 + 128) >> 8;
}
/**
* Reads the barometric pressure
* @param Values to be sampled
* @return Pressure in Pa * 10
*/
uint32_t BME280_getPressure(bme280_t *handle, uint16_t means) {
int64_t var1, var2, p;
uint16_t tmp;
uint64_t sum = 0;
for(uint16_t i=0; i<means; i++) {
int32_t adc_P;
(*handle->i2c_read16)(handle->i2c_address, BME280_REGISTER_PRESSUREDATA, &tmp);
adc_P = tmp;
(*handle->i2c_read8)(handle->i2c_address, BME280_REGISTER_PRESSUREDATA+2, (uint8_t*)&tmp);
adc_P <<= 8;
adc_P |= tmp & 0xFF;
adc_P >>= 4;
var1 = ((int64_t)handle->t_fine) - 128000;
var2 = var1 * var1 * (int64_t)handle->calib.dig_P6;
var2 = var2 + ((var1*(int64_t)handle->calib.dig_P5)<<17);
var2 = var2 + (((int64_t)handle->calib.dig_P4)<<35);
var1 = ((var1 * var1 * (int64_t)handle->calib.dig_P3)>>8) + ((var1 * (int64_t)handle->calib.dig_P2)<<12);
var1 = (((((int64_t)1)<<47)+var1))*((int64_t)handle->calib.dig_P1)>>33;
if (var1 == 0)
return 0; // avoid exception caused by division by zero
p = 1048576 - adc_P;
p = (((p<<31) - var2)*3125) / var1;
var1 = (((int64_t)handle->calib.dig_P9) * (p>>13) * (p>>13)) >> 25;
var2 = (((int64_t)handle->calib.dig_P8) * p) >> 19;
sum += ((p + var1 + var2) >> 8) + (((int64_t)handle->calib.dig_P7)<<4);
}
return sum/(means*26);
}
/**
* Reads the relative humidity
* @return rel. humidity in % * 10
*/
uint8_t BME280_getHumidity(bme280_t *handle) {
int32_t adc_H;
uint16_t tmp;
(*handle->i2c_read16)(handle->i2c_address, BME280_REGISTER_HUMIDDATA, &tmp);
adc_H = tmp;
int32_t v_x1_u32r;
v_x1_u32r = (handle->t_fine - ((int32_t)76800));
v_x1_u32r = (((((adc_H << 14) - (((int32_t)handle->calib.dig_H4) << 20) -
(((int32_t)handle->calib.dig_H5) * v_x1_u32r)) + ((int32_t)16384)) >> 15) *
(((((((v_x1_u32r * ((int32_t)handle->calib.dig_H6)) >> 10) *
(((v_x1_u32r * ((int32_t)handle->calib.dig_H3)) >> 11) + ((int32_t)32768))) >> 10) +
((int32_t)2097152)) * ((int32_t)handle->calib.dig_H2) + 8192) >> 14));
v_x1_u32r = (v_x1_u32r - (((((v_x1_u32r >> 15) * (v_x1_u32r >> 15)) >> 7) * ((int32_t)handle->calib.dig_H1)) >> 4));
v_x1_u32r = (v_x1_u32r < 0) ? 0 : v_x1_u32r;
v_x1_u32r = (v_x1_u32r > 419430400) ? 419430400 : v_x1_u32r;
float h = (v_x1_u32r>>12);
return h / 1020;
}
/**
* Calculates the altitude (in meters) from the specified atmospheric
* pressure (in Pa*10), and sea-level pressure (in Pa).
* @param seaLevel Sea-level pressure in Pa
* @param atmospheric Airpressure in Pa*10
* @return altitude in cm
*/
int32_t BME280_getAltitude(uint32_t seaLevel, uint32_t atmospheric)
{
return (1.0-pow((float)atmospheric/(float)(seaLevel*10), 1.0/5.255)) * 288150000 / 65;
}

Wyświetl plik

@ -263,7 +263,7 @@ void getSensors(dataPoint_t* tp) {
tp->sen_i1_hum = BME280_getHumidity(&handle);
tp->sen_i1_temp = BME280_getTemperature(&handle);
} else { // No internal BME280 found
TRACE_ERROR("COLL > Internal BME280 I1 not found");
TRACE_ERROR("COLL > Internal BME280 I1 not operational");
tp->sen_i1_press = 0;
tp->sen_i1_hum = 0;
tp->sen_i1_temp = 0;
@ -271,6 +271,7 @@ void getSensors(dataPoint_t* tp) {
}
#if ENABLE_EXTERNAL_I2C == TRUE
#if BMEE1_IS_FITTED == TRUE
// External BME280 Sensor 1
if(BME280_isAvailable(BME280_E1)) {
BME280_Init(&handle, BME280_E1);
@ -278,13 +279,17 @@ void getSensors(dataPoint_t* tp) {
tp->sen_e1_hum = BME280_getHumidity(&handle);
tp->sen_e1_temp = BME280_getTemperature(&handle);
} else { // No external BME280 found
TRACE_WARN("COLL > External BME280 E1 not found");
TRACE_WARN("COLL > External BME280 E1 not operational");
tp->sen_e1_press = 0;
tp->sen_e1_hum = 0;
tp->sen_e1_temp = 0;
bme280_error |= 0x4;
}
#else
bme280_error |= 0x8;
#endif
#if BMEE2_IS_FITTED == TRUE
// External BME280 Sensor 2
if(BME280_isAvailable(BME280_E2)) {
BME280_Init(&handle, BME280_E2);
@ -292,12 +297,15 @@ void getSensors(dataPoint_t* tp) {
tp->sen_e2_hum = BME280_getHumidity(&handle);
tp->sen_e2_temp = BME280_getTemperature(&handle);
} else { // No external BME280 found
TRACE_WARN("COLL > External BME280 E2 not found");
TRACE_WARN("COLL > External BME280 E2 not operational");
tp->sen_e2_press = 0;
tp->sen_e2_hum = 0;
tp->sen_e2_temp = 0;
bme280_error |= 0x10;
}
#else
bme280_error |= 0x20;
#endif
#else
/* Set status to "not fitted" for E1 & E2. */
bme280_error |= 0x28;

Wyświetl plik

@ -24,7 +24,8 @@
#define BMEE2_STATUS_SHIFT BMEI1_STATUS_SHIFT + BME_STATUS)BITS
#define BMEE2_STATUS_MASK (BME_STATUS_MASK << BMEE2_STATUS_SHIFT)
#define USE_NEW_COLLECTOR TRUE
#define BMEE1_IS_FITTED TRUE
#define BMEE2_IS_FITTED FALSE
typedef enum {
GPS_LOCKED1, // The GPS is locked, the GPS has been switched off

Wyświetl plik

@ -41,16 +41,12 @@ THD_FUNCTION(bcnThread, arg) {
pktDisplayFrequencyCode(conf->radio_conf.freq, code_s, sizeof(code_s));
TRACE_INFO("POS > Do module BEACON cycle for %s on %s",
conf->call, code_s);
#if USE_NEW_COLLECTOR == TRUE
extern thread_t *collector_thd;
/*
* Pass pointer to beacon config to the collector thread.
*/
dataPoint_t * dataPoint =
(dataPoint_t *)chMsgSend(collector_thd, (msg_t)conf);
#else
dataPoint_t* dataPoint = getLastDataPoint();
#endif
if(!p_sleep(&conf->beacon.sleep_conf)) {
if(!isPositionValid(dataPoint)) {