Skip to content

Іnteger overflow in the ADC current sensor code #3293

@vanessaorgua

Description

@vanessaorgua

In the src/main/sensors/battery.c following code is available

int32_t currentSensorToCentiamps(uint16_t src)
{
int32_t microvolts = ((uint32_t)src * ADCVREF * 1000) / 0xFFF - (int32_t)batteryConfig()->current.offset * 1000;
return microvolts / batteryConfig()->current.scale; // current in 0.01A steps
}

range of src variable values - 0x0 to 0xFFF
ADCVREF eq 3300
let us assume src = 4095
calculation
0x00000FFF * 3300*1000 = 0x3 2577 A560 <- Іnteger overflow here

On my OMNIBUS F4 PRO maximum displayed current is 9,63 Amp , if more -1

maybe this is the best option
int32_t microvolts = ((uint32_t)src * 806UL) - (int32_t)batteryConfig()->current.offset * 1000;
then there will be no overflow, but there will be less precision

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions