Fix QMP6988

This commit is contained in:
Theo Arends 2024-06-12 10:50:29 +02:00
parent bd47d99ceb
commit 7bde0b89d1
2 changed files with 39 additions and 22 deletions

View File

@ -58,6 +58,10 @@ TwoWire& I2cGetWire(uint8_t bus = 0) {
}
}
/*-------------------------------------------------------------------------------------------*\
* Return code: 0 = Error, 1 = OK
\*-------------------------------------------------------------------------------------------*/
bool I2cValidRead(uint8_t addr, uint8_t reg, uint8_t size, uint8_t bus = 0) {
i2c_buffer = 0;
@ -75,51 +79,51 @@ bool I2cValidRead(uint8_t addr, uint8_t reg, uint8_t size, uint8_t bus = 0) {
for (uint32_t i = 0; i < size; i++) {
i2c_buffer = i2c_buffer << 8 | myWire.read(); // receive DATA
}
status = true;
status = true; // 1 = OK
}
}
retry--;
}
if (!retry) myWire.endTransmission();
return status;
return status; // 0 = Error, 1 = OK
}
bool I2cValidRead8(uint8_t *data, uint8_t addr, uint8_t reg, uint8_t bus = 0) {
bool status = I2cValidRead(addr, reg, 1, bus);
*data = (uint8_t)i2c_buffer;
return status;
return status; // 0 = Error, 1 = OK
}
bool I2cValidRead16(uint16_t *data, uint8_t addr, uint8_t reg, uint8_t bus = 0) {
bool status = I2cValidRead(addr, reg, 2, bus);
*data = (uint16_t)i2c_buffer;
return status;
return status; // 0 = Error, 1 = OK
}
bool I2cValidReadS16(int16_t *data, uint8_t addr, uint8_t reg, uint8_t bus = 0) {
bool status = I2cValidRead(addr, reg, 2, bus);
*data = (int16_t)i2c_buffer;
return status;
return status; // 0 = Error, 1 = OK
}
bool I2cValidRead16LE(uint16_t *data, uint8_t addr, uint8_t reg, uint8_t bus = 0) {
uint16_t ldata;
bool status = I2cValidRead16(&ldata, addr, reg, bus);
*data = (ldata >> 8) | (ldata << 8);
return status;
return status; // 0 = Error, 1 = OK
}
bool I2cValidReadS16_LE(int16_t *data, uint8_t addr, uint8_t reg, uint8_t bus = 0) {
uint16_t ldata;
bool status = I2cValidRead16LE(&ldata, addr, reg, bus);
*data = (int16_t)ldata;
return status;
return status; // 0 = Error, 1 = OK
}
bool I2cValidRead24(int32_t *data, uint8_t addr, uint8_t reg, uint8_t bus = 0) {
bool status = I2cValidRead(addr, reg, 3, bus);
*data = i2c_buffer;
return status;
return status; // 0 = Error, 1 = OK
}
uint8_t I2cRead8(uint8_t addr, uint8_t reg, uint8_t bus = 0) {
@ -166,17 +170,21 @@ bool I2cWrite(uint8_t addr, uint8_t reg, uint32_t val, uint8_t size, uint8_t bus
}
x--;
} while (myWire.endTransmission(true) != 0 && x != 0); // end transmission
return (x);
return (x); // 0 = Error, 1 = OK
}
bool I2cWrite8(uint8_t addr, uint8_t reg, uint32_t val, uint8_t bus = 0) {
return I2cWrite(addr, reg, val, 1, bus);
return I2cWrite(addr, reg, val, 1, bus); // 0 = Error, 1 = OK
}
bool I2cWrite16(uint8_t addr, uint8_t reg, uint32_t val, uint8_t bus = 0) {
return I2cWrite(addr, reg, val, 2, bus);
return I2cWrite(addr, reg, val, 2, bus); // 0 = Error, 1 = OK
}
/*-------------------------------------------------------------------------------------------*\
* Return code: 0 = OK, 1 = Error
\*-------------------------------------------------------------------------------------------*/
bool I2cReadBuffer(uint8_t addr, uint8_t reg, uint8_t *reg_data, uint16_t len, uint8_t bus = 0) {
TwoWire& myWire = I2cGetWire(bus);
if (&myWire == nullptr) { return true; } // No valid I2c bus
@ -185,18 +193,18 @@ bool I2cReadBuffer(uint8_t addr, uint8_t reg, uint8_t *reg_data, uint16_t len, u
myWire.write((uint8_t)reg);
myWire.endTransmission();
if (len != myWire.requestFrom((uint8_t)addr, (uint8_t)len)) {
return true; // Error
return true; // 1 = Error
}
while (len--) {
*reg_data = (uint8_t)myWire.read();
reg_data++;
}
return false; // OK
return false; // 0 = OK
}
int8_t I2cWriteBuffer(uint8_t addr, uint8_t reg, uint8_t *reg_data, uint16_t len, uint8_t bus = 0) {
TwoWire& myWire = I2cGetWire(bus);
if (&myWire == nullptr) { return 1; } // No valid I2c bus
if (&myWire == nullptr) { return 1; } // 1 = Error, No valid I2c bus
myWire.beginTransmission((uint8_t)addr);
myWire.write((uint8_t)reg);
@ -205,9 +213,11 @@ int8_t I2cWriteBuffer(uint8_t addr, uint8_t reg, uint8_t *reg_data, uint16_t len
reg_data++;
}
myWire.endTransmission();
return 0; // OK
return 0; // 0 = OK
}
/*-------------------------------------------------------------------------------------------*/
void I2cScan(uint8_t bus = 0) {
// Return error codes defined in twi.h and core_esp8266_si2c.c
// I2C_OK 0

View File

@ -125,11 +125,18 @@ qmp6988_data_t *Qmp6988 = nullptr;
/*********************************************************************************************/
bool QMP6988I2cReadBuffer(uint8_t reg, uint8_t *reg_data, uint16_t len) {
return I2cReadBuffer(Qmp6988->address, reg, reg_data, len, Qmp6988->bus);
bool ret = !I2cReadBuffer(Qmp6988->address, reg, reg_data, len, Qmp6988->bus);
// AddLog(LOG_LEVEL_DEBUG, PSTR("QMP: Rd %02X '%*_H', Rslt %d"), reg, len, reg_data, ret);
return ret;
}
uint8_t QMP6988I2cWrite(uint8_t reg, uint32_t val) {
uint8_t ret = I2cWrite8(Qmp6988->address, reg, val, Qmp6988->bus);
bool QMP6988I2cWrite(uint8_t reg, uint32_t val) {
bool ret = I2cWrite8(Qmp6988->address, reg, val, Qmp6988->bus);
// AddLog(LOG_LEVEL_DEBUG, PSTR("QMP: Wr %02X '%02X', Rslt %d"), reg, val, ret);
delay(20);
return ret;
}
@ -272,7 +279,7 @@ int QMP6988GetPressure02e(qmp6988_ik_data_t* ik, int dp, int16_t tx) {
void QMP6988Reset(void) {
uint8_t ret = QMP6988I2cWrite(QMP6988_RESET_REG, 0xe6);
if (0 == ret) {
AddLog(LOG_LEVEL_DEBUG, PSTR("QMP: Reset fail"));
QMP6988_LOG("reset fail!!! \r\n");
}
QMP6988I2cWrite(QMP6988_RESET_REG, 0x00);
}
@ -322,7 +329,7 @@ bool QMP6988ValidChip(void) {
uint8_t data;
QMP6988I2cReadBuffer(QMP6988_CHIP_ID_REG, &data, 1);
AddLog(LOG_LEVEL_DEBUG, PSTR("QMP: Chip Id 0x%02X"), data);
QMP6988_LOG("chip id=0x%x \r\n", data);
return (QMP6988_CHIP_ID == data);
}
@ -345,7 +352,7 @@ void QMP6988CalcPressureAndTemperature(void) {
int P_int = QMP6988GetPressure02e(&(Qmp6988->ik), P_raw, T_int);
Qmp6988->temperature = (float)T_int / 256.0f;
Qmp6988->pressure = (float)P_int / 16.0f;
Qmp6988->pressure = (float)P_int / 1600.0f;
}
void Qmp6988Detect(void) {