This example demonstrates the FlexSense Device interfacing with a BME280 sensor using the I2C interface on the FlexSense External Interface Cable (Only implement I2C communication with a cable length of 1m or less). The example operates as follows:
#include <stdio.h>
#include <string.h>
#include "flex.h"
#define APPLICATION_NAME "I2C BME280 Example"
#define MESSAGES_PER_DAY (4)
#define BME280_I2C_ADDRESS (0x77)
#define BME280_ID (0x60)
#define BME280_INIT_VALUE (0x00)
#define BME280_SUCCESS_VALUE (0)
#define BME280_FAIL_VALUE (-1)
#define BME280_REGISTER_READ_1MS_DELAY (1)
#define BME280_ID_READ_COUNT_MAX (5)
#define BME280_REG_ID (0xD0)
#define BME280_REG_CONFIG (0xF5)
#define BME280_REG_CTRL_MEAS (0xF4)
#define BME280_REG_HUMIDITY (0xF2)
#define BME280_REG_HUMIDITY_LSB (0xFE)
#define BME280_REG_HUMIDITY_MSB (0xFD)
#define BME280_TEMPERATURE_CONFIG_RESERVED_MASK (0x02)
#define BME280_HUMIDITY_CONFIG_RESERVED_MASK (0xF8)
#define BME280_TEMPERATURE_CONFIG (0x00)
#define BME280_HUMIDITY_CONFIG (0x01)
#define BME280_ACQUISITION_CONFIG (0x21)
typedef struct {
uint16_t sequence_number;
uint16_t humidity;
uint32_t time;
static int ReadRegister8(uint8_t reg) {
uint8_t rx;
if (
FLEX_ExtI2CRead(BME280_I2C_ADDRESS, ®,
sizeof(reg), &rx,
sizeof(rx)) == 0) {
return rx;
}
return BME280_FAIL_VALUE;
}
static int WriteRegister8(uint8_t reg, uint8_t value) {
uint8_t tx[2];
tx[0] = reg;
tx[1] = value;
return BME280_SUCCESS_VALUE;
}
return BME280_FAIL_VALUE;
}
static int SetTemperatureConfig(void) {
int currentValue = ReadRegister8(BME280_REG_CONFIG);
if (currentValue == BME280_FAIL_VALUE) {
return BME280_FAIL_VALUE;
}
uint8_t desiredValue =
(currentValue & BME280_TEMPERATURE_CONFIG_RESERVED_MASK) | BME280_TEMPERATURE_CONFIG;
return WriteRegister8(BME280_REG_CONFIG, desiredValue);
}
static int SetHumidityConfig() {
int currentValue = ReadRegister8(BME280_REG_HUMIDITY);
if (currentValue == BME280_FAIL_VALUE) {
return BME280_FAIL_VALUE;
}
uint8_t humidityConfig =
(currentValue & BME280_HUMIDITY_CONFIG_RESERVED_MASK) | BME280_HUMIDITY_CONFIG;
return WriteRegister8(BME280_REG_HUMIDITY, humidityConfig);
}
static int SetDataAcquisition() {
return WriteRegister8(BME280_REG_HUMIDITY, BME280_ACQUISITION_CONFIG);
}
static time_t ReadHumidity() {
static uint16_t sequence_number = 0;
if (SetDataAcquisition() == BME280_FAIL_VALUE) {
printf("Failed to trigger a read!\n");
}
message message;
int humidityLsb = ReadRegister8(BME280_REG_HUMIDITY_LSB);
int humidityMsb = ReadRegister8(BME280_REG_HUMIDITY_MSB);
if (humidityLsb == BME280_FAIL_VALUE || humidityMsb == BME280_FAIL_VALUE) {
printf("Failed to read humidity!\n");
}
message.sequence_number = sequence_number++;
message.humidity = (humidityMsb << 8) | humidityLsb;
printf("Scheduled message: Uncompensated Humidity: %u Time: %lu Seq Num: %u\n",
message.humidity / 1024, message.time, message.sequence_number);
}
static int Init(void) {
int retVal = BME280_FAIL_VALUE;
uint8_t deviceid = BME280_INIT_VALUE;
uint8_t idReadCount = BME280_ID_READ_COUNT_MAX;
while (idReadCount > 0) {
deviceid = ReadRegister8(BME280_REG_ID);
if (deviceid == BME280_ID) {
break;
}
idReadCount--;
}
retVal = (idReadCount == BME280_INIT_VALUE) ? BME280_FAIL_VALUE : BME280_SUCCESS_VALUE;
if (retVal == BME280_FAIL_VALUE) {
retVal += SetTemperatureConfig();
retVal += SetHumidityConfig();
retVal += SetDataAcquisition();
}
return retVal;
}
void FLEX_AppInit() {
printf("%s\n", APPLICATION_NAME);
if (Init() == BME280_SUCCESS_VALUE) {
printf("Sensor Initialised.\n");
} else {
printf("Failed to initialise the sensor!\n");
}
}