From cb879ea97f1302543a8156f0cbda972f6a9b15b6 Mon Sep 17 00:00:00 2001 From: Robin Mueller Date: Tue, 13 Jun 2023 23:25:44 +0200 Subject: [PATCH 1/3] tmp sign bugfix --- mission/tcs/Tmp1075Handler.cpp | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/mission/tcs/Tmp1075Handler.cpp b/mission/tcs/Tmp1075Handler.cpp index df57aa8a..054b2118 100644 --- a/mission/tcs/Tmp1075Handler.cpp +++ b/mission/tcs/Tmp1075Handler.cpp @@ -86,8 +86,15 @@ ReturnValue_t Tmp1075Handler::scanForReply(const uint8_t *start, size_t remainin ReturnValue_t Tmp1075Handler::interpretDeviceReply(DeviceCommandId_t id, const uint8_t *packet) { switch (id) { case TMP1075::GET_TEMP: { - int16_t tempValueRaw = 0; - tempValueRaw = packet[0] << 4 | packet[1] >> 4; + // Ignore the sign bit, subtract it later when applicable. + int16_t tempValueRaw = static_cast((packet[0] << 8) | packet[1]) & 0x7fff; + // 12 bit value, so perform the shift for correct values. + tempValueRaw >>= 4; + if(((packet[0] >> 7) & 0b1) == 0b1) { + // Perform two's complement by subtracting the sign + tempValueRaw -= 0x800; + } + // 0.0625 is the sensor sensitivity. float tempValue = ((static_cast(tempValueRaw)) * 0.0625); #if OBSW_DEBUG_TMP1075 == 1 sif::info << "Tmp1075 with object id: 0x" << std::hex << getObjectId() -- 2.43.0 From 38305e723f0ad3486a1047a022373cfe1d2ab722 Mon Sep 17 00:00:00 2001 From: Robin Mueller Date: Tue, 13 Jun 2023 23:26:27 +0200 Subject: [PATCH 2/3] changelog --- CHANGELOG.md | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 1c450e1a..61434dc0 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -18,6 +18,13 @@ will consitute of a breaking change warranting a new major release: # [v4.0.0] to be released +# [v3.1.0] + +## Fixed + +- TMP1075 bugfix where negative temperatures could not be measured because of a two's-complement + conversion bug. + # [v3.0.0] 2023-06-11 - `eive-tmtc` version v4.0.0 -- 2.43.0 From 12bc9268f76e8b6f4dea94c8da74a80ca78351e4 Mon Sep 17 00:00:00 2001 From: Robin Mueller Date: Wed, 14 Jun 2023 18:51:06 +0200 Subject: [PATCH 3/3] perform proper sign extension --- mission/tcs/Tmp1075Handler.cpp | 12 ++++-------- 1 file changed, 4 insertions(+), 8 deletions(-) diff --git a/mission/tcs/Tmp1075Handler.cpp b/mission/tcs/Tmp1075Handler.cpp index 054b2118..30331e1f 100644 --- a/mission/tcs/Tmp1075Handler.cpp +++ b/mission/tcs/Tmp1075Handler.cpp @@ -86,14 +86,10 @@ ReturnValue_t Tmp1075Handler::scanForReply(const uint8_t *start, size_t remainin ReturnValue_t Tmp1075Handler::interpretDeviceReply(DeviceCommandId_t id, const uint8_t *packet) { switch (id) { case TMP1075::GET_TEMP: { - // Ignore the sign bit, subtract it later when applicable. - int16_t tempValueRaw = static_cast((packet[0] << 8) | packet[1]) & 0x7fff; - // 12 bit value, so perform the shift for correct values. - tempValueRaw >>= 4; - if(((packet[0] >> 7) & 0b1) == 0b1) { - // Perform two's complement by subtracting the sign - tempValueRaw -= 0x800; - } + // Convert 12 bit MSB first raw temperature to 16 bit first. + int16_t tempValueRaw = static_cast((packet[0] << 8) | packet[1]) >> 4; + // Sign extension to 16 bits: If the sign bit is set, fill up with ones on the left. + tempValueRaw = (packet[0] & 0x80) ? (tempValueRaw | 0xF000) : tempValueRaw; // 0.0625 is the sensor sensitivity. float tempValue = ((static_cast(tempValueRaw)) * 0.0625); #if OBSW_DEBUG_TMP1075 == 1 -- 2.43.0