From 1580884bda819cabda8dd15459998ad5947d16a4 Mon Sep 17 00:00:00 2001 From: zhaohe Date: Mon, 21 Apr 2025 19:45:57 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BC=98=E5=8C=96=E6=B8=A9=E5=BA=A6=E8=AF=BB?= =?UTF-8?q?=E5=8F=96?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- sdk/components/sensors/tmp117/tmp117.cpp | 23 +++++++++++++---------- 1 file changed, 13 insertions(+), 10 deletions(-) diff --git a/sdk/components/sensors/tmp117/tmp117.cpp b/sdk/components/sensors/tmp117/tmp117.cpp index 0f89680..0afe420 100644 --- a/sdk/components/sensors/tmp117/tmp117.cpp +++ b/sdk/components/sensors/tmp117/tmp117.cpp @@ -23,14 +23,17 @@ void TMP117::initializate(I2C_HandleTypeDef* i2c, i2c_reset_cb_t i2cResetCb, ID_ m_lastCallStatus = HAL_OK; m_i2cResetCb = i2cResetCb; - setConfiguration(0x0220); - setTemperatureOffset(0x0000); + if (!m_isError) setConfiguration(0x0220); + if (!m_isError) setTemperatureOffset(0x0000); + if (!m_isError) getTemperature(); - m_isOnlineWhenPowerOn = isOnline(); + m_isOnlineWhenPowerOn = !m_isError; + + ZLOGI(TAG, "TMP117 %d initialized..., isonline:%d", m_id, m_isOnlineWhenPowerOn); } bool TMP117::isOnline() { getTemperature(); - return m_isError; + return !m_isError; } bool TMP117::isError() { return m_isError; } @@ -51,13 +54,11 @@ const char* TMP117::getLastCallStatusString() { float TMP117::getTemperature() { uint16_t ret = 0; bool suc = readreg(TemperatureRegister, (uint16_t*)&ret); - if (!suc) { - m_isError = true; + if (suc) { + return ret * 0.0078125; } else { - m_isError = false; + return 0; } - - return ret * 0.0078125; } uint16_t TMP117::getConfiguration() { uint16_t ret = 0; @@ -87,9 +88,10 @@ bool TMP117::writereg(uint8_t regoff, uint16_t data) { * 目前还没有找到原因,暂时在代码中通过重置I2C来解决这个问题。 */ if (m_i2cResetCb) m_i2cResetCb(); - ZLOGI(TAG, "TMP117 %d WRITE REG FAIL", m_id); + ZLOGI(TAG, "TMP117 %d WRITE %d REG FAIL", m_id, regoff); osDelay(100); } + m_isError = !suc; osDelay(1); return suc; } @@ -107,6 +109,7 @@ bool TMP117::readreg(uint8_t regoff, uint16_t* data) { ZLOGI(TAG, "TMP117 %d WRITE REG FAIL", m_id); osDelay(100); } + m_isError = !suc; osDelay(1); return suc; }