|
|
@ -13,7 +13,7 @@ using namespace iflytop; |
|
|
|
return 0; \ |
|
|
|
} |
|
|
|
#ifdef HAL_I2C_MODULE_ENABLED
|
|
|
|
|
|
|
|
#define TAG "TMP117"
|
|
|
|
TMP117::TMP117(/* args */) {} |
|
|
|
TMP117::~TMP117() {} |
|
|
|
|
|
|
@ -30,9 +30,7 @@ bool TMP117::isOnline() { |
|
|
|
getTemperature(); |
|
|
|
return m_lastCallStatus == HAL_OK; |
|
|
|
} |
|
|
|
bool TMP117::isError() { |
|
|
|
return m_lastCallStatus != HAL_OK; |
|
|
|
} |
|
|
|
bool TMP117::isError() { return m_lastCallStatus != HAL_OK; } |
|
|
|
|
|
|
|
const char* TMP117::getLastCallStatusString() { |
|
|
|
switch (m_lastCallStatus) { |
|
|
@ -50,54 +48,79 @@ const char* TMP117::getLastCallStatusString() { |
|
|
|
} |
|
|
|
|
|
|
|
float TMP117::getTemperature() { |
|
|
|
uint8_t buf[3]; |
|
|
|
buf[0] = TemperatureRegister; |
|
|
|
|
|
|
|
IF_FAIL_RETURN0(HAL_I2C_Master_Transmit(m_i2c, (uint16_t)m_id, buf, 1, 10)); |
|
|
|
HAL_Delay(1); |
|
|
|
IF_FAIL_RETURN0(HAL_I2C_Master_Receive(m_i2c, (uint16_t)m_id, buf, 2, 10)); |
|
|
|
HAL_Delay(1); |
|
|
|
return ((((buf[0] << 8) | buf[1])) * 0.0078125); |
|
|
|
uint16_t ret = 0; |
|
|
|
readreg(TemperatureRegister, (uint16_t*)&ret); |
|
|
|
return ret * 0.0078125; |
|
|
|
} |
|
|
|
uint16_t TMP117::getConfiguration() { |
|
|
|
uint8_t buf[3]; |
|
|
|
buf[0] = ConfigurationRegister; |
|
|
|
|
|
|
|
IF_FAIL_RETURN0(HAL_I2C_Master_Transmit(m_i2c, (uint16_t)m_id, buf, 1, 10)); |
|
|
|
HAL_Delay(1); |
|
|
|
IF_FAIL_RETURN0(HAL_I2C_Master_Receive(m_i2c, (uint16_t)m_id, buf, 2, 10)); |
|
|
|
HAL_Delay(1); |
|
|
|
return ((buf[0] << 8) | buf[1]); |
|
|
|
} |
|
|
|
void TMP117::setConfiguration(uint16_t value) { |
|
|
|
static uint8_t buf[3]; |
|
|
|
buf[0] = ConfigurationRegister; |
|
|
|
buf[1] = (value >> 8) & 0x0f; |
|
|
|
buf[2] = value & 0x0f; |
|
|
|
|
|
|
|
IF_FAIL_RETURN_VOID(HAL_I2C_Master_Transmit(m_i2c, (uint16_t)m_id, buf, 2, 10)); |
|
|
|
HAL_Delay(1); |
|
|
|
uint16_t ret = 0; |
|
|
|
readreg(ConfigurationRegister, (uint16_t*)&ret); |
|
|
|
return ret; |
|
|
|
} |
|
|
|
void TMP117::setConfiguration(uint16_t value) { writereg(ConfigurationRegister, value); } |
|
|
|
void TMP117::setTemperatureOffset(uint16_t tempoffset) { //
|
|
|
|
setTemperatureOffset((tempoffset >> 8) & 0x0f, tempoffset & 0x0f); |
|
|
|
// setTemperatureOffset((tempoffset >> 8) & 0x0f, tempoffset & 0x0f);
|
|
|
|
writereg(Temperature_Offset, tempoffset); |
|
|
|
} |
|
|
|
void TMP117::setTemperatureOffset(uint8_t first, uint8_t second) { |
|
|
|
static uint8_t buf[3]; |
|
|
|
buf[0] = Temperature_Offset; |
|
|
|
buf[1] = first; |
|
|
|
buf[2] = second; |
|
|
|
|
|
|
|
IF_FAIL_RETURN_VOID(HAL_I2C_Master_Transmit(m_i2c, (uint16_t)m_id, buf, 2, 10)); |
|
|
|
HAL_Delay(1); |
|
|
|
} |
|
|
|
uint16_t TMP117::getTemperatureOffset() { |
|
|
|
static uint8_t buf[3]; |
|
|
|
buf[0] = Temperature_Offset; |
|
|
|
IF_FAIL_RETURN0(HAL_I2C_Master_Transmit(m_i2c, (uint16_t)m_id, buf, 1, 10)); |
|
|
|
uint16_t ret = 0; |
|
|
|
readreg(Temperature_Offset, (uint16_t*)&ret); |
|
|
|
return ret; |
|
|
|
} |
|
|
|
bool TMP117::writereg(uint8_t regoff, uint16_t data) { |
|
|
|
bool suc = false; |
|
|
|
for (int32_t i = 0; i < 3; i++) { |
|
|
|
suc = _writereg(regoff, data); |
|
|
|
if (suc) break; |
|
|
|
/**
|
|
|
|
* @brief |
|
|
|
* 代码测试发现,当读取温度多次之后,会出现通信一直失败的现象(每30毫秒读一次,读5s左右),重启I2C之后可以恢复正常。 |
|
|
|
* 目前还没有找到原因,暂时在代码中通过重置I2C来解决这个问题。 |
|
|
|
*/ |
|
|
|
ZLOGW(TAG, "writereg fail, reset i2c"); |
|
|
|
HAL_I2C_DeInit(m_i2c); |
|
|
|
HAL_I2C_Init(m_i2c); |
|
|
|
} |
|
|
|
HAL_Delay(1); |
|
|
|
IF_FAIL_RETURN0(HAL_I2C_Master_Receive(m_i2c, (uint16_t)m_id, buf, 2, 10)); |
|
|
|
return suc; |
|
|
|
} |
|
|
|
bool TMP117::readreg(uint8_t regoff, uint16_t* data) { |
|
|
|
bool suc = false; |
|
|
|
for (int32_t i = 0; i < 3; i++) { |
|
|
|
suc = _readreg(regoff, data); |
|
|
|
if (suc) break; |
|
|
|
/**
|
|
|
|
* @brief |
|
|
|
* 代码测试发现,当读取温度多次之后,会出现通信一直失败的现象(每30毫秒读一次,读5s左右),重启I2C之后可以恢复正常。 |
|
|
|
* 目前还没有找到原因,暂时在代码中通过重置I2C来解决这个问题。 |
|
|
|
*/ |
|
|
|
HAL_I2C_DeInit(m_i2c); |
|
|
|
HAL_I2C_Init(m_i2c); |
|
|
|
} |
|
|
|
HAL_Delay(1); |
|
|
|
return ((buf[0] << 8) | buf[1]); |
|
|
|
return suc; |
|
|
|
} |
|
|
|
|
|
|
|
bool TMP117::_writereg(uint8_t regoff, uint16_t data) { |
|
|
|
uint8_t buf[3] = {0}; |
|
|
|
buf[0] = regoff; |
|
|
|
buf[1] = (data >> 8) & 0x0f; |
|
|
|
buf[2] = data & 0x0f; |
|
|
|
m_lastCallStatus = HAL_I2C_Master_Transmit(m_i2c, (uint16_t)m_id, buf, 3, 10); |
|
|
|
if (m_lastCallStatus != HAL_OK) return false; |
|
|
|
return true; |
|
|
|
} |
|
|
|
bool TMP117::_readreg(uint8_t regoff, uint16_t* data) { |
|
|
|
uint8_t buf[3] = {0}; |
|
|
|
buf[0] = regoff; |
|
|
|
m_lastCallStatus = HAL_I2C_Master_Transmit(m_i2c, (uint16_t)m_id, buf, 1, 10); |
|
|
|
if (m_lastCallStatus != HAL_OK) return false; |
|
|
|
m_lastCallStatus = HAL_I2C_Master_Receive(m_i2c, (uint16_t)m_id, &buf[1], 2, 10); |
|
|
|
if (m_lastCallStatus != HAL_OK) return false; |
|
|
|
// return ((buf[1] << 8) | buf[2]);
|
|
|
|
*data = ((buf[1] << 8) | buf[2]); |
|
|
|
return true; |
|
|
|
} |
|
|
|
|
|
|
|
#endif
|