Skip to content

Commit 1ae83c7

Browse files
sensor: akm09918c: Fix conversion constant
The sensor driver uses the value 500 to convert bit counts to microgauss values, but it should actually be 1500. Edit the driver unit test to use the macro instead of a magic number. Signed-off-by: Tristan Honscheid <honscheid@google.com>
1 parent ab90285 commit 1ae83c7

File tree

2 files changed

+19
-7
lines changed

2 files changed

+19
-7
lines changed

drivers/sensor/akm09918c/akm09918c.h

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
#define ZEPHYR_DRIVERS_SENSOR_AKM09918C_AKM09918C_H_
88

99
#include <zephyr/device.h>
10+
#include <zephyr/drivers/i2c.h>
1011
#include <zephyr/drivers/sensor.h>
1112

1213
#include "akm09918c_reg.h"
@@ -15,7 +16,17 @@
1516
#define AKM09918C_MEASURE_TIME_US 9000
1617

1718
/* Conversion values */
18-
#define AKM09918C_MICRO_GAUSS_PER_BIT INT64_C(500)
19+
#define AKM09918C_MICRO_GAUSS_PER_BIT INT64_C(1500)
20+
21+
/* Maximum and minimum raw register values for magnetometer data per datasheet */
22+
#define AKM09918C_MAGN_MAX_DATA_REG (32752)
23+
#define AKM09918C_MAGN_MIN_DATA_REG (-32752)
24+
25+
/* Maximum and minimum magnetometer values in microgauss. +/-32752 is the maximum range of the
26+
* data registers (slightly less than the range of int16). This works out to +/- 49,128,000 uGs
27+
*/
28+
#define AKM09918C_MAGN_MAX_MICRO_GAUSS (AKM09918C_MAGN_MAX_DATA_REG * AKM09918C_MICRO_GAUSS_PER_BIT)
29+
#define AKM09918C_MAGN_MIN_MICRO_GAUSS (AKM09918C_MAGN_MIN_DATA_REG * AKM09918C_MICRO_GAUSS_PER_BIT)
1930

2031
struct akm09918c_data {
2132
int16_t x_sample;

tests/drivers/sensor/akm09918c/src/main.c

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
#include <zephyr/drivers/sensor.h>
1111
#include <zephyr/ztest.h>
1212

13+
#include "akm09918c.h"
1314
#include "akm09918c_emul.h"
1415
#include "akm09918c_reg.h"
1516

@@ -72,19 +73,19 @@ static void test_fetch_magnetic_field(const struct akm09918c_fixture *fixture,
7273

7374
/* Assert the data is within 0.000005 Gauss */
7475
actual_ugauss = values[0].val1 * INT64_C(1000000) + values[0].val2;
75-
expect_ugauss = magn_percent[0] * INT64_C(500);
76+
expect_ugauss = magn_percent[0] * AKM09918C_MICRO_GAUSS_PER_BIT;
7677
zassert_within(expect_ugauss, actual_ugauss, INT64_C(5),
7778
"(X) expected %" PRIi64 " micro-gauss, got %" PRIi64 " micro-gauss",
7879
expect_ugauss, actual_ugauss);
7980

8081
actual_ugauss = values[1].val1 * INT64_C(1000000) + values[1].val2;
81-
expect_ugauss = magn_percent[1] * INT64_C(500);
82+
expect_ugauss = magn_percent[1] * AKM09918C_MICRO_GAUSS_PER_BIT;
8283
zassert_within(expect_ugauss, actual_ugauss, INT64_C(5),
8384
"(Y) expected %" PRIi64 " micro-gauss, got %" PRIi64 " micro-gauss",
8485
expect_ugauss, actual_ugauss);
8586

8687
actual_ugauss = values[2].val1 * INT64_C(1000000) + values[2].val2;
87-
expect_ugauss = magn_percent[2] * INT64_C(500);
88+
expect_ugauss = magn_percent[2] * AKM09918C_MICRO_GAUSS_PER_BIT;
8889
zassert_within(expect_ugauss, actual_ugauss, INT64_C(5),
8990
"(Z) expected %" PRIi64 " micro-gauss, got %" PRIi64 " micro-gauss",
9091
expect_ugauss, actual_ugauss);
@@ -94,9 +95,9 @@ ZTEST_F(akm09918c, test_fetch_magn)
9495
{
9596
/* Use (0.25, -0.33..., 0.91) as the factors */
9697
const int16_t magn_percent[3] = {
97-
INT16_C(32752) / INT16_C(4),
98-
INT16_C(-32751) / INT16_C(3),
99-
(int16_t)(INT16_C(32752) * INT32_C(91) / INT32_C(100)),
98+
INT16_C(AKM09918C_MAGN_MAX_DATA_REG) / INT16_C(4),
99+
INT16_C(AKM09918C_MAGN_MIN_DATA_REG) / INT16_C(3),
100+
(int16_t)(INT16_C(AKM09918C_MAGN_MAX_DATA_REG) * INT32_C(91) / INT32_C(100)),
100101
};
101102

102103
test_fetch_magnetic_field(fixture, magn_percent);

0 commit comments

Comments
 (0)