From 909167f31379854e19fd60985047ea1bc6acc2f8 Mon Sep 17 00:00:00 2001 From: rtlopez Date: Thu, 27 Jul 2023 11:49:01 +0200 Subject: [PATCH] I2C fixes --- lib/EspWire/src/EspWire.cpp | 3 ++- lib/EspWire/src/EspWire.h | 2 +- lib/Espfc/src/Device/BaroBMP085.h | 8 ++++---- lib/Espfc/src/Device/BusI2C.h | 2 +- lib/Espfc/src/Hardware.h | 4 ++-- lib/Espfc/src/Model.h | 1 + lib/Espfc/src/Target/TargetESP32.h | 7 +++---- lib/Espfc/src/Target/TargetESP8266.h | 5 ++--- lib/Espfc/src/Target/TargetRP2040.h | 2 +- 9 files changed, 17 insertions(+), 17 deletions(-) diff --git a/lib/EspWire/src/EspWire.cpp b/lib/EspWire/src/EspWire.cpp index ade321ec..0ef8317a 100644 --- a/lib/EspWire/src/EspWire.cpp +++ b/lib/EspWire/src/EspWire.cpp @@ -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(); } diff --git a/lib/EspWire/src/EspWire.h b/lib/EspWire/src/EspWire.h index aedd1d26..ef84d050 100644 --- a/lib/EspWire/src/EspWire.h +++ b/lib/EspWire/src/EspWire.h @@ -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); diff --git a/lib/Espfc/src/Device/BaroBMP085.h b/lib/Espfc/src/Device/BaroBMP085.h index ad0eb161..b228ecfb 100644 --- a/lib/Espfc/src/Device/BaroBMP085.h +++ b/lib/Espfc/src/Device/BaroBMP085.h @@ -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 @@ -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 { @@ -40,7 +40,7 @@ class BaroBMP085: public BaroDevice int16_t mb; int16_t mc; int16_t md; - }; + } __attribute__ ((__packed__)); int begin(BusDevice * bus) override { @@ -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; } diff --git a/lib/Espfc/src/Device/BusI2C.h b/lib/Espfc/src/Device/BusI2C.h index 27e2ef2f..31f32c10 100644 --- a/lib/Espfc/src/Device/BusI2C.h +++ b/lib/Espfc/src/Device/BusI2C.h @@ -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; diff --git a/lib/Espfc/src/Hardware.h b/lib/Espfc/src/Hardware.h index b471d052..6fc6a6a9 100644 --- a/lib/Espfc/src/Hardware.h +++ b/lib/Espfc/src/Hardware.h @@ -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 } diff --git a/lib/Espfc/src/Model.h b/lib/Espfc/src/Model.h index b961f50e..2eef78a7 100644 --- a/lib/Espfc/src/Model.h +++ b/lib/Espfc/src/Model.h @@ -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; diff --git a/lib/Espfc/src/Target/TargetESP32.h b/lib/Espfc/src/Target/TargetESP32.h index de3b7848..1dd9e205 100644 --- a/lib/Espfc/src/Target/TargetESP32.h +++ b/lib/Espfc/src/Target/TargetESP32.h @@ -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 @@ -143,10 +143,9 @@ inline int targetSPIInit(T& dev, int8_t sck, int8_t mosi, int8_t miso, int8_t ss } template -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; } diff --git a/lib/Espfc/src/Target/TargetESP8266.h b/lib/Espfc/src/Target/TargetESP8266.h index e95038e3..46ec1da4 100644 --- a/lib/Espfc/src/Target/TargetESP8266.h +++ b/lib/Espfc/src/Target/TargetESP8266.h @@ -101,10 +101,9 @@ inline int targetSPIInit(T& dev, int8_t sck, int8_t mosi, int8_t miso, int8_t ss } template -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; } diff --git a/lib/Espfc/src/Target/TargetRP2040.h b/lib/Espfc/src/Target/TargetRP2040.h index 7998d710..e6adc835 100644 --- a/lib/Espfc/src/Target/TargetRP2040.h +++ b/lib/Espfc/src/Target/TargetRP2040.h @@ -134,7 +134,7 @@ inline int targetSPIInit(T& dev, int8_t sck, int8_t mosi, int8_t miso, int8_t ss } template -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;