Skip to content

Commit

Permalink
I2C fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
rtlopez committed Jul 27, 2023
1 parent add5b6b commit 909167f
Show file tree
Hide file tree
Showing 9 changed files with 17 additions and 17 deletions.
3 changes: 2 additions & 1 deletion lib/EspWire/src/EspWire.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -54,10 +54,11 @@ EspTwoWire::EspTwoWire(){}

// Public Methods //////////////////////////////////////////////////////////////

void EspTwoWire::begin(int sda, int scl){
void EspTwoWire::begin(int sda, int scl, uint32_t frequency){
default_sda_pin = sda;
default_scl_pin = scl;
esp_twi_init(sda, scl);
if(frequency) setClock(frequency);
flush();
}

Expand Down
2 changes: 1 addition & 1 deletion lib/EspWire/src/EspWire.h
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ class EspTwoWire : public Stream
static void onReceiveService(uint8_t*, int);
public:
EspTwoWire();
void begin(int sda, int scl);
void begin(int sda, int scl, uint32_t frequency = 0);
void pins(int sda, int scl) __attribute__((deprecated)); // use begin(sda, scl) in new code
void begin();
void begin(uint8_t);
Expand Down
8 changes: 4 additions & 4 deletions lib/Espfc/src/Device/BaroBMP085.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
#include "Debug_Espfc.h"

#define BMP085_DEFAULT_ADDRESS 0x77
#define BMP085_WHOAMI_ID 0x55

#define BMP085_CALIB_REG 0xAA
#define BMP085_WHOAMI_REG 0xD0
Expand All @@ -19,7 +20,6 @@
#define BMP085_MEASURE_P2 0xB4 // pressure measurement oversampling x4
#define BMP085_MEASURE_P3 0xF4 // pressure measurement oversampling x8

#define BMP085_WHOAMI_ID 0x55

namespace Espfc {

Expand All @@ -40,7 +40,7 @@ class BaroBMP085: public BaroDevice
int16_t mb;
int16_t mc;
int16_t md;
};
} __attribute__ ((__packed__));

int begin(BusDevice * bus) override
{
Expand Down Expand Up @@ -77,8 +77,8 @@ class BaroBMP085: public BaroDevice

int32_t x1 = ((ut - (int32_t)_cal.ac6) * (int32_t)_cal.ac5) >> 15;
int32_t x2 = ((int32_t)_cal.mc << 11) / (x1 + _cal.md);
//_t_fine = x1 + x2;
_t_fine = (_t_fine + x1 + x2 + 1) >> 1; // avg of last two samples
_t_fine = x1 + x2;
//_t_fine = (_t_fine + x1 + x2 + 1) >> 1; // avg of last two samples
return (float)((_t_fine + 8) >> 4) / 10.0f;
}

Expand Down
2 changes: 1 addition & 1 deletion lib/Espfc/src/Device/BusI2C.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ class BusI2C: public BusDevice

BusType getType() const override { return BUS_I2C; }

int begin(int sda, int scl, int speed)
int begin(int sda, int scl, uint32_t speed)
{
if(sda == -1 || scl == -1) return 0;

Expand Down
4 changes: 2 additions & 2 deletions lib/Espfc/src/Hardware.h
Original file line number Diff line number Diff line change
Expand Up @@ -71,9 +71,9 @@ class Hardware
_model.logger.info().log(F("SPI SETUP")).log(_model.config.pin[PIN_SPI_0_SCK]).log(_model.config.pin[PIN_SPI_0_MOSI]).log(_model.config.pin[PIN_SPI_0_MISO]).logln(spiResult);
#endif
#if defined(ESPFC_I2C_0)
int i2cResult = i2cBus.begin(_model.config.pin[PIN_I2C_0_SDA], _model.config.pin[PIN_I2C_0_SCL], _model.config.i2cSpeed * 1000);
int i2cResult = i2cBus.begin(_model.config.pin[PIN_I2C_0_SDA], _model.config.pin[PIN_I2C_0_SCL], _model.config.i2cSpeed * 1000ul);
i2cBus.onError = std::bind(&Hardware::onI2CError, this);
_model.logger.info().log(F("I2C SETUP")).log(_model.config.i2cSpeed).logln(i2cResult);
_model.logger.info().log(F("I2C SETUP")).log(_model.config.pin[PIN_I2C_0_SDA]).log(_model.config.pin[PIN_I2C_0_SCL]).log(_model.config.i2cSpeed).logln(i2cResult);
#endif
}

Expand Down
1 change: 1 addition & 0 deletions lib/Espfc/src/Model.h
Original file line number Diff line number Diff line change
Expand Up @@ -551,6 +551,7 @@ class Model
void logStorageResult()
{
#ifndef UNIT_TEST
logger.info().log(F("F_CPU")).logln(F_CPU);
switch(_storageResult)
{
case STORAGE_LOAD_SUCCESS: logger.info().logln(F("EEPROM loaded")); break;
Expand Down
7 changes: 3 additions & 4 deletions lib/Espfc/src/Target/TargetESP32.h
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@
#define ESPFC_I2C_0
#define ESPFC_I2C_0_SCL 22
#define ESPFC_I2C_0_SDA 21
#define ESPFC_I2C_0_SOFT
//#define ESPFC_I2C_0_SOFT

#define ESPFC_BUZZER
#define ESPFC_BUZZER_PIN 0
Expand Down Expand Up @@ -143,10 +143,9 @@ inline int targetSPIInit(T& dev, int8_t sck, int8_t mosi, int8_t miso, int8_t ss
}

template<typename T>
inline int targetI2CInit(T& dev, int8_t sda, int8_t scl, int speed)
inline int targetI2CInit(T& dev, int8_t sda, int8_t scl, uint32_t speed)
{
dev.setClock(speed);
dev.begin(sda, scl);
dev.begin(sda, scl, speed);
return 1;
}

Expand Down
5 changes: 2 additions & 3 deletions lib/Espfc/src/Target/TargetESP8266.h
Original file line number Diff line number Diff line change
Expand Up @@ -101,10 +101,9 @@ inline int targetSPIInit(T& dev, int8_t sck, int8_t mosi, int8_t miso, int8_t ss
}

template<typename T>
inline int targetI2CInit(T& dev, int8_t sda, int8_t scl, int8_t speed)
inline int targetI2CInit(T& dev, int8_t sda, int8_t scl, uint32_t speed)
{
dev.setClock(speed);
dev.begin(sda, scl);
dev.begin(sda, scl, speed);
return 1;
}

Expand Down
2 changes: 1 addition & 1 deletion lib/Espfc/src/Target/TargetRP2040.h
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,7 @@ inline int targetSPIInit(T& dev, int8_t sck, int8_t mosi, int8_t miso, int8_t ss
}

template<typename T>
inline int targetI2CInit(T& dev, int8_t sda, int8_t scl, int speed)
inline int targetI2CInit(T& dev, int8_t sda, int8_t scl, uint32_t speed)
{
if(!dev.setSCL(scl)) return -1;
if(!dev.setSDA(sda)) return -2;
Expand Down

0 comments on commit 909167f

Please sign in to comment.