From 11d4987411b2504fa7dfeefefeecdff4f4061c63 Mon Sep 17 00:00:00 2001 From: rdon Date: Thu, 18 Sep 2025 19:21:46 +0900 Subject: [PATCH] fix: use int64 in ReadTemperature to avoid overflow --- src/machine/machine_rp2_adc.go | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/machine/machine_rp2_adc.go b/src/machine/machine_rp2_adc.go index d5f21b1aaf..e0d6a459a9 100644 --- a/src/machine/machine_rp2_adc.go +++ b/src/machine/machine_rp2_adc.go @@ -100,7 +100,8 @@ func ReadTemperature() (millicelsius int32) { rp.ADC.CS.SetBits(rp.ADC_CS_TS_EN) // T = 27 - (ADC_voltage - 0.706)/0.001721 - return (27000<<16 - (int32(thermChan.getVoltage())-706<<16)*581) >> 16 + // 1/0.001721 ≈ 581 + return int32(((int64(27000) << 16) - ((int64(thermChan.getVoltage()) - (int64(706) << 16)) * 581)) >> 16) } // waitForReady spins waiting for the ADC peripheral to become ready.