drivers: sensor: ltrf216a: fix overflow in conversion
The conversion of the raw sensor value overflows because only a 32 bit multiplication is executed. Fix the issue by promoting the raw sensor value to uint64_t before executing the multiplication. Analysis: The current implementation overflows for all raw values grater than 9544(14-bit). But according to the datasheet the sensor has a maximum resolution of 20-bit. So Multiplying that value with 450.000 would need at least 39 bit to avoid an overflow, hence do it using 64-bit arithmetic. Fixes CID 330657 Signed-off-by: Thomas Stranger <thomas.stranger@outlook.com>
This commit is contained in:
parent
25227aa4b7
commit
61f065230a
1 changed files with 1 additions and 1 deletions
|
@ -127,7 +127,7 @@ static int ltrf216a_channel_get(const struct device *dev, enum sensor_channel ch
|
|||
* 0.45 -> 45 / 100, multiplied by 1000000 for millilux
|
||||
* gain 3 (default), integration time 100ms=1
|
||||
*/
|
||||
uint64_t microlux = (greendata * 45000 * LTRF216A_WIN_FAC * 10) / (3 * 1);
|
||||
uint64_t microlux = ((uint64_t)greendata * 45000 * LTRF216A_WIN_FAC * 10) / (3 * 1);
|
||||
|
||||
val->val1 = microlux / 1000000;
|
||||
val->val2 = microlux % 1000000;
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue