drivers/analog/hx711.c: fix averaging on negative values

ptype of ((int32_t)0 * (uint32_t)0) is (unsigned). "i" counter was
declared unsigned to not make comparision with "unsigned char" in for loop.
This resulted in calculation in average to be implicitly casted to unsigned,
and when negative number was added to it, it turned into huge value.

Change type of "i" to signed, and just cast (unsigned char) to (int)
to fix this.

Signed-off-by: Michał Łyszczek <michal.lyszczek@bofc.pl>
This commit is contained in:
Michał Łyszczek 2024-03-02 17:23:33 +01:00 committed by Xiang Xiao
parent 3bd35487f0
commit 459b4434b1

View File

@ -648,7 +648,7 @@ static ssize_t hx711_read(FAR struct file *filep,
int ret;
int32_t value; /* 24bit value from hx711 will be stored here */
int32_t average;
unsigned i;
int i;
value = 0;
dev = filep->f_inode->i_private;
@ -671,7 +671,7 @@ static ssize_t hx711_read(FAR struct file *filep,
return ret;
}
for (i = 1; i <= dev->average; i++)
for (i = 1; i <= (int)dev->average; i++)
{
value = hx711_single_read(dev);
if (value == INT32_MIN)