Skip to content

Commit e8972a9

Browse files
Russ Webbercfriedt
authored andcommitted
drivers: sensors: as5600: change to raw reading
Changed to use the raw reading rather than the scaled value, as that's what the angle calculation is expecting. Added log messages that communicate magnet sense state. Signed-off-by: Russ Webber <[email protected]>
1 parent 9aecdd4 commit e8972a9

File tree

1 file changed

+43
-9
lines changed

1 file changed

+43
-9
lines changed

drivers/sensor/ams/ams_as5600/ams_as5600.c

Lines changed: 43 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -11,16 +11,25 @@
1111
#include <zephyr/sys/util.h>
1212
#include <zephyr/device.h>
1313
#include <zephyr/init.h>
14+
#include <zephyr/sys/byteorder.h>
1415
#include <zephyr/drivers/sensor.h>
1516
#include <zephyr/drivers/i2c.h>
1617
#include <zephyr/logging/log.h>
18+
1719
LOG_MODULE_REGISTER(ams_as5600, CONFIG_SENSOR_LOG_LEVEL);
1820

19-
#define AS5600_ANGLE_REGISTER_H 0x0E
21+
#define AS5600_ANGLE_REGISTER_H 0x0E
22+
#define AS5600_ANGLE_REGISTER_RAW_H 0x0C
23+
#define AS5600_STATUS_REGISTER 0x0B
2024
#define AS5600_FULL_ANGLE 360
2125
#define AS5600_PULSES_PER_REV 4096
2226
#define AS5600_MILLION_UNIT 1000000
2327

28+
#define AS5600_STATUS_MH_BIT (3) /* Magnet too strong */
29+
#define AS5600_STATUS_ML_BIT (4) /* Magnet too weak */
30+
#define AS5600_STATUS_MD_BIT (5) /* Magnet detected */
31+
32+
2433
struct as5600_dev_cfg {
2534
struct i2c_dt_spec i2c_port;
2635
};
@@ -35,18 +44,43 @@ static int as5600_fetch(const struct device *dev, enum sensor_channel chan)
3544
struct as5600_dev_data *dev_data = dev->data;
3645
const struct as5600_dev_cfg *dev_cfg = dev->config;
3746

38-
uint8_t read_data[2] = {0, 0};
39-
uint8_t angle_reg = AS5600_ANGLE_REGISTER_H;
47+
uint8_t status;
48+
int err = i2c_reg_read_byte_dt(&dev_cfg->i2c_port,
49+
AS5600_STATUS_REGISTER,
50+
&status);
51+
52+
if (err != 0) {
53+
LOG_ERR("Failed to read status register: %d", err);
54+
return err;
55+
}
56+
57+
/* Check if the magnet is present */
58+
if (!(status & BIT(AS5600_STATUS_MD_BIT))) {
59+
LOG_WRN("Magnet not detected.");
60+
return -ENODATA;
61+
}
62+
63+
/* Check if the magnet is too strong or too weak */
64+
if (status & BIT(AS5600_STATUS_MH_BIT)) {
65+
LOG_WRN("Magnet too strong.");
66+
return -ENODATA;
67+
}
68+
69+
if (status & BIT(AS5600_STATUS_ML_BIT)) {
70+
LOG_WRN("Magnet too weak.");
71+
return -ENODATA;
72+
}
73+
74+
uint8_t buffer[2] = {0, 0};
4075

41-
int err = i2c_write_read_dt(&dev_cfg->i2c_port,
42-
&angle_reg,
43-
1,
44-
&read_data,
45-
sizeof(read_data));
76+
err = i2c_burst_read_dt(&dev_cfg->i2c_port,
77+
AS5600_ANGLE_REGISTER_RAW_H,
78+
(uint8_t *) &buffer,
79+
sizeof(buffer));
4680

4781
/* invalid readings preserves the last good value */
4882
if (!err) {
49-
dev_data->position = ((uint16_t)read_data[0] << 8) | read_data[1];
83+
dev_data->position = sys_get_be16(buffer);
5084
}
5185

5286
return err;

0 commit comments

Comments
 (0)