From 1c5fa831bc81188d06d8baba8ec1bd9fb40caca4 Mon Sep 17 00:00:00 2001 From: Robin van der Gracht Date: Fri, 5 Jul 2024 19:21:31 +0200 Subject: [PATCH] DS4432: Fix integer overflow for vcore values of 4900 mv and higher Signed-off-by: Robin van der Gracht --- main/DS4432U.c | 6 +++--- main/DS4432U.h | 2 +- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/main/DS4432U.c b/main/DS4432U.c index bc60f7b7..a42f6b0b 100644 --- a/main/DS4432U.c +++ b/main/DS4432U.c @@ -49,13 +49,13 @@ bool DS4432U_test(void) uint8_t DS4432U_voltage_to_reg(uint32_t vout_mv, uint32_t vnom_mv, uint32_t ra_ohm, uint32_t rb_ohm, - int32_t ifs_na, uint32_t vfb_mv) + uint32_t ifs_na, uint32_t vfb_mv) { uint8_t reg; // Calculate current flowing though bottom resistor (Rb) in nA - int32_t irb_na = (vfb_mv * 1000 * 1000) / rb_ohm; + int32_t irb_na = ((uint64_t)vfb_mv * 1000 * 1000) / rb_ohm; // Calculate current required through top resistor (Ra) to achieve vout in nA - int32_t ira_na = ((vout_mv - vfb_mv) * 1000 * 1000) / ra_ohm; + int32_t ira_na = ((uint64_t)(vout_mv - vfb_mv) * 1000 * 1000) / ra_ohm; // Calculate the delta current the DAC needs to sink/source in nA uint32_t dac_na = abs(irb_na - ira_na); // Calculate required DAC steps to get dac_na (rounded) diff --git a/main/DS4432U.h b/main/DS4432U.h index 6afcba7b..b9460160 100644 --- a/main/DS4432U.h +++ b/main/DS4432U.h @@ -11,5 +11,5 @@ esp_err_t DS4432U_set_current_code(uint8_t output, uint8_t code); esp_err_t DS4432U_get_current_code(uint8_t output, uint8_t *code); uint8_t DS4432U_voltage_to_reg(uint32_t vout_mv, uint32_t vnom_mv, uint32_t ra_ohm, uint32_t rb_ohm, - int32_t ifs_na, uint32_t vfb_mv); + uint32_t ifs_na, uint32_t vfb_mv); #endif /* DS4432U_H_ */