Skip to content

Commit

Permalink
baro fix approach
Browse files Browse the repository at this point in the history
  • Loading branch information
rtlopez committed Jul 24, 2023
1 parent d6aacaa commit e6abdc9
Show file tree
Hide file tree
Showing 4 changed files with 57 additions and 28 deletions.
51 changes: 37 additions & 14 deletions lib/Espfc/src/Device/BaroBMP280.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,21 @@
#define BMP280_WHOAMI_REG 0xD0
#define BMP280_VERSION_REG 0xD1
#define BMP280_RESET_REG 0xE0
#define BMP280_RESET_VAL 0xB6

#define BMP280_CALIB_REG 0x88
#define BMP280_CAL26_REG 0xE1 // R calibration stored in 0xE1-0xF0

#define BMP280_STATUS_REG 0xF3
#define BMP280_CONTROL_REG 0xF4
#define BMP280_CONFIG_REG 0xF5
#define BMP280_PRESSURE_REG 0xF7
#define BMP280_TEMPERATURE_REG 0xFA

#define BMP280_OSPS_T_X1 (1 << 5)
#define BMP280_OSPS_P_X1 (1 << 2)
#define BMP280_MODE_NORMAL 0x03

namespace Espfc {

namespace Device {
Expand All @@ -40,13 +46,13 @@ class BaroBMP280: public BaroDevice
int16_t dig_P7;
int16_t dig_P8;
int16_t dig_P9;
uint8_t dig_H1;
/*uint8_t dig_H1;
int16_t dig_H2;
uint8_t dig_H3;
int16_t dig_H4;
int16_t dig_H5;
int8_t dig_H6;
};
int8_t dig_H6;*/
} __attribute__ ((__packed__));

int begin(BusDevice * bus) override
{
Expand All @@ -59,16 +65,17 @@ class BaroBMP280: public BaroDevice

if(!testConnection()) return 0;

_bus->writeByte(_addr, BMP280_RESET_REG, 0xB6); // device reset
delay(10);
readReg(BMP280_CALIB_REG, (uint8_t*)&_cal, sizeof(CalibrationData)); // read callibration

writeReg(BMP280_RESET_REG, BMP280_RESET_VAL); // device reset
delay(100);

writeReg(BMP280_CONTROL_REG, BMP280_OSPS_T_X1 | BMP280_OSPS_P_X1 | BMP280_MODE_NORMAL); // set sampling mode

_bus->read(_addr, BMP280_CALIB_REG, sizeof(CalibrationData), (uint8_t*)&_cal);
writeReg(BMP280_CONFIG_REG, 0); // set minimal standby and turn off IIR filter
delay(100);

uint8_t conf = 0;
conf |= (1 << 5); // osps_t: x1
conf |= (1 << 2); // osps_p: x1
conf |= 3; // mode: normal;
_bus->writeByte(_addr, BMP280_CONTROL_REG, conf);
readReg8(BMP280_STATUS_REG);

return 1;
}
Expand Down Expand Up @@ -131,19 +138,35 @@ class BaroBMP280: public BaroDevice

bool testConnection() override
{
uint8_t whoami = 0;
_bus->readByte(_addr, BMP280_WHOAMI_REG, &whoami);
uint8_t whoami = readReg8(BMP280_WHOAMI_REG);
return whoami == BMP280_WHOAMI_ID;
}

protected:
uint8_t readReg8(uint8_t reg)
{
uint8_t buffer = 0;
_bus->read(_addr, reg, 1, &buffer);
return buffer;
}

int32_t readReg(uint8_t reg)
{
uint8_t buffer[3];
uint8_t buffer[3] = {0, 0, 0};
_bus->read(_addr, reg, 3, buffer);
return buffer[2] | (buffer[1] << 8) | (buffer[0] << 16);
}

int32_t readReg(uint8_t reg, uint8_t * buffer, uint8_t length)
{
return _bus->read(_addr, reg, length, buffer);
}

int8_t writeReg(uint8_t reg, uint8_t val)
{
return _bus->write(_addr, reg, 1, &val);
}

int8_t _mode;
int32_t _t_fine;
CalibrationData _cal;
Expand Down
30 changes: 16 additions & 14 deletions lib/Espfc/src/Device/BusSPI.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,11 @@ class BusSPI: public BusDevice
public:
BusSPI(ESPFC_SPI_0_DEV_T& spi): _dev(spi) {}

static const uint8_t SPI_READ = 0x80;
static const uint8_t SPI_READ = 0x80;
static const uint8_t SPI_WRITE = 0x7f;

static const uint32_t SPI_SPEED_NORMAL = 1000000;
static const uint32_t SPI_SPEED_FAST = 10000000;
static const uint32_t SPI_SPEED_NORMAL = 1000000;
static const uint32_t SPI_SPEED_FAST = 10000000;

BusType getType() const override { return BUS_SPI; }

Expand All @@ -30,38 +31,39 @@ class BusSPI: public BusDevice

int8_t read(uint8_t devAddr, uint8_t regAddr, uint8_t length, uint8_t *data, uint16_t timeout = ESPFC_BUS_TIMEOUT) override
{
//D("spi:r", regAddr, length, *data);
//D("spi:r", regAddr, length);
transfer(devAddr, regAddr | SPI_READ, length, NULL, data, SPI_SPEED_NORMAL);
return length;
}

int8_t readFast(uint8_t devAddr, uint8_t regAddr, uint8_t length, uint8_t *data, uint16_t timeout = ESPFC_BUS_TIMEOUT) override
{
//D("spi:r", regAddr, length, *data);
//D("spi:r", regAddr, length);
transfer(devAddr, regAddr | SPI_READ, length, NULL, data, SPI_SPEED_FAST);
return length;
}

bool write(uint8_t devAddr, uint8_t regAddr, uint8_t length, uint8_t* data) override
{
//D("spi:w", regAddr, length, *data);
transfer(devAddr, regAddr, length, data, NULL, SPI_SPEED_NORMAL);
transfer(devAddr, regAddr & SPI_WRITE, length, data, NULL, SPI_SPEED_NORMAL);
return true;
}

private:
void transfer(uint8_t devAddr, uint8_t regAddr, uint8_t length, uint8_t *in, uint8_t *out, uint32_t speed)
{
digitalWrite(devAddr, LOW);
_dev.beginTransaction(SPISettings(speed, MSBFIRST, SPI_MODE0));
_dev.transfer(regAddr); // specify the starting register address
for(uint8_t i = 0; i < length; i++)
{
uint8_t v = _dev.transfer(in ? in[i] : 0);
if(out) out[i] = v; // write received data
}
_dev.endTransaction();
digitalWrite(devAddr, LOW);
#if defined (ARCH_RP2040)
_dev.transfer(regAddr);
_dev.transfer(in, out, length);
#else
_dev.write(regAddr);
_dev.transferBytes(in, out, length);
#endif
digitalWrite(devAddr, HIGH);
_dev.endTransaction();
}
ESPFC_SPI_0_DEV_T& _dev;
};
Expand Down
3 changes: 3 additions & 0 deletions lib/Espfc/src/Hardware.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,9 @@

namespace {
#if defined(ESPFC_SPI_0)
#ifdef ESP32
static SPIClass SPI1(VSPI);
#endif
static Espfc::Device::BusSPI spiBus(ESPFC_SPI_0_DEV);
#endif
#if defined(ESPFC_I2C_0)
Expand Down
1 change: 1 addition & 0 deletions lib/Espfc/src/Target/TargetESP32.h
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@
#define SERIAL_TX_FIFO_SIZE 0xFF

#define ESPFC_SPI_0
#define ESPFC_SPI_0_DEV SPI1
#define ESPFC_SPI_0_SCK 18
#define ESPFC_SPI_0_MOSI 23
#define ESPFC_SPI_0_MISO 19
Expand Down

0 comments on commit e6abdc9

Please sign in to comment.